1313#include <linux/debugfs.h>
1414#include <linux/uaccess.h>
1515#include <linux/ftrace.h>
16+ #include <linux/slab.h>
1617#include <linux/fs.h>
1718
1819#include "trace.h"
1920
20- /* function tracing enabled */
21- static int ftrace_function_enabled ;
21+ static void tracing_start_function_trace (struct trace_array * tr );
22+ static void tracing_stop_function_trace (struct trace_array * tr );
23+ static void
24+ function_trace_call (unsigned long ip , unsigned long parent_ip ,
25+ struct ftrace_ops * op , struct pt_regs * pt_regs );
26+ static void
27+ function_stack_trace_call (unsigned long ip , unsigned long parent_ip ,
28+ struct ftrace_ops * op , struct pt_regs * pt_regs );
29+ static struct ftrace_ops trace_ops ;
30+ static struct ftrace_ops trace_stack_ops ;
31+ static struct tracer_flags func_flags ;
32+
33+ /* Our option */
34+ enum {
35+ TRACE_FUNC_OPT_STACK = 0x1 ,
36+ };
37+
38+ static int allocate_ftrace_ops (struct trace_array * tr )
39+ {
40+ struct ftrace_ops * ops ;
2241
23- static struct trace_array * func_trace ;
42+ ops = kzalloc (sizeof (* ops ), GFP_KERNEL );
43+ if (!ops )
44+ return - ENOMEM ;
2445
25- static void tracing_start_function_trace (void );
26- static void tracing_stop_function_trace (void );
46+ /* Currently only the non stack verision is supported */
47+ ops -> func = function_trace_call ;
48+ ops -> flags = FTRACE_OPS_FL_RECURSION_SAFE ;
49+
50+ tr -> ops = ops ;
51+ ops -> private = tr ;
52+ return 0 ;
53+ }
2754
2855static int function_trace_init (struct trace_array * tr )
2956{
30- func_trace = tr ;
57+ struct ftrace_ops * ops ;
58+ int ret ;
59+
60+ if (tr -> flags & TRACE_ARRAY_FL_GLOBAL ) {
61+ /* There's only one global tr */
62+ if (!trace_ops .private ) {
63+ trace_ops .private = tr ;
64+ trace_stack_ops .private = tr ;
65+ }
66+
67+ if (func_flags .val & TRACE_FUNC_OPT_STACK )
68+ ops = & trace_stack_ops ;
69+ else
70+ ops = & trace_ops ;
71+ tr -> ops = ops ;
72+ } else {
73+ ret = allocate_ftrace_ops (tr );
74+ if (ret )
75+ return ret ;
76+ }
77+
3178 tr -> trace_buffer .cpu = get_cpu ();
3279 put_cpu ();
3380
3481 tracing_start_cmdline_record ();
35- tracing_start_function_trace ();
82+ tracing_start_function_trace (tr );
3683 return 0 ;
3784}
3885
3986static void function_trace_reset (struct trace_array * tr )
4087{
41- tracing_stop_function_trace ();
88+ tracing_stop_function_trace (tr );
4289 tracing_stop_cmdline_record ();
90+ if (!(tr -> flags & TRACE_ARRAY_FL_GLOBAL ))
91+ kfree (tr -> ops );
92+ tr -> ops = NULL ;
4393}
4494
4595static void function_trace_start (struct trace_array * tr )
4696{
4797 tracing_reset_online_cpus (& tr -> trace_buffer );
4898}
4999
50- /* Our option */
51- enum {
52- TRACE_FUNC_OPT_STACK = 0x1 ,
53- };
54-
55- static struct tracer_flags func_flags ;
56-
57100static void
58101function_trace_call (unsigned long ip , unsigned long parent_ip ,
59102 struct ftrace_ops * op , struct pt_regs * pt_regs )
60103{
61- struct trace_array * tr = func_trace ;
104+ struct trace_array * tr = op -> private ;
62105 struct trace_array_cpu * data ;
63106 unsigned long flags ;
64107 int bit ;
65108 int cpu ;
66109 int pc ;
67110
68- if (unlikely (!ftrace_function_enabled ))
111+ if (unlikely (!tr -> function_enabled ))
69112 return ;
70113
71114 pc = preempt_count ();
@@ -91,14 +134,14 @@ static void
91134function_stack_trace_call (unsigned long ip , unsigned long parent_ip ,
92135 struct ftrace_ops * op , struct pt_regs * pt_regs )
93136{
94- struct trace_array * tr = func_trace ;
137+ struct trace_array * tr = op -> private ;
95138 struct trace_array_cpu * data ;
96139 unsigned long flags ;
97140 long disabled ;
98141 int cpu ;
99142 int pc ;
100143
101- if (unlikely (!ftrace_function_enabled ))
144+ if (unlikely (!tr -> function_enabled ))
102145 return ;
103146
104147 /*
@@ -128,7 +171,6 @@ function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
128171 local_irq_restore (flags );
129172}
130173
131-
132174static struct ftrace_ops trace_ops __read_mostly =
133175{
134176 .func = function_trace_call ,
@@ -153,26 +195,17 @@ static struct tracer_flags func_flags = {
153195 .opts = func_opts
154196};
155197
156- static void tracing_start_function_trace (void )
198+ static void tracing_start_function_trace (struct trace_array * tr )
157199{
158- ftrace_function_enabled = 0 ;
159-
160- if (func_flags .val & TRACE_FUNC_OPT_STACK )
161- register_ftrace_function (& trace_stack_ops );
162- else
163- register_ftrace_function (& trace_ops );
164-
165- ftrace_function_enabled = 1 ;
200+ tr -> function_enabled = 0 ;
201+ register_ftrace_function (tr -> ops );
202+ tr -> function_enabled = 1 ;
166203}
167204
168- static void tracing_stop_function_trace (void )
205+ static void tracing_stop_function_trace (struct trace_array * tr )
169206{
170- ftrace_function_enabled = 0 ;
171-
172- if (func_flags .val & TRACE_FUNC_OPT_STACK )
173- unregister_ftrace_function (& trace_stack_ops );
174- else
175- unregister_ftrace_function (& trace_ops );
207+ tr -> function_enabled = 0 ;
208+ unregister_ftrace_function (tr -> ops );
176209}
177210
178211static int
@@ -184,12 +217,14 @@ func_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
184217 if (!!set == !!(func_flags .val & TRACE_FUNC_OPT_STACK ))
185218 break ;
186219
220+ unregister_ftrace_function (tr -> ops );
221+
187222 if (set ) {
188- unregister_ftrace_function ( & trace_ops ) ;
189- register_ftrace_function (& trace_stack_ops );
223+ tr -> ops = & trace_stack_ops ;
224+ register_ftrace_function (tr -> ops );
190225 } else {
191- unregister_ftrace_function ( & trace_stack_ops ) ;
192- register_ftrace_function (& trace_ops );
226+ tr -> ops = & trace_ops ;
227+ register_ftrace_function (tr -> ops );
193228 }
194229
195230 break ;
@@ -209,6 +244,7 @@ static struct tracer function_trace __tracer_data =
209244 .wait_pipe = poll_wait_pipe ,
210245 .flags = & func_flags ,
211246 .set_flag = func_set_flag ,
247+ .allow_instances = true,
212248#ifdef CONFIG_FTRACE_SELFTEST
213249 .selftest = trace_selftest_startup_function ,
214250#endif
0 commit comments