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 trace.depth = ++current->curr_ret_depth;
107 if (ftrace_push_return_trace(ret, func, frame_pointer, retp))
110 /* Only trace if the calling function expects to */
111 if (!ftrace_graph_entry(&trace))
116 current->curr_ret_stack--;
118 current->curr_ret_depth--;
122 /* Retrieve a function return address to the trace stack on thread info.*/
124 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
125 unsigned long frame_pointer)
129 index = current->curr_ret_stack;
131 if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
134 /* Might as well panic, otherwise we have no where to go */
135 *ret = (unsigned long)panic;
139 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
141 * The arch may choose to record the frame pointer used
142 * and check it here to make sure that it is what we expect it
143 * to be. If gcc does not set the place holder of the return
144 * address in the frame pointer, and does a copy instead, then
145 * the function graph trace will fail. This test detects this
148 * Currently, x86_32 with optimize for size (-Os) makes the latest
151 * Note, -mfentry does not use frame pointers, and this test
152 * is not needed if CC_USING_FENTRY is set.
154 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
156 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
157 " from func %ps return to %lx\n",
158 current->ret_stack[index].fp,
160 (void *)current->ret_stack[index].func,
161 current->ret_stack[index].ret);
162 *ret = (unsigned long)panic;
167 *ret = current->ret_stack[index].ret;
168 trace->func = current->ret_stack[index].func;
169 trace->calltime = current->ret_stack[index].calltime;
170 trace->overrun = atomic_read(¤t->trace_overrun);
171 trace->depth = current->curr_ret_depth--;
173 * We still want to trace interrupts coming in if
174 * max_depth is set to 1. Make sure the decrement is
175 * seen before ftrace_graph_return.
181 * Hibernation protection.
182 * The state of the current task is too much unstable during
183 * suspend/restore to disk. We want to protect against that.
186 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
190 case PM_HIBERNATION_PREPARE:
191 pause_graph_tracing();
194 case PM_POST_HIBERNATION:
195 unpause_graph_tracing();
201 static struct notifier_block ftrace_suspend_notifier = {
202 .notifier_call = ftrace_suspend_notifier_call,
206 * Send the trace to the ring-buffer.
207 * @return the original return address.
209 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
211 struct ftrace_graph_ret trace;
214 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
215 trace.rettime = trace_clock_local();
216 ftrace_graph_return(&trace);
218 * The ftrace_graph_return() may still access the current
219 * ret_stack structure, we need to make sure the update of
220 * curr_ret_stack is after that.
223 current->curr_ret_stack--;
225 if (unlikely(!ret)) {
228 /* Might as well panic. What else to do? */
229 ret = (unsigned long)panic;
235 static struct ftrace_ops graph_ops = {
237 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
238 FTRACE_OPS_FL_INITIALIZED |
241 #ifdef FTRACE_GRAPH_TRAMP_ADDR
242 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
243 /* trampoline_size is only needed for dynamically allocated tramps */
245 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
248 void ftrace_graph_sleep_time_control(bool enable)
250 fgraph_sleep_time = enable;
253 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
258 /* The callbacks that hook a function */
259 trace_func_graph_ret_t ftrace_graph_return =
260 (trace_func_graph_ret_t)ftrace_stub;
261 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
262 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
264 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
265 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
269 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
270 struct task_struct *g, *t;
272 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
274 kmalloc_array(FTRACE_RETFUNC_DEPTH,
275 sizeof(struct ftrace_ret_stack),
277 if (!ret_stack_list[i]) {
285 read_lock(&tasklist_lock);
286 do_each_thread(g, t) {
292 if (t->ret_stack == NULL) {
293 atomic_set(&t->tracing_graph_pause, 0);
294 atomic_set(&t->trace_overrun, 0);
295 t->curr_ret_stack = -1;
296 t->curr_ret_depth = -1;
297 /* Make sure the tasks see the -1 first: */
299 t->ret_stack = ret_stack_list[start++];
301 } while_each_thread(g, t);
304 read_unlock(&tasklist_lock);
306 for (i = start; i < end; i++)
307 kfree(ret_stack_list[i]);
312 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
313 struct task_struct *prev, struct task_struct *next)
315 unsigned long long timestamp;
319 * Does the user want to count the time a function was asleep.
320 * If so, do not update the time stamps.
322 if (fgraph_sleep_time)
325 timestamp = trace_clock_local();
327 prev->ftrace_timestamp = timestamp;
329 /* only process tasks that we timestamped */
330 if (!next->ftrace_timestamp)
334 * Update all the counters in next to make up for the
335 * time next was sleeping.
337 timestamp -= next->ftrace_timestamp;
339 for (index = next->curr_ret_stack; index >= 0; index--)
340 next->ret_stack[index].calltime += timestamp;
343 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
345 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
347 return __ftrace_graph_entry(trace);
351 * The function graph tracer should only trace the functions defined
352 * by set_ftrace_filter and set_ftrace_notrace. If another function
353 * tracer ops is registered, the graph tracer requires testing the
354 * function against the global ops, and not just trace any function
355 * that any ftrace_ops registered.
357 void update_function_graph_func(void)
359 struct ftrace_ops *op;
360 bool do_test = false;
363 * The graph and global ops share the same set of functions
364 * to test. If any other ops is on the list, then
365 * the graph tracing needs to test if its the function
368 do_for_each_ftrace_op(op, ftrace_ops_list) {
369 if (op != &global_ops && op != &graph_ops &&
370 op != &ftrace_list_end) {
372 /* in double loop, break out with goto */
375 } while_for_each_ftrace_op(op);
378 ftrace_graph_entry = ftrace_graph_entry_test;
380 ftrace_graph_entry = __ftrace_graph_entry;
383 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
386 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
388 atomic_set(&t->tracing_graph_pause, 0);
389 atomic_set(&t->trace_overrun, 0);
390 t->ftrace_timestamp = 0;
391 /* make curr_ret_stack visible before we add the ret_stack */
393 t->ret_stack = ret_stack;
397 * Allocate a return stack for the idle task. May be the first
398 * time through, or it may be done by CPU hotplug online.
400 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
402 t->curr_ret_stack = -1;
403 t->curr_ret_depth = -1;
405 * The idle task has no parent, it either has its own
406 * stack or no stack at all.
409 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
411 if (ftrace_graph_active) {
412 struct ftrace_ret_stack *ret_stack;
414 ret_stack = per_cpu(idle_ret_stack, cpu);
417 kmalloc_array(FTRACE_RETFUNC_DEPTH,
418 sizeof(struct ftrace_ret_stack),
422 per_cpu(idle_ret_stack, cpu) = ret_stack;
424 graph_init_task(t, ret_stack);
428 /* Allocate a return stack for newly created task */
429 void ftrace_graph_init_task(struct task_struct *t)
431 /* Make sure we do not use the parent ret_stack */
433 t->curr_ret_stack = -1;
434 t->curr_ret_depth = -1;
436 if (ftrace_graph_active) {
437 struct ftrace_ret_stack *ret_stack;
439 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
440 sizeof(struct ftrace_ret_stack),
444 graph_init_task(t, ret_stack);
448 void ftrace_graph_exit_task(struct task_struct *t)
450 struct ftrace_ret_stack *ret_stack = t->ret_stack;
453 /* NULL must become visible to IRQs before we free it: */
459 /* Allocate a return stack for each task */
460 static int start_graph_tracing(void)
462 struct ftrace_ret_stack **ret_stack_list;
465 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
466 sizeof(struct ftrace_ret_stack *),
472 /* The cpu_boot init_task->ret_stack will never be freed */
473 for_each_online_cpu(cpu) {
474 if (!idle_task(cpu)->ret_stack)
475 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
479 ret = alloc_retstack_tasklist(ret_stack_list);
480 } while (ret == -EAGAIN);
483 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
485 pr_info("ftrace_graph: Couldn't activate tracepoint"
486 " probe to kernel_sched_switch\n");
489 kfree(ret_stack_list);
493 int register_ftrace_graph(struct fgraph_ops *gops)
497 mutex_lock(&ftrace_lock);
499 /* we currently allow only one tracer registered at a time */
500 if (ftrace_graph_active) {
505 register_pm_notifier(&ftrace_suspend_notifier);
507 ftrace_graph_active++;
508 ret = start_graph_tracing();
510 ftrace_graph_active--;
514 ftrace_graph_return = gops->retfunc;
517 * Update the indirect function to the entryfunc, and the
518 * function that gets called to the entry_test first. Then
519 * call the update fgraph entry function to determine if
520 * the entryfunc should be called directly or not.
522 __ftrace_graph_entry = gops->entryfunc;
523 ftrace_graph_entry = ftrace_graph_entry_test;
524 update_function_graph_func();
526 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
528 mutex_unlock(&ftrace_lock);
532 void unregister_ftrace_graph(struct fgraph_ops *gops)
534 mutex_lock(&ftrace_lock);
536 if (unlikely(!ftrace_graph_active))
539 ftrace_graph_active--;
540 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
541 ftrace_graph_entry = ftrace_graph_entry_stub;
542 __ftrace_graph_entry = ftrace_graph_entry_stub;
543 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
544 unregister_pm_notifier(&ftrace_suspend_notifier);
545 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
548 mutex_unlock(&ftrace_lock);