|
| 1 | +From: Kuniyuki Iwashima < [email protected]> |
| 2 | +Subject: list: fix a data-race around ep->rdllist |
| 3 | + |
| 4 | +ep_poll() first calls ep_events_available() with no lock held and checks |
| 5 | +if ep->rdllist is empty by list_empty_careful(), which reads |
| 6 | +rdllist->prev. Thus all accesses to it need some protection to avoid |
| 7 | +store/load-tearing. |
| 8 | + |
| 9 | +Note INIT_LIST_HEAD_RCU() already has the annotation for both prev |
| 10 | +and next. |
| 11 | + |
| 12 | +Commit bf3b9f6372c4 ("epoll: Add busy poll support to epoll with socket |
| 13 | +fds.") added the first lockless ep_events_available(), and commit |
| 14 | +c5a282e9635e ("fs/epoll: reduce the scope of wq lock in epoll_wait()") |
| 15 | +made some ep_events_available() calls lockless and added single call under |
| 16 | +a lock, finally commit e59d3c64cba6 ("epoll: eliminate unnecessary lock |
| 17 | +for zero timeout") made the last ep_events_available() lockless. |
| 18 | + |
| 19 | +BUG: KCSAN: data-race in do_epoll_wait / do_epoll_wait |
| 20 | + |
| 21 | +write to 0xffff88810480c7d8 of 8 bytes by task 1802 on cpu 0: |
| 22 | + INIT_LIST_HEAD include/linux/list.h:38 [inline] |
| 23 | + list_splice_init include/linux/list.h:492 [inline] |
| 24 | + ep_start_scan fs/eventpoll.c:622 [inline] |
| 25 | + ep_send_events fs/eventpoll.c:1656 [inline] |
| 26 | + ep_poll fs/eventpoll.c:1806 [inline] |
| 27 | + do_epoll_wait+0x4eb/0xf40 fs/eventpoll.c:2234 |
| 28 | + do_epoll_pwait fs/eventpoll.c:2268 [inline] |
| 29 | + __do_sys_epoll_pwait fs/eventpoll.c:2281 [inline] |
| 30 | + __se_sys_epoll_pwait+0x12b/0x240 fs/eventpoll.c:2275 |
| 31 | + __x64_sys_epoll_pwait+0x74/0x80 fs/eventpoll.c:2275 |
| 32 | + do_syscall_x64 arch/x86/entry/common.c:50 [inline] |
| 33 | + do_syscall_64+0x44/0xd0 arch/x86/entry/common.c:80 |
| 34 | + entry_SYSCALL_64_after_hwframe+0x44/0xae |
| 35 | + |
| 36 | +read to 0xffff88810480c7d8 of 8 bytes by task 1799 on cpu 1: |
| 37 | + list_empty_careful include/linux/list.h:329 [inline] |
| 38 | + ep_events_available fs/eventpoll.c:381 [inline] |
| 39 | + ep_poll fs/eventpoll.c:1797 [inline] |
| 40 | + do_epoll_wait+0x279/0xf40 fs/eventpoll.c:2234 |
| 41 | + do_epoll_pwait fs/eventpoll.c:2268 [inline] |
| 42 | + __do_sys_epoll_pwait fs/eventpoll.c:2281 [inline] |
| 43 | + __se_sys_epoll_pwait+0x12b/0x240 fs/eventpoll.c:2275 |
| 44 | + __x64_sys_epoll_pwait+0x74/0x80 fs/eventpoll.c:2275 |
| 45 | + do_syscall_x64 arch/x86/entry/common.c:50 [inline] |
| 46 | + do_syscall_64+0x44/0xd0 arch/x86/entry/common.c:80 |
| 47 | + entry_SYSCALL_64_after_hwframe+0x44/0xae |
| 48 | + |
| 49 | +value changed: 0xffff88810480c7d0 -> 0xffff888103c15098 |
| 50 | + |
| 51 | +Reported by Kernel Concurrency Sanitizer on: |
| 52 | +CPU: 1 PID: 1799 Comm: syz-fuzzer Tainted: G W 5.17.0-rc7-syzkaller-dirty #0 |
| 53 | +Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 |
| 54 | + |
| 55 | +Link: https://lkml.kernel.org/r/ [email protected] |
| 56 | +Fixes: e59d3c64cba6 ("epoll: eliminate unnecessary lock for zero timeout") |
| 57 | +Fixes: c5a282e9635e ("fs/epoll: reduce the scope of wq lock in epoll_wait()") |
| 58 | +Fixes: bf3b9f6372c4 ("epoll: Add busy poll support to epoll with socket fds.") |
| 59 | +Signed-off-by: Kuniyuki Iwashima < [email protected]> |
| 60 | + |
| 61 | + |
| 62 | +Cc: Kuniyuki Iwashima < [email protected]> |
| 63 | +Cc: Kuniyuki Iwashima < [email protected]> |
| 64 | +Cc: "Soheil Hassas Yeganeh" < [email protected]> |
| 65 | +Cc: Davidlohr Bueso < [email protected]> |
| 66 | +Cc: "Sridhar Samudrala" < [email protected]> |
| 67 | +Cc: Alexander Duyck < [email protected]> |
| 68 | +Signed-off-by: Andrew Morton < [email protected]> |
| 69 | +--- |
| 70 | + |
| 71 | + include/linux/list.h | 6 +++--- |
| 72 | + 1 file changed, 3 insertions(+), 3 deletions(-) |
| 73 | + |
| 74 | +--- a/include/linux/list.h~list-fix-a-data-race-around-ep-rdllist |
| 75 | ++++ a/include/linux/list.h |
| 76 | +@@ -35,7 +35,7 @@ |
| 77 | + static inline void INIT_LIST_HEAD(struct list_head *list) |
| 78 | + { |
| 79 | + WRITE_ONCE(list->next, list); |
| 80 | +- list->prev = list; |
| 81 | ++ WRITE_ONCE(list->prev, list); |
| 82 | + } |
| 83 | + |
| 84 | + #ifdef CONFIG_DEBUG_LIST |
| 85 | +@@ -306,7 +306,7 @@ static inline int list_empty(const struc |
| 86 | + static inline void list_del_init_careful(struct list_head *entry) |
| 87 | + { |
| 88 | + __list_del_entry(entry); |
| 89 | +- entry->prev = entry; |
| 90 | ++ WRITE_ONCE(entry->prev, entry); |
| 91 | + smp_store_release(&entry->next, entry); |
| 92 | + } |
| 93 | + |
| 94 | +@@ -326,7 +326,7 @@ static inline void list_del_init_careful |
| 95 | + static inline int list_empty_careful(const struct list_head *head) |
| 96 | + { |
| 97 | + struct list_head *next = smp_load_acquire(&head->next); |
| 98 | +- return list_is_head(next, head) && (next == head->prev); |
| 99 | ++ return list_is_head(next, head) && (next == READ_ONCE(head->prev)); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | +_ |
0 commit comments