[PATCH 2/2] kdb: Call vkdb_printf() from vprintk_default() only when wanted

From: Petr Mladek
Date: Fri Oct 21 2016 - 08:51:25 EST


kdb_trap_printk allows to pass normal printk() messages to kdb via
vkdb_printk(). For example, it is used to get backtrace using
the classic show_stack(), see kdb_show_stack().

vkdb_printf() tries to avoid a potential infinite loop by disabling
the trap. But this approach is racy, for example:

CPU1 CPU2

vkdb_printf()
// assume that kdb_trap_printk == 0
saved_trap_printk = kdb_trap_printk;
kdb_trap_printk = 0;

kdb_show_stack()
kdb_trap_printk++;

Problem1: Now, a nested printk() on CPU0 calls vkdb_printf()
even when it should have been disabled. It will not
cause a deadlock but...

// using the outdated saved value: 0
kdb_trap_printk = saved_trap_printk;

kdb_trap_printk--;

Problem2: Now, kdb_trap_printk == -1 and will stay like this.
It means that all messages will get passed to kdb from
now on.

This patch removes the racy saved_trap_printk handling. Instead,
the recursion is prevented by a check for the locked CPU.

Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
include/linux/kdb.h | 1 +
kernel/debug/kdb/kdb_io.c | 9 ++-------
kernel/printk/printk.c | 4 +++-
3 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/include/linux/kdb.h b/include/linux/kdb.h
index 410decacff8f..ccd4cf838c66 100644
--- a/include/linux/kdb.h
+++ b/include/linux/kdb.h
@@ -162,6 +162,7 @@ enum kdb_msgsrc {
};

extern int kdb_trap_printk;
+extern int kdb_printf_cpu;
extern __printf(2, 0) int vkdb_printf(enum kdb_msgsrc src, const char *fmt,
va_list args);
extern __printf(1, 2) int kdb_printf(const char *, ...);
diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
index 227b59ec7dbe..209507e30bd4 100644
--- a/kernel/debug/kdb/kdb_io.c
+++ b/kernel/debug/kdb/kdb_io.c
@@ -30,6 +30,7 @@
char kdb_prompt_str[CMD_BUFLEN];

int kdb_trap_printk;
+int kdb_printf_cpu = -1;

static int kgdb_transition_check(char *buffer)
{
@@ -554,24 +555,19 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
int linecount;
int colcount;
int logging, saved_loglevel = 0;
- int saved_trap_printk;
int retlen = 0;
int fnd, len;
int this_cpu, old_cpu;
- static int kdb_printf_cpu = -1;
char *cp, *cp2, *cphold = NULL, replaced_byte = ' ';
char *moreprompt = "more> ";
struct console *c = console_drivers;
unsigned long uninitialized_var(flags);

- local_irq_save(flags);
- saved_trap_printk = kdb_trap_printk;
- kdb_trap_printk = 0;
-
/* Serialize kdb_printf if multiple cpus try to write at once.
* But if any cpu goes recursive in kdb, just print the output,
* even if it is interleaved with any other text.
*/
+ local_irq_save(flags);
this_cpu = smp_processor_id();
atomic_inc(&kdb_event);
for (;;) {
@@ -853,7 +849,6 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
/* Update kdb_event around the locked section. */
smp_mb__before_atomic();
atomic_dec(&kdb_event);
- kdb_trap_printk = saved_trap_printk;
local_irq_restore(flags);
return retlen;
}
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index d5e397315473..db73e33811e7 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1941,7 +1941,9 @@ int vprintk_default(const char *fmt, va_list args)
int r;

#ifdef CONFIG_KGDB_KDB
- if (unlikely(kdb_trap_printk)) {
+ /* Allow to pass printk() to kdb but avoid a recursion. */
+ if (unlikely(kdb_trap_printk &&
+ kdb_printf_cpu != smp_processor_id())) {
r = vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args);
return r;
}
--
1.8.5.6