From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796117037; b=V1LIMUThhPygruwNazqUtY6fSAMaLq1P+2YAeH89d7DggwePNivqdFOfkWHnWugtVL5C m+L8XttroosbWG7TcmEJB5hKt79BXCBEVOxCXcLRP6V8YMMaHDYO0BbiS1GgkOPRtV89S WaGgifwm4ysVaXh94AFaAal8BjVk6yrNpkzcehZG2F8otrfjopHqpqKeT72lc4snJvvAy DKv0vF0yFk1LOQVwMhUIu69OI1ICxIM//crDo2e0W/LieRumZ8XGvLQY0vayIiKOOpHMc K53KMpRq0rhgf5CZR+0y5nB3MEzdZb47Bkn/F87yjh18V6VG37RNlNUg9gxo2WFQtFw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796117037; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent:Cc: Subject:To:References:Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=4jVJFN1IOa/Twh5v5uQrHkb+1hct7M6pgE1ODM6w5X0=; b=oFxZVIvHnJIPxv7lUB39Ql1E3Lfgoh8FRBzK029t9Tfhb7aO4iqOHSYc+90lfsdf8UuQ CqrMmJbrq3L5vxzi22WWiE+9DsGk4S8WsS3kyDsM8ZOkRpagQqr52hVbkGYGEHl3nQ9Qa NaUEbUsSU5YOXt/Rj+iHkD480mmapcKHBq3qAB8MfJwbJmHyFkx3Gf2tN/Uqe3Iyh2KZt 0MO7VF60vn8+g0A91GTBSExbQj9kawGZr7plRvhRiI5KsvFPNUVCrmBMiISuq//1HRDcl KHuEiMMCIB5ETWLADQ9LzmKCfsq9LqF9CgKJgzkZjTukYzwveWqI6/ypze1g/OLLluw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="gbh+GB/N"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEC64606A31A; Tue, 1 Dec 2026 09:23:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=gbh+GB/N Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2E81A1F00897; Tue, 1 Dec 2026 09:23:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796117037; bh=4jVJFN1IOa/Twh5v5uQrHkb+1hct7M6pgE1ODM6w5X0=; h=Date:Cc:Subject:To:References:From:In-Reply-To; b=gbh+GB/NmlyUCsPziVZ/S/MLVfhe1UVuLl4+8MUpW9cOrSOW6v8vcJ6W3PWB955Eg xXtSu9Kt8wUg5SAgWnjKjazVSHvnyM3XonTrvB+1+rz2LrXNyTui88xcQltZmgOlqU mxy6SjbEUj4yIzHcQ+/OeiT7uQyyivtAbcWstQDn12FgXUZDcIYCuepuY4waIdAFWD Qz4m1FyiJxOC8Raxnaw1MeQPzju5nMC2Wy8d9NNP8P6cuiBRihzdOArQieOmyZRrz9 0Gv+ZfmzwMKmmc84U5X3w4gC8W73Kfa+8wsfDqeG9v3C0syy0og0E67hFg5I6Pivv0 mfZuh4piSVTQg== Message-ID: <007d482a-b02a-42d1-8408-0f1cb4cd4628@kernel.org> Date: Tue, 1 Dec 2026 17:23:54 +0800 X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: chao@kernel.org, Daeho Jeong , stable@vger.kernel.org Subject: Re: [f2fs-dev] [PATCH] f2fs: don't run out of free sections in the dry run of resize_fs To: Daeho Jeong , linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com References: <20261116193542.111825-1-daeho43@gmail.com> Content-Language: en-US From: Chao Yu In-Reply-To: <20261116193542.111825-1-daeho43@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/17/26 03:35, Daeho Jeong wrote: > From: Daeho Jeong > > 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. 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 3 of 4 runs on a 4KB page kernel, and in none of > 4 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 > --- > This applies on top of "f2fs: reserve space to be shrunk during resize_fs" > (v3), which changes the same function. > https://lore.kernel.org/linux-f2fs-devel/20261114011807.3811774-1-daeho43@gmail.com/ > > fs/f2fs/gc.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- > 1 file changed, 46 insertions(+), 5 deletions(-) > > diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c > index 7609c23ec27b..2d2cc3417c36 100644 > --- a/fs/f2fs/gc.c > +++ b/fs/f2fs/gc.c > @@ -2241,6 +2241,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); > > @@ -2411,15 +2419,30 @@ 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; > bool reserved = false; > int err = 0; > __u32 rem; > @@ -2468,9 +2491,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) > > __get_available_block_count(sbi, false)) { > @@ -2488,10 +2508,31 @@ 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. The rest is migrated after freeze_super(). > + */ > + 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); Do we need to add a timeout like we did in f2fs_disable_checkpoint() to avoid long time loop during resize_fs? since it may block other checkpoint by gc_lock. Thanks, > > out_unlock: > - f2fs_unlock_op(sbi, &lc); > f2fs_up_write_trace(&sbi->gc_lock, &glc); > out_drop_write: > mnt_drop_write_file(filp);