Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp648742ybm; Wed, 27 May 2020 04:47:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJymKSpqqKnGgwT6MouHbDUt7R+KPpiU4H3IM2kmj9nmGx8GAhmsYpeS5F8uemunVX07cswa X-Received: by 2002:aa7:d35a:: with SMTP id m26mr23394731edr.25.1590580053851; Wed, 27 May 2020 04:47:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590580053; cv=none; d=google.com; s=arc-20160816; b=0TLkQfDBSpJYfU0JHJDxSZdOJPjcERYK4sOWqZImxGzw6TfYbgBzHGr7Eyp88s5Hh3 m6un3eROgjZq94xAGfzcy+VYfxFO4p848k2ScznRsVzD5YBuNspLHr4/cJxFgg9fvDeL uHkzf7q19W3TLLSXXm3roHZZl8VST1ZESYQju0i4wm6v0Dvy1HJEl6T/ALw0jOkTLCvG N6bUu6gzEgVUPaV7lxJtDePJzcJlKWgB2697UNVgx7XMLnAg7bJSZQ+/QB1EpXWEoT1G oegHup/B8fLWRL7GgMNLFQulDcH0tPs5LGy9457pQ6Y7DMSdJQhpEAGF74mpxuhLks4Q qHug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=AxgryR443q/eyG9WKki++/fM8WnoUEW2qj2DTcqDzdk=; b=MgI7mL+Q0Pl4lImt39ESMV7LLVkp5OmlpdaOZnlKMz5peRw/btySjLQiCRrouYNh9R tkJBshVI9XKAtTNT4J7+Vg3mHHT8EjrF65yfWdA2spwGkN6AkPCy/X+EKW+A/5OZh7V3 3C6EPEKeY4CUxbPJyKcIzyTW3+Q1/MM16zqBfJI1ZAOtpv6njXIIShQ+Q3O6cpQB+Pc9 2BNPnO3VLYyC7zGf2hrRx5/MJVZvBJaJ2c24lB8bCUGeTPIHLfjR9pai1n50GHinqvRr HBiA3RID0kzsE2VS2b69OFiBVBfAW+g52iAFkgwpQXPfHHCW2Ffoklg+eR0lt6DKzBC9 eOhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=blIZRIwb; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce6si1472180edb.379.2020.05.27.04.47.09; Wed, 27 May 2020 04:47:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=blIZRIwb; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726964AbgE0KWK (ORCPT + 99 others); Wed, 27 May 2020 06:22:10 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:60910 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729783AbgE0KWK (ORCPT ); Wed, 27 May 2020 06:22:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590574927; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=AxgryR443q/eyG9WKki++/fM8WnoUEW2qj2DTcqDzdk=; b=blIZRIwb98muNg2C+hhGT0GngbWwkHUBxVkzxJW/BFEuTYAi4kEEehE89+3XbdIVM8jcro hqyh8eUD99p9LCJ8s+w2r6HBBWkU4zMSJRtj9qrMLG2XtUeVJlTTmMd5a6Rqh7fpkv9dpd vNbhUjDKv0x4vUGaF65EqKQssLElB04= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-202-1IE8eDrnMbqQeYDq5CINqg-1; Wed, 27 May 2020 06:22:05 -0400 X-MC-Unique: 1IE8eDrnMbqQeYDq5CINqg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 96FE918FE88C; Wed, 27 May 2020 10:22:04 +0000 (UTC) Received: from work (unknown [10.40.194.50]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0A99F98A5C; Wed, 27 May 2020 10:22:02 +0000 (UTC) Date: Wed, 27 May 2020 12:21:58 +0200 From: Lukas Czerner To: Wang Shilong Cc: Ext4 Developers List , Wang Shilong , Shuichi Ihara , Andreas Dilger Subject: Re: [PATCH] ext4: introduce EXT4_BG_WAS_TRIMMED to optimize trim Message-ID: <20200527102158.n2bozg5wivzjot2b@work> References: <1590565130-23773-1-git-send-email-wangshilong1991@gmail.com> <20200527091938.647363ekmnz7av7y@work> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Wed, May 27, 2020 at 06:06:09PM +0800, Wang Shilong wrote: > On Wed, May 27, 2020 at 5:19 PM Lukas Czerner wrote: > > > > On Wed, May 27, 2020 at 04:38:50PM +0900, Wang Shilong wrote: > > > From: Wang Shilong > > > > > > Currently WAS_TRIMMED flag is not persistent, whenever filesystem was > > > remounted, fstrim need walk all block groups again, the problem with > > > this is FSTRIM could be slow on very large LUN SSD based filesystem. > > > > > > To avoid this kind of problem, we introduce a block group flag > > > EXT4_BG_WAS_TRIMMED, the side effect of this is we need introduce > > > extra one block group dirty write after trimming block group. > > > > Hi > > > > that's fair enough, however once you make this persistent we also need > > to have a way to clear the flag, or at least bypass it. Storage can be > > changed and if it does we might want to re-run the fstrim. > > Yup, i thought about that. > > 1) we might add an mount option or sys interface, something force_fstrim > 2) Add an option to e2fsck to force clear this block group flag. Option for tune2fs to clear the flags sounds way better than yet another mount option. Thanks! -Lukas > > > > > We also need to set this flag in mke2fs and e2fsck if appropriate. > > > > Good point. > > > more below... > > > > > > > > And When clearing TRIMMED flag, block group will be journalled > > > anyway, so it won't introduce any overhead. > > > > > > Cc: Shuichi Ihara > > > Cc: Andreas Dilger > > > Cc: Wang Shilong > > > Signed-off-by: Wang Shilong > > > --- > > > fs/ext4/ext4.h | 18 +++++++-------- > > > fs/ext4/ext4_jbd2.h | 3 ++- > > > fs/ext4/mballoc.c | 54 ++++++++++++++++++++++++++++++++++----------- > > > 3 files changed, 52 insertions(+), 23 deletions(-) > > > > > > diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h > > > index ad2dbf6e4924..23c2dc529a28 100644 > > > --- a/fs/ext4/ext4.h > > > +++ b/fs/ext4/ext4.h > > > @@ -357,6 +357,7 @@ struct flex_groups { > > > #define EXT4_BG_INODE_UNINIT 0x0001 /* Inode table/bitmap not in use */ > > > #define EXT4_BG_BLOCK_UNINIT 0x0002 /* Block bitmap not in use */ > > > #define EXT4_BG_INODE_ZEROED 0x0004 /* On-disk itable initialized to zero */ > > > +#define EXT4_BG_WAS_TRIMMED 0x0008 /* block group was trimmed */ > > > > > > /* > > > * Macro-instructions used to manage group descriptors > > > @@ -3112,9 +3113,8 @@ struct ext4_group_info { > > > }; > > > > > > #define EXT4_GROUP_INFO_NEED_INIT_BIT 0 > > > -#define EXT4_GROUP_INFO_WAS_TRIMMED_BIT 1 > > > -#define EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT 2 > > > -#define EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT 3 > > > +#define EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT 1 > > > +#define EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT 2 > > > #define EXT4_GROUP_INFO_BBITMAP_CORRUPT \ > > > (1 << EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT) > > > #define EXT4_GROUP_INFO_IBITMAP_CORRUPT \ > > > @@ -3127,12 +3127,12 @@ struct ext4_group_info { > > > #define EXT4_MB_GRP_IBITMAP_CORRUPT(grp) \ > > > (test_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &((grp)->bb_state))) > > > > > > -#define EXT4_MB_GRP_WAS_TRIMMED(grp) \ > > > - (test_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state))) > > > -#define EXT4_MB_GRP_SET_TRIMMED(grp) \ > > > - (set_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state))) > > > -#define EXT4_MB_GRP_CLEAR_TRIMMED(grp) \ > > > - (clear_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state))) > > > +#define EXT4_MB_GDP_WAS_TRIMMED(gdp) \ > > > + (gdp->bg_flags & cpu_to_le16(EXT4_BG_WAS_TRIMMED)) > > > +#define EXT4_MB_GDP_SET_TRIMMED(gdp) \ > > > + (gdp->bg_flags |= cpu_to_le16(EXT4_BG_WAS_TRIMMED)) > > > +#define EXT4_MB_GDP_CLEAR_TRIMMED(gdp) \ > > > + (gdp->bg_flags &= ~cpu_to_le16(EXT4_BG_WAS_TRIMMED)) > > > > > > #define EXT4_MAX_CONTENTION 8 > > > #define EXT4_CONTENTION_THRESHOLD 2 > > > diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h > > > index 4b9002f0e84c..4094a5b247f7 100644 > > > --- a/fs/ext4/ext4_jbd2.h > > > +++ b/fs/ext4/ext4_jbd2.h > > > @@ -123,7 +123,8 @@ > > > #define EXT4_HT_MOVE_EXTENTS 9 > > > #define EXT4_HT_XATTR 10 > > > #define EXT4_HT_EXT_CONVERT 11 > > > -#define EXT4_HT_MAX 12 > > > +#define EXT4_HT_FS_TRIM 12 > > > +#define EXT4_HT_MAX 13 > > > > > > /** > > > * struct ext4_journal_cb_entry - Base structure for callback information. > > > diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c > > > index 30d5d97548c4..d25377948994 100644 > > > --- a/fs/ext4/mballoc.c > > > +++ b/fs/ext4/mballoc.c > > > @@ -2829,15 +2829,6 @@ static void ext4_free_data_in_buddy(struct super_block *sb, > > > rb_erase(&entry->efd_node, &(db->bb_free_root)); > > > mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count); > > > > > > - /* > > > - * Clear the trimmed flag for the group so that the next > > > - * ext4_trim_fs can trim it. > > > - * If the volume is mounted with -o discard, online discard > > > - * is supported and the free blocks will be trimmed online. > > > - */ > > > - if (!test_opt(sb, DISCARD)) > > > - EXT4_MB_GRP_CLEAR_TRIMMED(db); > > > - > > > if (!db->bb_free_root.rb_node) { > > > /* No more items in the per group rb tree > > > * balance refcounts from ext4_mb_free_metadata() > > > @@ -4928,8 +4919,7 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode, > > > " group:%d block:%d count:%lu failed" > > > " with %d", block_group, bit, count, > > > err); > > > - } else > > > - EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info); > > > + } > > > > > > ext4_lock_group(sb, block_group); > > > mb_clear_bits(bitmap_bh->b_data, bit, count_clusters); > > > @@ -4939,6 +4929,14 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode, > > > ret = ext4_free_group_clusters(sb, gdp) + count_clusters; > > > ext4_free_group_clusters_set(sb, gdp, ret); > > > ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh); > > > + /* > > > + * Clear the trimmed flag for the group so that the next > > > + * ext4_trim_fs can trim it. > > > + * If the volume is mounted with -o discard, online discard > > > + * is supported and the free blocks will be trimmed online. > > > + */ > > > + if (!test_opt(sb, DISCARD)) > > > + EXT4_MB_GDP_CLEAR_TRIMMED(gdp); > > > ext4_group_desc_csum_set(sb, block_group, gdp); > > > ext4_unlock_group(sb, block_group); > > > > > > @@ -5192,8 +5190,15 @@ ext4_trim_all_free(struct super_block *sb, ext4_group_t group, > > > ext4_grpblk_t next, count = 0, free_count = 0; > > > struct ext4_buddy e4b; > > > int ret = 0; > > > + struct ext4_group_desc *gdp; > > > + struct buffer_head *gdp_bh; > > > > > > trace_ext4_trim_all_free(sb, group, start, max); > > > + gdp = ext4_get_group_desc(sb, group, &gdp_bh); > > > + if (!gdp) { > > > + ret = -EIO; > > > + return ret; > > > + } > > > > > > ret = ext4_mb_load_buddy(sb, group, &e4b); > > > if (ret) { > > > @@ -5204,7 +5209,7 @@ ext4_trim_all_free(struct super_block *sb, ext4_group_t group, > > > bitmap = e4b.bd_bitmap; > > > > > > ext4_lock_group(sb, group); > > > - if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) && > > > + if (EXT4_MB_GDP_WAS_TRIMMED(gdp) && > > > minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks)) > > > goto out; > > > > > > @@ -5245,12 +5250,35 @@ ext4_trim_all_free(struct super_block *sb, ext4_group_t group, > > > > > > if (!ret) { > > > ret = count; > > > - EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info); > > > + EXT4_MB_GDP_SET_TRIMMED(gdp); > > > + ext4_group_desc_csum_set(sb, group, gdp); > > > } > > > out: > > > ext4_unlock_group(sb, group); > > > ext4_mb_unload_buddy(&e4b); > > > + if (ret > 0) { > > > + int err; > > > + handle_t *handle; > > > > > > + handle = ext4_journal_start_sb(sb, EXT4_HT_FS_TRIM, 1); > > > + if (IS_ERR(handle)) { > > > + ret = PTR_ERR(handle); > > > + goto out_return; > > > + } > > > + err = ext4_journal_get_write_access(handle, gdp_bh); > > > + if (err) { > > > + ret = err; > > > + goto out_journal; > > > + } > > > > Don't we need to do this before we set the flag in gdp? > > > > Sorry about this, you are right. > > > -Lukas > > > > > + err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh); > > > + if (err) > > > + ret = err; > > > +out_journal: > > > + err = ext4_journal_stop(handle); > > > + if (err) > > > + ret = err; > > > + } > > > +out_return: > > > ext4_debug("trimmed %d blocks in the group %d\n", > > > count, group); > > > > > > -- > > > 2.25.4 > > > > > >