Skip to contentSkip to conversation

ext4: Fix performance regression with mballoc

PATCH Applied Jan Kara 4 participants 14 messages Sep 8, 2022 – Sep 26, 2022

Subsystems (2)
lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

v3 cover ext4: Fix performance regression with mballoc

Hello,

Here is the third version of my mballoc improvements to avoid spreading allocations with mb_optimize_scan=1. Since v2 there are only small changes and fixes found during review and testing. Overall the series looks mostly ready to go, I just didn't see any comments regarding patch 3 - a fix of metabg handling in the Orlov allocator which is kind of independent, I've just found it when reading the code. Also patch 5 needs final review after all the fixes.

Changes since v1:
- reworked data structure for CR 1 scan
- make small closed files use locality group preallocation
- fix metabg handling in the Orlov allocator

Changes since v2:
- whitespace fixes
- fix outdated comment
- fix handling of mb_structs_summary procfs file
- fix bad unlock on error recovery path

Original cover letter:

The patches fix the performance regression I was able to reproduce with reaim on my test machine:

mb_optimize_scan=0 mb_optimize_scan=1 patched

Hmean     disk-1       2076.12 (   0.00%)     2099.37 (   1.12%)     2032.52 (  -2.10%)
Hmean     disk-41     92481.20 (   0.00%)    83787.47 *  -9.40%*    90308.37 (  -2.35%)
Hmean     disk-81    155073.39 (   0.00%)   135527.05 * -12.60%*   154285.71 (  -0.51%)
Hmean     disk-121   185109.64 (   0.00%)   166284.93 * -10.17%*   185298.62 (   0.10%)
Hmean     disk-161   229890.53 (   0.00%)   207563.39 *  -9.71%*   232883.32 *   1.30%*
Hmean     disk-201   223333.33 (   0.00%)   203235.59 *  -9.00%*   221446.93 (  -0.84%)
Hmean     disk-241   235735.25 (   0.00%)   217705.51 *  -7.65%*   239483.27 *   1.59%*
Hmean     disk-281   266772.15 (   0.00%)   241132.72 *  -9.61%*   263108.62 (  -1.37%)
Hmean     disk-321   265435.50 (   0.00%)   245412.84 *  -7.54%*   267277.27 (   0.69%)

The changes also significanly reduce spreading of allocations for small / moderately sized files. I'm not able to measure a performance difference resulting from this but on eMMC storage this seems to be the main culprit of reduced performance. Untarring of raspberry-pi archive touches following numbers of groups:

mb_optimize_scan=0 mb_optimize_scan=1 patched
groups 4 22 7

To achieve this I have added two more changes on top of v1 - patches 4 and 5. Patch 4 makes sure we use locality group preallocation even for files that are not likely to grow anymore (previously we have disabled all preallocations for such files, however locality group preallocation still makes a lot of sense for such files). This patch reduced spread of a small file allocations but larger file allocations were still spread significantly because they avoid locality group preallocation and as they are not power-of-two in size, they also immediately start with cr=1 scan. To address that I've changed the data structure for looking up the best block group to allocate from (see patch 5 for details).

Honza
Previous versions:

author reviewer
#1 lore raw

PATCH v3 1/5 ext4: Make mballoc try target group first even with mb_optimize_scan

One of the side-effects of mb_optimize_scan was that the optimized functions to select next group to try were called even before we tried the goal group. As a result we no longer allocate files close to corresponding inodes as well as we don't try to expand currently allocated extent in the same group. This results in reaim regression with workfile.disk workload of upto 8% with many clients on my test machine:

baseline mb_optimize_scan

Hmean     disk-1       2114.16 (   0.00%)     2099.37 (  -0.70%)
Hmean     disk-41     87794.43 (   0.00%)    83787.47 *  -4.56%*
Hmean     disk-81    148170.73 (   0.00%)   135527.05 *  -8.53%*
Hmean     disk-121   177506.11 (   0.00%)   166284.93 *  -6.32%*
Hmean     disk-161   220951.51 (   0.00%)   207563.39 *  -6.06%*
Hmean     disk-201   208722.74 (   0.00%)   203235.59 (  -2.63%)
Hmean     disk-241   222051.60 (   0.00%)   217705.51 (  -1.96%)
Hmean     disk-281   252244.17 (   0.00%)   241132.72 *  -4.41%*
Hmean     disk-321   255844.84 (   0.00%)   245412.84 *  -4.08%*

Also this is causing huge regression (time increased by a factor of 5 or so) when untarring archive with lots of small files on some eMMC storage cards.

Fix the problem by making sure we try goal group first.

---
 fs/ext4/mballoc.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
fs/ext4/mballoc.c +7 -7
index bd8f8b5c3d30..41e1cfecac3b 100644
@@ -1049,8 +1049,10 @@ static void ext4_mb_choose_next_group(struct ext4_allocation_context *ac,
 {
 	*new_cr = ac->ac_criteria;
 
-	if (!should_optimize_scan(ac) || ac->ac_groups_linear_remaining)
+	if (!should_optimize_scan(ac) || ac->ac_groups_linear_remaining) {
+		*group = next_linear_group(ac, *group, ngroups);
 		return;
+	}
 
 	if (*new_cr == 0) {
 		ext4_mb_choose_next_group_cr0(ac, new_cr, group, ngroups);
@@ -2636,7 +2638,7 @@ static noinline_for_stack int
 ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
 {
 	ext4_group_t prefetch_grp = 0, ngroups, group, i;
-	int cr = -1;
+	int cr = -1, new_cr;
 	int err = 0, first_err = 0;
 	unsigned int nr = 0, prefetch_ios = 0;
 	struct ext4_sb_info *sbi;
@@ -2711,13 +2713,11 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
 		ac->ac_groups_linear_remaining = sbi->s_mb_max_linear_groups;
 		prefetch_grp = group;
 
-		for (i = 0; i < ngroups; group = next_linear_group(ac, group, ngroups),
-			     i++) {
-			int ret = 0, new_cr;
+		for (i = 0, new_cr = cr; i < ngroups; i++,
+		     ext4_mb_choose_next_group(ac, &new_cr, &group, ngroups)) {
+			int ret = 0;
 
 			cond_resched();
-
-			ext4_mb_choose_next_group(ac, &new_cr, &group, ngroups);
 			if (new_cr != cr) {
 				cr = new_cr;
 				goto repeat;
Signature · 1 line
2.35.3
maintainer Applied
#2 lore raw

Re: [PATCH 1/5] ext4: Make mballoc try target group first even with mb_optimize_scan

Quoted 9 lines · Jan Kara

One of the side-effects of mb_optimize_scan was that the optimized functions to select next group to try were called even before we tried the goal group. As a result we no longer allocate files close to corresponding inodes as well as we don't try to expand currently allocated extent in the same group. This results in reaim regression with workfile.disk workload of upto 8% with many clients on my test machine:

[...]

Applied, thanks!

[1/5] ext4: Make mballoc try target group first even with mb_optimize_scan

      commit: 4fca50d440cc5d4dc570ad5484cc0b70b381bc2a
[2/5] ext4: Avoid unnecessary spreading of allocations among groups
      commit: 1940265ede6683f6317cba0d428ce6505eaca944
[3/5] ext4: Make directory inode spreading reflect flexbg size
      commit: 613c5a85898d1cd44e68f28d65eccf64a8ace9cf
[4/5] ext4: Use locality group preallocation for small closed files
      commit: a9f2a2931d0e197ab28c6007966053fdababd53f
[5/5] ext4: Use buckets for cr 1 block scan instead of rbtree
      commit: 83e80a6e3543f37f74c8e48a5f305b054b65ce2a

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>
author reviewer
#3 lore raw

Re: [PATCH 1/5] ext4: Make mballoc try target group first even with mb_optimize_scan

Quoted 14 lines · Theodore Ts'o
Quoted 9 lines · Jan Kara

One of the side-effects of mb_optimize_scan was that the optimized functions to select next group to try were called even before we tried the goal group. As a result we no longer allocate files close to corresponding inodes as well as we don't try to expand currently allocated extent in the same group. This results in reaim regression with workfile.disk workload of upto 8% with many clients on my test machine:

[...]

Applied, thanks!

[1/5] ext4: Make mballoc try target group first even with mb_optimize_scan

Quoted 9 lines
      commit: 4fca50d440cc5d4dc570ad5484cc0b70b381bc2a
[2/5] ext4: Avoid unnecessary spreading of allocations among groups
      commit: 1940265ede6683f6317cba0d428ce6505eaca944
[3/5] ext4: Make directory inode spreading reflect flexbg size
      commit: 613c5a85898d1cd44e68f28d65eccf64a8ace9cf
[4/5] ext4: Use locality group preallocation for small closed files
      commit: a9f2a2931d0e197ab28c6007966053fdababd53f
[5/5] ext4: Use buckets for cr 1 block scan instead of rbtree
      commit: 83e80a6e3543f37f74c8e48a5f305b054b65ce2a

Thanks Ted! I just have locally a small fixup to the series that was reported by Smatch. It is attached, either fold it into the last patch or just merge it as a separate patch. Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR

From 8885b11fb253e08ecfa90a28beffb01719af84f5 Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Thu, 22 Sep 2022 11:09:29 +0200
Subject: [PATCH] ext4: Fixup possible uninitialized variable access in
ext4_mb_choose_next_group_cr1()

Variable 'grp' may be left uninitialized if there's no group with suitable average fragment size (or larger). Fix the problem by initializing it earlier.

Fixes 83e80a6e3543 ("ext4: use buckets for cr 1 block scan instead of rbtree")
---
 fs/ext4/mballoc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/ext4/mballoc.c +1 -2
index 71f5b67d7f28..9dad93059945 100644
@@ -910,7 +910,7 @@ static void ext4_mb_choose_next_group_cr1(struct ext4_allocation_context *ac,
 		int *new_cr, ext4_group_t *group, ext4_group_t ngroups)
 {
 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
-	struct ext4_group_info *grp, *iter;
+	struct ext4_group_info *grp = NULL, *iter;
 	int i;
 
 	if (unlikely(ac->ac_flags & EXT4_MB_CR1_OPTIMIZED)) {
@@ -927,7 +927,6 @@ static void ext4_mb_choose_next_group_cr1(struct ext4_allocation_context *ac,
 			read_unlock(&sbi->s_mb_avg_fragment_size_locks[i]);
 			continue;
 		}
-		grp = NULL;
 		list_for_each_entry(iter, &sbi->s_mb_avg_fragment_size[i],
 				    bb_avg_fragment_size_node) {
 			if (sbi->s_mb_stats)
Signature · 1 line
2.35.3
author reviewer
#4 lore raw

Re: [PATCH 1/5] ext4: Make mballoc try target group first even with mb_optimize_scan

Quoted 25 lines · Jan Kara
Quoted 14 lines · Theodore Ts'o
Quoted 9 lines · Jan Kara

One of the side-effects of mb_optimize_scan was that the optimized functions to select next group to try were called even before we tried the goal group. As a result we no longer allocate files close to corresponding inodes as well as we don't try to expand currently allocated extent in the same group. This results in reaim regression with workfile.disk workload of upto 8% with many clients on my test machine:

[...]

Applied, thanks!

[1/5] ext4: Make mballoc try target group first even with mb_optimize_scan

Quoted 9 lines
      commit: 4fca50d440cc5d4dc570ad5484cc0b70b381bc2a
[2/5] ext4: Avoid unnecessary spreading of allocations among groups
      commit: 1940265ede6683f6317cba0d428ce6505eaca944
[3/5] ext4: Make directory inode spreading reflect flexbg size
      commit: 613c5a85898d1cd44e68f28d65eccf64a8ace9cf
[4/5] ext4: Use locality group preallocation for small closed files
      commit: a9f2a2931d0e197ab28c6007966053fdababd53f
[5/5] ext4: Use buckets for cr 1 block scan instead of rbtree
      commit: 83e80a6e3543f37f74c8e48a5f305b054b65ce2a
Quoted 3 lines

Thanks Ted! I just have locally a small fixup to the series that was reported by Smatch. It is attached, either fold it into the last patch or just merge it as a separate patch. Thanks!

Ted, I've noticed you've merged my mballoc fixes (and pushed them to Linus) without this fixup. Can you please merge it? The use of uninitialized variable seems rare but possible...

Honza

Quoted 41 lines

From 8885b11fb253e08ecfa90a28beffb01719af84f5 Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Thu, 22 Sep 2022 11:09:29 +0200
Subject: [PATCH] ext4: Fixup possible uninitialized variable access in
ext4_mb_choose_next_group_cr1()

Variable 'grp' may be left uninitialized if there's no group with suitable average fragment size (or larger). Fix the problem by initializing it earlier.

Fixes 83e80a6e3543 ("ext4: use buckets for cr 1 block scan instead of rbtree")
---
 fs/ext4/mballoc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/ext4/mballoc.c +1 -2
index 71f5b67d7f28..9dad93059945 100644
@@ -910,7 +910,7 @@ static void ext4_mb_choose_next_group_cr1(struct ext4_allocation_context *ac,
 		int *new_cr, ext4_group_t *group, ext4_group_t ngroups)
 {
 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
-	struct ext4_group_info *grp, *iter;
+	struct ext4_group_info *grp = NULL, *iter;
 	int i;
 
 	if (unlikely(ac->ac_flags & EXT4_MB_CR1_OPTIMIZED)) {
@@ -927,7 +927,6 @@ static void ext4_mb_choose_next_group_cr1(struct ext4_allocation_context *ac,
 			read_unlock(&sbi->s_mb_avg_fragment_size_locks[i]);
 			continue;
 		}
-		grp = NULL;
 		list_for_each_entry(iter, &sbi->s_mb_avg_fragment_size[i],
 				    bb_avg_fragment_size_node) {
 			if (sbi->s_mb_stats)
Signature · 1 line
2.35.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#5 lore raw

Re: [PATCH 1/5] ext4: Make mballoc try target group first even with mb_optimize_scan

Quoted 4 lines · Jan Kara

Ted, I've noticed you've merged my mballoc fixes (and pushed them to Linus) without this fixup. Can you please merge it? The use of uninitialized variable seems rare but possible...

Oops, sorry my bad. I had forgotten to merge the fix before I pushed them to Linus. I'm preparing a follow-up push to Linus right now. Once a smoke test passes, I'll sned out the pull request.

- Ted

author reviewer
#6 lore raw

PATCH v3 2/5 ext4: Avoid unnecessary spreading of allocations among groups

In reply to Jan Kara

mb_set_largest_free_order() updates lists containing groups with largest chunk of free space of given order. The way it updates it leads to always moving the group to the tail of the list. Thus allocations looking for free space of given order effectively end up cycling through all groups (and due to initialization in last to first order). This spreads allocations among block groups which reduces performance for rotating disks or low-end flash media. Change mb_set_largest_free_order() to only update lists if the order of the largest free chunk in the group changed.

Fixes 196e402adf2e ("ext4: improve cr 0 / cr 1 group scanning")
Reported-and-tested-by Stefan Wahren <[email protected]>
---
 fs/ext4/mballoc.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
fs/ext4/mballoc.c +13 -11
index 41e1cfecac3b..6251b4a6cc63 100644
@@ -1077,23 +1077,25 @@ mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	int i;
 
-	if (test_opt2(sb, MB_OPTIMIZE_SCAN) && grp->bb_largest_free_order >= 0) {
+	for (i = MB_NUM_ORDERS(sb) - 1; i >= 0; i--)
+		if (grp->bb_counters[i] > 0)
+			break;
+	/* No need to move between order lists? */
+	if (!test_opt2(sb, MB_OPTIMIZE_SCAN) ||
+	    i == grp->bb_largest_free_order) {
+		grp->bb_largest_free_order = i;
+		return;
+	}
+
+	if (grp->bb_largest_free_order >= 0) {
 		write_lock(&sbi->s_mb_largest_free_orders_locks[
 					      grp->bb_largest_free_order]);
 		list_del_init(&grp->bb_largest_free_order_node);
 		write_unlock(&sbi->s_mb_largest_free_orders_locks[
 					      grp->bb_largest_free_order]);
 	}
-	grp->bb_largest_free_order = -1; /* uninit */
-
-	for (i = MB_NUM_ORDERS(sb) - 1; i >= 0; i--) {
-		if (grp->bb_counters[i] > 0) {
-			grp->bb_largest_free_order = i;
-			break;
-		}
-	}
-	if (test_opt2(sb, MB_OPTIMIZE_SCAN) &&
-	    grp->bb_largest_free_order >= 0 && grp->bb_free) {
+	grp->bb_largest_free_order = i;
+	if (grp->bb_largest_free_order >= 0 && grp->bb_free) {
 		write_lock(&sbi->s_mb_largest_free_orders_locks[
 					      grp->bb_largest_free_order]);
 		list_add_tail(&grp->bb_largest_free_order_node,
Signature · 1 line
2.35.3
author reviewer
#7 lore raw

PATCH v3 3/5 ext4: Make directory inode spreading reflect flexbg size

In reply to Jan Kara

Currently the Orlov inode allocator searches for free inodes for a directory only in flex block groups with at most inodes_per_group/16 more directory inodes than average per flex block group. However with growing size of flex block group this becomes unnecessarily strict. Scale allowed difference from average directory count per flex block group with flex block group size as we do with other metrics.

---
 fs/ext4/ialloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/ialloc.c +1 -1
index f73e5eb43eae..208b87ce8858 100644
@@ -510,7 +510,7 @@ static int find_group_orlov(struct super_block *sb, struct inode *parent,
 		goto fallback;
 	}
 
-	max_dirs = ndirs / ngroups + inodes_per_group / 16;
+	max_dirs = ndirs / ngroups + inodes_per_group*flex_size / 16;
 	min_inodes = avefreei - inodes_per_group*flex_size / 4;
 	if (min_inodes < 1)
 		min_inodes = 1;
Signature · 1 line
2.35.3
author reviewer
#8 lore raw

PATCH v3 4/5 ext4: Use locality group preallocation for small closed files

In reply to Jan Kara

Curently we don't use any preallocation when a file is already closed when allocating blocks (from writeback code when converting delayed allocation). However for small files, using locality group preallocation is actually desirable as that is not specific to a particular file. Rather it is a method to pack small files together to reduce fragmentation and for that the fact the file is closed is actually even stronger hint the file would benefit from packing. So change the logic to allow locality group preallocation in this case.

Fixes 196e402adf2e ("ext4: improve cr 0 / cr 1 group scanning")
Reported-and-tested-by Stefan Wahren <[email protected]>
---
 fs/ext4/mballoc.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
fs/ext4/mballoc.c +15 -12
index 6251b4a6cc63..af1e49c3603f 100644
@@ -5195,6 +5195,7 @@ static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
 	int bsbits = ac->ac_sb->s_blocksize_bits;
 	loff_t size, isize;
+	bool inode_pa_eligible, group_pa_eligible;
 
 	if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
 		return;
@@ -5202,25 +5203,27 @@ static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
 	if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
 		return;
 
+	group_pa_eligible = sbi->s_mb_group_prealloc > 0;
+	inode_pa_eligible = true;
 	size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
 	isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
 		>> bsbits;
 
+	/* No point in using inode preallocation for closed files */
 	if ((size == isize) && !ext4_fs_is_busy(sbi) &&
-	    !inode_is_open_for_write(ac->ac_inode)) {
-		ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
-		return;
-	}
+	    !inode_is_open_for_write(ac->ac_inode))
+		inode_pa_eligible = false;
 
-	if (sbi->s_mb_group_prealloc <= 0) {
-		ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
-		return;
-	}
-
-	/* don't use group allocation for large files */
 	size = max(size, isize);
-	if (size > sbi->s_mb_stream_request) {
-		ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
+	/* Don't use group allocation for large files */
+	if (size > sbi->s_mb_stream_request)
+		group_pa_eligible = false;
+
+	if (!group_pa_eligible) {
+		if (inode_pa_eligible)
+			ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
+		else
+			ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
 		return;
 	}
 
Signature · 1 line
2.35.3
author reviewer
#9 lore raw

PATCH v3 5/5 ext4: Use buckets for cr 1 block scan instead of rbtree

In reply to Jan Kara

Using rbtree for sorting groups by average fragment size is relatively expensive (needs rbtree update on every block freeing or allocation) and leads to wide spreading of allocations because selection of block group is very sentitive both to changes in free space and amount of blocks allocated. Furthermore selecting group with the best matching average fragment size is not necessary anyway, even more so because the variability of fragment sizes within a group is likely large so average is not telling much. We just need a group with large enough average fragment size so that we have high probability of finding large enough free extent and we don't want average fragment size to be too big so that we are likely to find free extent only somewhat larger than what we need.

So instead of maintaing rbtree of groups sorted by fragment size keep bins (lists) or groups where average fragment size is in the interval [2^i, 2^(i+1)). This structure requires less updates on block allocation / freeing, generally avoids chaotic spreading of allocations into block groups, and still is able to quickly (even faster that the rbtree) provide a block group which is likely to have a suitably sized free space extent.

This patch reduces number of block groups used when untarring archive with medium sized files (size somewhat above 64k which is default mballoc limit for avoiding locality group preallocation) to about half and thus improves write speeds for eMMC flash significantly.

Fixes 196e402adf2e ("ext4: improve cr 0 / cr 1 group scanning")
Reported-and-tested-by Stefan Wahren <[email protected]>
---
 fs/ext4/ext4.h    |  10 +-
 fs/ext4/mballoc.c | 249 ++++++++++++++++++++--------------------------
 fs/ext4/mballoc.h |   1 -
 3 files changed, 111 insertions(+), 149 deletions(-)
fs/ext4/ext4.h +5 -5
index 9bca5565547b..3bf9a6926798 100644
@@ -167,8 +167,6 @@ enum SHIFT_DIRECTION {
 #define EXT4_MB_CR0_OPTIMIZED		0x8000
 /* Avg fragment size rb tree lookup succeeded at least once for cr = 1 */
 #define EXT4_MB_CR1_OPTIMIZED		0x00010000
-/* Perform linear traversal for one group */
-#define EXT4_MB_SEARCH_NEXT_LINEAR	0x00020000
 struct ext4_allocation_request {
 	/* target inode for block we're allocating */
 	struct inode *inode;
@@ -1600,8 +1598,8 @@ struct ext4_sb_info {
 	struct list_head s_discard_list;
 	struct work_struct s_discard_work;
 	atomic_t s_retry_alloc_pending;
-	struct rb_root s_mb_avg_fragment_size_root;
-	rwlock_t s_mb_rb_lock;
+	struct list_head *s_mb_avg_fragment_size;
+	rwlock_t *s_mb_avg_fragment_size_locks;
 	struct list_head *s_mb_largest_free_orders;
 	rwlock_t *s_mb_largest_free_orders_locks;
 
@@ -3413,6 +3411,8 @@ struct ext4_group_info {
 	ext4_grpblk_t	bb_first_free;	/* first free block */
 	ext4_grpblk_t	bb_free;	/* total free blocks */
 	ext4_grpblk_t	bb_fragments;	/* nr of freespace fragments */
+	int		bb_avg_fragment_size_order;	/* order of average
+							   fragment in BG */
 	ext4_grpblk_t	bb_largest_free_order;/* order of largest frag in BG */
 	ext4_group_t	bb_group;	/* Group number */
 	struct          list_head bb_prealloc_list;
@@ -3420,7 +3420,7 @@ struct ext4_group_info {
 	void            *bb_bitmap;
 #endif
 	struct rw_semaphore alloc_sem;
-	struct rb_node	bb_avg_fragment_size_rb;
+	struct list_head bb_avg_fragment_size_node;
 	struct list_head bb_largest_free_order_node;
 	ext4_grpblk_t	bb_counters[];	/* Nr of free power-of-two-block
 					 * regions, index is order.
fs/ext4/mballoc.c +106 -143

407 lines not on this page · raw message

fs/ext4/mballoc.h +0 -1
index 39da92ceabf8..dcda2a943cee 100644
@@ -178,7 +178,6 @@ struct ext4_allocation_context {
 	/* copy of the best found extent taken before preallocation efforts */
 	struct ext4_free_extent ac_f_ex;
 
-	ext4_group_t ac_last_optimal_group;
 	__u32 ac_groups_considered;
 	__u32 ac_flags;		/* allocation hints */
 	__u16 ac_groups_scanned;
Signature · 1 line
2.35.3
reviewer Reviewed-by on v3 5/5
#10 lore raw

Re: [PATCH 5/5] ext4: Use buckets for cr 1 block scan instead of rbtree

Quoted 31 lines · Jan Kara

Using rbtree for sorting groups by average fragment size is relatively expensive (needs rbtree update on every block freeing or allocation) and leads to wide spreading of allocations because selection of block group is very sentitive both to changes in free space and amount of blocks allocated. Furthermore selecting group with the best matching average fragment size is not necessary anyway, even more so because the variability of fragment sizes within a group is likely large so average is not telling much. We just need a group with large enough average fragment size so that we have high probability of finding large enough free extent and we don't want average fragment size to be too big so that we are likely to find free extent only somewhat larger than what we need.

So instead of maintaing rbtree of groups sorted by fragment size keep bins (lists) or groups where average fragment size is in the interval [2^i, 2^(i+1)). This structure requires less updates on block allocation / freeing, generally avoids chaotic spreading of allocations into block groups, and still is able to quickly (even faster that the rbtree) provide a block group which is likely to have a suitably sized free space extent.

This patch reduces number of block groups used when untarring archive with medium sized files (size somewhat above 64k which is default mballoc limit for avoiding locality group preallocation) to about half and thus improves write speeds for eMMC flash significantly.

Fixes 196e402adf2e ("ext4: improve cr 0 / cr 1 group scanning")
Reported-and-tested-by Stefan Wahren <[email protected]>
Quoted 5 lines
---
 fs/ext4/ext4.h    |  10 +-
 fs/ext4/mballoc.c | 249 ++++++++++++++++++++--------------------------
 fs/ext4/mballoc.h |   1 -
 3 files changed, 111 insertions(+), 149 deletions(-)

Hello Jan,

I have reviewed the patch and also verified bb_fragments. That indeed will atleast be 1 (ext4_mb_generate_buddy()) as you had mentioned.

The patch looks good to me. Please feel free to add:

#11 lore raw
In reply to Jan Kara

Hi Jan,

Am 08.09.22 um 11:21 schrieb Jan Kara:

Quoted 14 lines

Hello,

Here is the third version of my mballoc improvements to avoid spreading allocations with mb_optimize_scan=1. Since v2 there are only small changes and fixes found during review and testing. Overall the series looks mostly ready to go, I just didn't see any comments regarding patch 3 - a fix of metabg handling in the Orlov allocator which is kind of independent, I've just found it when reading the code. Also patch 5 needs final review after all the fixes.

Changes since v1:
- reworked data structure for CR 1 scan
- make small closed files use locality group preallocation
- fix metabg handling in the Orlov allocator

Quoted 5 lines

Changes since v2:
- whitespace fixes
- fix outdated comment
- fix handling of mb_structs_summary procfs file
- fix bad unlock on error recovery path

unfortunately the real patches doesn't have v3 which leads to confusion.

Just a note: in case this series cannot be applied for stable (5.15), we need a second solution to fix the regression there.

author reviewer
#12 lore raw
Quoted 17 lines · Stefan Wahren

Hi Jan,

Am 08.09.22 um 11:21 schrieb Jan Kara:

Quoted 14 lines

Hello,

Here is the third version of my mballoc improvements to avoid spreading allocations with mb_optimize_scan=1. Since v2 there are only small changes and fixes found during review and testing. Overall the series looks mostly ready to go, I just didn't see any comments regarding patch 3 - a fix of metabg handling in the Orlov allocator which is kind of independent, I've just found it when reading the code. Also patch 5 needs final review after all the fixes.

Changes since v1:
- reworked data structure for CR 1 scan
- make small closed files use locality group preallocation
- fix metabg handling in the Orlov allocator

Quoted 7 lines
Quoted 5 lines

Changes since v2:
- whitespace fixes
- fix outdated comment
- fix handling of mb_structs_summary procfs file
- fix bad unlock on error recovery path

unfortunately the real patches doesn't have v3 which leads to confusion.

Yeah, OK, I've updated my scripting for posting patches to include version in each posted patch :)

Quoted 2 lines

Just a note: in case this series cannot be applied for stable (5.15), we need a second solution to fix the regression there.

My plan is to backport the current series. It is not that invasive so it should be doable...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#13 lore raw

Hi Jan,

Am 09.09.22 um 12:40 schrieb Jan Kara:

Quoted 23 lines · Stefan Wahren
Quoted 22 lines

Hi Jan,

Am 08.09.22 um 11:21 schrieb Jan Kara:

Quoted 19 lines

Hello,

Here is the third version of my mballoc improvements to avoid spreading allocations with mb_optimize_scan=1. Since v2 there are only small changes and fixes found during review and testing. Overall the series looks mostly ready to go, I just didn't see any comments regarding patch 3 - a fix of metabg handling in the Orlov allocator which is kind of independent, I've just found it when reading the code. Also patch 5 needs final review after all the fixes.

Changes since v1:
- reworked data structure for CR 1 scan
- make small closed files use locality group preallocation
- fix metabg handling in the Orlov allocator

Changes since v2:
- whitespace fixes
- fix outdated comment
- fix handling of mb_structs_summary procfs file
- fix bad unlock on error recovery path

Quoted 8 lines
Quoted 1 line

unfortunately the real patches doesn't have v3 which leads to confusion.

Yeah, OK, I've updated my scripting for posting patches to include version in each posted patch :)

Quoted 2 lines

Just a note: in case this series cannot be applied for stable (5.15), we need a second solution to fix the regression there.

My plan is to backport the current series. It is not that invasive so it should be doable...

This would be great.

Btw i retested this version and i can confirm the procfs crash is fixed.

Quoted 2 lines

Honza