[PATCH] stat: annotate the lockless inode block count snapshot
From: Zhu Jun
Date: Wed Nov 11 2026 - 01:56:50 EST
generic_fillattr() reads i_blocks without inode->i_lock, while concurrent
space accounting updates it under that lock. KCSAN reports the read
racing with inode_add_bytes(), for example during a tmpfs write.
The block count returned by stat is a snapshot taken while file contents
may change. Annotate this intentional lockless read with data_race(),
without marking or weakening the locked space-accounting updates.
Fixes: 1da177e4c3f41 ("Linux-2.6.12-rc2")
Signed-off-by: Zhu Jun <zhujun2@xxxxxxxxxxxxxxxxxxxx>
---
fs/stat.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/fs/stat.c b/fs/stat.c
index a2e63c6c45b7..6e6f99aaa628 100644
--- a/fs/stat.c
+++ b/fs/stat.c
@@ -103,7 +103,8 @@ void generic_fillattr(const struct mnt_idmap *idmap, u32 request_mask,
}
stat->blksize = i_blocksize(inode);
- stat->blocks = inode->i_blocks;
+ /* The block count is a snapshot and may race with space accounting. */
+ stat->blocks = data_race(inode->i_blocks);
if ((request_mask & STATX_CHANGE_COOKIE) && IS_I_VERSION(inode)) {
stat->result_mask |= STATX_CHANGE_COOKIE;
--
2.51.0