Skip to contentSkip to conversation

Cleaning printk stuff in NMI context

PATCH Superseded Petr Mladek 2 participants 9 messages Dec 11, 2015 – Feb 8, 2016

Subsystems (10)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Petr Mladek

author maintainer
#0 lore raw

v4 cover Cleaning printk stuff in NMI context

Hi Andrew,

this is just a quick respin of the previous version. It changes the few details as you suggested. Also it fixes the build problem on ARM as reported by Geert and Arnd.

I rather send the whole patch set because there is the renamed header. Also the extra blank space affects two patches. I hope that it will safe you some work. Please, let me know if you would prefer incremental patches.

Changes against v3:

+ used size_t for "len" and "size"

+ replaced WARN() with pr_err()

+ renamed kernel/printk/printk.h -> internal.h

+ fixed build on ARM (undefined NMI_LOG_BUF_SHIFT)

Changes against v2:

+ fixed compilation problems reported by 0-day build robot

+ MN10300 and Xtensa architectures will get handled separately

+ dropped the patch that printed NMI messages directly when Oops in progress; it made the solution less reliable

+ made the size of the buffer configurable; use real numbers instead of PAGE_SIZE

Changes against v1:

+ rebased on top of 4.4-rc2; there the old implementation was moved to lib/nmi_backtrace.c and used also on arm; I hope that I got the arm side correctly; I was not able to test on arm :-(

+ defined HAVE_NMI on arm for !CPU_V7M instead of !CPU_V7; handle_fiq_as_nmi() is called from entry-armv.S that is compiled when !CPU_V7M

+ defined HAVE_NMI also on mips; it calls nmi_enter() and seems to have real NMIs (or am I wrong?)

+ serialized backtraces when printing directly
(oops_in_progress)

Petr Mladek (4):
printk/nmi: Generic solution for safe printk in NMI printk/nmi: Use IRQ work only when ready
printk/nmi: Warn when some message has been lost in NMI context printk/nmi: Increase the size of NMI buffer and make it configurable

 arch/Kconfig                  |   7 ++
 arch/arm/Kconfig              |   2 +
 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                  |  27 +++++
 init/main.c                   |   1 +
 kernel/printk/Makefile        |   1 +
 kernel/printk/internal.h      |  55 +++++++++++
 kernel/printk/nmi.c           | 222 ++++++++++++++++++++++++++++++++++++++++++
 kernel/printk/printk.c        |  29 +++---
 lib/nmi_backtrace.c           |  89 +----------------
 24 files changed, 356 insertions(+), 107 deletions(-)
 create mode 100644 kernel/printk/internal.h
 create mode 100644 kernel/printk/nmi.c
Signature · 1 line
1.8.5.6
author maintainer
#1 lore raw

PATCH v4 1/4 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").

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.

One exception is arm where the deferred printing is used for printing backtraces even without NMI. For this purpose, we define NEED_PRINTK_NMI Kconfig flag. The alternative printk_func is explicitly set when IPI_CPU_BACKTRACE is handled.

The other exceptions 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

Cc Jan Kara <[email protected]>
Cc Russell King <[email protected]>
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]>
---
 arch/Kconfig                  |   7 ++
 arch/arm/Kconfig              |   2 +
 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           | 207 ++++++++++++++++++++++++++++++++++++++++++
 kernel/printk/printk.c        |  19 +---
 lib/nmi_backtrace.c           |  89 +-----------------
 24 files changed, 298 insertions(+), 107 deletions(-)
 create mode 100644 kernel/printk/internal.h
 create mode 100644 kernel/printk/nmi.c
arch/Kconfig +7 -0
index 4e949e58b192..7ce5101c2472 100644
@@ -187,7 +187,14 @@ config HAVE_OPTPROBES
 config HAVE_KPROBES_ON_FTRACE
 	bool
 
+config HAVE_NMI
+	bool
+
+config NEED_PRINTK_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 +2 -0
index 34e1569a11ee..96d2c275f0f7 100644
@@ -63,6 +63,8 @@ config ARM
 	select HAVE_KRETPROBES if (HAVE_KPROBES)
 	select HAVE_MEMBLOCK
 	select HAVE_MOD_ARCH_SPECIFIC
