All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fsnotify: use rcu_access_pointer() to test the mark connector pointers
@ 2026-12-02 18:13 Jiayi Zhang
  2026-12-03  1:03 ` kernel test robot
  0 siblings, 1 reply; 2+ messages in thread
From: Jiayi Zhang @ 2026-12-02 18:13 UTC (permalink / raw)
  To: Jan Kara, Amir Goldstein
  Cc: Christian Brauner, Alexander Viro, Miklos Szeredi, linux-fsdevel,
	linux-kernel, syzbot+f968afcac117a84b7f14, syzkaller-bugs

fsnotify() skips objects without marks by testing sb_marks,
mnt_fsnotify_marks, i_fsnotify_marks and n_fsnotify_marks for NULL
before it takes SRCU, and fsnotify_mnt() and mnt_notify_add() do the
same for n_fsnotify_marks. These are __rcu pointers that
fsnotify_detach_connector_from_object() clears with
rcu_assign_pointer() when the last mark goes away, but the tests read
them with plain loads. KCSAN reports:

  BUG: KCSAN: data-race in fsnotify / fsnotify_detach_connector_from_object

  write (marked) to 0xffff88810024bbb8 of 8 bytes by task 4110 on cpu 1:
   fsnotify_detach_connector_from_object+0x319/0x370 fs/notify/mark.c:423
   fsnotify_put_mark+0x1d8/0x590 fs/notify/mark.c:476
   __do_sys_inotify_rm_watch fs/notify/inotify/inotify_user.c:796 [inline]
   ...

  read to 0xffff88810024bbb8 of 8 bytes by task 11515 on cpu 0:
   fsnotify+0x339/0x18b0 fs/notify/fsnotify.c:542
   __fsnotify_parent+0x2f8/0x3a0 fs/notify/fsnotify.c:241
   fsnotify_parent include/linux/fsnotify.h:96 [inline]
   ...

The race has no functional effect: the tests only compare the pointers
with NULL to skip the SRCU section, and a stale value only means one
event is or is not delivered to a mark that is being added or removed
at that moment. The plain loads still let the compiler tear or reload
them, and KCSAN reports them.

Use rcu_access_pointer(), the accessor for testing an __rcu pointer
without dereferencing it, for these tests.

Fixes: 08991e83b728 ("fsnotify: Free fsnotify_mark_connector when there is no mark attached")
Reported-by: syzbot+f968afcac117a84b7f14@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=f968afcac117a84b7f14
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. kernel/auditsc.c has the same
plain test of i_fsnotify_marks in two places; that change is sent to
the audit maintainers separately.

syzbot has no reproducer. Tested in QEMU (2 CPUs, the syzbot config,
kernel 8d7fe9885c62) with a test program in which threads open() and
close() one file while other threads add and remove an inotify watch
on it, with KCSAN switched on at run time and a report filter for the
functions on both sides. Without the patch all 5 instances reported a
race under this title within about 90 seconds, 4 of them this pair
(fsnotify.c:542 against mark.c:423). The same title also covers the
i_fsnotify_mask stores in fsnotify_detach_connector_from_object(),
which "fsnotify: use WRITE_ONCE() when clearing the object mask on
detach" (sent the same day) fixes; with this patch alone the instances
stopped at that pair after 98 to 159 seconds. With both patches none
of 5 instances reported anything in 10 minutes each.

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

 fs/mount.h           |    4 ++--
 fs/notify/fsnotify.c |   12 ++++++------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/fs/mount.h b/fs/mount.h
index 4d23cad..b4a019f 100644
--- a/fs/mount.h
+++ b/fs/mount.h
@@ -244,8 +244,8 @@ static inline void mnt_notify_add(struct mount *m)
 	if (!list_empty(&m->to_notify))
 		return;
 	/* Optimize the case where there are no watches */
-	if ((m->mnt_ns && m->mnt_ns->n_fsnotify_marks) ||
-	    (m->prev_ns && m->prev_ns->n_fsnotify_marks))
+	if ((m->mnt_ns && rcu_access_pointer(m->mnt_ns->n_fsnotify_marks)) ||
+	    (m->prev_ns && rcu_access_pointer(m->prev_ns->n_fsnotify_marks)))
 		list_add_tail(&m->to_notify, &notify_list);
 	else
 		m->prev_ns = m->mnt_ns;
diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index 8657bb9..a496220 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -537,11 +537,11 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
 	 * SRCU because we have no references to any objects and do not
 	 * need SRCU to keep them "alive".
 	 */
-	if ((!sbinfo || !sbinfo->sb_marks) &&
-	    (!mnt || !mnt->mnt_fsnotify_marks) &&
-	    (!inode || !inode->i_fsnotify_marks) &&
-	    (!inode2 || !inode2->i_fsnotify_marks) &&
-	    (!mnt_data || !mnt_data->ns->n_fsnotify_marks))
+	if ((!sbinfo || !rcu_access_pointer(sbinfo->sb_marks)) &&
+	    (!mnt || !rcu_access_pointer(mnt->mnt_fsnotify_marks)) &&
+	    (!inode || !rcu_access_pointer(inode->i_fsnotify_marks)) &&
+	    (!inode2 || !rcu_access_pointer(inode2->i_fsnotify_marks)) &&
+	    (!mnt_data || !rcu_access_pointer(mnt_data->ns->n_fsnotify_marks)))
 		return 0;
 
 	if (sb)
@@ -710,7 +710,7 @@ void fsnotify_mnt(__u32 mask, struct mnt_namespace *ns, struct vfsmount *mnt)
 	 * This is an optimization as well as making sure fsnotify_init() has
 	 * been called.
 	 */
-	if (!ns->n_fsnotify_marks)
+	if (!rcu_access_pointer(ns->n_fsnotify_marks))
 		return;
 
 	fsnotify(mask, &data, FSNOTIFY_EVENT_MNT, NULL, NULL, NULL, 0);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] fsnotify: use rcu_access_pointer() to test the mark connector pointers
  2026-12-02 18:13 [PATCH] fsnotify: use rcu_access_pointer() to test the mark connector pointers Jiayi Zhang
@ 2026-12-03  1:03 ` kernel test robot
  0 siblings, 0 replies; 2+ messages in thread
