stacktrace.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 noinline __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. trace->skip++;
  51. __save_stack_trace(trace, current, NULL, false);
  52. }
  53. EXPORT_SYMBOL_GPL(save_stack_trace);
  54. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  55. {
  56. __save_stack_trace(trace, current, regs, false);
  57. }
  58. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  59. {
  60. if (!try_get_task_stack(tsk))
  61. return;
  62. if (tsk == current)
  63. trace->skip++;
  64. __save_stack_trace(trace, tsk, NULL, true);
  65. put_task_stack(tsk);
  66. }
  67. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
  68. #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
  69. static int __always_inline
  70. __save_stack_trace_reliable(struct stack_trace *trace,
  71. struct task_struct *task)
  72. {
  73. struct unwind_state state;
  74. struct pt_regs *regs;
  75. unsigned long addr;
  76. for (unwind_start(&state, task, NULL, NULL);
  77. !unwind_done(&state) && !unwind_error(&state);
  78. unwind_next_frame(&state)) {
  79. regs = unwind_get_entry_regs(&state, NULL);
  80. if (regs) {
  81. /* Success path for user tasks */
  82. if (user_mode(regs))
  83. goto success;
  84. /*
  85. * Kernel mode registers on the stack indicate an
  86. * in-kernel interrupt or exception (e.g., preemption
  87. * or a page fault), which can make frame pointers
  88. * unreliable.
  89. */
  90. if (IS_ENABLED(CONFIG_FRAME_POINTER))
  91. return -EINVAL;
  92. }
  93. addr = unwind_get_return_address(&state);
  94. /*
  95. * A NULL or invalid return address probably means there's some
  96. * generated code which __kernel_text_address() doesn't know
  97. * about.
  98. */
  99. if (!addr)
  100. return -EINVAL;
  101. if (save_stack_address(trace, addr, false))
  102. return -EINVAL;
  103. }
  104. /* Check for stack corruption */
  105. if (unwind_error(&state))
  106. return -EINVAL;
  107. /* Success path for non-user tasks, i.e. kthreads and idle tasks */
  108. if (!(task->flags & (PF_KTHREAD | PF_IDLE)))
  109. return -EINVAL;
  110. success:
  111. if (trace->nr_entries < trace->max_entries)
  112. trace->entries[trace->nr_entries++] = ULONG_MAX;
  113. return 0;
  114. }
  115. /*
  116. * This function returns an error if it detects any unreliable features of the
  117. * stack. Otherwise it guarantees that the stack trace is reliable.
  118. *
  119. * If the task is not 'current', the caller *must* ensure the task is inactive.
  120. */
  121. int save_stack_trace_tsk_reliable(struct task_struct *tsk,
  122. struct stack_trace *trace)
  123. {
  124. int ret;
  125. /*
  126. * If the task doesn't have a stack (e.g., a zombie), the stack is
  127. * "reliably" empty.
  128. */
  129. if (!try_get_task_stack(tsk))
  130. return 0;
  131. ret = __save_stack_trace_reliable(trace, tsk);
  132. put_task_stack(tsk);
  133. return ret;
  134. }
  135. #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
  136. /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
  137. struct stack_frame_user {
  138. const void __user *next_fp;
  139. unsigned long ret_addr;
  140. };
  141. static int
  142. copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
  143. {
  144. int ret;
  145. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  146. return 0;
  147. ret = 1;
  148. pagefault_disable();
  149. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  150. ret = 0;
  151. pagefault_enable();
  152. return ret;
  153. }
  154. static inline void __save_stack_trace_user(struct stack_trace *trace)
  155. {
  156. const struct pt_regs *regs = task_pt_regs(current);
  157. const void __user *fp = (const void __user *)regs->bp;
  158. if (trace->nr_entries < trace->max_entries)
  159. trace->entries[trace->nr_entries++] = regs->ip;
  160. while (trace->nr_entries < trace->max_entries) {
  161. struct stack_frame_user frame;
  162. frame.next_fp = NULL;
  163. frame.ret_addr = 0;
  164. if (!copy_stack_frame(fp, &frame))
  165. break;
  166. if ((unsigned long)fp < regs->sp)
  167. break;
  168. if (frame.ret_addr) {
  169. trace->entries[trace->nr_entries++] =
  170. frame.ret_addr;
  171. }
  172. if (fp == frame.next_fp)
  173. break;
  174. fp = frame.next_fp;
  175. }
  176. }
  177. void save_stack_trace_user(struct stack_trace *trace)
  178. {
  179. /*
  180. * Trace user stack if we are not a kernel thread
  181. */
  182. if (current->mm) {
  183. __save_stack_trace_user(trace);
  184. }
  185. if (trace->nr_entries < trace->max_entries)
  186. trace->entries[trace->nr_entries++] = ULONG_MAX;
  187. }