Skip to contentSkip to conversation

clk: imx93: fix memory leak and missing unwind goto in imx93_clocks_probe

PATCH Applied Zhanhao Hu 4 participants 6 messages Jun 1, 2023 – Jan 5, 2024

Subsystems (4)
lore
Share & tools
lore

Zhanhao Hu

author
#0 lore raw

PATCH v2 1/1 clk: imx93: fix memory leak and missing unwind goto in imx93_clocks_probe

In function probe(), it returns directly without unregistered hws when error occurs.

Fix this by adding 'goto unregister_hws;' on line 295 and line 310.

Use devm_kzalloc() instead of kzalloc() to automatically free the memory using devm_kfree() when error occurs.

Replace of_iomap() with devm_of_iomap() to automatically handle the unused ioremap region and delete 'iounmap(anatop_base);' in unregister_hws.

Fixes 24defbe194b6 ("clk: imx: add i.MX93 clk")

---
V1 -> V2 : Rephrase commit message in imperative mood. This issue is found by static analysis and remains untested.

---
 drivers/clk/imx/clk-imx93.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/clk/imx/clk-imx93.c +8 -7
index 07b4a043e449..b6c7c2725906 100644
@@ -264,7 +264,7 @@ static int imx93_clocks_probe(struct platform_device *pdev)
 	void __iomem *base, *anatop_base;
 	int i, ret;
 
-	clk_hw_data = kzalloc(struct_size(clk_hw_data, hws,
+	clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data, hws,
 					  IMX93_CLK_END), GFP_KERNEL);
 	if (!clk_hw_data)
 		return -ENOMEM;
@@ -288,10 +288,12 @@ static int imx93_clocks_probe(struct platform_device *pdev)
 								    "sys_pll_pfd2", 1, 2);
 
 	np = of_find_compatible_node(NULL, NULL, "fsl,imx93-anatop");
-	anatop_base = of_iomap(np, 0);
+	anatop_base = devm_of_iomap(dev, np, 0, NULL);
 	of_node_put(np);
-	if (WARN_ON(!anatop_base))
-		return -ENOMEM;
+	if (WARN_ON(IS_ERR(anatop_base))) {
+		ret = PTR_ERR(base);
+		goto unregister_hws;
+	}
 
 	clks[IMX93_CLK_ARM_PLL] = imx_clk_fracn_gppll_integer("arm_pll", "osc_24m",
 							      anatop_base + 0x1000,
@@ -304,8 +306,8 @@ static int imx93_clocks_probe(struct platform_device *pdev)
 	np = dev->of_node;
 	base = devm_platform_ioremap_resource(pdev, 0);
 	if (WARN_ON(IS_ERR(base))) {
-		iounmap(anatop_base);
-		return PTR_ERR(base);
+		ret = PTR_ERR(base);
+		goto unregister_hws;
 	}
 
 	for (i = 0; i < ARRAY_SIZE(root_array); i++) {
@@ -345,7 +347,6 @@ static int imx93_clocks_probe(struct platform_device *pdev)
 
 unregister_hws:
 	imx_unregister_hw_clocks(clks, IMX93_CLK_END);
-	iounmap(anatop_base);
 
 	return ret;
 }
Signature · 7 lines
2.34.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Zhanhao Hu

In function probe(), it returns directly without unregistered hws when error occurs.

Fix this by adding 'goto unregister_hws;' on line 295 and line 310.

Use devm_kzalloc() instead of kzalloc() to automatically free the memory using devm_kfree() when error occurs.

Quoted 6 lines

Replace of_iomap() with devm_of_iomap() to automatically handle the unused ioremap region and delete 'iounmap(anatop_base);' in unregister_hws.

Fixes 24defbe194b6 ("clk: imx: add i.MX93 clk")
Quoted 58 lines

---
V1 -> V2 : Rephrase commit message in imperative mood. This issue is found by static analysis and remains untested.

---
 drivers/clk/imx/clk-imx93.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/clk/imx/clk-imx93.c +8 -7
index 07b4a043e449..b6c7c2725906 100644
@@ -264,7 +264,7 @@ static int imx93_clocks_probe(struct platform_device *pdev)
 	void __iomem *base, *anatop_base;
 	int i, ret;
 
-	clk_hw_data = kzalloc(struct_size(clk_hw_data, hws,
+	clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data, hws,
 					  IMX93_CLK_END), GFP_KERNEL);
 	if (!clk_hw_data)
 		return -ENOMEM;
@@ -288,10 +288,12 @@ static int imx93_clocks_probe(struct platform_device *pdev)
 								    "sys_pll_pfd2", 1, 2);
 
 	np = of_find_compatible_node(NULL, NULL, "fsl,imx93-anatop");
-	anatop_base = of_iomap(np, 0);
+	anatop_base = devm_of_iomap(dev, np, 0, NULL);
 	of_node_put(np);
