mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] RDMA/erdma: Use vcalloc() instead of vzalloc()
@ 2025-08-20 13:21 Qianfeng Rong
  2025-08-20 14:34 ` Markus Elfring
  0 siblings, 1 reply; 4+ messages in thread
From: Qianfeng Rong @ 2025-08-20 13:21 UTC (permalink / raw)
  To: Cheng Xu, Kai Shen, Jason Gunthorpe, Leon Romanovsky, linux-rdma,
	linux-kernel
  Cc: Qianfeng Rong

Replace vzalloc() with vcalloc() in vmalloc_to_dma_addrs().  As noted
in the kernel documentation [1], open-coded multiplication in allocator
arguments is discouraged because it can lead to integer overflow.

Use vcalloc() to gain built-in overflow protection, making memory
allocation safer when calculating allocation size compared to explicit
multiplication.

[1]: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/infiniband/hw/erdma/erdma_verbs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/infiniband/hw/erdma/erdma_verbs.c b/drivers/infiniband/hw/erdma/erdma_verbs.c
index 996860f49b2f..63cabb8a6d96 100644
--- a/drivers/infiniband/hw/erdma/erdma_verbs.c
+++ b/drivers/infiniband/hw/erdma/erdma_verbs.c
@@ -671,7 +671,7 @@ static u32 vmalloc_to_dma_addrs(struct erdma_dev *dev, dma_addr_t **dma_addrs,
 
 	npages = (PAGE_ALIGN((u64)buf + len) - PAGE_ALIGN_DOWN((u64)buf)) >>
 		 PAGE_SHIFT;
-	pg_dma = vzalloc(npages * sizeof(dma_addr_t));
+	pg_dma = vcalloc(npages, sizeof(dma_addr_t));
 	if (!pg_dma)
 		return 0;
 
-- 
2.34.1


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

* Re: [PATCH] RDMA/erdma: Use vcalloc() instead of vzalloc()
  2025-08-20 13:21 [PATCH] RDMA/erdma: Use vcalloc() instead of vzalloc() Qianfeng Rong
@ 2025-08-20 14:34 ` Markus Elfring
  2025-08-21  6:26   ` Cheng Xu
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Elfring @ 2025-08-20 14:34 UTC (permalink / raw)
  To: Qianfeng Rong, linux-rdma
  Cc: LKML, Cheng Xu, Jason Gunthorpe, Kai Shen, Leon Romanovsky,
	Liao Yuanhong

> Replace vzalloc() with vcalloc() in vmalloc_to_dma_addrs(). …



…
> +++ b/drivers/infiniband/hw/erdma/erdma_verbs.c
> @@ -671,7 +671,7 @@ static u32 vmalloc_to_dma_addrs(struct erdma_dev *dev, dma_addr_t **dma_addrs,
>  
>  	npages = (PAGE_ALIGN((u64)buf + len) - PAGE_ALIGN_DOWN((u64)buf)) >>
>  		 PAGE_SHIFT;
> -	pg_dma = vzalloc(npages * sizeof(dma_addr_t));
> +	pg_dma = vcalloc(npages, sizeof(dma_addr_t));
…

How do you think about to adjust also the size determination?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.17-rc2#n944

	pg_dma = vcalloc(npages, sizeof(*pg_dma));


Regards,
Markus

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

* Re: [PATCH] RDMA/erdma: Use vcalloc() instead of vzalloc()
  2025-08-20 14:34 ` Markus Elfring
@ 2025-08-21  6:26   ` Cheng Xu
  2025-08-21  6:44     ` Qianfeng Rong
  0 siblings, 1 reply; 4+ messages in thread
From: Cheng Xu @ 2025-08-21  6:26 UTC (permalink / raw)
  To: Markus Elfring, Qianfeng Rong, linux-rdma
  Cc: LKML, Jason Gunthorpe, Kai Shen, Leon Romanovsky, Liao Yuanhong



On 8/20/25 10:34 PM, Markus Elfring wrote:
>> Replace vzalloc() with vcalloc() in vmalloc_to_dma_addrs(). …
> 
> 
> 
> …
>> +++ b/drivers/infiniband/hw/erdma/erdma_verbs.c
>> @@ -671,7 +671,7 @@ static u32 vmalloc_to_dma_addrs(struct erdma_dev *dev, dma_addr_t **dma_addrs,
>>  
>>  	npages = (PAGE_ALIGN((u64)buf + len) - PAGE_ALIGN_DOWN((u64)buf)) >>
>>  		 PAGE_SHIFT;
>> -	pg_dma = vzalloc(npages * sizeof(dma_addr_t));
>> +	pg_dma = vcalloc(npages, sizeof(dma_addr_t));
> …
> 
> How do you think about to adjust also the size determination?
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.17-rc2#n944
> 
> 	pg_dma = vcalloc(npages, sizeof(*pg_dma));
> 

Hi Qianfeng and Markus,

Both your changes look good to me.

Qianfeng, Could you send v2 including Markus's change?

Thanks,
Cheng Xu

> 
> Regards,
> Markus

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

* Re: [PATCH] RDMA/erdma: Use vcalloc() instead of vzalloc()
  2025-08-21  6:26   ` Cheng Xu
@ 2025-08-21  6:44     ` Qianfeng Rong
  0 siblings, 0 replies; 4+ messages in thread
From: Qianfeng Rong @ 2025-08-21  6:44 UTC (permalink / raw)
  To: Cheng Xu, Markus Elfring, linux-rdma
  Cc: LKML, Jason Gunthorpe, Kai Shen, Leon Romanovsky, Liao Yuanhong


在 2025/8/21 14:26, Cheng Xu 写道:
>
> On 8/20/25 10:34 PM, Markus Elfring wrote:
>>> Replace vzalloc() with vcalloc() in vmalloc_to_dma_addrs(). …
>>
>>
>> …
>>> +++ b/drivers/infiniband/hw/erdma/erdma_verbs.c
>>> @@ -671,7 +671,7 @@ static u32 vmalloc_to_dma_addrs(struct erdma_dev *dev, dma_addr_t **dma_addrs,
>>>   
>>>   	npages = (PAGE_ALIGN((u64)buf + len) - PAGE_ALIGN_DOWN((u64)buf)) >>
>>>   		 PAGE_SHIFT;
>>> -	pg_dma = vzalloc(npages * sizeof(dma_addr_t));
>>> +	pg_dma = vcalloc(npages, sizeof(dma_addr_t));
>> …
>>
>> How do you think about to adjust also the size determination?
>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.17-rc2#n944
>>
>> 	pg_dma = vcalloc(npages, sizeof(*pg_dma));
>>
> Hi Qianfeng and Markus,
>
> Both your changes look good to me.
>
> Qianfeng, Could you send v2 including Markus's change?
Ok, I will release v2 with Markus's change later.
>
> Thanks,
> Cheng Xu
>
>> Regards,
>> Markus

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

end of thread, other threads:[~2025-08-21  6:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-20 13:21 [PATCH] RDMA/erdma: Use vcalloc() instead of vzalloc() Qianfeng Rong
2025-08-20 14:34 ` Markus Elfring
2025-08-21  6:26   ` Cheng Xu
2025-08-21  6:44     ` Qianfeng Rong

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

all inboxes | Powered by JetHome®