[PATCH] f2fs: refactor f2fs_gc to call checkpoint in urgent condition

From: Jaegeuk Kim
Date: Mon Apr 10 2023 - 17:48:50 EST


The major change is to call checkpoint, if there's not enough space while having
some prefree segments in FG_GC case.

Signed-off-by: Jaegeuk Kim <jaegeuk@xxxxxxxxxx>
---
fs/f2fs/gc.c | 26 ++++++++++++--------------
1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index c748cdfb0501..0a823d2e8b9d 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -1829,7 +1829,10 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control)
goto stop;
}

- if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) {
+ /* Let's run FG_GC, if we don't have enough space. */
+ if (has_not_enough_free_secs(sbi, 0, 0)) {
+ gc_type = FG_GC;
+
/*
* For example, if there are many prefree_segments below given
* threshold, we can make them free by checkpoint. Then, we
@@ -1840,8 +1843,6 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control)
if (ret)
goto stop;
}
- if (has_not_enough_free_secs(sbi, 0, 0))
- gc_type = FG_GC;
}

/* f2fs_balance_fs doesn't need to do BG_GC in critical path. */
@@ -1868,19 +1869,14 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control)
if (seg_freed == f2fs_usable_segs_in_sec(sbi, segno))
sec_freed++;

- if (gc_type == FG_GC)
+ if (gc_type == FG_GC) {
sbi->cur_victim_sec = NULL_SEGNO;

- if (gc_control->init_gc_type == FG_GC ||
- !has_not_enough_free_secs(sbi,
- (gc_type == FG_GC) ? sec_freed : 0, 0)) {
- if (gc_type == FG_GC && sec_freed < gc_control->nr_free_secs)
- goto go_gc_more;
- goto stop;
- }
-
- /* FG_GC stops GC by skip_count */
- if (gc_type == FG_GC) {
+ if (!has_not_enough_free_secs(sbi, sec_freed, 0)) {
+ if (sec_freed < gc_control->nr_free_secs)
+ goto go_gc_more;
+ goto stop;
+ }
if (sbi->skipped_gc_rwsem)
skipped_round++;
round++;
@@ -1889,6 +1885,8 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control)
ret = f2fs_write_checkpoint(sbi, &cpc);
goto stop;
}
+ } else if (!has_not_enough_free_secs(sbi, 0, 0)) {
+ goto stop;
}

__get_secs_required(sbi, NULL, &upper_secs, NULL);
--
2.40.0.577.gac1e443424-goog


>
> Thanks,
>
> >
> > 1832
> > 1833 if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) {
> > 1834 /*
> > 1835 * For example, if there are many prefree_segments below given
> > 1836 * threshold, we can make them free by checkpoint. Then, we
> > 1837 * secure free segments which doesn't need fggc any more.
> > 1838 */
> > 1839 if (prefree_segments(sbi)) {
> > 1840 ret = f2fs_write_checkpoint(sbi, &cpc);
> > 1841 if (ret)
> > 1842 goto stop;
> > 1843 }
> > 1844 if (has_not_enough_free_secs(sbi, 0, 0))
> > 1845 gc_type = FG_GC;
> > 1846 }
> >
> > >
> > > One other concern is for those path as below:
> > > - disable_checkpoint
> > > - ioc_gc
> > > - ioc_gc_range
> > > - ioc_resize
> > > ...
> >
> > I think the upper caller should decide to call checkpoint, if they want to
> > reclaim the prefree likewise f2fs_disable_checkpoint.
> >
> > >
> > > We've passed gc_type as FG_GC, but the demand here is to migrate block in time,
> > > rather than dirtying blocks, and callers don't expect checkpoint in f2fs_gc(),
> > > instead the callers will do the checkpoit as it needs.
> > >
> > > That means it's better to decouple FG_GC and write_checkpoint behavior, so I
> > > added another parameter .reclaim_space to just let f2fs_balance_fs() to trigger
> > > checkpoit in the end of f2fs_gc().
> >
> > >
> > > Thanks,
> > >
> > > > + } else if (gc_type == FG_GC) {
> > > > + /* FG_GC stops GC by skip_count */
> > > > if (sbi->skipped_gc_rwsem)
> > > > skipped_round++;
> > > > round++;
> > > > if (skipped_round > MAX_SKIP_GC_COUNT &&
> > > > - skipped_round * 2 >= round) {
> > > > - ret = f2fs_write_checkpoint(sbi, &cpc);
> > > > - goto stop;
> > > > - }
> > > > + skipped_round * 2 >= round)
> > > > + stop_gc = true;
> > > > }
> > > > __get_secs_required(sbi, NULL, &upper_secs, NULL);
> > > > @@ -1901,12 +1898,13 @@ int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control)
> > > > prefree_segments(sbi)) {
> > > > ret = f2fs_write_checkpoint(sbi, &cpc);
> > > > if (ret)
> > > > - goto stop;
> > > > + stop_gc = true;
> > > > }
> > > > go_gc_more:
> > > > - segno = NULL_SEGNO;
> > > > - goto gc_more;
> > > > -
> > > > + if (!stop_gc) {
> > > > + segno = NULL_SEGNO;
> > > > + goto gc_more;
> > > > + }
> > > > stop:
> > > > SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0;
> > > > SIT_I(sbi)->last_victim[FLUSH_DEVICE] = gc_control->victim_segno;