From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.229.9 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796234321; b=L74sAerMbR9F1wHnhmgbD/VNU0PENHM1oaI7Tjqytcgmrvtk4HtVyyFJTDSZZzC5zwJm p2QzbW5nubRbsgqoSe+Ht/QQJv9dAmD+F8QctgJ7cVZzbv/MDy/UJ/HHTnCZocLYj7ivd U8iRJ+byBboEnFynAevOjl41yno7okiDQiQdHwBstpTTcrqh3LdJa3romhRZDLiIXYXBb Nx4OAM+bOySegW2dv4sPjS96K8nXds5nwDupZ41kdrLbRKZ8/G2kBFkkKlU8dcx68V2Za WgqqUAR1KFhI405YnFUpn3lUGS1JpUUoKDyOt2BQHQ8eGTj6GGvRvDXzbNhAaMzmCaQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796234321; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=+wLjR62LLNHqpGAbed+Cv6yBIvqW66IDWscFj/SKqp8=; b=YM86/rTJC/0xXye7lkF7J2bTWjzloARI84mXITMY9JYFfhzS8/yZwUMA3hhpK0s7yJR1 J9N1NqDdhjXjrzCk16BGVhdizRDdPBTSl5mOXWlZQkv/irxB80NtTIC40qJd78Mt+h+0+ bLkhb12WbbmfDf92abEE0A1+18AWdX2Xd/h2iU7t5ncKI6toRfRykTqDG5tzx9DG9aBXM AdZFPE6TUI4A1qc8mBCZkamHgZBOgU/oRp+XEBlNw44EiXqq8UJEwZhcXXk31B12v20xZ zUWK/y8RchZzLBczcf/TSlo+026L6IydHqNgYCQ5QOR4oZLe1LQLz+D+T5c2tit6gyw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=VAnuJrCm; arc=none smtp.remote-ip=74.125.229.9 Received: from mail-dy2-f9.google.com (mail-dy2-f9.google.com [74.125.229.9]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 997A7606A681 for ; Wed, 2 Dec 2026 17:58:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 997A7606A681 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=VAnuJrCm Received: by mail-dy2-f9.google.com with SMTP id 5a478bee46e88-377157fde7bso4031642eec.0 for ; Wed, 02 Dec 2026 09:58:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796234320; x=1796839120; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=+wLjR62LLNHqpGAbed+Cv6yBIvqW66IDWscFj/SKqp8=; b=VAnuJrCmz/Tx6IGz2MDkejEKJA4iR/yCL++qSZ6DoVwKljXkJ1foFfm2yEC3MIbgx/ YWz850FmIFOyrXCk4IeBsRmjvKZTuzx0W9WrSNhigiUiOvt/8XYIy5Dhb68etVFuSAhB j7bxUKGC1r6bNPCDdFIBcXDsb85Aw87Ghc7BkJbhX60vhgWHhVZWsAo2SL5ZjJSvo2ko gF6LtedDKjMFhjrSbmX2HDMbhf7KEYQL5yawu1B7+kQLmnL008pcKEuLhCggVv+JH34W Ul+8q4Q+EdhOYzqoC1x7BWAm6viMQwHbW5WfbF5EDxZxNXGoHgiG7fG69tsJmBnInIfx HCDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796234320; x=1796839120; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+wLjR62LLNHqpGAbed+Cv6yBIvqW66IDWscFj/SKqp8=; b=JaW2u9OhiwAuaZWPcrPJpkLna2BAPrOEsZybwwdqoOwnzcYh/sJB3jnD9ngzRxY5wb j/bcI6+iDNB/xwjYM0qjU7TnaKpRu1PsoCcefWM7EdEgLcPCJKLpgzFVh2Us19MT5VXM SjDEk1aU25zukw4ODWgk/sO8llno1C4jRpyCO00217/t7u+UzgVm8E7JDA20CK4WEKFI f77rVXGjYCX8Oyj07QBCnNy9G4zRA2omimPe9ObixL7b1aXbzNA4uKhIS4Uwr5Y/zH0e crg+abNA23hz68hjRS09OtZjiODc2XqzO4OwZ28ZDqYBhQ16qwgUk5xPw08W4ZUIjsIo ogeA== X-Forwarded-Encrypted: i=1; AL+hErHPSwK0OMD4lc3xKtbJgQEYQNod5AQ3A9jEgertra/iD6Bgj+Zvo0+gkvEB+eLBnd55pkCfOJ/5wMzvzRA=@vger.kernel.org X-Gm-Message-State: APuU4Z92yOU1Yb/FEbZja2f40vexDO2Z2pkdtb29pBDYG7i9D3d6H+O4 eXpCGrcjzIt/ucPFNxyLsQyRjLpphUPBBs4DNG87MUaB4g2ZHGEsikzP X-Gm-Gg: AZFdTZd+TYotDhXRUbG6HpLIeXuOis2fRTlWXvZmiYzdrJCZTRJRY1/rUxCQcyg9JLo JEPVKDQA+YOrWRP4DHFJ4Yu8qkLsrpXG3GLiSXkiZSQFWy5oTnIuut43MMS/HMYU2+N7qLQevN6 bEYkwfKs43XZ4j0ORVi0c9qElIAvj2wJHX8CwpBpnqUl+oOlNXvSBrumhVA+cW4HsOgK4TdPhUk 0UJEMUMNHzWCMkSKtAHu4parJWdd6oemKPqLghxbrOBxpGf8QwH11zdyrH+/rxzIsJRxr4gZl80 1lbEK7+v/jcCEH9ook2aYNqPZ0Zq8CdviiNQac5xwvBF+vwKCX0KBtmBKYYMWmSfEqjYFmx8EVC 5GKlBVHDClTYm8IgXcnVmZgFUEbOdAviqBQXEaYg6I+5bzs9cz5iiio8r3VFn6oyVn5f+2C/2PW GG6YpaeAPL0YaWtUaf7fUoa45j/+SMmvusWkK365RVgbBReWMWsbI65NzYKoFZ+7QStjUzh4GBe egYUyKNvF/m/zLYucIrYUAQZm7e8iZWwGlaIN/baCS8Gqfo0SwwsY3Exy7rRmjnQJCmmxpoVIcq tgg8MnwQSsgAan8ER3vOLTE/rx/MiSELV+1N5PMq7BO/QEVnTekvuscUlQ== X-Received: by 2002:a05:7301:160c:b0:382:c1cf:163b with SMTP id 5a478bee46e88-38571419d38mr36998388eec.32.1796234319300; Wed, 02 Dec 2026 09:58:39 -0800 (PST) Received: from jiayi-vm-sprl-server17.local ([155.69.195.24]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385666e96bdsm112364420eec.7.2026.12.02.09.58.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 09:58:39 -0800 (PST) From: Jiayi Zhang To: Alexander Viro , Christian Brauner , Jan Kara Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+76819c64ceed92d01ea7@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com Subject: [PATCH] pipe: annotate lockless accesses to pipe->readers and pipe->writers Date: Wed, 2 Dec 2026 17:58:30 +0000 Message-ID: <20261202175830.4135657-1-zhangjiayi1014@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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