mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] pipe: annotate lockless accesses to pipe->readers and pipe->writers
@ 2026-12-02 17:58 Jiayi Zhang
  0 siblings, 0 replies; only message in thread
From: Jiayi Zhang @ 2026-12-02 17:58 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner, Jan Kara
  Cc: linux-fsdevel, linux-kernel, syzbot+76819c64ceed92d01ea7, syzkaller-bugs

pipe_readable() and pipe_writable() read pipe->writers and
pipe->readers with READ_ONCE() while they wait without pipe->mutex,
and pipe_poll() and the wait in wait_for_dump_helpers() read them
without the mutex too. The counters are changed under the mutex with
plain stores, so KCSAN reports a race when the last writer closes the
pipe while a reader waits:

  BUG: KCSAN: data-race in anon_pipe_read / pipe_release

  read (marked) to 0xffff888101d06560 of 4 bytes by task 14831 on cpu 1:
   pipe_readable fs/pipe.c:265 [inline]
   anon_pipe_read+0x54f/0x9b0 fs/pipe.c:420
   new_sync_read fs/read_write.c:473 [inline]
   vfs_read+0x6ac/0x7d0 fs/read_write.c:554
   ksys_read+0xdc/0x1a0 fs/read_write.c:696

  write to 0xffff888101d06560 of 4 bytes by task 14845 on cpu 0:
   pipe_release+0xb6/0x1d0 fs/pipe.c:773
   __fput+0x292/0x660 fs/file_table.c:512
   fput_close_sync+0xd5/0x1d0 fs/file_table.c:618

The race has no functional effect: all changes are made under
pipe->mutex, pipe_release() wakes the waiters afterwards, and the wait
re-tests the condition. KCSAN reports the plain store only with
CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n, as on syzbot.

Change the counters with WRITE_ONCE() in pipe_release(), fifo_open()
and wait_for_dump_helpers(), and read them with READ_ONCE() where they
are read without the mutex: in pipe_poll() and in the wait condition
of wait_for_dump_helpers().

Fixes: 85190d15f4ea ("pipe: don't use 'pipe_wait() for basic pipe IO")
Reported-by: syzbot+76819c64ceed92d01ea7@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=76819c64ceed92d01ea7
Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>
---
The syzbot report is in syzbot's moderation queue and was not sent to
the lists; the Closes: link shows it.

Tested in QEMU with a test program in which, on one pipe, two threads
read one byte at a time, one thread writes one byte at a time, and the
main thread opens a further write end through /proc/self/fd and closes
it again in a loop (KCSAN report filter set to anon_pipe_read,
pipe_readable and pipe_release). Without the patch all 5 instances
reported this race within 6 minutes. With the patch none of the 5
instances reported it; each of them ran 1.5 to 11 minutes until KCSAN
reported a different race between the two readers: pipe_update_tail()
stores pipe->tail under the mutex with a plain store while
pipe_readable() in the other reader reads pipe->head_tail with
READ_ONCE().

pipe->head and pipe->tail follow the same pattern: more than 15 plain
stores in fs/pipe.c, fs/splice.c and fs/fuse/dev.c against
READ_ONCE(pipe->head_tail). This patch does not change them; I can
annotate them in a separate patch if that is wanted. pipe->w_counter,
read in the same pipe_poll() condition, is also left as it is.

AI tools assisted with the analysis, the code and the changelog.

 fs/coredump.c |   10 +++++-----
 fs/pipe.c     |   24 ++++++++++++++----------
 2 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/fs/coredump.c b/fs/coredump.c
index 82ef9d1..e052d17 100644
--- a/fs/coredump.c
+++ b/fs/coredump.c
@@ -597,8 +597,8 @@ static void wait_for_dump_helpers(struct file *file)
 	struct pipe_inode_info *pipe = file->private_data;
 
 	pipe_lock(pipe);
-	pipe->readers++;
-	pipe->writers--;
+	WRITE_ONCE(pipe->readers, pipe->readers + 1);
+	WRITE_ONCE(pipe->writers, pipe->writers - 1);
 	wake_up_interruptible_sync(&pipe->rd_wait);
 	kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
 	pipe_unlock(pipe);
@@ -607,11 +607,11 @@ static void wait_for_dump_helpers(struct file *file)
 	 * We actually want wait_event_freezable() but then we need
 	 * to clear TIF_SIGPENDING and improve dump_interrupted().
 	 */
