Skip to contentSkip to conversation

f2fs: don't run out of free sections in the dry run of resize_fs

PATCH Applied Daeho Jeong 3 participants 3 messages Dec 1 – Dec 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 f2fs: don't run out of free sections in the dry run of resize_fs

From: Daeho Jeong <[email protected]>

f2fs_resize_fs() migrates the blocks in the range to be shrunk in a dry run before freeze_super(). Checkpoint is blocked there to protect MAIN_SECS, so the space freed in the meantime can't be reused, while GC and other writers keep using free sections in the remaining area. On a busy filesystem, free sections can run out and we hit the below.

kernel BUG at fs/f2fs/segment.c:3020!

 Call trace:
  new_curseg+0x9dc/0xa50
  f2fs_allocate_data_block+0xa2c/0xcb8
  do_write_block+0xd4/0x2b4
  f2fs_outplace_write_data+0x78/0xcc
  f2fs_do_write_data_page+0x564/0x69c
  do_garbage_collect+0x1668/0x21ec
  f2fs_gc_range+0x274/0x41c
  free_segment_range+0x290/0x33c
  f2fs_resize_fs+0x200/0x530

Without CONFIG_F2FS_CHECK_FS, checkpoint is stopped by STOP_CP_REASON_NO_SEGMENT instead, and writes fail with -EIO.

Stop the dry run before free sections run out. To migrate as much as possible before freeze_super(), write a checkpoint before the dry run, and repeat them as long as the valid blocks in the range decrease. As gc_lock is held meanwhile and keeps other checkpoints waiting, don't start another round after 5 seconds. The rest, if any, is migrated after freeze_super() as before, where no one else allocates blocks.

Shrinking a 4GB filesystem down to 4MB of free space with two writers running hit the BUG in 5 of 12 runs on a 4KB page kernel, and in none of 6 runs with this patch.

Fixes b4b10061ef98 ("f2fs: refactor resize_fs to avoid meta updates in progress")

---
v2:
- don't start another dry run once 5 seconds have passed (DEF_RESIZE_DRY_RUN_TIMEOUT), as Chao suggested, since gc_lock keeps other checkpoints waiting during the loop. The test numbers are from this version on the dev branch.
- based on dev, without "f2fs: reserve space to be shrunk during resize_fs", like the version in dev-test.

v1: https://lore.kernel.org/linux-f2fs-devel/[email protected]/

 fs/f2fs/f2fs.h |  1 +
 fs/f2fs/gc.c   | 56 +++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 52 insertions(+), 5 deletions(-)
fs/f2fs/f2fs.h +1 -0
index 58f99474d2d5..4e300e59cc10 100644
@@ -327,6 +327,7 @@ enum {
 #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
 #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
 #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
+#define DEF_RESIZE_DRY_RUN_TIMEOUT	5	/* 5 secs */
 
 enum cp_time {
 	CP_TIME_START,		/* begin */
fs/f2fs/gc.c +51 -5

91 lines not on this page · raw message

Signature · 1 line
2.56.1.987.g2ec0ab4260-goog
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 30 lines · Daeho Jeong

From: Daeho Jeong <[email protected]>

f2fs_resize_fs() migrates the blocks in the range to be shrunk in a dry run before freeze_super(). Checkpoint is blocked there to protect MAIN_SECS, so the space freed in the meantime can't be reused, while GC and other writers keep using free sections in the remaining area. On a busy filesystem, free sections can run out and we hit the below.

kernel BUG at fs/f2fs/segment.c:3020!

 Call trace:
  new_curseg+0x9dc/0xa50
  f2fs_allocate_data_block+0xa2c/0xcb8
  do_write_block+0xd4/0x2b4
  f2fs_outplace_write_data+0x78/0xcc
  f2fs_do_write_data_page+0x564/0x69c
  do_garbage_collect+0x1668/0x21ec
  f2fs_gc_range+0x274/0x41c
  free_segment_range+0x290/0x33c
  f2fs_resize_fs+0x200/0x530

Without CONFIG_F2FS_CHECK_FS, checkpoint is stopped by STOP_CP_REASON_NO_SEGMENT instead, and writes fail with -EIO.

Stop the dry run before free sections run out. To migrate as much as possible before freeze_super(), write a checkpoint before the dry run, and repeat them as long as the valid blocks in the range decrease. As gc_lock is held meanwhile and keeps other checkpoints waiting, don't start another round after 5 seconds. The rest, if any, is migrated after freeze_super() as before, where no one else allocates blocks.

Quoted 7 lines

Shrinking a 4GB filesystem down to 4MB of free space with two writers running hit the BUG in 5 of 12 runs on a 4KB page kernel, and in none of 6 runs with this patch.

Fixes b4b10061ef98 ("f2fs: refactor resize_fs to avoid meta updates in progress")

Thanks,

#2 lore raw
In reply to Daeho Jeong
This patch was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:

Hello:

This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:

Quoted 9 lines · On Tue, 1 Dec 2026 10:36:49 -0800 you wrote:

From: Daeho Jeong <[email protected]>

f2fs_resize_fs() migrates the blocks in the range to be shrunk in a dry run before freeze_super(). Checkpoint is blocked there to protect MAIN_SECS, so the space freed in the meantime can't be reused, while GC and other writers keep using free sections in the remaining area. On a busy filesystem, free sections can run out and we hit the below.

[...]

Here is the summary with links:
- [f2fs-dev,v2] f2fs: don't run out of free sections in the dry run of resize_fs

    https://git.kernel.org/jaegeuk/f2fs/c/029b14ff22f2

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html