Skip to contentSkip to conversation

Use kmalloc_array() instead of kmalloc()

PATCH In review Sidharth Seela 2 participants 2 messages Nov 23, 2025 – Jan 10

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

PATCH v2 1/1 Use kmalloc_array() instead of kmalloc()

Refactor to new API, for cases with dynamic size calculations inside kmalloc(). Such calculations were found using grep in the sources. The patch compiles properly with multi_v7_defconfig and multi_v5_defconfig and generates object files for the following driver files.

---
Changelog:
v1:https://lore.kernel.org/all/[email protected]/ -refactor code and run checkpatch.
-use sizeof(*ptr) not sizeof(void*).
-add RFT and the how the issue was found.

drivers/clk/at91/sam9x7.c +3 -3
index 89868a0aeaba..01bf557034e0 100644
@@ -754,9 +754,9 @@ static void __init sam9x7_pmc_setup(struct device_node *np)
 	if (!sam9x7_pmc)
 		return;
 
-	clk_mux_buffer = kmalloc(sizeof(void *) *
-				 (ARRAY_SIZE(sam9x7_gck)),
-				 GFP_KERNEL);
+	clk_mux_buffer = kmalloc_array(ARRAY_SIZE(sam9x7_gck),
+				       sizeof(*clk_mux_buffer),
+				       GFP_KERNEL);
 	if (!clk_mux_buffer)
 		goto err_free;
 
drivers/clk/at91/sama7d65.c +3 -3
index 7dee2b160ffb..91a5615f724a 100644
@@ -1127,9 +1127,9 @@ static void __init sama7d65_pmc_setup(struct device_node *np)
 	if (!sama7d65_pmc)
 		return;
 
-	alloc_mem = kmalloc(sizeof(void *) *
-			    (ARRAY_SIZE(sama7d65_mckx) + ARRAY_SIZE(sama7d65_gck)),
-			    GFP_KERNEL);
+	alloc_mem = kmalloc_array(ARRAY_SIZE(sama7d65_mckx) + ARRAY_SIZE(sama7d65_gck),
+				  sizeof(*alloc_mem),
+				  GFP_KERNEL);
 	if (!alloc_mem)
 		goto err_free;
 
drivers/clk/at91/sama7g5.c +3 -3
index 1340c2b00619..a12381f6c068 100644
@@ -1004,9 +1004,9 @@ static void __init sama7g5_pmc_setup(struct device_node *np)
 	if (!sama7g5_pmc)
 		return;
 
-	alloc_mem = kmalloc(sizeof(void *) *
-			    (ARRAY_SIZE(sama7g5_mckx) + ARRAY_SIZE(sama7g5_gck)),
-			    GFP_KERNEL);
+	alloc_mem = kmalloc_array(ARRAY_SIZE(sama7g5_mckx) + ARRAY_SIZE(sama7g5_gck),
+				  sizeof(*alloc_mem),
+				  GFP_KERNEL);
 	if (!alloc_mem)
 		goto err_free;
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Please use "clk: at91: Use kmalloc_array() instead of kmalloc()" in patch title.

Quoted 5 lines · Sidharth Seela

Refactor to new API, for cases with dynamic size calculations inside kmalloc(). Such calculations were found using grep in the sources. The patch compiles properly with multi_v7_defconfig and multi_v5_defconfig and generates object files for the following driver files.

Patch description should answer to "what?" are you doing and "why?" are you doing it. Please specify also why are you doing this change.

The rest looks good to me.

I tested it and saw no issues.

Thank you,
Claudiu