All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hwmon: (gpio-fan) Update the speed cache before dropping the PM reference
@ 2026-10-31  9:50 Baek Kyu, Kim
  2026-10-31  9:56 ` sashiko-bot
  2026-10-31 15:38 ` Guenter Roeck
  0 siblings, 2 replies; 3+ messages in thread
From: Baek Kyu, Kim @ 2026-10-31  9:50 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Baek Kyu, Kim, linux-hwmon, linux-kernel

set_fan_speed() writes the GPIO control value before dropping the runtime
PM reference when stopping the fan. pm_runtime_put_sync() decrements the
usage count even when suspending the device fails, but the error return
currently leaves speed_index at the previous running speed.

A request for that same running speed then returns success without
changing the GPIOs or taking a runtime PM reference. Retrying the stop
instead drops a reference the driver no longer owns and can trigger a
runtime PM usage count underflow warning.

Update speed_index after writing the GPIOs and before dropping the PM
reference. The initial same-speed check already excludes a stopped fan,
so a zero target is sufficient to identify the transition that needs a
PM put. Preserve the PM error return and the CONFIG_PM=n exception.

This keeps the cached control state and PM reference ownership consistent
without restoring running GPIOs after a possibly partial supply failure.
It does not clear persistent runtime PM errors or claim that a failing
regulator has successfully powered off.

Fixes: 0d01110e6356 ("hwmon: (gpio-fan) Add regulator support")

Signed-off-by: Baek Kyu, Kim <pocj8ur4in@gmail.com>
---
An arm64 QEMU/KVM kernel exercised gpio-fan through hwmon sysfs with
virtual GPIO and regulator providers and the actual runtime PM core.
An injected -EBUSY regulator disable error after a running-to-stopped
request left GPIOs off and PM usage at zero. Before the change the
speed cache stayed running and a repeated stop underflowed PM usage;
after the change the cache stayed stopped and no PM underflow occurred.
No physical fan or RPM was tested.

 drivers/hwmon/gpio-fan.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/hwmon/gpio-fan.c b/drivers/hwmon/gpio-fan.c
index 3f78375eeb448..08b58b49be5b9 100644
--- a/drivers/hwmon/gpio-fan.c
+++ b/drivers/hwmon/gpio-fan.c
@@ -150,7 +150,10 @@ static int set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
 
 	__set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
 
-	if (fan_data->speed_index > 0 && speed_index == 0) {
+	/* The GPIOs have changed, and a PM put drops the reference on error. */
+	fan_data->speed_index = speed_index;
+
+	if (speed_index == 0) {
 		int ret;
 
 		ret = pm_runtime_put_sync(fan_data->dev);
@@ -158,8 +161,6 @@ static int set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
 			return ret;
 	}
 
-	fan_data->speed_index = speed_index;
-
 	return 0;
 }
 
-- 
2.55.0


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

* Re: [PATCH] hwmon: (gpio-fan) Update the speed cache before dropping the PM reference
  2026-10-31  9:50 [PATCH] hwmon: (gpio-fan) Update the speed cache before dropping the PM reference Baek Kyu, Kim
@ 2026-10-31  9:56 ` sashiko-bot
  2026-10-31 15:38 ` Guenter Roeck
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-10-31  9:56 UTC (permalink / raw)
  To: Baek Kyu, Kim; +Cc: linux-hwmon

> set_fan_speed() writes the GPIO control value before dropping the runtime
> PM reference when stopping the fan. pm_runtime_put_sync() decrements the
> usage count even when suspending the device fails, but the error return
> currently leaves speed_index at the previous running speed.
> 
> A request for that same running speed then returns success without
> changing the GPIOs or taking a runtime PM reference. Retrying the stop
> instead drops a reference the driver no longer owns and can trigger a
> runtime PM usage count underflow warning.
> 
> Update speed_index after writing the GPIOs and before dropping the PM
> reference. The initial same-speed check already excludes a stopped fan,
> so a zero target is sufficient to identify the transition that needs a
> PM put. Preserve the PM error return and the CONFIG_PM=n exception.
> 
> This keeps the cached control state and PM reference ownership consistent
> without restoring running GPIOs after a possibly partial supply failure.
> It does not clear persistent runtime PM errors or claim that a failing
> regulator has successfully powered off.
> 
> Fixes: 0d01110e6356 ("hwmon: (gpio-fan) Add regulator support")
> 
> Signed-off-by: Baek Kyu, Kim <pocj8ur4in@gmail.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261031095039.11683-1-pocj8ur4in@gmail.com?part=1


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

