linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Input: misc: pwm-vibra: fix resource leaks on start failure
@ 2025-11-26 14:56 Haotian Zhang
  2025-11-27 23:20 ` Sebastian Reichel
  0 siblings, 1 reply; 2+ messages in thread
From: Haotian Zhang @ 2025-11-26 14:56 UTC (permalink / raw)
  To: sebastian.reichel, dmitry.torokhov
  Cc: linux-input, linux-kernel, Haotian Zhang

The pwm_vibrator_start() function returns immediately if
pwm_apply_might_sleep() fails, neglecting to disable the
regulator or reset the enable GPIO. This results in a
potential resource leak.

Introduce a local flag to track regulator enablement
and implement an error handling path. Deassert the enable
GPIO and disable the regulator upon failure.

Fixes: 3e5b08518f6a ("Input: add a driver for PWM controllable vibrators")
Signed-off-by: Haotian Zhang <vulab@iscas.ac.cn>
---
 drivers/input/misc/pwm-vibra.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/drivers/input/misc/pwm-vibra.c b/drivers/input/misc/pwm-vibra.c
index 3e5ed685ed8f..d323a12596c3 100644
--- a/drivers/input/misc/pwm-vibra.c
+++ b/drivers/input/misc/pwm-vibra.c
@@ -40,6 +40,7 @@ static int pwm_vibrator_start(struct pwm_vibrator *vibrator)
 	struct device *pdev = vibrator->input->dev.parent;
 	struct pwm_state state;
 	int err;
+	bool new_vcc_on = false;
 
 	if (!vibrator->vcc_on) {
 		err = regulator_enable(vibrator->vcc);
@@ -48,6 +49,7 @@ static int pwm_vibrator_start(struct pwm_vibrator *vibrator)
 			return err;
 		}
 		vibrator->vcc_on = true;
+		new_vcc_on = true;
 	}
 
 	gpiod_set_value_cansleep(vibrator->enable_gpio, 1);
@@ -59,7 +61,7 @@ static int pwm_vibrator_start(struct pwm_vibrator *vibrator)
 	err = pwm_apply_might_sleep(vibrator->pwm, &state);
 	if (err) {
 		dev_err(pdev, "failed to apply pwm state: %d\n", err);
-		return err;
+		goto err_gpio;
 	}
 
 	if (vibrator->pwm_dir) {
@@ -71,11 +73,19 @@ static int pwm_vibrator_start(struct pwm_vibrator *vibrator)
 		if (err) {
 			dev_err(pdev, "failed to apply dir-pwm state: %d\n", err);
 			pwm_disable(vibrator->pwm);
-			return err;
+			goto err_gpio;
 		}
 	}
 
 	return 0;
+
+err_gpio:
+	gpiod_set_value_cansleep(vibrator->enable_gpio, 0);
+	if (new_vcc_on) {
+		regulator_disable(vibrator->vcc);
+		vibrator->vcc_on = false;
+	}
+	return err;
 }
 
 static void pwm_vibrator_stop(struct pwm_vibrator *vibrator)
-- 
2.50.1.windows.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-11-27 23:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-26 14:56 [PATCH] Input: misc: pwm-vibra: fix resource leaks on start failure Haotian Zhang
2025-11-27 23:20 ` Sebastian Reichel

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).