1 // SPDX-License-Identifier: GPL-2.0
3 * Infrastructure to took into function calls and returns.
5 * Mostly borrowed from function tracer which
8 * Highly modified by Steven Rostedt (VMware).
10 #include <linux/jump_label.h>
11 #include <linux/suspend.h>
12 #include <linux/ftrace.h>
13 #include <linux/slab.h>
15 #include <trace/events/sched.h>
17 #include "ftrace_internal.h"
19 #ifdef CONFIG_DYNAMIC_FTRACE
20 #define ASSIGN_OPS_HASH(opsname, val) \
22 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
24 #define ASSIGN_OPS_HASH(opsname, val)
27 DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph);
28 int ftrace_graph_active;
30 /* Both enabled by default (can be cleared by function_graph tracer flags */
31 static bool fgraph_sleep_time = true;
34 * ftrace_graph_stop - set to permanently disable function graph tracing
36 * In case of an error int function graph tracing, this is called
37 * to try to keep function graph tracing from causing any more harm.
38 * Usually this is pretty severe and this is called to try to at least
39 * get a warning out to the user.
41 void ftrace_graph_stop(void)
43 static_branch_enable(&kill_ftrace_graph);
46 /* Add a function return address to the trace stack on thread info.*/
48 ftrace_push_return_trace(unsigned long ret, unsigned long func,
49 unsigned long frame_pointer, unsigned long *retp)
51 unsigned long long calltime;
54 if (unlikely(ftrace_graph_is_dead()))
57 if (!current->ret_stack)
61 * We must make sure the ret_stack is tested before we read
66 /* The return trace stack is full */
67 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
68 atomic_inc(¤t->trace_overrun);
72 calltime = trace_clock_local();
74 index = ++current->curr_ret_stack;
76 current->ret_stack[index].ret = ret;
77 current->ret_stack[index].func = func;
78 current->ret_stack[index].calltime = calltime;
79 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
80 current->ret_stack[index].fp = frame_pointer;
82 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
83 current->ret_stack[index].retp = retp;
89 * Not all archs define MCOUNT_INSN_SIZE which is used to look for direct
90 * functions. But those archs currently don't support direct functions
91 * anyway, and ftrace_find_rec_direct() is just a stub for them.
92 * Define MCOUNT_INSN_SIZE to keep those archs compiling.
94 #ifndef MCOUNT_INSN_SIZE
95 /* Make sure this only works without direct calls */
96 # ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
97 # error MCOUNT_INSN_SIZE not defined with direct calls enabled
99 # define MCOUNT_INSN_SIZE 0
102 int function_graph_enter(unsigned long ret, unsigned long func,
103 unsigned long frame_pointer, unsigned long *retp)
105 struct ftrace_graph_ent trace;
107 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
109 * Skip graph tracing if the return location is served by direct trampoline,
110 * since call sequence and return addresses are unpredictable anyway.
111 * Ex: BPF trampoline may call original function and may skip frame
112 * depending on type of BPF programs attached.
114 if (ftrace_direct_func_count &&
115 ftrace_find_rec_direct(ret - MCOUNT_INSN_SIZE))
119 trace.depth = ++current->curr_ret_depth;
121 if (ftrace_push_return_trace(ret, func, frame_pointer, retp))
124 /* Only trace if the calling function expects to */
125 if (!ftrace_graph_entry(&trace))
130 current->curr_ret_stack--;
132 current->curr_ret_depth--;
136 /* Retrieve a function return address to the trace stack on thread info.*/
138 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
139 unsigned long frame_pointer)
143 index = current->curr_ret_stack;
145 if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
148 /* Might as well panic, otherwise we have no where to go */
149 *ret = (unsigned long)panic;
153 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
155 * The arch may choose to record the frame pointer used
156 * and check it here to make sure that it is what we expect it
157 * to be. If gcc does not set the place holder of the return
158 * address in the frame pointer, and does a copy instead, then
159 * the function graph trace will fail. This test detects this
162 * Currently, x86_32 with optimize for size (-Os) makes the latest
165 * Note, -mfentry does not use frame pointers, and this test
166 * is not needed if CC_USING_FENTRY is set.
168 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
170 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
171 " from func %ps return to %lx\n",
172 current->ret_stack[index].fp,
174 (void *)current->ret_stack[index].func,
175 current->ret_stack[index].ret);
176 *ret = (unsigned long)panic;
181 *ret = current->ret_stack[index].ret;
182 trace->func = current->ret_stack[index].func;
183 trace->calltime = current->ret_stack[index].calltime;
184 trace->overrun = atomic_read(¤t->trace_overrun);
185 trace->depth = current->curr_ret_depth--;
187 * We still want to trace interrupts coming in if
188 * max_depth is set to 1. Make sure the decrement is
189 * seen before ftrace_graph_return.
195 * Hibernation protection.
196 * The state of the current task is too much unstable during
197 * suspend/restore to disk. We want to protect against that.
200 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
204 case PM_HIBERNATION_PREPARE:
205 pause_graph_tracing();
208 case PM_POST_HIBERNATION:
209 unpause_graph_tracing();
215 static struct notifier_block ftrace_suspend_notifier = {
216 .notifier_call = ftrace_suspend_notifier_call,
220 * Send the trace to the ring-buffer.
221 * @return the original return address.
223 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
225 struct ftrace_graph_ret trace;
228 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
229 trace.rettime = trace_clock_local();
230 ftrace_graph_return(&trace);
232 * The ftrace_graph_return() may still access the current
233 * ret_stack structure, we need to make sure the update of
234 * curr_ret_stack is after that.
237 current->curr_ret_stack--;
239 if (unlikely(!ret)) {
242 /* Might as well panic. What else to do? */
243 ret = (unsigned long)panic;
250 * ftrace_graph_get_ret_stack - return the entry of the shadow stack
251 * @task: The task to read the shadow stack from
252 * @idx: Index down the shadow stack
254 * Return the ret_struct on the shadow stack of the @task at the
255 * call graph at @idx starting with zero. If @idx is zero, it
256 * will return the last saved ret_stack entry. If it is greater than
257 * zero, it will return the corresponding ret_stack for the depth
258 * of saved return addresses.
260 struct ftrace_ret_stack *
261 ftrace_graph_get_ret_stack(struct task_struct *task, int idx)
263 idx = task->curr_ret_stack - idx;
265 if (idx >= 0 && idx <= task->curr_ret_stack)
266 return &task->ret_stack[idx];
272 * ftrace_graph_ret_addr - convert a potentially modified stack return address
273 * to its original value
275 * This function can be called by stack unwinding code to convert a found stack
276 * return address ('ret') to its original value, in case the function graph
277 * tracer has modified it to be 'return_to_handler'. If the address hasn't
278 * been modified, the unchanged value of 'ret' is returned.
280 * 'idx' is a state variable which should be initialized by the caller to zero
281 * before the first call.
283 * 'retp' is a pointer to the return address on the stack. It's ignored if
284 * the arch doesn't have HAVE_FUNCTION_GRAPH_RET_ADDR_PTR defined.
286 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
287 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
288 unsigned long ret, unsigned long *retp)
290 int index = task->curr_ret_stack;
293 if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
299 for (i = 0; i <= index; i++)
300 if (task->ret_stack[i].retp == retp)
301 return task->ret_stack[i].ret;
305 #else /* !HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
306 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
307 unsigned long ret, unsigned long *retp)
311 if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
314 task_idx = task->curr_ret_stack;
316 if (!task->ret_stack || task_idx < *idx)
322 return task->ret_stack[task_idx].ret;
324 #endif /* HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
326 static struct ftrace_ops graph_ops = {
327 .func = ftrace_graph_func,
328 .flags = FTRACE_OPS_FL_INITIALIZED |
330 FTRACE_OPS_GRAPH_STUB,
331 #ifdef FTRACE_GRAPH_TRAMP_ADDR
332 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
333 /* trampoline_size is only needed for dynamically allocated tramps */
335 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
338 void ftrace_graph_sleep_time_control(bool enable)
340 fgraph_sleep_time = enable;
343 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
349 * Simply points to ftrace_stub, but with the proper protocol.
350 * Defined by the linker script in linux/vmlinux.lds.h
352 extern void ftrace_stub_graph(struct ftrace_graph_ret *);
354 /* The callbacks that hook a function */
355 trace_func_graph_ret_t ftrace_graph_return = ftrace_stub_graph;
356 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
357 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
359 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
360 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
364 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
365 struct task_struct *g, *t;
367 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
369 kmalloc_array(FTRACE_RETFUNC_DEPTH,
370 sizeof(struct ftrace_ret_stack),
372 if (!ret_stack_list[i]) {
381 for_each_process_thread(g, t) {
387 if (t->ret_stack == NULL) {
388 atomic_set(&t->trace_overrun, 0);
389 t->curr_ret_stack = -1;
390 t->curr_ret_depth = -1;
391 /* Make sure the tasks see the -1 first: */
393 t->ret_stack = ret_stack_list[start++];
400 for (i = start; i < end; i++)
401 kfree(ret_stack_list[i]);
406 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
407 unsigned int prev_state,
408 struct task_struct *prev,
409 struct task_struct *next)
411 unsigned long long timestamp;
415 * Does the user want to count the time a function was asleep.
416 * If so, do not update the time stamps.
418 if (fgraph_sleep_time)
421 timestamp = trace_clock_local();
423 prev->ftrace_timestamp = timestamp;
425 /* only process tasks that we timestamped */
426 if (!next->ftrace_timestamp)
430 * Update all the counters in next to make up for the
431 * time next was sleeping.
433 timestamp -= next->ftrace_timestamp;
435 for (index = next->curr_ret_stack; index >= 0; index--)
436 next->ret_stack[index].calltime += timestamp;
439 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
441 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
443 return __ftrace_graph_entry(trace);
447 * The function graph tracer should only trace the functions defined
448 * by set_ftrace_filter and set_ftrace_notrace. If another function
449 * tracer ops is registered, the graph tracer requires testing the
450 * function against the global ops, and not just trace any function
451 * that any ftrace_ops registered.
453 void update_function_graph_func(void)
455 struct ftrace_ops *op;
456 bool do_test = false;
459 * The graph and global ops share the same set of functions
460 * to test. If any other ops is on the list, then
461 * the graph tracing needs to test if its the function
464 do_for_each_ftrace_op(op, ftrace_ops_list) {
465 if (op != &global_ops && op != &graph_ops &&
466 op != &ftrace_list_end) {
468 /* in double loop, break out with goto */
471 } while_for_each_ftrace_op(op);
474 ftrace_graph_entry = ftrace_graph_entry_test;
476 ftrace_graph_entry = __ftrace_graph_entry;
479 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
482 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
484 atomic_set(&t->trace_overrun, 0);
485 t->ftrace_timestamp = 0;
486 /* make curr_ret_stack visible before we add the ret_stack */
488 t->ret_stack = ret_stack;
492 * Allocate a return stack for the idle task. May be the first
493 * time through, or it may be done by CPU hotplug online.
495 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
497 t->curr_ret_stack = -1;
498 t->curr_ret_depth = -1;
500 * The idle task has no parent, it either has its own
501 * stack or no stack at all.
504 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
506 if (ftrace_graph_active) {
507 struct ftrace_ret_stack *ret_stack;
509 ret_stack = per_cpu(idle_ret_stack, cpu);
512 kmalloc_array(FTRACE_RETFUNC_DEPTH,
513 sizeof(struct ftrace_ret_stack),
517 per_cpu(idle_ret_stack, cpu) = ret_stack;
519 graph_init_task(t, ret_stack);
523 /* Allocate a return stack for newly created task */
524 void ftrace_graph_init_task(struct task_struct *t)
526 /* Make sure we do not use the parent ret_stack */
528 t->curr_ret_stack = -1;
529 t->curr_ret_depth = -1;
531 if (ftrace_graph_active) {
532 struct ftrace_ret_stack *ret_stack;
534 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
535 sizeof(struct ftrace_ret_stack),
539 graph_init_task(t, ret_stack);
543 void ftrace_graph_exit_task(struct task_struct *t)
545 struct ftrace_ret_stack *ret_stack = t->ret_stack;
548 /* NULL must become visible to IRQs before we free it: */
554 /* Allocate a return stack for each task */
555 static int start_graph_tracing(void)
557 struct ftrace_ret_stack **ret_stack_list;
560 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
561 sizeof(struct ftrace_ret_stack *),
567 /* The cpu_boot init_task->ret_stack will never be freed */
568 for_each_online_cpu(cpu) {
569 if (!idle_task(cpu)->ret_stack)
570 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
574 ret = alloc_retstack_tasklist(ret_stack_list);
575 } while (ret == -EAGAIN);
578 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
580 pr_info("ftrace_graph: Couldn't activate tracepoint"
581 " probe to kernel_sched_switch\n");
584 kfree(ret_stack_list);
588 int register_ftrace_graph(struct fgraph_ops *gops)
592 mutex_lock(&ftrace_lock);
594 /* we currently allow only one tracer registered at a time */
595 if (ftrace_graph_active) {
600 register_pm_notifier(&ftrace_suspend_notifier);
602 ftrace_graph_active++;
603 ret = start_graph_tracing();
605 ftrace_graph_active--;
609 ftrace_graph_return = gops->retfunc;
612 * Update the indirect function to the entryfunc, and the
613 * function that gets called to the entry_test first. Then
614 * call the update fgraph entry function to determine if
615 * the entryfunc should be called directly or not.
617 __ftrace_graph_entry = gops->entryfunc;
618 ftrace_graph_entry = ftrace_graph_entry_test;
619 update_function_graph_func();
621 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
623 mutex_unlock(&ftrace_lock);
627 void unregister_ftrace_graph(struct fgraph_ops *gops)
629 mutex_lock(&ftrace_lock);
631 if (unlikely(!ftrace_graph_active))
634 ftrace_graph_active--;
635 ftrace_graph_return = ftrace_stub_graph;
636 ftrace_graph_entry = ftrace_graph_entry_stub;
637 __ftrace_graph_entry = ftrace_graph_entry_stub;
638 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
639 unregister_pm_notifier(&ftrace_suspend_notifier);
640 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
643 mutex_unlock(&ftrace_lock);