^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2008 ARM Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2014 Regents of the University of California
^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/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kallsyms.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/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) register unsigned long sp_in_global __asm__("sp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #ifdef CONFIG_FRAME_POINTER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct stackframe {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) unsigned long fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned long ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) bool (*fn)(unsigned long, void *), void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned long fp, sp, pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) fp = frame_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) sp = user_stack_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) pc = instruction_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) } else if (task == NULL || task == current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) const register unsigned long current_sp = sp_in_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) fp = (unsigned long)__builtin_frame_address(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) sp = current_sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) pc = (unsigned long)walk_stackframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* task blocked in __switch_to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) fp = task->thread.s[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) sp = task->thread.sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) pc = task->thread.ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long low, high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct stackframe *frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (unlikely(!__kernel_text_address(pc) || fn(pc, arg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* Validate frame pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) low = sp + sizeof(struct stackframe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) high = ALIGN(sp, THREAD_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (unlikely(fp < low || fp > high || fp & 0x7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* Unwind stack frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) frame = (struct stackframe *)fp - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) sp = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) fp = frame->fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) (unsigned long *)(fp - 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #else /* !CONFIG_FRAME_POINTER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) void notrace walk_stackframe(struct task_struct *task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct pt_regs *regs, bool (*fn)(unsigned long, void *), void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) unsigned long sp, pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned long *ksp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) sp = user_stack_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) pc = instruction_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) } else if (task == NULL || task == current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) sp = sp_in_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) pc = (unsigned long)walk_stackframe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* task blocked in __switch_to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) sp = task->thread.sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pc = task->thread.ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (unlikely(sp & 0x7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ksp = (unsigned long *)sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) while (!kstack_end(ksp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (__kernel_text_address(pc) && unlikely(fn(pc, arg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) pc = (*ksp++) - 0x4;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #endif /* CONFIG_FRAME_POINTER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static bool print_trace_address(unsigned long pc, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) const char *loglvl = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) print_ip_sym(loglvl, pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) pr_cont("Call Trace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) walk_stackframe(task, NULL, print_trace_address, (void *)loglvl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static bool save_wchan(unsigned long pc, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (!in_sched_functions(pc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) unsigned long *p = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) *p = pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) unsigned long get_wchan(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned long pc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (likely(task && task != current && task->state != TASK_RUNNING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) walk_stackframe(task, NULL, save_wchan, &pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #ifdef CONFIG_STACKTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static bool __save_trace(unsigned long pc, void *arg, bool nosched)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct stack_trace *trace = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (unlikely(nosched && in_sched_functions(pc)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (unlikely(trace->skip > 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) trace->skip--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) trace->entries[trace->nr_entries++] = pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return (trace->nr_entries >= trace->max_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static bool save_trace(unsigned long pc, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return __save_trace(pc, arg, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * Save stack-backtrace addresses into a stack_trace buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) walk_stackframe(tsk, NULL, save_trace, trace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void save_stack_trace(struct stack_trace *trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) save_stack_trace_tsk(NULL, trace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) EXPORT_SYMBOL_GPL(save_stack_trace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #endif /* CONFIG_STACKTRACE */