-	wait_event_interruptible(pipe->rd_wait, pipe->readers == 1);
+	wait_event_interruptible(pipe->rd_wait, READ_ONCE(pipe->readers) == 1);
 
 	pipe_lock(pipe);
-	pipe->readers--;
-	pipe->writers++;
+	WRITE_ONCE(pipe->readers, pipe->readers - 1);
+	WRITE_ONCE(pipe->writers, pipe->writers + 1);
 	pipe_unlock(pipe);
 }
 
diff --git a/fs/pipe.c b/fs/pipe.c
index 4c11225..324ea39 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -728,7 +728,7 @@ pipe_poll(struct file *filp, poll_table *wait)
 	if (filp->f_mode & FMODE_READ) {
 		if (!pipe_empty(idx.head, idx.tail))
 			mask |= EPOLLIN | EPOLLRDNORM;
-		if (!pipe->writers && filp->f_pipe != pipe->w_counter)
+		if (!READ_ONCE(pipe->writers) && filp->f_pipe != pipe->w_counter)
 			mask |= EPOLLHUP;
 	}
 
@@ -739,7 +739,7 @@ pipe_poll(struct file *filp, poll_table *wait)
 		 * Most Unices do not set EPOLLERR for FIFOs but on Linux they
 		 * behave exactly like pipes for poll().
 		 */
-		if (!pipe->readers)
+		if (!READ_ONCE(pipe->readers))
 			mask |= EPOLLERR;
 	}
 
@@ -768,9 +768,9 @@ pipe_release(struct inode *inode, struct file *file)
 
 	mutex_lock(&pipe->mutex);
 	if (file->f_mode & FMODE_READ)
-		pipe->readers--;
+		WRITE_ONCE(pipe->readers, pipe->readers - 1);
 	if (file->f_mode & FMODE_WRITE)
-		pipe->writers--;
+		WRITE_ONCE(pipe->writers, pipe->writers - 1);
 
 	/* Was that the last reader or writer, but not the other side? */
 	if (!pipe->readers != !pipe->writers) {
@@ -1204,7 +1204,8 @@ static int fifo_open(struct inode *inode, struct file *filp)
 	 *  opened, even when there is no process writing the FIFO.
 	 */
 		pipe->r_counter++;
-		if (pipe->readers++ == 0)
+		WRITE_ONCE(pipe->readers, pipe->readers + 1);
+		if (pipe->readers == 1)
 			wake_up_partner(pipe);
 
 		if (!is_pipe && !pipe->writers) {
@@ -1230,7 +1231,8 @@ static int fifo_open(struct inode *inode, struct file *filp)
 			goto err;
 
 		pipe->w_counter++;
-		if (!pipe->writers++)
+		WRITE_ONCE(pipe->writers, pipe->writers + 1);
+		if (pipe->writers == 1)
 			wake_up_partner(pipe);
 
 		if (!is_pipe && !pipe->readers) {
@@ -1247,8 +1249,8 @@ static int fifo_open(struct inode *inode, struct file *filp)
 	 *  the process can at least talk to itself.
 	 */
 
-		pipe->readers++;
-		pipe->writers++;
+		WRITE_ONCE(pipe->readers, pipe->readers + 1);
+		WRITE_ONCE(pipe->writers, pipe->writers + 1);
 		pipe->r_counter++;
 		pipe->w_counter++;
 		if (pipe->readers == 1 || pipe->writers == 1)
@@ -1265,13 +1267,15 @@ static int fifo_open(struct inode *inode, struct file *filp)
 	return 0;
 
 err_rd:
-	if (!--pipe->readers)
+	WRITE_ONCE(pipe->readers, pipe->readers - 1);
+	if (!pipe->readers)
 		wake_up_interruptible(&pipe->wr_wait);
 	ret = -ERESTARTSYS;
 	goto err;
 
 err_wr:
-	if (!--pipe->writers)
+	WRITE_ONCE(pipe->writers, pipe->writers - 1);
+	if (!pipe->writers)
 		wake_up_interruptible_all(&pipe->rd_wait);
 	ret = -ERESTARTSYS;
 	goto err;
-- 
2.53.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-02 17:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 17:58 [PATCH] pipe: annotate lockless accesses to pipe->readers and pipe->writers Jiayi Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®