2022-04-13 08:58:53

by baihaowen

[permalink] [raw]
Subject: [PATCH V2] clk: renesas: Fix memory leak of 'cpg'

Fix this issue by freeing the cpg when exiting the function in the
error/normal path.

Signed-off-by: Haowen Bai <[email protected]>
---
V1->V2: free both cpg&clks.

drivers/clk/renesas/clk-r8a73a4.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/renesas/clk-r8a73a4.c b/drivers/clk/renesas/clk-r8a73a4.c
index cfed11c659d9..5a8d976f49e0 100644
--- a/drivers/clk/renesas/clk-r8a73a4.c
+++ b/drivers/clk/renesas/clk-r8a73a4.c
@@ -215,7 +215,7 @@ static void __init r8a73a4_cpg_clocks_init(struct device_node *np)

cpg->reg = of_iomap(np, 0);
if (WARN_ON(cpg->reg == NULL))
- return;
+ goto out_free_cpg;

for (i = 0; i < num_clks; ++i) {
const char *name;
@@ -233,6 +233,9 @@ static void __init r8a73a4_cpg_clocks_init(struct device_node *np)
}

of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
+out_free_cpg:
+ kfree(cpg);
+ kfree(clks);
}
CLK_OF_DECLARE(r8a73a4_cpg_clks, "renesas,r8a73a4-cpg-clocks",
r8a73a4_cpg_clocks_init);
--
2.7.4


2022-04-13 10:10:47

by Geert Uytterhoeven

[permalink] [raw]
Subject: Re: [PATCH V2] clk: renesas: Fix memory leak of 'cpg'

Hi Haowen,

On Wed, Apr 13, 2022 at 10:30 AM Haowen Bai <[email protected]> wrote:
> Fix this issue by freeing the cpg when exiting the function in the
> error/normal path.
>
> Signed-off-by: Haowen Bai <[email protected]>

Thanks for your patch!

> --- a/drivers/clk/renesas/clk-r8a73a4.c
> +++ b/drivers/clk/renesas/clk-r8a73a4.c
> @@ -215,7 +215,7 @@ static void __init r8a73a4_cpg_clocks_init(struct device_node *np)
>
> cpg->reg = of_iomap(np, 0);
> if (WARN_ON(cpg->reg == NULL))
> - return;
> + goto out_free_cpg;

Note that this is a fatal error, i.e. no chance the system will survive this,
so cleaning up is moot.

>
> for (i = 0; i < num_clks; ++i) {
> const char *name;
> @@ -233,6 +233,9 @@ static void __init r8a73a4_cpg_clocks_init(struct device_node *np)
> }
>
> of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
> +out_free_cpg:
> + kfree(cpg);
> + kfree(clks);

Both cpg and clks are still used after returning from this function,
through the registered clocks and clock provider.

> }
> CLK_OF_DECLARE(r8a73a4_cpg_clks, "renesas,r8a73a4-cpg-clocks",
> r8a73a4_cpg_clocks_init);

NAKed-by: Geert Uytterhoeven <[email protected]>

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

2022-04-13 11:57:41

by baihaowen

[permalink] [raw]
Subject: Re: [PATCH V2] clk: renesas: Fix memory leak of 'cpg'

