Skip to content

Commit 7d92bda

Browse files
dianderstorvalds
authored andcommitted
kgdb: don't use a notifier to enter kgdb at panic; call directly
Right now kgdb/kdb hooks up to debug panics by registering for the panic notifier. This works OK except that it means that kgdb/kdb gets called _after_ the CPUs in the system are taken offline. That means that if anything important was happening on those CPUs (like something that might have contributed to the panic) you can't debug them. Specifically I ran into a case where I got a panic because a task was "blocked for more than 120 seconds" which was detected on CPU 2. I nicely got shown stack traces in the kernel log for all CPUs including CPU 0, which was running 'PID: 111 Comm: kworker/0:1H' and was in the middle of __mmc_switch(). I then ended up at the kdb prompt where switched over to kgdb to try to look at local variables of the process on CPU 0. I found that I couldn't. Digging more, I found that I had no info on any tasks running on CPUs other than CPU 2 and that asking kdb for help showed me "Error: no saved data for this cpu". This was because all the CPUs were offline. Let's move the entry of kdb/kgdb to a direct call from panic() and stop using the generic notifier. Putting a direct call in allows us to order things more properly and it also doesn't seem like we're breaking any abstractions by calling into the debugger from the panic function. Daniel said: : This patch changes the way kdump and kgdb interact with each other. : However it would seem rather odd to have both tools simultaneously armed : and, even if they were, the user still has the option to use panic_timeout : to force a kdump to happen. Thus I think the change of order is : acceptable. Link: http://lkml.kernel.org/r/20190703170354.217312-1-dianders@chromium.org Signed-off-by: Douglas Anderson <dianders@chromium.org> Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org> Cc: Jason Wessel <jason.wessel@windriver.com> Cc: Kees Cook <keescook@chromium.org> Cc: Borislav Petkov <bp@suse.de> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Feng Tang <feng.tang@intel.com> Cc: YueHaibing <yuehaibing@huawei.com> Cc: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com> Cc: "Steven Rostedt (VMware)" <rostedt@goodmis.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent ac7c3e4 commit 7d92bda

3 files changed

Lines changed: 21 additions & 20 deletions

File tree

‎include/linux/kgdb.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,10 @@ extern atomic_t kgdb_active;
326326
(raw_smp_processor_id() == atomic_read(&kgdb_active))
327327
extern bool dbg_is_early;
328328
extern void __init dbg_late_init(void);
329+
extern void kgdb_panic(const char *msg);
329330
#else /* ! CONFIG_KGDB */
330331
#define in_dbg_master() (0)
331332
#define dbg_late_init()
333+
static inline void kgdb_panic(const char *msg) {}
332334
#endif /* ! CONFIG_KGDB */
333335
#endif /* _KGDB_H_ */

‎kernel/debug/debug_core.c‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -893,30 +893,25 @@ static struct sysrq_key_op sysrq_dbg_op = {
893893
};
894894
#endif
895895

896-
static int kgdb_panic_event(struct notifier_block *self,
897-
unsigned long val,
898-
void *data)
896+
void kgdb_panic(const char *msg)
899897
{
898+
if (!kgdb_io_module_registered)
899+
return;
900+
900901
/*
901-
* Avoid entering the debugger if we were triggered due to a panic
902-
* We don't want to get stuck waiting for input from user in such case.
903-
* panic_timeout indicates the system should automatically
902+
* We don't want to get stuck waiting for input from user if
903+
* "panic_timeout" indicates the system should automatically
904904
* reboot on panic.
905905
*/
906906
if (panic_timeout)
907-
return NOTIFY_DONE;
907+
return;
908908

909909
if (dbg_kdb_mode)
910-
kdb_printf("PANIC: %s\n", (char *)data);
910+
kdb_printf("PANIC: %s\n", msg);
911+
911912
kgdb_breakpoint();
912-
return NOTIFY_DONE;
913913
}
914914

915-
static struct notifier_block kgdb_panic_event_nb = {
916-
.notifier_call = kgdb_panic_event,
917-
.priority = INT_MAX,
918-
};
919-
920915
void __weak kgdb_arch_late(void)
921916
{
922917
}
@@ -965,8 +960,6 @@ static void kgdb_register_callbacks(void)
965960
kgdb_arch_late();
966961
register_module_notifier(&dbg_module_load_nb);
967962
register_reboot_notifier(&dbg_reboot_notifier);
968-
atomic_notifier_chain_register(&panic_notifier_list,
969-
&kgdb_panic_event_nb);
970963
#ifdef CONFIG_MAGIC_SYSRQ
971964
register_sysrq_key('g', &sysrq_dbg_op);
972965
#endif
@@ -980,16 +973,14 @@ static void kgdb_register_callbacks(void)
980973
static void kgdb_unregister_callbacks(void)
981974
{
982975
/*
983-
* When this routine is called KGDB should unregister from the
984-
* panic handler and clean up, making sure it is not handling any
976+
* When this routine is called KGDB should unregister from
977+
* handlers and clean up, making sure it is not handling any
985978
* break exceptions at the time.
986979
*/
987980
if (kgdb_io_module_registered) {
988981
kgdb_io_module_registered = 0;
989982
unregister_reboot_notifier(&dbg_reboot_notifier);
990983
unregister_module_notifier(&dbg_module_load_nb);
991-
atomic_notifier_chain_unregister(&panic_notifier_list,
992-
&kgdb_panic_event_nb);
993984
kgdb_arch_exit();
994985
#ifdef CONFIG_MAGIC_SYSRQ
995986
unregister_sysrq_key('g', &sysrq_dbg_op);

‎kernel/panic.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/debug_locks.h>
1313
#include <linux/sched/debug.h>
1414
#include <linux/interrupt.h>
15+
#include <linux/kgdb.h>
1516
#include <linux/kmsg_dump.h>
1617
#include <linux/kallsyms.h>
1718
#include <linux/notifier.h>
@@ -219,6 +220,13 @@ void panic(const char *fmt, ...)
219220
dump_stack();
220221
#endif
221222

223+
/*
224+
* If kgdb is enabled, give it a chance to run before we stop all
225+
* the other CPUs or else we won't be able to debug processes left
226+
* running on them.
227+
*/
228+
kgdb_panic(buf);
229+
222230
/*
223231
* If we have crashed and we have a crash kernel loaded let it handle
224232
* everything else.

0 commit comments

Comments
 (0)