Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH 0/2] ext4: fix goal trimming in CR_BEST_AVAIL_LEN
@ 2026-11-22 18:56 Muhammad Umer
  2026-11-22 18:56 ` [PATCH 1/2] ext4: scan all groups before trimming the goal " Muhammad Umer
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Muhammad Umer @ 2026-11-22 18:56 UTC (permalink / raw)
  To: linux-ext4
  Cc: tytso, adilger.kernel, libaokun, jack, ojaswin, ritesh.list,
	yi.zhang, Muhammad Umer

Two issues in ext4_mb_scan_groups_best_avail() that make mballoc trim
allocations more than needed or skip CR_BEST_AVAIL_LEN entirely.

1/2: groups below the scan start are only searched after the goal has
     been trimmed to min_order. Regression from a3ce570a5d6a (v6.17).
2/2: min_order is one order too high for power-of-two requests, so they
     fall through to CR_GOAL_LEN_SLOW. Present since 7e170922f06b.

Both reproduce on 6.18.44, 7.1.5 and 7.2.15. The two fixes were tested
together on 7.2.15. Reproducers (loop device, run as root, read the
ext4_mballoc_alloc tracepoint):

  https://github.com/mumer2834/ext4-best-avail-repro

Patch 1 only changes the order in which CR_BEST_AVAIL_LEN visits groups;
CR_POWER2_ALIGNED and CR_GOAL_LEN_FAST keep the linear-like traversal
from a3ce570a5d6a. I have not measured multi-writer contention with this
change.



Muhammad Umer (2):
  ext4: scan all groups before trimming the goal in CR_BEST_AVAIL_LEN
  ext4: fix off-by-one in CR_BEST_AVAIL_LEN min_order

 fs/ext4/mballoc.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)


base-commit: 4843f6873f7c4835e62f2c73ba0fd538e57a2f22
-- 
2.53.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] ext4: scan all groups before trimming the goal in CR_BEST_AVAIL_LEN
  2026-11-22 18:56 [PATCH 0/2] ext4: fix goal trimming in CR_BEST_AVAIL_LEN Muhammad Umer
@ 2026-11-22 18:56 ` Muhammad Umer
  2026-11-22 18:56 ` [PATCH 2/2] ext4: fix off-by-one in CR_BEST_AVAIL_LEN min_order Muhammad Umer
  2026-11-22 19:01 ` [PATCH 0/2] ext4: fix goal trimming in CR_BEST_AVAIL_LEN sashiko-bot
  2 siblings, 0 replies; 4+ messages in thread
From: Muhammad Umer @ 2026-11-22 18:56 UTC (permalink / raw)
  To: linux-ext4
  Cc: tytso, adilger.kernel, libaokun, jack, ojaswin, ritesh.list,
	yi.zhang, Muhammad Umer

Since commit a3ce570a5d6a ("ext4: implement linear-like traversal across
order xarrays"), ext4_mb_scan_groups_best_avail() walks every order from
the goal order down to min_order over groups [start, ngroups) before it
looks at [0, start). A group below start is only considered after the
goal has been trimmed to min_order, so a 1024-cluster free extent in
group 2 loses to a 256-cluster one in group 9.

The ordering is harmless for CR_POWER2_ALIGNED and CR_GOAL_LEN_FAST,
where every order scanned satisfies the full goal. In CR_BEST_AVAIL_LEN
a lower order means a smaller allocation, so search both ranges at each
order before trimming further.

start is rarely 0: stream allocations start at s_mb_last_groups[], and
the linear prescan moves it forward by s_mb_max_linear_groups.

Reproducer: 16 groups, groups 0-7 with only 1024-block free extents,
groups 8-15 with only 256-block free extents. 32 MiB written with 64 KiB
O_DIRECT writes to a file with a 64 MiB i_size (8 MiB normalized goal).
Allocations made:

  start group, max_linear_groups   before               after
  0, 0                             8 x 1024             8 x 1024
  8, 0                             7 x 1024, 4 x 256    8 x 1024
  0, 4                             6 x 1024, 2 x 512,   8 x 1024
                                   4 x 256

Fixes: a3ce570a5d6a ("ext4: implement linear-like traversal across order xarrays")
Signed-off-by: Muhammad Umer <mumer.mobi@gmail.com>
---
 fs/ext4/mballoc.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index a15285224..37825743f 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -1097,7 +1097,6 @@ static int ext4_mb_scan_groups_best_avail(struct ext4_allocation_context *ac,
 
 	start = group;
 	end = ext4_get_allocation_groups_count(ac);
-wrap_around:
 	for (i = order; i >= min_order; i--) {
 		int frag_order;
 		/*
@@ -1120,15 +1119,20 @@ static int ext4_mb_scan_groups_best_avail(struct ext4_allocation_context *ac,
 		frag_order = mb_avg_fragment_size_order(ac->ac_sb,
 							ac->ac_g_ex.fe_len);
 
+		/*
+		 * A lower order means a smaller allocation, so search all
+		 * groups at this order before trimming the goal further.
+		 */
 		ret = ext4_mb_scan_groups_avg_frag_order_range(ac, frag_order,
 							       start, end);
 		if (ret || ac->ac_status != AC_STATUS_CONTINUE)
 			return ret;
-	}
-	if (start) {
-		end = start;
-		start = 0;
-		goto wrap_around;
+		if (start) {
+			ret = ext4_mb_scan_groups_avg_frag_order_range(ac,
+							frag_order, 0, start);
+			if (ret || ac->ac_status != AC_STATUS_CONTINUE)
+				return ret;
+		}
 	}
 
 	/* Reset goal length to original goal length before falling into CR_GOAL_LEN_SLOW */
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] ext4: fix off-by-one in CR_BEST_AVAIL_LEN min_order
  2026-11-22 18:56 [PATCH 0/2] ext4: fix goal trimming in CR_BEST_AVAIL_LEN Muhammad Umer
  2026-11-22 18:56 ` [PATCH 1/2] ext4: scan all groups before trimming the goal " Muhammad Umer
@ 2026-11-22 18:56 ` Muhammad Umer
  2026-11-22 19:01 ` [PATCH 0/2] ext4: fix goal trimming in CR_BEST_AVAIL_LEN sashiko-bot
  2 siblings, 0 replies; 4+ messages in thread
From: Muhammad Umer @ 2026-11-22 18:56 UTC (permalink / raw)
  To: linux-ext4
  Cc: tytso, adilger.kernel, libaokun, jack, ojaswin, ritesh.list,
	yi.zhang, Muhammad Umer

When the original request is longer than 1 << min_order, min_order is
raised to fls(ac_o_ex.fe_len). For a power-of-two length that is one
order too high: a 512-cluster request is never trimmed to 512 and falls
through to CR_GOAL_LEN_SLOW even when 512-cluster free extents exist.

Use order_base_2() so the trimmed goal is the smallest power of two that
covers the original request.

Reproducer: every group has only 512-block free extents. 2 MiB-aligned
O_DIRECT writes to a file with a 64 MiB i_size; 9 of 12 allocations
reach CR_BEST_AVAIL_LEN:

  write size   before                   after
  511 blocks   9 x CR_BEST_AVAIL_LEN    9 x CR_BEST_AVAIL_LEN
  512 blocks   9 x CR_GOAL_LEN_SLOW     9 x CR_BEST_AVAIL_LEN

Fixes: 7e170922f06b ("ext4: Add allocation criteria 1.5 (CR1_5)")
Signed-off-by: Muhammad Umer <mumer.mobi@gmail.com>
---
 fs/ext4/mballoc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 37825743f..8743ff5be 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -1093,7 +1093,7 @@ static int ext4_mb_scan_groups_best_avail(struct ext4_allocation_context *ac,
 	}
 
 	if (1 << min_order < ac->ac_o_ex.fe_len)
-		min_order = fls(ac->ac_o_ex.fe_len);
+		min_order = order_base_2(ac->ac_o_ex.fe_len);
 
 	start = group;
 	end = ext4_get_allocation_groups_count(ac);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] ext4: fix goal trimming in CR_BEST_AVAIL_LEN
  2026-11-22 18:56 [PATCH 0/2] ext4: fix goal trimming in CR_BEST_AVAIL_LEN Muhammad Umer
  2026-11-22 18:56 ` [PATCH 1/2] ext4: scan all groups before trimming the goal " Muhammad Umer
  2026-11-22 18:56 ` [PATCH 2/2] ext4: fix off-by-one in CR_BEST_AVAIL_LEN min_order Muhammad Umer
@ 2026-11-22 19:01 ` sashiko-bot
  2 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-22 19:01 UTC (permalink / raw)
  To: Muhammad Umer, linux-ext4; +Cc: tytso

Sashiko has reviewed this 2-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261122185630.2112-1-mumer.mobi@gmail.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-22 19:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 18:56 [PATCH 0/2] ext4: fix goal trimming in CR_BEST_AVAIL_LEN Muhammad Umer
2026-11-22 18:56 ` [PATCH 1/2] ext4: scan all groups before trimming the goal " Muhammad Umer
2026-11-22 18:56 ` [PATCH 2/2] ext4: fix off-by-one in CR_BEST_AVAIL_LEN min_order Muhammad Umer
2026-11-22 19:01 ` [PATCH 0/2] ext4: fix goal trimming in CR_BEST_AVAIL_LEN sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox