blob: f5a33b6f773f7f5725dc92fab7d597abc8dd1abc [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>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05008#include <linux/export.h>
Tejun Heo196779b2013-04-30 15:27:12 -07009#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010010#include <linux/sched/debug.h>
Alex Thorltonb58d9772013-07-03 15:04:59 -070011#include <linux/smp.h>
12#include <linux/atomic.h>
Dave Younge36df282018-02-13 15:28:34 +080013#include <linux/kexec.h>
14#include <linux/utsname.h>
Peter Zijlstraa8b62fd2020-09-21 12:58:17 +020015#include <linux/stop_machine.h>
Dave Younge36df282018-02-13 15:28:34 +080016
17static char dump_stack_arch_desc_str[128];
18
19/**
20 * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
21 * @fmt: printf-style format string
22 * @...: arguments for the format string
23 *
24 * The configured string will be printed right after utsname during task
25 * dumps. Usually used to add arch-specific system identifiers. If an
26 * arch wants to make use of such an ID string, it should initialize this
27 * as soon as possible during boot.
28 */
29void __init dump_stack_set_arch_desc(const char *fmt, ...)
30{
31 va_list args;
32
33 va_start(args, fmt);
34 vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str),
35 fmt, args);
36 va_end(args);
37}
38
39/**
40 * dump_stack_print_info - print generic debug info for dump_stack()
41 * @log_lvl: log level
42 *
43 * Arch-specific dump_stack() implementations can use this function to
44 * print out the same debug information as the generic dump_stack().
45 */
46void dump_stack_print_info(const char *log_lvl)
47{
48 printk("%sCPU: %d PID: %d Comm: %.20s %s%s %s %.*s\n",
49 log_lvl, raw_smp_processor_id(), current->pid, current->comm,
50 kexec_crash_loaded() ? "Kdump: loaded " : "",
51 print_tainted(),
52 init_utsname()->release,
53 (int)strcspn(init_utsname()->version, " "),
54 init_utsname()->version);
55
56 if (dump_stack_arch_desc_str[0] != '\0')
57 printk("%sHardware name: %s\n",
58 log_lvl, dump_stack_arch_desc_str);
59
60 print_worker_info(log_lvl, current);
Peter Zijlstraa8b62fd2020-09-21 12:58:17 +020061 print_stop_info(log_lvl, current);
Dave Younge36df282018-02-13 15:28:34 +080062}
63
64/**
65 * show_regs_print_info - print generic debug info for show_regs()
66 * @log_lvl: log level
67 *
68 * show_regs() implementations can use this function to print out generic
69 * debug information.
70 */
71void show_regs_print_info(const char *log_lvl)
72{
73 dump_stack_print_info(log_lvl);
74}
Alex Thorltonb58d9772013-07-03 15:04:59 -070075
76static void __dump_stack(void)
77{
78 dump_stack_print_info(KERN_DEFAULT);
Dmitry Safonov9cb8f062020-06-08 21:32:29 -070079 show_stack(NULL, NULL, KERN_DEFAULT);
Alex Thorltonb58d9772013-07-03 15:04:59 -070080}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Tejun Heo196779b2013-04-30 15:27:12 -070082/**
83 * dump_stack - dump the current task information and its stack trace
84 *
85 * Architectures can override this implementation by implementing its own.
86 */
Alex Thorltonb58d9772013-07-03 15:04:59 -070087#ifdef CONFIG_SMP
88static atomic_t dump_lock = ATOMIC_INIT(-1);
89
Andi Kleen722a9f92014-05-02 00:44:38 +020090asmlinkage __visible void dump_stack(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Eric Dumazetd7ce3692016-02-05 15:36:16 -080092 unsigned long flags;
Alex Thorltonb58d9772013-07-03 15:04:59 -070093 int was_locked;
94 int old;
95 int cpu;
96
97 /*
98 * Permit this cpu to perform nested stack dumps while serialising
99 * against other CPUs
100 */
Alex Thorltonb58d9772013-07-03 15:04:59 -0700101retry:
Eric Dumazetd7ce3692016-02-05 15:36:16 -0800102 local_irq_save(flags);
Alex Thorltonb58d9772013-07-03 15:04:59 -0700103 cpu = smp_processor_id();
104 old = atomic_cmpxchg(&dump_lock, -1, cpu);
105 if (old == -1) {
106 was_locked = 0;
107 } else if (old == cpu) {
108 was_locked = 1;
109 } else {
Eric Dumazetd7ce3692016-02-05 15:36:16 -0800110 local_irq_restore(flags);
Kevin Hao5cbf2ff2019-11-05 21:16:57 -0800111 /*
112 * Wait for the lock to release before jumping to
113 * atomic_cmpxchg() in order to mitigate the thundering herd
114 * problem.
115 */
116 do { cpu_relax(); } while (atomic_read(&dump_lock) != -1);
Alex Thorltonb58d9772013-07-03 15:04:59 -0700117 goto retry;
118 }
119
120 __dump_stack();
121
122 if (!was_locked)
123 atomic_set(&dump_lock, -1);
124
Eric Dumazetd7ce3692016-02-05 15:36:16 -0800125 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
Alex Thorltonb58d9772013-07-03 15:04:59 -0700127#else
Andi Kleen722a9f92014-05-02 00:44:38 +0200128asmlinkage __visible void dump_stack(void)
Alex Thorltonb58d9772013-07-03 15:04:59 -0700129{
130 __dump_stack();
131}
132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133EXPORT_SYMBOL(dump_stack);