Skip to content

Commit b1a57bb

Browse files
diandersDaniel Thompson
authored andcommitted
kgdb: Delay "kgdbwait" to dbg_late_init() by default
Using kgdb requires at least some level of architecture-level initialization. If nothing else, it relies on the architecture to pass breakpoints / crashes onto kgdb. On some architectures this all works super early, specifically it starts working at some point in time before Linux parses early_params's. On other architectures it doesn't. A survey of a few platforms: a) x86: Presumably it all works early since "ekgdboc" is documented to work here. b) arm64: Catching crashes works; with a simple patch breakpoints can also be made to work. c) arm: Nothing in kgdb works until paging_init() -> devicemaps_init() -> early_trap_init() Let's be conservative and, by default, process "kgdbwait" (which tells the kernel to drop into the debugger ASAP at boot) a bit later at dbg_late_init() time. If an architecture has tested it and wants to re-enable super early debugging, they can select the ARCH_HAS_EARLY_DEBUG KConfig option. We'll do this for x86 to start. It should be noted that dbg_late_init() is still called quite early in the system. Note that this patch doesn't affect when kgdb runs its init. If kgdb is set to initialize early it will still initialize when parsing early_param's. This patch _only_ inhibits the initial breakpoint from "kgdbwait". This means: * Without any extra patches arm64 platforms will at least catch crashes after kgdb inits. * arm platforms will catch crashes (and could handle a hardcoded kgdb_breakpoint()) any time after early_trap_init() runs, even before dbg_late_init(). Signed-off-by: Douglas Anderson <dianders@chromium.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ingo Molnar <mingo@redhat.com> Cc: Borislav Petkov <bp@alien8.de> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://lore.kernel.org/r/20200507130644.v4.4.I3113aea1b08d8ce36dc3720209392ae8b815201b@changeid Signed-off-by: Daniel Thompson <daniel.thompson@linaro.org>
1 parent 68e55f6 commit b1a57bb

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

‎arch/x86/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ config X86
6060
select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI
6161
select ARCH_HAS_DEBUG_VIRTUAL
6262
select ARCH_HAS_DEVMEM_IS_ALLOWED
63+
select ARCH_HAS_EARLY_DEBUG if KGDB
6364
select ARCH_HAS_ELF_RANDOMIZE
6465
select ARCH_HAS_FAST_MULTIPLIER
6566
select ARCH_HAS_FILTER_PGPROT

‎kernel/debug/debug_core.c‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,14 @@ void kgdb_panic(const char *msg)
948948
kgdb_breakpoint();
949949
}
950950

951+
static void kgdb_initial_breakpoint(void)
952+
{
953+
kgdb_break_asap = 0;
954+
955+
pr_crit("Waiting for connection from remote gdb...\n");
956+
kgdb_breakpoint();
957+
}
958+
951959
void __weak kgdb_arch_late(void)
952960
{
953961
}
@@ -958,6 +966,9 @@ void __init dbg_late_init(void)
958966
if (kgdb_io_module_registered)
959967
kgdb_arch_late();
960968
kdb_init(KDB_INIT_FULL);
969+
970+
if (kgdb_io_module_registered && kgdb_break_asap)
971+
kgdb_initial_breakpoint();
961972
}
962973

963974
static int
@@ -1053,14 +1064,6 @@ void kgdb_schedule_breakpoint(void)
10531064
}
10541065
EXPORT_SYMBOL_GPL(kgdb_schedule_breakpoint);
10551066

1056-
static void kgdb_initial_breakpoint(void)
1057-
{
1058-
kgdb_break_asap = 0;
1059-
1060-
pr_crit("Waiting for connection from remote gdb...\n");
1061-
kgdb_breakpoint();
1062-
}
1063-
10641067
/**
10651068
* kgdb_register_io_module - register KGDB IO module
10661069
* @new_dbg_io_ops: the io ops vector
@@ -1097,7 +1100,8 @@ int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
10971100
/* Arm KGDB now. */
10981101
kgdb_register_callbacks();
10991102

1100-
if (kgdb_break_asap)
1103+
if (kgdb_break_asap &&
1104+
(!dbg_is_early || IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG)))
11011105
kgdb_initial_breakpoint();
11021106

11031107
return 0;
@@ -1167,7 +1171,8 @@ static int __init opt_kgdb_wait(char *str)
11671171
kgdb_break_asap = 1;
11681172

11691173
kdb_init(KDB_INIT_EARLY);
1170-
if (kgdb_io_module_registered)
1174+
if (kgdb_io_module_registered &&
1175+
IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG))
11711176
kgdb_initial_breakpoint();
11721177

11731178
return 0;

‎lib/Kconfig.kgdb‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,22 @@ config KDB_CONTINUE_CATASTROPHIC
124124
CONFIG_KDB_CONTINUE_CATASTROPHIC == 2. KDB forces a reboot.
125125
If you are not sure, say 0.
126126

127+
config ARCH_HAS_EARLY_DEBUG
128+
bool
129+
default n
130+
help
131+
If an architecture can definitely handle entering the debugger
132+
when early_param's are parsed then it select this config.
133+
Otherwise, if "kgdbwait" is passed on the kernel command line it
134+
won't actually be processed until dbg_late_init() just after the
135+
call to kgdb_arch_late() is made.
136+
137+
NOTE: Even if this isn't selected by an architecture we will
138+
still try to register kgdb to handle breakpoints and crashes
139+
when early_param's are parsed, we just won't act on the
140+
"kgdbwait" parameter until dbg_late_init(). If you get a
141+
crash and try to drop into kgdb somewhere between these two
142+
places you might or might not end up being able to use kgdb
143+
depending on exactly how far along the architecture has initted.
144+
127145
endif # KGDB

0 commit comments

Comments
 (0)