Root Cause Analysis
The KCSAN report describes a data race on the 4-byte unsigned int i_flags field of struct inode (include/linux/fs.h:766, at offset +4 bytes):
-
Writer side (ioctl_setflags -> inode_set_flags):
-
Reader side (openat -> IS_PRIVATE(inode)):
- Concurrently,
do_open() (fs/namei.c:4796) executes without holding inode->i_rwsem:
IS_PRIVATE(inode) is defined in include/linux/fs.h:2172 as ((inode)->i_flags & S_PRIVATE), which performs an unmarked plain 4-byte load of inode->i_flags (the same applies to the other IS_*(inode) macros in include/linux/fs.h:2145-2183, such as IS_IMMUTABLE and IS_APPEND also checked during inode_permission() / may_open()).
Synchronization / Invariant Mismatch
While inode_set_flags() expects writers to hold i_rwsem exclusively (or have exclusive access during inode instantiation) and additionally uses try_cmpxchg() in set_mask_bits() to serialize concurrent writers, lockless readers of inode->i_flags during path/open permission checks do not hold i_rwsem and do not wrap their accesses with READ_ONCE(). KCSAN therefore flags the unmarked read in IS_PRIVATE(inode) racing with the marked try_cmpxchg() update in inode_set_flags().
Notes on Mechanism and Potential Fix
inode->i_flags is a 4-byte aligned unsigned int updated atomically via try_cmpxchg(), so bits outside mask (including S_PRIVATE) are preserved across the atomic RMW.
- Using
READ_ONCE(inode->i_flags) in IS_PRIVATE() (and the sibling IS_* flag-testing macros in include/linux/fs.h) would annotate the lockless read side and prevent compiler load tearing/reloading as well as silencing the KCSAN report.