GNU Linux-libre 4.14.254-gnu1
[releases.git] / lib / dump_stack.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Provide a default dump_stack() function for architectures
4  * which don't implement their own.
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/export.h>
9 #include <linux/sched.h>
10 #include <linux/sched/debug.h>
11 #include <linux/smp.h>
12 #include <linux/atomic.h>
13
14 static void __dump_stack(void)
15 {
16         dump_stack_print_info(KERN_DEFAULT);
17         show_stack(NULL, NULL);
18 }
19
20 /**
21  * dump_stack - dump the current task information and its stack trace
22  *
23  * Architectures can override this implementation by implementing its own.
24  */
25 #ifdef CONFIG_SMP
26 static atomic_t dump_lock = ATOMIC_INIT(-1);
27
28 asmlinkage __visible void dump_stack(void)
29 {
30         unsigned long flags;
31         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          */
39 retry:
40         local_irq_save(flags);
41         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 {
48                 local_irq_restore(flags);
49                 /*
50                  * Wait for the lock to release before jumping to
51                  * atomic_cmpxchg() in order to mitigate the thundering herd
52                  * problem.
53                  */
54                 do { cpu_relax(); } while (atomic_read(&dump_lock) != -1);
55                 goto retry;
56         }
57
58         __dump_stack();
59
60         if (!was_locked)
61                 atomic_set(&dump_lock, -1);
62
63         local_irq_restore(flags);
64 }
65 #else
66 asmlinkage __visible void dump_stack(void)
67 {
68         __dump_stack();
69 }
70 #endif
71 EXPORT_SYMBOL(dump_stack);