From: kernel test robot @ 2026-12-03  1:03 UTC (permalink / raw)
  To: Jiayi Zhang, Jan Kara, Amir Goldstein
  Cc: oe-kbuild-all, Christian Brauner, Alexander Viro, Miklos Szeredi,
	linux-fsdevel, linux-kernel, syzbot+f968afcac117a84b7f14,
	syzkaller-bugs

Hi Jiayi,

kernel test robot noticed the following build errors:

[auto build test ERROR on brauner-vfs/vfs.all]
[also build test ERROR on jack-fs/fsnotify linus/master v7.4-rc5 next-20261130]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Jiayi-Zhang/fsnotify-use-rcu_access_pointer-to-test-the-mark-connector-pointers/20261202-181338
base:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all
patch link:    https://lore.kernel.org/r/20261202181338.4162502-1-zhangjiayi1014%40gmail.com
patch subject: [PATCH] fsnotify: use rcu_access_pointer() to test the mark connector pointers
config: arm-randconfig-1003-20261203 (https://download.01.org/0day-ci/archive/20261203/20261203083353.XuNYnFsf-lkp@intel.com/config)
compiler: arm-linux-gnueabi-gcc (GCC) 8.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261203/20261203083353.XuNYnFsf-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/20261203083353.XuNYnFsf-lkp@intel.com/

All errors (new ones prefixed by >>):

   In file included from include/linux/compiler_types.h:44,
                    from <command-line>:
   fs/mount.h: In function 'mnt_notify_add':
>> include/linux/rcupdate.h:504:9: error: dereferencing pointer to incomplete type 'struct fsnotify_mark_connector'
     typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
            ^
   include/linux/compiler-context-analysis.h:218:2: note: in definition of macro 'context_unsafe'
     __VA_ARGS__;   \
     ^~~~~~~~~~~
   include/linux/rcupdate.h:625:31: note: in expansion of macro '__rcu_access_pointer'
    #define rcu_access_pointer(p) __rcu_access_pointer((p), __UNIQUE_ID(rcu), __rcu)
                                  ^~~~~~~~~~~~~~~~~~~~
   fs/mount.h:247:20: note: in expansion of macro 'rcu_access_pointer'
     if ((m->mnt_ns && rcu_access_pointer(m->mnt_ns->n_fsnotify_marks)) ||
                       ^~~~~~~~~~~~~~~~~~
--
   In file included from include/linux/compiler_types.h:44,
                    from <command-line>:
   fs/autofs/../mount.h: In function 'mnt_notify_add':
>> include/linux/rcupdate.h:504:9: error: dereferencing pointer to incomplete type 'struct fsnotify_mark_connector'
     typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
            ^
   include/linux/compiler-context-analysis.h:218:2: note: in definition of macro 'context_unsafe'
     __VA_ARGS__;   \
     ^~~~~~~~~~~
   include/linux/rcupdate.h:625:31: note: in expansion of macro '__rcu_access_pointer'
    #define rcu_access_pointer(p) __rcu_access_pointer((p), __UNIQUE_ID(rcu), __rcu)
                                  ^~~~~~~~~~~~~~~~~~~~
   fs/autofs/../mount.h:247:20: note: in expansion of macro 'rcu_access_pointer'
     if ((m->mnt_ns && rcu_access_pointer(m->mnt_ns->n_fsnotify_marks)) ||
                       ^~~~~~~~~~~~~~~~~~


vim +504 include/linux/rcupdate.h

76c8eaafe4f061f Paul E. McKenney        2021-04-21  501  
573d76f7db5ba05 Paul E. McKenney        2026-06-28  502  #define __rcu_access_pointer(p, local, space) context_unsafe( \
ca5ecddfa8fcbd9 Paul E. McKenney        2010-04-28  503  ({ \
24ba53017e188e0 Chun-Hung Tseng         2021-09-15 @504  	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
423a86a610cad12 Joel Fernandes (Google  2018-12-12  505) 	rcu_check_sparse(p, space); \
0381ade2ea82b48 Ricardo Ribalda         2026-09-14  506  	((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(local)); \
573d76f7db5ba05 Paul E. McKenney        2026-06-28  507  }) )
24ba53017e188e0 Chun-Hung Tseng         2021-09-15  508  #define __rcu_dereference_check(p, local, c, space) \
ca5ecddfa8fcbd9 Paul E. McKenney        2010-04-28  509  ({ \
ac59853c06993a4 Pranith Kumar           2014-11-13  510  	/* Dependency order vs. p above. */ \
24ba53017e188e0 Chun-Hung Tseng         2021-09-15  511  	typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \
f78f5b90c4ffa55 Paul E. McKenney        2015-06-18  512  	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
423a86a610cad12 Joel Fernandes (Google  2018-12-12  513) 	rcu_check_sparse(p, space); \
0381ade2ea82b48 Ricardo Ribalda         2026-09-14  514  	((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(local)); \
ca5ecddfa8fcbd9 Paul E. McKenney        2010-04-28  515  })
24ba53017e188e0 Chun-Hung Tseng         2021-09-15  516  #define __rcu_dereference_protected(p, local, c, space) \
ca5ecddfa8fcbd9 Paul E. McKenney        2010-04-28  517  ({ \
f78f5b90c4ffa55 Paul E. McKenney        2015-06-18  518  	RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
423a86a610cad12 Joel Fernandes (Google  2018-12-12  519) 	rcu_check_sparse(p, space); \
0381ade2ea82b48 Ricardo Ribalda         2026-09-14  520  	((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(p)); \
ca5ecddfa8fcbd9 Paul E. McKenney        2010-04-28  521  })
24ba53017e188e0 Chun-Hung Tseng         2021-09-15  522  #define __rcu_dereference_raw(p, local) \
995f1405610bd84 Paul E. McKenney        2016-07-01  523  ({ \
995f1405610bd84 Paul E. McKenney        2016-07-01  524  	/* Dependency order vs. p above. */ \
24ba53017e188e0 Chun-Hung Tseng         2021-09-15  525  	typeof(p) local = READ_ONCE(p); \
0381ade2ea82b48 Ricardo Ribalda         2026-09-14  526  	((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(local)); \
995f1405610bd84 Paul E. McKenney        2016-07-01  527  })
24ba53017e188e0 Chun-Hung Tseng         2021-09-15  528  #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu))
ca5ecddfa8fcbd9 Paul E. McKenney        2010-04-28  529  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03  1:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 18:13 [PATCH] fsnotify: use rcu_access_pointer() to test the mark connector pointers Jiayi Zhang
2026-12-03  1:03 ` kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.