stacktrace.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Stack trace utility functions etc.
  4. *
  5. * Copyright 2008 Christoph Hellwig, IBM Corp.
  6. * Copyright 2018 SUSE Linux GmbH
  7. * Copyright 2018 Nick Piggin, Michael Ellerman, IBM Corp.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/export.h>
  11. #include <linux/kallsyms.h>
  12. #include <linux/module.h>
  13. #include <linux/nmi.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/debug.h>
  16. #include <linux/sched/task_stack.h>
  17. #include <linux/stacktrace.h>
  18. #include <asm/ptrace.h>
  19. #include <asm/processor.h>
  20. #include <linux/ftrace.h>
  21. #include <asm/kprobes.h>
  22. #include <asm/paca.h>
  23. void __no_sanitize_address arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
  24. struct task_struct *task, struct pt_regs *regs)
  25. {
  26. unsigned long sp;
  27. if (regs && !consume_entry(cookie, regs->nip))
  28. return;
  29. if (regs)
  30. sp = regs->gpr[1];
  31. else if (task == current)
  32. sp = current_stack_frame();
  33. else
  34. sp = task->thread.ksp;
  35. for (;;) {
  36. unsigned long *stack = (unsigned long *) sp;
  37. unsigned long newsp, ip;
  38. if (!validate_sp(sp, task, STACK_FRAME_OVERHEAD))
  39. return;
  40. newsp = stack[0];
  41. ip = stack[STACK_FRAME_LR_SAVE];
  42. if (!consume_entry(cookie, ip))
  43. return;
  44. sp = newsp;
  45. }
  46. }
  47. /*
  48. * This function returns an error if it detects any unreliable features of the
  49. * stack. Otherwise it guarantees that the stack trace is reliable.
  50. *
  51. * If the task is not 'current', the caller *must* ensure the task is inactive.
  52. */
  53. int __no_sanitize_address arch_stack_walk_reliable(stack_trace_consume_fn consume_entry,
  54. void *cookie, struct task_struct *task)
  55. {
  56. unsigned long sp;
  57. unsigned long newsp;
  58. unsigned long stack_page = (unsigned long)task_stack_page(task);
  59. unsigned long stack_end;
  60. int graph_idx = 0;
  61. bool firstframe;
  62. stack_end = stack_page + THREAD_SIZE;
  63. if (!is_idle_task(task)) {
  64. /*
  65. * For user tasks, this is the SP value loaded on
  66. * kernel entry, see "PACAKSAVE(r13)" in _switch() and
  67. * system_call_common()/EXCEPTION_PROLOG_COMMON().
  68. *
  69. * Likewise for non-swapper kernel threads,
  70. * this also happens to be the top of the stack
  71. * as setup by copy_thread().
  72. *
  73. * Note that stack backlinks are not properly setup by
  74. * copy_thread() and thus, a forked task() will have
  75. * an unreliable stack trace until it's been
  76. * _switch()'ed to for the first time.
  77. */
  78. stack_end -= STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  79. } else {
  80. /*
  81. * idle tasks have a custom stack layout,
  82. * c.f. cpu_idle_thread_init().
  83. */
  84. stack_end -= STACK_FRAME_OVERHEAD;
  85. }
  86. if (task == current)
  87. sp = current_stack_frame();
  88. else
  89. sp = task->thread.ksp;
  90. if (sp < stack_page + sizeof(struct thread_struct) ||
  91. sp > stack_end - STACK_FRAME_MIN_SIZE) {
  92. return -EINVAL;
  93. }
  94. for (firstframe = true; sp != stack_end;
  95. firstframe = false, sp = newsp) {
  96. unsigned long *stack = (unsigned long *) sp;
  97. unsigned long ip;
  98. /* sanity check: ABI requires SP to be aligned 16 bytes. */
  99. if (sp & 0xF)
  100. return -EINVAL;
  101. newsp = stack[0];
  102. /* Stack grows downwards; unwinder may only go up. */
  103. if (newsp <= sp)
  104. return -EINVAL;
  105. if (newsp != stack_end &&
  106. newsp > stack_end - STACK_FRAME_MIN_SIZE) {
  107. return -EINVAL; /* invalid backlink, too far up. */
  108. }
  109. /*
  110. * We can only trust the bottom frame's backlink, the
  111. * rest of the frame may be uninitialized, continue to
  112. * the next.
  113. */
  114. if (firstframe)
  115. continue;
  116. /* Mark stacktraces with exception frames as unreliable. */
  117. if (sp <= stack_end - STACK_INT_FRAME_SIZE &&
  118. stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
  119. return -EINVAL;
  120. }
  121. /* Examine the saved LR: it must point into kernel code. */
  122. ip = stack[STACK_FRAME_LR_SAVE];
  123. if (!__kernel_text_address(ip))
  124. return -EINVAL;
  125. /*
  126. * FIXME: IMHO these tests do not belong in
  127. * arch-dependent code, they are generic.
  128. */
  129. ip = ftrace_graph_ret_addr(task, &graph_idx, ip, stack);
  130. #ifdef CONFIG_KPROBES
  131. /*
  132. * Mark stacktraces with kretprobed functions on them
  133. * as unreliable.
  134. */
  135. if (ip == (unsigned long)__kretprobe_trampoline)
  136. return -EINVAL;
  137. #endif
  138. if (!consume_entry(cookie, ip))
  139. return -EINVAL;
  140. }
  141. return 0;
  142. }
  143. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI)
  144. static void handle_backtrace_ipi(struct pt_regs *regs)
  145. {
  146. nmi_cpu_backtrace(regs);
  147. }
  148. static void raise_backtrace_ipi(cpumask_t *mask)
  149. {
  150. struct paca_struct *p;
  151. unsigned int cpu;
  152. u64 delay_us;
  153. for_each_cpu(cpu, mask) {
  154. if (cpu == smp_processor_id()) {
  155. handle_backtrace_ipi(NULL);
  156. continue;
  157. }
  158. delay_us = 5 * USEC_PER_SEC;
  159. if (smp_send_safe_nmi_ipi(cpu, handle_backtrace_ipi, delay_us)) {
  160. // Now wait up to 5s for the other CPU to do its backtrace
  161. while (cpumask_test_cpu(cpu, mask) && delay_us) {
  162. udelay(1);
  163. delay_us--;
  164. }
  165. // Other CPU cleared itself from the mask
  166. if (delay_us)
  167. continue;
  168. }
  169. p = paca_ptrs[cpu];
  170. cpumask_clear_cpu(cpu, mask);
  171. pr_warn("CPU %d didn't respond to backtrace IPI, inspecting paca.\n", cpu);
  172. if (!virt_addr_valid(p)) {
  173. pr_warn("paca pointer appears corrupt? (%px)\n", p);
  174. continue;
  175. }
  176. pr_warn("irq_soft_mask: 0x%02x in_mce: %d in_nmi: %d",
  177. p->irq_soft_mask, p->in_mce, p->in_nmi);
  178. if (virt_addr_valid(p->__current))
  179. pr_cont(" current: %d (%s)\n", p->__current->pid,
  180. p->__current->comm);
  181. else
  182. pr_cont(" current pointer corrupt? (%px)\n", p->__current);
  183. pr_warn("Back trace of paca->saved_r1 (0x%016llx) (possibly stale):\n", p->saved_r1);
  184. show_stack(p->__current, (unsigned long *)p->saved_r1, KERN_WARNING);
  185. }
  186. }
  187. void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
  188. {
  189. nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi);
  190. }
  191. #endif /* defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI) */