Root Cause Analysis
Involved Functions, Structs, and Fields
- Source Files:
- Data Structures and Fields:
struct inode::i_fsnotify_marks (struct fsnotify_mark_connector __rcu * in include/linux/fs.h:870)
struct fsnotify_mark_connector (conn)
fsnotify_connp_t (struct fsnotify_mark_connector *const *)
- Functions:
Bug Mechanism and Invariant Violation
-
Write Side (fs/notify/mark.c:423):
When an inotify watch is removed (e.g. via sys_inotify_rm_watch()), fsnotify_put_mark() destroys the mark. When detaching the last mark attached to the inode connector, fsnotify_detach_connector_from_object() clears the inode's connector pointer using RCU:
fsnotify_connp_t *connp = fsnotify_object_connp(conn->obj, conn->type);
...
rcu_assign_pointer(*connp, NULL);
For an inode, *connp is inode->i_fsnotify_marks. rcu_assign_pointer() issues an 8-byte marked store (smp_store_release).
After clearing *connp, line 427 calls fsnotify_update_sb_watchers(sb, conn). Because the superblock watcher count is updated after *connp is cleared, a concurrent notification path can still see fsnotify_sb_has_watchers(sb) == true and proceed into fsnotify().
-
Read Side (fs/notify/fsnotify.c:540–545):
Concurrently, a task closing a file enters close() $\to$ __fput() $\to$ fsnotify_close() $\to$ __fsnotify_parent() $\to$ fsnotify().
Before taking srcu_read_lock(&fsnotify_mark_srcu), fsnotify() executes an opportunistic fast-path optimization to avoid the memory barrier overhead of srcu_read_lock() if no objects on the notification path have any marks:
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))
return 0;
Although inode->i_fsnotify_marks (and the other connector pointers) is declared with __rcu, this check reads it as a plain, unmarked C load (!inode->i_fsnotify_marks) without using rcu_access_pointer() or READ_ONCE().
-
KCSAN Detection:
KCSAN detects the concurrent unmarked 8-byte read of inode->i_fsnotify_marks in fsnotify() racing against the marked 8-byte write (rcu_assign_pointer(*connp, NULL)) in fsnotify_detach_connector_from_object().
Invariants and Suggested Fix
The read in fsnotify() is only testing whether the __rcu pointer is non-NULL and does not dereference it at this point (if non-NULL, it enters srcu_read_lock(&fsnotify_mark_srcu) and later safely accesses *connp via srcu_dereference() inside fsnotify_first_mark()).
According to kernel concurrency rules, lockless NULL checks on __rcu pointers outside of an RCU/SRCU read-side critical section must be wrapped in rcu_access_pointer(). The fix is to wrap the connector pointer checks in fsnotify() (and similar checks like fsnotify_mnt() at fs/notify/fsnotify.c:713) with rcu_access_pointer():
--- 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,
* However, if we do not walk the lists, we do not have to do
* 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;