Re: [PATCH] f2fs: fix missing discard for active segments

From: Chunhai Guo
Date: Thu Jan 02 2025 - 05:53:03 EST


在 12/3/2024 2:51 PM, Chunhai Guo 写道:

Dear Chao & Jaegeuk,

Could you please help review this patch? Currently, this issue is quite
easy to reproduce in some projects.

Thanks,

> locate_dirty_segment() does not set any current active segment as a
> prefree segment. Thus, the issue described below may occur:
>
> Step 1: During a checkpoint, add_discard_addrs() does not handle the
> current active 'segment X' with 0 valid blocks (and non-zero discard
> blocks). As a result, no struct discard_cmd is created for 'segment X'
> and the value of sbi->discard_blks cannot be reduced to 0 after the
> checkpoint.
>
> Step 2: f2fs is umounted without setting CP_TRIMMED_FLAG, as
> sbi->discard_blks is non-zero.
>
> Since add_discard_addrs() can handle active segments with non-zero valid
> blocks, it is reasonable to fix this issue by also handling active
> segments with 0 valid blocks in add_discard_addrs().
>
> Signed-off-by: Chunhai Guo <guochunhai@xxxxxxxx>
> ---
> fs/f2fs/segment.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> index eade36c5ef13..4fb1dc4aab97 100644
> --- a/fs/f2fs/segment.c
> +++ b/fs/f2fs/segment.c
> @@ -2090,7 +2090,9 @@ static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
> return false;
>
> if (!force) {
> - if (!f2fs_realtime_discard_enable(sbi) || !se->valid_blocks ||
> + if (!f2fs_realtime_discard_enable(sbi) ||
> + (!se->valid_blocks &&
> + !IS_CURSEG(sbi, cpc->trim_start)) ||
> SM_I(sbi)->dcc_info->nr_discards >=
> SM_I(sbi)->dcc_info->max_discards)
> return false;