f2fs: skip further SIT updates for already invalidated blocks
PATCH 1/1 f2fs: skip further SIT updates for already invalidated blocks
When update_sit_entry_for_release() encounters a block whose bit is already cleared in cur_valid_map, it reports the inconsistency and corrects se->valid_blocks and del. However, it still proceeds with the discard bitmap and checkpoint accounting updates.
This can clear the discard bit unnecessarily and incorrectly decrement the segment and section ckpt_valid_blocks counters when the block is not marked in ckpt_valid_map.
Continue to the next block after correcting se->valid_blocks and del, so that an already invalidated block does not trigger further discard bitmap or checkpoint counter updates.
--- fs/f2fs/segment.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fs/f2fs/segment.c +4 -1
@@ -2560,7 +2560,10 @@ static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_entf2fs_bug_on (sbi,1 ); se->valid_blocks++; del +=1 ; - }else if (unlikely (is_sbi_flag_set (sbi,SBI_CP_DISABLED ))) { +continue ; + } + +if (unlikely (is_sbi_flag_set (sbi,SBI_CP_DISABLED ))) {/* * If checkpoints are off, we must not reuse data that * was used in the previous checkpoint. If it was used
Signature · 1 line
2.53.0
Chao Yu
maintainerQuoted 5 lines · Seongjae Jeong
When
update_sit_entry_for_release()encounters a block whose bit is already cleared incur_valid_map, it reports the inconsistency and correctsse->valid_blocksand del. However, it still proceeds with the discard bitmap and checkpoint accounting updates.
Quoted 8 lines
This can clear the discard bit unnecessarily and incorrectly decrement the segment and section
ckpt_valid_blockscounters when the block is not marked inckpt_valid_map.Continue to the next block after correcting
se->valid_blocksand del, so that an already invalidated block does not trigger further discard bitmap or checkpoint counter updates.
Cc stable.
Quoted 1 line
Fixes 81ffbd224e5f ("f2fs: update_sit_entry_for_release() supports consecutive blocks.")
This commit didn't change previous logic? IIUC.
Otherwise it looks good to me.
Thanks,
Quoted 21 lines
Signed-off-by Seongjae Jeong <[email protected]>--- fs/f2fs/segment.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)fs/f2fs/segment.c +4
-1@@ -2560,7 +2560,10 @@ static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_entf2fs_bug_on (sbi,1 ); se->valid_blocks++; del +=1 ; - }else if (unlikely (is_sbi_flag_set (sbi,SBI_CP_DISABLED ))) { +continue ; + } + +if (unlikely (is_sbi_flag_set (sbi,SBI_CP_DISABLED ))) {/* * If checkpoints are off, we must not reuse data that * was used in the previous checkpoint. If it was used
Hi Chao,
Quoted 5 lines
Cc stable.
Quoted 1 line
Fixes 81ffbd224e5f ("f2fs: update_sit_entry_for_release() supports consecutive blocks.")This commit didn't change previous logic? IIUC.
The discard bitmap handling already existed before this commit. It was introduced
by a66cdd985532 ("f2fs: introduce discard_map for f2fs_trim_fs").
However, 81ffbd224e5f did change the checkpoint accounting. Previously, del was set to 0 when the cur_valid_map bit was already cleared, so se->ckpt_valid_blocks += del left the counter unchanged.
That commit replaced this logic with se->ckpt_valid_blocks -= 1, regardless of the del value.
Thus, the counter is incorrectly decremented.
Would it make sense to add a second Fixes tag for a66cdd985532 to cover the discard bitmap handling?
Chao Yu
maintainerQuoted 5 lines · Seongjae Jeong
Hi Chao,
Quoted 3 lines
Cc stable.
Quoted 1 line
Fixes 81ffbd224e5f ("f2fs: update_sit_entry_for_release() supports consecutive blocks.")
Hi Seongjae,
Oh, sorry, it seems original Fixes line is correct, my bad.
81ffbd224e5f ("f2fs: update_sit_entry_for_release() supports consecutive blocks."):
- if (unlikely(!exist)) {
- f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u", blkaddr); - f2fs_bug_on(sbi, 1); - se->valid_blocks++; - del = 0;
+ if (unlikely(!exist)) {
+ f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u", blkaddr + i); + f2fs_bug_on(sbi, 1); + se->valid_blocks++; + del += 1;
The patch changed from "del = 0" to "del += 1" incorrectly.
- if (!f2fs_test_bit(offset, se->ckpt_valid_map))
- se->ckpt_valid_blocks += del;
+ if (!f2fs_test_bit(offset + i, se->ckpt_valid_map))
+ se->ckpt_valid_blocks -= 1;
+ }
And from "se->ckpt_valid_blocks += del" to "se->ckpt_valid_blocks -= 1".
Let me know if I missed anything. :)
Thanks,
Quoted 16 lines
Quoted 2 lines
This commit didn't change previous logic? IIUC.
The discard bitmap handling already existed before this commit. It was introduced
bya66cdd985532("f2fs: introducediscard_mapforf2fs_trim_fs").However,
81ffbd224e5fdid change the checkpoint accounting. Previously, del was set to 0 when thecur_valid_mapbit was already cleared, sose->ckpt_valid_blocks+= del left the counter unchanged.
That commit replaced this logic withse->ckpt_valid_blocks-= 1, regardless of the del value.
Thus, the counter is incorrectly decremented.Would it make sense to add a second Fixes tag for
a66cdd985532to cover the discard bitmap handling?
Hi Chao,
Thanks for checking.
I agree that 81ffbd224e5f is the right Fixes tag.
I’ve sent v2 with stable on Cc and an additional Fixes tag for a66cdd985532 to cover the discard bitmap handling. Does the second Fixes tag look appropriate to you?
Thanks,
Seongjae
Chao Yu
maintainerQuoted 8 lines · Seongjae Jeong
Hi Chao,
Thanks for checking.
I agree that81ffbd224e5fis the right Fixes tag.I’ve sent v2 with stable on Cc and an additional Fixes tag for
a66cdd985532to cover the discard bitmap handling. Does the second Fixes tag look appropriate to you?
Hi SeongJae,
I don't see how commit a66cdd985532 ("f2fs: introduce discard_map for f2fs_trim_fs") introduce the bug? from the timeline, at that point there is no error handling in update_sit_entries() if sit bitmap is inconsistent, right?
Thanks,
Quoted 3 lines
Thanks,
Seongjae
Quoted 4 lines
I don't see how commit
a66cdd985532("f2fs: introducediscard_mapforf2fs_trim_fs") introduce the bug? from the timeline, at that point there is no error handling inupdate_sit_entries()if sit bitmap is inconsistent, right?
Hi Chao,
Commit a66cdd985532 added the following two lines:
if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
f2fs_bug_on(sbi, 1);
+ if (f2fs_test_and_clear_bit(offset, se->discard_map))
+ sbi->discard_blks++;
Even if the cur_valid_map bit was already clear, the added code could still clear the discard bit and increment discard_blks if the discard bit was set.
Since my patch skips that update for an already invalidated block, I thought a66cdd985532 was an appropriate Fixes tag for the discard bitmap part of the change.
Thanks,
Seongjae
Chao Yu
maintainerHi Seongjae,
Quoted 11 lines · Seongjae Jeong
Quoted 4 lines
I don't see how commit
a66cdd985532("f2fs: introducediscard_mapforf2fs_trim_fs") introduce the bug? from the timeline, at that point there is no error handling inupdate_sit_entries()if sit bitmap is inconsistent, right?Hi Chao,
Commit
a66cdd985532added the following two lines:if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map)) f2fs_bug_on(sbi, 1);
Previously, we will trigger f2fs_bug_on() and there is no further error handling at that point, so that we will update SIT_I(sbi)->written_valid_blocks as well incorrectly, so I don't think this is a bug of a66cdd985532.
Thanks,
Quoted 13 lines
+ if (
f2fs_test_and_clear_bit(offset, se->discard_map))
+ sbi->discard_blks++;Even if the
cur_valid_mapbit was already clear, the added code could still clear the discard bit and incrementdiscard_blksif the discard bit was set.Since my patch skips that update for an already invalidated block, I thought
a66cdd985532was an appropriate Fixes tag for the discard bitmap part of the change.Thanks,
Seongjae
Hi Chao,
Quoted 3 lines
Previously, we will trigger
f2fs_bug_on()and there is no further error handling at that point, so that we will update SIT_I(sbi)->written_valid_blocks as well incorrectly, so I don't think this is a bug ofa66cdd985532.
Understood, thanks for the clarification.
Please keep only the original Fixes tag for 81ffbd224e5f.
Thanks,
Seongjae