1 // SPDX-License-Identifier: GPL-2.0
3 * Infrastructure to took into function calls and returns.
4 * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
5 * Mostly borrowed from function tracer which
6 * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
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"
20 #ifdef CONFIG_DYNAMIC_FTRACE
21 #define ASSIGN_OPS_HASH(opsname, val) \
23 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
25 #define ASSIGN_OPS_HASH(opsname, val)
28 DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph);
29 int ftrace_graph_active;
31 /* Both enabled by default (can be cleared by function_graph tracer flags */
32 static bool fgraph_sleep_time = true;
34 #ifdef CONFIG_DYNAMIC_FTRACE
36 * archs can override this function if they must do something
37 * to enable hook for graph tracer.
39 int __weak ftrace_enable_ftrace_graph_caller(void)
45 * archs can override this function if they must do something
46 * to disable hook for graph tracer.
48 int __weak ftrace_disable_ftrace_graph_caller(void)
55 * ftrace_graph_stop - set to permanently disable function graph tracing
57 * In case of an error int function graph tracing, this is called
58 * to try to keep function graph tracing from causing any more harm.
59 * Usually this is pretty severe and this is called to try to at least
60 * get a warning out to the user.
62 void ftrace_graph_stop(void)
64 static_branch_enable(&kill_ftrace_graph);
67 /* Add a function return address to the trace stack on thread info.*/
69 ftrace_push_return_trace(unsigned long ret, unsigned long func,
70 unsigned long frame_pointer, unsigned long *retp)
72 unsigned long long calltime;
75 if (unlikely(ftrace_graph_is_dead()))
78 if (!current->ret_stack)
82 * We must make sure the ret_stack is tested before we read
87 /* The return trace stack is full */
88 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
89 atomic_inc(¤t->trace_overrun);
93 calltime = trace_clock_local();
95 index = ++current->curr_ret_stack;
97 current->ret_stack[index].ret = ret;
98 current->ret_stack[index].func = func;
99 current->ret_stack[index].calltime = calltime;
100 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
101 current->ret_stack[index].fp = frame_pointer;
103 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
104 current->ret_stack[index].retp = retp;
110 * Not all archs define MCOUNT_INSN_SIZE which is used to look for direct
111 * functions. But those archs currently don't support direct functions
112 * anyway, and ftrace_find_rec_direct() is just a stub for them.
113 * Define MCOUNT_INSN_SIZE to keep those archs compiling.
115 #ifndef MCOUNT_INSN_SIZE
116 /* Make sure this only works without direct calls */
117 # ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
118 # error MCOUNT_INSN_SIZE not defined with direct calls enabled
120 # define MCOUNT_INSN_SIZE 0
123 int function_graph_enter(unsigned long ret, unsigned long func,
124 unsigned long frame_pointer, unsigned long *retp)
126 struct ftrace_graph_ent trace;
128 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
130 * Skip graph tracing if the return location is served by direct trampoline,
131 * since call sequence and return addresses are unpredictable anyway.
132 * Ex: BPF trampoline may call original function and may skip frame
133 * depending on type of BPF programs attached.
135 if (ftrace_direct_func_count &&
136 ftrace_find_rec_direct(ret - MCOUNT_INSN_SIZE))
140 trace.depth = ++current->curr_ret_depth;
142 if (ftrace_push_return_trace(ret, func, frame_pointer, retp))
145 /* Only trace if the calling function expects to */
146 if (!ftrace_graph_entry(&trace))
151 current->curr_ret_stack--;
153 current->curr_ret_depth--;
157 /* Retrieve a function return address to the trace stack on thread info.*/
159 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
160 unsigned long frame_pointer)
164 index = current->curr_ret_stack;
166 if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
169 /* Might as well panic, otherwise we have no where to go */
170 *ret = (unsigned long)panic;
174 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
176 * The arch may choose to record the frame pointer used
177 * and check it here to make sure that it is what we expect it
178 * to be. If gcc does not set the place holder of the return
179 * address in the frame pointer, and does a copy instead, then
180 * the function graph trace will fail. This test detects this
183 * Currently, x86_32 with optimize for size (-Os) makes the latest
186 * Note, -mfentry does not use frame pointers, and this test
187 * is not needed if CC_USING_FENTRY is set.
189 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
191 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
192 " from func %ps return to %lx\n",
193 current->ret_stack[index].fp,
195 (void *)current->ret_stack[index].func,
196 current->ret_stack[index].ret);
197 *ret = (unsigned long)panic;
202 *ret = current->ret_stack[index].ret;
203 trace->func = current->ret_stack[index].func;
204 trace->calltime = current->ret_stack[index].calltime;
205 trace->overrun = atomic_read(¤t->trace_overrun);
206 trace->depth = current->curr_ret_depth--;
208 * We still want to trace interrupts coming in if
209 * max_depth is set to 1. Make sure the decrement is
210 * seen before ftrace_graph_return.
216 * Hibernation protection.
217 * The state of the current task is too much unstable during
218 * suspend/restore to disk. We want to protect against that.
221 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
225 case PM_HIBERNATION_PREPARE:
226 pause_graph_tracing();
229 case PM_POST_HIBERNATION:
230 unpause_graph_tracing();
236 static struct notifier_block ftrace_suspend_notifier = {
237 .notifier_call = ftrace_suspend_notifier_call,
240 /* fgraph_ret_regs is not defined without CONFIG_FUNCTION_GRAPH_RETVAL */
241 struct fgraph_ret_regs;
244 * Send the trace to the ring-buffer.
245 * @return the original return address.
247 static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs,
248 unsigned long frame_pointer)
250 struct ftrace_graph_ret trace;
253 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
254 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL
255 trace.retval = fgraph_ret_regs_return_value(ret_regs);
257 trace.rettime = trace_clock_local();
258 ftrace_graph_return(&trace);
260 * The ftrace_graph_return() may still access the current
261 * ret_stack structure, we need to make sure the update of
262 * curr_ret_stack is after that.
265 current->curr_ret_stack--;
267 if (unlikely(!ret)) {
270 /* Might as well panic. What else to do? */
271 ret = (unsigned long)panic;
278 * After all architecures have selected HAVE_FUNCTION_GRAPH_RETVAL, we can
279 * leave only ftrace_return_to_handler(ret_regs).
281 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_RETVAL
282 unsigned long ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs)
284 return __ftrace_return_to_handler(ret_regs,
285 fgraph_ret_regs_frame_pointer(ret_regs));
288 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
290 return __ftrace_return_to_handler(NULL, frame_pointer);
295 * ftrace_graph_get_ret_stack - return the entry of the shadow stack
296 * @task: The task to read the shadow stack from
297 * @idx: Index down the shadow stack
299 * Return the ret_struct on the shadow stack of the @task at the
300 * call graph at @idx starting with zero. If @idx is zero, it
301 * will return the last saved ret_stack entry. If it is greater than
302 * zero, it will return the corresponding ret_stack for the depth
303 * of saved return addresses.
305 struct ftrace_ret_stack *
306 ftrace_graph_get_ret_stack(struct task_struct *task, int idx)
308 idx = task->curr_ret_stack - idx;
310 if (idx >= 0 && idx <= task->curr_ret_stack)
311 return &task->ret_stack[idx];
317 * ftrace_graph_ret_addr - convert a potentially modified stack return address
318 * to its original value
320 * This function can be called by stack unwinding code to convert a found stack
321 * return address ('ret') to its original value, in case the function graph
322 * tracer has modified it to be 'return_to_handler'. If the address hasn't
323 * been modified, the unchanged value of 'ret' is returned.
325 * 'idx' is a state variable which should be initialized by the caller to zero
326 * before the first call.
328 * 'retp' is a pointer to the return address on the stack. It's ignored if
329 * the arch doesn't have HAVE_FUNCTION_GRAPH_RET_ADDR_PTR defined.
331 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
332 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
333 unsigned long ret, unsigned long *retp)
335 int index = task->curr_ret_stack;
338 if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
344 for (i = 0; i <= index; i++)
345 if (task->ret_stack[i].retp == retp)
346 return task->ret_stack[i].ret;
350 #else /* !HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
351 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
352 unsigned long ret, unsigned long *retp)
356 if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
359 task_idx = task->curr_ret_stack;
361 if (!task->ret_stack || task_idx < *idx)
367 return task->ret_stack[task_idx].ret;
369 #endif /* HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
371 static struct ftrace_ops graph_ops = {
372 .func = ftrace_graph_func,
373 .flags = FTRACE_OPS_FL_INITIALIZED |
375 FTRACE_OPS_GRAPH_STUB,
376 #ifdef FTRACE_GRAPH_TRAMP_ADDR
377 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
378 /* trampoline_size is only needed for dynamically allocated tramps */
380 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
383 void ftrace_graph_sleep_time_control(bool enable)
385 fgraph_sleep_time = enable;
388 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
394 * Simply points to ftrace_stub, but with the proper protocol.
395 * Defined by the linker script in linux/vmlinux.lds.h
397 extern void ftrace_stub_graph(struct ftrace_graph_ret *);
399 /* The callbacks that hook a function */
400 trace_func_graph_ret_t ftrace_graph_return = ftrace_stub_graph;
401 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
402 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
404 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
405 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
409 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
410 struct task_struct *g, *t;
412 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
414 kmalloc_array(FTRACE_RETFUNC_DEPTH,
415 sizeof(struct ftrace_ret_stack),
417 if (!ret_stack_list[i]) {
426 for_each_process_thread(g, t) {
432 if (t->ret_stack == NULL) {
433 atomic_set(&t->trace_overrun, 0);
434 t->curr_ret_stack = -1;
435 t->curr_ret_depth = -1;
436 /* Make sure the tasks see the -1 first: */
438 t->ret_stack = ret_stack_list[start++];
445 for (i = start; i < end; i++)
446 kfree(ret_stack_list[i]);
451 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
452 struct task_struct *prev,
453 struct task_struct *next,
454 unsigned int prev_state)
456 unsigned long long timestamp;
460 * Does the user want to count the time a function was asleep.
461 * If so, do not update the time stamps.
463 if (fgraph_sleep_time)
466 timestamp = trace_clock_local();
468 prev->ftrace_timestamp = timestamp;
470 /* only process tasks that we timestamped */
471 if (!next->ftrace_timestamp)
475 * Update all the counters in next to make up for the
476 * time next was sleeping.
478 timestamp -= next->ftrace_timestamp;
480 for (index = next->curr_ret_stack; index >= 0; index--)
481 next->ret_stack[index].calltime += timestamp;
484 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
486 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
488 return __ftrace_graph_entry(trace);
492 * The function graph tracer should only trace the functions defined
493 * by set_ftrace_filter and set_ftrace_notrace. If another function
494 * tracer ops is registered, the graph tracer requires testing the
495 * function against the global ops, and not just trace any function
496 * that any ftrace_ops registered.
498 void update_function_graph_func(void)
500 struct ftrace_ops *op;
501 bool do_test = false;
504 * The graph and global ops share the same set of functions
505 * to test. If any other ops is on the list, then
506 * the graph tracing needs to test if its the function
509 do_for_each_ftrace_op(op, ftrace_ops_list) {
510 if (op != &global_ops && op != &graph_ops &&
511 op != &ftrace_list_end) {
513 /* in double loop, break out with goto */
516 } while_for_each_ftrace_op(op);
519 ftrace_graph_entry = ftrace_graph_entry_test;
521 ftrace_graph_entry = __ftrace_graph_entry;
524 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
527 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
529 atomic_set(&t->trace_overrun, 0);
530 t->ftrace_timestamp = 0;
531 /* make curr_ret_stack visible before we add the ret_stack */
533 t->ret_stack = ret_stack;
537 * Allocate a return stack for the idle task. May be the first
538 * time through, or it may be done by CPU hotplug online.
540 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
542 t->curr_ret_stack = -1;
543 t->curr_ret_depth = -1;
545 * The idle task has no parent, it either has its own
546 * stack or no stack at all.
549 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
551 if (ftrace_graph_active) {
552 struct ftrace_ret_stack *ret_stack;
554 ret_stack = per_cpu(idle_ret_stack, cpu);
557 kmalloc_array(FTRACE_RETFUNC_DEPTH,
558 sizeof(struct ftrace_ret_stack),
562 per_cpu(idle_ret_stack, cpu) = ret_stack;
564 graph_init_task(t, ret_stack);
568 /* Allocate a return stack for newly created task */
569 void ftrace_graph_init_task(struct task_struct *t)
571 /* Make sure we do not use the parent ret_stack */
573 t->curr_ret_stack = -1;
574 t->curr_ret_depth = -1;
576 if (ftrace_graph_active) {
577 struct ftrace_ret_stack *ret_stack;
579 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
580 sizeof(struct ftrace_ret_stack),
584 graph_init_task(t, ret_stack);
588 void ftrace_graph_exit_task(struct task_struct *t)
590 struct ftrace_ret_stack *ret_stack = t->ret_stack;
593 /* NULL must become visible to IRQs before we free it: */
599 /* Allocate a return stack for each task */
600 static int start_graph_tracing(void)
602 struct ftrace_ret_stack **ret_stack_list;
605 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
606 sizeof(struct ftrace_ret_stack *),
612 /* The cpu_boot init_task->ret_stack will never be freed */
613 for_each_online_cpu(cpu) {
614 if (!idle_task(cpu)->ret_stack)
615 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
619 ret = alloc_retstack_tasklist(ret_stack_list);
620 } while (ret == -EAGAIN);
623 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
625 pr_info("ftrace_graph: Couldn't activate tracepoint"
626 " probe to kernel_sched_switch\n");
629 kfree(ret_stack_list);
633 int register_ftrace_graph(struct fgraph_ops *gops)
637 mutex_lock(&ftrace_lock);
639 /* we currently allow only one tracer registered at a time */
640 if (ftrace_graph_active) {
645 register_pm_notifier(&ftrace_suspend_notifier);
647 ftrace_graph_active++;
648 ret = start_graph_tracing();
650 ftrace_graph_active--;
654 ftrace_graph_return = gops->retfunc;
657 * Update the indirect function to the entryfunc, and the
658 * function that gets called to the entry_test first. Then
659 * call the update fgraph entry function to determine if
660 * the entryfunc should be called directly or not.
662 __ftrace_graph_entry = gops->entryfunc;
663 ftrace_graph_entry = ftrace_graph_entry_test;
664 update_function_graph_func();
666 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
668 mutex_unlock(&ftrace_lock);
672 void unregister_ftrace_graph(struct fgraph_ops *gops)
674 mutex_lock(&ftrace_lock);
676 if (unlikely(!ftrace_graph_active))
679 ftrace_graph_active--;
680 ftrace_graph_return = ftrace_stub_graph;
681 ftrace_graph_entry = ftrace_graph_entry_stub;
682 __ftrace_graph_entry = ftrace_graph_entry_stub;
683 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
684 unregister_pm_notifier(&ftrace_suspend_notifier);
685 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
688 mutex_unlock(&ftrace_lock);