Skip to contentSkip to conversation

PM: sleep: wakeirq: harden dev_pm_clear_wake_irq() against races

PATCH Superseded Gui-Dong Han 3 participants 3 messages Jan 31 – Feb 2

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PM: sleep: wakeirq: harden dev_pm_clear_wake_irq() against races

dev_pm_clear_wake_irq() currently uses a dangerous pattern where dev->power.wakeirq is read and checked for NULL outside the lock. If two callers invoke this function concurrently, both might see a valid pointer and proceed. This could result in a double-free when the second caller acquires the lock and tries to release the same object.

Address this by using double-checked locking. This retains the performance benefit of avoiding the lock when dev->power.wakeirq is NULL, consistent with the original logic, but adds a necessary re-check after acquiring dev->power.lock. Additionally, use READ_ONCE() and WRITE_ONCE() to annotate the shared variable accesses to avoid data races as defined by the kernel documentation.

Based on a quick scan of current users, I did not find an actual bug as drivers seem to rely on their own synchronization. However, since asynchronous usage patterns exist (e.g., in drivers/net/wireless/ti/wlcore), I believe a race is theoretically possible if the API is used less carefully in the future. This change hardens the API to be robust against such cases.

Fixes 4990d4fe327b ("PM / Wakeirq: Add automated device wake IRQ handling")

---
While studying wakeirq, I noticed dev_pm_clear_wake_irq() handles sequential re-entry (via the NULL check) but may lead to a double-free on concurrent calls.

I considered whether we should simply document that concurrent calls are forbidden. However, since the double-check locking pattern is straightforward and adds negligible performance overhead (we still skip the lock in the NULL case), I believe hardening the API is the better approach.

I also noticed comments for dev_pm_enable_wake_irq_check() and friends appear outdated. They claim "Caller must hold &dev->power.lock" and limit usage to rpm_suspend/resume, yet pm_runtime_force_suspend/resume() now call them lockless. While this usage appears safe due to the specific context, it conflicts with the comments.

I can submit a follow-up patch to fix this doc drift but am unsure whether to simply remove the restriction text or complicate it with exceptions. Guidance is welcome.

---
 drivers/base/power/wakeirq.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/base/power/wakeirq.c +9 -3
index 8aa28c08b289..acb520626195 100644
@@ -30,7 +30,7 @@ static int dev_pm_attach_wake_irq(struct device *dev, struct wake_irq *wirq)
 		return -EEXIST;
 	}
 
-	dev->power.wakeirq = wirq;
+	WRITE_ONCE(dev->power.wakeirq, wirq);
 	device_wakeup_attach_irq(dev, wirq);
 
 	spin_unlock_irqrestore(&dev->power.lock, flags);
