[PATCH] btrfs: Refactor allocation size calculation in kzalloc()

Mehdi Ben Hadj Khelifa posted 1 patch 2 months, 2 weeks ago
There is a newer version of this series
fs/btrfs/volumes.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
[PATCH] btrfs: Refactor allocation size calculation in kzalloc()
Posted by Mehdi Ben Hadj Khelifa 2 months, 2 weeks ago
Wrap allocation size calculation in size_add() and size_mul() to avoid
any potential overflow.

Signed-off-by: Mehdi Ben Hadj Khelifa <mehdi.benhadjkhelifa@gmail.com>
---
 fs/btrfs/volumes.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index c6e3efd6f602..3f1f19b28aac 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -6076,12 +6076,11 @@ struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_info,
 {
 	struct btrfs_io_context *bioc;
 
-	bioc = kzalloc(
-		 /* The size of btrfs_io_context */
-		sizeof(struct btrfs_io_context) +
-		/* Plus the variable array for the stripes */
-		sizeof(struct btrfs_io_stripe) * (total_stripes),
-		GFP_NOFS);
+	/* The size of btrfs_io_context */
+	/* Plus the variable array for the stripes */
+	bioc = kzalloc(size_add(sizeof(struct btrfs_io_context),
+				size_mul(sizeof(struct btrfs_io_stripe),
+						total_stripes)), GFP_NOFS);
 
 	if (!bioc)
 		return NULL;
-- 
2.51.0
Re: [PATCH] btrfs: Refactor allocation size calculation in kzalloc()
Posted by Qu Wenruo 2 months, 2 weeks ago

在 2025/9/30 18:44, Mehdi Ben Hadj Khelifa 写道:
> Wrap allocation size calculation in size_add() and size_mul() to avoid
> any potential overflow.
> 
> Signed-off-by: Mehdi Ben Hadj Khelifa <mehdi.benhadjkhelifa@gmail.com>
> ---
>   fs/btrfs/volumes.c | 11 +++++------
>   1 file changed, 5 insertions(+), 6 deletions(-)
> 
> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> index c6e3efd6f602..3f1f19b28aac 100644
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -6076,12 +6076,11 @@ struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_info,
>   {
>   	struct btrfs_io_context *bioc;
>   
> -	bioc = kzalloc(
> -		 /* The size of btrfs_io_context */
> -		sizeof(struct btrfs_io_context) +
> -		/* Plus the variable array for the stripes */
> -		sizeof(struct btrfs_io_stripe) * (total_stripes),
> -		GFP_NOFS);
> +	/* The size of btrfs_io_context */
> +	/* Plus the variable array for the stripes */
> +	bioc = kzalloc(size_add(sizeof(struct btrfs_io_context),

Please use struct_size() instead.

And if you're using struct_size() there iwll be no need for any comments.
> +				size_mul(sizeof(struct btrfs_io_stripe),
> +						total_stripes)), GFP_NOFS);
>   
>   	if (!bioc)
>   		return NULL;
Re: [PATCH] btrfs: Refactor allocation size calculation in kzalloc()
Posted by Mehdi Ben Hadj Khelifa 2 months, 2 weeks ago
On 9/30/25 10:19 AM, Qu Wenruo wrote:
> 
> 
> 在 2025/9/30 18:44, Mehdi Ben Hadj Khelifa 写道:
>> Wrap allocation size calculation in size_add() and size_mul() to avoid
>> any potential overflow.
>>
>> Signed-off-by: Mehdi Ben Hadj Khelifa <mehdi.benhadjkhelifa@gmail.com>
>> ---
>>   fs/btrfs/volumes.c | 11 +++++------
>>   1 file changed, 5 insertions(+), 6 deletions(-)
>>
>> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
>> index c6e3efd6f602..3f1f19b28aac 100644
>> --- a/fs/btrfs/volumes.c
>> +++ b/fs/btrfs/volumes.c
>> @@ -6076,12 +6076,11 @@ struct btrfs_io_context 
>> *alloc_btrfs_io_context(struct btrfs_fs_info *fs_info,
>>   {
>>       struct btrfs_io_context *bioc;
>> -    bioc = kzalloc(
>> -         /* The size of btrfs_io_context */
>> -        sizeof(struct btrfs_io_context) +
>> -        /* Plus the variable array for the stripes */
>> -        sizeof(struct btrfs_io_stripe) * (total_stripes),
>> -        GFP_NOFS);
>> +    /* The size of btrfs_io_context */
>> +    /* Plus the variable array for the stripes */
>> +    bioc = kzalloc(size_add(sizeof(struct btrfs_io_context),
> 
> Please use struct_size() instead.
> 
> And if you're using struct_size() there iwll be no need for any comments.

Understood, I will get v2 on the way with your recommendation.
Thank you for the suggestion.
Regards,
Mehdi

>> +                size_mul(sizeof(struct btrfs_io_stripe),
>> +                        total_stripes)), GFP_NOFS);
>>       if (!bioc)
>>           return NULL;
>