blob: d4a66ce93f52280531a6e059d4a7ea3d7762ade6 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2
3/*
4 * Stack trace utility functions etc.
5 *
6 * Copyright 2008 Christoph Hellwig, IBM Corp.
7 * Copyright 2018 SUSE Linux GmbH
8 * Copyright 2018 Nick Piggin, Michael Ellerman, IBM Corp.
9 */
10
Olivier Deprez0e641232021-09-23 10:07:05 +020011#include <linux/delay.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#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>
Olivier Deprez0e641232021-09-23 10:07:05 +020023#include <linux/delay.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024#include <asm/kprobes.h>
25
26#include <asm/paca.h>
27
28/*
29 * Save stack-backtrace addresses into a stack_trace buffer.
30 */
31static void save_context_stack(struct stack_trace *trace, unsigned long sp,
32 struct task_struct *tsk, int savesched)
33{
34 for (;;) {
35 unsigned long *stack = (unsigned long *) sp;
36 unsigned long newsp, ip;
37
38 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
39 return;
40
41 newsp = stack[0];
42 ip = stack[STACK_FRAME_LR_SAVE];
43
44 if (savesched || !in_sched_functions(ip)) {
45 if (!trace->skip)
46 trace->entries[trace->nr_entries++] = ip;
47 else
48 trace->skip--;
49 }
50
51 if (trace->nr_entries >= trace->max_entries)
52 return;
53
54 sp = newsp;
55 }
56}
57
58void save_stack_trace(struct stack_trace *trace)
59{
60 unsigned long sp;
61
Olivier Deprez157378f2022-04-04 15:47:50 +020062 sp = current_stack_frame();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063
64 save_context_stack(trace, sp, current, 1);
65}
66EXPORT_SYMBOL_GPL(save_stack_trace);
67
68void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
69{
70 unsigned long sp;
71
David Brazdil0f672f62019-12-10 10:32:29 +000072 if (!try_get_task_stack(tsk))
73 return;
74
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075 if (tsk == current)
Olivier Deprez157378f2022-04-04 15:47:50 +020076 sp = current_stack_frame();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077 else
78 sp = tsk->thread.ksp;
79
80 save_context_stack(trace, sp, tsk, 0);
David Brazdil0f672f62019-12-10 10:32:29 +000081
82 put_task_stack(tsk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083}
84EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
85
86void
87save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
88{
89 save_context_stack(trace, regs->gpr[1], current, 0);
90}
91EXPORT_SYMBOL_GPL(save_stack_trace_regs);
92
93#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
David Brazdil0f672f62019-12-10 10:32:29 +000094/*
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.
97 *
98 * If the task is not 'current', the caller *must* ensure the task is inactive.
99 */
100static int __save_stack_trace_tsk_reliable(struct task_struct *tsk,
101 struct stack_trace *trace)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000102{
103 unsigned long sp;
David Brazdil0f672f62019-12-10 10:32:29 +0000104 unsigned long newsp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105 unsigned long stack_page = (unsigned long)task_stack_page(tsk);
106 unsigned long stack_end;
107 int graph_idx = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000108 bool firstframe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109
110 stack_end = stack_page + THREAD_SIZE;
111 if (!is_idle_task(tsk)) {
112 /*
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().
116 *
117 * Likewise for non-swapper kernel threads,
118 * this also happens to be the top of the stack
119 * as setup by copy_thread().
120 *
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.
125 */
126 stack_end -= STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
127 } else {
128 /*
129 * idle tasks have a custom stack layout,
130 * c.f. cpu_idle_thread_init().
131 */
132 stack_end -= STACK_FRAME_OVERHEAD;
133 }
134
David Brazdil0f672f62019-12-10 10:32:29 +0000135 if (tsk == current)
Olivier Deprez157378f2022-04-04 15:47:50 +0200136 sp = current_stack_frame();
David Brazdil0f672f62019-12-10 10:32:29 +0000137 else
138 sp = tsk->thread.ksp;
139
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000140 if (sp < stack_page + sizeof(struct thread_struct) ||
141 sp > stack_end - STACK_FRAME_MIN_SIZE) {
David Brazdil0f672f62019-12-10 10:32:29 +0000142 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143 }
144
David Brazdil0f672f62019-12-10 10:32:29 +0000145 for (firstframe = true; sp != stack_end;
146 firstframe = false, sp = newsp) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147 unsigned long *stack = (unsigned long *) sp;
David Brazdil0f672f62019-12-10 10:32:29 +0000148 unsigned long ip;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000149
150 /* sanity check: ABI requires SP to be aligned 16 bytes. */
151 if (sp & 0xF)
David Brazdil0f672f62019-12-10 10:32:29 +0000152 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153
154 newsp = stack[0];
155 /* Stack grows downwards; unwinder may only go up. */
156 if (newsp <= sp)
David Brazdil0f672f62019-12-10 10:32:29 +0000157 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000158
159 if (newsp != stack_end &&
160 newsp > stack_end - STACK_FRAME_MIN_SIZE) {
David Brazdil0f672f62019-12-10 10:32:29 +0000161 return -EINVAL; /* invalid backlink, too far up. */
162 }
163
164 /*
165 * We can only trust the bottom frame's backlink, the
166 * rest of the frame may be uninitialized, continue to
167 * the next.
168 */
169 if (firstframe)
170 continue;
171
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) {
175 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000176 }
177
178 /* Examine the saved LR: it must point into kernel code. */
179 ip = stack[STACK_FRAME_LR_SAVE];
David Brazdil0f672f62019-12-10 10:32:29 +0000180 if (!__kernel_text_address(ip))
181 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000182
183 /*
184 * FIXME: IMHO these tests do not belong in
185 * arch-dependent code, they are generic.
186 */
David Brazdil0f672f62019-12-10 10:32:29 +0000187 ip = ftrace_graph_ret_addr(tsk, &graph_idx, ip, stack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000188#ifdef CONFIG_KPROBES
189 /*
190 * Mark stacktraces with kretprobed functions on them
191 * as unreliable.
192 */
193 if (ip == (unsigned long)kretprobe_trampoline)
David Brazdil0f672f62019-12-10 10:32:29 +0000194 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195#endif
196
David Brazdil0f672f62019-12-10 10:32:29 +0000197 if (trace->nr_entries >= trace->max_entries)
198 return -E2BIG;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000199 if (!trace->skip)
200 trace->entries[trace->nr_entries++] = ip;
201 else
202 trace->skip--;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000203 }
204 return 0;
205}
David Brazdil0f672f62019-12-10 10:32:29 +0000206
207int save_stack_trace_tsk_reliable(struct task_struct *tsk,
208 struct stack_trace *trace)
209{
210 int ret;
211
212 /*
213 * If the task doesn't have a stack (e.g., a zombie), the stack is
214 * "reliably" empty.
215 */
216 if (!try_get_task_stack(tsk))
217 return 0;
218
219 ret = __save_stack_trace_tsk_reliable(tsk, trace);
220
221 put_task_stack(tsk);
222
223 return ret;
224}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000225#endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
226
227#if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI)
228static void handle_backtrace_ipi(struct pt_regs *regs)
229{
230 nmi_cpu_backtrace(regs);
231}
232
233static void raise_backtrace_ipi(cpumask_t *mask)
234{
Olivier Deprez0e641232021-09-23 10:07:05 +0200235 struct paca_struct *p;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000236 unsigned int cpu;
Olivier Deprez0e641232021-09-23 10:07:05 +0200237 u64 delay_us;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000238
239 for_each_cpu(cpu, mask) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200240 if (cpu == smp_processor_id()) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000241 handle_backtrace_ipi(NULL);
Olivier Deprez0e641232021-09-23 10:07:05 +0200242 continue;
243 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000244
Olivier Deprez0e641232021-09-23 10:07:05 +0200245 delay_us = 5 * USEC_PER_SEC;
246
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) {
250 udelay(1);
251 delay_us--;
252 }
253
254 // Other CPU cleared itself from the mask
255 if (delay_us)
256 continue;
257 }
258
259 p = paca_ptrs[cpu];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000260
261 cpumask_clear_cpu(cpu, mask);
262
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);
266 continue;
267 }
268
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);
271
272 if (virt_addr_valid(p->__current))
273 pr_cont(" current: %d (%s)\n", p->__current->pid,
274 p->__current->comm);
275 else
276 pr_cont(" current pointer corrupt? (%px)\n", p->__current);
277
278 pr_warn("Back trace of paca->saved_r1 (0x%016llx) (possibly stale):\n", p->saved_r1);
Olivier Deprez157378f2022-04-04 15:47:50 +0200279 show_stack(p->__current, (unsigned long *)p->saved_r1, KERN_WARNING);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000280 }
281}
282
283void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
284{
285 nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi);
286}
287#endif /* defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI) */