]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
list: add "list_del_init_careful()" to go with "list_empty_careful()"
authorLinus Torvalds <torvalds@linux-foundation.org>
Thu, 23 Jul 2020 19:33:41 +0000 (12:33 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 28 Jun 2023 08:18:35 +0000 (10:18 +0200)
[ Upstream commit c6fe44d96fc1536af5b11cd859686453d1b7bfd1 ]

That gives us ordering guarantees around the pair.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Stable-dep-of: 2192bba03d80 ("epoll: ep_autoremove_wake_function should use list_del_init_careful")
Signed-off-by: Sasha Levin <sashal@kernel.org>
include/linux/list.h
kernel/sched/wait.c
mm/filemap.c

index ce19c6b632a5905533c819bf34bc2acd4a8c1f58..231ff089f7d1cc7af43fce9d2aa9a1fae6b1fbff 100644 (file)
@@ -268,6 +268,24 @@ static inline int list_empty(const struct list_head *head)
        return READ_ONCE(head->next) == head;
 }
 
+/**
+ * list_del_init_careful - deletes entry from list and reinitialize it.
+ * @entry: the element to delete from the list.
+ *
+ * This is the same as list_del_init(), except designed to be used
+ * together with list_empty_careful() in a way to guarantee ordering
+ * of other memory operations.
+ *
+ * Any memory operations done before a list_del_init_careful() are
+ * guaranteed to be visible after a list_empty_careful() test.
+ */
+static inline void list_del_init_careful(struct list_head *entry)
+{
+       __list_del_entry(entry);
+       entry->prev = entry;
+       smp_store_release(&entry->next, entry);
+}
+
 /**
  * list_empty_careful - tests whether a list is empty and not being modified
  * @head: the list to test
@@ -283,7 +301,7 @@ static inline int list_empty(const struct list_head *head)
  */
 static inline int list_empty_careful(const struct list_head *head)
 {
-       struct list_head *next = head->next;
+       struct list_head *next = smp_load_acquire(&head->next);
        return (next == head) && (next == head->prev);
 }
 
index 7d668b31dbc6d4b985c63c045721c70b50600015..c76fe1d4d91e2d727b20c647a8c4135565cb9129 100644 (file)
@@ -384,7 +384,7 @@ int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, i
        int ret = default_wake_function(wq_entry, mode, sync, key);
 
        if (ret)
-               list_del_init(&wq_entry->entry);
+               list_del_init_careful(&wq_entry->entry);
 
        return ret;
 }
index 83b324420046be5417a0bcb93e7dbff8d1b1ccf0..a106d63e84679f5596dfe6054f3d1a87aba6f7e2 100644 (file)
@@ -1085,13 +1085,8 @@ static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync,
         * since after list_del_init(&wait->entry) the wait entry
         * might be de-allocated and the process might even have
         * exited.
-        *
-        * We _really_ should have a "list_del_init_careful()" to
-        * properly pair with the unlocked "list_empty_careful()"
-        * in finish_wait().
         */
-       smp_mb();
-       list_del_init(&wait->entry);
+       list_del_init_careful(&wait->entry);
        return ret;
 }