Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2202845iof; Tue, 7 Jun 2022 23:05:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyWp5xAP66T/KUFmoHKwlAfbM/hHW18cPcS2atsmleGnDSMvPq0B1KOCr27DtZ3uHlRHGDZ X-Received: by 2002:a17:902:ac8f:b0:163:fbb7:b057 with SMTP id h15-20020a170902ac8f00b00163fbb7b057mr32990663plr.67.1654668341064; Tue, 07 Jun 2022 23:05:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654668341; cv=none; d=google.com; s=arc-20160816; b=d+3vNzE0LxLycZozoWQXtPG071yEfhj/foISILhYysoeJ2YS9rcEsbaeURCQNq1Huh n2xeZqFbhFRu/slq/hdJrkE9byNyXJjYYBzb0cjO8j475kOi9MlQqC4jrSKpl6Vqmw7e 0PlCs6BQpnGuR6QDTjiJsDSkMi5F6qZmgw3AXj9vDuY3SIfbdK2Ez4/I4czAb70iuzPc qDbzR2fdeF1WvAE32uxpBRLD+K+w+i4FmbVMXUqO4UI6m7yQxJLIcy7zkbLVuNd5eNL3 wjEZg0+9NlpW/U+yfLxAvr3vIEgauVWy8z8VZhptjnUGAEmLKjRw0U9vbgGmXNUMcqbc Xdrg== 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=6quQAQOoS23/twhA5XQeTEcAFxapCCMyhKQQgVwA38Q=; b=0wPuvXiDcNzvwuDknfK/T8NAUj16QmqAQZH0rKVCZ84vOm54QPwRBlgz8P/1PcD4E2 l+Eq/2JH8LE07yfDbPIKNuyeKYP3dWv0E0SzC2LgonpFwCRPX8hrqZ8JEmHSNqEWE8lZ lSB1vYeVYkRdM1R5wlNobiQS3uRU/+gPsmgS//gTncVET4E7TkTrqqcjdiNPlxlZA9Eo sqVdh6jVD/bCkym7nZFV/ApVN3gLGKHHL7XdcAnwG7v1rm60v2Jz0mg5iS+sPI60FhdL C2EglSrY3A4KNToNB3b/zjWUioXKYYObtf2vK2uqvptI5sf3QS+4uehadVBOXR5to6Z2 RXmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=pWTiAM3b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id u69-20020a638548000000b003f5ea6774f8si19061509pgd.518.2022.06.07.23.05.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 23:05:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=pWTiAM3b; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2FDFF456190; Tue, 7 Jun 2022 22:33:39 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1389837AbiFHBqV (ORCPT + 99 others); Tue, 7 Jun 2022 21:46:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1384731AbiFGWQF (ORCPT ); Tue, 7 Jun 2022 18:16:05 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB6322612BA; Tue, 7 Jun 2022 12:20:03 -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 dfw.source.kernel.org (Postfix) with ESMTPS id B49D36193E; Tue, 7 Jun 2022 19:19:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3837C385A2; Tue, 7 Jun 2022 19:19:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629560; bh=uDFJt36J43JNrQpnP2g5KapftTC+j4X2m4rJ87PLIAI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pWTiAM3bB7BjY7Bs3jCMoXBJixbsNaXqJ8SvVD7dgCUmonWebji2ybSySWCZizsmm J2JIPuiGlTz41RZpWwo8tfxwfzzSs/l3vG/3MbOrREewHjUMIXO7a6XWaW7dOjNK06 cmebVmF57lPysuqRdWn4YUPP1qDvkrqm/IkoiI5o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dmitry Monakhov , Theodore Tso , stable@kernel.org Subject: [PATCH 5.18 736/879] ext4: mark group as trimmed only if it was fully scanned Date: Tue, 7 Jun 2022 19:04:14 +0200 Message-Id: <20220607165024.220534344@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@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=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: Dmitry Monakhov commit d63c00ea435a5352f486c259665a4ced60399421 upstream. Otherwise nonaligned fstrim calls will works inconveniently for iterative scanners, for example: // trim [0,16MB] for group-1, but mark full group as trimmed fstrim -o $((1024*1024*128)) -l $((1024*1024*16)) ./m // handle [16MB,16MB] for group-1, do nothing because group already has the flag. fstrim -o $((1024*1024*144)) -l $((1024*1024*16)) ./m [ Update function documentation for ext4_trim_all_free -- TYT ] Signed-off-by: Dmitry Monakhov Link: https://lore.kernel.org/r/1650214995-860245-1-git-send-email-dmtrmonakhov@yandex-team.ru Signed-off-by: Theodore Ts'o Cc: stable@kernel.org Signed-off-by: Greg Kroah-Hartman --- fs/ext4/mballoc.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -6398,6 +6398,7 @@ __releases(ext4_group_lock_ptr(sb, e4b-> * @start: first group block to examine * @max: last group block to examine * @minblocks: minimum extent block count + * @set_trimmed: set the trimmed flag if at least one block is trimmed * * ext4_trim_all_free walks through group's block bitmap searching for free * extents. When the free extent is found, mark it as used in group buddy @@ -6407,7 +6408,7 @@ __releases(ext4_group_lock_ptr(sb, e4b-> static ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, ext4_group_t group, ext4_grpblk_t start, ext4_grpblk_t max, - ext4_grpblk_t minblocks) + ext4_grpblk_t minblocks, bool set_trimmed) { struct ext4_buddy e4b; int ret; @@ -6426,7 +6427,7 @@ ext4_trim_all_free(struct super_block *s if (!EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) || minblocks < EXT4_SB(sb)->s_last_trim_minblks) { ret = ext4_try_to_trim_range(sb, &e4b, start, max, minblocks); - if (ret >= 0) + if (ret >= 0 && set_trimmed) EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info); } else { ret = 0; @@ -6463,6 +6464,7 @@ int ext4_trim_fs(struct super_block *sb, ext4_fsblk_t first_data_blk = le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block); ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es); + bool whole_group, eof = false; int ret = 0; start = range->start >> sb->s_blocksize_bits; @@ -6481,8 +6483,10 @@ int ext4_trim_fs(struct super_block *sb, if (minlen > EXT4_CLUSTERS_PER_GROUP(sb)) goto out; } - if (end >= max_blks) + if (end >= max_blks - 1) { end = max_blks - 1; + eof = true; + } if (end <= first_data_blk) goto out; if (start < first_data_blk) @@ -6496,6 +6500,7 @@ int ext4_trim_fs(struct super_block *sb, /* end now represents the last cluster to discard in this group */ end = EXT4_CLUSTERS_PER_GROUP(sb) - 1; + whole_group = true; for (group = first_group; group <= last_group; group++) { grp = ext4_get_group_info(sb, group); @@ -6512,12 +6517,13 @@ int ext4_trim_fs(struct super_block *sb, * change it for the last group, note that last_cluster is * already computed earlier by ext4_get_group_no_and_offset() */ - if (group == last_group) + if (group == last_group) { end = last_cluster; - + whole_group = eof ? true : end == EXT4_CLUSTERS_PER_GROUP(sb) - 1; + } if (grp->bb_free >= minlen) { cnt = ext4_trim_all_free(sb, group, first_cluster, - end, minlen); + end, minlen, whole_group); if (cnt < 0) { ret = cnt; break;