2020-08-23 08:27:46

by Dinghao Liu

[permalink] [raw]
Subject: [PATCH] rtc: sun6i: Fix memleak in sun6i_rtc_clk_init

When clk_hw_register_fixed_rate_with_accuracy() fails,
clk_data should be freed. It's the same for the subsequent
error paths.

Signed-off-by: Dinghao Liu <[email protected]>
---
drivers/rtc/rtc-sun6i.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/rtc/rtc-sun6i.c b/drivers/rtc/rtc-sun6i.c
index e2b8b150bcb4..a837c5a40508 100644
--- a/drivers/rtc/rtc-sun6i.c
+++ b/drivers/rtc/rtc-sun6i.c
@@ -272,7 +272,7 @@ static void __init sun6i_rtc_clk_init(struct device_node *node,
300000000);
if (IS_ERR(rtc->int_osc)) {
pr_crit("Couldn't register the internal oscillator\n");
- return;
+ goto err;
}

parents[0] = clk_hw_get_name(rtc->int_osc);
@@ -290,7 +290,7 @@ static void __init sun6i_rtc_clk_init(struct device_node *node,
rtc->losc = clk_register(NULL, &rtc->hw);
if (IS_ERR(rtc->losc)) {
pr_crit("Couldn't register the LOSC clock\n");
- return;
+ goto err;
}

of_property_read_string_index(node, "clock-output-names", 1,
@@ -301,7 +301,7 @@ static void __init sun6i_rtc_clk_init(struct device_node *node,
&rtc->lock);
if (IS_ERR(rtc->ext_losc)) {
pr_crit("Couldn't register the LOSC external gate\n");
- return;
+ goto err;
}

clk_data->num = 2;
--
2.17.1


2020-08-25 14:52:58

by Chen-Yu Tsai

[permalink] [raw]
Subject: Re: [PATCH] rtc: sun6i: Fix memleak in sun6i_rtc_clk_init

On Sun, Aug 23, 2020 at 3:59 PM Dinghao Liu <[email protected]> wrote:
>
> When clk_hw_register_fixed_rate_with_accuracy() fails,
> clk_data should be freed. It's the same for the subsequent
> error paths.

I suppose you should also unregister the already registered clocks
in the latter two error paths?

> Signed-off-by: Dinghao Liu <[email protected]>
> ---
> drivers/rtc/rtc-sun6i.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/rtc/rtc-sun6i.c b/drivers/rtc/rtc-sun6i.c
> index e2b8b150bcb4..a837c5a40508 100644
> --- a/drivers/rtc/rtc-sun6i.c
> +++ b/drivers/rtc/rtc-sun6i.c
> @@ -272,7 +272,7 @@ static void __init sun6i_rtc_clk_init(struct device_node *node,
> 300000000);
> if (IS_ERR(rtc->int_osc)) {
> pr_crit("Couldn't register the internal oscillator\n");
> - return;
> + goto err;
> }
>
> parents[0] = clk_hw_get_name(rtc->int_osc);
> @@ -290,7 +290,7 @@ static void __init sun6i_rtc_clk_init(struct device_node *node,
> rtc->losc = clk_register(NULL, &rtc->hw);
> if (IS_ERR(rtc->losc)) {
> pr_crit("Couldn't register the LOSC clock\n");
> - return;
> + goto err;
> }
>
> of_property_read_string_index(node, "clock-output-names", 1,
> @@ -301,7 +301,7 @@ static void __init sun6i_rtc_clk_init(struct device_node *node,
> &rtc->lock);
> if (IS_ERR(rtc->ext_losc)) {
> pr_crit("Couldn't register the LOSC external gate\n");
> - return;
> + goto err;
> }
>
> clk_data->num = 2;
> --
> 2.17.1
>
>
> _______________________________________________
> linux-arm-kernel mailing list
> [email protected]
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

2020-08-26 14:10:53

by Dinghao Liu

[permalink] [raw]
Subject: Re: Re: [PATCH] rtc: sun6i: Fix memleak in sun6i_rtc_clk_init

> On Sun, Aug 23, 2020 at 3:59 PM Dinghao Liu <[email protected]> wrote:
> >
> > When clk_hw_register_fixed_rate_with_accuracy() fails,
> > clk_data should be freed. It's the same for the subsequent
> > error paths.
>
> I suppose you should also unregister the already registered clocks
> in the latter two error paths?
>

Sounds reasonable. But I find that the existing kernel code takes different
strategies for this case. of_sama5d4_sckc_setup() uses clk_hw_unregister()
after clk_hw_register_fixed_rate_with_accuracy(), while _of_fixed_clk_setup()
uses clk_hw_unregister_fixed_rate(). But at91sam926x_pmc_setup() just does
nothing in this case.

Also, tcon_ch1_setup() uses clk_unregister() after clk_register(), while
clk_register_vco_pll() just does nothing.

So I'm not sure if we should register here and which unregister function to
use. Would you please give me more specific advice about this problem?

Regards,
Dinghao

2020-10-10 04:13:37

by Alexandre Belloni

[permalink] [raw]
Subject: Re: Re: [PATCH] rtc: sun6i: Fix memleak in sun6i_rtc_clk_init

On 26/08/2020 16:55:14+0800, [email protected] wrote:
> > On Sun, Aug 23, 2020 at 3:59 PM Dinghao Liu <[email protected]> wrote:
> > >
> > > When clk_hw_register_fixed_rate_with_accuracy() fails,
> > > clk_data should be freed. It's the same for the subsequent
> > > error paths.
> >
> > I suppose you should also unregister the already registered clocks
> > in the latter two error paths?
> >
>
> Sounds reasonable. But I find that the existing kernel code takes different
> strategies for this case. of_sama5d4_sckc_setup() uses clk_hw_unregister()
> after clk_hw_register_fixed_rate_with_accuracy(), while _of_fixed_clk_setup()
> uses clk_hw_unregister_fixed_rate(). But at91sam926x_pmc_setup() just does
> nothing in this case.

I guess you should use clk_hw_unregister_fixed_rate after
clk_hw_register_fixed_rate_with_accuracy. clk_hw_unregister will leak
the struct clk_fixed_rate. It doesn't matter too much for
of_sama5d4_sckc_setup and at91sam926x_pmc_setup because if th clock
can't be registered, the platform will not boot.

>
> Also, tcon_ch1_setup() uses clk_unregister() after clk_register(), while
> clk_register_vco_pll() just does nothing.
>
> So I'm not sure if we should register here and which unregister function to
> use. Would you please give me more specific advice about this problem?
>
> Regards,
> Dinghao

--
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

2020-10-18 08:54:38

by Dinghao Liu

[permalink] [raw]
Subject: Re: Re: Re: [PATCH] rtc: sun6i: Fix memleak in sun6i_rtc_clk_init

> On 26/08/2020 16:55:14+0800, [email protected] wrote:
> > > On Sun, Aug 23, 2020 at 3:59 PM Dinghao Liu <[email protected]> wrote:
> > > >
> > > > When clk_hw_register_fixed_rate_with_accuracy() fails,
> > > > clk_data should be freed. It's the same for the subsequent
> > > > error paths.
> > >
> > > I suppose you should also unregister the already registered clocks
> > > in the latter two error paths?
> > >
> >
> > Sounds reasonable. But I find that the existing kernel code takes different
> > strategies for this case. of_sama5d4_sckc_setup() uses clk_hw_unregister()
> > after clk_hw_register_fixed_rate_with_accuracy(), while _of_fixed_clk_setup()
> > uses clk_hw_unregister_fixed_rate(). But at91sam926x_pmc_setup() just does
> > nothing in this case.
>
> I guess you should use clk_hw_unregister_fixed_rate after
> clk_hw_register_fixed_rate_with_accuracy. clk_hw_unregister will leak
> the struct clk_fixed_rate. It doesn't matter too much for
> of_sama5d4_sckc_setup and at91sam926x_pmc_setup because if th clock
> can't be registered, the platform will not boot.

Thank you for your advice! I will submit a new patch soon.

Regards,
Dinghao