From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4AF342E8FA for ; Tue, 18 Aug 2026 17:14:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787073259; cv=none; b=tugANIyC7tQeZ6kTmBbChUxO3dw9WQI82+laYgnvAU3RQTt5oROTnERVzKni32iMo60FyWMy6SSsnuKpwPKaciWQBht1zXMY/l1qtiS9JMqtD2gRYcomUAHymJQEFnxxRPlkpQfxt0WHNyYhr6nQH4we6LN2lGl5BChEfRuC6yc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787073259; c=relaxed/simple; bh=1+yrLxKFrksVPxn44XjNPm4lv7NnTsMdhGarsIQp2w8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=JJ9UA5/ncuFOpPE/bgOeNhY1WjgfG7OyPNPfw6f/gvsRIy5pI2RyiFRnMvPa4ix5jSTuKPS4fDpXfnbk0ZUbzIsIsu0u58sB2f+AP3BrZhlZ6aYfJGqt7LTlWn2iy5bkBctwfLaqJSw/jHRafth6wBdYMVhYhw8WyouHYZsuk6E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=s9elZnY5; arc=none smtp.client-ip=209.85.215.178 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.b="s9elZnY5" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-c9ef3e1337fso16707a12.2 for ; Tue, 18 Aug 2026 10:14:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787073257; x=1787678057; 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=6uxCYjzk6Sw3PcKM0MHXuF6QyCEyzVfr72+hK3fcU0U=; b=s9elZnY5b7+619Q3oGat99TO95T6dPXNRLk09QdvitNIcMOLS1oIo9mLx+OOSOa6Ak IL4Zd5NFNm/D1xEzqmrVPyBZ0OMzswqvrB9rqNz2sdmDg/wIwAQZodEYSbXUdvyZ5snZ 54XoskguU/Kx0mEQ3ziE27IAgxkpakp1VQj4T17Lhhl1cBdYC80rT/VYpkiVpzn2YzB+ hOcQBMkJM3m8LutPfdmSYVtxgQN6WPx5JA4qMPMaYAVrMy+uyX9H9x5WcH/t0M1J3SuG SIGvN6pKw39GWN+5HF9prGRDrxeflVkQx8gov1e2/KX4JTi+Awx9YrwhxRKPdvEJ5WsJ lSAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787073257; x=1787678057; 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=6uxCYjzk6Sw3PcKM0MHXuF6QyCEyzVfr72+hK3fcU0U=; b=WG3IrrBq9vSsFF8+UkQzW8B090A38f3O732vloUx85om+KmeFolM/mwvj5zp+0ppmh 3746M5pQ2gWFLLvuOzsuIO4FWLq7JSdY+B1D/d1qv52mPJSF49BiXeUWd5uc3hK7vkQ1 9cl3fZbL6EGeWJou5ujA5vpluJTkT8gxMsRO3MSqNLrLXpSoW7kS3+zWFT+oNi1sDWws YjGI6yyZ6XF66gDEaV5lB+QpqFoFsFnfeCS0szwImVjwZwJzf/dqqcmA8550/JSDWd1J nkWoPchM/mjTfcCxAe1hPouu8pyiiWlZr8qpIQjKoJFz5RW28HySAkNpj4V4ZG29STBb 4V2Q== X-Gm-Message-State: AOJu0YyOcXex2GSsGS19ZLSKCu5U6/qj4Hti7SOXb6lPHQKcEtGI+ulQ V6FpoalhC38GJcda4DxKJBgz3gXfNqOwZHO/yZKlfDw+OCzEludA8eIh6jbVZw== X-Gm-Gg: AR+sD11p13ga8NoCmrEkBpK5vhRjO26J6/+C22idE0gwCjHIlIKR96jlhNhOF01cexu zyZ4QGOly6/YBXbgBznb4NJCmDCZsKtiHUZcvClA9peWGiI0LrrwQaYWk5iCfYwROkn5dLtTgDX oM5TOAfYh7EGPCyZB/nEH/PGBpk2CHOWGyr/TMoyhK3lDnKUMCsh0cSvnlIpXf26cHJD577MNjf QWTPDVmWpAd4mXf5YVgvINm3RKAlV4DK129EpFvZKt+HdmHg2mK8/HZ5qC2LEzPsjWiaz/Hgosw N0lQRbK+szU34z/eylkcQsmctVQ6DvNiQvAPws+Li1i9OVxGvrGR72AFehVQg0Y8XNO2BzIOmPQ h/e9fBtfmiDsOH3EE8GKZWu+tlDZnNmSarUfme9boMujwE1Uq7nZDcisjy4Yy4s4Y6FKsGCNQBG 1fGoDe5S3wX4rlW0unkolccc3tAPWT+XKm/wFFWDRcuw80r251AYbBCfipB452TARSh82dbg3xx 5kv+V7VFDRwSYGOaZPAW5oFdwswI4B9aR3ntVtfyDhRmRWKMDO4MCQY3ArWOVTgcJiy45OSZaIG MUzLKoAo7fnw1w== X-Received: by 2002:a05:6a21:3a48:b0:3c3:b57b:6291 with SMTP id adf61e73a8af0-3ccdac00616mr17004571637.18.1787073256979; Tue, 18 Aug 2026 10:14:16 -0700 (PDT) Received: from daehojeong-desktop.mtv.corp.google.com ([2a00:79e0:2e7c:8:50d9:e296:7754:dc83]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-326798c566asm21135719eec.11.2026.08.18.10.14.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Aug 2026 10:14:16 -0700 (PDT) From: Daeho Jeong To: linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com Cc: Daeho Jeong , Sunmin Jeong Subject: [PATCH] f2fs: accurately adjust free_sections during free_segment_range Date: Tue, 18 Aug 2026 10:14:12 -0700 Message-ID: <20260818171412.3201082-1-daeho43@gmail.com> X-Mailer: git-send-email 2.55.0.691.gc56d675ccc-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Daeho Jeong In free_segment_range(), MAIN_SECS(sbi) is temporarily reduced by `secs` while valid blocks in the truncated range are evacuated by GC. However, if any sections within the truncated range were already free, failing to deduct them from FREE_I(sbi)->free_sections leads to an over-estimation of available space in the reduced main area, causing inconsistent free section accounting. Fix this by calculating the number of already-free sections in the truncated range under segmap_lock, deducting them from free_sections upon entering free_segment_range(), and restoring them under segmap_lock on exit. Signed-off-by: Daeho Jeong Signed-off-by: Sunmin Jeong --- fs/f2fs/gc.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c index 787133ee2eb2..f3a6fc6d08ae 100644 --- a/fs/f2fs/gc.c +++ b/fs/f2fs/gc.c @@ -2200,8 +2200,9 @@ int f2fs_gc_range(struct f2fs_sb_info *sbi, static int free_segment_range(struct f2fs_sb_info *sbi, unsigned int secs, bool dry_run) { - unsigned int next_inuse, start, end; + unsigned int secno, next_inuse, start, end, end_secno; struct cp_control cpc = { CP_RESIZE, 0, 0, 0 }; + unsigned int freed_secs = 0; int gc_mode, gc_type; int err = 0; int type; @@ -2210,6 +2211,7 @@ static int free_segment_range(struct f2fs_sb_info *sbi, MAIN_SECS(sbi) -= secs; start = MAIN_SECS(sbi) * SEGS_PER_SEC(sbi); end = MAIN_SEGS(sbi) - 1; + end_secno = GET_SEC_FROM_SEG(sbi, end); mutex_lock(&DIRTY_I(sbi)->seglist_lock); for (gc_mode = 0; gc_mode < MAX_GC_POLICY; gc_mode++) @@ -2221,6 +2223,14 @@ static int free_segment_range(struct f2fs_sb_info *sbi, sbi->next_victim_seg[gc_type] = NULL_SEGNO; mutex_unlock(&DIRTY_I(sbi)->seglist_lock); + spin_lock(&FREE_I(sbi)->segmap_lock); + for (secno = MAIN_SECS(sbi); secno <= end_secno; secno++) { + if (!test_bit(secno, FREE_I(sbi)->free_secmap)) + freed_secs++; + } + FREE_I(sbi)->free_sections -= freed_secs; + spin_unlock(&FREE_I(sbi)->segmap_lock); + /* Move out cursegs from the target range */ for (type = CURSEG_HOT_DATA; type < NR_CURSEG_TYPE; type++) { err = f2fs_allocate_segment_for_resize(sbi, type, start, end); @@ -2245,6 +2255,9 @@ static int free_segment_range(struct f2fs_sb_info *sbi, f2fs_bug_on(sbi, 1); } out: + spin_lock(&FREE_I(sbi)->segmap_lock); + FREE_I(sbi)->free_sections += freed_secs; + spin_unlock(&FREE_I(sbi)->segmap_lock); MAIN_SECS(sbi) += secs; return err; } -- 2.55.0.691.gc56d675ccc-goog