RFC: sched: Prevent wakeup to enter critical section needlessly
Subsystems (2)
PATCH 1/1 RFC: sched: Prevent wakeup to enter critical section needlessly
Check the waitqueue task list to be non empty before entering the critical section. This prevents locking the spin lock needlessly in case the queue was empty, and therefor also prevent scheduling overhead on a PREEMPT_RT system.
---
Request for comments:
- Does this make any sense?
- I assume that I can safely use the list_empty_careful() function here, but is that correct?
Background to this patch:
Testing on a PREEMPT_RT system with TTY serial communication. Each time the TTY line discipline is dereferenced the Idle handling wait queue is woken up (see function put_ldisc in /drivers/tty/tty_ldisc.c)
However line discipline idle handling is not used very often so the wait queue is empty most of the time. But still the wake_up() function enters the critical section guarded by spin locks. This causes additional scheduling overhead when a lower priority thread has control of that same lock.
kernel/sched/core.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
kernel/sched/core.c +13 -3
@@ -3631,9 +3631,19 @@ void __wake_up(wait_queue_head_t *q, unsigned int mode, {unsigned long flags; -spin_lock_irqsave (&q->lock, flags); -__wake_up_common (q, mode, nr_exclusive,0 , key); -spin_unlock_irqrestore (&q->lock, flags); +/* +* We can check for list emptiness outside the lock by using the +* "careful" check that verifies both the next and prev pointers, so +* that there cannot be any half-pending updates in progress. +* +* This prevents the wake up to enter the critical section needlessly +* when the task list is empty. +*/ +if (!list_empty_careful (&q->task_list)) { +spin_lock_irqsave (&q->lock, flags); +__wake_up_common (q, mode, nr_exclusive,0 , key); +spin_unlock_irqrestore (&q->lock, flags); + } }EXPORT_SYMBOL (__wake_up);
Signature · 1 line
1.7.9.5
Check the waitqueue task list to be non empty before entering the critical section. This prevents locking the spin lock needlessly in case the queue was empty, and therefor also prevent scheduling overhead on a PREEMPT_RT system.
---
REPOST:
Request for comments:
- Does this make any sense?
- I assume that I can safely use the list_empty_careful() function here, but is that correct?
Background to this patch:
Testing on a PREEMPT_RT system with TTY serial communication. Each time the TTY line discipline is dereferenced the Idle handling wait queue is woken up (see function put_ldisc in /drivers/tty/tty_ldisc.c)
However line discipline idle handling is not used very often so the wait queue is empty most of the time. But still the wake_up() function enters the critical section guarded by spin locks. This causes additional scheduling overhead when a lower priority thread has control of that same lock.
kernel/sched/core.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
kernel/sched/core.c +13 -3
@@ -3090,9 +3090,19 @@ void __wake_up(wait_queue_head_t *q, unsigned int mode, {unsigned long flags; -spin_lock_irqsave (&q->lock, flags); -__wake_up_common (q, mode, nr_exclusive,0 , key); -spin_unlock_irqrestore (&q->lock, flags); +/* +* We can check for list emptiness outside the lock by using the +* "careful" check that verifies both the next and prev pointers, so +* that there cannot be any half-pending updates in progress. +* +* This prevents the wake up to enter the critical section needlessly +* when the task list is empty. +*/ +if (!list_empty_careful (&q->task_list)) { +spin_lock_irqsave (&q->lock, flags); +__wake_up_common (q, mode, nr_exclusive,0 , key); +spin_unlock_irqrestore (&q->lock, flags); + } }EXPORT_SYMBOL (__wake_up);
Signature · 1 line
1.7.9.5
Quoted 11 lines · Ivo Sieben
Check the waitqueue task list to be non empty before entering the critical section. This prevents locking the spin lock needlessly in case the queue was empty, and therefor also prevent scheduling overhead on a
PREEMPT_RTsystem.Signed-off-by Ivo Sieben <[email protected]>---
REPOST:
Request for comments:
- Does this make any sense?
Looks good to me. Avoiding any spinlock is good. I don't even think you need "careful", if you miss an update it was just as it happened a little later.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only
Peter Zijlstra
maintainerQuoted 10 lines · Andi Kleen
Quoted 8 lines · Ivo Sieben
Check the waitqueue task list to be non empty before entering the critical section. This prevents locking the spin lock needlessly in case the queue was empty, and therefor also prevent scheduling overhead on a
PREEMPT_RTsystem.Signed-off-by Ivo Sieben <[email protected]>---
Quoted 7 lines
Quoted 3 lines
REPOST:
Request for comments:
- Does this make any sense?Looks good to me. Avoiding any spinlock is good. I don't even think you need "careful", if you miss an update it was just as it happened a little later.
Yeah, so I've started looking at this several times, but never had the time to actually think it through. I think I'll agree with you that using the careful list empty check isn't needed.
In general there's already the race of doing a wakeup before the wait and if the code using the waitqueue doesn't deal with that its already broken, so in that respect you should be good, since you're simply shifting the timing a little.
One thing you might need to consider is the memory ordering, will the list_empty -- either careful or not -- observe the right list pointer, or could it -- when racing with wait_event()/prepare_to_wait() -- observe a stale value. Or.. is that all already covered in on the use site.
I started auditing a few users to see what they all assume, if they're already broken and or if they would now be broken.. but like said, I didn't get anywhere.
I'd like to see this patch/Changelog amended to at least cover these points so that I feel all warm and fuzzy when I read it and not have to think too hard ;-)
Quoted 6 lines · Peter Zijlstra
One thing you might need to consider is the memory ordering, will the
list_empty-- either careful or not -- observe the right list pointer, or could it -- when racing with wait_event()/prepare_to_wait() -- observe a stale value. Or.. is that all already covered in on the use site.
I agree.
Without spin_lock(q->lock) (or some other barriers) wait_event-like code can miss an event.
wait_event:
prepare_to_wait(wq) // takes wq->lock if (!CONDITION) schedule();
Now,
CONDITION = 1; wake_up(wq);
at least need the full mb() before lits_empty().
Oleg.
Quoted 7 lines
wait_event:prepare_to_wait(wq) // takes wq->lock if (!CONDITION) schedule();
Quoted 6 lines
Now,
CONDITION = 1; wake_up(wq);at least need the full
mb()beforelits_empty().
You're right, but it would probably only matter for inlining with LTO (if the LTO compiler ever decides to do that)
Without that a call should be always enough barrier in practice.
So yes I would add the mb, but most likely it will not make much difference. Just make sure to comment it.
-Andi
PATCH v2 1/1 sched: Prevent wakeup to enter critical section needlessly
Check the waitqueue task list to be non empty before entering the critical section. This prevents locking the spin lock needlessly in case the queue was empty, and therefor also prevent scheduling overhead on a PREEMPT_RT system.
---
v2:
- We don't need the "careful" list empty, a normal list empty is sufficient:
if you miss an update it was just as it happened a little later.
- Because of memory ordering problems we can observe an unupdated list administration. This can cause an wait_event-like code to miss an event. Adding a memory barrier befor checking the list to be empty will guarantee we evaluate a 100% updated list adminsitration.
kernel/sched/core.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
kernel/sched/core.c +16 -3
@@ -3090,9 +3090,22 @@ void __wake_up(wait_queue_head_t *q, unsigned int mode, {unsigned long flags; -spin_lock_irqsave (&q->lock, flags); -__wake_up_common (q, mode, nr_exclusive,0 , key); -spin_unlock_irqrestore (&q->lock, flags); +/* +* We check for list emptiness outside the lock. This prevents the wake +* up to enter the critical section needlessly when the task list is +* empty. +* +* Placed a full memory barrier before checking list emptiness to make +* 100% sure this function sees an up-to-date list administration. +* Note that other code that manipulates the list uses a spin_lock and +* therefore doesn't need additional memory barriers. +*/ +smp_mb (); +if (!list_empty (&q->task_list)) { +spin_lock_irqsave (&q->lock, flags); +__wake_up_common (q, mode, nr_exclusive,0 , key); +spin_unlock_irqrestore (&q->lock, flags); + } }EXPORT_SYMBOL (__wake_up);
Signature · 1 line
1.7.9.5
[REPOST-v2] sched: Prevent wakeup to enter critical section needlessly
Check the waitqueue task list to be non empty before entering the critical section. This prevents locking the spin lock needlessly in case the queue was empty, and therefor also prevent scheduling overhead on a PREEMPT_RT system.
---
repost:
Did I apply the memory barrier correct?
v2:
- We don't need the "careful" list empty, a normal list empty is sufficient:
if you miss an update it was just as it happened a little later.
- Because of memory ordering problems we can observe an unupdated list administration. This can cause an wait_event-like code to miss an event. Adding a memory barrier befor checking the list to be empty will guarantee we evaluate a 100% updated list adminsitration.
kernel/sched/core.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
kernel/sched/core.c +16 -3
@@ -3090,9 +3090,22 @@ void __wake_up(wait_queue_head_t *q, unsigned int mode, {unsigned long flags; -spin_lock_irqsave (&q->lock, flags); -__wake_up_common (q, mode, nr_exclusive,0 , key); -spin_unlock_irqrestore (&q->lock, flags); +/* +* We check for list emptiness outside the lock. This prevents the wake +* up to enter the critical section needlessly when the task list is +* empty. +* +* Placed a full memory barrier before checking list emptiness to make +* 100% sure this function sees an up-to-date list administration. +* Note that other code that manipulates the list uses a spin_lock and +* therefore doesn't need additional memory barriers. +*/ +smp_mb (); +if (!list_empty (&q->task_list)) { +spin_lock_irqsave (&q->lock, flags); +__wake_up_common (q, mode, nr_exclusive,0 , key); +spin_unlock_irqrestore (&q->lock, flags); + } }EXPORT_SYMBOL (__wake_up);
Signature · 1 line
1.7.9.5
[REPOST-v2] sched: Prevent wakeup to enter critical section needlessly
Check the waitqueue task list to be non empty before entering the critical section. This prevents locking the spin lock needlessly in case the queue was empty, and therefor also prevent scheduling overhead on a PREEMPT_RT system.
---
a second repost of this patch v2: Can anyone respond? Did I apply the memory barrier correct?
v2:
- We don't need the "careful" list empty, a normal list empty is sufficient:
if you miss an update it was just as it happened a little later.
- Because of memory ordering problems we can observe an unupdated list administration. This can cause an wait_event-like code to miss an event. Adding a memory barrier befor checking the list to be empty will guarantee we evaluate a 100% updated list adminsitration.
kernel/sched/core.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
kernel/sched/core.c +16 -3
@@ -3090,9 +3090,22 @@ void __wake_up(wait_queue_head_t *q, unsigned int mode, {unsigned long flags; -spin_lock_irqsave (&q->lock, flags); -__wake_up_common (q, mode, nr_exclusive,0 , key); -spin_unlock_irqrestore (&q->lock, flags); +/* +* We check for list emptiness outside the lock. This prevents the wake +* up to enter the critical section needlessly when the task list is +* empty. +* +* Placed a full memory barrier before checking list emptiness to make +* 100% sure this function sees an up-to-date list administration. +* Note that other code that manipulates the list uses a spin_lock and +* therefore doesn't need additional memory barriers. +*/ +smp_mb (); +if (!list_empty (&q->task_list)) { +spin_lock_irqsave (&q->lock, flags); +__wake_up_common (q, mode, nr_exclusive,0 , key); +spin_unlock_irqrestore (&q->lock, flags); + } }EXPORT_SYMBOL (__wake_up);
Signature · 1 line
1.7.9.5
Preeti U Murthy
Reviewed-byRe: [REPOST-v2] sched: Prevent wakeup to enter critical section needlessly
Hi Ivo,
Quoted 43 lines · Ivo Sieben
Check the waitqueue task list to be non empty before entering the critical section. This prevents locking the spin lock needlessly in case the queue was empty, and therefor also prevent scheduling overhead on a
PREEMPT_RTsystem.Signed-off-by Ivo Sieben <[email protected]>---
a second repost of this patch v2: Can anyone respond? Did I apply the memory barrier correct?
v2:
- We don't need the "careful" list empty, a normal list empty is sufficient:
if you miss an update it was just as it happened a little later.
- Because of memory ordering problems we can observe an unupdated list administration. This can cause an wait_event-like code to miss an event. Adding a memory barrier befor checking the list to be empty will guarantee we evaluate a 100% updated list adminsitration.kernel/sched/core.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)kernel/sched/core.c +9
-3@@ -3090,9 +3090,22 @@ void __wake_up(wait_queue_head_t *q, unsigned int mode, {unsigned long flags; -spin_lock_irqsave (&q->lock, flags); -__wake_up_common (q, mode, nr_exclusive,0 , key); -spin_unlock_irqrestore (&q->lock, flags); +/* +* We check for list emptiness outside the lock. This prevents the wake +* up to enter the critical section needlessly when the task list is +* empty. +* +* Placed a full memory barrier before checking list emptiness to make +* 100% sure this function sees an up-to-date list administration. +* Note that other code that manipulates the list uses a spin_lock and +* therefore doesn't need additional memory barriers.
kernel/sched/core.c void __wake_up(wait_queue_head_t *q, unsigned int mode, · line 3111
Quoted 10 lines
kernel/sched/core.c +7
-0+*/ +smp_mb (); +if (!list_empty (&q->task_list)) { +spin_lock_irqsave (&q->lock, flags); +__wake_up_common (q, mode, nr_exclusive,0 , key); +spin_unlock_irqrestore (&q->lock, flags); + } }EXPORT_SYMBOL (__wake_up);
Looks good to me.
Regards
Preeti U Murthy
Re: [REPOST-v2] sched: Prevent wakeup to enter critical section needlessly
In reply to Ivo SiebenQuoted 12 lines · Ivo Sieben
kernel/sched/core.c +2
-3@@ -3090,9 +3090,22 @@ void __wake_up(wait_queue_head_t *q, unsigned int mode, {unsigned long flags; -spin_lock_irqsave (&q->lock, flags); -__wake_up_common (q, mode, nr_exclusive,0 , key); -spin_unlock_irqrestore (&q->lock, flags); +/* +* We check for list emptiness outside the lock. This prevents the wake
kernel/sched/core.c void __wake_up(wait_queue_head_t *q, unsigned int mode, · line 3104
Quoted 10 lines
kernel/sched/core.c +10
-0+* up to enter the critical section needlessly when the task list is +* empty. +* +* Placed a full memory barrier before checking list emptiness to make +* 100% sure this function sees an up-to-date list administration. +* Note that other code that manipulates the list uses a spin_lock and +* therefore doesn't need additional memory barriers. +*/ +smp_mb (); +if (!list_empty (&q->task_list)) {
waitqueue_active() ?
kernel/sched/core.c void __wake_up(wait_queue_head_t *q, unsigned int mode, · line 3108
Quoted 4 lines
kernel/sched/core.c +4
-0+spin_lock_irqsave (&q->lock, flags); +__wake_up_common (q, mode, nr_exclusive,0 , key); +spin_unlock_irqrestore (&q->lock, flags); + }
I am wondering if it makes sense unconditionally. A lot of callers do
if (waitqueue_active(q)) wake_up(...);
this patch makes the optimization above pointless and adds mb().
But I won't argue.
Oleg.
Re: [REPOST-v2] sched: Prevent wakeup to enter critical section needlessly
Hi
2012/11/19 Oleg Nesterov <[email protected]>:
Quoted 6 lines
I am wondering if it makes sense unconditionally. A lot of callers do
if (waitqueue_active(q)) wake_up(...);
Quoted 7 lines
this patch makes the optimization above pointless and adds
mb().But I won't argue.
Oleg.
This patch solved an issue for me that I had with the TTY line discipline idle handling:
Testing on a PREEMPT_RT system with TTY serial communication. Each time the TTY line discipline is dereferenced the Idle handling wait queue is woken up (see function put_ldisc in /drivers/tty/tty_ldisc.c) However line discipline idle handling is not used very often so the wait queue is empty most of the time. But still the wake_up() function enters the critical section guarded by spin locks. This causes additional scheduling overhead when a lower priority thread has control of that same lock.
The /drivers/tty/tty_ldisc.c did not use the waitqueue_active() call to check if the waitqueue was filled.... maybe I should solve this problem the other way around: and make tty_ldisc.c first do the waitqueue_active() call?
Regards,
Ivo
Re: [REPOST-v2] sched: Prevent wakeup to enter critical section needlessly
Quoted 29 lines · Ivo Sieben
Hi
2012/11/19 Oleg Nesterov <[email protected]>:
Quoted 6 lines
I am wondering if it makes sense unconditionally. A lot of callers do
if (waitqueue_active(q)) wake_up(...);Quoted 7 lines
this patch makes the optimization above pointless and adds
mb().But I won't argue.
Oleg.
This patch solved an issue for me that I had with the TTY line discipline idle handling:
Testing on aPREEMPT_RTsystem with TTY serial communication. Each time the TTY line discipline is dereferenced the Idle handling wait queue is woken up (see functionput_ldiscin /drivers/tty/tty_ldisc.c) However line discipline idle handling is not used very often so the wait queue is empty most of the time. But still thewake_up()function enters the critical section guarded by spin locks. This causes additional scheduling overhead when a lower priority thread has control of that same lock.
Quoted 4 lines
The /drivers/tty/tty_ldisc.c did not use the
waitqueue_active()call to check if the waitqueue was filled.... maybe I should solve this problem the other way around: and maketty_ldisc.cfirst do thewaitqueue_active()call?
IMHO yes...
Because on a second thought I suspect this change is wrong.
Just for example, please look at kauditd_thread(). It does
set_current_state(TASK_INTERRUPTIBLE); add_wait_queue(&kauditd_wait, &wait); if (!CONDITION) // <-- LOAD schedule();
And the last LOAD can leak into the critical section protected by wait_queue_head_t->lock, and it can be reordered with list_add() inside this critical section. In this case we can race with wake_up() unless it takes the same lock.
Oleg.
Re: [REPOST-v2] sched: Prevent wakeup to enter critical section needlessly
Hi
2012/11/19 Oleg Nesterov <[email protected]>:
Quoted 12 lines
Because on a second thought I suspect this change is wrong.
Just for example, please look at
kauditd_thread(). It doesset_current_state(TASK_INTERRUPTIBLE); add_wait_queue(&kauditd_wait, &wait); if (!CONDITION) // <-- LOAD schedule();
Quoted 7 lines
And the last LOAD can leak into the critical section protected by
wait_queue_head_t->lock, and it can be reordered withlist_add()inside this critical section. In this case we can race withwake_up()unless it takes the same lock.Oleg.
I agree that I should solve my problem using the waitqueue_active() function locally. I'll abandon this patch and fix it in the tty_ldisc.c.
But we try to understand your fault scenario: How can the LOAD leak into the critical section? As far as we understand the spin_unlock() function also contains a memory barrier to prevent such a reordering from happening.
Regards,
Ivo
Re: [REPOST-v2] sched: Prevent wakeup to enter critical section needlessly
Quoted 4 lines
But we try to understand your fault scenario: How can the LOAD leak into the critical section? As far as we understand the
spin_unlock()function also contains a memory barrier to prevent such a reordering from happening.
It does - it would be very interesting for someone to look at the assembly if this is making a difference.
Alan
Re: [REPOST-v2] sched: Prevent wakeup to enter critical section needlessly
In reply to Ivo SiebenQuoted 23 lines · Ivo Sieben
Hi
2012/11/19 Oleg Nesterov <[email protected]>:
Quoted 12 lines
Because on a second thought I suspect this change is wrong.
Just for example, please look at
kauditd_thread(). It doesset_current_state(TASK_INTERRUPTIBLE); add_wait_queue(&kauditd_wait, &wait); if (!CONDITION) // <-- LOAD schedule();Quoted 7 lines
And the last LOAD can leak into the critical section protected by
wait_queue_head_t->lock, and it can be reordered withlist_add()inside this critical section. In this case we can race withwake_up()unless it takes the same lock.Oleg.
Quoted 7 lines
I agree that I should solve my problem using the
waitqueue_active()function locally. I'll abandon this patch and fix it in thetty_ldisc.c.But we try to understand your fault scenario: How can the LOAD leak into the critical section? As far as we understand the
spin_unlock()function also contains a memory barrier
^^^^^^^^^^^^^^
Not really, in general unlock is a one-way barrier.
Quoted 2 lines
to prevent such a reordering
from happening.
Please look at the comment above prepare_to_wait(), for example. Or look at wmb() in try_to_wake_up().
I guess this is not possible on x86, but in general
X; LOCK(); UNLOCK(); Y;
can be reordered as
LOCK(); Y; X; UNLOCK();
UNLOCK + LOCK is the full memory barrier.
Oleg.