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 --- 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