@@ -83,15 +83,21 @@ EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
  */
 void dev_pm_clear_wake_irq(struct device *dev)
 {
-	struct wake_irq *wirq = dev->power.wakeirq;
+	struct wake_irq *wirq = READ_ONCE(dev->power.wakeirq);
 	unsigned long flags;
 
 	if (!wirq)
 		return;
 
 	spin_lock_irqsave(&dev->power.lock, flags);
+	wirq = dev->power.wakeirq;
+	if (!wirq) {
+		spin_unlock_irqrestore(&dev->power.lock, flags);
+		return;
+	}
+
 	device_wakeup_detach_irq(dev);
-	dev->power.wakeirq = NULL;
+	WRITE_ONCE(dev->power.wakeirq, NULL);
 	spin_unlock_irqrestore(&dev->power.lock, flags);
 
 	if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) {
Signature · 1 line
2.43.0
#1 lore raw

…

Quoted 6 lines

Address this by using double-checked locking. This retains the performance benefit of avoiding the lock when dev->power.wakeirq is NULL, consistent with the original logic, but adds a necessary re-check after acquiring dev->power.lock. Additionally, use READ_ONCE() and WRITE_ONCE() to annotate the shared variable accesses to avoid data races as defined by the kernel documentation.

…

How do you think about to apply lock guards in the affected function implementations?

Regards,
Markus

#2 lore raw
In reply to Gui-Dong Han
Quoted 61 lines · Gui-Dong Han

dev_pm_clear_wake_irq() currently uses a dangerous pattern where dev->power.wakeirq is read and checked for NULL outside the lock. If two callers invoke this function concurrently, both might see a valid pointer and proceed. This could result in a double-free when the second caller acquires the lock and tries to release the same object.

Address this by using double-checked locking. This retains the performance benefit of avoiding the lock when dev->power.wakeirq is NULL, consistent with the original logic, but adds a necessary re-check after acquiring dev->power.lock. Additionally, use READ_ONCE() and WRITE_ONCE() to annotate the shared variable accesses to avoid data races as defined by the kernel documentation.

Based on a quick scan of current users, I did not find an actual bug as drivers seem to rely on their own synchronization. However, since asynchronous usage patterns exist (e.g., in drivers/net/wireless/ti/wlcore), I believe a race is theoretically possible if the API is used less carefully in the future. This change hardens the API to be robust against such cases.

Fixes 4990d4fe327b ("PM / Wakeirq: Add automated device wake IRQ handling")

---
While studying wakeirq, I noticed dev_pm_clear_wake_irq() handles sequential re-entry (via the NULL check) but may lead to a double-free on concurrent calls.

I considered whether we should simply document that concurrent calls are forbidden. However, since the double-check locking pattern is straightforward and adds negligible performance overhead (we still skip the lock in the NULL case), I believe hardening the API is the better approach.

I also noticed comments for dev_pm_enable_wake_irq_check() and friends appear outdated. They claim "Caller must hold &dev->power.lock" and limit usage to rpm_suspend/resume, yet pm_runtime_force_suspend/resume() now call them lockless. While this usage appears safe due to the specific context, it conflicts with the comments.

I can submit a follow-up patch to fix this doc drift but am unsure whether to simply remove the restriction text or complicate it with exceptions. Guidance is welcome.

---
 drivers/base/power/wakeirq.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/base/power/wakeirq.c +1 -1
index 8aa28c08b289..acb520626195 100644
@@ -30,7 +30,7 @@ static int dev_pm_attach_wake_irq(struct device *dev, struct wake_irq *wirq)
                return -EEXIST;
        }
 
-       dev->power.wakeirq = wirq;
+       WRITE_ONCE(dev->power.wakeirq, wirq);
        device_wakeup_attach_irq(dev, wirq);
 
        spin_unlock_irqrestore(&dev->power.lock, flags);

drivers/base/power/wakeirq.c EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq); · line 86

Quoted 5 lines
drivers/base/power/wakeirq.c +1 -1
@@ -83,15 +83,21 @@ EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq);
  */
 void dev_pm_clear_wake_irq(struct device *dev)
 {
-       struct wake_irq *wirq = dev->power.wakeirq;
+       struct wake_irq *wirq = READ_ONCE(dev->power.wakeirq);

Just remove the dev->power.wakeirq access from here.

drivers/base/power/wakeirq.c EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq); · line 90

Quoted 4 lines
drivers/base/power/wakeirq.c +0 -0
        unsigned long flags;
 
        if (!wirq)
                return;

Along with the check above because it is still racy.

drivers/base/power/wakeirq.c EXPORT_SYMBOL_GPL(dev_pm_set_wake_irq); · line 97

Quoted 6 lines
drivers/base/power/wakeirq.c +5 -0
        spin_lock_irqsave(&dev->power.lock, flags);
+       wirq = dev->power.wakeirq;
+       if (!wirq) {
+               spin_unlock_irqrestore(&dev->power.lock, flags);
+               return;
+       }

And just do the above.

WRITE_ONCE() will not be needed then.

Quoted 8 lines
drivers/base/power/wakeirq.c +2 -2
+
        device_wakeup_detach_irq(dev);
-       dev->power.wakeirq = NULL;
+       WRITE_ONCE(dev->power.wakeirq, NULL);
        spin_unlock_irqrestore(&dev->power.lock, flags);
 
        if (wirq->status & WAKE_IRQ_DEDICATED_ALLOCATED) {
--