linux-xfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] xfs: use kmalloc_array() instead of kmalloc() for map allocation
@ 2025-10-18 19:45 ` Kriish Sharma
  2025-10-21  7:02   ` Carlos Maiolino
                     ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Kriish Sharma @ 2025-10-18 19:45 UTC (permalink / raw)
  To: Carlos Maiolino
  Cc: linux-xfs, linux-kernel, linux-kernel-mentees, skhan,
	david.hunter.linux, Kriish Sharma

Using kmalloc_array() better reflects the intent to allocate an array of
map entries, and improves consistency with similar allocations across the
kernel.

No functional change intended.

Signed-off-by: Kriish Sharma <kriish.sharma2006@gmail.com>
---
 fs/xfs/xfs_qm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/xfs/xfs_qm.c b/fs/xfs/xfs_qm.c
index 23ba84ec919a..34ec61e455ff 100644
--- a/fs/xfs/xfs_qm.c
+++ b/fs/xfs/xfs_qm.c
@@ -1218,7 +1218,7 @@ xfs_qm_reset_dqcounts_buf(
 	if (qip->i_nblocks == 0)
 		return 0;
 
-	map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
+	map = kmalloc_array(XFS_DQITER_MAP_SIZE, sizeof(*map),
 			GFP_KERNEL | __GFP_NOFAIL);
 
 	lblkno = 0;
-- 
2.34.1


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

* Re: [PATCH] xfs: use kmalloc_array() instead of kmalloc() for map allocation
  2025-10-18 19:45 ` [PATCH] xfs: use kmalloc_array() instead of kmalloc() for map allocation Kriish Sharma
@ 2025-10-21  7:02   ` Carlos Maiolino
  2025-10-21 14:51   ` Darrick J. Wong
  2025-10-24  6:49   ` Nirjhar Roy (IBM)
  2 siblings, 0 replies; 4+ messages in thread
From: Carlos Maiolino @ 2025-10-21  7:02 UTC (permalink / raw)
  To: Kriish Sharma
  Cc: linux-xfs, linux-kernel, linux-kernel-mentees, skhan,
	david.hunter.linux

Hello.

On Sat, Oct 18, 2025 at 07:45:28PM +0000, Kriish Sharma wrote:
> Using kmalloc_array() better reflects the intent to allocate an array of
> map entries, and improves consistency with similar allocations across the
> kernel.
> 
> No functional change intended.

Thanks for the patch.
Have you ran xfstests against this patch? All patches sent to xfs should
at least pass basic testing via xfstests. I.e. ensure there is no new
failure between an unpatched kernel and a kernel with your patch.

If you did, please send me the results summary, if you did not, please
run it, and send a V2 of this patch  including in the patch description
the summary of the results.

Any changes includes risk, and so every patch sent to xfs should be at
least basically tested against xfstests auto group.

Thanks,
Carlos

> 
> Signed-off-by: Kriish Sharma <kriish.sharma2006@gmail.com>
> ---
>  fs/xfs/xfs_qm.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/xfs/xfs_qm.c b/fs/xfs/xfs_qm.c
> index 23ba84ec919a..34ec61e455ff 100644
> --- a/fs/xfs/xfs_qm.c
> +++ b/fs/xfs/xfs_qm.c
> @@ -1218,7 +1218,7 @@ xfs_qm_reset_dqcounts_buf(
>  	if (qip->i_nblocks == 0)
>  		return 0;
> 
> -	map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
> +	map = kmalloc_array(XFS_DQITER_MAP_SIZE, sizeof(*map),
>  			GFP_KERNEL | __GFP_NOFAIL);
> 
>  	lblkno = 0;
> --
> 2.34.1
> 

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

* Re: [PATCH] xfs: use kmalloc_array() instead of kmalloc() for map allocation
  2025-10-18 19:45 ` [PATCH] xfs: use kmalloc_array() instead of kmalloc() for map allocation Kriish Sharma
  2025-10-21  7:02   ` Carlos Maiolino
@ 2025-10-21 14:51   ` Darrick J. Wong
  2025-10-24  6:49   ` Nirjhar Roy (IBM)
  2 siblings, 0 replies; 4+ messages in thread
From: Darrick J. Wong @ 2025-10-21 14:51 UTC (permalink / raw)
  To: Kriish Sharma
  Cc: Carlos Maiolino, linux-xfs, linux-kernel, linux-kernel-mentees,
	skhan, david.hunter.linux

On Sat, Oct 18, 2025 at 07:45:28PM +0000, Kriish Sharma wrote:
> Using kmalloc_array() better reflects the intent to allocate an array of
> map entries, and improves consistency with similar allocations across the
> kernel.
> 
> No functional change intended.
> 
> Signed-off-by: Kriish Sharma <kriish.sharma2006@gmail.com>
> ---
>  fs/xfs/xfs_qm.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/xfs/xfs_qm.c b/fs/xfs/xfs_qm.c
> index 23ba84ec919a..34ec61e455ff 100644
> --- a/fs/xfs/xfs_qm.c
> +++ b/fs/xfs/xfs_qm.c
> @@ -1218,7 +1218,7 @@ xfs_qm_reset_dqcounts_buf(
>  	if (qip->i_nblocks == 0)
>  		return 0;
>  
> -	map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
> +	map = kmalloc_array(XFS_DQITER_MAP_SIZE, sizeof(*map),

Why would we replace a constant that can be computed at compile time
with a dynamic computation that is now fallible despite NOFAIL?

--D

>  			GFP_KERNEL | __GFP_NOFAIL);
>  
>  	lblkno = 0;
> -- 
> 2.34.1
> 
> 

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

* Re: [PATCH] xfs: use kmalloc_array() instead of kmalloc() for map allocation
  2025-10-18 19:45 ` [PATCH] xfs: use kmalloc_array() instead of kmalloc() for map allocation Kriish Sharma
  2025-10-21  7:02   ` Carlos Maiolino
  2025-10-21 14:51   ` Darrick J. Wong
@ 2025-10-24  6:49   ` Nirjhar Roy (IBM)
  2 siblings, 0 replies; 4+ messages in thread
From: Nirjhar Roy (IBM) @ 2025-10-24  6:49 UTC (permalink / raw)
  To: Kriish Sharma, Carlos Maiolino
  Cc: linux-xfs, linux-kernel, linux-kernel-mentees, skhan,
	david.hunter.linux

On Sat, 2025-10-18 at 19:45 +0000, Kriish Sharma wrote:
> Using kmalloc_array() better reflects the intent to allocate an array of
> map entries, and improves consistency with similar allocations across the
> kernel.
> 
> No functional change intended.
> 
> Signed-off-by: Kriish Sharma <kriish.sharma2006@gmail.com>
> ---
>  fs/xfs/xfs_qm.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/xfs/xfs_qm.c b/fs/xfs/xfs_qm.c
> index 23ba84ec919a..34ec61e455ff 100644
> --- a/fs/xfs/xfs_qm.c
> +++ b/fs/xfs/xfs_qm.c
> @@ -1218,7 +1218,7 @@ xfs_qm_reset_dqcounts_buf(
>  	if (qip->i_nblocks == 0)
>  		return 0;
>  
> -	map = kmalloc(XFS_DQITER_MAP_SIZE * sizeof(*map),
> +	map = kmalloc_array(XFS_DQITER_MAP_SIZE, sizeof(*map),
>  			GFP_KERNEL | __GFP_NOFAIL);
I think kmalloc_array is more useful when the size of memory to be allocated is dynamic with
kmalloc_array doing some additional checks on the size that is being passed. In this case, both
XFS_QUITER_MAP_SIZE  and sizeof(*map) are constant i.e, we are allocating constant size memory, so
maybe this isn't quite necessary?

Definition of kmalloc_array()

static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)
{
	if (size != 0 && n > SIZE_MAX / size)
		return NULL;
	return kmalloc(n * size, flags);
}


--NR
>  
>  	lblkno = 0;


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

end of thread, other threads:[~2025-10-24  6:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <eeslnTm4EeKA2atDj3UKUcXAaGwgiPzioO9tR3Kf0pBEyUjxEO_9DaFIpNYIlc_9ey63h8gqdBsIt9Xjgn7AkA==@protonmail.internalid>
2025-10-18 19:45 ` [PATCH] xfs: use kmalloc_array() instead of kmalloc() for map allocation Kriish Sharma
2025-10-21  7:02   ` Carlos Maiolino
2025-10-21 14:51   ` Darrick J. Wong
2025-10-24  6:49   ` Nirjhar Roy (IBM)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).