+	select HAVE_NMI if (!CPU_V7M)
+	select NEED_PRINTK_NMI if (CPU_V7M)
 	select HAVE_OPROFILE if (HAVE_PERF_EVENTS)
 	select HAVE_OPTPROBES if !THUMB2_KERNEL
 	select HAVE_PERF_EVENTS
arch/arm/kernel/smp.c +2 -0
index b26361355dae..a960adb9bd7d 100644
@@ -648,7 +648,9 @@ void handle_IPI(int ipinr, struct pt_regs *regs)
 
 	case IPI_CPU_BACKTRACE:
 		irq_enter();
+		printk_nmi_enter();
 		nmi_cpu_backtrace(regs);
+		printk_nmi_exit();
 		irq_exit();
 		break;
 
arch/avr32/Kconfig +1 -0
index b6878eb64884..9dc3e2b1180b 100644
@@ -17,6 +17,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 +1 -0
index af76634f8d98..47c0a55acafd 100644
@@ -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 +1 -0
index e086f9e93728..62148eaf0189 100644
@@ -69,6 +69,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 +1 -0
index 71683a853372..35ae01fe980a 100644
@@ -63,6 +63,7 @@ config MIPS
 	select HAVE_IRQ_TIME_ACCOUNTING
 	select GENERIC_TIME_VSYSCALL
 	select ARCH_CLOCKSOURCE_DATA
+	select HAVE_NMI
 
 menu "Machine selection"
 
arch/powerpc/Kconfig +1 -0
index db49e0d796b1..1bd8a7503a48 100644
@@ -156,6 +156,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 +1 -0
index 3a55f493c7da..9a8a76829508 100644
@@ -159,6 +159,7 @@ config S390
 	select TTY
 	select VIRT_CPU_ACCOUNTING
 	select VIRT_TO_BUS
+	select HAVE_NMI
 
 
 config SCHED_OMIT_FRAME_POINTER
arch/sh/Kconfig +1 -0
index d514df7e04dd..ff8093b574f1 100644
@@ -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 +1 -0
index 56442d2d7bbc..e05ea973fd6a 100644
@@ -80,6 +80,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 +1 -0
index 106c21bd7f44..050468c41dc6 100644
@@ -29,6 +29,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 +1 -0
index db3622f22b61..e83f99194c4c 100644
@@ -126,6 +126,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 +0 -1
index 045e424fb368..7788ce643bf4 100644
@@ -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 +2 -0
index dfd59d6bc6f0..a477e0766d2e 100644
@@ -67,10 +67,12 @@ extern void irq_exit(void);
 		preempt_count_add(NMI_OFFSET + HARDIRQ_OFFSET);	\
 		rcu_nmi_enter();				\
 		trace_hardirq_enter();				\
