]> Git Repo - linux.git/commit
printk/nmi: generic solution for safe printk in NMI
authorPetr Mladek <[email protected]>
Sat, 21 May 2016 00:00:33 +0000 (17:00 -0700)
committerLinus Torvalds <[email protected]>
Sat, 21 May 2016 00:58:30 +0000 (17:58 -0700)
commit42a0bb3f71383b457a7db362f1c69e7afb96732b
treec63f12bed74fee20662fbcc8cc985d53a0d20def
parent2eeed7e98d6a1341b1574893a95ce5b8379140f2
printk/nmi: generic solution for safe printk in NMI

printk() takes some locks and could not be used a safe way in NMI
context.

The chance of a deadlock is real especially when printing stacks from
all CPUs.  This particular problem has been addressed on x86 by the
commit a9edc8809328 ("x86/nmi: Perform a safe NMI stack trace on all
CPUs").

The patchset brings two big advantages.  First, it makes the NMI
backtraces safe on all architectures for free.  Second, it makes all NMI
messages almost safe on all architectures (the temporary buffer is
limited.  We still should keep the number of messages in NMI context at
minimum).

Note that there already are several messages printed in NMI context:
WARN_ON(in_nmi()), BUG_ON(in_nmi()), anything being printed out from MCE
handlers.  These are not easy to avoid.

This patch reuses most of the code and makes it generic.  It is useful
for all messages and architectures that support NMI.

The alternative printk_func is set when entering and is reseted when
leaving NMI context.  It queues IRQ work to copy the messages into the
main ring buffer in a safe context.

__printk_nmi_flush() copies all available messages and reset the buffer.
Then we could use a simple cmpxchg operations to get synchronized with
writers.  There is also used a spinlock to get synchronized with other
flushers.

We do not longer use seq_buf because it depends on external lock.  It
would be hard to make all supported operations safe for a lockless use.
It would be confusing and error prone to make only some operations safe.

The code is put into separate printk/nmi.c as suggested by Steven
Rostedt.  It needs a per-CPU buffer and is compiled only on
architectures that call nmi_enter().  This is achieved by the new
HAVE_NMI Kconfig flag.

The are MN10300 and Xtensa architectures.  We need to clean up NMI
handling there first.  Let's do it separately.

The patch is heavily based on the draft from Peter Zijlstra, see

  https://lkml.org/lkml/2015/6/10/327

[[email protected]: printk-nmi: use %zu format string for size_t]
[[email protected]: min_t->min - all types are size_t here]
Signed-off-by: Petr Mladek <[email protected]>
Suggested-by: Peter Zijlstra <[email protected]>
Suggested-by: Steven Rostedt <[email protected]>
Cc: Jan Kara <[email protected]>
Acked-by: Russell King <[email protected]> [arm part]
Cc: Daniel Thompson <[email protected]>
Cc: Jiri Kosina <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ralf Baechle <[email protected]>
Cc: Benjamin Herrenschmidt <[email protected]>
Cc: Martin Schwidefsky <[email protected]>
Cc: David Miller <[email protected]>
Cc: Daniel Thompson <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
24 files changed:
arch/Kconfig
arch/arm/Kconfig
arch/arm/kernel/smp.c
arch/avr32/Kconfig
arch/blackfin/Kconfig
arch/cris/Kconfig
arch/mips/Kconfig
arch/powerpc/Kconfig
arch/s390/Kconfig
arch/sh/Kconfig
arch/sparc/Kconfig
arch/tile/Kconfig
arch/x86/Kconfig
arch/x86/kernel/apic/hw_nmi.c
include/linux/hardirq.h
include/linux/percpu.h
include/linux/printk.h
init/Kconfig
init/main.c
kernel/printk/Makefile
kernel/printk/internal.h [new file with mode: 0644]
kernel/printk/nmi.c [new file with mode: 0644]
kernel/printk/printk.c
lib/nmi_backtrace.c
This page took 0.077097 seconds and 4 git commands to generate.