fgraph.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Infrastructure to took into function calls and returns.
  4. * Copyright (c) 2008-2009 Frederic Weisbecker <[email protected]>
  5. * Mostly borrowed from function tracer which
  6. * is Copyright (c) Steven Rostedt <[email protected]>
  7. *
  8. * Highly modified by Steven Rostedt (VMware).
  9. */
  10. #include <linux/jump_label.h>
  11. #include <linux/suspend.h>
  12. #include <linux/ftrace.h>
  13. #include <linux/slab.h>
  14. #include <trace/events/sched.h>
  15. #include "ftrace_internal.h"
  16. #ifdef CONFIG_DYNAMIC_FTRACE
  17. #define ASSIGN_OPS_HASH(opsname, val) \
  18. .func_hash = val, \
  19. .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
  20. #else
  21. #define ASSIGN_OPS_HASH(opsname, val)
  22. #endif
  23. DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph);
  24. int ftrace_graph_active;
  25. /* Both enabled by default (can be cleared by function_graph tracer flags */
  26. static bool fgraph_sleep_time = true;
  27. #ifdef CONFIG_DYNAMIC_FTRACE
  28. /*
  29. * archs can override this function if they must do something
  30. * to enable hook for graph tracer.
  31. */
  32. int __weak ftrace_enable_ftrace_graph_caller(void)
  33. {
  34. return 0;
  35. }
  36. /*
  37. * archs can override this function if they must do something
  38. * to disable hook for graph tracer.
  39. */
  40. int __weak ftrace_disable_ftrace_graph_caller(void)
  41. {
  42. return 0;
  43. }
  44. #endif
  45. /**
  46. * ftrace_graph_stop - set to permanently disable function graph tracing
  47. *
  48. * In case of an error int function graph tracing, this is called
  49. * to try to keep function graph tracing from causing any more harm.
  50. * Usually this is pretty severe and this is called to try to at least
  51. * get a warning out to the user.
  52. */
  53. void ftrace_graph_stop(void)
  54. {
  55. static_branch_enable(&kill_ftrace_graph);
  56. }
  57. /* Add a function return address to the trace stack on thread info.*/
  58. static int
  59. ftrace_push_return_trace(unsigned long ret, unsigned long func,
  60. unsigned long frame_pointer, unsigned long *retp)
  61. {
  62. unsigned long long calltime;
  63. int index;
  64. if (unlikely(ftrace_graph_is_dead()))
  65. return -EBUSY;
  66. if (!current->ret_stack)
  67. return -EBUSY;
  68. /*
  69. * We must make sure the ret_stack is tested before we read
  70. * anything else.
  71. */
  72. smp_rmb();
  73. /* The return trace stack is full */
  74. if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
  75. atomic_inc(&current->trace_overrun);
  76. return -EBUSY;
  77. }
  78. calltime = trace_clock_local();
  79. index = ++current->curr_ret_stack;
  80. barrier();
  81. current->ret_stack[index].ret = ret;
  82. current->ret_stack[index].func = func;
  83. current->ret_stack[index].calltime = calltime;
  84. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  85. current->ret_stack[index].fp = frame_pointer;
  86. #endif
  87. #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
  88. current->ret_stack[index].retp = retp;
  89. #endif
  90. return 0;
  91. }
  92. /*
  93. * Not all archs define MCOUNT_INSN_SIZE which is used to look for direct
  94. * functions. But those archs currently don't support direct functions
  95. * anyway, and ftrace_find_rec_direct() is just a stub for them.
  96. * Define MCOUNT_INSN_SIZE to keep those archs compiling.
  97. */
  98. #ifndef MCOUNT_INSN_SIZE
  99. /* Make sure this only works without direct calls */
  100. # ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
  101. # error MCOUNT_INSN_SIZE not defined with direct calls enabled
  102. # endif
  103. # define MCOUNT_INSN_SIZE 0
  104. #endif
  105. int function_graph_enter(unsigned long ret, unsigned long func,
  106. unsigned long frame_pointer, unsigned long *retp)
  107. {
  108. struct ftrace_graph_ent trace;
  109. #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
  110. /*
  111. * Skip graph tracing if the return location is served by direct trampoline,
  112. * since call sequence and return addresses are unpredictable anyway.
  113. * Ex: BPF trampoline may call original function and may skip frame
  114. * depending on type of BPF programs attached.
  115. */
  116. if (ftrace_direct_func_count &&
  117. ftrace_find_rec_direct(ret - MCOUNT_INSN_SIZE))
  118. return -EBUSY;
  119. #endif
  120. trace.func = func;
  121. trace.depth = ++current->curr_ret_depth;
  122. if (ftrace_push_return_trace(ret, func, frame_pointer, retp))
  123. goto out;
  124. /* Only trace if the calling function expects to */
  125. if (!ftrace_graph_entry(&trace))
  126. goto out_ret;
  127. return 0;
  128. out_ret:
  129. current->curr_ret_stack--;
  130. out:
  131. current->curr_ret_depth--;
  132. return -EBUSY;
  133. }
  134. /* Retrieve a function return address to the trace stack on thread info.*/
  135. static void
  136. ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
  137. unsigned long frame_pointer)
  138. {
  139. int index;
  140. index = current->curr_ret_stack;
  141. if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
  142. ftrace_graph_stop();
  143. WARN_ON(1);
  144. /* Might as well panic, otherwise we have no where to go */
  145. *ret = (unsigned long)panic;
  146. return;
  147. }
  148. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  149. /*
  150. * The arch may choose to record the frame pointer used
  151. * and check it here to make sure that it is what we expect it
  152. * to be. If gcc does not set the place holder of the return
  153. * address in the frame pointer, and does a copy instead, then
  154. * the function graph trace will fail. This test detects this
  155. * case.
  156. *
  157. * Currently, x86_32 with optimize for size (-Os) makes the latest
  158. * gcc do the above.
  159. *
  160. * Note, -mfentry does not use frame pointers, and this test
  161. * is not needed if CC_USING_FENTRY is set.
  162. */
  163. if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
  164. ftrace_graph_stop();
  165. WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
  166. " from func %ps return to %lx\n",
  167. current->ret_stack[index].fp,
  168. frame_pointer,
  169. (void *)current->ret_stack[index].func,
  170. current->ret_stack[index].ret);
  171. *ret = (unsigned long)panic;
  172. return;
  173. }
  174. #endif
  175. *ret = current->ret_stack[index].ret;
  176. trace->func = current->ret_stack[index].func;
  177. trace->calltime = current->ret_stack[index].calltime;
  178. trace->overrun = atomic_read(&current->trace_overrun);
  179. trace->depth = current->curr_ret_depth--;
  180. /*
  181. * We still want to trace interrupts coming in if
  182. * max_depth is set to 1. Make sure the decrement is
  183. * seen before ftrace_graph_return.
  184. */
  185. barrier();
  186. }
  187. /*
  188. * Hibernation protection.
  189. * The state of the current task is too much unstable during
  190. * suspend/restore to disk. We want to protect against that.
  191. */
  192. static int
  193. ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
  194. void *unused)
  195. {
  196. switch (state) {
  197. case PM_HIBERNATION_PREPARE:
  198. pause_graph_tracing();
  199. break;
  200. case PM_POST_HIBERNATION:
  201. unpause_graph_tracing();
  202. break;
  203. }
  204. return NOTIFY_DONE;
  205. }
  206. static struct notifier_block ftrace_suspend_notifier = {
  207. .notifier_call = ftrace_suspend_notifier_call,
  208. };
  209. /*
  210. * Send the trace to the ring-buffer.
  211. * @return the original return address.
  212. */
  213. unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
  214. {
  215. struct ftrace_graph_ret trace;
  216. unsigned long ret;
  217. ftrace_pop_return_trace(&trace, &ret, frame_pointer);
  218. trace.rettime = trace_clock_local();
  219. ftrace_graph_return(&trace);
  220. /*
  221. * The ftrace_graph_return() may still access the current
  222. * ret_stack structure, we need to make sure the update of
  223. * curr_ret_stack is after that.
  224. */
  225. barrier();
  226. current->curr_ret_stack--;
  227. if (unlikely(!ret)) {
  228. ftrace_graph_stop();
  229. WARN_ON(1);
  230. /* Might as well panic. What else to do? */
  231. ret = (unsigned long)panic;
  232. }
  233. return ret;
  234. }
  235. /**
  236. * ftrace_graph_get_ret_stack - return the entry of the shadow stack
  237. * @task: The task to read the shadow stack from
  238. * @idx: Index down the shadow stack
  239. *
  240. * Return the ret_struct on the shadow stack of the @task at the
  241. * call graph at @idx starting with zero. If @idx is zero, it
  242. * will return the last saved ret_stack entry. If it is greater than
  243. * zero, it will return the corresponding ret_stack for the depth
  244. * of saved return addresses.
  245. */
  246. struct ftrace_ret_stack *
  247. ftrace_graph_get_ret_stack(struct task_struct *task, int idx)
  248. {
  249. idx = task->curr_ret_stack - idx;
  250. if (idx >= 0 && idx <= task->curr_ret_stack)
  251. return &task->ret_stack[idx];
  252. return NULL;
  253. }
  254. /**
  255. * ftrace_graph_ret_addr - convert a potentially modified stack return address
  256. * to its original value
  257. *
  258. * This function can be called by stack unwinding code to convert a found stack
  259. * return address ('ret') to its original value, in case the function graph
  260. * tracer has modified it to be 'return_to_handler'. If the address hasn't
  261. * been modified, the unchanged value of 'ret' is returned.
  262. *
  263. * 'idx' is a state variable which should be initialized by the caller to zero
  264. * before the first call.
  265. *
  266. * 'retp' is a pointer to the return address on the stack. It's ignored if
  267. * the arch doesn't have HAVE_FUNCTION_GRAPH_RET_ADDR_PTR defined.
  268. */
  269. #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
  270. unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
  271. unsigned long ret, unsigned long *retp)
  272. {
  273. int index = task->curr_ret_stack;
  274. int i;
  275. if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
  276. return ret;
  277. if (index < 0)
  278. return ret;
  279. for (i = 0; i <= index; i++)
  280. if (task->ret_stack[i].retp == retp)
  281. return task->ret_stack[i].ret;
  282. return ret;
  283. }
  284. #else /* !HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
  285. unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
  286. unsigned long ret, unsigned long *retp)
  287. {
  288. int task_idx;
  289. if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
  290. return ret;
  291. task_idx = task->curr_ret_stack;
  292. if (!task->ret_stack || task_idx < *idx)
  293. return ret;
  294. task_idx -= *idx;
  295. (*idx)++;
  296. return task->ret_stack[task_idx].ret;
  297. }
  298. #endif /* HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
  299. static struct ftrace_ops graph_ops = {
  300. .func = ftrace_graph_func,
  301. .flags = FTRACE_OPS_FL_INITIALIZED |
  302. FTRACE_OPS_FL_PID |
  303. FTRACE_OPS_GRAPH_STUB,
  304. #ifdef FTRACE_GRAPH_TRAMP_ADDR
  305. .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
  306. /* trampoline_size is only needed for dynamically allocated tramps */
  307. #endif
  308. ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
  309. };
  310. void ftrace_graph_sleep_time_control(bool enable)
  311. {
  312. fgraph_sleep_time = enable;
  313. }
  314. int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
  315. {
  316. return 0;
  317. }
  318. /*
  319. * Simply points to ftrace_stub, but with the proper protocol.
  320. * Defined by the linker script in linux/vmlinux.lds.h
  321. */
  322. extern void ftrace_stub_graph(struct ftrace_graph_ret *);
  323. /* The callbacks that hook a function */
  324. trace_func_graph_ret_t ftrace_graph_return = ftrace_stub_graph;
  325. trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
  326. static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
  327. /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
  328. static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
  329. {
  330. int i;
  331. int ret = 0;
  332. int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
  333. struct task_struct *g, *t;
  334. for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
  335. ret_stack_list[i] =
  336. kmalloc_array(FTRACE_RETFUNC_DEPTH,
  337. sizeof(struct ftrace_ret_stack),
  338. GFP_KERNEL);
  339. if (!ret_stack_list[i]) {
  340. start = 0;
  341. end = i;
  342. ret = -ENOMEM;
  343. goto free;
  344. }
  345. }
  346. rcu_read_lock();
  347. for_each_process_thread(g, t) {
  348. if (start == end) {
  349. ret = -EAGAIN;
  350. goto unlock;
  351. }
  352. if (t->ret_stack == NULL) {
  353. atomic_set(&t->trace_overrun, 0);
  354. t->curr_ret_stack = -1;
  355. t->curr_ret_depth = -1;
  356. /* Make sure the tasks see the -1 first: */
  357. smp_wmb();
  358. t->ret_stack = ret_stack_list[start++];
  359. }
  360. }
  361. unlock:
  362. rcu_read_unlock();
  363. free:
  364. for (i = start; i < end; i++)
  365. kfree(ret_stack_list[i]);
  366. return ret;
  367. }
  368. static void
  369. ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
  370. struct task_struct *prev,
  371. struct task_struct *next,
  372. unsigned int prev_state)
  373. {
  374. unsigned long long timestamp;
  375. int index;
  376. /*
  377. * Does the user want to count the time a function was asleep.
  378. * If so, do not update the time stamps.
  379. */
  380. if (fgraph_sleep_time)
  381. return;
  382. timestamp = trace_clock_local();
  383. prev->ftrace_timestamp = timestamp;
  384. /* only process tasks that we timestamped */
  385. if (!next->ftrace_timestamp)
  386. return;
  387. /*
  388. * Update all the counters in next to make up for the
  389. * time next was sleeping.
  390. */
  391. timestamp -= next->ftrace_timestamp;
  392. for (index = next->curr_ret_stack; index >= 0; index--)
  393. next->ret_stack[index].calltime += timestamp;
  394. }
  395. static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
  396. {
  397. if (!ftrace_ops_test(&global_ops, trace->func, NULL))
  398. return 0;
  399. return __ftrace_graph_entry(trace);
  400. }
  401. /*
  402. * The function graph tracer should only trace the functions defined
  403. * by set_ftrace_filter and set_ftrace_notrace. If another function
  404. * tracer ops is registered, the graph tracer requires testing the
  405. * function against the global ops, and not just trace any function
  406. * that any ftrace_ops registered.
  407. */
  408. void update_function_graph_func(void)
  409. {
  410. struct ftrace_ops *op;
  411. bool do_test = false;
  412. /*
  413. * The graph and global ops share the same set of functions
  414. * to test. If any other ops is on the list, then
  415. * the graph tracing needs to test if its the function
  416. * it should call.
  417. */
  418. do_for_each_ftrace_op(op, ftrace_ops_list) {
  419. if (op != &global_ops && op != &graph_ops &&
  420. op != &ftrace_list_end) {
  421. do_test = true;
  422. /* in double loop, break out with goto */
  423. goto out;
  424. }
  425. } while_for_each_ftrace_op(op);
  426. out:
  427. if (do_test)
  428. ftrace_graph_entry = ftrace_graph_entry_test;
  429. else
  430. ftrace_graph_entry = __ftrace_graph_entry;
  431. }
  432. static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
  433. static void
  434. graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
  435. {
  436. atomic_set(&t->trace_overrun, 0);
  437. t->ftrace_timestamp = 0;
  438. /* make curr_ret_stack visible before we add the ret_stack */
  439. smp_wmb();
  440. t->ret_stack = ret_stack;
  441. }
  442. /*
  443. * Allocate a return stack for the idle task. May be the first
  444. * time through, or it may be done by CPU hotplug online.
  445. */
  446. void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
  447. {
  448. t->curr_ret_stack = -1;
  449. t->curr_ret_depth = -1;
  450. /*
  451. * The idle task has no parent, it either has its own
  452. * stack or no stack at all.
  453. */
  454. if (t->ret_stack)
  455. WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
  456. if (ftrace_graph_active) {
  457. struct ftrace_ret_stack *ret_stack;
  458. ret_stack = per_cpu(idle_ret_stack, cpu);
  459. if (!ret_stack) {
  460. ret_stack =
  461. kmalloc_array(FTRACE_RETFUNC_DEPTH,
  462. sizeof(struct ftrace_ret_stack),
  463. GFP_KERNEL);
  464. if (!ret_stack)
  465. return;
  466. per_cpu(idle_ret_stack, cpu) = ret_stack;
  467. }
  468. graph_init_task(t, ret_stack);
  469. }
  470. }
  471. /* Allocate a return stack for newly created task */
  472. void ftrace_graph_init_task(struct task_struct *t)
  473. {
  474. /* Make sure we do not use the parent ret_stack */
  475. t->ret_stack = NULL;
  476. t->curr_ret_stack = -1;
  477. t->curr_ret_depth = -1;
  478. if (ftrace_graph_active) {
  479. struct ftrace_ret_stack *ret_stack;
  480. ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
  481. sizeof(struct ftrace_ret_stack),
  482. GFP_KERNEL);
  483. if (!ret_stack)
  484. return;
  485. graph_init_task(t, ret_stack);
  486. }
  487. }
  488. void ftrace_graph_exit_task(struct task_struct *t)
  489. {
  490. struct ftrace_ret_stack *ret_stack = t->ret_stack;
  491. t->ret_stack = NULL;
  492. /* NULL must become visible to IRQs before we free it: */
  493. barrier();
  494. kfree(ret_stack);
  495. }
  496. /* Allocate a return stack for each task */
  497. static int start_graph_tracing(void)
  498. {
  499. struct ftrace_ret_stack **ret_stack_list;
  500. int ret, cpu;
  501. ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
  502. sizeof(struct ftrace_ret_stack *),
  503. GFP_KERNEL);
  504. if (!ret_stack_list)
  505. return -ENOMEM;
  506. /* The cpu_boot init_task->ret_stack will never be freed */
  507. for_each_online_cpu(cpu) {
  508. if (!idle_task(cpu)->ret_stack)
  509. ftrace_graph_init_idle_task(idle_task(cpu), cpu);
  510. }
  511. do {
  512. ret = alloc_retstack_tasklist(ret_stack_list);
  513. } while (ret == -EAGAIN);
  514. if (!ret) {
  515. ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  516. if (ret)
  517. pr_info("ftrace_graph: Couldn't activate tracepoint"
  518. " probe to kernel_sched_switch\n");
  519. }
  520. kfree(ret_stack_list);
  521. return ret;
  522. }
  523. int register_ftrace_graph(struct fgraph_ops *gops)
  524. {
  525. int ret = 0;
  526. mutex_lock(&ftrace_lock);
  527. /* we currently allow only one tracer registered at a time */
  528. if (ftrace_graph_active) {
  529. ret = -EBUSY;
  530. goto out;
  531. }
  532. register_pm_notifier(&ftrace_suspend_notifier);
  533. ftrace_graph_active++;
  534. ret = start_graph_tracing();
  535. if (ret) {
  536. ftrace_graph_active--;
  537. goto out;
  538. }
  539. ftrace_graph_return = gops->retfunc;
  540. /*
  541. * Update the indirect function to the entryfunc, and the
  542. * function that gets called to the entry_test first. Then
  543. * call the update fgraph entry function to determine if
  544. * the entryfunc should be called directly or not.
  545. */
  546. __ftrace_graph_entry = gops->entryfunc;
  547. ftrace_graph_entry = ftrace_graph_entry_test;
  548. update_function_graph_func();
  549. ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
  550. out:
  551. mutex_unlock(&ftrace_lock);
  552. return ret;
  553. }
  554. void unregister_ftrace_graph(struct fgraph_ops *gops)
  555. {
  556. mutex_lock(&ftrace_lock);
  557. if (unlikely(!ftrace_graph_active))
  558. goto out;
  559. ftrace_graph_active--;
  560. ftrace_graph_return = ftrace_stub_graph;
  561. ftrace_graph_entry = ftrace_graph_entry_stub;
  562. __ftrace_graph_entry = ftrace_graph_entry_stub;
  563. ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
  564. unregister_pm_notifier(&ftrace_suspend_notifier);
  565. unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
  566. out:
  567. mutex_unlock(&ftrace_lock);
  568. }