All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] Fix data-races around epoll reported by KCSAN.
@ 2022-03-22  0:26 Kuniyuki Iwashima
  2022-03-22  0:26 ` [PATCH 1/2] pipe: Make poll_usage boolean and annotate its access Kuniyuki Iwashima
  2022-03-22  0:26 ` [PATCH 2/2] list: Fix a data-race around ep->rdllist Kuniyuki Iwashima
  0 siblings, 2 replies; 4+ messages in thread
From: Kuniyuki Iwashima @ 2022-03-22  0:26 UTC (permalink / raw)
  To: Al Viro, Andrew Morton
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, linux-fsdevel, linux-kernel

This series suppresses a false positive KCSAN's message and fixes a real
data-race.

Kuniyuki Iwashima (2):
  pipe: Make poll_usage boolean and annotate its access.
  list: Fix a data-race around ep->rdllist.

 fs/pipe.c                 | 2 +-
 include/linux/list.h      | 6 +++---
 include/linux/pipe_fs_i.h | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

-- 
2.30.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] pipe: Make poll_usage boolean and annotate its access.
  2022-03-22  0:26 [PATCH 0/2] Fix data-races around epoll reported by KCSAN Kuniyuki Iwashima
@ 2022-03-22  0:26 ` Kuniyuki Iwashima
  2022-03-29 17:03   ` Marco Elver
  2022-03-22  0:26 ` [PATCH 2/2] list: Fix a data-race around ep->rdllist Kuniyuki Iwashima
  1 sibling, 1 reply; 4+ messages in thread
From: Kuniyuki Iwashima @ 2022-03-22  0:26 UTC (permalink / raw)
  To: Al Viro, Andrew Morton
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, linux-fsdevel,
	linux-kernel, Linus Torvalds

pipe_poll() runs locklessly and assigns 1 to poll_usage.  Once poll_usage
is set to 1, it never changes in other places.  However, concurrent writes
of a value trigger KCSAN, so let's make KCSAN happy.

BUG: KCSAN: data-race in pipe_poll / pipe_poll

write to 0xffff8880042f6678 of 4 bytes by task 174 on cpu 3:
 pipe_poll (fs/pipe.c:656)
 ep_item_poll.isra.0 (./include/linux/poll.h:88 fs/eventpoll.c:853)
 do_epoll_wait (fs/eventpoll.c:1692 fs/eventpoll.c:1806 fs/eventpoll.c:2234)
 __x64_sys_epoll_wait (fs/eventpoll.c:2246 fs/eventpoll.c:2241 fs/eventpoll.c:2241)
 do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80)
 entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:113)

write to 0xffff8880042f6678 of 4 bytes by task 177 on cpu 1:
 pipe_poll (fs/pipe.c:656)
 ep_item_poll.isra.0 (./include/linux/poll.h:88 fs/eventpoll.c:853)
 do_epoll_wait (fs/eventpoll.c:1692 fs/eventpoll.c:1806 fs/eventpoll.c:2234)
 __x64_sys_epoll_wait (fs/eventpoll.c:2246 fs/eventpoll.c:2241 fs/eventpoll.c:2241)
 do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80)
 entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:113)

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 177 Comm: epoll_race Not tainted 5.17.0-58927-gf443e374ae13 #6
Hardware name: Red Hat KVM, BIOS 1.11.0-2.amzn2 04/01/2014

Fixes: 3b844826b6c6 ("pipe: avoid unnecessary EPOLLET wakeups under normal loads")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
---
CC: Linus Torvalds <torvalds@linux-foundation.org>

Note that the message is false positive for now, so the Fixes tag might not
be necessary.
---
 fs/pipe.c                 | 2 +-
 include/linux/pipe_fs_i.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/pipe.c b/fs/pipe.c
index 9648ac151..e9f8290f8 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -653,7 +653,7 @@ pipe_poll(struct file *filp, poll_table *wait)
 	unsigned int head, tail;
 
 	/* Epoll has some historical nasty semantics, this enables them */
-	pipe->poll_usage = 1;
+	WRITE_ONCE(pipe->poll_usage, true);
 
 	/*
 	 * Reading pipe state only -- no need for acquiring the semaphore.
diff --git a/include/linux/pipe_fs_i.h b/include/linux/pipe_fs_i.h
index c00c618ef..cb0fd633a 100644
--- a/include/linux/pipe_fs_i.h
+++ b/include/linux/pipe_fs_i.h
@@ -71,7 +71,7 @@ struct pipe_inode_info {
 	unsigned int files;
 	unsigned int r_counter;
 	unsigned int w_counter;
-	unsigned int poll_usage;
+	bool poll_usage;
 	struct page *tmp_page;
 	struct fasync_struct *fasync_readers;
 	struct fasync_struct *fasync_writers;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] list: Fix a data-race around ep->rdllist.
  2022-03-22  0:26 [PATCH 0/2] Fix data-races around epoll reported by KCSAN Kuniyuki Iwashima
  2022-03-22  0:26 ` [PATCH 1/2] pipe: Make poll_usage boolean and annotate its access Kuniyuki Iwashima
