Skip to contentSkip to conversation

f2fs: add READ_ONCE() for i_blocks in f2fs_update_inode()

PATCH Superseded Cen Zhang 2 participants 3 messages Mar 17 – Mar 18 F2FS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Cen Zhang

author
#0 lore raw

PATCH 1/1 f2fs: add READ_ONCE() for i_blocks in f2fs_update_inode()

f2fs_update_inode() reads inode->i_blocks without holding i_lock to serialize it to the on-disk inode, while concurrent truncate or allocation paths may modify i_blocks under i_lock. Since blkcnt_t is u64, this risks torn reads on 32-bit architectures.

Following the approach in ext4_inode_blocks_set(), add READ_ONCE() to prevent potential compiler-induced tearing.

---
 fs/f2fs/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/f2fs/inode.c +1 -1
index 078874db918c..73b913dbe02a 100644
@@ -694,7 +694,7 @@ void f2fs_update_inode(struct inode *inode, struct folio *node_folio)
 	ri->i_uid = cpu_to_le32(i_uid_read(inode));
 	ri->i_gid = cpu_to_le32(i_gid_read(inode));
 	ri->i_links = cpu_to_le32(inode->i_nlink);
-	ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
+	ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(READ_ONCE(inode->i_blocks)) + 1);
 
 	if (!f2fs_is_atomic_file(inode) ||
 			is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 8 lines · Cen Zhang

f2fs_update_inode() reads inode->i_blocks without holding i_lock to serialize it to the on-disk inode, while concurrent truncate or allocation paths may modify i_blocks under i_lock. Since blkcnt_t is u64, this risks torn reads on 32-bit architectures.

Following the approach in ext4_inode_blocks_set(), add READ_ONCE() to prevent potential compiler-induced tearing.

Need a Fixes line and Cc [email protected]?

Otherwise, it looks good to me.

Thanks,

Quoted 18 lines
---
  fs/f2fs/inode.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/f2fs/inode.c +1 -1
index 078874db918c..73b913dbe02a 100644
@@ -694,7 +694,7 @@ void f2fs_update_inode(struct inode *inode, struct folio *node_folio)
  	ri->i_uid = cpu_to_le32(i_uid_read(inode));
  	ri->i_gid = cpu_to_le32(i_gid_read(inode));
  	ri->i_links = cpu_to_le32(inode->i_nlink);
-	ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
+	ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(READ_ONCE(inode->i_blocks)) + 1);
 
  	if (!f2fs_is_atomic_file(inode) ||
  			is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
author
#2 lore raw

Hi Chao,

Thank you for the review.

Quoted 1 line · Chao Yu

Need a Fixes line and Cc [email protected]?

Good point, added both in v2:

Fixes 19f99cee206c ("f2fs: add core inode operations")
Quoted 1 line

Otherwise, it looks good to me.

Thanks!

Best regards,
Cen