printk/nmi: generic solution for safe printk in NMI
Subsystems (10)
[email protected]
maintainerPATCH 81/162 printk/nmi: generic solution for safe printk in NMI
From: Petr Mladek <[email protected]>
Subject: 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
---
arch/Kconfig | 4 arch/arm/Kconfig | 1 arch/arm/kernel/smp.c | 2 arch/avr32/Kconfig | 1 arch/blackfin/Kconfig | 1 arch/cris/Kconfig | 1 arch/mips/Kconfig | 1 arch/powerpc/Kconfig | 1 arch/s390/Kconfig | 1 arch/sh/Kconfig | 1 arch/sparc/Kconfig | 1 arch/tile/Kconfig | 1 arch/x86/Kconfig | 1 arch/x86/kernel/apic/hw_nmi.c | 1 include/linux/hardirq.h | 2 include/linux/percpu.h | 3 include/linux/printk.h | 12 + init/Kconfig | 5 init/main.c | 1 kernel/printk/Makefile | 1 kernel/printk/internal.h | 44 ++++++ kernel/printk/nmi.c | 219 ++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 19 -- lib/nmi_backtrace.c | 89 ------------- 24 files changed, 306 insertions(+), 107 deletions(-)
arch/Kconfig renamed from arch/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +4 -0
@@ -187,7 +187,11 @@ config HAVE_OPTPROBES config HAVE_KPROBES_ON_FTRACE bool +config HAVE_NMI + bool + config HAVE_NMI_WATCHDOG + depends on HAVE_NMI bool # # An arch should select this if it provides all these things:
arch/arm/Kconfig renamed from arch/arm/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -67,6 +67,7 @@ config ARM select HAVE_KRETPROBES if (HAVE_KPROBES) select HAVE_MEMBLOCK select HAVE_MOD_ARCH_SPECIFIC + select HAVE_NMI select HAVE_OPROFILE if (HAVE_PERF_EVENTS) select HAVE_OPTPROBES if !THUMB2_KERNEL select HAVE_PERF_EVENTS
arch/arm/kernel/smp.c renamed from arch/arm/kernel/smp.c~printk-nmi-generic-solution-for-safe-printk-in-nmi +2 -0
@@ -644,9 +644,11 @@ void handle_IPI(int ipinr, struct pt_regbreak ;case IPI_CPU_BACKTRACE : +printk_nmi_enter ();irq_enter ();nmi_cpu_backtrace (regs);irq_exit (); +printk_nmi_exit ();break ;default :
arch/avr32/Kconfig renamed from arch/avr32/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -18,6 +18,7 @@ config AVR32 select GENERIC_CLOCKEVENTS select HAVE_MOD_ARCH_SPECIFIC select MODULES_USE_ELF_RELA + select HAVE_NMI help AVR32 is a high-performance 32-bit RISC microprocessor core, designed for cost-sensitive embedded applications, with particular
arch/blackfin/Kconfig renamed from arch/blackfin/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -40,6 +40,7 @@ config BLACKFIN select HAVE_MOD_ARCH_SPECIFIC select MODULES_USE_ELF_RELA select HAVE_DEBUG_STACKOVERFLOW + select HAVE_NMI config GENERIC_CSUM def_bool y
arch/cris/Kconfig renamed from arch/cris/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -70,6 +70,7 @@ config CRIS select GENERIC_CLOCKEVENTS if ETRAX_ARCH_V32 select GENERIC_SCHED_CLOCK if ETRAX_ARCH_V32 select HAVE_DEBUG_BUGVERBOSE if ETRAX_ARCH_V32 + select HAVE_NMI config HZ int
arch/mips/Kconfig renamed from arch/mips/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -48,6 +48,7 @@ config MIPS select GENERIC_SCHED_CLOCK if !CAVIUM_OCTEON_SOC select GENERIC_CMOS_UPDATE select HAVE_MOD_ARCH_SPECIFIC + select HAVE_NMI select VIRT_TO_BUS select MODULES_USE_ELF_REL if MODULES select MODULES_USE_ELF_RELA if MODULES && 64BIT
arch/powerpc/Kconfig renamed from arch/powerpc/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -155,6 +155,7 @@ config PPC select NO_BOOTMEM select HAVE_GENERIC_RCU_GUP select HAVE_PERF_EVENTS_NMI if PPC64 + select HAVE_NMI if PERF_EVENTS select EDAC_SUPPORT select EDAC_ATOMIC_SCRUB select ARCH_HAS_DMA_SET_COHERENT_MASK
arch/s390/Kconfig renamed from arch/s390/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -166,6 +166,7 @@ config S390 select TTY select VIRT_CPU_ACCOUNTING select VIRT_TO_BUS + select HAVE_NMI config SCHED_OMIT_FRAME_POINTER
arch/sh/Kconfig renamed from arch/sh/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -44,6 +44,7 @@ config SUPERH select OLD_SIGSUSPEND select OLD_SIGACTION select HAVE_ARCH_AUDITSYSCALL + select HAVE_NMI help The SuperH is a RISC processor targeted for use in embedded systems and consumer electronics; it was also used in the Sega Dreamcast
arch/sparc/Kconfig renamed from arch/sparc/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -79,6 +79,7 @@ config SPARC64 select NO_BOOTMEM select HAVE_ARCH_AUDITSYSCALL select ARCH_SUPPORTS_ATOMIC_RMW + select HAVE_NMI config ARCH_DEFCONFIG string
arch/tile/Kconfig renamed from arch/tile/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -30,6 +30,7 @@ config TILE select HAVE_DEBUG_STACKOVERFLOW select ARCH_WANT_FRAME_POINTERS select HAVE_CONTEXT_TRACKING + select HAVE_NMI if USE_PMC select EDAC_SUPPORT select GENERIC_STRNCPY_FROM_USER select GENERIC_STRNLEN_USER
arch/x86/Kconfig renamed from arch/x86/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -131,6 +131,7 @@ config X86 select HAVE_MEMBLOCK select HAVE_MEMBLOCK_NODE_MAP select HAVE_MIXED_BREAKPOINTS_REGS + select HAVE_NMI select HAVE_OPROFILE select HAVE_OPTPROBES select HAVE_PCSPKR_PLATFORM
arch/x86/kernel/apic/hw_nmi.c renamed from arch/x86/kernel/apic/hw_nmi.c~printk-nmi-generic-solution-for-safe-printk-in-nmi +0 -1
@@ -18,7 +18,6 @@#include <linux/nmi.h> #include <linux/module.h> #include <linux/delay.h> -#include <linux/seq_buf.h> #ifdef CONFIG_HARDLOCKUP_DETECTOR u64 hw_nmi_get_sample_period (int watchdog_thresh)
include/linux/hardirq.h renamed from include/linux/hardirq.h~printk-nmi-generic-solution-for-safe-printk-in-nmi +2 -0
@@ -61,6 +61,7 @@ extern void irq_exit(void);#define nmi_enter () \ do { \ + printk_nmi_enter(); \ lockdep_off(); \ ftrace_nmi_enter(); \ BUG_ON(in_nmi()); \
@@ -77,6 +78,7 @@ extern void irq_exit(void);preempt_count_sub (NMI_OFFSET +HARDIRQ_OFFSET ); \ftrace_nmi_exit (); \lockdep_on (); \ +printk_nmi_exit (); \ } while (0)#endif /* LINUX_HARDIRQ_H */
include/linux/percpu.h renamed from include/linux/percpu.h~printk-nmi-generic-solution-for-safe-printk-in-nmi +0 -3
@@ -129,7 +129,4 @@ extern phys_addr_t per_cpu_ptr_to_phys(v (typeof(type) __percpu *)__alloc_percpu(sizeof(type), \ __alignof__(type)) -/* To avoid include hell, as printk can not declare this, we declare it here */ -DECLARE_PER_CPU (printk_func_t, printk_func); -#endif /* __LINUX_PERCPU_H */
include/linux/printk.h renamed from include/linux/printk.h~printk-nmi-generic-solution-for-safe-printk-in-nmi +11 -1
@@ -122,7 +122,17 @@ static inline __printf(1, 2) __coldvoid early_printk (const char *s, ...) { }#endif -typedef __printf(1, 0) int (*printk_func_t)(const char *fmt, va_list args); +#ifdef CONFIG_PRINTK_NMI +extern void printk_nmi_init (void ); +extern void printk_nmi_enter (void ); +extern void printk_nmi_exit (void ); +extern void printk_nmi_flush (void ); +#else +static inline void printk_nmi_init (void ) { } +static inline void printk_nmi_enter (void ) { } +static inline void printk_nmi_exit (void ) { } +static inline void printk_nmi_flush (void ) { } +#endif /* PRINTK_NMI */ #ifdef CONFIG_PRINTK asmlinkage __printf(5, 0)
init/Kconfig renamed from init/Kconfig~printk-nmi-generic-solution-for-safe-printk-in-nmi +5 -0
@@ -1454,6 +1454,11 @@ config PRINTK very difficult to diagnose system problems, saying N here is strongly discouraged. +config PRINTK_NMI + def_bool y + depends on PRINTK + depends on HAVE_NMI + config BUG bool "BUG() support" if EXPERT default y
init/main.c renamed from init/main.c~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -569,6 +569,7 @@ asmlinkage __visible void __init start_ktimekeeping_init ();time_init ();sched_clock_postinit (); +printk_nmi_init ();perf_event_init ();profile_init ();call_function_init ();
kernel/printk/Makefile renamed from kernel/printk/Makefile~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -0
@@ -1,2 +1,3 @@ obj-y = printk.o +obj-$(CONFIG_PRINTK_NMI) += nmi.o obj-$(CONFIG_A11Y_BRAILLE_CONSOLE) += braille.o
kernel/printk/internal.h new file +44 -0
44 lines not on this page · raw message
kernel/printk/nmi.c new file +219 -0
219 lines not on this page · raw message
kernel/printk/printk.c renamed from kernel/printk/printk.c~printk-nmi-generic-solution-for-safe-printk-in-nmi +1 -18
@@ -55,6 +55,7 @@#include "console_cmdline.h" #include "braille.h" +#include "internal.h" int console_printk[4 ] = {CONSOLE_LOGLEVEL_DEFAULT ,/* console_loglevel */
@@ -1807,14 +1808,6 @@ int vprintk_default(const char *fmt, va_ }EXPORT_SYMBOL_GPL (vprintk_default); -/* -* This allows printk to be diverted to another function per cpu. -* This is useful for calling printk functions from within NMI -* without worrying about race conditions that can lock up the -* box. -*/ -DEFINE_PER_CPU (printk_func_t, printk_func) = vprintk_default; -/** * printk - print a kernel message * @fmt: format string
@@ -1838,21 +1831,11 @@ DEFINE_PER_CPU(printk_func_t, printk_fun*/ asmlinkage __visible int printk(const char *fmt, ...) { -printk_func_t vprintk_func;va_list args;int r;va_start (args, fmt); - -/* -* If a caller overrides the per_cpu printk_func, then it needs -* to disable preemption when calling printk(). Otherwise -* the printk_func should be set to the default. No need to -* disable preemption here. -*/ - vprintk_func =this_cpu_read (printk_func); r =vprintk_func (fmt, args); -va_end (args);return r;
lib/nmi_backtrace.c renamed from lib/nmi_backtrace.c~printk-nmi-generic-solution-for-safe-printk-in-nmi +5 -83
132 lines not on this page · raw message