public inbox for linux-ext4@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] ext4: simplify mballoc preallocation size rounding for small files
@ 2026-02-24 12:51 cuiweixie
  2026-02-25  0:00 ` Andreas Dilger
  0 siblings, 1 reply; 2+ messages in thread
From: cuiweixie @ 2026-02-24 12:51 UTC (permalink / raw)
  To: dilger.kernel; +Cc: linux-ext4, linux-kernel, Weixie Cui

From: Weixie Cui <cuiweixie@gmail.com>

The if-else ladder in ext4_mb_normalize_request() manually rounds up
the preallocation size to the next power of two for files up to 1MB,
enumerating each step from 16KB to 1MB individually. Replace this with
a single roundup_pow_of_two() call clamped to a 16KB minimum, which
is functionally equivalent but much more concise.

Also replace raw byte constants with SZ_1M and SZ_16K from
<linux/sizes.h> for clarity, and remove the stale "XXX: should this
table be tunable?" comment that has been there since the original
mballoc code.

No functional change.

Signed-off-by: Weixie Cui <cuiweixie@gmail.com>
---
 fs/ext4/mballoc.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 20e9fdaf4301..a5c51daaba78 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -4561,22 +4561,17 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac,
 		(req <= (size) || max <= (chunk_size))
 
 	/* first, try to predict filesize */
-	/* XXX: should this table be tunable? */
 	start_off = 0;
-	if (size <= 16 * 1024) {
-		size = 16 * 1024;
-	} else if (size <= 32 * 1024) {
-		size = 32 * 1024;
-	} else if (size <= 64 * 1024) {
-		size = 64 * 1024;
-	} else if (size <= 128 * 1024) {
-		size = 128 * 1024;
-	} else if (size <= 256 * 1024) {
-		size = 256 * 1024;
-	} else if (size <= 512 * 1024) {
-		size = 512 * 1024;
-	} else if (size <= 1024 * 1024) {
-		size = 1024 * 1024;
+	if (size <= SZ_1M) {
+		/*
+		 * For files up to 1MB, round up the preallocation size to
+		 * the next power of two, with a minimum of 16KB.
+		 */
+		if (size <= (unsigned long)SZ_16K) {
+			size = SZ_16K;
+		} else {
+			size = roundup_pow_of_two(size);
+		}
 	} else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
 		start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
 						(21 - bsbits)) << 21;
-- 
2.39.5 (Apple Git-154)


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

* Re: [PATCH v3] ext4: simplify mballoc preallocation size rounding for small files
  2026-02-24 12:51 [PATCH v3] ext4: simplify mballoc preallocation size rounding for small files cuiweixie
@ 2026-02-25  0:00 ` Andreas Dilger
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Dilger @ 2026-02-25  0:00 UTC (permalink / raw)
  To: cuiweixie; +Cc: linux-ext4, linux-kernel

On Feb 24, 2026, at 05:51, cuiweixie@gmail.com wrote:
> 
> From: Weixie Cui <cuiweixie@gmail.com>
> 
> The if-else ladder in ext4_mb_normalize_request() manually rounds up
> the preallocation size to the next power of two for files up to 1MB,
> enumerating each step from 16KB to 1MB individually. Replace this with
> a single roundup_pow_of_two() call clamped to a 16KB minimum, which
> is functionally equivalent but much more concise.
> 
> Also replace raw byte constants with SZ_1M and SZ_16K from
> <linux/sizes.h> for clarity, and remove the stale "XXX: should this
> table be tunable?" comment that has been there since the original
> mballoc code.
> 
> No functional change.
> 
> Signed-off-by: Weixie Cui <cuiweixie@gmail.com>

One minor code style nit below, but otherwise,

Reviewed-by: Andreas Dilger <adilger@dilger.ca <mailto:adilger@dilger.ca>>

> ---
> fs/ext4/mballoc.c | 25 ++++++++++---------------
> 1 file changed, 10 insertions(+), 15 deletions(-)
> 
> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
> index 20e9fdaf4301..a5c51daaba78 100644
> --- a/fs/ext4/mballoc.c
> +++ b/fs/ext4/mballoc.c
> @@ -4561,22 +4561,17 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac,
>  		(req <= (size) || max <= (chunk_size))
> 
>  	/* first, try to predict filesize */
> -	/* XXX: should this table be tunable? */
>  	start_off = 0;
> -	if (size <= 16 * 1024) {
> -		size = 16 * 1024;
> -	} else if (size <= 32 * 1024) {
> -		size = 32 * 1024;
> -	} else if (size <= 64 * 1024) {
> -		size = 64 * 1024;
> -	} else if (size <= 128 * 1024) {
> -		size = 128 * 1024;
> -	} else if (size <= 256 * 1024) {
> -		size = 256 * 1024;
> -	} else if (size <= 512 * 1024) {
> -		size = 512 * 1024;
> -	} else if (size <= 1024 * 1024) {
> -		size = 1024 * 1024;
> +	if (size <= SZ_1M) {
> +		/*
> +		 * For files up to 1MB, round up the preallocation size to
> +		 * the next power of two, with a minimum of 16KB.
> +		 */
> +		if (size <= (unsigned long)SZ_16K) {
> +			size = SZ_16K;
> +		} else {
> +			size = roundup_pow_of_two(size);
> +		}

No need for {} around single-line if-else blocks.

>  	} else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
>  		start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
>  						(21 - bsbits)) << 21;
> -- 
> 2.39.5 (Apple Git-154)
> 


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

end of thread, other threads:[~2026-02-25  0:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-24 12:51 [PATCH v3] ext4: simplify mballoc preallocation size rounding for small files cuiweixie
2026-02-25  0:00 ` Andreas Dilger

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