Skip to content

Commit 2a9e5de

Browse files
committed
printk/kdb: Redirect printk messages into kdb in any context
kdb has to get messages on consoles even when the system is stopped. It uses kdb_printf() internally and calls console drivers on its own. It uses a hack to reuse an existing code. It sets "kdb_trap_printk" global variable to redirect even the normal printk() into the kdb_printf() variant. The variable "kdb_trap_printk" is checked in printk_default() and it is ignored when printk is redirected to printk_safe in NMI context. Solve this by moving the check into printk_func(). It is obvious that it is not fully safe. But it does not make things worse. The console drivers are already called in this context by db_printf() direct calls. Reported-by: Sumit Garg <[email protected]> Tested-by: Sumit Garg <[email protected]> Reviewed-by: Daniel Thompson <[email protected]> Acked-by: Sergey Senozhatsky <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/20200520102233.GC3464@linux-b0ei
1 parent ca1f5df commit 2a9e5de

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

kernel/printk/printk.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <linux/memblock.h>
3636
#include <linux/syscalls.h>
3737
#include <linux/crash_core.h>
38-
#include <linux/kdb.h>
3938
#include <linux/ratelimit.h>
4039
#include <linux/kmsg_dump.h>
4140
#include <linux/syslog.h>
@@ -2047,18 +2046,7 @@ EXPORT_SYMBOL(vprintk);
20472046

20482047
int vprintk_default(const char *fmt, va_list args)
20492048
{
2050-
int r;
2051-
2052-
#ifdef CONFIG_KGDB_KDB
2053-
/* Allow to pass printk() to kdb but avoid a recursion. */
2054-
if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0)) {
2055-
r = vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args);
2056-
return r;
2057-
}
2058-
#endif
2059-
r = vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args);
2060-
2061-
return r;
2049+
return vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args);
20622050
}
20632051
EXPORT_SYMBOL_GPL(vprintk_default);
20642052

kernel/printk/printk_safe.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/preempt.h>
77
#include <linux/spinlock.h>
88
#include <linux/debug_locks.h>
9+
#include <linux/kdb.h>
910
#include <linux/smp.h>
1011
#include <linux/cpumask.h>
1112
#include <linux/irq_work.h>
@@ -359,6 +360,12 @@ void __printk_safe_exit(void)
359360

360361
__printf(1, 0) int vprintk_func(const char *fmt, va_list args)
361362
{
363+
#ifdef CONFIG_KGDB_KDB
364+
/* Allow to pass printk() to kdb but avoid a recursion. */
365+
if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0))
366+
return vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args);
367+
#endif
368+
362369
/*
363370
* Try to use the main logbuf even in NMI. But avoid calling console
364371
* drivers that might have their own locks.

0 commit comments

Comments
 (0)