Removal of printk safe buffers delays NMI context printk
Subsystems (3)
Nicholas Piggin
reviewerHi John,
It seems printk from NMI context is now delayed indefinitely and there is no printk_safe_flush equivalent (or I can't see one) to allow a NMI buffer to be flushed by a different CPU.
This causes hard lockup watchdog messages to not get shown on the console. I can call printk from a different CPU and that seems to flush the stuck CPU's NMI buffer immediately.
What's the best way to expose this? Can we have something like tihs?
void printk_flush(void)
{
preempt_disable();
if (console_trylock_spinning())
console_unlock();
preempt_enable();
wake_up_klogd();
}Thanks,
Nick
John Ogness
maintainerHi Nick,
Quoted 3 lines · Nicholas Piggin
It seems printk from NMI context is now delayed indefinitely and there is no
printk_safe_flushequivalent (or I can't see one) to allow a NMI buffer to be flushed by a different CPU.
NMI flushing is triggered using irq work (for the same CPU). This should not have changed recently. Are you reporting a new issue?
Quoted 3 lines
This causes hard lockup watchdog messages to not get shown on the console. I can call printk from a different CPU and that seems to flush the stuck CPU's NMI buffer immediately.
Perhaps we should be triggering the irq work on multiple CPUs if from NMI context?
Quoted 10 lines
What's the best way to expose this? Can we have something like tihs?
void
printk_flush(void){ preempt_disable(); if (console_trylock_spinning()) console_unlock(); preempt_enable(); wake_up_klogd(); }
We are planning on implementing a pr_flush() that will do something similar. But I am wondering how you are planning on triggering a CPU to call that function.
John Ogness
Nicholas Piggin
reviewerExcerpts from John Ogness's message of November 5, 2021 2:18 am:
Quoted 9 lines
Hi Nick,
Quoted 3 lines · Nicholas Piggin
It seems printk from NMI context is now delayed indefinitely and there is no
printk_safe_flushequivalent (or I can't see one) to allow a NMI buffer to be flushed by a different CPU.NMI flushing is triggered using irq work (for the same CPU). This should not have changed recently. Are you reporting a new issue?
Previously prink_safe_flush from another CPU would flush those out.
Quoted 6 lines
Quoted 3 lines
This causes hard lockup watchdog messages to not get shown on the console. I can call printk from a different CPU and that seems to flush the stuck CPU's NMI buffer immediately.
Perhaps we should be triggering the irq work on multiple CPUs if from NMI context?
Possibly. I don't know if it's really needed because the irq work should usually be fine, the problem are lockup detector messages where the printing CPU does not enable irqs after it returns from NMI.
Quoted 12 lines
Quoted 11 lines
What's the best way to expose this? Can we have something like tihs?
void
printk_flush(void){ preempt_disable(); if (console_trylock_spinning()) console_unlock(); preempt_enable(); wake_up_klogd(); }
Quoted 3 lines
We are planning on implementing a
pr_flush()that will do something similar. But I am wondering how you are planning on triggering a CPU to call that function.
Similar way as was removed by commit 93d102f094b ("printk: remove safe buffers") fro nmi_backtrace and powerpc watchdog. The following patch is what I'm using which seems to work for me.
What would pr_flush do differently and where were you planning to use it?
Thanks,
Nick
--
printk: restore flushing of NMI buffers on remote CPUs after NMI backtraces
printk from NMI context relies on irq work being raised on the local CPU to print to console. This can be a problem if the NMI was raised by a lockup detector to print lockup stack and regs, because the CPU may not enable irqs (because it is locked up).
Introduce printk_flush() that can be called from non-NMI context on another CPU to try to get those messages to the console.
arch/powerpc/kernel/watchdog.c +6 -0
@@ -227,6 +227,12 @@ static void watchdog_smp_panic(int cpu)cpumask_clear (&wd_smp_cpus_ipi); } +/* +* Force flush any remote buffers that might be stuck in IRQ context +* and therefore could not run their irq_work. +*/ +printk_flush (); +if (hardlockup_panic)nmi_panic (NULL ,"Hard LOCKUP" );
include/linux/printk.h +4 -0
@@ -198,6 +198,7 @@ void dump_stack_print_info(const char *log_lvl);void show_regs_print_info (const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; +void printk_flush (void );#else static inline __printf(1, 0) int vprintk(const char *s, va_list args)
@@ -274,6 +275,9 @@ static inline void dump_stack_lvl(const char *log_lvl)static inline void dump_stack (void ) { } +static inline void printk_flush (void ) +{ +}#endif #ifdef CONFIG_SMP
kernel/printk/printk.c +24 -17
54 lines not on this page · raw message
lib/nmi_backtrace.c +6 -0
@@ -75,6 +75,12 @@ void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,touch_softlockup_watchdog (); } +/* +* Force flush any remote buffers that might be stuck in IRQ context +* and therefore could not run their irq_work. +*/ +printk_flush (); +clear_bit_unlock (0 , &backtrace_flag);put_cpu (); }
John Ogness
maintainerQuoted 7 lines · Nicholas Piggin
Quoted 3 lines
We are planning on implementing a
pr_flush()that will do something similar. But I am wondering how you are planning on triggering a CPU to call that function.Similar way as was removed by commit
93d102f094b("printk: remove safe buffers") fronmi_backtraceand powerpc watchdog. The following patch is what I'm using which seems to work for me.
What was removed from 93d102f094b was irq_work triggering on all CPUs. Your patch is attempting to directly print from a non-NMI context. This is obviously a better approach if a caller is known to be non-NMI.
Also, before 93d102f094b the safe buffer flushing was performed before triggering the NMI backtraces (in some cases). So clearly it worked before by accident rather than by design. (Not that that really matters.)
Quoted 2 lines
What would
pr_flushdo differently and where were you planning to use it?
Currently, and particularly when we move to threaded printers, printk callers do not know when their messages are actually visible. However, in some cases the printk caller (or some related task) wants to be sure the message is visible.
pr_flush() will allow a task to block until all pending messages (at the time of the call) are output on all consoles.
Your patch is doing something similar for non-NMI contexts, except that it will exit early if it fails to get the console lock. For your case this is probably acceptable because you are only concerned that some context is printing the messages, even if the latest messages may not have been printed yet.
Quoted 18 lines
printk: restore flushing of NMI buffers on remote CPUs after NMI backtraces
printk from NMI context relies on irq work being raised on the local CPU to print to console. This can be a problem if the NMI was raised by a lockup detector to print lockup stack and regs, because the CPU may not enable irqs (because it is locked up).
Introduce
printk_flush()that can be called from non-NMI context on another CPU to try to get those messages to the console.Fixes 93d102f094be ("printk: remove safe buffers")Signed-off-by Nicholas Piggin <[email protected]>arch/powerpc/kernel/watchdog.c +0
-0
arch/powerpc/kernel/watchdog.c static void watchdog_smp_panic(int cpu) · line 235
Quoted 9 lines
arch/powerpc/kernel/watchdog.c +6
-0@@ -227,6 +227,12 @@ static void watchdog_smp_panic(int cpu)cpumask_clear (&wd_smp_cpus_ipi); } +/* +* Force flush any remote buffers that might be stuck in IRQ context +* and therefore could not run their irq_work. +*/ +printk_flush (); +
nmi_trigger_cpumask_backtrace() tracks and waits (up to 10 seconds) for the CPUs to print their backtrace before flushing. Here there is no waiting. I suppose in practice the NMI backtrace will be fast enough, but I am just wondering if there should be some sort of tracking/waiting like in nmi_trigger_cpumask_backtrace().
Quoted 42 lines
arch/powerpc/kernel/watchdog.c +0
-0if (hardlockup_panic)nmi_panic (NULL ,"Hard LOCKUP" );include/linux/printk.h +4
-0@@ -198,6 +198,7 @@ void dump_stack_print_info(const char *log_lvl);void show_regs_print_info (const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; +void printk_flush (void );#else static inline __printf(1, 0) int vprintk(const char *s, va_list args)@@ -274,6 +275,9 @@ static inline void dump_stack_lvl(const char *log_lvl)static inline void dump_stack (void ) { } +static inline void printk_flush (void ) +{ +}#endif #ifdef CONFIG_SMP kernel/printk/printk.c +9
-0@@ -2206,6 +2206,26 @@ int vprintk_store(int facility, int level,return ret; } +void printk_flush (void ) +{ +/* +* Disable preemption to avoid being preempted while holding +* console_sem which would prevent anyone from printing to +* console +*/ +preempt_disable (); +/*
kernel/printk/printk.c int vprintk_store(int facility, int level, · line 2227
Quoted 10 lines
kernel/printk/printk.c +10
-0+* Try to acquire and then immediately release the console +* semaphore. The release will print out buffers and wake up +* /dev/kmsg and syslog() users. +*/ +if (console_trylock_spinning ()) +console_unlock (); +preempt_enable (); + +wake_up_klogd (); +}
I have mixed feelings about this printk_flush() because it is exactly this code that is about to undergo massive reworking. If this function is accepted, then I wonder if printk_trigger_flush() might be a better name since it does not necessarily wait until the messages are printed. (There could be another task printing and already a queued handover printer. Or this task could handover printing before finishing.)
John Ogness
Nicholas Piggin
reviewerExcerpts from John Ogness's message of November 5, 2021 7:55 pm:
Quoted 11 lines · Nicholas Piggin
Quoted 7 lines
Quoted 3 lines
We are planning on implementing a
pr_flush()that will do something similar. But I am wondering how you are planning on triggering a CPU to call that function.Similar way as was removed by commit
93d102f094b("printk: remove safe buffers") fronmi_backtraceand powerpc watchdog. The following patch is what I'm using which seems to work for me.What was removed from
93d102f094bwasirq_worktriggering on all CPUs.
No, it was the caller executing the flush for all remote CPUs itself. irq work was not involved (and irq work can't be raised in a remote CPU from NMI context).
Quoted 8 lines
Your patch is attempting to directly print from a non-NMI context. This is obviously a better approach if a caller is known to be non-NMI.
Also, before
93d102f094bthe safe buffer flushing was performed before triggering the NMI backtraces (in some cases). So clearly it worked before by accident rather than by design. (Not that that really matters.)
That's not the case, in the core nmi_backtrace.c, the flush happens afterward.
In the powerpc code it's not actually the case either but it wasn't obvious why at least should have been commented. powerpc's trigger_allbutself_cpu_backtrace() is not a NMI IPI, just a normal one. So if a CPU takes it then it will flush its own buffer via irq_work. You only have to do the printk flush for the case of the true NMI IPI which comes before.
That code does need a bit more work for other reasons, but we do need that printk flush capability back there and for nmi_backtrace.
Quoted 8 lines
Quoted 2 lines
What would
pr_flushdo differently and where were you planning to use it?Currently, and particularly when we move to threaded printers, printk callers do not know when their messages are actually visible. However, in some cases the printk caller (or some related task) wants to be sure the message is visible.
Quoted 8 lines
pr_flush()will allow a task to block until all pending messages (at the time of the call) are output on all consoles.Your patch is doing something similar for non-NMI contexts, except that it will exit early if it fails to get the console lock. For your case this is probably acceptable because you are only concerned that some context is printing the messages, even if the latest messages may not have been printed yet.
Yep, if something else has the lock hopefully it should release it and do the flush when it does. We're already 10 seconds delayed here so a bit longer doesn't matter, just needs to get out at some point.
Quoted 21 lines
Quoted 21 lines
printk: restore flushing of NMI buffers on remote CPUs after NMI backtraces
printk from NMI context relies on irq work being raised on the local CPU to print to console. This can be a problem if the NMI was raised by a lockup detector to print lockup stack and regs, because the CPU may not enable irqs (because it is locked up).
Introduce
printk_flush()that can be called from non-NMI context on another CPU to try to get those messages to the console.Fixes 93d102f094be ("printk: remove safe buffers")Signed-off-by Nicholas Piggin <[email protected]>arch/powerpc/kernel/watchdog.c +0
-0@@ -227,6 +227,12 @@ static void watchdog_smp_panic(int cpu)cpumask_clear (&wd_smp_cpus_ipi); }
Quoted 12 lines
arch/powerpc/kernel/watchdog.c static void watchdog_smp_panic(int cpu) · line 235
Quoted 6 lines
arch/powerpc/kernel/watchdog.c +6
-0+/* +* Force flush any remote buffers that might be stuck in IRQ context +* and therefore could not run their irq_work. +*/ +printk_flush (); +
nmi_trigger_cpumask_backtrace()tracks and waits (up to 10 seconds) for the CPUs to print their backtrace before flushing. Here there is no waiting. I suppose in practice the NMI backtrace will be fast enough, but I am just wondering if there should be some sort of tracking/waiting like innmi_trigger_cpumask_backtrace().
Yeah it has tended to work, but I have something to fix that up.
Quoted 48 lines
Quoted 48 lines
arch/powerpc/kernel/watchdog.c +0
-0if (hardlockup_panic)nmi_panic (NULL ,"Hard LOCKUP" );include/linux/printk.h +4
-0@@ -198,6 +198,7 @@ void dump_stack_print_info(const char *log_lvl);void show_regs_print_info (const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; +void printk_flush (void );#else static inline __printf(1, 0) int vprintk(const char *s, va_list args)@@ -274,6 +275,9 @@ static inline void dump_stack_lvl(const char *log_lvl)static inline void dump_stack (void ) { } +static inline void printk_flush (void ) +{ +}#endif #ifdef CONFIG_SMP kernel/printk/printk.c +14
-0@@ -2206,6 +2206,26 @@ int vprintk_store(int facility, int level,return ret; } +void printk_flush (void ) +{ +/* +* Disable preemption to avoid being preempted while holding +* console_sem which would prevent anyone from printing to +* console +*/ +preempt_disable (); +/* +* Try to acquire and then immediately release the console +* semaphore. The release will print out buffers and wake up +* /dev/kmsg and syslog() users. +*/ +if (console_trylock_spinning ())
Quoted 8 lines
kernel/printk/printk.c int vprintk_store(int facility, int level, · line 2227
Quoted 5 lines
kernel/printk/printk.c +5
-0+console_unlock (); +preempt_enable (); + +wake_up_klogd (); +}I have mixed feelings about this
printk_flush()because it is exactly this code that is about to undergo massive reworking.
Better to fix up the regression before said massive reworking I think.
Quoted 6 lines
If this function
is accepted, then I wonder ifprintk_trigger_flush()might be a better name since it does not necessarily wait until the messages are printed. (There could be another task printing and already a queued handover printer. Or this task could handover printing before finishing.)
Sure I'm not wedded to the name. I can resend with the name change if there's no other issues (the powerpc fixups will go as separate series).
Thanks,
Nick
John Ogness
maintainerQuoted 6 lines · Nicholas Piggin
Quoted 2 lines
What was removed from
93d102f094bwasirq_worktriggering on all CPUs.No, it was the caller executing the flush for all remote CPUs itself. irq work was not involved (and irq work can't be raised in a remote CPU from NMI context).
Maybe I am missing something. In 93d102f094b~1 I see:
watchdog_smp_panic
printk_safe_flush
__printk_safe_flush
printk_safe_flush_buffer
printk_safe_flush_line
printk_deferred
vprintk_deferred
vprintk_emit (but no direct printing) defer_console_output
irq_work_queue
AFAICT, using defer_console_output() instead of your new printk_flush() should cause the exact behavior as before.
Quoted 2 lines
but we do need that printk flush capability back there and for
nmi_backtrace.
Agreed. I had not considered this necessary side-effect when I removed the NMI safe buffers.
I am just wondering if we should fix the regression by going back to using irq_work (such as defer_console_output()) or if we want to introduce something new that introduces direct printing.
John Ogness
Petr Mladek
maintainerQuoted 45 lines · Nicholas Piggin
printk: restore flushing of NMI buffers on remote CPUs after NMI backtraces
printk from NMI context relies on irq work being raised on the local CPU to print to console. This can be a problem if the NMI was raised by a lockup detector to print lockup stack and regs, because the CPU may not enable irqs (because it is locked up).
Introduce
printk_flush()that can be called from non-NMI context on another CPU to try to get those messages to the console.Fixes 93d102f094be ("printk: remove safe buffers")Signed-off-by Nicholas Piggin <[email protected]>kernel/printk/printk.c +20
-0@@ -2206,6 +2206,26 @@ int vprintk_store(int facility, int level,return ret; } +void printk_flush (void ) +{ +/* +* Disable preemption to avoid being preempted while holding +* console_sem which would prevent anyone from printing to +* console +*/ +preempt_disable (); +/* +* Try to acquire and then immediately release the console +* semaphore. The release will print out buffers and wake up +* /dev/kmsg and syslog() users. +*/ +if (console_trylock_spinning ()) +console_unlock (); +preempt_enable (); + +wake_up_klogd (); +} + asmlinkage int vprintk_emit(int facility, int level,const struct dev_printk_info *dev_info,const char *fmt,va_list args)
lib/nmi_backtrace.c void nmi_trigger_cpumask_backtrace(const cpumask_t *mask, · line 82
Quoted 8 lines
lib/nmi_backtrace.c +5
-0@@ -75,6 +75,12 @@ void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,touch_softlockup_watchdog (); } +/* +* Force flush any remote buffers that might be stuck in IRQ context +* and therefore could not run their irq_work. +*/ +printk_flush ();
IMHO, nmi_trigger_cpumask_backtrace() might be called also from NMI context.
printk_flush() is not safe in NMI context because console drivers have their own locks. Even conosle_trylock() takes a lock, see raw_spin_lock_irqsave(&sem->lock, flags) in down_trylock().
Best Regards,
Petr
Quoted 4 lines
lib/nmi_backtrace.c +1
-0+clear_bit_unlock (0 , &backtrace_flag);put_cpu (); }
Petr Mladek
maintainerQuoted 21 lines · John Ogness
Quoted 6 lines · Nicholas Piggin
Quoted 2 lines
What was removed from
93d102f094bwasirq_worktriggering on all CPUs.No, it was the caller executing the flush for all remote CPUs itself. irq work was not involved (and irq work can't be raised in a remote CPU from NMI context).
Maybe I am missing something. In 93d102f094b~1 I see:
watchdog_smp_panic
printk_safe_flush
__printk_safe_flush
printk_safe_flush_buffer
printk_safe_flush_line
printk_deferred
vprintk_deferred
vprintk_emit(but no direct printing)defer_console_output
irq_work_queue
Quoted 2 lines
AFAICT, using
defer_console_output()instead of your newprintk_flush()should cause the exact behavior as before.
I agree. printk_safe_flush() used printk_deferred(). It only queued the irq_work and never called consoles directly.
Quoted 5 lines
Quoted 2 lines
but we do need that printk flush capability back there and for
nmi_backtrace.Agreed. I had not considered this necessary side-effect when I removed the NMI safe buffers.
Honestly, I do not understand why it stopped working or how it worked before.
printk() calls vprintk(). Current vprintk() does:
asmlinkage int vprintk(const char *fmt, va_list args) {
[...]
/*
* Use the main logbuf even in NMI. But avoid calling console
* drivers that might have their own locks.
*/
if (this_cpu_read(printk_context) || in_nmi()) {
int len;
len = vprintk_store(0, LOGLEVEL_DEFAULT, NULL, fmt, args);
defer_console_output();
return len;
}
/* No obstacles. */
return vprintk_default(fmt, args);
}By other words, current vprintk():
+ queues irq_work() in NMI context
+ tries to flush consoles immeditely otherwise
Quoted 3 lines
I am just wondering if we should fix the regression by going back to using
irq_work(such asdefer_console_output()) or if we want to introduce something new that introduces direct printing.
Yup, defer_console_output() should do the same as printk_safe_flush() before. We do not longer need to copy the messages because they are already in the main lockless log buffer.
Well, I am curious about the original code. The commit 93d102f094be9beab28e5 ("printk: remove safe buffers") did the following:
arch/powerpc/kernel/watchdog.c +0 -5
@@ -183,11 +183,6 @@ static void watchdog_smp_panic(int cpu, u64 tb)wd_smp_unlock (&flags); -printk_safe_flush (); -/* -* printk_safe_flush() seems to require another print -* before anything actually goes out to console. -*/ if (sysctl_hardlockup_all_cpu_backtrace)trigger_allbutself_cpu_backtrace ();
And I am curious because:
+ Why was printk_safe_flush() called before triggering backtraces on other CPUs?
+ The comment says that another print is needed before the messages goes to the console. It makes sense because printk_safe_flush() only set irq_work. But the patch did not remove any printk(). So, nobody called any printk() even before.
Best Regards,
Petr
John Ogness
maintainerQuoted 10 lines · Petr Mladek
Quoted 6 lines · John Ogness
Quoted 2 lines · Nicholas Piggin
but we do need that printk flush capability back there and for
nmi_backtrace.Agreed. I had not considered this necessary side-effect when I removed the NMI safe buffers.
Honestly, I do not understand why it stopped working or how it worked before.
IIUC, Nick is presenting a problem where a lockup on the other CPUs is detected. Those CPUs will dump their backtraces per NMI context. But in their lockup state the irq_work for those CPUs is not functional. So even though the messages are in the buffer, there is no one printing the buffer.
printk_safe_flush() would dump the NMI safe buffers for all the CPUs into the printk buffer, then trigger an irq_work on itself (the non-locked-up CPU).
That irq_work trigger was critical, because the other CPUs (which also triggered irq_works for themselves) aren't able to process irq_works. I did not consider this case. Which is why we still need to trigger irq_work here. (Or, as the removed comment hinted at, add some printk() call to either directly print or trigger the irq_work.)
John Ogness
Nicholas Piggin
reviewerExcerpts from John Ogness's message of November 5, 2021 11:57 pm:
Quoted 10 lines · Nicholas Piggin
Quoted 6 lines
Quoted 2 lines
What was removed from
93d102f094bwasirq_worktriggering on all CPUs.No, it was the caller executing the flush for all remote CPUs itself. irq work was not involved (and irq work can't be raised in a remote CPU from NMI context).
Maybe I am missing something. In 93d102f094b~1 I see:
Quoted 10 lines
watchdog_smp_panic
printk_safe_flush
__printk_safe_flush
printk_safe_flush_buffer
printk_safe_flush_line
printk_deferred
vprintk_deferred
vprintk_emit(but no direct printing)defer_console_output
irq_work_queue
Oh I thought you meant irq_work triggering on all CPUs (i.e., including remote CPUs) was the key.
Quoted 2 lines
AFAICT, using
defer_console_output()instead of your newprintk_flush()should cause the exact behavior as before.
It does.
Quoted 9 lines
Quoted 2 lines
but we do need that printk flush capability back there and for
nmi_backtrace.Agreed. I had not considered this necessary side-effect when I removed the NMI safe buffers.
I am just wondering if we should fix the regression by going back to using
irq_work(such asdefer_console_output()) or if we want to introduce something new that introduces direct printing.
irq_work works for this situation so for a minimal fix I think it's fine. When you do the big rework it would be okay to do it directly if you have such a facility for other reaons.
Thanks,
Nick
Nicholas Piggin
reviewerExcerpts from Petr Mladek's message of November 6, 2021 2:23 am:
Quoted 93 lines · John Ogness
Quoted 21 lines · Nicholas Piggin
Quoted 6 lines
Quoted 2 lines
What was removed from
93d102f094bwasirq_worktriggering on all CPUs.No, it was the caller executing the flush for all remote CPUs itself. irq work was not involved (and irq work can't be raised in a remote CPU from NMI context).
Maybe I am missing something. In 93d102f094b~1 I see:
watchdog_smp_panic
printk_safe_flush
__printk_safe_flush
printk_safe_flush_buffer
printk_safe_flush_line
printk_deferred
vprintk_deferred
vprintk_emit(but no direct printing)defer_console_output
irq_work_queueQuoted 2 lines
AFAICT, using
defer_console_output()instead of your newprintk_flush()should cause the exact behavior as before.I agree.
printk_safe_flush()usedprintk_deferred(). It only queued theirq_workand never called consoles directly.Quoted 5 lines
Quoted 2 lines
but we do need that printk flush capability back there and for
nmi_backtrace.Agreed. I had not considered this necessary side-effect when I removed the NMI safe buffers.
Honestly, I do not understand why it stopped working or how it worked before.
printk()callsvprintk(). Currentvprintk()does:asmlinkage int
vprintk(const char *fmt, va_list args){
[...]/* * Use the main logbuf even in NMI. But avoid calling console * drivers that might have their own locks. */ if (this_cpu_read(printk_context) || in_nmi()) { int len; len = vprintk_store(0, LOGLEVEL_DEFAULT, NULL, fmt, args); defer_console_output(); return len; } /* No obstacles. */ return vprintk_default(fmt, args); }By other words, current
vprintk():+ queues
irq_work()in NMI context
+ tries to flush consoles immeditely otherwiseQuoted 3 lines
I am just wondering if we should fix the regression by going back to using
irq_work(such asdefer_console_output()) or if we want to introduce something new that introduces direct printing.Yup,
defer_console_output()should do the same asprintk_safe_flush()before. We do not longer need to copy the messages because they are already in the main lockless log buffer.Well, I am curious about the original code. The commit
93d102f094be9beab28e5("printk: remove safe buffers") did the following:arch/powerpc/kernel/watchdog.c +0
-5@@ -183,11 +183,6 @@ static void watchdog_smp_panic(int cpu, u64 tb)wd_smp_unlock (&flags); -printk_safe_flush (); -/* -* printk_safe_flush() seems to require another print -* before anything actually goes out to console. -*/ if (sysctl_hardlockup_all_cpu_backtrace)trigger_allbutself_cpu_backtrace ();And I am curious because:
Quoted 2 lines
+ Why was
printk_safe_flush()called before triggering backtraces on other CPUs?
I commented about that earlier in the thread. The flush is for a previous non-maskable IPI. trigger_allbutself_cpu_backtrace is not NMI on powerpc it's just regular irq context so if they did respond to it they would also be able to run the irq work.
Quoted 4 lines
+ The comment says that another print is needed before the messages goes to the console. It makes sense because
printk_safe_flush()only setirq_work. But the patch did not remove anyprintk(). So, nobody called anyprintk()even before.
I think that was my misunderstanding as to the problem. The NMI IPI is actually asynchronous so it was not guaranteed that targets had completed their backtraces at this point. powerpc watchdog needs a separate fix (after which it seems to work fine with printk_safe_flush)
Thanks,
Nick
Nicholas Piggin
reviewerExcerpts from John Ogness's message of November 6, 2021 2:44 am:
Quoted 9 lines · Petr Mladek
Quoted 8 lines · John Ogness
Quoted 6 lines · Nicholas Piggin
Quoted 2 lines
but we do need that printk flush capability back there and for
nmi_backtrace.Agreed. I had not considered this necessary side-effect when I removed the NMI safe buffers.
Quoted 8 lines
Quoted 2 lines
Honestly, I do not understand why it stopped working or how it worked before.
IIUC, Nick is presenting a problem where a lockup on the other CPUs is detected. Those CPUs will dump their backtraces per NMI context. But in their lockup state the
irq_workfor those CPUs is not functional. So even though the messages are in the buffer, there is no one printing the buffer.
Yep.
Quoted 9 lines
printk_safe_flush()would dump the NMI safe buffers for all the CPUs into the printk buffer, then trigger anirq_workon itself (the non-locked-up CPU).That
irq_worktrigger was critical, because the other CPUs (which also triggeredirq_worksfor themselves) aren't able to processirq_works. I did not consider this case. Which is why we still need to triggerirq_workhere. (Or, as the removed comment hinted at, add someprintk()call to either directly print or trigger theirq_work.)
This patch seems to work, I can submit it if you'd like?
Thanks,
Nick
--- arch/powerpc/kernel/watchdog.c | 6 ++++++ include/linux/printk.h | 4 ++++ kernel/printk/printk.c | 5 +++++ lib/nmi_backtrace.c | 6 ++++++ 4 files changed, 21 insertions(+)
arch/powerpc/kernel/watchdog.c +6 -0
@@ -227,6 +227,12 @@ static void watchdog_smp_panic(int cpu)cpumask_clear (&wd_smp_cpus_ipi); } +/* +* Force flush any remote buffers that might be stuck in IRQ context +* and therefore could not run their irq_work. +*/ +printk_trigger_flush (); +if (hardlockup_panic)nmi_panic (NULL ,"Hard LOCKUP" );
include/linux/printk.h +4 -0
@@ -198,6 +198,7 @@ void dump_stack_print_info(const char *log_lvl);void show_regs_print_info (const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; +void printk_trigger_flush (void );#else static inline __printf(1, 0) int vprintk(const char *s, va_list args)
@@ -274,6 +275,9 @@ static inline void dump_stack_lvl(const char *log_lvl)static inline void dump_stack (void ) { } +static inline void printk_trigger_flush (void ) +{ +}#endif #ifdef CONFIG_SMP
kernel/printk/printk.c +5 -0
@@ -3252,6 +3252,11 @@ void defer_console_output(void)preempt_enable (); } +void printk_trigger_flush (void ) +{ +defer_console_output (); +} +int vprintk_deferred (const char *fmt,va_list args) {int r;
lib/nmi_backtrace.c +6 -0
@@ -75,6 +75,12 @@ void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,touch_softlockup_watchdog (); } +/* +* Force flush any remote buffers that might be stuck in IRQ context +* and therefore could not run their irq_work. +*/ +printk_trigger_flush (); +clear_bit_unlock (0 , &backtrace_flag);put_cpu (); }
Signature · 1 line
2.23.0
John Ogness
maintainerQuoted 1 line · Nicholas Piggin
This patch seems to work, I can submit it if you'd like?
It looks good to me. Thanks for identifying and fixing this regression.
John Ogness
Quoted 81 lines
--- arch/powerpc/kernel/watchdog.c | 6 ++++++ include/linux/printk.h | 4 ++++ kernel/printk/printk.c | 5 +++++ lib/nmi_backtrace.c | 6 ++++++ 4 files changed, 21 insertions(+)arch/powerpc/kernel/watchdog.c +6
-0@@ -227,6 +227,12 @@ static void watchdog_smp_panic(int cpu)cpumask_clear (&wd_smp_cpus_ipi); } +/* +* Force flush any remote buffers that might be stuck in IRQ context +* and therefore could not run their irq_work. +*/ +printk_trigger_flush (); +if (hardlockup_panic)nmi_panic (NULL ,"Hard LOCKUP" );include/linux/printk.h +4
-0@@ -198,6 +198,7 @@ void dump_stack_print_info(const char *log_lvl);void show_regs_print_info (const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; +void printk_trigger_flush (void );#else static inline __printf(1, 0) int vprintk(const char *s, va_list args)@@ -274,6 +275,9 @@ static inline void dump_stack_lvl(const char *log_lvl)static inline void dump_stack (void ) { } +static inline void printk_trigger_flush (void ) +{ +}#endif #ifdef CONFIG_SMP kernel/printk/printk.c +5
-0@@ -3252,6 +3252,11 @@ void defer_console_output(void)preempt_enable (); } +void printk_trigger_flush (void ) +{ +defer_console_output (); +} +int vprintk_deferred (const char *fmt,va_list args) {int r;lib/nmi_backtrace.c +6
-0@@ -75,6 +75,12 @@ void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,touch_softlockup_watchdog (); } +/* +* Force flush any remote buffers that might be stuck in IRQ context +* and therefore could not run their irq_work. +*/ +printk_trigger_flush (); +clear_bit_unlock (0 , &backtrace_flag);put_cpu (); }Signature · 1 line
2.23.0