@ 2022-03-22  0:26 ` Kuniyuki Iwashima
  1 sibling, 0 replies; 4+ messages in thread
From: Kuniyuki Iwashima @ 2022-03-22  0:26 UTC (permalink / raw)
  To: Al Viro, Andrew Morton
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, linux-fsdevel,
	linux-kernel, syzbot+bdd6e38a1ed5ee58d8bd, Soheil Hassas Yeganeh,
	Davidlohr Bueso, Sridhar Samudrala, Alexander Duyck

ep_poll() first calls ep_events_available() with no lock held and
checks if ep->rdllist is empty by list_empty_careful(), which reads
rdllist->prev.  Thus all accesses to it need some protection to avoid
store/load-tearing.

Note INIT_LIST_HEAD_RCU() already has the annotation for both prev
and next.

Commit bf3b9f6372c4 ("epoll: Add busy poll support to epoll with
socket fds.") added the first lockless ep_events_available(), and
commit c5a282e9635e ("fs/epoll: reduce the scope of wq lock in
epoll_wait()") made some ep_events_available() calls lockless and
added single call under a lock, finally commit e59d3c64cba6 ("epoll:
eliminate unnecessary lock for zero timeout") made the last
ep_events_available() lockless.

BUG: KCSAN: data-race in do_epoll_wait / do_epoll_wait

write to 0xffff88810480c7d8 of 8 bytes by task 1802 on cpu 0:
 INIT_LIST_HEAD include/linux/list.h:38 [inline]
 list_splice_init include/linux/list.h:492 [inline]
 ep_start_scan fs/eventpoll.c:622 [inline]
 ep_send_events fs/eventpoll.c:1656 [inline]
 ep_poll fs/eventpoll.c:1806 [inline]
 do_epoll_wait+0x4eb/0xf40 fs/eventpoll.c:2234
 do_epoll_pwait fs/eventpoll.c:2268 [inline]
 __do_sys_epoll_pwait fs/eventpoll.c:2281 [inline]
 __se_sys_epoll_pwait+0x12b/0x240 fs/eventpoll.c:2275
 __x64_sys_epoll_pwait+0x74/0x80 fs/eventpoll.c:2275
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x44/0xd0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x44/0xae

read to 0xffff88810480c7d8 of 8 bytes by task 1799 on cpu 1:
 list_empty_careful include/linux/list.h:329 [inline]
 ep_events_available fs/eventpoll.c:381 [inline]
 ep_poll fs/eventpoll.c:1797 [inline]
 do_epoll_wait+0x279/0xf40 fs/eventpoll.c:2234
 do_epoll_pwait fs/eventpoll.c:2268 [inline]
 __do_sys_epoll_pwait fs/eventpoll.c:2281 [inline]
 __se_sys_epoll_pwait+0x12b/0x240 fs/eventpoll.c:2275
 __x64_sys_epoll_pwait+0x74/0x80 fs/eventpoll.c:2275
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x44/0xd0 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x44/0xae

value changed: 0xffff88810480c7d0 -> 0xffff888103c15098

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 1799 Comm: syz-fuzzer Tainted: G        W         5.17.0-rc7-syzkaller-dirty #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011

Fixes: e59d3c64cba6 ("epoll: eliminate unnecessary lock for zero timeout")
Fixes: c5a282e9635e ("fs/epoll: reduce the scope of wq lock in epoll_wait()")
Fixes: bf3b9f6372c4 ("epoll: Add busy poll support to epoll with socket fds.")
Reported-by: syzbot+bdd6e38a1ed5ee58d8bd@syzkaller.appspotmail.com
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
---
CC: Soheil Hassas Yeganeh <soheil@google.com>
CC: Davidlohr Bueso <dave@stgolabs.net>
CC: Sridhar Samudrala <sridhar.samudrala@intel.com>
CC: Alexander Duyck <alexander.h.duyck@intel.com>
---
 include/linux/list.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/linux/list.h b/include/linux/list.h
index dd6c2041d..d7d2bfa1a 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -35,7 +35,7 @@
 static inline void INIT_LIST_HEAD(struct list_head *list)
 {
 	WRITE_ONCE(list->next, list);
-	list->prev = list;
+	WRITE_ONCE(list->prev, list);
 }
 
 #ifdef CONFIG_DEBUG_LIST
@@ -306,7 +306,7 @@ static inline int list_empty(const struct list_head *head)
 static inline void list_del_init_careful(struct list_head *entry)
 {
 	__list_del_entry(entry);
-	entry->prev = entry;
+	WRITE_ONCE(entry->prev, entry);
 	smp_store_release(&entry->next, entry);
 }
 
@@ -326,7 +326,7 @@ static inline void list_del_init_careful(struct list_head *entry)
 static inline int list_empty_careful(const struct list_head *head)
 {
 	struct list_head *next = smp_load_acquire(&head->next);
-	return list_is_head(next, head) && (next == head->prev);
+	return list_is_head(next, head) && (next == READ_ONCE(head->prev));
 }
 
 /**
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] pipe: Make poll_usage boolean and annotate its access.
  2022-03-22  0:26 ` [PATCH 1/2] pipe: Make poll_usage boolean and annotate its access Kuniyuki Iwashima
