* [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
* Re: [PATCH] Input: misc: pwm-vibra: fix resource leaks on start failure
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
0 siblings, 0 replies; 2+ messages in thread
From: Sebastian Reichel @ 2025-11-27 23:20 UTC (permalink / raw)
To: Haotian Zhang; +Cc: dmitry.torokhov, linux-input, linux-kernel, kernel
[-- Attachment #1: Type: text/plain, Size: 2837 bytes --]
Hi,
On Wed, Nov 26, 2025 at 10:56:35PM +0800, Haotian Zhang wrote:
> 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>
> ---
As the remaining driver code right now always ignores the return
code from pwm_vibrator_start(), it is expected running even with the
PWM failure. There is no real resource leak, since things will
properly be disabled in pwm_vibrator_stop() when userspace is done
with the vibrator request.
So I think this patch does not really add any value without changing
the error handling behavior in general.
I guess this has been found by some code analysis tool and not on
real hardware...
Greetings,
-- Sebastian
> 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
>
>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [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).