printk/nmi: Prevent deadlock when serializing NMI backtraces
Subsystems (4)
Petr Mladek
maintainerPATCH 1/1 printk/nmi: Prevent deadlock when serializing NMI backtraces
The commit 719f6a7040f1bdaf96fcc ("printk: Use the main logbuf in NMI when logbuf_lock is available") tried to detect when logbuf_lock was taken on another CPU. Then it looked safe to wait for the lock even in NMI.
It would be safe if other locks were not involved. Ironically the same commit introduced an ABBA deadlock scenario. It added a spin lock into nmi_cpu_backtrace() to serialize logs from different CPUs. The effect is that also the NMI handlers are serialized. As a result, logbuf_lock might be blocked by NMI on another CPU:
CPU0 CPU1 CPU2
printk()
vprintk_emit()
spin_lock(&logbuf_lock) trigger_all_cpu_backtrace() raise() nmi_enter() printk_nmi_enter() if (this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK) // false else // looks safe to use printk_deferred() this_cpu_or(printk_context, PRINTK_NMI_DEFERRED_CONTEXT_MASK); nmi_cpu_backtrace() arch_spin_lock(&lock); show_regs()
nmi_enter()
nmi_cpu_backtrace()
arch_spin_lock(&lock); printk() vprintk_func() vprintk_deferred() vprintk_emit() spin_lock(&logbuf_lock)
DEADLOCK: between &logbuf_lock from vprintk_emit() and
&lock from nmi_cpu_backtrace().
CPU0 CPU1lock(logbuf_lock) lock(lock)
lock(lock) lock(logbuf_lock)
I have found this problem when stress testing trigger_all_cpu_backtrace() and the system frozen.
Note that lockdep is not able to detect these dependencies because there is no support for NMI context. Let's stay on the safe side and always use printk_safe buffers when logbuf_lock is taken when entering NMI.
--- kernel/printk/printk_safe.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/printk/printk_safe.c +3 -6
@@ -310,15 +310,12 @@ void printk_nmi_enter(void) {/* * The size of the extra per-CPU buffer is limited. Use it only when -* the main one is locked. If this CPU is not in the safe context, -* the lock must be taken on another CPU and we could wait for it. +* the main one is locked. */ -if ((this_cpu_read (printk_context) &PRINTK_SAFE_CONTEXT_MASK ) && -raw_spin_is_locked (&logbuf_lock)) { +if (raw_spin_is_locked (&logbuf_lock))this_cpu_or (printk_context,PRINTK_NMI_CONTEXT_MASK ); - }else { +else this_cpu_or (printk_context,PRINTK_NMI_DEFERRED_CONTEXT_MASK ); - } }void printk_nmi_exit (void )
Signature · 1 line
2.13.6
Quoted 41 lines · Petr Mladek
CPU0 CPU1 CPU2
printk()
vprintk_emit()spin_lock(&logbuf_lock) trigger_all_cpu_backtrace() raise() nmi_enter() printk_nmi_enter() if (this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK) // false else // looks safe to use printk_deferred() this_cpu_or(printk_context, PRINTK_NMI_DEFERRED_CONTEXT_MASK); nmi_cpu_backtrace() arch_spin_lock(&lock); show_regs()
nmi_enter()
nmi_cpu_backtrace()arch_spin_lock(&lock); printk() vprintk_func() vprintk_deferred() vprintk_emit() spin_lock(&logbuf_lock)DEADLOCK: between
&logbuf_lockfromvprintk_emit()and
&lock fromnmi_cpu_backtrace().CPU0 CPU1
lock(logbuf_lock)lock(lock)
lock(lock)lock(logbuf_lock)
[..]
Quoted 1 line
Signed-off-by Petr Mladek <[email protected]>
This is a pretty cool find!
kernel/printk/printk_safe.c void printk_nmi_enter(void) · line 321
Quoted 8 lines
kernel/printk/printk_safe.c +2
-4-if ((this_cpu_read (printk_context) &PRINTK_SAFE_CONTEXT_MASK ) && -raw_spin_is_locked (&logbuf_lock)) { +if (raw_spin_is_locked (&logbuf_lock))this_cpu_or (printk_context,PRINTK_NMI_CONTEXT_MASK ); - }else { +else this_cpu_or (printk_context,PRINTK_NMI_DEFERRED_CONTEXT_MASK ); - }
A question - can we switch to a bitwise OR?
if (this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK) ||
raw_spin_is_locked(&logbuf_lock)
just to check per-CPU printk_context' first and only afterwards access the global logbuf_lock'. printk_nmi_enter() happens on every CPU, so maybe we can avoid some overhead by checking the local per-CPU data first.
-ss
Quoted 8 lines · Sergey Senozhatsky
if (
this_cpu_read(printk_context)&PRINTK_SAFE_CONTEXT_MASK) ||
raw_spin_is_locked(&logbuf_lock)just to check per-CPU
printk_context' first and only afterwards access the globallogbuf_lock'.printk_nmi_enter()happens on every CPU, so maybe we can avoid some overhead by checking the local per-CPU data first.
Nah, may be it won't. This, probably, would have been the case if we had continue to call console drivers from printk_safe section [at least]. CPUs don't spend that much time in printk_safe sections.
-ss
Petr Mladek
maintainerQuoted 5 lines · Sergey Senozhatsky
Quoted 4 lines · Sergey Senozhatsky
if (
this_cpu_read(printk_context)&PRINTK_SAFE_CONTEXT_MASK) ||
raw_spin_is_locked(&logbuf_lock)
Quoted 8 lines
Quoted 4 lines
just to check per-CPU
printk_context' first and only afterwards access the globallogbuf_lock'.printk_nmi_enter()happens on every CPU, so maybe we can avoid some overhead by checking the local per-CPU data first.Nah, may be it won't. This, probably, would have been the case if we had continue to call console drivers from
printk_safesection [at least]. CPUs don't spend that much time inprintk_safesections.
Yeah, I do not think that the more complicated code is worth it.
There is really minimal chance to hit printk_safe context on the given CPU. The eventual win would be negligible to the cost of printk(). In case of trigger_all_cpu_backtrace(), most CPUs spend a lot of time waiting for the spinlock in nmi_cpu_backtrace() anyway.
Thanks for the ack.
Best Regards,
Petr
Steven Rostedt
maintainerQuoted 23 lines · On Thu, 17 May 2018 16:39:03 +0200 Petr Mladek <[email protected]> wrote:
The commit
719f6a7040f1bdaf96fcc("printk: Use the main logbuf in NMI whenlogbuf_lockis available") tried to detect whenlogbuf_lockwas taken on another CPU. Then it looked safe to wait for the lock even in NMI.It would be safe if other locks were not involved. Ironically the same commit introduced an ABBA deadlock scenario. It added a spin lock into
nmi_cpu_backtrace()to serialize logs from different CPUs. The effect is that also the NMI handlers are serialized. As a result,logbuf_lockmight be blocked by NMI on another CPU:CPU0 CPU1 CPU2
printk()
vprintk_emit()spin_lock(&logbuf_lock) trigger_all_cpu_backtrace() raise() nmi_enter() printk_nmi_enter() if (this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK)
Quoted 8 lines
// false else // looks safe to use printk_deferred() this_cpu_or(printk_context, PRINTK_NMI_DEFERRED_CONTEXT_MASK); nmi_cpu_backtrace() arch_spin_lock(&lock);
What branch is this based on, because I can't find the "arch_spin_lock()" you are talking about here.
-- Steve
Quoted 57 lines
show_regs()
nmi_enter()
nmi_cpu_backtrace()arch_spin_lock(&lock); printk() vprintk_func() vprintk_deferred() vprintk_emit() spin_lock(&logbuf_lock)DEADLOCK: between
&logbuf_lockfromvprintk_emit()and
&lock fromnmi_cpu_backtrace().CPU0 CPU1
lock(logbuf_lock)lock(lock)
lock(lock)lock(logbuf_lock)I have found this problem when stress testing
trigger_all_cpu_backtrace()and the system frozen.Note that lockdep is not able to detect these dependencies because there is no support for NMI context. Let's stay on the safe side and always use
printk_safebuffers whenlogbuf_lockis taken when entering NMI.Fixes 719f6a7040f1bdaf96fcc ("printk: Use the main logbuf in NMI when logbuf_lock is available")Cc 4.13+ <[email protected]> # v4.13+Signed-off-by Petr Mladek <[email protected]>--- kernel/printk/printk_safe.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)kernel/printk/printk_safe.c +3
-6@@ -310,15 +310,12 @@ void printk_nmi_enter(void) {/* * The size of the extra per-CPU buffer is limited. Use it only when -* the main one is locked. If this CPU is not in the safe context, -* the lock must be taken on another CPU and we could wait for it. +* the main one is locked. */ -if ((this_cpu_read (printk_context) &PRINTK_SAFE_CONTEXT_MASK ) && -raw_spin_is_locked (&logbuf_lock)) { +if (raw_spin_is_locked (&logbuf_lock))this_cpu_or (printk_context,PRINTK_NMI_CONTEXT_MASK ); - }else { +else this_cpu_or (printk_context,PRINTK_NMI_DEFERRED_CONTEXT_MASK ); - } }void printk_nmi_exit (void )
Quoted 19 lines · Steven Rostedt
Quoted 19 lines
CPU0 CPU1 CPU2
printk()
vprintk_emit()spin_lock(&logbuf_lock) trigger_all_cpu_backtrace() raise() nmi_enter() printk_nmi_enter() if (this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK) // false else // looks safe to use printk_deferred() this_cpu_or(printk_context, PRINTK_NMI_DEFERRED_CONTEXT_MASK);
Quoted 5 lines
Quoted 2 lines
nmi_cpu_backtrace() arch_spin_lock(&lock);What branch is this based on, because I can't find the "
arch_spin_lock()" you are talking about here.
This arch_spin_lock() is in lib/nmi_backtrace.c
-ss
Petr Mladek
maintainerQuoted 23 lines · Sergey Senozhatsky
Quoted 22 lines · Steven Rostedt
Quoted 21 lines
CPU0 CPU1 CPU2
printk()
vprintk_emit()spin_lock(&logbuf_lock) trigger_all_cpu_backtrace() raise() nmi_enter() printk_nmi_enter() if (this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK) // false else // looks safe to use printk_deferred() this_cpu_or(printk_context, PRINTK_NMI_DEFERRED_CONTEXT_MASK); nmi_cpu_backtrace() arch_spin_lock(&lock);
Quoted 6 lines
Quoted 2 lines
What branch is this based on, because I can't find the "
arch_spin_lock()" you are talking about here.This
arch_spin_lock()is inlib/nmi_backtrace.c
JFYI, I have pushed the patch into printk.git, for-4.18 branch, see
https://git.kernel.org/pub/scm/linux/kernel/git/pmladek/printk.git/commit/?h=for-4.18&id=42f139d887cbb2c396dd61f4f13d4facfde5739e
Best Regards,
Petr
Petr Mladek
maintainerQuoted 71 lines · Petr Mladek
The commit
719f6a7040f1bdaf96fcc("printk: Use the main logbuf in NMI whenlogbuf_lockis available") tried to detect whenlogbuf_lockwas taken on another CPU. Then it looked safe to wait for the lock even in NMI.It would be safe if other locks were not involved. Ironically the same commit introduced an ABBA deadlock scenario. It added a spin lock into
nmi_cpu_backtrace()to serialize logs from different CPUs. The effect is that also the NMI handlers are serialized. As a result,logbuf_lockmight be blocked by NMI on another CPU:CPU0 CPU1 CPU2
printk()
vprintk_emit()spin_lock(&logbuf_lock) trigger_all_cpu_backtrace() raise() nmi_enter() printk_nmi_enter() if (this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK) // false else // looks safe to use printk_deferred() this_cpu_or(printk_context, PRINTK_NMI_DEFERRED_CONTEXT_MASK); nmi_cpu_backtrace() arch_spin_lock(&lock); show_regs()
nmi_enter()
nmi_cpu_backtrace()arch_spin_lock(&lock); printk() vprintk_func() vprintk_deferred() vprintk_emit() spin_lock(&logbuf_lock)DEADLOCK: between
&logbuf_lockfromvprintk_emit()and
&lock fromnmi_cpu_backtrace().CPU0 CPU1
lock(logbuf_lock)lock(lock)
lock(lock)lock(logbuf_lock)I have found this problem when stress testing
trigger_all_cpu_backtrace()and the system frozen.Note that lockdep is not able to detect these dependencies because there is no support for NMI context. Let's stay on the safe side and always use
printk_safebuffers whenlogbuf_lockis taken when entering NMI.Fixes 719f6a7040f1bdaf96fcc ("printk: Use the main logbuf in NMI when logbuf_lock is available")Cc 4.13+ <[email protected]> # v4.13+Signed-off-by Petr Mladek <[email protected]>--- kernel/printk/printk_safe.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)kernel/printk/printk_safe.c +0
-0@@ -310,15 +310,12 @@ void printk_nmi_enter(void) {
kernel/printk/printk_safe.c void printk_nmi_enter(void) · line 316
Quoted 10 lines
kernel/printk/printk_safe.c +2
-4/* * The size of the extra per-CPU buffer is limited. Use it only when -* the main one is locked. If this CPU is not in the safe context, -* the lock must be taken on another CPU and we could wait for it. +* the main one is locked. */ -if ((this_cpu_read (printk_context) &PRINTK_SAFE_CONTEXT_MASK ) && -raw_spin_is_locked (&logbuf_lock)) { +if (raw_spin_is_locked (&logbuf_lock))this_cpu_or (printk_context,PRINTK_NMI_CONTEXT_MASK );
Grr, the ABBA deadlock is still there. NMIs are not sent to the other CPUs atomically. Even if we detect that logbuf_lock is available in printk_nmi_enter() on some CPUs, it might still get locked on another CPU before the other CPU gets NMI.
By other words, any check in printk_safe_enter() is racy and not sufficient
=> I suggest to revert the commit 719f6a7040f1bdaf96fcc70
"printk: Use the main logbuf in NMI when logbuf_lock is available" for-4.18 and stable until we get a better solution.
The only safe solution seems to be a trylock() in NMI in vprintk_emit() and fallback to vprintk_safe() when the lock is not taken. I mean something like:
kernel/printk/printk.c +7 -1
@@ -1845,7 +1845,13 @@ asmlinkage int vprintk_emit(int facility, int level,printk_delay ();/* This stops the holder of console_sem just where we want him */ -logbuf_lock_irqsave (flags); +printk_safe_enter_irqsave (flags); +if (in_nmi () && !raw_spin_trylock (&logbuf_lock)) { +vprintk_nmi (fmt, args); +printk_safe_exit_irqrestore (flags); +return ; + }else +raw_spin_lock (&logbuf_lock);/* * The printf needs to come first; we need the syslog * prefix which might be passed-in as a parameter.
Sigh, this looks like a material for-4.19. We might need to revisit if printk_context still makes sense, ...
Best Regards,
Petr
PS: I realized this when writing the pull request for-4.18. I removed this patch from the pull request.
On (06/05/18 14:47), Petr Mladek wrote:
[..]
Quoted 4 lines
Grr, the ABBA deadlock is still there. NMIs are not sent to the other CPUs atomically. Even if we detect that
logbuf_lockis available inprintk_nmi_enter()on some CPUs, it might still get locked on another CPU before the other CPU gets NMI.
Can we do something about "B"? :) I mean - any chance we can rework locking in nmi_cpu_backtrace()?
Quoted 2 lines
By other words, any check in
printk_safe_enter()is racy and not sufficient
I suppose you meant printk_nmi_enter().
Quoted 3 lines
=> I suggest to revert the commit
719f6a7040f1bdaf96fcc70
"printk: Use the main logbuf in NMI whenlogbuf_lockis available" for-4.18 and stable until we get a better solution.
Just random thoughts.
May be we need to revert it, but let's not "panic". I think [but don't insist on it] that the patch in question is *probably* "good enough". It addresses a bug report after all.
How often do we have arch_trigger_cpumask_backtrace() on all CPUs these days? I tend to think that it used to be much more popular in the past, because we had a loops_per_jiffy based spin_lock lockup detection which would trigger NMI backtracase, but this functionality has gone, see bc88c10d7e6900916f5e1ba3829d66a9de92b633 for details. I'm not saying that the race condition that you found is unrealistic, I'm just saying that _it seems_ that nmi_panic()->printk() on a single CPU is more common now, so having that nmi_printk()->printk_deferred() might be quite valuable at the end of the day.
May be I'm wrong!
Quoted 13 lines
The only safe solution seems to be a
trylock()in NMI invprintk_emit()and fallback tovprintk_safe()when the lock is not taken. I mean something like:kernel/printk/printk.c +0
-1@@ -1845,7 +1845,13 @@ asmlinkage int vprintk_emit(int facility, int level,printk_delay ();/* This stops the holder of console_sem just where we want him */ -logbuf_lock_irqsave (flags);
kernel/printk/printk.c asmlinkage int vprintk_emit(int facility, int level, · line 1857
Quoted 10 lines
kernel/printk/printk.c +7
-0+printk_safe_enter_irqsave (flags); +if (in_nmi () && !raw_spin_trylock (&logbuf_lock)) { +vprintk_nmi (fmt, args); +printk_safe_exit_irqrestore (flags); +return ; + }else +raw_spin_lock (&logbuf_lock);/* * The printf needs to come first; we need the syslog * prefix which might be passed-in as a parameter.
I need some time to think about it.
Quoted 1 line
Sigh, this looks like a material for-4.19.
Agreed.
Quoted 1 line
We might need to revisit if
printk_contextstill makes sense, ...
What do you mean by this?
Quoted 2 lines
PS: I realized this when writing the pull request for-4.18. I removed this patch from the pull request.
Yep. Good job!
-ss
Quoted 9 lines · Sergey Senozhatsky
On (06/05/18 14:47), Petr Mladek wrote:
[..]Quoted 4 lines
Grr, the ABBA deadlock is still there. NMIs are not sent to the other CPUs atomically. Even if we detect that
logbuf_lockis available inprintk_nmi_enter()on some CPUs, it might still get locked on another CPU before the other CPU gets NMI.Can we do something about "B"? :) I mean - any chance we can rework locking in
nmi_cpu_backtrace()?
Sorry, I don't have that much free time at the moment, so can't fully concentrate on this issue.
Here is a quick-n-dirty thought.
The whole idea of printk_nmi() was to reduce the number of locks we take performing printk() from NMI context - ideally down to 0. We added logbuf spin_lock later on. One lock was fine. But then we added another one - nmi_cpu_backtrace() lock. And two locks is too many. So can we drop the nmi_cpu_backtrace() lock, and in exchange extend printk-safe API with functions that will disable/enable PRINTK_NMI_DEFERRED_CONTEXT_MASK on a particular CPU?
I refer to it as HARD and SOFT printk_nmi :) Just for fun. Hard one has no right to use logbuf and will use only per-CPU buffer, while soft one can use either per-CPU buffer or logbuf.
---
lib/nmi_backtrace.c +2 -3
@@ -90,11 +90,10 @@ void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,bool nmi_cpu_backtrace (struct pt_regs *regs) { -static arch_spinlock_t lock = __ARCH_SPIN_LOCK_UNLOCKED;int cpu =smp_processor_id ();if (cpumask_test_cpu (cpu,to_cpumask (backtrace_mask))) { -arch_spin_lock (&lock); +printk_nmi_hard ();if (regs &&cpu_in_idle (instruction_pointer (regs))) {pr_warn ("NMI backtrace for cpu %d skipped: idling at %pS\n" , cpu, (void *)instruction_pointer (regs));
@@ -105,7 +104,7 @@ bool nmi_cpu_backtrace(struct pt_regs *regs)else dump_stack (); } -arch_spin_unlock (&lock); +printk_nmi_sort ();cpumask_clear_cpu (cpu,to_cpumask (backtrace_mask));return true; }
Petr Mladek
maintainerQuoted 9 lines · Sergey Senozhatsky
On (06/05/18 14:47), Petr Mladek wrote:
[..]Quoted 4 lines
Grr, the ABBA deadlock is still there. NMIs are not sent to the other CPUs atomically. Even if we detect that
logbuf_lockis available inprintk_nmi_enter()on some CPUs, it might still get locked on another CPU before the other CPU gets NMI.Can we do something about "B"? :) I mean - any chance we can rework locking in
nmi_cpu_backtrace()?
I can't think of any possibility at the moment. Well, it does not mean that it does not exist.
The irony is that we need the extra lock in nmi_cpu_backtrace() only because we try to store the messages into the common log buffer. If we always use the per-CPU buffers in NMI then the lock would not cause any problems but it also won't be necessary.
Quoted 4 lines
Quoted 2 lines
By other words, any check in
printk_safe_enter()is racy and not sufficientI suppose you meant
printk_nmi_enter().
Yup, I am sorry for confusion.
Quoted 9 lines
Quoted 3 lines
=> I suggest to revert the commit
719f6a7040f1bdaf96fcc70
"printk: Use the main logbuf in NMI whenlogbuf_lockis available" for-4.18 and stable until we get a better solution.Just random thoughts.
May be we need to revert it, but let's not "panic". I think [but don't insist on it] that the patch in question is *probably* "good enough". It addresses a bug report after all.
It was a problem reported by me. I found it when testing other changes. The patch improved the situation definitely. The question is if it is enough in practice.
Quoted 5 lines
How often do we have
arch_trigger_cpumask_backtrace()on all CPUs these days? I tend to think that it used to be much more popular in the past, because we had aloops_per_jiffybasedspin_locklockup detection which would trigger NMI backtracase, but this functionality has gone, seebc88c10d7e6900916f5e1ba3829d66a9de92b633for details.
AFAIK, we (SUSE) started to work on the printk/NMI deadlocks because it was too risky to use sysrq-l on systems with many CPUs. It means that the use case is still there.
Hmm, the original problem was that any interrupted logbuf_lock owner caused the deadlock. It is _different now_ because the interrupted logbuf_lock owner must block another CPU via the lock in nmi_cpu_backtrace().
I actually wonder why this patch actually helped so much in my test (while true; do echo l >/proc/sysrq-trigger; done). My guess is that it increased a lot the number of CPUs using PRINTK_NMI_CONTEXT (per-CPU buffers). Therefore the deadlock was less likely. On the other hand, the original problem was basically back without this patch.
Quoted 5 lines
I'm not saying that the race condition that you found is unrealistic, I'm just saying that
_itseems_that nmi_panic()->printk() on a single CPU is more common now, so having that nmi_printk()->printk_deferred() might be quite valuable at the end of the day.
I see the point. I need to think about it.
Quoted 13 lines
May be I'm wrong!
Quoted 11 lines
The only safe solution seems to be a
trylock()in NMI invprintk_emit()and fallback tovprintk_safe()when the lock is not taken. I mean something like:kernel/printk/printk.c +0
-0@@ -1845,7 +1845,13 @@ asmlinkage int vprintk_emit(int facility, int level,printk_delay ();
Quoted 14 lines
kernel/printk/printk.c asmlinkage int vprintk_emit(int facility, int level, · line 1857
Quoted 12 lines
kernel/printk/printk.c +7
-1/* This stops the holder of console_sem just where we want him */ -logbuf_lock_irqsave (flags); +printk_safe_enter_irqsave (flags); +if (in_nmi () && !raw_spin_trylock (&logbuf_lock)) { +vprintk_nmi (fmt, args); +printk_safe_exit_irqrestore (flags); +return ; + }else +raw_spin_lock (&logbuf_lock);/* * The printf needs to come first; we need the syslog * prefix which might be passed-in as a parameter. I need some time to think about it.
The bad thing about this solution is that different messages might be handled different way during a single nmi_cpu_backtrace(). The result might be mixed backtraces in the log.
Quoted 7 lines
Quoted 1 line
Sigh, this looks like a material for-4.19.
Agreed.
Quoted 1 line
We might need to revisit if
printk_contextstill makes sense, ...What do you mean by this?
My dream was to select the right vprintk implementation according to printk_context on a single place (vprintk_func()). If this is not usable for NMI, we might need to think about another solution. But this is premature. I believe the printk_context will most likely make still sense.
I need to think more about it.
Best Regards,
Petr
Quoted 11 lines · Petr Mladek
Quoted 9 lines · Sergey Senozhatsky
On (06/05/18 14:47), Petr Mladek wrote:
[..]Quoted 4 lines
Grr, the ABBA deadlock is still there. NMIs are not sent to the other CPUs atomically. Even if we detect that
logbuf_lockis available inprintk_nmi_enter()on some CPUs, it might still get locked on another CPU before the other CPU gets NMI.Can we do something about "B"? :) I mean - any chance we can rework locking in
nmi_cpu_backtrace()?
Quoted 7 lines
I can't think of any possibility at the moment. Well, it does not mean that it does not exist.
The irony is that we need the extra lock in
nmi_cpu_backtrace()only because we try to store the messages into the common log buffer. If we always use the per-CPU buffers in NMI then the lock would not cause any problems but it also won't be necessary.
Yep. I think we can come up with something. It seems that the only problem is that we want in this particular case to avoid printk_nmi()->logbuf and instead we want to enforce per-CPU printk_nmi buffers. Then we can drop nmi_cpu_backtrace() spinlock, because the messages will be serialized by printk_safe flush spin_lock. That doesn't sound like an impossible thing to do. What am I missing?
Could you please check my follow up email?
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
=> I suggest to revert the commit
719f6a7040f1bdaf96fcc70
"printk: Use the main logbuf in NMI whenlogbuf_lockis available" for-4.18 and stable until we get a better solution.Just random thoughts.
Quoted 7 lines
Quoted 3 lines
May be we need to revert it, but let's not "panic". I think [but don't insist on it] that the patch in question is *probably* "good enough". It addresses a bug report after all.
It was a problem reported by me. I found it when testing other changes. The patch improved the situation definitely. The question is if it is enough in practice.
Oh, certainly. But I was talking about 719f6a7040f1bdaf96fcc70. We introduced that change in response to a bug report from Steven. He would not be able to debug his kernel otherwise, because per-CPU printk_nmi was too limited in size.
So on one hand we have the problem that you reported, which you found while you were hammering/testing NMI printk-s [a valid report on its own]; on the other hand we have the problem that Steven reported, which he triggered while he was debugging the kernel. It might be the case that Steven's problem is more likely to happen in real world. So that's why I proposed to keep 719f6a7040f1bda for the time being [until we come up with another fix]. I may be wrong.
-ss
Petr Mladek
maintainerQuoted 16 lines · Sergey Senozhatsky
Quoted 9 lines · Sergey Senozhatsky
On (06/05/18 14:47), Petr Mladek wrote:
[..]Quoted 4 lines
Grr, the ABBA deadlock is still there. NMIs are not sent to the other CPUs atomically. Even if we detect that
logbuf_lockis available inprintk_nmi_enter()on some CPUs, it might still get locked on another CPU before the other CPU gets NMI.Can we do something about "B"? :) I mean - any chance we can rework locking in
nmi_cpu_backtrace()?Sorry, I don't have that much free time at the moment, so can't fully concentrate on this issue.
Here is a quick-n-dirty thought.
Quoted 7 lines
The whole idea of
printk_nmi()was to reduce the number of locks we take performingprintk()from NMI context - ideally down to 0. We added logbufspin_locklater on. One lock was fine. But then we added another one -nmi_cpu_backtrace()lock. And two locks is too many. So can we drop thenmi_cpu_backtrace()lock, and in exchange extend printk-safe API with functions that will disable/enablePRINTK_NMI_DEFERRED_CONTEXT_MASKon a particular CPU?
I ended with similar conclusion. I am just nervous by the fact that the check in printk_nmi_enter() will always be unreliable. We already deal with situations when we want to check the actual lock state in panic(). Also the lock in nmi_cpu_backtrace() need not be the only lock serializing NMIs. I am always surprised what code can be called in NMI.
I played with this a bit and came up with the following:
Quoted 1 line
From
25c904dbe8eb95d823b311a343f39ed926b7a7b2Mon Sep 17 00:00:00 2001
From: Petr Mladek <[email protected]>
Date: Fri, 8 Jun 2018 11:21:07 +0200
Subject: [PATCH] printk/nmi: More reliable approach to use the main log buffer in NMI
The check whether logbuf_lock is available is not safe in printk_nmi_enter(). It might get blocked by another CPU that takes the lock and enter NMI later.
The race still would be acceptable if the other CPU could finish the NMI handler independently. But this is not a case with nmi_trigger_cpumask_backtrace(). It has to serialize all NMI handlers to prevent mixed output.
It would be possible to make the decision later in vprintk_emit(). But it would cause mixed output and make nmi_trigger_cpumask_backtrace() unusable.
Also it would be possible to avoid the serialization and force using the per-CPU buffers in nmi_cpu_backtrace(). But this problem is generic and we do not know how many other such deadlocks are possible.
But wait, the motivation to use the main log buffer in NMI was for flushing ftrace log in panic(). It can produce hundreds of lines that do not fit the per-CPU buffers. The good think is that they have its own time stamp and can be sorted later. Therefore it might be acceptable to risk the mixed output in this case.
This patch introduces new printk_nmi_direct() context. It tries to use the main log buffer when possible but there is a risk of mixed messages. It will be used only when flushing the ftrace buffer.
The special printk_nmi per-CPU buffers will always be used in the other situation again. As a result, nmi_cpu_backtrace() need not longer be serialized.
--- include/linux/printk.h | 4 ++++ kernel/printk/internal.h | 8 ++++++- kernel/printk/printk.c | 8 ++++++- kernel/printk/printk_safe.c | 57 ++++++++++++++++++++++++--------------------- kernel/trace/trace.c | 4 +++- lib/nmi_backtrace.c | 3 --- 6 files changed, 51 insertions(+), 33 deletions(-)
include/linux/printk.h +4 -0
@@ -148,9 +148,13 @@ void early_printk(const char *s, ...) { }#ifdef CONFIG_PRINTK_NMI extern void printk_nmi_enter (void );extern void printk_nmi_exit (void ); +extern void printk_nmi_direct_enter (void ); +extern void printk_nmi_direct_exit (void );#else static inline void printk_nmi_enter (void ) { }static inline void printk_nmi_exit (void ) { } +static void printk_nmi_direct_enter (void ) { } +static void printk_nmi_direct_exit (void ) { }#endif /* PRINTK_NMI */ #ifdef CONFIG_PRINTK
kernel/printk/internal.h +7 -1
@@ -19,11 +19,17 @@#ifdef CONFIG_PRINTK #define PRINTK_SAFE_CONTEXT_MASK 0x3fffffff -#define PRINTK_NMI_DEFERRED_CONTEXT_MASK 0x40000000 +#define PRINTK_NMI_DIRECT_CONTEXT_MASK 0x40000000#define PRINTK_NMI_CONTEXT_MASK 0x80000000extern raw_spinlock_t logbuf_lock; +#ifdef CONFIG_PRINTK_NMI +__printf(1, 0) int vprintk_nmi(const char *fmt, va_list args); +#else +__printf(1, 0) int vprintk_nmi(const char *fmt, va_list args) { return 0; } +#endif + __printf(1, 0) int vprintk_default(const char *fmt, va_list args); __printf(1, 0) int vprintk_deferred(const char *fmt, va_list args); __printf(1, 0) int vprintk_func(const char *fmt, va_list args);
kernel/printk/printk.c +7 -1
@@ -1845,7 +1845,13 @@ asmlinkage int vprintk_emit(int facility, int level,printk_delay ();/* This stops the holder of console_sem just where we want him */ -logbuf_lock_irqsave (flags); +printk_safe_enter_irqsave (flags); +if (in_nmi () && !raw_spin_trylock (&logbuf_lock)) { + printed_len =vprintk_nmi (fmt, args); +printk_safe_exit_irqrestore (flags); +return printed_len; + }else +raw_spin_lock (&logbuf_lock);/* * The printf needs to come first; we need the syslog * prefix which might be passed-in as a parameter.
kernel/printk/printk_safe.c +30 -27
89 lines not on this page · raw message
kernel/trace/trace.c +3 -1
@@ -8279,6 +8279,7 @@ void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)tracing_off ();local_irq_save (flags); +printk_nmi_direct_enter ();/* Simulate the iterator */ trace_init_global_iter (&iter);
@@ -8358,7 +8359,8 @@ void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)for_each_tracing_cpu (cpu) {atomic_dec (&per_cpu_ptr (iter.trace_buffer->data, cpu)->disabled); } -atomic_dec (&dump_running); +atomic_dec (&dump_running); +printk_nmi_direct_exit ();local_irq_restore (flags); }EXPORT_SYMBOL_GPL (ftrace_dump);
lib/nmi_backtrace.c +0 -3
@@ -87,11 +87,9 @@ void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,bool nmi_cpu_backtrace (struct pt_regs *regs) { -static arch_spinlock_t lock = __ARCH_SPIN_LOCK_UNLOCKED;int cpu =smp_processor_id ();if (cpumask_test_cpu (cpu,to_cpumask (backtrace_mask))) { -arch_spin_lock (&lock);if (regs &&cpu_in_idle (instruction_pointer (regs))) {pr_warn ("NMI backtrace for cpu %d skipped: idling at %pS\n" , cpu, (void *)instruction_pointer (regs));
@@ -102,7 +100,6 @@ bool nmi_cpu_backtrace(struct pt_regs *regs)else dump_stack (); } -arch_spin_unlock (&lock);cpumask_clear_cpu (cpu,to_cpumask (backtrace_mask));return true; }
Signature · 1 line
2.13.6
On (06/08/18 12:48), Petr Mladek wrote:
[..]
Quoted 5 lines
include/linux/printk.h +0
-0
include/linux/printk.h void early_printk(const char *s, ...) { } · line 157
Quoted 10 lines
include/linux/printk.h +4
-0@@ -148,9 +148,13 @@ void early_printk(const char *s, ...) { }#ifdef CONFIG_PRINTK_NMI extern void printk_nmi_enter (void );extern void printk_nmi_exit (void ); +extern void printk_nmi_direct_enter (void ); +extern void printk_nmi_direct_exit (void );#else static inline void printk_nmi_enter (void ) { }static inline void printk_nmi_exit (void ) { } +static void printk_nmi_direct_enter (void ) { } +static void printk_nmi_direct_exit (void ) { }
Can we have better names may be? Since direct printk_nmi is not in fact always `direct'.
include/linux/printk.h void early_printk(const char *s, ...) { }
Quoted 5 lines
include/linux/printk.h +5
-0+#ifdef CONFIG_PRINTK_NMI +__printf(1, 0) int vprintk_nmi(const char *fmt, va_list args); +#else +__printf(1, 0) int vprintk_nmi(const char *fmt, va_list args) { return 0; } +#endif
Hmm, printk_safe.c knows about printk.c, printk.c knows about printk_safe.c.
Quoted 9 lines
__printf(1, 0)intvprintk_default(const char *fmt, va_list args);__printf(1, 0)intvprintk_deferred(const char *fmt, va_list args);__printf(1, 0)intvprintk_func(const char *fmt, va_list args);kernel/printk/printk.c +0
-0@@ -1845,7 +1845,13 @@ asmlinkage int vprintk_emit(int facility, int level,printk_delay ();
kernel/printk/printk.c asmlinkage int vprintk_emit(int facility, int level, · line 1854
Quoted 10 lines
kernel/printk/printk.c +7
-1/* This stops the holder of console_sem just where we want him */ -logbuf_lock_irqsave (flags); +printk_safe_enter_irqsave (flags); +if (in_nmi () && !raw_spin_trylock (&logbuf_lock)) { + printed_len =vprintk_nmi (fmt, args); +printk_safe_exit_irqrestore (flags); +return printed_len; + }else +raw_spin_lock (&logbuf_lock);
OK... Can we do this in vprintk_func()? The race window should be super tiny [if matters at all], but in exchange we don't have to mix nmi, printk, printk_mni, etc.
So over all I understand why you did it this way. May be I'd prefer to have less universal but shorter solution (e.g. modify only nmi_backtrace function and put there "printk_nmi_restricted_buffer"), but I won't really object your patch [unless I see some real issues with it].
-ss
Petr Mladek
maintainerQuoted 12 lines · Sergey Senozhatsky
On (06/08/18 12:48), Petr Mladek wrote:
[..]Quoted 10 lines
include/linux/printk.h +2
-0@@ -148,9 +148,13 @@ void early_printk(const char *s, ...) { }#ifdef CONFIG_PRINTK_NMI extern void printk_nmi_enter (void );extern void printk_nmi_exit (void ); +extern void printk_nmi_direct_enter (void ); +extern void printk_nmi_direct_exit (void );
Quoted 8 lines
include/linux/printk.h void early_printk(const char *s, ...) { } · line 157
Quoted 5 lines
include/linux/printk.h +2
-0#else static inline void printk_nmi_enter (void ) { }static inline void printk_nmi_exit (void ) { } +static void printk_nmi_direct_enter (void ) { } +static void printk_nmi_direct_exit (void ) { }Can we have better names may be? Since direct
printk_nmiis not in fact always `direct'.
What about printk_chatty_nmi_enter(), printk_large_nmi_enter() or something similar?
Quoted 8 lines
include/linux/printk.h void early_printk(const char *s, ...) { }
Quoted 5 lines
include/linux/printk.h +5
-0+#ifdef CONFIG_PRINTK_NMI +__printf(1, 0) int vprintk_nmi(const char *fmt, va_list args); +#else +__printf(1, 0) int vprintk_nmi(const char *fmt, va_list args) { return 0; } +#endif Hmm,
printk_safe.cknows aboutprintk.c,printk.cknows aboutprintk_safe.c.
I am sorry but I do not understand the problem. The function is defined in printk_safe.c and we need to call it also from printk.c. It seems reasonable to declare it in kernel/printk/internal.h.
Quoted 10 lines
Quoted 10 lines
__printf(1, 0)intvprintk_default(const char *fmt, va_list args);__printf(1, 0)intvprintk_deferred(const char *fmt, va_list args);__printf(1, 0)intvprintk_func(const char *fmt, va_list args);kernel/printk/printk.c +0
-0@@ -1845,7 +1845,13 @@ asmlinkage int vprintk_emit(int facility, int level,printk_delay ();
Quoted 13 lines
kernel/printk/printk.c asmlinkage int vprintk_emit(int facility, int level, · line 1854
Quoted 9 lines
kernel/printk/printk.c +7
-1/* This stops the holder of console_sem just where we want him */ -logbuf_lock_irqsave (flags); +printk_safe_enter_irqsave (flags); +if (in_nmi () && !raw_spin_trylock (&logbuf_lock)) { + printed_len =vprintk_nmi (fmt, args); +printk_safe_exit_irqrestore (flags); +return printed_len; + }else +raw_spin_lock (&logbuf_lock);OK... Can we do this in
vprintk_func()? The race window should be super tiny [if matters at all], but in exchange we don't have to mix nmi, printk,printk_mni, etc.
You are right that it would still solve the main risk (NMI comes inside logbuf_lock critical section).
In fact, the only real risk would be another lock serializing NMIs and printk() called with that lock. This patch removes one in nmi_backtrace() and I am not aware of any other.
The less hairy code really might be worth the rather theoretical risk.
Quoted 4 lines
So over all I understand why you did it this way. May be I'd prefer to have less universal but shorter solution (e.g. modify only
nmi_backtracefunction and put there "printk_nmi_restricted_buffer"), but I won't really object your patch [unless I see some real issues with it].
Thanks in advance. I'll send v2 once we have a conclusion on the function names and includes.
Best Regards,
Petr
On (06/18/18 11:39), Petr Mladek wrote:
[..]
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
extern void
printk_nmi_enter(void);
extern voidprintk_nmi_exit(void);include/linux/printk.h +2
-0+extern void printk_nmi_direct_enter (void ); +extern void printk_nmi_direct_exit (void );Quoted 8 lines
include/linux/printk.h void early_printk(const char *s, ...) { } · line 157
Quoted 5 lines
#else
static inline voidprintk_nmi_enter(void){ }
static inline voidprintk_nmi_exit(void){ }include/linux/printk.h +2
-0+static void printk_nmi_direct_enter (void ) { } +static void printk_nmi_direct_exit (void ) { }Can we have better names may be? Since direct
printk_nmiis not in fact always `direct'.
Quoted 2 lines
What about
printk_chatty_nmi_enter(),printk_large_nmi_enter()or something similar?
Hmm. Can't answer right now :)
Quoted 9 lines
Quoted 8 lines
kernel/printk/internal.h line 31
Quoted 5 lines
kernel/printk/internal.h +5
-0+#ifdef CONFIG_PRINTK_NMI +__printf(1, 0) int vprintk_nmi(const char *fmt, va_list args); +#else +__printf(1, 0) int vprintk_nmi(const char *fmt, va_list args) { return 0; } +#endif Hmm,
printk_safe.cknows aboutprintk.c,printk.cknows aboutprintk_safe.c.
Quoted 3 lines
I am sorry but I do not understand the problem. The function is defined in
printk_safe.cand we need to call it also fromprintk.c. It seems reasonable to declare it inkernel/printk/internal.h.
Just wanted to suggest to keep printk_safe/printk_nmi stuff in printk_safe.c. We already have everything we need there, so let's just add the vprintk_nmi() fallback, avoiding spreading printk_safe/printk_nmi logic and details across printk.c and printk_safe.c
Quoted 13 lines
Quoted 3 lines
OK... Can we do this in
vprintk_func()? The race window should be super tiny [if matters at all], but in exchange we don't have to mix nmi, printk,printk_mni, etc.You are right that it would still solve the main risk (NMI comes inside
logbuf_lockcritical section).In fact, the only real risk would be another lock serializing NMIs and
printk()called with that lock. This patch removes one innmi_backtrace()and I am not aware of any other.The less hairy code really might be worth the rather theoretical risk.
Quoted 7 lines
Quoted 4 lines
So over all I understand why you did it this way. May be I'd prefer to have less universal but shorter solution (e.g. modify only
nmi_backtracefunction and put there "printk_nmi_restricted_buffer"), but I won't really object your patch [unless I see some real issues with it].Thanks in advance. I'll send v2 once we have a conclusion on the function names and includes.
Does this mean that we agreed to handle the printk_nmi per-CPU buffer fallback in printk_safe.c?
-ss
Petr Mladek
maintainerQuoted 15 lines · Sergey Senozhatsky
On (06/18/18 11:39), Petr Mladek wrote:
[..]Quoted 13 lines
Quoted 12 lines
include/linux/printk.h void early_printk(const char *s, ...) { } · line 157
Quoted 9 lines
extern void
printk_nmi_enter(void);
extern voidprintk_nmi_exit(void);include/linux/printk.h +4
-0+extern void printk_nmi_direct_enter (void ); +extern void printk_nmi_direct_exit (void );#else static inline void printk_nmi_enter (void ) { }static inline void printk_nmi_exit (void ) { } +static void printk_nmi_direct_enter (void ) { } +static void printk_nmi_direct_exit (void ) { }Can we have better names may be? Since direct
printk_nmiis not in fact always `direct'.
Quoted 4 lines
Quoted 2 lines
What about
printk_chatty_nmi_enter(),printk_large_nmi_enter()or something similar?Hmm. Can't answer right now :)
Please, let me know what name you would like ;-)
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
kernel/printk/internal.h line 31
Quoted 5 lines
kernel/printk/internal.h +5
-0+#ifdef CONFIG_PRINTK_NMI +__printf(1, 0) int vprintk_nmi(const char *fmt, va_list args); +#else +__printf(1, 0) int vprintk_nmi(const char *fmt, va_list args) { return 0; } +#endif Hmm,
printk_safe.cknows aboutprintk.c,printk.cknows aboutprintk_safe.c.
Quoted 4 lines
Just wanted to suggest to keep printk_safe/printk_nmi stuff in
printk_safe.c. We already have everything we need there, so let's just add thevprintk_nmi()fallback, avoiding spreading printk_safe/printk_nmi logic and details acrossprintk.candprintk_safe.c
This won't be needed in v2 where the decision will be made in vprintk_func().
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
OK... Can we do this in
vprintk_func()? The race window should be super tiny [if matters at all], but in exchange we don't have to mix nmi, printk,printk_mni, etc.You are right that it would still solve the main risk (NMI comes inside
logbuf_lockcritical section).
Quoted 8 lines
Quoted 5 lines
In fact, the only real risk would be another lock serializing NMIs and
printk()called with that lock. This patch removes one innmi_backtrace()and I am not aware of any other.The less hairy code really might be worth the rather theoretical risk.
Does this mean that we agreed to handle the
printk_nmiper-CPU buffer fallback inprintk_safe.c?
Yes, I feel persuaded.
Best Regards,
Petr
Quoted 7 lines · Petr Mladek
Quoted 4 lines · Sergey Senozhatsky
On (06/18/18 11:39), Petr Mladek wrote:
[..]Hmm. Can't answer right now :)
Please, let me know what name you would like ;-)
:) Wow, it's hard. Maybe we can derive some bits from the ftrace_dump() function name? Dunno... Does printk_dump_nmi_enter() sound terrible?
Maybe Steven has some opinions on this?
-ss
Steven Rostedt
maintainerQuoted 5 lines · On Tue, 19 Jun 2018 17:27:16 +0900 Sergey Senozhatsky…
Quoted 4 lines · Petr Mladek
Quoted 3 lines · Sergey Senozhatsky
On (06/18/18 11:39), Petr Mladek wrote:
[..]
Quoted 9 lines
Quoted 3 lines
Quoted 1 line
Hmm. Can't answer right now :)
Please, let me know what name you would like ;-)
:) Wow, it's hard. Maybe we can derive some bits from the
ftrace_dump()function name? Dunno... Doesprintk_dump_nmi_enter()sound terrible?Maybe Steven has some opinions on this?
What exactly is the question?
Also, from a previous email in this thread, if you have to risk interleaved output to solve a deadlock, then just do that.
-- Steve
Quoted 9 lines · Steven Rostedt
Quoted 9 lines · Petr Mladek
Quoted 7 lines · Sergey Senozhatsky
Quoted 4 lines · Petr Mladek
[..]
Hmm. Can't answer right now :)
Please, let me know what name you would like ;-)
Quoted 7 lines
Quoted 5 lines
:) Wow, it's hard. Maybe we can derive some bits from the
ftrace_dump()function name? Dunno... Doesprintk_dump_nmi_enter()sound terrible?Maybe Steven has some opinions on this?
What exactly is the question?
Which one of these you'd prefer to see in ftrace_dump():
- printk_nmi_direct_enter() / printk_nmi_direct_exit()
- printk_chatty_nmi_enter() / printk_chatty_nmi_exit()
- printk_large_nmi_enter() / printk_large_nmi_exit()
- printk_dump_nmi_enter() / printk_dump_nmi_exit()
Quoted 2 lines
Also, from a previous email in this thread, if you have to risk interleaved output to solve a deadlock, then just do that.
Yep.
-ss
Steven Rostedt
maintainerQuoted 3 lines · On Wed, 20 Jun 2018 10:58:34 +0900 Sergey Senozhatsky…
Which one of these you'd prefer to see in
ftrace_dump():-
printk_nmi_direct_enter()/printk_nmi_direct_exit()
The above appears to be the most sane.
-- Steve
Quoted 3 lines
-
printk_chatty_nmi_enter()/printk_chatty_nmi_exit()
-printk_large_nmi_enter()/printk_large_nmi_exit()
-printk_dump_nmi_enter()/printk_dump_nmi_exit()
Quoted 8 lines · Steven Rostedt
Quoted 3 lines · On Wed, 20 Jun 2018 10:58:34 +0900 Sergey Senozhatsky…
Which one of these you'd prefer to see in
ftrace_dump():-
printk_nmi_direct_enter()/printk_nmi_direct_exit()The above appears to be the most sane.
OK. The original name suggested by Petr.
-ss