Skip to contentSkip to conversation

fsnotify: use rcu_access_pointer() to test the mark connector pointers

PATCH Superseded Jiayi Zhang 3 participants 4 messages Dec 2 – Dec 3

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fsnotify: use rcu_access_pointer() to test the mark connector pointers

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 [email protected]

---
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(-)
fs/mount.h +2 -2
index 4d23cad..b4a019f 100644
@@ -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;
fs/notify/fsnotify.c +6 -6
index 8657bb9..a496220 100644
@@ -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);
Signature · 1 line
2.53.0
#1 lore raw
kernel test robot noticed the following build errors:

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/[email protected]/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/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

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  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#2 lore raw
In reply to Jiayi Zhang
Quoted 28 lines · Jiayi Zhang

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.

Quoted 7 lines

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 [email protected]

Thanks. Added to my tree.

Honza

Quoted 73 lines

---
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(-)
fs/mount.h +2 -2
index 4d23cad..b4a019f 100644
@@ -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;
fs/notify/fsnotify.c +6 -6
index 8657bb9..a496220 100644
@@ -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);
Signature · 1 line
2.53.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#3 lore raw
In reply to Jiayi Zhang
Quoted 28 lines · Jiayi Zhang

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.

Quoted 7 lines

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 [email protected]

I've removed this from my tree again because of 0-day testing failure. The patch is OK in principle but it requires some massaging around mnt_notify_add() so that it compiles properly. I don't want to pull fsnotify internals to all fs/mount.h users so I'd probably make mnt_notify_add() just a thin inline wrapper calling __mnt_notify_add() if we need to check the marks and put __mnt_notify_add() out of line into fs/namespace.c.

Honza

Quoted 73 lines

---
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(-)
fs/mount.h +2 -2
index 4d23cad..b4a019f 100644
@@ -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;
fs/notify/fsnotify.c +6 -6
index 8657bb9..a496220 100644
@@ -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);
Signature · 1 line
2.53.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR