Skip to contentSkip to conversation

EXT4 trim bug fixes and improvement.

PATCH Superseded Tao Ma 4 participants 19 messages Feb 9, 2011 – Feb 24, 2011

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tao Ma

author
#0 lore raw

cover EXT4 trim bug fixes and improvement.

Hi Ted and the list,
Here are 4 patches for trim support in ext4.
Patch 1/4 is a fix for the 'len' overflow. As we have decided that
we don't adjust start to s_first_data_block, this patch is still needed.
Patch 2/4 is a speedup for trim in one group.
Patch 3/4 adds trace points for the functions used in FITRIM. Patch 4/4 is a speedup for trim in the whole volume. For more
details, please check the commit log.

Regards,
Tao

author
#1 lore raw

PATCH 1/4 ext4: fix trim length underflow with small trim length.

From: Tao Ma <[email protected]>

In 0f0a25b, we adjust 'len' with s_first_data_block - start, but it could underflow in case blocksize=1K, fstrim_range.len=512 and fstrim_range.start = 0. In this case, when we run the code:
len -= first_data_blk - start; len will be underflow to -1ULL. In the end, although we are safe that last_group check later will limit the trim to the whole volume, but that isn't what the user really want.

So this patch fix it. It also adds the check for 'start' like ext3 so that we can break immediately if the start is invalid.

---
 fs/ext4/mballoc.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
fs/ext4/mballoc.c +4 -0
index 02cff4a..94e9f23 100644
@@ -4839,6 +4839,9 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
 
 	if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)))
 		return -EINVAL;
+	if (start >= ext4_blocks_count(EXT4_SB(sb)->s_es) ||
+	    start + len <= first_data_blk)
+		goto out;
 	if (start < first_data_blk) {
 		len -= first_data_blk - start;
 		start = first_data_blk;
@@ -4883,5 +4886,6 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
 	}
 	range->len = trimmed * sb->s_blocksize;
 
+out:
 	return ret;
 }
Signature · 1 line
1.6.3.GIT
author
#2 lore raw

PATCH 2/4 ext4: speed up group trim with the right free block count.

In reply to Tao Ma

From: Tao Ma <[email protected]>

When we trim some free blocks in a group of ext4, we should calculate the free blocks properly and check whether there are enough freed blocks left for us to trim. Current solution will only calculate free spaces if they are large for a trim which isn't appropriate.

Let us see a small example:
a group has 1.5M free which are 300k, 300k, 300k, 300k, 300k. And minblocks is 1M. With current solution, we have to iterate the whole group since these 300k will never be subtracted from 1.5M. But actually we should exit after we find the first 2 free spaces since the left 3 chunks only sum up to 900K if we subtract the first 600K although they can't be trimed.

---
 fs/ext4/mballoc.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
fs/ext4/mballoc.c +3 -2
index 94e9f23..806adb2 100644
@@ -4757,7 +4757,7 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 		ext4_grpblk_t start, ext4_grpblk_t max, ext4_grpblk_t minblocks)
 {
 	void *bitmap;
-	ext4_grpblk_t next, count = 0;
+	ext4_grpblk_t next, count = 0, free_count = 0;
 	ext4_group_t group;
 	int ret = 0;
 
@@ -4782,6 +4782,7 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 				break;
 			count += next - start;
 		}
+		free_count += next - start;
 		start = next + 1;
 
 		if (fatal_signal_pending(current)) {
@@ -4795,7 +4796,7 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 			ext4_lock_group(sb, group);
 		}
 
-		if ((e4b->bd_info->bb_free - count) < minblocks)
+		if ((e4b->bd_info->bb_free - free_count) < minblocks)
 			break;
 	}
 	ext4_unlock_group(sb, group);
Signature · 1 line
1.6.3.GIT
author
#3 lore raw

PATCH 3/4 ext4: Add new ext4 trim tracepoints

In reply to Tao Ma

From: Tao Ma <[email protected]>

Add ext4_trim_extent and ext4_trim_all_free.

---
 fs/ext4/mballoc.c           |    5 ++++
 include/trace/events/ext4.h |   49 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 0 deletions(-)
fs/ext4/mballoc.c +5 -0
index 806adb2..4eadac8 100644
@@ -4715,6 +4715,8 @@ static int ext4_trim_extent(struct super_block *sb, int start, int count,
 	struct ext4_free_extent ex;
 	int ret = 0;
 
+	trace_ext4_trim_extent(sb, group, start, count);
+
 	assert_spin_locked(ext4_group_lock_ptr(sb, group));
 
 	ex.fe_start = start;
@@ -4767,8 +4769,11 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 	group = e4b->bd_group;
 	start = (e4b->bd_info->bb_first_free > start) ?
 		e4b->bd_info->bb_first_free : start;
+
 	ext4_lock_group(sb, group);
 
+	trace_ext4_trim_all_free(sb, group, start, max);
+
 	while (start < max) {
 		start = mb_find_next_zero_bit(bitmap, max, start);
 		if (start >= max)
include/trace/events/ext4.h +49 -0

55 lines not on this page · raw message

Signature · 1 line
1.6.3.GIT
author
#4 lore raw

PATCH 4/4 ext4: Speed up FITRIM by recording flags in ext4_group_info.

In reply to Tao Ma

From: Tao Ma <[email protected]>

In ext4, when FITRIM is called every time, we iterate all the groups and do trim one by one. It is a bit time wasting if the group has been trimmed and there is no change since the last trim.

So this patch adds a new flag in ext4_group_info->bb_state to indicate that the group has been trimmed, and it will be cleared if some blocks is freed(in release_blocks_on_commit). Another trim_minlen is added in ext4_sb_info to record the last minlen we use to trim the volume, so that if the caller provide a small one, we will go on the trim regardless of the bb_state. The idea is inspired by Andreas and Lukas Czerner.

Cc Andreas Dilger <[email protected]>
Cc Lukas Czerner <[email protected]>
---
 fs/ext4/ext4.h    |    8 +++++++-
 fs/ext4/mballoc.c |   22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletions(-)
fs/ext4/ext4.h +7 -1
index 0c8d97b..684f48e 100644
@@ -1200,6 +1200,9 @@ struct ext4_sb_info {
 	struct ext4_li_request *s_li_request;
 	/* Wait multiplier for lazy initialization thread */
 	unsigned int s_li_wait_mult;
+
+	/* record the last minlen when FITRIM is called. */
+	u64 trim_minlen;
 };
 
 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -1970,10 +1973,13 @@ struct ext4_group_info {
 					 * 5 free 8-block regions. */
 };
 
-#define EXT4_GROUP_INFO_NEED_INIT_BIT	0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_HAS_BEEN_TRIMMED	1
 
 #define EXT4_MB_GRP_NEED_INIT(grp)	\
 	(test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
+#define EXT4_MB_GRP_HAS_BEEN_TRIMMED(grp)	\
+	(test_bit(EXT4_GROUP_INFO_HAS_BEEN_TRIMMED, &((grp)->bb_state)))
 
 #define EXT4_MAX_CONTENTION		8
 #define EXT4_CONTENTION_THRESHOLD	2
fs/ext4/mballoc.c +22 -0
index 4eadac8..3f6b809 100644
@@ -1563,6 +1563,9 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
 	mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
 	mb_check_buddy(e4b);
 
+	if (!ret)
+		EXT4_SB(sb)->trim_minlen = minlen;
+
 	return ret;
 }
 
@@ -2687,6 +2690,16 @@ static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
 		rb_erase(&entry->node, &(db->bb_free_root));
 		mb_free_blocks(NULL, &e4b, entry->start_blk, entry->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))
+			clear_bit(EXT4_GROUP_INFO_HAS_BEEN_TRIMMED,
+				  &(db->bb_state));
+
 		if (!db->bb_free_root.rb_node) {
 			/* No more items in the per group rb tree
 			 * balance refcounts from ext4_mb_free_metadata()
@@ -4772,6 +4785,10 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 
 	ext4_lock_group(sb, group);
 
+	if (EXT4_MB_GRP_HAS_BEEN_TRIMMED(e4b->bd_info) &&
+	    minblocks >= EXT4_SB(sb)->trim_minlen)
+		goto out;
+
 	trace_ext4_trim_all_free(sb, group, start, max);
 
 	while (start < max) {
@@ -4804,6 +4821,11 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 		if ((e4b->bd_info->bb_free - free_count) < minblocks)
 			break;
 	}
+
+	if (!ret)
+		set_bit(EXT4_GROUP_INFO_HAS_BEEN_TRIMMED,
+			&(e4b->bd_info->bb_state));
+out:
 	ext4_unlock_group(sb, group);
 
 	ext4_debug("trimmed %d blocks in the group %d\n",
Signature · 1 line
1.6.3.GIT
#5 lore raw

Re: [PATCH 4/4] ext4: Speed up FITRIM by recording flags in ext4_group_info.

Quoted 7 lines · Tao Ma

From: Tao Ma <[email protected]>

In ext4, when FITRIM is called every time, we iterate all the groups and do trim one by one. It is a bit time wasting if the group has been trimmed and there is no change since the last trim.

Quoted 7 lines

So this patch adds a new flag in ext4_group_info->bb_state to indicate that the group has been trimmed, and it will be cleared if some blocks is freed(in release_blocks_on_commit). Another trim_minlen is added in ext4_sb_info to record the last minlen we use to trim the volume, so that if the caller provide a small one, we will go on the trim regardless of the bb_state. The idea is inspired by Andreas and Lukas Czerner.

Hi Tao,

This is great, thanks for the patch! Do you have any benchmark numbers showing how much does it improve FSTRIM time ? It seems it might help a lot. Couple of comments bellow.

Quoted 14 lines
Cc Andreas Dilger <[email protected]>
Cc Lukas Czerner <[email protected]>
---
 fs/ext4/ext4.h    |    8 +++++++-
 fs/ext4/mballoc.c |   22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletions(-)
fs/ext4/ext4.h +0 -0
index 0c8d97b..684f48e 100644

fs/ext4/ext4.h struct ext4_sb_info { · line 1205

Quoted 6 lines
fs/ext4/ext4.h +3 -0
@@ -1200,6 +1200,9 @@ struct ext4_sb_info {
 	struct ext4_li_request *s_li_request;
 	/* Wait multiplier for lazy initialization thread */
 	unsigned int s_li_wait_mult;
+
+	/* record the last minlen when FITRIM is called. */
+	u64 trim_minlen;

Maybe this is a bit nitpicking, but it is not very clear what is the "trim_minlen" for. I would rather use something like "last_trim_minblks", or similar.

Quoted 23 lines
fs/ext4/ext4.h +4 -1
 };
 
 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -1970,10 +1973,13 @@ struct ext4_group_info {
 					 * 5 free 8-block regions. */
 };
 
-#define EXT4_GROUP_INFO_NEED_INIT_BIT	0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_HAS_BEEN_TRIMMED	1
 
 #define EXT4_MB_GRP_NEED_INIT(grp)	\
 	(test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
+#define EXT4_MB_GRP_HAS_BEEN_TRIMMED(grp)	\
+	(test_bit(EXT4_GROUP_INFO_HAS_BEEN_TRIMMED, &((grp)->bb_state)))
 
 #define EXT4_MAX_CONTENTION		8
 #define EXT4_CONTENTION_THRESHOLD	2
fs/ext4/mballoc.c +0 -0
index 4eadac8..3f6b809 100644

fs/ext4/mballoc.c static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex) · line 1568

Quoted 6 lines
fs/ext4/mballoc.c +3 -0
@@ -1563,6 +1563,9 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
 	mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
 	mb_check_buddy(e4b);
 
+	if (!ret)
+		EXT4_SB(sb)->trim_minlen = minlen;
+

Did you even tried to compile it ? "minlen" is not defined in mb_mark_used(), also it does make sense to set trim_minlen there. Did not you meant it to be in ext4_trim_fs ?

Quoted 21 lines
fs/ext4/mballoc.c +10 -0
 	return ret;
 }
 
@@ -2687,6 +2690,16 @@ static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
 		rb_erase(&entry->node, &(db->bb_free_root));
 		mb_free_blocks(NULL, &e4b, entry->start_blk, entry->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))
+			clear_bit(EXT4_GROUP_INFO_HAS_BEEN_TRIMMED,
+				  &(db->bb_state));
+
 		if (!db->bb_free_root.rb_node) {
 			/* No more items in the per group rb tree
 			 * balance refcounts from ext4_mb_free_metadata()

fs/ext4/mballoc.c ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b, · line 4791

Quoted 7 lines
fs/ext4/mballoc.c +4 -0
@@ -4772,6 +4785,10 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 
 	ext4_lock_group(sb, group);
 
+	if (EXT4_MB_GRP_HAS_BEEN_TRIMMED(e4b->bd_info) &&
+	    minblocks >= EXT4_SB(sb)->trim_minlen)
+		goto out;
+

I think this can be placed in the ext4_trim_fs() in the for() loop so we can avoid unnecessary call to ext4_trim_all_free.

Quoted 6 lines
fs/ext4/mballoc.c +0 -0
 	trace_ext4_trim_all_free(sb, group, start, max);
 
 	while (start < max) {
@@ -4804,6 +4821,11 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 		if ((e4b->bd_info->bb_free - free_count) < minblocks)

fs/ext4/mballoc.c ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b, · line 4831

Quoted 10 lines
fs/ext4/mballoc.c +5 -0
 			break;
 	}
+
+	if (!ret)
+		set_bit(EXT4_GROUP_INFO_HAS_BEEN_TRIMMED,
+			&(e4b->bd_info->bb_state));
+out:
 	ext4_unlock_group(sb, group);
 
 	ext4_debug("trimmed %d blocks in the group %d\n",

Thanks!
-Lukas

#6 lore raw

Re: [PATCH 4/4] ext4: Speed up FITRIM by recording flags in ext4_group_info.

Quoted 11 lines · Lukas Czerner

fs/ext4/ext4.h struct ext4_sb_info { · line 1205

Quoted 7 lines
fs/ext4/ext4.h +3 -0
@@ -1200,6 +1200,9 @@ struct ext4_sb_info {
 	struct ext4_li_request *s_li_request;
 	/* Wait multiplier for lazy initialization thread */
 	unsigned int s_li_wait_mult;
+
+	/* record the last minlen when FITRIM is called. */
+	u64 trim_minlen;

Maybe this is a bit nitpicking, but it is not very clear what is the "trim_minlen" for. I would rather use something like "last_trim_minblks", or similar.

And for good measure, this should use the "s_" prefix to indicate it is in the superblock.

Quoted 2 lines

fs/ext4/ext4.h struct ext4_group_info { · line 1977

Quoted 2 lines
fs/ext4/ext4.h +2 -0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_HAS_BEEN_TRIMMED	1

It would also be good if the names of these fields were consistent, like

+#define EXT4_GROUP_INFO_NEED_INIT_BIT 0
+#define EXT4_GROUP_INFO_WAS_TRIMMED_BIT 1

Quoted 11 lines

fs/ext4/mballoc.c static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex) · line 1568

Quoted 7 lines
fs/ext4/mballoc.c +3 -0
@@ -1563,6 +1563,9 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
 	mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
 	mb_check_buddy(e4b);
 
+	if (!ret)
+		EXT4_SB(sb)->trim_minlen = minlen;
+

Did you even tried to compile it ? "minlen" is not defined in mb_mark_used(), also it does make sense to set trim_minlen there. Did not you meant it to be in ext4_trim_fs ?

I was wondering exactly the same thing, but I haven't had time to look at the code.

Cheers, Andreas

author
#7 lore raw

Re: [PATCH 4/4] ext4: Speed up FITRIM by recording flags in ext4_group_info.

Quoted 14 lines · Andreas Dilger
Quoted 12 lines · Lukas Czerner

fs/ext4/ext4.h struct ext4_sb_info { · line 1206

Quoted 8 lines
fs/ext4/ext4.h +3 -0
@@ -1200,6 +1200,9 @@ struct ext4_sb_info {
 	struct ext4_li_request *s_li_request;
 	/* Wait multiplier for lazy initialization thread */
 	unsigned int s_li_wait_mult;
+
+	/* record the last minlen when FITRIM is called. */
+	u64 trim_minlen;
 

Maybe this is a bit nitpicking, but it is not very clear what is the "trim_minlen" for. I would rather use something like "last_trim_minblks", or similar.

Quoted 2 lines

And for good measure, this should use the "s_" prefix to indicate it is in the superblock.

ok.

Review

Quoted 9 lines
Quoted 4 lines

fs/ext4/ext4.h struct ext4_group_info { · line 1977

Quoted 4 lines
fs/ext4/ext4.h +2 -0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_HAS_BEEN_TRIMMED	1

It would also be good if the names of these fields were consistent, like

(quoted diff) +2 -0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_WAS_TRIMMED_BIT	1

thanks for the advice.

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
fs/ext4/mballoc.c +0 -0
@@ -1563,6 +1563,9 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
 	mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
 	mb_check_buddy(e4b);
 
Quoted 8 lines

fs/ext4/mballoc.c static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex) · line 1571

Quoted 4 lines
fs/ext4/mballoc.c +3 -0
+	if (!ret)
+		EXT4_SB(sb)->trim_minlen = minlen;
+
 

Did you even tried to compile it ? "minlen" is not defined in mb_mark_used(), also it does make sense to set trim_minlen there. Did not you meant it to be in ext4_trim_fs ?

Quoted 2 lines

I was wondering exactly the same thing, but I haven't had time to look at the code.

Sorry, it seems to be caused by my rebase. The right place should be in the end of ext4_trim_fs.

Regards,
Tao

Quoted 11 lines

Cheers, Andreas

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#8 lore raw

Re: [PATCH 4/4] ext4: Speed up FITRIM by recording flags in ext4_group_info.

In reply to Lukas Czerner
Quoted 18 lines · Lukas Czerner
Quoted 7 lines · Tao Ma

From: Tao Ma<[email protected]>

In ext4, when FITRIM is called every time, we iterate all the groups and do trim one by one. It is a bit time wasting if the group has been trimmed and there is no change since the last trim.

Quoted 8 lines

So this patch adds a new flag in ext4_group_info->bb_state to indicate that the group has been trimmed, and it will be cleared if some blocks is freed(in release_blocks_on_commit). Another trim_minlen is added in ext4_sb_info to record the last minlen we use to trim the volume, so that if the caller provide a small one, we will go on the trim regardless of the bb_state. The idea is inspired by Andreas and Lukas Czerner.

Quoted 6 lines

Hi Tao,

This is great, thanks for the patch! Do you have any benchmark numbers showing how much does it improve FSTRIM time ? It seems it might help a lot. Couple of comments bellow.

I have a intel x25m ssd. I will try to collect some data about it and update them in the commit log.

Quoted 9 lines
Quoted 9 lines
Cc Andreas Dilger<[email protected]>
Cc Lukas Czerner<[email protected]>
---
  fs/ext4/ext4.h    |    8 +++++++-
  fs/ext4/mballoc.c |   22 ++++++++++++++++++++++
  2 files changed, 29 insertions(+), 1 deletions(-)
Quoted 16 lines

fs/ext4/ext4.h struct ext4_sb_info { · line 1206

Quoted 12 lines
fs/ext4/ext4.h +3 -0
index 0c8d97b..684f48e 100644
@@ -1200,6 +1200,9 @@ struct ext4_sb_info {
  	struct ext4_li_request *s_li_request;
  	/* Wait multiplier for lazy initialization thread */
  	unsigned int s_li_wait_mult;
+
+	/* record the last minlen when FITRIM is called. */
+	u64 trim_minlen;
 

Maybe this is a bit nitpicking, but it is not very clear what is the "trim_minlen" for. I would rather use something like "last_trim_minblks", or similar.

ok, I will change it in the next round.

Quoted 27 lines
Quoted 27 lines
fs/ext4/ext4.h +0 -0
  };
 

static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)

fs/ext4/ext4.h +4 -1
@@ -1970,10 +1973,13 @@ struct ext4_group_info {
  					 * 5 free 8-block regions. */
  };
 
-#define EXT4_GROUP_INFO_NEED_INIT_BIT	0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_HAS_BEEN_TRIMMED	1
 
  #define EXT4_MB_GRP_NEED_INIT(grp)	\
  	(test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,&((grp)->bb_state)))
+#define EXT4_MB_GRP_HAS_BEEN_TRIMMED(grp)	\
+	(test_bit(EXT4_GROUP_INFO_HAS_BEEN_TRIMMED,&((grp)->bb_state)))
 
  #define EXT4_MAX_CONTENTION		8
  #define EXT4_CONTENTION_THRESHOLD	2
fs/ext4/mballoc.c +0 -0
index 4eadac8..3f6b809 100644
@@ -1563,6 +1563,9 @@ static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
  	mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
  	mb_check_buddy(e4b);
 
Quoted 8 lines

fs/ext4/mballoc.c static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex) · line 1571

Quoted 4 lines
fs/ext4/mballoc.c +3 -0
+	if (!ret)
+		EXT4_SB(sb)->trim_minlen = minlen;
+
 

Did you even tried to compile it ? "minlen" is not defined in mb_mark_used(), also it does make sense to set trim_minlen there. Did not you meant it to be in ext4_trim_fs ?

oh, sorry, this line should be in the function ext4_trim_fs. I just rebased my branch to the lastest kernel to see whether it conflicts. I don't know why they are moved here. So the right position is:
@@ -4892,6 +4911,9 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)

        }
        range->len = trimmed * sb->s_blocksize;

+ if (!ret)
+ EXT4_SB(sb)->trim_minlen = minlen;
+
out:

        return ret;

}

Sorry for the trouble.

Quoted 25 lines
Quoted 25 lines
fs/ext4/mballoc.c +10 -0
  	return ret;
  }
@@ -2687,6 +2690,16 @@ static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
  		rb_erase(&entry->node,&(db->bb_free_root));
  		mb_free_blocks(NULL,&e4b, entry->start_blk, entry->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))
+			clear_bit(EXT4_GROUP_INFO_HAS_BEEN_TRIMMED,
+				&(db->bb_state));
+
  		if (!db->bb_free_root.rb_node) {
  			/* No more items in the per group rb tree
  			 * balance refcounts from ext4_mb_free_metadata()
@@ -4772,6 +4785,10 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 
  	ext4_lock_group(sb, group);
 
Quoted 8 lines

fs/ext4/mballoc.c ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b, · line 4793

Quoted 5 lines
fs/ext4/mballoc.c +4 -0
+	if (EXT4_MB_GRP_HAS_BEEN_TRIMMED(e4b->bd_info)&&
+	    minblocks>= EXT4_SB(sb)->trim_minlen)
+		goto out;
+
 

I think this can be placed in the ext4_trim_fs() in the for() loop so we can avoid unnecessary call to ext4_trim_all_free.

fair enough. I will update it.

Regards,
Tao

author
#9 lore raw

Re: [PATCH 4/4] ext4: Speed up FITRIM by recording flags in ext4_group_info.

In reply to Lukas Czerner

Hi Lukas,

Quoted 24 lines · Lukas Czerner
Quoted 7 lines · Tao Ma

From: Tao Ma<[email protected]>

In ext4, when FITRIM is called every time, we iterate all the groups and do trim one by one. It is a bit time wasting if the group has been trimmed and there is no change since the last trim.

Quoted 8 lines

So this patch adds a new flag in ext4_group_info->bb_state to indicate that the group has been trimmed, and it will be cleared if some blocks is freed(in release_blocks_on_commit). Another trim_minlen is added in ext4_sb_info to record the last minlen we use to trim the volume, so that if the caller provide a small one, we will go on the trim regardless of the bb_state. The idea is inspired by Andreas and Lukas Czerner.

Hi Tao,

This is great, thanks for the patch! Do you have any benchmark numbers showing how much does it improve FSTRIM time ? It seems it might help a lot. Couple of comments bellow.

<snip>

Quoted 5 lines
Quoted 5 lines
fs/ext4/mballoc.c +0 -0
@@ -4772,6 +4785,10 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 
  	ext4_lock_group(sb, group);
 
Quoted 8 lines

fs/ext4/mballoc.c ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b, · line 4793

Quoted 5 lines
fs/ext4/mballoc.c +4 -0
+	if (EXT4_MB_GRP_HAS_BEEN_TRIMMED(e4b->bd_info)&&
+	    minblocks>= EXT4_SB(sb)->trim_minlen)
+		goto out;
+
 

I think this can be placed in the ext4_trim_fs() in the for() loop so we can avoid unnecessary call to ext4_trim_all_free.

I remembered why I put the check here. We have to check this flag with the group locked. So in ext4_trim_fs, we don't lock the group and it is unsafe to check it there since we may free some blocks and clear the bit in the same time in release_blocks_on_commit.

Regards,
Tao

author
#10 lore raw

PATCH v2 4/4 ext4: Speed up FITRIM by recording flags in ext4_group_info.

In reply to Tao Ma

From: Tao Ma <[email protected]>

In ext4, when FITRIM is called every time, we iterate all the groups and do trim one by one. It is a bit time wasting if the group has been trimmed and there is no change since the last trim.

So this patch adds a new flag in ext4_group_info->bb_state to indicate that the group has been trimmed, and it will be cleared if some blocks is freed(in release_blocks_on_commit). Another trim_minlen is added in ext4_sb_info to record the last minlen we use to trim the volume, so that if the caller provide a small one, we will go on the trim regardless of the bb_state.

A simple test with my intel x25m ssd:
df -h shows:
/dev/sdb2 108G 35G 68G 34% /mnt/ext4
Block size: 4096

run the FITRIM with the following parameter:
range.start = 0;
range.len = UINT64_MAX;
range.minlen = 1048576;

without the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m4.039s
user 0m0.000s
sys 0m1.020s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.577s
user 0m0.001s
sys 0m1.004s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.380s
user 0m0.000s
sys 0m0.991s

with the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.466s
user 0m0.000s
sys 0m0.966s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.001s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.000s

A big improvement for the 2nd and 3rd run.

After I delete some big image files and re-run the trim, it is still much faster than iterating the whole disk.
/dev/sdb2 108G 25G 78G 24% /mnt/ext4

[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.513s
user 0m0.000s
sys 0m0.069s

Cc Andreas Dilger <[email protected]>
Cc Lukas Czerner <[email protected]>
---
 fs/ext4/ext4.h    |    8 +++++++-
 fs/ext4/mballoc.c |   22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletions(-)
fs/ext4/ext4.h +7 -1
index 0c8d97b..1d59a63 100644
@@ -1200,6 +1200,9 @@ struct ext4_sb_info {
 	struct ext4_li_request *s_li_request;
 	/* Wait multiplier for lazy initialization thread */
 	unsigned int s_li_wait_mult;
+
+	/* record the last minlen when FITRIM is called. */
+	u64 s_last_trim_minblks;
 };
 
 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -1970,10 +1973,13 @@ struct ext4_group_info {
 					 * 5 free 8-block regions. */
 };
 
