[PATCH] clk: renesas: fix memory leak in cpg_mssr_reserved_init()

chenyuan_fl@163.com posted 1 patch 2 weeks, 3 days ago
There is a newer version of this series
drivers/clk/renesas/renesas-cpg-mssr.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
[PATCH] clk: renesas: fix memory leak in cpg_mssr_reserved_init()
Posted by chenyuan_fl@163.com 2 weeks, 3 days ago
From: Yuan CHen <chenyuan@kylinos.cn>

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")
Signed-off-by: Yuan CHen <chenyuan@kylinos.cn>
---
 drivers/clk/renesas/renesas-cpg-mssr.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/renesas/renesas-cpg-mssr.c b/drivers/clk/renesas/renesas-cpg-mssr.c
index 5ff6ee1f7d4b..de1cf7ba45b7 100644
--- a/drivers/clk/renesas/renesas-cpg-mssr.c
+++ b/drivers/clk/renesas/renesas-cpg-mssr.c
@@ -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() */
-- 
2.39.5
Re: [PATCH] clk: renesas: fix memory leak in cpg_mssr_reserved_init()
Posted by Kuninori Morimoto 2 weeks, 2 days ago
Hi chenyuan, Geet

# It seems 1st mail had strange header, repost

Thank you for the patch

> 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")
> Signed-off-by: Yuan CHen <chenyuan@kylinos.cn>
> ---

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

Acked-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>

Thank you for your help !!

Best regards
---
Kuninori Morimoto
Re: [PATCH] clk: renesas: fix memory leak in cpg_mssr_reserved_init()
Posted by Kuninori Morimoto 2 weeks, 2 days ago
Hi chenyuan, Geet

Thank you for the patch

> 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")
> Signed-off-by: Yuan CHen <chenyuan@kylinos.cn>
> ---

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

Acked-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>

Thank you for your help !!

Best regards
---
Kuninori Morimoto