Skip to content

Commit bbf9f7d

Browse files
Sahitya TummalaJaegeuk Kim
authored andcommitted
f2fs: Fix indefinite loop in f2fs_gc()
Policy - Foreground GC, LFS and greedy GC mode. Under this policy, f2fs_gc() loops forever to GC as it doesn't have enough free segements to proceed and thus it keeps calling gc_more for the same victim segment. This can happen if the selected victim segment could not be GC'd due to failed blkaddr validity check i.e. is_alive() returns false for the blocks set in current validity map. Fix this by keeping track of such invalid segments and skip those segments for selection in get_victim_by_default() to avoid endless GC loop under such error scenarios. Currently, add this logic under CONFIG_F2FS_CHECK_FS to be able to root cause the issue in debug version. Signed-off-by: Sahitya Tummala <stummala@codeaurora.org> Reviewed-by: Chao Yu <yuchao0@huawei.com> [Jaegeuk Kim: fix wrong bitmap size] Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 2fde3dd commit bbf9f7d

3 files changed

Lines changed: 46 additions & 9 deletions

File tree

‎fs/f2fs/gc.c‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,16 @@ static int get_victim_by_default(struct f2fs_sb_info *sbi,
382382
nsearched++;
383383
}
384384

385+
#ifdef CONFIG_F2FS_CHECK_FS
386+
/*
387+
* skip selecting the invalid segno (that is failed due to block
388+
* validity check failure during GC) to avoid endless GC loop in
389+
* such cases.
390+
*/
391+
if (test_bit(segno, sm->invalid_segmap))
392+
goto next;
393+
#endif
394+
385395
secno = GET_SEC_FROM_SEG(sbi, segno);
386396

387397
if (sec_usage_check(sbi, secno))
@@ -627,8 +637,21 @@ static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
627637
source_blkaddr = datablock_addr(NULL, node_page, ofs_in_node);
628638
f2fs_put_page(node_page, 1);
629639

630-
if (source_blkaddr != blkaddr)
640+
if (source_blkaddr != blkaddr) {
641+
#ifdef CONFIG_F2FS_CHECK_FS
642+
unsigned int segno = GET_SEGNO(sbi, blkaddr);
643+
unsigned long offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
644+
645+
if (unlikely(check_valid_map(sbi, segno, offset))) {
646+
if (!test_and_set_bit(segno, SIT_I(sbi)->invalid_segmap)) {
647+
f2fs_err(sbi, "mismatched blkaddr %u (source_blkaddr %u) in seg %u\n",
648+
blkaddr, source_blkaddr, segno);
649+
f2fs_bug_on(sbi, 1);
650+
}
651+
}
652+
#endif
631653
return false;
654+
}
632655
return true;
633656
}
634657

‎fs/f2fs/segment.c‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -817,9 +817,13 @@ static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
817817
if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
818818
dirty_i->nr_dirty[t]--;
819819

820-
if (get_valid_blocks(sbi, segno, true) == 0)
820+
if (get_valid_blocks(sbi, segno, true) == 0) {
821821
clear_bit(GET_SEC_FROM_SEG(sbi, segno),
822822
dirty_i->victim_secmap);
823+
#ifdef CONFIG_F2FS_CHECK_FS
824+
clear_bit(segno, SIT_I(sbi)->invalid_segmap);
825+
#endif
826+
}
823827
}
824828
}
825829

@@ -3946,7 +3950,7 @@ static int build_sit_info(struct f2fs_sb_info *sbi)
39463950
struct sit_info *sit_i;
39473951
unsigned int sit_segs, start;
39483952
char *src_bitmap, *bitmap;
3949-
unsigned int bitmap_size;
3953+
unsigned int bitmap_size, main_bitmap_size, sit_bitmap_size;
39503954

39513955
/* allocate memory for SIT information */
39523956
sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL);
@@ -3962,8 +3966,8 @@ static int build_sit_info(struct f2fs_sb_info *sbi)
39623966
if (!sit_i->sentries)
39633967
return -ENOMEM;
39643968

3965-
bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
3966-
sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, bitmap_size,
3969+
main_bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
3970+
sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, main_bitmap_size,
39673971
GFP_KERNEL);
39683972
if (!sit_i->dirty_sentries_bitmap)
39693973
return -ENOMEM;
@@ -4012,17 +4016,23 @@ static int build_sit_info(struct f2fs_sb_info *sbi)
40124016
sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
40134017

40144018
/* setup SIT bitmap from ckeckpoint pack */
4015-
bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
4019+
sit_bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
40164020
src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
40174021

4018-
sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
4022+
sit_i->sit_bitmap = kmemdup(src_bitmap, sit_bitmap_size, GFP_KERNEL);
40194023
if (!sit_i->sit_bitmap)
40204024
return -ENOMEM;
40214025

40224026
#ifdef CONFIG_F2FS_CHECK_FS
4023-
sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
4027+
sit_i->sit_bitmap_mir = kmemdup(src_bitmap,
4028+
sit_bitmap_size, GFP_KERNEL);
40244029
if (!sit_i->sit_bitmap_mir)
40254030
return -ENOMEM;
4031+
4032+
sit_i->invalid_segmap = f2fs_kvzalloc(sbi,
4033+
main_bitmap_size, GFP_KERNEL);
4034+
if (!sit_i->invalid_segmap)
4035+
return -ENOMEM;
40264036
#endif
40274037

40284038
/* init SIT information */
@@ -4031,7 +4041,7 @@ static int build_sit_info(struct f2fs_sb_info *sbi)
40314041
sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
40324042
sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
40334043
sit_i->written_valid_blocks = 0;
4034-
sit_i->bitmap_size = bitmap_size;
4044+
sit_i->bitmap_size = sit_bitmap_size;
40354045
sit_i->dirty_sentries = 0;
40364046
sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
40374047
sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
@@ -4514,6 +4524,7 @@ static void destroy_sit_info(struct f2fs_sb_info *sbi)
45144524
kvfree(sit_i->sit_bitmap);
45154525
#ifdef CONFIG_F2FS_CHECK_FS
45164526
kvfree(sit_i->sit_bitmap_mir);
4527+
kvfree(sit_i->invalid_segmap);
45174528
#endif
45184529
kvfree(sit_i);
45194530
}

‎fs/f2fs/segment.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ struct sit_info {
230230
char *sit_bitmap; /* SIT bitmap pointer */
231231
#ifdef CONFIG_F2FS_CHECK_FS
232232
char *sit_bitmap_mir; /* SIT bitmap mirror */
233+
234+
/* bitmap of segments to be ignored by GC in case of errors */
235+
unsigned long *invalid_segmap;
233236
#endif
234237
unsigned int bitmap_size; /* SIT bitmap size */
235238

0 commit comments

Comments
 (0)