+		printk_nmi_enter();				\
 	} while (0)
 
 #define nmi_exit()						\
 	do {							\
+		printk_nmi_exit();				\
 		trace_hardirq_exit();				\
 		rcu_nmi_exit();					\
 		BUG_ON(!in_nmi());				\
include/linux/percpu.h +0 -3
index caebf2a758dc..04c68b9f56f8 100644
@@ -135,7 +135,4 @@ extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
 	(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 +11 -1
index 9729565c25ff..7aba6b92d020 100644
@@ -122,7 +122,17 @@ static inline __printf(1, 2) __cold
 void 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 +5 -0
index c24b6f767bf0..c1c0b6a2d712 100644
@@ -1456,6 +1456,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 || NEED_PRINTK_NMI
+
 config BUG
 	bool "BUG() support" if EXPERT
 	default y
init/main.c +1 -0
index 9e64d7097f1a..45c1e05125dc 100644
@@ -591,6 +591,7 @@ asmlinkage __visible void __init start_kernel(void)
 	timekeeping_init();
 	time_init();
 	sched_clock_postinit();
+	printk_nmi_init();
 	perf_event_init();
 	profile_init();
 	call_function_init();
kernel/printk/Makefile +1 -0
index 85405bdcf2b3..abb0042a427b 100644
@@ -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 +207 -0

207 lines not on this page · raw message

kernel/printk/printk.c +1 -18
index 2ce8826f1053..480ad05ab299 100644
@@ -54,6 +54,7 @@
 
 #include "console_cmdline.h"
 #include "braille.h"
+#include "internal.h"
 
 int console_printk[4] = {
 	CONSOLE_LOGLEVEL_DEFAULT,	/* console_loglevel */
@@ -1867,14 +1868,6 @@ int vprintk_default(const char *fmt, va_list args)
 }
 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
@@ -1898,21 +1891,11 @@ DEFINE_PER_CPU(printk_func_t, printk_func) = vprintk_default;
  */
 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 +5 -84

136 lines not on this page · raw message

Signature · 1 line
1.8.5.6
author maintainer
#2 lore raw

PATCH v4 2/4 printk/nmi: Use IRQ work only when ready

In reply to Petr Mladek

NMIs could happen at any time. This patch makes sure that the safe printk() in NMI will schedule IRQ work only when the related structs are initialized.

All pending messages are flushed when the IRQ work is being initialized.

Cc Jan Kara <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Russell King <[email protected]>
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]>
---
 kernel/printk/nmi.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/printk/nmi.c +12 -1
index b5d1359402c5..9a506bd18b9d 100644
@@ -38,6 +38,7 @@
  * were handled or when IRQs are blocked.
  */
 DEFINE_PER_CPU(printk_func_t, printk_func) = vprintk_default;
+static int printk_nmi_irq_ready;
 
 #define NMI_LOG_BUF_LEN (4096 - sizeof(atomic_t) - sizeof(struct irq_work))
 
@@ -84,8 +85,11 @@ again:
 		goto again;
 
 	/* Get flushed in a more safe context. */
-	if (add)
+	if (add && printk_nmi_irq_ready) {
+		/* Make sure that IRQ work is really initialized. */
+		smp_rmb();
 		irq_work_queue(&s->work);
+	}
 
 	return add;
 }
@@ -194,6 +198,13 @@ void __init printk_nmi_init(void)
 
 		init_irq_work(&s->work, __printk_nmi_flush);
 	}
+
+	/* Make sure that IRQ works are initialized before enabling. */
+	smp_wmb();
+	printk_nmi_irq_ready = 1;
+
+	/* Flush pending messages that did not have scheduled IRQ works. */
+	printk_nmi_flush();
 }
 
 void printk_nmi_enter(void)
Signature · 1 line
1.8.5.6
author maintainer
#3 lore raw

PATCH v4 3/4 printk/nmi: Warn when some message has been lost in NMI context

In reply to Petr Mladek

We could not resize the temporary buffer in NMI context. Let's warn if a message is lost.

This is rather theoretical. printk() should not be used in NMI. The only sensible use is when we want to print backtrace from all CPUs. The current buffer should be enough for this purpose.

Cc Jan Kara <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Russell King <[email protected]>
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]>
---
 kernel/printk/internal.h | 11 +++++++++++
 kernel/printk/nmi.c      |  5 ++++-
 kernel/printk/printk.c   | 10 ++++++++++
 3 files changed, 25 insertions(+), 1 deletion(-)
kernel/printk/internal.h +11 -0
index 2de99faedfc1..341bedccc065 100644
@@ -34,6 +34,12 @@ static inline __printf(1, 0) int vprintk_func(const char *fmt, va_list args)
 	return this_cpu_read(printk_func)(fmt, args);
 }
 
+extern atomic_t nmi_message_lost;
+static inline int get_nmi_message_lost(void)
+{
+	return atomic_xchg(&nmi_message_lost, 0);
+}
+
 #else /* CONFIG_PRINTK_NMI */
 
 static inline __printf(1, 0) int vprintk_func(const char *fmt, va_list args)
@@ -41,4 +47,9 @@ static inline __printf(1, 0) int vprintk_func(const char *fmt, va_list args)
 	return vprintk_default(fmt, args);
 }
 
+static inline int get_nmi_message_lost(void)
+{
+	return 0;
+}
+
 #endif /* CONFIG_PRINTK_NMI */
kernel/printk/nmi.c +4 -1
index 9a506bd18b9d..d86f55cccf31 100644
@@ -39,6 +39,7 @@
  */
 DEFINE_PER_CPU(printk_func_t, printk_func) = vprintk_default;
 static int printk_nmi_irq_ready;
+atomic_t nmi_message_lost;
 
 #define NMI_LOG_BUF_LEN (4096 - sizeof(atomic_t) - sizeof(struct irq_work))
 
