Skip to contentSkip to conversation

RDMA/erdma: Use vcalloc() instead of vzalloc()

PATCH Applied Qianfeng Rong 3 participants 3 messages Aug 21, 2025 – Aug 25, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 RDMA/erdma: Use vcalloc() instead of vzalloc()

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

---
v2: change sizeof(dma_addr_t) to sizeof(*pg_dma) to improve code
robustness as suggested by Markus.

---
 drivers/infiniband/hw/erdma/erdma_verbs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/hw/erdma/erdma_verbs.c +1 -1
index 996860f49b2f..109a3f3de911 100644
@@ -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(*pg_dma));
 	if (!pg_dma)
 		return 0;
 
Signature · 1 line
2.34.1
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · 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

Quoted 8 lines

---
v2: change sizeof(dma_addr_t) to sizeof(*pg_dma) to improve code
robustness as suggested by Markus.

---
 drivers/infiniband/hw/erdma/erdma_verbs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks.

Quoted 13 lines
drivers/infiniband/hw/erdma/erdma_verbs.c +1 -1
index 996860f49b2f..109a3f3de911 100644
@@ -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(*pg_dma));
 	if (!pg_dma)
 		return 0;
 
maintainer Applied
#2 lore raw
In reply to Qianfeng Rong
Quoted 10 lines · 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

Quoted 7 lines

---
v2: change sizeof(dma_addr_t) to sizeof(*pg_dma) to improve code
robustness as suggested by Markus.

---
 drivers/infiniband/hw/erdma/erdma_verbs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Applied to for-next, thanks

Jason