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 tracincg
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;
99 int function_graph_enter(unsigned long ret, unsigned long func,
100 unsigned long frame_pointer, unsigned long *retp)
102 struct ftrace_graph_ent trace;
105 * Skip graph tracing if the return location is served by direct trampoline,
106 * since call sequence and return addresses is unpredicatable anymore.
107 * Ex: BPF trampoline may call original function and may skip frame
108 * depending on type of BPF programs attached.
110 if (ftrace_direct_func_count &&
111 ftrace_find_rec_direct(ret - MCOUNT_INSN_SIZE))
114 trace.depth = ++current->curr_ret_depth;
116 if (ftrace_push_return_trace(ret, func, frame_pointer, retp))
119 /* Only trace if the calling function expects to */
120 if (!ftrace_graph_entry(&trace))
125 current->curr_ret_stack--;
127 current->curr_ret_depth--;
131 /* Retrieve a function return address to the trace stack on thread info.*/
133 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
134 unsigned long frame_pointer)
138 index = current->curr_ret_stack;
140 if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
143 /* Might as well panic, otherwise we have no where to go */
144 *ret = (unsigned long)panic;
148 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
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
157 * Currently, x86_32 with optimize for size (-Os) makes the latest
160 * Note, -mfentry does not use frame pointers, and this test
161 * is not needed if CC_USING_FENTRY is set.
163 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
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,
169 (void *)current->ret_stack[index].func,
170 current->ret_stack[index].ret);
171 *ret = (unsigned long)panic;
176 *ret = current->ret_stack[index].ret;
177 trace->func = current->ret_stack[index].func;
178 trace->calltime = current->ret_stack[index].calltime;
179 trace->overrun = atomic_read(¤t->trace_overrun);
180 trace->depth = current->curr_ret_depth--;
182 * We still want to trace interrupts coming in if
183 * max_depth is set to 1. Make sure the decrement is
184 * seen before ftrace_graph_return.
190 * Hibernation protection.
191 * The state of the current task is too much unstable during
192 * suspend/restore to disk. We want to protect against that.
195 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
199 case PM_HIBERNATION_PREPARE:
200 pause_graph_tracing();
203 case PM_POST_HIBERNATION:
204 unpause_graph_tracing();
210 static struct notifier_block ftrace_suspend_notifier = {
211 .notifier_call = ftrace_suspend_notifier_call,
215 * Send the trace to the ring-buffer.
216 * @return the original return address.
218 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
220 struct ftrace_graph_ret trace;
223 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
224 trace.rettime = trace_clock_local();
225 ftrace_graph_return(&trace);
227 * The ftrace_graph_return() may still access the current
228 * ret_stack structure, we need to make sure the update of
229 * curr_ret_stack is after that.
232 current->curr_ret_stack--;
234 if (unlikely(!ret)) {
237 /* Might as well panic. What else to do? */
238 ret = (unsigned long)panic;
245 * ftrace_graph_get_ret_stack - return the entry of the shadow stack
246 * @task: The task to read the shadow stack from
247 * @idx: Index down the shadow stack
249 * Return the ret_struct on the shadow stack of the @task at the
250 * call graph at @idx starting with zero. If @idx is zero, it
251 * will return the last saved ret_stack entry. If it is greater than
252 * zero, it will return the corresponding ret_stack for the depth
253 * of saved return addresses.
255 struct ftrace_ret_stack *
256 ftrace_graph_get_ret_stack(struct task_struct *task, int idx)
258 idx = task->curr_ret_stack - idx;
260 if (idx >= 0 && idx <= task->curr_ret_stack)
261 return &task->ret_stack[idx];
267 * ftrace_graph_ret_addr - convert a potentially modified stack return address
268 * to its original value
270 * This function can be called by stack unwinding code to convert a found stack
271 * return address ('ret') to its original value, in case the function graph
272 * tracer has modified it to be 'return_to_handler'. If the address hasn't
273 * been modified, the unchanged value of 'ret' is returned.
275 * 'idx' is a state variable which should be initialized by the caller to zero
276 * before the first call.
278 * 'retp' is a pointer to the return address on the stack. It's ignored if
279 * the arch doesn't have HAVE_FUNCTION_GRAPH_RET_ADDR_PTR defined.
281 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
282 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
283 unsigned long ret, unsigned long *retp)
285 int index = task->curr_ret_stack;
288 if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
294 for (i = 0; i <= index; i++)
295 if (task->ret_stack[i].retp == retp)
296 return task->ret_stack[i].ret;
300 #else /* !HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
301 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
302 unsigned long ret, unsigned long *retp)
306 if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
309 task_idx = task->curr_ret_stack;
311 if (!task->ret_stack || task_idx < *idx)
317 return task->ret_stack[task_idx].ret;
319 #endif /* HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
321 static struct ftrace_ops graph_ops = {
323 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
324 FTRACE_OPS_FL_INITIALIZED |
327 #ifdef FTRACE_GRAPH_TRAMP_ADDR
328 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
329 /* trampoline_size is only needed for dynamically allocated tramps */
331 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
334 void ftrace_graph_sleep_time_control(bool enable)
336 fgraph_sleep_time = enable;
339 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
345 * Simply points to ftrace_stub, but with the proper protocol.
346 * Defined by the linker script in linux/vmlinux.lds.h
348 extern void ftrace_stub_graph(struct ftrace_graph_ret *);
350 /* The callbacks that hook a function */
351 trace_func_graph_ret_t ftrace_graph_return = ftrace_stub_graph;
352 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
353 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
355 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
356 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
360 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
361 struct task_struct *g, *t;
363 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
365 kmalloc_array(FTRACE_RETFUNC_DEPTH,
366 sizeof(struct ftrace_ret_stack),
368 if (!ret_stack_list[i]) {
376 read_lock(&tasklist_lock);
377 do_each_thread(g, t) {
383 if (t->ret_stack == NULL) {
384 atomic_set(&t->tracing_graph_pause, 0);
385 atomic_set(&t->trace_overrun, 0);
386 t->curr_ret_stack = -1;
387 t->curr_ret_depth = -1;
388 /* Make sure the tasks see the -1 first: */
390 t->ret_stack = ret_stack_list[start++];
392 } while_each_thread(g, t);
395 read_unlock(&tasklist_lock);
397 for (i = start; i < end; i++)
398 kfree(ret_stack_list[i]);
403 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
404 struct task_struct *prev, struct task_struct *next)
406 unsigned long long timestamp;
410 * Does the user want to count the time a function was asleep.
411 * If so, do not update the time stamps.
413 if (fgraph_sleep_time)
416 timestamp = trace_clock_local();
418 prev->ftrace_timestamp = timestamp;
420 /* only process tasks that we timestamped */
421 if (!next->ftrace_timestamp)
425 * Update all the counters in next to make up for the
426 * time next was sleeping.
428 timestamp -= next->ftrace_timestamp;
430 for (index = next->curr_ret_stack; index >= 0; index--)
431 next->ret_stack[index].calltime += timestamp;
434 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
436 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
438 return __ftrace_graph_entry(trace);
442 * The function graph tracer should only trace the functions defined
443 * by set_ftrace_filter and set_ftrace_notrace. If another function
444 * tracer ops is registered, the graph tracer requires testing the
445 * function against the global ops, and not just trace any function
446 * that any ftrace_ops registered.
448 void update_function_graph_func(void)
450 struct ftrace_ops *op;
451 bool do_test = false;
454 * The graph and global ops share the same set of functions
455 * to test. If any other ops is on the list, then
456 * the graph tracing needs to test if its the function
459 do_for_each_ftrace_op(op, ftrace_ops_list) {
460 if (op != &global_ops && op != &graph_ops &&
461 op != &ftrace_list_end) {
463 /* in double loop, break out with goto */
466 } while_for_each_ftrace_op(op);
469 ftrace_graph_entry = ftrace_graph_entry_test;
471 ftrace_graph_entry = __ftrace_graph_entry;
474 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
477 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
479 atomic_set(&t->tracing_graph_pause, 0);
480 atomic_set(&t->trace_overrun, 0);
481 t->ftrace_timestamp = 0;
482 /* make curr_ret_stack visible before we add the ret_stack */
484 t->ret_stack = ret_stack;
488 * Allocate a return stack for the idle task. May be the first
489 * time through, or it may be done by CPU hotplug online.
491 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
493 t->curr_ret_stack = -1;
494 t->curr_ret_depth = -1;
496 * The idle task has no parent, it either has its own
497 * stack or no stack at all.
500 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
502 if (ftrace_graph_active) {
503 struct ftrace_ret_stack *ret_stack;
505 ret_stack = per_cpu(idle_ret_stack, cpu);
508 kmalloc_array(FTRACE_RETFUNC_DEPTH,
509 sizeof(struct ftrace_ret_stack),
513 per_cpu(idle_ret_stack, cpu) = ret_stack;
515 graph_init_task(t, ret_stack);
519 /* Allocate a return stack for newly created task */
520 void ftrace_graph_init_task(struct task_struct *t)
522 /* Make sure we do not use the parent ret_stack */
524 t->curr_ret_stack = -1;
525 t->curr_ret_depth = -1;
527 if (ftrace_graph_active) {
528 struct ftrace_ret_stack *ret_stack;
530 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
531 sizeof(struct ftrace_ret_stack),
535 graph_init_task(t, ret_stack);
539 void ftrace_graph_exit_task(struct task_struct *t)
541 struct ftrace_ret_stack *ret_stack = t->ret_stack;
544 /* NULL must become visible to IRQs before we free it: */
550 /* Allocate a return stack for each task */
551 static int start_graph_tracing(void)
553 struct ftrace_ret_stack **ret_stack_list;
556 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
557 sizeof(struct ftrace_ret_stack *),
563 /* The cpu_boot init_task->ret_stack will never be freed */
564 for_each_online_cpu(cpu) {
565 if (!idle_task(cpu)->ret_stack)
566 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
570 ret = alloc_retstack_tasklist(ret_stack_list);
571 } while (ret == -EAGAIN);
574 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
576 pr_info("ftrace_graph: Couldn't activate tracepoint"
577 " probe to kernel_sched_switch\n");
580 kfree(ret_stack_list);
584 int register_ftrace_graph(struct fgraph_ops *gops)
588 mutex_lock(&ftrace_lock);
590 /* we currently allow only one tracer registered at a time */
591 if (ftrace_graph_active) {
596 register_pm_notifier(&ftrace_suspend_notifier);
598 ftrace_graph_active++;
599 ret = start_graph_tracing();
601 ftrace_graph_active--;
605 ftrace_graph_return = gops->retfunc;
608 * Update the indirect function to the entryfunc, and the
609 * function that gets called to the entry_test first. Then
610 * call the update fgraph entry function to determine if
611 * the entryfunc should be called directly or not.
613 __ftrace_graph_entry = gops->entryfunc;
614 ftrace_graph_entry = ftrace_graph_entry_test;
615 update_function_graph_func();
617 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
619 mutex_unlock(&ftrace_lock);
623 void unregister_ftrace_graph(struct fgraph_ops *gops)
625 mutex_lock(&ftrace_lock);
627 if (unlikely(!ftrace_graph_active))
630 ftrace_graph_active--;
631 ftrace_graph_return = ftrace_stub_graph;
632 ftrace_graph_entry = ftrace_graph_entry_stub;
633 __ftrace_graph_entry = ftrace_graph_entry_stub;
634 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
635 unregister_pm_notifier(&ftrace_suspend_notifier);
636 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
639 mutex_unlock(&ftrace_lock);