public inbox for linux-clk@vger.kernel.org
 help / color / mirror / Atom feed
From: Jerome Brunet <jbrunet@baylibre.com>
To: Martin Blumenstingl <martin.blumenstingl@googlemail.com>,
	Bernard Zhao <bernard@vivo.com>,
	Neil Armstrong <narmstrong@baylibre.com>,
	Stephen Boyd <sboyd@kernel.org>,
	Kevin Hilman <khilman@baylibre.com>,
	linux-amlogic@lists.infradead.org, linux-clk@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com
Subject: Re: [PATCH] clk/meson: fixes memleak issue in init err branch
Date: Wed, 29 Apr 2020 14:37:09 +0200	[thread overview]
Message-ID: <1jpnbqii2y.fsf@starbuckisacylon.baylibre.com> (raw)
In-Reply-To: <20200429031416.3900-1-bernard@vivo.com>


On Wed 29 Apr 2020 at 05:14, Bernard Zhao <bernard@vivo.com> wrote:

> In common init function, when run into err branch, we didn`t
> use kfree to release kzmalloc area, this may bring in memleak

Thx for reporting this Bernard.
I'm not a fan of adding kfree everywhere. I'd much prefer a label and
clear error exit path.

That being said, the allocation is probably not the only thing that
needs to be undone in case of error. I guess this is due to conversion
to CLK_OF_DECLARE_DRIVER() which forced to drop all the devm_
This was done because the clock controller was required early in the
boot sequence.

There is 2 paths to properly solve this:
1) Old school: manually undo everything with every error exit condition
   Doable but probably a bit messy
2) Convert back the driver to a real platform driver and use devm_.
   We would still need the controller to register early but I wonder if
   we could use the same method as drivers/clk/mediatek/clk-mt2701.c and
   use arch_initcall() ?

Martin, you did the initial conversion, what do you think of option 2 ?
Would it still answer the problem you were trying to solve back then ?

One added benefit of option 2 is we could drop CLK_OF_DECLARE_DRIVER().
We could even do the same in for the other SoCs, which I suppose would
avoid a fair amount of probe deferral.

>
> Signed-off-by: Bernard Zhao <bernard@vivo.com>
> ---
>  drivers/clk/meson/meson8b.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/clk/meson/meson8b.c b/drivers/clk/meson/meson8b.c
> index 34a70c4b4899..0f07d5a4cd16 100644
> --- a/drivers/clk/meson/meson8b.c
> +++ b/drivers/clk/meson/meson8b.c
> @@ -3687,6 +3687,7 @@ static void __init meson8b_clkc_init_common(struct device_node *np,
>  	if (ret) {
>  		pr_err("%s: Failed to register clkc reset controller: %d\n",
>  		       __func__, ret);
> +		kfree(rstc);
>  		return;
>  	}
>  
> @@ -3710,8 +3711,10 @@ static void __init meson8b_clkc_init_common(struct device_node *np,
>  			continue;
>  
>  		ret = of_clk_hw_register(np, clk_hw_onecell_data->hws[i]);
> -		if (ret)
> +		if (ret) {
> +			kfree(rstc);
>  			return;
> +		}
>  	}
>  
>  	meson8b_cpu_nb_data.cpu_clk = clk_hw_onecell_data->hws[CLKID_CPUCLK];
> @@ -3727,13 +3730,16 @@ static void __init meson8b_clkc_init_common(struct device_node *np,
>  	if (ret) {
>  		pr_err("%s: failed to register the CPU clock notifier\n",
>  		       __func__);
> +		kfree(rstc);
>  		return;
>  	}
>  
>  	ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get,
>  				     clk_hw_onecell_data);
> -	if (ret)
> +	if (ret) {
>  		pr_err("%s: failed to register clock provider\n", __func__);
> +		kfree(rstc);
> +	}
>  }
>  
>  static void __init meson8_clkc_init(struct device_node *np)


  reply	other threads:[~2020-04-29 12:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-29  3:14 [PATCH] clk/meson: fixes memleak issue in init err branch Bernard Zhao
2020-04-29 12:37 ` Jerome Brunet [this message]
2020-04-29 17:43   ` Martin Blumenstingl
2020-04-30  3:33     ` 赵军奎
2020-05-02 11:10       ` Martin Blumenstingl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1jpnbqii2y.fsf@starbuckisacylon.baylibre.com \
    --to=jbrunet@baylibre.com \
    --cc=bernard@vivo.com \
    --cc=khilman@baylibre.com \
    --cc=linux-amlogic@lists.infradead.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.blumenstingl@googlemail.com \
    --cc=narmstrong@baylibre.com \
    --cc=opensource.kernel@vivo.com \
    --cc=sboyd@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox