[PATCH] sched/swait: Reduce lock contention in swake_up_all

From: lirongqing

Date: Tue May 05 2026 - 05:05:22 EST


From: Li RongQing <lirongqing@xxxxxxxxx>

The entire task list have been moved a local list under the lock,
it is unnecessary to hold the lock to wake tasks, This reduces lock
operations from O(n) to O(1).

Move list_del_init before wake_up_state to prevent potential
use-after-free if the woken task exits immediately and releases
its memory.

Signed-off-by: Li RongQing <lirongqing@xxxxxxxxx>
---
kernel/sched/swait.c | 10 ++--------
1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/kernel/sched/swait.c b/kernel/sched/swait.c
index 0fef649..ee4e658 100644
--- a/kernel/sched/swait.c
+++ b/kernel/sched/swait.c
@@ -66,19 +66,13 @@ void swake_up_all(struct swait_queue_head *q)

raw_spin_lock_irq(&q->lock);
list_splice_init(&q->task_list, &tmp);
+ raw_spin_unlock_irq(&q->lock);
while (!list_empty(&tmp)) {
curr = list_first_entry(&tmp, typeof(*curr), task_list);

- wake_up_state(curr->task, TASK_NORMAL);
list_del_init(&curr->task_list);
-
- if (list_empty(&tmp))
- break;
-
- raw_spin_unlock_irq(&q->lock);
- raw_spin_lock_irq(&q->lock);
+ wake_up_state(curr->task, TASK_NORMAL);
}
- raw_spin_unlock_irq(&q->lock);
}
EXPORT_SYMBOL(swake_up_all);

--
2.9.4