diff mbox series

pwm: sti: Fix the error handling path of sti_pwm_probe()

Message ID ef5d6301cb120db5d52175a7bf94b5095beaaeef.1681633924.git.christophe.jaillet@wanadoo.fr
State Changes Requested
Headers show
Series pwm: sti: Fix the error handling path of sti_pwm_probe() | expand

Commit Message

Christophe JAILLET April 16, 2023, 8:32 a.m. UTC
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(-)

Comments

Uwe Kleine-König April 16, 2023, 5:02 p.m. UTC | #1
Hello,

On Sun, Apr 16, 2023 at 10:32:24AM +0200, Christophe JAILLET wrote:
> Rewrite the error handing path of sti_pwm_probe() to avoid some leaks.

There are also some clk_put()'s missing. See
https://lore.kernel.org/linux-pwm/20201013081531.661528-1-uwe@kleine-koenig.org
for an older fix of the things you noticed.

Best regards
Uwe
diff mbox series

Patch

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)