From: Daeho Jeong <daeho43@gmail.com>
To: linux-kernel@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com
Cc: Daeho Jeong <daehojeong@google.com>, stable@vger.kernel.org
Subject: [PATCH v2] f2fs: don't run out of free sections in the dry run of resize_fs
Date: Tue, 1 Dec 2026 10:36:49 -0800 [thread overview]
Message-ID: <20261201183649.4162618-1-daeho43@gmail.com> (raw)
From: Daeho Jeong <daehojeong@google.com>
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")
Cc: stable@vger.kernel.org
Signed-off-by: Daeho Jeong <daehojeong@google.com>
---
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/20261116193542.111825-1-daeho43@gmail.com/
fs/f2fs/f2fs.h | 1 +
fs/f2fs/gc.c | 56 +++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 52 insertions(+), 5 deletions(-)
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 58f99474d2d5..4e300e59cc10 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -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 */
diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index c681f3558e85..efa62c733e02 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -2286,6 +2286,14 @@ int f2fs_gc_range(struct f2fs_sb_info *sbi,
if (is_cursec(sbi, GET_SEC_FROM_SEG(sbi, segno)))
goto next;
+ /*
+ * Checkpoint is blocked in the dry run of resize, so the space
+ * freed in the meantime can't be reused. Stop before free
+ * sections run out, and let the caller migrate the rest later.
+ */
+ if (dry_run && !lock && has_not_enough_free_secs(sbi, 0, 0))
+ break;
+
do_garbage_collect(sbi, segno, &gc_list, FG_GC, true, false);
put_gc_inode(&gc_list);
@@ -2456,15 +2464,31 @@ static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs)
}
}
+/* Count the valid blocks in the last @secs sections. */
+static block_t f2fs_valid_blocks_in_tail(struct f2fs_sb_info *sbi,
+ unsigned int secs)
+{
+ unsigned int secno;
+ block_t valid = 0;
+
+ for (secno = MAIN_SECS(sbi) - secs; secno < MAIN_SECS(sbi); secno++)
+ valid += get_valid_blocks(sbi, GET_SEG_FROM_SEC(sbi, secno),
+ true);
+ return valid;
+}
+
int f2fs_resize_fs(struct file *filp, __u64 block_count)
{
struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
__u64 old_block_count, shrunk_blocks;
struct cp_control cpc = { CP_RESIZE, 0, 0, 0 };
+ struct cp_control sync_cpc = { __get_cp_reason(sbi), 0, 0, 0 };
struct f2fs_lock_context lc;
struct f2fs_lock_context glc;
struct f2fs_lock_context clc;
unsigned int secs, old_tail;
+ block_t valid, prev_valid;
+ unsigned long timeout;
int err = 0;
__u32 rem;
@@ -2512,9 +2536,6 @@ int f2fs_resize_fs(struct file *filp, __u64 block_count)
goto out_drop_write;
}
- /* stop CP to protect MAIN_SEC in free_segment_range */
- f2fs_lock_op(sbi, &lc);
-
spin_lock(&sbi->stat_lock);
if (shrunk_blocks + valid_user_blocks(sbi) +
sbi->current_reserved_blocks + sbi->unusable_block_count +
@@ -2525,10 +2546,35 @@ int f2fs_resize_fs(struct file *filp, __u64 block_count)
if (err)
goto out_unlock;
- err = free_segment_range(sbi, secs, true);
+ /*
+ * Migrate the blocks in the range to be shrunk before freeze_super().
+ * Checkpoint is blocked in the dry run, so the dry run stops before
+ * free sections run out. A checkpoint makes the space freed so far
+ * reusable: write one first, and repeat as long as the range gets
+ * emptier. gc_lock keeps other checkpoints waiting meanwhile, so don't
+ * start another round after DEF_RESIZE_DRY_RUN_TIMEOUT. The rest is
+ * migrated after freeze_super().
+ */
+ timeout = jiffies + DEF_RESIZE_DRY_RUN_TIMEOUT * HZ;
+ valid = f2fs_valid_blocks_in_tail(sbi, secs);
+ do {
+ prev_valid = valid;
+
+ stat_inc_cp_call_count(sbi, TOTAL_CALL);
+ err = f2fs_write_checkpoint(sbi, &sync_cpc);
+ if (err)
+ break;
+
+ /* stop CP to protect MAIN_SEC in free_segment_range */
+ f2fs_lock_op(sbi, &lc);
+ err = free_segment_range(sbi, secs, true);
+ f2fs_unlock_op(sbi, &lc);
+
+ valid = f2fs_valid_blocks_in_tail(sbi, secs);
+ } while (!err && valid && valid < prev_valid &&
+ time_before(jiffies, timeout));
out_unlock:
- f2fs_unlock_op(sbi, &lc);
f2fs_up_write_trace(&sbi->gc_lock, &glc);
out_drop_write:
mnt_drop_write_file(filp);
--
2.56.1.987.g2ec0ab4260-goog
reply other threads:[~2026-12-01 18:37 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201183649.4162618-1-daeho43@gmail.com \
--to=daeho43@gmail.com \
--cc=daehojeong@google.com \
--cc=kernel-team@android.com \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox