Root Cause Analysis
Involved Subsystem, Files, Functions, and Fields
- Files:
- Functions:
- Data Structures & Fields:
Technical Mechanism and Trigger Conditions
In Linux VFS, attribute retrieval (stat(), statx(), fstatat()) via vfs_getattr() does not acquire the inode's i_rwsem. To allow safe lockless inspection of file and directory sizes, VFS provides the i_size_read() and i_size_write() helpers defined in include/linux/fs.h:
i_size_read(inode) reads inode->i_size using smp_load_acquire() on 64-bit architectures (or under inode->i_size_seqcount seqlock on 32-bit SMP architectures).
i_size_write(inode, size) updates inode->i_size using smp_store_release() on 64-bit architectures (and brackets the write with write_seqcount_begin() / write_seqcount_end(&inode->i_size_seqcount) on 32-bit SMP).
In tmpfs (mm/shmem.c), directory entries pretend to occupy a synthetic size (#define BOGO_DIRENT_SIZE 20). When creating, removing, or renaming directory entries, tmpfs increments or decrements the parent directory's inode->i_size.
In shmem_rename2() (mm/shmem.c:4284-4285):
old_dir->i_size -= BOGO_DIRENT_SIZE;
new_dir->i_size += BOGO_DIRENT_SIZE;
Instead of invoking i_size_write(), shmem_rename2() directly reads and writes old_dir->i_size and new_dir->i_size as plain, unmarked memory operations. (Similar direct modifications exist in other tmpfs directory helpers like shmem_mknod(), shmem_link(), and shmem_unlink()).
While shmem_rename2() holds lock_rename(old_dir, new_dir) protecting against concurrent directory modifications, concurrent readers executing stat() on either directory run locklessly through:
vfs_fstatat() -> vfs_statx() -> vfs_getattr() -> shmem_getattr() -> generic_fillattr() -> i_size_read()
generic_fillattr() calls i_size_read(), performing a marked acquire load of inode->i_size. Concurrently, shmem_rename2() executes an unmarked read-modify-write on the same 8-byte field. Kernel Concurrency Sanitizer (KCSAN) flags this discrepancy as a data race between a marked read and an unmarked write.