All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH]  printk: make printk_safe_flush safe in NMI context by skipping flushing
@ 2018-05-29  2:51 Hoeun Ryu
  2018-05-29 12:13 ` Sergey Senozhatsky
  0 siblings, 1 reply; 7+ messages in thread
From: Hoeun Ryu @ 2018-05-29  2:51 UTC (permalink / raw)
  To: Petr Mladek, Sergey Senozhatsky, Steven Rostedt; +Cc: Hoeun Ryu, linux-kernel

From: Hoeun Ryu <hoeun.ryu@lge.com>

 Make printk_safe_flush() safe in NMI context.
nmi_trigger_cpumask_backtrace() can be called in NMI context. For example the
function is called in watchdog_overflow_callback() if the flag of hardlockup
backtrace (sysctl_hardlockup_all_cpu_backtrace) is true and
watchdog_overflow_callback() function is called in NMI context on some
architectures.
 Calling printk_safe_flush() in nmi_trigger_cpumask_backtrace() eventually tries
to lock logbuf_lock in vprintk_emit() but the logbuf_lock can be already locked in
preempted contexts (task or irq in this case) or by other CPUs and it may cause
deadlocks.
 By making printk_safe_flush() safe in NMI context, the backtrace triggering CPU
just skips flushing if the lock is not avaiable in NMI context. The messages in
per-cpu nmi buffer of the backtrace triggering CPU can be lost if the CPU is in
hard lockup (because irq is disabled here) but if panic() is not called. The
flushing can be delayed by the next irq work in normal cases.

Suggested-by: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
Signed-off-by: Hoeun Ryu <hoeun.ryu@lge.com>
---
 kernel/printk/printk_safe.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/kernel/printk/printk_safe.c b/kernel/printk/printk_safe.c
index 3e3c200..62bcc9b 100644
--- a/kernel/printk/printk_safe.c
+++ b/kernel/printk/printk_safe.c
@@ -254,6 +254,16 @@ void printk_safe_flush(void)
 {
 	int cpu;
 
+	/*
+	 * Just avoid deadlocks here, we could loose the messages in per-cpu nmi buffer
+	 * in the case that hardlockup happens but panic() is not called (irq_work won't
+	 * work).
+	 * The flushing can be delayed by the next irq_work if flushing is skippped here
+	 * in normal cases.
+	 */
+	if (this_cpu_read(printk_context) & PRINTK_NMI_CONTEXT_MASK)
+		return;
+
 	for_each_possible_cpu(cpu) {
 #ifdef CONFIG_PRINTK_NMI
 		__printk_safe_flush(&per_cpu(nmi_print_seq, cpu).work);
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-06-01  9:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-29  2:51 [PATCH] printk: make printk_safe_flush safe in NMI context by skipping flushing Hoeun Ryu
2018-05-29 12:13 ` Sergey Senozhatsky
2018-05-29 22:43   ` Hoeun Ryu
2018-05-30  8:32   ` Petr Mladek
2018-06-01  1:55     ` Hoeun Ryu
2018-06-01  5:17     ` Hoeun Ryu
2018-06-01  9:05       ` Petr Mladek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.