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/suspend.h>
11 #include <linux/ftrace.h>
12 #include <linux/slab.h>
14 #include <trace/events/sched.h>
16 #include "ftrace_internal.h"
18 #ifdef CONFIG_DYNAMIC_FTRACE
19 #define ASSIGN_OPS_HASH(opsname, val) \
21 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
23 #define ASSIGN_OPS_HASH(opsname, val)
26 static bool kill_ftrace_graph;
27 int ftrace_graph_active;
29 /* Both enabled by default (can be cleared by function_graph tracer flags */
30 static bool fgraph_sleep_time = true;
33 * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
35 * ftrace_graph_stop() is called when a severe error is detected in
36 * the function graph tracing. This function is called by the critical
37 * paths of function graph to keep those paths from doing any more harm.
39 bool ftrace_graph_is_dead(void)
41 return kill_ftrace_graph;
45 * ftrace_graph_stop - set to permanently disable function graph tracing
47 * In case of an error int function graph tracing, this is called
48 * to try to keep function graph tracing from causing any more harm.
49 * Usually this is pretty severe and this is called to try to at least
50 * get a warning out to the user.
52 void ftrace_graph_stop(void)
54 kill_ftrace_graph = true;
57 /* Add a function return address to the trace stack on thread info.*/
59 ftrace_push_return_trace(unsigned long ret, unsigned long func,
60 unsigned long frame_pointer, unsigned long *retp)
62 unsigned long long calltime;
65 if (unlikely(ftrace_graph_is_dead()))
68 if (!current->ret_stack)
72 * We must make sure the ret_stack is tested before we read
77 /* The return trace stack is full */
78 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
79 atomic_inc(¤t->trace_overrun);
83 calltime = trace_clock_local();
85 index = ++current->curr_ret_stack;
87 current->ret_stack[index].ret = ret;
88 current->ret_stack[index].func = func;
89 current->ret_stack[index].calltime = calltime;
90 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
91 current->ret_stack[index].fp = frame_pointer;
93 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
94 current->ret_stack[index].retp = retp;
100 * Not all archs define MCOUNT_INSN_SIZE which is used to look for direct
101 * functions. But those archs currently don't support direct functions
102 * anyway, and ftrace_find_rec_direct() is just a stub for them.
103 * Define MCOUNT_INSN_SIZE to keep those archs compiling.
105 #ifndef MCOUNT_INSN_SIZE
106 /* Make sure this only works without direct calls */
107 # ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
108 # error MCOUNT_INSN_SIZE not defined with direct calls enabled
110 # define MCOUNT_INSN_SIZE 0
113 int function_graph_enter(unsigned long ret, unsigned long func,
114 unsigned long frame_pointer, unsigned long *retp)
116 struct ftrace_graph_ent trace;
118 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
120 * Skip graph tracing if the return location is served by direct trampoline,
121 * since call sequence and return addresses are unpredictable anyway.
122 * Ex: BPF trampoline may call original function and may skip frame
123 * depending on type of BPF programs attached.
125 if (ftrace_direct_func_count &&
126 ftrace_find_rec_direct(ret - MCOUNT_INSN_SIZE))
130 trace.depth = ++current->curr_ret_depth;
132 if (ftrace_push_return_trace(ret, func, frame_pointer, retp))
135 /* Only trace if the calling function expects to */
136 if (!ftrace_graph_entry(&trace))
141 current->curr_ret_stack--;
143 current->curr_ret_depth--;
147 /* Retrieve a function return address to the trace stack on thread info.*/
149 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
150 unsigned long frame_pointer)
154 index = current->curr_ret_stack;
156 if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
159 /* Might as well panic, otherwise we have no where to go */
160 *ret = (unsigned long)panic;
164 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
166 * The arch may choose to record the frame pointer used
167 * and check it here to make sure that it is what we expect it
168 * to be. If gcc does not set the place holder of the return
169 * address in the frame pointer, and does a copy instead, then
170 * the function graph trace will fail. This test detects this
173 * Currently, x86_32 with optimize for size (-Os) makes the latest
176 * Note, -mfentry does not use frame pointers, and this test
177 * is not needed if CC_USING_FENTRY is set.
179 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
181 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
182 " from func %ps return to %lx\n",
183 current->ret_stack[index].fp,
185 (void *)current->ret_stack[index].func,
186 current->ret_stack[index].ret);
187 *ret = (unsigned long)panic;
192 *ret = current->ret_stack[index].ret;
193 trace->func = current->ret_stack[index].func;
194 trace->calltime = current->ret_stack[index].calltime;
195 trace->overrun = atomic_read(¤t->trace_overrun);
196 trace->depth = current->curr_ret_depth--;
198 * We still want to trace interrupts coming in if
199 * max_depth is set to 1. Make sure the decrement is
200 * seen before ftrace_graph_return.
206 * Hibernation protection.
207 * The state of the current task is too much unstable during
208 * suspend/restore to disk. We want to protect against that.
211 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
215 case PM_HIBERNATION_PREPARE:
216 pause_graph_tracing();
219 case PM_POST_HIBERNATION:
220 unpause_graph_tracing();
226 static struct notifier_block ftrace_suspend_notifier = {
227 .notifier_call = ftrace_suspend_notifier_call,
231 * Send the trace to the ring-buffer.
232 * @return the original return address.
234 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
236 struct ftrace_graph_ret trace;
239 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
240 trace.rettime = trace_clock_local();
241 ftrace_graph_return(&trace);
243 * The ftrace_graph_return() may still access the current
244 * ret_stack structure, we need to make sure the update of
245 * curr_ret_stack is after that.
248 current->curr_ret_stack--;
250 if (unlikely(!ret)) {
253 /* Might as well panic. What else to do? */
254 ret = (unsigned long)panic;
261 * ftrace_graph_get_ret_stack - return the entry of the shadow stack
262 * @task: The task to read the shadow stack from
263 * @idx: Index down the shadow stack
265 * Return the ret_struct on the shadow stack of the @task at the
266 * call graph at @idx starting with zero. If @idx is zero, it
267 * will return the last saved ret_stack entry. If it is greater than
268 * zero, it will return the corresponding ret_stack for the depth
269 * of saved return addresses.
271 struct ftrace_ret_stack *
272 ftrace_graph_get_ret_stack(struct task_struct *task, int idx)
274 idx = task->curr_ret_stack - idx;
276 if (idx >= 0 && idx <= task->curr_ret_stack)
277 return &task->ret_stack[idx];
283 * ftrace_graph_ret_addr - convert a potentially modified stack return address
284 * to its original value
286 * This function can be called by stack unwinding code to convert a found stack
287 * return address ('ret') to its original value, in case the function graph
288 * tracer has modified it to be 'return_to_handler'. If the address hasn't
289 * been modified, the unchanged value of 'ret' is returned.
291 * 'idx' is a state variable which should be initialized by the caller to zero
292 * before the first call.
294 * 'retp' is a pointer to the return address on the stack. It's ignored if
295 * the arch doesn't have HAVE_FUNCTION_GRAPH_RET_ADDR_PTR defined.
297 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
298 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
299 unsigned long ret, unsigned long *retp)
301 int index = task->curr_ret_stack;
304 if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
310 for (i = 0; i <= index; i++)
311 if (task->ret_stack[i].retp == retp)
312 return task->ret_stack[i].ret;
316 #else /* !HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
317 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
318 unsigned long ret, unsigned long *retp)
322 if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
325 task_idx = task->curr_ret_stack;
327 if (!task->ret_stack || task_idx < *idx)
333 return task->ret_stack[task_idx].ret;
335 #endif /* HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
337 static struct ftrace_ops graph_ops = {
338 .func = ftrace_graph_func,
339 .flags = FTRACE_OPS_FL_INITIALIZED |
341 FTRACE_OPS_GRAPH_STUB,
342 #ifdef FTRACE_GRAPH_TRAMP_ADDR
343 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
344 /* trampoline_size is only needed for dynamically allocated tramps */
346 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
349 void ftrace_graph_sleep_time_control(bool enable)
351 fgraph_sleep_time = enable;
354 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
360 * Simply points to ftrace_stub, but with the proper protocol.
361 * Defined by the linker script in linux/vmlinux.lds.h
363 extern void ftrace_stub_graph(struct ftrace_graph_ret *);
365 /* The callbacks that hook a function */
366 trace_func_graph_ret_t ftrace_graph_return = ftrace_stub_graph;
367 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
368 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
370 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
371 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
375 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
376 struct task_struct *g, *t;
378 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
380 kmalloc_array(FTRACE_RETFUNC_DEPTH,
381 sizeof(struct ftrace_ret_stack),
383 if (!ret_stack_list[i]) {
392 for_each_process_thread(g, t) {
398 if (t->ret_stack == NULL) {
399 atomic_set(&t->trace_overrun, 0);
400 t->curr_ret_stack = -1;
401 t->curr_ret_depth = -1;
402 /* Make sure the tasks see the -1 first: */
404 t->ret_stack = ret_stack_list[start++];
411 for (i = start; i < end; i++)
412 kfree(ret_stack_list[i]);
417 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
418 struct task_struct *prev, struct task_struct *next)
420 unsigned long long timestamp;
424 * Does the user want to count the time a function was asleep.
425 * If so, do not update the time stamps.
427 if (fgraph_sleep_time)
430 timestamp = trace_clock_local();
432 prev->ftrace_timestamp = timestamp;
434 /* only process tasks that we timestamped */
435 if (!next->ftrace_timestamp)
439 * Update all the counters in next to make up for the
440 * time next was sleeping.
442 timestamp -= next->ftrace_timestamp;
444 for (index = next->curr_ret_stack; index >= 0; index--)
445 next->ret_stack[index].calltime += timestamp;
448 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
450 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
452 return __ftrace_graph_entry(trace);
456 * The function graph tracer should only trace the functions defined
457 * by set_ftrace_filter and set_ftrace_notrace. If another function
458 * tracer ops is registered, the graph tracer requires testing the
459 * function against the global ops, and not just trace any function
460 * that any ftrace_ops registered.
462 void update_function_graph_func(void)
464 struct ftrace_ops *op;
465 bool do_test = false;
468 * The graph and global ops share the same set of functions
469 * to test. If any other ops is on the list, then
470 * the graph tracing needs to test if its the function
473 do_for_each_ftrace_op(op, ftrace_ops_list) {
474 if (op != &global_ops && op != &graph_ops &&
475 op != &ftrace_list_end) {
477 /* in double loop, break out with goto */
480 } while_for_each_ftrace_op(op);
483 ftrace_graph_entry = ftrace_graph_entry_test;
485 ftrace_graph_entry = __ftrace_graph_entry;
488 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
491 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
493 atomic_set(&t->trace_overrun, 0);
494 t->ftrace_timestamp = 0;
495 /* make curr_ret_stack visible before we add the ret_stack */
497 t->ret_stack = ret_stack;
501 * Allocate a return stack for the idle task. May be the first
502 * time through, or it may be done by CPU hotplug online.
504 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
506 t->curr_ret_stack = -1;
507 t->curr_ret_depth = -1;
509 * The idle task has no parent, it either has its own
510 * stack or no stack at all.
513 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
515 if (ftrace_graph_active) {
516 struct ftrace_ret_stack *ret_stack;
518 ret_stack = per_cpu(idle_ret_stack, cpu);
521 kmalloc_array(FTRACE_RETFUNC_DEPTH,
522 sizeof(struct ftrace_ret_stack),
526 per_cpu(idle_ret_stack, cpu) = ret_stack;
528 graph_init_task(t, ret_stack);
532 /* Allocate a return stack for newly created task */
533 void ftrace_graph_init_task(struct task_struct *t)
535 /* Make sure we do not use the parent ret_stack */
537 t->curr_ret_stack = -1;
538 t->curr_ret_depth = -1;
540 if (ftrace_graph_active) {
541 struct ftrace_ret_stack *ret_stack;
543 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
544 sizeof(struct ftrace_ret_stack),
548 graph_init_task(t, ret_stack);
552 void ftrace_graph_exit_task(struct task_struct *t)
554 struct ftrace_ret_stack *ret_stack = t->ret_stack;
557 /* NULL must become visible to IRQs before we free it: */
563 /* Allocate a return stack for each task */
564 static int start_graph_tracing(void)
566 struct ftrace_ret_stack **ret_stack_list;
569 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
570 sizeof(struct ftrace_ret_stack *),
576 /* The cpu_boot init_task->ret_stack will never be freed */
577 for_each_online_cpu(cpu) {
578 if (!idle_task(cpu)->ret_stack)
579 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
583 ret = alloc_retstack_tasklist(ret_stack_list);
584 } while (ret == -EAGAIN);
587 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
589 pr_info("ftrace_graph: Couldn't activate tracepoint"
590 " probe to kernel_sched_switch\n");
593 kfree(ret_stack_list);
597 int register_ftrace_graph(struct fgraph_ops *gops)
601 mutex_lock(&ftrace_lock);
603 /* we currently allow only one tracer registered at a time */
604 if (ftrace_graph_active) {
609 register_pm_notifier(&ftrace_suspend_notifier);
611 ftrace_graph_active++;
612 ret = start_graph_tracing();
614 ftrace_graph_active--;
618 ftrace_graph_return = gops->retfunc;
621 * Update the indirect function to the entryfunc, and the
622 * function that gets called to the entry_test first. Then
623 * call the update fgraph entry function to determine if
624 * the entryfunc should be called directly or not.
626 __ftrace_graph_entry = gops->entryfunc;
627 ftrace_graph_entry = ftrace_graph_entry_test;
628 update_function_graph_func();
630 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
632 mutex_unlock(&ftrace_lock);
636 void unregister_ftrace_graph(struct fgraph_ops *gops)
638 mutex_lock(&ftrace_lock);
640 if (unlikely(!ftrace_graph_active))
643 ftrace_graph_active--;
644 ftrace_graph_return = ftrace_stub_graph;
645 ftrace_graph_entry = ftrace_graph_entry_stub;
646 __ftrace_graph_entry = ftrace_graph_entry_stub;
647 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
648 unregister_pm_notifier(&ftrace_suspend_notifier);
649 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
652 mutex_unlock(&ftrace_lock);