dump_stack.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include <linux/kernel.h>
  7. #include <linux/export.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/debug.h>
  10. #include <linux/smp.h>
  11. #include <linux/atomic.h>
  12. #include <linux/kexec.h>
  13. #include <linux/utsname.h>
  14. static char dump_stack_arch_desc_str[128];
  15. /**
  16. * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
  17. * @fmt: printf-style format string
  18. * @...: arguments for the format string
  19. *
  20. * The configured string will be printed right after utsname during task
  21. * dumps. Usually used to add arch-specific system identifiers. If an
  22. * arch wants to make use of such an ID string, it should initialize this
  23. * as soon as possible during boot.
  24. */
  25. void __init dump_stack_set_arch_desc(const char *fmt, ...)
  26. {
  27. va_list args;
  28. va_start(args, fmt);
  29. vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str),
  30. fmt, args);
  31. va_end(args);
  32. }
  33. /**
  34. * dump_stack_print_info - print generic debug info for dump_stack()
  35. * @log_lvl: log level
  36. *
  37. * Arch-specific dump_stack() implementations can use this function to
  38. * print out the same debug information as the generic dump_stack().
  39. */
  40. void dump_stack_print_info(const char *log_lvl)
  41. {
  42. printk("%sCPU: %d PID: %d Comm: %.20s %s%s %s %.*s\n",
  43. log_lvl, raw_smp_processor_id(), current->pid, current->comm,
  44. kexec_crash_loaded() ? "Kdump: loaded " : "",
  45. print_tainted(),
  46. init_utsname()->release,
  47. (int)strcspn(init_utsname()->version, " "),
  48. init_utsname()->version);
  49. if (dump_stack_arch_desc_str[0] != '\0')
  50. printk("%sHardware name: %s\n",
  51. log_lvl, dump_stack_arch_desc_str);
  52. print_worker_info(log_lvl, current);
  53. }
  54. /**
  55. * show_regs_print_info - print generic debug info for show_regs()
  56. * @log_lvl: log level
  57. *
  58. * show_regs() implementations can use this function to print out generic
  59. * debug information.
  60. */
  61. void show_regs_print_info(const char *log_lvl)
  62. {
  63. dump_stack_print_info(log_lvl);
  64. }
  65. static void __dump_stack(const char *log_lvl)
  66. {
  67. dump_stack_print_info(log_lvl);
  68. show_stack(NULL, NULL, log_lvl);
  69. }
  70. /**
  71. * dump_stack - dump the current task information and its stack trace
  72. *
  73. * Architectures can override this implementation by implementing its own.
  74. */
  75. #ifdef CONFIG_SMP
  76. static atomic_t dump_lock = ATOMIC_INIT(-1);
  77. asmlinkage __visible void dump_stack_lvl(const char *log_lvl)
  78. {
  79. unsigned long flags;
  80. int was_locked;
  81. int old;
  82. int cpu;
  83. /*
  84. * Permit this cpu to perform nested stack dumps while serialising
  85. * against other CPUs
  86. */
  87. retry:
  88. local_irq_save(flags);
  89. cpu = smp_processor_id();
  90. old = atomic_cmpxchg(&dump_lock, -1, cpu);
  91. if (old == -1) {
  92. was_locked = 0;
  93. } else if (old == cpu) {
  94. was_locked = 1;
  95. } else {
  96. local_irq_restore(flags);
  97. /*
  98. * Wait for the lock to release before jumping to
  99. * atomic_cmpxchg() in order to mitigate the thundering herd
  100. * problem.
  101. */
  102. do { cpu_relax(); } while (atomic_read(&dump_lock) != -1);
  103. goto retry;
  104. }
  105. __dump_stack(log_lvl);
  106. if (!was_locked)
  107. atomic_set(&dump_lock, -1);
  108. local_irq_restore(flags);
  109. }
  110. #else
  111. asmlinkage __visible void dump_stack_lvl(const char *log_lvl)
  112. {
  113. __dump_stack(log_lvl);
  114. }
  115. #endif
  116. EXPORT_SYMBOL(dump_stack_lvl);
  117. asmlinkage __visible void dump_stack(void)
  118. {
  119. dump_stack_lvl(KERN_DEFAULT);
  120. }
  121. EXPORT_SYMBOL(dump_stack);