From nobody Sun May 10 13:28:54 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD638C433F5 for ; Wed, 4 May 2022 00:41:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244734AbiEDAo6 (ORCPT ); Tue, 3 May 2022 20:44:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236921AbiEDAo4 (ORCPT ); Tue, 3 May 2022 20:44:56 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC84F13D0B; Tue, 3 May 2022 17:41:22 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 6E2D5B817A7; Wed, 4 May 2022 00:41:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B1529C385A4; Wed, 4 May 2022 00:41:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651624880; bh=1sRYRjLbt8trZsAxQP405ve5/+hZRzhVT+rXdU002IU=; h=From:To:Cc:Subject:Date:From; b=JdHXMCIdcKimHtn1qV4jHB0cf6iOcdtQ5pD3TFKARDhTTqLR9aA/Z0TBjQZ36GSRa JQxFFWkbORN2XNInCg4MvUw0ODHGfVtYF9X2QGOWjd93MCiv3P3YvWpOaeYx90wrSn CMjC+HmtTfiFzmw9H+GpnDyM8nA0bGynxVb/ght29WV2CaFc4l9zSHVGrgzOSMlF5f O36U0a/Ep3Yqas9JhpB2HWj0WW9f35mK4JoW4hdYPQVtryZr6cMW9D5i8W082KoReQ kXUnxfKtSVJc02E5/YGdaZB9BemEPrQ+fId6qQHPRzp/sRfHWDdXvVdaB5WkSqmGXt 4ODiMubLzvu9A== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , stable@vger.kernel.org, Ming Yan , Chao Yu Subject: [PATCH v2] f2fs: fix deadloop in foreground GC Date: Wed, 4 May 2022 00:24:37 +0800 Message-Id: <20220503162437.20913-1-chao@kernel.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" As Yanming reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=3D215914 The root cause is: in a very small sized image, it's very easy to exceed threshold of foreground GC, if we calculate free space and dirty data based on section granularity, in corner case, has_not_enough_free_secs() will always return true, result in deadloop in f2fs_gc(). So this patch refactors has_not_enough_free_secs() as below to fix this issue: 1. calculate needed space based on block granularity, and separate all blocks to two parts, section part, and block part, comparing section part to free section, and comparing block part to free space in openned log. 2. account F2FS_DIRTY_NODES, F2FS_DIRTY_IMETA and F2FS_DIRTY_DENTS as node block consumer; 3. account F2FS_DIRTY_DENTS as data block consumer; Cc: stable@vger.kernel.org Reported-by: Ming Yan Signed-off-by: Chao Yu --- v2: - fix performance regression fs/f2fs/segment.h | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/fs/f2fs/segment.h b/fs/f2fs/segment.h index 8a591455d796..c38263dbc5ca 100644 --- a/fs/f2fs/segment.h +++ b/fs/f2fs/segment.h @@ -575,11 +575,10 @@ static inline int reserved_sections(struct f2fs_sb_in= fo *sbi) return GET_SEC_FROM_SEG(sbi, reserved_segments(sbi)); } =20 -static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi) +static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi, + unsigned int node_blocks, unsigned int dent_blocks) { - unsigned int node_blocks =3D get_pages(sbi, F2FS_DIRTY_NODES) + - get_pages(sbi, F2FS_DIRTY_DENTS); - unsigned int dent_blocks =3D get_pages(sbi, F2FS_DIRTY_DENTS); + unsigned int segno, left_blocks; int i; =20 @@ -605,19 +604,28 @@ static inline bool has_curseg_enough_space(struct f2f= s_sb_info *sbi) static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi, int freed, int needed) { - int node_secs =3D get_blocktype_secs(sbi, F2FS_DIRTY_NODES); - int dent_secs =3D get_blocktype_secs(sbi, F2FS_DIRTY_DENTS); - int imeta_secs =3D get_blocktype_secs(sbi, F2FS_DIRTY_IMETA); + unsigned int total_node_blocks =3D get_pages(sbi, F2FS_DIRTY_NODES) + + get_pages(sbi, F2FS_DIRTY_DENTS) + + get_pages(sbi, F2FS_DIRTY_IMETA); + unsigned int total_dent_blocks =3D get_pages(sbi, F2FS_DIRTY_DENTS); + unsigned int node_secs =3D total_node_blocks / BLKS_PER_SEC(sbi); + unsigned int dent_secs =3D total_dent_blocks / BLKS_PER_SEC(sbi); + unsigned int node_blocks =3D total_node_blocks % BLKS_PER_SEC(sbi); + unsigned int dent_blocks =3D total_dent_blocks % BLKS_PER_SEC(sbi); + unsigned int free, need_lower, need_upper; =20 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) return false; =20 - if (free_sections(sbi) + freed =3D=3D reserved_sections(sbi) + needed && - has_curseg_enough_space(sbi)) + free =3D free_sections(sbi) + freed; + need_lower =3D node_secs + dent_secs + reserved_sections(sbi) + needed; + need_upper =3D need_lower + node_blocks ? 1 : 0 + dent_blocks ? 1 : 0; + + if (free > need_upper) return false; - return (free_sections(sbi) + freed) <=3D - (node_secs + 2 * dent_secs + imeta_secs + - reserved_sections(sbi) + needed); + else if (free <=3D need_lower) + return true; + return !has_curseg_enough_space(sbi, node_blocks, dent_blocks); } =20 static inline bool f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi) --=20 2.32.0