linux-pwm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
To: "Thierry Reding" <thierry.reding@gmail.com>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"Lee Jones" <lee.jones@linaro.org>
Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org,
	Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
	linux-pwm@vger.kernel.org
Subject: [PATCH] pwm: sti: Fix the error handling path of sti_pwm_probe()
Date: Sun, 16 Apr 2023 10:32:24 +0200	[thread overview]
Message-ID: <ef5d6301cb120db5d52175a7bf94b5095beaaeef.1681633924.git.christophe.jaillet@wanadoo.fr> (raw)

Rewrite the error handing path of sti_pwm_probe() to avoid some leaks.

Fixes: 3f0925b5a864 ("pwm: sti: Initialise PWM capture device data")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
I also wonder if some clk_put() are also missing in the error handling path
and in the remove function.

This could be split in several patches because some issues were
introduced before 3f0925b5a864.
But it is already nearly 7 years old, so it should be enough, should it be
back-ported.
---
 drivers/pwm/pwm-sti.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/drivers/pwm/pwm-sti.c b/drivers/pwm/pwm-sti.c
index b1d1373648a3..7e678cab2991 100644
--- a/drivers/pwm/pwm-sti.c
+++ b/drivers/pwm/pwm-sti.c
@@ -630,13 +630,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_unprepare_pwm;
 		}
 
 		ret = clk_prepare(pc->cpt_clk);
 		if (ret) {
 			dev_err(dev, "failed to prepare clock\n");
-			return ret;
+			goto err_unprepare_pwm;
 		}
 	}
 
@@ -645,18 +646,17 @@ static int sti_pwm_probe(struct platform_device *pdev)
 	pc->chip.npwm = pc->cdata->pwm_num_devs;
 
 	ret = pwmchip_add(&pc->chip);
-	if (ret < 0) {
-		clk_unprepare(pc->pwm_clk);
-		clk_unprepare(pc->cpt_clk);
-		return ret;
-	}
+	if (ret < 0)
+		goto err_unprepare_cpt;
 
 	for (i = 0; i < cdata->cpt_num_devs; i++) {
 		struct sti_cpt_ddata *ddata;
 
 		ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
-		if (!ddata)
-			return -ENOMEM;
+		if (!ddata) {
+			ret = -ENOMEM;
+			goto err_remove_pwmchip;
+		}
 
 		init_waitqueue_head(&ddata->wait);
 		mutex_init(&ddata->lock);
@@ -667,6 +667,14 @@ static int sti_pwm_probe(struct platform_device *pdev)
 	platform_set_drvdata(pdev, pc);
 
 	return 0;
+
+err_remove_pwmchip:
+	pwmchip_remove(&pc->chip);
+err_unprepare_cpt:
+	clk_unprepare(pc->cpt_clk);
+err_unprepare_pwm:
+	clk_unprepare(pc->pwm_clk);
+	return ret;
 }
 
 static void sti_pwm_remove(struct platform_device *pdev)
-- 
2.34.1


             reply	other threads:[~2023-04-16  8:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-16  8:32 Christophe JAILLET [this message]
2023-04-16 17:02 ` [PATCH] pwm: sti: Fix the error handling path of sti_pwm_probe() Uwe Kleine-König

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=ef5d6301cb120db5d52175a7bf94b5095beaaeef.1681633924.git.christophe.jaillet@wanadoo.fr \
    --to=christophe.jaillet@wanadoo.fr \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=lee.jones@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=u.kleine-koenig@pengutronix.de \
    /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;
as well as URLs for NNTP newsgroup(s).