Skip to contentSkip to conversation

f2fs: fix indefinite loop scanning for free nid

PATCH In review Sahitya Tummala 2 participants 2 messages Aug 18, 2020 – Aug 19, 2020 F2FS file system

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 f2fs: fix indefinite loop scanning for free nid

If the sbi->ckpt->next_free_nid is not NAT block aligned and if there are free nids in that NAT block between the start of the block and next_free_nid, then those free nids will not be scanned in scan_nat_page(). This results into mismatch between nm_i->available_nids and the sum of nm_i->free_nid_count of all NAT blocks scanned. And nm_i->available_nids will always be greater than the sum of free nids in all the blocks. Under this condition, if we use all the currently scanned free nids, then it will loop forever in f2fs_alloc_nid() as nm_i->available_nids is still not zero but nm_i->free_nid_count of that partially scanned NAT block is zero.

Fix this to align the nm_i->next_scan_nid to the first nid of the corresponding NAT block.

---
v2:
- address Chao's comments to move the check to beginning of function and to add unlikely

 fs/f2fs/node.c | 3 +++
 1 file changed, 3 insertions(+)
fs/f2fs/node.c +3 -0
index 9bbaa26..a2ddca1 100644
@@ -2373,6 +2373,9 @@ static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
 	if (unlikely(nid >= nm_i->max_nid))
 		nid = 0;
 
+	if (unlikely(nid % NAT_ENTRY_PER_BLOCK))
+		nid = NAT_BLOCK_OFFSET(nid) * NAT_ENTRY_PER_BLOCK;
+
 	/* Enough entries */
 	if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
 		return 0;
Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Sahitya Tummala

If the sbi->ckpt->next_free_nid is not NAT block aligned and if there are free nids in that NAT block between the start of the block and next_free_nid, then those free nids will not be scanned in scan_nat_page(). This results into mismatch between nm_i->available_nids and the sum of nm_i->free_nid_count of all NAT blocks scanned. And nm_i->available_nids will always be greater than the sum of free nids in all the blocks. Under this condition, if we use all the currently scanned free nids, then it will loop forever in f2fs_alloc_nid() as nm_i->available_nids is still not zero but nm_i->free_nid_count of that partially scanned NAT block is zero.

Quoted 4 lines

Fix this to align the nm_i->next_scan_nid to the first nid of the corresponding NAT block.

Quoted 2 lines

During CP we update only ckpt->next_free_nid but not the nm_i->next_free_nid, which is done only once during boot up.

Oops, thanks for correcting that...

Thanks,