aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/printk
diff options
context:
space:
mode:
authorPetr Mladek <pmladek@suse.com>2018-06-27 10:20:28 -0400
committerPetr Mladek <pmladek@suse.com>2018-07-09 08:10:40 -0400
commit03fc7f9c99c1e7ae2925d459e8487f1a6f199f79 (patch)
tree8cb7129c15859fd980dcab0dc22f1f816145f841 /kernel/printk
parenta338f84dc196f44b63ba0863d2f34fd9b1613572 (diff)
printk/nmi: Prevent deadlock when accessing the main log buffer in NMI
The commit 719f6a7040f1bdaf96 ("printk: Use the main logbuf in NMI when logbuf_lock is available") brought back the possible deadlocks in printk() and NMI. The check of logbuf_lock is done only in printk_nmi_enter() to prevent mixed output. But another CPU might take the lock later, enter NMI, and: + Both NMIs might be serialized by yet another lock, for example, the one in nmi_cpu_backtrace(). + The other CPU might get stopped in NMI, see smp_send_stop() in panic(). The only safe solution is to use trylock when storing the message into the main log-buffer. It might cause reordering when some lines go to the main lock buffer directly and others are delayed via the per-CPU buffer. It means that it is not useful in general. This patch replaces the problematic NMI deferred context with NMI direct context. It can be used to mark a code that might produce many messages in NMI and the risk of losing them is more critical than problems with eventual reordering. The context is then used when dumping trace buffers on oops. It was the primary motivation for the original fix. Also the reordering is even smaller issue there because some traces have their own time stamps. Finally, nmi_cpu_backtrace() need not longer be serialized because it will always us the per-CPU buffers again. Fixes: 719f6a7040f1bdaf96 ("printk: Use the main logbuf in NMI when logbuf_lock is available") Cc: stable@vger.kernel.org Link: http://lkml.kernel.org/r/20180627142028.11259-1-pmladek@suse.com To: Steven Rostedt <rostedt@goodmis.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Cc: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com> Cc: linux-kernel@vger.kernel.org Cc: stable@vger.kernel.org Acked-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Signed-off-by: Petr Mladek <pmladek@suse.com>
Diffstat (limited to 'kernel/printk')
-rw-r--r--kernel/printk/internal.h9
-rw-r--r--kernel/printk/printk_safe.c58
2 files changed, 45 insertions, 22 deletions
diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
index 2a7d04049af4..0f1898820cba 100644
--- a/kernel/printk/internal.h
+++ b/kernel/printk/internal.h
@@ -19,11 +19,16 @@
19#ifdef CONFIG_PRINTK 19#ifdef CONFIG_PRINTK
20 20
21#define PRINTK_SAFE_CONTEXT_MASK 0x3fffffff 21#define PRINTK_SAFE_CONTEXT_MASK 0x3fffffff
22#define PRINTK_NMI_DEFERRED_CONTEXT_MASK 0x40000000 22#define PRINTK_NMI_DIRECT_CONTEXT_MASK 0x40000000
23#define PRINTK_NMI_CONTEXT_MASK 0x80000000 23#define PRINTK_NMI_CONTEXT_MASK 0x80000000
24 24
25extern raw_spinlock_t logbuf_lock; 25extern raw_spinlock_t logbuf_lock;
26 26
27__printf(5, 0)
28int vprintk_store(int facility, int level,
29 const char *dict, size_t dictlen,
30 const char *fmt, va_list args);
31
27__printf(1, 0) int vprintk_default(const char *fmt, va_list args); 32__printf(1, 0) int vprintk_default(const char *fmt, va_list args);
28__printf(1, 0) int vprintk_deferred(const char *fmt, va_list args); 33__printf(1, 0) int vprintk_deferred(const char *fmt, va_list args);
29__printf(1, 0) int vprintk_func(const char *fmt, va_list args); 34__printf(1, 0) int vprintk_func(const char *fmt, va_list args);
@@ -54,6 +59,8 @@ void __printk_safe_exit(void);
54 local_irq_enable(); \ 59 local_irq_enable(); \
55 } while (0) 60 } while (0)
56 61
62void defer_console_output(void);
63
57#else 64#else
58 65
59__printf(1, 0) int vprintk_func(const char *fmt, va_list args) { return 0; } 66__printf(1, 0) int vprintk_func(const char *fmt, va_list args) { return 0; }
diff --git a/kernel/printk/printk_safe.c b/kernel/printk/printk_safe.c
index d7d091309054..a0a74c533e4b 100644
--- a/kernel/printk/printk_safe.c
+++ b/kernel/printk/printk_safe.c
@@ -308,24 +308,33 @@ static __printf(1, 0) int vprintk_nmi(const char *fmt, va_list args)
308 308
309void printk_nmi_enter(void) 309void printk_nmi_enter(void)
310{ 310{
311 /* 311 this_cpu_or(printk_context, PRINTK_NMI_CONTEXT_MASK);
312 * The size of the extra per-CPU buffer is limited. Use it only when
313 * the main one is locked. If this CPU is not in the safe context,
314 * the lock must be taken on another CPU and we could wait for it.
315 */
316 if ((this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK) &&
317 raw_spin_is_locked(&logbuf_lock)) {
318 this_cpu_or(printk_context, PRINTK_NMI_CONTEXT_MASK);
319 } else {
320 this_cpu_or(printk_context, PRINTK_NMI_DEFERRED_CONTEXT_MASK);
321 }
322} 312}
323 313
324void printk_nmi_exit(void) 314void printk_nmi_exit(void)
325{ 315{
326 this_cpu_and(printk_context, 316 this_cpu_and(printk_context, ~PRINTK_NMI_CONTEXT_MASK);
327 ~(PRINTK_NMI_CONTEXT_MASK | 317}
328 PRINTK_NMI_DEFERRED_CONTEXT_MASK)); 318
319/*
320 * Marks a code that might produce many messages in NMI context
321 * and the risk of losing them is more critical than eventual
322 * reordering.
323 *
324 * It has effect only when called in NMI context. Then printk()
325 * will try to store the messages into the main logbuf directly
326 * and use the per-CPU buffers only as a fallback when the lock
327 * is not available.
328 */
329void printk_nmi_direct_enter(void)
330{
331 if (this_cpu_read(printk_context) & PRINTK_NMI_CONTEXT_MASK)
332 this_cpu_or(printk_context, PRINTK_NMI_DIRECT_CONTEXT_MASK);
333}
334
335void printk_nmi_direct_exit(void)
336{
337 this_cpu_and(printk_context, ~PRINTK_NMI_DIRECT_CONTEXT_MASK);
329} 338}
330 339
331#else 340#else
@@ -363,6 +372,20 @@ void __printk_safe_exit(void)
363 372
364__printf(1, 0) int vprintk_func(const char *fmt, va_list args) 373__printf(1, 0) int vprintk_func(const char *fmt, va_list args)
365{ 374{
375 /*
376 * Try to use the main logbuf even in NMI. But avoid calling console
377 * drivers that might have their own locks.
378 */
379 if ((this_cpu_read(printk_context) & PRINTK_NMI_DIRECT_CONTEXT_MASK) &&
380 raw_spin_trylock(&logbuf_lock)) {
381 int len;
382
383 len = vprintk_store(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args);
384 raw_spin_unlock(&logbuf_lock);
385 defer_console_output();
386 return len;
387 }
388
366 /* Use extra buffer in NMI when logbuf_lock is taken or in safe mode. */ 389 /* Use extra buffer in NMI when logbuf_lock is taken or in safe mode. */
367 if (this_cpu_read(printk_context) & PRINTK_NMI_CONTEXT_MASK) 390 if (this_cpu_read(printk_context) & PRINTK_NMI_CONTEXT_MASK)
368 return vprintk_nmi(fmt, args); 391 return vprintk_nmi(fmt, args);
@@ -371,13 +394,6 @@ __printf(1, 0) int vprintk_func(const char *fmt, va_list args)
371 if (this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK) 394 if (this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK)
372 return vprintk_safe(fmt, args); 395 return vprintk_safe(fmt, args);
373 396
374 /*
375 * Use the main logbuf when logbuf_lock is available in NMI.
376 * But avoid calling console drivers that might have their own locks.
377 */
378 if (this_cpu_read(printk_context) & PRINTK_NMI_DEFERRED_CONTEXT_MASK)
379 return vprintk_deferred(fmt, args);
380
381 /* No obstacles. */ 397 /* No obstacles. */
382 return vprintk_default(fmt, args); 398 return vprintk_default(fmt, args);
383} 399}