Viewing: ext4-introduce-EXT4_BG_TRIMMED-to-optimize-fstrim.patch
commit ad30edf91065d733d7da68de755acf1043429101
Author: Wang Shilong <wshilong@ddn.com>
AuthorDate: Fri Aug 11 15:32:31 2023 +1000
LU-14712 ldiskfs: introduce EXT4_BG_TRIMMED to optimize fstrim
Currently the WAS_TRIMMED flag indicating block group has been
trimmed is not persistent, and fstrim status will be lost at
unmount. As a result, fstrim cannot skip the already trimmed
groups on remount, which could be slow on very large devices.
To avoid this kind of problem, introduce a new block group flag
EXT4_BG_TRIMMED to be stored persistently in the block group
descriptor after trimming each block group. This adds one extra
group descriptor write after trimming a block group. When the
flag is cleared the block group descriptor is journaled already
so it does not introduce any overhead.
Add a new super block flag EXT2_FLAGS_TRACK_TRIM, to indicate if
we should honour persistent EXT4_BG_TRIMMED when doing fstrim.
The new super block flag can be turned on/off via tune2fs.
Cc: Shuichi Ihara <sihara@ddn.com>
Cc: Wang Shilong <wangshilong1991@gmail.com>
Signed-off-by: Wang Shilong <wshilong@ddn.com>
Signed-off-by: Dongyang Li <dongyangli@ddn.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Change-Id: I7faaca4754b1726ad05d0aafe3e90e0e9f591617
Reviewed-on: https://review.whamcloud.com/51923
Index: linux-4.18.0-425.10.1.el8_7/fs/ext4/ext4.h
===================================================================
--- linux-4.18.0-425.10.1.el8_7.orig/fs/ext4/ext4.h
+++ linux-4.18.0-425.10.1.el8_7/fs/ext4/ext4.h
@@ -358,6 +358,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_TRIMMED 0x0008 /* block group was trimmed */
/*
* Macro-instructions used to manage group descriptors
@@ -1124,6 +1125,7 @@ struct ext4_inode_info {
#define EXT2_FLAGS_SIGNED_HASH 0x0001 /* Signed dirhash in use */
#define EXT2_FLAGS_UNSIGNED_HASH 0x0002 /* Unsigned dirhash in use */
#define EXT2_FLAGS_TEST_FILESYS 0x0004 /* to test development code */
+#define EXT2_FLAGS_TRACK_TRIM 0x0008 /* Track trim status in each bg */
/*
* Mount flags set via mount options or defaults
@@ -1223,6 +1225,9 @@ extern void ext4_set_bits(void *bm, int
#define EXT4_DFL_MAX_MNT_COUNT 20 /* Allow 20 mounts */
#define EXT4_DFL_CHECKINTERVAL 0 /* Don't use interval check */
+/* Default min freed blocks which we could clear BG_TRIMMED flag */
+#define EXT4_DEF_BG_TRIMMED_THRESHOLD 128
+
/*
* Behaviour when detecting errors
*/
@@ -1527,6 +1532,9 @@ struct ext4_sb_info {
/* the size of zero-out chunk */
unsigned int s_extent_max_zeroout_kb;
+ /* Min freed blocks per group that we could clear BG_TRIMMED on it */
+ unsigned long s_bg_trimmed_threshold;
+
unsigned int s_log_groups_per_flex;
struct flex_groups * __rcu *s_flex_groups;
ext4_group_t s_flex_groups_allocated;
@@ -3168,6 +3176,7 @@ struct ext4_group_info {
struct rb_root bb_free_root;
ext4_grpblk_t bb_first_free; /* first free block */
ext4_grpblk_t bb_free; /* total free blocks */
+ ext4_grpblk_t bb_freed_since_trim; /* blocks freed since last trim */
ext4_grpblk_t bb_fragments; /* nr of freespace fragments */
ext4_grpblk_t bb_largest_free_order;/* order of largest frag in BG */
struct list_head bb_prealloc_list;
@@ -3183,7 +3192,6 @@ 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 \
@@ -3199,12 +3207,6 @@ 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_GRP_TEST(grp) \
(test_bit(EXT4_GROUP_INFO_BBITMAP_READ_BIT, &((grp)->bb_state)))
#define EXT4_MB_GRP_TEST_AND_SET_READ(grp) \
Index: linux-4.18.0-425.10.1.el8_7/fs/ext4/ext4_jbd2.h
===================================================================
--- linux-4.18.0-425.10.1.el8_7.orig/fs/ext4/ext4_jbd2.h
+++ linux-4.18.0-425.10.1.el8_7/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.
Index: linux-4.18.0-425.10.1.el8_7/fs/ext4/mballoc.c
===================================================================
--- linux-4.18.0-425.10.1.el8_7.orig/fs/ext4/mballoc.c
+++ linux-4.18.0-425.10.1.el8_7/fs/ext4/mballoc.c
@@ -2956,6 +2956,7 @@ int ext4_mb_add_groupinfo(struct super_b
init_rwsem(&meta_group_info[i]->alloc_sem);
meta_group_info[i]->bb_free_root = RB_ROOT;
meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
+ meta_group_info[i]->bb_freed_since_trim = 0;
mb_group_bb_bitmap_alloc(sb, meta_group_info[i], group);
return 0;
@@ -3209,6 +3210,8 @@ int ext4_mb_init(struct super_block *sb)
if (sbi->s_mb_prefetch_limit > ext4_get_groups_count(sb))
sbi->s_mb_prefetch_limit = ext4_get_groups_count(sb);
+ sbi->s_bg_trimmed_threshold = EXT4_DEF_BG_TRIMMED_THRESHOLD;
+
sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
if (sbi->s_locality_groups == NULL) {
ret = -ENOMEM;
@@ -3382,15 +3385,6 @@ static void ext4_free_data_in_buddy(stru
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()
@@ -5624,8 +5618,7 @@ do_more:
" 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);
@@ -5635,6 +5628,22 @@ do_more:
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)) {
+ struct ext4_super_block *es = sbi->s_es;
+
+ e4b.bd_info->bb_freed_since_trim += count;
+
+ if (e4b.bd_info->bb_freed_since_trim >=
+ sbi->s_bg_trimmed_threshold ||
+ !(es->s_flags & cpu_to_le32(EXT2_FLAGS_TRACK_TRIM)))
+ gdp->bg_flags &= cpu_to_le16(~EXT4_BG_TRIMMED);
+ }
ext4_group_desc_csum_set(sb, block_group, gdp);
ext4_unlock_group(sb, block_group);
@@ -5889,9 +5898,19 @@ ext4_trim_all_free(struct super_block *s
void *bitmap;
ext4_grpblk_t next, count = 0, free_count = 0;
struct ext4_buddy e4b;
+ struct ext4_super_block *es = EXT4_SB(sb)->s_es;
+ struct ext4_group_desc *gdp;
+ struct ext4_group_info *grp = ext4_get_group_info(sb, group);
+ struct buffer_head *gd_bh;
+ ext4_grpblk_t freed_last_trimmed_orig;
+ bool track_trim = (es->s_flags & cpu_to_le32(EXT2_FLAGS_TRACK_TRIM)) &&
+ !sb_rdonly(sb);
int ret = 0;
trace_ext4_trim_all_free(sb, group, start, max);
+ gdp = ext4_get_group_desc(sb, group, &gd_bh);
+ if (!gdp)
+ return -EIO;
ret = ext4_mb_load_buddy(sb, group, &e4b);
if (ret)
@@ -5899,10 +5917,11 @@ ext4_trim_all_free(struct super_block *s
bitmap = e4b.bd_bitmap;
ext4_lock_group(sb, group);
- if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
+ if (gdp->bg_flags & cpu_to_le16(EXT4_BG_TRIMMED) &&
minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
goto out;
+ freed_last_trimmed_orig = grp->bb_freed_since_trim;
start = (e4b.bd_info->bb_first_free > start) ?
e4b.bd_info->bb_first_free : start;
@@ -5938,14 +5957,54 @@ ext4_trim_all_free(struct super_block *s
break;
}
- if (!ret) {
+ if (!ret)
ret = count;
- EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
- }
out:
ext4_unlock_group(sb, group);
ext4_mb_unload_buddy(&e4b);
+ if (ret > 0) {
+ handle_t *handle = NULL;
+ int err;
+
+ if (track_trim) {
+ 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, gd_bh);
+ if (err) {
+ ret = err;
+ goto out_journal;
+ }
+ }
+
+ ext4_lock_group(sb, group);
+ /* someone freed blocks while we were working on the group */
+ if (freed_last_trimmed_orig != grp->bb_freed_since_trim) {
+ ext4_unlock_group(sb, group);
+ goto out_journal;
+ }
+ gdp->bg_flags |= cpu_to_le16(EXT4_BG_TRIMMED);
+ grp->bb_freed_since_trim = 0;
+ ext4_group_desc_csum_set(sb, group, gdp);
+ ext4_unlock_group(sb, group);
+
+ if (track_trim) {
+ err = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
+ if (err)
+ ret = err;
+ }
+out_journal:
+ if (track_trim) {
+ err = ext4_journal_stop(handle);
+ if (err)
+ ret = err;
+ }
+ }
+
+out_return:
ext4_debug("trimmed %d blocks in the group %d\n",
count, group);
Index: linux-4.18.0-425.10.1.el8_7/fs/ext4/sysfs.c
===================================================================
--- linux-4.18.0-425.10.1.el8_7.orig/fs/ext4/sysfs.c
+++ linux-4.18.0-425.10.1.el8_7/fs/ext4/sysfs.c
@@ -223,6 +223,7 @@ EXT4_RW_ATTR_SBI_UI(mb_small_req, s_mb_s
EXT4_RW_ATTR_SBI_UI(mb_large_req, s_mb_large_req);
EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
+EXT4_RW_ATTR_SBI_UI(bg_trimmed_threshold, s_bg_trimmed_threshold);
EXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
EXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
EXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
@@ -262,6 +263,7 @@ static struct attribute *ext4_attrs[] =
ATTR_LIST(mb_group_prealloc),
ATTR_LIST(max_writeback_mb_bump),
ATTR_LIST(extent_max_zeroout_kb),
+ ATTR_LIST(bg_trimmed_threshold),
ATTR_LIST(trigger_fs_error),
ATTR_LIST(err_ratelimit_interval_ms),
ATTR_LIST(err_ratelimit_burst),
Index: linux-4.18.0-425.10.1.el8_7/fs/ext4/super.c
===================================================================
--- linux-4.18.0-425.10.1.el8_7.orig/fs/ext4/super.c
+++ linux-4.18.0-425.10.1.el8_7/fs/ext4/super.c
@@ -2703,6 +2703,10 @@ static int ext4_check_descriptors(struct
return 0;
}
}
+ if (!(sbi->s_es->s_flags & cpu_to_le32(EXT2_FLAGS_TRACK_TRIM))) {
+ gdp->bg_flags &= cpu_to_le16(~EXT4_BG_TRIMMED);
+ ext4_group_desc_csum_set(sb, i, gdp);
+ }
ext4_unlock_group(sb, i);
if (!flexbg_flag)
first_block += EXT4_BLOCKS_PER_GROUP(sb);