* Re: [PATCH] hwmon: (gpio-fan) Update the speed cache before dropping the PM reference
  2026-10-31  9:50 [PATCH] hwmon: (gpio-fan) Update the speed cache before dropping the PM reference Baek Kyu, Kim
  2026-10-31  9:56 ` sashiko-bot
@ 2026-10-31 15:38 ` Guenter Roeck
  1 sibling, 0 replies; 3+ messages in thread
From: Guenter Roeck @ 2026-10-31 15:38 UTC (permalink / raw)
  To: Baek Kyu, Kim; +Cc: linux-hwmon, linux-kernel

On Sat, Oct 31, 2026 at 06:50:39PM +0900, Baek Kyu, Kim wrote:
> set_fan_speed() writes the GPIO control value before dropping the runtime
> PM reference when stopping the fan. pm_runtime_put_sync() decrements the
> usage count even when suspending the device fails, but the error return
> currently leaves speed_index at the previous running speed.
> 
> A request for that same running speed then returns success without
> changing the GPIOs or taking a runtime PM reference. Retrying the stop
> instead drops a reference the driver no longer owns and can trigger a
> runtime PM usage count underflow warning.
> 
> Update speed_index after writing the GPIOs and before dropping the PM
> reference. The initial same-speed check already excludes a stopped fan,
> so a zero target is sufficient to identify the transition that needs a
> PM put. Preserve the PM error return and the CONFIG_PM=n exception.
> 
> This keeps the cached control state and PM reference ownership consistent
> without restoring running GPIOs after a possibly partial supply failure.
> It does not clear persistent runtime PM errors or claim that a failing
> regulator has successfully powered off.
> 
> Fixes: 0d01110e6356 ("hwmon: (gpio-fan) Add regulator support")
> 
For the future: No blank line here, please.

> Signed-off-by: Baek Kyu, Kim <pocj8ur4in@gmail.com>

Applied.

Thanks,
Guenter

> ---
> An arm64 QEMU/KVM kernel exercised gpio-fan through hwmon sysfs with
> virtual GPIO and regulator providers and the actual runtime PM core.
> An injected -EBUSY regulator disable error after a running-to-stopped
> request left GPIOs off and PM usage at zero. Before the change the
> speed cache stayed running and a repeated stop underflowed PM usage;
> after the change the cache stayed stopped and no PM underflow occurred.
> No physical fan or RPM was tested.
> 
>  drivers/hwmon/gpio-fan.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/hwmon/gpio-fan.c b/drivers/hwmon/gpio-fan.c
> index 3f78375eeb448..08b58b49be5b9 100644
> --- a/drivers/hwmon/gpio-fan.c
> +++ b/drivers/hwmon/gpio-fan.c
> @@ -150,7 +150,10 @@ static int set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
>  
>  	__set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
>  
> -	if (fan_data->speed_index > 0 && speed_index == 0) {
> +	/* The GPIOs have changed, and a PM put drops the reference on error. */
> +	fan_data->speed_index = speed_index;
> +
> +	if (speed_index == 0) {
>  		int ret;
>  
>  		ret = pm_runtime_put_sync(fan_data->dev);
> @@ -158,8 +161,6 @@ static int set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
>  			return ret;
>  	}
>  
> -	fan_data->speed_index = speed_index;
> -
>  	return 0;
>  }
>  

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

end of thread, other threads:[~2026-10-31 15:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-31  9:50 [PATCH] hwmon: (gpio-fan) Update the speed cache before dropping the PM reference Baek Kyu, Kim
2026-10-31  9:56 ` sashiko-bot
2026-10-31 15:38 ` Guenter Roeck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.