Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp579700iog; Mon, 13 Jun 2022 08:30:51 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v0zL6eR7DpNbmsEZQWHPgub/vUQosbUX+QJgHB3nzY8meWLIJ9tpf6I+bw8H43nblviXdG X-Received: by 2002:a17:903:32d2:b0:166:3747:8461 with SMTP id i18-20020a17090332d200b0016637478461mr257819plr.30.1655134251741; Mon, 13 Jun 2022 08:30:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655134251; cv=none; d=google.com; s=arc-20160816; b=h4JHtzsZTMbeXXkdLUUJnkTStLXCw/0W+8mNDrcRysrwfczyKKZ6smqkIht4nnMWZb zAJekHqWW2fL8a1cGgYGpgHpWgOLzvOPNAjZp7ssq2U1IDjn7TNOwklEjV4wn2Cy1V19 Cw5C/NQB4Bi/HblzPxRBs6E5Zq68KHIDoiLrtki3bIo1Lp5zIPqpUKgkjRdyQ/G6pCkb HAk7HBZg2Xl9YCLL3sq3JVhP2GAI8TmkPZhWsiLky/HA5cII0DBnEAwC6anSW/8WFwFN W0G4TMJKE9i6P7jKi4vsk0qNjiYiRjUktTqG9u6fTBunT7KHf7vpogmDNtjdBH5nyQI3 j3fQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Q54BJwbJUO9C1PJ6J6ZEfxEb8BCGxTg037PVlptL4aA=; b=LH+aMhlHSdPcRr1ljAsUunqHl6ItvxhN9WP4ejifUzreq8T2OcvW+msKjiaFy+yTVc 6j5s4KtmG2H8uGu3LruMDX7hYVsygNU9PTOEPtBY5fj8XsRdmDFf+kWWvCOe/Ey7/cqx B456C+gxLhp6qrGoKXhG7GBXRSrIXIKPJJVcAcvN/6fV2bw0GkMFx+DmPD+nmXyXuaQn eXd2qaPxbBgUF7/OGk3WYzOv80dT1AlYaTxCzprcUkMq3nkZsYI2zUWV9QQPSuZofgrf Nup6B/q8lNnrS/C5LcDohkyJZe1tkBH0XA/Y8aKf/x0Ei+gqbluRFcuvEOxmlotYso+P F5ZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="w0VQ/ZwN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n66-20020a632745000000b003fda3856f04si9821969pgn.157.2022.06.13.08.30.39; Mon, 13 Jun 2022 08:30:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="w0VQ/ZwN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353970AbiFML0j (ORCPT + 99 others); Mon, 13 Jun 2022 07:26:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353748AbiFMLT4 (ORCPT ); Mon, 13 Jun 2022 07:19: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 E164B3B54E; Mon, 13 Jun 2022 03:41:37 -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 94A6FB80E94; Mon, 13 Jun 2022 10:41:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0D559C34114; Mon, 13 Jun 2022 10:41:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655116895; bh=fAjevsgNN7Ppv3gJX01w6oeiKWkS74OX2dtq+OkZRa8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=w0VQ/ZwNJKQ+ACIkGRKVFsPb6hE1K/yp9Pfrh8NTEh2agspP1eG9MpEakKwFGMIK4 2fr0wQ5FvnURFOqbrkUQ222XRDpmLnSohuOwq4MBFu7zXqDhsSeb9cLv7VV787PtbJ pVPHuI/WrTzSDNFy9ya8OEnBUH8SQEONcp98AhqA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ming Yan , Chao Yu , Jaegeuk Kim Subject: [PATCH 5.4 212/411] f2fs: fix deadloop in foreground GC Date: Mon, 13 Jun 2022 12:08:05 +0200 Message-Id: <20220613094935.011737155@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094928.482772422@linuxfoundation.org> References: <20220613094928.482772422@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chao Yu commit cfd66bb715fd11fde3338d0660cffa1396adc27d upstream. As Yanming reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=215914 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 Signed-off-by: Jaegeuk Kim Signed-off-by: Greg Kroah-Hartman --- fs/f2fs/segment.h | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) --- a/fs/f2fs/segment.h +++ b/fs/f2fs/segment.h @@ -542,11 +542,10 @@ static inline int reserved_sections(stru return GET_SEC_FROM_SEG(sbi, (unsigned int)reserved_segments(sbi)); } -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 = get_pages(sbi, F2FS_DIRTY_NODES) + - get_pages(sbi, F2FS_DIRTY_DENTS); - unsigned int dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS); + unsigned int segno, left_blocks; int i; @@ -572,19 +571,28 @@ static inline bool has_curseg_enough_spa static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi, int freed, int needed) { - int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES); - int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS); - int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA); + unsigned int total_node_blocks = get_pages(sbi, F2FS_DIRTY_NODES) + + get_pages(sbi, F2FS_DIRTY_DENTS) + + get_pages(sbi, F2FS_DIRTY_IMETA); + unsigned int total_dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS); + unsigned int node_secs = total_node_blocks / BLKS_PER_SEC(sbi); + unsigned int dent_secs = total_dent_blocks / BLKS_PER_SEC(sbi); + unsigned int node_blocks = total_node_blocks % BLKS_PER_SEC(sbi); + unsigned int dent_blocks = total_dent_blocks % BLKS_PER_SEC(sbi); + unsigned int free, need_lower, need_upper; if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) return false; - if (free_sections(sbi) + freed == reserved_sections(sbi) + needed && - has_curseg_enough_space(sbi)) + free = free_sections(sbi) + freed; + need_lower = node_secs + dent_secs + reserved_sections(sbi) + needed; + need_upper = need_lower + (node_blocks ? 1 : 0) + (dent_blocks ? 1 : 0); + + if (free > need_upper) return false; - return (free_sections(sbi) + freed) <= - (node_secs + 2 * dent_secs + imeta_secs + - reserved_sections(sbi) + needed); + else if (free <= need_lower) + return true; + return !has_curseg_enough_space(sbi, node_blocks, dent_blocks); } static inline bool f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi)