ext4: fix goal trimming in CR_BEST_AVAIL_LEN
cover ext4: fix goal trimming in CR_BEST_AVAIL_LEN
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
Signature · 1 line
2.53.0
PATCH 1/2 ext4: scan all groups before trimming the goal in CR_BEST_AVAIL_LEN
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--- fs/ext4/mballoc.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
fs/ext4/mballoc.c +10 -6
@@ -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 */
Signature · 1 line
2.53.0
PATCH 2/2 ext4: fix off-by-one in CR_BEST_AVAIL_LEN min_order
In reply to Muhammad UmerWhen 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
--- fs/ext4/mballoc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/mballoc.c +1 -1
@@ -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);
Signature · 1 line
2.53.0
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]