Skip to contentSkip to conversation

clk: renesas: fix memory leak in cpg_mssr_reserved_init()

PATCH Superseded [email protected] 3 participants 8 messages Sep 5, 2025 – Sep 15, 2025

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

PATCH 1/1 clk: renesas: fix memory leak in cpg_mssr_reserved_init()

From: Yuan CHen <[email protected]>

In the current implementation, when krealloc_array() fails, the original memory pointer is incorrectly set to NULL after kfree(), resulting in a memory leak during reallocation failure.

Fixes 6aa17547649 ("clk: renesas: cpg-mssr: Ignore all clocks assigned to non-Linux system")
---
 drivers/clk/renesas/renesas-cpg-mssr.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/clk/renesas/renesas-cpg-mssr.c +5 -2
index 5ff6ee1f7d4b..de1cf7ba45b7 100644
@@ -1082,6 +1082,7 @@ static int __init cpg_mssr_reserved_init(struct cpg_mssr_priv *priv,
 
 		of_for_each_phandle(&it, rc, node, "clocks", "#clock-cells", -1) {
 			int idx;
+			unsigned int *new_ids;
 
 			if (it.node != priv->np)
 				continue;
@@ -1092,11 +1093,13 @@ static int __init cpg_mssr_reserved_init(struct cpg_mssr_priv *priv,
 			if (args[0] != CPG_MOD)
 				continue;
 
-			ids = krealloc_array(ids, (num + 1), sizeof(*ids), GFP_KERNEL);
-			if (!ids) {
+			new_ids = krealloc_array(ids, (num + 1), sizeof(*ids), GFP_KERNEL);
+			if (!new_ids) {
 				of_node_put(it.node);
+				kfree(ids);
 				return -ENOMEM;
 			}
+			ids = new_ids;
 
 			if (priv->reg_layout == CLK_REG_LAYOUT_RZ_A)
 				idx = MOD_CLK_PACK_10(args[1]);	/* for DEF_MOD_STB() */
Signature · 1 line
2.39.5
#1 lore raw

Hi chenyuan

Thank you for your patch

Quoted 6 lines

In the current implementation, when krealloc_array() fails, the original memory pointer is incorrectly set to NULL after kfree(), resulting in a memory leak during reallocation failure.

Fixes 6aa17547649 ("clk: renesas: cpg-mssr: Ignore all clocks assigned to non-Linux system")

Basically, I can agree that current code has memory leak issue.

But what does "the original memory pointer is incorrectly set to NULL after kfree()," mean ? I guess git-log needs update ?

Thank you for your help !!

Best regards
---
Kuninori Morimoto

#2 lore raw

PATCH v2 1/1 clk: renesas: fix memory leak in cpg_mssr_reserved_init()

From: Yuan CHen <[email protected]>

In the current implementation, when krealloc_array() fails, the error handling path incorrectly sets the original memory pointer to NULL before it is freed, resulting in a memory leak during reallocation failure.

Fixes 6aa17547649 ("clk: renesas: cpg-mssr: Ignore all clocks assigned to non-Linux system")
---
 drivers/clk/renesas/renesas-cpg-mssr.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/clk/renesas/renesas-cpg-mssr.c +5 -2
index 5ff6ee1f7d4b..de1cf7ba45b7 100644
@@ -1082,6 +1082,7 @@ static int __init cpg_mssr_reserved_init(struct cpg_mssr_priv *priv,
 
 		of_for_each_phandle(&it, rc, node, "clocks", "#clock-cells", -1) {
 			int idx;
+			unsigned int *new_ids;
 
 			if (it.node != priv->np)
 				continue;
@@ -1092,11 +1093,13 @@ static int __init cpg_mssr_reserved_init(struct cpg_mssr_priv *priv,
 			if (args[0] != CPG_MOD)
 				continue;
 
-			ids = krealloc_array(ids, (num + 1), sizeof(*ids), GFP_KERNEL);
-			if (!ids) {
+			new_ids = krealloc_array(ids, (num + 1), sizeof(*ids), GFP_KERNEL);
+			if (!new_ids) {
 				of_node_put(it.node);
+				kfree(ids);
 				return -ENOMEM;
 			}
+			ids = new_ids;
 
 			if (priv->reg_layout == CLK_REG_LAYOUT_RZ_A)
 				idx = MOD_CLK_PACK_10(args[1]);	/* for DEF_MOD_STB() */
Signature · 1 line
2.39.5
#3 lore raw

Hi chenyuan

Thank you for the patch

Quoted 6 lines

In the current implementation, when krealloc_array() fails, the error handling path incorrectly sets the original memory pointer to NULL before it is freed, resulting in a memory leak during reallocation failure.

Fixes 6aa17547649 ("clk: renesas: cpg-mssr: Ignore all clocks assigned to non-Linux system")

You want to use ${LINUX}/scripts/checkpatch.pl to get maximum 75 chars warning

Patch itself is agree, but I still don't understand the git-log.

incorrectly sets the original memory pointer to NULL before it is freed,

I still don't understand this part. which part are you talking about ? What does this "original memory pointer" mean ? And what does this "freed" mean ? freed what ??

I guess you want to indicate is like this ?

In case of krealloc_array() failure, current error handling just returns from function without freeing alloced array. It cause a memory leak. Fixup it.

Thank you for your help !!

Best regards
---
Kuninori Morimoto

Reviewed-by
#4 lore raw
Quoted 17 lines · Kuninori Morimoto
Quoted 6 lines

In the current implementation, when krealloc_array() fails, the error handling path incorrectly sets the original memory pointer to NULL before it is freed, resulting in a memory leak during reallocation failure.

Fixes 6aa17547649 ("clk: renesas: cpg-mssr: Ignore all clocks assigned to non-Linux system")

You want to use ${LINUX}/scripts/checkpatch.pl to get maximum 75 chars warning

Patch itself is agree, but I still don't understand the git-log.

incorrectly sets the original memory pointer to NULL before it is freed,

I still don't understand this part. which part are you talking about ? What does this "original memory pointer" mean ? And what does this "freed" mean ? freed what ??

Quoted 5 lines

I guess you want to indicate is like this ?

In case of krealloc_array() failure, current error handling just returns from function without freeing alloced array. It cause a memory leak. Fixup it.

i.e. will queue in renesas-clk for v6.18, with the patch description reworded.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#5 lore raw

PATCH 1/1 clk: renesas: fix memory leak in cpg_mssr_reserved_init()

In reply to Kuninori Morimoto

From: Yuan CHen <[email protected]>

In case of krealloc_array() failure, current error handling just returns from function without freeing alloced array. It cause a memory leak. Fixup it.

Fixes 6aa175476490 ("clk: renesas: cpg-mssr: Ignore all clocks assigned to non-Linux system")
---
 drivers/clk/renesas/renesas-cpg-mssr.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/clk/renesas/renesas-cpg-mssr.c +5 -2
index 5ff6ee1f7d4b..de1cf7ba45b7 100644
@@ -1082,6 +1082,7 @@ static int __init cpg_mssr_reserved_init(struct cpg_mssr_priv *priv,
 
 		of_for_each_phandle(&it, rc, node, "clocks", "#clock-cells", -1) {
 			int idx;
+			unsigned int *new_ids;
 
 			if (it.node != priv->np)
 				continue;
@@ -1092,11 +1093,13 @@ static int __init cpg_mssr_reserved_init(struct cpg_mssr_priv *priv,
 			if (args[0] != CPG_MOD)
 				continue;
 
-			ids = krealloc_array(ids, (num + 1), sizeof(*ids), GFP_KERNEL);
-			if (!ids) {
+			new_ids = krealloc_array(ids, (num + 1), sizeof(*ids), GFP_KERNEL);
+			if (!new_ids) {
 				of_node_put(it.node);
+				kfree(ids);
 				return -ENOMEM;
 			}
+			ids = new_ids;
 
 			if (priv->reg_layout == CLK_REG_LAYOUT_RZ_A)
 				idx = MOD_CLK_PACK_10(args[1]);	/* for DEF_MOD_STB() */
Signature · 1 line
2.39.5
Acked-by on v1 1/1
#6 lore raw

Hi chenyuan, Geet

Thank you for the patch

Quoted 7 lines

In case of krealloc_array() failure, current error handling just returns from function without freeing alloced array. It cause a memory leak. Fixup it.

Fixes 6aa175476490 ("clk: renesas: cpg-mssr: Ignore all clocks assigned to non-Linux system")

---

I think Geet already applied this patch with fixing git-log, but

Thank you for your help !!

Best regards
---
Kuninori Morimoto

Acked-by on v1 1/1
#7 lore raw
In reply to [email protected]

Hi chenyuan, Geet

# It seems 1st mail had strange header, repost

Thank you for the patch

Quoted 7 lines

In case of krealloc_array() failure, current error handling just returns from function without freeing alloced array. It cause a memory leak. Fixup it.

Fixes 6aa175476490 ("clk: renesas: cpg-mssr: Ignore all clocks assigned to non-Linux system")

---

I think Geet already applied this patch with fixing git-log, but

Thank you for your help !!

Best regards
---
Kuninori Morimoto