Skip to contentSkip to conversation

PM: Delete timer before removing wakeup source from list

PATCH Superseded Kaushlendra Kumar 2 participants 7 messages Sep 21, 2025 – Sep 24, 2025

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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
index d1283ff1080b..ae6ec9f04b61 100644
@@ -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
#1 lore raw
Quoted 8 lines · Kaushlendra Kumar

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.

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.

---
 drivers/base/power/wakeup.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/base/power/wakeup.c +1 -3
index d1283ff1080b..ae6ec9f04b61 100644
@@ -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

#2 lore raw
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines · Kaushlendra Kumar

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.

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

#3 lore raw
Quoted 23 lines · Kaushlendra
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines · Kaushlendra Kumar

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.

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 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

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 after list_del_rcu() but before timer_delete_sync(), accessing a wakeup_source that'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 cleanup

The 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.

#4 lore raw
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() 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.

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 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

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 after list_del_rcu() but before timer_delete_sync(), accessing a wakeup_source that'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 cleanup

The 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.

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

#5 lore raw
Quoted 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() 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.

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 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

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 after list_del_rcu() but before timer_delete_sync(), accessing a wakeup_source that'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 cleanup

The 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!

#6 lore raw
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() 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.

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 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

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 after list_del_rcu() but before timer_delete_sync(), accessing a wakeup_source that'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 cleanup

The 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 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