mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4] hwmon: (gpio-fan) fix pm_runtime imbalance for alarm-only fans
@ 2026-09-14 11:38 Cong Nguyen
  0 siblings, 0 replies; only message in thread
From: Cong Nguyen @ 2026-09-14 11:38 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Alexander Stein, linux-hwmon, linux-kernel

pm_runtime_enable() runs unconditionally in probe, but its devm cleanup
is only registered when control GPIOs are present, so alarm-only fans
never get pm_runtime_disable() on unbind.

v2's fix moved pm_runtime_enable() earlier and opened a sysfs race; v3
fixed that. Guenter asked this version also close the other two PM
issues Sashiko found: a pm_enabled guard so an early probe failure
can't drop a PM ref that was never taken, and a lock around the
enable-and-check block so a racing sysfs write can't double-take one.

Fixes: 0d01110e6356 ("hwmon: (gpio-fan) Add regulator support")
Reported-by: Sashiko AI review <sashiko-bot@kernel.org>
Link: https://lore.kernel.org/r/20260901111903.660681-1-congnt264@gmail.com
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-4
Signed-off-by: Cong Nguyen <congnt264@gmail.com>
---
Changes in v4:
- Covers the other 2 PM findings Guenter asked for: pm_enabled guard
  on gpio_fan_stop(), and a lock around the enable-and-check block.

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

diff --git a/drivers/hwmon/gpio-fan.c b/drivers/hwmon/gpio-fan.c
index 084828e1e281..93dd685c4fa5 100644
--- a/drivers/hwmon/gpio-fan.c
+++ b/drivers/hwmon/gpio-fan.c
@@ -46,6 +46,7 @@ struct gpio_fan_data {
 	struct gpio_desc	*alarm_gpio;
 	struct work_struct	alarm_work;
 	struct regulator	*supply;
+	bool			pm_enabled;
 };
 
 /*
@@ -522,10 +523,21 @@ static void gpio_fan_stop(void *data)
 	struct gpio_fan_data *fan_data = data;
 
 	mutex_lock(&fan_data->lock);
-	set_fan_speed(data, 0);
+	/* set_fan_speed(0) drops a PM ref; only valid once PM is enabled. */
+	if (fan_data->pm_enabled)
+		set_fan_speed(data, 0);
+	else
+		__set_fan_ctrl(fan_data, fan_data->speed[0].ctrl_val);
 	mutex_unlock(&fan_data->lock);
+}
 
-	pm_runtime_disable(fan_data->dev);
+static void gpio_fan_pm_runtime_disable(void *data)
+{
+	struct gpio_fan_data *fan_data = data;
+
+	/* Registered before pm_runtime_enable() runs; skip if it never did. */
+	if (fan_data->pm_enabled)
+		pm_runtime_disable(fan_data->dev);
 }
 
 static int gpio_fan_probe(struct platform_device *pdev)
@@ -553,6 +565,11 @@ static int gpio_fan_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, PTR_ERR(fan_data->supply),
 				     "Failed to get fan-supply");
 
+	/* Register before gpio_fan_stop() so LIFO teardown runs that first. */
+	err = devm_add_action_or_reset(dev, gpio_fan_pm_runtime_disable, fan_data);
+	if (err)
+		return err;
+
 	/* Configure control GPIOs if available. */
 	if (fan_data->gpios && fan_data->num_gpios > 0) {
 		if (!fan_data->speed || fan_data->num_speed <= 1)
@@ -580,16 +597,22 @@ static int gpio_fan_probe(struct platform_device *pdev)
 			return err;
 	}
 
+	/* Lock so a racing sysfs write can't double-take a PM ref here. */
+	mutex_lock(&fan_data->lock);
 	pm_runtime_set_suspended(&pdev->dev);
 	pm_runtime_enable(&pdev->dev);
+	fan_data->pm_enabled = true;
 	/* If current GPIO state is active, mark RPM as active as well */
 	if (fan_data->speed_index > 0) {
 		int ret;
 
 		ret = pm_runtime_resume_and_get(&pdev->dev);
-		if (ret)
+		if (ret) {
+			mutex_unlock(&fan_data->lock);
 			return ret;
+		}
 	}
+	mutex_unlock(&fan_data->lock);
 
 	/* Optional cooling device register for Device tree platforms */
 	fan_data->cdev = devm_thermal_of_child_cooling_device_register(dev, np,
-- 
2.25.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-09-14 11:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-14 11:38 [PATCH v4] hwmon: (gpio-fan) fix pm_runtime imbalance for alarm-only fans Cong Nguyen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®