Re: [PATCH v3] f2fs: fix missing discard candidates in fstrim

From: Chao Yu
Date: Wed Mar 12 2025 - 07:29:01 EST


On 3/12/25 18:20, Chunhai Guo wrote:
> fstrim may miss candidates that need to be discarded, as shown in the
> examples below.
>
> The root cause is that when cpc->reason is set with CP_DISCARD,
> add_discard_addrs() expects that ckpt_valid_map and cur_valid_map have
> been synced by seg_info_to_raw_sit() [1], and it tries to find the
> candidates based on ckpt_valid_map and discard_map. However,
> seg_info_to_raw_sit() does not actually run before
> f2fs_exist_trim_candidates(), resulting in the failure.
>
> The code logic can be simplified for all cases by finding all the
> discard blocks based only on discard_map. This might result in more
> discard blocks being sent for the segment during the first checkpoint
> after mounting, which were originally expected to be sent only in
> fstrim. Regardless, these discard blocks should eventually be sent, and
> the simplified code makes sense in this context.
>
> root# cp testfile /f2fs_mountpoint
>
> root# f2fs_io fiemap 0 1 /f2fs_mountpoint/testfile
> Fiemap: offset = 0 len = 1
> logical addr. physical addr. length flags
> 0 0000000000000000 0000000406a00000 000000003d800000 00001000
>
> root# rm /f2fs_mountpoint/testfile
>
> root# fstrim -v -o 0x406a00000 -l 1024M /f2fs_mountpoint -- no candidate is found
> /f2fs_mountpoint: 0 B (0 bytes) trimmed
>
> Relevant code process of the root cause:
> f2fs_trim_fs()
> f2fs_write_checkpoint()
> ...
> if (cpc->reason & CP_DISCARD) {
> if (!f2fs_exist_trim_candidates(sbi, cpc)) {
> unblock_operations(sbi);
> goto out; // No candidates are found here, and it exits.
> }
> ...
> }
>
> [1] Please refer to commit d7bc2484b8d4 ("f2fs: fix small discards not
> to issue redundantly") for the relationship between
> seg_info_to_raw_sit() and add_discard_addrs().
>
> Fixes: 25290fa5591d ("f2fs: return fs_trim if there is no candidate")
> Signed-off-by: Chunhai Guo <guochunhai@xxxxxxxx>

Reviewed-by: Chao Yu <chao@xxxxxxxxxx>

To Jaegeuk, I'm fine w/ this change, but discard is critical, could you
please double check it?

Thanks,