-#define EXT4_GROUP_INFO_NEED_INIT_BIT	0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_WAS_TRIMMED_BIT		1
 
 #define EXT4_MB_GRP_NEED_INIT(grp)	\
 	(test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
+#define EXT4_MB_GRP_HAS_BEEN_TRIMMED(grp)	\
+	(test_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
 
 #define EXT4_MAX_CONTENTION		8
 #define EXT4_CONTENTION_THRESHOLD	2
fs/ext4/mballoc.c +22 -0
index 4eadac8..c7aa094 100644
@@ -2687,6 +2687,16 @@ static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
 		rb_erase(&entry->node, &(db->bb_free_root));
 		mb_free_blocks(NULL, &e4b, entry->start_blk, entry->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))
+			clear_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT,
+				  &(db->bb_state));
+
 		if (!db->bb_free_root.rb_node) {
 			/* No more items in the per group rb tree
 			 * balance refcounts from ext4_mb_free_metadata()
@@ -4772,6 +4782,10 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 
 	ext4_lock_group(sb, group);
 
+	if (EXT4_MB_GRP_HAS_BEEN_TRIMMED(e4b->bd_info) &&
+	    minblocks >= EXT4_SB(sb)->s_last_trim_minblks)
+		goto out;
+
 	trace_ext4_trim_all_free(sb, group, start, max);
 
 	while (start < max) {
@@ -4804,6 +4818,11 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 		if ((e4b->bd_info->bb_free - free_count) < minblocks)
 			break;
 	}
+
+	if (!ret)
+		set_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT,
+			&(e4b->bd_info->bb_state));
+out:
 	ext4_unlock_group(sb, group);
 
 	ext4_debug("trimmed %d blocks in the group %d\n",
@@ -4892,6 +4911,9 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
 	}
 	range->len = trimmed * sb->s_blocksize;
 
+	if (!ret)
+		EXT4_SB(sb)->s_last_trim_minblks = minlen;
+
 out:
 	return ret;
 }
Signature · 1 line
1.6.3.GIT
#11 lore raw

Re: [PATCH 4/4 v2] ext4: Speed up FITRIM by recording flags in ext4_group_info.

Quoted 54 lines · Tao Ma

From: Tao Ma <[email protected]>

In ext4, when FITRIM is called every time, we iterate all the groups and do trim one by one. It is a bit time wasting if the group has been trimmed and there is no change since the last trim.

So this patch adds a new flag in ext4_group_info->bb_state to indicate that the group has been trimmed, and it will be cleared if some blocks is freed(in release_blocks_on_commit). Another trim_minlen is added in ext4_sb_info to record the last minlen we use to trim the volume, so that if the caller provide a small one, we will go on the trim regardless of the bb_state.

A simple test with my intel x25m ssd:
df -h shows:
/dev/sdb2 108G 35G 68G 34% /mnt/ext4
Block size: 4096

run the FITRIM with the following parameter:
range.start = 0;
range.len = UINT64_MAX;
range.minlen = 1048576;

without the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m4.039s
user 0m0.000s
sys 0m1.020s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.577s
user 0m0.001s
sys 0m1.004s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.380s
user 0m0.000s
sys 0m0.991s

with the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.466s
user 0m0.000s
sys 0m0.966s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.001s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.000s

A big improvement for the 2nd and 3rd run.

Quoted 8 lines

After I delete some big image files and re-run the trim, it is still much faster than iterating the whole disk.
/dev/sdb2 108G 25G 78G 24% /mnt/ext4

[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.513s
user 0m0.000s
sys 0m0.069s

Great it looks really good.

Quoted 83 lines
Cc Andreas Dilger <[email protected]>
Cc Lukas Czerner <[email protected]>
---
 fs/ext4/ext4.h    |    8 +++++++-
 fs/ext4/mballoc.c |   22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletions(-)
fs/ext4/ext4.h +7 -1
index 0c8d97b..1d59a63 100644
@@ -1200,6 +1200,9 @@ struct ext4_sb_info {
 	struct ext4_li_request *s_li_request;
 	/* Wait multiplier for lazy initialization thread */
 	unsigned int s_li_wait_mult;
+
+	/* record the last minlen when FITRIM is called. */
+	u64 s_last_trim_minblks;
 };
 
 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -1970,10 +1973,13 @@ struct ext4_group_info {
 					 * 5 free 8-block regions. */
 };
 
-#define EXT4_GROUP_INFO_NEED_INIT_BIT	0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_WAS_TRIMMED_BIT		1
 
 #define EXT4_MB_GRP_NEED_INIT(grp)	\
 	(test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
+#define EXT4_MB_GRP_HAS_BEEN_TRIMMED(grp)	\
+	(test_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
 
 #define EXT4_MAX_CONTENTION		8
 #define EXT4_CONTENTION_THRESHOLD	2
fs/ext4/mballoc.c +19 -0
index 4eadac8..c7aa094 100644
@@ -2687,6 +2687,16 @@ static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
 		rb_erase(&entry->node, &(db->bb_free_root));
 		mb_free_blocks(NULL, &e4b, entry->start_blk, entry->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))
+			clear_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT,
+				  &(db->bb_state));
+
 		if (!db->bb_free_root.rb_node) {
 			/* No more items in the per group rb tree
 			 * balance refcounts from ext4_mb_free_metadata()
@@ -4772,6 +4782,10 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 
 	ext4_lock_group(sb, group);
 
+	if (EXT4_MB_GRP_HAS_BEEN_TRIMMED(e4b->bd_info) &&
+	    minblocks >= EXT4_SB(sb)->s_last_trim_minblks)
+		goto out;
+
 	trace_ext4_trim_all_free(sb, group, start, max);
 
 	while (start < max) {
@@ -4804,6 +4818,11 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 		if ((e4b->bd_info->bb_free - free_count) < minblocks)
 			break;
 	}
+
+	if (!ret)
+		set_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT,
+			&(e4b->bd_info->bb_state));
+out:
 	ext4_unlock_group(sb, group);
 
 	ext4_debug("trimmed %d blocks in the group %d\n",

fs/ext4/mballoc.c int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) · line 4916

Quoted 6 lines
fs/ext4/mballoc.c +3 -0
@@ -4892,6 +4911,9 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
 	}
 	range->len = trimmed * sb->s_blocksize;
 
+	if (!ret)
+		EXT4_SB(sb)->s_last_trim_minblks = minlen;
+

Since this is not protected by any lock, would not it race in case of multiple FITRIM calls ?

fs/ext4/mballoc.c int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) · line 4919

Quoted 4 lines
fs/ext4/mballoc.c +0 -0
 out:
 	return ret;
 }

Thanks!
-Lukas

#12 lore raw

Re: [PATCH 4/4 v2] ext4: Speed up FITRIM by recording flags in ext4_group_info.

Quoted 157 lines · Tao Ma
Quoted 55 lines

From: Tao Ma <[email protected]>

In ext4, when FITRIM is called every time, we iterate all the groups and do trim one by one. It is a bit time wasting if the group has been trimmed and there is no change since the last trim.

So this patch adds a new flag in ext4_group_info->bb_state to indicate that the group has been trimmed, and it will be cleared if some blocks is freed(in release_blocks_on_commit). Another trim_minlen is added in ext4_sb_info to record the last minlen we use to trim the volume, so that if the caller provide a small one, we will go on the trim regardless of the bb_state.

A simple test with my intel x25m ssd:
df -h shows:
/dev/sdb2 108G 35G 68G 34% /mnt/ext4
Block size: 4096

run the FITRIM with the following parameter:
range.start = 0;
range.len = UINT64_MAX;
range.minlen = 1048576;

without the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m4.039s
user 0m0.000s
sys 0m1.020s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.577s
user 0m0.001s
sys 0m1.004s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.380s
user 0m0.000s
sys 0m0.991s

with the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.466s
user 0m0.000s
sys 0m0.966s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.001s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.000s

A big improvement for the 2nd and 3rd run.

Quoted 8 lines

After I delete some big image files and re-run the trim, it is still much faster than iterating the whole disk.
/dev/sdb2 108G 25G 78G 24% /mnt/ext4

[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.513s
user 0m0.000s
sys 0m0.069s

Great it looks really good.

Quoted 90 lines
Cc Andreas Dilger <[email protected]>
Cc Lukas Czerner <[email protected]>
---
 fs/ext4/ext4.h    |    8 +++++++-
 fs/ext4/mballoc.c |   22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletions(-)
fs/ext4/ext4.h +7 -1
index 0c8d97b..1d59a63 100644
@@ -1200,6 +1200,9 @@ struct ext4_sb_info {
 	struct ext4_li_request *s_li_request;
 	/* Wait multiplier for lazy initialization thread */
 	unsigned int s_li_wait_mult;
+
+	/* record the last minlen when FITRIM is called. */
+	u64 s_last_trim_minblks;
 };
 
 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -1970,10 +1973,13 @@ struct ext4_group_info {
 					 * 5 free 8-block regions. */
 };
 
