* [PATCH v2 1/3] inotify: do not expose a zero mask while replacing a watch
2026-12-06 8:06 [PATCH v2 0/3] fsnotify: fix races on mark mask, flags and ignore mask Seiji Nishikawa
@ 2026-12-06 8:15 ` Seiji Nishikawa
2026-12-06 8:15 ` [PATCH v2 2/3] fsnotify: clear the ignore mask under mark->lock Seiji Nishikawa
2026-12-06 8:15 ` [PATCH v2 3/3] fsnotify: annotate lockless accesses to mark masks and flags Seiji Nishikawa
2 siblings, 0 replies; 4+ messages in thread
From: Seiji Nishikawa @ 2026-12-06 8:15 UTC (permalink / raw)
To: Jan Kara
Cc: Amir Goldstein, linux-fsdevel, linux-kernel, Seiji Nishikawa,
syzbot+181b8b439f5207144b16
When inotify_add_watch() is called on an existing watch without
IN_MASK_ADD, inotify_update_existing_watch() first sets mark->mask to 0
and clears the inotify mark flags, and then sets the new bits. This is
done under mark->lock, but event delivery reads mask and flags without
that lock, in send_to_group(), fsnotify_iter_select_report_types(),
fsnotify_ignore_mask(), fsnotify_handle_inode_event() and
inotify_handle_inode_event().
So if an event comes in the middle of the update, a lockless reader of
mark->mask can see 0 and drop the event, even when both the old mask
and the new mask include the bit for that event. KCSAN reports this as
a data race:
==================================================================
BUG: KCSAN: data-race in __se_sys_inotify_add_watch / fsnotify_handle_inode_event
write to 0xffff88811afe9be0 of 4 bytes by task 3651 on cpu 0:
inotify_update_existing_watch fs/notify/inotify/inotify_user.c:558 [inline]
inotify_update_watch fs/notify/inotify/inotify_user.c:632 [inline]
__do_sys_inotify_add_watch fs/notify/inotify/inotify_user.c:769 [inline]
__se_sys_inotify_add_watch+0x2ea/0x720 fs/notify/inotify/inotify_user.c:717
__x64_sys_inotify_add_watch+0x43/0x50 fs/notify/inotify/inotify_user.c:717
x64_sys_call+0xa19/0x2550 arch/x86/include/generated/asm/syscalls_64.h:255
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x112/0x360 arch/x86/entry/syscall_64.c:84
entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff88811afe9be0 of 4 bytes by task 3653 on cpu 1:
fsnotify_handle_inode_event+0x18a/0x230 fs/notify/fsnotify.c:272
fsnotify_handle_event fs/notify/fsnotify.c:330 [inline]
send_to_group fs/notify/fsnotify.c:378 [inline]
fsnotify+0x16ae/0x1860 fs/notify/fsnotify.c:597
__fsnotify_parent+0x2f8/0x3a0 fs/notify/fsnotify.c:241
fsnotify_parent include/linux/fsnotify.h:96 [inline]
fsnotify_path include/linux/fsnotify.h:113 [inline]
fsnotify_file include/linux/fsnotify.h:127 [inline]
fsnotify_close include/linux/fsnotify.h:461 [inline]
__fput+0x1e2/0x630 fs/file_table.c:498
fput_close_sync+0xad/0x190 fs/file_table.c:617
__do_sys_close fs/open.c:1560 [inline]
__se_sys_close fs/open.c:1545 [inline]
__x64_sys_close+0x55/0xf0 fs/open.c:1545
x64_sys_call+0x812/0x2550 arch/x86/include/generated/asm/syscalls_64.h:4
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x112/0x360 arch/x86/entry/syscall_64.c:84
entry_SYSCALL_64_after_hwframe+0x77/0x7f
value changed: 0x00002018 -> 0x00000000
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 3653 Comm: syz-executor126 Not tainted syzkaller #0 PREEMPT(lazy)
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 07/24/2026
==================================================================
The old value 0x00002018 in the report is FS_UNMOUNT |
FS_CLOSE_NOWRITE | FS_CLOSE_WRITE. The syzbot reproducer has one thread
that calls inotify_add_watch() on a regular file in a loop, always with
IN_CLOSE_WRITE | IN_CLOSE_NOWRITE and without IN_MASK_ADD, and another
thread that opens and closes the same file. Calling inotify_add_watch()
again on a watched file with the same mask is allowed, and it returns
the same watch descriptor. But each such call still sets the mask to 0
first, as described above, so the mask changes from 0x2018 to 0 and
then back to 0x2018. The report shows the first thread (task 3651, in
inotify_add_watch()) setting the mask to 0 while the second thread
(task 3653, in close()) was reading it.
The same problem can happen to the IN_EXCL_UNLINK and IN_ONESHOT bits
in mark->flags.
To check that events are really dropped, the dropped events were counted
with bpftrace while the syzbot reproducer was running. The kernel is
v7.4-rc5 built with the syzbot config plus CONFIG_KPROBES,
CONFIG_KPROBE_EVENTS and CONFIG_FANOTIFY. The script counts the calls of
fsnotify_handle_inode_event() that return without calling
inotify_handle_inode_event(), that is, the events dropped there:
kprobe:fsnotify_handle_inode_event
/comm == "repro" || comm == "repro_add"/
{
@calls = count();
@pending[tid] = 1;
}
kprobe:inotify_handle_inode_event
/@pending[tid] == 1/
{
@passed = count();
@pending[tid] = 0;
}
kretprobe:fsnotify_handle_inode_event
/@pending[tid] == 1/
{
@dropped = count();
@pending[tid] = 0;
}
Without this patch, 2963 of 364184 events were dropped in 5 seconds with
the reproducer built as "repro":
@calls: 364184
@dropped: 2963
@passed: 361221
Without this patch, no events were dropped when IN_MASK_ADD was added to
the inotify_add_watch() call in the reproducer, built as "repro_add":
@calls: 446037
@dropped: 0
@passed: 446037
With this patch, no events were dropped with "repro", even without
IN_MASK_ADD:
@calls: 387785
@dropped: 0
@passed: 387785
Commit 38035c04f586 ("inotify: move control flags from mask to mark
flags") changed the call without IN_MASK_ADD to first clear the mask and
the inotify flags, and then OR in the new bits with the same code as the
IN_MASK_ADD case. The mask is cleared and set again under mark->lock,
but the lockless readers do not take that lock, so they can see the
zero mask after it is cleared and before the new bits are set, and
drop events.
Write the new mask and flags to the mark with one WRITE_ONCE() each, so
a lockless reader sees either the old value or the new value, never a
zero mask in between. Also use READ_ONCE() in the lockless readers on
the event delivery path, to pair with the WRITE_ONCE() and mark these
reads as intentionally lockless.
Fixes: 38035c04f586 ("inotify: move control flags from mask to mark flags")
Reported-by: syzbot+181b8b439f5207144b16@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=181b8b439f5207144b16
Cc: stable@vger.kernel.org
Signed-off-by: Seiji Nishikawa <snishika@redhat.com>
---
v2: test results redone on v7.4-rc5
fs/notify/fsnotify.c | 10 +++++-----
fs/notify/inotify/inotify_fsnotify.c | 2 +-
fs/notify/inotify/inotify_user.c | 17 +++++++++++++----
include/linux/fsnotify_backend.h | 4 ++--
4 files changed, 21 insertions(+), 12 deletions(-)
diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index 7e2f330fd..4687ba54d 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -264,12 +264,12 @@ static int fsnotify_handle_inode_event(struct fsnotify_group *group,
if (WARN_ON_ONCE(!inode && !dir))
return 0;
- if ((inode_mark->flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK) &&
+ if ((READ_ONCE(inode_mark->flags) & FSNOTIFY_MARK_FLAG_EXCL_UNLINK) &&
path && d_unlinked(path->dentry))
return 0;
/* Check interest of this mark in case event was sent with two marks */
- if (!(mask & inode_mark->mask & ALL_FSNOTIFY_EVENTS))
+ if (!(mask & READ_ONCE(inode_mark->mask) & ALL_FSNOTIFY_EVENTS))
return 0;
return ops->handle_inode_event(inode_mark, mask, inode, dir, name, cookie);
@@ -349,7 +349,7 @@ static int send_to_group(__u32 mask, const void *data, int data_type,
/* clear ignored on inode modification */
if (mask & FS_MODIFY) {
fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
- if (!(mark->flags &
+ if (!(READ_ONCE(mark->flags) &
FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
mark->ignore_mask = 0;
}
@@ -358,7 +358,7 @@ static int send_to_group(__u32 mask, const void *data, int data_type,
/* Are any of the group marks interested in this event? */
fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
group = mark->group;
- marks_mask |= mark->mask;
+ marks_mask |= READ_ONCE(mark->mask);
marks_ignore_mask |=
fsnotify_effective_ignore_mask(mark, is_dir, type);
}
@@ -439,7 +439,7 @@ static bool fsnotify_iter_select_report_types(
* But is *this mark* watching children?
*/
if (type == FSNOTIFY_ITER_TYPE_PARENT &&
- !(mark->mask & FS_EVENT_ON_CHILD) &&
+ !(READ_ONCE(mark->mask) & FS_EVENT_ON_CHILD) &&
!(fsnotify_ignore_mask(mark) & FS_EVENT_ON_CHILD))
continue;
diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c
index 7c326ec2e..da506ef03 100644
--- a/fs/notify/inotify/inotify_fsnotify.c
+++ b/fs/notify/inotify/inotify_fsnotify.c
@@ -129,7 +129,7 @@ int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, u32 mask,
fsnotify_destroy_event(group, fsn_event);
}
- if (inode_mark->flags & FSNOTIFY_MARK_FLAG_IN_ONESHOT)
+ if (READ_ONCE(inode_mark->flags) & FSNOTIFY_MARK_FLAG_IN_ONESHOT)
fsnotify_destroy_mark(inode_mark, group);
return 0;
diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c
index 5f19c24ec..98a13699e 100644
--- a/fs/notify/inotify/inotify_user.c
+++ b/fs/notify/inotify/inotify_user.c
@@ -554,12 +554,21 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
spin_lock(&fsn_mark->lock);
+ /*
+ * Event delivery reads mask and flags without mark->lock, so update
+ * each of them with a single store and never expose a partial value.
+ */
if (replace) {
- fsn_mark->mask = 0;
- fsn_mark->flags &= ~INOTIFY_MARK_FLAGS;
+ WRITE_ONCE(fsn_mark->mask, inotify_arg_to_mask(inode, arg));
+ WRITE_ONCE(fsn_mark->flags,
+ (fsn_mark->flags & ~INOTIFY_MARK_FLAGS) |
+ inotify_arg_to_flags(arg));
+ } else {
+ WRITE_ONCE(fsn_mark->mask,
+ fsn_mark->mask | inotify_arg_to_mask(inode, arg));
+ WRITE_ONCE(fsn_mark->flags,
+ fsn_mark->flags | inotify_arg_to_flags(arg));
}
- fsn_mark->mask |= inotify_arg_to_mask(inode, arg);
- fsn_mark->flags |= inotify_arg_to_flags(arg);
spin_unlock(&fsn_mark->lock);
fsnotify_recalc_mask(fsn_mark->connector);
diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h
index a6d5c1a89..5f0b94bfd 100644
--- a/include/linux/fsnotify_backend.h
+++ b/include/linux/fsnotify_backend.h
@@ -802,7 +802,7 @@ static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark)
__u32 ignore_mask = mark->ignore_mask;
/* The event flags in ignore mask take effect */
- if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
+ if (READ_ONCE(mark->flags) & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
return ignore_mask;
/*
@@ -812,7 +812,7 @@ static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark)
*/
ignore_mask |= FS_ISDIR;
ignore_mask &= ~FS_EVENT_ON_CHILD;
- ignore_mask |= mark->mask & FS_EVENT_ON_CHILD;
+ ignore_mask |= READ_ONCE(mark->mask) & FS_EVENT_ON_CHILD;
return ignore_mask;
}
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v2 2/3] fsnotify: clear the ignore mask under mark->lock
2026-12-06 8:06 [PATCH v2 0/3] fsnotify: fix races on mark mask, flags and ignore mask Seiji Nishikawa
2026-12-06 8:15 ` [PATCH v2 1/3] inotify: do not expose a zero mask while replacing a watch Seiji Nishikawa
@ 2026-12-06 8:15 ` Seiji Nishikawa
2026-12-06 8:15 ` [PATCH v2 3/3] fsnotify: annotate lockless accesses to mark masks and flags Seiji Nishikawa
2 siblings, 0 replies; 4+ messages in thread
From: Seiji Nishikawa @ 2026-12-06 8:15 UTC (permalink / raw)
To: Jan Kara; +Cc: Amir Goldstein, linux-fsdevel, linux-kernel, Seiji Nishikawa
When a file is modified, send_to_group() clears the ignore mask of each
mark that does not have FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY. It does
this without mark->lock. On the other hand, fanotify_mark_add_to_mask()
sets the ignore mask and then sets this flag, both under mark->lock.
A modify event can come between these two writes:
CPU 0 CPU 1
fanotify_mark_add_to_mask()
spin_lock(&mark->lock)
mark->ignore_mask |= mask
send_to_group() for FS_MODIFY
mark->flags does not have
FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY
mark->ignore_mask = 0
fanotify_mark_update_flags()
mark->flags |=
FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY
spin_unlock(&mark->lock)
After this, mark->flags has FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY but
mark->ignore_mask stays 0, so the events that the user asked to ignore
are delivered.
The clear of the ignore mask can also be lost when it races with an
update of the ignore mask in fanotify_mark_add_to_mask() or
fanotify_mark_remove_from_mask(). "mark->ignore_mask |= mask" reads the
ignore mask and then writes the new value, and send_to_group() can come
between the read and the write:
CPU 0 CPU 1
fanotify_mark_add_to_mask()
spin_lock(&mark->lock)
mark->ignore_mask |= mask
old = mark->ignore_mask
send_to_group() for FS_MODIFY
mark->flags does not have
FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY
mark->ignore_mask = 0
mark->ignore_mask = old | mask
spin_unlock(&mark->lock)
The old ignore mask that send_to_group() cleared is written back, so
the events that it ignored are still ignored after the file is modified.
"mark->ignore_mask &= ~mask" in fanotify_mark_remove_from_mask() has the
same problem.
This was reproduced on v7.4-rc5 with the syzbot reproducer of the bug
fixed by the previous patch, slightly changed with the assistance of an
LLM. One thread adds an ignore mask with FAN_MARK_IGNORED_SURV_MODIFY,
reads it back from fdinfo and removes the mark, in a loop. Another
thread writes to the same file. The program prints "lost: N of M", where
N is the number of loops where the ignore mask read back was 0, and M is
the number of all loops. Each run is 5 seconds, 3 runs:
lost: 2321 of 354297
lost: 1674 of 356127
lost: 1378 of 367778
So without this patch, in every run, the ignore mask that was just added
with FAN_MARK_IGNORED_SURV_MODIFY was already 0 in more than 1000 loops,
because a modify event cleared it.
With this patch:
lost: 0 of 339212
lost: 0 of 245721
lost: 0 of 363328
Clear the ignore mask under mark->lock, and check the flag again under
the lock. Take the lock only when the lockless checks show that there is
an ignore mask to clear, so the modify path does not take it for marks
without an ignore mask.
Fixes: e898386146de ("fsnotify: clear ignored mask on modify")
Reported-by: Jan Kara <jack@suse.cz>
Link: https://lore.kernel.org/all/pitzz3qt6efds6xqkowtaybzwh5tsd2arn3en263qqccs5wb3s@rrky5c2aggkq/
Signed-off-by: Seiji Nishikawa <snishika@redhat.com>
---
fs/notify/fsnotify.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index cc323f9e4..b082b4e34 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -349,9 +349,23 @@ static int send_to_group(__u32 mask, const void *data, int data_type,
/* clear ignored on inode modification */
if (mask & FS_MODIFY) {
fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
- if (!(READ_ONCE(mark->flags) &
+ /* The flag is never cleared once set */
+ if (READ_ONCE(mark->flags) &
+ FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
+ continue;
+ /* Most marks have no ignore mask to clear */
+ if (!READ_ONCE(mark->ignore_mask))
+ continue;
+ /*
+ * The ignore mask and the mark flags are updated
+ * under mark->lock, so check the flag again and clear
+ * the ignore mask under that lock.
+ */
+ spin_lock(&mark->lock);
+ if (!(mark->flags &
FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
- mark->ignore_mask = 0;
+ WRITE_ONCE(mark->ignore_mask, 0);
+ spin_unlock(&mark->lock);
}
}
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v2 3/3] fsnotify: annotate lockless accesses to mark masks and flags
2026-12-06 8:06 [PATCH v2 0/3] fsnotify: fix races on mark mask, flags and ignore mask Seiji Nishikawa
2026-12-06 8:15 ` [PATCH v2 1/3] inotify: do not expose a zero mask while replacing a watch Seiji Nishikawa
2026-12-06 8:15 ` [PATCH v2 2/3] fsnotify: clear the ignore mask under mark->lock Seiji Nishikawa
@ 2026-12-06 8:15 ` Seiji Nishikawa
2 siblings, 0 replies; 4+ messages in thread
From: Seiji Nishikawa @ 2026-12-06 8:15 UTC (permalink / raw)
To: Jan Kara; +Cc: Amir Goldstein, linux-fsdevel, linux-kernel, Seiji Nishikawa
mark->mask, mark->flags and mark->ignore_mask are written under
mark->lock, but they are also read without it. Add READ_ONCE() to these
reads and WRITE_ONCE() to the writes.
Also write the mask once in fsnotify_modify_mark_mask(), and read a
field once into a local variable where a function reads it more than
once without mark->lock.
Signed-off-by: Seiji Nishikawa <snishika@redhat.com>
---
fs/notify/dnotify/dnotify.c | 2 +-
fs/notify/fanotify/fanotify.c | 12 +++++++----
fs/notify/fanotify/fanotify.h | 7 ++++---
fs/notify/fanotify/fanotify_user.c | 32 +++++++++++++++++++-----------
fs/notify/fdinfo.c | 10 ++++++----
fs/notify/inotify/inotify.h | 7 ++++---
fs/notify/mark.c | 32 +++++++++++++++++-------------
include/linux/fsnotify_backend.h | 13 ++++++------
8 files changed, 68 insertions(+), 47 deletions(-)
diff --git a/fs/notify/dnotify/dnotify.c b/fs/notify/dnotify/dnotify.c
index 9fb73bafd..073cc7537 100644
--- a/fs/notify/dnotify/dnotify.c
+++ b/fs/notify/dnotify/dnotify.c
@@ -73,7 +73,7 @@ static void dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark)
new_mask |= (dn->dn_mask & ~FS_DN_MULTISHOT);
if (fsn_mark->mask == new_mask)
return;
- fsn_mark->mask = new_mask;
+ WRITE_ONCE(fsn_mark->mask, new_mask);
fsnotify_recalc_mask(fsn_mark->connector);
}
diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c
index 71b3a2361..5cff9423a 100644
--- a/fs/notify/fanotify/fanotify.c
+++ b/fs/notify/fanotify/fanotify.c
@@ -307,6 +307,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
struct fsnotify_mark *mark;
bool ondir = event_mask & FAN_ONDIR;
+ __u32 mark_mask;
int type;
pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
@@ -338,10 +339,11 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
/*
* Send the event depending on event flags in mark mask.
*/
- if (!fsnotify_mask_applicable(mark->mask, ondir, type))
+ mark_mask = READ_ONCE(mark->mask);
+ if (!fsnotify_mask_applicable(mark_mask, ondir, type))
continue;
- marks_mask |= mark->mask;
+ marks_mask |= mark_mask;
/* Record the mark types of this group that matched the event */
*match_mask |= 1U << type;
@@ -894,10 +896,12 @@ static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
__kernel_fsid_t fsid = {};
fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
- if (!(mark->flags & FSNOTIFY_MARK_FLAG_HAS_FSID))
+ unsigned int flags = READ_ONCE(mark->flags);
+
+ if (!(flags & FSNOTIFY_MARK_FLAG_HAS_FSID))
continue;
fsid = FANOTIFY_MARK(mark)->fsid;
- if (!(mark->flags & FSNOTIFY_MARK_FLAG_WEAK_FSID) &&
+ if (!(flags & FSNOTIFY_MARK_FLAG_WEAK_FSID) &&
WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
continue;
return fsid;
diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h
index 87ba78857..ad1764070 100644
--- a/fs/notify/fanotify/fanotify.h
+++ b/fs/notify/fanotify/fanotify.h
@@ -542,13 +542,14 @@ static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark)
{
+ unsigned int flags = READ_ONCE(mark->flags);
unsigned int mflags = 0;
- if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
+ if (flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
- if (mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)
+ if (flags & FSNOTIFY_MARK_FLAG_NO_IREF)
mflags |= FAN_MARK_EVICTABLE;
- if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
+ if (flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
mflags |= FAN_MARK_IGNORE;
return mflags;
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 63c9759fc..172761890 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -1229,9 +1229,10 @@ static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
spin_lock(&fsn_mark->lock);
oldmask = fsnotify_calc_mask(fsn_mark);
if (!(flags & FANOTIFY_MARK_IGNORE_BITS)) {
- fsn_mark->mask &= ~mask;
+ WRITE_ONCE(fsn_mark->mask, fsn_mark->mask & ~mask);
} else {
- fsn_mark->ignore_mask &= ~mask;
+ WRITE_ONCE(fsn_mark->ignore_mask,
+ fsn_mark->ignore_mask & ~mask);
}
newmask = fsnotify_calc_mask(fsn_mark);
/*
@@ -1290,7 +1291,8 @@ static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
* will result in EEXIST error.
*/
if (ignore == FAN_MARK_IGNORE)
- fsn_mark->flags |= FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS;
+ WRITE_ONCE(fsn_mark->flags, fsn_mark->flags |
+ FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS);
/*
* Setting FAN_MARK_IGNORED_SURV_MODIFY for the first time may lead to
@@ -1299,7 +1301,8 @@ static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
*/
if (ignore && (fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
!(fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) {
- fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
+ WRITE_ONCE(fsn_mark->flags, fsn_mark->flags |
+ FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY);
if (!(fsn_mark->mask & FS_MODIFY))
recalc = true;
}
@@ -1313,7 +1316,8 @@ static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
* When removed, fsnotify_recalc_mask() will take the inode ref.
*/
WARN_ON_ONCE(!want_iref);
- fsn_mark->flags &= ~FSNOTIFY_MARK_FLAG_NO_IREF;
+ WRITE_ONCE(fsn_mark->flags,
+ fsn_mark->flags & ~FSNOTIFY_MARK_FLAG_NO_IREF);
return true;
}
@@ -1327,10 +1331,11 @@ static bool fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
spin_lock(&fsn_mark->lock);
if (!(fan_flags & FANOTIFY_MARK_IGNORE_BITS)) {
old_mask = fsn_mark->mask;
- fsn_mark->mask |= mask;
+ WRITE_ONCE(fsn_mark->mask, old_mask | mask);
recalc = old_mask != fsn_mark->mask;
} else {
- fsn_mark->ignore_mask |= mask;
+ WRITE_ONCE(fsn_mark->ignore_mask,
+ fsn_mark->ignore_mask | mask);
recalc = true;
}
@@ -1378,7 +1383,8 @@ static int fanotify_set_mark_fsid(struct fsnotify_group *group,
return 0;
/* Do not allow mixing of marks with weak and strong fsid */
- if ((mark->flags ^ old->flags) & FSNOTIFY_MARK_FLAG_WEAK_FSID)
+ if ((mark->flags ^ READ_ONCE(old->flags)) &
+ FSNOTIFY_MARK_FLAG_WEAK_FSID)
return -EXDEV;
/* Allow mixing of marks with strong fsid from different fs */
@@ -1465,18 +1471,20 @@ static int fanotify_group_init_error_pool(struct fsnotify_group *group)
static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,
__u32 mask, unsigned int fan_flags)
{
+ unsigned int flags = READ_ONCE(fsn_mark->flags);
+
/*
* Non evictable mark cannot be downgraded to evictable mark.
*/
if (fan_flags & FAN_MARK_EVICTABLE &&
- !(fsn_mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
+ !(flags & FSNOTIFY_MARK_FLAG_NO_IREF))
return -EEXIST;
/*
* New ignore mask semantics cannot be downgraded to old semantics.
*/
if (fan_flags & FAN_MARK_IGNORED_MASK &&
- fsn_mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
+ flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
return -EEXIST;
/*
@@ -1487,11 +1495,11 @@ static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,
*/
if (fan_flags & FAN_MARK_IGNORE &&
!(fan_flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
- fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
+ flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
return -EEXIST;
/* For now pre-content events are not generated for directories */
- mask |= fsn_mark->mask;
+ mask |= READ_ONCE(fsn_mark->mask);
if (mask & FANOTIFY_PRE_CONTENT_EVENTS && mask & FAN_ONDIR)
return -EEXIST;
diff --git a/fs/notify/fdinfo.c b/fs/notify/fdinfo.c
index 0f731edde..63dd90e59 100644
--- a/fs/notify/fdinfo.c
+++ b/fs/notify/fdinfo.c
@@ -105,6 +105,8 @@ void inotify_show_fdinfo(struct seq_file *m, struct file *f)
static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
{
unsigned int mflags = fanotify_mark_user_flags(mark);
+ __u32 mask = READ_ONCE(mark->mask);
+ __u32 ignore_mask = READ_ONCE(mark->ignore_mask);
struct inode *inode;
if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
@@ -113,7 +115,7 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
return;
seq_printf(m, "fanotify ino:%llx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
inode->i_ino, inode->i_sb->s_dev,
- mflags, mark->mask, mark->ignore_mask);
+ mflags, mask, ignore_mask);
show_mark_fhandle(m, inode);
seq_putc(m, '\n');
iput(inode);
@@ -121,17 +123,17 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
struct mount *mnt = fsnotify_conn_mount(mark->connector);
seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
- mnt->mnt_id, mflags, mark->mask, mark->ignore_mask);
+ mnt->mnt_id, mflags, mask, ignore_mask);
} else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_SB) {
struct super_block *sb = fsnotify_conn_sb(mark->connector);
seq_printf(m, "fanotify sdev:%x mflags:%x mask:%x ignored_mask:%x\n",
- sb->s_dev, mflags, mark->mask, mark->ignore_mask);
+ sb->s_dev, mflags, mask, ignore_mask);
} else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_MNTNS) {
struct mnt_namespace *mnt_ns = fsnotify_conn_mntns(mark->connector);
seq_printf(m, "fanotify mnt_ns:%u mflags:%x mask:%x ignored_mask:%x\n",
- mnt_ns->ns.inum, mflags, mark->mask, mark->ignore_mask);
+ mnt_ns->ns.inum, mflags, mask, ignore_mask);
}
}
diff --git a/fs/notify/inotify/inotify.h b/fs/notify/inotify/inotify.h
index 7d5df7a21..992e223b6 100644
--- a/fs/notify/inotify/inotify.h
+++ b/fs/notify/inotify/inotify.h
@@ -31,11 +31,12 @@ static inline struct inotify_event_info *INOTIFY_E(struct fsnotify_event *fse)
static inline __u32 inotify_mark_user_mask(struct fsnotify_mark *fsn_mark)
{
- __u32 mask = fsn_mark->mask & INOTIFY_USER_MASK;
+ __u32 mask = READ_ONCE(fsn_mark->mask) & INOTIFY_USER_MASK;
+ unsigned int flags = READ_ONCE(fsn_mark->flags);
- if (fsn_mark->flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK)
+ if (flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK)
mask |= IN_EXCL_UNLINK;
- if (fsn_mark->flags & FSNOTIFY_MARK_FLAG_IN_ONESHOT)
+ if (flags & FSNOTIFY_MARK_FLAG_IN_ONESHOT)
mask |= IN_ONESHOT;
return mask;
diff --git a/fs/notify/mark.c b/fs/notify/mark.c
index cb07f42d3..c7c9c2903 100644
--- a/fs/notify/mark.c
+++ b/fs/notify/mark.c
@@ -254,11 +254,13 @@ static bool __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
if (!fsnotify_valid_obj_type(conn->type))
return NULL;
hlist_for_each_entry(mark, &conn->list, obj_list) {
- if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED))
+ unsigned int flags = READ_ONCE(mark->flags);
+
+ if (!(flags & FSNOTIFY_MARK_FLAG_ATTACHED))
continue;
new_mask |= fsnotify_calc_mask(mark);
if (conn->type == FSNOTIFY_OBJ_TYPE_INODE &&
- !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
+ !(flags & FSNOTIFY_MARK_FLAG_NO_IREF))
want_iref = true;
}
/*
@@ -360,11 +362,11 @@ void fsnotify_modify_mark_mask(struct fsnotify_mark *mark, u32 set, u32 clear)
WARN_ON_ONCE(clear & set);
spin_lock(&mark->lock);
- mask = mark->mask;
- mark->mask |= set;
- mark->mask &= ~clear;
- if (mark->mask != mask)
+ mask = (mark->mask | set) & ~clear;
+ if (mark->mask != mask) {
+ WRITE_ONCE(mark->mask, mask);
recalc = true;
+ }
spin_unlock(&mark->lock);
if (recalc)
@@ -613,7 +615,8 @@ void fsnotify_detach_mark(struct fsnotify_mark *mark)
fsnotify_group_assert_locked(mark->group);
WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) &&
refcount_read(&mark->refcnt) < 1 +
- !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED));
+ !!(READ_ONCE(mark->flags) &
+ FSNOTIFY_MARK_FLAG_ATTACHED));
spin_lock(&mark->lock);
/* something else already called this function on this mark */
@@ -621,7 +624,7 @@ void fsnotify_detach_mark(struct fsnotify_mark *mark)
spin_unlock(&mark->lock);
return;
}
- mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
+ WRITE_ONCE(mark->flags, mark->flags & ~FSNOTIFY_MARK_FLAG_ATTACHED);
list_del_init(&mark->g_list);
spin_unlock(&mark->lock);
@@ -647,7 +650,7 @@ void fsnotify_free_mark(struct fsnotify_mark *mark)
spin_unlock(&mark->lock);
return;
}
- mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
+ WRITE_ONCE(mark->flags, mark->flags & ~FSNOTIFY_MARK_FLAG_ALIVE);
spin_unlock(&mark->lock);
/*
@@ -945,7 +948,7 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark, void *obj,
last = lmark;
if ((lmark->group == mark->group) &&
- (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) &&
+ (READ_ONCE(lmark->flags) & FSNOTIFY_MARK_FLAG_ATTACHED) &&
!(mark->group->flags & FSNOTIFY_GROUP_DUPS)) {
err = -EEXIST;
goto out_err;
@@ -997,7 +1000,8 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
* mark->connector->lock
*/
spin_lock(&mark->lock);
- mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
+ WRITE_ONCE(mark->flags, mark->flags | FSNOTIFY_MARK_FLAG_ALIVE |
+ FSNOTIFY_MARK_FLAG_ATTACHED);
list_add(&mark->g_list, &group->marks_list);
fsnotify_get_mark(mark); /* for g_list */
@@ -1012,8 +1016,8 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
return ret;
err:
spin_lock(&mark->lock);
- mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
- FSNOTIFY_MARK_FLAG_ATTACHED);
+ WRITE_ONCE(mark->flags, mark->flags & ~(FSNOTIFY_MARK_FLAG_ALIVE |
+ FSNOTIFY_MARK_FLAG_ATTACHED));
list_del_init(&mark->g_list);
spin_unlock(&mark->lock);
@@ -1054,7 +1058,7 @@ struct fsnotify_mark *fsnotify_find_mark(void *obj, unsigned int obj_type,
hlist_for_each_entry(mark, &conn->list, obj_list) {
if (mark->group == group &&
- (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
+ (READ_ONCE(mark->flags) & FSNOTIFY_MARK_FLAG_ATTACHED)) {
fsnotify_get_mark(mark);
spin_unlock(&conn->lock);
return mark;
diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h
index 5f0b94bfd..58ef5ba6d 100644
--- a/include/linux/fsnotify_backend.h
+++ b/include/linux/fsnotify_backend.h
@@ -799,7 +799,7 @@ extern void fsnotify_remove_queued_event(struct fsnotify_group *group,
*/
static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark)
{
- __u32 ignore_mask = mark->ignore_mask;
+ __u32 ignore_mask = READ_ONCE(mark->ignore_mask);
/* The event flags in ignore mask take effect */
if (READ_ONCE(mark->flags) & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
@@ -820,7 +820,7 @@ static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark)
/* Legacy ignored_mask - only event types to ignore */
static inline __u32 fsnotify_ignored_events(struct fsnotify_mark *mark)
{
- return mark->ignore_mask & ALL_FSNOTIFY_EVENTS;
+ return READ_ONCE(mark->ignore_mask) & ALL_FSNOTIFY_EVENTS;
}
/*
@@ -868,20 +868,21 @@ static inline __u32 fsnotify_effective_ignore_mask(struct fsnotify_mark *mark,
/* Get mask for calculating object interest taking ignore mask into account */
static inline __u32 fsnotify_calc_mask(struct fsnotify_mark *mark)
{
- __u32 mask = mark->mask;
+ __u32 mask = READ_ONCE(mark->mask);
+ __u32 ignore_mask = READ_ONCE(mark->ignore_mask);
- if (!fsnotify_ignored_events(mark))
+ if (!(ignore_mask & ALL_FSNOTIFY_EVENTS))
return mask;
/* Interest in FS_MODIFY may be needed for clearing ignore mask */
- if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
+ if (!(READ_ONCE(mark->flags) & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
mask |= FS_MODIFY;
/*
* If mark is interested in ignoring events on children, the object must
* show interest in those events for fsnotify_parent() to notice it.
*/
- return mask | mark->ignore_mask;
+ return mask | ignore_mask;
}
/* Get mask of events for a list of marks */
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread