Skip to content

Commit 5d5460f

Browse files
OjaswinMtytso
authored andcommitted
ext4: fix off by one issue in ext4_mb_choose_next_group_best_avail()
In ext4_mb_choose_next_group_best_avail(), we want the start order to be 1 less than goal length and the min_order to be, at max, 1 more than the original length. This commit fixes an off by one issue that arose due to the fact that 1 << fls(n) > (n). After all the processing: order = 1 order below goal len min_order = maximum of the three:- - order - trim_order - 1 order below B2C(s_stripe) - 1 order above original len Cc: stable@kernel.org Fixes: 33122aa ("ext4: Add allocation criteria 1.5 (CR1_5)") Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com> Link: https://lore.kernel.org/r/20230609103403.112807-1-ojaswin@linux.ibm.com Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 6909cf5 commit 5d5460f

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

fs/ext4/mballoc.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,24 +1006,28 @@ static void ext4_mb_choose_next_group_best_avail(struct ext4_allocation_context
10061006
* fls() instead since we need to know the actual length while modifying
10071007
* goal length.
10081008
*/
1009-
order = fls(ac->ac_g_ex.fe_len);
1009+
order = fls(ac->ac_g_ex.fe_len) - 1;
10101010
min_order = order - sbi->s_mb_best_avail_max_trim_order;
10111011
if (min_order < 0)
10121012
min_order = 0;
10131013

1014-
if (1 << min_order < ac->ac_o_ex.fe_len)
1015-
min_order = fls(ac->ac_o_ex.fe_len) + 1;
1016-
10171014
if (sbi->s_stripe > 0) {
10181015
/*
10191016
* We are assuming that stripe size is always a multiple of
10201017
* cluster ratio otherwise __ext4_fill_super exists early.
10211018
*/
10221019
num_stripe_clusters = EXT4_NUM_B2C(sbi, sbi->s_stripe);
10231020
if (1 << min_order < num_stripe_clusters)
1024-
min_order = fls(num_stripe_clusters);
1021+
/*
1022+
* We consider 1 order less because later we round
1023+
* up the goal len to num_stripe_clusters
1024+
*/
1025+
min_order = fls(num_stripe_clusters) - 1;
10251026
}
10261027

1028+
if (1 << min_order < ac->ac_o_ex.fe_len)
1029+
min_order = fls(ac->ac_o_ex.fe_len);
1030+
10271031
for (i = order; i >= min_order; i--) {
10281032
int frag_order;
10291033
/*

0 commit comments

Comments
 (0)