linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] clk: imx: scu: Fix memory leak in __imx_clk_gpr_scu()
@ 2023-12-10 17:19 Kuan-Wei Chiu
  2023-12-12  0:40 ` Peng Fan
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Kuan-Wei Chiu @ 2023-12-10 17:19 UTC (permalink / raw)
  To: abelvesa
  Cc: peng.fan, mturquette, sboyd, shawnguo, s.hauer, kernel, festevam,
	linux-imx, linux-clk, linux-arm-kernel, linux-kernel,
	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.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
 drivers/clk/imx/clk-scu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/imx/clk-scu.c b/drivers/clk/imx/clk-scu.c
index be89180dd19c..e48a904c0013 100644
--- a/drivers/clk/imx/clk-scu.c
+++ b/drivers/clk/imx/clk-scu.c
@@ -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) {
-- 
2.25.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* RE: [PATCH] clk: imx: scu: Fix memory leak in __imx_clk_gpr_scu()
  2023-12-10 17:19 [PATCH] clk: imx: scu: Fix memory leak in __imx_clk_gpr_scu() Kuan-Wei Chiu
@ 2023-12-12  0:40 ` Peng Fan
  2023-12-20  9:25 ` Abel Vesa
  2023-12-22  9:23 ` [PATCH] clk: imx: scu: Use common error handling code " Markus Elfring
  2 siblings, 0 replies; 8+ messages in thread
From: Peng Fan @ 2023-12-12  0:40 UTC (permalink / raw)
  To: Kuan-Wei Chiu, abelvesa@kernel.org
  Cc: mturquette@baylibre.com, sboyd@kernel.org, shawnguo@kernel.org,
	s.hauer@pengutronix.de, kernel@pengutronix.de, festevam@gmail.com,
	dl-linux-imx, linux-clk@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org

> 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.
> 
> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>

Reviewed-by: Peng Fan <peng.fan@nxp.com>

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] clk: imx: scu: Fix memory leak in __imx_clk_gpr_scu()
  2023-12-10 17:19 [PATCH] clk: imx: scu: Fix memory leak in __imx_clk_gpr_scu() Kuan-Wei Chiu
  2023-12-12  0:40 ` Peng Fan
@ 2023-12-20  9:25 ` Abel Vesa
  2023-12-22  9:23 ` [PATCH] clk: imx: scu: Use common error handling code " Markus Elfring
  2 siblings, 0 replies; 8+ messages in thread
From: Abel Vesa @ 2023-12-20  9:25 UTC (permalink / raw)
  To: abelvesa, Kuan-Wei Chiu
  Cc: peng.fan, mturquette, sboyd, shawnguo, s.hauer, kernel, festevam,
	linux-imx, linux-clk, linux-arm-kernel, linux-kernel


On Mon, 11 Dec 2023 01:19:07 +0800, Kuan-Wei Chiu wrote:
> 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,
-- 
Abel Vesa <abel.vesa@linaro.org>

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] clk: imx: scu: Use common error handling code in __imx_clk_gpr_scu()
  2023-12-10 17:19 [PATCH] clk: imx: scu: Fix memory leak in __imx_clk_gpr_scu() Kuan-Wei Chiu
  2023-12-12  0:40 ` Peng Fan
  2023-12-20  9:25 ` Abel Vesa
@ 2023-12-22  9:23 ` Markus Elfring
  2024-02-02 18:45   ` Abel Vesa
  2 siblings, 1 reply; 8+ messages in thread
From: Markus Elfring @ 2023-12-22  9:23 UTC (permalink / raw)
  To: Kuan-Wei Chiu, Abel Vesa, Fabio Estevam, Michael Turquette,
	Peng Fan, Sascha Hauer, Shawn Guo, Stephen Boyd, kernel,
	linux-imx, linux-clk, linux-arm-kernel
  Cc: LKML, cocci

From: Markus Elfring <elfring@users.sourceforge.net>
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.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/clk/imx/clk-scu.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/clk/imx/clk-scu.c b/drivers/clk/imx/clk-scu.c
index e48a904c0013..4ca9dccf3d3b 100644
--- a/drivers/clk/imx/clk-scu.c
+++ b/drivers/clk/imx/clk-scu.c
@@ -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


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] clk: imx: scu: Use common error handling code in __imx_clk_gpr_scu()
  2023-12-22  9:23 ` [PATCH] clk: imx: scu: Use common error handling code " Markus Elfring
