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

From: Christophe JAILLET
Date: Sun Apr 16 2023 - 04:32:38 EST


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@xxxxxxxxxx>
---
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