Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5799027rdb; Wed, 13 Dec 2023 22:43:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IG235e2rWxzpAp/tGDqwlBhhjX+040fnLhYE8qe/W1hoSyznCO5YJK4ucQ26MsX/lXG0pLL X-Received: by 2002:a0c:cdc2:0:b0:67e:f4f8:3662 with SMTP id a2-20020a0ccdc2000000b0067ef4f83662mr3063900qvn.0.1702536195656; Wed, 13 Dec 2023 22:43:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702536195; cv=none; d=google.com; s=arc-20160816; b=n331bqqze1t9umjn9o/1h/9YETygZlJrMIIQfyM6VcjYlNouBgBpsxVcnjB3xAysoM T9ZVOWJD/9/X9VSMcTGqAR35ipkdg14yRW4Xy/cF9nyuN16i0n0A4P/2Ita9gkg8Q1Pc Yqzs2OF5N45mUBS6EEx3fzSc3f65OePkdPIbv3BlAdblYkqbJ5YEOOPcPD9wobfUP0o/ AslSxpyGfN2jK8ClX19ClEP2vGESKZxNZ2Ir0BUWJejxWabkzBKwHVFk3FyhzfpFJU2Y SXigYvkCMzYqWAMLoksXz0XfXmAx/RNhWNPyHMi2CujP9OniU83st42HDp6nK0uj6RlI NhPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=DNy07d8jf23jRh8+ML7UnuKBzZGC67gIIe0bzac3mAE=; fh=0YRSAtnrSRvhtzUzSfhpD62rtNa0OOsDCBO4xMqK++A=; b=Yh7q5Nq9/239LbcJTP2UEfRzJA3+CNWNT53T5ym0o7WsJzB40fL+ZJdOjRLyybx3qJ 5CMrwobDTQsLrNiwIjGDxZBVYzZ18xhW8n91MMDxxcRmdYN/MmsPehIQ0EP+Z09Zn7tF dOyAYEvcK9BtX1HPQL12GKZ6UxkVyvzBWfZoNLqT+3qpurpttA+b0W+vi4jv4oLBlZsl GaZXAPFMXTzHuGjeBcpgKFFVyVvfHlQuHRIBnpYPPXAZtRR9oW/oJV/fqwiJh65l1Mgz XEZNmQ4qddrnN3lFYu7Og6fDWByqwDECywOECIk/s3UxxMZFs4D6QaEsW2orqlyssd+v 5zMw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4+bounces-443-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-ext4+bounces-443-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id r18-20020ad44052000000b0067abd18c974si14868033qvp.325.2023.12.13.22.43.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 22:43:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4+bounces-443-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4+bounces-443-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-ext4+bounces-443-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 694901C210D0 for ; Thu, 14 Dec 2023 06:43:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C0640D2E4; Thu, 14 Dec 2023 06:43:09 +0000 (UTC) X-Original-To: linux-ext4@vger.kernel.org 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 Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To canpemm500010.china.huawei.com (7.192.105.118) There will not issue discard cmd when do segment fstrim for ext4 fs, however, 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'+ 'Range2'. To solve above issue, we need to find the longest free space to discard. Signed-off-by: Ye Bin --- 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_block *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 = false; void *bitmap; + last = ext4_last_grp_cluster(sb, e4b->bd_group); bitmap = e4b->bd_bitmap; - if (start == 0 && max >= ext4_last_grp_cluster(sb, e4b->bd_group)) + if (start == 0 && max >= last) set_trimmed = true; + origin_start = start; start = max(e4b->bd_info->bb_first_free, start); count = 0; free_count = 0; @@ -6768,7 +6770,10 @@ __releases(ext4_group_lock_ptr(sb, e4b->bd_group)) start = mb_find_next_zero_bit(bitmap, max + 1, start); if (start > max) break; - next = mb_find_next_bit(bitmap, max + 1, start); + + next = mb_find_next_bit(bitmap, last + 1, start); + if (origin_start == 0 && next >= last) + set_trimmed = true; if ((next - start) >= minblocks) { int ret = ext4_trim_extent(sb, start, next - start, e4b); -- 2.31.1