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_pointer(); 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 (tsk == current) 71 sp = current_stack_pointer(); 72 else 73 sp = tsk->thread.ksp; 74 75 save_context_stack(trace, sp, tsk, 0); 76 } 77 EXPORT_SYMBOL_GPL(save_stack_trace_tsk); 78 79 void 80 save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) 81 { 82 save_context_stack(trace, regs->gpr[1], current, 0); 83 } 84 EXPORT_SYMBOL_GPL(save_stack_trace_regs); 85 86 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE 87 int 88 save_stack_trace_tsk_reliable(struct task_struct *tsk, 89 struct stack_trace *trace) 90 { 91 unsigned long sp; 92 unsigned long stack_page = (unsigned long)task_stack_page(tsk); 93 unsigned long stack_end; 94 int graph_idx = 0; 95 96 /* 97 * The last frame (unwinding first) may not yet have saved 98 * its LR onto the stack. 99 */ 100 int firstframe = 1; 101 102 if (tsk == current) 103 sp = current_stack_pointer(); 104 else 105 sp = tsk->thread.ksp; 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 (sp < stack_page + sizeof(struct thread_struct) || 133 sp > stack_end - STACK_FRAME_MIN_SIZE) { 134 return 1; 135 } 136 137 for (;;) { 138 unsigned long *stack = (unsigned long *) sp; 139 unsigned long newsp, ip; 140 141 /* sanity check: ABI requires SP to be aligned 16 bytes. */ 142 if (sp & 0xF) 143 return 1; 144 145 /* Mark stacktraces with exception frames as unreliable. */ 146 if (sp <= stack_end - STACK_INT_FRAME_SIZE && 147 stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) { 148 return 1; 149 } 150 151 newsp = stack[0]; 152 /* Stack grows downwards; unwinder may only go up. */ 153 if (newsp <= sp) 154 return 1; 155 156 if (newsp != stack_end && 157 newsp > stack_end - STACK_FRAME_MIN_SIZE) { 158 return 1; /* invalid backlink, too far up. */ 159 } 160 161 /* Examine the saved LR: it must point into kernel code. */ 162 ip = stack[STACK_FRAME_LR_SAVE]; 163 if (!firstframe && !__kernel_text_address(ip)) 164 return 1; 165 firstframe = 0; 166 167 /* 168 * FIXME: IMHO these tests do not belong in 169 * arch-dependent code, they are generic. 170 */ 171 ip = ftrace_graph_ret_addr(tsk, &graph_idx, ip, NULL); 172 #ifdef CONFIG_KPROBES 173 /* 174 * Mark stacktraces with kretprobed functions on them 175 * as unreliable. 176 */ 177 if (ip == (unsigned long)kretprobe_trampoline) 178 return 1; 179 #endif 180 181 if (!trace->skip) 182 trace->entries[trace->nr_entries++] = ip; 183 else 184 trace->skip--; 185 186 if (newsp == stack_end) 187 break; 188 189 if (trace->nr_entries >= trace->max_entries) 190 return -E2BIG; 191 192 sp = newsp; 193 } 194 return 0; 195 } 196 EXPORT_SYMBOL_GPL(save_stack_trace_tsk_reliable); 197 #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */ 198 199 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI) 200 static void handle_backtrace_ipi(struct pt_regs *regs) 201 { 202 nmi_cpu_backtrace(regs); 203 } 204 205 static void raise_backtrace_ipi(cpumask_t *mask) 206 { 207 unsigned int cpu; 208 209 for_each_cpu(cpu, mask) { 210 if (cpu == smp_processor_id()) 211 handle_backtrace_ipi(NULL); 212 else 213 smp_send_safe_nmi_ipi(cpu, handle_backtrace_ipi, 5 * USEC_PER_SEC); 214 } 215 216 for_each_cpu(cpu, mask) { 217 struct paca_struct *p = paca_ptrs[cpu]; 218 219 cpumask_clear_cpu(cpu, mask); 220 221 pr_warn("CPU %d didn't respond to backtrace IPI, inspecting paca.\n", cpu); 222 if (!virt_addr_valid(p)) { 223 pr_warn("paca pointer appears corrupt? (%px)\n", p); 224 continue; 225 } 226 227 pr_warn("irq_soft_mask: 0x%02x in_mce: %d in_nmi: %d", 228 p->irq_soft_mask, p->in_mce, p->in_nmi); 229 230 if (virt_addr_valid(p->__current)) 231 pr_cont(" current: %d (%s)\n", p->__current->pid, 232 p->__current->comm); 233 else 234 pr_cont(" current pointer corrupt? (%px)\n", p->__current); 235 236 pr_warn("Back trace of paca->saved_r1 (0x%016llx) (possibly stale):\n", p->saved_r1); 237 show_stack(p->__current, (unsigned long *)p->saved_r1); 238 } 239 } 240 241 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self) 242 { 243 nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi); 244 } 245 #endif /* defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI) */ 246