-#define EXT4_GROUP_INFO_NEED_INIT_BIT	0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_WAS_TRIMMED_BIT		1
 
 #define EXT4_MB_GRP_NEED_INIT(grp)	\
 	(test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
+#define EXT4_MB_GRP_HAS_BEEN_TRIMMED(grp)	\
+	(test_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
 
 #define EXT4_MAX_CONTENTION		8
 #define EXT4_CONTENTION_THRESHOLD	2
fs/ext4/mballoc.c +10 -0
index 4eadac8..c7aa094 100644
@@ -2687,6 +2687,16 @@ static void release_blocks_on_commit(journal_t
 *journal, transaction_t *txn)
 		rb_erase(&entry->node, &(db->bb_free_root));
 		mb_free_blocks(NULL, &e4b, entry->start_blk, entry->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))
+			clear_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT,
+				  &(db->bb_state));
+
 		if (!db->bb_free_root.rb_node) {
 			/* No more items in the per group rb tree
fs/ext4/mballoc.c +4 -0
 			 * balance refcounts from ext4_mb_free_metadata()
@@ -4772,6 +4782,10 @@ ext4_grpblk_t ext4_trim_all_free(struct
 super_block *sb, struct ext4_buddy *e4b,
 
 	ext4_lock_group(sb, group);
 
+	if (EXT4_MB_GRP_HAS_BEEN_TRIMMED(e4b->bd_info) &&
+	    minblocks >= EXT4_SB(sb)->s_last_trim_minblks)
+		goto out;
+
 	trace_ext4_trim_all_free(sb, group, start, max);
 
fs/ext4/mballoc.c +5 -0
 	while (start < max) {
@@ -4804,6 +4818,11 @@ ext4_grpblk_t ext4_trim_all_free(struct
 super_block *sb, struct ext4_buddy *e4b,
 		if ((e4b->bd_info->bb_free - free_count) < minblocks)
 			break;
 	}
+
+	if (!ret)
+		set_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT,
+			&(e4b->bd_info->bb_state));
+out:
 	ext4_unlock_group(sb, group);
 
fs/ext4/mballoc.c +0 -0
 	ext4_debug("trimmed %d blocks in the group %d\n",
@@ -4892,6 +4911,9 @@ int ext4_trim_fs(struct super_block *sb, struct
 fstrim_range *range)
 	}
 	range->len = trimmed * sb->s_blocksize;
 
Quoted 6 lines

fs/ext4/mballoc.c int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) · line 4916

Quoted 3 lines
fs/ext4/mballoc.c +3 -0
+	if (!ret)
+		EXT4_SB(sb)->s_last_trim_minblks = minlen;
+

Since this is not protected by any lock, would not it race in case of multiple FITRIM calls ?

yeah, I am also thinking of this, but I don't think we need a new lock just for this. And I guess atomic_t isn't good here because minlen is a u64.

Do you think we can use some other spin_lock in ext4 system? I am not quite familiar with ext4 by now, so do you have any suggestion?

Regards,
Tao

#13 lore raw

Re: [PATCH 4/4 v2] ext4: Speed up FITRIM by recording flags in ext4_group_info.

Quoted 167 lines · On Thu, 10 Feb 2011, [email protected] wrote:
Quoted 157 lines · Tao Ma
Quoted 55 lines

From: Tao Ma <[email protected]>

In ext4, when FITRIM is called every time, we iterate all the groups and do trim one by one. It is a bit time wasting if the group has been trimmed and there is no change since the last trim.

So this patch adds a new flag in ext4_group_info->bb_state to indicate that the group has been trimmed, and it will be cleared if some blocks is freed(in release_blocks_on_commit). Another trim_minlen is added in ext4_sb_info to record the last minlen we use to trim the volume, so that if the caller provide a small one, we will go on the trim regardless of the bb_state.

A simple test with my intel x25m ssd:
df -h shows:
/dev/sdb2 108G 35G 68G 34% /mnt/ext4
Block size: 4096

run the FITRIM with the following parameter:
range.start = 0;
range.len = UINT64_MAX;
range.minlen = 1048576;

without the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m4.039s
user 0m0.000s
sys 0m1.020s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.577s
user 0m0.001s
sys 0m1.004s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.380s
user 0m0.000s
sys 0m0.991s

with the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.466s
user 0m0.000s
sys 0m0.966s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.001s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.000s

A big improvement for the 2nd and 3rd run.

Quoted 8 lines

After I delete some big image files and re-run the trim, it is still much faster than iterating the whole disk.
/dev/sdb2 108G 25G 78G 24% /mnt/ext4

[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.513s
user 0m0.000s
sys 0m0.069s

Great it looks really good.

Quoted 90 lines
Cc Andreas Dilger <[email protected]>
Cc Lukas Czerner <[email protected]>
---
 fs/ext4/ext4.h    |    8 +++++++-
 fs/ext4/mballoc.c |   22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletions(-)
fs/ext4/ext4.h +7 -1
index 0c8d97b..1d59a63 100644
@@ -1200,6 +1200,9 @@ struct ext4_sb_info {
 	struct ext4_li_request *s_li_request;
 	/* Wait multiplier for lazy initialization thread */
 	unsigned int s_li_wait_mult;
+
+	/* record the last minlen when FITRIM is called. */
+	u64 s_last_trim_minblks;
 };
 
 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -1970,10 +1973,13 @@ struct ext4_group_info {
 					 * 5 free 8-block regions. */
 };
 
-#define EXT4_GROUP_INFO_NEED_INIT_BIT	0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_WAS_TRIMMED_BIT		1
 
 #define EXT4_MB_GRP_NEED_INIT(grp)	\
 	(test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
+#define EXT4_MB_GRP_HAS_BEEN_TRIMMED(grp)	\
+	(test_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
 
 #define EXT4_MAX_CONTENTION		8
 #define EXT4_CONTENTION_THRESHOLD	2
fs/ext4/mballoc.c +10 -0
index 4eadac8..c7aa094 100644
@@ -2687,6 +2687,16 @@ static void release_blocks_on_commit(journal_t
 *journal, transaction_t *txn)
 		rb_erase(&entry->node, &(db->bb_free_root));
 		mb_free_blocks(NULL, &e4b, entry->start_blk, entry->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))
+			clear_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT,
+				  &(db->bb_state));
+
 		if (!db->bb_free_root.rb_node) {
 			/* No more items in the per group rb tree
fs/ext4/mballoc.c +4 -0
 			 * balance refcounts from ext4_mb_free_metadata()
@@ -4772,6 +4782,10 @@ ext4_grpblk_t ext4_trim_all_free(struct
 super_block *sb, struct ext4_buddy *e4b,
 
 	ext4_lock_group(sb, group);
 
+	if (EXT4_MB_GRP_HAS_BEEN_TRIMMED(e4b->bd_info) &&
+	    minblocks >= EXT4_SB(sb)->s_last_trim_minblks)
+		goto out;
+
 	trace_ext4_trim_all_free(sb, group, start, max);
 
fs/ext4/mballoc.c +5 -0
 	while (start < max) {
@@ -4804,6 +4818,11 @@ ext4_grpblk_t ext4_trim_all_free(struct
 super_block *sb, struct ext4_buddy *e4b,
 		if ((e4b->bd_info->bb_free - free_count) < minblocks)
 			break;
 	}
+
+	if (!ret)
+		set_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT,
+			&(e4b->bd_info->bb_state));
+out:
 	ext4_unlock_group(sb, group);
 
fs/ext4/mballoc.c +0 -0
 	ext4_debug("trimmed %d blocks in the group %d\n",
@@ -4892,6 +4911,9 @@ int ext4_trim_fs(struct super_block *sb, struct
 fstrim_range *range)
 	}
 	range->len = trimmed * sb->s_blocksize;
 
Quoted 6 lines

fs/ext4/mballoc.c int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) · line 4916

Quoted 3 lines
fs/ext4/mballoc.c +3 -0
+	if (!ret)
+		EXT4_SB(sb)->s_last_trim_minblks = minlen;
+

Since this is not protected by any lock, would not it race in case of multiple FITRIM calls ?

yeah, I am also thinking of this, but I don't think we need a new lock just for this. And I guess atomic_t isn't good here because minlen is a u64.

Quoted 5 lines

Do you think we can use some other spin_lock in ext4 system? I am not quite familiar with ext4 by now, so do you have any suggestion?

Regards,
Tao

I am sorry for late answer. Even though minlen is 64-bit long, it can not be that long in ext4, because it can not be bigger than size of the allocation group (see mballoc.c:4840). I hope it helps.

I would be very happy to see this upstream, because it can help a lot!

Thanks!
-Lukas

author
#14 lore raw

PATCH v3 4/4 ext4: Speed up FITRIM by recording flags in ext4_group_info.

changlog from v2 -> v3:
change s_last_trim_minblks from u64 to atomic_t to avoid a race.

Regards,
Tao

Quoted 1 line

From 3875493dea3cc25be5460c175267f08a7ed540bc Mon Sep 17 00:00:00 2001

From: Tao Ma <[email protected]>
Date: Fri, 25 Feb 2011 06:10:26 +0800
Subject: [PATCH 4/4 V3] ext4: Speed up FITRIM by recording flags in ext4_group_info.

In ext4, when FITRIM is called every time, we iterate all the groups and do trim one by one. It is a bit time wasting if the group has been trimmed and there is no change since the last trim.

So this patch adds a new flag in ext4_group_info->bb_state to indicate that the group has been trimmed, and it will be cleared if some blocks is freed(in release_blocks_on_commit). Another trim_minlen is added in ext4_sb_info to record the last minlen we use to trim the volume, so that if the caller provide a small one, we will go on the trim regardless of the bb_state.

A simple test with my intel x25m ssd:
df -h shows:
/dev/sdb2 108G 35G 68G 34% /mnt/ext4
Block size: 4096

run the FITRIM with the following parameter:
range.start = 0;
range.len = UINT64_MAX;
range.minlen = 1048576;

without the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m4.039s
user 0m0.000s
sys 0m1.020s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.577s
user 0m0.001s
sys 0m1.004s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.380s
user 0m0.000s
sys 0m0.991s

with the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.466s
user 0m0.000s
sys 0m0.966s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.001s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.000s

A big improvement for the 2nd and 3rd run.

Even after I delete some big image files, it is still much faster than iterating the whole disk.
/dev/sdb2 108G 25G 78G 24% /mnt/ext4

[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.513s
user 0m0.000s
sys 0m0.069s

---
 fs/ext4/ext4.h    |   13 ++++++++++++-
 fs/ext4/mballoc.c |   20 ++++++++++++++++++++
 fs/ext4/super.c   |    2 ++
 3 files changed, 34 insertions(+), 1 deletions(-)
fs/ext4/ext4.h +12 -1
index 0c8d97b..715c0a4 100644
@@ -1200,6 +1200,9 @@ struct ext4_sb_info {
 	struct ext4_li_request *s_li_request;
 	/* Wait multiplier for lazy initialization thread */
 	unsigned int s_li_wait_mult;
+
+	/* record the last minlen when FITRIM is called. */
+	atomic_t s_last_trim_minblks;
 };
 
 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -1970,11 +1973,19 @@ struct ext4_group_info {
 					 * 5 free 8-block regions. */
 };
 
