blob: 72de6444934dc23731ee336328025a3e3ade92bd [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);
Kevin Hao1bb77722019-11-05 21:16:57 -080047 /*
48 * Wait for the lock to release before jumping to
49 * atomic_cmpxchg() in order to mitigate the thundering herd
50 * problem.
51 */
52 do { cpu_relax(); } while (atomic_read(&dump_lock) != -1);
Alex Thorltonb58d9772013-07-03 15:04:59 -070053 goto retry;
54 }
55
56 __dump_stack();
57
58 if (!was_locked)
59 atomic_set(&dump_lock, -1);
60
Eric Dumazetd7ce3692016-02-05 15:36:16 -080061 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
Alex Thorltonb58d9772013-07-03 15:04:59 -070063#else
Andi Kleen722a9f92014-05-02 00:44:38 +020064asmlinkage __visible void dump_stack(void)
Alex Thorltonb58d9772013-07-03 15:04:59 -070065{
66 __dump_stack();
67}
68#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070069EXPORT_SYMBOL(dump_stack);