Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 removing the lockless check of dev->power.wakeirq.
Instead, acquire dev->power.lock immediately to ensure the check and
the subsequent operations are atomic. If dev->power.wakeirq is NULL
under the lock, simply unlock and return. This guarantees that
concurrent calls cannot race to free the same object.

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")
Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com>
Link: https://patch.msgid.link/20260203031943.1924-1-hanguidong02@gmail.com
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

authored by

Gui-Dong Han and committed by
Rafael J. Wysocki
5c9ecd8e 75ce02f4

+7 -4
+7 -4
drivers/base/power/wakeirq.c
··· 83 83 */ 84 84 void dev_pm_clear_wake_irq(struct device *dev) 85 85 { 86 - struct wake_irq *wirq = dev->power.wakeirq; 86 + struct wake_irq *wirq; 87 87 unsigned long flags; 88 88 89 - if (!wirq) 90 - return; 91 - 92 89 spin_lock_irqsave(&dev->power.lock, flags); 90 + wirq = dev->power.wakeirq; 91 + if (!wirq) { 92 + spin_unlock_irqrestore(&dev->power.lock, flags); 93 + return; 94 + } 95 + 93 96 device_wakeup_detach_irq(dev); 94 97 dev->power.wakeirq = NULL; 95 98 spin_unlock_irqrestore(&dev->power.lock, flags);