6262#define FTRACE_HASH_DEFAULT_BITS 10
6363#define FTRACE_HASH_MAX_BITS 12
6464
65+ #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66+
6567/* ftrace_enabled is a method to turn ftrace on or off */
6668int ftrace_enabled __read_mostly ;
6769static int last_ftrace_enabled ;
@@ -89,12 +91,14 @@ static struct ftrace_ops ftrace_list_end __read_mostly = {
8991};
9092
9193static struct ftrace_ops * ftrace_global_list __read_mostly = & ftrace_list_end ;
94+ static struct ftrace_ops * ftrace_control_list __read_mostly = & ftrace_list_end ;
9295static struct ftrace_ops * ftrace_ops_list __read_mostly = & ftrace_list_end ;
9396ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub ;
9497static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub ;
9598ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub ;
9699ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub ;
97100static struct ftrace_ops global_ops ;
101+ static struct ftrace_ops control_ops ;
98102
99103static void
100104ftrace_ops_list_func (unsigned long ip , unsigned long parent_ip );
@@ -168,6 +172,32 @@ static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
168172}
169173#endif
170174
175+ static void control_ops_disable_all (struct ftrace_ops * ops )
176+ {
177+ int cpu ;
178+
179+ for_each_possible_cpu (cpu )
180+ * per_cpu_ptr (ops -> disabled , cpu ) = 1 ;
181+ }
182+
183+ static int control_ops_alloc (struct ftrace_ops * ops )
184+ {
185+ int __percpu * disabled ;
186+
187+ disabled = alloc_percpu (int );
188+ if (!disabled )
189+ return - ENOMEM ;
190+
191+ ops -> disabled = disabled ;
192+ control_ops_disable_all (ops );
193+ return 0 ;
194+ }
195+
196+ static void control_ops_free (struct ftrace_ops * ops )
197+ {
198+ free_percpu (ops -> disabled );
199+ }
200+
171201static void update_global_ops (void )
172202{
173203 ftrace_func_t func ;
@@ -259,6 +289,26 @@ static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
259289 return 0 ;
260290}
261291
292+ static void add_ftrace_list_ops (struct ftrace_ops * * list ,
293+ struct ftrace_ops * main_ops ,
294+ struct ftrace_ops * ops )
295+ {
296+ int first = * list == & ftrace_list_end ;
297+ add_ftrace_ops (list , ops );
298+ if (first )
299+ add_ftrace_ops (& ftrace_ops_list , main_ops );
300+ }
301+
302+ static int remove_ftrace_list_ops (struct ftrace_ops * * list ,
303+ struct ftrace_ops * main_ops ,
304+ struct ftrace_ops * ops )
305+ {
306+ int ret = remove_ftrace_ops (list , ops );
307+ if (!ret && * list == & ftrace_list_end )
308+ ret = remove_ftrace_ops (& ftrace_ops_list , main_ops );
309+ return ret ;
310+ }
311+
262312static int __register_ftrace_function (struct ftrace_ops * ops )
263313{
264314 if (ftrace_disabled )
@@ -270,15 +320,20 @@ static int __register_ftrace_function(struct ftrace_ops *ops)
270320 if (WARN_ON (ops -> flags & FTRACE_OPS_FL_ENABLED ))
271321 return - EBUSY ;
272322
323+ /* We don't support both control and global flags set. */
324+ if ((ops -> flags & FL_GLOBAL_CONTROL_MASK ) == FL_GLOBAL_CONTROL_MASK )
325+ return - EINVAL ;
326+
273327 if (!core_kernel_data ((unsigned long )ops ))
274328 ops -> flags |= FTRACE_OPS_FL_DYNAMIC ;
275329
276330 if (ops -> flags & FTRACE_OPS_FL_GLOBAL ) {
277- int first = ftrace_global_list == & ftrace_list_end ;
278- add_ftrace_ops (& ftrace_global_list , ops );
331+ add_ftrace_list_ops (& ftrace_global_list , & global_ops , ops );
279332 ops -> flags |= FTRACE_OPS_FL_ENABLED ;
280- if (first )
281- add_ftrace_ops (& ftrace_ops_list , & global_ops );
333+ } else if (ops -> flags & FTRACE_OPS_FL_CONTROL ) {
334+ if (control_ops_alloc (ops ))
335+ return - ENOMEM ;
336+ add_ftrace_list_ops (& ftrace_control_list , & control_ops , ops );
282337 } else
283338 add_ftrace_ops (& ftrace_ops_list , ops );
284339
@@ -302,11 +357,23 @@ static int __unregister_ftrace_function(struct ftrace_ops *ops)
302357 return - EINVAL ;
303358
304359 if (ops -> flags & FTRACE_OPS_FL_GLOBAL ) {
305- ret = remove_ftrace_ops (& ftrace_global_list , ops );
306- if (!ret && ftrace_global_list == & ftrace_list_end )
307- ret = remove_ftrace_ops (& ftrace_ops_list , & global_ops );
360+ ret = remove_ftrace_list_ops (& ftrace_global_list ,
361+ & global_ops , ops );
308362 if (!ret )
309363 ops -> flags &= ~FTRACE_OPS_FL_ENABLED ;
364+ } else if (ops -> flags & FTRACE_OPS_FL_CONTROL ) {
365+ ret = remove_ftrace_list_ops (& ftrace_control_list ,
366+ & control_ops , ops );
367+ if (!ret ) {
368+ /*
369+ * The ftrace_ops is now removed from the list,
370+ * so there'll be no new users. We must ensure
371+ * all current users are done before we free
372+ * the control data.
373+ */
374+ synchronize_sched ();
375+ control_ops_free (ops );
376+ }
310377 } else
311378 ret = remove_ftrace_ops (& ftrace_ops_list , ops );
312379
@@ -1119,6 +1186,12 @@ static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
11191186 call_rcu_sched (& hash -> rcu , __free_ftrace_hash_rcu );
11201187}
11211188
1189+ void ftrace_free_filter (struct ftrace_ops * ops )
1190+ {
1191+ free_ftrace_hash (ops -> filter_hash );
1192+ free_ftrace_hash (ops -> notrace_hash );
1193+ }
1194+
11221195static struct ftrace_hash * alloc_ftrace_hash (int size_bits )
11231196{
11241197 struct ftrace_hash * hash ;
@@ -3873,6 +3946,36 @@ ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
38733946
38743947#endif /* CONFIG_DYNAMIC_FTRACE */
38753948
3949+ static void
3950+ ftrace_ops_control_func (unsigned long ip , unsigned long parent_ip )
3951+ {
3952+ struct ftrace_ops * op ;
3953+
3954+ if (unlikely (trace_recursion_test (TRACE_CONTROL_BIT )))
3955+ return ;
3956+
3957+ /*
3958+ * Some of the ops may be dynamically allocated,
3959+ * they must be freed after a synchronize_sched().
3960+ */
3961+ preempt_disable_notrace ();
3962+ trace_recursion_set (TRACE_CONTROL_BIT );
3963+ op = rcu_dereference_raw (ftrace_control_list );
3964+ while (op != & ftrace_list_end ) {
3965+ if (!ftrace_function_local_disabled (op ) &&
3966+ ftrace_ops_test (op , ip ))
3967+ op -> func (ip , parent_ip );
3968+
3969+ op = rcu_dereference_raw (op -> next );
3970+ };
3971+ trace_recursion_clear (TRACE_CONTROL_BIT );
3972+ preempt_enable_notrace ();
3973+ }
3974+
3975+ static struct ftrace_ops control_ops = {
3976+ .func = ftrace_ops_control_func ,
3977+ };
3978+
38763979static void
38773980ftrace_ops_list_func (unsigned long ip , unsigned long parent_ip )
38783981{
0 commit comments