Skip to contentSkip to conversation

f2fs: fix unsigned underflow in has_enough_free_blks()

PATCH In review Yangyang Zang 1 participant 2 messages Oct 22 – Nov 28 F2FS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs: fix unsigned underflow in has_enough_free_blks()

has_enough_free_blks() checks the filesystem has enough free blocks by computing

avail_user_block_count(sbi) - valid_user_blocks(sbi)

Both operands are unsigned, so when avail_user_block_count is less than valid_user_blocks the subtraction wraps around to a large positive value and the function incorrectly reports that enough free blocks are available.

Replace the subtraction with a direct comparison so the check stays correct regardless of which operand is larger.

Fixes 84b5bb8bf0f6 (f2fs: modify f2fs_is_checkpoint_ready logic to allow more data to be written with the CP disable)
---
 fs/f2fs/segment.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/f2fs/segment.h +3 -3
index 526764ba31ae..c69550084bea 100644
@@ -707,17 +707,17 @@ static inline bool has_enough_free_secs(struct f2fs_sb_info *sbi,
 
 static inline bool has_enough_free_blks(struct f2fs_sb_info *sbi)
 {
-	unsigned int total_free_blocks = 0;
+	unsigned int valid_user_block_count;
 	unsigned int avail_user_block_count;
 
 	spin_lock(&sbi->stat_lock);
 
 	avail_user_block_count = get_available_block_count(sbi, NULL, true);
-	total_free_blocks = avail_user_block_count - (unsigned int)valid_user_blocks(sbi);
+	valid_user_block_count = (unsigned int)valid_user_blocks(sbi);
 
 	spin_unlock(&sbi->stat_lock);
 
-	return total_free_blocks > 0;
+	return avail_user_block_count > valid_user_block_count;
 }
 
 static inline bool f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi)
Signature · 8 lines
2.43.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw
Quoted 34 lines · Yangyang Zang

has_enough_free_blks() checks the filesystem has enough free blocks by computing

avail_user_block_count(sbi) - valid_user_blocks(sbi)

Both operands are unsigned, so when avail_user_block_count is less than valid_user_blocks the subtraction wraps around to a large positive value and the function incorrectly reports that enough free blocks are available.

Replace the subtraction with a direct comparison so the check stays correct regardless of which operand is larger.

Fixes 84b5bb8bf0f6 (f2fs: modify f2fs_is_checkpoint_ready logic to allow more data to be written with the CP disable)
---
  fs/f2fs/segment.h | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
fs/f2fs/segment.h +1 -1
index 526764ba31ae..c69550084bea 100644
@@ -707,17 +707,17 @@ static inline bool has_enough_free_secs(struct f2fs_sb_info *sbi,
 
  static inline bool has_enough_free_blks(struct f2fs_sb_info *sbi)
  {
-	unsigned int total_free_blocks = 0;
+	unsigned int valid_user_block_count;
  	unsigned int avail_user_block_count;
 
  	spin_lock(&sbi->stat_lock);
 
  	avail_user_block_count = get_available_block_count(sbi, NULL, true);

fs/f2fs/segment.h static inline bool has_enough_free_secs(struct f2fs_sb_info *sbi, · line 723

Quoted 10 lines
fs/f2fs/segment.h +2 -2
-	total_free_blocks = avail_user_block_count - (unsigned int)valid_user_blocks(sbi);
+	valid_user_block_count = (unsigned int)valid_user_blocks(sbi);
 
  	spin_unlock(&sbi->stat_lock);
 
-	return total_free_blocks > 0;
+	return avail_user_block_count > valid_user_block_count;
  }
 
  static inline bool f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi)

ping

Thanks,
Yangyang

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel