blob: 222c6d6c8281a421b1a58f49b5e11e5048dce784 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Provide a default dump_stack() function for architectures
4 * which don't implement their own.
5 */
6
7#include <linux/kernel.h>
Stephen Boyd22f4e662021-07-07 18:09:17 -07008#include <linux/buildid.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05009#include <linux/export.h>
Tejun Heo196779b2013-04-30 15:27:12 -070010#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010011#include <linux/sched/debug.h>
Alex Thorltonb58d9772013-07-03 15:04:59 -070012#include <linux/smp.h>
13#include <linux/atomic.h>
Dave Younge36df282018-02-13 15:28:34 +080014#include <linux/kexec.h>
15#include <linux/utsname.h>
Peter Zijlstraa8b62fd2020-09-21 12:58:17 +020016#include <linux/stop_machine.h>
Dave Younge36df282018-02-13 15:28:34 +080017
18static char dump_stack_arch_desc_str[128];
19
20/**
21 * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
22 * @fmt: printf-style format string
23 * @...: arguments for the format string
24 *
25 * The configured string will be printed right after utsname during task
26 * dumps. Usually used to add arch-specific system identifiers. If an
27 * arch wants to make use of such an ID string, it should initialize this
28 * as soon as possible during boot.
29 */
30void __init dump_stack_set_arch_desc(const char *fmt, ...)
31{
32 va_list args;
33
34 va_start(args, fmt);
35 vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str),
36 fmt, args);
37 va_end(args);
38}
39
Stephen Boyd22f4e662021-07-07 18:09:17 -070040#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
41#define BUILD_ID_FMT " %20phN"
42#define BUILD_ID_VAL vmlinux_build_id
43#else
44#define BUILD_ID_FMT "%s"
45#define BUILD_ID_VAL ""
46#endif
47
Dave Younge36df282018-02-13 15:28:34 +080048/**
49 * dump_stack_print_info - print generic debug info for dump_stack()
50 * @log_lvl: log level
51 *
52 * Arch-specific dump_stack() implementations can use this function to
53 * print out the same debug information as the generic dump_stack().
54 */
55void dump_stack_print_info(const char *log_lvl)
56{
Stephen Boyd22f4e662021-07-07 18:09:17 -070057 printk("%sCPU: %d PID: %d Comm: %.20s %s%s %s %.*s" BUILD_ID_FMT "\n",
Dave Younge36df282018-02-13 15:28:34 +080058 log_lvl, raw_smp_processor_id(), current->pid, current->comm,
59 kexec_crash_loaded() ? "Kdump: loaded " : "",
60 print_tainted(),
61 init_utsname()->release,
62 (int)strcspn(init_utsname()->version, " "),
Stephen Boyd22f4e662021-07-07 18:09:17 -070063 init_utsname()->version, BUILD_ID_VAL);
Dave Younge36df282018-02-13 15:28:34 +080064
65 if (dump_stack_arch_desc_str[0] != '\0')
66 printk("%sHardware name: %s\n",
67 log_lvl, dump_stack_arch_desc_str);
68
69 print_worker_info(log_lvl, current);
Peter Zijlstraa8b62fd2020-09-21 12:58:17 +020070 print_stop_info(log_lvl, current);
Dave Younge36df282018-02-13 15:28:34 +080071}
72
73/**
74 * show_regs_print_info - print generic debug info for show_regs()
75 * @log_lvl: log level
76 *
77 * show_regs() implementations can use this function to print out generic
78 * debug information.
79 */
80void show_regs_print_info(const char *log_lvl)
81{
82 dump_stack_print_info(log_lvl);
83}
Alex Thorltonb58d9772013-07-03 15:04:59 -070084
Alexander Potapenko4469c0f2021-06-28 19:40:30 -070085static void __dump_stack(const char *log_lvl)
Alex Thorltonb58d9772013-07-03 15:04:59 -070086{
Alexander Potapenko4469c0f2021-06-28 19:40:30 -070087 dump_stack_print_info(log_lvl);
88 show_stack(NULL, NULL, log_lvl);
Alex Thorltonb58d9772013-07-03 15:04:59 -070089}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Tejun Heo196779b2013-04-30 15:27:12 -070091/**
Randy Dunlap83a29be2021-09-07 19:58:51 -070092 * dump_stack_lvl - dump the current task information and its stack trace
93 * @log_lvl: log level
Tejun Heo196779b2013-04-30 15:27:12 -070094 *
95 * Architectures can override this implementation by implementing its own.
96 */
Alexander Potapenko4469c0f2021-06-28 19:40:30 -070097asmlinkage __visible void dump_stack_lvl(const char *log_lvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
John Ogness7412dc62024-02-07 14:47:03 +010699 bool in_panic = this_cpu_in_panic();
Eric Dumazetd7ce3692016-02-05 15:36:16 -0800100 unsigned long flags;
Alex Thorltonb58d9772013-07-03 15:04:59 -0700101
102 /*
103 * Permit this cpu to perform nested stack dumps while serialising
John Ogness7412dc62024-02-07 14:47:03 +0106104 * against other CPUs, unless this CPU is in panic.
105 *
106 * When in panic, non-panic CPUs are not permitted to store new
107 * printk messages so there is no need to synchronize the output.
108 * This avoids potential deadlock in panic() if another CPU is
109 * holding and unable to release the printk_cpu_sync.
Alex Thorltonb58d9772013-07-03 15:04:59 -0700110 */
John Ogness7412dc62024-02-07 14:47:03 +0106111 if (!in_panic)
112 printk_cpu_sync_get_irqsave(flags);
113
Alexander Potapenko4469c0f2021-06-28 19:40:30 -0700114 __dump_stack(log_lvl);
John Ogness7412dc62024-02-07 14:47:03 +0106115
116 if (!in_panic)
117 printk_cpu_sync_put_irqrestore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
Alexander Potapenko4469c0f2021-06-28 19:40:30 -0700119EXPORT_SYMBOL(dump_stack_lvl);
120
121asmlinkage __visible void dump_stack(void)
122{
123 dump_stack_lvl(KERN_DEFAULT);
124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125EXPORT_SYMBOL(dump_stack);