From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796150221; b=5L30AjbGJafN63L9telgDk16Z5r6/Q6Bm5O0Ddnupikba3uPtikYR5yA7IJ65TJRLBkm +zNM6gV22VcJ9C8pY9CMhkqF9N95jY+wkX841WAkOMlJyoNvlg+QAmror1yj3Ln80+S8m oelGErUFe1CKnwQysJJ3EH92tJYq0s0EJzEdjWdJ24M0SSJayBuLG1urYE3BSbbaHouS/ EHR1V09JWTHx2RJDyQFtZnvb2+R+LqYsob39K77H++n05RnPEQQ2n9vF1/UMkN5zDSXE9 MtZNi+OHxmS+OvK80ix2+UqkVN0LA5SUJVbzXHZvOOSCqtt1tbrDWtpnJtIf6rdlW3A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796150221; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=lzOn4KlJ2Izeh4Cys2S5DEJk/dEveIQW9efqRC0Utvo=; b=sP9grUCjPcmfhLN8kgDy744dKvg2WA2lq8vGaPOpsY2M/zBUwpEpFquAw3R9EibWBi2x g7a6wUI8OnRs2JDk9EQcH4oyjD9OFR/GMmuecNPJIYk+dMJOqOF5yof45Vj3zJPZf14E2 cGr1PQ5VW/ntRap284mN+YwEW7TIVIzct9WUvD1jyCPeOrvC6Km0pZqA/KcrZQaAxu7q5 1tFBNY2bNcDzLrblPgGEnjGTP+Kjvrk0xBdkP/eiDnzHkSC1Gypqz7tojT/VvUxphR23u 7JVY6qBpRJtB9Nn8hWrsC/H+bYAW8+drG+aC8CoGg9WoV2HXH1jKYReJhW4qkSu0NBg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=cFho76ml; arc=none smtp.remote-ip=209.85.214.169 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B75ED606A325 for ; Tue, 1 Dec 2026 18:37:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B75ED606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=cFho76ml Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2f01f296937so90638055ad.3 for ; Tue, 01 Dec 2026 10:37:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796150220; x=1796755020; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=lzOn4KlJ2Izeh4Cys2S5DEJk/dEveIQW9efqRC0Utvo=; b=cFho76mlyLLstdN8EXwxlr1/jZAiLEf0emFFwJdDGC7U0eR3oyWUMJUP99hNNaBpH0 v8vkTrnOTW/JweAG6SLK+LoiXlwjS8eH4KRvXcr3LDNoBEOAh0nDoG/7/SBtX37SHVGG 7RHGn6JHdjrP2bkzEmEFAJ16d035+0YGbXTzdRwos+GANwNsweRYzLbeovrr3YivOThe pZzkBxB5MhxkrRFdQnH6Cixg7IdQEEkH5rmz5dxOC6cbgiNVeZFDI6DCajJtoYoN4nEX rlkntSG7qDs3hqlDIOdXzkxJJfd/DjW3p+VoKtgUzSyfyobSVLNxJ0Qisddrffo9vbYq 5n2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796150220; x=1796755020; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=lzOn4KlJ2Izeh4Cys2S5DEJk/dEveIQW9efqRC0Utvo=; b=UIrkUn2tigy+1Q4LCGUsMzedyo1dCv1Hli2GLZQcSzdE5jSo8zZHDQpHKcy+ZFjDro 3p6gk5une88b1LZxdhlXE+fWke6aVJXniWEUXqQR4LiyLayMaubxZaQbc6akKi8nVOj4 pVr1aZv9rcZ2LxtSzJhZSzYOg7r+TyKBPBh+xA1GJXEkIgvxK3ObDr1hRiPZLPWmKPbZ mlGzakdwWc7rLlNd+cst63chL4WNy148txdWDgIy4qoPyPyx9PxfWjyKf0HusmStwXzc ekW5H2fIxPZza4c64E6cb6RirFFx+XTvthLiaZYs94t9zbDdiTfB26AkPnDf7sxZ3iiH 6/BQ== X-Gm-Message-State: APuU4Z8+9en2RaIJHOUL5G35psu0qFS3uBeuSuFiM0SORK8Sxdkc1dF2 QFhb8eicAZ/S6GEUwb8eEXbiQmvO20+aLBjOITcexPMm9Zw2I/VaIJ+vquoi1g== X-Gm-Gg: AZFdTZfvqmMTsMFu8Ds53bRMyddPlRXLfqJZFFRtd4M7b6hsSrCTtqRqAUFtnVTkg22 kU3Boqp/yll0uuDcWUy9ejgVCYETYaRSQ4BY/Czc83rHDhUzhsg526pPXOXXVDdeZCrZk5SOt+U +dF3AJO8Sw1l7rIedDHjMLQ1iMbiJFiVIqF9upy36J8F3cSBpzjqagY5r7HzucMBGfwZ9MZtyS4 cmIVJ5rkHZcMz+klANArEKS0B/zo6LzhT0iYdFekkidiy8o/2ZSpaZgDS0mqqL22codhsa1hy2t +3XkIU8HYpMB1Xn0svtMO6uoJW8Vahm88bf/28+L9QKV0K1Z4cihfZOWTm5ultsZdGUP2u1Rnf7 cJb7WfHYmSbLjpyupdgVv/qzsL0OJqRSj+s908cIUp/1I4VlMLG4GdB/iB6E4VudVi3KHTxhWo/ W487VZstkwgSRmtQ2mSjlwPhRYKC04E4vAz4Qb6KDVH2sGLENl1JRYDGCbK3DpaobeG7hfCcgoW whsu1kSMYCD2Wa1KtoY6M68ANiBiUfuDn/YL4SWC7sfDuVyLf/UpzyPlTkkMl4j+g0zLNO2EtWT FFApOJsElgDJMoUeWfaBDNfER+F9l0yrQ95dxqHdMCBAsaWaSTkoCXTUw4a0gYrLaka6L7tJFa6 dH5hW6kJ5aln9yWzNDrF95a3iS2qWgz6XOPVb/qBdFvUSLA5wFwd/Mx+Osom6Gu3ClXg= X-Received: by 2002:a17:902:ea01:b0:2e9:8ef0:cf6c with SMTP id d9443c01a7336-2f03f1ed55dmr400169835ad.26.1796150219555; Tue, 01 Dec 2026 10:36:59 -0800 (PST) Received: from f2fs-test.c.googlers.com.com (193.67.125.34.bc.googleusercontent.com. [34.125.67.193]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f03f120769sm189573835ad.57.2026.12.01.10.36.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 10:36:58 -0800 (PST) From: Daeho Jeong To: linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com Cc: Daeho Jeong , 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 Message-ID: <20261201183649.4162618-1-daeho43@gmail.com> X-Mailer: git-send-email 2.56.1.987.g2ec0ab4260-goog X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. 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 --- 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 From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 651B6606A320 for ; Tue, 1 Dec 2026 18:37:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=lists.sourceforge.net header.i=@lists.sourceforge.net header.a=rsa-sha256 header.s=beta header.b=L4CVuhCZ; dkim=fail reason="signature verification failed" (1024-bit key, unprotected) header.d=sourceforge.net header.i=@sourceforge.net header.a=rsa-sha256 header.s=x header.b=PYoTJM6W; dkim=fail reason="signature verification failed" (1024-bit key, unprotected) header.d=sf.net header.i=@sf.net header.a=rsa-sha256 header.s=x header.b=HnK8x4KY; dkim=neutral header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=bnIEvNfS DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type:Cc: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:Message-ID:Date:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Unsubscribe-Post:List-Owner; bh=x+k7a6Dsnt1JVzZldhrCCbPok5kDiQDVELqhmp3Vf1o=; b=L4CVuhCZZGbSjZ8LAYv6SblGMs 7wdX7utg4l24PfoX1CY/5mK6pFuYG4cr7eQPBirysrdQBBqYuYlU5Bs7OGc3e+ag5jrIDgmWd13bF Yw9ouKWXF/ZdfyUfPalArEes5wwlQNxAqQy+QSO2iGVfp05puZX0n+8Z7uUnZWib7Zg0=; Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.100.1) (envelope-from ) id 1xYSix-000000006qX-36KS; Tue, 01 Dec 2026 18:37:07 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.100.1) (envelope-from ) id 1xYSiw-000000006qQ-0leC for linux-f2fs-devel@lists.sourceforge.net; Tue, 01 Dec 2026 18:37:06 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Unsubscribe-Post:List-Subscribe:List-Post:List-Owner:List-Archive; bh=lzOn4KlJ2Izeh4Cys2S5DEJk/dEveIQW9efqRC0Utvo=; b=PYoTJM6Wxa/FI8pZ5y5pCmvYI+ OxA9H5YJq2M9377D6pnIyeslK4kAIeWyzmAN3rvYTxmXrVNka1dAGr/Syees7tYno/bWfzh/qUFUS afO87iWATnHS4FQkoX7B5V+msDMzrHzGvMNBlaliQyyh+dI6izO20U3Sav1d8D/xqqIU=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject:Cc:To:From :Sender:Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Unsubscribe-Post: List-Subscribe:List-Post:List-Owner:List-Archive; bh=lzOn4KlJ2Izeh4Cys2S5DEJk/dEveIQW9efqRC0Utvo=; b=HnK8x4KYgsFsSx1wLZdwgzRmum PpG8B4ghuqTtgC9ydSHUj0ZNkJnG+mno7gVdvbfpmQ01XI1jz4Tf6bG6C69v/+tPxZyhLDT8n8Xzr oGspPyZJ6+ea2nPEY3JS8GVlDcZrqzrt4UXaibNLgImiMVPQNmsGh/DBvYGGtYr6+lTE=; Received: from mail-pl1-f181.google.com ([209.85.214.181]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.100.1) id 1xYSiv-000000005Ri-1p7o for linux-f2fs-devel@lists.sourceforge.net; Tue, 01 Dec 2026 18:37:06 +0000 Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2f037bc348dso94710075ad.0 for ; Tue, 01 Dec 2026 10:37:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796150220; x=1796755020; darn=lists.sourceforge.net; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=lzOn4KlJ2Izeh4Cys2S5DEJk/dEveIQW9efqRC0Utvo=; b=bnIEvNfS7l/exikgN1K+vVxH0Ln4DLZdiYKdywPJxZ8nRomMMkPc5ArKXqz/ESLlPB fYu6xtHUcqUnKzWSabP1+/ZB6t9KEjzcN/C69Lh+j8D0qh8TdxsXiumcpXINoBByvD+s nhuArpYnduWl/8xPx9rYn2jlbW7XLmhlRN3+FfJITPoy5EQ/4BfpiSr61VyUuJ580sNu Zz7/+6bfJNob1kgfQiC1mXtF8+RDb0Hwiy/MTJFtB+ZHXjX+ZxuzylVjIJ6YZfFgDEfs vpey7t8q2KZYqwb5bReLPNjhkbZiQtx0BK8N2PpcMT/9I2KHePy5WGj10/vhVjgPlCm/ 6EqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796150220; x=1796755020; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=lzOn4KlJ2Izeh4Cys2S5DEJk/dEveIQW9efqRC0Utvo=; b=Q6JTUcYMb43iTtqQ9eHtVSAvotqYUiSLndz5KVs+nDt0eo1ckJeQCfZ8YqZkZT6CEx ClEGhxgb3+aUAlr6PgILyOrmWIsjrLjNKk0yhMZHSSSOmdm7GUqjFkfOlT1CbmC1nHpa tmwaXBB27B/P2pipIBvSE3rSfjbAesYAXswUFE1jHQmkUCaWgZteWWYo3t+aqT3pTXyW 8/yoka8uMFYJnVvJjSOLgUbRm2vs0MzVFTxdNLi2f8x63XSvEXRzJ2J+2YqH79OuO6sN nrYGcHfug/T/0Yfw6PZTSCLBuk9ksI2abmK3yC8UXM8iIP0xf8S4MpIQMMs52YTJUKuV PvTw== X-Forwarded-Encrypted: i=1; AL+hErGz6vAiQO6N6jYnhRh6ru7F0ToFLzPUgbwxo3nslj8JbeQQJbUqbsDyrYVc4BVznWM+CJOpYu/FkV9cqHF0Eqzz@lists.sourceforge.net X-Gm-Message-State: APuU4Z8dBJTWkqA3Tij4bMlkwDfHFQPW9pzWgpEv20tc3LuSPMDaVqrx RW7VF1ieJmz95MVKAl8b2oubESpHlvmpSs/ZNhPHhCn6tW2G2dAjR2do X-Gm-Gg: AZFdTZcSMrgVbka4P6xwkwlkoSJIC33Xe73YkOUOrC6vP7O/8neHncN2cjcv6lfgNI/ UGGQBBb0KjYYay7o4J1EmvK03lvcLtY5Pdu8LPMbL2G9mGPxDoifRVmwrQefnlBhe1A4sAcCWHS uVteI3VXG4pDKuXTokSQUKR9N53jLwoJLHpjXn+O6pVHjnukVeydBifJUc7hsDOzQJKd9mwiVXD 6s+2AiO7DFHjPrXx6rbOKvdLxGhCyAavpHaxEAItyQaHqNHSbDZWK3Fz9EigWMof1G8pK2mICWN j42kJJ2tMVusl5w6GBNVCiXj8MHCTk6ow3+OuPpYNwwrFv4YCXMsO5RS6bexPTBXfJAD1vjEgux JEvP4XfIPRt//C5/wxkQGEZV2S3TL4k9sGbxd2cVIcPtP0QJAhFVeNU8Py7mb0s23Tq9/S6fInq enomUDPUIyGlc7fhLH+rOzqrYPCSFhPgGcm+U0Qlr6kFzaaczLLwymxuuOms9yItFm9+3c2926c Wp7Zsz6q3Z2gXPNw0G9yar6Pl4Xh+U5ghXadLS7fofb2fKZUwLZ7JQqibceCNjMpehoMIDZeueh 6Xj141wacqyW0R/HWKSfbXogX4B1blvaK73v8js8U6Uxcr3XjvNpzdHwA6eMza/84MgK9VzlisG 8kSY7xbSoRjXWDj7mk5d9NVnZYdY3drlJLKRwnKPu3bI6CsErE8cEg6xFv5fSukA0ASE= X-Received: by 2002:a17:902:ea01:b0:2e9:8ef0:cf6c with SMTP id d9443c01a7336-2f03f1ed55dmr400169835ad.26.1796150219555; Tue, 01 Dec 2026 10:36:59 -0800 (PST) Received: from f2fs-test.c.googlers.com.com (193.67.125.34.bc.googleusercontent.com. [34.125.67.193]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f03f120769sm189573835ad.57.2026.12.01.10.36.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 10:36:58 -0800 (PST) From: Daeho Jeong To: linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com Date: Tue, 1 Dec 2026 10:36:49 -0800 Message-ID: <20261201183649.4162618-1-daeho43@gmail.com> X-Mailer: git-send-email 2.56.1.987.g2ec0ab4260-goog X-Archived-By: MIME-Version: 1.0 X-Spam-Score: 1.1 (+) X-Headers-End: 1xYSiv-000000005Ri-1p7o Subject: [f2fs-dev] [PATCH v2] f2fs: don't run out of free sections in the dry run of resize_fs X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Daeho Jeong , stable@vger.kernel.org Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.9 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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. 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 --- 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 _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel