Skip to contentSkip to conversation

clk: hisilicon: Use devm_kcalloc() instead of devm_kzalloc()

PATCH Applied Erick Archer 4 participants 4 messages Jan 21, 2024 – Feb 22, 2024 Common clk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk: hisilicon: Use devm_kcalloc() instead of devm_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 devm_kcalloc() function instead of the argument size * count in the devm_kzalloc() function.

---
 drivers/clk/hisilicon/clk-hi3559a.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/clk/hisilicon/clk-hi3559a.c +1 -2
index ff4ca0edce06..da476f940326 100644
@@ -461,8 +461,7 @@ static void hisi_clk_register_pll(struct hi3559av100_pll_clock *clks,
 	struct clk_init_data init;
 	int i;
 
-	p_clk = devm_kzalloc(dev, sizeof(*p_clk) * nums, GFP_KERNEL);
-
+	p_clk = devm_kcalloc(dev, nums, sizeof(*p_clk), GFP_KERNEL);
 	if (!p_clk)
 		return;
 

--
2.25.1

Reviewed-by on v1 1/1
#1 lore raw
Quoted 23 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 devm_kcalloc() function instead of the argument size * count in the devm_kzalloc() function.

---
 drivers/clk/hisilicon/clk-hi3559a.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/clk/hisilicon/clk-hi3559a.c +0 -0
index ff4ca0edce06..da476f940326 100644

drivers/clk/hisilicon/clk-hi3559a.c static void hisi_clk_register_pll(struct hi3559av100_pll_clock *clks, · line 466

Quoted 8 lines
drivers/clk/hisilicon/clk-hi3559a.c +1 -2
@@ -461,8 +461,7 @@ static void hisi_clk_register_pll(struct hi3559av100_pll_clock *clks,
 	struct clk_init_data init;
 	int i;
 
-	p_clk = devm_kzalloc(dev, sizeof(*p_clk) * nums, GFP_KERNEL);
-
+	p_clk = devm_kcalloc(dev, nums, sizeof(*p_clk), GFP_KERNEL);
 	if (!p_clk)
 		return;

Looks good,

My first impulse was to ask why hisi_clk_register_pll doesn't return an error if the allocation fails, but I guess that happens so early that no sensible error handling is possible?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
Reviewed-by on v1 1/1
#2 lore raw
In reply to Erick Archer
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 6 lines

So, use the purpose specific devm_kcalloc() function instead of the argument size * count in the devm_kzalloc() function.

Thanks!

Signature · 1 line
Gustavo
Quoted 22 lines
---
  drivers/clk/hisilicon/clk-hi3559a.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/clk/hisilicon/clk-hi3559a.c +1 -3
index ff4ca0edce06..da476f940326 100644
@@ -461,8 +461,7 @@ static void hisi_clk_register_pll(struct hi3559av100_pll_clock *clks,
  	struct clk_init_data init;
  	int i;
 
-	p_clk = devm_kzalloc(dev, sizeof(*p_clk) * nums, GFP_KERNEL);
-
+	p_clk = devm_kcalloc(dev, nums, sizeof(*p_clk), GFP_KERNEL);
  	if (!p_clk)
  		return;
 
--

2.25.1

maintainer Applied
#3 lore raw
In reply to Erick Archer

Quoting Erick Archer (2024-01-21 06:29:46)

Quoted 8 lines

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 devm_kcalloc() function instead of the argument size * count in the devm_kzalloc() function.

---

Applied to clk-next