-	if (WARN_ON(!anatop_base))
-		return -ENOMEM;
+	if (WARN_ON(IS_ERR(anatop_base))) {
+		ret = PTR_ERR(base);
+		goto unregister_hws;
+	}
 
 	clks[IMX93_CLK_ARM_PLL] = imx_clk_fracn_gppll_integer("arm_pll", "osc_24m",
 							      anatop_base + 0x1000,
@@ -304,8 +306,8 @@ static int imx93_clocks_probe(struct platform_device *pdev)
 	np = dev->of_node;
 	base = devm_platform_ioremap_resource(pdev, 0);
 	if (WARN_ON(IS_ERR(base))) {
-		iounmap(anatop_base);
-		return PTR_ERR(base);
+		ret = PTR_ERR(base);
+		goto unregister_hws;
 	}
 
 	for (i = 0; i < ARRAY_SIZE(root_array); i++) {
@@ -345,7 +347,6 @@ static int imx93_clocks_probe(struct platform_device *pdev)
 
 unregister_hws:
 	imx_unregister_hw_clocks(clks, IMX93_CLK_END);
-	iounmap(anatop_base);
 
 	return ret;
 }
Signature · 1 line
2.34.1
Applied
#2 lore raw
In reply to Zhanhao Hu
Quoted 10 lines · Zhanhao Hu

In function probe(), it returns directly without unregistered hws when error occurs.

Fix this by adding 'goto unregister_hws;' on line 295 and line 310.

Use devm_kzalloc() instead of kzalloc() to automatically free the memory using devm_kfree() when error occurs.

[...]

Applied, thanks!

[1/1] clk: imx93: fix memory leak and missing unwind goto in imx93_clocks_probe

      commit: e02ba11b457647050cb16e7cad16cec3c252fade

Best regards,

Signature · 1 line
Abel Vesa <[email protected]>
#3 lore raw

PATCH 1/1 clk: imx93: Fix an error code assignment in imx93_clocks_probe()

In reply to Zhanhao Hu

From: Markus Elfring <[email protected]>
Date: Tue, 13 Jun 2023 21:50:50 +0200

The variable “base” was passed to a call of the function “PTR_ERR” in the implementation of the function “imx93_clocks_probe”. Unfortunately, the variable was not assigned to an error pointer before this if branch.
Thus use the variable “anatop_base” for an error code assignment instead.

Fixes e02ba11b4576 ("clk: imx93: fix memory leak and missing unwind goto in imx93_clocks_probe")
---
 drivers/clk/imx/clk-imx93.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/imx/clk-imx93.c +1 -1
index b6c7c2725906..44f435103c65 100644
@@ -291,7 +291,7 @@ static int imx93_clocks_probe(struct platform_device *pdev)
 	anatop_base = devm_of_iomap(dev, np, 0, NULL);
 	of_node_put(np);
 	if (WARN_ON(IS_ERR(anatop_base))) {
-		ret = PTR_ERR(base);
+		ret = PTR_ERR(anatop_base);
 		goto unregister_hws;
 	}
 

--
2.41.0

reviewer Reviewed-by on v1 1/1
#4 lore raw

RE: [PATCH] clk: imx93: Fix an error code assignment in imx93_clocks_probe()

Quoted 24 lines

Subject: [PATCH] clk: imx93: Fix an error code assignment in imx93_clocks_probe()

From: Markus Elfring <[email protected]>
Date: Tue, 13 Jun 2023 21:50:50 +0200

The variable “base” was passed to a call of the function “PTR_ERR” in the implementation of the function “imx93_clocks_probe”. Unfortunately, the variable was not assigned to an error pointer before this if branch.
Thus use the variable “anatop_base” for an error code assignment instead.

Fixes e02ba11b4576 ("clk: imx93: fix memory leak and missing unwind goto

in imx93_clocks_probe")

---
 drivers/clk/imx/clk-imx93.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/imx/clk-imx93.c index renamed from drivers/clk/imx/clk-imx93.c +0 -0

b6c7c2725906..44f435103c65 100644

drivers/clk/imx/clk-imx93.c static int imx93_clocks_probe(struct · line 297

Quoted 8 lines
drivers/clk/imx/clk-imx93.c +1 -1
@@ -291,7 +291,7 @@ static int imx93_clocks_probe(struct
 platform_device *pdev)
 	anatop_base = devm_of_iomap(dev, np, 0, NULL);
 	of_node_put(np);
 	if (WARN_ON(IS_ERR(anatop_base))) {
-		ret = PTR_ERR(base);
+		ret = PTR_ERR(anatop_base);
 		goto unregister_hws;
 	}
Quoted 3 lines

--
2.41.0

#5 lore raw

Re: [PATCH] clk: imx93: Fix an error code assignment in imx93_clocks_probe()

In reply to Markus Elfring
Quoted 20 lines

From: Markus Elfring <[email protected]>
Date: Tue, 13 Jun 2023 21:50:50 +0200

The variable “base” was passed to a call of the function “PTR_ERR” in the implementation of the function “imx93_clocks_probe”. Unfortunately, the variable was not assigned to an error pointer before this if branch.
Thus use the variable “anatop_base” for an error code assignment instead.

Fixes e02ba11b4576 ("clk: imx93: fix memory leak and missing unwind goto in imx93_clocks_probe")
---
 drivers/clk/imx/clk-imx93.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/imx/clk-imx93.c +0 -0
index b6c7c2725906..44f435103c65 100644

drivers/clk/imx/clk-imx93.c static int imx93_clocks_probe(struct platform_device *pdev) · line 297

Quoted 8 lines
drivers/clk/imx/clk-imx93.c +1 -1
@@ -291,7 +291,7 @@ static int imx93_clocks_probe(struct platform_device *pdev)
 	anatop_base = devm_of_iomap(dev, np, 0, NULL);
 	of_node_put(np);
 	if (WARN_ON(IS_ERR(anatop_base))) {
-		ret = PTR_ERR(base);
+		ret = PTR_ERR(anatop_base);
 		goto unregister_hws;
 	}
 

Is this patch still in review queues?

See also:
https://lore.kernel.org/cocci/[email protected]/
https://sympa.inria.fr/sympa/arc/cocci/2023-06/msg00016.html

Regards,
Markus