public inbox for linux-pwm@vger.kernel.org
 help / color / mirror / Atom feed
From: Lee Jones <lee.jones@linaro.org>
To: "Uwe Kleine-König" <uwe@kleine-koenig.org>
Cc: Ajit Pal Singh <ajitpal.singh@st.com>,
	Thierry Reding <thierry.reding@gmail.com>,
	linux-pwm@vger.kernel.org
Subject: Re: [PATCH] pwm: sti: fix error handling
Date: Fri, 6 Nov 2020 08:29:14 +0000	[thread overview]
Message-ID: <20201106082914.GX4488@dell> (raw)
In-Reply-To: <20201013081531.661528-1-uwe@kleine-koenig.org>

On Tue, 13 Oct 2020, Uwe Kleine-König wrote:

> This commit fixes several faults:
> 
>  - Iff a clk was returned by of_clk_get_by_name() it must be dereferenced
>    by calling clk_put().
>  - A clk that was prepared must be unprepared.
>  - The .remove callback isn't supposed to call pwm_disable().
>  - The necessary resources needed by the PWM must not be freed before
>    pwmchip_remove() was called.
> 
> Fixes: 378fe115d19d ("pwm: sti: Add new driver for ST's PWM IP")
> Signed-off-by: Uwe Kleine-König <uwe@kleine-koenig.org>
> ---
>  drivers/pwm/pwm-sti.c | 49 ++++++++++++++++++++++++++++++++-----------
>  1 file changed, 37 insertions(+), 12 deletions(-)

Sorry for the delay, this ended up in spam.

> diff --git a/drivers/pwm/pwm-sti.c b/drivers/pwm/pwm-sti.c
> index 1508616d794c..f89f8cbdfdfc 100644
> --- a/drivers/pwm/pwm-sti.c
> +++ b/drivers/pwm/pwm-sti.c
> @@ -605,7 +605,7 @@ static int sti_pwm_probe(struct platform_device *pdev)
>  	ret = clk_prepare(pc->pwm_clk);
>  	if (ret) {
>  		dev_err(dev, "failed to prepare clock\n");
> -		return ret;
> +		goto err_pwm_clk_prepare;

I would prefer these to indicate the intention, rather than were they
were called from.  So err_put_cpt_clk for this one, etc.

>  	}
>  
>  skip_pwm:
> @@ -615,13 +615,14 @@ static int sti_pwm_probe(struct platform_device *pdev)
>  	pc->cpt_clk = of_clk_get_by_name(dev->of_node, "capture");
>  	if (IS_ERR(pc->cpt_clk)) {
>  		dev_err(dev, "failed to get PWM capture clock\n");
> -		return PTR_ERR(pc->cpt_clk);
> +		ret = PTR_ERR(pc->cpt_clk);
> +		goto err_cpt_clk_get;

err_unprepare_pwm_clk, etc.

>  	}
>  
>  	ret = clk_prepare(pc->cpt_clk);
>  	if (ret) {
>  		dev_err(dev, "failed to prepare clock\n");
> -		return ret;
> +		goto err_cpt_clk_prepare;
>  	}
>  
>  skip_cpt:
> @@ -632,9 +633,7 @@ static int sti_pwm_probe(struct platform_device *pdev)
>  
>  	ret = pwmchip_add(&pc->chip);
>  	if (ret < 0) {
> -		clk_unprepare(pc->pwm_clk);
> -		clk_unprepare(pc->cpt_clk);
> -		return ret;
> +		goto err_pwmchip_add;
>  	}
>  
>  	for (i = 0; i < cdata->cpt_num_devs; i++) {
> @@ -653,20 +652,46 @@ static int sti_pwm_probe(struct platform_device *pdev)
>  	platform_set_drvdata(pdev, pc);
>  
>  	return 0;
> +
> +err_pwmchip_add:
> +
> +	if (cdata->cpt_num_devs) {

No need for the evaluations, clk_*() calls are NULL safe.

> +		clk_unprepare(pc->cpt_clk);
> +err_cpt_clk_prepare:
> +
> +		clk_put(pc->cpt_clk);
> +	}
> +err_cpt_clk_get:
> +
> +	if (cdata->pwm_num_devs) {
> +		clk_unprepare(pc->pwm_clk);
> +err_pwm_clk_prepare:
> +
> +		clk_put(pc->pwm_clk);
> +	}
> +	return ret;
>  }
>  
>  static int sti_pwm_remove(struct platform_device *pdev)
>  {
>  	struct sti_pwm_chip *pc = platform_get_drvdata(pdev);
> -	unsigned int i;
> +	int ret;
> +
> +	ret = pwmchip_remove(&pc->chip);
> +	if (ret)
> +		return ret;
>  
> -	for (i = 0; i < pc->cdata->pwm_num_devs; i++)
> -		pwm_disable(&pc->chip.pwms[i]);
> +	if (pc->cdata->cpt_num_devs) {
> +		clk_unprepare(pc->cpt_clk);
> +		clk_put(pc->cpt_clk);
> +	}
>  
> -	clk_unprepare(pc->pwm_clk);
> -	clk_unprepare(pc->cpt_clk);
> +	if (pc->cdata->pwm_num_devs) {
> +		clk_unprepare(pc->pwm_clk);
> +		clk_put(pc->pwm_clk);
> +	}

Again, no need for the evaluations, just call clk_{unprepare,put}.

> -	return pwmchip_remove(&pc->chip);
> +	return 0;
>  }
>  
>  static const struct of_device_id sti_pwm_of_match[] = {

-- 
Lee Jones [李琼斯]
Senior Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog

  reply	other threads:[~2020-11-06  8:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-13  8:15 [PATCH] pwm: sti: fix error handling Uwe Kleine-König
2020-11-06  8:29 ` Lee Jones [this message]
2020-11-06  9:34   ` Uwe Kleine-König
2020-11-06 10:29     ` Lee Jones
2020-11-11 19:28       ` Thierry Reding
2020-11-11 19:43         ` Uwe Kleine-König
2020-11-11 19:52           ` Thierry Reding
2020-11-11 20:39             ` Uwe Kleine-König
2020-11-11 21:16               ` Thierry Reding

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=20201106082914.GX4488@dell \
    --to=lee.jones@linaro.org \
    --cc=ajitpal.singh@st.com \
    --cc=linux-pwm@vger.kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=uwe@kleine-koenig.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