PM: Delete timer before removing wakeup source from list
Subsystems (4)
PATCH 1/1 PM: Delete timer before removing wakeup source from list
Move timer_delete_sync() before list_del_rcu() in wakeup_source_remove() to ensure proper cleanup ordering. This prevents the timer callback from executing after the wakeup source has been removed from the events list.
The previous order could allow the timer callback to access the wakeup source entry after removal but before timer deletion, potentially causing use-after-free issues or list corruption.
Deleting the timer first ensures that no callbacks can execute during the wakeup source removal process, providing safer cleanup semantics.
--- drivers/base/power/wakeup.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/base/power/wakeup.c +1 -2
@@ -189,12 +189,11 @@ static void wakeup_source_remove(struct wakeup_source *ws)if (WARN_ON (!ws))return ; +timer_delete_sync (&ws->timer);raw_spin_lock_irqsave (&events_lock, flags);list_del_rcu (&ws->entry);raw_spin_unlock_irqrestore (&events_lock, flags);synchronize_srcu (&wakeup_srcu); - -timer_delete_sync (&ws->timer);/* * Clear timer.function to make wakeup_source_not_registered() treat * this wakeup source as not registered.
Signature · 1 line
2.34.1
Rafael J. Wysocki
maintainerQuoted 8 lines · Kaushlendra Kumar
Move
timer_delete_sync()beforelist_del_rcu()inwakeup_source_remove()to ensure proper cleanup ordering. This prevents the timer callback from executing after the wakeup source has been removed from the events list.The previous order could allow the timer callback to access the wakeup source entry after removal but before timer deletion, potentially causing use-after-free issues or list corruption.
How so? You need to specify the scenario in which that can happen.
Quoted 29 lines
Deleting the timer first ensures that no callbacks can execute during the wakeup source removal process, providing safer cleanup semantics.
Signed-off-by Kaushlendra Kumar <[email protected]>--- drivers/base/power/wakeup.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/base/power/wakeup.c +1
-3@@ -189,12 +189,11 @@ static void wakeup_source_remove(struct wakeup_source *ws)if (WARN_ON (!ws))return ; +timer_delete_sync (&ws->timer);raw_spin_lock_irqsave (&events_lock, flags);list_del_rcu (&ws->entry);raw_spin_unlock_irqrestore (&events_lock, flags);synchronize_srcu (&wakeup_srcu); - -timer_delete_sync (&ws->timer);/* * Clear timer.function to make wakeup_source_not_registered() treat * this wakeup source as not registered. -- 2.34.1
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines · Kaushlendra Kumar
Move
timer_delete_sync()beforelist_del_rcu()inwakeup_source_remove()to ensure proper cleanup ordering. This
prevents the timer callback from executing after the wakeup source has been removed from the events list.The previous order could allow the timer callback to access the wakeup source entry after removal but before timer deletion, potentially causing use-after-free issues or list corruption.
How so? You need to specify the scenario in which that can happen.
Hi Rafael,
Thank you for asking for clarification. Here's the specific scenario where the it can occur:
**Issue Condition Timeline:**
1. **Thread A** calls wakeup_source_remove():
raw_spin_lock_irqsave(&events_lock, flags); list_del_rcu(&ws->entry); // Remove from events list raw_spin_unlock_irqrestore(&events_lock, flags); synchronize_srcu(&wakeup_srcu); // Wait for RCU readers // *** ISSUE WINDOW HERE *** timer_delete_sync(&ws->timer); // Timer still active!
2. **Thread B** (timer callback) fires during the issue window:
static void pm_wakeup_timer_fn(struct timer_list *t) { struct wakeup_source *ws = from_timer(ws, t, timer); // Problem: ws->entry was already removed from events list
// but timer callback still executes
**Specific Issues:**
**Timer accesses removed wakeup_source**: The timer callback (pm_wakeup_timer_fn) can execute after list_del_rcu() but before timer_delete_sync(), accessing a wakeup_source that's no longer in the events list.
**Why moving timer_delete_sync() first fixes this:**
- Ensures timer cannot fire during list removal
- Guarantees no timer callbacks execute after we start cleanup
The fix ensures that once we start wakeup_source_remove(), no timer callbacks can execute
Rafael J. Wysocki
maintainerQuoted 23 lines · Kaushlendra
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines · Kaushlendra Kumar
Move
timer_delete_sync()beforelist_del_rcu()inwakeup_source_remove()to ensure proper cleanup ordering. This
prevents the timer callback from executing after the wakeup source has been removed from the events list.The previous order could allow the timer callback to access the wakeup source entry after removal but before timer deletion, potentially causing use-after-free issues or list corruption.
How so? You need to specify the scenario in which that can happen.
Hi Rafael,
Thank you for asking for clarification. Here's the specific scenario where the it can occur:
**Issue Condition Timeline:**
1. **Thread A** calls
wakeup_source_remove():
Quoted 7 lines
raw_spin_lock_irqsave(&events_lock, flags);list_del_rcu(&ws->entry); // Remove from events listraw_spin_unlock_irqrestore(&events_lock, flags);synchronize_srcu(&wakeup_srcu); // Wait for RCU readers // *** ISSUE WINDOW HERE ***timer_delete_sync(&ws->timer); // Timer still active!2. **Thread B** (timer callback) fires during the issue window:
static void
pm_wakeup_timer_fn(struct timer_list *t){ structwakeup_source*ws =from_timer(ws, t, timer); // Problem:ws->entrywas already removed from events list
// but timer callback still executes
Why is this a problem? ws is still there and the timer callback doesn't do anything with ws->entry AFAICS.
Quoted 6 lines
**Specific Issues:**
**Timer accesses removed wakeup_source**: The timer callback (
pm_wakeup_timer_fn) can execute afterlist_del_rcu()but beforetimer_delete_sync(), accessing awakeup_sourcethat's no longer in the events list.
Again, why is this a problem?
Quoted 7 lines
**Why moving
timer_delete_sync()first fixes this:**- Ensures timer cannot fire during list removal
- Guarantees no timer callbacks execute after we start cleanupThe fix ensures that once we start
wakeup_source_remove(), no timer callbacks can execute
You may argue that the new code will be cleaner (fair enough), but unless there is a specific correctness matter here, you can't call it a fix.
Quoted 45 lines · Rafael J. Wysocki
Quoted 23 lines · Kaushlendra
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines · Kaushlendra Kumar
Move
timer_delete_sync()beforelist_del_rcu()inwakeup_source_remove()to ensure proper cleanup ordering. This
prevents the timer callback from executing after the wakeup source has been removed from the events list.The previous order could allow the timer callback to access the wakeup source entry after removal but before timer deletion, potentially causing use-after-free issues or list corruption.
How so? You need to specify the scenario in which that can happen.
Hi Rafael,
Thank you for asking for clarification. Here's the specific scenario where the it can occur:
**Issue Condition Timeline:**
1. **Thread A** calls
wakeup_source_remove():Quoted 7 lines
raw_spin_lock_irqsave(&events_lock, flags);list_del_rcu(&ws->entry); // Remove from events listraw_spin_unlock_irqrestore(&events_lock, flags);synchronize_srcu(&wakeup_srcu); // Wait for RCU readers // *** ISSUE WINDOW HERE ***timer_delete_sync(&ws->timer); // Timer still active!2. **Thread B** (timer callback) fires during the issue window:
static void
pm_wakeup_timer_fn(struct timer_list *t){ structwakeup_source*ws =from_timer(ws, t, timer); // Problem:ws->entrywas already removed from events list
// but timer callback still executesWhy is this a problem? ws is still there and the timer callback doesn't do anything with
ws->entryAFAICS.Quoted 6 lines
**Specific Issues:**
**Timer accesses removed wakeup_source**: The timer callback (
pm_wakeup_timer_fn) can execute afterlist_del_rcu()but beforetimer_delete_sync(), accessing awakeup_sourcethat's no longer in the events list.Again, why is this a problem?
Quoted 2 lines
**Why moving
timer_delete_sync()first fixes this:**
Quoted 7 lines
Quoted 5 lines
- Ensures timer cannot fire during list removal
- Guarantees no timer callbacks execute after we start cleanupThe fix ensures that once we start
wakeup_source_remove(), no timer callbacks can executeYou may argue that the new code will be cleaner (fair enough), but unless there is a specific correctness matter here, you can't call it a fix.
Hi Rafael,
You're absolutely correct, I don't have any evidence in real use cases for this. Given your feedback, I should Reframe it as a cleanup/improvement patch rather than a "fix"
If it is fine with you then I can create v2 patch with your suggestions.
Best regards,
Kaushlendra
Rafael J. Wysocki
maintainerQuoted 53 lines · Kaushlendra
Quoted 51 lines · Rafael J. Wysocki
Quoted 23 lines · Kaushlendra
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines · Kaushlendra Kumar
Move
timer_delete_sync()beforelist_del_rcu()inwakeup_source_remove()to ensure proper cleanup ordering. This
prevents the timer callback from executing after the wakeup source has been removed from the events list.The previous order could allow the timer callback to access the wakeup source entry after removal but before timer deletion, potentially causing use-after-free issues or list corruption.
How so? You need to specify the scenario in which that can happen.
Hi Rafael,
Thank you for asking for clarification. Here's the specific scenario where the it can occur:
**Issue Condition Timeline:**
1. **Thread A** calls
wakeup_source_remove():Quoted 7 lines
raw_spin_lock_irqsave(&events_lock, flags);list_del_rcu(&ws->entry); // Remove from events listraw_spin_unlock_irqrestore(&events_lock, flags);synchronize_srcu(&wakeup_srcu); // Wait for RCU readers // *** ISSUE WINDOW HERE ***timer_delete_sync(&ws->timer); // Timer still active!2. **Thread B** (timer callback) fires during the issue window:
static void
pm_wakeup_timer_fn(struct timer_list *t){ structwakeup_source*ws =from_timer(ws, t, timer); // Problem:ws->entrywas already removed from events list
// but timer callback still executesWhy is this a problem? ws is still there and the timer callback doesn't do anything with
ws->entryAFAICS.Quoted 6 lines
**Specific Issues:**
**Timer accesses removed wakeup_source**: The timer callback (
pm_wakeup_timer_fn) can execute afterlist_del_rcu()but beforetimer_delete_sync(), accessing awakeup_sourcethat's no longer in the events list.Again, why is this a problem?
Quoted 7 lines
**Why moving
timer_delete_sync()first fixes this:**- Ensures timer cannot fire during list removal
- Guarantees no timer callbacks execute after we start cleanupThe fix ensures that once we start
wakeup_source_remove(), no timer callbacks can execute
Quoted 8 lines
Quoted 1 line
You may argue that the new code will be cleaner (fair enough), but unless there is a specific correctness matter here, you can't call it a fix.
Hi Rafael,
You're absolutely correct, I don't have any evidence in real use cases for this. Given your feedback, I should Reframe it as a cleanup/improvement patch rather than a "fix"
If it is fine with you then I can create v2 patch with your suggestions.
Yes, please!
Quoted 56 lines · Rafael J. Wysocki
Quoted 55 lines · Kaushlendra
Quoted 52 lines · Rafael J. Wysocki
Quoted 23 lines · Kaushlendra
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines · Kaushlendra Kumar
Move
timer_delete_sync()beforelist_del_rcu()inwakeup_source_remove()to ensure proper cleanup ordering. This
prevents the timer callback from executing after the wakeup source has been removed from the events list.The previous order could allow the timer callback to access the wakeup source entry after removal but before timer deletion, potentially causing use-after-free issues or list corruption.
How so? You need to specify the scenario in which that can happen.
Hi Rafael,
Thank you for asking for clarification. Here's the specific scenario where the it can occur:
**Issue Condition Timeline:**
1. **Thread A** calls
wakeup_source_remove():Quoted 7 lines
raw_spin_lock_irqsave(&events_lock, flags);list_del_rcu(&ws->entry); // Remove from events listraw_spin_unlock_irqrestore(&events_lock, flags);synchronize_srcu(&wakeup_srcu); // Wait for RCU readers // *** ISSUE WINDOW HERE ***timer_delete_sync(&ws->timer); // Timer still active!2. **Thread B** (timer callback) fires during the issue window:
static void
pm_wakeup_timer_fn(struct timer_list *t){ structwakeup_source*ws =from_timer(ws, t, timer); // Problem:ws->entrywas already removed from events list
// but timer callback still executesWhy is this a problem? ws is still there and the timer callback doesn't do anything with
ws->entryAFAICS.Quoted 6 lines
**Specific Issues:**
**Timer accesses removed wakeup_source**: The timer callback (
pm_wakeup_timer_fn) can execute afterlist_del_rcu()but beforetimer_delete_sync(), accessing awakeup_sourcethat's no longer in the events list.Again, why is this a problem?
Quoted 7 lines
**Why moving
timer_delete_sync()first fixes this:**- Ensures timer cannot fire during list removal
- Guarantees no timer callbacks execute after we start cleanupThe fix ensures that once we start
wakeup_source_remove(), no timer callbacks can executeYou may argue that the new code will be cleaner (fair enough), but unless there is a specific correctness matter here, you can't call it a fix.
Quoted 8 lines
Quoted 6 lines
Hi Rafael,
You're absolutely correct, I don't have any evidence in real use cases
for this. Given your feedback, I should Reframe it as a cleanup/improvement patch rather than a "fix"If it is fine with you then I can create v2 patch with your suggestions.
Yes, please!
Hi Rafael,
Thank you for the approval!. I have sent a v2 patch with the proper framing as a code improvement
Best regards,
Kaushlendra