1 // SPDX-License-Identifier: GPL-2.0-only
3 * Stack tracing support
5 * Copyright (C) 2012 ARM Ltd.
7 #include <linux/kernel.h>
8 #include <linux/export.h>
9 #include <linux/ftrace.h>
10 #include <linux/sched.h>
11 #include <linux/sched/debug.h>
12 #include <linux/sched/task_stack.h>
13 #include <linux/stacktrace.h>
16 #include <asm/stack_pointer.h>
17 #include <asm/stacktrace.h>
20 * Start an unwind from a pt_regs.
22 * The unwind will begin at the PC within the regs.
24 * The regs must be on a stack currently owned by the calling task.
26 static inline void unwind_init_from_regs(struct unwind_state *state,
29 unwind_init_common(state, current);
31 state->fp = regs->regs[29];
36 * Start an unwind from a caller.
38 * The unwind will begin at the caller of whichever function this is inlined
41 * The function which invokes this must be noinline.
43 static __always_inline void unwind_init_from_caller(struct unwind_state *state)
45 unwind_init_common(state, current);
47 state->fp = (unsigned long)__builtin_frame_address(1);
48 state->pc = (unsigned long)__builtin_return_address(0);
52 * Start an unwind from a blocked task.
54 * The unwind will begin at the blocked tasks saved PC (i.e. the caller of
57 * The caller should ensure the task is blocked in cpu_switch_to() for the
58 * duration of the unwind, or the unwind will be bogus. It is never valid to
59 * call this for the current task.
61 static inline void unwind_init_from_task(struct unwind_state *state,
62 struct task_struct *task)
64 unwind_init_common(state, task);
66 state->fp = thread_saved_fp(task);
67 state->pc = thread_saved_pc(task);
71 * We can only safely access per-cpu stacks from current in a non-preemptible
74 static bool on_accessible_stack(const struct task_struct *tsk,
75 unsigned long sp, unsigned long size,
76 struct stack_info *info)
79 info->type = STACK_TYPE_UNKNOWN;
81 if (on_task_stack(tsk, sp, size, info))
83 if (tsk != current || preemptible())
85 if (on_irq_stack(sp, size, info))
87 if (on_overflow_stack(sp, size, info))
89 if (on_sdei_stack(sp, size, info))
96 * Unwind from one frame record (A) to the next frame record (B).
98 * We terminate early if the location of B indicates a malformed chain of frame
99 * records (e.g. a cycle), determined based on the location and fp value of A
100 * and the location (but not the fp value) of B.
102 static int notrace unwind_next(struct unwind_state *state)
104 struct task_struct *tsk = state->task;
105 unsigned long fp = state->fp;
106 struct stack_info info;
109 /* Final frame; nothing to unwind */
110 if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
113 err = unwind_next_common(state, &info, on_accessible_stack, NULL);
117 state->pc = ptrauth_strip_insn_pac(state->pc);
119 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
120 if (tsk->ret_stack &&
121 (state->pc == (unsigned long)return_to_handler)) {
122 unsigned long orig_pc;
124 * This is a case where function graph tracer has
125 * modified a return address (LR) in a stack frame
126 * to hook a function return.
127 * So replace it to an original value.
129 orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc,
131 if (WARN_ON_ONCE(state->pc == orig_pc))
135 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
136 #ifdef CONFIG_KRETPROBES
137 if (is_kretprobe_trampoline(state->pc))
138 state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur);
143 NOKPROBE_SYMBOL(unwind_next);
145 static void notrace unwind(struct unwind_state *state,
146 stack_trace_consume_fn consume_entry, void *cookie)
151 if (!consume_entry(cookie, state->pc))
153 ret = unwind_next(state);
158 NOKPROBE_SYMBOL(unwind);
160 static bool dump_backtrace_entry(void *arg, unsigned long where)
163 printk("%s %pSb\n", loglvl, (void *)where);
167 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
170 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
172 if (regs && user_mode(regs))
178 if (!try_get_task_stack(tsk))
181 printk("%sCall trace:\n", loglvl);
182 arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
187 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
189 dump_backtrace(NULL, tsk, loglvl);
193 noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry,
194 void *cookie, struct task_struct *task,
195 struct pt_regs *regs)
197 struct unwind_state state;
202 unwind_init_from_regs(&state, regs);
203 } else if (task == current) {
204 unwind_init_from_caller(&state);
206 unwind_init_from_task(&state, task);
209 unwind(&state, consume_entry, cookie);