@ 2024-02-02 18:45   ` Abel Vesa
  2024-02-02 20:21     ` Markus Elfring
  0 siblings, 1 reply; 8+ messages in thread
From: Abel Vesa @ 2024-02-02 18:45 UTC (permalink / raw)
  To: Markus Elfring
  Cc: Kuan-Wei Chiu, Abel Vesa, Fabio Estevam, Michael Turquette,
	Peng Fan, Sascha Hauer, Shawn Guo, Stephen Boyd, kernel,
	linux-imx, linux-clk, linux-arm-kernel, LKML, cocci

On 23-12-22 10:23:13, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> 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.

> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  drivers/clk/imx/clk-scu.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/clk/imx/clk-scu.c b/drivers/clk/imx/clk-scu.c
> index e48a904c0013..4ca9dccf3d3b 100644
> --- a/drivers/clk/imx/clk-scu.c
> +++ b/drivers/clk/imx/clk-scu.c
> @@ -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
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: clk: imx: scu: Use common error handling code in __imx_clk_gpr_scu()
  2024-02-02 18:45   ` Abel Vesa
@ 2024-02-02 20:21     ` Markus Elfring
  2024-02-26  9:08       ` Abel Vesa
  0 siblings, 1 reply; 8+ messages in thread
From: Markus Elfring @ 2024-02-02 20:21 UTC (permalink / raw)
  To: Abel Vesa, kernel, linux-imx, linux-clk, linux-arm-kernel,
	kernel-janitors
  Cc: Kuan-Wei Chiu, Abel Vesa, Fabio Estevam, Michael Turquette,
	Peng Fan, Sascha Hauer, Shawn Guo, Stephen Boyd, LKML, cocci

>> 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

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: clk: imx: scu: Use common error handling code in __imx_clk_gpr_scu()
  2024-02-02 20:21     ` Markus Elfring
@ 2024-02-26  9:08       ` Abel Vesa
  2024-02-26 14:35         ` Markus Elfring
  0 siblings, 1 reply; 8+ messages in thread
From: Abel Vesa @ 2024-02-26  9:08 UTC (permalink / raw)
  To: Markus Elfring
  Cc: kernel, linux-imx, linux-clk, linux-arm-kernel, kernel-janitors,
	Kuan-Wei Chiu, Abel Vesa, Fabio Estevam, Michael Turquette,
	Peng Fan, Sascha Hauer, Shawn Guo, Stephen Boyd, LKML, cocci

On 24-02-02 21:21:19, Markus Elfring wrote:
> >> 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.

> Regards,
> Markus

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: clk: imx: scu: Use common error handling code in __imx_clk_gpr_scu()
  2024-02-26  9:08       ` Abel Vesa
@ 2024-02-26 14:35         ` Markus Elfring
  0 siblings, 0 replies; 8+ messages in thread
From: Markus Elfring @ 2024-02-26 14:35 UTC (permalink / raw)
  To: Abel Vesa, kernel, linux-imx, linux-clk, linux-arm-kernel,
	kernel-janitors
  Cc: Kuan-Wei Chiu, Abel Vesa, Fabio Estevam, Michael Turquette,
	Peng Fan, Sascha Hauer, Shawn Guo, Stephen Boyd, LKML, cocci

>>>> 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)?


> 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

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-02-26 14:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-10 17:19 [PATCH] clk: imx: scu: Fix memory leak in __imx_clk_gpr_scu() Kuan-Wei Chiu
2023-12-12  0:40 ` Peng Fan
2023-12-20  9:25 ` Abel Vesa
2023-12-22  9:23 ` [PATCH] clk: imx: scu: Use common error handling code " Markus Elfring
2024-02-02 18:45   ` Abel Vesa
2024-02-02 20:21     ` Markus Elfring
2024-02-26  9:08       ` Abel Vesa
2024-02-26 14:35         ` Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).