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/export.h> 12 #include <linux/kallsyms.h> 13 #include <linux/module.h> 14 #include <linux/nmi.h> 15 #include <linux/sched.h> 16 #include <linux/sched/debug.h> 17 #include <linux/sched/task_stack.h> 18 #include <linux/stacktrace.h> 19 #include <asm/ptrace.h> 20 #include <asm/processor.h> 21 #include <linux/ftrace.h> 22 #include <asm/kprobes.h> 23 24 #include <asm/paca.h> 25 26 /* 27 * Save stack-backtrace addresses into a stack_trace buffer. 28 */ 29 static void save_context_stack(struct stack_trace *trace, unsigned long sp, 30 struct task_struct *tsk, int savesched) 31 { 32 for (;;) { 33 unsigned long *stack = (unsigned long *) sp; 34 unsigned long newsp, ip; 35 36 if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD)) 37 return; 38 39 newsp = stack[0]; 40 ip = stack[STACK_FRAME_LR_SAVE]; 41 42 if (savesched || !in_sched_functions(ip)) { 43 if (!trace->skip) 44 trace->entries[trace->nr_entries++] = ip; 45 else 46 trace->skip--; 47 } 48 49 if (trace->nr_entries >= trace->max_entries) 50 return; 51 52 sp = newsp; 53 } 54 } 55 56 void save_stack_trace(struct stack_trace *trace) 57 { 58 unsigned long sp; 59 60 sp = current_stack_frame(); 61 62 save_context_stack(trace, sp, current, 1); 63 } 64 EXPORT_SYMBOL_GPL(save_stack_trace); 65 66 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) 67 { 68 unsigned long sp; 69 70 if (!try_get_task_stack(tsk)) 71 return; 72 73 if (tsk == current) 74 sp = current_stack_frame(); 75 else 76 sp = tsk->thread.ksp; 77 78 save_context_stack(trace, sp, tsk, 0); 79 80 put_task_stack(tsk); 81 } 82 EXPORT_SYMBOL_GPL(save_stack_trace_tsk); 83 84 void 85 save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) 86 { 87 save_context_stack(trace, regs->gpr[1], current, 0); 88 } 89 EXPORT_SYMBOL_GPL(save_stack_trace_regs); 90 91 /* 92 * This function returns an error if it detects any unreliable features of the 93 * stack. Otherwise it guarantees that the stack trace is reliable. 94 * 95 * If the task is not 'current', the caller *must* ensure the task is inactive. 96 */ 97 static int __save_stack_trace_tsk_reliable(struct task_struct *tsk, 98 struct stack_trace *trace) 99 { 100 unsigned long sp; 101 unsigned long newsp; 102 unsigned long stack_page = (unsigned long)task_stack_page(tsk); 103 unsigned long stack_end; 104 int graph_idx = 0; 105 bool firstframe; 106 107 stack_end = stack_page + THREAD_SIZE; 108 if (!is_idle_task(tsk)) { 109 /* 110 * For user tasks, this is the SP value loaded on 111 * kernel entry, see "PACAKSAVE(r13)" in _switch() and 112 * system_call_common()/EXCEPTION_PROLOG_COMMON(). 113 * 114 * Likewise for non-swapper kernel threads, 115 * this also happens to be the top of the stack 116 * as setup by copy_thread(). 117 * 118 * Note that stack backlinks are not properly setup by 119 * copy_thread() and thus, a forked task() will have 120 * an unreliable stack trace until it's been 121 * _switch()'ed to for the first time. 122 */ 123 stack_end -= STACK_FRAME_OVERHEAD + sizeof(struct pt_regs); 124 } else { 125 /* 126 * idle tasks have a custom stack layout, 127 * c.f. cpu_idle_thread_init(). 128 */ 129 stack_end -= STACK_FRAME_OVERHEAD; 130 } 131 132 if (tsk == current) 133 sp = current_stack_frame(); 134 else 135 sp = tsk->thread.ksp; 136 137 if (sp < stack_page + sizeof(struct thread_struct) || 138 sp > stack_end - STACK_FRAME_MIN_SIZE) { 139 return -EINVAL; 140 } 141 142 for (firstframe = true; sp != stack_end; 143 firstframe = false, sp = newsp) { 144 unsigned long *stack = (unsigned long *) sp; 145 unsigned long ip; 146 147 /* sanity check: ABI requires SP to be aligned 16 bytes. */ 148 if (sp & 0xF) 149 return -EINVAL; 150 151 newsp = stack[0]; 152 /* Stack grows downwards; unwinder may only go up. */ 153 if (newsp <= sp) 154 return -EINVAL; 155 156 if (newsp != stack_end && 157 newsp > stack_end - STACK_FRAME_MIN_SIZE) { 158 return -EINVAL; /* invalid backlink, too far up. */ 159 } 160 161 /* 162 * We can only trust the bottom frame's backlink, the 163 * rest of the frame may be uninitialized, continue to 164 * the next. 165 */ 166 if (firstframe) 167 continue; 168 169 /* Mark stacktraces with exception frames as unreliable. */ 170 if (sp <= stack_end - STACK_INT_FRAME_SIZE && 171 stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) { 172 return -EINVAL; 173 } 174 175 /* Examine the saved LR: it must point into kernel code. */ 176 ip = stack[STACK_FRAME_LR_SAVE]; 177 if (!__kernel_text_address(ip)) 178 return -EINVAL; 179 180 /* 181 * FIXME: IMHO these tests do not belong in 182 * arch-dependent code, they are generic. 183 */ 184 ip = ftrace_graph_ret_addr(tsk, &graph_idx, ip, stack); 185 #ifdef CONFIG_KPROBES 186 /* 187 * Mark stacktraces with kretprobed functions on them 188 * as unreliable. 189 */ 190 if (ip == (unsigned long)kretprobe_trampoline) 191 return -EINVAL; 192 #endif 193 194 if (trace->nr_entries >= trace->max_entries) 195 return -E2BIG; 196 if (!trace->skip) 197 trace->entries[trace->nr_entries++] = ip; 198 else 199 trace->skip--; 200 } 201 return 0; 202 } 203 204 int save_stack_trace_tsk_reliable(struct task_struct *tsk, 205 struct stack_trace *trace) 206 { 207 int ret; 208 209 /* 210 * If the task doesn't have a stack (e.g., a zombie), the stack is 211 * "reliably" empty. 212 */ 213 if (!try_get_task_stack(tsk)) 214 return 0; 215 216 ret = __save_stack_trace_tsk_reliable(tsk, trace); 217 218 put_task_stack(tsk); 219 220 return ret; 221 } 222 223 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI) 224 static void handle_backtrace_ipi(struct pt_regs *regs) 225 { 226 nmi_cpu_backtrace(regs); 227 } 228 229 static void raise_backtrace_ipi(cpumask_t *mask) 230 { 231 unsigned int cpu; 232 233 for_each_cpu(cpu, mask) { 234 if (cpu == smp_processor_id()) 235 handle_backtrace_ipi(NULL); 236 else 237 smp_send_safe_nmi_ipi(cpu, handle_backtrace_ipi, 5 * USEC_PER_SEC); 238 } 239 240 for_each_cpu(cpu, mask) { 241 struct paca_struct *p = paca_ptrs[cpu]; 242 243 cpumask_clear_cpu(cpu, mask); 244 245 pr_warn("CPU %d didn't respond to backtrace IPI, inspecting paca.\n", cpu); 246 if (!virt_addr_valid(p)) { 247 pr_warn("paca pointer appears corrupt? (%px)\n", p); 248 continue; 249 } 250 251 pr_warn("irq_soft_mask: 0x%02x in_mce: %d in_nmi: %d", 252 p->irq_soft_mask, p->in_mce, p->in_nmi); 253 254 if (virt_addr_valid(p->__current)) 255 pr_cont(" current: %d (%s)\n", p->__current->pid, 256 p->__current->comm); 257 else 258 pr_cont(" current pointer corrupt? (%px)\n", p->__current); 259 260 pr_warn("Back trace of paca->saved_r1 (0x%016llx) (possibly stale):\n", p->saved_r1); 261 show_stack(p->__current, (unsigned long *)p->saved_r1, KERN_WARNING); 262 } 263 } 264 265 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self) 266 { 267 nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi); 268 } 269 #endif /* defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI) */ 270