[PATCH 5/7] sched/wait: always queue new waiters at the tail

From: Linus Torvalds

Date: Tue Nov 24 2026 - 12:46:43 EST


From: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 23 Nov 2026 16:48:44 -0800
Subject: [PATCH 5/7] sched/wait: always queue new waiters at the tail

Non-exclusive waiters have been added at the head and exclusive ones at
the tail, so that an exclusive wakeup stopping early still woke every
non-exclusive waiter.

That only matters for waiters that mix both kinds on one queue like
read() and poll() on a pipe - and even then it doesn't actually buy
anything but complexity and fragility of the code.

An exclusive reader consumes the event, the poller had nothing to see,
so there's no real point in keeping the two ordered.

The pointless ordering results in a special rule for everybody adding
themselves to the wait queue, and that rule has not been consistently
followed anyway (see for example do_wait_intr() and do_wait_intr_irq())

So just queue everyone in FIFO order. LIFO might be better for
throughput, but every time we've ever tried that optimization in real
life, it has caused fairness issues, so let's just do the obvious thing
and add new waiters at the end of the list.

Priority entries (used by the hypervisor code - questionable special
case) are still placed at the head by add_wait_queue_priority*().

And because epoll is a pile of stinking manure that I am afraid to touch
this leaves __add_wait_queue_exclusive() alone and it adds its entry at
the head for epoll's ep->wq so its own wakeup order doesn't change.

Now that it isn't a "real" exclusive wake event, its ordering thankfully
doesn't actually matter for anybody else. It should be fixed, but it's
now basically just an internal epoll thing (and that will be made more
obvious in following patches).

Cc: Christian Brauner <brauner@xxxxxxxxxx>
Cc: Oleg Nesterov <oleg@xxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Juri Lelli <juri.lelli@xxxxxxxxxx>
Cc: Vincent Guittot <vincent.guittot@xxxxxxxxxx>
Signed-off-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
---
include/linux/wait.h | 14 +++-----------
kernel/sched/wait.c | 29 ++++++++++++++++-------------
2 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/include/linux/wait.h b/include/linux/wait.h
index 09518f8ff10c..bf0a3713ed0f 100644
--- a/include/linux/wait.h
+++ b/include/linux/wait.h
@@ -171,25 +171,17 @@ extern void remove_wait_queue(struct
wait_queue_head *wq_head, struct wait_queue

static inline void __add_wait_queue(struct wait_queue_head *wq_head,
struct wait_queue_entry *wq_entry)
{
- struct list_head *head = &wq_head->head;
- struct wait_queue_entry *wq;
-
- list_for_each_entry(wq, &wq_head->head, entry) {
- if (!(wq->flags & WQ_FLAG_PRIORITY))
- break;
- head = &wq->entry;
- }
- list_add(&wq_entry->entry, head);
+ list_add_tail(&wq_entry->entry, &wq_head->head);
}

/*
- * Used for wake-one threads:
+ * Adds at the head, for epoll's LIFO wakeup of its epoll_wait()ers.
*/
static inline void
__add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct
wait_queue_entry *wq_entry)
{
wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
- __add_wait_queue(wq_head, wq_entry);
+ list_add(&wq_entry->entry, &wq_head->head);
}

static inline void __add_wait_queue_entry_tail(struct wait_queue_head
*wq_head, struct wait_queue_entry *wq_entry)
diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c
index 9f126cc5c338..a14e14202279 100644
--- a/kernel/sched/wait.c
+++ b/kernel/sched/wait.c
@@ -40,11 +40,18 @@ EXPORT_SYMBOL(add_wait_queue_exclusive);

void add_wait_queue_priority(struct wait_queue_head *wq_head, struct
wait_queue_entry *wq_entry)
{
+ struct list_head *head = &wq_head->head;
+ struct wait_queue_entry *wq;
unsigned long flags;

wq_entry->flags |= WQ_FLAG_PRIORITY;
spin_lock_irqsave(&wq_head->lock, flags);
- __add_wait_queue(wq_head, wq_entry);
+ list_for_each_entry(wq, &wq_head->head, entry) {
+ if (!(wq->flags & WQ_FLAG_PRIORITY))
+ break;
+ head = &wq->entry;
+ }
+ list_add(&wq_entry->entry, head);
spin_unlock_irqrestore(&wq_head->lock, flags);
}
EXPORT_SYMBOL_GPL(add_wait_queue_priority);
@@ -78,12 +85,12 @@ EXPORT_SYMBOL(remove_wait_queue);

/*
* The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
- * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
- * number) then we wake that number of exclusive tasks, and potentially all
- * the non-exclusive tasks. Normally, exclusive tasks will be at the end of
- * the list and any non-exclusive tasks will be woken first. A priority task
- * may be at the head of the list, and can consume the event without any other
- * tasks being woken if it's also an exclusive task.
+ * wake everything up. Otherwise the walk stops once nr_exclusive exclusive
+ * tasks have been woken. Waiters are queued in FIFO order, so non-exclusive
+ * waiters queued behind that point are not woken: the exclusive waiters will
+ * consume the event. A priority task may be at the head of the list, and can
+ * consume the event without any other tasks being woken if it's also an
+ * exclusive task.
*
* An exclusive waiter that returns >0 promises to handle the event. Pollers
* (EPOLLEXCLUSIVE) can't promise that, since userspace may never consume it.
@@ -308,12 +315,8 @@ long prepare_to_wait_event(struct wait_queue_head
*wq_head, struct wait_queue_en
list_del_init(&wq_entry->entry);
ret = -ERESTARTSYS;
} else {
- if (list_empty(&wq_entry->entry)) {
- if (wq_entry->flags & WQ_FLAG_EXCLUSIVE)
- __add_wait_queue_entry_tail(wq_head, wq_entry);
- else
- __add_wait_queue(wq_head, wq_entry);
- }
+ if (list_empty(&wq_entry->entry))
+ __add_wait_queue(wq_head, wq_entry);
set_current_state(state);
}
spin_unlock_irqrestore(&wq_head->lock, flags);
--
2.56.0.313.g975a75ca90