blob: c5edbedd364dce20f028614c0b7679878e70913d [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>
13
14static void __dump_stack(void)
15{
16 dump_stack_print_info(KERN_DEFAULT);
17 show_stack(NULL, NULL);
18}
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Tejun Heo196779b2013-04-30 15:27:12 -070020/**
21 * dump_stack - dump the current task information and its stack trace
22 *
23 * Architectures can override this implementation by implementing its own.
24 */
Alex Thorltonb58d9772013-07-03 15:04:59 -070025#ifdef CONFIG_SMP
26static atomic_t dump_lock = ATOMIC_INIT(-1);
27
Andi Kleen722a9f92014-05-02 00:44:38 +020028asmlinkage __visible void dump_stack(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Eric Dumazetd7ce3692016-02-05 15:36:16 -080030 unsigned long flags;
Alex Thorltonb58d9772013-07-03 15:04:59 -070031 int was_locked;
32 int old;
33 int cpu;
34
35 /*
36 * Permit this cpu to perform nested stack dumps while serialising
37 * against other CPUs
38 */
Alex Thorltonb58d9772013-07-03 15:04:59 -070039retry:
Eric Dumazetd7ce3692016-02-05 15:36:16 -080040 local_irq_save(flags);
Alex Thorltonb58d9772013-07-03 15:04:59 -070041 cpu = smp_processor_id();
42 old = atomic_cmpxchg(&dump_lock, -1, cpu);
43 if (old == -1) {
44 was_locked = 0;
45 } else if (old == cpu) {
46 was_locked = 1;
47 } else {
Eric Dumazetd7ce3692016-02-05 15:36:16 -080048 local_irq_restore(flags);
Alex Thorltonb58d9772013-07-03 15:04:59 -070049 cpu_relax();
50 goto retry;
51 }
52
53 __dump_stack();
54
55 if (!was_locked)
56 atomic_set(&dump_lock, -1);
57
Eric Dumazetd7ce3692016-02-05 15:36:16 -080058 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
Alex Thorltonb58d9772013-07-03 15:04:59 -070060#else
Andi Kleen722a9f92014-05-02 00:44:38 +020061asmlinkage __visible void dump_stack(void)
Alex Thorltonb58d9772013-07-03 15:04:59 -070062{
63 __dump_stack();
64}
65#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070066EXPORT_SYMBOL(dump_stack);