Skip to contentSkip to conversation

clk: imx: scu: Fix memory leak in __imx_clk_gpr_scu()

PATCH Applied Kuan-Wei Chiu 4 participants 8 messages Dec 10, 2023 – Feb 26, 2024

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

PATCH 1/1 clk: imx: scu: Fix memory leak in __imx_clk_gpr_scu()

In cases where imx_clk_is_resource_owned() returns false, the code path does not handle the failure gracefully, potentially leading to a memory leak. This fix ensures proper cleanup by freeing the allocated memory for 'clk_node' before returning.

---
 drivers/clk/imx/clk-scu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/clk/imx/clk-scu.c +3 -1
index be89180dd19c..e48a904c0013 100644
@@ -886,8 +886,10 @@ struct clk_hw *__imx_clk_gpr_scu(const char *name, const char * const *parent_na
 		return ERR_PTR(-EINVAL);
 	}
 
-	if (!imx_clk_is_resource_owned(rsrc_id))
+	if (!imx_clk_is_resource_owned(rsrc_id)) {
+		kfree(clk_node);
 		return NULL;
+	}
 
 	clk = kzalloc(sizeof(*clk), GFP_KERNEL);
 	if (!clk) {
Signature · 1 line
2.25.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines

Subject: [PATCH] clk: imx: scu: Fix memory leak in __imx_clk_gpr_scu()

In cases where imx_clk_is_resource_owned() returns false, the code path does not handle the failure gracefully, potentially leading to a memory leak. This fix ensures proper cleanup by freeing the allocated memory for 'clk_node' before returning.

Applied
#2 lore raw
In reply to Kuan-Wei Chiu
Quoted 6 lines · Kuan-Wei Chiu

In cases where imx_clk_is_resource_owned() returns false, the code path does not handle the failure gracefully, potentially leading to a memory leak. This fix ensures proper cleanup by freeing the allocated memory for 'clk_node' before returning.

Applied, thanks!

[1/1] clk: imx: scu: Fix memory leak in __imx_clk_gpr_scu()

      commit: 986439a9b3a32a6ac20042ec17acd443f1e7e86a

Best regards,

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

PATCH 1/1 clk: imx: scu: Use common error handling code in __imx_clk_gpr_scu()

In reply to Kuan-Wei Chiu

From: Markus Elfring <[email protected]>
Date: Fri, 22 Dec 2023 10:05:32 +0100

Use another label so that a bit of exception handling can be better reused at the end of this function.

---
 drivers/clk/imx/clk-scu.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/clk/imx/clk-scu.c +7 -6
index e48a904c0013..4ca9dccf3d3b 100644
@@ -882,19 +882,19 @@ struct clk_hw *__imx_clk_gpr_scu(const char *name, const char * const *parent_na
 		return ERR_PTR(-ENOMEM);
 
 	if (!imx_scu_clk_is_valid(rsrc_id)) {
-		kfree(clk_node);
-		return ERR_PTR(-EINVAL);
+		ret = -EINVAL;
+		goto free_clk_node;
 	}
 
 	if (!imx_clk_is_resource_owned(rsrc_id)) {
-		kfree(clk_node);
-		return NULL;
+		ret = 0;
+		goto free_clk_node;
 	}
 
 	clk = kzalloc(sizeof(*clk), GFP_KERNEL);
 	if (!clk) {
-		kfree(clk_node);
-		return ERR_PTR(-ENOMEM);
+		ret = -ENOMEM;
+		goto free_clk_node;
 	}
 
 	clk->rsrc_id = rsrc_id;
@@ -922,6 +922,7 @@ struct clk_hw *__imx_clk_gpr_scu(const char *name, const char * const *parent_na
 	ret = clk_hw_register(NULL, hw);
 	if (ret) {
 		kfree(clk);
+free_clk_node:
 		kfree(clk_node);
 		hw = ERR_PTR(ret);
 	} else {

--
2.43.0

#4 lore raw

Re: [PATCH] clk: imx: scu: Use common error handling code in __imx_clk_gpr_scu()

Quoted 5 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Fri, 22 Dec 2023 10:05:32 +0100

Use another label so that a bit of exception handling can be better reused at the end of this function.

Please don't send patches as reply to other(s) patches.

Quoted 47 lines
---
 drivers/clk/imx/clk-scu.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/clk/imx/clk-scu.c +7 -7
index e48a904c0013..4ca9dccf3d3b 100644
@@ -882,19 +882,19 @@ struct clk_hw *__imx_clk_gpr_scu(const char *name, const char * const *parent_na
 		return ERR_PTR(-ENOMEM);
 
 	if (!imx_scu_clk_is_valid(rsrc_id)) {
-		kfree(clk_node);
-		return ERR_PTR(-EINVAL);
+		ret = -EINVAL;
+		goto free_clk_node;
 	}
 
 	if (!imx_clk_is_resource_owned(rsrc_id)) {
-		kfree(clk_node);
-		return NULL;
+		ret = 0;
+		goto free_clk_node;
 	}
 
 	clk = kzalloc(sizeof(*clk), GFP_KERNEL);
 	if (!clk) {
-		kfree(clk_node);
-		return ERR_PTR(-ENOMEM);
+		ret = -ENOMEM;
+		goto free_clk_node;
 	}
 
 	clk->rsrc_id = rsrc_id;
@@ -922,6 +922,7 @@ struct clk_hw *__imx_clk_gpr_scu(const char *name, const char * const *parent_na
 	ret = clk_hw_register(NULL, hw);
 	if (ret) {
 		kfree(clk);
+free_clk_node:
 		kfree(clk_node);
 		hw = ERR_PTR(ret);
 	} else {
--

2.43.0

#5 lore raw

Re: clk: imx: scu: Use common error handling code in __imx_clk_gpr_scu()

Quoted 4 lines
Quoted 2 lines

Use another label so that a bit of exception handling can be better reused at the end of this function.

Please don't send patches as reply to other(s) patches.

This is a general possibility to connect an information sources with a corresponding change idea.
Will the acceptance grow for the presented source code transformation?

Regards,
Markus

#6 lore raw

Re: clk: imx: scu: Use common error handling code in __imx_clk_gpr_scu()

Quoted 9 lines · Markus Elfring
Quoted 4 lines
Quoted 2 lines

Use another label so that a bit of exception handling can be better reused at the end of this function.

Please don't send patches as reply to other(s) patches.

This is a general possibility to connect an information sources with a corresponding change idea.
Will the acceptance grow for the presented source code transformation?

Nope, please don't do that. The b4 tool will pick up the old patch if you do this.

Quoted 2 lines

Regards,
Markus

#7 lore raw

Re: clk: imx: scu: Use common error handling code in __imx_clk_gpr_scu()

Quoted 11 lines
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Use another label so that a bit of exception handling can be better reused at the end of this function.

Please don't send patches as reply to other(s) patches.

This is a general possibility to connect an information sources with a corresponding change idea.
Will the acceptance grow for the presented source code transformation?

Nope, please don't do that.

Do you find the proposed source code transformation reasonable (in principle)?

Quoted 1 line

The b4 tool will pick up the old patch if you do this.

Are you looking for further improvements for this development tool?

Regards,
Markus