1 // SPDX-License-Identifier: GPL-2.0
4 * Stack trace utility functions etc.
6 * Copyright 2008 Christoph Hellwig, IBM Corp.
7 * Copyright 2018 SUSE Linux GmbH
8 * Copyright 2018 Nick Piggin, Michael Ellerman, IBM Corp.
11 #include <linux/delay.h>
12 #include <linux/export.h>
13 #include <linux/kallsyms.h>
14 #include <linux/module.h>
15 #include <linux/nmi.h>
16 #include <linux/sched.h>
17 #include <linux/sched/debug.h>
18 #include <linux/sched/task_stack.h>
19 #include <linux/stacktrace.h>
20 #include <asm/ptrace.h>
21 #include <asm/processor.h>
22 #include <linux/ftrace.h>
23 #include <linux/delay.h>
24 #include <asm/kprobes.h>
29 * Save stack-backtrace addresses into a stack_trace buffer.
31 static void save_context_stack(struct stack_trace *trace, unsigned long sp,
32 struct task_struct *tsk, int savesched)
35 unsigned long *stack = (unsigned long *) sp;
36 unsigned long newsp, ip;
38 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
42 ip = stack[STACK_FRAME_LR_SAVE];
44 if (savesched || !in_sched_functions(ip)) {
46 trace->entries[trace->nr_entries++] = ip;
51 if (trace->nr_entries >= trace->max_entries)
58 void save_stack_trace(struct stack_trace *trace)
62 sp = current_stack_frame();
64 save_context_stack(trace, sp, current, 1);
66 EXPORT_SYMBOL_GPL(save_stack_trace);
68 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
72 if (!try_get_task_stack(tsk))
76 sp = current_stack_frame();
80 save_context_stack(trace, sp, tsk, 0);
84 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
87 save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
89 save_context_stack(trace, regs->gpr[1], current, 0);
91 EXPORT_SYMBOL_GPL(save_stack_trace_regs);
93 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
95 * This function returns an error if it detects any unreliable features of the
96 * stack. Otherwise it guarantees that the stack trace is reliable.
98 * If the task is not 'current', the caller *must* ensure the task is inactive.
100 static int __save_stack_trace_tsk_reliable(struct task_struct *tsk,
101 struct stack_trace *trace)
105 unsigned long stack_page = (unsigned long)task_stack_page(tsk);
106 unsigned long stack_end;
110 stack_end = stack_page + THREAD_SIZE;
111 if (!is_idle_task(tsk)) {
113 * For user tasks, this is the SP value loaded on
114 * kernel entry, see "PACAKSAVE(r13)" in _switch() and
115 * system_call_common()/EXCEPTION_PROLOG_COMMON().
117 * Likewise for non-swapper kernel threads,
118 * this also happens to be the top of the stack
119 * as setup by copy_thread().
121 * Note that stack backlinks are not properly setup by
122 * copy_thread() and thus, a forked task() will have
123 * an unreliable stack trace until it's been
124 * _switch()'ed to for the first time.
126 stack_end -= STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
129 * idle tasks have a custom stack layout,
130 * c.f. cpu_idle_thread_init().
132 stack_end -= STACK_FRAME_OVERHEAD;
136 sp = current_stack_frame();
138 sp = tsk->thread.ksp;
140 if (sp < stack_page + sizeof(struct thread_struct) ||
141 sp > stack_end - STACK_FRAME_MIN_SIZE) {
145 for (firstframe = true; sp != stack_end;
146 firstframe = false, sp = newsp) {
147 unsigned long *stack = (unsigned long *) sp;
150 /* sanity check: ABI requires SP to be aligned 16 bytes. */
155 /* Stack grows downwards; unwinder may only go up. */
159 if (newsp != stack_end &&
160 newsp > stack_end - STACK_FRAME_MIN_SIZE) {
161 return -EINVAL; /* invalid backlink, too far up. */
165 * We can only trust the bottom frame's backlink, the
166 * rest of the frame may be uninitialized, continue to
172 /* Mark stacktraces with exception frames as unreliable. */
173 if (sp <= stack_end - STACK_INT_FRAME_SIZE &&
174 stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
178 /* Examine the saved LR: it must point into kernel code. */
179 ip = stack[STACK_FRAME_LR_SAVE];
180 if (!__kernel_text_address(ip))
184 * FIXME: IMHO these tests do not belong in
185 * arch-dependent code, they are generic.
187 ip = ftrace_graph_ret_addr(tsk, &graph_idx, ip, stack);
188 #ifdef CONFIG_KPROBES
190 * Mark stacktraces with kretprobed functions on them
193 if (ip == (unsigned long)kretprobe_trampoline)
197 if (trace->nr_entries >= trace->max_entries)
200 trace->entries[trace->nr_entries++] = ip;
207 int save_stack_trace_tsk_reliable(struct task_struct *tsk,
208 struct stack_trace *trace)
213 * If the task doesn't have a stack (e.g., a zombie), the stack is
216 if (!try_get_task_stack(tsk))
219 ret = __save_stack_trace_tsk_reliable(tsk, trace);
225 #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
227 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI)
228 static void handle_backtrace_ipi(struct pt_regs *regs)
230 nmi_cpu_backtrace(regs);
233 static void raise_backtrace_ipi(cpumask_t *mask)
235 struct paca_struct *p;
239 for_each_cpu(cpu, mask) {
240 if (cpu == smp_processor_id()) {
241 handle_backtrace_ipi(NULL);
245 delay_us = 5 * USEC_PER_SEC;
247 if (smp_send_safe_nmi_ipi(cpu, handle_backtrace_ipi, delay_us)) {
248 // Now wait up to 5s for the other CPU to do its backtrace
249 while (cpumask_test_cpu(cpu, mask) && delay_us) {
254 // Other CPU cleared itself from the mask
261 cpumask_clear_cpu(cpu, mask);
263 pr_warn("CPU %d didn't respond to backtrace IPI, inspecting paca.\n", cpu);
264 if (!virt_addr_valid(p)) {
265 pr_warn("paca pointer appears corrupt? (%px)\n", p);
269 pr_warn("irq_soft_mask: 0x%02x in_mce: %d in_nmi: %d",
270 p->irq_soft_mask, p->in_mce, p->in_nmi);
272 if (virt_addr_valid(p->__current))
273 pr_cont(" current: %d (%s)\n", p->__current->pid,
276 pr_cont(" current pointer corrupt? (%px)\n", p->__current);
278 pr_warn("Back trace of paca->saved_r1 (0x%016llx) (possibly stale):\n", p->saved_r1);
279 show_stack(p->__current, (unsigned long *)p->saved_r1, KERN_WARNING);
283 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
285 nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi);
287 #endif /* defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI) */