mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiayi Zhang <zhangjiayi1014@gmail.com>
To: Jan Kara <jack@suse.cz>, Amir Goldstein <amir73il@gmail.com>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	syzbot+f2d61c1b2357c03fb539@syzkaller.appspotmail.com,
	syzkaller-bugs@googlegroups.com
Subject: [PATCH] fsnotify: use WRITE_ONCE() when clearing the object mask on detach
Date: Wed,  2 Dec 2026 17:57:45 +0000	[thread overview]
Message-ID: <20261202175745.4134238-1-zhangjiayi1014@gmail.com> (raw)

fsnotify_detach_connector_from_object() clears the fsnotify mask of the
inode, mount, superblock or mount namespace with a plain store. The
mask is read without locks: __fsnotify_parent() and fsnotify() read it
with READ_ONCE(), and __fsnotify_recalc_mask() writes it with
WRITE_ONCE() for that reason. KCSAN reports the plain store:

  BUG: KCSAN: data-race in __fsnotify_parent / fsnotify_detach_connector_from_object

  write to 0xffff888101690808 of 4 bytes by task 3372 on cpu 1:
   fsnotify_detach_connector_from_object+0x1e3/0x370 fs/notify/mark.c:409
   fsnotify_put_mark+0x1d8/0x590 fs/notify/mark.c:476
   __do_sys_inotify_rm_watch fs/notify/inotify/inotify_user.c:796 [inline]

  read (marked) to 0xffff888101690808 of 4 bytes by task 3017 on cpu 0:
   fsnotify_object_watched fs/notify/fsnotify.c:152 [inline]
   __fsnotify_parent+0xfd/0x3a0 fs/notify/fsnotify.c:202

The race has no functional effect: the store writes a constant zero to
an aligned u32, and a reader that still sees the old mask goes on to
look up the marks as before. Commit 35ceae44742e ("fsnotify: Avoid
data race between fsnotify_recalc_mask() and fsnotify_object_watched()")
marked the readers and the store in __fsnotify_recalc_mask() but not
these four stores. Use WRITE_ONCE() for them too. KCSAN reports the plain store only with
CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n, as on syzbot.

Fixes: 35ceae44742e ("fsnotify: Avoid data race between fsnotify_recalc_mask() and fsnotify_object_watched()")
Reported-by: syzbot+f2d61c1b2357c03fb539@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=f2d61c1b2357c03fb539
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.

The syzbot reproducer only mounts an image; the race comes from
inotify users running on the syzbot machines. Tested in QEMU instead
with a small program that runs open() and inotify_add_watch()/
inotify_rm_watch() on one file in parallel threads, with KCSAN on and
unrelated races filtered: without the patch 1 of 5 instances reported
this race first; with the patch none of 10 did. Both runs also report
other races in fsnotify (for example fsnotify_free_mark() against
fsnotify_handle_inode_event(), and the plain read of the connector
pointer in fsnotify()), which this patch does not address.

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

 fs/notify/mark.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/notify/mark.c b/fs/notify/mark.c
index cb07f42..d24941b 100644
--- a/fs/notify/mark.c
+++ b/fs/notify/mark.c
@@ -406,18 +406,18 @@ static void *fsnotify_detach_connector_from_object(
 
 	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
 		inode = fsnotify_conn_inode(conn);
-		inode->i_fsnotify_mask = 0;
+		WRITE_ONCE(inode->i_fsnotify_mask, 0);
 		fsnotify_untrack_connector(conn);
 
 		/* Unpin inode when detaching from connector */
 		if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF))
 			inode = NULL;
 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
-		fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
+		WRITE_ONCE(fsnotify_conn_mount(conn)->mnt_fsnotify_mask, 0);
 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) {
-		fsnotify_conn_sb(conn)->s_fsnotify_mask = 0;
+		WRITE_ONCE(fsnotify_conn_sb(conn)->s_fsnotify_mask, 0);
 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_MNTNS) {
-		fsnotify_conn_mntns(conn)->n_fsnotify_mask = 0;
+		WRITE_ONCE(fsnotify_conn_mntns(conn)->n_fsnotify_mask, 0);
 	}
 
 	rcu_assign_pointer(*connp, NULL);
-- 
2.53.0


             reply	other threads:[~2026-12-02 17:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 17:57 Jiayi Zhang [this message]
2026-12-03 10:14 ` Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202175745.4134238-1-zhangjiayi1014@gmail.com \
    --to=zhangjiayi1014@gmail.com \
    --cc=amir73il@gmail.com \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=syzbot+f2d61c1b2357c03fb539@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®