blob: 22061d38fc00fd65661a824099dba1eebfa9fe7d [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/**
Ingo Molnarf2cc0202021-03-23 18:49:35 +010045 * ftrace_graph_stop - set to permanently disable function graph tracing
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -050046 *
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
Steven Rostedt (VMware)d2ccbcc2020-01-02 21:56:44 -050099/*
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.
104 */
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
109# endif
110# define MCOUNT_INSN_SIZE 0
111#endif
112
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -0500113int function_graph_enter(unsigned long ret, unsigned long func,
114 unsigned long frame_pointer, unsigned long *retp)
115{
116 struct ftrace_graph_ent trace;
117
Steven Rostedt (VMware)0c0593b2021-10-08 11:13:31 +0200118#ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
Alexei Starovoitovff205762019-12-08 16:01:12 -0800119 /*
120 * Skip graph tracing if the return location is served by direct trampoline,
Ingo Molnarf2cc0202021-03-23 18:49:35 +0100121 * since call sequence and return addresses are unpredictable anyway.
Alexei Starovoitovff205762019-12-08 16:01:12 -0800122 * Ex: BPF trampoline may call original function and may skip frame
123 * depending on type of BPF programs attached.
124 */
125 if (ftrace_direct_func_count &&
126 ftrace_find_rec_direct(ret - MCOUNT_INSN_SIZE))
127 return -EBUSY;
Steven Rostedt (VMware)0c0593b2021-10-08 11:13:31 +0200128#endif
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -0500129 trace.func = func;
130 trace.depth = ++current->curr_ret_depth;
131
132 if (ftrace_push_return_trace(ret, func, frame_pointer, retp))
133 goto out;
134
135 /* Only trace if the calling function expects to */
136 if (!ftrace_graph_entry(&trace))
137 goto out_ret;
138
139 return 0;
140 out_ret:
141 current->curr_ret_stack--;
142 out:
143 current->curr_ret_depth--;
144 return -EBUSY;
145}
146
147/* Retrieve a function return address to the trace stack on thread info.*/
148static void
149ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
150 unsigned long frame_pointer)
151{
152 int index;
153
154 index = current->curr_ret_stack;
155
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -0500156 if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
157 ftrace_graph_stop();
158 WARN_ON(1);
159 /* Might as well panic, otherwise we have no where to go */
160 *ret = (unsigned long)panic;
161 return;
162 }
163
164#ifdef HAVE_FUNCTION_GRAPH_FP_TEST
165 /*
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
171 * case.
172 *
173 * Currently, x86_32 with optimize for size (-Os) makes the latest
174 * gcc do the above.
175 *
176 * Note, -mfentry does not use frame pointers, and this test
177 * is not needed if CC_USING_FENTRY is set.
178 */
179 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
180 ftrace_graph_stop();
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,
184 frame_pointer,
185 (void *)current->ret_stack[index].func,
186 current->ret_stack[index].ret);
187 *ret = (unsigned long)panic;
188 return;
189 }
190#endif
191
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(&current->trace_overrun);
196 trace->depth = current->curr_ret_depth--;
197 /*
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.
201 */
202 barrier();
203}
204
205/*
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500206 * 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.
209 */
210static int
211ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
212 void *unused)
213{
214 switch (state) {
215 case PM_HIBERNATION_PREPARE:
216 pause_graph_tracing();
217 break;
218
219 case PM_POST_HIBERNATION:
220 unpause_graph_tracing();
221 break;
222 }
223 return NOTIFY_DONE;
224}
225
226static struct notifier_block ftrace_suspend_notifier = {
227 .notifier_call = ftrace_suspend_notifier_call,
228};
229
230/*
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -0500231 * Send the trace to the ring-buffer.
232 * @return the original return address.
233 */
234unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
235{
236 struct ftrace_graph_ret trace;
237 unsigned long ret;
238
239 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
240 trace.rettime = trace_clock_local();
241 ftrace_graph_return(&trace);
242 /*
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.
246 */
247 barrier();
248 current->curr_ret_stack--;
Steven Rostedt (VMware)d864a3c2018-11-12 15:21:22 -0500249
250 if (unlikely(!ret)) {
251 ftrace_graph_stop();
252 WARN_ON(1);
253 /* Might as well panic. What else to do? */
254 ret = (unsigned long)panic;
255 }
256
257 return ret;
258}
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500259
Steven Rostedt (VMware)45fe4392018-12-07 12:25:52 -0500260/**
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
264 *
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.
270 */
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500271struct ftrace_ret_stack *
272ftrace_graph_get_ret_stack(struct task_struct *task, int idx)
273{
Steven Rostedt (VMware)e8d086d2018-12-18 15:50:02 -0500274 idx = task->curr_ret_stack - idx;
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500275
276 if (idx >= 0 && idx <= task->curr_ret_stack)
Steven Rostedt (VMware)e8d086d2018-12-18 15:50:02 -0500277 return &task->ret_stack[idx];
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500278
279 return NULL;
280}
281
Steven Rostedt (VMware)76b42b62018-11-18 18:36:19 -0500282/**
283 * ftrace_graph_ret_addr - convert a potentially modified stack return address
284 * to its original value
285 *
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.
290 *
291 * 'idx' is a state variable which should be initialized by the caller to zero
292 * before the first call.
293 *
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.
296 */
297#ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
298unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
299 unsigned long ret, unsigned long *retp)
300{
301 int index = task->curr_ret_stack;
302 int i;
303
Naveen N. Raoa3db31f2019-09-05 23:50:28 +0530304 if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
Steven Rostedt (VMware)76b42b62018-11-18 18:36:19 -0500305 return ret;
306
307 if (index < 0)
308 return ret;
309
310 for (i = 0; i <= index; i++)
311 if (task->ret_stack[i].retp == retp)
312 return task->ret_stack[i].ret;
313
314 return ret;
315}
316#else /* !HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
317unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
318 unsigned long ret, unsigned long *retp)
319{
320 int task_idx;
321
Naveen N. Raoa3db31f2019-09-05 23:50:28 +0530322 if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler))
Steven Rostedt (VMware)76b42b62018-11-18 18:36:19 -0500323 return ret;
324
325 task_idx = task->curr_ret_stack;
326
327 if (!task->ret_stack || task_idx < *idx)
328 return ret;
329
330 task_idx -= *idx;
331 (*idx)++;
332
333 return task->ret_stack[task_idx].ret;
334}
335#endif /* HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
336
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500337static struct ftrace_ops graph_ops = {
Steven Rostedt (VMware)0c0593b2021-10-08 11:13:31 +0200338 .func = ftrace_graph_func,
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -0500339 .flags = FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500340 FTRACE_OPS_FL_PID |
Steven Rostedt (VMware)0c0593b2021-10-08 11:13:31 +0200341 FTRACE_OPS_GRAPH_STUB,
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500342#ifdef FTRACE_GRAPH_TRAMP_ADDR
343 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
344 /* trampoline_size is only needed for dynamically allocated tramps */
345#endif
346 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
347};
348
349void ftrace_graph_sleep_time_control(bool enable)
350{
351 fgraph_sleep_time = enable;
352}
353
354int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
355{
356 return 0;
357}
358
Steven Rostedt (VMware)b83b43f2019-10-15 09:00:55 -0400359/*
360 * Simply points to ftrace_stub, but with the proper protocol.
361 * Defined by the linker script in linux/vmlinux.lds.h
362 */
Steven Rostedt (VMware)46f94692019-11-18 10:41:29 -0500363extern void ftrace_stub_graph(struct ftrace_graph_ret *);
Steven Rostedt (VMware)b83b43f2019-10-15 09:00:55 -0400364
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500365/* The callbacks that hook a function */
Steven Rostedt (VMware)46f94692019-11-18 10:41:29 -0500366trace_func_graph_ret_t ftrace_graph_return = ftrace_stub_graph;
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500367trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
368static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
369
370/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
371static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
372{
373 int i;
374 int ret = 0;
375 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
376 struct task_struct *g, *t;
377
378 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
379 ret_stack_list[i] =
380 kmalloc_array(FTRACE_RETFUNC_DEPTH,
381 sizeof(struct ftrace_ret_stack),
382 GFP_KERNEL);
383 if (!ret_stack_list[i]) {
384 start = 0;
385 end = i;
386 ret = -ENOMEM;
387 goto free;
388 }
389 }
390
Davidlohr Bueso40d14da2020-09-06 18:33:26 -0700391 rcu_read_lock();
392 for_each_process_thread(g, t) {
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500393 if (start == end) {
394 ret = -EAGAIN;
395 goto unlock;
396 }
397
398 if (t->ret_stack == NULL) {
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500399 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: */
403 smp_wmb();
404 t->ret_stack = ret_stack_list[start++];
405 }
Davidlohr Bueso40d14da2020-09-06 18:33:26 -0700406 }
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500407
408unlock:
Davidlohr Bueso40d14da2020-09-06 18:33:26 -0700409 rcu_read_unlock();
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500410free:
411 for (i = start; i < end; i++)
412 kfree(ret_stack_list[i]);
413 return ret;
414}
415
416static void
417ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
418 struct task_struct *prev, struct task_struct *next)
419{
420 unsigned long long timestamp;
421 int index;
422
423 /*
424 * Does the user want to count the time a function was asleep.
425 * If so, do not update the time stamps.
426 */
427 if (fgraph_sleep_time)
428 return;
429
430 timestamp = trace_clock_local();
431
432 prev->ftrace_timestamp = timestamp;
433
434 /* only process tasks that we timestamped */
435 if (!next->ftrace_timestamp)
436 return;
437
438 /*
439 * Update all the counters in next to make up for the
440 * time next was sleeping.
441 */
442 timestamp -= next->ftrace_timestamp;
443
444 for (index = next->curr_ret_stack; index >= 0; index--)
445 next->ret_stack[index].calltime += timestamp;
446}
447
448static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
449{
450 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
451 return 0;
452 return __ftrace_graph_entry(trace);
453}
454
455/*
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.
461 */
462void update_function_graph_func(void)
463{
464 struct ftrace_ops *op;
465 bool do_test = false;
466
467 /*
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
471 * it should call.
472 */
473 do_for_each_ftrace_op(op, ftrace_ops_list) {
474 if (op != &global_ops && op != &graph_ops &&
475 op != &ftrace_list_end) {
476 do_test = true;
477 /* in double loop, break out with goto */
478 goto out;
479 }
480 } while_for_each_ftrace_op(op);
481 out:
482 if (do_test)
483 ftrace_graph_entry = ftrace_graph_entry_test;
484 else
485 ftrace_graph_entry = __ftrace_graph_entry;
486}
487
488static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
489
490static void
491graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
492{
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500493 atomic_set(&t->trace_overrun, 0);
494 t->ftrace_timestamp = 0;
495 /* make curr_ret_stack visible before we add the ret_stack */
496 smp_wmb();
497 t->ret_stack = ret_stack;
498}
499
500/*
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.
503 */
504void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
505{
506 t->curr_ret_stack = -1;
507 t->curr_ret_depth = -1;
508 /*
509 * The idle task has no parent, it either has its own
510 * stack or no stack at all.
511 */
512 if (t->ret_stack)
513 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
514
515 if (ftrace_graph_active) {
516 struct ftrace_ret_stack *ret_stack;
517
518 ret_stack = per_cpu(idle_ret_stack, cpu);
519 if (!ret_stack) {
520 ret_stack =
521 kmalloc_array(FTRACE_RETFUNC_DEPTH,
522 sizeof(struct ftrace_ret_stack),
523 GFP_KERNEL);
524 if (!ret_stack)
525 return;
526 per_cpu(idle_ret_stack, cpu) = ret_stack;
527 }
528 graph_init_task(t, ret_stack);
529 }
530}
531
532/* Allocate a return stack for newly created task */
533void ftrace_graph_init_task(struct task_struct *t)
534{
535 /* Make sure we do not use the parent ret_stack */
536 t->ret_stack = NULL;
537 t->curr_ret_stack = -1;
538 t->curr_ret_depth = -1;
539
540 if (ftrace_graph_active) {
541 struct ftrace_ret_stack *ret_stack;
542
543 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
544 sizeof(struct ftrace_ret_stack),
545 GFP_KERNEL);
546 if (!ret_stack)
547 return;
548 graph_init_task(t, ret_stack);
549 }
550}
551
552void ftrace_graph_exit_task(struct task_struct *t)
553{
554 struct ftrace_ret_stack *ret_stack = t->ret_stack;
555
556 t->ret_stack = NULL;
557 /* NULL must become visible to IRQs before we free it: */
558 barrier();
559
560 kfree(ret_stack);
561}
562
563/* Allocate a return stack for each task */
564static int start_graph_tracing(void)
565{
566 struct ftrace_ret_stack **ret_stack_list;
567 int ret, cpu;
568
569 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
570 sizeof(struct ftrace_ret_stack *),
571 GFP_KERNEL);
572
573 if (!ret_stack_list)
574 return -ENOMEM;
575
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);
580 }
581
582 do {
583 ret = alloc_retstack_tasklist(ret_stack_list);
584 } while (ret == -EAGAIN);
585
586 if (!ret) {
587 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
588 if (ret)
589 pr_info("ftrace_graph: Couldn't activate tracepoint"
590 " probe to kernel_sched_switch\n");
591 }
592
593 kfree(ret_stack_list);
594 return ret;
595}
596
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500597int register_ftrace_graph(struct fgraph_ops *gops)
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500598{
599 int ret = 0;
600
601 mutex_lock(&ftrace_lock);
602
603 /* we currently allow only one tracer registered at a time */
604 if (ftrace_graph_active) {
605 ret = -EBUSY;
606 goto out;
607 }
608
609 register_pm_notifier(&ftrace_suspend_notifier);
610
611 ftrace_graph_active++;
612 ret = start_graph_tracing();
613 if (ret) {
614 ftrace_graph_active--;
615 goto out;
616 }
617
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500618 ftrace_graph_return = gops->retfunc;
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500619
620 /*
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.
625 */
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500626 __ftrace_graph_entry = gops->entryfunc;
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500627 ftrace_graph_entry = ftrace_graph_entry_test;
628 update_function_graph_func();
629
630 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
631out:
632 mutex_unlock(&ftrace_lock);
633 return ret;
634}
635
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500636void unregister_ftrace_graph(struct fgraph_ops *gops)
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500637{
638 mutex_lock(&ftrace_lock);
639
640 if (unlikely(!ftrace_graph_active))
641 goto out;
642
643 ftrace_graph_active--;
Steven Rostedt (VMware)46f94692019-11-18 10:41:29 -0500644 ftrace_graph_return = ftrace_stub_graph;
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500645 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);
650
651 out:
652 mutex_unlock(&ftrace_lock);
653}