Skip to content

Commit c6fe44d

Browse files
committed
list: add "list_del_init_careful()" to go with "list_empty_careful()"
That gives us ordering guarantees around the pair. Signed-off-by: Linus Torvalds <[email protected]>
1 parent 2a9127f commit c6fe44d

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

include/linux/list.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,24 @@ static inline int list_empty(const struct list_head *head)
282282
return READ_ONCE(head->next) == head;
283283
}
284284

285+
/**
286+
* list_del_init_careful - deletes entry from list and reinitialize it.
287+
* @entry: the element to delete from the list.
288+
*
289+
* This is the same as list_del_init(), except designed to be used
290+
* together with list_empty_careful() in a way to guarantee ordering
291+
* of other memory operations.
292+
*
293+
* Any memory operations done before a list_del_init_careful() are
294+
* guaranteed to be visible after a list_empty_careful() test.
295+
*/
296+
static inline void list_del_init_careful(struct list_head *entry)
297+
{
298+
__list_del_entry(entry);
299+
entry->prev = entry;
300+
smp_store_release(&entry->next, entry);
301+
}
302+
285303
/**
286304
* list_empty_careful - tests whether a list is empty and not being modified
287305
* @head: the list to test
@@ -297,7 +315,7 @@ static inline int list_empty(const struct list_head *head)
297315
*/
298316
static inline int list_empty_careful(const struct list_head *head)
299317
{
300-
struct list_head *next = head->next;
318+
struct list_head *next = smp_load_acquire(&head->next);
301319
return (next == head) && (next == head->prev);
302320
}
303321

kernel/sched/wait.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, i
389389
int ret = default_wake_function(wq_entry, mode, sync, key);
390390

391391
if (ret)
392-
list_del_init(&wq_entry->entry);
392+
list_del_init_careful(&wq_entry->entry);
393393

394394
return ret;
395395
}

mm/filemap.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,13 +1041,8 @@ static int wake_page_function(wait_queue_entry_t *wait, unsigned mode, int sync,
10411041
* since after list_del_init(&wait->entry) the wait entry
10421042
* might be de-allocated and the process might even have
10431043
* exited.
1044-
*
1045-
* We _really_ should have a "list_del_init_careful()" to
1046-
* properly pair with the unlocked "list_empty_careful()"
1047-
* in finish_wait().
10481044
*/
1049-
smp_mb();
1050-
list_del_init(&wait->entry);
1045+
list_del_init_careful(&wait->entry);
10511046
return ret;
10521047
}
10531048

0 commit comments

Comments
 (0)