RDMA/erdma: Use vcalloc() instead of vzalloc()
Subsystems (2)
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.
---
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
@@ -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
Quoted 10 lines · Qianfeng Rong
Replace
vzalloc()withvcalloc()invmalloc_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.
Quoted 8 lines
Signed-off-by Qianfeng Rong <[email protected]>---
v2: changesizeof(dma_addr_t)tosizeof(*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@@ -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 ;
Quoted 10 lines · Qianfeng Rong
Replace
vzalloc()withvcalloc()invmalloc_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.
Quoted 7 lines
Signed-off-by Qianfeng Rong <[email protected]>---
v2: changesizeof(dma_addr_t)tosizeof(*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