@@ -64,8 +65,10 @@ static int vprintk_nmi(const char *fmt, va_list args)
 again:
 	len = atomic_read(&s->len);
 
-	if (len >= sizeof(s->buffer))
+	if (len >= sizeof(s->buffer)) {
+		atomic_inc(&nmi_message_lost);
 		return 0;
+	}
 
 	/*
 	 * Make sure that all old data have been read before the buffer was
kernel/printk/printk.c +10 -0
index 480ad05ab299..71183a308b9e 100644
@@ -1669,6 +1669,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 	unsigned long flags;
 	int this_cpu;
 	int printed_len = 0;
+	int nmi_message_lost;
 	bool in_sched = false;
 	/* cpu currently holding logbuf_lock in this function */
 	static unsigned int logbuf_cpu = UINT_MAX;
@@ -1719,6 +1720,15 @@ asmlinkage int vprintk_emit(int facility, int level,
 					 strlen(recursion_msg));
 	}
 
+	nmi_message_lost = get_nmi_message_lost();
+	if (unlikely(nmi_message_lost)) {
+		text_len = scnprintf(textbuf, sizeof(textbuf),
+				     "BAD LUCK: lost %d message(s) from NMI context!",
+				     nmi_message_lost);
+		printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
+					 NULL, 0, textbuf, text_len);
+	}
+
 	/*
 	 * The printf needs to come first; we need the syslog
 	 * prefix which might be passed-in as a parameter.
Signature · 1 line
1.8.5.6
author maintainer
#4 lore raw

PATCH v4 4/4 printk/nmi: Increase the size of NMI buffer and make it configurable

In reply to Petr Mladek

Testing has shown that the backtrace sometimes does not fit into the 4kB temporary buffer that is used in NMI context. The warnings are gone when I double the temporary buffer size.

This patch doubles the buffer size and makes it configurable.

Note that this problem existed even in the x86-specific implementation that was added by the commit a9edc8809328 ("x86/nmi: Perform a safe NMI stack trace on all CPUs"). Nobody noticed it because it did not print any warnings.

Cc Jan Kara <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Russell King <[email protected]>
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]>
---
 init/Kconfig        | 22 ++++++++++++++++++++++
 kernel/printk/nmi.c |  3 ++-
 2 files changed, 24 insertions(+), 1 deletion(-)
init/Kconfig +22 -0
index c1c0b6a2d712..61cfd96a3c96 100644
@@ -866,6 +866,28 @@ config LOG_CPU_MAX_BUF_SHIFT
 		     13 =>   8 KB for each CPU
 		     12 =>   4 KB for each CPU
 
+config NMI_LOG_BUF_SHIFT
+	int "Temporary per-CPU NMI log buffer size (12 => 4KB, 13 => 8KB)"
+	range 10 21
+	default 13
+	depends on PRINTK_NMI
+	help
+	  Select the size of a per-CPU buffer where NMI messages are temporary
+	  stored. They are copied to the main log buffer in a safe context
+	  to avoid a deadlock. The value defines the size as a power of 2.
+
+	  NMI messages are rare and limited. The largest one is when
+	  a backtrace is printed. It usually fits into 4KB. Select
+	  8KB if you want to be on the safe side.
+
+	  Examples:
+		     17 => 128 KB for each CPU
+		     16 =>  64 KB for each CPU
+		     15 =>  32 KB for each CPU
+		     14 =>  16 KB for each CPU
+		     13 =>   8 KB for each CPU
+		     12 =>   4 KB for each CPU
+
 #
 # Architectures with an unreliable sched_clock() should select this:
 #
kernel/printk/nmi.c +2 -1
index d86f55cccf31..92c61e960c4a 100644
@@ -41,7 +41,8 @@ DEFINE_PER_CPU(printk_func_t, printk_func) = vprintk_default;
 static int printk_nmi_irq_ready;
 atomic_t nmi_message_lost;
 
-#define NMI_LOG_BUF_LEN (4096 - sizeof(atomic_t) - sizeof(struct irq_work))
+#define NMI_LOG_BUF_LEN ((1 << CONFIG_NMI_LOG_BUF_SHIFT) -		\
+			 sizeof(atomic_t) - sizeof(struct irq_work))
 
 struct nmi_seq_buf {
 	atomic_t		len;	/* length of written data */
Signature · 1 line
1.8.5.6
author maintainer
#5 lore raw
In reply to Petr Mladek
Quoted 14 lines · Petr Mladek

Hi Andrew,

this is just a quick respin of the previous version. It changes the few details as you suggested. Also it fixes the build problem on ARM as reported by Geert and Arnd.

I rather send the whole patch set because there is the renamed header. Also the extra blank space affects two patches. I hope that it will safe you some work. Please, let me know if you would prefer incremental patches.

Changes against v3:

Quoted 7 lines

+ used size_t for "len" and "size"

+ replaced WARN() with pr_err()

+ renamed kernel/printk/printk.h -> internal.h

+ fixed build on ARM (undefined NMI_LOG_BUF_SHIFT)

Please, find below the diff between the two branches based on v3 and v4 of the patch set. I hope that it might help.

init/Kconfig +1 -1
index efcff25a112d..61cfd96a3c96 100644
@@ -870,7 +870,7 @@ config NMI_LOG_BUF_SHIFT
 	int "Temporary per-CPU NMI log buffer size (12 => 4KB, 13 => 8KB)"
 	range 10 21
 	default 13
-	depends on PRINTK && HAVE_NMI
+	depends on PRINTK_NMI
 	help
 	  Select the size of a per-CPU buffer where NMI messages are temporary
 	  stored. They are copied to the main log buffer in a safe context
kernel/printk/internal.h renamed from kernel/printk/printk.h +1 -1
similarity index 97%
rename from kernel/printk/printk.h
rename to kernel/printk/internal.h
index 7f778b609492..341bedccc065 100644
@@ -1,5 +1,5 @@
 /*
- * printk.h - printk internal definitions
+ * internal.h - printk internal definitions
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
kernel/printk/nmi.c +9 -5
index 78c07d441b4e..92c61e960c4a 100644
@@ -22,7 +22,7 @@
 #include <linux/irq_work.h>
 #include <linux/printk.h>
 
-#include "printk.h"
+#include "internal.h"
 
 /*
  * printk() could not take logbuf_lock in NMI context. Instead,
@@ -60,12 +60,13 @@ static DEFINE_PER_CPU(struct nmi_seq_buf, nmi_print_seq);
 static int vprintk_nmi(const char *fmt, va_list args)
 {
 	struct nmi_seq_buf *s = this_cpu_ptr(&nmi_print_seq);
-	int add = 0, len;
+	int add = 0;
+	size_t len;
 
 again:
 	len = atomic_read(&s->len);
 
-	if (len >=  sizeof(s->buffer)) {
+	if (len >= sizeof(s->buffer)) {
 		atomic_inc(&nmi_message_lost);
 		return 0;
 	}
@@ -117,7 +118,8 @@ static void __printk_nmi_flush(struct irq_work *work)
 	static raw_spinlock_t read_lock =
 		__RAW_SPIN_LOCK_INITIALIZER(read_lock);
 	struct nmi_seq_buf *s = container_of(work, struct nmi_seq_buf, work);
-	int len, size, i, last_i;
+	size_t len, size;
+	int i, last_i;
 
 	/*
 	 * The lock has two functions. First, one reader has to flush all
@@ -137,7 +139,9 @@ more:
 	 * the buffer an unexpected way. If we printed something then
 	 * @len must only increase.
 	 */
-	WARN_ON(i && i >= len);
+	if (i && i >= len)
+		pr_err("printk_nmi_flush: internal error: i=%d >= len=%lu\n",
+		       i, len);
 
 	if (!len)
 		goto out; /* Someone else has already flushed the buffer. */
kernel/printk/printk.c +1 -1
index a6bb0b0838f3..71183a308b9e 100644
@@ -54,7 +54,7 @@
 
 #include "console_cmdline.h"
 #include "braille.h"
-#include "printk.h"
+#include "internal.h"
 
 int console_printk[4] = {
 	CONSOLE_LOGLEVEL_DEFAULT,	/* console_loglevel */
maintainer
#6 lore raw
In reply to Petr Mladek
Quoted 12 lines · On Fri, 11 Dec 2015 14:20:48 +0100 Petr Mladek <[email protected]> wrote:

this is just a quick respin of the previous version. It changes the few details as you suggested. Also it fixes the build problem on ARM as reported by Geert and Arnd.

I rather send the whole patch set because there is the renamed header. Also the extra blank space affects two patches. I hope that it will safe you some work. Please, let me know if you would prefer incremental patches.

Changes against v3:

Quoted 7 lines

+ used size_t for "len" and "size"

+ replaced WARN() with pr_err()

+ renamed kernel/printk/printk.h -> internal.h

+ fixed build on ARM (undefined NMI_LOG_BUF_SHIFT)

So the review of the v3 patchset was ... inconclusive. And everyone has gone quiet about v4.

Probably because you didn't cc the V3 discussion participants when sending out V4. Big mistake, sorry, I can't check everything!

But v4 is basically unaltered from v3 so can we please rev this up again? yay or nay? Thanks.

author maintainer
#7 lore raw
Quoted 20 lines · Andrew Morton
Quoted 18 lines · On Fri, 11 Dec 2015 14:20:48 +0100 Petr Mladek <[email protected]> wrote:

this is just a quick respin of the previous version. It changes the few details as you suggested. Also it fixes the build problem on ARM as reported by Geert and Arnd.

I rather send the whole patch set because there is the renamed header. Also the extra blank space affects two patches. I hope that it will safe you some work. Please, let me know if you would prefer incremental patches.

Changes against v3:

+ used size_t for "len" and "size"

+ replaced WARN() with pr_err()

+ renamed kernel/printk/printk.h -> internal.h

Quoted 7 lines
Quoted 1 line

+ fixed build on ARM (undefined NMI_LOG_BUF_SHIFT)

So the review of the v3 patchset was ... inconclusive. And everyone has gone quiet about v4.

Probably because you didn't cc the V3 discussion participants when sending out V4. Big mistake, sorry, I can't check everything!

Ah, v4 was sent too fast after v3 before others reacted. It included rather cosmetic changes based on your (Andrew's) feedback.

Quoted 2 lines

But v4 is basically unaltered from v3 so can we please rev this up again? yay or nay? Thanks.

The patch set prevents deadlocks that happen in a real life. It increases a chance to get a valid crash dump when NMI is involved.

It does not handle well the situation when NMI is involved and the crash dump could not get produced. But this is not handled well even now. As Peter Zijlstra writes this a usually a real mess when he needs to use special hacks (early_printk) anyway.

I still believe that the patch set makes sense and is acceptable as is.

Thanks for taking care of it,
Petr

author maintainer
#8 lore raw
Quoted 39 lines · Petr Mladek
Quoted 20 lines · Andrew Morton
Quoted 18 lines · On Fri, 11 Dec 2015 14:20:48 +0100 Petr Mladek <[email protected]> wrote:

this is just a quick respin of the previous version. It changes the few details as you suggested. Also it fixes the build problem on ARM as reported by Geert and Arnd.

I rather send the whole patch set because there is the renamed header. Also the extra blank space affects two patches. I hope that it will safe you some work. Please, let me know if you would prefer incremental patches.

Changes against v3:

+ used size_t for "len" and "size"

+ replaced WARN() with pr_err()

+ renamed kernel/printk/printk.h -> internal.h

Quoted 7 lines
Quoted 1 line

+ fixed build on ARM (undefined NMI_LOG_BUF_SHIFT)

So the review of the v3 patchset was ... inconclusive. And everyone has gone quiet about v4.

Probably because you didn't cc the V3 discussion participants when sending out V4. Big mistake, sorry, I can't check everything!

Ah, v4 was sent too fast after v3 before others reacted. It included rather cosmetic changes based on your (Andrew's) feedback.

Quoted 2 lines

But v4 is basically unaltered from v3 so can we please rev this up again? yay or nay? Thanks.

The patch set prevents deadlocks that happen in a real life. It increases a chance to get a valid crash dump when NMI is involved.

Quoted 7 lines

It does not handle well the situation when NMI is involved and the crash dump could not get produced. But this is not handled well even now. As Peter Zijlstra writes this a usually a real mess when he needs to use special hacks (early_printk) anyway.

I still believe that the patch set makes sense and is acceptable as is.

Is anyone against getting this into the mainline for 4.6, please?

Best Regards,
Petr