Bug Mechanism and Technical Root Cause
1. Functions, Structs, and Fields Involved
fs/kernfs/inode.c: kernfs_iop_getattr(), kernfs_refresh_inode()
fs/notify/dnotify/dnotify.c: dnotify_flush()
struct inode: umode_t i_mode (at offset 0, 2 bytes)
struct kernfs_node: umode_t mode
struct kernfs_root: struct rw_semaphore kernfs_iattr_rwsem
2. Detailed Root Cause Analysis
In the VFS layer, querying file metadata via stat(), fstat(), or statx() invokes vfs_getattr_nosec(), which calls the filesystem's inode->i_op->getattr callback (kernfs_iop_getattr() in kernfs). A getattr call is fundamentally a read-only metadata retrieval operation and does not acquire inode->i_rwsem or any other VFS lock on struct inode.
In kernfs (fs/kernfs/inode.c), the backing kernfs_node stores persistent metadata, while struct inode is an ephemeral or cached VFS object created on demand. When getattr is invoked:
int kernfs_iop_getattr(const struct mnt_idmap *idmap,
const struct path *path, struct kstat *stat,
u32 request_mask, unsigned int query_flags)
{
struct inode *inode = d_inode(path->dentry);
struct kernfs_node *kn = inode->i_private;
struct kernfs_root *root = kernfs_root(kn);
down_read(&root->kernfs_iattr_rwsem);
kernfs_refresh_inode(kn, inode);
generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
up_read(&root->kernfs_iattr_rwsem);
return 0;
}
kernfs_refresh_inode() refreshes the VFS inode attributes from the kernfs_node so that generic_fillattr() can copy them into struct kstat *stat:
static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
{
struct kernfs_iattrs *attrs;
inode->i_mode = kn->mode;
attrs = kernfs_iattrs_noalloc(kn);
if (attrs)
set_inode_attr(inode, attrs);
if (kernfs_type(kn) == KERNFS_DIR && !(kn->flags & KERNFS_REMOVING))
set_nlink(inode, kn->dir.subdirs + 2);
}
Notice that kernfs_iop_getattr() only acquires a shared read lock (down_read(&root->kernfs_iattr_rwsem)). Previously, kernfs_refresh_inode() was protected by spin_lock(&inode->i_lock), but commit aa1d058d48f2 ("kernfs: dont take i_lock on inode attr read") dropped inode->i_lock on the assumption that concurrent callers always copy the same values from kn.
However, kernfs_refresh_inode() unconditionally performs a direct C assignment (inode->i_mode = kn->mode;), generating an unsynchronized 2-byte store instruction directly into inode->i_mode.
3. Concurrent Trigger and Invariant Violation
Concurrently, another task closing a file descriptor on the same inode calls close() -> __do_sys_close() -> filp_close_sync() -> filp_flush(), which executes dnotify_flush(filp, id):
void dnotify_flush(struct file *filp, fl_owner_t id)
{
...
inode = file_inode(filp);
if (!S_ISDIR(inode->i_mode))
return;
...
}
dnotify_flush() performs a plain load from inode->i_mode at line 161 without holding any lock. In VFS, the file type bits (S_IFMT) of inode->i_mode are immutable after inode creation (a directory never changes its type to a non-directory, nor does a regular file become a directory). VFS callers throughout the kernel therefore routinely check S_ISDIR(inode->i_mode) locklessly.
Because kernfs_iop_getattr() performs an uninstrumented store to inode->i_mode without synchronization (or without checking if inode->i_mode != kn->mode / using WRITE_ONCE()), KCSAN detects the concurrent read and write to 0xffff88810632faa0 (&inode->i_mode) and reports a data race between dnotify_flush() and kernfs_refresh_inode().