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_pointer();
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)
73 sp = current_stack_pointer();
77 save_context_stack(trace, sp, tsk, 0);
79 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
82 save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
84 save_context_stack(trace, regs->gpr[1], current, 0);
86 EXPORT_SYMBOL_GPL(save_stack_trace_regs);
88 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
90 save_stack_trace_tsk_reliable(struct task_struct *tsk,
91 struct stack_trace *trace)
94 unsigned long stack_page = (unsigned long)task_stack_page(tsk);
95 unsigned long stack_end;
99 * The last frame (unwinding first) may not yet have saved
100 * its LR onto the stack.
105 sp = current_stack_pointer();
107 sp = tsk->thread.ksp;
109 stack_end = stack_page + THREAD_SIZE;
110 if (!is_idle_task(tsk)) {
112 * For user tasks, this is the SP value loaded on
113 * kernel entry, see "PACAKSAVE(r13)" in _switch() and
114 * system_call_common()/EXCEPTION_PROLOG_COMMON().
116 * Likewise for non-swapper kernel threads,
117 * this also happens to be the top of the stack
118 * as setup by copy_thread().
120 * Note that stack backlinks are not properly setup by
121 * copy_thread() and thus, a forked task() will have
122 * an unreliable stack trace until it's been
123 * _switch()'ed to for the first time.
125 stack_end -= STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
128 * idle tasks have a custom stack layout,
129 * c.f. cpu_idle_thread_init().
131 stack_end -= STACK_FRAME_OVERHEAD;
134 if (sp < stack_page + sizeof(struct thread_struct) ||
135 sp > stack_end - STACK_FRAME_MIN_SIZE) {
140 unsigned long *stack = (unsigned long *) sp;
141 unsigned long newsp, ip;
143 /* sanity check: ABI requires SP to be aligned 16 bytes. */
147 /* Mark stacktraces with exception frames as unreliable. */
148 if (sp <= stack_end - STACK_INT_FRAME_SIZE &&
149 stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
154 /* Stack grows downwards; unwinder may only go up. */
158 if (newsp != stack_end &&
159 newsp > stack_end - STACK_FRAME_MIN_SIZE) {
160 return 1; /* invalid backlink, too far up. */
163 /* Examine the saved LR: it must point into kernel code. */
164 ip = stack[STACK_FRAME_LR_SAVE];
165 if (!firstframe && !__kernel_text_address(ip))
170 * FIXME: IMHO these tests do not belong in
171 * arch-dependent code, they are generic.
173 ip = ftrace_graph_ret_addr(tsk, &graph_idx, ip, NULL);
174 #ifdef CONFIG_KPROBES
176 * Mark stacktraces with kretprobed functions on them
179 if (ip == (unsigned long)kretprobe_trampoline)
184 trace->entries[trace->nr_entries++] = ip;
188 if (newsp == stack_end)
191 if (trace->nr_entries >= trace->max_entries)
198 EXPORT_SYMBOL_GPL(save_stack_trace_tsk_reliable);
199 #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
201 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI)
202 static void handle_backtrace_ipi(struct pt_regs *regs)
204 nmi_cpu_backtrace(regs);
207 static void raise_backtrace_ipi(cpumask_t *mask)
209 struct paca_struct *p;
213 for_each_cpu(cpu, mask) {
214 if (cpu == smp_processor_id()) {
215 handle_backtrace_ipi(NULL);
219 delay_us = 5 * USEC_PER_SEC;
221 if (smp_send_safe_nmi_ipi(cpu, handle_backtrace_ipi, delay_us)) {
222 // Now wait up to 5s for the other CPU to do its backtrace
223 while (cpumask_test_cpu(cpu, mask) && delay_us) {
228 // Other CPU cleared itself from the mask
235 cpumask_clear_cpu(cpu, mask);
237 pr_warn("CPU %d didn't respond to backtrace IPI, inspecting paca.\n", cpu);
238 if (!virt_addr_valid(p)) {
239 pr_warn("paca pointer appears corrupt? (%px)\n", p);
243 pr_warn("irq_soft_mask: 0x%02x in_mce: %d in_nmi: %d",
244 p->irq_soft_mask, p->in_mce, p->in_nmi);
246 if (virt_addr_valid(p->__current))
247 pr_cont(" current: %d (%s)\n", p->__current->pid,
250 pr_cont(" current pointer corrupt? (%px)\n", p->__current);
252 pr_warn("Back trace of paca->saved_r1 (0x%016llx) (possibly stale):\n", p->saved_r1);
253 show_stack(p->__current, (unsigned long *)p->saved_r1);
257 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
259 nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi);
261 #endif /* defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI) */