public inbox for linux-kernel-mentees@lists.linux-foundation.org
 help / color / mirror / Atom feed
* [PATCH v2] btrfs: Refactor allocation size calculation in kzalloc()
@ 2025-09-30 10:03 Mehdi Ben Hadj Khelifa
  2025-09-30 10:09 ` Qu Wenruo
  0 siblings, 1 reply; 2+ messages in thread
From: Mehdi Ben Hadj Khelifa @ 2025-09-30 10:03 UTC (permalink / raw)
  To: clm, dsterba
  Cc: linux-btrfs, linux-kernel, linux-kernel-mentees, skhan,
	david.hunter.linux, Mehdi Ben Hadj Khelifa

Wrap allocation size calculation in struct_size() to 
avoid potential overflows.

Signed-off-by: Mehdi Ben Hadj Khelifa <mehdi.benhadjkhelifa@gmail.com>
---
Changelog:

Changes since v1:

-Use of struct_size() instead of size_add() and size_mul()
Link:https://lore.kernel.org/linux-btrfs/342929a3-ac5f-4953-a763-b81c60e66554@gmail.com/T/#mbe2932fec1a56e7db21bc8a3d1f1271a2c1422d7

 fs/btrfs/volumes.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index c6e3efd6f602..d349d0b180ac 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -6076,12 +6076,7 @@ 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);
+	bioc = kzalloc(struct_size(bioc, stripes, total_stripes), GFP_NOFS);
 
 	if (!bioc)
 		return NULL;
-- 
2.51.0


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

* Re: [PATCH v2] btrfs: Refactor allocation size calculation in kzalloc()
  2025-09-30 10:03 [PATCH v2] btrfs: Refactor allocation size calculation in kzalloc() Mehdi Ben Hadj Khelifa
@ 2025-09-30 10:09 ` Qu Wenruo
  0 siblings, 0 replies; 2+ messages in thread
From: Qu Wenruo @ 2025-09-30 10:09 UTC (permalink / raw)
  To: Mehdi Ben Hadj Khelifa, clm, dsterba
  Cc: linux-btrfs, linux-kernel, linux-kernel-mentees, skhan,
	david.hunter.linux



在 2025/9/30 19:33, Mehdi Ben Hadj Khelifa 写道:
> Wrap allocation size calculation in struct_size() to
> avoid potential overflows.

I don't think it's really about overflow or whatever, just pure code 
refactor to make it more structured/easier to read.

You don't need to send out a v3, we can handle it at merge time.

> 
> Signed-off-by: Mehdi Ben Hadj Khelifa <mehdi.benhadjkhelifa@gmail.com>

Otherwise it looks good to me.

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu
> ---
> Changelog:
> 
> Changes since v1:
> 
> -Use of struct_size() instead of size_add() and size_mul()
> Link:https://lore.kernel.org/linux-btrfs/342929a3-ac5f-4953-a763-b81c60e66554@gmail.com/T/#mbe2932fec1a56e7db21bc8a3d1f1271a2c1422d7
> 
>   fs/btrfs/volumes.c | 7 +------
>   1 file changed, 1 insertion(+), 6 deletions(-)
> 
> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> index c6e3efd6f602..d349d0b180ac 100644
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -6076,12 +6076,7 @@ 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);
> +	bioc = kzalloc(struct_size(bioc, stripes, total_stripes), GFP_NOFS);
>   
>   	if (!bioc)
>   		return NULL;


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

end of thread, other threads:[~2025-09-30 10:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-30 10:03 [PATCH v2] btrfs: Refactor allocation size calculation in kzalloc() Mehdi Ben Hadj Khelifa
2025-09-30 10:09 ` Qu Wenruo

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