-#define EXT4_GROUP_INFO_NEED_INIT_BIT	0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_WAS_TRIMMED_BIT		1
 
 #define EXT4_MB_GRP_NEED_INIT(grp)	\
 	(test_bit(EXT4_GROUP_INFO_NEED_INIT_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_MAX_CONTENTION		8
 #define EXT4_CONTENTION_THRESHOLD	2
 
fs/ext4/mballoc.c +20 -0
index 4eadac8..29d7d17 100644
@@ -2687,6 +2687,15 @@ static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
 		rb_erase(&entry->node, &(db->bb_free_root));
 		mb_free_blocks(NULL, &e4b, entry->start_blk, entry->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()
@@ -4772,6 +4781,10 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 
 	ext4_lock_group(sb, group);
 
+	if (EXT4_MB_GRP_WAS_TRIMMED(e4b->bd_info) &&
+	    minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
+		goto out;
+
 	trace_ext4_trim_all_free(sb, group, start, max);
 
 	while (start < max) {
@@ -4804,6 +4817,10 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 		if ((e4b->bd_info->bb_free - free_count) < minblocks)
 			break;
 	}
+
+	if (!ret)
+		EXT4_MB_GRP_SET_TRIMMED(e4b->bd_info);
+out:
 	ext4_unlock_group(sb, group);
 
 	ext4_debug("trimmed %d blocks in the group %d\n",
@@ -4892,6 +4909,9 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
 	}
 	range->len = trimmed * sb->s_blocksize;
 
+	if (!ret)
+		atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
+
 out:
 	return ret;
 }
fs/ext4/super.c +2 -0
index 4898cb1..0fa3ed6 100644
@@ -3045,6 +3045,8 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
 		sbi->s_sectors_written_start =
 			part_stat_read(sb->s_bdev->bd_part, sectors[1]);
 
+	atomic_set(&sbi->s_last_trim_minblks, 0);
+
 	/* Cleanup superblock name */
 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
 		*cp = '!';
Signature · 1 line
1.6.3.GIT
#15 lore raw

Re: [PATCH 4/4 v2] ext4: Speed up FITRIM by recording flags in ext4_group_info.

In reply to Tao Ma
Quoted 8 lines · Tao Ma

After I delete some big image files and re-run the trim, it is still much faster than iterating the whole disk.
/dev/sdb2 108G 25G 78G 24% /mnt/ext4

[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.513s
user 0m0.000s
sys 0m0.069s

Excellent results.

Quoted 6 lines
fs/ext4/ext4.h +2 -0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT        0
+#define EXT4_GROUP_INFO_WAS_TRIMMED_BIT        1
#define EXT4_MB_GRP_NEED_INIT(grp)    \
   (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))

+#define EXT4_MB_GRP_HAS_BEEN_TRIMMED(grp) \

For consistency, it would be better to call this:

EXT4_MB_GRP_WAS_TRIMMED(grp)

And also add and use:

EXT4_MB_GRP_SET_TRIMMED(grp)

And

EXT4_MB_GRP_CLEAR_TRIMMED(grp)

Quoted 1 line

In the code. Then you can add Reviewed-by: on this patch.

Cheers, Andreas

author
#16 lore raw

PATCH v3 4/4 ext4: Speed up FITRIM by recording flags in ext4_group_info.

From: Tao Ma <[email protected]>

In ext4, when FITRIM is called every time, we iterate all the groups and do trim one by one. It is a bit time wasting if the group has been trimmed and there is no change since the last trim.

So this patch adds a new flag in ext4_group_info->bb_state to indicate that the group has been trimmed, and it will be cleared if some blocks is freed(in release_blocks_on_commit). Another trim_minlen is added in ext4_sb_info to record the last minlen we use to trim the volume, so that if the caller provide a small one, we will go on the trim regardless of the bb_state.

A simple test with my intel x25m ssd:
df -h shows:
/dev/sdb2 108G 35G 68G 34% /mnt/ext4
Block size: 4096

run the FITRIM with the following parameter:
range.start = 0;
range.len = UINT64_MAX;
range.minlen = 1048576;

without the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m4.039s
user 0m0.000s
sys 0m1.020s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.577s
user 0m0.001s
sys 0m1.004s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.380s
user 0m0.000s
sys 0m0.991s

with the patch:
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m3.466s
user 0m0.000s
sys 0m0.966s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.001s
[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.001s
user 0m0.000s
sys 0m0.000s

A big improvement for the 2nd and 3rd run.

Even after I delete some big image files, it is still much faster than iterating the whole disk.
/dev/sdb2 108G 25G 78G 24% /mnt/ext4

[root@boyu-tm test]# time ./ftrim /mnt/ext4/a
real 0m0.513s
user 0m0.000s
sys 0m0.069s

---
 fs/ext4/ext4.h    |   13 ++++++++++++-
 fs/ext4/mballoc.c |   20 ++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletions(-)
fs/ext4/ext4.h +12 -1
index 0c8d97b..259887d 100644
@@ -1200,6 +1200,9 @@ struct ext4_sb_info {
 	struct ext4_li_request *s_li_request;
 	/* Wait multiplier for lazy initialization thread */
 	unsigned int s_li_wait_mult;
+
+	/* record the last minlen when FITRIM is called. */
+	u64 s_last_trim_minblks;
 };
 
 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -1970,11 +1973,19 @@ struct ext4_group_info {
 					 * 5 free 8-block regions. */
 };
 
-#define EXT4_GROUP_INFO_NEED_INIT_BIT	0
+#define EXT4_GROUP_INFO_NEED_INIT_BIT		0
+#define EXT4_GROUP_INFO_WAS_TRIMMED_BIT		1
 
 #define EXT4_MB_GRP_NEED_INIT(grp)	\
 	(test_bit(EXT4_GROUP_INFO_NEED_INIT_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_MAX_CONTENTION		8
 #define EXT4_CONTENTION_THRESHOLD	2
 
fs/ext4/mballoc.c +20 -0
index 4eadac8..f1eef35 100644
@@ -2687,6 +2687,15 @@ static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
 		rb_erase(&entry->node, &(db->bb_free_root));
 		mb_free_blocks(NULL, &e4b, entry->start_blk, entry->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()
@@ -4772,6 +4781,10 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 
 	ext4_lock_group(sb, group);
 
+	if (EXT4_MB_GRP_WAS_TRIMMED(e4b->bd_info) &&
+	    minblocks >= EXT4_SB(sb)->s_last_trim_minblks)
+		goto out;
+
 	trace_ext4_trim_all_free(sb, group, start, max);
 
 	while (start < max) {
@@ -4804,6 +4817,10 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 		if ((e4b->bd_info->bb_free - free_count) < minblocks)
 			break;
 	}
+
+	if (!ret)
+		EXT4_MB_GRP_SET_TRIMMED(e4b->bd_info);
+out:
 	ext4_unlock_group(sb, group);
 
 	ext4_debug("trimmed %d blocks in the group %d\n",
@@ -4892,6 +4909,9 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
 	}
 	range->len = trimmed * sb->s_blocksize;
 
+	if (!ret)
+		EXT4_SB(sb)->s_last_trim_minblks = minlen;
+
 out:
 	return ret;
 }
Signature · 1 line
1.6.3.GIT
author
#17 lore raw
In reply to Tao Ma

Hi Ted,
any comments for this patch set? Are there ready for the next merge
window?
The most complicate patch [4/4] has already reviewed by Andreas.

Regards,
Tao

Quoted 15 lines · Tao Ma

Hi Ted and the list,
Here are 4 patches for trim support in ext4.
Patch 1/4 is a fix for the 'len' overflow. As we have decided that
we don't adjust start to s_first_data_block, this patch is still needed.
Patch 2/4 is a speedup for trim in one group.
Patch 3/4 adds trace points for the functions used in FITRIM. Patch 4/4 is a speedup for trim in the whole volume. For more
details, please check the commit log.

Regards,
Tao

Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#18 lore raw
Quoted 4 lines · Tao Ma

Hi Ted,
any comments for this patch set? Are there ready for the next merge
window?
The most complicate patch [4/4] has already reviewed by Andreas.

But still, I think there is a race -- see my previous comments in the thread where I suggested using atomic_t for s_last_trim_minblks.

Thanks!
-Lukas

Quoted 25 lines

Regards,
Tao

Quoted 9 lines · Tao Ma

Hi Ted and the list,
Here are 4 patches for trim support in ext4.
Patch 1/4 is a fix for the 'len' overflow. As we have decided that we
don't adjust start to s_first_data_block, this patch is still needed.
Patch 2/4 is a speedup for trim in one group.
Patch 3/4 adds trace points for the functions used in FITRIM. Patch 4/4 is a speedup for trim in the whole volume. For more details,
please check the commit log.

Quoted 6 lines

Regards,
Tao

Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html