xref: /linux-6.15/arch/arm64/kernel/stacktrace.c (revision 5b1b0861)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Stack tracing support
4  *
5  * Copyright (C) 2012 ARM Ltd.
6  */
7 #include <linux/kernel.h>
8 #include <linux/export.h>
9 #include <linux/ftrace.h>
10 #include <linux/kprobes.h>
11 #include <linux/sched.h>
12 #include <linux/sched/debug.h>
13 #include <linux/sched/task_stack.h>
14 #include <linux/stacktrace.h>
15 
16 #include <asm/irq.h>
17 #include <asm/pointer_auth.h>
18 #include <asm/stack_pointer.h>
19 #include <asm/stacktrace.h>
20 
21 /*
22  * Start an unwind from a pt_regs.
23  *
24  * The unwind will begin at the PC within the regs.
25  *
26  * The regs must be on a stack currently owned by the calling task.
27  */
28 static inline void unwind_init_from_regs(struct unwind_state *state,
29 					 struct pt_regs *regs)
30 {
31 	unwind_init_common(state, current);
32 
33 	state->fp = regs->regs[29];
34 	state->pc = regs->pc;
35 }
36 
37 /*
38  * Start an unwind from a caller.
39  *
40  * The unwind will begin at the caller of whichever function this is inlined
41  * into.
42  *
43  * The function which invokes this must be noinline.
44  */
45 static __always_inline void unwind_init_from_caller(struct unwind_state *state)
46 {
47 	unwind_init_common(state, current);
48 
49 	state->fp = (unsigned long)__builtin_frame_address(1);
50 	state->pc = (unsigned long)__builtin_return_address(0);
51 }
52 
53 /*
54  * Start an unwind from a blocked task.
55  *
56  * The unwind will begin at the blocked tasks saved PC (i.e. the caller of
57  * cpu_switch_to()).
58  *
59  * The caller should ensure the task is blocked in cpu_switch_to() for the
60  * duration of the unwind, or the unwind will be bogus. It is never valid to
61  * call this for the current task.
62  */
63 static inline void unwind_init_from_task(struct unwind_state *state,
64 					 struct task_struct *task)
65 {
66 	unwind_init_common(state, task);
67 
68 	state->fp = thread_saved_fp(task);
69 	state->pc = thread_saved_pc(task);
70 }
71 
72 /*
73  * Unwind from one frame record (A) to the next frame record (B).
74  *
75  * We terminate early if the location of B indicates a malformed chain of frame
76  * records (e.g. a cycle), determined based on the location and fp value of A
77  * and the location (but not the fp value) of B.
78  */
79 static int notrace unwind_next(struct unwind_state *state)
80 {
81 	struct task_struct *tsk = state->task;
82 	unsigned long fp = state->fp;
83 	struct stack_info info;
84 	int err;
85 
86 	/* Final frame; nothing to unwind */
87 	if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
88 		return -ENOENT;
89 
90 	err = unwind_next_common(state, &info, NULL);
91 	if (err)
92 		return err;
93 
94 	state->pc = ptrauth_strip_insn_pac(state->pc);
95 
96 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
97 	if (tsk->ret_stack &&
98 		(state->pc == (unsigned long)return_to_handler)) {
99 		unsigned long orig_pc;
100 		/*
101 		 * This is a case where function graph tracer has
102 		 * modified a return address (LR) in a stack frame
103 		 * to hook a function return.
104 		 * So replace it to an original value.
105 		 */
106 		orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc,
107 						(void *)state->fp);
108 		if (WARN_ON_ONCE(state->pc == orig_pc))
109 			return -EINVAL;
110 		state->pc = orig_pc;
111 	}
112 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
113 #ifdef CONFIG_KRETPROBES
114 	if (is_kretprobe_trampoline(state->pc))
115 		state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur);
116 #endif
117 
118 	return 0;
119 }
120 NOKPROBE_SYMBOL(unwind_next);
121 
122 static void notrace unwind(struct unwind_state *state,
123 			   stack_trace_consume_fn consume_entry, void *cookie)
124 {
125 	while (1) {
126 		int ret;
127 
128 		if (!consume_entry(cookie, state->pc))
129 			break;
130 		ret = unwind_next(state);
131 		if (ret < 0)
132 			break;
133 	}
134 }
135 NOKPROBE_SYMBOL(unwind);
136 
137 static bool dump_backtrace_entry(void *arg, unsigned long where)
138 {
139 	char *loglvl = arg;
140 	printk("%s %pSb\n", loglvl, (void *)where);
141 	return true;
142 }
143 
144 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
145 		    const char *loglvl)
146 {
147 	pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
148 
149 	if (regs && user_mode(regs))
150 		return;
151 
152 	if (!tsk)
153 		tsk = current;
154 
155 	if (!try_get_task_stack(tsk))
156 		return;
157 
158 	printk("%sCall trace:\n", loglvl);
159 	arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
160 
161 	put_task_stack(tsk);
162 }
163 
164 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
165 {
166 	dump_backtrace(NULL, tsk, loglvl);
167 	barrier();
168 }
169 
170 noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry,
171 			      void *cookie, struct task_struct *task,
172 			      struct pt_regs *regs)
173 {
174 	struct unwind_state state;
175 
176 	if (regs) {
177 		if (task != current)
178 			return;
179 		unwind_init_from_regs(&state, regs);
180 	} else if (task == current) {
181 		unwind_init_from_caller(&state);
182 	} else {
183 		unwind_init_from_task(&state, task);
184 	}
185 
186 	unwind(&state, consume_entry, cookie);
187 }
188