[PATCH] Input: misc: pwm-vibra: fix resource leaks on start failure

Haotian Zhang posted 1 patch 5 days, 7 hours ago
drivers/input/misc/pwm-vibra.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
[PATCH] Input: misc: pwm-vibra: fix resource leaks on start failure
Posted by Haotian Zhang 5 days, 7 hours ago
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
Re: [PATCH] Input: misc: pwm-vibra: fix resource leaks on start failure
Posted by Sebastian Reichel 3 days, 23 hours ago
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
> 
>