Skip to content

Commit cfd66bb

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: fix deadloop in foreground GC
As Yanming reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=215914 The root cause is: in a very small sized image, it's very easy to exceed threshold of foreground GC, if we calculate free space and dirty data based on section granularity, in corner case, has_not_enough_free_secs() will always return true, result in deadloop in f2fs_gc(). So this patch refactors has_not_enough_free_secs() as below to fix this issue: 1. calculate needed space based on block granularity, and separate all blocks to two parts, section part, and block part, comparing section part to free section, and comparing block part to free space in openned log. 2. account F2FS_DIRTY_NODES, F2FS_DIRTY_IMETA and F2FS_DIRTY_DENTS as node block consumer; 3. account F2FS_DIRTY_DENTS as data block consumer; Cc: stable@vger.kernel.org Reported-by: Ming Yan <yanming@tju.edu.cn> Signed-off-by: Chao Yu <chao.yu@oppo.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 25f8236 commit cfd66bb

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

fs/f2fs/segment.h

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,10 @@ static inline int reserved_sections(struct f2fs_sb_info *sbi)
572572
return GET_SEC_FROM_SEG(sbi, reserved_segments(sbi));
573573
}
574574

575-
static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi)
575+
static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi,
576+
unsigned int node_blocks, unsigned int dent_blocks)
576577
{
577-
unsigned int node_blocks = get_pages(sbi, F2FS_DIRTY_NODES) +
578-
get_pages(sbi, F2FS_DIRTY_DENTS);
579-
unsigned int dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS);
578+
580579
unsigned int segno, left_blocks;
581580
int i;
582581

@@ -602,19 +601,28 @@ static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi)
602601
static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
603602
int freed, int needed)
604603
{
605-
int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
606-
int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
607-
int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
604+
unsigned int total_node_blocks = get_pages(sbi, F2FS_DIRTY_NODES) +
605+
get_pages(sbi, F2FS_DIRTY_DENTS) +
606+
get_pages(sbi, F2FS_DIRTY_IMETA);
607+
unsigned int total_dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS);
608+
unsigned int node_secs = total_node_blocks / BLKS_PER_SEC(sbi);
609+
unsigned int dent_secs = total_dent_blocks / BLKS_PER_SEC(sbi);
610+
unsigned int node_blocks = total_node_blocks % BLKS_PER_SEC(sbi);
611+
unsigned int dent_blocks = total_dent_blocks % BLKS_PER_SEC(sbi);
612+
unsigned int free, need_lower, need_upper;
608613

609614
if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
610615
return false;
611616

612-
if (free_sections(sbi) + freed == reserved_sections(sbi) + needed &&
613-
has_curseg_enough_space(sbi))
617+
free = free_sections(sbi) + freed;
618+
need_lower = node_secs + dent_secs + reserved_sections(sbi) + needed;
619+
need_upper = need_lower + (node_blocks ? 1 : 0) + (dent_blocks ? 1 : 0);
620+
621+
if (free > need_upper)
614622
return false;
615-
return (free_sections(sbi) + freed) <=
616-
(node_secs + 2 * dent_secs + imeta_secs +
617-
reserved_sections(sbi) + needed);
623+
else if (free <= need_lower)
624+
return true;
625+
return !has_curseg_enough_space(sbi, node_blocks, dent_blocks);
618626
}
619627

620628
static inline bool f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi)

0 commit comments

Comments
 (0)