GNU Linux-libre 4.19.245-gnu1
[releases.git] / arch / powerpc / kernel / stacktrace.c
1 // 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
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>
25
26 #include <asm/paca.h>
27
28 /*
29  * Save stack-backtrace addresses into a stack_trace buffer.
30  */
31 static 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
58 void save_stack_trace(struct stack_trace *trace)
59 {
60         unsigned long sp;
61
62         sp = current_stack_pointer();
63
64         save_context_stack(trace, sp, current, 1);
65 }
66 EXPORT_SYMBOL_GPL(save_stack_trace);
67
68 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
69 {
70         unsigned long sp;
71
72         if (tsk == current)
73                 sp = current_stack_pointer();
74         else
75                 sp = tsk->thread.ksp;
76
77         save_context_stack(trace, sp, tsk, 0);
78 }
79 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
80
81 void
82 save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
83 {
84         save_context_stack(trace, regs->gpr[1], current, 0);
85 }
86 EXPORT_SYMBOL_GPL(save_stack_trace_regs);
87
88 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
89 int
90 save_stack_trace_tsk_reliable(struct task_struct *tsk,
91                                 struct stack_trace *trace)
92 {
93         unsigned long sp;
94         unsigned long stack_page = (unsigned long)task_stack_page(tsk);
95         unsigned long stack_end;
96         int graph_idx = 0;
97
98         /*
99          * The last frame (unwinding first) may not yet have saved
100          * its LR onto the stack.
101          */
102         int firstframe = 1;
103
104         if (tsk == current)
105                 sp = current_stack_pointer();
106         else
107                 sp = tsk->thread.ksp;
108
109         stack_end = stack_page + THREAD_SIZE;
110         if (!is_idle_task(tsk)) {
111                 /*
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().
115                  *
116                  * Likewise for non-swapper kernel threads,
117                  * this also happens to be the top of the stack
118                  * as setup by copy_thread().
119                  *
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.
124                  */
125                 stack_end -= STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
126         } else {
127                 /*
128                  * idle tasks have a custom stack layout,
129                  * c.f. cpu_idle_thread_init().
130                  */
131                 stack_end -= STACK_FRAME_OVERHEAD;
132         }
133
134         if (sp < stack_page + sizeof(struct thread_struct) ||
135             sp > stack_end - STACK_FRAME_MIN_SIZE) {
136                 return 1;
137         }
138
139         for (;;) {
140                 unsigned long *stack = (unsigned long *) sp;
141                 unsigned long newsp, ip;
142
143                 /* sanity check: ABI requires SP to be aligned 16 bytes. */
144                 if (sp & 0xF)
145                         return 1;
146
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) {
150                         return 1;
151                 }
152
153                 newsp = stack[0];
154                 /* Stack grows downwards; unwinder may only go up. */
155                 if (newsp <= sp)
156                         return 1;
157
158                 if (newsp != stack_end &&
159                     newsp > stack_end - STACK_FRAME_MIN_SIZE) {
160                         return 1; /* invalid backlink, too far up. */
161                 }
162
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))
166                         return 1;
167                 firstframe = 0;
168
169                 /*
170                  * FIXME: IMHO these tests do not belong in
171                  * arch-dependent code, they are generic.
172                  */
173                 ip = ftrace_graph_ret_addr(tsk, &graph_idx, ip, NULL);
174 #ifdef CONFIG_KPROBES
175                 /*
176                  * Mark stacktraces with kretprobed functions on them
177                  * as unreliable.
178                  */
179                 if (ip == (unsigned long)kretprobe_trampoline)
180                         return 1;
181 #endif
182
183                 if (!trace->skip)
184                         trace->entries[trace->nr_entries++] = ip;
185                 else
186                         trace->skip--;
187
188                 if (newsp == stack_end)
189                         break;
190
191                 if (trace->nr_entries >= trace->max_entries)
192                         return -E2BIG;
193
194                 sp = newsp;
195         }
196         return 0;
197 }
198 EXPORT_SYMBOL_GPL(save_stack_trace_tsk_reliable);
199 #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
200
201 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI)
202 static void handle_backtrace_ipi(struct pt_regs *regs)
203 {
204         nmi_cpu_backtrace(regs);
205 }
206
207 static void raise_backtrace_ipi(cpumask_t *mask)
208 {
209         struct paca_struct *p;
210         unsigned int cpu;
211         u64 delay_us;
212
213         for_each_cpu(cpu, mask) {
214                 if (cpu == smp_processor_id()) {
215                         handle_backtrace_ipi(NULL);
216                         continue;
217                 }
218
219                 delay_us = 5 * USEC_PER_SEC;
220
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) {
224                                 udelay(1);
225                                 delay_us--;
226                         }
227
228                         // Other CPU cleared itself from the mask
229                         if (delay_us)
230                                 continue;
231                 }
232
233                 p = paca_ptrs[cpu];
234
235                 cpumask_clear_cpu(cpu, mask);
236
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);
240                         continue;
241                 }
242
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);
245
246                 if (virt_addr_valid(p->__current))
247                         pr_cont(" current: %d (%s)\n", p->__current->pid,
248                                 p->__current->comm);
249                 else
250                         pr_cont(" current pointer corrupt? (%px)\n", p->__current);
251
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);
254         }
255 }
256
257 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
258 {
259         nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi);
260 }
261 #endif /* defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI) */