@ 2022-03-29 17:03   ` Marco Elver
  0 siblings, 0 replies; 4+ messages in thread
From: Marco Elver @ 2022-03-29 17:03 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: Al Viro, Andrew Morton, Kuniyuki Iwashima, linux-fsdevel,
	linux-kernel, Linus Torvalds

On Tue, Mar 22, 2022 at 09:26AM +0900, Kuniyuki Iwashima wrote:
> pipe_poll() runs locklessly and assigns 1 to poll_usage.  Once poll_usage
> is set to 1, it never changes in other places.  However, concurrent writes
> of a value trigger KCSAN, so let's make KCSAN happy.
> 
> BUG: KCSAN: data-race in pipe_poll / pipe_poll
> 
> write to 0xffff8880042f6678 of 4 bytes by task 174 on cpu 3:
>  pipe_poll (fs/pipe.c:656)
>  ep_item_poll.isra.0 (./include/linux/poll.h:88 fs/eventpoll.c:853)
>  do_epoll_wait (fs/eventpoll.c:1692 fs/eventpoll.c:1806 fs/eventpoll.c:2234)
>  __x64_sys_epoll_wait (fs/eventpoll.c:2246 fs/eventpoll.c:2241 fs/eventpoll.c:2241)
>  do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80)
>  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:113)
> 
> write to 0xffff8880042f6678 of 4 bytes by task 177 on cpu 1:
>  pipe_poll (fs/pipe.c:656)
>  ep_item_poll.isra.0 (./include/linux/poll.h:88 fs/eventpoll.c:853)
>  do_epoll_wait (fs/eventpoll.c:1692 fs/eventpoll.c:1806 fs/eventpoll.c:2234)
>  __x64_sys_epoll_wait (fs/eventpoll.c:2246 fs/eventpoll.c:2241 fs/eventpoll.c:2241)
>  do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80)
>  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:113)
> 
> Reported by Kernel Concurrency Sanitizer on:
> CPU: 1 PID: 177 Comm: epoll_race Not tainted 5.17.0-58927-gf443e374ae13 #6
> Hardware name: Red Hat KVM, BIOS 1.11.0-2.amzn2 04/01/2014
> 
> Fixes: 3b844826b6c6 ("pipe: avoid unnecessary EPOLLET wakeups under normal loads")
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
> ---
> CC: Linus Torvalds <torvalds@linux-foundation.org>
> 
> Note that the message is false positive for now, so the Fixes tag might not
> be necessary.
> ---
>  fs/pipe.c                 | 2 +-
>  include/linux/pipe_fs_i.h | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/pipe.c b/fs/pipe.c
> index 9648ac151..e9f8290f8 100644
> --- a/fs/pipe.c
> +++ b/fs/pipe.c
> @@ -653,7 +653,7 @@ pipe_poll(struct file *filp, poll_table *wait)
>  	unsigned int head, tail;
>  
>  	/* Epoll has some historical nasty semantics, this enables them */
> -	pipe->poll_usage = 1;
> +	WRITE_ONCE(pipe->poll_usage, true);

This reminds me of [1] (look for "idempotent write").

I'm not sure what KCSAN config you're using, but it looks like it's not
a default config (you seem to have KCSAN_REPORT_VALUE_CHANGE_ONLY off).
My guess is you can't see this data race with the default config, which
was a choice made from discussion in [1].

[1] https://lore.kernel.org/linux-fsdevel/CAHk-=wh_-1pj0vsAHiHf_FVardKkN7AZGX73QwGpViMyF7_mvQ@mail.gmail.com/T/

It's your choice to change the default of course, but if the report/fix
is picked up is anyone's guess (if you are not the code's maintainer).

Also see https://lwn.net/Articles/816854/

Thanks,

--Marco

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-03-29 17:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-22  0:26 [PATCH 0/2] Fix data-races around epoll reported by KCSAN Kuniyuki Iwashima
2022-03-22  0:26 ` [PATCH 1/2] pipe: Make poll_usage boolean and annotate its access Kuniyuki Iwashima
2022-03-29 17:03   ` Marco Elver
2022-03-22  0:26 ` [PATCH 2/2] list: Fix a data-race around ep->rdllist Kuniyuki Iwashima

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.