2023-12-10 17:19:28

by Kuan-Wei Chiu

[permalink] [raw]
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 <[email protected]>
---
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


2023-12-12 00:40:39

by Peng Fan

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

> 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 <[email protected]>

Reviewed-by: Peng Fan <[email protected]>

2023-12-20 09:25:44

by Abel Vesa

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


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 <[email protected]>

2023-12-22 09:24:17

by Markus Elfring

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

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.

Signed-off-by: Markus Elfring <[email protected]>
---
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


2024-02-02 19:10:55

by Abel Vesa

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

On 23-12-22 10:23:13, Markus Elfring wrote:
> 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.

>
> Signed-off-by: Markus Elfring <[email protected]>
> ---
> 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
>

2024-02-02 20:23:27

by Markus Elfring

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

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

2024-02-26 09:44:40

by Abel Vesa

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

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

2024-02-26 14:59:51

by Markus Elfring

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

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