Skip to contentSkip to conversation

sched/wait: avoid modifying entry on failed priority registration

PATCH Applied Hui Su 3 participants 3 messages Oct 11 – Oct 15 Scheduler

lore
Share & tools
lore

Hui Su

author
#0 lore raw

PATCH 1/1 sched/wait: avoid modifying entry on failed priority registration

add_wait_queue_priority_exclusive() sets WQ_FLAG_EXCLUSIVE and WQ_FLAG_PRIORITY before checking whether another priority waiter is already present. Thus, when registration fails with -EBUSY, the entry is left unqueued but with modified flags.

There is no need to modify the entry before the failure check. Move the flag update after the check so that a rejected registration does not unnecessarily alter the entry.

---
Testing:
- Private x86_64/QEMU differential oracle: the old path modified the entry flags on -EBUSY; the patched path preserved them.

---
 kernel/sched/wait.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sched/wait.c +1 -2
index d033f600f48c..93b353fbd0c9 100644
@@ -54,14 +54,13 @@ int add_wait_queue_priority_exclusive(struct wait_queue_head *wq_head,
 {
 	struct list_head *head = &wq_head->head;
 
-	wq_entry->flags |= WQ_FLAG_EXCLUSIVE | WQ_FLAG_PRIORITY;
-
 	guard(spinlock_irqsave)(&wq_head->lock);
 
 	if (!list_empty(head) &&
 	    (list_first_entry(head, typeof(*wq_entry), entry)->flags & WQ_FLAG_PRIORITY))
 		return -EBUSY;
 
+	wq_entry->flags |= WQ_FLAG_EXCLUSIVE | WQ_FLAG_PRIORITY;
 	list_add(&wq_entry->entry, head);
 	return 0;
 }

base-commit: 3857c2fe5449541e24afc5efdb0f81a8a8f9a3a0

Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Hui Su

add_wait_queue_priority_exclusive() sets WQ_FLAG_EXCLUSIVE and WQ_FLAG_PRIORITY before checking whether another priority waiter is already present. Thus, when registration fails with -EBUSY, the entry is left unqueued but with modified flags.

There is no need to modify the entry before the failure check. Move the flag update after the check so that a rejected registration does not unnecessarily alter the entry.

In case anyone is concerned about this being a meaningful bug, kvm_irqfd_register() is the only user of add_wait_queue_priority_exclusive() and KVM never touches its entry if registration fails. I.e. this is completely benign in the current code base.

Quoted 17 lines

---
Testing:
- Private x86_64/QEMU differential oracle: the old path modified the entry flags on -EBUSY; the patched path preserved them.

---
 kernel/sched/wait.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sched/wait.c +0 -0
index d033f600f48c..93b353fbd0c9 100644
@@ -54,14 +54,13 @@ int add_wait_queue_priority_exclusive(struct wait_queue_head *wq_head,
 {
 	struct list_head *head = &wq_head->head;

kernel/sched/wait.c int add_wait_queue_priority_exclusive(struct wait_queue_head *wq_head, · line 63

Quoted 10 lines
kernel/sched/wait.c +1 -2
 
-	wq_entry->flags |= WQ_FLAG_EXCLUSIVE | WQ_FLAG_PRIORITY;
-
 	guard(spinlock_irqsave)(&wq_head->lock);
 
 	if (!list_empty(head) &&
 	    (list_first_entry(head, typeof(*wq_entry), entry)->flags & WQ_FLAG_PRIORITY))
 		return -EBUSY;
 
+	wq_entry->flags |= WQ_FLAG_EXCLUSIVE | WQ_FLAG_PRIORITY;

AFAICT, nothing peeks at wait queue entries outside of the spinlock (which is stating the obvious to some extent since there's no RCU in sight), so we're not relying on the above spinlock acquisition to order the flags update before the list_add().

Quoted 7 lines
kernel/sched/wait.c +0 -0
 	list_add(&wq_entry->entry, head);
 	return 0;
 }

base-commit: 3857c2fe5449541e24afc5efdb0f81a8a8f9a3a0

merge bot Applied
#2 lore raw

[tip: sched/core] sched/wait: Avoid modifying entry on failed priority registration

In reply to Hui Su
The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     159b172906110f736fdbd7af8a21d0e0b469f671
Gitweb:        https://git.kernel.org/tip/159b172906110f736fdbd7af8a21d0e0b469f671
Author:        Hui Su <[email protected]>
AuthorDate:    Sun, 11 Oct 2026 10:50:19 +09:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 13 Oct 2026 13:51:28 +02:00

sched/wait: Avoid modifying entry on failed priority registration

add_wait_queue_priority_exclusive() sets WQ_FLAG_EXCLUSIVE and WQ_FLAG_PRIORITY before checking whether another priority waiter is already present. Thus, when registration fails with -EBUSY, the entry is left unqueued but with modified flags.

There is no need to modify the entry before the failure check. Move the flag update after the check so that a rejected registration does not unnecessarily alter the entry.

---
 kernel/sched/wait.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/sched/wait.c +1 -2
index 477e4bf..f8d6d8b 100644
@@ -54,14 +54,13 @@ int add_wait_queue_priority_exclusive(struct wait_queue_head *wq_head,
 {
 	struct list_head *head = &wq_head->head;
 
-	wq_entry->flags |= WQ_FLAG_EXCLUSIVE | WQ_FLAG_PRIORITY;
-
 	guard(spinlock_irqsave)(&wq_head->lock);
 
 	if (!list_empty(head) &&
 	    (list_first_entry(head, typeof(*wq_entry), entry)->flags & WQ_FLAG_PRIORITY))
 		return -EBUSY;
 
+	wq_entry->flags |= WQ_FLAG_EXCLUSIVE | WQ_FLAG_PRIORITY;
 	list_add(&wq_entry->entry, head);
 	return 0;
 }