Message ID | 20231214064635.4128391-1-yebin10@huawei.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8356388dys; Wed, 13 Dec 2023 22:43:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IGWSC45/SFK+O2ZQbSWV+gPPHpyhy1++QcKTiwQRsLxRZYwoBtdQefiSagdk9m3ggSbqiqx X-Received: by 2002:a17:902:7795:b0:1cf:fa64:6e50 with SMTP id o21-20020a170902779500b001cffa646e50mr5248407pll.62.1702536198648; Wed, 13 Dec 2023 22:43:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702536198; cv=none; d=google.com; s=arc-20160816; b=YauE5xHygWhkpZ7m8aqvU46obwpl+ukKPL68caAb6Hlz7lTzzqgJUpc7a61knOE49u isv2/hkgInJHU+FF03j3ESgk3iBC/Yw8maPbwI56EgI2kNZoH4sxg9ChQ6xenfUz3HtM F7Avzah+Bvb0dnKXr95ZO9mfWiz76K6hMBZG2j14JDtKjACq4mzS7EQpxxHeB6Wy4AhT bZKVHLCiphNsJ7zT3HWJWyG2BBKa6+G63tnZYfD82YcrI9dxD5YK98i2CD2yIq87GXhn muy7/Je6H2Nz/LAgV6wDFrFdFZvGfvQLXljVCGsYW5aHZoowujyhXEqcZIx9GlbdPVsB PIgA== 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 :message-id:date:subject:cc:to:from; bh=DNy07d8jf23jRh8+ML7UnuKBzZGC67gIIe0bzac3mAE=; fh=0YRSAtnrSRvhtzUzSfhpD62rtNa0OOsDCBO4xMqK++A=; b=ZpghNSJigVxp37iQa5kIVJJODYUZrppvs4wt4miCvL+A5BFHHCSksT4oVsua3Bb/bm 86IT+2He7XpWYH5v4gYCE9Ime55Feqs8Ca2SMNsptzF1BSl0bCfXMQLaNVJhDHuH2Mq1 68GZqo0tsZfaTXexSphyGrJz7AjNWNMWHRJMAKizF5b7xxIqKm087Ge5vIIMcFyO0eB0 wGQaf5c4sTJZ0YpP/+Z5/lr1eZ3DYoNtCClBdnwzbSkN/mznA9NFca5yyACAptA676Xu Y1qabDcpNcVG0Y8zJPQ902082VJMooKOwEpAVPi3lwK51DyzXS9WOLTS2G6rfeyzdbCM thsg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id bc10-20020a170902930a00b001cc53dbf53dsi1602010plb.648.2023.12.13.22.43.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 22:43:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id AEC4182F2C50; Wed, 13 Dec 2023 22:43:15 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234275AbjLNGnA (ORCPT <rfc822;dexuan.linux@gmail.com> + 99 others); Thu, 14 Dec 2023 01:43:00 -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 <rfc822;linux-kernel@vger.kernel.org>); 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 <yebin10@huawei.com> To: <tytso@mit.edu>, <adilger.kernel@dilger.ca>, <linux-ext4@vger.kernel.org> CC: <linux-kernel@vger.kernel.org>, <jack@suse.cz>, Ye Bin <yebin10@huawei.com> 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: 7BIT Content-Type: text/plain; charset=US-ASCII 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) X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,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 morse.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 13 Dec 2023 22:43:15 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785238597020390383 X-GMAIL-MSGID: 1785238597020390383 |
Series |
ext4: fix inconsistent between segment fstrim and full fstrim
|
|
Commit Message
Ye Bin
Dec. 14, 2023, 6:46 a.m. UTC
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 <yebin10@huawei.com>
---
fs/ext4/mballoc.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
Comments
On Thu 14-12-23 14:46:35, Ye Bin wrote: > 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 <yebin10@huawei.com> OK, I agree that there is this behavioral difference. However is that a practical problem? I mean I would not expect the range to be particularly small, rather something like 1GB and then these boundary conditions don't really matter. This is also sensible so that we can properly track whether the whole block group was trimmed or not. Finally I'd also argue that trimming outside of specified range might be unexpected for the user. So a *fix* for this in my opinion lays in userspace which needs to select sensible ranges to use for trimming. Honza > --- > 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 >
On 2023/12/14 16:58, Jan Kara wrote: > On Thu 14-12-23 14:46:35, Ye Bin wrote: >> 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 <yebin10@huawei.com> > OK, I agree that there is this behavioral difference. However is that a > practical problem? I mean I would not expect the range to be particularly > small, rather something like 1GB and then these boundary conditions don't > really matter. This is also sensible so that we can properly track whether > the whole block group was trimmed or not. Finally I'd also argue that > trimming outside of specified range might be unexpected for the user. So a > *fix* for this in my opinion lays in userspace which needs to select > sensible ranges to use for trimming. > > Honza Thanks for your reply. Our product fstrim entire file system, found to take a long time, thus affecting other processes. So they want to segment the file system fstrim based on the IO of the system. But they found that fragmented fstrims didn't work the same as fstrim for the entire file system. Users do not know the distribution of free blocks in the file system, and they do not know the reasonable range. The user's simple perception is that the effect of segmented fstrim and full fstrim should be consistent. I researched the implementation of fstrim on the XFS file system, and for the scenario described in my patch, the results of both operations are consistent. >> --- >> 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 >>
On Thu 14-12-23 21:06:46, yebin (H) wrote: > > > On 2023/12/14 16:58, Jan Kara wrote: > > On Thu 14-12-23 14:46:35, Ye Bin wrote: > > > 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 <yebin10@huawei.com> > > OK, I agree that there is this behavioral difference. However is that a > > practical problem? I mean I would not expect the range to be particularly > > small, rather something like 1GB and then these boundary conditions don't > > really matter. This is also sensible so that we can properly track whether > > the whole block group was trimmed or not. Finally I'd also argue that > > trimming outside of specified range might be unexpected for the user. So a > > *fix* for this in my opinion lays in userspace which needs to select > > sensible ranges to use for trimming. > > > > Honza > Thanks for your reply. > Our product fstrim entire file system, found to take a long time, thus > affecting other processes. > So they want to segment the file system fstrim based on the IO of the > system. But they found > that fragmented fstrims didn't work the same as fstrim for the entire file > system. > Users do not know the distribution of free blocks in the file system, and > they do not know the > reasonable range. The user's simple perception is that the effect of > segmented fstrim and full > fstrim should be consistent. > I researched the implementation of fstrim on the XFS file system, and for > the scenario described > in my patch, the results of both operations are consistent. OK, we've discussed this on yesterday's ext4 call and we've came to a conclusion that we don't care that much and consistency among filesystems is good so I'll go back and review your patch. Honza
Remembered one more thing to note: On Fri 15-12-23 12:11:08, Jan Kara wrote: > On Thu 14-12-23 21:06:46, yebin (H) wrote: > > On 2023/12/14 16:58, Jan Kara wrote: > > > On Thu 14-12-23 14:46:35, Ye Bin wrote: > > > > 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 <yebin10@huawei.com> > > > OK, I agree that there is this behavioral difference. However is that a > > > practical problem? I mean I would not expect the range to be particularly > > > small, rather something like 1GB and then these boundary conditions don't > > > really matter. This is also sensible so that we can properly track whether > > > the whole block group was trimmed or not. Finally I'd also argue that > > > trimming outside of specified range might be unexpected for the user. So a > > > *fix* for this in my opinion lays in userspace which needs to select > > > sensible ranges to use for trimming. > > > > > > Honza > > Thanks for your reply. > > > Our product fstrim entire file system, found to take a long time, thus > > affecting other processes. So they want to segment the file system > > fstrim based on the IO of the system. But they found that fragmented > > fstrims didn't work the same as fstrim for the entire file system. So I agree that trimming the whole fs at once may take too long. But also note that if you make ranges smaller than block group size (128MB with 4k block size), then we will never record that the group has been fully trimmed and thus we will always trim all free extents in a group even though there was no allocation there since the last trim. So in this sense trims split to small ranges will not be equivalent to a large trim even after your change. > > Users do not know the distribution of free blocks in the file system, > > and they do not know the reasonable range. The user's simple perception > > is that the effect of segmented fstrim and full fstrim should be > > consistent. Personally I don't see a good reason to make trim range smaller than 1GB, it just adds overhead, but I understand it may be a bit surprising. Honza
On Thu 14-12-23 14:46:35, Ye Bin wrote: > 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. The patch looks good so feel free to add: Reviewed-by: Jan Kara <jack@suse.cz> I'd just rephrase the changelog to make it a bit easier to read: Suppose we issue two FITRIM ioctls for ranges [0,15] and [16,31] with mininum length of trimmed range set to 8 blocks. If we have say a range of blocks 10-22 free, this range will not be trimmed because it straddles the boundary of the two FITRIM ranges and neither part is big enough. This is a bit surprising to some users that call FITRIM on smaller ranges of blocks to limit impact on the system. Also XFS trims all free space extents that overlap with the specified range so we are inconsistent among filesystems. Let's change ext4_try_to_trim_range() to consider for trimming the whole free space extent that straddles the end of specified range, not just the part of it within the range. Honza > Signed-off-by: Ye Bin <yebin10@huawei.com> > --- > 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 >
On 2023/12/15 19:41, Jan Kara wrote: > On Thu 14-12-23 14:46:35, Ye Bin wrote: >> 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. > The patch looks good so feel free to add: > > Reviewed-by: Jan Kara <jack@suse.cz> > > I'd just rephrase the changelog to make it a bit easier to read: > > Suppose we issue two FITRIM ioctls for ranges [0,15] and [16,31] with > mininum length of trimmed range set to 8 blocks. If we have say a range of > blocks 10-22 free, this range will not be trimmed because it straddles the > boundary of the two FITRIM ranges and neither part is big enough. This is a > bit surprising to some users that call FITRIM on smaller ranges of blocks > to limit impact on the system. Also XFS trims all free space extents that > overlap with the specified range so we are inconsistent among filesystems. > Let's change ext4_try_to_trim_range() to consider for trimming the whole > free space extent that straddles the end of specified range, not just the > part of it within the range. > > Honza > Thank you very much for your clear explanation of the patch. I'll update patch's changelog and resend a version. >> Signed-off-by: Ye Bin <yebin10@huawei.com> >> --- >> 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 >>
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);