hwmon: (gpio-fan) fix pm_runtime imbalance for alarm-only fans
PATCH v3 1/1 hwmon: (gpio-fan) fix pm_runtime imbalance for alarm-only fans
pm_runtime_enable() runs unconditionally in probe, but the devm cleanup that calls pm_runtime_disable() is only registered when control GPIOs are present. Alarm-only fans never get it, so unbind warns about the missing disable.
v2 fixed this by switching to devm_pm_runtime_enable() and moving it before gpio_fan_stop()'s devm registration, so LIFO teardown runs gpio_fan_stop() first. Sashiko/Guenter correctly flagged that moving the actual pm_runtime_enable() call earlier opens a new race: hwmon sysfs (pwm1, fan1_target) is now exposed while PM is already enabled, so a concurrent sysfs write transitioning speed 0->nonzero can call pm_runtime_resume_and_get() successfully, and probe's own final speed_index check does it again -- a double-increment that permanently blocks runtime suspend. In the original code this same race just fails cleanly with -EACCES, since PM isn't enabled yet at that point.
Fix this properly: reserve the devm cleanup slot early (a small gpio_fan_pm_runtime_disable() wrapper, registered right after the regulator is acquired, before gpio_fan_stop()'s registration) without touching when pm_runtime_enable() itself actually runs. The enable call stays at its original position, after hwmon registration and alarm init, so the race window is identical to the pristine driver -- only the devm teardown order changes, restoring gpio_fan_stop() running before the PM disable on unbind, exactly as v2 intended.
---
Changes in v3:
- v2 moved the actual pm_runtime_enable() call earlier, which fixed the LIFO ordering but opened a new race (Sashiko/Guenter): sysfs exposed while PM already enabled. v3 only moves the devm cleanup registration early, leaving pm_runtime_enable() at its original position -- no behavior change to the race window, only teardown order changes.
drivers/hwmon/gpio-fan.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/hwmon/gpio-fan.c +15 -1
@@ -524,8 +524,11 @@ static void gpio_fan_stop(void *data)mutex_lock (&fan_data->lock);set_fan_speed (data,0 );mutex_unlock (&fan_data->lock); +} -pm_runtime_disable (fan_data->dev); +static void gpio_fan_pm_runtime_disable (void *data) +{ +pm_runtime_disable (data); }static int gpio_fan_probe (struct platform_device *pdev)
@@ -553,6 +556,17 @@ static int gpio_fan_probe(struct platform_device *pdev)return dev_err_probe (dev,PTR_ERR (fan_data->supply),"Failed to get fan-supply" ); +/* +* Reserve this devm slot before gpio_fan_stop()'s so LIFO teardown +* runs gpio_fan_stop() (needs PM enabled to disable the regulator) +* first. pm_runtime_enable() itself still happens at its original +* position below, so this doesn't change when PM actually becomes +* enabled -- only where its eventual disable is queued. +*/ + err =devm_add_action_or_reset (dev, gpio_fan_pm_runtime_disable, dev); +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 )
Signature · 1 line
2.25.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
New issues:
- [High] Unbalanced pm_runtime_disable() on probe error paths permanently disables runtime PM.
Pre-existing issues:
- [High] Failing to resume the device at the end of probe causes a usage count underflow during devm cleanup.
- [High] A race condition between probe and sysfs writes can cause a double-increment of the runtime PM usage count, permanently blocking runtime suspend.
- [High] Missing synchronize_irq() in runtime suspend callback for shared IRQ.
- [High] Missing cancel_work_sync() for alarm_work leads to a Use-After-Free on driver unbind or probe failure.
- [High] Unconditional IRQ_NONE return in a shared interrupt handler.
--
commit c1b61438a4a40096fe54d7b86037b9d5eebc1cae
Author: Cong Nguyen <[email protected]>
hwmon: (gpio-fan) fix pm_runtime imbalance for alarm-only fans
pm_runtime_enable() runs unconditionally in probe, but the devm cleanup that calls pm_runtime_disable() is only registered when control GPIOs are present. Alarm-only fans never get it, so unbind warns about the missing disable.
Quoted 4 lines
drivers/hwmon/gpio-fan.c +0
-0
[ ... ]
Quoted 4 lines
drivers/hwmon/gpio-fan.c +0
-0@@ -553,6 +556,17 @@ static int gpio_fan_probe(struct platform_device *pdev)return dev_err_probe (dev,PTR_ERR (fan_data->supply),"Failed to get fan-supply" );
drivers/hwmon/gpio-fan.c static int gpio_fan_probe(struct platform_device *pdev) · line 568
Quoted 10 lines
drivers/hwmon/gpio-fan.c +10
-0+/* +* Reserve this devm slot before gpio_fan_stop()'s so LIFO teardown +* runs gpio_fan_stop() (needs PM enabled to disable the regulator) +* first. pm_runtime_enable() itself still happens at its original +* position below, so this doesn't change when PM actually becomes +* enabled -- only where its eventual disable is queued. +*/ + err =devm_add_action_or_reset (dev, gpio_fan_pm_runtime_disable, dev); +if (err) +return err;
[Severity: High]
Does unconditionally registering devm_add_action_or_reset() early in the probe path cause an unbalanced pm_runtime_disable() on probe error paths?
If probe fails at any intermediate point (e.g., in fan_ctrl_init(), hwmon_device_register_with_groups(), or fan_alarm_init()), devm teardown will execute gpio_fan_pm_runtime_disable(). Since pm_runtime_enable() hasn't been reached yet, this incorrectly increments dev->power.disable_depth. On a subsequent probe attempt, pm_runtime_enable() will only decrement it to 1, leaving runtime PM permanently disabled.
[Severity: High]
This isn't a bug introduced by this patch, but does failing to resume the device at the end of probe cause a usage count underflow during devm cleanup?
fan_ctrl_init() initializes fan_data->speed_index based on the hardware state. If the fan is active, speed_index > 0. gpio_fan_stop() is then queued as a devm action. If probe subsequently fails before the final pm_runtime_resume_and_get() succeeds, the devm cleanup runs gpio_fan_stop(), which calls set_fan_speed():
drivers/hwmon/gpio-fan.c:set_fan_speed() {
...
if (fan_data->speed_index > 0 && speed_index == 0) {
int ret;
ret = pm_runtime_put_sync(fan_data->dev);
if (ret < 0 && ret != -ENOSYS)
return ret;
}
...
}Because speed_index > 0, set_fan_speed() drops a PM reference that was never actually acquired, pushing the usage count below zero.
[Severity: High]
This is a pre-existing issue, but does a race condition between probe and sysfs writes cause a double-increment of the runtime PM usage count?
Sysfs attributes are exposed early by devm_hwmon_device_register_with_groups(). Userspace can write to pwm1 right after probe calls pm_runtime_enable(). The sysfs handler acquires a PM reference and sets speed_index > 0.
The probe thread then checks the state:
drivers/hwmon/gpio-fan.c:gpio_fan_probe() {
...
/* 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)
return ret;
}
...
}Since this check doesn't hold fan_data->lock, probe sees the new value and takes a second PM reference. When userspace later stops the fan, only one reference is dropped, permanently blocking runtime suspend.
[Severity: High]
This is a pre-existing issue, but is synchronize_irq() missing in the runtime suspend callback for a shared IRQ?
The driver requests a shared interrupt for the alarm GPIO:
drivers/hwmon/gpio-fan.c:fan_alarm_init() {
...
return devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler,
IRQF_SHARED, "GPIO fan alarm", fan_data);
}The PM subsystem guide mandates that drivers using IRQF_SHARED MUST call synchronize_irq() in their runtime suspend callback before powering down hardware.
Since this is missing, the IRQ handler could execute mid-flight while the regulator is being disabled in gpio_fan_runtime_suspend():
drivers/hwmon/gpio-fan.c:gpio_fan_runtime_suspend() {
...
if (fan_data->supply)
ret = regulator_disable(fan_data->supply);
...
}[Severity: High]
This is a pre-existing issue, but does missing cancel_work_sync() for alarm_work lead to a use-after-free on driver unbind or probe failure?
The driver initializes fan_data->alarm_work and requests an interrupt to schedule it:
drivers/hwmon/gpio-fan.c:fan_alarm_init() {
...
INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
...
return devm_request_irq(dev, alarm_irq, ...);
}It fails to register a cleanup action to call cancel_work_sync(). During driver unbind or probe failure, devm_request_irq is unwound, which synchronizes with running handlers but does not cancel pending work. fan_data is then freed by devm. If an interrupt fired right before cleanup, the work executes asynchronously and dereferences the freed fan_data pointer.
[Severity: High]
This is a pre-existing issue, but does the shared interrupt handler return an unconditional IRQ_NONE?
drivers/hwmon/gpio-fan.c:fan_alarm_irq_handler() {
...
schedule_work(&fan_data->alarm_work);
return IRQ_NONE;
}The driver requests an IRQF_SHARED interrupt. In its handler, it processes the event but unconditionally returns IRQ_NONE. When the kernel sees IRQ_NONE from all handlers on a shared line, it registers an unhandled interrupt. After enough unhandled interrupts, the kernel disables the IRQ line permanently, breaking all devices sharing it.
Quoted 4 lines
drivers/hwmon/gpio-fan.c +1
-0+/* Configure control GPIOs if available. */ if (fan_data->gpios && fan_data->num_gpios >0 ) {if (!fan_data->speed || fan_data->num_speed <=1 )
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Guenter Roeck
maintainerQuoted 11 lines · On 9/1/26 04:32, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
New issues:
- [High] Unbalancedpm_runtime_disable()on probe error paths permanently disables runtime PM.Pre-existing issues:
- [High] Failing to resume the device at the end of probe causes a usage count underflow during devm cleanup.
- [High] A race condition between probe and sysfs writes can cause a double-increment of the runtime PM usage count, permanently blocking runtime suspend.
- [High] Missingsynchronize_irq()in runtime suspend callback for shared IRQ.
- [High] Missingcancel_work_sync()foralarm_workleads to a Use-After-Free on driver unbind or probe failure.
- [High] UnconditionalIRQ_NONEreturn in a shared interrupt handler.
PM seems to be almost impossible to get right :-(.
I don't expect the other issues to be fixed as part of this patch, but it should fix all the PM issues.
Thanks,
Guenter
Quoted 164 lines
--
commit
c1b61438a4a40096fe54d7b86037b9d5eebc1cae
Author: Cong Nguyen <[email protected]>hwmon: (gpio-fan) fix
pm_runtimeimbalance for alarm-only fans
pm_runtime_enable()runs unconditionally in probe, but the devm cleanup that callspm_runtime_disable()is only registered when control GPIOs are present. Alarm-only fans never get it, so unbind warns about the missing disable.Quoted 4 lines
drivers/hwmon/gpio-fan.c +0
-0[ ... ]
Quoted 4 lines
drivers/hwmon/gpio-fan.c +0
-0@@ -553,6 +556,17 @@ static int gpio_fan_probe(struct platform_device *pdev)return dev_err_probe (dev,PTR_ERR (fan_data->supply),"Failed to get fan-supply" );drivers/hwmon/gpio-fan.c static int gpio_fan_probe(struct platform_device *pdev) · line 568
Quoted 10 lines
drivers/hwmon/gpio-fan.c +10
-0+/* +* Reserve this devm slot before gpio_fan_stop()'s so LIFO teardown +* runs gpio_fan_stop() (needs PM enabled to disable the regulator) +* first. pm_runtime_enable() itself still happens at its original +* position below, so this doesn't change when PM actually becomes +* enabled -- only where its eventual disable is queued. +*/ + err =devm_add_action_or_reset (dev, gpio_fan_pm_runtime_disable, dev); +if (err) +return err;[Severity: High]
Does unconditionally registeringdevm_add_action_or_reset()early in the probe path cause an unbalancedpm_runtime_disable()on probe error paths?If probe fails at any intermediate point (e.g., in
fan_ctrl_init(),hwmon_device_register_with_groups(), orfan_alarm_init()), devm teardown will executegpio_fan_pm_runtime_disable(). Sincepm_runtime_enable()hasn't been reached yet, this incorrectly incrementsdev->power.disable_depth. On a subsequent probe attempt,pm_runtime_enable()will only decrement it to 1, leaving runtime PM permanently disabled.[Severity: High]
This isn't a bug introduced by this patch, but does failing to resume the device at the end of probe cause a usage count underflow during devm cleanup?
fan_ctrl_init()initializesfan_data->speed_indexbased on the hardware state. If the fan is active,speed_index> 0.gpio_fan_stop()is then queued as a devm action. If probe subsequently fails before the finalpm_runtime_resume_and_get()succeeds, the devm cleanup runsgpio_fan_stop(), which callsset_fan_speed():drivers/hwmon/gpio-fan.c:set_fan_speed() { ... if (fan_data->speed_index > 0 && speed_index == 0) { int ret; ret = pm_runtime_put_sync(fan_data->dev); if (ret < 0 && ret != -ENOSYS) return ret; } ... }Because
speed_index> 0,set_fan_speed()drops a PM reference that was never actually acquired, pushing the usage count below zero.[Severity: High]
This is a pre-existing issue, but does a race condition between probe and sysfs writes cause a double-increment of the runtime PM usage count?Sysfs attributes are exposed early by
devm_hwmon_device_register_with_groups(). Userspace can write to pwm1 right after probe callspm_runtime_enable(). The sysfs handler acquires a PM reference and setsspeed_index> 0.The probe thread then checks the state:
drivers/hwmon/gpio-fan.c:gpio_fan_probe() { ... /* 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) return ret; } ... }Since this check doesn't hold
fan_data->lock, probe sees the new value and takes a second PM reference. When userspace later stops the fan, only one reference is dropped, permanently blocking runtime suspend.[Severity: High]
This is a pre-existing issue, but issynchronize_irq()missing in the runtime suspend callback for a shared IRQ?The driver requests a shared interrupt for the alarm GPIO:
drivers/hwmon/gpio-fan.c:fan_alarm_init() { ... return devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler, IRQF_SHARED, "GPIO fan alarm", fan_data); }The PM subsystem guide mandates that drivers using
IRQF_SHAREDMUST callsynchronize_irq()in their runtime suspend callback before powering down hardware.Since this is missing, the IRQ handler could execute mid-flight while the regulator is being disabled in
gpio_fan_runtime_suspend():drivers/hwmon/gpio-fan.c:gpio_fan_runtime_suspend() { ... if (fan_data->supply) ret = regulator_disable(fan_data->supply); ... }[Severity: High]
This is a pre-existing issue, but does missingcancel_work_sync()foralarm_worklead to a use-after-free on driver unbind or probe failure?The driver initializes
fan_data->alarm_workand requests an interrupt to schedule it:drivers/hwmon/gpio-fan.c:fan_alarm_init() { ... INIT_WORK(&fan_data->alarm_work, fan_alarm_notify); ... return devm_request_irq(dev, alarm_irq, ...); }It fails to register a cleanup action to call
cancel_work_sync(). During driver unbind or probe failure,devm_request_irqis unwound, which synchronizes with running handlers but does not cancel pending work.fan_datais then freed by devm. If an interrupt fired right before cleanup, the work executes asynchronously and dereferences the freedfan_datapointer.[Severity: High]
This is a pre-existing issue, but does the shared interrupt handler return an unconditionalIRQ_NONE?drivers/hwmon/gpio-fan.c:fan_alarm_irq_handler() { ... schedule_work(&fan_data->alarm_work); return IRQ_NONE; }The driver requests an
IRQF_SHAREDinterrupt. In its handler, it processes the event but unconditionally returnsIRQ_NONE. When the kernel seesIRQ_NONEfrom all handlers on a shared line, it registers an unhandled interrupt. After enough unhandled interrupts, the kernel disables the IRQ line permanently, breaking all devices sharing it.Quoted 4 lines
drivers/hwmon/gpio-fan.c +1
-0+/* Configure control GPIOs if available. */ if (fan_data->gpios && fan_data->num_gpios >0 ) {if (!fan_data->speed || fan_data->num_speed <=1 )
Quoted 14 lines · Guenter Roeck
Quoted 11 lines · On 9/1/26 04:32, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
New issues:
- [High] Unbalancedpm_runtime_disable()on probe error paths permanently disables runtime PM.Pre-existing issues:
- [High] Failing to resume the device at the end of probe causes a usage count underflow during devm cleanup.
- [High] A race condition between probe and sysfs writes can cause a double-increment of the runtime PM usage count, permanently blocking runtime suspend.
- [High] Missingsynchronize_irq()in runtime suspend callback for shared IRQ.
- [High] Missingcancel_work_sync()foralarm_workleads to a Use-After-Free on driver unbind or probe failure.
- [High] UnconditionalIRQ_NONEreturn in a shared interrupt handler.
Quoted 3 lines
PM seems to be almost impossible to get right :-(.
I don't expect the other issues to be fixed as part of this patch, but it should fix all the PM issues.
Agreed, I sent v4 covers all 3 PM findings from Sashiko's review of v3. Hope that makes things right :)
thanks,
Cong
Quoted 169 lines
Thanks,
GuenterQuoted 164 lines
--
commit
c1b61438a4a40096fe54d7b86037b9d5eebc1cae
Author: Cong Nguyen <[email protected]>hwmon: (gpio-fan) fix
pm_runtimeimbalance for alarm-only fans
pm_runtime_enable()runs unconditionally in probe, but the devm cleanup that callspm_runtime_disable()is only registered when control GPIOs are present. Alarm-only fans never get it, so unbind warns about the missing disable.Quoted 4 lines
drivers/hwmon/gpio-fan.c +0
-0[ ... ]
Quoted 4 lines
drivers/hwmon/gpio-fan.c +0
-0@@ -553,6 +556,17 @@ static int gpio_fan_probe(struct platform_device *pdev)return dev_err_probe (dev,PTR_ERR (fan_data->supply),"Failed to get fan-supply" );drivers/hwmon/gpio-fan.c static int gpio_fan_probe(struct platform_device *pdev) · line 568
Quoted 10 lines
drivers/hwmon/gpio-fan.c +10
-0+/* +* Reserve this devm slot before gpio_fan_stop()'s so LIFO teardown +* runs gpio_fan_stop() (needs PM enabled to disable the regulator) +* first. pm_runtime_enable() itself still happens at its original +* position below, so this doesn't change when PM actually becomes +* enabled -- only where its eventual disable is queued. +*/ + err =devm_add_action_or_reset (dev, gpio_fan_pm_runtime_disable, dev); +if (err) +return err;[Severity: High]
Does unconditionally registeringdevm_add_action_or_reset()early in the probe path cause an unbalancedpm_runtime_disable()on probe error paths?If probe fails at any intermediate point (e.g., in
fan_ctrl_init(),hwmon_device_register_with_groups(), orfan_alarm_init()), devm teardown will executegpio_fan_pm_runtime_disable(). Sincepm_runtime_enable()hasn't been reached yet, this incorrectly incrementsdev->power.disable_depth. On a subsequent probe attempt,pm_runtime_enable()will only decrement it to 1, leaving runtime PM permanently disabled.[Severity: High]
This isn't a bug introduced by this patch, but does failing to resume the device at the end of probe cause a usage count underflow during devm cleanup?
fan_ctrl_init()initializesfan_data->speed_indexbased on the hardware state. If the fan is active,speed_index> 0.gpio_fan_stop()is then queued as a devm action. If probe subsequently fails before the finalpm_runtime_resume_and_get()succeeds, the devm cleanup runsgpio_fan_stop(), which callsset_fan_speed():drivers/hwmon/gpio-fan.c:set_fan_speed() { ... if (fan_data->speed_index > 0 && speed_index == 0) { int ret; ret = pm_runtime_put_sync(fan_data->dev); if (ret < 0 && ret != -ENOSYS) return ret; } ... }Because
speed_index> 0,set_fan_speed()drops a PM reference that was never actually acquired, pushing the usage count below zero.[Severity: High]
This is a pre-existing issue, but does a race condition between probe and sysfs writes cause a double-increment of the runtime PM usage count?Sysfs attributes are exposed early by
devm_hwmon_device_register_with_groups(). Userspace can write to pwm1 right after probe callspm_runtime_enable(). The sysfs handler acquires a PM reference and setsspeed_index> 0.The probe thread then checks the state:
drivers/hwmon/gpio-fan.c:gpio_fan_probe() { ... /* 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) return ret; } ... }Since this check doesn't hold
fan_data->lock, probe sees the new value and takes a second PM reference. When userspace later stops the fan, only one reference is dropped, permanently blocking runtime suspend.[Severity: High]
This is a pre-existing issue, but issynchronize_irq()missing in the runtime suspend callback for a shared IRQ?The driver requests a shared interrupt for the alarm GPIO:
drivers/hwmon/gpio-fan.c:fan_alarm_init() { ... return devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler, IRQF_SHARED, "GPIO fan alarm", fan_data); }The PM subsystem guide mandates that drivers using
IRQF_SHAREDMUST callsynchronize_irq()in their runtime suspend callback before powering down hardware.Since this is missing, the IRQ handler could execute mid-flight while the regulator is being disabled in
gpio_fan_runtime_suspend():drivers/hwmon/gpio-fan.c:gpio_fan_runtime_suspend() { ... if (fan_data->supply) ret = regulator_disable(fan_data->supply); ... }[Severity: High]
This is a pre-existing issue, but does missingcancel_work_sync()foralarm_worklead to a use-after-free on driver unbind or probe failure?The driver initializes
fan_data->alarm_workand requests an interrupt to schedule it:drivers/hwmon/gpio-fan.c:fan_alarm_init() { ... INIT_WORK(&fan_data->alarm_work, fan_alarm_notify); ... return devm_request_irq(dev, alarm_irq, ...); }It fails to register a cleanup action to call
cancel_work_sync(). During driver unbind or probe failure,devm_request_irqis unwound, which synchronizes with running handlers but does not cancel pending work.fan_datais then freed by devm. If an interrupt fired right before cleanup, the work executes asynchronously and dereferences the freedfan_datapointer.[Severity: High]
This is a pre-existing issue, but does the shared interrupt handler return an unconditionalIRQ_NONE?drivers/hwmon/gpio-fan.c:fan_alarm_irq_handler() { ... schedule_work(&fan_data->alarm_work); return IRQ_NONE; }The driver requests an
IRQF_SHAREDinterrupt. In its handler, it processes the event but unconditionally returnsIRQ_NONE. When the kernel seesIRQ_NONEfrom all handlers on a shared line, it registers an unhandled interrupt. After enough unhandled interrupts, the kernel disables the IRQ line permanently, breaking all devices sharing it.Quoted 4 lines
drivers/hwmon/gpio-fan.c +1
-0+/* Configure control GPIOs if available. */ if (fan_data->gpios && fan_data->num_gpios >0 ) {if (!fan_data->speed || fan_data->num_speed <=1 )