xref: /linux-6.15/arch/arm64/kernel/stacktrace.c (revision 32ed1205)
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/efi.h>
9 #include <linux/export.h>
10 #include <linux/filter.h>
11 #include <linux/ftrace.h>
12 #include <linux/kprobes.h>
13 #include <linux/sched.h>
14 #include <linux/sched/debug.h>
15 #include <linux/sched/task_stack.h>
16 #include <linux/stacktrace.h>
17 
18 #include <asm/efi.h>
19 #include <asm/irq.h>
20 #include <asm/stack_pointer.h>
21 #include <asm/stacktrace.h>
22 
23 enum kunwind_source {
24 	KUNWIND_SOURCE_UNKNOWN,
25 	KUNWIND_SOURCE_FRAME,
26 	KUNWIND_SOURCE_CALLER,
27 	KUNWIND_SOURCE_TASK,
28 	KUNWIND_SOURCE_REGS_PC,
29 };
30 
31 union unwind_flags {
32 	unsigned long	all;
33 	struct {
34 		unsigned long	fgraph : 1,
35 				kretprobe : 1;
36 	};
37 };
38 
39 /*
40  * Kernel unwind state
41  *
42  * @common:      Common unwind state.
43  * @task:        The task being unwound.
44  * @graph_idx:   Used by ftrace_graph_ret_addr() for optimized stack unwinding.
45  * @kr_cur:      When KRETPROBES is selected, holds the kretprobe instance
46  *               associated with the most recently encountered replacement lr
47  *               value.
48  */
49 struct kunwind_state {
50 	struct unwind_state common;
51 	struct task_struct *task;
52 	int graph_idx;
53 #ifdef CONFIG_KRETPROBES
54 	struct llist_node *kr_cur;
55 #endif
56 	enum kunwind_source source;
57 	union unwind_flags flags;
58 	struct pt_regs *regs;
59 };
60 
61 static __always_inline void
62 kunwind_init(struct kunwind_state *state,
63 	     struct task_struct *task)
64 {
65 	unwind_init_common(&state->common);
66 	state->task = task;
67 	state->source = KUNWIND_SOURCE_UNKNOWN;
68 	state->flags.all = 0;
69 	state->regs = NULL;
70 }
71 
72 /*
73  * Start an unwind from a pt_regs.
74  *
75  * The unwind will begin at the PC within the regs.
76  *
77  * The regs must be on a stack currently owned by the calling task.
78  */
79 static __always_inline void
80 kunwind_init_from_regs(struct kunwind_state *state,
81 		       struct pt_regs *regs)
82 {
83 	kunwind_init(state, current);
84 
85 	state->regs = regs;
86 	state->common.fp = regs->regs[29];
87 	state->common.pc = regs->pc;
88 	state->source = KUNWIND_SOURCE_REGS_PC;
89 }
90 
91 /*
92  * Start an unwind from a caller.
93  *
94  * The unwind will begin at the caller of whichever function this is inlined
95  * into.
96  *
97  * The function which invokes this must be noinline.
98  */
99 static __always_inline void
100 kunwind_init_from_caller(struct kunwind_state *state)
101 {
102 	kunwind_init(state, current);
103 
104 	state->common.fp = (unsigned long)__builtin_frame_address(1);
105 	state->common.pc = (unsigned long)__builtin_return_address(0);
106 	state->source = KUNWIND_SOURCE_CALLER;
107 }
108 
109 /*
110  * Start an unwind from a blocked task.
111  *
112  * The unwind will begin at the blocked tasks saved PC (i.e. the caller of
113  * cpu_switch_to()).
114  *
115  * The caller should ensure the task is blocked in cpu_switch_to() for the
116  * duration of the unwind, or the unwind will be bogus. It is never valid to
117  * call this for the current task.
118  */
119 static __always_inline void
120 kunwind_init_from_task(struct kunwind_state *state,
121 		       struct task_struct *task)
122 {
123 	kunwind_init(state, task);
124 
125 	state->common.fp = thread_saved_fp(task);
126 	state->common.pc = thread_saved_pc(task);
127 	state->source = KUNWIND_SOURCE_TASK;
128 }
129 
130 static __always_inline int
131 kunwind_recover_return_address(struct kunwind_state *state)
132 {
133 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
134 	if (state->task->ret_stack &&
135 	    (state->common.pc == (unsigned long)return_to_handler)) {
136 		unsigned long orig_pc;
137 		orig_pc = ftrace_graph_ret_addr(state->task, &state->graph_idx,
138 						state->common.pc,
139 						(void *)state->common.fp);
140 		if (WARN_ON_ONCE(state->common.pc == orig_pc))
141 			return -EINVAL;
142 		state->common.pc = orig_pc;
143 		state->flags.fgraph = 1;
144 	}
145 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
146 
147 #ifdef CONFIG_KRETPROBES
148 	if (is_kretprobe_trampoline(state->common.pc)) {
149 		unsigned long orig_pc;
150 		orig_pc = kretprobe_find_ret_addr(state->task,
151 						  (void *)state->common.fp,
152 						  &state->kr_cur);
153 		state->common.pc = orig_pc;
154 		state->flags.kretprobe = 1;
155 	}
156 #endif /* CONFIG_KRETPROBES */
157 
158 	return 0;
159 }
160 
161 static __always_inline
162 int kunwind_next_regs_pc(struct kunwind_state *state)
163 {
164 	struct stack_info *info;
165 	unsigned long fp = state->common.fp;
166 	struct pt_regs *regs;
167 
168 	regs = container_of((u64 *)fp, struct pt_regs, stackframe.record.fp);
169 
170 	info = unwind_find_stack(&state->common, (unsigned long)regs, sizeof(*regs));
171 	if (!info)
172 		return -EINVAL;
173 
174 	unwind_consume_stack(&state->common, info, (unsigned long)regs,
175 			     sizeof(*regs));
176 
177 	state->regs = regs;
178 	state->common.pc = regs->pc;
179 	state->common.fp = regs->regs[29];
180 	state->regs = NULL;
181 	state->source = KUNWIND_SOURCE_REGS_PC;
182 	return 0;
183 }
184 
185 static __always_inline int
186 kunwind_next_frame_record_meta(struct kunwind_state *state)
187 {
188 	struct task_struct *tsk = state->task;
189 	unsigned long fp = state->common.fp;
190 	struct frame_record_meta *meta;
191 	struct stack_info *info;
192 
193 	info = unwind_find_stack(&state->common, fp, sizeof(*meta));
194 	if (!info)
195 		return -EINVAL;
196 
197 	meta = (struct frame_record_meta *)fp;
198 	switch (READ_ONCE(meta->type)) {
199 	case FRAME_META_TYPE_FINAL:
200 		if (meta == &task_pt_regs(tsk)->stackframe)
201 			return -ENOENT;
202 		WARN_ON_ONCE(1);
203 		return -EINVAL;
204 	case FRAME_META_TYPE_PT_REGS:
205 		return kunwind_next_regs_pc(state);
206 	default:
207 		WARN_ON_ONCE(1);
208 		return -EINVAL;
209 	}
210 }
211 
212 static __always_inline int
213 kunwind_next_frame_record(struct kunwind_state *state)
214 {
215 	unsigned long fp = state->common.fp;
216 	struct frame_record *record;
217 	struct stack_info *info;
218 	unsigned long new_fp, new_pc;
219 
220 	if (fp & 0x7)
221 		return -EINVAL;
222 
223 	info = unwind_find_stack(&state->common, fp, sizeof(*record));
224 	if (!info)
225 		return -EINVAL;
226 
227 	record = (struct frame_record *)fp;
228 	new_fp = READ_ONCE(record->fp);
229 	new_pc = READ_ONCE(record->lr);
230 
231 	if (!new_fp && !new_pc)
232 		return kunwind_next_frame_record_meta(state);
233 
234 	unwind_consume_stack(&state->common, info, fp, sizeof(*record));
235 
236 	state->common.fp = new_fp;
237 	state->common.pc = new_pc;
238 	state->source = KUNWIND_SOURCE_FRAME;
239 
240 	return 0;
241 }
242 
243 /*
244  * Unwind from one frame record (A) to the next frame record (B).
245  *
246  * We terminate early if the location of B indicates a malformed chain of frame
247  * records (e.g. a cycle), determined based on the location and fp value of A
248  * and the location (but not the fp value) of B.
249  */
250 static __always_inline int
251 kunwind_next(struct kunwind_state *state)
252 {
253 	int err;
254 
255 	state->flags.all = 0;
256 
257 	switch (state->source) {
258 	case KUNWIND_SOURCE_FRAME:
259 	case KUNWIND_SOURCE_CALLER:
260 	case KUNWIND_SOURCE_TASK:
261 	case KUNWIND_SOURCE_REGS_PC:
262 		err = kunwind_next_frame_record(state);
263 		break;
264 	default:
265 		err = -EINVAL;
266 	}
267 
268 	if (err)
269 		return err;
270 
271 	state->common.pc = ptrauth_strip_kernel_insn_pac(state->common.pc);
272 
273 	return kunwind_recover_return_address(state);
274 }
275 
276 typedef bool (*kunwind_consume_fn)(const struct kunwind_state *state, void *cookie);
277 
278 static __always_inline void
279 do_kunwind(struct kunwind_state *state, kunwind_consume_fn consume_state,
280 	   void *cookie)
281 {
282 	if (kunwind_recover_return_address(state))
283 		return;
284 
285 	while (1) {
286 		int ret;
287 
288 		if (!consume_state(state, cookie))
289 			break;
290 		ret = kunwind_next(state);
291 		if (ret < 0)
292 			break;
293 	}
294 }
295 
296 /*
297  * Per-cpu stacks are only accessible when unwinding the current task in a
298  * non-preemptible context.
299  */
300 #define STACKINFO_CPU(name)					\
301 	({							\
302 		((task == current) && !preemptible())		\
303 			? stackinfo_get_##name()		\
304 			: stackinfo_get_unknown();		\
305 	})
306 
307 /*
308  * SDEI stacks are only accessible when unwinding the current task in an NMI
309  * context.
310  */
311 #define STACKINFO_SDEI(name)					\
312 	({							\
313 		((task == current) && in_nmi())			\
314 			? stackinfo_get_sdei_##name()		\
315 			: stackinfo_get_unknown();		\
316 	})
317 
318 #define STACKINFO_EFI						\
319 	({							\
320 		((task == current) && current_in_efi())		\
321 			? stackinfo_get_efi()			\
322 			: stackinfo_get_unknown();		\
323 	})
324 
325 static __always_inline void
326 kunwind_stack_walk(kunwind_consume_fn consume_state,
327 		   void *cookie, struct task_struct *task,
328 		   struct pt_regs *regs)
329 {
330 	struct stack_info stacks[] = {
331 		stackinfo_get_task(task),
332 		STACKINFO_CPU(irq),
333 #if defined(CONFIG_VMAP_STACK)
334 		STACKINFO_CPU(overflow),
335 #endif
336 #if defined(CONFIG_VMAP_STACK) && defined(CONFIG_ARM_SDE_INTERFACE)
337 		STACKINFO_SDEI(normal),
338 		STACKINFO_SDEI(critical),
339 #endif
340 #ifdef CONFIG_EFI
341 		STACKINFO_EFI,
342 #endif
343 	};
344 	struct kunwind_state state = {
345 		.common = {
346 			.stacks = stacks,
347 			.nr_stacks = ARRAY_SIZE(stacks),
348 		},
349 	};
350 
351 	if (regs) {
352 		if (task != current)
353 			return;
354 		kunwind_init_from_regs(&state, regs);
355 	} else if (task == current) {
356 		kunwind_init_from_caller(&state);
357 	} else {
358 		kunwind_init_from_task(&state, task);
359 	}
360 
361 	do_kunwind(&state, consume_state, cookie);
362 }
363 
364 struct kunwind_consume_entry_data {
365 	stack_trace_consume_fn consume_entry;
366 	void *cookie;
367 };
368 
369 static __always_inline bool
370 arch_kunwind_consume_entry(const struct kunwind_state *state, void *cookie)
371 {
372 	struct kunwind_consume_entry_data *data = cookie;
373 	return data->consume_entry(data->cookie, state->common.pc);
374 }
375 
376 noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry,
377 			      void *cookie, struct task_struct *task,
378 			      struct pt_regs *regs)
379 {
380 	struct kunwind_consume_entry_data data = {
381 		.consume_entry = consume_entry,
382 		.cookie = cookie,
383 	};
384 
385 	kunwind_stack_walk(arch_kunwind_consume_entry, &data, task, regs);
386 }
387 
388 struct bpf_unwind_consume_entry_data {
389 	bool (*consume_entry)(void *cookie, u64 ip, u64 sp, u64 fp);
390 	void *cookie;
391 };
392 
393 static bool
394 arch_bpf_unwind_consume_entry(const struct kunwind_state *state, void *cookie)
395 {
396 	struct bpf_unwind_consume_entry_data *data = cookie;
397 
398 	return data->consume_entry(data->cookie, state->common.pc, 0,
399 				   state->common.fp);
400 }
401 
402 noinline noinstr void arch_bpf_stack_walk(bool (*consume_entry)(void *cookie, u64 ip, u64 sp,
403 								u64 fp), void *cookie)
404 {
405 	struct bpf_unwind_consume_entry_data data = {
406 		.consume_entry = consume_entry,
407 		.cookie = cookie,
408 	};
409 
410 	kunwind_stack_walk(arch_bpf_unwind_consume_entry, &data, current, NULL);
411 }
412 
413 static const char *state_source_string(const struct kunwind_state *state)
414 {
415 	switch (state->source) {
416 	case KUNWIND_SOURCE_FRAME:	return NULL;
417 	case KUNWIND_SOURCE_CALLER:	return "C";
418 	case KUNWIND_SOURCE_TASK:	return "T";
419 	case KUNWIND_SOURCE_REGS_PC:	return "P";
420 	default:			return "U";
421 	}
422 }
423 
424 static bool dump_backtrace_entry(const struct kunwind_state *state, void *arg)
425 {
426 	const char *source = state_source_string(state);
427 	union unwind_flags flags = state->flags;
428 	bool has_info = source || flags.all;
429 	char *loglvl = arg;
430 
431 	printk("%s %pSb%s%s%s%s%s\n", loglvl,
432 		(void *)state->common.pc,
433 		has_info ? " (" : "",
434 		source ? source : "",
435 		flags.fgraph ? "F" : "",
436 		flags.kretprobe ? "K" : "",
437 		has_info ? ")" : "");
438 
439 	return true;
440 }
441 
442 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
443 		    const char *loglvl)
444 {
445 	pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
446 
447 	if (regs && user_mode(regs))
448 		return;
449 
450 	if (!tsk)
451 		tsk = current;
452 
453 	if (!try_get_task_stack(tsk))
454 		return;
455 
456 	printk("%sCall trace:\n", loglvl);
457 	kunwind_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
458 
459 	put_task_stack(tsk);
460 }
461 
462 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
463 {
464 	dump_backtrace(NULL, tsk, loglvl);
465 	barrier();
466 }
467 
468 /*
469  * The struct defined for userspace stack frame in AARCH64 mode.
470  */
471 struct frame_tail {
472 	struct frame_tail	__user *fp;
473 	unsigned long		lr;
474 } __attribute__((packed));
475 
476 /*
477  * Get the return address for a single stackframe and return a pointer to the
478  * next frame tail.
479  */
480 static struct frame_tail __user *
481 unwind_user_frame(struct frame_tail __user *tail, void *cookie,
482 	       stack_trace_consume_fn consume_entry)
483 {
484 	struct frame_tail buftail;
485 	unsigned long err;
486 	unsigned long lr;
487 
488 	/* Also check accessibility of one struct frame_tail beyond */
489 	if (!access_ok(tail, sizeof(buftail)))
490 		return NULL;
491 
492 	pagefault_disable();
493 	err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
494 	pagefault_enable();
495 
496 	if (err)
497 		return NULL;
498 
499 	lr = ptrauth_strip_user_insn_pac(buftail.lr);
500 
501 	if (!consume_entry(cookie, lr))
502 		return NULL;
503 
504 	/*
505 	 * Frame pointers should strictly progress back up the stack
506 	 * (towards higher addresses).
507 	 */
508 	if (tail >= buftail.fp)
509 		return NULL;
510 
511 	return buftail.fp;
512 }
513 
514 #ifdef CONFIG_COMPAT
515 /*
516  * The registers we're interested in are at the end of the variable
517  * length saved register structure. The fp points at the end of this
518  * structure so the address of this struct is:
519  * (struct compat_frame_tail *)(xxx->fp)-1
520  *
521  * This code has been adapted from the ARM OProfile support.
522  */
523 struct compat_frame_tail {
524 	compat_uptr_t	fp; /* a (struct compat_frame_tail *) in compat mode */
525 	u32		sp;
526 	u32		lr;
527 } __attribute__((packed));
528 
529 static struct compat_frame_tail __user *
530 unwind_compat_user_frame(struct compat_frame_tail __user *tail, void *cookie,
531 				stack_trace_consume_fn consume_entry)
532 {
533 	struct compat_frame_tail buftail;
534 	unsigned long err;
535 
536 	/* Also check accessibility of one struct frame_tail beyond */
537 	if (!access_ok(tail, sizeof(buftail)))
538 		return NULL;
539 
540 	pagefault_disable();
541 	err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
542 	pagefault_enable();
543 
544 	if (err)
545 		return NULL;
546 
547 	if (!consume_entry(cookie, buftail.lr))
548 		return NULL;
549 
550 	/*
551 	 * Frame pointers should strictly progress back up the stack
552 	 * (towards higher addresses).
553 	 */
554 	if (tail + 1 >= (struct compat_frame_tail __user *)
555 			compat_ptr(buftail.fp))
556 		return NULL;
557 
558 	return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1;
559 }
560 #endif /* CONFIG_COMPAT */
561 
562 
563 void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
564 					const struct pt_regs *regs)
565 {
566 	if (!consume_entry(cookie, regs->pc))
567 		return;
568 
569 	if (!compat_user_mode(regs)) {
570 		/* AARCH64 mode */
571 		struct frame_tail __user *tail;
572 
573 		tail = (struct frame_tail __user *)regs->regs[29];
574 		while (tail && !((unsigned long)tail & 0x7))
575 			tail = unwind_user_frame(tail, cookie, consume_entry);
576 	} else {
577 #ifdef CONFIG_COMPAT
578 		/* AARCH32 compat mode */
579 		struct compat_frame_tail __user *tail;
580 
581 		tail = (struct compat_frame_tail __user *)regs->compat_fp - 1;
582 		while (tail && !((unsigned long)tail & 0x3))
583 			tail = unwind_compat_user_frame(tail, cookie, consume_entry);
584 #endif
585 	}
586 }
587