Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Provide a default dump_stack() function for architectures |
| 3 | * which don't implement their own. |
| 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 7 | #include <linux/export.h> |
Tejun Heo | 196779b | 2013-04-30 15:27:12 -0700 | [diff] [blame] | 8 | #include <linux/sched.h> |
Alex Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 9 | #include <linux/smp.h> |
| 10 | #include <linux/atomic.h> |
| 11 | |
| 12 | static void __dump_stack(void) |
| 13 | { |
| 14 | dump_stack_print_info(KERN_DEFAULT); |
| 15 | show_stack(NULL, NULL); |
| 16 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Tejun Heo | 196779b | 2013-04-30 15:27:12 -0700 | [diff] [blame] | 18 | /** |
| 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 Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 23 | #ifdef CONFIG_SMP |
| 24 | static atomic_t dump_lock = ATOMIC_INIT(-1); |
| 25 | |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 26 | asmlinkage __visible void dump_stack(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Eric Dumazet | d7ce369 | 2016-02-05 15:36:16 -0800 | [diff] [blame] | 28 | unsigned long flags; |
Alex Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 29 | 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 Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 37 | retry: |
Eric Dumazet | d7ce369 | 2016-02-05 15:36:16 -0800 | [diff] [blame] | 38 | local_irq_save(flags); |
Alex Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 39 | 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 Dumazet | d7ce369 | 2016-02-05 15:36:16 -0800 | [diff] [blame] | 46 | local_irq_restore(flags); |
Kevin Hao | 1bb7772 | 2019-11-05 21:16:57 -0800 | [diff] [blame] | 47 | /* |
| 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 Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 53 | goto retry; |
| 54 | } |
| 55 | |
| 56 | __dump_stack(); |
| 57 | |
| 58 | if (!was_locked) |
| 59 | atomic_set(&dump_lock, -1); |
| 60 | |
Eric Dumazet | d7ce369 | 2016-02-05 15:36:16 -0800 | [diff] [blame] | 61 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
Alex Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 63 | #else |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 64 | asmlinkage __visible void dump_stack(void) |
Alex Thorlton | b58d977 | 2013-07-03 15:04:59 -0700 | [diff] [blame] | 65 | { |
| 66 | __dump_stack(); |
| 67 | } |
| 68 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | EXPORT_SYMBOL(dump_stack); |