Technical Root Cause Analysis
1. Involved Files, Functions, Structs, and Fields
- Source Files:
- Data Structures and Fields:
struct fsnotify_mark::flags (unsigned int, offset 0x44 / 68 bytes), defined in include/linux/fsnotify_backend.h.
- Flags involved:
FSNOTIFY_MARK_FLAG_ALIVE (0x0001), FSNOTIFY_MARK_FLAG_EXCL_UNLINK (0x0010).
- Functions Involved:
2. Detailed Technical Mechanism
A. Reader Path (fsnotify_handle_inode_event):
During file closure (close() -> __fput() -> fsnotify_close() -> fsnotify()), the notification engine walks registered mark lists for watched objects:
fsnotify() acquires srcu_read_lock(&fsnotify_mark_srcu) (fs/notify/fsnotify.c:568) to iterate over attached struct fsnotify_mark entries without taking per-mark spinlocks.
- In
send_to_group() and fsnotify_handle_event(), fsnotify_handle_inode_event() is called to evaluate event delivery rules.
- At
fs/notify/fsnotify.c:267, it performs an unannotated, plain C read of inode_mark->flags:
if ((inode_mark->flags & FSNOTIFY_MARK_FLAG_EXCL_UNLINK) &&
path && d_unlinked(path->dentry))
return 0;
B. Writer Path (fsnotify_free_mark):
Concurrently, a user-space task removes a watch via inotify_rm_watch(fd, wd):
inotify_rm_watch() locates the mark via inotify_idr_find() and invokes fsnotify_destroy_mark().
fsnotify_destroy_mark() detaches the mark from the group and calls fsnotify_free_mark().
- In
fsnotify_free_mark() (fs/notify/mark.c:644-651), the function takes spin_lock(&mark->lock) and modifies the lifecycle state by clearing FSNOTIFY_MARK_FLAG_ALIVE:
spin_lock(&mark->lock);
if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
spin_unlock(&mark->lock);
return;
}
mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
spin_unlock(&mark->lock);
3. Invariant Violation and Root Cause
In include/linux/fsnotify_backend.h:
struct fsnotify_mark {
...
spinlock_t lock; /* Protects inode / mnt pointers, flags, masks */
...
unsigned int flags; /* flags [mark->lock] */
};
Per fsnotify's locking architecture, mark->lock serializes all modifications to mark->flags. The lifetime of the fsnotify_mark allocation itself during event iteration is protected by SRCU (fsnotify_mark_srcu), which guarantees the mark memory is not freed until an SRCU grace period finishes.
Inotify control flags (FSNOTIFY_MARK_FLAG_EXCL_UNLINK and FSNOTIFY_MARK_FLAG_IN_ONESHOT) were placed into the same flags field as internal mark lifecycle flags (FSNOTIFY_MARK_FLAG_ALIVE and FSNOTIFY_MARK_FLAG_ATTACHED). Because the SRCU event-delivery reader inspects inode_mark->flags locklessly via a plain C read while the writer modifies mark->flags via a plain C read-modify-write under mark->lock, the concurrent access constitutes a data race under the Linux Kernel Memory Model, as flagged by KCSAN.