stacktrace.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Stack trace management functions
  3. *
  4. * Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/sched/debug.h>
  8. #include <linux/sched/task_stack.h>
  9. #include <linux/stacktrace.h>
  10. #include <linux/export.h>
  11. #include <linux/uaccess.h>
  12. #include <asm/stacktrace.h>
  13. #include <asm/unwind.h>
  14. static int save_stack_address(struct stack_trace *trace, unsigned long addr,
  15. bool nosched)
  16. {
  17. if (nosched && in_sched_functions(addr))
  18. return 0;
  19. if (trace->skip > 0) {
  20. trace->skip--;
  21. return 0;
  22. }
  23. if (trace->nr_entries >= trace->max_entries)
  24. return -1;
  25. trace->entries[trace->nr_entries++] = addr;
  26. return 0;
  27. }
  28. static void __save_stack_trace(struct stack_trace *trace,
  29. struct task_struct *task, struct pt_regs *regs,
  30. bool nosched)
  31. {
  32. struct unwind_state state;
  33. unsigned long addr;
  34. if (regs)
  35. save_stack_address(trace, regs->ip, nosched);
  36. for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
  37. unwind_next_frame(&state)) {
  38. addr = unwind_get_return_address(&state);
  39. if (!addr || save_stack_address(trace, addr, nosched))
  40. break;
  41. }
  42. if (trace->nr_entries < trace->max_entries)
  43. trace->entries[trace->nr_entries++] = ULONG_MAX;
  44. }
  45. /*
  46. * Save stack-backtrace addresses into a stack_trace buffer.
  47. */
  48. void save_stack_trace(struct stack_trace *trace)
  49. {
  50. __save_stack_trace(trace, current, NULL, false);
  51. }
  52. EXPORT_SYMBOL_GPL(save_stack_trace);
  53. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  54. {
  55. __save_stack_trace(trace, current, regs, false);
  56. }
  57. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  58. {
  59. if (!try_get_task_stack(tsk))
  60. return;
  61. __save_stack_trace(trace, tsk, NULL, true);
  62. put_task_stack(tsk);
  63. }
  64. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
  65. #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
  66. #define STACKTRACE_DUMP_ONCE(task) ({ \
  67. static bool __section(.data.unlikely) __dumped; \
  68. \
  69. if (!__dumped) { \
  70. __dumped = true; \
  71. WARN_ON(1); \
  72. show_stack(task, NULL); \
  73. } \
  74. })
  75. static int __save_stack_trace_reliable(struct stack_trace *trace,
  76. struct task_struct *task)
  77. {
  78. struct unwind_state state;
  79. struct pt_regs *regs;
  80. unsigned long addr;
  81. for (unwind_start(&state, task, NULL, NULL); !unwind_done(&state);
  82. unwind_next_frame(&state)) {
  83. regs = unwind_get_entry_regs(&state);
  84. if (regs) {
  85. /*
  86. * Kernel mode registers on the stack indicate an
  87. * in-kernel interrupt or exception (e.g., preemption
  88. * or a page fault), which can make frame pointers
  89. * unreliable.
  90. */
  91. if (!user_mode(regs))
  92. return -EINVAL;
  93. /*
  94. * The last frame contains the user mode syscall
  95. * pt_regs. Skip it and finish the unwind.
  96. */
  97. unwind_next_frame(&state);
  98. if (!unwind_done(&state)) {
  99. STACKTRACE_DUMP_ONCE(task);
  100. return -EINVAL;
  101. }
  102. break;
  103. }
  104. addr = unwind_get_return_address(&state);
  105. /*
  106. * A NULL or invalid return address probably means there's some
  107. * generated code which __kernel_text_address() doesn't know
  108. * about.
  109. */
  110. if (!addr) {
  111. STACKTRACE_DUMP_ONCE(task);
  112. return -EINVAL;
  113. }
  114. if (save_stack_address(trace, addr, false))
  115. return -EINVAL;
  116. }
  117. /* Check for stack corruption */
  118. if (unwind_error(&state)) {
  119. STACKTRACE_DUMP_ONCE(task);
  120. return -EINVAL;
  121. }
  122. if (trace->nr_entries < trace->max_entries)
  123. trace->entries[trace->nr_entries++] = ULONG_MAX;
  124. return 0;
  125. }
  126. /*
  127. * This function returns an error if it detects any unreliable features of the
  128. * stack. Otherwise it guarantees that the stack trace is reliable.
  129. *
  130. * If the task is not 'current', the caller *must* ensure the task is inactive.
  131. */
  132. int save_stack_trace_tsk_reliable(struct task_struct *tsk,
  133. struct stack_trace *trace)
  134. {
  135. int ret;
  136. if (!try_get_task_stack(tsk))
  137. return -EINVAL;
  138. ret = __save_stack_trace_reliable(trace, tsk);
  139. put_task_stack(tsk);
  140. return ret;
  141. }
  142. #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
  143. /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
  144. struct stack_frame_user {
  145. const void __user *next_fp;
  146. unsigned long ret_addr;
  147. };
  148. static int
  149. copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
  150. {
  151. int ret;
  152. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  153. return 0;
  154. ret = 1;
  155. pagefault_disable();
  156. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  157. ret = 0;
  158. pagefault_enable();
  159. return ret;
  160. }
  161. static inline void __save_stack_trace_user(struct stack_trace *trace)
  162. {
  163. const struct pt_regs *regs = task_pt_regs(current);
  164. const void __user *fp = (const void __user *)regs->bp;
  165. if (trace->nr_entries < trace->max_entries)
  166. trace->entries[trace->nr_entries++] = regs->ip;
  167. while (trace->nr_entries < trace->max_entries) {
  168. struct stack_frame_user frame;
  169. frame.next_fp = NULL;
  170. frame.ret_addr = 0;
  171. if (!copy_stack_frame(fp, &frame))
  172. break;
  173. if ((unsigned long)fp < regs->sp)
  174. break;
  175. if (frame.ret_addr) {
  176. trace->entries[trace->nr_entries++] =
  177. frame.ret_addr;
  178. }
  179. if (fp == frame.next_fp)
  180. break;
  181. fp = frame.next_fp;
  182. }
  183. }
  184. void save_stack_trace_user(struct stack_trace *trace)
  185. {
  186. /*
  187. * Trace user stack if we are not a kernel thread
  188. */
  189. if (current->mm) {
  190. __save_stack_trace_user(trace);
  191. }
  192. if (trace->nr_entries < trace->max_entries)
  193. trace->entries[trace->nr_entries++] = ULONG_MAX;
  194. }