blob: a3704ec8b59905276b2933bb7d2337c3f51e00af [file] [log] [blame]
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -05001// SPDX-License-Identifier: GPL-2.0
2/*
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>
7 *
8 * Highly modified by Steven Rostedt (VMware).
9 */
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -050010#include <linux/suspend.h>
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -050011#include <linux/ftrace.h>
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -050012#include <linux/slab.h>
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -050013
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -050014#include <trace/events/sched.h>
15
16#include "ftrace_internal.h"
17
18#ifdef CONFIG_DYNAMIC_FTRACE
19#define ASSIGN_OPS_HASH(opsname, val) \
20 .func_hash = val, \
21 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
22#else
23#define ASSIGN_OPS_HASH(opsname, val)
24#endif
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -050025
26static bool kill_ftrace_graph;
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -050027int ftrace_graph_active;
28
29/* Both enabled by default (can be cleared by function_graph tracer flags */
30static bool fgraph_sleep_time = true;
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -050031
32/**
33 * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
34 *
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.
38 */
39bool ftrace_graph_is_dead(void)
40{
41 return kill_ftrace_graph;
42}
43
44/**
45 * ftrace_graph_stop - set to permanently disable function graph tracincg
46 *
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.
51 */
52void ftrace_graph_stop(void)
53{
54 kill_ftrace_graph = true;
55}
56
57/* Add a function return address to the trace stack on thread info.*/
58static int
59ftrace_push_return_trace(unsigned long ret, unsigned long func,
60 unsigned long frame_pointer, unsigned long *retp)
61{
62 unsigned long long calltime;
63 int index;
64
65 if (unlikely(ftrace_graph_is_dead()))
66 return -EBUSY;
67
68 if (!current->ret_stack)
69 return -EBUSY;
70
71 /*
72 * We must make sure the ret_stack is tested before we read
73 * anything else.
74 */
75 smp_rmb();
76
77 /* The return trace stack is full */
78 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
79 atomic_inc(&current->trace_overrun);
80 return -EBUSY;
81 }
82
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -050083 calltime = trace_clock_local();
84
85 index = ++current->curr_ret_stack;
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -050086 barrier();
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;
92#endif
93#ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
94 current->ret_stack[index].retp = retp;
95#endif
96 return 0;
97}
98
99int function_graph_enter(unsigned long ret, unsigned long func,
100 unsigned long frame_pointer, unsigned long *retp)
101{
102 struct ftrace_graph_ent trace;
103
104 trace.func = func;
105 trace.depth = ++current->curr_ret_depth;
106
107 if (ftrace_push_return_trace(ret, func, frame_pointer, retp))
108 goto out;
109
110 /* Only trace if the calling function expects to */
111 if (!ftrace_graph_entry(&trace))
112 goto out_ret;
113
114 return 0;
115 out_ret:
116 current->curr_ret_stack--;
117 out:
118 current->curr_ret_depth--;
119 return -EBUSY;
120}
121
122/* Retrieve a function return address to the trace stack on thread info.*/
123static void
124ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
125 unsigned long frame_pointer)
126{
127 int index;
128
129 index = current->curr_ret_stack;
130
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -0500131 if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
132 ftrace_graph_stop();
133 WARN_ON(1);
134 /* Might as well panic, otherwise we have no where to go */
135 *ret = (unsigned long)panic;
136 return;
137 }
138
139#ifdef HAVE_FUNCTION_GRAPH_FP_TEST
140 /*
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
146 * case.
147 *
148 * Currently, x86_32 with optimize for size (-Os) makes the latest
149 * gcc do the above.
150 *
151 * Note, -mfentry does not use frame pointers, and this test
152 * is not needed if CC_USING_FENTRY is set.
153 */
154 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
155 ftrace_graph_stop();
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,
159 frame_pointer,
160 (void *)current->ret_stack[index].func,
161 current->ret_stack[index].ret);
162 *ret = (unsigned long)panic;
163 return;
164 }
165#endif
166
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(&current->trace_overrun);
171 trace->depth = current->curr_ret_depth--;
172 /*
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.
176 */
177 barrier();
178}
179
180/*
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500181 * 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.
184 */
185static int
186ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
187 void *unused)
188{
189 switch (state) {
190 case PM_HIBERNATION_PREPARE:
191 pause_graph_tracing();
192 break;
193
194 case PM_POST_HIBERNATION:
195 unpause_graph_tracing();
196 break;
197 }
198 return NOTIFY_DONE;
199}
200
201static struct notifier_block ftrace_suspend_notifier = {
202 .notifier_call = ftrace_suspend_notifier_call,
203};
204
205/*
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -0500206 * Send the trace to the ring-buffer.
207 * @return the original return address.
208 */
209unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
210{
211 struct ftrace_graph_ret trace;
212 unsigned long ret;
213
214 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
215 trace.rettime = trace_clock_local();
216 ftrace_graph_return(&trace);
217 /*
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.
221 */
222 barrier();
223 current->curr_ret_stack--;
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -0500224
225 if (unlikely(!ret)) {
226 ftrace_graph_stop();
227 WARN_ON(1);
228 /* Might as well panic. What else to do? */
229 ret = (unsigned long)panic;
230 }
231
232 return ret;
233}
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500234
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500235struct ftrace_ret_stack *
236ftrace_graph_get_ret_stack(struct task_struct *task, int idx)
237{
238 idx = current->curr_ret_stack - idx;
239
240 if (idx >= 0 && idx <= task->curr_ret_stack)
241 return &current->ret_stack[idx];
242
243 return NULL;
244}
245
Steven Rostedt (VMware)76b42b62018-11-18 18:36:19 -0500246/**
247 * ftrace_graph_ret_addr - convert a potentially modified stack return address
248 * to its original value
249 *
250 * This function can be called by stack unwinding code to convert a found stack
251 * return address ('ret') to its original value, in case the function graph
252 * tracer has modified it to be 'return_to_handler'. If the address hasn't
253 * been modified, the unchanged value of 'ret' is returned.
254 *
255 * 'idx' is a state variable which should be initialized by the caller to zero
256 * before the first call.
257 *
258 * 'retp' is a pointer to the return address on the stack. It's ignored if
259 * the arch doesn't have HAVE_FUNCTION_GRAPH_RET_ADDR_PTR defined.
260 */
261#ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
262unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
263 unsigned long ret, unsigned long *retp)
264{
265 int index = task->curr_ret_stack;
266 int i;
267
268 if (ret != (unsigned long)return_to_handler)
269 return ret;
270
271 if (index < 0)
272 return ret;
273
274 for (i = 0; i <= index; i++)
275 if (task->ret_stack[i].retp == retp)
276 return task->ret_stack[i].ret;
277
278 return ret;
279}
280#else /* !HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
281unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
282 unsigned long ret, unsigned long *retp)
283{
284 int task_idx;
285
286 if (ret != (unsigned long)return_to_handler)
287 return ret;
288
289 task_idx = task->curr_ret_stack;
290
291 if (!task->ret_stack || task_idx < *idx)
292 return ret;
293
294 task_idx -= *idx;
295 (*idx)++;
296
297 return task->ret_stack[task_idx].ret;
298}
299#endif /* HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
300
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500301static struct ftrace_ops graph_ops = {
302 .func = ftrace_stub,
303 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
304 FTRACE_OPS_FL_INITIALIZED |
305 FTRACE_OPS_FL_PID |
306 FTRACE_OPS_FL_STUB,
307#ifdef FTRACE_GRAPH_TRAMP_ADDR
308 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
309 /* trampoline_size is only needed for dynamically allocated tramps */
310#endif
311 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
312};
313
314void ftrace_graph_sleep_time_control(bool enable)
315{
316 fgraph_sleep_time = enable;
317}
318
319int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
320{
321 return 0;
322}
323
324/* The callbacks that hook a function */
325trace_func_graph_ret_t ftrace_graph_return =
326 (trace_func_graph_ret_t)ftrace_stub;
327trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
328static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
329
330/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
331static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
332{
333 int i;
334 int ret = 0;
335 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
336 struct task_struct *g, *t;
337
338 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
339 ret_stack_list[i] =
340 kmalloc_array(FTRACE_RETFUNC_DEPTH,
341 sizeof(struct ftrace_ret_stack),
342 GFP_KERNEL);
343 if (!ret_stack_list[i]) {
344 start = 0;
345 end = i;
346 ret = -ENOMEM;
347 goto free;
348 }
349 }
350
351 read_lock(&tasklist_lock);
352 do_each_thread(g, t) {
353 if (start == end) {
354 ret = -EAGAIN;
355 goto unlock;
356 }
357
358 if (t->ret_stack == NULL) {
359 atomic_set(&t->tracing_graph_pause, 0);
360 atomic_set(&t->trace_overrun, 0);
361 t->curr_ret_stack = -1;
362 t->curr_ret_depth = -1;
363 /* Make sure the tasks see the -1 first: */
364 smp_wmb();
365 t->ret_stack = ret_stack_list[start++];
366 }
367 } while_each_thread(g, t);
368
369unlock:
370 read_unlock(&tasklist_lock);
371free:
372 for (i = start; i < end; i++)
373 kfree(ret_stack_list[i]);
374 return ret;
375}
376
377static void
378ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
379 struct task_struct *prev, struct task_struct *next)
380{
381 unsigned long long timestamp;
382 int index;
383
384 /*
385 * Does the user want to count the time a function was asleep.
386 * If so, do not update the time stamps.
387 */
388 if (fgraph_sleep_time)
389 return;
390
391 timestamp = trace_clock_local();
392
393 prev->ftrace_timestamp = timestamp;
394
395 /* only process tasks that we timestamped */
396 if (!next->ftrace_timestamp)
397 return;
398
399 /*
400 * Update all the counters in next to make up for the
401 * time next was sleeping.
402 */
403 timestamp -= next->ftrace_timestamp;
404
405 for (index = next->curr_ret_stack; index >= 0; index--)
406 next->ret_stack[index].calltime += timestamp;
407}
408
409static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
410{
411 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
412 return 0;
413 return __ftrace_graph_entry(trace);
414}
415
416/*
417 * The function graph tracer should only trace the functions defined
418 * by set_ftrace_filter and set_ftrace_notrace. If another function
419 * tracer ops is registered, the graph tracer requires testing the
420 * function against the global ops, and not just trace any function
421 * that any ftrace_ops registered.
422 */
423void update_function_graph_func(void)
424{
425 struct ftrace_ops *op;
426 bool do_test = false;
427
428 /*
429 * The graph and global ops share the same set of functions
430 * to test. If any other ops is on the list, then
431 * the graph tracing needs to test if its the function
432 * it should call.
433 */
434 do_for_each_ftrace_op(op, ftrace_ops_list) {
435 if (op != &global_ops && op != &graph_ops &&
436 op != &ftrace_list_end) {
437 do_test = true;
438 /* in double loop, break out with goto */
439 goto out;
440 }
441 } while_for_each_ftrace_op(op);
442 out:
443 if (do_test)
444 ftrace_graph_entry = ftrace_graph_entry_test;
445 else
446 ftrace_graph_entry = __ftrace_graph_entry;
447}
448
449static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
450
451static void
452graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
453{
454 atomic_set(&t->tracing_graph_pause, 0);
455 atomic_set(&t->trace_overrun, 0);
456 t->ftrace_timestamp = 0;
457 /* make curr_ret_stack visible before we add the ret_stack */
458 smp_wmb();
459 t->ret_stack = ret_stack;
460}
461
462/*
463 * Allocate a return stack for the idle task. May be the first
464 * time through, or it may be done by CPU hotplug online.
465 */
466void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
467{
468 t->curr_ret_stack = -1;
469 t->curr_ret_depth = -1;
470 /*
471 * The idle task has no parent, it either has its own
472 * stack or no stack at all.
473 */
474 if (t->ret_stack)
475 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
476
477 if (ftrace_graph_active) {
478 struct ftrace_ret_stack *ret_stack;
479
480 ret_stack = per_cpu(idle_ret_stack, cpu);
481 if (!ret_stack) {
482 ret_stack =
483 kmalloc_array(FTRACE_RETFUNC_DEPTH,
484 sizeof(struct ftrace_ret_stack),
485 GFP_KERNEL);
486 if (!ret_stack)
487 return;
488 per_cpu(idle_ret_stack, cpu) = ret_stack;
489 }
490 graph_init_task(t, ret_stack);
491 }
492}
493
494/* Allocate a return stack for newly created task */
495void ftrace_graph_init_task(struct task_struct *t)
496{
497 /* Make sure we do not use the parent ret_stack */
498 t->ret_stack = NULL;
499 t->curr_ret_stack = -1;
500 t->curr_ret_depth = -1;
501
502 if (ftrace_graph_active) {
503 struct ftrace_ret_stack *ret_stack;
504
505 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
506 sizeof(struct ftrace_ret_stack),
507 GFP_KERNEL);
508 if (!ret_stack)
509 return;
510 graph_init_task(t, ret_stack);
511 }
512}
513
514void ftrace_graph_exit_task(struct task_struct *t)
515{
516 struct ftrace_ret_stack *ret_stack = t->ret_stack;
517
518 t->ret_stack = NULL;
519 /* NULL must become visible to IRQs before we free it: */
520 barrier();
521
522 kfree(ret_stack);
523}
524
525/* Allocate a return stack for each task */
526static int start_graph_tracing(void)
527{
528 struct ftrace_ret_stack **ret_stack_list;
529 int ret, cpu;
530
531 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
532 sizeof(struct ftrace_ret_stack *),
533 GFP_KERNEL);
534
535 if (!ret_stack_list)
536 return -ENOMEM;
537
538 /* The cpu_boot init_task->ret_stack will never be freed */
539 for_each_online_cpu(cpu) {
540 if (!idle_task(cpu)->ret_stack)
541 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
542 }
543
544 do {
545 ret = alloc_retstack_tasklist(ret_stack_list);
546 } while (ret == -EAGAIN);
547
548 if (!ret) {
549 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
550 if (ret)
551 pr_info("ftrace_graph: Couldn't activate tracepoint"
552 " probe to kernel_sched_switch\n");
553 }
554
555 kfree(ret_stack_list);
556 return ret;
557}
558
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500559int register_ftrace_graph(struct fgraph_ops *gops)
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500560{
561 int ret = 0;
562
563 mutex_lock(&ftrace_lock);
564
565 /* we currently allow only one tracer registered at a time */
566 if (ftrace_graph_active) {
567 ret = -EBUSY;
568 goto out;
569 }
570
571 register_pm_notifier(&ftrace_suspend_notifier);
572
573 ftrace_graph_active++;
574 ret = start_graph_tracing();
575 if (ret) {
576 ftrace_graph_active--;
577 goto out;
578 }
579
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500580 ftrace_graph_return = gops->retfunc;
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500581
582 /*
583 * Update the indirect function to the entryfunc, and the
584 * function that gets called to the entry_test first. Then
585 * call the update fgraph entry function to determine if
586 * the entryfunc should be called directly or not.
587 */
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500588 __ftrace_graph_entry = gops->entryfunc;
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500589 ftrace_graph_entry = ftrace_graph_entry_test;
590 update_function_graph_func();
591
592 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
593out:
594 mutex_unlock(&ftrace_lock);
595 return ret;
596}
597
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500598void unregister_ftrace_graph(struct fgraph_ops *gops)
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500599{
600 mutex_lock(&ftrace_lock);
601
602 if (unlikely(!ftrace_graph_active))
603 goto out;
604
605 ftrace_graph_active--;
606 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
607 ftrace_graph_entry = ftrace_graph_entry_stub;
608 __ftrace_graph_entry = ftrace_graph_entry_stub;
609 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
610 unregister_pm_notifier(&ftrace_suspend_notifier);
611 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
612
613 out:
614 mutex_unlock(&ftrace_lock);
615}