accel/habanalabs: use kcalloc() instead of kzalloc()
PATCH 1/1 accel/habanalabs: use kcalloc() instead of kzalloc()
As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.
So, use the purpose specific kcalloc() function instead of the argument size * count in the kzalloc() function.
--- drivers/accel/habanalabs/common/mmu/mmu_v1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/accel/habanalabs/common/mmu/mmu_v1.c +1 -1
@@ -232,7 +232,7 @@ static int dram_default_mapping_init(struct hl_ctx *ctx)/* add hop1 and hop2 */ total_hops = num_of_hop3 +2 ; - ctx->dram_default_hops =kzalloc (HL_PTE_SIZE * total_hops,GFP_KERNEL ); + ctx->dram_default_hops =kcalloc (total_hops,HL_PTE_SIZE ,GFP_KERNEL );if (!ctx->dram_default_hops)return -ENOMEM ;
--
2.25.1
Quoted 8 lines · Erick Archer
As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.
Quoted 7 lines
So, use the purpose specific
kcalloc()function instead of the argument size * count in thekzalloc()function.Signed-off-by Erick Archer <[email protected]>
Thanks!
Signature · 1 line
Gustavo
Quoted 21 lines
--- drivers/accel/habanalabs/common/mmu/mmu_v1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/accel/habanalabs/common/mmu/mmu_v1.c +1
-2@@ -232,7 +232,7 @@ static int dram_default_mapping_init(struct hl_ctx *ctx)/* add hop1 and hop2 */ total_hops = num_of_hop3 +2 ; - ctx->dram_default_hops =kzalloc (HL_PTE_SIZE * total_hops,GFP_KERNEL ); + ctx->dram_default_hops =kcalloc (total_hops,HL_PTE_SIZE ,GFP_KERNEL );if (!ctx->dram_default_hops)return -ENOMEM ; --2.25.1
Quoted 14 lines · Gustavo A. R. Silva
Quoted 11 lines · Erick Archer
As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.
So, use the purpose specific
kcalloc()function instead of the argument size * count in thekzalloc()function.
Quoted 8 lines
Quoted 4 lines
Signed-off-by Erick Archer <[email protected]>Reviewed-by Gustavo A. R. Silva <[email protected]>Thanks!
Applied to -next.
Thanks,
Oded