[PATCH] fsnotify: use WRITE_ONCE() when clearing the object mask on detach

From: Jiayi Zhang

Date: Wed Dec 02 2026 - 12:58:00 EST


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@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=f2d61c1b2357c03fb539
Signed-off-by: Jiayi Zhang <zhangjiayi1014@xxxxxxxxx>
---
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