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