在 4/13/22 4:41 PM, Geert Uytterhoeven 写道:
> Hi Haowen,
>
> On Wed, Apr 13, 2022 at 10:30 AM Haowen Bai <[email protected]> wrote:
>> Fix this issue by freeing the cpg when exiting the function in the
>> error/normal path.
>>
>> Signed-off-by: Haowen Bai <[email protected]>
> Thanks for your patch!
>
>> --- a/drivers/clk/renesas/clk-r8a73a4.c
>> +++ b/drivers/clk/renesas/clk-r8a73a4.c
>> @@ -215,7 +215,7 @@ static void __init r8a73a4_cpg_clocks_init(struct device_node *np)
>>
>> cpg->reg = of_iomap(np, 0);
>> if (WARN_ON(cpg->reg == NULL))
>> - return;
>> + goto out_free_cpg;
> Note that this is a fatal error, i.e. no chance the system will survive this,
> so cleaning up is moot.
>
>> for (i = 0; i < num_clks; ++i) {
>> const char *name;
>> @@ -233,6 +233,9 @@ static void __init r8a73a4_cpg_clocks_init(struct device_node *np)
>> }
>>
>> of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
>> +out_free_cpg:
>> + kfree(cpg);
>> + kfree(clks);
> Both cpg and clks are still used after returning from this function,
> through the registered clocks and clock provider.
>
>> }
>> CLK_OF_DECLARE(r8a73a4_cpg_clks, "renesas,r8a73a4-cpg-clocks",
>> r8a73a4_cpg_clocks_init);
> NAKed-by: Geert Uytterhoeven <[email protected]>
>
> Gr{oetje,eeting}s,
>
> Geert
>
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
>
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
> -- Linus Torvalds
Dear Geert Uytterhoeven
Could you show me how and when cpg & clks free ?

--
Haowen Bai

2022-04-13 20:49:29

by Geert Uytterhoeven

[permalink] [raw]
Subject: Re: [PATCH V2] clk: renesas: Fix memory leak of 'cpg'

Hi Haowen,

On Wed, Apr 13, 2022 at 11:24 AM baihaowen <[email protected]> wrote:
> 在 4/13/22 4:41 PM, Geert Uytterhoeven 写道:
> > On Wed, Apr 13, 2022 at 10:30 AM Haowen Bai <[email protected]> wrote:
> >> Fix this issue by freeing the cpg when exiting the function in the
> >> error/normal path.
> >>
> >> Signed-off-by: Haowen Bai <[email protected]>
> > Thanks for your patch!
> >
> >> --- a/drivers/clk/renesas/clk-r8a73a4.c
> >> +++ b/drivers/clk/renesas/clk-r8a73a4.c
> >> @@ -215,7 +215,7 @@ static void __init r8a73a4_cpg_clocks_init(struct device_node *np)
> >>
> >> cpg->reg = of_iomap(np, 0);
> >> if (WARN_ON(cpg->reg == NULL))
> >> - return;
> >> + goto out_free_cpg;
> > Note that this is a fatal error, i.e. no chance the system will survive this,
> > so cleaning up is moot.
> >
> >> for (i = 0; i < num_clks; ++i) {
> >> const char *name;
> >> @@ -233,6 +233,9 @@ static void __init r8a73a4_cpg_clocks_init(struct device_node *np)
> >> }
> >>
> >> of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
> >> +out_free_cpg:
> >> + kfree(cpg);
> >> + kfree(clks);
> > Both cpg and clks are still used after returning from this function,
> > through the registered clocks and clock provider.
> >
> >> }
> >> CLK_OF_DECLARE(r8a73a4_cpg_clks, "renesas,r8a73a4-cpg-clocks",
> >> r8a73a4_cpg_clocks_init);
> > NAKed-by: Geert Uytterhoeven <[email protected]>

> Could you show me how and when cpg & clks free ?

They are never freed, as they stay in-use for the lifetime of the system.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

2022-04-15 15:30:52

by Sergey Shtylyov

[permalink] [raw]
Subject: Re: [PATCH V2] clk: renesas: Fix memory leak of 'cpg'

Hello!

On 4/13/22 11:19 AM, Haowen Bai wrote:

> Fix this issue by freeing the cpg when exiting the function in the
> error/normal path.
>
> Signed-off-by: Haowen Bai <[email protected]>
> ---
> V1->V2: free both cpg&clks.
>
> drivers/clk/renesas/clk-r8a73a4.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)

2 patches with the same name won't do -- you always need to include the chip name
part of the file name in the subject (in this case r8a73a4).

MBR, Sergey