public inbox for linux-kernel-mentees@lists.linux-foundation.org
 help / color / mirror / Atom feed
* [PATCH] btrfs: Refactor allocation size calculation in kzalloc()
@ 2025-09-30  9:14 Mehdi Ben Hadj Khelifa
  2025-09-30  9:19 ` Qu Wenruo
  0 siblings, 1 reply; 3+ messages in thread
From: Mehdi Ben Hadj Khelifa @ 2025-09-30  9:14 UTC (permalink / raw)
  To: clm, dsterba
  Cc: linux-btrfs, linux-kernel, skhan, david.hunter.linux,
	linux-kernel-mentees, 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),
+				size_mul(sizeof(struct btrfs_io_stripe),
+						total_stripes)), GFP_NOFS);
 
 	if (!bioc)
 		return NULL;
-- 
2.51.0


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

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

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

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