From nobody Sun Dec 28 00:57:32 2025 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 78F3AC4332F for ; Thu, 14 Dec 2023 06:43:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234302AbjLNGnB (ORCPT ); Thu, 14 Dec 2023 01:43:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229629AbjLNGnA (ORCPT ); Thu, 14 Dec 2023 01:43:00 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BDC2E8; Wed, 13 Dec 2023 22:43:06 -0800 (PST) Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4SrN7X385mzvS8H; Thu, 14 Dec 2023 14:42:16 +0800 (CST) Received: from canpemm500010.china.huawei.com (unknown [7.192.105.118]) by mail.maildlp.com (Postfix) with ESMTPS id 388BB140258; Thu, 14 Dec 2023 14:43:04 +0800 (CST) Received: from huawei.com (10.175.127.227) by canpemm500010.china.huawei.com (7.192.105.118) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Thu, 14 Dec 2023 14:43:03 +0800 From: Ye Bin To: , , CC: , , Ye Bin Subject: [PATCH] ext4: fix inconsistent between segment fstrim and full fstrim Date: Thu, 14 Dec 2023 14:46:35 +0800 Message-ID: <20231214064635.4128391-1-yebin10@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To canpemm500010.china.huawei.com (7.192.105.118) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" There will not issue discard cmd when do segment fstrim for ext4 fs, howeve= r, if full fstrim for the same fs will issue discard cmd. Above issue may happens as follows: Precondition: 1. Fstrim range [0, 15] and [16, 31]; 2. Discard granularity is 16; Range1 Range2 1111000000000000 0000111010101011 There's no free space length large or equal than 16 in 'Range1' or 'Range2'. As ext4_try_to_trim_range() only search free space among range which user specified. However, there's maximum free space length 16 in 'Range1'+ 'Rang= e2'. To solve above issue, we need to find the longest free space to discard. Signed-off-by: Ye Bin Reviewed-by: Jan Kara --- fs/ext4/mballoc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index d72b5e3c92ec..d195461123d8 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -6753,13 +6753,15 @@ static int ext4_try_to_trim_range(struct super_bloc= k *sb, __acquires(ext4_group_lock_ptr(sb, e4b->bd_group)) __releases(ext4_group_lock_ptr(sb, e4b->bd_group)) { - ext4_grpblk_t next, count, free_count; + ext4_grpblk_t next, count, free_count, last, origin_start; bool set_trimmed =3D false; void *bitmap; =20 + last =3D ext4_last_grp_cluster(sb, e4b->bd_group); bitmap =3D e4b->bd_bitmap; - if (start =3D=3D 0 && max >=3D ext4_last_grp_cluster(sb, e4b->bd_group)) + if (start =3D=3D 0 && max >=3D last) set_trimmed =3D true; + origin_start =3D start; start =3D max(e4b->bd_info->bb_first_free, start); count =3D 0; free_count =3D 0; @@ -6768,7 +6770,10 @@ __releases(ext4_group_lock_ptr(sb, e4b->bd_group)) start =3D mb_find_next_zero_bit(bitmap, max + 1, start); if (start > max) break; - next =3D mb_find_next_bit(bitmap, max + 1, start); + + next =3D mb_find_next_bit(bitmap, last + 1, start); + if (origin_start =3D=3D 0 && next >=3D last) + set_trimmed =3D true; =20 if ((next - start) >=3D minblocks) { int ret =3D ext4_trim_extent(sb, start, next - start, e4b); --=20 2.31.1