1 // SPDX-License-Identifier: GPL-2.0
3 * Infrastructure for profiling code inserted by 'gcc -pg'.
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
8 * Originally ported from the -rt patch by:
9 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
11 * Based on code in the latency_tracer, that is:
13 * Copyright (C) 2004-2006 Ingo Molnar
14 * Copyright (C) 2004 Nadia Yvette Chambers
17 #include <linux/stop_machine.h>
18 #include <linux/clocksource.h>
19 #include <linux/sched/task.h>
20 #include <linux/kallsyms.h>
21 #include <linux/seq_file.h>
22 #include <linux/tracefs.h>
23 #include <linux/hardirq.h>
24 #include <linux/kthread.h>
25 #include <linux/uaccess.h>
26 #include <linux/bsearch.h>
27 #include <linux/module.h>
28 #include <linux/ftrace.h>
29 #include <linux/sysctl.h>
30 #include <linux/slab.h>
31 #include <linux/ctype.h>
32 #include <linux/sort.h>
33 #include <linux/list.h>
34 #include <linux/hash.h>
35 #include <linux/rcupdate.h>
37 #include <trace/events/sched.h>
39 #include <asm/sections.h>
40 #include <asm/setup.h>
42 #include "ftrace_internal.h"
43 #include "trace_output.h"
44 #include "trace_stat.h"
46 #define FTRACE_WARN_ON(cond) \
54 #define FTRACE_WARN_ON_ONCE(cond) \
57 if (WARN_ON_ONCE(___r)) \
62 /* hash bits for specific function selection */
63 #define FTRACE_HASH_BITS 7
64 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
65 #define FTRACE_HASH_DEFAULT_BITS 10
66 #define FTRACE_HASH_MAX_BITS 12
68 #ifdef CONFIG_DYNAMIC_FTRACE
69 #define INIT_OPS_HASH(opsname) \
70 .func_hash = &opsname.local_hash, \
71 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
72 #define ASSIGN_OPS_HASH(opsname, val) \
74 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
76 #define INIT_OPS_HASH(opsname)
77 #define ASSIGN_OPS_HASH(opsname, val)
81 FTRACE_MODIFY_ENABLE_FL
= (1 << 0),
82 FTRACE_MODIFY_MAY_SLEEP_FL
= (1 << 1),
85 struct ftrace_ops ftrace_list_end __read_mostly
= {
87 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_STUB
,
88 INIT_OPS_HASH(ftrace_list_end
)
91 /* ftrace_enabled is a method to turn ftrace on or off */
92 int ftrace_enabled __read_mostly
;
93 static int last_ftrace_enabled
;
95 /* Current function tracing op */
96 struct ftrace_ops
*function_trace_op __read_mostly
= &ftrace_list_end
;
97 /* What to set function_trace_op to */
98 static struct ftrace_ops
*set_function_trace_op
;
100 static bool ftrace_pids_enabled(struct ftrace_ops
*ops
)
102 struct trace_array
*tr
;
104 if (!(ops
->flags
& FTRACE_OPS_FL_PID
) || !ops
->private)
109 return tr
->function_pids
!= NULL
;
112 static void ftrace_update_trampoline(struct ftrace_ops
*ops
);
115 * ftrace_disabled is set when an anomaly is discovered.
116 * ftrace_disabled is much stronger than ftrace_enabled.
118 static int ftrace_disabled __read_mostly
;
120 DEFINE_MUTEX(ftrace_lock
);
122 struct ftrace_ops __rcu
*ftrace_ops_list __read_mostly
= &ftrace_list_end
;
123 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
124 struct ftrace_ops global_ops
;
126 #if ARCH_SUPPORTS_FTRACE_OPS
127 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
128 struct ftrace_ops
*op
, struct pt_regs
*regs
);
130 /* See comment below, where ftrace_ops_list_func is defined */
131 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
);
132 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
135 static inline void ftrace_ops_init(struct ftrace_ops
*ops
)
137 #ifdef CONFIG_DYNAMIC_FTRACE
138 if (!(ops
->flags
& FTRACE_OPS_FL_INITIALIZED
)) {
139 mutex_init(&ops
->local_hash
.regex_lock
);
140 ops
->func_hash
= &ops
->local_hash
;
141 ops
->flags
|= FTRACE_OPS_FL_INITIALIZED
;
146 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
,
147 struct ftrace_ops
*op
, struct pt_regs
*regs
)
149 struct trace_array
*tr
= op
->private;
151 if (tr
&& this_cpu_read(tr
->trace_buffer
.data
->ftrace_ignore_pid
))
154 op
->saved_func(ip
, parent_ip
, op
, regs
);
157 static void ftrace_sync(struct work_struct
*work
)
160 * This function is just a stub to implement a hard force
161 * of synchronize_rcu(). This requires synchronizing
162 * tasks even in userspace and idle.
164 * Yes, function tracing is rude.
168 static void ftrace_sync_ipi(void *data
)
170 /* Probably not needed, but do it anyway */
174 static ftrace_func_t
ftrace_ops_get_list_func(struct ftrace_ops
*ops
)
177 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
178 * then it needs to call the list anyway.
180 if (ops
->flags
& (FTRACE_OPS_FL_DYNAMIC
| FTRACE_OPS_FL_RCU
) ||
181 FTRACE_FORCE_LIST_FUNC
)
182 return ftrace_ops_list_func
;
184 return ftrace_ops_get_func(ops
);
187 static void update_ftrace_function(void)
192 * Prepare the ftrace_ops that the arch callback will use.
193 * If there's only one ftrace_ops registered, the ftrace_ops_list
194 * will point to the ops we want.
196 set_function_trace_op
= rcu_dereference_protected(ftrace_ops_list
,
197 lockdep_is_held(&ftrace_lock
));
199 /* If there's no ftrace_ops registered, just call the stub function */
200 if (set_function_trace_op
== &ftrace_list_end
) {
204 * If we are at the end of the list and this ops is
205 * recursion safe and not dynamic and the arch supports passing ops,
206 * then have the mcount trampoline call the function directly.
208 } else if (rcu_dereference_protected(ftrace_ops_list
->next
,
209 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
210 func
= ftrace_ops_get_list_func(ftrace_ops_list
);
213 /* Just use the default ftrace_ops */
214 set_function_trace_op
= &ftrace_list_end
;
215 func
= ftrace_ops_list_func
;
218 update_function_graph_func();
220 /* If there's no change, then do nothing more here */
221 if (ftrace_trace_function
== func
)
225 * If we are using the list function, it doesn't care
226 * about the function_trace_ops.
228 if (func
== ftrace_ops_list_func
) {
229 ftrace_trace_function
= func
;
231 * Don't even bother setting function_trace_ops,
232 * it would be racy to do so anyway.
237 #ifndef CONFIG_DYNAMIC_FTRACE
239 * For static tracing, we need to be a bit more careful.
240 * The function change takes affect immediately. Thus,
241 * we need to coorditate the setting of the function_trace_ops
242 * with the setting of the ftrace_trace_function.
244 * Set the function to the list ops, which will call the
245 * function we want, albeit indirectly, but it handles the
246 * ftrace_ops and doesn't depend on function_trace_op.
248 ftrace_trace_function
= ftrace_ops_list_func
;
250 * Make sure all CPUs see this. Yes this is slow, but static
251 * tracing is slow and nasty to have enabled.
253 schedule_on_each_cpu(ftrace_sync
);
254 /* Now all cpus are using the list ops. */
255 function_trace_op
= set_function_trace_op
;
256 /* Make sure the function_trace_op is visible on all CPUs */
258 /* Nasty way to force a rmb on all cpus */
259 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
260 /* OK, we are all set to update the ftrace_trace_function now! */
261 #endif /* !CONFIG_DYNAMIC_FTRACE */
263 ftrace_trace_function
= func
;
266 static void add_ftrace_ops(struct ftrace_ops __rcu
**list
,
267 struct ftrace_ops
*ops
)
269 rcu_assign_pointer(ops
->next
, *list
);
272 * We are entering ops into the list but another
273 * CPU might be walking that list. We need to make sure
274 * the ops->next pointer is valid before another CPU sees
275 * the ops pointer included into the list.
277 rcu_assign_pointer(*list
, ops
);
280 static int remove_ftrace_ops(struct ftrace_ops __rcu
**list
,
281 struct ftrace_ops
*ops
)
283 struct ftrace_ops
**p
;
286 * If we are removing the last function, then simply point
287 * to the ftrace_stub.
289 if (rcu_dereference_protected(*list
,
290 lockdep_is_held(&ftrace_lock
)) == ops
&&
291 rcu_dereference_protected(ops
->next
,
292 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
293 *list
= &ftrace_list_end
;
297 for (p
= list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
308 static void ftrace_update_trampoline(struct ftrace_ops
*ops
);
310 int __register_ftrace_function(struct ftrace_ops
*ops
)
312 if (ops
->flags
& FTRACE_OPS_FL_DELETED
)
315 if (WARN_ON(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
318 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
320 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
321 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
322 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
324 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
&&
325 !(ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
))
328 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
)
329 ops
->flags
|= FTRACE_OPS_FL_SAVE_REGS
;
332 if (!core_kernel_data((unsigned long)ops
))
333 ops
->flags
|= FTRACE_OPS_FL_DYNAMIC
;
335 add_ftrace_ops(&ftrace_ops_list
, ops
);
337 /* Always save the function, and reset at unregistering */
338 ops
->saved_func
= ops
->func
;
340 if (ftrace_pids_enabled(ops
))
341 ops
->func
= ftrace_pid_func
;
343 ftrace_update_trampoline(ops
);
346 update_ftrace_function();
351 int __unregister_ftrace_function(struct ftrace_ops
*ops
)
355 if (WARN_ON(!(ops
->flags
& FTRACE_OPS_FL_ENABLED
)))
358 ret
= remove_ftrace_ops(&ftrace_ops_list
, ops
);
364 update_ftrace_function();
366 ops
->func
= ops
->saved_func
;
371 static void ftrace_update_pid_func(void)
373 struct ftrace_ops
*op
;
375 /* Only do something if we are tracing something */
376 if (ftrace_trace_function
== ftrace_stub
)
379 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
380 if (op
->flags
& FTRACE_OPS_FL_PID
) {
381 op
->func
= ftrace_pids_enabled(op
) ?
382 ftrace_pid_func
: op
->saved_func
;
383 ftrace_update_trampoline(op
);
385 } while_for_each_ftrace_op(op
);
387 update_ftrace_function();
390 #ifdef CONFIG_FUNCTION_PROFILER
391 struct ftrace_profile
{
392 struct hlist_node node
;
394 unsigned long counter
;
395 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
396 unsigned long long time
;
397 unsigned long long time_squared
;
401 struct ftrace_profile_page
{
402 struct ftrace_profile_page
*next
;
404 struct ftrace_profile records
[];
407 struct ftrace_profile_stat
{
409 struct hlist_head
*hash
;
410 struct ftrace_profile_page
*pages
;
411 struct ftrace_profile_page
*start
;
412 struct tracer_stat stat
;
415 #define PROFILE_RECORDS_SIZE \
416 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
418 #define PROFILES_PER_PAGE \
419 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
421 static int ftrace_profile_enabled __read_mostly
;
423 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
424 static DEFINE_MUTEX(ftrace_profile_lock
);
426 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
428 #define FTRACE_PROFILE_HASH_BITS 10
429 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
432 function_stat_next(void *v
, int idx
)
434 struct ftrace_profile
*rec
= v
;
435 struct ftrace_profile_page
*pg
;
437 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
443 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
447 rec
= &pg
->records
[0];
455 static void *function_stat_start(struct tracer_stat
*trace
)
457 struct ftrace_profile_stat
*stat
=
458 container_of(trace
, struct ftrace_profile_stat
, stat
);
460 if (!stat
|| !stat
->start
)
463 return function_stat_next(&stat
->start
->records
[0], 0);
466 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
467 /* function graph compares on total time */
468 static int function_stat_cmp(void *p1
, void *p2
)
470 struct ftrace_profile
*a
= p1
;
471 struct ftrace_profile
*b
= p2
;
473 if (a
->time
< b
->time
)
475 if (a
->time
> b
->time
)
481 /* not function graph compares against hits */
482 static int function_stat_cmp(void *p1
, void *p2
)
484 struct ftrace_profile
*a
= p1
;
485 struct ftrace_profile
*b
= p2
;
487 if (a
->counter
< b
->counter
)
489 if (a
->counter
> b
->counter
)
496 static int function_stat_headers(struct seq_file
*m
)
498 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
499 seq_puts(m
, " Function "
502 "--- ---- --- ---\n");
504 seq_puts(m
, " Function Hit\n"
510 static int function_stat_show(struct seq_file
*m
, void *v
)
512 struct ftrace_profile
*rec
= v
;
513 char str
[KSYM_SYMBOL_LEN
];
515 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
516 static struct trace_seq s
;
517 unsigned long long avg
;
518 unsigned long long stddev
;
520 mutex_lock(&ftrace_profile_lock
);
522 /* we raced with function_profile_reset() */
523 if (unlikely(rec
->counter
== 0)) {
528 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
530 do_div(avg
, rec
->counter
);
531 if (tracing_thresh
&& (avg
< tracing_thresh
))
535 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
536 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
538 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
541 /* Sample standard deviation (s^2) */
542 if (rec
->counter
<= 1)
546 * Apply Welford's method:
547 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
549 stddev
= rec
->counter
* rec
->time_squared
-
550 rec
->time
* rec
->time
;
553 * Divide only 1000 for ns^2 -> us^2 conversion.
554 * trace_print_graph_duration will divide 1000 again.
556 do_div(stddev
, rec
->counter
* (rec
->counter
- 1) * 1000);
560 trace_print_graph_duration(rec
->time
, &s
);
561 trace_seq_puts(&s
, " ");
562 trace_print_graph_duration(avg
, &s
);
563 trace_seq_puts(&s
, " ");
564 trace_print_graph_duration(stddev
, &s
);
565 trace_print_seq(m
, &s
);
569 mutex_unlock(&ftrace_profile_lock
);
574 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
576 struct ftrace_profile_page
*pg
;
578 pg
= stat
->pages
= stat
->start
;
581 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
586 memset(stat
->hash
, 0,
587 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
590 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
592 struct ftrace_profile_page
*pg
;
597 /* If we already allocated, do nothing */
601 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
605 #ifdef CONFIG_DYNAMIC_FTRACE
606 functions
= ftrace_update_tot_cnt
;
609 * We do not know the number of functions that exist because
610 * dynamic tracing is what counts them. With past experience
611 * we have around 20K functions. That should be more than enough.
612 * It is highly unlikely we will execute every function in
618 pg
= stat
->start
= stat
->pages
;
620 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
622 for (i
= 1; i
< pages
; i
++) {
623 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
634 unsigned long tmp
= (unsigned long)pg
;
646 static int ftrace_profile_init_cpu(int cpu
)
648 struct ftrace_profile_stat
*stat
;
651 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
654 /* If the profile is already created, simply reset it */
655 ftrace_profile_reset(stat
);
660 * We are profiling all functions, but usually only a few thousand
661 * functions are hit. We'll make a hash of 1024 items.
663 size
= FTRACE_PROFILE_HASH_SIZE
;
665 stat
->hash
= kcalloc(size
, sizeof(struct hlist_head
), GFP_KERNEL
);
670 /* Preallocate the function profiling pages */
671 if (ftrace_profile_pages_init(stat
) < 0) {
680 static int ftrace_profile_init(void)
685 for_each_possible_cpu(cpu
) {
686 ret
= ftrace_profile_init_cpu(cpu
);
694 /* interrupts must be disabled */
695 static struct ftrace_profile
*
696 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
698 struct ftrace_profile
*rec
;
699 struct hlist_head
*hhd
;
702 key
= hash_long(ip
, FTRACE_PROFILE_HASH_BITS
);
703 hhd
= &stat
->hash
[key
];
705 if (hlist_empty(hhd
))
708 hlist_for_each_entry_rcu_notrace(rec
, hhd
, node
) {
716 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
717 struct ftrace_profile
*rec
)
721 key
= hash_long(rec
->ip
, FTRACE_PROFILE_HASH_BITS
);
722 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
726 * The memory is already allocated, this simply finds a new record to use.
728 static struct ftrace_profile
*
729 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
731 struct ftrace_profile
*rec
= NULL
;
733 /* prevent recursion (from NMIs) */
734 if (atomic_inc_return(&stat
->disabled
) != 1)
738 * Try to find the function again since an NMI
739 * could have added it
741 rec
= ftrace_find_profiled_func(stat
, ip
);
745 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
746 if (!stat
->pages
->next
)
748 stat
->pages
= stat
->pages
->next
;
751 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
753 ftrace_add_profile(stat
, rec
);
756 atomic_dec(&stat
->disabled
);
762 function_profile_call(unsigned long ip
, unsigned long parent_ip
,
763 struct ftrace_ops
*ops
, struct pt_regs
*regs
)
765 struct ftrace_profile_stat
*stat
;
766 struct ftrace_profile
*rec
;
769 if (!ftrace_profile_enabled
)
772 local_irq_save(flags
);
774 stat
= this_cpu_ptr(&ftrace_profile_stats
);
775 if (!stat
->hash
|| !ftrace_profile_enabled
)
778 rec
= ftrace_find_profiled_func(stat
, ip
);
780 rec
= ftrace_profile_alloc(stat
, ip
);
787 local_irq_restore(flags
);
790 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
791 static bool fgraph_graph_time
= true;
793 void ftrace_graph_graph_time_control(bool enable
)
795 fgraph_graph_time
= enable
;
798 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
800 struct ftrace_ret_stack
*ret_stack
;
802 function_profile_call(trace
->func
, 0, NULL
, NULL
);
804 /* If function graph is shutting down, ret_stack can be NULL */
805 if (!current
->ret_stack
)
808 ret_stack
= ftrace_graph_get_ret_stack(current
, 0);
810 ret_stack
->subtime
= 0;
815 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
817 struct ftrace_ret_stack
*ret_stack
;
818 struct ftrace_profile_stat
*stat
;
819 unsigned long long calltime
;
820 struct ftrace_profile
*rec
;
823 local_irq_save(flags
);
824 stat
= this_cpu_ptr(&ftrace_profile_stats
);
825 if (!stat
->hash
|| !ftrace_profile_enabled
)
828 /* If the calltime was zero'd ignore it */
829 if (!trace
->calltime
)
832 calltime
= trace
->rettime
- trace
->calltime
;
834 if (!fgraph_graph_time
) {
836 /* Append this call time to the parent time to subtract */
837 ret_stack
= ftrace_graph_get_ret_stack(current
, 1);
839 ret_stack
->subtime
+= calltime
;
841 ret_stack
= ftrace_graph_get_ret_stack(current
, 0);
842 if (ret_stack
&& ret_stack
->subtime
< calltime
)
843 calltime
-= ret_stack
->subtime
;
848 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
850 rec
->time
+= calltime
;
851 rec
->time_squared
+= calltime
* calltime
;
855 local_irq_restore(flags
);
858 static struct fgraph_ops fprofiler_ops
= {
859 .entryfunc
= &profile_graph_entry
,
860 .retfunc
= &profile_graph_return
,
863 static int register_ftrace_profiler(void)
865 return register_ftrace_graph(&fprofiler_ops
);
868 static void unregister_ftrace_profiler(void)
870 unregister_ftrace_graph(&fprofiler_ops
);
873 static struct ftrace_ops ftrace_profile_ops __read_mostly
= {
874 .func
= function_profile_call
,
875 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
876 INIT_OPS_HASH(ftrace_profile_ops
)
879 static int register_ftrace_profiler(void)
881 return register_ftrace_function(&ftrace_profile_ops
);
884 static void unregister_ftrace_profiler(void)
886 unregister_ftrace_function(&ftrace_profile_ops
);
888 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
891 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
892 size_t cnt
, loff_t
*ppos
)
897 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
903 mutex_lock(&ftrace_profile_lock
);
904 if (ftrace_profile_enabled
^ val
) {
906 ret
= ftrace_profile_init();
912 ret
= register_ftrace_profiler();
917 ftrace_profile_enabled
= 1;
919 ftrace_profile_enabled
= 0;
921 * unregister_ftrace_profiler calls stop_machine
922 * so this acts like an synchronize_rcu.
924 unregister_ftrace_profiler();
928 mutex_unlock(&ftrace_profile_lock
);
936 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
937 size_t cnt
, loff_t
*ppos
)
939 char buf
[64]; /* big enough to hold a number */
942 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
943 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
946 static const struct file_operations ftrace_profile_fops
= {
947 .open
= tracing_open_generic
,
948 .read
= ftrace_profile_read
,
949 .write
= ftrace_profile_write
,
950 .llseek
= default_llseek
,
953 /* used to initialize the real stat files */
954 static struct tracer_stat function_stats __initdata
= {
956 .stat_start
= function_stat_start
,
957 .stat_next
= function_stat_next
,
958 .stat_cmp
= function_stat_cmp
,
959 .stat_headers
= function_stat_headers
,
960 .stat_show
= function_stat_show
963 static __init
void ftrace_profile_tracefs(struct dentry
*d_tracer
)
965 struct ftrace_profile_stat
*stat
;
966 struct dentry
*entry
;
971 for_each_possible_cpu(cpu
) {
972 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
974 name
= kasprintf(GFP_KERNEL
, "function%d", cpu
);
977 * The files created are permanent, if something happens
978 * we still do not free memory.
981 "Could not allocate stat file for cpu %d\n",
985 stat
->stat
= function_stats
;
986 stat
->stat
.name
= name
;
987 ret
= register_stat_tracer(&stat
->stat
);
990 "Could not register function stat for cpu %d\n",
997 entry
= tracefs_create_file("function_profile_enabled", 0644,
998 d_tracer
, NULL
, &ftrace_profile_fops
);
1000 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1003 #else /* CONFIG_FUNCTION_PROFILER */
1004 static __init
void ftrace_profile_tracefs(struct dentry
*d_tracer
)
1007 #endif /* CONFIG_FUNCTION_PROFILER */
1009 #ifdef CONFIG_DYNAMIC_FTRACE
1011 static struct ftrace_ops
*removed_ops
;
1014 * Set when doing a global update, like enabling all recs or disabling them.
1015 * It is not set when just updating a single ftrace_ops.
1017 static bool update_all_ops
;
1019 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1020 # error Dynamic ftrace depends on MCOUNT_RECORD
1023 struct ftrace_func_entry
{
1024 struct hlist_node hlist
;
1028 struct ftrace_func_probe
{
1029 struct ftrace_probe_ops
*probe_ops
;
1030 struct ftrace_ops ops
;
1031 struct trace_array
*tr
;
1032 struct list_head list
;
1038 * We make these constant because no one should touch them,
1039 * but they are used as the default "empty hash", to avoid allocating
1040 * it all the time. These are in a read only section such that if
1041 * anyone does try to modify it, it will cause an exception.
1043 static const struct hlist_head empty_buckets
[1];
1044 static const struct ftrace_hash empty_hash
= {
1045 .buckets
= (struct hlist_head
*)empty_buckets
,
1047 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1049 struct ftrace_ops global_ops
= {
1050 .func
= ftrace_stub
,
1051 .local_hash
.notrace_hash
= EMPTY_HASH
,
1052 .local_hash
.filter_hash
= EMPTY_HASH
,
1053 INIT_OPS_HASH(global_ops
)
1054 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
|
1055 FTRACE_OPS_FL_INITIALIZED
|
1060 * Used by the stack undwinder to know about dynamic ftrace trampolines.
1062 struct ftrace_ops
*ftrace_ops_trampoline(unsigned long addr
)
1064 struct ftrace_ops
*op
= NULL
;
1067 * Some of the ops may be dynamically allocated,
1068 * they are freed after a synchronize_rcu().
1070 preempt_disable_notrace();
1072 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
1074 * This is to check for dynamically allocated trampolines.
1075 * Trampolines that are in kernel text will have
1076 * core_kernel_text() return true.
1078 if (op
->trampoline
&& op
->trampoline_size
)
1079 if (addr
>= op
->trampoline
&&
1080 addr
< op
->trampoline
+ op
->trampoline_size
) {
1081 preempt_enable_notrace();
1084 } while_for_each_ftrace_op(op
);
1085 preempt_enable_notrace();
1091 * This is used by __kernel_text_address() to return true if the
1092 * address is on a dynamically allocated trampoline that would
1093 * not return true for either core_kernel_text() or
1094 * is_module_text_address().
1096 bool is_ftrace_trampoline(unsigned long addr
)
1098 return ftrace_ops_trampoline(addr
) != NULL
;
1101 struct ftrace_page
{
1102 struct ftrace_page
*next
;
1103 struct dyn_ftrace
*records
;
1108 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1109 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1111 /* estimate from running different kernels */
1112 #define NR_TO_INIT 10000
1114 static struct ftrace_page
*ftrace_pages_start
;
1115 static struct ftrace_page
*ftrace_pages
;
1117 static __always_inline
unsigned long
1118 ftrace_hash_key(struct ftrace_hash
*hash
, unsigned long ip
)
1120 if (hash
->size_bits
> 0)
1121 return hash_long(ip
, hash
->size_bits
);
1126 /* Only use this function if ftrace_hash_empty() has already been tested */
1127 static __always_inline
struct ftrace_func_entry
*
1128 __ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1131 struct ftrace_func_entry
*entry
;
1132 struct hlist_head
*hhd
;
1134 key
= ftrace_hash_key(hash
, ip
);
1135 hhd
= &hash
->buckets
[key
];
1137 hlist_for_each_entry_rcu_notrace(entry
, hhd
, hlist
) {
1138 if (entry
->ip
== ip
)
1145 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1146 * @hash: The hash to look at
1147 * @ip: The instruction pointer to test
1149 * Search a given @hash to see if a given instruction pointer (@ip)
1152 * Returns the entry that holds the @ip if found. NULL otherwise.
1154 struct ftrace_func_entry
*
1155 ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1157 if (ftrace_hash_empty(hash
))
1160 return __ftrace_lookup_ip(hash
, ip
);
1163 static void __add_hash_entry(struct ftrace_hash
*hash
,
1164 struct ftrace_func_entry
*entry
)
1166 struct hlist_head
*hhd
;
1169 key
= ftrace_hash_key(hash
, entry
->ip
);
1170 hhd
= &hash
->buckets
[key
];
1171 hlist_add_head(&entry
->hlist
, hhd
);
1175 static int add_hash_entry(struct ftrace_hash
*hash
, unsigned long ip
)
1177 struct ftrace_func_entry
*entry
;
1179 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1184 __add_hash_entry(hash
, entry
);
1190 free_hash_entry(struct ftrace_hash
*hash
,
1191 struct ftrace_func_entry
*entry
)
1193 hlist_del(&entry
->hlist
);
1199 remove_hash_entry(struct ftrace_hash
*hash
,
1200 struct ftrace_func_entry
*entry
)
1202 hlist_del_rcu(&entry
->hlist
);
1206 static void ftrace_hash_clear(struct ftrace_hash
*hash
)
1208 struct hlist_head
*hhd
;
1209 struct hlist_node
*tn
;
1210 struct ftrace_func_entry
*entry
;
1211 int size
= 1 << hash
->size_bits
;
1217 for (i
= 0; i
< size
; i
++) {
1218 hhd
= &hash
->buckets
[i
];
1219 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
)
1220 free_hash_entry(hash
, entry
);
1222 FTRACE_WARN_ON(hash
->count
);
1225 static void free_ftrace_mod(struct ftrace_mod_load
*ftrace_mod
)
1227 list_del(&ftrace_mod
->list
);
1228 kfree(ftrace_mod
->module
);
1229 kfree(ftrace_mod
->func
);
1233 static void clear_ftrace_mod_list(struct list_head
*head
)
1235 struct ftrace_mod_load
*p
, *n
;
1237 /* stack tracer isn't supported yet */
1241 mutex_lock(&ftrace_lock
);
1242 list_for_each_entry_safe(p
, n
, head
, list
)
1244 mutex_unlock(&ftrace_lock
);
1247 static void free_ftrace_hash(struct ftrace_hash
*hash
)
1249 if (!hash
|| hash
== EMPTY_HASH
)
1251 ftrace_hash_clear(hash
);
1252 kfree(hash
->buckets
);
1256 static void __free_ftrace_hash_rcu(struct rcu_head
*rcu
)
1258 struct ftrace_hash
*hash
;
1260 hash
= container_of(rcu
, struct ftrace_hash
, rcu
);
1261 free_ftrace_hash(hash
);
1264 static void free_ftrace_hash_rcu(struct ftrace_hash
*hash
)
1266 if (!hash
|| hash
== EMPTY_HASH
)
1268 call_rcu(&hash
->rcu
, __free_ftrace_hash_rcu
);
1271 void ftrace_free_filter(struct ftrace_ops
*ops
)
1273 ftrace_ops_init(ops
);
1274 free_ftrace_hash(ops
->func_hash
->filter_hash
);
1275 free_ftrace_hash(ops
->func_hash
->notrace_hash
);
1278 static struct ftrace_hash
*alloc_ftrace_hash(int size_bits
)
1280 struct ftrace_hash
*hash
;
1283 hash
= kzalloc(sizeof(*hash
), GFP_KERNEL
);
1287 size
= 1 << size_bits
;
1288 hash
->buckets
= kcalloc(size
, sizeof(*hash
->buckets
), GFP_KERNEL
);
1290 if (!hash
->buckets
) {
1295 hash
->size_bits
= size_bits
;
1301 static int ftrace_add_mod(struct trace_array
*tr
,
1302 const char *func
, const char *module
,
1305 struct ftrace_mod_load
*ftrace_mod
;
1306 struct list_head
*mod_head
= enable
? &tr
->mod_trace
: &tr
->mod_notrace
;
1308 ftrace_mod
= kzalloc(sizeof(*ftrace_mod
), GFP_KERNEL
);
1312 ftrace_mod
->func
= kstrdup(func
, GFP_KERNEL
);
1313 ftrace_mod
->module
= kstrdup(module
, GFP_KERNEL
);
1314 ftrace_mod
->enable
= enable
;
1316 if (!ftrace_mod
->func
|| !ftrace_mod
->module
)
1319 list_add(&ftrace_mod
->list
, mod_head
);
1324 free_ftrace_mod(ftrace_mod
);
1329 static struct ftrace_hash
*
1330 alloc_and_copy_ftrace_hash(int size_bits
, struct ftrace_hash
*hash
)
1332 struct ftrace_func_entry
*entry
;
1333 struct ftrace_hash
*new_hash
;
1338 new_hash
= alloc_ftrace_hash(size_bits
);
1343 new_hash
->flags
= hash
->flags
;
1346 if (ftrace_hash_empty(hash
))
1349 size
= 1 << hash
->size_bits
;
1350 for (i
= 0; i
< size
; i
++) {
1351 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
1352 ret
= add_hash_entry(new_hash
, entry
->ip
);
1358 FTRACE_WARN_ON(new_hash
->count
!= hash
->count
);
1363 free_ftrace_hash(new_hash
);
1368 ftrace_hash_rec_disable_modify(struct ftrace_ops
*ops
, int filter_hash
);
1370 ftrace_hash_rec_enable_modify(struct ftrace_ops
*ops
, int filter_hash
);
1372 static int ftrace_hash_ipmodify_update(struct ftrace_ops
*ops
,
1373 struct ftrace_hash
*new_hash
);
1375 static struct ftrace_hash
*
1376 __ftrace_hash_move(struct ftrace_hash
*src
)
1378 struct ftrace_func_entry
*entry
;
1379 struct hlist_node
*tn
;
1380 struct hlist_head
*hhd
;
1381 struct ftrace_hash
*new_hash
;
1382 int size
= src
->count
;
1387 * If the new source is empty, just return the empty_hash.
1389 if (ftrace_hash_empty(src
))
1393 * Make the hash size about 1/2 the # found
1395 for (size
/= 2; size
; size
>>= 1)
1398 /* Don't allocate too much */
1399 if (bits
> FTRACE_HASH_MAX_BITS
)
1400 bits
= FTRACE_HASH_MAX_BITS
;
1402 new_hash
= alloc_ftrace_hash(bits
);
1406 new_hash
->flags
= src
->flags
;
1408 size
= 1 << src
->size_bits
;
1409 for (i
= 0; i
< size
; i
++) {
1410 hhd
= &src
->buckets
[i
];
1411 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
) {
1412 remove_hash_entry(src
, entry
);
1413 __add_hash_entry(new_hash
, entry
);
1421 ftrace_hash_move(struct ftrace_ops
*ops
, int enable
,
1422 struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1424 struct ftrace_hash
*new_hash
;
1427 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1428 if (ops
->flags
& FTRACE_OPS_FL_IPMODIFY
&& !enable
)
1431 new_hash
= __ftrace_hash_move(src
);
1435 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1437 /* IPMODIFY should be updated only when filter_hash updating */
1438 ret
= ftrace_hash_ipmodify_update(ops
, new_hash
);
1440 free_ftrace_hash(new_hash
);
1446 * Remove the current set, update the hash and add
1449 ftrace_hash_rec_disable_modify(ops
, enable
);
1451 rcu_assign_pointer(*dst
, new_hash
);
1453 ftrace_hash_rec_enable_modify(ops
, enable
);
1458 static bool hash_contains_ip(unsigned long ip
,
1459 struct ftrace_ops_hash
*hash
)
1462 * The function record is a match if it exists in the filter
1463 * hash and not in the notrace hash. Note, an emty hash is
1464 * considered a match for the filter hash, but an empty
1465 * notrace hash is considered not in the notrace hash.
1467 return (ftrace_hash_empty(hash
->filter_hash
) ||
1468 __ftrace_lookup_ip(hash
->filter_hash
, ip
)) &&
1469 (ftrace_hash_empty(hash
->notrace_hash
) ||
1470 !__ftrace_lookup_ip(hash
->notrace_hash
, ip
));
1474 * Test the hashes for this ops to see if we want to call
1475 * the ops->func or not.
1477 * It's a match if the ip is in the ops->filter_hash or
1478 * the filter_hash does not exist or is empty,
1480 * the ip is not in the ops->notrace_hash.
1482 * This needs to be called with preemption disabled as
1483 * the hashes are freed with call_rcu().
1486 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
1488 struct ftrace_ops_hash hash
;
1491 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1493 * There's a small race when adding ops that the ftrace handler
1494 * that wants regs, may be called without them. We can not
1495 * allow that handler to be called if regs is NULL.
1497 if (regs
== NULL
&& (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
))
1501 rcu_assign_pointer(hash
.filter_hash
, ops
->func_hash
->filter_hash
);
1502 rcu_assign_pointer(hash
.notrace_hash
, ops
->func_hash
->notrace_hash
);
1504 if (hash_contains_ip(ip
, &hash
))
1513 * This is a double for. Do not use 'break' to break out of the loop,
1514 * you must use a goto.
1516 #define do_for_each_ftrace_rec(pg, rec) \
1517 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1519 for (_____i = 0; _____i < pg->index; _____i++) { \
1520 rec = &pg->records[_____i];
1522 #define while_for_each_ftrace_rec() \
1527 static int ftrace_cmp_recs(const void *a
, const void *b
)
1529 const struct dyn_ftrace
*key
= a
;
1530 const struct dyn_ftrace
*rec
= b
;
1532 if (key
->flags
< rec
->ip
)
1534 if (key
->ip
>= rec
->ip
+ MCOUNT_INSN_SIZE
)
1540 * ftrace_location_range - return the first address of a traced location
1541 * if it touches the given ip range
1542 * @start: start of range to search.
1543 * @end: end of range to search (inclusive). @end points to the last byte
1546 * Returns rec->ip if the related ftrace location is a least partly within
1547 * the given address range. That is, the first address of the instruction
1548 * that is either a NOP or call to the function tracer. It checks the ftrace
1549 * internal tables to determine if the address belongs or not.
1551 unsigned long ftrace_location_range(unsigned long start
, unsigned long end
)
1553 struct ftrace_page
*pg
;
1554 struct dyn_ftrace
*rec
;
1555 struct dyn_ftrace key
;
1558 key
.flags
= end
; /* overload flags, as it is unsigned long */
1560 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
1561 if (end
< pg
->records
[0].ip
||
1562 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
1564 rec
= bsearch(&key
, pg
->records
, pg
->index
,
1565 sizeof(struct dyn_ftrace
),
1575 * ftrace_location - return true if the ip giving is a traced location
1576 * @ip: the instruction pointer to check
1578 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1579 * That is, the instruction that is either a NOP or call to
1580 * the function tracer. It checks the ftrace internal tables to
1581 * determine if the address belongs or not.
1583 unsigned long ftrace_location(unsigned long ip
)
1585 return ftrace_location_range(ip
, ip
);
1589 * ftrace_text_reserved - return true if range contains an ftrace location
1590 * @start: start of range to search
1591 * @end: end of range to search (inclusive). @end points to the last byte to check.
1593 * Returns 1 if @start and @end contains a ftrace location.
1594 * That is, the instruction that is either a NOP or call to
1595 * the function tracer. It checks the ftrace internal tables to
1596 * determine if the address belongs or not.
1598 int ftrace_text_reserved(const void *start
, const void *end
)
1602 ret
= ftrace_location_range((unsigned long)start
,
1603 (unsigned long)end
);
1608 /* Test if ops registered to this rec needs regs */
1609 static bool test_rec_ops_needs_regs(struct dyn_ftrace
*rec
)
1611 struct ftrace_ops
*ops
;
1612 bool keep_regs
= false;
1614 for (ops
= ftrace_ops_list
;
1615 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
1616 /* pass rec in as regs to have non-NULL val */
1617 if (ftrace_ops_test(ops
, rec
->ip
, rec
)) {
1618 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
) {
1628 static bool __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1632 struct ftrace_hash
*hash
;
1633 struct ftrace_hash
*other_hash
;
1634 struct ftrace_page
*pg
;
1635 struct dyn_ftrace
*rec
;
1636 bool update
= false;
1640 /* Only update if the ops has been registered */
1641 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1645 * In the filter_hash case:
1646 * If the count is zero, we update all records.
1647 * Otherwise we just update the items in the hash.
1649 * In the notrace_hash case:
1650 * We enable the update in the hash.
1651 * As disabling notrace means enabling the tracing,
1652 * and enabling notrace means disabling, the inc variable
1656 hash
= ops
->func_hash
->filter_hash
;
1657 other_hash
= ops
->func_hash
->notrace_hash
;
1658 if (ftrace_hash_empty(hash
))
1662 hash
= ops
->func_hash
->notrace_hash
;
1663 other_hash
= ops
->func_hash
->filter_hash
;
1665 * If the notrace hash has no items,
1666 * then there's nothing to do.
1668 if (ftrace_hash_empty(hash
))
1672 do_for_each_ftrace_rec(pg
, rec
) {
1673 int in_other_hash
= 0;
1677 if (rec
->flags
& FTRACE_FL_DISABLED
)
1682 * Only the filter_hash affects all records.
1683 * Update if the record is not in the notrace hash.
1685 if (!other_hash
|| !ftrace_lookup_ip(other_hash
, rec
->ip
))
1688 in_hash
= !!ftrace_lookup_ip(hash
, rec
->ip
);
1689 in_other_hash
= !!ftrace_lookup_ip(other_hash
, rec
->ip
);
1692 * If filter_hash is set, we want to match all functions
1693 * that are in the hash but not in the other hash.
1695 * If filter_hash is not set, then we are decrementing.
1696 * That means we match anything that is in the hash
1697 * and also in the other_hash. That is, we need to turn
1698 * off functions in the other hash because they are disabled
1701 if (filter_hash
&& in_hash
&& !in_other_hash
)
1703 else if (!filter_hash
&& in_hash
&&
1704 (in_other_hash
|| ftrace_hash_empty(other_hash
)))
1712 if (FTRACE_WARN_ON(ftrace_rec_count(rec
) == FTRACE_REF_MAX
))
1716 * If there's only a single callback registered to a
1717 * function, and the ops has a trampoline registered
1718 * for it, then we can call it directly.
1720 if (ftrace_rec_count(rec
) == 1 && ops
->trampoline
)
1721 rec
->flags
|= FTRACE_FL_TRAMP
;
1724 * If we are adding another function callback
1725 * to this function, and the previous had a
1726 * custom trampoline in use, then we need to go
1727 * back to the default trampoline.
1729 rec
->flags
&= ~FTRACE_FL_TRAMP
;
1732 * If any ops wants regs saved for this function
1733 * then all ops will get saved regs.
1735 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
1736 rec
->flags
|= FTRACE_FL_REGS
;
1738 if (FTRACE_WARN_ON(ftrace_rec_count(rec
) == 0))
1743 * If the rec had REGS enabled and the ops that is
1744 * being removed had REGS set, then see if there is
1745 * still any ops for this record that wants regs.
1746 * If not, we can stop recording them.
1748 if (ftrace_rec_count(rec
) > 0 &&
1749 rec
->flags
& FTRACE_FL_REGS
&&
1750 ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
) {
1751 if (!test_rec_ops_needs_regs(rec
))
1752 rec
->flags
&= ~FTRACE_FL_REGS
;
1756 * If the rec had TRAMP enabled, then it needs to
1757 * be cleared. As TRAMP can only be enabled iff
1758 * there is only a single ops attached to it.
1759 * In otherwords, always disable it on decrementing.
1760 * In the future, we may set it if rec count is
1761 * decremented to one, and the ops that is left
1764 rec
->flags
&= ~FTRACE_FL_TRAMP
;
1767 * flags will be cleared in ftrace_check_record()
1768 * if rec count is zero.
1773 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1774 update
|= ftrace_test_record(rec
, 1) != FTRACE_UPDATE_IGNORE
;
1776 /* Shortcut, if we handled all records, we are done. */
1777 if (!all
&& count
== hash
->count
)
1779 } while_for_each_ftrace_rec();
1784 static bool ftrace_hash_rec_disable(struct ftrace_ops
*ops
,
1787 return __ftrace_hash_rec_update(ops
, filter_hash
, 0);
1790 static bool ftrace_hash_rec_enable(struct ftrace_ops
*ops
,
1793 return __ftrace_hash_rec_update(ops
, filter_hash
, 1);
1796 static void ftrace_hash_rec_update_modify(struct ftrace_ops
*ops
,
1797 int filter_hash
, int inc
)
1799 struct ftrace_ops
*op
;
1801 __ftrace_hash_rec_update(ops
, filter_hash
, inc
);
1803 if (ops
->func_hash
!= &global_ops
.local_hash
)
1807 * If the ops shares the global_ops hash, then we need to update
1808 * all ops that are enabled and use this hash.
1810 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
1814 if (op
->func_hash
== &global_ops
.local_hash
)
1815 __ftrace_hash_rec_update(op
, filter_hash
, inc
);
1816 } while_for_each_ftrace_op(op
);
1819 static void ftrace_hash_rec_disable_modify(struct ftrace_ops
*ops
,
1822 ftrace_hash_rec_update_modify(ops
, filter_hash
, 0);
1825 static void ftrace_hash_rec_enable_modify(struct ftrace_ops
*ops
,
1828 ftrace_hash_rec_update_modify(ops
, filter_hash
, 1);
1832 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1833 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1834 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1835 * Note that old_hash and new_hash has below meanings
1836 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1837 * - If the hash is EMPTY_HASH, it hits nothing
1838 * - Anything else hits the recs which match the hash entries.
1840 static int __ftrace_hash_update_ipmodify(struct ftrace_ops
*ops
,
1841 struct ftrace_hash
*old_hash
,
1842 struct ftrace_hash
*new_hash
)
1844 struct ftrace_page
*pg
;
1845 struct dyn_ftrace
*rec
, *end
= NULL
;
1848 /* Only update if the ops has been registered */
1849 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1852 if (!(ops
->flags
& FTRACE_OPS_FL_IPMODIFY
))
1856 * Since the IPMODIFY is a very address sensitive action, we do not
1857 * allow ftrace_ops to set all functions to new hash.
1859 if (!new_hash
|| !old_hash
)
1862 /* Update rec->flags */
1863 do_for_each_ftrace_rec(pg
, rec
) {
1865 if (rec
->flags
& FTRACE_FL_DISABLED
)
1868 /* We need to update only differences of filter_hash */
1869 in_old
= !!ftrace_lookup_ip(old_hash
, rec
->ip
);
1870 in_new
= !!ftrace_lookup_ip(new_hash
, rec
->ip
);
1871 if (in_old
== in_new
)
1875 /* New entries must ensure no others are using it */
1876 if (rec
->flags
& FTRACE_FL_IPMODIFY
)
1878 rec
->flags
|= FTRACE_FL_IPMODIFY
;
1879 } else /* Removed entry */
1880 rec
->flags
&= ~FTRACE_FL_IPMODIFY
;
1881 } while_for_each_ftrace_rec();
1888 /* Roll back what we did above */
1889 do_for_each_ftrace_rec(pg
, rec
) {
1891 if (rec
->flags
& FTRACE_FL_DISABLED
)
1897 in_old
= !!ftrace_lookup_ip(old_hash
, rec
->ip
);
1898 in_new
= !!ftrace_lookup_ip(new_hash
, rec
->ip
);
1899 if (in_old
== in_new
)
1903 rec
->flags
&= ~FTRACE_FL_IPMODIFY
;
1905 rec
->flags
|= FTRACE_FL_IPMODIFY
;
1906 } while_for_each_ftrace_rec();
1912 static int ftrace_hash_ipmodify_enable(struct ftrace_ops
*ops
)
1914 struct ftrace_hash
*hash
= ops
->func_hash
->filter_hash
;
1916 if (ftrace_hash_empty(hash
))
1919 return __ftrace_hash_update_ipmodify(ops
, EMPTY_HASH
, hash
);
1922 /* Disabling always succeeds */
1923 static void ftrace_hash_ipmodify_disable(struct ftrace_ops
*ops
)
1925 struct ftrace_hash
*hash
= ops
->func_hash
->filter_hash
;
1927 if (ftrace_hash_empty(hash
))
1930 __ftrace_hash_update_ipmodify(ops
, hash
, EMPTY_HASH
);
1933 static int ftrace_hash_ipmodify_update(struct ftrace_ops
*ops
,
1934 struct ftrace_hash
*new_hash
)
1936 struct ftrace_hash
*old_hash
= ops
->func_hash
->filter_hash
;
1938 if (ftrace_hash_empty(old_hash
))
1941 if (ftrace_hash_empty(new_hash
))
1944 return __ftrace_hash_update_ipmodify(ops
, old_hash
, new_hash
);
1947 static void print_ip_ins(const char *fmt
, const unsigned char *p
)
1951 printk(KERN_CONT
"%s", fmt
);
1953 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
1954 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
1957 static struct ftrace_ops
*
1958 ftrace_find_tramp_ops_any(struct dyn_ftrace
*rec
);
1959 static struct ftrace_ops
*
1960 ftrace_find_tramp_ops_next(struct dyn_ftrace
*rec
, struct ftrace_ops
*ops
);
1962 enum ftrace_bug_type ftrace_bug_type
;
1963 const void *ftrace_expected
;
1965 static void print_bug_type(void)
1967 switch (ftrace_bug_type
) {
1968 case FTRACE_BUG_UNKNOWN
:
1970 case FTRACE_BUG_INIT
:
1971 pr_info("Initializing ftrace call sites\n");
1973 case FTRACE_BUG_NOP
:
1974 pr_info("Setting ftrace call site to NOP\n");
1976 case FTRACE_BUG_CALL
:
1977 pr_info("Setting ftrace call site to call ftrace function\n");
1979 case FTRACE_BUG_UPDATE
:
1980 pr_info("Updating ftrace call site to call a different ftrace function\n");
1986 * ftrace_bug - report and shutdown function tracer
1987 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1988 * @rec: The record that failed
1990 * The arch code that enables or disables the function tracing
1991 * can call ftrace_bug() when it has detected a problem in
1992 * modifying the code. @failed should be one of either:
1993 * EFAULT - if the problem happens on reading the @ip address
1994 * EINVAL - if what is read at @ip is not what was expected
1995 * EPERM - if the problem happens on writting to the @ip address
1997 void ftrace_bug(int failed
, struct dyn_ftrace
*rec
)
1999 unsigned long ip
= rec
? rec
->ip
: 0;
2003 FTRACE_WARN_ON_ONCE(1);
2004 pr_info("ftrace faulted on modifying ");
2008 FTRACE_WARN_ON_ONCE(1);
2009 pr_info("ftrace failed to modify ");
2011 print_ip_ins(" actual: ", (unsigned char *)ip
);
2013 if (ftrace_expected
) {
2014 print_ip_ins(" expected: ", ftrace_expected
);
2019 FTRACE_WARN_ON_ONCE(1);
2020 pr_info("ftrace faulted on writing ");
2024 FTRACE_WARN_ON_ONCE(1);
2025 pr_info("ftrace faulted on unknown error ");
2030 struct ftrace_ops
*ops
= NULL
;
2032 pr_info("ftrace record flags: %lx\n", rec
->flags
);
2033 pr_cont(" (%ld)%s", ftrace_rec_count(rec
),
2034 rec
->flags
& FTRACE_FL_REGS
? " R" : " ");
2035 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
2036 ops
= ftrace_find_tramp_ops_any(rec
);
2039 pr_cont("\ttramp: %pS (%pS)",
2040 (void *)ops
->trampoline
,
2042 ops
= ftrace_find_tramp_ops_next(rec
, ops
);
2045 pr_cont("\ttramp: ERROR!");
2048 ip
= ftrace_get_addr_curr(rec
);
2049 pr_cont("\n expected tramp: %lx\n", ip
);
2053 static int ftrace_check_record(struct dyn_ftrace
*rec
, int enable
, int update
)
2055 unsigned long flag
= 0UL;
2057 ftrace_bug_type
= FTRACE_BUG_UNKNOWN
;
2059 if (rec
->flags
& FTRACE_FL_DISABLED
)
2060 return FTRACE_UPDATE_IGNORE
;
2063 * If we are updating calls:
2065 * If the record has a ref count, then we need to enable it
2066 * because someone is using it.
2068 * Otherwise we make sure its disabled.
2070 * If we are disabling calls, then disable all records that
2073 if (enable
&& ftrace_rec_count(rec
))
2074 flag
= FTRACE_FL_ENABLED
;
2077 * If enabling and the REGS flag does not match the REGS_EN, or
2078 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2079 * this record. Set flags to fail the compare against ENABLED.
2082 if (!(rec
->flags
& FTRACE_FL_REGS
) !=
2083 !(rec
->flags
& FTRACE_FL_REGS_EN
))
2084 flag
|= FTRACE_FL_REGS
;
2086 if (!(rec
->flags
& FTRACE_FL_TRAMP
) !=
2087 !(rec
->flags
& FTRACE_FL_TRAMP_EN
))
2088 flag
|= FTRACE_FL_TRAMP
;
2091 /* If the state of this record hasn't changed, then do nothing */
2092 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
2093 return FTRACE_UPDATE_IGNORE
;
2096 /* Save off if rec is being enabled (for return value) */
2097 flag
^= rec
->flags
& FTRACE_FL_ENABLED
;
2100 rec
->flags
|= FTRACE_FL_ENABLED
;
2101 if (flag
& FTRACE_FL_REGS
) {
2102 if (rec
->flags
& FTRACE_FL_REGS
)
2103 rec
->flags
|= FTRACE_FL_REGS_EN
;
2105 rec
->flags
&= ~FTRACE_FL_REGS_EN
;
2107 if (flag
& FTRACE_FL_TRAMP
) {
2108 if (rec
->flags
& FTRACE_FL_TRAMP
)
2109 rec
->flags
|= FTRACE_FL_TRAMP_EN
;
2111 rec
->flags
&= ~FTRACE_FL_TRAMP_EN
;
2116 * If this record is being updated from a nop, then
2117 * return UPDATE_MAKE_CALL.
2119 * return UPDATE_MODIFY_CALL to tell the caller to convert
2120 * from the save regs, to a non-save regs function or
2121 * vice versa, or from a trampoline call.
2123 if (flag
& FTRACE_FL_ENABLED
) {
2124 ftrace_bug_type
= FTRACE_BUG_CALL
;
2125 return FTRACE_UPDATE_MAKE_CALL
;
2128 ftrace_bug_type
= FTRACE_BUG_UPDATE
;
2129 return FTRACE_UPDATE_MODIFY_CALL
;
2133 /* If there's no more users, clear all flags */
2134 if (!ftrace_rec_count(rec
))
2138 * Just disable the record, but keep the ops TRAMP
2139 * and REGS states. The _EN flags must be disabled though.
2141 rec
->flags
&= ~(FTRACE_FL_ENABLED
| FTRACE_FL_TRAMP_EN
|
2145 ftrace_bug_type
= FTRACE_BUG_NOP
;
2146 return FTRACE_UPDATE_MAKE_NOP
;
2150 * ftrace_update_record, set a record that now is tracing or not
2151 * @rec: the record to update
2152 * @enable: set to 1 if the record is tracing, zero to force disable
2154 * The records that represent all functions that can be traced need
2155 * to be updated when tracing has been enabled.
2157 int ftrace_update_record(struct dyn_ftrace
*rec
, int enable
)
2159 return ftrace_check_record(rec
, enable
, 1);
2163 * ftrace_test_record, check if the record has been enabled or not
2164 * @rec: the record to test
2165 * @enable: set to 1 to check if enabled, 0 if it is disabled
2167 * The arch code may need to test if a record is already set to
2168 * tracing to determine how to modify the function code that it
2171 int ftrace_test_record(struct dyn_ftrace
*rec
, int enable
)
2173 return ftrace_check_record(rec
, enable
, 0);
2176 static struct ftrace_ops
*
2177 ftrace_find_tramp_ops_any(struct dyn_ftrace
*rec
)
2179 struct ftrace_ops
*op
;
2180 unsigned long ip
= rec
->ip
;
2182 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2184 if (!op
->trampoline
)
2187 if (hash_contains_ip(ip
, op
->func_hash
))
2189 } while_for_each_ftrace_op(op
);
2194 static struct ftrace_ops
*
2195 ftrace_find_tramp_ops_next(struct dyn_ftrace
*rec
,
2196 struct ftrace_ops
*op
)
2198 unsigned long ip
= rec
->ip
;
2200 while_for_each_ftrace_op(op
) {
2202 if (!op
->trampoline
)
2205 if (hash_contains_ip(ip
, op
->func_hash
))
2212 static struct ftrace_ops
*
2213 ftrace_find_tramp_ops_curr(struct dyn_ftrace
*rec
)
2215 struct ftrace_ops
*op
;
2216 unsigned long ip
= rec
->ip
;
2219 * Need to check removed ops first.
2220 * If they are being removed, and this rec has a tramp,
2221 * and this rec is in the ops list, then it would be the
2222 * one with the tramp.
2225 if (hash_contains_ip(ip
, &removed_ops
->old_hash
))
2230 * Need to find the current trampoline for a rec.
2231 * Now, a trampoline is only attached to a rec if there
2232 * was a single 'ops' attached to it. But this can be called
2233 * when we are adding another op to the rec or removing the
2234 * current one. Thus, if the op is being added, we can
2235 * ignore it because it hasn't attached itself to the rec
2238 * If an ops is being modified (hooking to different functions)
2239 * then we don't care about the new functions that are being
2240 * added, just the old ones (that are probably being removed).
2242 * If we are adding an ops to a function that already is using
2243 * a trampoline, it needs to be removed (trampolines are only
2244 * for single ops connected), then an ops that is not being
2245 * modified also needs to be checked.
2247 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2249 if (!op
->trampoline
)
2253 * If the ops is being added, it hasn't gotten to
2254 * the point to be removed from this tree yet.
2256 if (op
->flags
& FTRACE_OPS_FL_ADDING
)
2261 * If the ops is being modified and is in the old
2262 * hash, then it is probably being removed from this
2265 if ((op
->flags
& FTRACE_OPS_FL_MODIFYING
) &&
2266 hash_contains_ip(ip
, &op
->old_hash
))
2269 * If the ops is not being added or modified, and it's
2270 * in its normal filter hash, then this must be the one
2273 if (!(op
->flags
& FTRACE_OPS_FL_MODIFYING
) &&
2274 hash_contains_ip(ip
, op
->func_hash
))
2277 } while_for_each_ftrace_op(op
);
2282 static struct ftrace_ops
*
2283 ftrace_find_tramp_ops_new(struct dyn_ftrace
*rec
)
2285 struct ftrace_ops
*op
;
2286 unsigned long ip
= rec
->ip
;
2288 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2289 /* pass rec in as regs to have non-NULL val */
2290 if (hash_contains_ip(ip
, op
->func_hash
))
2292 } while_for_each_ftrace_op(op
);
2298 * ftrace_get_addr_new - Get the call address to set to
2299 * @rec: The ftrace record descriptor
2301 * If the record has the FTRACE_FL_REGS set, that means that it
2302 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2303 * is not not set, then it wants to convert to the normal callback.
2305 * Returns the address of the trampoline to set to
2307 unsigned long ftrace_get_addr_new(struct dyn_ftrace
*rec
)
2309 struct ftrace_ops
*ops
;
2311 /* Trampolines take precedence over regs */
2312 if (rec
->flags
& FTRACE_FL_TRAMP
) {
2313 ops
= ftrace_find_tramp_ops_new(rec
);
2314 if (FTRACE_WARN_ON(!ops
|| !ops
->trampoline
)) {
2315 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2316 (void *)rec
->ip
, (void *)rec
->ip
, rec
->flags
);
2317 /* Ftrace is shutting down, return anything */
2318 return (unsigned long)FTRACE_ADDR
;
2320 return ops
->trampoline
;
2323 if (rec
->flags
& FTRACE_FL_REGS
)
2324 return (unsigned long)FTRACE_REGS_ADDR
;
2326 return (unsigned long)FTRACE_ADDR
;
2330 * ftrace_get_addr_curr - Get the call address that is already there
2331 * @rec: The ftrace record descriptor
2333 * The FTRACE_FL_REGS_EN is set when the record already points to
2334 * a function that saves all the regs. Basically the '_EN' version
2335 * represents the current state of the function.
2337 * Returns the address of the trampoline that is currently being called
2339 unsigned long ftrace_get_addr_curr(struct dyn_ftrace
*rec
)
2341 struct ftrace_ops
*ops
;
2343 /* Trampolines take precedence over regs */
2344 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
2345 ops
= ftrace_find_tramp_ops_curr(rec
);
2346 if (FTRACE_WARN_ON(!ops
)) {
2347 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2348 (void *)rec
->ip
, (void *)rec
->ip
);
2349 /* Ftrace is shutting down, return anything */
2350 return (unsigned long)FTRACE_ADDR
;
2352 return ops
->trampoline
;
2355 if (rec
->flags
& FTRACE_FL_REGS_EN
)
2356 return (unsigned long)FTRACE_REGS_ADDR
;
2358 return (unsigned long)FTRACE_ADDR
;
2362 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
2364 unsigned long ftrace_old_addr
;
2365 unsigned long ftrace_addr
;
2368 ftrace_addr
= ftrace_get_addr_new(rec
);
2370 /* This needs to be done before we call ftrace_update_record */
2371 ftrace_old_addr
= ftrace_get_addr_curr(rec
);
2373 ret
= ftrace_update_record(rec
, enable
);
2375 ftrace_bug_type
= FTRACE_BUG_UNKNOWN
;
2378 case FTRACE_UPDATE_IGNORE
:
2381 case FTRACE_UPDATE_MAKE_CALL
:
2382 ftrace_bug_type
= FTRACE_BUG_CALL
;
2383 return ftrace_make_call(rec
, ftrace_addr
);
2385 case FTRACE_UPDATE_MAKE_NOP
:
2386 ftrace_bug_type
= FTRACE_BUG_NOP
;
2387 return ftrace_make_nop(NULL
, rec
, ftrace_old_addr
);
2389 case FTRACE_UPDATE_MODIFY_CALL
:
2390 ftrace_bug_type
= FTRACE_BUG_UPDATE
;
2391 return ftrace_modify_call(rec
, ftrace_old_addr
, ftrace_addr
);
2394 return -1; /* unknow ftrace bug */
2397 void __weak
ftrace_replace_code(int mod_flags
)
2399 struct dyn_ftrace
*rec
;
2400 struct ftrace_page
*pg
;
2401 int enable
= mod_flags
& FTRACE_MODIFY_ENABLE_FL
;
2402 int schedulable
= mod_flags
& FTRACE_MODIFY_MAY_SLEEP_FL
;
2405 if (unlikely(ftrace_disabled
))
2408 do_for_each_ftrace_rec(pg
, rec
) {
2410 if (rec
->flags
& FTRACE_FL_DISABLED
)
2413 failed
= __ftrace_replace_code(rec
, enable
);
2415 ftrace_bug(failed
, rec
);
2416 /* Stop processing */
2421 } while_for_each_ftrace_rec();
2424 struct ftrace_rec_iter
{
2425 struct ftrace_page
*pg
;
2430 * ftrace_rec_iter_start, start up iterating over traced functions
2432 * Returns an iterator handle that is used to iterate over all
2433 * the records that represent address locations where functions
2436 * May return NULL if no records are available.
2438 struct ftrace_rec_iter
*ftrace_rec_iter_start(void)
2441 * We only use a single iterator.
2442 * Protected by the ftrace_lock mutex.
2444 static struct ftrace_rec_iter ftrace_rec_iter
;
2445 struct ftrace_rec_iter
*iter
= &ftrace_rec_iter
;
2447 iter
->pg
= ftrace_pages_start
;
2450 /* Could have empty pages */
2451 while (iter
->pg
&& !iter
->pg
->index
)
2452 iter
->pg
= iter
->pg
->next
;
2461 * ftrace_rec_iter_next, get the next record to process.
2462 * @iter: The handle to the iterator.
2464 * Returns the next iterator after the given iterator @iter.
2466 struct ftrace_rec_iter
*ftrace_rec_iter_next(struct ftrace_rec_iter
*iter
)
2470 if (iter
->index
>= iter
->pg
->index
) {
2471 iter
->pg
= iter
->pg
->next
;
2474 /* Could have empty pages */
2475 while (iter
->pg
&& !iter
->pg
->index
)
2476 iter
->pg
= iter
->pg
->next
;
2486 * ftrace_rec_iter_record, get the record at the iterator location
2487 * @iter: The current iterator location
2489 * Returns the record that the current @iter is at.
2491 struct dyn_ftrace
*ftrace_rec_iter_record(struct ftrace_rec_iter
*iter
)
2493 return &iter
->pg
->records
[iter
->index
];
2497 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
2501 if (unlikely(ftrace_disabled
))
2504 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
2506 ftrace_bug_type
= FTRACE_BUG_INIT
;
2507 ftrace_bug(ret
, rec
);
2514 * archs can override this function if they must do something
2515 * before the modifying code is performed.
2517 int __weak
ftrace_arch_code_modify_prepare(void)
2523 * archs can override this function if they must do something
2524 * after the modifying code is performed.
2526 int __weak
ftrace_arch_code_modify_post_process(void)
2531 void ftrace_modify_all_code(int command
)
2533 int update
= command
& FTRACE_UPDATE_TRACE_FUNC
;
2537 if (command
& FTRACE_MAY_SLEEP
)
2538 mod_flags
= FTRACE_MODIFY_MAY_SLEEP_FL
;
2541 * If the ftrace_caller calls a ftrace_ops func directly,
2542 * we need to make sure that it only traces functions it
2543 * expects to trace. When doing the switch of functions,
2544 * we need to update to the ftrace_ops_list_func first
2545 * before the transition between old and new calls are set,
2546 * as the ftrace_ops_list_func will check the ops hashes
2547 * to make sure the ops are having the right functions
2551 err
= ftrace_update_ftrace_func(ftrace_ops_list_func
);
2552 if (FTRACE_WARN_ON(err
))
2556 if (command
& FTRACE_UPDATE_CALLS
)
2557 ftrace_replace_code(mod_flags
| FTRACE_MODIFY_ENABLE_FL
);
2558 else if (command
& FTRACE_DISABLE_CALLS
)
2559 ftrace_replace_code(mod_flags
);
2561 if (update
&& ftrace_trace_function
!= ftrace_ops_list_func
) {
2562 function_trace_op
= set_function_trace_op
;
2564 /* If irqs are disabled, we are in stop machine */
2565 if (!irqs_disabled())
2566 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
2567 err
= ftrace_update_ftrace_func(ftrace_trace_function
);
2568 if (FTRACE_WARN_ON(err
))
2572 if (command
& FTRACE_START_FUNC_RET
)
2573 err
= ftrace_enable_ftrace_graph_caller();
2574 else if (command
& FTRACE_STOP_FUNC_RET
)
2575 err
= ftrace_disable_ftrace_graph_caller();
2576 FTRACE_WARN_ON(err
);
2579 static int __ftrace_modify_code(void *data
)
2581 int *command
= data
;
2583 ftrace_modify_all_code(*command
);
2589 * ftrace_run_stop_machine, go back to the stop machine method
2590 * @command: The command to tell ftrace what to do
2592 * If an arch needs to fall back to the stop machine method, the
2593 * it can call this function.
2595 void ftrace_run_stop_machine(int command
)
2597 stop_machine(__ftrace_modify_code
, &command
, NULL
);
2601 * arch_ftrace_update_code, modify the code to trace or not trace
2602 * @command: The command that needs to be done
2604 * Archs can override this function if it does not need to
2605 * run stop_machine() to modify code.
2607 void __weak
arch_ftrace_update_code(int command
)
2609 ftrace_run_stop_machine(command
);
2612 static void ftrace_run_update_code(int command
)
2616 ret
= ftrace_arch_code_modify_prepare();
2617 FTRACE_WARN_ON(ret
);
2622 * By default we use stop_machine() to modify the code.
2623 * But archs can do what ever they want as long as it
2624 * is safe. The stop_machine() is the safest, but also
2625 * produces the most overhead.
2627 arch_ftrace_update_code(command
);
2629 ret
= ftrace_arch_code_modify_post_process();
2630 FTRACE_WARN_ON(ret
);
2633 static void ftrace_run_modify_code(struct ftrace_ops
*ops
, int command
,
2634 struct ftrace_ops_hash
*old_hash
)
2636 ops
->flags
|= FTRACE_OPS_FL_MODIFYING
;
2637 ops
->old_hash
.filter_hash
= old_hash
->filter_hash
;
2638 ops
->old_hash
.notrace_hash
= old_hash
->notrace_hash
;
2639 ftrace_run_update_code(command
);
2640 ops
->old_hash
.filter_hash
= NULL
;
2641 ops
->old_hash
.notrace_hash
= NULL
;
2642 ops
->flags
&= ~FTRACE_OPS_FL_MODIFYING
;
2645 static ftrace_func_t saved_ftrace_func
;
2646 static int ftrace_start_up
;
2648 void __weak
arch_ftrace_trampoline_free(struct ftrace_ops
*ops
)
2652 static void ftrace_startup_enable(int command
)
2654 if (saved_ftrace_func
!= ftrace_trace_function
) {
2655 saved_ftrace_func
= ftrace_trace_function
;
2656 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2659 if (!command
|| !ftrace_enabled
)
2662 ftrace_run_update_code(command
);
2665 static void ftrace_startup_all(int command
)
2667 update_all_ops
= true;
2668 ftrace_startup_enable(command
);
2669 update_all_ops
= false;
2672 int ftrace_startup(struct ftrace_ops
*ops
, int command
)
2676 if (unlikely(ftrace_disabled
))
2679 ret
= __register_ftrace_function(ops
);
2686 * Note that ftrace probes uses this to start up
2687 * and modify functions it will probe. But we still
2688 * set the ADDING flag for modification, as probes
2689 * do not have trampolines. If they add them in the
2690 * future, then the probes will need to distinguish
2691 * between adding and updating probes.
2693 ops
->flags
|= FTRACE_OPS_FL_ENABLED
| FTRACE_OPS_FL_ADDING
;
2695 ret
= ftrace_hash_ipmodify_enable(ops
);
2697 /* Rollback registration process */
2698 __unregister_ftrace_function(ops
);
2700 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
2704 if (ftrace_hash_rec_enable(ops
, 1))
2705 command
|= FTRACE_UPDATE_CALLS
;
2707 ftrace_startup_enable(command
);
2709 ops
->flags
&= ~FTRACE_OPS_FL_ADDING
;
2714 int ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
2718 if (unlikely(ftrace_disabled
))
2721 ret
= __unregister_ftrace_function(ops
);
2727 * Just warn in case of unbalance, no need to kill ftrace, it's not
2728 * critical but the ftrace_call callers may be never nopped again after
2729 * further ftrace uses.
2731 WARN_ON_ONCE(ftrace_start_up
< 0);
2733 /* Disabling ipmodify never fails */
2734 ftrace_hash_ipmodify_disable(ops
);
2736 if (ftrace_hash_rec_disable(ops
, 1))
2737 command
|= FTRACE_UPDATE_CALLS
;
2739 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
2741 if (saved_ftrace_func
!= ftrace_trace_function
) {
2742 saved_ftrace_func
= ftrace_trace_function
;
2743 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2746 if (!command
|| !ftrace_enabled
) {
2748 * If these are dynamic or per_cpu ops, they still
2749 * need their data freed. Since, function tracing is
2750 * not currently active, we can just free them
2751 * without synchronizing all CPUs.
2753 if (ops
->flags
& FTRACE_OPS_FL_DYNAMIC
)
2760 * If the ops uses a trampoline, then it needs to be
2761 * tested first on update.
2763 ops
->flags
|= FTRACE_OPS_FL_REMOVING
;
2766 /* The trampoline logic checks the old hashes */
2767 ops
->old_hash
.filter_hash
= ops
->func_hash
->filter_hash
;
2768 ops
->old_hash
.notrace_hash
= ops
->func_hash
->notrace_hash
;
2770 ftrace_run_update_code(command
);
2773 * If there's no more ops registered with ftrace, run a
2774 * sanity check to make sure all rec flags are cleared.
2776 if (rcu_dereference_protected(ftrace_ops_list
,
2777 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
2778 struct ftrace_page
*pg
;
2779 struct dyn_ftrace
*rec
;
2781 do_for_each_ftrace_rec(pg
, rec
) {
2782 if (FTRACE_WARN_ON_ONCE(rec
->flags
& ~FTRACE_FL_DISABLED
))
2783 pr_warn(" %pS flags:%lx\n",
2784 (void *)rec
->ip
, rec
->flags
);
2785 } while_for_each_ftrace_rec();
2788 ops
->old_hash
.filter_hash
= NULL
;
2789 ops
->old_hash
.notrace_hash
= NULL
;
2792 ops
->flags
&= ~FTRACE_OPS_FL_REMOVING
;
2795 * Dynamic ops may be freed, we must make sure that all
2796 * callers are done before leaving this function.
2797 * The same goes for freeing the per_cpu data of the per_cpu
2800 if (ops
->flags
& FTRACE_OPS_FL_DYNAMIC
) {
2802 * We need to do a hard force of sched synchronization.
2803 * This is because we use preempt_disable() to do RCU, but
2804 * the function tracers can be called where RCU is not watching
2805 * (like before user_exit()). We can not rely on the RCU
2806 * infrastructure to do the synchronization, thus we must do it
2809 schedule_on_each_cpu(ftrace_sync
);
2812 * When the kernel is preeptive, tasks can be preempted
2813 * while on a ftrace trampoline. Just scheduling a task on
2814 * a CPU is not good enough to flush them. Calling
2815 * synchornize_rcu_tasks() will wait for those tasks to
2816 * execute and either schedule voluntarily or enter user space.
2818 if (IS_ENABLED(CONFIG_PREEMPT
))
2819 synchronize_rcu_tasks();
2822 arch_ftrace_trampoline_free(ops
);
2828 static void ftrace_startup_sysctl(void)
2832 if (unlikely(ftrace_disabled
))
2835 /* Force update next time */
2836 saved_ftrace_func
= NULL
;
2837 /* ftrace_start_up is true if we want ftrace running */
2838 if (ftrace_start_up
) {
2839 command
= FTRACE_UPDATE_CALLS
;
2840 if (ftrace_graph_active
)
2841 command
|= FTRACE_START_FUNC_RET
;
2842 ftrace_startup_enable(command
);
2846 static void ftrace_shutdown_sysctl(void)
2850 if (unlikely(ftrace_disabled
))
2853 /* ftrace_start_up is true if ftrace is running */
2854 if (ftrace_start_up
) {
2855 command
= FTRACE_DISABLE_CALLS
;
2856 if (ftrace_graph_active
)
2857 command
|= FTRACE_STOP_FUNC_RET
;
2858 ftrace_run_update_code(command
);
2862 static u64 ftrace_update_time
;
2863 unsigned long ftrace_update_tot_cnt
;
2865 static inline int ops_traces_mod(struct ftrace_ops
*ops
)
2868 * Filter_hash being empty will default to trace module.
2869 * But notrace hash requires a test of individual module functions.
2871 return ftrace_hash_empty(ops
->func_hash
->filter_hash
) &&
2872 ftrace_hash_empty(ops
->func_hash
->notrace_hash
);
2876 * Check if the current ops references the record.
2878 * If the ops traces all functions, then it was already accounted for.
2879 * If the ops does not trace the current record function, skip it.
2880 * If the ops ignores the function via notrace filter, skip it.
2883 ops_references_rec(struct ftrace_ops
*ops
, struct dyn_ftrace
*rec
)
2885 /* If ops isn't enabled, ignore it */
2886 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
2889 /* If ops traces all then it includes this function */
2890 if (ops_traces_mod(ops
))
2893 /* The function must be in the filter */
2894 if (!ftrace_hash_empty(ops
->func_hash
->filter_hash
) &&
2895 !__ftrace_lookup_ip(ops
->func_hash
->filter_hash
, rec
->ip
))
2898 /* If in notrace hash, we ignore it too */
2899 if (ftrace_lookup_ip(ops
->func_hash
->notrace_hash
, rec
->ip
))
2905 static int ftrace_update_code(struct module
*mod
, struct ftrace_page
*new_pgs
)
2907 struct ftrace_page
*pg
;
2908 struct dyn_ftrace
*p
;
2910 unsigned long update_cnt
= 0;
2911 unsigned long rec_flags
= 0;
2914 start
= ftrace_now(raw_smp_processor_id());
2917 * When a module is loaded, this function is called to convert
2918 * the calls to mcount in its text to nops, and also to create
2919 * an entry in the ftrace data. Now, if ftrace is activated
2920 * after this call, but before the module sets its text to
2921 * read-only, the modification of enabling ftrace can fail if
2922 * the read-only is done while ftrace is converting the calls.
2923 * To prevent this, the module's records are set as disabled
2924 * and will be enabled after the call to set the module's text
2928 rec_flags
|= FTRACE_FL_DISABLED
;
2930 for (pg
= new_pgs
; pg
; pg
= pg
->next
) {
2932 for (i
= 0; i
< pg
->index
; i
++) {
2934 /* If something went wrong, bail without enabling anything */
2935 if (unlikely(ftrace_disabled
))
2938 p
= &pg
->records
[i
];
2939 p
->flags
= rec_flags
;
2941 #ifndef CC_USING_NOP_MCOUNT
2943 * Do the initial record conversion from mcount jump
2944 * to the NOP instructions.
2946 if (!ftrace_code_disable(mod
, p
))
2954 stop
= ftrace_now(raw_smp_processor_id());
2955 ftrace_update_time
= stop
- start
;
2956 ftrace_update_tot_cnt
+= update_cnt
;
2961 static int ftrace_allocate_records(struct ftrace_page
*pg
, int count
)
2966 if (WARN_ON(!count
))
2969 order
= get_count_order(DIV_ROUND_UP(count
, ENTRIES_PER_PAGE
));
2972 * We want to fill as much as possible. No more than a page
2975 while ((PAGE_SIZE
<< order
) / ENTRY_SIZE
>= count
+ ENTRIES_PER_PAGE
)
2979 pg
->records
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
2982 /* if we can't allocate this size, try something smaller */
2989 cnt
= (PAGE_SIZE
<< order
) / ENTRY_SIZE
;
2998 static struct ftrace_page
*
2999 ftrace_allocate_pages(unsigned long num_to_init
)
3001 struct ftrace_page
*start_pg
;
3002 struct ftrace_page
*pg
;
3009 start_pg
= pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
3014 * Try to allocate as much as possible in one continues
3015 * location that fills in all of the space. We want to
3016 * waste as little space as possible.
3019 cnt
= ftrace_allocate_records(pg
, num_to_init
);
3027 pg
->next
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
3039 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
3040 free_pages((unsigned long)pg
->records
, order
);
3041 start_pg
= pg
->next
;
3045 pr_info("ftrace: FAILED to allocate memory for functions\n");
3049 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3051 struct ftrace_iterator
{
3055 struct ftrace_page
*pg
;
3056 struct dyn_ftrace
*func
;
3057 struct ftrace_func_probe
*probe
;
3058 struct ftrace_func_entry
*probe_entry
;
3059 struct trace_parser parser
;
3060 struct ftrace_hash
*hash
;
3061 struct ftrace_ops
*ops
;
3062 struct trace_array
*tr
;
3063 struct list_head
*mod_list
;
3070 t_probe_next(struct seq_file
*m
, loff_t
*pos
)
3072 struct ftrace_iterator
*iter
= m
->private;
3073 struct trace_array
*tr
= iter
->ops
->private;
3074 struct list_head
*func_probes
;
3075 struct ftrace_hash
*hash
;
3076 struct list_head
*next
;
3077 struct hlist_node
*hnd
= NULL
;
3078 struct hlist_head
*hhd
;
3087 func_probes
= &tr
->func_probes
;
3088 if (list_empty(func_probes
))
3092 next
= func_probes
->next
;
3093 iter
->probe
= list_entry(next
, struct ftrace_func_probe
, list
);
3096 if (iter
->probe_entry
)
3097 hnd
= &iter
->probe_entry
->hlist
;
3099 hash
= iter
->probe
->ops
.func_hash
->filter_hash
;
3100 size
= 1 << hash
->size_bits
;
3103 if (iter
->pidx
>= size
) {
3104 if (iter
->probe
->list
.next
== func_probes
)
3106 next
= iter
->probe
->list
.next
;
3107 iter
->probe
= list_entry(next
, struct ftrace_func_probe
, list
);
3108 hash
= iter
->probe
->ops
.func_hash
->filter_hash
;
3109 size
= 1 << hash
->size_bits
;
3113 hhd
= &hash
->buckets
[iter
->pidx
];
3115 if (hlist_empty(hhd
)) {
3131 if (WARN_ON_ONCE(!hnd
))
3134 iter
->probe_entry
= hlist_entry(hnd
, struct ftrace_func_entry
, hlist
);
3139 static void *t_probe_start(struct seq_file
*m
, loff_t
*pos
)
3141 struct ftrace_iterator
*iter
= m
->private;
3145 if (!(iter
->flags
& FTRACE_ITER_DO_PROBES
))
3148 if (iter
->mod_pos
> *pos
)
3152 iter
->probe_entry
= NULL
;
3154 for (l
= 0; l
<= (*pos
- iter
->mod_pos
); ) {
3155 p
= t_probe_next(m
, &l
);
3162 /* Only set this if we have an item */
3163 iter
->flags
|= FTRACE_ITER_PROBE
;
3169 t_probe_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
3171 struct ftrace_func_entry
*probe_entry
;
3172 struct ftrace_probe_ops
*probe_ops
;
3173 struct ftrace_func_probe
*probe
;
3175 probe
= iter
->probe
;
3176 probe_entry
= iter
->probe_entry
;
3178 if (WARN_ON_ONCE(!probe
|| !probe_entry
))
3181 probe_ops
= probe
->probe_ops
;
3183 if (probe_ops
->print
)
3184 return probe_ops
->print(m
, probe_entry
->ip
, probe_ops
, probe
->data
);
3186 seq_printf(m
, "%ps:%ps\n", (void *)probe_entry
->ip
,
3187 (void *)probe_ops
->func
);
3193 t_mod_next(struct seq_file
*m
, loff_t
*pos
)
3195 struct ftrace_iterator
*iter
= m
->private;
3196 struct trace_array
*tr
= iter
->tr
;
3201 iter
->mod_list
= iter
->mod_list
->next
;
3203 if (iter
->mod_list
== &tr
->mod_trace
||
3204 iter
->mod_list
== &tr
->mod_notrace
) {
3205 iter
->flags
&= ~FTRACE_ITER_MOD
;
3209 iter
->mod_pos
= *pos
;
3214 static void *t_mod_start(struct seq_file
*m
, loff_t
*pos
)
3216 struct ftrace_iterator
*iter
= m
->private;
3220 if (iter
->func_pos
> *pos
)
3223 iter
->mod_pos
= iter
->func_pos
;
3225 /* probes are only available if tr is set */
3229 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
3230 p
= t_mod_next(m
, &l
);
3235 iter
->flags
&= ~FTRACE_ITER_MOD
;
3236 return t_probe_start(m
, pos
);
3239 /* Only set this if we have an item */
3240 iter
->flags
|= FTRACE_ITER_MOD
;
3246 t_mod_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
3248 struct ftrace_mod_load
*ftrace_mod
;
3249 struct trace_array
*tr
= iter
->tr
;
3251 if (WARN_ON_ONCE(!iter
->mod_list
) ||
3252 iter
->mod_list
== &tr
->mod_trace
||
3253 iter
->mod_list
== &tr
->mod_notrace
)
3256 ftrace_mod
= list_entry(iter
->mod_list
, struct ftrace_mod_load
, list
);
3258 if (ftrace_mod
->func
)
3259 seq_printf(m
, "%s", ftrace_mod
->func
);
3263 seq_printf(m
, ":mod:%s\n", ftrace_mod
->module
);
3269 t_func_next(struct seq_file
*m
, loff_t
*pos
)
3271 struct ftrace_iterator
*iter
= m
->private;
3272 struct dyn_ftrace
*rec
= NULL
;
3277 if (iter
->idx
>= iter
->pg
->index
) {
3278 if (iter
->pg
->next
) {
3279 iter
->pg
= iter
->pg
->next
;
3284 rec
= &iter
->pg
->records
[iter
->idx
++];
3285 if (((iter
->flags
& (FTRACE_ITER_FILTER
| FTRACE_ITER_NOTRACE
)) &&
3286 !ftrace_lookup_ip(iter
->hash
, rec
->ip
)) ||
3288 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
3289 !(rec
->flags
& FTRACE_FL_ENABLED
))) {
3299 iter
->pos
= iter
->func_pos
= *pos
;
3306 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3308 struct ftrace_iterator
*iter
= m
->private;
3309 loff_t l
= *pos
; /* t_probe_start() must use original pos */
3312 if (unlikely(ftrace_disabled
))
3315 if (iter
->flags
& FTRACE_ITER_PROBE
)
3316 return t_probe_next(m
, pos
);
3318 if (iter
->flags
& FTRACE_ITER_MOD
)
3319 return t_mod_next(m
, pos
);
3321 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
3322 /* next must increment pos, and t_probe_start does not */
3324 return t_mod_start(m
, &l
);
3327 ret
= t_func_next(m
, pos
);
3330 return t_mod_start(m
, &l
);
3335 static void reset_iter_read(struct ftrace_iterator
*iter
)
3339 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
| FTRACE_ITER_PROBE
| FTRACE_ITER_MOD
);
3342 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
3344 struct ftrace_iterator
*iter
= m
->private;
3348 mutex_lock(&ftrace_lock
);
3350 if (unlikely(ftrace_disabled
))
3354 * If an lseek was done, then reset and start from beginning.
3356 if (*pos
< iter
->pos
)
3357 reset_iter_read(iter
);
3360 * For set_ftrace_filter reading, if we have the filter
3361 * off, we can short cut and just print out that all
3362 * functions are enabled.
3364 if ((iter
->flags
& (FTRACE_ITER_FILTER
| FTRACE_ITER_NOTRACE
)) &&
3365 ftrace_hash_empty(iter
->hash
)) {
3366 iter
->func_pos
= 1; /* Account for the message */
3368 return t_mod_start(m
, pos
);
3369 iter
->flags
|= FTRACE_ITER_PRINTALL
;
3370 /* reset in case of seek/pread */
3371 iter
->flags
&= ~FTRACE_ITER_PROBE
;
3375 if (iter
->flags
& FTRACE_ITER_MOD
)
3376 return t_mod_start(m
, pos
);
3379 * Unfortunately, we need to restart at ftrace_pages_start
3380 * every time we let go of the ftrace_mutex. This is because
3381 * those pointers can change without the lock.
3383 iter
->pg
= ftrace_pages_start
;
3385 for (l
= 0; l
<= *pos
; ) {
3386 p
= t_func_next(m
, &l
);
3392 return t_mod_start(m
, pos
);
3397 static void t_stop(struct seq_file
*m
, void *p
)
3399 mutex_unlock(&ftrace_lock
);
3403 arch_ftrace_trampoline_func(struct ftrace_ops
*ops
, struct dyn_ftrace
*rec
)
3408 static void add_trampoline_func(struct seq_file
*m
, struct ftrace_ops
*ops
,
3409 struct dyn_ftrace
*rec
)
3413 ptr
= arch_ftrace_trampoline_func(ops
, rec
);
3415 seq_printf(m
, " ->%pS", ptr
);
3418 static int t_show(struct seq_file
*m
, void *v
)
3420 struct ftrace_iterator
*iter
= m
->private;
3421 struct dyn_ftrace
*rec
;
3423 if (iter
->flags
& FTRACE_ITER_PROBE
)
3424 return t_probe_show(m
, iter
);
3426 if (iter
->flags
& FTRACE_ITER_MOD
)
3427 return t_mod_show(m
, iter
);
3429 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
3430 if (iter
->flags
& FTRACE_ITER_NOTRACE
)
3431 seq_puts(m
, "#### no functions disabled ####\n");
3433 seq_puts(m
, "#### all functions enabled ####\n");
3442 seq_printf(m
, "%ps", (void *)rec
->ip
);
3443 if (iter
->flags
& FTRACE_ITER_ENABLED
) {
3444 struct ftrace_ops
*ops
;
3446 seq_printf(m
, " (%ld)%s%s",
3447 ftrace_rec_count(rec
),
3448 rec
->flags
& FTRACE_FL_REGS
? " R" : " ",
3449 rec
->flags
& FTRACE_FL_IPMODIFY
? " I" : " ");
3450 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
3451 ops
= ftrace_find_tramp_ops_any(rec
);
3454 seq_printf(m
, "\ttramp: %pS (%pS)",
3455 (void *)ops
->trampoline
,
3457 add_trampoline_func(m
, ops
, rec
);
3458 ops
= ftrace_find_tramp_ops_next(rec
, ops
);
3461 seq_puts(m
, "\ttramp: ERROR!");
3463 add_trampoline_func(m
, NULL
, rec
);
3472 static const struct seq_operations show_ftrace_seq_ops
= {
3480 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
3482 struct ftrace_iterator
*iter
;
3484 if (unlikely(ftrace_disabled
))
3487 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
3491 iter
->pg
= ftrace_pages_start
;
3492 iter
->ops
= &global_ops
;
3498 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
3500 struct ftrace_iterator
*iter
;
3502 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
3506 iter
->pg
= ftrace_pages_start
;
3507 iter
->flags
= FTRACE_ITER_ENABLED
;
3508 iter
->ops
= &global_ops
;
3514 * ftrace_regex_open - initialize function tracer filter files
3515 * @ops: The ftrace_ops that hold the hash filters
3516 * @flag: The type of filter to process
3517 * @inode: The inode, usually passed in to your open routine
3518 * @file: The file, usually passed in to your open routine
3520 * ftrace_regex_open() initializes the filter files for the
3521 * @ops. Depending on @flag it may process the filter hash or
3522 * the notrace hash of @ops. With this called from the open
3523 * routine, you can use ftrace_filter_write() for the write
3524 * routine if @flag has FTRACE_ITER_FILTER set, or
3525 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3526 * tracing_lseek() should be used as the lseek routine, and
3527 * release must call ftrace_regex_release().
3530 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
3531 struct inode
*inode
, struct file
*file
)
3533 struct ftrace_iterator
*iter
;
3534 struct ftrace_hash
*hash
;
3535 struct list_head
*mod_head
;
3536 struct trace_array
*tr
= ops
->private;
3539 ftrace_ops_init(ops
);
3541 if (unlikely(ftrace_disabled
))
3544 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
3548 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
3557 mutex_lock(&ops
->func_hash
->regex_lock
);
3559 if (flag
& FTRACE_ITER_NOTRACE
) {
3560 hash
= ops
->func_hash
->notrace_hash
;
3561 mod_head
= tr
? &tr
->mod_notrace
: NULL
;
3563 hash
= ops
->func_hash
->filter_hash
;
3564 mod_head
= tr
? &tr
->mod_trace
: NULL
;
3567 iter
->mod_list
= mod_head
;
3569 if (file
->f_mode
& FMODE_WRITE
) {
3570 const int size_bits
= FTRACE_HASH_DEFAULT_BITS
;
3572 if (file
->f_flags
& O_TRUNC
) {
3573 iter
->hash
= alloc_ftrace_hash(size_bits
);
3574 clear_ftrace_mod_list(mod_head
);
3576 iter
->hash
= alloc_and_copy_ftrace_hash(size_bits
, hash
);
3580 trace_parser_put(&iter
->parser
);
3588 if (file
->f_mode
& FMODE_READ
) {
3589 iter
->pg
= ftrace_pages_start
;
3591 ret
= seq_open(file
, &show_ftrace_seq_ops
);
3593 struct seq_file
*m
= file
->private_data
;
3597 free_ftrace_hash(iter
->hash
);
3598 trace_parser_put(&iter
->parser
);
3602 file
->private_data
= iter
;
3605 mutex_unlock(&ops
->func_hash
->regex_lock
);
3611 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
3613 struct ftrace_ops
*ops
= inode
->i_private
;
3615 return ftrace_regex_open(ops
,
3616 FTRACE_ITER_FILTER
| FTRACE_ITER_DO_PROBES
,
3621 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
3623 struct ftrace_ops
*ops
= inode
->i_private
;
3625 return ftrace_regex_open(ops
, FTRACE_ITER_NOTRACE
,
3629 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3630 struct ftrace_glob
{
3637 * If symbols in an architecture don't correspond exactly to the user-visible
3638 * name of what they represent, it is possible to define this function to
3639 * perform the necessary adjustments.
3641 char * __weak
arch_ftrace_match_adjust(char *str
, const char *search
)
3646 static int ftrace_match(char *str
, struct ftrace_glob
*g
)
3651 str
= arch_ftrace_match_adjust(str
, g
->search
);
3655 if (strcmp(str
, g
->search
) == 0)
3658 case MATCH_FRONT_ONLY
:
3659 if (strncmp(str
, g
->search
, g
->len
) == 0)
3662 case MATCH_MIDDLE_ONLY
:
3663 if (strstr(str
, g
->search
))
3666 case MATCH_END_ONLY
:
3668 if (slen
>= g
->len
&&
3669 memcmp(str
+ slen
- g
->len
, g
->search
, g
->len
) == 0)
3673 if (glob_match(g
->search
, str
))
3682 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int clear_filter
)
3684 struct ftrace_func_entry
*entry
;
3687 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
3689 /* Do nothing if it doesn't exist */
3693 free_hash_entry(hash
, entry
);
3695 /* Do nothing if it exists */
3699 ret
= add_hash_entry(hash
, rec
->ip
);
3705 add_rec_by_index(struct ftrace_hash
*hash
, struct ftrace_glob
*func_g
,
3708 long index
= simple_strtoul(func_g
->search
, NULL
, 0);
3709 struct ftrace_page
*pg
;
3710 struct dyn_ftrace
*rec
;
3712 /* The index starts at 1 */
3716 do_for_each_ftrace_rec(pg
, rec
) {
3717 if (pg
->index
<= index
) {
3719 /* this is a double loop, break goes to the next page */
3722 rec
= &pg
->records
[index
];
3723 enter_record(hash
, rec
, clear_filter
);
3725 } while_for_each_ftrace_rec();
3730 ftrace_match_record(struct dyn_ftrace
*rec
, struct ftrace_glob
*func_g
,
3731 struct ftrace_glob
*mod_g
, int exclude_mod
)
3733 char str
[KSYM_SYMBOL_LEN
];
3736 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
3739 int mod_matches
= (modname
) ? ftrace_match(modname
, mod_g
) : 0;
3741 /* blank module name to match all modules */
3743 /* blank module globbing: modname xor exclude_mod */
3744 if (!exclude_mod
!= !modname
)
3750 * exclude_mod is set to trace everything but the given
3751 * module. If it is set and the module matches, then
3752 * return 0. If it is not set, and the module doesn't match
3753 * also return 0. Otherwise, check the function to see if
3756 if (!mod_matches
== !exclude_mod
)
3759 /* blank search means to match all funcs in the mod */
3764 return ftrace_match(str
, func_g
);
3768 match_records(struct ftrace_hash
*hash
, char *func
, int len
, char *mod
)
3770 struct ftrace_page
*pg
;
3771 struct dyn_ftrace
*rec
;
3772 struct ftrace_glob func_g
= { .type
= MATCH_FULL
};
3773 struct ftrace_glob mod_g
= { .type
= MATCH_FULL
};
3774 struct ftrace_glob
*mod_match
= (mod
) ? &mod_g
: NULL
;
3775 int exclude_mod
= 0;
3778 int clear_filter
= 0;
3781 func_g
.type
= filter_parse_regex(func
, len
, &func_g
.search
,
3783 func_g
.len
= strlen(func_g
.search
);
3787 mod_g
.type
= filter_parse_regex(mod
, strlen(mod
),
3788 &mod_g
.search
, &exclude_mod
);
3789 mod_g
.len
= strlen(mod_g
.search
);
3792 mutex_lock(&ftrace_lock
);
3794 if (unlikely(ftrace_disabled
))
3797 if (func_g
.type
== MATCH_INDEX
) {
3798 found
= add_rec_by_index(hash
, &func_g
, clear_filter
);
3802 do_for_each_ftrace_rec(pg
, rec
) {
3804 if (rec
->flags
& FTRACE_FL_DISABLED
)
3807 if (ftrace_match_record(rec
, &func_g
, mod_match
, exclude_mod
)) {
3808 ret
= enter_record(hash
, rec
, clear_filter
);
3815 } while_for_each_ftrace_rec();
3817 mutex_unlock(&ftrace_lock
);
3823 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
3825 return match_records(hash
, buff
, len
, NULL
);
3828 static void ftrace_ops_update_code(struct ftrace_ops
*ops
,
3829 struct ftrace_ops_hash
*old_hash
)
3831 struct ftrace_ops
*op
;
3833 if (!ftrace_enabled
)
3836 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
) {
3837 ftrace_run_modify_code(ops
, FTRACE_UPDATE_CALLS
, old_hash
);
3842 * If this is the shared global_ops filter, then we need to
3843 * check if there is another ops that shares it, is enabled.
3844 * If so, we still need to run the modify code.
3846 if (ops
->func_hash
!= &global_ops
.local_hash
)
3849 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
3850 if (op
->func_hash
== &global_ops
.local_hash
&&
3851 op
->flags
& FTRACE_OPS_FL_ENABLED
) {
3852 ftrace_run_modify_code(op
, FTRACE_UPDATE_CALLS
, old_hash
);
3853 /* Only need to do this once */
3856 } while_for_each_ftrace_op(op
);
3859 static int ftrace_hash_move_and_update_ops(struct ftrace_ops
*ops
,
3860 struct ftrace_hash
**orig_hash
,
3861 struct ftrace_hash
*hash
,
3864 struct ftrace_ops_hash old_hash_ops
;
3865 struct ftrace_hash
*old_hash
;
3868 old_hash
= *orig_hash
;
3869 old_hash_ops
.filter_hash
= ops
->func_hash
->filter_hash
;
3870 old_hash_ops
.notrace_hash
= ops
->func_hash
->notrace_hash
;
3871 ret
= ftrace_hash_move(ops
, enable
, orig_hash
, hash
);
3873 ftrace_ops_update_code(ops
, &old_hash_ops
);
3874 free_ftrace_hash_rcu(old_hash
);
3879 static bool module_exists(const char *module
)
3881 /* All modules have the symbol __this_module */
3882 const char this_mod
[] = "__this_module";
3883 char modname
[MAX_PARAM_PREFIX_LEN
+ sizeof(this_mod
) + 2];
3887 n
= snprintf(modname
, sizeof(modname
), "%s:%s", module
, this_mod
);
3889 if (n
> sizeof(modname
) - 1)
3892 val
= module_kallsyms_lookup_name(modname
);
3896 static int cache_mod(struct trace_array
*tr
,
3897 const char *func
, char *module
, int enable
)
3899 struct ftrace_mod_load
*ftrace_mod
, *n
;
3900 struct list_head
*head
= enable
? &tr
->mod_trace
: &tr
->mod_notrace
;
3903 mutex_lock(&ftrace_lock
);
3905 /* We do not cache inverse filters */
3906 if (func
[0] == '!') {
3910 /* Look to remove this hash */
3911 list_for_each_entry_safe(ftrace_mod
, n
, head
, list
) {
3912 if (strcmp(ftrace_mod
->module
, module
) != 0)
3915 /* no func matches all */
3916 if (strcmp(func
, "*") == 0 ||
3917 (ftrace_mod
->func
&&
3918 strcmp(ftrace_mod
->func
, func
) == 0)) {
3920 free_ftrace_mod(ftrace_mod
);
3928 /* We only care about modules that have not been loaded yet */
3929 if (module_exists(module
))
3932 /* Save this string off, and execute it when the module is loaded */
3933 ret
= ftrace_add_mod(tr
, func
, module
, enable
);
3935 mutex_unlock(&ftrace_lock
);
3941 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3942 int reset
, int enable
);
3944 #ifdef CONFIG_MODULES
3945 static void process_mod_list(struct list_head
*head
, struct ftrace_ops
*ops
,
3946 char *mod
, bool enable
)
3948 struct ftrace_mod_load
*ftrace_mod
, *n
;
3949 struct ftrace_hash
**orig_hash
, *new_hash
;
3950 LIST_HEAD(process_mods
);
3954 mutex_lock(&ops
->func_hash
->regex_lock
);
3957 orig_hash
= &ops
->func_hash
->filter_hash
;
3959 orig_hash
= &ops
->func_hash
->notrace_hash
;
3961 new_hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
,
3964 goto out
; /* warn? */
3966 mutex_lock(&ftrace_lock
);
3968 list_for_each_entry_safe(ftrace_mod
, n
, head
, list
) {
3970 if (strcmp(ftrace_mod
->module
, mod
) != 0)
3973 if (ftrace_mod
->func
)
3974 func
= kstrdup(ftrace_mod
->func
, GFP_KERNEL
);
3976 func
= kstrdup("*", GFP_KERNEL
);
3978 if (!func
) /* warn? */
3981 list_del(&ftrace_mod
->list
);
3982 list_add(&ftrace_mod
->list
, &process_mods
);
3984 /* Use the newly allocated func, as it may be "*" */
3985 kfree(ftrace_mod
->func
);
3986 ftrace_mod
->func
= func
;
3989 mutex_unlock(&ftrace_lock
);
3991 list_for_each_entry_safe(ftrace_mod
, n
, &process_mods
, list
) {
3993 func
= ftrace_mod
->func
;
3995 /* Grabs ftrace_lock, which is why we have this extra step */
3996 match_records(new_hash
, func
, strlen(func
), mod
);
3997 free_ftrace_mod(ftrace_mod
);
4000 if (enable
&& list_empty(head
))
4001 new_hash
->flags
&= ~FTRACE_HASH_FL_MOD
;
4003 mutex_lock(&ftrace_lock
);
4005 ret
= ftrace_hash_move_and_update_ops(ops
, orig_hash
,
4007 mutex_unlock(&ftrace_lock
);
4010 mutex_unlock(&ops
->func_hash
->regex_lock
);
4012 free_ftrace_hash(new_hash
);
4015 static void process_cached_mods(const char *mod_name
)
4017 struct trace_array
*tr
;
4020 mod
= kstrdup(mod_name
, GFP_KERNEL
);
4024 mutex_lock(&trace_types_lock
);
4025 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
4026 if (!list_empty(&tr
->mod_trace
))
4027 process_mod_list(&tr
->mod_trace
, tr
->ops
, mod
, true);
4028 if (!list_empty(&tr
->mod_notrace
))
4029 process_mod_list(&tr
->mod_notrace
, tr
->ops
, mod
, false);
4031 mutex_unlock(&trace_types_lock
);
4038 * We register the module command as a template to show others how
4039 * to register the a command as well.
4043 ftrace_mod_callback(struct trace_array
*tr
, struct ftrace_hash
*hash
,
4044 char *func_orig
, char *cmd
, char *module
, int enable
)
4049 /* match_records() modifies func, and we need the original */
4050 func
= kstrdup(func_orig
, GFP_KERNEL
);
4055 * cmd == 'mod' because we only registered this func
4056 * for the 'mod' ftrace_func_command.
4057 * But if you register one func with multiple commands,
4058 * you can tell which command was used by the cmd
4061 ret
= match_records(hash
, func
, strlen(func
), module
);
4065 return cache_mod(tr
, func_orig
, module
, enable
);
4071 static struct ftrace_func_command ftrace_mod_cmd
= {
4073 .func
= ftrace_mod_callback
,
4076 static int __init
ftrace_mod_cmd_init(void)
4078 return register_ftrace_command(&ftrace_mod_cmd
);
4080 core_initcall(ftrace_mod_cmd_init
);
4082 static void function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
,
4083 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
4085 struct ftrace_probe_ops
*probe_ops
;
4086 struct ftrace_func_probe
*probe
;
4088 probe
= container_of(op
, struct ftrace_func_probe
, ops
);
4089 probe_ops
= probe
->probe_ops
;
4092 * Disable preemption for these calls to prevent a RCU grace
4093 * period. This syncs the hash iteration and freeing of items
4094 * on the hash. rcu_read_lock is too dangerous here.
4096 preempt_disable_notrace();
4097 probe_ops
->func(ip
, parent_ip
, probe
->tr
, probe_ops
, probe
->data
);
4098 preempt_enable_notrace();
4101 struct ftrace_func_map
{
4102 struct ftrace_func_entry entry
;
4106 struct ftrace_func_mapper
{
4107 struct ftrace_hash hash
;
4111 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4113 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4115 struct ftrace_func_mapper
*allocate_ftrace_func_mapper(void)
4117 struct ftrace_hash
*hash
;
4120 * The mapper is simply a ftrace_hash, but since the entries
4121 * in the hash are not ftrace_func_entry type, we define it
4122 * as a separate structure.
4124 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
4125 return (struct ftrace_func_mapper
*)hash
;
4129 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4130 * @mapper: The mapper that has the ip maps
4131 * @ip: the instruction pointer to find the data for
4133 * Returns the data mapped to @ip if found otherwise NULL. The return
4134 * is actually the address of the mapper data pointer. The address is
4135 * returned for use cases where the data is no bigger than a long, and
4136 * the user can use the data pointer as its data instead of having to
4137 * allocate more memory for the reference.
4139 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper
*mapper
,
4142 struct ftrace_func_entry
*entry
;
4143 struct ftrace_func_map
*map
;
4145 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
4149 map
= (struct ftrace_func_map
*)entry
;
4154 * ftrace_func_mapper_add_ip - Map some data to an ip
4155 * @mapper: The mapper that has the ip maps
4156 * @ip: The instruction pointer address to map @data to
4157 * @data: The data to map to @ip
4159 * Returns 0 on succes otherwise an error.
4161 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper
*mapper
,
4162 unsigned long ip
, void *data
)
4164 struct ftrace_func_entry
*entry
;
4165 struct ftrace_func_map
*map
;
4167 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
4171 map
= kmalloc(sizeof(*map
), GFP_KERNEL
);
4178 __add_hash_entry(&mapper
->hash
, &map
->entry
);
4184 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4185 * @mapper: The mapper that has the ip maps
4186 * @ip: The instruction pointer address to remove the data from
4188 * Returns the data if it is found, otherwise NULL.
4189 * Note, if the data pointer is used as the data itself, (see
4190 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4191 * if the data pointer was set to zero.
4193 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper
*mapper
,
4196 struct ftrace_func_entry
*entry
;
4197 struct ftrace_func_map
*map
;
4200 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
4204 map
= (struct ftrace_func_map
*)entry
;
4207 remove_hash_entry(&mapper
->hash
, entry
);
4214 * free_ftrace_func_mapper - free a mapping of ips and data
4215 * @mapper: The mapper that has the ip maps
4216 * @free_func: A function to be called on each data item.
4218 * This is used to free the function mapper. The @free_func is optional
4219 * and can be used if the data needs to be freed as well.
4221 void free_ftrace_func_mapper(struct ftrace_func_mapper
*mapper
,
4222 ftrace_mapper_func free_func
)
4224 struct ftrace_func_entry
*entry
;
4225 struct ftrace_func_map
*map
;
4226 struct hlist_head
*hhd
;
4227 int size
= 1 << mapper
->hash
.size_bits
;
4230 if (free_func
&& mapper
->hash
.count
) {
4231 for (i
= 0; i
< size
; i
++) {
4232 hhd
= &mapper
->hash
.buckets
[i
];
4233 hlist_for_each_entry(entry
, hhd
, hlist
) {
4234 map
= (struct ftrace_func_map
*)entry
;
4239 free_ftrace_hash(&mapper
->hash
);
4242 static void release_probe(struct ftrace_func_probe
*probe
)
4244 struct ftrace_probe_ops
*probe_ops
;
4246 mutex_lock(&ftrace_lock
);
4248 WARN_ON(probe
->ref
<= 0);
4250 /* Subtract the ref that was used to protect this instance */
4254 probe_ops
= probe
->probe_ops
;
4256 * Sending zero as ip tells probe_ops to free
4257 * the probe->data itself
4259 if (probe_ops
->free
)
4260 probe_ops
->free(probe_ops
, probe
->tr
, 0, probe
->data
);
4261 list_del(&probe
->list
);
4264 mutex_unlock(&ftrace_lock
);
4267 static void acquire_probe_locked(struct ftrace_func_probe
*probe
)
4270 * Add one ref to keep it from being freed when releasing the
4271 * ftrace_lock mutex.
4277 register_ftrace_function_probe(char *glob
, struct trace_array
*tr
,
4278 struct ftrace_probe_ops
*probe_ops
,
4281 struct ftrace_func_entry
*entry
;
4282 struct ftrace_func_probe
*probe
;
4283 struct ftrace_hash
**orig_hash
;
4284 struct ftrace_hash
*old_hash
;
4285 struct ftrace_hash
*hash
;
4294 /* We do not support '!' for function probes */
4295 if (WARN_ON(glob
[0] == '!'))
4299 mutex_lock(&ftrace_lock
);
4300 /* Check if the probe_ops is already registered */
4301 list_for_each_entry(probe
, &tr
->func_probes
, list
) {
4302 if (probe
->probe_ops
== probe_ops
)
4305 if (&probe
->list
== &tr
->func_probes
) {
4306 probe
= kzalloc(sizeof(*probe
), GFP_KERNEL
);
4308 mutex_unlock(&ftrace_lock
);
4311 probe
->probe_ops
= probe_ops
;
4312 probe
->ops
.func
= function_trace_probe_call
;
4314 ftrace_ops_init(&probe
->ops
);
4315 list_add(&probe
->list
, &tr
->func_probes
);
4318 acquire_probe_locked(probe
);
4320 mutex_unlock(&ftrace_lock
);
4322 mutex_lock(&probe
->ops
.func_hash
->regex_lock
);
4324 orig_hash
= &probe
->ops
.func_hash
->filter_hash
;
4325 old_hash
= *orig_hash
;
4326 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, old_hash
);
4328 ret
= ftrace_match_records(hash
, glob
, strlen(glob
));
4330 /* Nothing found? */
4337 size
= 1 << hash
->size_bits
;
4338 for (i
= 0; i
< size
; i
++) {
4339 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
4340 if (ftrace_lookup_ip(old_hash
, entry
->ip
))
4343 * The caller might want to do something special
4344 * for each function we find. We call the callback
4345 * to give the caller an opportunity to do so.
4347 if (probe_ops
->init
) {
4348 ret
= probe_ops
->init(probe_ops
, tr
,
4352 if (probe_ops
->free
&& count
)
4353 probe_ops
->free(probe_ops
, tr
,
4363 mutex_lock(&ftrace_lock
);
4366 /* Nothing was added? */
4371 ret
= ftrace_hash_move_and_update_ops(&probe
->ops
, orig_hash
,
4376 /* One ref for each new function traced */
4377 probe
->ref
+= count
;
4379 if (!(probe
->ops
.flags
& FTRACE_OPS_FL_ENABLED
))
4380 ret
= ftrace_startup(&probe
->ops
, 0);
4383 mutex_unlock(&ftrace_lock
);
4388 mutex_unlock(&probe
->ops
.func_hash
->regex_lock
);
4389 free_ftrace_hash(hash
);
4391 release_probe(probe
);
4396 if (!probe_ops
->free
|| !count
)
4399 /* Failed to do the move, need to call the free functions */
4400 for (i
= 0; i
< size
; i
++) {
4401 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
4402 if (ftrace_lookup_ip(old_hash
, entry
->ip
))
4404 probe_ops
->free(probe_ops
, tr
, entry
->ip
, probe
->data
);
4411 unregister_ftrace_function_probe_func(char *glob
, struct trace_array
*tr
,
4412 struct ftrace_probe_ops
*probe_ops
)
4414 struct ftrace_ops_hash old_hash_ops
;
4415 struct ftrace_func_entry
*entry
;
4416 struct ftrace_func_probe
*probe
;
4417 struct ftrace_glob func_g
;
4418 struct ftrace_hash
**orig_hash
;
4419 struct ftrace_hash
*old_hash
;
4420 struct ftrace_hash
*hash
= NULL
;
4421 struct hlist_node
*tmp
;
4422 struct hlist_head hhd
;
4423 char str
[KSYM_SYMBOL_LEN
];
4425 int i
, ret
= -ENODEV
;
4428 if (!glob
|| !strlen(glob
) || !strcmp(glob
, "*"))
4429 func_g
.search
= NULL
;
4433 func_g
.type
= filter_parse_regex(glob
, strlen(glob
),
4434 &func_g
.search
, ¬);
4435 func_g
.len
= strlen(func_g
.search
);
4437 /* we do not support '!' for function probes */
4442 mutex_lock(&ftrace_lock
);
4443 /* Check if the probe_ops is already registered */
4444 list_for_each_entry(probe
, &tr
->func_probes
, list
) {
4445 if (probe
->probe_ops
== probe_ops
)
4448 if (&probe
->list
== &tr
->func_probes
)
4449 goto err_unlock_ftrace
;
4452 if (!(probe
->ops
.flags
& FTRACE_OPS_FL_INITIALIZED
))
4453 goto err_unlock_ftrace
;
4455 acquire_probe_locked(probe
);
4457 mutex_unlock(&ftrace_lock
);
4459 mutex_lock(&probe
->ops
.func_hash
->regex_lock
);
4461 orig_hash
= &probe
->ops
.func_hash
->filter_hash
;
4462 old_hash
= *orig_hash
;
4464 if (ftrace_hash_empty(old_hash
))
4467 old_hash_ops
.filter_hash
= old_hash
;
4468 /* Probes only have filters */
4469 old_hash_ops
.notrace_hash
= NULL
;
4472 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, old_hash
);
4476 INIT_HLIST_HEAD(&hhd
);
4478 size
= 1 << hash
->size_bits
;
4479 for (i
= 0; i
< size
; i
++) {
4480 hlist_for_each_entry_safe(entry
, tmp
, &hash
->buckets
[i
], hlist
) {
4482 if (func_g
.search
) {
4483 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
4485 if (!ftrace_match(str
, &func_g
))
4489 remove_hash_entry(hash
, entry
);
4490 hlist_add_head(&entry
->hlist
, &hhd
);
4494 /* Nothing found? */
4500 mutex_lock(&ftrace_lock
);
4502 WARN_ON(probe
->ref
< count
);
4504 probe
->ref
-= count
;
4506 if (ftrace_hash_empty(hash
))
4507 ftrace_shutdown(&probe
->ops
, 0);
4509 ret
= ftrace_hash_move_and_update_ops(&probe
->ops
, orig_hash
,
4512 /* still need to update the function call sites */
4513 if (ftrace_enabled
&& !ftrace_hash_empty(hash
))
4514 ftrace_run_modify_code(&probe
->ops
, FTRACE_UPDATE_CALLS
,
4518 hlist_for_each_entry_safe(entry
, tmp
, &hhd
, hlist
) {
4519 hlist_del(&entry
->hlist
);
4520 if (probe_ops
->free
)
4521 probe_ops
->free(probe_ops
, tr
, entry
->ip
, probe
->data
);
4524 mutex_unlock(&ftrace_lock
);
4527 mutex_unlock(&probe
->ops
.func_hash
->regex_lock
);
4528 free_ftrace_hash(hash
);
4530 release_probe(probe
);
4535 mutex_unlock(&ftrace_lock
);
4539 void clear_ftrace_function_probes(struct trace_array
*tr
)
4541 struct ftrace_func_probe
*probe
, *n
;
4543 list_for_each_entry_safe(probe
, n
, &tr
->func_probes
, list
)
4544 unregister_ftrace_function_probe_func(NULL
, tr
, probe
->probe_ops
);
4547 static LIST_HEAD(ftrace_commands
);
4548 static DEFINE_MUTEX(ftrace_cmd_mutex
);
4551 * Currently we only register ftrace commands from __init, so mark this
4554 __init
int register_ftrace_command(struct ftrace_func_command
*cmd
)
4556 struct ftrace_func_command
*p
;
4559 mutex_lock(&ftrace_cmd_mutex
);
4560 list_for_each_entry(p
, &ftrace_commands
, list
) {
4561 if (strcmp(cmd
->name
, p
->name
) == 0) {
4566 list_add(&cmd
->list
, &ftrace_commands
);
4568 mutex_unlock(&ftrace_cmd_mutex
);
4574 * Currently we only unregister ftrace commands from __init, so mark
4577 __init
int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
4579 struct ftrace_func_command
*p
, *n
;
4582 mutex_lock(&ftrace_cmd_mutex
);
4583 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
4584 if (strcmp(cmd
->name
, p
->name
) == 0) {
4586 list_del_init(&p
->list
);
4591 mutex_unlock(&ftrace_cmd_mutex
);
4596 static int ftrace_process_regex(struct ftrace_iterator
*iter
,
4597 char *buff
, int len
, int enable
)
4599 struct ftrace_hash
*hash
= iter
->hash
;
4600 struct trace_array
*tr
= iter
->ops
->private;
4601 char *func
, *command
, *next
= buff
;
4602 struct ftrace_func_command
*p
;
4605 func
= strsep(&next
, ":");
4608 ret
= ftrace_match_records(hash
, func
, len
);
4618 command
= strsep(&next
, ":");
4620 mutex_lock(&ftrace_cmd_mutex
);
4621 list_for_each_entry(p
, &ftrace_commands
, list
) {
4622 if (strcmp(p
->name
, command
) == 0) {
4623 ret
= p
->func(tr
, hash
, func
, command
, next
, enable
);
4628 mutex_unlock(&ftrace_cmd_mutex
);
4634 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
4635 size_t cnt
, loff_t
*ppos
, int enable
)
4637 struct ftrace_iterator
*iter
;
4638 struct trace_parser
*parser
;
4644 if (file
->f_mode
& FMODE_READ
) {
4645 struct seq_file
*m
= file
->private_data
;
4648 iter
= file
->private_data
;
4650 if (unlikely(ftrace_disabled
))
4653 /* iter->hash is a local copy, so we don't need regex_lock */
4655 parser
= &iter
->parser
;
4656 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
4658 if (read
>= 0 && trace_parser_loaded(parser
) &&
4659 !trace_parser_cont(parser
)) {
4660 ret
= ftrace_process_regex(iter
, parser
->buffer
,
4661 parser
->idx
, enable
);
4662 trace_parser_clear(parser
);
4673 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
4674 size_t cnt
, loff_t
*ppos
)
4676 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
4680 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
4681 size_t cnt
, loff_t
*ppos
)
4683 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
4687 ftrace_match_addr(struct ftrace_hash
*hash
, unsigned long ip
, int remove
)
4689 struct ftrace_func_entry
*entry
;
4691 if (!ftrace_location(ip
))
4695 entry
= ftrace_lookup_ip(hash
, ip
);
4698 free_hash_entry(hash
, entry
);
4702 return add_hash_entry(hash
, ip
);
4706 ftrace_set_hash(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
4707 unsigned long ip
, int remove
, int reset
, int enable
)
4709 struct ftrace_hash
**orig_hash
;
4710 struct ftrace_hash
*hash
;
4713 if (unlikely(ftrace_disabled
))
4716 mutex_lock(&ops
->func_hash
->regex_lock
);
4719 orig_hash
= &ops
->func_hash
->filter_hash
;
4721 orig_hash
= &ops
->func_hash
->notrace_hash
;
4724 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
4726 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
4730 goto out_regex_unlock
;
4733 if (buf
&& !ftrace_match_records(hash
, buf
, len
)) {
4735 goto out_regex_unlock
;
4738 ret
= ftrace_match_addr(hash
, ip
, remove
);
4740 goto out_regex_unlock
;
4743 mutex_lock(&ftrace_lock
);
4744 ret
= ftrace_hash_move_and_update_ops(ops
, orig_hash
, hash
, enable
);
4745 mutex_unlock(&ftrace_lock
);
4748 mutex_unlock(&ops
->func_hash
->regex_lock
);
4750 free_ftrace_hash(hash
);
4755 ftrace_set_addr(struct ftrace_ops
*ops
, unsigned long ip
, int remove
,
4756 int reset
, int enable
)
4758 return ftrace_set_hash(ops
, 0, 0, ip
, remove
, reset
, enable
);
4762 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4763 * @ops - the ops to set the filter with
4764 * @ip - the address to add to or remove from the filter.
4765 * @remove - non zero to remove the ip from the filter
4766 * @reset - non zero to reset all filters before applying this filter.
4768 * Filters denote which functions should be enabled when tracing is enabled
4769 * If @ip is NULL, it failes to update filter.
4771 int ftrace_set_filter_ip(struct ftrace_ops
*ops
, unsigned long ip
,
4772 int remove
, int reset
)
4774 ftrace_ops_init(ops
);
4775 return ftrace_set_addr(ops
, ip
, remove
, reset
, 1);
4777 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip
);
4780 * ftrace_ops_set_global_filter - setup ops to use global filters
4781 * @ops - the ops which will use the global filters
4783 * ftrace users who need global function trace filtering should call this.
4784 * It can set the global filter only if ops were not initialized before.
4786 void ftrace_ops_set_global_filter(struct ftrace_ops
*ops
)
4788 if (ops
->flags
& FTRACE_OPS_FL_INITIALIZED
)
4791 ftrace_ops_init(ops
);
4792 ops
->func_hash
= &global_ops
.local_hash
;
4794 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter
);
4797 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
4798 int reset
, int enable
)
4800 return ftrace_set_hash(ops
, buf
, len
, 0, 0, reset
, enable
);
4804 * ftrace_set_filter - set a function to filter on in ftrace
4805 * @ops - the ops to set the filter with
4806 * @buf - the string that holds the function filter text.
4807 * @len - the length of the string.
4808 * @reset - non zero to reset all filters before applying this filter.
4810 * Filters denote which functions should be enabled when tracing is enabled.
4811 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4813 int ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
4816 ftrace_ops_init(ops
);
4817 return ftrace_set_regex(ops
, buf
, len
, reset
, 1);
4819 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
4822 * ftrace_set_notrace - set a function to not trace in ftrace
4823 * @ops - the ops to set the notrace filter with
4824 * @buf - the string that holds the function notrace text.
4825 * @len - the length of the string.
4826 * @reset - non zero to reset all filters before applying this filter.
4828 * Notrace Filters denote which functions should not be enabled when tracing
4829 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4832 int ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
4835 ftrace_ops_init(ops
);
4836 return ftrace_set_regex(ops
, buf
, len
, reset
, 0);
4838 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
4840 * ftrace_set_global_filter - set a function to filter on with global tracers
4841 * @buf - the string that holds the function filter text.
4842 * @len - the length of the string.
4843 * @reset - non zero to reset all filters before applying this filter.
4845 * Filters denote which functions should be enabled when tracing is enabled.
4846 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4848 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
4850 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
4852 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
4855 * ftrace_set_global_notrace - set a function to not trace with global tracers
4856 * @buf - the string that holds the function notrace text.
4857 * @len - the length of the string.
4858 * @reset - non zero to reset all filters before applying this filter.
4860 * Notrace Filters denote which functions should not be enabled when tracing
4861 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4864 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
4866 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
4868 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
4871 * command line interface to allow users to set filters on boot up.
4873 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4874 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4875 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4877 /* Used by function selftest to not test if filter is set */
4878 bool ftrace_filter_param __initdata
;
4880 static int __init
set_ftrace_notrace(char *str
)
4882 ftrace_filter_param
= true;
4883 strlcpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
4886 __setup("ftrace_notrace=", set_ftrace_notrace
);
4888 static int __init
set_ftrace_filter(char *str
)
4890 ftrace_filter_param
= true;
4891 strlcpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
4894 __setup("ftrace_filter=", set_ftrace_filter
);
4896 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4897 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4898 static char ftrace_graph_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4899 static int ftrace_graph_set_hash(struct ftrace_hash
*hash
, char *buffer
);
4901 static int __init
set_graph_function(char *str
)
4903 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
4906 __setup("ftrace_graph_filter=", set_graph_function
);
4908 static int __init
set_graph_notrace_function(char *str
)
4910 strlcpy(ftrace_graph_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
4913 __setup("ftrace_graph_notrace=", set_graph_notrace_function
);
4915 static int __init
set_graph_max_depth_function(char *str
)
4919 fgraph_max_depth
= simple_strtoul(str
, NULL
, 0);
4922 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function
);
4924 static void __init
set_ftrace_early_graph(char *buf
, int enable
)
4928 struct ftrace_hash
*hash
;
4930 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
4935 func
= strsep(&buf
, ",");
4936 /* we allow only one expression at a time */
4937 ret
= ftrace_graph_set_hash(hash
, func
);
4939 printk(KERN_DEBUG
"ftrace: function %s not "
4940 "traceable\n", func
);
4944 ftrace_graph_hash
= hash
;
4946 ftrace_graph_notrace_hash
= hash
;
4948 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4951 ftrace_set_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
4955 ftrace_ops_init(ops
);
4958 func
= strsep(&buf
, ",");
4959 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
4963 static void __init
set_ftrace_early_filters(void)
4965 if (ftrace_filter_buf
[0])
4966 ftrace_set_early_filter(&global_ops
, ftrace_filter_buf
, 1);
4967 if (ftrace_notrace_buf
[0])
4968 ftrace_set_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
4969 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4970 if (ftrace_graph_buf
[0])
4971 set_ftrace_early_graph(ftrace_graph_buf
, 1);
4972 if (ftrace_graph_notrace_buf
[0])
4973 set_ftrace_early_graph(ftrace_graph_notrace_buf
, 0);
4974 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4977 int ftrace_regex_release(struct inode
*inode
, struct file
*file
)
4979 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
4980 struct ftrace_iterator
*iter
;
4981 struct ftrace_hash
**orig_hash
;
4982 struct trace_parser
*parser
;
4986 if (file
->f_mode
& FMODE_READ
) {
4988 seq_release(inode
, file
);
4990 iter
= file
->private_data
;
4992 parser
= &iter
->parser
;
4993 if (trace_parser_loaded(parser
)) {
4994 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
4997 trace_parser_put(parser
);
4999 mutex_lock(&iter
->ops
->func_hash
->regex_lock
);
5001 if (file
->f_mode
& FMODE_WRITE
) {
5002 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
5005 orig_hash
= &iter
->ops
->func_hash
->filter_hash
;
5006 if (iter
->tr
&& !list_empty(&iter
->tr
->mod_trace
))
5007 iter
->hash
->flags
|= FTRACE_HASH_FL_MOD
;
5009 orig_hash
= &iter
->ops
->func_hash
->notrace_hash
;
5011 mutex_lock(&ftrace_lock
);
5012 ret
= ftrace_hash_move_and_update_ops(iter
->ops
, orig_hash
,
5013 iter
->hash
, filter_hash
);
5014 mutex_unlock(&ftrace_lock
);
5016 /* For read only, the hash is the ops hash */
5020 mutex_unlock(&iter
->ops
->func_hash
->regex_lock
);
5021 free_ftrace_hash(iter
->hash
);
5027 static const struct file_operations ftrace_avail_fops
= {
5028 .open
= ftrace_avail_open
,
5030 .llseek
= seq_lseek
,
5031 .release
= seq_release_private
,
5034 static const struct file_operations ftrace_enabled_fops
= {
5035 .open
= ftrace_enabled_open
,
5037 .llseek
= seq_lseek
,
5038 .release
= seq_release_private
,
5041 static const struct file_operations ftrace_filter_fops
= {
5042 .open
= ftrace_filter_open
,
5044 .write
= ftrace_filter_write
,
5045 .llseek
= tracing_lseek
,
5046 .release
= ftrace_regex_release
,
5049 static const struct file_operations ftrace_notrace_fops
= {
5050 .open
= ftrace_notrace_open
,
5052 .write
= ftrace_notrace_write
,
5053 .llseek
= tracing_lseek
,
5054 .release
= ftrace_regex_release
,
5057 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5059 static DEFINE_MUTEX(graph_lock
);
5061 struct ftrace_hash
*ftrace_graph_hash
= EMPTY_HASH
;
5062 struct ftrace_hash
*ftrace_graph_notrace_hash
= EMPTY_HASH
;
5064 enum graph_filter_type
{
5065 GRAPH_FILTER_NOTRACE
= 0,
5066 GRAPH_FILTER_FUNCTION
,
5069 #define FTRACE_GRAPH_EMPTY ((void *)1)
5071 struct ftrace_graph_data
{
5072 struct ftrace_hash
*hash
;
5073 struct ftrace_func_entry
*entry
;
5074 int idx
; /* for hash table iteration */
5075 enum graph_filter_type type
;
5076 struct ftrace_hash
*new_hash
;
5077 const struct seq_operations
*seq_ops
;
5078 struct trace_parser parser
;
5082 __g_next(struct seq_file
*m
, loff_t
*pos
)
5084 struct ftrace_graph_data
*fgd
= m
->private;
5085 struct ftrace_func_entry
*entry
= fgd
->entry
;
5086 struct hlist_head
*head
;
5087 int i
, idx
= fgd
->idx
;
5089 if (*pos
>= fgd
->hash
->count
)
5093 hlist_for_each_entry_continue(entry
, hlist
) {
5101 for (i
= idx
; i
< 1 << fgd
->hash
->size_bits
; i
++) {
5102 head
= &fgd
->hash
->buckets
[i
];
5103 hlist_for_each_entry(entry
, head
, hlist
) {
5113 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
5116 return __g_next(m
, pos
);
5119 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
5121 struct ftrace_graph_data
*fgd
= m
->private;
5123 mutex_lock(&graph_lock
);
5125 if (fgd
->type
== GRAPH_FILTER_FUNCTION
)
5126 fgd
->hash
= rcu_dereference_protected(ftrace_graph_hash
,
5127 lockdep_is_held(&graph_lock
));
5129 fgd
->hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
5130 lockdep_is_held(&graph_lock
));
5132 /* Nothing, tell g_show to print all functions are enabled */
5133 if (ftrace_hash_empty(fgd
->hash
) && !*pos
)
5134 return FTRACE_GRAPH_EMPTY
;
5138 return __g_next(m
, pos
);
5141 static void g_stop(struct seq_file
*m
, void *p
)
5143 mutex_unlock(&graph_lock
);
5146 static int g_show(struct seq_file
*m
, void *v
)
5148 struct ftrace_func_entry
*entry
= v
;
5153 if (entry
== FTRACE_GRAPH_EMPTY
) {
5154 struct ftrace_graph_data
*fgd
= m
->private;
5156 if (fgd
->type
== GRAPH_FILTER_FUNCTION
)
5157 seq_puts(m
, "#### all functions enabled ####\n");
5159 seq_puts(m
, "#### no functions disabled ####\n");
5163 seq_printf(m
, "%ps\n", (void *)entry
->ip
);
5168 static const struct seq_operations ftrace_graph_seq_ops
= {
5176 __ftrace_graph_open(struct inode
*inode
, struct file
*file
,
5177 struct ftrace_graph_data
*fgd
)
5180 struct ftrace_hash
*new_hash
= NULL
;
5182 if (file
->f_mode
& FMODE_WRITE
) {
5183 const int size_bits
= FTRACE_HASH_DEFAULT_BITS
;
5185 if (trace_parser_get_init(&fgd
->parser
, FTRACE_BUFF_MAX
))
5188 if (file
->f_flags
& O_TRUNC
)
5189 new_hash
= alloc_ftrace_hash(size_bits
);
5191 new_hash
= alloc_and_copy_ftrace_hash(size_bits
,
5199 if (file
->f_mode
& FMODE_READ
) {
5200 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
5202 struct seq_file
*m
= file
->private_data
;
5206 free_ftrace_hash(new_hash
);
5210 file
->private_data
= fgd
;
5213 if (ret
< 0 && file
->f_mode
& FMODE_WRITE
)
5214 trace_parser_put(&fgd
->parser
);
5216 fgd
->new_hash
= new_hash
;
5219 * All uses of fgd->hash must be taken with the graph_lock
5220 * held. The graph_lock is going to be released, so force
5221 * fgd->hash to be reinitialized when it is taken again.
5229 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
5231 struct ftrace_graph_data
*fgd
;
5234 if (unlikely(ftrace_disabled
))
5237 fgd
= kmalloc(sizeof(*fgd
), GFP_KERNEL
);
5241 mutex_lock(&graph_lock
);
5243 fgd
->hash
= rcu_dereference_protected(ftrace_graph_hash
,
5244 lockdep_is_held(&graph_lock
));
5245 fgd
->type
= GRAPH_FILTER_FUNCTION
;
5246 fgd
->seq_ops
= &ftrace_graph_seq_ops
;
5248 ret
= __ftrace_graph_open(inode
, file
, fgd
);
5252 mutex_unlock(&graph_lock
);
5257 ftrace_graph_notrace_open(struct inode
*inode
, struct file
*file
)
5259 struct ftrace_graph_data
*fgd
;
5262 if (unlikely(ftrace_disabled
))
5265 fgd
= kmalloc(sizeof(*fgd
), GFP_KERNEL
);
5269 mutex_lock(&graph_lock
);
5271 fgd
->hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
5272 lockdep_is_held(&graph_lock
));
5273 fgd
->type
= GRAPH_FILTER_NOTRACE
;
5274 fgd
->seq_ops
= &ftrace_graph_seq_ops
;
5276 ret
= __ftrace_graph_open(inode
, file
, fgd
);
5280 mutex_unlock(&graph_lock
);
5285 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
5287 struct ftrace_graph_data
*fgd
;
5288 struct ftrace_hash
*old_hash
, *new_hash
;
5289 struct trace_parser
*parser
;
5292 if (file
->f_mode
& FMODE_READ
) {
5293 struct seq_file
*m
= file
->private_data
;
5296 seq_release(inode
, file
);
5298 fgd
= file
->private_data
;
5302 if (file
->f_mode
& FMODE_WRITE
) {
5304 parser
= &fgd
->parser
;
5306 if (trace_parser_loaded((parser
))) {
5307 ret
= ftrace_graph_set_hash(fgd
->new_hash
,
5311 trace_parser_put(parser
);
5313 new_hash
= __ftrace_hash_move(fgd
->new_hash
);
5319 mutex_lock(&graph_lock
);
5321 if (fgd
->type
== GRAPH_FILTER_FUNCTION
) {
5322 old_hash
= rcu_dereference_protected(ftrace_graph_hash
,
5323 lockdep_is_held(&graph_lock
));
5324 rcu_assign_pointer(ftrace_graph_hash
, new_hash
);
5326 old_hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
5327 lockdep_is_held(&graph_lock
));
5328 rcu_assign_pointer(ftrace_graph_notrace_hash
, new_hash
);
5331 mutex_unlock(&graph_lock
);
5333 /* Wait till all users are no longer using the old hash */
5336 free_ftrace_hash(old_hash
);
5340 free_ftrace_hash(fgd
->new_hash
);
5347 ftrace_graph_set_hash(struct ftrace_hash
*hash
, char *buffer
)
5349 struct ftrace_glob func_g
;
5350 struct dyn_ftrace
*rec
;
5351 struct ftrace_page
*pg
;
5352 struct ftrace_func_entry
*entry
;
5357 func_g
.type
= filter_parse_regex(buffer
, strlen(buffer
),
5358 &func_g
.search
, ¬);
5360 func_g
.len
= strlen(func_g
.search
);
5362 mutex_lock(&ftrace_lock
);
5364 if (unlikely(ftrace_disabled
)) {
5365 mutex_unlock(&ftrace_lock
);
5369 do_for_each_ftrace_rec(pg
, rec
) {
5371 if (rec
->flags
& FTRACE_FL_DISABLED
)
5374 if (ftrace_match_record(rec
, &func_g
, NULL
, 0)) {
5375 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
5382 if (add_hash_entry(hash
, rec
->ip
) < 0)
5386 free_hash_entry(hash
, entry
);
5391 } while_for_each_ftrace_rec();
5393 mutex_unlock(&ftrace_lock
);
5402 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
5403 size_t cnt
, loff_t
*ppos
)
5405 ssize_t read
, ret
= 0;
5406 struct ftrace_graph_data
*fgd
= file
->private_data
;
5407 struct trace_parser
*parser
;
5412 /* Read mode uses seq functions */
5413 if (file
->f_mode
& FMODE_READ
) {
5414 struct seq_file
*m
= file
->private_data
;
5418 parser
= &fgd
->parser
;
5420 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
5422 if (read
>= 0 && trace_parser_loaded(parser
) &&
5423 !trace_parser_cont(parser
)) {
5425 ret
= ftrace_graph_set_hash(fgd
->new_hash
,
5427 trace_parser_clear(parser
);
5436 static const struct file_operations ftrace_graph_fops
= {
5437 .open
= ftrace_graph_open
,
5439 .write
= ftrace_graph_write
,
5440 .llseek
= tracing_lseek
,
5441 .release
= ftrace_graph_release
,
5444 static const struct file_operations ftrace_graph_notrace_fops
= {
5445 .open
= ftrace_graph_notrace_open
,
5447 .write
= ftrace_graph_write
,
5448 .llseek
= tracing_lseek
,
5449 .release
= ftrace_graph_release
,
5451 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5453 void ftrace_create_filter_files(struct ftrace_ops
*ops
,
5454 struct dentry
*parent
)
5457 trace_create_file("set_ftrace_filter", 0644, parent
,
5458 ops
, &ftrace_filter_fops
);
5460 trace_create_file("set_ftrace_notrace", 0644, parent
,
5461 ops
, &ftrace_notrace_fops
);
5465 * The name "destroy_filter_files" is really a misnomer. Although
5466 * in the future, it may actualy delete the files, but this is
5467 * really intended to make sure the ops passed in are disabled
5468 * and that when this function returns, the caller is free to
5471 * The "destroy" name is only to match the "create" name that this
5472 * should be paired with.
5474 void ftrace_destroy_filter_files(struct ftrace_ops
*ops
)
5476 mutex_lock(&ftrace_lock
);
5477 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
)
5478 ftrace_shutdown(ops
, 0);
5479 ops
->flags
|= FTRACE_OPS_FL_DELETED
;
5480 ftrace_free_filter(ops
);
5481 mutex_unlock(&ftrace_lock
);
5484 static __init
int ftrace_init_dyn_tracefs(struct dentry
*d_tracer
)
5487 trace_create_file("available_filter_functions", 0444,
5488 d_tracer
, NULL
, &ftrace_avail_fops
);
5490 trace_create_file("enabled_functions", 0444,
5491 d_tracer
, NULL
, &ftrace_enabled_fops
);
5493 ftrace_create_filter_files(&global_ops
, d_tracer
);
5495 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5496 trace_create_file("set_graph_function", 0644, d_tracer
,
5498 &ftrace_graph_fops
);
5499 trace_create_file("set_graph_notrace", 0644, d_tracer
,
5501 &ftrace_graph_notrace_fops
);
5502 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5507 static int ftrace_cmp_ips(const void *a
, const void *b
)
5509 const unsigned long *ipa
= a
;
5510 const unsigned long *ipb
= b
;
5519 static int ftrace_process_locs(struct module
*mod
,
5520 unsigned long *start
,
5523 struct ftrace_page
*start_pg
;
5524 struct ftrace_page
*pg
;
5525 struct dyn_ftrace
*rec
;
5526 unsigned long count
;
5529 unsigned long flags
= 0; /* Shut up gcc */
5532 count
= end
- start
;
5537 sort(start
, count
, sizeof(*start
),
5538 ftrace_cmp_ips
, NULL
);
5540 start_pg
= ftrace_allocate_pages(count
);
5544 mutex_lock(&ftrace_lock
);
5547 * Core and each module needs their own pages, as
5548 * modules will free them when they are removed.
5549 * Force a new page to be allocated for modules.
5552 WARN_ON(ftrace_pages
|| ftrace_pages_start
);
5553 /* First initialization */
5554 ftrace_pages
= ftrace_pages_start
= start_pg
;
5559 if (WARN_ON(ftrace_pages
->next
)) {
5560 /* Hmm, we have free pages? */
5561 while (ftrace_pages
->next
)
5562 ftrace_pages
= ftrace_pages
->next
;
5565 ftrace_pages
->next
= start_pg
;
5571 addr
= ftrace_call_adjust(*p
++);
5573 * Some architecture linkers will pad between
5574 * the different mcount_loc sections of different
5575 * object files to satisfy alignments.
5576 * Skip any NULL pointers.
5581 if (pg
->index
== pg
->size
) {
5582 /* We should have allocated enough */
5583 if (WARN_ON(!pg
->next
))
5588 rec
= &pg
->records
[pg
->index
++];
5592 /* We should have used all pages */
5595 /* Assign the last page to ftrace_pages */
5599 * We only need to disable interrupts on start up
5600 * because we are modifying code that an interrupt
5601 * may execute, and the modification is not atomic.
5602 * But for modules, nothing runs the code we modify
5603 * until we are finished with it, and there's no
5604 * reason to cause large interrupt latencies while we do it.
5607 local_irq_save(flags
);
5608 ftrace_update_code(mod
, start_pg
);
5610 local_irq_restore(flags
);
5613 mutex_unlock(&ftrace_lock
);
5618 struct ftrace_mod_func
{
5619 struct list_head list
;
5625 struct ftrace_mod_map
{
5626 struct rcu_head rcu
;
5627 struct list_head list
;
5629 unsigned long start_addr
;
5630 unsigned long end_addr
;
5631 struct list_head funcs
;
5632 unsigned int num_funcs
;
5635 #ifdef CONFIG_MODULES
5637 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5639 static LIST_HEAD(ftrace_mod_maps
);
5641 static int referenced_filters(struct dyn_ftrace
*rec
)
5643 struct ftrace_ops
*ops
;
5646 for (ops
= ftrace_ops_list
; ops
!= &ftrace_list_end
; ops
= ops
->next
) {
5647 if (ops_references_rec(ops
, rec
))
5655 clear_mod_from_hash(struct ftrace_page
*pg
, struct ftrace_hash
*hash
)
5657 struct ftrace_func_entry
*entry
;
5658 struct dyn_ftrace
*rec
;
5661 if (ftrace_hash_empty(hash
))
5664 for (i
= 0; i
< pg
->index
; i
++) {
5665 rec
= &pg
->records
[i
];
5666 entry
= __ftrace_lookup_ip(hash
, rec
->ip
);
5668 * Do not allow this rec to match again.
5669 * Yeah, it may waste some memory, but will be removed
5670 * if/when the hash is modified again.
5677 /* Clear any records from hashs */
5678 static void clear_mod_from_hashes(struct ftrace_page
*pg
)
5680 struct trace_array
*tr
;
5682 mutex_lock(&trace_types_lock
);
5683 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
5684 if (!tr
->ops
|| !tr
->ops
->func_hash
)
5686 mutex_lock(&tr
->ops
->func_hash
->regex_lock
);
5687 clear_mod_from_hash(pg
, tr
->ops
->func_hash
->filter_hash
);
5688 clear_mod_from_hash(pg
, tr
->ops
->func_hash
->notrace_hash
);
5689 mutex_unlock(&tr
->ops
->func_hash
->regex_lock
);
5691 mutex_unlock(&trace_types_lock
);
5694 static void ftrace_free_mod_map(struct rcu_head
*rcu
)
5696 struct ftrace_mod_map
*mod_map
= container_of(rcu
, struct ftrace_mod_map
, rcu
);
5697 struct ftrace_mod_func
*mod_func
;
5698 struct ftrace_mod_func
*n
;
5700 /* All the contents of mod_map are now not visible to readers */
5701 list_for_each_entry_safe(mod_func
, n
, &mod_map
->funcs
, list
) {
5702 kfree(mod_func
->name
);
5703 list_del(&mod_func
->list
);
5710 void ftrace_release_mod(struct module
*mod
)
5712 struct ftrace_mod_map
*mod_map
;
5713 struct ftrace_mod_map
*n
;
5714 struct dyn_ftrace
*rec
;
5715 struct ftrace_page
**last_pg
;
5716 struct ftrace_page
*tmp_page
= NULL
;
5717 struct ftrace_page
*pg
;
5720 mutex_lock(&ftrace_lock
);
5722 if (ftrace_disabled
)
5725 list_for_each_entry_safe(mod_map
, n
, &ftrace_mod_maps
, list
) {
5726 if (mod_map
->mod
== mod
) {
5727 list_del_rcu(&mod_map
->list
);
5728 call_rcu(&mod_map
->rcu
, ftrace_free_mod_map
);
5734 * Each module has its own ftrace_pages, remove
5735 * them from the list.
5737 last_pg
= &ftrace_pages_start
;
5738 for (pg
= ftrace_pages_start
; pg
; pg
= *last_pg
) {
5739 rec
= &pg
->records
[0];
5740 if (within_module_core(rec
->ip
, mod
) ||
5741 within_module_init(rec
->ip
, mod
)) {
5743 * As core pages are first, the first
5744 * page should never be a module page.
5746 if (WARN_ON(pg
== ftrace_pages_start
))
5749 /* Check if we are deleting the last page */
5750 if (pg
== ftrace_pages
)
5751 ftrace_pages
= next_to_ftrace_page(last_pg
);
5753 ftrace_update_tot_cnt
-= pg
->index
;
5754 *last_pg
= pg
->next
;
5756 pg
->next
= tmp_page
;
5759 last_pg
= &pg
->next
;
5762 mutex_unlock(&ftrace_lock
);
5764 for (pg
= tmp_page
; pg
; pg
= tmp_page
) {
5766 /* Needs to be called outside of ftrace_lock */
5767 clear_mod_from_hashes(pg
);
5769 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
5770 free_pages((unsigned long)pg
->records
, order
);
5771 tmp_page
= pg
->next
;
5776 void ftrace_module_enable(struct module
*mod
)
5778 struct dyn_ftrace
*rec
;
5779 struct ftrace_page
*pg
;
5781 mutex_lock(&ftrace_lock
);
5783 if (ftrace_disabled
)
5787 * If the tracing is enabled, go ahead and enable the record.
5789 * The reason not to enable the record immediatelly is the
5790 * inherent check of ftrace_make_nop/ftrace_make_call for
5791 * correct previous instructions. Making first the NOP
5792 * conversion puts the module to the correct state, thus
5793 * passing the ftrace_make_call check.
5795 * We also delay this to after the module code already set the
5796 * text to read-only, as we now need to set it back to read-write
5797 * so that we can modify the text.
5799 if (ftrace_start_up
)
5800 ftrace_arch_code_modify_prepare();
5802 do_for_each_ftrace_rec(pg
, rec
) {
5805 * do_for_each_ftrace_rec() is a double loop.
5806 * module text shares the pg. If a record is
5807 * not part of this module, then skip this pg,
5808 * which the "break" will do.
5810 if (!within_module_core(rec
->ip
, mod
) &&
5811 !within_module_init(rec
->ip
, mod
))
5817 * When adding a module, we need to check if tracers are
5818 * currently enabled and if they are, and can trace this record,
5819 * we need to enable the module functions as well as update the
5820 * reference counts for those function records.
5822 if (ftrace_start_up
)
5823 cnt
+= referenced_filters(rec
);
5825 /* This clears FTRACE_FL_DISABLED */
5828 if (ftrace_start_up
&& cnt
) {
5829 int failed
= __ftrace_replace_code(rec
, 1);
5831 ftrace_bug(failed
, rec
);
5836 } while_for_each_ftrace_rec();
5839 if (ftrace_start_up
)
5840 ftrace_arch_code_modify_post_process();
5843 mutex_unlock(&ftrace_lock
);
5845 process_cached_mods(mod
->name
);
5848 void ftrace_module_init(struct module
*mod
)
5850 if (ftrace_disabled
|| !mod
->num_ftrace_callsites
)
5853 ftrace_process_locs(mod
, mod
->ftrace_callsites
,
5854 mod
->ftrace_callsites
+ mod
->num_ftrace_callsites
);
5857 static void save_ftrace_mod_rec(struct ftrace_mod_map
*mod_map
,
5858 struct dyn_ftrace
*rec
)
5860 struct ftrace_mod_func
*mod_func
;
5861 unsigned long symsize
;
5862 unsigned long offset
;
5863 char str
[KSYM_SYMBOL_LEN
];
5867 ret
= kallsyms_lookup(rec
->ip
, &symsize
, &offset
, &modname
, str
);
5871 mod_func
= kmalloc(sizeof(*mod_func
), GFP_KERNEL
);
5875 mod_func
->name
= kstrdup(str
, GFP_KERNEL
);
5876 if (!mod_func
->name
) {
5881 mod_func
->ip
= rec
->ip
- offset
;
5882 mod_func
->size
= symsize
;
5884 mod_map
->num_funcs
++;
5886 list_add_rcu(&mod_func
->list
, &mod_map
->funcs
);
5889 static struct ftrace_mod_map
*
5890 allocate_ftrace_mod_map(struct module
*mod
,
5891 unsigned long start
, unsigned long end
)
5893 struct ftrace_mod_map
*mod_map
;
5895 mod_map
= kmalloc(sizeof(*mod_map
), GFP_KERNEL
);
5900 mod_map
->start_addr
= start
;
5901 mod_map
->end_addr
= end
;
5902 mod_map
->num_funcs
= 0;
5904 INIT_LIST_HEAD_RCU(&mod_map
->funcs
);
5906 list_add_rcu(&mod_map
->list
, &ftrace_mod_maps
);
5912 ftrace_func_address_lookup(struct ftrace_mod_map
*mod_map
,
5913 unsigned long addr
, unsigned long *size
,
5914 unsigned long *off
, char *sym
)
5916 struct ftrace_mod_func
*found_func
= NULL
;
5917 struct ftrace_mod_func
*mod_func
;
5919 list_for_each_entry_rcu(mod_func
, &mod_map
->funcs
, list
) {
5920 if (addr
>= mod_func
->ip
&&
5921 addr
< mod_func
->ip
+ mod_func
->size
) {
5922 found_func
= mod_func
;
5929 *size
= found_func
->size
;
5931 *off
= addr
- found_func
->ip
;
5933 strlcpy(sym
, found_func
->name
, KSYM_NAME_LEN
);
5935 return found_func
->name
;
5942 ftrace_mod_address_lookup(unsigned long addr
, unsigned long *size
,
5943 unsigned long *off
, char **modname
, char *sym
)
5945 struct ftrace_mod_map
*mod_map
;
5946 const char *ret
= NULL
;
5948 /* mod_map is freed via call_rcu() */
5950 list_for_each_entry_rcu(mod_map
, &ftrace_mod_maps
, list
) {
5951 ret
= ftrace_func_address_lookup(mod_map
, addr
, size
, off
, sym
);
5954 *modname
= mod_map
->mod
->name
;
5963 int ftrace_mod_get_kallsym(unsigned int symnum
, unsigned long *value
,
5964 char *type
, char *name
,
5965 char *module_name
, int *exported
)
5967 struct ftrace_mod_map
*mod_map
;
5968 struct ftrace_mod_func
*mod_func
;
5971 list_for_each_entry_rcu(mod_map
, &ftrace_mod_maps
, list
) {
5973 if (symnum
>= mod_map
->num_funcs
) {
5974 symnum
-= mod_map
->num_funcs
;
5978 list_for_each_entry_rcu(mod_func
, &mod_map
->funcs
, list
) {
5984 *value
= mod_func
->ip
;
5986 strlcpy(name
, mod_func
->name
, KSYM_NAME_LEN
);
5987 strlcpy(module_name
, mod_map
->mod
->name
, MODULE_NAME_LEN
);
6000 static void save_ftrace_mod_rec(struct ftrace_mod_map
*mod_map
,
6001 struct dyn_ftrace
*rec
) { }
6002 static inline struct ftrace_mod_map
*
6003 allocate_ftrace_mod_map(struct module
*mod
,
6004 unsigned long start
, unsigned long end
)
6008 #endif /* CONFIG_MODULES */
6010 struct ftrace_init_func
{
6011 struct list_head list
;
6015 /* Clear any init ips from hashes */
6017 clear_func_from_hash(struct ftrace_init_func
*func
, struct ftrace_hash
*hash
)
6019 struct ftrace_func_entry
*entry
;
6021 if (ftrace_hash_empty(hash
))
6024 entry
= __ftrace_lookup_ip(hash
, func
->ip
);
6027 * Do not allow this rec to match again.
6028 * Yeah, it may waste some memory, but will be removed
6029 * if/when the hash is modified again.
6036 clear_func_from_hashes(struct ftrace_init_func
*func
)
6038 struct trace_array
*tr
;
6040 mutex_lock(&trace_types_lock
);
6041 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
6042 if (!tr
->ops
|| !tr
->ops
->func_hash
)
6044 mutex_lock(&tr
->ops
->func_hash
->regex_lock
);
6045 clear_func_from_hash(func
, tr
->ops
->func_hash
->filter_hash
);
6046 clear_func_from_hash(func
, tr
->ops
->func_hash
->notrace_hash
);
6047 mutex_unlock(&tr
->ops
->func_hash
->regex_lock
);
6049 mutex_unlock(&trace_types_lock
);
6052 static void add_to_clear_hash_list(struct list_head
*clear_list
,
6053 struct dyn_ftrace
*rec
)
6055 struct ftrace_init_func
*func
;
6057 func
= kmalloc(sizeof(*func
), GFP_KERNEL
);
6059 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6064 list_add(&func
->list
, clear_list
);
6067 void ftrace_free_mem(struct module
*mod
, void *start_ptr
, void *end_ptr
)
6069 unsigned long start
= (unsigned long)(start_ptr
);
6070 unsigned long end
= (unsigned long)(end_ptr
);
6071 struct ftrace_page
**last_pg
= &ftrace_pages_start
;
6072 struct ftrace_page
*pg
;
6073 struct dyn_ftrace
*rec
;
6074 struct dyn_ftrace key
;
6075 struct ftrace_mod_map
*mod_map
= NULL
;
6076 struct ftrace_init_func
*func
, *func_next
;
6077 struct list_head clear_hash
;
6080 INIT_LIST_HEAD(&clear_hash
);
6083 key
.flags
= end
; /* overload flags, as it is unsigned long */
6085 mutex_lock(&ftrace_lock
);
6088 * If we are freeing module init memory, then check if
6089 * any tracer is active. If so, we need to save a mapping of
6090 * the module functions being freed with the address.
6092 if (mod
&& ftrace_ops_list
!= &ftrace_list_end
)
6093 mod_map
= allocate_ftrace_mod_map(mod
, start
, end
);
6095 for (pg
= ftrace_pages_start
; pg
; last_pg
= &pg
->next
, pg
= *last_pg
) {
6096 if (end
< pg
->records
[0].ip
||
6097 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
6100 rec
= bsearch(&key
, pg
->records
, pg
->index
,
6101 sizeof(struct dyn_ftrace
),
6106 /* rec will be cleared from hashes after ftrace_lock unlock */
6107 add_to_clear_hash_list(&clear_hash
, rec
);
6110 save_ftrace_mod_rec(mod_map
, rec
);
6113 ftrace_update_tot_cnt
--;
6115 *last_pg
= pg
->next
;
6116 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
6117 free_pages((unsigned long)pg
->records
, order
);
6119 pg
= container_of(last_pg
, struct ftrace_page
, next
);
6124 memmove(rec
, rec
+ 1,
6125 (pg
->index
- (rec
- pg
->records
)) * sizeof(*rec
));
6126 /* More than one function may be in this block */
6129 mutex_unlock(&ftrace_lock
);
6131 list_for_each_entry_safe(func
, func_next
, &clear_hash
, list
) {
6132 clear_func_from_hashes(func
);
6137 void __init
ftrace_free_init_mem(void)
6139 void *start
= (void *)(&__init_begin
);
6140 void *end
= (void *)(&__init_end
);
6142 ftrace_free_mem(NULL
, start
, end
);
6145 void __init
ftrace_init(void)
6147 extern unsigned long __start_mcount_loc
[];
6148 extern unsigned long __stop_mcount_loc
[];
6149 unsigned long count
, flags
;
6152 local_irq_save(flags
);
6153 ret
= ftrace_dyn_arch_init();
6154 local_irq_restore(flags
);
6158 count
= __stop_mcount_loc
- __start_mcount_loc
;
6160 pr_info("ftrace: No functions to be traced?\n");
6164 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6165 count
, count
/ ENTRIES_PER_PAGE
+ 1);
6167 last_ftrace_enabled
= ftrace_enabled
= 1;
6169 ret
= ftrace_process_locs(NULL
,
6173 set_ftrace_early_filters();
6177 ftrace_disabled
= 1;
6180 /* Do nothing if arch does not support this */
6181 void __weak
arch_ftrace_update_trampoline(struct ftrace_ops
*ops
)
6185 static void ftrace_update_trampoline(struct ftrace_ops
*ops
)
6187 arch_ftrace_update_trampoline(ops
);
6190 void ftrace_init_trace_array(struct trace_array
*tr
)
6192 INIT_LIST_HEAD(&tr
->func_probes
);
6193 INIT_LIST_HEAD(&tr
->mod_trace
);
6194 INIT_LIST_HEAD(&tr
->mod_notrace
);
6198 struct ftrace_ops global_ops
= {
6199 .func
= ftrace_stub
,
6200 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
|
6201 FTRACE_OPS_FL_INITIALIZED
|
6205 static int __init
ftrace_nodyn_init(void)
6210 core_initcall(ftrace_nodyn_init
);
6212 static inline int ftrace_init_dyn_tracefs(struct dentry
*d_tracer
) { return 0; }
6213 static inline void ftrace_startup_enable(int command
) { }
6214 static inline void ftrace_startup_all(int command
) { }
6216 # define ftrace_startup_sysctl() do { } while (0)
6217 # define ftrace_shutdown_sysctl() do { } while (0)
6219 static void ftrace_update_trampoline(struct ftrace_ops
*ops
)
6223 #endif /* CONFIG_DYNAMIC_FTRACE */
6225 __init
void ftrace_init_global_array_ops(struct trace_array
*tr
)
6227 tr
->ops
= &global_ops
;
6228 tr
->ops
->private = tr
;
6229 ftrace_init_trace_array(tr
);
6232 void ftrace_init_array_ops(struct trace_array
*tr
, ftrace_func_t func
)
6234 /* If we filter on pids, update to use the pid function */
6235 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
) {
6236 if (WARN_ON(tr
->ops
->func
!= ftrace_stub
))
6237 printk("ftrace ops had %pS for function\n",
6240 tr
->ops
->func
= func
;
6241 tr
->ops
->private = tr
;
6244 void ftrace_reset_array_ops(struct trace_array
*tr
)
6246 tr
->ops
->func
= ftrace_stub
;
6250 __ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
6251 struct ftrace_ops
*ignored
, struct pt_regs
*regs
)
6253 struct ftrace_ops
*op
;
6256 bit
= trace_test_and_set_recursion(TRACE_LIST_START
, TRACE_LIST_MAX
);
6261 * Some of the ops may be dynamically allocated,
6262 * they must be freed after a synchronize_rcu().
6264 preempt_disable_notrace();
6266 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
6268 * Check the following for each ops before calling their func:
6269 * if RCU flag is set, then rcu_is_watching() must be true
6270 * if PER_CPU is set, then ftrace_function_local_disable()
6272 * Otherwise test if the ip matches the ops filter
6274 * If any of the above fails then the op->func() is not executed.
6276 if ((!(op
->flags
& FTRACE_OPS_FL_RCU
) || rcu_is_watching()) &&
6277 ftrace_ops_test(op
, ip
, regs
)) {
6278 if (FTRACE_WARN_ON(!op
->func
)) {
6279 pr_warn("op=%p %pS\n", op
, op
);
6282 op
->func(ip
, parent_ip
, op
, regs
);
6284 } while_for_each_ftrace_op(op
);
6286 preempt_enable_notrace();
6287 trace_clear_recursion(bit
);
6291 * Some archs only support passing ip and parent_ip. Even though
6292 * the list function ignores the op parameter, we do not want any
6293 * C side effects, where a function is called without the caller
6294 * sending a third parameter.
6295 * Archs are to support both the regs and ftrace_ops at the same time.
6296 * If they support ftrace_ops, it is assumed they support regs.
6297 * If call backs want to use regs, they must either check for regs
6298 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6299 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6300 * An architecture can pass partial regs with ftrace_ops and still
6301 * set the ARCH_SUPPORTS_FTRACE_OPS.
6303 #if ARCH_SUPPORTS_FTRACE_OPS
6304 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
6305 struct ftrace_ops
*op
, struct pt_regs
*regs
)
6307 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, regs
);
6310 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
)
6312 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, NULL
);
6317 * If there's only one function registered but it does not support
6318 * recursion, needs RCU protection and/or requires per cpu handling, then
6319 * this function will be called by the mcount trampoline.
6321 static void ftrace_ops_assist_func(unsigned long ip
, unsigned long parent_ip
,
6322 struct ftrace_ops
*op
, struct pt_regs
*regs
)
6326 if ((op
->flags
& FTRACE_OPS_FL_RCU
) && !rcu_is_watching())
6329 bit
= trace_test_and_set_recursion(TRACE_LIST_START
, TRACE_LIST_MAX
);
6333 preempt_disable_notrace();
6335 op
->func(ip
, parent_ip
, op
, regs
);
6337 preempt_enable_notrace();
6338 trace_clear_recursion(bit
);
6342 * ftrace_ops_get_func - get the function a trampoline should call
6343 * @ops: the ops to get the function for
6345 * Normally the mcount trampoline will call the ops->func, but there
6346 * are times that it should not. For example, if the ops does not
6347 * have its own recursion protection, then it should call the
6348 * ftrace_ops_assist_func() instead.
6350 * Returns the function that the trampoline should call for @ops.
6352 ftrace_func_t
ftrace_ops_get_func(struct ftrace_ops
*ops
)
6355 * If the function does not handle recursion, needs to be RCU safe,
6356 * or does per cpu logic, then we need to call the assist handler.
6358 if (!(ops
->flags
& FTRACE_OPS_FL_RECURSION_SAFE
) ||
6359 ops
->flags
& FTRACE_OPS_FL_RCU
)
6360 return ftrace_ops_assist_func
;
6366 ftrace_filter_pid_sched_switch_probe(void *data
, bool preempt
,
6367 struct task_struct
*prev
, struct task_struct
*next
)
6369 struct trace_array
*tr
= data
;
6370 struct trace_pid_list
*pid_list
;
6372 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6374 this_cpu_write(tr
->trace_buffer
.data
->ftrace_ignore_pid
,
6375 trace_ignore_this_task(pid_list
, next
));
6379 ftrace_pid_follow_sched_process_fork(void *data
,
6380 struct task_struct
*self
,
6381 struct task_struct
*task
)
6383 struct trace_pid_list
*pid_list
;
6384 struct trace_array
*tr
= data
;
6386 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6387 trace_filter_add_remove_task(pid_list
, self
, task
);
6391 ftrace_pid_follow_sched_process_exit(void *data
, struct task_struct
*task
)
6393 struct trace_pid_list
*pid_list
;
6394 struct trace_array
*tr
= data
;
6396 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6397 trace_filter_add_remove_task(pid_list
, NULL
, task
);
6400 void ftrace_pid_follow_fork(struct trace_array
*tr
, bool enable
)
6403 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork
,
6405 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit
,
6408 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork
,
6410 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit
,
6415 static void clear_ftrace_pids(struct trace_array
*tr
)
6417 struct trace_pid_list
*pid_list
;
6420 pid_list
= rcu_dereference_protected(tr
->function_pids
,
6421 lockdep_is_held(&ftrace_lock
));
6425 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe
, tr
);
6427 for_each_possible_cpu(cpu
)
6428 per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->ftrace_ignore_pid
= false;
6430 rcu_assign_pointer(tr
->function_pids
, NULL
);
6432 /* Wait till all users are no longer using pid filtering */
6435 trace_free_pid_list(pid_list
);
6438 void ftrace_clear_pids(struct trace_array
*tr
)
6440 mutex_lock(&ftrace_lock
);
6442 clear_ftrace_pids(tr
);
6444 mutex_unlock(&ftrace_lock
);
6447 static void ftrace_pid_reset(struct trace_array
*tr
)
6449 mutex_lock(&ftrace_lock
);
6450 clear_ftrace_pids(tr
);
6452 ftrace_update_pid_func();
6453 ftrace_startup_all(0);
6455 mutex_unlock(&ftrace_lock
);
6458 /* Greater than any max PID */
6459 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6461 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
6464 struct trace_pid_list
*pid_list
;
6465 struct trace_array
*tr
= m
->private;
6467 mutex_lock(&ftrace_lock
);
6468 rcu_read_lock_sched();
6470 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6473 return !(*pos
) ? FTRACE_NO_PIDS
: NULL
;
6475 return trace_pid_start(pid_list
, pos
);
6478 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
6480 struct trace_array
*tr
= m
->private;
6481 struct trace_pid_list
*pid_list
= rcu_dereference_sched(tr
->function_pids
);
6483 if (v
== FTRACE_NO_PIDS
)
6486 return trace_pid_next(pid_list
, v
, pos
);
6489 static void fpid_stop(struct seq_file
*m
, void *p
)
6492 rcu_read_unlock_sched();
6493 mutex_unlock(&ftrace_lock
);
6496 static int fpid_show(struct seq_file
*m
, void *v
)
6498 if (v
== FTRACE_NO_PIDS
) {
6499 seq_puts(m
, "no pid\n");
6503 return trace_pid_show(m
, v
);
6506 static const struct seq_operations ftrace_pid_sops
= {
6507 .start
= fpid_start
,
6514 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
6516 struct trace_array
*tr
= inode
->i_private
;
6520 if (trace_array_get(tr
) < 0)
6523 if ((file
->f_mode
& FMODE_WRITE
) &&
6524 (file
->f_flags
& O_TRUNC
))
6525 ftrace_pid_reset(tr
);
6527 ret
= seq_open(file
, &ftrace_pid_sops
);
6529 trace_array_put(tr
);
6531 m
= file
->private_data
;
6532 /* copy tr over to seq ops */
6539 static void ignore_task_cpu(void *data
)
6541 struct trace_array
*tr
= data
;
6542 struct trace_pid_list
*pid_list
;
6545 * This function is called by on_each_cpu() while the
6546 * event_mutex is held.
6548 pid_list
= rcu_dereference_protected(tr
->function_pids
,
6549 mutex_is_locked(&ftrace_lock
));
6551 this_cpu_write(tr
->trace_buffer
.data
->ftrace_ignore_pid
,
6552 trace_ignore_this_task(pid_list
, current
));
6556 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
6557 size_t cnt
, loff_t
*ppos
)
6559 struct seq_file
*m
= filp
->private_data
;
6560 struct trace_array
*tr
= m
->private;
6561 struct trace_pid_list
*filtered_pids
= NULL
;
6562 struct trace_pid_list
*pid_list
;
6568 mutex_lock(&ftrace_lock
);
6570 filtered_pids
= rcu_dereference_protected(tr
->function_pids
,
6571 lockdep_is_held(&ftrace_lock
));
6573 ret
= trace_pid_write(filtered_pids
, &pid_list
, ubuf
, cnt
);
6577 rcu_assign_pointer(tr
->function_pids
, pid_list
);
6579 if (filtered_pids
) {
6581 trace_free_pid_list(filtered_pids
);
6582 } else if (pid_list
) {
6583 /* Register a probe to set whether to ignore the tracing of a task */
6584 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe
, tr
);
6588 * Ignoring of pids is done at task switch. But we have to
6589 * check for those tasks that are currently running.
6590 * Always do this in case a pid was appended or removed.
6592 on_each_cpu(ignore_task_cpu
, tr
, 1);
6594 ftrace_update_pid_func();
6595 ftrace_startup_all(0);
6597 mutex_unlock(&ftrace_lock
);
6606 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
6608 struct trace_array
*tr
= inode
->i_private
;
6610 trace_array_put(tr
);
6612 return seq_release(inode
, file
);
6615 static const struct file_operations ftrace_pid_fops
= {
6616 .open
= ftrace_pid_open
,
6617 .write
= ftrace_pid_write
,
6619 .llseek
= tracing_lseek
,
6620 .release
= ftrace_pid_release
,
6623 void ftrace_init_tracefs(struct trace_array
*tr
, struct dentry
*d_tracer
)
6625 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
6626 tr
, &ftrace_pid_fops
);
6629 void __init
ftrace_init_tracefs_toplevel(struct trace_array
*tr
,
6630 struct dentry
*d_tracer
)
6632 /* Only the top level directory has the dyn_tracefs and profile */
6633 WARN_ON(!(tr
->flags
& TRACE_ARRAY_FL_GLOBAL
));
6635 ftrace_init_dyn_tracefs(d_tracer
);
6636 ftrace_profile_tracefs(d_tracer
);
6640 * ftrace_kill - kill ftrace
6642 * This function should be used by panic code. It stops ftrace
6643 * but in a not so nice way. If you need to simply kill ftrace
6644 * from a non-atomic section, use ftrace_kill.
6646 void ftrace_kill(void)
6648 ftrace_disabled
= 1;
6650 ftrace_trace_function
= ftrace_stub
;
6654 * Test if ftrace is dead or not.
6656 int ftrace_is_dead(void)
6658 return ftrace_disabled
;
6662 * register_ftrace_function - register a function for profiling
6663 * @ops - ops structure that holds the function for profiling.
6665 * Register a function to be called by all functions in the
6668 * Note: @ops->func and all the functions it calls must be labeled
6669 * with "notrace", otherwise it will go into a
6672 int register_ftrace_function(struct ftrace_ops
*ops
)
6676 ftrace_ops_init(ops
);
6678 mutex_lock(&ftrace_lock
);
6680 ret
= ftrace_startup(ops
, 0);
6682 mutex_unlock(&ftrace_lock
);
6686 EXPORT_SYMBOL_GPL(register_ftrace_function
);
6689 * unregister_ftrace_function - unregister a function for profiling.
6690 * @ops - ops structure that holds the function to unregister
6692 * Unregister a function that was added to be called by ftrace profiling.
6694 int unregister_ftrace_function(struct ftrace_ops
*ops
)
6698 mutex_lock(&ftrace_lock
);
6699 ret
= ftrace_shutdown(ops
, 0);
6700 mutex_unlock(&ftrace_lock
);
6704 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
6707 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
6708 void __user
*buffer
, size_t *lenp
,
6713 mutex_lock(&ftrace_lock
);
6715 if (unlikely(ftrace_disabled
))
6718 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
6720 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
6723 last_ftrace_enabled
= !!ftrace_enabled
;
6725 if (ftrace_enabled
) {
6727 /* we are starting ftrace again */
6728 if (rcu_dereference_protected(ftrace_ops_list
,
6729 lockdep_is_held(&ftrace_lock
)) != &ftrace_list_end
)
6730 update_ftrace_function();
6732 ftrace_startup_sysctl();
6735 /* stopping ftrace calls (just send to ftrace_stub) */
6736 ftrace_trace_function
= ftrace_stub
;
6738 ftrace_shutdown_sysctl();
6742 mutex_unlock(&ftrace_lock
);