locking/semaphore: Use wake_q to wake up processes outside lock critical section
Waiman Long
reviewerPATCH v2 1/1 locking/semaphore: Use wake_q to wake up processes outside lock critical section
It was found that a circular lock dependency can happen with the following locking sequence:
+--> (console_sem).lock --> &p->pi_lock --> &rq->__lock --+ | | +---------------------------------------------------------+
The &p->pi_lock --> &rq->__lock sequence is very common in all the task_rq_lock() calls.
The &rq->__lock --> (console_sem).lock sequence happens when the scheduler code calling printk() or more likely the various WARN*() macros while holding the rq lock. The (console_sem).lock is actually a raw spinlock guarding the semaphore. In the particular lockdep splat that I saw, it was caused by SCHED_WARN_ON() call in update_rq_clock(). To work around this locking sequence, we may have to ban all WARN*() calls when the rq lock is held, which may be too restrictive, or we may have to add a WARN_DEFERRED() call and modify all the call sites to use it.
Even then, a deferred printk or WARN function may still call console_trylock() which may, in turn, calls up_console_sem() leading to this locking sequence.
The other ((console_sem).lock --> &p->pi_lock) locking sequence was caused by the fact that the semaphore up() function is calling wake_up_process() while holding the semaphore raw spinlock. This lockiing sequence can be easily eliminated by moving the wake_up_processs() call out of the raw spinlock critical section using wake_q which is what this patch implements. That is the easiest and the most certain way to break this circular locking sequence.
v1: https://lore.kernel.org/lkml/[email protected]/
--- kernel/locking/semaphore.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
kernel/locking/semaphore.c +9 -4
@@ -29,6 +29,7 @@#include <linux/export.h> #include <linux/sched.h> #include <linux/sched/debug.h> +#include <linux/sched/wake_q.h> #include <linux/semaphore.h> #include <linux/spinlock.h> #include <linux/ftrace.h>
@@ -38,7 +39,7 @@ static noinline void __down(struct semaphore *sem); static noinline int __down_interruptible(struct semaphore *sem); static noinline int __down_killable(struct semaphore *sem); static noinline int __down_timeout(struct semaphore *sem, long timeout); -static noinline void __up(struct semaphore *sem); +static noinline void __up(struct semaphore *sem, struct wake_q_head *wake_q);/** * down - acquire the semaphore
@@ -183,13 +184,16 @@ EXPORT_SYMBOL(down_timeout);void up (struct semaphore *sem) {unsigned long flags; +DEFINE_WAKE_Q (wake_q);raw_spin_lock_irqsave (&sem->lock, flags);if (likely (list_empty (&sem->wait_list))) sem->count++;else -__up (sem); +__up (sem, &wake_q);raw_spin_unlock_irqrestore (&sem->lock, flags); +if (!wake_q_empty (&wake_q)) +wake_up_q (&wake_q); }EXPORT_SYMBOL (up);
@@ -269,11 +273,12 @@ static noinline int __sched __down_timeout(struct semaphore *sem, long timeout)return __down_common (sem,TASK_UNINTERRUPTIBLE , timeout); } -static noinline void __sched __up(struct semaphore *sem) +static noinline void __sched __up(struct semaphore *sem, +struct wake_q_head *wake_q) {struct semaphore_waiter *waiter =list_first_entry (&sem->wait_list, struct semaphore_waiter, list);list_del (&waiter->list); waiter->up = true; -wake_up_process (waiter->task); +wake_q_add (wake_q, waiter->task); }
Signature · 1 line
2.31.1
Waiman Long
reviewerQuoted 32 lines · Waiman Long
It was found that a circular lock dependency can happen with the following locking sequence:
+--> (console_sem).lock --> &p->pi_lock --> &rq->__lock --+ | | +---------------------------------------------------------+The
&p->pi_lock-->&rq->__locksequence is very common in all thetask_rq_lock()calls.The
&rq->__lock--> (console_sem).lock sequence happens when the scheduler code callingprintk()or more likely the various WARN*() macros while holding the rq lock. The (console_sem).lock is actually a raw spinlock guarding the semaphore. In the particular lockdep splat that I saw, it was caused bySCHED_WARN_ON()call inupdate_rq_clock(). To work around this locking sequence, we may have to ban all WARN*() calls when the rq lock is held, which may be too restrictive, or we may have to add aWARN_DEFERRED()call and modify all the call sites to use it.Even then, a deferred printk or WARN function may still call
console_trylock()which may, in turn, callsup_console_sem()leading to this locking sequence.The other ((console_sem).lock -->
&p->pi_lock) locking sequence was caused by the fact that the semaphoreup()function is callingwake_up_process()while holding the semaphore raw spinlock. This lockiing sequence can be easily eliminated by moving thewake_up_processs()call out of the raw spinlock critical section usingwake_qwhich is what this patch implements. That is the easiest and the most certain way to break this circular locking sequence.
Quoted 3 lines
v1: https://lore.kernel.org/lkml/[email protected]/
Signed-off-by Waiman Long <[email protected]>
Ping!
Note that the current printk_deferred() code path may also hit this problem as an up() call of console_sem may be issued.
Cheers,
Longman
Quoted 10 lines · Waiman Long
It was found that a circular lock dependency can happen with the following locking sequence:
+--> (console_sem).lock --> &p->pi_lock --> &rq->__lock --+ | | +---------------------------------------------------------+The
&p->pi_lock-->&rq->__locksequence is very common in all thetask_rq_lock()calls.
Thanks for sending this out! I've been hitting these lockdep warningns a lot recently, particularly if I have any debug printks/WARN_ONs in the scheduler that trip, so I'm eager to get a fix for this!
That said, running with your patch, I'm seeing bootup hang pretty close to where init starts when I've had a fair amount of debug printks go off. It's odd because the lockup detectors don't seem to fire.
I'll try to debug further, but wanted to give you a heads up. Let me know if you have any suggestions.
thanks
-john
Waiman Long
reviewerQuoted 14 lines · John Stultz
Quoted 9 lines · Waiman Long
It was found that a circular lock dependency can happen with the following locking sequence:
+--> (console_sem).lock --> &p->pi_lock --> &rq->__lock --+ | | +---------------------------------------------------------+The
&p->pi_lock-->&rq->__locksequence is very common in all thetask_rq_lock()calls.Thanks for sending this out! I've been hitting these lockdep warningns a lot recently, particularly if I have any debug printks/WARN_ONs in the scheduler that trip, so I'm eager to get a fix for this!
Quoted 6 lines
That said, running with your patch, I'm seeing bootup hang pretty close to where init starts when I've had a fair amount of debug printks go off. It's odd because the lockup detectors don't seem to fire.
I'll try to debug further, but wanted to give you a heads up. Let me know if you have any suggestions.
Thanks for testing this patch. This was not merged because Peter thought the merging of atomic console would probably make this not an issue at all. We are close to getting the atomic console merged. Let's see if this is really the case.
Cheers,
Longman
Peter Zijlstra
maintainerQuoted 9 lines · John Stultz
Quoted 8 lines · Waiman Long
It was found that a circular lock dependency can happen with the following locking sequence:
+--> (console_sem).lock --> &p->pi_lock --> &rq->__lock --+ | | +---------------------------------------------------------+
Quoted 6 lines
Quoted 2 lines
The
&p->pi_lock-->&rq->__locksequence is very common in all thetask_rq_lock()calls.Thanks for sending this out! I've been hitting these lockdep warningns a lot recently, particularly if I have any debug printks/WARN_ONs in the scheduler that trip, so I'm eager to get a fix for this!
https://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git/log/?h=debug/experimental
and use with: earlyprintk=serial,ttyS0,115200 force_early_printk, or somesuch.
I could not have done perf or much of the sched patches without it.
Peter Zijlstra
maintainerQuoted 10 lines · Waiman Long
It was found that a circular lock dependency can happen with the following locking sequence:
+--> (console_sem).lock --> &p->pi_lock --> &rq->__lock --+ | | +---------------------------------------------------------+The
&p->pi_lock-->&rq->__locksequence is very common in all thetask_rq_lock()calls.
Quoted 9 lines
The
&rq->__lock--> (console_sem).lock sequence happens when the scheduler code callingprintk()or more likely the various WARN*() macros while holding the rq lock. The (console_sem).lock is actually a raw spinlock guarding the semaphore. In the particular lockdep splat that I saw, it was caused bySCHED_WARN_ON()call inupdate_rq_clock(). To work around this locking sequence, we may have to ban all WARN*() calls when the rq lock is held, which may be too restrictive, or we may have to add aWARN_DEFERRED()call and modify all the call sites to use it.
No, this is all because printk() is pure garbage -- but I believe it's being worked on.
And I despise that whole deferred thing -- that's just worse garbage.
If you map printk to early_printk none of this is a problem (and this is what i've been doing for something close to a decade).
Printk should not do synchronous, or in-context, printing to non-atomic consoles. Doubly so when atomic console are actually available.
As long as it does this printk is fundamentally unreliable and any of these hacks are just that.
Quoted 11 lines
Even then, a deferred printk or WARN function may still call
console_trylock()which may, in turn, callsup_console_sem()leading to this locking sequence.The other ((console_sem).lock -->
&p->pi_lock) locking sequence was caused by the fact that the semaphoreup()function is callingwake_up_process()while holding the semaphore raw spinlock. This lockiing sequence can be easily eliminated by moving thewake_up_processs()call out of the raw spinlock critical section usingwake_qwhich is what this patch implements. That is the easiest and the most certain way to break this circular locking sequence.
So I don't mind the patch, but I hate everything about your justification for it.
Waiman Long
reviewerQuoted 25 lines · Peter Zijlstra
Quoted 10 lines · Waiman Long
It was found that a circular lock dependency can happen with the following locking sequence:
+--> (console_sem).lock --> &p->pi_lock --> &rq->__lock --+ | | +---------------------------------------------------------+The
&p->pi_lock-->&rq->__locksequence is very common in all thetask_rq_lock()calls.Quoted 9 lines
The
&rq->__lock--> (console_sem).lock sequence happens when the scheduler code callingprintk()or more likely the various WARN*() macros while holding the rq lock. The (console_sem).lock is actually a raw spinlock guarding the semaphore. In the particular lockdep splat that I saw, it was caused bySCHED_WARN_ON()call inupdate_rq_clock(). To work around this locking sequence, we may have to ban all WARN*() calls when the rq lock is held, which may be too restrictive, or we may have to add aWARN_DEFERRED()call and modify all the call sites to use it.No, this is all because
printk()is pure garbage -- but I believe it's being worked on.And I despise that whole deferred thing -- that's just worse garbage.
Quoted 8 lines
If you map printk to
early_printknone of this is a problem (and this is what i've been doing for something close to a decade).Printk should not do synchronous, or in-context, printing to non-atomic consoles. Doubly so when atomic console are actually available.
As long as it does this printk is fundamentally unreliable and any of these hacks are just that.
Thanks for the explanation.
I believe early_printk should only be used in the non-SMP boot process. The use of printk() is frequently used for debugging purpose and the insertion of printk at some lock critical section can cause the lockdep splat to come out obscuring the debugging process.
Quoted 12 lines
Quoted 5 lines
Even then, a deferred printk or WARN function may still call
console_trylock()which may, in turn, callsup_console_sem()leading to this locking sequence.Quoted 7 lines
The other ((console_sem).lock -->
&p->pi_lock) locking sequence was caused by the fact that the semaphoreup()function is callingwake_up_process()while holding the semaphore raw spinlock. This lockiing sequence can be easily eliminated by moving thewake_up_processs()call out of the raw spinlock critical section usingwake_qwhich is what this patch implements. That is the easiest and the most certain way to break this circular locking sequence.
Quoted 3 lines
So I don't mind the patch, but I hate everything about your justification for it.
OK, I can reword the commit log and see you are OK with that.
Cheers,
Longman
Peter Zijlstra
maintainerQuoted 4 lines · Waiman Long
I believe
early_printkshould only be used in the non-SMP boot process. The use ofprintk()is frequently used for debugging purpose and the insertion of printk at some lock critical section can cause the lockdep splat to come out obscuring the debugging process.
By default early_printk is disabled somewhere early, but it has a keep argument to keep it around.
Anyway, printk() as it exists today is wholly unsuited for debugging. There are too many contexts where it will flat out not work. When you use early_print with keep then you can use early_printk() instead of printk() to debug.
Also, see the patches I pointed John at. Perf would not be what it is without those patches.
Serial lines and early printk are not optional. That is, I flat out refuse to develop on machines without them.
Waiman Long
reviewerQuoted 14 lines · Peter Zijlstra
Quoted 4 lines · Waiman Long
I believe
early_printkshould only be used in the non-SMP boot process. The use ofprintk()is frequently used for debugging purpose and the insertion of printk at some lock critical section can cause the lockdep splat to come out obscuring the debugging process.By default
early_printkis disabled somewhere early, but it has a keep argument to keep it around.Anyway,
printk()as it exists today is wholly unsuited for debugging. There are too many contexts where it will flat out not work. When you useearly_printwith keep then you can useearly_printk()instead ofprintk()to debug.
Quoted 5 lines
Also, see the patches I pointed John at. Perf would not be what it is without those patches.
Serial lines and early printk are not optional. That is, I flat out refuse to develop on machines without them.
Thanks for the debugging tip.
BTW, it is not just printk() that can be problematic in some contexts. I believe the various WARN*() calls cause the same kind of lockdep problem even though these WARN() calls shouldn't be triggered that often
Cheers,
Longman