Skip to contentSkip to conversation

accel/habanalabs: use kcalloc() instead of kzalloc()

PATCH Applied Erick Archer 3 participants 3 messages Jan 20, 2024 – Jan 25, 2024

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

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
index d925dc4dd097..e3d42cfead27 100644
@@ -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

Reviewed-by on v1 1/1
#1 lore raw
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 the kzalloc() function.

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
index d925dc4dd097..e3d42cfead27 100644
@@ -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

maintainer Applied
#2 lore raw
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 the kzalloc() function.

Quoted 8 lines
Quoted 4 lines

Thanks!

Applied to -next.
Thanks,
Oded