blob: c30d07e99dba4cc32be6aeb4d353d79058509b4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Provide a default dump_stack() function for architectures
3 * which don't implement their own.
4 */
5
6#include <linux/kernel.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05007#include <linux/export.h>
Tejun Heo196779b2013-04-30 15:27:12 -07008#include <linux/sched.h>
Alex Thorltonb58d9772013-07-03 15:04:59 -07009#include <linux/smp.h>
10#include <linux/atomic.h>
11
12static void __dump_stack(void)
13{
14 dump_stack_print_info(KERN_DEFAULT);
15 show_stack(NULL, NULL);
16}
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Tejun Heo196779b2013-04-30 15:27:12 -070018/**
19 * dump_stack - dump the current task information and its stack trace
20 *
21 * Architectures can override this implementation by implementing its own.
22 */
Alex Thorltonb58d9772013-07-03 15:04:59 -070023#ifdef CONFIG_SMP
24static atomic_t dump_lock = ATOMIC_INIT(-1);
25
Andi Kleen722a9f92014-05-02 00:44:38 +020026asmlinkage __visible void dump_stack(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Eric Dumazetd7ce3692016-02-05 15:36:16 -080028 unsigned long flags;
Alex Thorltonb58d9772013-07-03 15:04:59 -070029 int was_locked;
30 int old;
31 int cpu;
32
33 /*
34 * Permit this cpu to perform nested stack dumps while serialising
35 * against other CPUs
36 */
Alex Thorltonb58d9772013-07-03 15:04:59 -070037retry:
Eric Dumazetd7ce3692016-02-05 15:36:16 -080038 local_irq_save(flags);
Alex Thorltonb58d9772013-07-03 15:04:59 -070039 cpu = smp_processor_id();
40 old = atomic_cmpxchg(&dump_lock, -1, cpu);
41 if (old == -1) {
42 was_locked = 0;
43 } else if (old == cpu) {
44 was_locked = 1;
45 } else {
Eric Dumazetd7ce3692016-02-05 15:36:16 -080046 local_irq_restore(flags);
Alex Thorltonb58d9772013-07-03 15:04:59 -070047 cpu_relax();
48 goto retry;
49 }
50
51 __dump_stack();
52
53 if (!was_locked)
54 atomic_set(&dump_lock, -1);
55
Eric Dumazetd7ce3692016-02-05 15:36:16 -080056 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
Alex Thorltonb58d9772013-07-03 15:04:59 -070058#else
Andi Kleen722a9f92014-05-02 00:44:38 +020059asmlinkage __visible void dump_stack(void)
Alex Thorltonb58d9772013-07-03 15:04:59 -070060{
61 __dump_stack();
62}
63#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070064EXPORT_SYMBOL(dump_stack);