Skip to contentSkip to conversation

f2fs: skip further SIT updates for already invalidated blocks

PATCH Superseded Seongjae Jeong 2 participants 9 messages Oct 4 – Nov 13 F2FS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.

Fixes 81ffbd224e5f ("f2fs: update_sit_entry_for_release() supports consecutive blocks.")
---
 fs/f2fs/segment.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/f2fs/segment.c +4 -1
index 51858f64e43e..fc2f0fd8e318 100644
@@ -2560,7 +2560,10 @@ static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_ent
 			f2fs_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
maintainer
#1 lore raw
Quoted 5 lines · Seongjae Jeong

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.

Quoted 8 lines

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.

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
---
  fs/f2fs/segment.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
fs/f2fs/segment.c +4 -1
index 51858f64e43e..fc2f0fd8e318 100644
@@ -2560,7 +2560,10 @@ static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_ent
  			f2fs_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
#2 lore raw

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?

maintainer
#3 lore raw
Quoted 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
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?

#4 lore raw

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

maintainer
#5 lore raw
Quoted 8 lines · Seongjae Jeong

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?

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

#6 lore raw
Quoted 4 lines

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?

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

maintainer
#7 lore raw

Hi Seongjae,

Quoted 11 lines · Seongjae Jeong
Quoted 4 lines

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?

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);

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_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

#8 lore raw

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 of a66cdd985532.

Understood, thanks for the clarification.
Please keep only the original Fixes tag for 81ffbd224e5f.

Thanks,
Seongjae