stacktrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Stack tracing support
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/efi.h>
  9. #include <linux/export.h>
  10. #include <linux/filter.h>
  11. #include <linux/ftrace.h>
  12. #include <linux/kprobes.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/debug.h>
  15. #include <linux/sched/task_stack.h>
  16. #include <linux/stacktrace.h>
  17. #include <asm/efi.h>
  18. #include <asm/irq.h>
  19. #include <asm/stack_pointer.h>
  20. #include <asm/stacktrace.h>
  21. /*
  22. * Kernel unwind state
  23. *
  24. * @common: Common unwind state.
  25. * @task: The task being unwound.
  26. * @graph_idx: Used by ftrace_graph_ret_addr() for optimized stack unwinding.
  27. * @kr_cur: When KRETPROBES is selected, holds the kretprobe instance
  28. * associated with the most recently encountered replacement lr
  29. * value.
  30. */
  31. struct kunwind_state {
  32. struct unwind_state common;
  33. struct task_struct *task;
  34. int graph_idx;
  35. #ifdef CONFIG_KRETPROBES
  36. struct llist_node *kr_cur;
  37. #endif
  38. };
  39. static __always_inline void
  40. kunwind_init(struct kunwind_state *state,
  41. struct task_struct *task)
  42. {
  43. unwind_init_common(&state->common);
  44. state->task = task;
  45. }
  46. /*
  47. * Start an unwind from a pt_regs.
  48. *
  49. * The unwind will begin at the PC within the regs.
  50. *
  51. * The regs must be on a stack currently owned by the calling task.
  52. */
  53. static __always_inline void
  54. kunwind_init_from_regs(struct kunwind_state *state,
  55. struct pt_regs *regs)
  56. {
  57. kunwind_init(state, current);
  58. state->common.fp = regs->regs[29];
  59. state->common.pc = regs->pc;
  60. }
  61. /*
  62. * Start an unwind from a caller.
  63. *
  64. * The unwind will begin at the caller of whichever function this is inlined
  65. * into.
  66. *
  67. * The function which invokes this must be noinline.
  68. */
  69. static __always_inline void
  70. kunwind_init_from_caller(struct kunwind_state *state)
  71. {
  72. kunwind_init(state, current);
  73. state->common.fp = (unsigned long)__builtin_frame_address(1);
  74. state->common.pc = (unsigned long)__builtin_return_address(0);
  75. }
  76. /*
  77. * Start an unwind from a blocked task.
  78. *
  79. * The unwind will begin at the blocked tasks saved PC (i.e. the caller of
  80. * cpu_switch_to()).
  81. *
  82. * The caller should ensure the task is blocked in cpu_switch_to() for the
  83. * duration of the unwind, or the unwind will be bogus. It is never valid to
  84. * call this for the current task.
  85. */
  86. static __always_inline void
  87. kunwind_init_from_task(struct kunwind_state *state,
  88. struct task_struct *task)
  89. {
  90. kunwind_init(state, task);
  91. state->common.fp = thread_saved_fp(task);
  92. state->common.pc = thread_saved_pc(task);
  93. }
  94. static __always_inline int
  95. kunwind_recover_return_address(struct kunwind_state *state)
  96. {
  97. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  98. if (state->task->ret_stack &&
  99. (state->common.pc == (unsigned long)return_to_handler)) {
  100. unsigned long orig_pc;
  101. orig_pc = ftrace_graph_ret_addr(state->task, &state->graph_idx,
  102. state->common.pc,
  103. (void *)state->common.fp);
  104. if (WARN_ON_ONCE(state->common.pc == orig_pc))
  105. return -EINVAL;
  106. state->common.pc = orig_pc;
  107. }
  108. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  109. #ifdef CONFIG_KRETPROBES
  110. if (is_kretprobe_trampoline(state->common.pc)) {
  111. unsigned long orig_pc;
  112. orig_pc = kretprobe_find_ret_addr(state->task,
  113. (void *)state->common.fp,
  114. &state->kr_cur);
  115. if (!orig_pc)
  116. return -EINVAL;
  117. state->common.pc = orig_pc;
  118. }
  119. #endif /* CONFIG_KRETPROBES */
  120. return 0;
  121. }
  122. /*
  123. * Unwind from one frame record (A) to the next frame record (B).
  124. *
  125. * We terminate early if the location of B indicates a malformed chain of frame
  126. * records (e.g. a cycle), determined based on the location and fp value of A
  127. * and the location (but not the fp value) of B.
  128. */
  129. static __always_inline int
  130. kunwind_next(struct kunwind_state *state)
  131. {
  132. struct task_struct *tsk = state->task;
  133. unsigned long fp = state->common.fp;
  134. int err;
  135. /* Final frame; nothing to unwind */
  136. if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
  137. return -ENOENT;
  138. err = unwind_next_frame_record(&state->common);
  139. if (err)
  140. return err;
  141. state->common.pc = ptrauth_strip_kernel_insn_pac(state->common.pc);
  142. return kunwind_recover_return_address(state);
  143. }
  144. typedef bool (*kunwind_consume_fn)(const struct kunwind_state *state, void *cookie);
  145. static __always_inline void
  146. do_kunwind(struct kunwind_state *state, kunwind_consume_fn consume_state,
  147. void *cookie)
  148. {
  149. if (kunwind_recover_return_address(state))
  150. return;
  151. while (1) {
  152. int ret;
  153. if (!consume_state(state, cookie))
  154. break;
  155. ret = kunwind_next(state);
  156. if (ret < 0)
  157. break;
  158. }
  159. }
  160. /*
  161. * Per-cpu stacks are only accessible when unwinding the current task in a
  162. * non-preemptible context.
  163. */
  164. #define STACKINFO_CPU(name) \
  165. ({ \
  166. ((task == current) && !preemptible()) \
  167. ? stackinfo_get_##name() \
  168. : stackinfo_get_unknown(); \
  169. })
  170. /*
  171. * SDEI stacks are only accessible when unwinding the current task in an NMI
  172. * context.
  173. */
  174. #define STACKINFO_SDEI(name) \
  175. ({ \
  176. ((task == current) && in_nmi()) \
  177. ? stackinfo_get_sdei_##name() \
  178. : stackinfo_get_unknown(); \
  179. })
  180. #define STACKINFO_EFI \
  181. ({ \
  182. ((task == current) && current_in_efi()) \
  183. ? stackinfo_get_efi() \
  184. : stackinfo_get_unknown(); \
  185. })
  186. static __always_inline void
  187. kunwind_stack_walk(kunwind_consume_fn consume_state,
  188. void *cookie, struct task_struct *task,
  189. struct pt_regs *regs)
  190. {
  191. struct stack_info stacks[] = {
  192. stackinfo_get_task(task),
  193. STACKINFO_CPU(irq),
  194. #if defined(CONFIG_VMAP_STACK)
  195. STACKINFO_CPU(overflow),
  196. #endif
  197. #if defined(CONFIG_VMAP_STACK) && defined(CONFIG_ARM_SDE_INTERFACE)
  198. STACKINFO_SDEI(normal),
  199. STACKINFO_SDEI(critical),
  200. #endif
  201. #ifdef CONFIG_EFI
  202. STACKINFO_EFI,
  203. #endif
  204. };
  205. struct kunwind_state state = {
  206. .common = {
  207. .stacks = stacks,
  208. .nr_stacks = ARRAY_SIZE(stacks),
  209. },
  210. };
  211. if (regs) {
  212. if (task != current)
  213. return;
  214. kunwind_init_from_regs(&state, regs);
  215. } else if (task == current) {
  216. kunwind_init_from_caller(&state);
  217. } else {
  218. kunwind_init_from_task(&state, task);
  219. }
  220. do_kunwind(&state, consume_state, cookie);
  221. }
  222. struct kunwind_consume_entry_data {
  223. stack_trace_consume_fn consume_entry;
  224. void *cookie;
  225. };
  226. static __always_inline bool
  227. arch_kunwind_consume_entry(const struct kunwind_state *state, void *cookie)
  228. {
  229. struct kunwind_consume_entry_data *data = cookie;
  230. return data->consume_entry(data->cookie, state->common.pc);
  231. }
  232. noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry,
  233. void *cookie, struct task_struct *task,
  234. struct pt_regs *regs)
  235. {
  236. struct kunwind_consume_entry_data data = {
  237. .consume_entry = consume_entry,
  238. .cookie = cookie,
  239. };
  240. kunwind_stack_walk(arch_kunwind_consume_entry, &data, task, regs);
  241. }
  242. struct bpf_unwind_consume_entry_data {
  243. bool (*consume_entry)(void *cookie, u64 ip, u64 sp, u64 fp);
  244. void *cookie;
  245. };
  246. static bool
  247. arch_bpf_unwind_consume_entry(const struct kunwind_state *state, void *cookie)
  248. {
  249. struct bpf_unwind_consume_entry_data *data = cookie;
  250. return data->consume_entry(data->cookie, state->common.pc, 0,
  251. state->common.fp);
  252. }
  253. noinline noinstr void arch_bpf_stack_walk(bool (*consume_entry)(void *cookie, u64 ip, u64 sp,
  254. u64 fp), void *cookie)
  255. {
  256. struct bpf_unwind_consume_entry_data data = {
  257. .consume_entry = consume_entry,
  258. .cookie = cookie,
  259. };
  260. kunwind_stack_walk(arch_bpf_unwind_consume_entry, &data, current, NULL);
  261. }
  262. static bool dump_backtrace_entry(void *arg, unsigned long where)
  263. {
  264. char *loglvl = arg;
  265. printk("%s %pSb\n", loglvl, (void *)where);
  266. return true;
  267. }
  268. void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
  269. const char *loglvl)
  270. {
  271. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  272. if (regs && user_mode(regs))
  273. return;
  274. if (!tsk)
  275. tsk = current;
  276. if (!try_get_task_stack(tsk))
  277. return;
  278. printk("%sCall trace:\n", loglvl);
  279. arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
  280. put_task_stack(tsk);
  281. }
  282. void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
  283. {
  284. dump_backtrace(NULL, tsk, loglvl);
  285. barrier();
  286. }
  287. /*
  288. * The struct defined for userspace stack frame in AARCH64 mode.
  289. */
  290. struct frame_tail {
  291. struct frame_tail __user *fp;
  292. unsigned long lr;
  293. } __attribute__((packed));
  294. /*
  295. * Get the return address for a single stackframe and return a pointer to the
  296. * next frame tail.
  297. */
  298. static struct frame_tail __user *
  299. unwind_user_frame(struct frame_tail __user *tail, void *cookie,
  300. stack_trace_consume_fn consume_entry)
  301. {
  302. struct frame_tail buftail;
  303. unsigned long err;
  304. unsigned long lr;
  305. /* Also check accessibility of one struct frame_tail beyond */
  306. if (!access_ok(tail, sizeof(buftail)))
  307. return NULL;
  308. pagefault_disable();
  309. err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
  310. pagefault_enable();
  311. if (err)
  312. return NULL;
  313. lr = ptrauth_strip_user_insn_pac(buftail.lr);
  314. if (!consume_entry(cookie, lr))
  315. return NULL;
  316. /*
  317. * Frame pointers should strictly progress back up the stack
  318. * (towards higher addresses).
  319. */
  320. if (tail >= buftail.fp)
  321. return NULL;
  322. return buftail.fp;
  323. }
  324. #ifdef CONFIG_COMPAT
  325. /*
  326. * The registers we're interested in are at the end of the variable
  327. * length saved register structure. The fp points at the end of this
  328. * structure so the address of this struct is:
  329. * (struct compat_frame_tail *)(xxx->fp)-1
  330. *
  331. * This code has been adapted from the ARM OProfile support.
  332. */
  333. struct compat_frame_tail {
  334. compat_uptr_t fp; /* a (struct compat_frame_tail *) in compat mode */
  335. u32 sp;
  336. u32 lr;
  337. } __attribute__((packed));
  338. static struct compat_frame_tail __user *
  339. unwind_compat_user_frame(struct compat_frame_tail __user *tail, void *cookie,
  340. stack_trace_consume_fn consume_entry)
  341. {
  342. struct compat_frame_tail buftail;
  343. unsigned long err;
  344. /* Also check accessibility of one struct frame_tail beyond */
  345. if (!access_ok(tail, sizeof(buftail)))
  346. return NULL;
  347. pagefault_disable();
  348. err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
  349. pagefault_enable();
  350. if (err)
  351. return NULL;
  352. if (!consume_entry(cookie, buftail.lr))
  353. return NULL;
  354. /*
  355. * Frame pointers should strictly progress back up the stack
  356. * (towards higher addresses).
  357. */
  358. if (tail + 1 >= (struct compat_frame_tail __user *)
  359. compat_ptr(buftail.fp))
  360. return NULL;
  361. return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1;
  362. }
  363. #endif /* CONFIG_COMPAT */
  364. void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
  365. const struct pt_regs *regs)
  366. {
  367. if (!consume_entry(cookie, regs->pc))
  368. return;
  369. if (!compat_user_mode(regs)) {
  370. /* AARCH64 mode */
  371. struct frame_tail __user *tail;
  372. tail = (struct frame_tail __user *)regs->regs[29];
  373. while (tail && !((unsigned long)tail & 0x7))
  374. tail = unwind_user_frame(tail, cookie, consume_entry);
  375. } else {
  376. #ifdef CONFIG_COMPAT
  377. /* AARCH32 compat mode */
  378. struct compat_frame_tail __user *tail;
  379. tail = (struct compat_frame_tail __user *)regs->compat_fp - 1;
  380. while (tail && !((unsigned long)tail & 0x3))
  381. tail = unwind_compat_user_frame(tail, cookie, consume_entry);
  382. #endif
  383. }
  384. }