2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/sched/task.h>
19 #include <linux/kallsyms.h>
20 #include <linux/seq_file.h>
21 #include <linux/suspend.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>
36 #include <linux/kprobes.h>
38 #include <trace/events/sched.h>
40 #include <asm/sections.h>
41 #include <asm/setup.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)
80 static struct ftrace_ops ftrace_list_end __read_mostly
= {
82 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_STUB
,
83 INIT_OPS_HASH(ftrace_list_end
)
86 /* ftrace_enabled is a method to turn ftrace on or off */
87 int ftrace_enabled __read_mostly
;
88 static int last_ftrace_enabled
;
90 /* Current function tracing op */
91 struct ftrace_ops
*function_trace_op __read_mostly
= &ftrace_list_end
;
92 /* What to set function_trace_op to */
93 static struct ftrace_ops
*set_function_trace_op
;
95 static bool ftrace_pids_enabled(struct ftrace_ops
*ops
)
97 struct trace_array
*tr
;
99 if (!(ops
->flags
& FTRACE_OPS_FL_PID
) || !ops
->private)
104 return tr
->function_pids
!= NULL
;
107 static void ftrace_update_trampoline(struct ftrace_ops
*ops
);
110 * ftrace_disabled is set when an anomaly is discovered.
111 * ftrace_disabled is much stronger than ftrace_enabled.
113 static int ftrace_disabled __read_mostly
;
115 static DEFINE_MUTEX(ftrace_lock
);
117 static struct ftrace_ops __rcu
*ftrace_ops_list __read_mostly
= &ftrace_list_end
;
118 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
119 static struct ftrace_ops global_ops
;
121 #if ARCH_SUPPORTS_FTRACE_OPS
122 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
123 struct ftrace_ops
*op
, struct pt_regs
*regs
);
125 /* See comment below, where ftrace_ops_list_func is defined */
126 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
);
127 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
131 * Traverse the ftrace_global_list, invoking all entries. The reason that we
132 * can use rcu_dereference_raw_notrace() is that elements removed from this list
133 * are simply leaked, so there is no need to interact with a grace-period
134 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
135 * concurrent insertions into the ftrace_global_list.
137 * Silly Alpha and silly pointer-speculation compiler optimizations!
139 #define do_for_each_ftrace_op(op, list) \
140 op = rcu_dereference_raw_notrace(list); \
144 * Optimized for just a single item in the list (as that is the normal case).
146 #define while_for_each_ftrace_op(op) \
147 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
148 unlikely((op) != &ftrace_list_end))
150 static inline void ftrace_ops_init(struct ftrace_ops
*ops
)
152 #ifdef CONFIG_DYNAMIC_FTRACE
153 if (!(ops
->flags
& FTRACE_OPS_FL_INITIALIZED
)) {
154 mutex_init(&ops
->local_hash
.regex_lock
);
155 ops
->func_hash
= &ops
->local_hash
;
156 ops
->flags
|= FTRACE_OPS_FL_INITIALIZED
;
162 * ftrace_nr_registered_ops - return number of ops registered
164 * Returns the number of ftrace_ops registered and tracing functions
166 int ftrace_nr_registered_ops(void)
168 struct ftrace_ops
*ops
;
171 mutex_lock(&ftrace_lock
);
173 for (ops
= rcu_dereference_protected(ftrace_ops_list
,
174 lockdep_is_held(&ftrace_lock
));
175 ops
!= &ftrace_list_end
;
176 ops
= rcu_dereference_protected(ops
->next
,
177 lockdep_is_held(&ftrace_lock
)))
180 mutex_unlock(&ftrace_lock
);
185 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
,
186 struct ftrace_ops
*op
, struct pt_regs
*regs
)
188 struct trace_array
*tr
= op
->private;
190 if (tr
&& this_cpu_read(tr
->trace_buffer
.data
->ftrace_ignore_pid
))
193 op
->saved_func(ip
, parent_ip
, op
, regs
);
197 * clear_ftrace_function - reset the ftrace function
199 * This NULLs the ftrace function and in essence stops
200 * tracing. There may be lag
202 void clear_ftrace_function(void)
204 ftrace_trace_function
= ftrace_stub
;
207 static void per_cpu_ops_disable_all(struct ftrace_ops
*ops
)
211 for_each_possible_cpu(cpu
)
212 *per_cpu_ptr(ops
->disabled
, cpu
) = 1;
215 static int per_cpu_ops_alloc(struct ftrace_ops
*ops
)
217 int __percpu
*disabled
;
219 if (WARN_ON_ONCE(!(ops
->flags
& FTRACE_OPS_FL_PER_CPU
)))
222 disabled
= alloc_percpu(int);
226 ops
->disabled
= disabled
;
227 per_cpu_ops_disable_all(ops
);
231 static void ftrace_sync(struct work_struct
*work
)
234 * This function is just a stub to implement a hard force
235 * of synchronize_sched(). This requires synchronizing
236 * tasks even in userspace and idle.
238 * Yes, function tracing is rude.
242 static void ftrace_sync_ipi(void *data
)
244 /* Probably not needed, but do it anyway */
248 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
249 static void update_function_graph_func(void);
251 /* Both enabled by default (can be cleared by function_graph tracer flags */
252 static bool fgraph_sleep_time
= true;
253 static bool fgraph_graph_time
= true;
256 static inline void update_function_graph_func(void) { }
260 static ftrace_func_t
ftrace_ops_get_list_func(struct ftrace_ops
*ops
)
263 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
264 * then it needs to call the list anyway.
266 if (ops
->flags
& (FTRACE_OPS_FL_DYNAMIC
| FTRACE_OPS_FL_PER_CPU
|
267 FTRACE_OPS_FL_RCU
) || FTRACE_FORCE_LIST_FUNC
)
268 return ftrace_ops_list_func
;
270 return ftrace_ops_get_func(ops
);
273 static void update_ftrace_function(void)
278 * Prepare the ftrace_ops that the arch callback will use.
279 * If there's only one ftrace_ops registered, the ftrace_ops_list
280 * will point to the ops we want.
282 set_function_trace_op
= rcu_dereference_protected(ftrace_ops_list
,
283 lockdep_is_held(&ftrace_lock
));
285 /* If there's no ftrace_ops registered, just call the stub function */
286 if (set_function_trace_op
== &ftrace_list_end
) {
290 * If we are at the end of the list and this ops is
291 * recursion safe and not dynamic and the arch supports passing ops,
292 * then have the mcount trampoline call the function directly.
294 } else if (rcu_dereference_protected(ftrace_ops_list
->next
,
295 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
296 func
= ftrace_ops_get_list_func(ftrace_ops_list
);
299 /* Just use the default ftrace_ops */
300 set_function_trace_op
= &ftrace_list_end
;
301 func
= ftrace_ops_list_func
;
304 update_function_graph_func();
306 /* If there's no change, then do nothing more here */
307 if (ftrace_trace_function
== func
)
311 * If we are using the list function, it doesn't care
312 * about the function_trace_ops.
314 if (func
== ftrace_ops_list_func
) {
315 ftrace_trace_function
= func
;
317 * Don't even bother setting function_trace_ops,
318 * it would be racy to do so anyway.
323 #ifndef CONFIG_DYNAMIC_FTRACE
325 * For static tracing, we need to be a bit more careful.
326 * The function change takes affect immediately. Thus,
327 * we need to coorditate the setting of the function_trace_ops
328 * with the setting of the ftrace_trace_function.
330 * Set the function to the list ops, which will call the
331 * function we want, albeit indirectly, but it handles the
332 * ftrace_ops and doesn't depend on function_trace_op.
334 ftrace_trace_function
= ftrace_ops_list_func
;
336 * Make sure all CPUs see this. Yes this is slow, but static
337 * tracing is slow and nasty to have enabled.
339 schedule_on_each_cpu(ftrace_sync
);
340 /* Now all cpus are using the list ops. */
341 function_trace_op
= set_function_trace_op
;
342 /* Make sure the function_trace_op is visible on all CPUs */
344 /* Nasty way to force a rmb on all cpus */
345 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
346 /* OK, we are all set to update the ftrace_trace_function now! */
347 #endif /* !CONFIG_DYNAMIC_FTRACE */
349 ftrace_trace_function
= func
;
352 int using_ftrace_ops_list_func(void)
354 return ftrace_trace_function
== ftrace_ops_list_func
;
357 static void add_ftrace_ops(struct ftrace_ops __rcu
**list
,
358 struct ftrace_ops
*ops
)
360 rcu_assign_pointer(ops
->next
, *list
);
363 * We are entering ops into the list but another
364 * CPU might be walking that list. We need to make sure
365 * the ops->next pointer is valid before another CPU sees
366 * the ops pointer included into the list.
368 rcu_assign_pointer(*list
, ops
);
371 static int remove_ftrace_ops(struct ftrace_ops __rcu
**list
,
372 struct ftrace_ops
*ops
)
374 struct ftrace_ops
**p
;
377 * If we are removing the last function, then simply point
378 * to the ftrace_stub.
380 if (rcu_dereference_protected(*list
,
381 lockdep_is_held(&ftrace_lock
)) == ops
&&
382 rcu_dereference_protected(ops
->next
,
383 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
384 *list
= &ftrace_list_end
;
388 for (p
= list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
399 static void ftrace_update_trampoline(struct ftrace_ops
*ops
);
401 static int __register_ftrace_function(struct ftrace_ops
*ops
)
403 if (ops
->flags
& FTRACE_OPS_FL_DELETED
)
406 if (WARN_ON(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
409 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
411 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
412 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
413 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
415 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
&&
416 !(ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
))
419 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
)
420 ops
->flags
|= FTRACE_OPS_FL_SAVE_REGS
;
423 if (!core_kernel_data((unsigned long)ops
))
424 ops
->flags
|= FTRACE_OPS_FL_DYNAMIC
;
426 if (ops
->flags
& FTRACE_OPS_FL_PER_CPU
) {
427 if (per_cpu_ops_alloc(ops
))
431 add_ftrace_ops(&ftrace_ops_list
, ops
);
433 /* Always save the function, and reset at unregistering */
434 ops
->saved_func
= ops
->func
;
436 if (ftrace_pids_enabled(ops
))
437 ops
->func
= ftrace_pid_func
;
439 ftrace_update_trampoline(ops
);
442 update_ftrace_function();
447 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
451 if (WARN_ON(!(ops
->flags
& FTRACE_OPS_FL_ENABLED
)))
454 ret
= remove_ftrace_ops(&ftrace_ops_list
, ops
);
460 update_ftrace_function();
462 ops
->func
= ops
->saved_func
;
467 static void ftrace_update_pid_func(void)
469 struct ftrace_ops
*op
;
471 /* Only do something if we are tracing something */
472 if (ftrace_trace_function
== ftrace_stub
)
475 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
476 if (op
->flags
& FTRACE_OPS_FL_PID
) {
477 op
->func
= ftrace_pids_enabled(op
) ?
478 ftrace_pid_func
: op
->saved_func
;
479 ftrace_update_trampoline(op
);
481 } while_for_each_ftrace_op(op
);
483 update_ftrace_function();
486 #ifdef CONFIG_FUNCTION_PROFILER
487 struct ftrace_profile
{
488 struct hlist_node node
;
490 unsigned long counter
;
491 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
492 unsigned long long time
;
493 unsigned long long time_squared
;
497 struct ftrace_profile_page
{
498 struct ftrace_profile_page
*next
;
500 struct ftrace_profile records
[];
503 struct ftrace_profile_stat
{
505 struct hlist_head
*hash
;
506 struct ftrace_profile_page
*pages
;
507 struct ftrace_profile_page
*start
;
508 struct tracer_stat stat
;
511 #define PROFILE_RECORDS_SIZE \
512 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
514 #define PROFILES_PER_PAGE \
515 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
517 static int ftrace_profile_enabled __read_mostly
;
519 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
520 static DEFINE_MUTEX(ftrace_profile_lock
);
522 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
524 #define FTRACE_PROFILE_HASH_BITS 10
525 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
528 function_stat_next(void *v
, int idx
)
530 struct ftrace_profile
*rec
= v
;
531 struct ftrace_profile_page
*pg
;
533 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
539 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
543 rec
= &pg
->records
[0];
551 static void *function_stat_start(struct tracer_stat
*trace
)
553 struct ftrace_profile_stat
*stat
=
554 container_of(trace
, struct ftrace_profile_stat
, stat
);
556 if (!stat
|| !stat
->start
)
559 return function_stat_next(&stat
->start
->records
[0], 0);
562 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
563 /* function graph compares on total time */
564 static int function_stat_cmp(void *p1
, void *p2
)
566 struct ftrace_profile
*a
= p1
;
567 struct ftrace_profile
*b
= p2
;
569 if (a
->time
< b
->time
)
571 if (a
->time
> b
->time
)
577 /* not function graph compares against hits */
578 static int function_stat_cmp(void *p1
, void *p2
)
580 struct ftrace_profile
*a
= p1
;
581 struct ftrace_profile
*b
= p2
;
583 if (a
->counter
< b
->counter
)
585 if (a
->counter
> b
->counter
)
592 static int function_stat_headers(struct seq_file
*m
)
594 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
595 seq_puts(m
, " Function "
598 "--- ---- --- ---\n");
600 seq_puts(m
, " Function Hit\n"
606 static int function_stat_show(struct seq_file
*m
, void *v
)
608 struct ftrace_profile
*rec
= v
;
609 char str
[KSYM_SYMBOL_LEN
];
611 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
612 static struct trace_seq s
;
613 unsigned long long avg
;
614 unsigned long long stddev
;
616 mutex_lock(&ftrace_profile_lock
);
618 /* we raced with function_profile_reset() */
619 if (unlikely(rec
->counter
== 0)) {
624 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
625 avg
= div64_ul(rec
->time
, rec
->counter
);
626 if (tracing_thresh
&& (avg
< tracing_thresh
))
630 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
631 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
633 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
636 /* Sample standard deviation (s^2) */
637 if (rec
->counter
<= 1)
641 * Apply Welford's method:
642 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
644 stddev
= rec
->counter
* rec
->time_squared
-
645 rec
->time
* rec
->time
;
648 * Divide only 1000 for ns^2 -> us^2 conversion.
649 * trace_print_graph_duration will divide 1000 again.
651 stddev
= div64_ul(stddev
,
652 rec
->counter
* (rec
->counter
- 1) * 1000);
656 trace_print_graph_duration(rec
->time
, &s
);
657 trace_seq_puts(&s
, " ");
658 trace_print_graph_duration(avg
, &s
);
659 trace_seq_puts(&s
, " ");
660 trace_print_graph_duration(stddev
, &s
);
661 trace_print_seq(m
, &s
);
665 mutex_unlock(&ftrace_profile_lock
);
670 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
672 struct ftrace_profile_page
*pg
;
674 pg
= stat
->pages
= stat
->start
;
677 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
682 memset(stat
->hash
, 0,
683 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
686 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
688 struct ftrace_profile_page
*pg
;
693 /* If we already allocated, do nothing */
697 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
701 #ifdef CONFIG_DYNAMIC_FTRACE
702 functions
= ftrace_update_tot_cnt
;
705 * We do not know the number of functions that exist because
706 * dynamic tracing is what counts them. With past experience
707 * we have around 20K functions. That should be more than enough.
708 * It is highly unlikely we will execute every function in
714 pg
= stat
->start
= stat
->pages
;
716 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
718 for (i
= 1; i
< pages
; i
++) {
719 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
730 unsigned long tmp
= (unsigned long)pg
;
742 static int ftrace_profile_init_cpu(int cpu
)
744 struct ftrace_profile_stat
*stat
;
747 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
750 /* If the profile is already created, simply reset it */
751 ftrace_profile_reset(stat
);
756 * We are profiling all functions, but usually only a few thousand
757 * functions are hit. We'll make a hash of 1024 items.
759 size
= FTRACE_PROFILE_HASH_SIZE
;
761 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
766 /* Preallocate the function profiling pages */
767 if (ftrace_profile_pages_init(stat
) < 0) {
776 static int ftrace_profile_init(void)
781 for_each_possible_cpu(cpu
) {
782 ret
= ftrace_profile_init_cpu(cpu
);
790 /* interrupts must be disabled */
791 static struct ftrace_profile
*
792 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
794 struct ftrace_profile
*rec
;
795 struct hlist_head
*hhd
;
798 key
= hash_long(ip
, FTRACE_PROFILE_HASH_BITS
);
799 hhd
= &stat
->hash
[key
];
801 if (hlist_empty(hhd
))
804 hlist_for_each_entry_rcu_notrace(rec
, hhd
, node
) {
812 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
813 struct ftrace_profile
*rec
)
817 key
= hash_long(rec
->ip
, FTRACE_PROFILE_HASH_BITS
);
818 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
822 * The memory is already allocated, this simply finds a new record to use.
824 static struct ftrace_profile
*
825 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
827 struct ftrace_profile
*rec
= NULL
;
829 /* prevent recursion (from NMIs) */
830 if (atomic_inc_return(&stat
->disabled
) != 1)
834 * Try to find the function again since an NMI
835 * could have added it
837 rec
= ftrace_find_profiled_func(stat
, ip
);
841 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
842 if (!stat
->pages
->next
)
844 stat
->pages
= stat
->pages
->next
;
847 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
849 ftrace_add_profile(stat
, rec
);
852 atomic_dec(&stat
->disabled
);
858 function_profile_call(unsigned long ip
, unsigned long parent_ip
,
859 struct ftrace_ops
*ops
, struct pt_regs
*regs
)
861 struct ftrace_profile_stat
*stat
;
862 struct ftrace_profile
*rec
;
865 if (!ftrace_profile_enabled
)
868 local_irq_save(flags
);
870 stat
= this_cpu_ptr(&ftrace_profile_stats
);
871 if (!stat
->hash
|| !ftrace_profile_enabled
)
874 rec
= ftrace_find_profiled_func(stat
, ip
);
876 rec
= ftrace_profile_alloc(stat
, ip
);
883 local_irq_restore(flags
);
886 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
887 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
889 int index
= trace
->depth
;
891 function_profile_call(trace
->func
, 0, NULL
, NULL
);
893 /* If function graph is shutting down, ret_stack can be NULL */
894 if (!current
->ret_stack
)
897 if (index
>= 0 && index
< FTRACE_RETFUNC_DEPTH
)
898 current
->ret_stack
[index
].subtime
= 0;
903 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
905 struct ftrace_profile_stat
*stat
;
906 unsigned long long calltime
;
907 struct ftrace_profile
*rec
;
910 local_irq_save(flags
);
911 stat
= this_cpu_ptr(&ftrace_profile_stats
);
912 if (!stat
->hash
|| !ftrace_profile_enabled
)
915 /* If the calltime was zero'd ignore it */
916 if (!trace
->calltime
)
919 calltime
= trace
->rettime
- trace
->calltime
;
921 if (!fgraph_graph_time
) {
924 index
= trace
->depth
;
926 /* Append this call time to the parent time to subtract */
928 current
->ret_stack
[index
- 1].subtime
+= calltime
;
930 if (current
->ret_stack
[index
].subtime
< calltime
)
931 calltime
-= current
->ret_stack
[index
].subtime
;
936 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
938 rec
->time
+= calltime
;
939 rec
->time_squared
+= calltime
* calltime
;
943 local_irq_restore(flags
);
946 static int register_ftrace_profiler(void)
948 return register_ftrace_graph(&profile_graph_return
,
949 &profile_graph_entry
);
952 static void unregister_ftrace_profiler(void)
954 unregister_ftrace_graph();
957 static struct ftrace_ops ftrace_profile_ops __read_mostly
= {
958 .func
= function_profile_call
,
959 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
960 INIT_OPS_HASH(ftrace_profile_ops
)
963 static int register_ftrace_profiler(void)
965 return register_ftrace_function(&ftrace_profile_ops
);
968 static void unregister_ftrace_profiler(void)
970 unregister_ftrace_function(&ftrace_profile_ops
);
972 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
975 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
976 size_t cnt
, loff_t
*ppos
)
981 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
987 mutex_lock(&ftrace_profile_lock
);
988 if (ftrace_profile_enabled
^ val
) {
990 ret
= ftrace_profile_init();
996 ret
= register_ftrace_profiler();
1001 ftrace_profile_enabled
= 1;
1003 ftrace_profile_enabled
= 0;
1005 * unregister_ftrace_profiler calls stop_machine
1006 * so this acts like an synchronize_sched.
1008 unregister_ftrace_profiler();
1012 mutex_unlock(&ftrace_profile_lock
);
1020 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
1021 size_t cnt
, loff_t
*ppos
)
1023 char buf
[64]; /* big enough to hold a number */
1026 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
1027 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1030 static const struct file_operations ftrace_profile_fops
= {
1031 .open
= tracing_open_generic
,
1032 .read
= ftrace_profile_read
,
1033 .write
= ftrace_profile_write
,
1034 .llseek
= default_llseek
,
1037 /* used to initialize the real stat files */
1038 static struct tracer_stat function_stats __initdata
= {
1039 .name
= "functions",
1040 .stat_start
= function_stat_start
,
1041 .stat_next
= function_stat_next
,
1042 .stat_cmp
= function_stat_cmp
,
1043 .stat_headers
= function_stat_headers
,
1044 .stat_show
= function_stat_show
1047 static __init
void ftrace_profile_tracefs(struct dentry
*d_tracer
)
1049 struct ftrace_profile_stat
*stat
;
1050 struct dentry
*entry
;
1055 for_each_possible_cpu(cpu
) {
1056 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
1058 name
= kasprintf(GFP_KERNEL
, "function%d", cpu
);
1061 * The files created are permanent, if something happens
1062 * we still do not free memory.
1065 "Could not allocate stat file for cpu %d\n",
1069 stat
->stat
= function_stats
;
1070 stat
->stat
.name
= name
;
1071 ret
= register_stat_tracer(&stat
->stat
);
1074 "Could not register function stat for cpu %d\n",
1081 entry
= tracefs_create_file("function_profile_enabled", 0644,
1082 d_tracer
, NULL
, &ftrace_profile_fops
);
1084 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1087 #else /* CONFIG_FUNCTION_PROFILER */
1088 static __init
void ftrace_profile_tracefs(struct dentry
*d_tracer
)
1091 #endif /* CONFIG_FUNCTION_PROFILER */
1093 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
1095 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1096 static int ftrace_graph_active
;
1098 # define ftrace_graph_active 0
1101 #ifdef CONFIG_DYNAMIC_FTRACE
1103 static struct ftrace_ops
*removed_ops
;
1106 * Set when doing a global update, like enabling all recs or disabling them.
1107 * It is not set when just updating a single ftrace_ops.
1109 static bool update_all_ops
;
1111 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1112 # error Dynamic ftrace depends on MCOUNT_RECORD
1115 struct ftrace_func_entry
{
1116 struct hlist_node hlist
;
1120 struct ftrace_func_probe
{
1121 struct ftrace_probe_ops
*probe_ops
;
1122 struct ftrace_ops ops
;
1123 struct trace_array
*tr
;
1124 struct list_head list
;
1130 * We make these constant because no one should touch them,
1131 * but they are used as the default "empty hash", to avoid allocating
1132 * it all the time. These are in a read only section such that if
1133 * anyone does try to modify it, it will cause an exception.
1135 static const struct hlist_head empty_buckets
[1];
1136 static const struct ftrace_hash empty_hash
= {
1137 .buckets
= (struct hlist_head
*)empty_buckets
,
1139 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1141 static struct ftrace_ops global_ops
= {
1142 .func
= ftrace_stub
,
1143 .local_hash
.notrace_hash
= EMPTY_HASH
,
1144 .local_hash
.filter_hash
= EMPTY_HASH
,
1145 INIT_OPS_HASH(global_ops
)
1146 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
|
1147 FTRACE_OPS_FL_INITIALIZED
|
1152 * This is used by __kernel_text_address() to return true if the
1153 * address is on a dynamically allocated trampoline that would
1154 * not return true for either core_kernel_text() or
1155 * is_module_text_address().
1157 bool is_ftrace_trampoline(unsigned long addr
)
1159 struct ftrace_ops
*op
;
1163 * Some of the ops may be dynamically allocated,
1164 * they are freed after a synchronize_sched().
1166 preempt_disable_notrace();
1168 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
1170 * This is to check for dynamically allocated trampolines.
1171 * Trampolines that are in kernel text will have
1172 * core_kernel_text() return true.
1174 if (op
->trampoline
&& op
->trampoline_size
)
1175 if (addr
>= op
->trampoline
&&
1176 addr
< op
->trampoline
+ op
->trampoline_size
) {
1180 } while_for_each_ftrace_op(op
);
1183 preempt_enable_notrace();
1188 struct ftrace_page
{
1189 struct ftrace_page
*next
;
1190 struct dyn_ftrace
*records
;
1195 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1196 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1198 /* estimate from running different kernels */
1199 #define NR_TO_INIT 10000
1201 static struct ftrace_page
*ftrace_pages_start
;
1202 static struct ftrace_page
*ftrace_pages
;
1204 static __always_inline
unsigned long
1205 ftrace_hash_key(struct ftrace_hash
*hash
, unsigned long ip
)
1207 if (hash
->size_bits
> 0)
1208 return hash_long(ip
, hash
->size_bits
);
1213 /* Only use this function if ftrace_hash_empty() has already been tested */
1214 static __always_inline
struct ftrace_func_entry
*
1215 __ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1218 struct ftrace_func_entry
*entry
;
1219 struct hlist_head
*hhd
;
1221 key
= ftrace_hash_key(hash
, ip
);
1222 hhd
= &hash
->buckets
[key
];
1224 hlist_for_each_entry_rcu_notrace(entry
, hhd
, hlist
) {
1225 if (entry
->ip
== ip
)
1232 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1233 * @hash: The hash to look at
1234 * @ip: The instruction pointer to test
1236 * Search a given @hash to see if a given instruction pointer (@ip)
1239 * Returns the entry that holds the @ip if found. NULL otherwise.
1241 struct ftrace_func_entry
*
1242 ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1244 if (ftrace_hash_empty(hash
))
1247 return __ftrace_lookup_ip(hash
, ip
);
1250 static void __add_hash_entry(struct ftrace_hash
*hash
,
1251 struct ftrace_func_entry
*entry
)
1253 struct hlist_head
*hhd
;
1256 key
= ftrace_hash_key(hash
, entry
->ip
);
1257 hhd
= &hash
->buckets
[key
];
1258 hlist_add_head(&entry
->hlist
, hhd
);
1262 static int add_hash_entry(struct ftrace_hash
*hash
, unsigned long ip
)
1264 struct ftrace_func_entry
*entry
;
1266 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1271 __add_hash_entry(hash
, entry
);
1277 free_hash_entry(struct ftrace_hash
*hash
,
1278 struct ftrace_func_entry
*entry
)
1280 hlist_del(&entry
->hlist
);
1286 remove_hash_entry(struct ftrace_hash
*hash
,
1287 struct ftrace_func_entry
*entry
)
1289 hlist_del_rcu(&entry
->hlist
);
1293 static void ftrace_hash_clear(struct ftrace_hash
*hash
)
1295 struct hlist_head
*hhd
;
1296 struct hlist_node
*tn
;
1297 struct ftrace_func_entry
*entry
;
1298 int size
= 1 << hash
->size_bits
;
1304 for (i
= 0; i
< size
; i
++) {
1305 hhd
= &hash
->buckets
[i
];
1306 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
)
1307 free_hash_entry(hash
, entry
);
1309 FTRACE_WARN_ON(hash
->count
);
1312 static void free_ftrace_mod(struct ftrace_mod_load
*ftrace_mod
)
1314 list_del(&ftrace_mod
->list
);
1315 kfree(ftrace_mod
->module
);
1316 kfree(ftrace_mod
->func
);
1320 static void clear_ftrace_mod_list(struct list_head
*head
)
1322 struct ftrace_mod_load
*p
, *n
;
1324 /* stack tracer isn't supported yet */
1328 mutex_lock(&ftrace_lock
);
1329 list_for_each_entry_safe(p
, n
, head
, list
)
1331 mutex_unlock(&ftrace_lock
);
1334 static void free_ftrace_hash(struct ftrace_hash
*hash
)
1336 if (!hash
|| hash
== EMPTY_HASH
)
1338 ftrace_hash_clear(hash
);
1339 kfree(hash
->buckets
);
1343 static void __free_ftrace_hash_rcu(struct rcu_head
*rcu
)
1345 struct ftrace_hash
*hash
;
1347 hash
= container_of(rcu
, struct ftrace_hash
, rcu
);
1348 free_ftrace_hash(hash
);
1351 static void free_ftrace_hash_rcu(struct ftrace_hash
*hash
)
1353 if (!hash
|| hash
== EMPTY_HASH
)
1355 call_rcu_sched(&hash
->rcu
, __free_ftrace_hash_rcu
);
1358 void ftrace_free_filter(struct ftrace_ops
*ops
)
1360 ftrace_ops_init(ops
);
1361 free_ftrace_hash(ops
->func_hash
->filter_hash
);
1362 free_ftrace_hash(ops
->func_hash
->notrace_hash
);
1365 static struct ftrace_hash
*alloc_ftrace_hash(int size_bits
)
1367 struct ftrace_hash
*hash
;
1370 hash
= kzalloc(sizeof(*hash
), GFP_KERNEL
);
1374 size
= 1 << size_bits
;
1375 hash
->buckets
= kcalloc(size
, sizeof(*hash
->buckets
), GFP_KERNEL
);
1377 if (!hash
->buckets
) {
1382 hash
->size_bits
= size_bits
;
1388 static int ftrace_add_mod(struct trace_array
*tr
,
1389 const char *func
, const char *module
,
1392 struct ftrace_mod_load
*ftrace_mod
;
1393 struct list_head
*mod_head
= enable
? &tr
->mod_trace
: &tr
->mod_notrace
;
1395 ftrace_mod
= kzalloc(sizeof(*ftrace_mod
), GFP_KERNEL
);
1399 ftrace_mod
->func
= kstrdup(func
, GFP_KERNEL
);
1400 ftrace_mod
->module
= kstrdup(module
, GFP_KERNEL
);
1401 ftrace_mod
->enable
= enable
;
1403 if (!ftrace_mod
->func
|| !ftrace_mod
->module
)
1406 list_add(&ftrace_mod
->list
, mod_head
);
1411 free_ftrace_mod(ftrace_mod
);
1416 static struct ftrace_hash
*
1417 alloc_and_copy_ftrace_hash(int size_bits
, struct ftrace_hash
*hash
)
1419 struct ftrace_func_entry
*entry
;
1420 struct ftrace_hash
*new_hash
;
1425 new_hash
= alloc_ftrace_hash(size_bits
);
1430 new_hash
->flags
= hash
->flags
;
1433 if (ftrace_hash_empty(hash
))
1436 size
= 1 << hash
->size_bits
;
1437 for (i
= 0; i
< size
; i
++) {
1438 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
1439 ret
= add_hash_entry(new_hash
, entry
->ip
);
1445 FTRACE_WARN_ON(new_hash
->count
!= hash
->count
);
1450 free_ftrace_hash(new_hash
);
1455 ftrace_hash_rec_disable_modify(struct ftrace_ops
*ops
, int filter_hash
);
1457 ftrace_hash_rec_enable_modify(struct ftrace_ops
*ops
, int filter_hash
);
1459 static int ftrace_hash_ipmodify_update(struct ftrace_ops
*ops
,
1460 struct ftrace_hash
*new_hash
);
1462 static struct ftrace_hash
*
1463 __ftrace_hash_move(struct ftrace_hash
*src
)
1465 struct ftrace_func_entry
*entry
;
1466 struct hlist_node
*tn
;
1467 struct hlist_head
*hhd
;
1468 struct ftrace_hash
*new_hash
;
1469 int size
= src
->count
;
1474 * If the new source is empty, just return the empty_hash.
1476 if (ftrace_hash_empty(src
))
1480 * Make the hash size about 1/2 the # found
1482 for (size
/= 2; size
; size
>>= 1)
1485 /* Don't allocate too much */
1486 if (bits
> FTRACE_HASH_MAX_BITS
)
1487 bits
= FTRACE_HASH_MAX_BITS
;
1489 new_hash
= alloc_ftrace_hash(bits
);
1493 new_hash
->flags
= src
->flags
;
1495 size
= 1 << src
->size_bits
;
1496 for (i
= 0; i
< size
; i
++) {
1497 hhd
= &src
->buckets
[i
];
1498 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
) {
1499 remove_hash_entry(src
, entry
);
1500 __add_hash_entry(new_hash
, entry
);
1508 ftrace_hash_move(struct ftrace_ops
*ops
, int enable
,
1509 struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1511 struct ftrace_hash
*new_hash
;
1514 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1515 if (ops
->flags
& FTRACE_OPS_FL_IPMODIFY
&& !enable
)
1518 new_hash
= __ftrace_hash_move(src
);
1522 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1524 /* IPMODIFY should be updated only when filter_hash updating */
1525 ret
= ftrace_hash_ipmodify_update(ops
, new_hash
);
1527 free_ftrace_hash(new_hash
);
1533 * Remove the current set, update the hash and add
1536 ftrace_hash_rec_disable_modify(ops
, enable
);
1538 rcu_assign_pointer(*dst
, new_hash
);
1540 ftrace_hash_rec_enable_modify(ops
, enable
);
1545 static bool hash_contains_ip(unsigned long ip
,
1546 struct ftrace_ops_hash
*hash
)
1549 * The function record is a match if it exists in the filter
1550 * hash and not in the notrace hash. Note, an emty hash is
1551 * considered a match for the filter hash, but an empty
1552 * notrace hash is considered not in the notrace hash.
1554 return (ftrace_hash_empty(hash
->filter_hash
) ||
1555 __ftrace_lookup_ip(hash
->filter_hash
, ip
)) &&
1556 (ftrace_hash_empty(hash
->notrace_hash
) ||
1557 !__ftrace_lookup_ip(hash
->notrace_hash
, ip
));
1561 * Test the hashes for this ops to see if we want to call
1562 * the ops->func or not.
1564 * It's a match if the ip is in the ops->filter_hash or
1565 * the filter_hash does not exist or is empty,
1567 * the ip is not in the ops->notrace_hash.
1569 * This needs to be called with preemption disabled as
1570 * the hashes are freed with call_rcu_sched().
1573 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
1575 struct ftrace_ops_hash hash
;
1578 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1580 * There's a small race when adding ops that the ftrace handler
1581 * that wants regs, may be called without them. We can not
1582 * allow that handler to be called if regs is NULL.
1584 if (regs
== NULL
&& (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
))
1588 rcu_assign_pointer(hash
.filter_hash
, ops
->func_hash
->filter_hash
);
1589 rcu_assign_pointer(hash
.notrace_hash
, ops
->func_hash
->notrace_hash
);
1591 if (hash_contains_ip(ip
, &hash
))
1600 * This is a double for. Do not use 'break' to break out of the loop,
1601 * you must use a goto.
1603 #define do_for_each_ftrace_rec(pg, rec) \
1604 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1606 for (_____i = 0; _____i < pg->index; _____i++) { \
1607 rec = &pg->records[_____i];
1609 #define while_for_each_ftrace_rec() \
1614 static int ftrace_cmp_recs(const void *a
, const void *b
)
1616 const struct dyn_ftrace
*key
= a
;
1617 const struct dyn_ftrace
*rec
= b
;
1619 if (key
->flags
< rec
->ip
)
1621 if (key
->ip
>= rec
->ip
+ MCOUNT_INSN_SIZE
)
1627 * ftrace_location_range - return the first address of a traced location
1628 * if it touches the given ip range
1629 * @start: start of range to search.
1630 * @end: end of range to search (inclusive). @end points to the last byte
1633 * Returns rec->ip if the related ftrace location is a least partly within
1634 * the given address range. That is, the first address of the instruction
1635 * that is either a NOP or call to the function tracer. It checks the ftrace
1636 * internal tables to determine if the address belongs or not.
1638 unsigned long ftrace_location_range(unsigned long start
, unsigned long end
)
1640 struct ftrace_page
*pg
;
1641 struct dyn_ftrace
*rec
;
1642 struct dyn_ftrace key
;
1645 key
.flags
= end
; /* overload flags, as it is unsigned long */
1647 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
1648 if (end
< pg
->records
[0].ip
||
1649 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
1651 rec
= bsearch(&key
, pg
->records
, pg
->index
,
1652 sizeof(struct dyn_ftrace
),
1662 * ftrace_location - return true if the ip giving is a traced location
1663 * @ip: the instruction pointer to check
1665 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1666 * That is, the instruction that is either a NOP or call to
1667 * the function tracer. It checks the ftrace internal tables to
1668 * determine if the address belongs or not.
1670 unsigned long ftrace_location(unsigned long ip
)
1672 return ftrace_location_range(ip
, ip
);
1676 * ftrace_text_reserved - return true if range contains an ftrace location
1677 * @start: start of range to search
1678 * @end: end of range to search (inclusive). @end points to the last byte to check.
1680 * Returns 1 if @start and @end contains a ftrace location.
1681 * That is, the instruction that is either a NOP or call to
1682 * the function tracer. It checks the ftrace internal tables to
1683 * determine if the address belongs or not.
1685 int ftrace_text_reserved(const void *start
, const void *end
)
1689 ret
= ftrace_location_range((unsigned long)start
,
1690 (unsigned long)end
);
1695 /* Test if ops registered to this rec needs regs */
1696 static bool test_rec_ops_needs_regs(struct dyn_ftrace
*rec
)
1698 struct ftrace_ops
*ops
;
1699 bool keep_regs
= false;
1701 for (ops
= ftrace_ops_list
;
1702 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
1703 /* pass rec in as regs to have non-NULL val */
1704 if (ftrace_ops_test(ops
, rec
->ip
, rec
)) {
1705 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
) {
1715 static struct ftrace_ops
*
1716 ftrace_find_tramp_ops_any(struct dyn_ftrace
*rec
);
1717 static struct ftrace_ops
*
1718 ftrace_find_tramp_ops_next(struct dyn_ftrace
*rec
, struct ftrace_ops
*ops
);
1720 static bool __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1724 struct ftrace_hash
*hash
;
1725 struct ftrace_hash
*other_hash
;
1726 struct ftrace_page
*pg
;
1727 struct dyn_ftrace
*rec
;
1728 bool update
= false;
1732 /* Only update if the ops has been registered */
1733 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1737 * In the filter_hash case:
1738 * If the count is zero, we update all records.
1739 * Otherwise we just update the items in the hash.
1741 * In the notrace_hash case:
1742 * We enable the update in the hash.
1743 * As disabling notrace means enabling the tracing,
1744 * and enabling notrace means disabling, the inc variable
1748 hash
= ops
->func_hash
->filter_hash
;
1749 other_hash
= ops
->func_hash
->notrace_hash
;
1750 if (ftrace_hash_empty(hash
))
1754 hash
= ops
->func_hash
->notrace_hash
;
1755 other_hash
= ops
->func_hash
->filter_hash
;
1757 * If the notrace hash has no items,
1758 * then there's nothing to do.
1760 if (ftrace_hash_empty(hash
))
1764 do_for_each_ftrace_rec(pg
, rec
) {
1765 int in_other_hash
= 0;
1769 if (rec
->flags
& FTRACE_FL_DISABLED
)
1774 * Only the filter_hash affects all records.
1775 * Update if the record is not in the notrace hash.
1777 if (!other_hash
|| !ftrace_lookup_ip(other_hash
, rec
->ip
))
1780 in_hash
= !!ftrace_lookup_ip(hash
, rec
->ip
);
1781 in_other_hash
= !!ftrace_lookup_ip(other_hash
, rec
->ip
);
1784 * If filter_hash is set, we want to match all functions
1785 * that are in the hash but not in the other hash.
1787 * If filter_hash is not set, then we are decrementing.
1788 * That means we match anything that is in the hash
1789 * and also in the other_hash. That is, we need to turn
1790 * off functions in the other hash because they are disabled
1793 if (filter_hash
&& in_hash
&& !in_other_hash
)
1795 else if (!filter_hash
&& in_hash
&&
1796 (in_other_hash
|| ftrace_hash_empty(other_hash
)))
1804 if (FTRACE_WARN_ON(ftrace_rec_count(rec
) == FTRACE_REF_MAX
))
1808 * If there's only a single callback registered to a
1809 * function, and the ops has a trampoline registered
1810 * for it, then we can call it directly.
1812 if (ftrace_rec_count(rec
) == 1 && ops
->trampoline
)
1813 rec
->flags
|= FTRACE_FL_TRAMP
;
1816 * If we are adding another function callback
1817 * to this function, and the previous had a
1818 * custom trampoline in use, then we need to go
1819 * back to the default trampoline.
1821 rec
->flags
&= ~FTRACE_FL_TRAMP
;
1824 * If any ops wants regs saved for this function
1825 * then all ops will get saved regs.
1827 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
1828 rec
->flags
|= FTRACE_FL_REGS
;
1830 if (FTRACE_WARN_ON(ftrace_rec_count(rec
) == 0))
1835 * If the rec had REGS enabled and the ops that is
1836 * being removed had REGS set, then see if there is
1837 * still any ops for this record that wants regs.
1838 * If not, we can stop recording them.
1840 if (ftrace_rec_count(rec
) > 0 &&
1841 rec
->flags
& FTRACE_FL_REGS
&&
1842 ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
) {
1843 if (!test_rec_ops_needs_regs(rec
))
1844 rec
->flags
&= ~FTRACE_FL_REGS
;
1848 * The TRAMP needs to be set only if rec count
1849 * is decremented to one, and the ops that is
1850 * left has a trampoline. As TRAMP can only be
1851 * enabled if there is only a single ops attached
1854 if (ftrace_rec_count(rec
) == 1 &&
1855 ftrace_find_tramp_ops_any(rec
))
1856 rec
->flags
|= FTRACE_FL_TRAMP
;
1858 rec
->flags
&= ~FTRACE_FL_TRAMP
;
1861 * flags will be cleared in ftrace_check_record()
1862 * if rec count is zero.
1867 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1868 update
|= ftrace_test_record(rec
, 1) != FTRACE_UPDATE_IGNORE
;
1870 /* Shortcut, if we handled all records, we are done. */
1871 if (!all
&& count
== hash
->count
)
1873 } while_for_each_ftrace_rec();
1878 static bool ftrace_hash_rec_disable(struct ftrace_ops
*ops
,
1881 return __ftrace_hash_rec_update(ops
, filter_hash
, 0);
1884 static bool ftrace_hash_rec_enable(struct ftrace_ops
*ops
,
1887 return __ftrace_hash_rec_update(ops
, filter_hash
, 1);
1890 static void ftrace_hash_rec_update_modify(struct ftrace_ops
*ops
,
1891 int filter_hash
, int inc
)
1893 struct ftrace_ops
*op
;
1895 __ftrace_hash_rec_update(ops
, filter_hash
, inc
);
1897 if (ops
->func_hash
!= &global_ops
.local_hash
)
1901 * If the ops shares the global_ops hash, then we need to update
1902 * all ops that are enabled and use this hash.
1904 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
1908 if (op
->func_hash
== &global_ops
.local_hash
)
1909 __ftrace_hash_rec_update(op
, filter_hash
, inc
);
1910 } while_for_each_ftrace_op(op
);
1913 static void ftrace_hash_rec_disable_modify(struct ftrace_ops
*ops
,
1916 ftrace_hash_rec_update_modify(ops
, filter_hash
, 0);
1919 static void ftrace_hash_rec_enable_modify(struct ftrace_ops
*ops
,
1922 ftrace_hash_rec_update_modify(ops
, filter_hash
, 1);
1926 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1927 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1928 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1929 * Note that old_hash and new_hash has below meanings
1930 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1931 * - If the hash is EMPTY_HASH, it hits nothing
1932 * - Anything else hits the recs which match the hash entries.
1934 static int __ftrace_hash_update_ipmodify(struct ftrace_ops
*ops
,
1935 struct ftrace_hash
*old_hash
,
1936 struct ftrace_hash
*new_hash
)
1938 struct ftrace_page
*pg
;
1939 struct dyn_ftrace
*rec
, *end
= NULL
;
1942 /* Only update if the ops has been registered */
1943 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1946 if (!(ops
->flags
& FTRACE_OPS_FL_IPMODIFY
))
1950 * Since the IPMODIFY is a very address sensitive action, we do not
1951 * allow ftrace_ops to set all functions to new hash.
1953 if (!new_hash
|| !old_hash
)
1956 /* Update rec->flags */
1957 do_for_each_ftrace_rec(pg
, rec
) {
1959 if (rec
->flags
& FTRACE_FL_DISABLED
)
1962 /* We need to update only differences of filter_hash */
1963 in_old
= !!ftrace_lookup_ip(old_hash
, rec
->ip
);
1964 in_new
= !!ftrace_lookup_ip(new_hash
, rec
->ip
);
1965 if (in_old
== in_new
)
1969 /* New entries must ensure no others are using it */
1970 if (rec
->flags
& FTRACE_FL_IPMODIFY
)
1972 rec
->flags
|= FTRACE_FL_IPMODIFY
;
1973 } else /* Removed entry */
1974 rec
->flags
&= ~FTRACE_FL_IPMODIFY
;
1975 } while_for_each_ftrace_rec();
1982 /* Roll back what we did above */
1983 do_for_each_ftrace_rec(pg
, rec
) {
1985 if (rec
->flags
& FTRACE_FL_DISABLED
)
1991 in_old
= !!ftrace_lookup_ip(old_hash
, rec
->ip
);
1992 in_new
= !!ftrace_lookup_ip(new_hash
, rec
->ip
);
1993 if (in_old
== in_new
)
1997 rec
->flags
&= ~FTRACE_FL_IPMODIFY
;
1999 rec
->flags
|= FTRACE_FL_IPMODIFY
;
2000 } while_for_each_ftrace_rec();
2006 static int ftrace_hash_ipmodify_enable(struct ftrace_ops
*ops
)
2008 struct ftrace_hash
*hash
= ops
->func_hash
->filter_hash
;
2010 if (ftrace_hash_empty(hash
))
2013 return __ftrace_hash_update_ipmodify(ops
, EMPTY_HASH
, hash
);
2016 /* Disabling always succeeds */
2017 static void ftrace_hash_ipmodify_disable(struct ftrace_ops
*ops
)
2019 struct ftrace_hash
*hash
= ops
->func_hash
->filter_hash
;
2021 if (ftrace_hash_empty(hash
))
2024 __ftrace_hash_update_ipmodify(ops
, hash
, EMPTY_HASH
);
2027 static int ftrace_hash_ipmodify_update(struct ftrace_ops
*ops
,
2028 struct ftrace_hash
*new_hash
)
2030 struct ftrace_hash
*old_hash
= ops
->func_hash
->filter_hash
;
2032 if (ftrace_hash_empty(old_hash
))
2035 if (ftrace_hash_empty(new_hash
))
2038 return __ftrace_hash_update_ipmodify(ops
, old_hash
, new_hash
);
2041 static void print_ip_ins(const char *fmt
, const unsigned char *p
)
2045 printk(KERN_CONT
"%s", fmt
);
2047 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
2048 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
2051 enum ftrace_bug_type ftrace_bug_type
;
2052 const void *ftrace_expected
;
2054 static void print_bug_type(void)
2056 switch (ftrace_bug_type
) {
2057 case FTRACE_BUG_UNKNOWN
:
2059 case FTRACE_BUG_INIT
:
2060 pr_info("Initializing ftrace call sites\n");
2062 case FTRACE_BUG_NOP
:
2063 pr_info("Setting ftrace call site to NOP\n");
2065 case FTRACE_BUG_CALL
:
2066 pr_info("Setting ftrace call site to call ftrace function\n");
2068 case FTRACE_BUG_UPDATE
:
2069 pr_info("Updating ftrace call site to call a different ftrace function\n");
2075 * ftrace_bug - report and shutdown function tracer
2076 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2077 * @rec: The record that failed
2079 * The arch code that enables or disables the function tracing
2080 * can call ftrace_bug() when it has detected a problem in
2081 * modifying the code. @failed should be one of either:
2082 * EFAULT - if the problem happens on reading the @ip address
2083 * EINVAL - if what is read at @ip is not what was expected
2084 * EPERM - if the problem happens on writting to the @ip address
2086 void ftrace_bug(int failed
, struct dyn_ftrace
*rec
)
2088 unsigned long ip
= rec
? rec
->ip
: 0;
2092 FTRACE_WARN_ON_ONCE(1);
2093 pr_info("ftrace faulted on modifying ");
2097 FTRACE_WARN_ON_ONCE(1);
2098 pr_info("ftrace failed to modify ");
2100 print_ip_ins(" actual: ", (unsigned char *)ip
);
2102 if (ftrace_expected
) {
2103 print_ip_ins(" expected: ", ftrace_expected
);
2108 FTRACE_WARN_ON_ONCE(1);
2109 pr_info("ftrace faulted on writing ");
2113 FTRACE_WARN_ON_ONCE(1);
2114 pr_info("ftrace faulted on unknown error ");
2119 struct ftrace_ops
*ops
= NULL
;
2121 pr_info("ftrace record flags: %lx\n", rec
->flags
);
2122 pr_cont(" (%ld)%s", ftrace_rec_count(rec
),
2123 rec
->flags
& FTRACE_FL_REGS
? " R" : " ");
2124 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
2125 ops
= ftrace_find_tramp_ops_any(rec
);
2128 pr_cont("\ttramp: %pS (%pS)",
2129 (void *)ops
->trampoline
,
2131 ops
= ftrace_find_tramp_ops_next(rec
, ops
);
2134 pr_cont("\ttramp: ERROR!");
2137 ip
= ftrace_get_addr_curr(rec
);
2138 pr_cont("\n expected tramp: %lx\n", ip
);
2142 static int ftrace_check_record(struct dyn_ftrace
*rec
, int enable
, int update
)
2144 unsigned long flag
= 0UL;
2146 ftrace_bug_type
= FTRACE_BUG_UNKNOWN
;
2148 if (rec
->flags
& FTRACE_FL_DISABLED
)
2149 return FTRACE_UPDATE_IGNORE
;
2152 * If we are updating calls:
2154 * If the record has a ref count, then we need to enable it
2155 * because someone is using it.
2157 * Otherwise we make sure its disabled.
2159 * If we are disabling calls, then disable all records that
2162 if (enable
&& ftrace_rec_count(rec
))
2163 flag
= FTRACE_FL_ENABLED
;
2166 * If enabling and the REGS flag does not match the REGS_EN, or
2167 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2168 * this record. Set flags to fail the compare against ENABLED.
2171 if (!(rec
->flags
& FTRACE_FL_REGS
) !=
2172 !(rec
->flags
& FTRACE_FL_REGS_EN
))
2173 flag
|= FTRACE_FL_REGS
;
2175 if (!(rec
->flags
& FTRACE_FL_TRAMP
) !=
2176 !(rec
->flags
& FTRACE_FL_TRAMP_EN
))
2177 flag
|= FTRACE_FL_TRAMP
;
2180 /* If the state of this record hasn't changed, then do nothing */
2181 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
2182 return FTRACE_UPDATE_IGNORE
;
2185 /* Save off if rec is being enabled (for return value) */
2186 flag
^= rec
->flags
& FTRACE_FL_ENABLED
;
2189 rec
->flags
|= FTRACE_FL_ENABLED
;
2190 if (flag
& FTRACE_FL_REGS
) {
2191 if (rec
->flags
& FTRACE_FL_REGS
)
2192 rec
->flags
|= FTRACE_FL_REGS_EN
;
2194 rec
->flags
&= ~FTRACE_FL_REGS_EN
;
2196 if (flag
& FTRACE_FL_TRAMP
) {
2197 if (rec
->flags
& FTRACE_FL_TRAMP
)
2198 rec
->flags
|= FTRACE_FL_TRAMP_EN
;
2200 rec
->flags
&= ~FTRACE_FL_TRAMP_EN
;
2205 * If this record is being updated from a nop, then
2206 * return UPDATE_MAKE_CALL.
2208 * return UPDATE_MODIFY_CALL to tell the caller to convert
2209 * from the save regs, to a non-save regs function or
2210 * vice versa, or from a trampoline call.
2212 if (flag
& FTRACE_FL_ENABLED
) {
2213 ftrace_bug_type
= FTRACE_BUG_CALL
;
2214 return FTRACE_UPDATE_MAKE_CALL
;
2217 ftrace_bug_type
= FTRACE_BUG_UPDATE
;
2218 return FTRACE_UPDATE_MODIFY_CALL
;
2222 /* If there's no more users, clear all flags */
2223 if (!ftrace_rec_count(rec
))
2227 * Just disable the record, but keep the ops TRAMP
2228 * and REGS states. The _EN flags must be disabled though.
2230 rec
->flags
&= ~(FTRACE_FL_ENABLED
| FTRACE_FL_TRAMP_EN
|
2234 ftrace_bug_type
= FTRACE_BUG_NOP
;
2235 return FTRACE_UPDATE_MAKE_NOP
;
2239 * ftrace_update_record, set a record that now is tracing or not
2240 * @rec: the record to update
2241 * @enable: set to 1 if the record is tracing, zero to force disable
2243 * The records that represent all functions that can be traced need
2244 * to be updated when tracing has been enabled.
2246 int ftrace_update_record(struct dyn_ftrace
*rec
, int enable
)
2248 return ftrace_check_record(rec
, enable
, 1);
2252 * ftrace_test_record, check if the record has been enabled or not
2253 * @rec: the record to test
2254 * @enable: set to 1 to check if enabled, 0 if it is disabled
2256 * The arch code may need to test if a record is already set to
2257 * tracing to determine how to modify the function code that it
2260 int ftrace_test_record(struct dyn_ftrace
*rec
, int enable
)
2262 return ftrace_check_record(rec
, enable
, 0);
2265 static struct ftrace_ops
*
2266 ftrace_find_tramp_ops_any(struct dyn_ftrace
*rec
)
2268 struct ftrace_ops
*op
;
2269 unsigned long ip
= rec
->ip
;
2271 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2273 if (!op
->trampoline
)
2276 if (hash_contains_ip(ip
, op
->func_hash
))
2278 } while_for_each_ftrace_op(op
);
2283 static struct ftrace_ops
*
2284 ftrace_find_tramp_ops_next(struct dyn_ftrace
*rec
,
2285 struct ftrace_ops
*op
)
2287 unsigned long ip
= rec
->ip
;
2289 while_for_each_ftrace_op(op
) {
2291 if (!op
->trampoline
)
2294 if (hash_contains_ip(ip
, op
->func_hash
))
2301 static struct ftrace_ops
*
2302 ftrace_find_tramp_ops_curr(struct dyn_ftrace
*rec
)
2304 struct ftrace_ops
*op
;
2305 unsigned long ip
= rec
->ip
;
2308 * Need to check removed ops first.
2309 * If they are being removed, and this rec has a tramp,
2310 * and this rec is in the ops list, then it would be the
2311 * one with the tramp.
2314 if (hash_contains_ip(ip
, &removed_ops
->old_hash
))
2319 * Need to find the current trampoline for a rec.
2320 * Now, a trampoline is only attached to a rec if there
2321 * was a single 'ops' attached to it. But this can be called
2322 * when we are adding another op to the rec or removing the
2323 * current one. Thus, if the op is being added, we can
2324 * ignore it because it hasn't attached itself to the rec
2327 * If an ops is being modified (hooking to different functions)
2328 * then we don't care about the new functions that are being
2329 * added, just the old ones (that are probably being removed).
2331 * If we are adding an ops to a function that already is using
2332 * a trampoline, it needs to be removed (trampolines are only
2333 * for single ops connected), then an ops that is not being
2334 * modified also needs to be checked.
2336 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2338 if (!op
->trampoline
)
2342 * If the ops is being added, it hasn't gotten to
2343 * the point to be removed from this tree yet.
2345 if (op
->flags
& FTRACE_OPS_FL_ADDING
)
2350 * If the ops is being modified and is in the old
2351 * hash, then it is probably being removed from this
2354 if ((op
->flags
& FTRACE_OPS_FL_MODIFYING
) &&
2355 hash_contains_ip(ip
, &op
->old_hash
))
2358 * If the ops is not being added or modified, and it's
2359 * in its normal filter hash, then this must be the one
2362 if (!(op
->flags
& FTRACE_OPS_FL_MODIFYING
) &&
2363 hash_contains_ip(ip
, op
->func_hash
))
2366 } while_for_each_ftrace_op(op
);
2371 static struct ftrace_ops
*
2372 ftrace_find_tramp_ops_new(struct dyn_ftrace
*rec
)
2374 struct ftrace_ops
*op
;
2375 unsigned long ip
= rec
->ip
;
2377 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2378 /* pass rec in as regs to have non-NULL val */
2379 if (hash_contains_ip(ip
, op
->func_hash
))
2381 } while_for_each_ftrace_op(op
);
2387 * ftrace_get_addr_new - Get the call address to set to
2388 * @rec: The ftrace record descriptor
2390 * If the record has the FTRACE_FL_REGS set, that means that it
2391 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2392 * is not not set, then it wants to convert to the normal callback.
2394 * Returns the address of the trampoline to set to
2396 unsigned long ftrace_get_addr_new(struct dyn_ftrace
*rec
)
2398 struct ftrace_ops
*ops
;
2400 /* Trampolines take precedence over regs */
2401 if (rec
->flags
& FTRACE_FL_TRAMP
) {
2402 ops
= ftrace_find_tramp_ops_new(rec
);
2403 if (FTRACE_WARN_ON(!ops
|| !ops
->trampoline
)) {
2404 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2405 (void *)rec
->ip
, (void *)rec
->ip
, rec
->flags
);
2406 /* Ftrace is shutting down, return anything */
2407 return (unsigned long)FTRACE_ADDR
;
2409 return ops
->trampoline
;
2412 if (rec
->flags
& FTRACE_FL_REGS
)
2413 return (unsigned long)FTRACE_REGS_ADDR
;
2415 return (unsigned long)FTRACE_ADDR
;
2419 * ftrace_get_addr_curr - Get the call address that is already there
2420 * @rec: The ftrace record descriptor
2422 * The FTRACE_FL_REGS_EN is set when the record already points to
2423 * a function that saves all the regs. Basically the '_EN' version
2424 * represents the current state of the function.
2426 * Returns the address of the trampoline that is currently being called
2428 unsigned long ftrace_get_addr_curr(struct dyn_ftrace
*rec
)
2430 struct ftrace_ops
*ops
;
2432 /* Trampolines take precedence over regs */
2433 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
2434 ops
= ftrace_find_tramp_ops_curr(rec
);
2435 if (FTRACE_WARN_ON(!ops
)) {
2436 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2437 (void *)rec
->ip
, (void *)rec
->ip
);
2438 /* Ftrace is shutting down, return anything */
2439 return (unsigned long)FTRACE_ADDR
;
2441 return ops
->trampoline
;
2444 if (rec
->flags
& FTRACE_FL_REGS_EN
)
2445 return (unsigned long)FTRACE_REGS_ADDR
;
2447 return (unsigned long)FTRACE_ADDR
;
2451 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
2453 unsigned long ftrace_old_addr
;
2454 unsigned long ftrace_addr
;
2457 ftrace_addr
= ftrace_get_addr_new(rec
);
2459 /* This needs to be done before we call ftrace_update_record */
2460 ftrace_old_addr
= ftrace_get_addr_curr(rec
);
2462 ret
= ftrace_update_record(rec
, enable
);
2464 ftrace_bug_type
= FTRACE_BUG_UNKNOWN
;
2467 case FTRACE_UPDATE_IGNORE
:
2470 case FTRACE_UPDATE_MAKE_CALL
:
2471 ftrace_bug_type
= FTRACE_BUG_CALL
;
2472 return ftrace_make_call(rec
, ftrace_addr
);
2474 case FTRACE_UPDATE_MAKE_NOP
:
2475 ftrace_bug_type
= FTRACE_BUG_NOP
;
2476 return ftrace_make_nop(NULL
, rec
, ftrace_old_addr
);
2478 case FTRACE_UPDATE_MODIFY_CALL
:
2479 ftrace_bug_type
= FTRACE_BUG_UPDATE
;
2480 return ftrace_modify_call(rec
, ftrace_old_addr
, ftrace_addr
);
2483 return -1; /* unknow ftrace bug */
2486 void __weak
ftrace_replace_code(int enable
)
2488 struct dyn_ftrace
*rec
;
2489 struct ftrace_page
*pg
;
2492 if (unlikely(ftrace_disabled
))
2495 do_for_each_ftrace_rec(pg
, rec
) {
2497 if (rec
->flags
& FTRACE_FL_DISABLED
)
2500 failed
= __ftrace_replace_code(rec
, enable
);
2502 ftrace_bug(failed
, rec
);
2503 /* Stop processing */
2506 } while_for_each_ftrace_rec();
2509 struct ftrace_rec_iter
{
2510 struct ftrace_page
*pg
;
2515 * ftrace_rec_iter_start, start up iterating over traced functions
2517 * Returns an iterator handle that is used to iterate over all
2518 * the records that represent address locations where functions
2521 * May return NULL if no records are available.
2523 struct ftrace_rec_iter
*ftrace_rec_iter_start(void)
2526 * We only use a single iterator.
2527 * Protected by the ftrace_lock mutex.
2529 static struct ftrace_rec_iter ftrace_rec_iter
;
2530 struct ftrace_rec_iter
*iter
= &ftrace_rec_iter
;
2532 iter
->pg
= ftrace_pages_start
;
2535 /* Could have empty pages */
2536 while (iter
->pg
&& !iter
->pg
->index
)
2537 iter
->pg
= iter
->pg
->next
;
2546 * ftrace_rec_iter_next, get the next record to process.
2547 * @iter: The handle to the iterator.
2549 * Returns the next iterator after the given iterator @iter.
2551 struct ftrace_rec_iter
*ftrace_rec_iter_next(struct ftrace_rec_iter
*iter
)
2555 if (iter
->index
>= iter
->pg
->index
) {
2556 iter
->pg
= iter
->pg
->next
;
2559 /* Could have empty pages */
2560 while (iter
->pg
&& !iter
->pg
->index
)
2561 iter
->pg
= iter
->pg
->next
;
2571 * ftrace_rec_iter_record, get the record at the iterator location
2572 * @iter: The current iterator location
2574 * Returns the record that the current @iter is at.
2576 struct dyn_ftrace
*ftrace_rec_iter_record(struct ftrace_rec_iter
*iter
)
2578 return &iter
->pg
->records
[iter
->index
];
2582 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
2586 if (unlikely(ftrace_disabled
))
2589 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
2591 ftrace_bug_type
= FTRACE_BUG_INIT
;
2592 ftrace_bug(ret
, rec
);
2599 * archs can override this function if they must do something
2600 * before the modifying code is performed.
2602 int __weak
ftrace_arch_code_modify_prepare(void)
2608 * archs can override this function if they must do something
2609 * after the modifying code is performed.
2611 int __weak
ftrace_arch_code_modify_post_process(void)
2616 void ftrace_modify_all_code(int command
)
2618 int update
= command
& FTRACE_UPDATE_TRACE_FUNC
;
2622 * If the ftrace_caller calls a ftrace_ops func directly,
2623 * we need to make sure that it only traces functions it
2624 * expects to trace. When doing the switch of functions,
2625 * we need to update to the ftrace_ops_list_func first
2626 * before the transition between old and new calls are set,
2627 * as the ftrace_ops_list_func will check the ops hashes
2628 * to make sure the ops are having the right functions
2632 err
= ftrace_update_ftrace_func(ftrace_ops_list_func
);
2633 if (FTRACE_WARN_ON(err
))
2637 if (command
& FTRACE_UPDATE_CALLS
)
2638 ftrace_replace_code(1);
2639 else if (command
& FTRACE_DISABLE_CALLS
)
2640 ftrace_replace_code(0);
2642 if (update
&& ftrace_trace_function
!= ftrace_ops_list_func
) {
2643 function_trace_op
= set_function_trace_op
;
2645 /* If irqs are disabled, we are in stop machine */
2646 if (!irqs_disabled())
2647 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
2648 err
= ftrace_update_ftrace_func(ftrace_trace_function
);
2649 if (FTRACE_WARN_ON(err
))
2653 if (command
& FTRACE_START_FUNC_RET
)
2654 err
= ftrace_enable_ftrace_graph_caller();
2655 else if (command
& FTRACE_STOP_FUNC_RET
)
2656 err
= ftrace_disable_ftrace_graph_caller();
2657 FTRACE_WARN_ON(err
);
2660 static int __ftrace_modify_code(void *data
)
2662 int *command
= data
;
2664 ftrace_modify_all_code(*command
);
2670 * ftrace_run_stop_machine, go back to the stop machine method
2671 * @command: The command to tell ftrace what to do
2673 * If an arch needs to fall back to the stop machine method, the
2674 * it can call this function.
2676 void ftrace_run_stop_machine(int command
)
2678 stop_machine(__ftrace_modify_code
, &command
, NULL
);
2682 * arch_ftrace_update_code, modify the code to trace or not trace
2683 * @command: The command that needs to be done
2685 * Archs can override this function if it does not need to
2686 * run stop_machine() to modify code.
2688 void __weak
arch_ftrace_update_code(int command
)
2690 ftrace_run_stop_machine(command
);
2693 static void ftrace_run_update_code(int command
)
2697 ret
= ftrace_arch_code_modify_prepare();
2698 FTRACE_WARN_ON(ret
);
2703 * By default we use stop_machine() to modify the code.
2704 * But archs can do what ever they want as long as it
2705 * is safe. The stop_machine() is the safest, but also
2706 * produces the most overhead.
2708 arch_ftrace_update_code(command
);
2710 ret
= ftrace_arch_code_modify_post_process();
2711 FTRACE_WARN_ON(ret
);
2714 static void ftrace_run_modify_code(struct ftrace_ops
*ops
, int command
,
2715 struct ftrace_ops_hash
*old_hash
)
2717 ops
->flags
|= FTRACE_OPS_FL_MODIFYING
;
2718 ops
->old_hash
.filter_hash
= old_hash
->filter_hash
;
2719 ops
->old_hash
.notrace_hash
= old_hash
->notrace_hash
;
2720 ftrace_run_update_code(command
);
2721 ops
->old_hash
.filter_hash
= NULL
;
2722 ops
->old_hash
.notrace_hash
= NULL
;
2723 ops
->flags
&= ~FTRACE_OPS_FL_MODIFYING
;
2726 static ftrace_func_t saved_ftrace_func
;
2727 static int ftrace_start_up
;
2729 void __weak
arch_ftrace_trampoline_free(struct ftrace_ops
*ops
)
2733 static void per_cpu_ops_free(struct ftrace_ops
*ops
)
2735 free_percpu(ops
->disabled
);
2738 static void ftrace_startup_enable(int command
)
2740 if (saved_ftrace_func
!= ftrace_trace_function
) {
2741 saved_ftrace_func
= ftrace_trace_function
;
2742 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2745 if (!command
|| !ftrace_enabled
)
2748 ftrace_run_update_code(command
);
2751 static void ftrace_startup_all(int command
)
2753 update_all_ops
= true;
2754 ftrace_startup_enable(command
);
2755 update_all_ops
= false;
2758 static int ftrace_startup(struct ftrace_ops
*ops
, int command
)
2762 if (unlikely(ftrace_disabled
))
2765 ret
= __register_ftrace_function(ops
);
2772 * Note that ftrace probes uses this to start up
2773 * and modify functions it will probe. But we still
2774 * set the ADDING flag for modification, as probes
2775 * do not have trampolines. If they add them in the
2776 * future, then the probes will need to distinguish
2777 * between adding and updating probes.
2779 ops
->flags
|= FTRACE_OPS_FL_ENABLED
| FTRACE_OPS_FL_ADDING
;
2781 ret
= ftrace_hash_ipmodify_enable(ops
);
2783 /* Rollback registration process */
2784 __unregister_ftrace_function(ops
);
2786 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
2790 if (ftrace_hash_rec_enable(ops
, 1))
2791 command
|= FTRACE_UPDATE_CALLS
;
2793 ftrace_startup_enable(command
);
2795 ops
->flags
&= ~FTRACE_OPS_FL_ADDING
;
2800 static int ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
2804 if (unlikely(ftrace_disabled
))
2807 ret
= __unregister_ftrace_function(ops
);
2813 * Just warn in case of unbalance, no need to kill ftrace, it's not
2814 * critical but the ftrace_call callers may be never nopped again after
2815 * further ftrace uses.
2817 WARN_ON_ONCE(ftrace_start_up
< 0);
2819 /* Disabling ipmodify never fails */
2820 ftrace_hash_ipmodify_disable(ops
);
2822 if (ftrace_hash_rec_disable(ops
, 1))
2823 command
|= FTRACE_UPDATE_CALLS
;
2825 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
2827 if (saved_ftrace_func
!= ftrace_trace_function
) {
2828 saved_ftrace_func
= ftrace_trace_function
;
2829 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2832 if (!command
|| !ftrace_enabled
) {
2834 * If these are dynamic or per_cpu ops, they still
2835 * need their data freed. Since, function tracing is
2836 * not currently active, we can just free them
2837 * without synchronizing all CPUs.
2839 if (ops
->flags
& (FTRACE_OPS_FL_DYNAMIC
| FTRACE_OPS_FL_PER_CPU
))
2846 * If the ops uses a trampoline, then it needs to be
2847 * tested first on update.
2849 ops
->flags
|= FTRACE_OPS_FL_REMOVING
;
2852 /* The trampoline logic checks the old hashes */
2853 ops
->old_hash
.filter_hash
= ops
->func_hash
->filter_hash
;
2854 ops
->old_hash
.notrace_hash
= ops
->func_hash
->notrace_hash
;
2856 ftrace_run_update_code(command
);
2859 * If there's no more ops registered with ftrace, run a
2860 * sanity check to make sure all rec flags are cleared.
2862 if (rcu_dereference_protected(ftrace_ops_list
,
2863 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
2864 struct ftrace_page
*pg
;
2865 struct dyn_ftrace
*rec
;
2867 do_for_each_ftrace_rec(pg
, rec
) {
2868 if (FTRACE_WARN_ON_ONCE(rec
->flags
& ~FTRACE_FL_DISABLED
))
2869 pr_warn(" %pS flags:%lx\n",
2870 (void *)rec
->ip
, rec
->flags
);
2871 } while_for_each_ftrace_rec();
2874 ops
->old_hash
.filter_hash
= NULL
;
2875 ops
->old_hash
.notrace_hash
= NULL
;
2878 ops
->flags
&= ~FTRACE_OPS_FL_REMOVING
;
2881 * Dynamic ops may be freed, we must make sure that all
2882 * callers are done before leaving this function.
2883 * The same goes for freeing the per_cpu data of the per_cpu
2886 if (ops
->flags
& (FTRACE_OPS_FL_DYNAMIC
| FTRACE_OPS_FL_PER_CPU
)) {
2888 * We need to do a hard force of sched synchronization.
2889 * This is because we use preempt_disable() to do RCU, but
2890 * the function tracers can be called where RCU is not watching
2891 * (like before user_exit()). We can not rely on the RCU
2892 * infrastructure to do the synchronization, thus we must do it
2895 schedule_on_each_cpu(ftrace_sync
);
2898 * When the kernel is preeptive, tasks can be preempted
2899 * while on a ftrace trampoline. Just scheduling a task on
2900 * a CPU is not good enough to flush them. Calling
2901 * synchornize_rcu_tasks() will wait for those tasks to
2902 * execute and either schedule voluntarily or enter user space.
2904 if (IS_ENABLED(CONFIG_PREEMPT
))
2905 synchronize_rcu_tasks();
2908 arch_ftrace_trampoline_free(ops
);
2910 if (ops
->flags
& FTRACE_OPS_FL_PER_CPU
)
2911 per_cpu_ops_free(ops
);
2917 static void ftrace_startup_sysctl(void)
2921 if (unlikely(ftrace_disabled
))
2924 /* Force update next time */
2925 saved_ftrace_func
= NULL
;
2926 /* ftrace_start_up is true if we want ftrace running */
2927 if (ftrace_start_up
) {
2928 command
= FTRACE_UPDATE_CALLS
;
2929 if (ftrace_graph_active
)
2930 command
|= FTRACE_START_FUNC_RET
;
2931 ftrace_startup_enable(command
);
2935 static void ftrace_shutdown_sysctl(void)
2939 if (unlikely(ftrace_disabled
))
2942 /* ftrace_start_up is true if ftrace is running */
2943 if (ftrace_start_up
) {
2944 command
= FTRACE_DISABLE_CALLS
;
2945 if (ftrace_graph_active
)
2946 command
|= FTRACE_STOP_FUNC_RET
;
2947 ftrace_run_update_code(command
);
2951 static u64 ftrace_update_time
;
2952 unsigned long ftrace_update_tot_cnt
;
2954 static inline int ops_traces_mod(struct ftrace_ops
*ops
)
2957 * Filter_hash being empty will default to trace module.
2958 * But notrace hash requires a test of individual module functions.
2960 return ftrace_hash_empty(ops
->func_hash
->filter_hash
) &&
2961 ftrace_hash_empty(ops
->func_hash
->notrace_hash
);
2965 * Check if the current ops references the record.
2967 * If the ops traces all functions, then it was already accounted for.
2968 * If the ops does not trace the current record function, skip it.
2969 * If the ops ignores the function via notrace filter, skip it.
2972 ops_references_rec(struct ftrace_ops
*ops
, struct dyn_ftrace
*rec
)
2974 /* If ops isn't enabled, ignore it */
2975 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
2978 /* If ops traces all then it includes this function */
2979 if (ops_traces_mod(ops
))
2982 /* The function must be in the filter */
2983 if (!ftrace_hash_empty(ops
->func_hash
->filter_hash
) &&
2984 !__ftrace_lookup_ip(ops
->func_hash
->filter_hash
, rec
->ip
))
2987 /* If in notrace hash, we ignore it too */
2988 if (ftrace_lookup_ip(ops
->func_hash
->notrace_hash
, rec
->ip
))
2994 static int ftrace_update_code(struct module
*mod
, struct ftrace_page
*new_pgs
)
2996 struct ftrace_page
*pg
;
2997 struct dyn_ftrace
*p
;
2999 unsigned long update_cnt
= 0;
3000 unsigned long rec_flags
= 0;
3003 start
= ftrace_now(raw_smp_processor_id());
3006 * When a module is loaded, this function is called to convert
3007 * the calls to mcount in its text to nops, and also to create
3008 * an entry in the ftrace data. Now, if ftrace is activated
3009 * after this call, but before the module sets its text to
3010 * read-only, the modification of enabling ftrace can fail if
3011 * the read-only is done while ftrace is converting the calls.
3012 * To prevent this, the module's records are set as disabled
3013 * and will be enabled after the call to set the module's text
3017 rec_flags
|= FTRACE_FL_DISABLED
;
3019 for (pg
= new_pgs
; pg
; pg
= pg
->next
) {
3021 for (i
= 0; i
< pg
->index
; i
++) {
3023 /* If something went wrong, bail without enabling anything */
3024 if (unlikely(ftrace_disabled
))
3027 p
= &pg
->records
[i
];
3028 p
->flags
= rec_flags
;
3031 * Do the initial record conversion from mcount jump
3032 * to the NOP instructions.
3034 if (!ftrace_code_disable(mod
, p
))
3041 stop
= ftrace_now(raw_smp_processor_id());
3042 ftrace_update_time
= stop
- start
;
3043 ftrace_update_tot_cnt
+= update_cnt
;
3048 static int ftrace_allocate_records(struct ftrace_page
*pg
, int count
)
3053 if (WARN_ON(!count
))
3056 order
= get_count_order(DIV_ROUND_UP(count
, ENTRIES_PER_PAGE
));
3059 * We want to fill as much as possible. No more than a page
3062 while ((PAGE_SIZE
<< order
) / ENTRY_SIZE
>= count
+ ENTRIES_PER_PAGE
)
3066 pg
->records
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
3069 /* if we can't allocate this size, try something smaller */
3076 cnt
= (PAGE_SIZE
<< order
) / ENTRY_SIZE
;
3085 static struct ftrace_page
*
3086 ftrace_allocate_pages(unsigned long num_to_init
)
3088 struct ftrace_page
*start_pg
;
3089 struct ftrace_page
*pg
;
3096 start_pg
= pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
3101 * Try to allocate as much as possible in one continues
3102 * location that fills in all of the space. We want to
3103 * waste as little space as possible.
3106 cnt
= ftrace_allocate_records(pg
, num_to_init
);
3114 pg
->next
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
3126 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
3127 free_pages((unsigned long)pg
->records
, order
);
3128 start_pg
= pg
->next
;
3132 pr_info("ftrace: FAILED to allocate memory for functions\n");
3136 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3138 struct ftrace_iterator
{
3142 struct ftrace_page
*pg
;
3143 struct dyn_ftrace
*func
;
3144 struct ftrace_func_probe
*probe
;
3145 struct ftrace_func_entry
*probe_entry
;
3146 struct trace_parser parser
;
3147 struct ftrace_hash
*hash
;
3148 struct ftrace_ops
*ops
;
3149 struct trace_array
*tr
;
3150 struct list_head
*mod_list
;
3157 t_probe_next(struct seq_file
*m
, loff_t
*pos
)
3159 struct ftrace_iterator
*iter
= m
->private;
3160 struct trace_array
*tr
= iter
->ops
->private;
3161 struct list_head
*func_probes
;
3162 struct ftrace_hash
*hash
;
3163 struct list_head
*next
;
3164 struct hlist_node
*hnd
= NULL
;
3165 struct hlist_head
*hhd
;
3174 func_probes
= &tr
->func_probes
;
3175 if (list_empty(func_probes
))
3179 next
= func_probes
->next
;
3180 iter
->probe
= list_entry(next
, struct ftrace_func_probe
, list
);
3183 if (iter
->probe_entry
)
3184 hnd
= &iter
->probe_entry
->hlist
;
3186 hash
= iter
->probe
->ops
.func_hash
->filter_hash
;
3189 * A probe being registered may temporarily have an empty hash
3190 * and it's at the end of the func_probes list.
3192 if (!hash
|| hash
== EMPTY_HASH
)
3195 size
= 1 << hash
->size_bits
;
3198 if (iter
->pidx
>= size
) {
3199 if (iter
->probe
->list
.next
== func_probes
)
3201 next
= iter
->probe
->list
.next
;
3202 iter
->probe
= list_entry(next
, struct ftrace_func_probe
, list
);
3203 hash
= iter
->probe
->ops
.func_hash
->filter_hash
;
3204 size
= 1 << hash
->size_bits
;
3208 hhd
= &hash
->buckets
[iter
->pidx
];
3210 if (hlist_empty(hhd
)) {
3226 if (WARN_ON_ONCE(!hnd
))
3229 iter
->probe_entry
= hlist_entry(hnd
, struct ftrace_func_entry
, hlist
);
3234 static void *t_probe_start(struct seq_file
*m
, loff_t
*pos
)
3236 struct ftrace_iterator
*iter
= m
->private;
3240 if (!(iter
->flags
& FTRACE_ITER_DO_PROBES
))
3243 if (iter
->mod_pos
> *pos
)
3247 iter
->probe_entry
= NULL
;
3249 for (l
= 0; l
<= (*pos
- iter
->mod_pos
); ) {
3250 p
= t_probe_next(m
, &l
);
3257 /* Only set this if we have an item */
3258 iter
->flags
|= FTRACE_ITER_PROBE
;
3264 t_probe_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
3266 struct ftrace_func_entry
*probe_entry
;
3267 struct ftrace_probe_ops
*probe_ops
;
3268 struct ftrace_func_probe
*probe
;
3270 probe
= iter
->probe
;
3271 probe_entry
= iter
->probe_entry
;
3273 if (WARN_ON_ONCE(!probe
|| !probe_entry
))
3276 probe_ops
= probe
->probe_ops
;
3278 if (probe_ops
->print
)
3279 return probe_ops
->print(m
, probe_entry
->ip
, probe_ops
, probe
->data
);
3281 seq_printf(m
, "%ps:%ps\n", (void *)probe_entry
->ip
,
3282 (void *)probe_ops
->func
);
3288 t_mod_next(struct seq_file
*m
, loff_t
*pos
)
3290 struct ftrace_iterator
*iter
= m
->private;
3291 struct trace_array
*tr
= iter
->tr
;
3296 iter
->mod_list
= iter
->mod_list
->next
;
3298 if (iter
->mod_list
== &tr
->mod_trace
||
3299 iter
->mod_list
== &tr
->mod_notrace
) {
3300 iter
->flags
&= ~FTRACE_ITER_MOD
;
3304 iter
->mod_pos
= *pos
;
3309 static void *t_mod_start(struct seq_file
*m
, loff_t
*pos
)
3311 struct ftrace_iterator
*iter
= m
->private;
3315 if (iter
->func_pos
> *pos
)
3318 iter
->mod_pos
= iter
->func_pos
;
3320 /* probes are only available if tr is set */
3324 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
3325 p
= t_mod_next(m
, &l
);
3330 iter
->flags
&= ~FTRACE_ITER_MOD
;
3331 return t_probe_start(m
, pos
);
3334 /* Only set this if we have an item */
3335 iter
->flags
|= FTRACE_ITER_MOD
;
3341 t_mod_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
3343 struct ftrace_mod_load
*ftrace_mod
;
3344 struct trace_array
*tr
= iter
->tr
;
3346 if (WARN_ON_ONCE(!iter
->mod_list
) ||
3347 iter
->mod_list
== &tr
->mod_trace
||
3348 iter
->mod_list
== &tr
->mod_notrace
)
3351 ftrace_mod
= list_entry(iter
->mod_list
, struct ftrace_mod_load
, list
);
3353 if (ftrace_mod
->func
)
3354 seq_printf(m
, "%s", ftrace_mod
->func
);
3358 seq_printf(m
, ":mod:%s\n", ftrace_mod
->module
);
3364 t_func_next(struct seq_file
*m
, loff_t
*pos
)
3366 struct ftrace_iterator
*iter
= m
->private;
3367 struct dyn_ftrace
*rec
= NULL
;
3372 if (iter
->idx
>= iter
->pg
->index
) {
3373 if (iter
->pg
->next
) {
3374 iter
->pg
= iter
->pg
->next
;
3379 rec
= &iter
->pg
->records
[iter
->idx
++];
3380 if (((iter
->flags
& (FTRACE_ITER_FILTER
| FTRACE_ITER_NOTRACE
)) &&
3381 !ftrace_lookup_ip(iter
->hash
, rec
->ip
)) ||
3383 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
3384 !(rec
->flags
& FTRACE_FL_ENABLED
))) {
3394 iter
->pos
= iter
->func_pos
= *pos
;
3401 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3403 struct ftrace_iterator
*iter
= m
->private;
3404 loff_t l
= *pos
; /* t_probe_start() must use original pos */
3407 if (unlikely(ftrace_disabled
))
3410 if (iter
->flags
& FTRACE_ITER_PROBE
)
3411 return t_probe_next(m
, pos
);
3413 if (iter
->flags
& FTRACE_ITER_MOD
)
3414 return t_mod_next(m
, pos
);
3416 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
3417 /* next must increment pos, and t_probe_start does not */
3419 return t_mod_start(m
, &l
);
3422 ret
= t_func_next(m
, pos
);
3425 return t_mod_start(m
, &l
);
3430 static void reset_iter_read(struct ftrace_iterator
*iter
)
3434 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
| FTRACE_ITER_PROBE
| FTRACE_ITER_MOD
);
3437 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
3439 struct ftrace_iterator
*iter
= m
->private;
3443 mutex_lock(&ftrace_lock
);
3445 if (unlikely(ftrace_disabled
))
3449 * If an lseek was done, then reset and start from beginning.
3451 if (*pos
< iter
->pos
)
3452 reset_iter_read(iter
);
3455 * For set_ftrace_filter reading, if we have the filter
3456 * off, we can short cut and just print out that all
3457 * functions are enabled.
3459 if ((iter
->flags
& (FTRACE_ITER_FILTER
| FTRACE_ITER_NOTRACE
)) &&
3460 ftrace_hash_empty(iter
->hash
)) {
3461 iter
->func_pos
= 1; /* Account for the message */
3463 return t_mod_start(m
, pos
);
3464 iter
->flags
|= FTRACE_ITER_PRINTALL
;
3465 /* reset in case of seek/pread */
3466 iter
->flags
&= ~FTRACE_ITER_PROBE
;
3470 if (iter
->flags
& FTRACE_ITER_MOD
)
3471 return t_mod_start(m
, pos
);
3474 * Unfortunately, we need to restart at ftrace_pages_start
3475 * every time we let go of the ftrace_mutex. This is because
3476 * those pointers can change without the lock.
3478 iter
->pg
= ftrace_pages_start
;
3480 for (l
= 0; l
<= *pos
; ) {
3481 p
= t_func_next(m
, &l
);
3487 return t_mod_start(m
, pos
);
3492 static void t_stop(struct seq_file
*m
, void *p
)
3494 mutex_unlock(&ftrace_lock
);
3498 arch_ftrace_trampoline_func(struct ftrace_ops
*ops
, struct dyn_ftrace
*rec
)
3503 static void add_trampoline_func(struct seq_file
*m
, struct ftrace_ops
*ops
,
3504 struct dyn_ftrace
*rec
)
3508 ptr
= arch_ftrace_trampoline_func(ops
, rec
);
3510 seq_printf(m
, " ->%pS", ptr
);
3513 static int t_show(struct seq_file
*m
, void *v
)
3515 struct ftrace_iterator
*iter
= m
->private;
3516 struct dyn_ftrace
*rec
;
3518 if (iter
->flags
& FTRACE_ITER_PROBE
)
3519 return t_probe_show(m
, iter
);
3521 if (iter
->flags
& FTRACE_ITER_MOD
)
3522 return t_mod_show(m
, iter
);
3524 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
3525 if (iter
->flags
& FTRACE_ITER_NOTRACE
)
3526 seq_puts(m
, "#### no functions disabled ####\n");
3528 seq_puts(m
, "#### all functions enabled ####\n");
3537 seq_printf(m
, "%ps", (void *)rec
->ip
);
3538 if (iter
->flags
& FTRACE_ITER_ENABLED
) {
3539 struct ftrace_ops
*ops
;
3541 seq_printf(m
, " (%ld)%s%s",
3542 ftrace_rec_count(rec
),
3543 rec
->flags
& FTRACE_FL_REGS
? " R" : " ",
3544 rec
->flags
& FTRACE_FL_IPMODIFY
? " I" : " ");
3545 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
3546 ops
= ftrace_find_tramp_ops_any(rec
);
3549 seq_printf(m
, "\ttramp: %pS (%pS)",
3550 (void *)ops
->trampoline
,
3552 add_trampoline_func(m
, ops
, rec
);
3553 ops
= ftrace_find_tramp_ops_next(rec
, ops
);
3556 seq_puts(m
, "\ttramp: ERROR!");
3558 add_trampoline_func(m
, NULL
, rec
);
3567 static const struct seq_operations show_ftrace_seq_ops
= {
3575 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
3577 struct ftrace_iterator
*iter
;
3579 if (unlikely(ftrace_disabled
))
3582 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
3586 iter
->pg
= ftrace_pages_start
;
3587 iter
->ops
= &global_ops
;
3593 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
3595 struct ftrace_iterator
*iter
;
3597 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
3601 iter
->pg
= ftrace_pages_start
;
3602 iter
->flags
= FTRACE_ITER_ENABLED
;
3603 iter
->ops
= &global_ops
;
3609 * ftrace_regex_open - initialize function tracer filter files
3610 * @ops: The ftrace_ops that hold the hash filters
3611 * @flag: The type of filter to process
3612 * @inode: The inode, usually passed in to your open routine
3613 * @file: The file, usually passed in to your open routine
3615 * ftrace_regex_open() initializes the filter files for the
3616 * @ops. Depending on @flag it may process the filter hash or
3617 * the notrace hash of @ops. With this called from the open
3618 * routine, you can use ftrace_filter_write() for the write
3619 * routine if @flag has FTRACE_ITER_FILTER set, or
3620 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3621 * tracing_lseek() should be used as the lseek routine, and
3622 * release must call ftrace_regex_release().
3625 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
3626 struct inode
*inode
, struct file
*file
)
3628 struct ftrace_iterator
*iter
;
3629 struct ftrace_hash
*hash
;
3630 struct list_head
*mod_head
;
3631 struct trace_array
*tr
= ops
->private;
3634 ftrace_ops_init(ops
);
3636 if (unlikely(ftrace_disabled
))
3639 if (tr
&& trace_array_get(tr
) < 0)
3642 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
3646 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
))
3653 mutex_lock(&ops
->func_hash
->regex_lock
);
3655 if (flag
& FTRACE_ITER_NOTRACE
) {
3656 hash
= ops
->func_hash
->notrace_hash
;
3657 mod_head
= tr
? &tr
->mod_notrace
: NULL
;
3659 hash
= ops
->func_hash
->filter_hash
;
3660 mod_head
= tr
? &tr
->mod_trace
: NULL
;
3663 iter
->mod_list
= mod_head
;
3665 if (file
->f_mode
& FMODE_WRITE
) {
3666 const int size_bits
= FTRACE_HASH_DEFAULT_BITS
;
3668 if (file
->f_flags
& O_TRUNC
) {
3669 iter
->hash
= alloc_ftrace_hash(size_bits
);
3670 clear_ftrace_mod_list(mod_head
);
3672 iter
->hash
= alloc_and_copy_ftrace_hash(size_bits
, hash
);
3676 trace_parser_put(&iter
->parser
);
3684 if (file
->f_mode
& FMODE_READ
) {
3685 iter
->pg
= ftrace_pages_start
;
3687 ret
= seq_open(file
, &show_ftrace_seq_ops
);
3689 struct seq_file
*m
= file
->private_data
;
3693 free_ftrace_hash(iter
->hash
);
3694 trace_parser_put(&iter
->parser
);
3697 file
->private_data
= iter
;
3700 mutex_unlock(&ops
->func_hash
->regex_lock
);
3706 trace_array_put(tr
);
3713 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
3715 struct ftrace_ops
*ops
= inode
->i_private
;
3717 return ftrace_regex_open(ops
,
3718 FTRACE_ITER_FILTER
| FTRACE_ITER_DO_PROBES
,
3723 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
3725 struct ftrace_ops
*ops
= inode
->i_private
;
3727 return ftrace_regex_open(ops
, FTRACE_ITER_NOTRACE
,
3731 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3732 struct ftrace_glob
{
3739 * If symbols in an architecture don't correspond exactly to the user-visible
3740 * name of what they represent, it is possible to define this function to
3741 * perform the necessary adjustments.
3743 char * __weak
arch_ftrace_match_adjust(char *str
, const char *search
)
3748 static int ftrace_match(char *str
, struct ftrace_glob
*g
)
3753 str
= arch_ftrace_match_adjust(str
, g
->search
);
3757 if (strcmp(str
, g
->search
) == 0)
3760 case MATCH_FRONT_ONLY
:
3761 if (strncmp(str
, g
->search
, g
->len
) == 0)
3764 case MATCH_MIDDLE_ONLY
:
3765 if (strstr(str
, g
->search
))
3768 case MATCH_END_ONLY
:
3770 if (slen
>= g
->len
&&
3771 memcmp(str
+ slen
- g
->len
, g
->search
, g
->len
) == 0)
3775 if (glob_match(g
->search
, str
))
3784 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int clear_filter
)
3786 struct ftrace_func_entry
*entry
;
3789 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
3791 /* Do nothing if it doesn't exist */
3795 free_hash_entry(hash
, entry
);
3797 /* Do nothing if it exists */
3801 ret
= add_hash_entry(hash
, rec
->ip
);
3807 ftrace_match_record(struct dyn_ftrace
*rec
, struct ftrace_glob
*func_g
,
3808 struct ftrace_glob
*mod_g
, int exclude_mod
)
3810 char str
[KSYM_SYMBOL_LEN
];
3813 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
3816 int mod_matches
= (modname
) ? ftrace_match(modname
, mod_g
) : 0;
3818 /* blank module name to match all modules */
3820 /* blank module globbing: modname xor exclude_mod */
3821 if (!exclude_mod
!= !modname
)
3827 * exclude_mod is set to trace everything but the given
3828 * module. If it is set and the module matches, then
3829 * return 0. If it is not set, and the module doesn't match
3830 * also return 0. Otherwise, check the function to see if
3833 if (!mod_matches
== !exclude_mod
)
3836 /* blank search means to match all funcs in the mod */
3841 return ftrace_match(str
, func_g
);
3845 match_records(struct ftrace_hash
*hash
, char *func
, int len
, char *mod
)
3847 struct ftrace_page
*pg
;
3848 struct dyn_ftrace
*rec
;
3849 struct ftrace_glob func_g
= { .type
= MATCH_FULL
};
3850 struct ftrace_glob mod_g
= { .type
= MATCH_FULL
};
3851 struct ftrace_glob
*mod_match
= (mod
) ? &mod_g
: NULL
;
3852 int exclude_mod
= 0;
3855 int clear_filter
= 0;
3858 func_g
.type
= filter_parse_regex(func
, len
, &func_g
.search
,
3860 func_g
.len
= strlen(func_g
.search
);
3864 mod_g
.type
= filter_parse_regex(mod
, strlen(mod
),
3865 &mod_g
.search
, &exclude_mod
);
3866 mod_g
.len
= strlen(mod_g
.search
);
3869 mutex_lock(&ftrace_lock
);
3871 if (unlikely(ftrace_disabled
))
3874 do_for_each_ftrace_rec(pg
, rec
) {
3876 if (rec
->flags
& FTRACE_FL_DISABLED
)
3879 if (ftrace_match_record(rec
, &func_g
, mod_match
, exclude_mod
)) {
3880 ret
= enter_record(hash
, rec
, clear_filter
);
3887 } while_for_each_ftrace_rec();
3889 mutex_unlock(&ftrace_lock
);
3895 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
3897 return match_records(hash
, buff
, len
, NULL
);
3900 static void ftrace_ops_update_code(struct ftrace_ops
*ops
,
3901 struct ftrace_ops_hash
*old_hash
)
3903 struct ftrace_ops
*op
;
3905 if (!ftrace_enabled
)
3908 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
) {
3909 ftrace_run_modify_code(ops
, FTRACE_UPDATE_CALLS
, old_hash
);
3914 * If this is the shared global_ops filter, then we need to
3915 * check if there is another ops that shares it, is enabled.
3916 * If so, we still need to run the modify code.
3918 if (ops
->func_hash
!= &global_ops
.local_hash
)
3921 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
3922 if (op
->func_hash
== &global_ops
.local_hash
&&
3923 op
->flags
& FTRACE_OPS_FL_ENABLED
) {
3924 ftrace_run_modify_code(op
, FTRACE_UPDATE_CALLS
, old_hash
);
3925 /* Only need to do this once */
3928 } while_for_each_ftrace_op(op
);
3931 static int ftrace_hash_move_and_update_ops(struct ftrace_ops
*ops
,
3932 struct ftrace_hash
**orig_hash
,
3933 struct ftrace_hash
*hash
,
3936 struct ftrace_ops_hash old_hash_ops
;
3937 struct ftrace_hash
*old_hash
;
3940 old_hash
= *orig_hash
;
3941 old_hash_ops
.filter_hash
= ops
->func_hash
->filter_hash
;
3942 old_hash_ops
.notrace_hash
= ops
->func_hash
->notrace_hash
;
3943 ret
= ftrace_hash_move(ops
, enable
, orig_hash
, hash
);
3945 ftrace_ops_update_code(ops
, &old_hash_ops
);
3946 free_ftrace_hash_rcu(old_hash
);
3951 static bool module_exists(const char *module
)
3953 /* All modules have the symbol __this_module */
3954 const char this_mod
[] = "__this_module";
3955 const int modname_size
= MAX_PARAM_PREFIX_LEN
+ sizeof(this_mod
) + 1;
3956 char modname
[modname_size
+ 1];
3960 n
= snprintf(modname
, modname_size
+ 1, "%s:%s", module
, this_mod
);
3962 if (n
> modname_size
)
3965 val
= module_kallsyms_lookup_name(modname
);
3969 static int cache_mod(struct trace_array
*tr
,
3970 const char *func
, char *module
, int enable
)
3972 struct ftrace_mod_load
*ftrace_mod
, *n
;
3973 struct list_head
*head
= enable
? &tr
->mod_trace
: &tr
->mod_notrace
;
3976 mutex_lock(&ftrace_lock
);
3978 /* We do not cache inverse filters */
3979 if (func
[0] == '!') {
3983 /* Look to remove this hash */
3984 list_for_each_entry_safe(ftrace_mod
, n
, head
, list
) {
3985 if (strcmp(ftrace_mod
->module
, module
) != 0)
3988 /* no func matches all */
3989 if (strcmp(func
, "*") == 0 ||
3990 (ftrace_mod
->func
&&
3991 strcmp(ftrace_mod
->func
, func
) == 0)) {
3993 free_ftrace_mod(ftrace_mod
);
4001 /* We only care about modules that have not been loaded yet */
4002 if (module_exists(module
))
4005 /* Save this string off, and execute it when the module is loaded */
4006 ret
= ftrace_add_mod(tr
, func
, module
, enable
);
4008 mutex_unlock(&ftrace_lock
);
4014 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
4015 int reset
, int enable
);
4017 #ifdef CONFIG_MODULES
4018 static void process_mod_list(struct list_head
*head
, struct ftrace_ops
*ops
,
4019 char *mod
, bool enable
)
4021 struct ftrace_mod_load
*ftrace_mod
, *n
;
4022 struct ftrace_hash
**orig_hash
, *new_hash
;
4023 LIST_HEAD(process_mods
);
4027 mutex_lock(&ops
->func_hash
->regex_lock
);
4030 orig_hash
= &ops
->func_hash
->filter_hash
;
4032 orig_hash
= &ops
->func_hash
->notrace_hash
;
4034 new_hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
,
4037 goto out
; /* warn? */
4039 mutex_lock(&ftrace_lock
);
4041 list_for_each_entry_safe(ftrace_mod
, n
, head
, list
) {
4043 if (strcmp(ftrace_mod
->module
, mod
) != 0)
4046 if (ftrace_mod
->func
)
4047 func
= kstrdup(ftrace_mod
->func
, GFP_KERNEL
);
4049 func
= kstrdup("*", GFP_KERNEL
);
4051 if (!func
) /* warn? */
4054 list_del(&ftrace_mod
->list
);
4055 list_add(&ftrace_mod
->list
, &process_mods
);
4057 /* Use the newly allocated func, as it may be "*" */
4058 kfree(ftrace_mod
->func
);
4059 ftrace_mod
->func
= func
;
4062 mutex_unlock(&ftrace_lock
);
4064 list_for_each_entry_safe(ftrace_mod
, n
, &process_mods
, list
) {
4066 func
= ftrace_mod
->func
;
4068 /* Grabs ftrace_lock, which is why we have this extra step */
4069 match_records(new_hash
, func
, strlen(func
), mod
);
4070 free_ftrace_mod(ftrace_mod
);
4073 if (enable
&& list_empty(head
))
4074 new_hash
->flags
&= ~FTRACE_HASH_FL_MOD
;
4076 mutex_lock(&ftrace_lock
);
4078 ret
= ftrace_hash_move_and_update_ops(ops
, orig_hash
,
4080 mutex_unlock(&ftrace_lock
);
4083 mutex_unlock(&ops
->func_hash
->regex_lock
);
4085 free_ftrace_hash(new_hash
);
4088 static void process_cached_mods(const char *mod_name
)
4090 struct trace_array
*tr
;
4093 mod
= kstrdup(mod_name
, GFP_KERNEL
);
4097 mutex_lock(&trace_types_lock
);
4098 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
4099 if (!list_empty(&tr
->mod_trace
))
4100 process_mod_list(&tr
->mod_trace
, tr
->ops
, mod
, true);
4101 if (!list_empty(&tr
->mod_notrace
))
4102 process_mod_list(&tr
->mod_notrace
, tr
->ops
, mod
, false);
4104 mutex_unlock(&trace_types_lock
);
4111 * We register the module command as a template to show others how
4112 * to register the a command as well.
4116 ftrace_mod_callback(struct trace_array
*tr
, struct ftrace_hash
*hash
,
4117 char *func_orig
, char *cmd
, char *module
, int enable
)
4122 /* match_records() modifies func, and we need the original */
4123 func
= kstrdup(func_orig
, GFP_KERNEL
);
4128 * cmd == 'mod' because we only registered this func
4129 * for the 'mod' ftrace_func_command.
4130 * But if you register one func with multiple commands,
4131 * you can tell which command was used by the cmd
4134 ret
= match_records(hash
, func
, strlen(func
), module
);
4138 return cache_mod(tr
, func_orig
, module
, enable
);
4144 static struct ftrace_func_command ftrace_mod_cmd
= {
4146 .func
= ftrace_mod_callback
,
4149 static int __init
ftrace_mod_cmd_init(void)
4151 return register_ftrace_command(&ftrace_mod_cmd
);
4153 core_initcall(ftrace_mod_cmd_init
);
4155 static void function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
,
4156 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
4158 struct ftrace_probe_ops
*probe_ops
;
4159 struct ftrace_func_probe
*probe
;
4161 probe
= container_of(op
, struct ftrace_func_probe
, ops
);
4162 probe_ops
= probe
->probe_ops
;
4165 * Disable preemption for these calls to prevent a RCU grace
4166 * period. This syncs the hash iteration and freeing of items
4167 * on the hash. rcu_read_lock is too dangerous here.
4169 preempt_disable_notrace();
4170 probe_ops
->func(ip
, parent_ip
, probe
->tr
, probe_ops
, probe
->data
);
4171 preempt_enable_notrace();
4174 struct ftrace_func_map
{
4175 struct ftrace_func_entry entry
;
4179 struct ftrace_func_mapper
{
4180 struct ftrace_hash hash
;
4184 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4186 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4188 struct ftrace_func_mapper
*allocate_ftrace_func_mapper(void)
4190 struct ftrace_hash
*hash
;
4193 * The mapper is simply a ftrace_hash, but since the entries
4194 * in the hash are not ftrace_func_entry type, we define it
4195 * as a separate structure.
4197 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
4198 return (struct ftrace_func_mapper
*)hash
;
4202 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4203 * @mapper: The mapper that has the ip maps
4204 * @ip: the instruction pointer to find the data for
4206 * Returns the data mapped to @ip if found otherwise NULL. The return
4207 * is actually the address of the mapper data pointer. The address is
4208 * returned for use cases where the data is no bigger than a long, and
4209 * the user can use the data pointer as its data instead of having to
4210 * allocate more memory for the reference.
4212 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper
*mapper
,
4215 struct ftrace_func_entry
*entry
;
4216 struct ftrace_func_map
*map
;
4218 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
4222 map
= (struct ftrace_func_map
*)entry
;
4227 * ftrace_func_mapper_add_ip - Map some data to an ip
4228 * @mapper: The mapper that has the ip maps
4229 * @ip: The instruction pointer address to map @data to
4230 * @data: The data to map to @ip
4232 * Returns 0 on succes otherwise an error.
4234 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper
*mapper
,
4235 unsigned long ip
, void *data
)
4237 struct ftrace_func_entry
*entry
;
4238 struct ftrace_func_map
*map
;
4240 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
4244 map
= kmalloc(sizeof(*map
), GFP_KERNEL
);
4251 __add_hash_entry(&mapper
->hash
, &map
->entry
);
4257 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4258 * @mapper: The mapper that has the ip maps
4259 * @ip: The instruction pointer address to remove the data from
4261 * Returns the data if it is found, otherwise NULL.
4262 * Note, if the data pointer is used as the data itself, (see
4263 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4264 * if the data pointer was set to zero.
4266 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper
*mapper
,
4269 struct ftrace_func_entry
*entry
;
4270 struct ftrace_func_map
*map
;
4273 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
4277 map
= (struct ftrace_func_map
*)entry
;
4280 remove_hash_entry(&mapper
->hash
, entry
);
4287 * free_ftrace_func_mapper - free a mapping of ips and data
4288 * @mapper: The mapper that has the ip maps
4289 * @free_func: A function to be called on each data item.
4291 * This is used to free the function mapper. The @free_func is optional
4292 * and can be used if the data needs to be freed as well.
4294 void free_ftrace_func_mapper(struct ftrace_func_mapper
*mapper
,
4295 ftrace_mapper_func free_func
)
4297 struct ftrace_func_entry
*entry
;
4298 struct ftrace_func_map
*map
;
4299 struct hlist_head
*hhd
;
4305 if (free_func
&& mapper
->hash
.count
) {
4306 size
= 1 << mapper
->hash
.size_bits
;
4307 for (i
= 0; i
< size
; i
++) {
4308 hhd
= &mapper
->hash
.buckets
[i
];
4309 hlist_for_each_entry(entry
, hhd
, hlist
) {
4310 map
= (struct ftrace_func_map
*)entry
;
4315 free_ftrace_hash(&mapper
->hash
);
4318 static void release_probe(struct ftrace_func_probe
*probe
)
4320 struct ftrace_probe_ops
*probe_ops
;
4322 mutex_lock(&ftrace_lock
);
4324 WARN_ON(probe
->ref
<= 0);
4326 /* Subtract the ref that was used to protect this instance */
4330 probe_ops
= probe
->probe_ops
;
4332 * Sending zero as ip tells probe_ops to free
4333 * the probe->data itself
4335 if (probe_ops
->free
)
4336 probe_ops
->free(probe_ops
, probe
->tr
, 0, probe
->data
);
4337 list_del(&probe
->list
);
4340 mutex_unlock(&ftrace_lock
);
4343 static void acquire_probe_locked(struct ftrace_func_probe
*probe
)
4346 * Add one ref to keep it from being freed when releasing the
4347 * ftrace_lock mutex.
4353 register_ftrace_function_probe(char *glob
, struct trace_array
*tr
,
4354 struct ftrace_probe_ops
*probe_ops
,
4357 struct ftrace_func_entry
*entry
;
4358 struct ftrace_func_probe
*probe
;
4359 struct ftrace_hash
**orig_hash
;
4360 struct ftrace_hash
*old_hash
;
4361 struct ftrace_hash
*hash
;
4370 /* We do not support '!' for function probes */
4371 if (WARN_ON(glob
[0] == '!'))
4375 mutex_lock(&ftrace_lock
);
4376 /* Check if the probe_ops is already registered */
4377 list_for_each_entry(probe
, &tr
->func_probes
, list
) {
4378 if (probe
->probe_ops
== probe_ops
)
4381 if (&probe
->list
== &tr
->func_probes
) {
4382 probe
= kzalloc(sizeof(*probe
), GFP_KERNEL
);
4384 mutex_unlock(&ftrace_lock
);
4387 probe
->probe_ops
= probe_ops
;
4388 probe
->ops
.func
= function_trace_probe_call
;
4390 ftrace_ops_init(&probe
->ops
);
4391 list_add(&probe
->list
, &tr
->func_probes
);
4394 acquire_probe_locked(probe
);
4396 mutex_unlock(&ftrace_lock
);
4399 * Note, there's a small window here that the func_hash->filter_hash
4400 * may be NULL or empty. Need to be carefule when reading the loop.
4402 mutex_lock(&probe
->ops
.func_hash
->regex_lock
);
4404 orig_hash
= &probe
->ops
.func_hash
->filter_hash
;
4405 old_hash
= *orig_hash
;
4406 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, old_hash
);
4413 ret
= ftrace_match_records(hash
, glob
, strlen(glob
));
4415 /* Nothing found? */
4422 size
= 1 << hash
->size_bits
;
4423 for (i
= 0; i
< size
; i
++) {
4424 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
4425 if (ftrace_lookup_ip(old_hash
, entry
->ip
))
4428 * The caller might want to do something special
4429 * for each function we find. We call the callback
4430 * to give the caller an opportunity to do so.
4432 if (probe_ops
->init
) {
4433 ret
= probe_ops
->init(probe_ops
, tr
,
4437 if (probe_ops
->free
&& count
)
4438 probe_ops
->free(probe_ops
, tr
,
4448 mutex_lock(&ftrace_lock
);
4451 /* Nothing was added? */
4456 ret
= ftrace_hash_move_and_update_ops(&probe
->ops
, orig_hash
,
4461 /* One ref for each new function traced */
4462 probe
->ref
+= count
;
4464 if (!(probe
->ops
.flags
& FTRACE_OPS_FL_ENABLED
))
4465 ret
= ftrace_startup(&probe
->ops
, 0);
4468 mutex_unlock(&ftrace_lock
);
4473 mutex_unlock(&probe
->ops
.func_hash
->regex_lock
);
4474 free_ftrace_hash(hash
);
4476 release_probe(probe
);
4481 if (!probe_ops
->free
|| !count
)
4484 /* Failed to do the move, need to call the free functions */
4485 for (i
= 0; i
< size
; i
++) {
4486 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
4487 if (ftrace_lookup_ip(old_hash
, entry
->ip
))
4489 probe_ops
->free(probe_ops
, tr
, entry
->ip
, probe
->data
);
4496 unregister_ftrace_function_probe_func(char *glob
, struct trace_array
*tr
,
4497 struct ftrace_probe_ops
*probe_ops
)
4499 struct ftrace_ops_hash old_hash_ops
;
4500 struct ftrace_func_entry
*entry
;
4501 struct ftrace_func_probe
*probe
;
4502 struct ftrace_glob func_g
;
4503 struct ftrace_hash
**orig_hash
;
4504 struct ftrace_hash
*old_hash
;
4505 struct ftrace_hash
*hash
= NULL
;
4506 struct hlist_node
*tmp
;
4507 struct hlist_head hhd
;
4508 char str
[KSYM_SYMBOL_LEN
];
4510 int i
, ret
= -ENODEV
;
4513 if (!glob
|| !strlen(glob
) || !strcmp(glob
, "*"))
4514 func_g
.search
= NULL
;
4518 func_g
.type
= filter_parse_regex(glob
, strlen(glob
),
4519 &func_g
.search
, ¬);
4520 func_g
.len
= strlen(func_g
.search
);
4522 /* we do not support '!' for function probes */
4527 mutex_lock(&ftrace_lock
);
4528 /* Check if the probe_ops is already registered */
4529 list_for_each_entry(probe
, &tr
->func_probes
, list
) {
4530 if (probe
->probe_ops
== probe_ops
)
4533 if (&probe
->list
== &tr
->func_probes
)
4534 goto err_unlock_ftrace
;
4537 if (!(probe
->ops
.flags
& FTRACE_OPS_FL_INITIALIZED
))
4538 goto err_unlock_ftrace
;
4540 acquire_probe_locked(probe
);
4542 mutex_unlock(&ftrace_lock
);
4544 mutex_lock(&probe
->ops
.func_hash
->regex_lock
);
4546 orig_hash
= &probe
->ops
.func_hash
->filter_hash
;
4547 old_hash
= *orig_hash
;
4549 if (ftrace_hash_empty(old_hash
))
4552 old_hash_ops
.filter_hash
= old_hash
;
4553 /* Probes only have filters */
4554 old_hash_ops
.notrace_hash
= NULL
;
4557 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, old_hash
);
4561 INIT_HLIST_HEAD(&hhd
);
4563 size
= 1 << hash
->size_bits
;
4564 for (i
= 0; i
< size
; i
++) {
4565 hlist_for_each_entry_safe(entry
, tmp
, &hash
->buckets
[i
], hlist
) {
4567 if (func_g
.search
) {
4568 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
4570 if (!ftrace_match(str
, &func_g
))
4574 remove_hash_entry(hash
, entry
);
4575 hlist_add_head(&entry
->hlist
, &hhd
);
4579 /* Nothing found? */
4585 mutex_lock(&ftrace_lock
);
4587 WARN_ON(probe
->ref
< count
);
4589 probe
->ref
-= count
;
4591 if (ftrace_hash_empty(hash
))
4592 ftrace_shutdown(&probe
->ops
, 0);
4594 ret
= ftrace_hash_move_and_update_ops(&probe
->ops
, orig_hash
,
4597 /* still need to update the function call sites */
4598 if (ftrace_enabled
&& !ftrace_hash_empty(hash
))
4599 ftrace_run_modify_code(&probe
->ops
, FTRACE_UPDATE_CALLS
,
4601 synchronize_sched();
4603 hlist_for_each_entry_safe(entry
, tmp
, &hhd
, hlist
) {
4604 hlist_del(&entry
->hlist
);
4605 if (probe_ops
->free
)
4606 probe_ops
->free(probe_ops
, tr
, entry
->ip
, probe
->data
);
4609 mutex_unlock(&ftrace_lock
);
4612 mutex_unlock(&probe
->ops
.func_hash
->regex_lock
);
4613 free_ftrace_hash(hash
);
4615 release_probe(probe
);
4620 mutex_unlock(&ftrace_lock
);
4624 void clear_ftrace_function_probes(struct trace_array
*tr
)
4626 struct ftrace_func_probe
*probe
, *n
;
4628 list_for_each_entry_safe(probe
, n
, &tr
->func_probes
, list
)
4629 unregister_ftrace_function_probe_func(NULL
, tr
, probe
->probe_ops
);
4632 static LIST_HEAD(ftrace_commands
);
4633 static DEFINE_MUTEX(ftrace_cmd_mutex
);
4636 * Currently we only register ftrace commands from __init, so mark this
4639 __init
int register_ftrace_command(struct ftrace_func_command
*cmd
)
4641 struct ftrace_func_command
*p
;
4644 mutex_lock(&ftrace_cmd_mutex
);
4645 list_for_each_entry(p
, &ftrace_commands
, list
) {
4646 if (strcmp(cmd
->name
, p
->name
) == 0) {
4651 list_add(&cmd
->list
, &ftrace_commands
);
4653 mutex_unlock(&ftrace_cmd_mutex
);
4659 * Currently we only unregister ftrace commands from __init, so mark
4662 __init
int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
4664 struct ftrace_func_command
*p
, *n
;
4667 mutex_lock(&ftrace_cmd_mutex
);
4668 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
4669 if (strcmp(cmd
->name
, p
->name
) == 0) {
4671 list_del_init(&p
->list
);
4676 mutex_unlock(&ftrace_cmd_mutex
);
4681 static int ftrace_process_regex(struct ftrace_iterator
*iter
,
4682 char *buff
, int len
, int enable
)
4684 struct ftrace_hash
*hash
= iter
->hash
;
4685 struct trace_array
*tr
= iter
->ops
->private;
4686 char *func
, *command
, *next
= buff
;
4687 struct ftrace_func_command
*p
;
4690 func
= strsep(&next
, ":");
4693 ret
= ftrace_match_records(hash
, func
, len
);
4703 command
= strsep(&next
, ":");
4705 mutex_lock(&ftrace_cmd_mutex
);
4706 list_for_each_entry(p
, &ftrace_commands
, list
) {
4707 if (strcmp(p
->name
, command
) == 0) {
4708 ret
= p
->func(tr
, hash
, func
, command
, next
, enable
);
4713 mutex_unlock(&ftrace_cmd_mutex
);
4719 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
4720 size_t cnt
, loff_t
*ppos
, int enable
)
4722 struct ftrace_iterator
*iter
;
4723 struct trace_parser
*parser
;
4729 if (file
->f_mode
& FMODE_READ
) {
4730 struct seq_file
*m
= file
->private_data
;
4733 iter
= file
->private_data
;
4735 if (unlikely(ftrace_disabled
))
4738 /* iter->hash is a local copy, so we don't need regex_lock */
4740 parser
= &iter
->parser
;
4741 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
4743 if (read
>= 0 && trace_parser_loaded(parser
) &&
4744 !trace_parser_cont(parser
)) {
4745 ret
= ftrace_process_regex(iter
, parser
->buffer
,
4746 parser
->idx
, enable
);
4747 trace_parser_clear(parser
);
4758 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
4759 size_t cnt
, loff_t
*ppos
)
4761 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
4765 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
4766 size_t cnt
, loff_t
*ppos
)
4768 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
4772 ftrace_match_addr(struct ftrace_hash
*hash
, unsigned long ip
, int remove
)
4774 struct ftrace_func_entry
*entry
;
4776 if (!ftrace_location(ip
))
4780 entry
= ftrace_lookup_ip(hash
, ip
);
4783 free_hash_entry(hash
, entry
);
4787 return add_hash_entry(hash
, ip
);
4791 ftrace_set_hash(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
4792 unsigned long ip
, int remove
, int reset
, int enable
)
4794 struct ftrace_hash
**orig_hash
;
4795 struct ftrace_hash
*hash
;
4798 if (unlikely(ftrace_disabled
))
4801 mutex_lock(&ops
->func_hash
->regex_lock
);
4804 orig_hash
= &ops
->func_hash
->filter_hash
;
4806 orig_hash
= &ops
->func_hash
->notrace_hash
;
4809 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
4811 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
4815 goto out_regex_unlock
;
4818 if (buf
&& !ftrace_match_records(hash
, buf
, len
)) {
4820 goto out_regex_unlock
;
4823 ret
= ftrace_match_addr(hash
, ip
, remove
);
4825 goto out_regex_unlock
;
4828 mutex_lock(&ftrace_lock
);
4829 ret
= ftrace_hash_move_and_update_ops(ops
, orig_hash
, hash
, enable
);
4830 mutex_unlock(&ftrace_lock
);
4833 mutex_unlock(&ops
->func_hash
->regex_lock
);
4835 free_ftrace_hash(hash
);
4840 ftrace_set_addr(struct ftrace_ops
*ops
, unsigned long ip
, int remove
,
4841 int reset
, int enable
)
4843 return ftrace_set_hash(ops
, 0, 0, ip
, remove
, reset
, enable
);
4847 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4848 * @ops - the ops to set the filter with
4849 * @ip - the address to add to or remove from the filter.
4850 * @remove - non zero to remove the ip from the filter
4851 * @reset - non zero to reset all filters before applying this filter.
4853 * Filters denote which functions should be enabled when tracing is enabled
4854 * If @ip is NULL, it failes to update filter.
4856 int ftrace_set_filter_ip(struct ftrace_ops
*ops
, unsigned long ip
,
4857 int remove
, int reset
)
4859 ftrace_ops_init(ops
);
4860 return ftrace_set_addr(ops
, ip
, remove
, reset
, 1);
4862 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip
);
4865 * ftrace_ops_set_global_filter - setup ops to use global filters
4866 * @ops - the ops which will use the global filters
4868 * ftrace users who need global function trace filtering should call this.
4869 * It can set the global filter only if ops were not initialized before.
4871 void ftrace_ops_set_global_filter(struct ftrace_ops
*ops
)
4873 if (ops
->flags
& FTRACE_OPS_FL_INITIALIZED
)
4876 ftrace_ops_init(ops
);
4877 ops
->func_hash
= &global_ops
.local_hash
;
4879 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter
);
4882 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
4883 int reset
, int enable
)
4885 return ftrace_set_hash(ops
, buf
, len
, 0, 0, reset
, enable
);
4889 * ftrace_set_filter - set a function to filter on in ftrace
4890 * @ops - the ops to set the filter with
4891 * @buf - the string that holds the function filter text.
4892 * @len - the length of the string.
4893 * @reset - non zero to reset all filters before applying this filter.
4895 * Filters denote which functions should be enabled when tracing is enabled.
4896 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4898 int ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
4901 ftrace_ops_init(ops
);
4902 return ftrace_set_regex(ops
, buf
, len
, reset
, 1);
4904 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
4907 * ftrace_set_notrace - set a function to not trace in ftrace
4908 * @ops - the ops to set the notrace filter with
4909 * @buf - the string that holds the function notrace text.
4910 * @len - the length of the string.
4911 * @reset - non zero to reset all filters before applying this filter.
4913 * Notrace Filters denote which functions should not be enabled when tracing
4914 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4917 int ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
4920 ftrace_ops_init(ops
);
4921 return ftrace_set_regex(ops
, buf
, len
, reset
, 0);
4923 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
4925 * ftrace_set_global_filter - set a function to filter on with global tracers
4926 * @buf - the string that holds the function filter text.
4927 * @len - the length of the string.
4928 * @reset - non zero to reset all filters before applying this filter.
4930 * Filters denote which functions should be enabled when tracing is enabled.
4931 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4933 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
4935 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
4937 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
4940 * ftrace_set_global_notrace - set a function to not trace with global tracers
4941 * @buf - the string that holds the function notrace text.
4942 * @len - the length of the string.
4943 * @reset - non zero to reset all filters before applying this filter.
4945 * Notrace Filters denote which functions should not be enabled when tracing
4946 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4949 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
4951 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
4953 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
4956 * command line interface to allow users to set filters on boot up.
4958 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4959 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4960 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4962 /* Used by function selftest to not test if filter is set */
4963 bool ftrace_filter_param __initdata
;
4965 static int __init
set_ftrace_notrace(char *str
)
4967 ftrace_filter_param
= true;
4968 strlcpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
4971 __setup("ftrace_notrace=", set_ftrace_notrace
);
4973 static int __init
set_ftrace_filter(char *str
)
4975 ftrace_filter_param
= true;
4976 strlcpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
4979 __setup("ftrace_filter=", set_ftrace_filter
);
4981 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4982 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4983 static char ftrace_graph_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4984 static int ftrace_graph_set_hash(struct ftrace_hash
*hash
, char *buffer
);
4986 static int __init
set_graph_function(char *str
)
4988 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
4991 __setup("ftrace_graph_filter=", set_graph_function
);
4993 static int __init
set_graph_notrace_function(char *str
)
4995 strlcpy(ftrace_graph_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
4998 __setup("ftrace_graph_notrace=", set_graph_notrace_function
);
5000 static int __init
set_graph_max_depth_function(char *str
)
5004 fgraph_max_depth
= simple_strtoul(str
, NULL
, 0);
5007 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function
);
5009 static void __init
set_ftrace_early_graph(char *buf
, int enable
)
5013 struct ftrace_hash
*hash
;
5015 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
5020 func
= strsep(&buf
, ",");
5021 /* we allow only one expression at a time */
5022 ret
= ftrace_graph_set_hash(hash
, func
);
5024 printk(KERN_DEBUG
"ftrace: function %s not "
5025 "traceable\n", func
);
5029 ftrace_graph_hash
= hash
;
5031 ftrace_graph_notrace_hash
= hash
;
5033 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5036 ftrace_set_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
5040 ftrace_ops_init(ops
);
5043 func
= strsep(&buf
, ",");
5044 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
5048 static void __init
set_ftrace_early_filters(void)
5050 if (ftrace_filter_buf
[0])
5051 ftrace_set_early_filter(&global_ops
, ftrace_filter_buf
, 1);
5052 if (ftrace_notrace_buf
[0])
5053 ftrace_set_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
5054 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5055 if (ftrace_graph_buf
[0])
5056 set_ftrace_early_graph(ftrace_graph_buf
, 1);
5057 if (ftrace_graph_notrace_buf
[0])
5058 set_ftrace_early_graph(ftrace_graph_notrace_buf
, 0);
5059 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5062 int ftrace_regex_release(struct inode
*inode
, struct file
*file
)
5064 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
5065 struct ftrace_iterator
*iter
;
5066 struct ftrace_hash
**orig_hash
;
5067 struct trace_parser
*parser
;
5071 if (file
->f_mode
& FMODE_READ
) {
5073 seq_release(inode
, file
);
5075 iter
= file
->private_data
;
5077 parser
= &iter
->parser
;
5078 if (trace_parser_loaded(parser
)) {
5079 parser
->buffer
[parser
->idx
] = 0;
5080 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
5083 trace_parser_put(parser
);
5085 mutex_lock(&iter
->ops
->func_hash
->regex_lock
);
5087 if (file
->f_mode
& FMODE_WRITE
) {
5088 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
5091 orig_hash
= &iter
->ops
->func_hash
->filter_hash
;
5092 if (iter
->tr
&& !list_empty(&iter
->tr
->mod_trace
))
5093 iter
->hash
->flags
|= FTRACE_HASH_FL_MOD
;
5095 orig_hash
= &iter
->ops
->func_hash
->notrace_hash
;
5097 mutex_lock(&ftrace_lock
);
5098 ret
= ftrace_hash_move_and_update_ops(iter
->ops
, orig_hash
,
5099 iter
->hash
, filter_hash
);
5100 mutex_unlock(&ftrace_lock
);
5102 /* For read only, the hash is the ops hash */
5106 mutex_unlock(&iter
->ops
->func_hash
->regex_lock
);
5107 free_ftrace_hash(iter
->hash
);
5109 trace_array_put(iter
->tr
);
5115 static const struct file_operations ftrace_avail_fops
= {
5116 .open
= ftrace_avail_open
,
5118 .llseek
= seq_lseek
,
5119 .release
= seq_release_private
,
5122 static const struct file_operations ftrace_enabled_fops
= {
5123 .open
= ftrace_enabled_open
,
5125 .llseek
= seq_lseek
,
5126 .release
= seq_release_private
,
5129 static const struct file_operations ftrace_filter_fops
= {
5130 .open
= ftrace_filter_open
,
5132 .write
= ftrace_filter_write
,
5133 .llseek
= tracing_lseek
,
5134 .release
= ftrace_regex_release
,
5137 static const struct file_operations ftrace_notrace_fops
= {
5138 .open
= ftrace_notrace_open
,
5140 .write
= ftrace_notrace_write
,
5141 .llseek
= tracing_lseek
,
5142 .release
= ftrace_regex_release
,
5145 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5147 static DEFINE_MUTEX(graph_lock
);
5149 struct ftrace_hash __rcu
*ftrace_graph_hash
= EMPTY_HASH
;
5150 struct ftrace_hash __rcu
*ftrace_graph_notrace_hash
= EMPTY_HASH
;
5152 enum graph_filter_type
{
5153 GRAPH_FILTER_NOTRACE
= 0,
5154 GRAPH_FILTER_FUNCTION
,
5157 #define FTRACE_GRAPH_EMPTY ((void *)1)
5159 struct ftrace_graph_data
{
5160 struct ftrace_hash
*hash
;
5161 struct ftrace_func_entry
*entry
;
5162 int idx
; /* for hash table iteration */
5163 enum graph_filter_type type
;
5164 struct ftrace_hash
*new_hash
;
5165 const struct seq_operations
*seq_ops
;
5166 struct trace_parser parser
;
5170 __g_next(struct seq_file
*m
, loff_t
*pos
)
5172 struct ftrace_graph_data
*fgd
= m
->private;
5173 struct ftrace_func_entry
*entry
= fgd
->entry
;
5174 struct hlist_head
*head
;
5175 int i
, idx
= fgd
->idx
;
5177 if (*pos
>= fgd
->hash
->count
)
5181 hlist_for_each_entry_continue(entry
, hlist
) {
5189 for (i
= idx
; i
< 1 << fgd
->hash
->size_bits
; i
++) {
5190 head
= &fgd
->hash
->buckets
[i
];
5191 hlist_for_each_entry(entry
, head
, hlist
) {
5201 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
5204 return __g_next(m
, pos
);
5207 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
5209 struct ftrace_graph_data
*fgd
= m
->private;
5211 mutex_lock(&graph_lock
);
5213 if (fgd
->type
== GRAPH_FILTER_FUNCTION
)
5214 fgd
->hash
= rcu_dereference_protected(ftrace_graph_hash
,
5215 lockdep_is_held(&graph_lock
));
5217 fgd
->hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
5218 lockdep_is_held(&graph_lock
));
5220 /* Nothing, tell g_show to print all functions are enabled */
5221 if (ftrace_hash_empty(fgd
->hash
) && !*pos
)
5222 return FTRACE_GRAPH_EMPTY
;
5226 return __g_next(m
, pos
);
5229 static void g_stop(struct seq_file
*m
, void *p
)
5231 mutex_unlock(&graph_lock
);
5234 static int g_show(struct seq_file
*m
, void *v
)
5236 struct ftrace_func_entry
*entry
= v
;
5241 if (entry
== FTRACE_GRAPH_EMPTY
) {
5242 struct ftrace_graph_data
*fgd
= m
->private;
5244 if (fgd
->type
== GRAPH_FILTER_FUNCTION
)
5245 seq_puts(m
, "#### all functions enabled ####\n");
5247 seq_puts(m
, "#### no functions disabled ####\n");
5251 seq_printf(m
, "%ps\n", (void *)entry
->ip
);
5256 static const struct seq_operations ftrace_graph_seq_ops
= {
5264 __ftrace_graph_open(struct inode
*inode
, struct file
*file
,
5265 struct ftrace_graph_data
*fgd
)
5268 struct ftrace_hash
*new_hash
= NULL
;
5270 if (file
->f_mode
& FMODE_WRITE
) {
5271 const int size_bits
= FTRACE_HASH_DEFAULT_BITS
;
5273 if (trace_parser_get_init(&fgd
->parser
, FTRACE_BUFF_MAX
))
5276 if (file
->f_flags
& O_TRUNC
)
5277 new_hash
= alloc_ftrace_hash(size_bits
);
5279 new_hash
= alloc_and_copy_ftrace_hash(size_bits
,
5287 if (file
->f_mode
& FMODE_READ
) {
5288 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
5290 struct seq_file
*m
= file
->private_data
;
5294 free_ftrace_hash(new_hash
);
5298 file
->private_data
= fgd
;
5301 if (ret
< 0 && file
->f_mode
& FMODE_WRITE
)
5302 trace_parser_put(&fgd
->parser
);
5304 fgd
->new_hash
= new_hash
;
5307 * All uses of fgd->hash must be taken with the graph_lock
5308 * held. The graph_lock is going to be released, so force
5309 * fgd->hash to be reinitialized when it is taken again.
5317 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
5319 struct ftrace_graph_data
*fgd
;
5322 if (unlikely(ftrace_disabled
))
5325 fgd
= kmalloc(sizeof(*fgd
), GFP_KERNEL
);
5329 mutex_lock(&graph_lock
);
5331 fgd
->hash
= rcu_dereference_protected(ftrace_graph_hash
,
5332 lockdep_is_held(&graph_lock
));
5333 fgd
->type
= GRAPH_FILTER_FUNCTION
;
5334 fgd
->seq_ops
= &ftrace_graph_seq_ops
;
5336 ret
= __ftrace_graph_open(inode
, file
, fgd
);
5340 mutex_unlock(&graph_lock
);
5345 ftrace_graph_notrace_open(struct inode
*inode
, struct file
*file
)
5347 struct ftrace_graph_data
*fgd
;
5350 if (unlikely(ftrace_disabled
))
5353 fgd
= kmalloc(sizeof(*fgd
), GFP_KERNEL
);
5357 mutex_lock(&graph_lock
);
5359 fgd
->hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
5360 lockdep_is_held(&graph_lock
));
5361 fgd
->type
= GRAPH_FILTER_NOTRACE
;
5362 fgd
->seq_ops
= &ftrace_graph_seq_ops
;
5364 ret
= __ftrace_graph_open(inode
, file
, fgd
);
5368 mutex_unlock(&graph_lock
);
5373 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
5375 struct ftrace_graph_data
*fgd
;
5376 struct ftrace_hash
*old_hash
, *new_hash
;
5377 struct trace_parser
*parser
;
5380 if (file
->f_mode
& FMODE_READ
) {
5381 struct seq_file
*m
= file
->private_data
;
5384 seq_release(inode
, file
);
5386 fgd
= file
->private_data
;
5390 if (file
->f_mode
& FMODE_WRITE
) {
5392 parser
= &fgd
->parser
;
5394 if (trace_parser_loaded((parser
))) {
5395 parser
->buffer
[parser
->idx
] = 0;
5396 ret
= ftrace_graph_set_hash(fgd
->new_hash
,
5400 trace_parser_put(parser
);
5402 new_hash
= __ftrace_hash_move(fgd
->new_hash
);
5408 mutex_lock(&graph_lock
);
5410 if (fgd
->type
== GRAPH_FILTER_FUNCTION
) {
5411 old_hash
= rcu_dereference_protected(ftrace_graph_hash
,
5412 lockdep_is_held(&graph_lock
));
5413 rcu_assign_pointer(ftrace_graph_hash
, new_hash
);
5415 old_hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
5416 lockdep_is_held(&graph_lock
));
5417 rcu_assign_pointer(ftrace_graph_notrace_hash
, new_hash
);
5420 mutex_unlock(&graph_lock
);
5423 * We need to do a hard force of sched synchronization.
5424 * This is because we use preempt_disable() to do RCU, but
5425 * the function tracers can be called where RCU is not watching
5426 * (like before user_exit()). We can not rely on the RCU
5427 * infrastructure to do the synchronization, thus we must do it
5430 schedule_on_each_cpu(ftrace_sync
);
5432 free_ftrace_hash(old_hash
);
5436 free_ftrace_hash(fgd
->new_hash
);
5443 ftrace_graph_set_hash(struct ftrace_hash
*hash
, char *buffer
)
5445 struct ftrace_glob func_g
;
5446 struct dyn_ftrace
*rec
;
5447 struct ftrace_page
*pg
;
5448 struct ftrace_func_entry
*entry
;
5453 func_g
.type
= filter_parse_regex(buffer
, strlen(buffer
),
5454 &func_g
.search
, ¬);
5456 func_g
.len
= strlen(func_g
.search
);
5458 mutex_lock(&ftrace_lock
);
5460 if (unlikely(ftrace_disabled
)) {
5461 mutex_unlock(&ftrace_lock
);
5465 do_for_each_ftrace_rec(pg
, rec
) {
5467 if (rec
->flags
& FTRACE_FL_DISABLED
)
5470 if (ftrace_match_record(rec
, &func_g
, NULL
, 0)) {
5471 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
5478 if (add_hash_entry(hash
, rec
->ip
) < 0)
5482 free_hash_entry(hash
, entry
);
5487 } while_for_each_ftrace_rec();
5489 mutex_unlock(&ftrace_lock
);
5498 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
5499 size_t cnt
, loff_t
*ppos
)
5501 ssize_t read
, ret
= 0;
5502 struct ftrace_graph_data
*fgd
= file
->private_data
;
5503 struct trace_parser
*parser
;
5508 /* Read mode uses seq functions */
5509 if (file
->f_mode
& FMODE_READ
) {
5510 struct seq_file
*m
= file
->private_data
;
5514 parser
= &fgd
->parser
;
5516 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
5518 if (read
>= 0 && trace_parser_loaded(parser
) &&
5519 !trace_parser_cont(parser
)) {
5521 ret
= ftrace_graph_set_hash(fgd
->new_hash
,
5523 trace_parser_clear(parser
);
5532 static const struct file_operations ftrace_graph_fops
= {
5533 .open
= ftrace_graph_open
,
5535 .write
= ftrace_graph_write
,
5536 .llseek
= tracing_lseek
,
5537 .release
= ftrace_graph_release
,
5540 static const struct file_operations ftrace_graph_notrace_fops
= {
5541 .open
= ftrace_graph_notrace_open
,
5543 .write
= ftrace_graph_write
,
5544 .llseek
= tracing_lseek
,
5545 .release
= ftrace_graph_release
,
5547 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5549 void ftrace_create_filter_files(struct ftrace_ops
*ops
,
5550 struct dentry
*parent
)
5553 trace_create_file("set_ftrace_filter", 0644, parent
,
5554 ops
, &ftrace_filter_fops
);
5556 trace_create_file("set_ftrace_notrace", 0644, parent
,
5557 ops
, &ftrace_notrace_fops
);
5561 * The name "destroy_filter_files" is really a misnomer. Although
5562 * in the future, it may actualy delete the files, but this is
5563 * really intended to make sure the ops passed in are disabled
5564 * and that when this function returns, the caller is free to
5567 * The "destroy" name is only to match the "create" name that this
5568 * should be paired with.
5570 void ftrace_destroy_filter_files(struct ftrace_ops
*ops
)
5572 mutex_lock(&ftrace_lock
);
5573 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
)
5574 ftrace_shutdown(ops
, 0);
5575 ops
->flags
|= FTRACE_OPS_FL_DELETED
;
5576 ftrace_free_filter(ops
);
5577 mutex_unlock(&ftrace_lock
);
5580 static __init
int ftrace_init_dyn_tracefs(struct dentry
*d_tracer
)
5583 trace_create_file("available_filter_functions", 0444,
5584 d_tracer
, NULL
, &ftrace_avail_fops
);
5586 trace_create_file("enabled_functions", 0444,
5587 d_tracer
, NULL
, &ftrace_enabled_fops
);
5589 ftrace_create_filter_files(&global_ops
, d_tracer
);
5591 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5592 trace_create_file("set_graph_function", 0444, d_tracer
,
5594 &ftrace_graph_fops
);
5595 trace_create_file("set_graph_notrace", 0444, d_tracer
,
5597 &ftrace_graph_notrace_fops
);
5598 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5603 static int ftrace_cmp_ips(const void *a
, const void *b
)
5605 const unsigned long *ipa
= a
;
5606 const unsigned long *ipb
= b
;
5615 static int ftrace_process_locs(struct module
*mod
,
5616 unsigned long *start
,
5619 struct ftrace_page
*start_pg
;
5620 struct ftrace_page
*pg
;
5621 struct dyn_ftrace
*rec
;
5622 unsigned long count
;
5625 unsigned long flags
= 0; /* Shut up gcc */
5628 count
= end
- start
;
5633 sort(start
, count
, sizeof(*start
),
5634 ftrace_cmp_ips
, NULL
);
5636 start_pg
= ftrace_allocate_pages(count
);
5640 mutex_lock(&ftrace_lock
);
5643 * Core and each module needs their own pages, as
5644 * modules will free them when they are removed.
5645 * Force a new page to be allocated for modules.
5648 WARN_ON(ftrace_pages
|| ftrace_pages_start
);
5649 /* First initialization */
5650 ftrace_pages
= ftrace_pages_start
= start_pg
;
5655 if (WARN_ON(ftrace_pages
->next
)) {
5656 /* Hmm, we have free pages? */
5657 while (ftrace_pages
->next
)
5658 ftrace_pages
= ftrace_pages
->next
;
5661 ftrace_pages
->next
= start_pg
;
5667 addr
= ftrace_call_adjust(*p
++);
5669 * Some architecture linkers will pad between
5670 * the different mcount_loc sections of different
5671 * object files to satisfy alignments.
5672 * Skip any NULL pointers.
5677 if (pg
->index
== pg
->size
) {
5678 /* We should have allocated enough */
5679 if (WARN_ON(!pg
->next
))
5684 rec
= &pg
->records
[pg
->index
++];
5688 /* We should have used all pages */
5691 /* Assign the last page to ftrace_pages */
5695 * We only need to disable interrupts on start up
5696 * because we are modifying code that an interrupt
5697 * may execute, and the modification is not atomic.
5698 * But for modules, nothing runs the code we modify
5699 * until we are finished with it, and there's no
5700 * reason to cause large interrupt latencies while we do it.
5703 local_irq_save(flags
);
5704 ftrace_update_code(mod
, start_pg
);
5706 local_irq_restore(flags
);
5709 mutex_unlock(&ftrace_lock
);
5714 #ifdef CONFIG_MODULES
5716 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5718 static int referenced_filters(struct dyn_ftrace
*rec
)
5720 struct ftrace_ops
*ops
;
5723 for (ops
= ftrace_ops_list
; ops
!= &ftrace_list_end
; ops
= ops
->next
) {
5724 if (ops_references_rec(ops
, rec
)) {
5726 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
5727 rec
->flags
|= FTRACE_FL_REGS
;
5735 clear_mod_from_hash(struct ftrace_page
*pg
, struct ftrace_hash
*hash
)
5737 struct ftrace_func_entry
*entry
;
5738 struct dyn_ftrace
*rec
;
5741 if (ftrace_hash_empty(hash
))
5744 for (i
= 0; i
< pg
->index
; i
++) {
5745 rec
= &pg
->records
[i
];
5746 entry
= __ftrace_lookup_ip(hash
, rec
->ip
);
5748 * Do not allow this rec to match again.
5749 * Yeah, it may waste some memory, but will be removed
5750 * if/when the hash is modified again.
5757 /* Clear any records from hashs */
5758 static void clear_mod_from_hashes(struct ftrace_page
*pg
)
5760 struct trace_array
*tr
;
5762 mutex_lock(&trace_types_lock
);
5763 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
5764 if (!tr
->ops
|| !tr
->ops
->func_hash
)
5766 mutex_lock(&tr
->ops
->func_hash
->regex_lock
);
5767 clear_mod_from_hash(pg
, tr
->ops
->func_hash
->filter_hash
);
5768 clear_mod_from_hash(pg
, tr
->ops
->func_hash
->notrace_hash
);
5769 mutex_unlock(&tr
->ops
->func_hash
->regex_lock
);
5771 mutex_unlock(&trace_types_lock
);
5774 void ftrace_release_mod(struct module
*mod
)
5776 struct dyn_ftrace
*rec
;
5777 struct ftrace_page
**last_pg
;
5778 struct ftrace_page
*tmp_page
= NULL
;
5779 struct ftrace_page
*pg
;
5782 mutex_lock(&ftrace_lock
);
5784 if (ftrace_disabled
)
5788 * Each module has its own ftrace_pages, remove
5789 * them from the list.
5791 last_pg
= &ftrace_pages_start
;
5792 for (pg
= ftrace_pages_start
; pg
; pg
= *last_pg
) {
5793 rec
= &pg
->records
[0];
5794 if (within_module_core(rec
->ip
, mod
)) {
5796 * As core pages are first, the first
5797 * page should never be a module page.
5799 if (WARN_ON(pg
== ftrace_pages_start
))
5802 /* Check if we are deleting the last page */
5803 if (pg
== ftrace_pages
)
5804 ftrace_pages
= next_to_ftrace_page(last_pg
);
5806 ftrace_update_tot_cnt
-= pg
->index
;
5807 *last_pg
= pg
->next
;
5809 pg
->next
= tmp_page
;
5812 last_pg
= &pg
->next
;
5815 mutex_unlock(&ftrace_lock
);
5817 for (pg
= tmp_page
; pg
; pg
= tmp_page
) {
5819 /* Needs to be called outside of ftrace_lock */
5820 clear_mod_from_hashes(pg
);
5822 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
5823 free_pages((unsigned long)pg
->records
, order
);
5824 tmp_page
= pg
->next
;
5829 void ftrace_module_enable(struct module
*mod
)
5831 struct dyn_ftrace
*rec
;
5832 struct ftrace_page
*pg
;
5834 mutex_lock(&ftrace_lock
);
5836 if (ftrace_disabled
)
5840 * If the tracing is enabled, go ahead and enable the record.
5842 * The reason not to enable the record immediatelly is the
5843 * inherent check of ftrace_make_nop/ftrace_make_call for
5844 * correct previous instructions. Making first the NOP
5845 * conversion puts the module to the correct state, thus
5846 * passing the ftrace_make_call check.
5848 * We also delay this to after the module code already set the
5849 * text to read-only, as we now need to set it back to read-write
5850 * so that we can modify the text.
5852 if (ftrace_start_up
)
5853 ftrace_arch_code_modify_prepare();
5855 do_for_each_ftrace_rec(pg
, rec
) {
5858 * do_for_each_ftrace_rec() is a double loop.
5859 * module text shares the pg. If a record is
5860 * not part of this module, then skip this pg,
5861 * which the "break" will do.
5863 if (!within_module_core(rec
->ip
, mod
))
5869 * When adding a module, we need to check if tracers are
5870 * currently enabled and if they are, and can trace this record,
5871 * we need to enable the module functions as well as update the
5872 * reference counts for those function records.
5874 if (ftrace_start_up
)
5875 cnt
+= referenced_filters(rec
);
5877 rec
->flags
&= ~FTRACE_FL_DISABLED
;
5880 if (ftrace_start_up
&& cnt
) {
5881 int failed
= __ftrace_replace_code(rec
, 1);
5883 ftrace_bug(failed
, rec
);
5888 } while_for_each_ftrace_rec();
5891 if (ftrace_start_up
)
5892 ftrace_arch_code_modify_post_process();
5895 mutex_unlock(&ftrace_lock
);
5897 process_cached_mods(mod
->name
);
5900 void ftrace_module_init(struct module
*mod
)
5902 if (ftrace_disabled
|| !mod
->num_ftrace_callsites
)
5905 ftrace_process_locs(mod
, mod
->ftrace_callsites
,
5906 mod
->ftrace_callsites
+ mod
->num_ftrace_callsites
);
5908 #endif /* CONFIG_MODULES */
5910 void __init
ftrace_free_init_mem(void)
5912 unsigned long start
= (unsigned long)(&__init_begin
);
5913 unsigned long end
= (unsigned long)(&__init_end
);
5914 struct ftrace_page
**last_pg
= &ftrace_pages_start
;
5915 struct ftrace_page
*pg
;
5916 struct dyn_ftrace
*rec
;
5917 struct dyn_ftrace key
;
5921 key
.flags
= end
; /* overload flags, as it is unsigned long */
5923 mutex_lock(&ftrace_lock
);
5925 for (pg
= ftrace_pages_start
; pg
; last_pg
= &pg
->next
, pg
= *last_pg
) {
5926 if (end
< pg
->records
[0].ip
||
5927 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
5930 rec
= bsearch(&key
, pg
->records
, pg
->index
,
5931 sizeof(struct dyn_ftrace
),
5936 ftrace_update_tot_cnt
--;
5938 *last_pg
= pg
->next
;
5939 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
5940 free_pages((unsigned long)pg
->records
, order
);
5942 pg
= container_of(last_pg
, struct ftrace_page
, next
);
5947 memmove(rec
, rec
+ 1,
5948 (pg
->index
- (rec
- pg
->records
)) * sizeof(*rec
));
5949 /* More than one function may be in this block */
5952 mutex_unlock(&ftrace_lock
);
5955 void __init
ftrace_init(void)
5957 extern unsigned long __start_mcount_loc
[];
5958 extern unsigned long __stop_mcount_loc
[];
5959 unsigned long count
, flags
;
5962 local_irq_save(flags
);
5963 ret
= ftrace_dyn_arch_init();
5964 local_irq_restore(flags
);
5968 count
= __stop_mcount_loc
- __start_mcount_loc
;
5970 pr_info("ftrace: No functions to be traced?\n");
5974 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5975 count
, count
/ ENTRIES_PER_PAGE
+ 1);
5977 last_ftrace_enabled
= ftrace_enabled
= 1;
5979 ret
= ftrace_process_locs(NULL
,
5983 set_ftrace_early_filters();
5987 ftrace_disabled
= 1;
5990 /* Do nothing if arch does not support this */
5991 void __weak
arch_ftrace_update_trampoline(struct ftrace_ops
*ops
)
5995 static void ftrace_update_trampoline(struct ftrace_ops
*ops
)
5997 arch_ftrace_update_trampoline(ops
);
6000 void ftrace_init_trace_array(struct trace_array
*tr
)
6002 INIT_LIST_HEAD(&tr
->func_probes
);
6003 INIT_LIST_HEAD(&tr
->mod_trace
);
6004 INIT_LIST_HEAD(&tr
->mod_notrace
);
6008 static struct ftrace_ops global_ops
= {
6009 .func
= ftrace_stub
,
6010 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
|
6011 FTRACE_OPS_FL_INITIALIZED
|
6015 static int __init
ftrace_nodyn_init(void)
6020 core_initcall(ftrace_nodyn_init
);
6022 static inline int ftrace_init_dyn_tracefs(struct dentry
*d_tracer
) { return 0; }
6023 static inline void ftrace_startup_enable(int command
) { }
6024 static inline void ftrace_startup_all(int command
) { }
6025 /* Keep as macros so we do not need to define the commands */
6026 # define ftrace_startup(ops, command) \
6028 int ___ret = __register_ftrace_function(ops); \
6030 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
6033 # define ftrace_shutdown(ops, command) \
6035 int ___ret = __unregister_ftrace_function(ops); \
6037 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
6041 # define ftrace_startup_sysctl() do { } while (0)
6042 # define ftrace_shutdown_sysctl() do { } while (0)
6045 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
6050 static void ftrace_update_trampoline(struct ftrace_ops
*ops
)
6054 #endif /* CONFIG_DYNAMIC_FTRACE */
6056 __init
void ftrace_init_global_array_ops(struct trace_array
*tr
)
6058 tr
->ops
= &global_ops
;
6059 tr
->ops
->private = tr
;
6060 ftrace_init_trace_array(tr
);
6063 void ftrace_init_array_ops(struct trace_array
*tr
, ftrace_func_t func
)
6065 /* If we filter on pids, update to use the pid function */
6066 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
) {
6067 if (WARN_ON(tr
->ops
->func
!= ftrace_stub
))
6068 printk("ftrace ops had %pS for function\n",
6071 tr
->ops
->func
= func
;
6072 tr
->ops
->private = tr
;
6075 void ftrace_reset_array_ops(struct trace_array
*tr
)
6077 tr
->ops
->func
= ftrace_stub
;
6080 static nokprobe_inline
void
6081 __ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
6082 struct ftrace_ops
*ignored
, struct pt_regs
*regs
)
6084 struct ftrace_ops
*op
;
6087 bit
= trace_test_and_set_recursion(TRACE_LIST_START
, TRACE_LIST_MAX
);
6092 * Some of the ops may be dynamically allocated,
6093 * they must be freed after a synchronize_sched().
6095 preempt_disable_notrace();
6097 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
6099 * Check the following for each ops before calling their func:
6100 * if RCU flag is set, then rcu_is_watching() must be true
6101 * if PER_CPU is set, then ftrace_function_local_disable()
6103 * Otherwise test if the ip matches the ops filter
6105 * If any of the above fails then the op->func() is not executed.
6107 if ((!(op
->flags
& FTRACE_OPS_FL_RCU
) || rcu_is_watching()) &&
6108 (!(op
->flags
& FTRACE_OPS_FL_PER_CPU
) ||
6109 !ftrace_function_local_disabled(op
)) &&
6110 ftrace_ops_test(op
, ip
, regs
)) {
6112 if (FTRACE_WARN_ON(!op
->func
)) {
6113 pr_warn("op=%p %pS\n", op
, op
);
6116 op
->func(ip
, parent_ip
, op
, regs
);
6118 } while_for_each_ftrace_op(op
);
6120 preempt_enable_notrace();
6121 trace_clear_recursion(bit
);
6125 * Some archs only support passing ip and parent_ip. Even though
6126 * the list function ignores the op parameter, we do not want any
6127 * C side effects, where a function is called without the caller
6128 * sending a third parameter.
6129 * Archs are to support both the regs and ftrace_ops at the same time.
6130 * If they support ftrace_ops, it is assumed they support regs.
6131 * If call backs want to use regs, they must either check for regs
6132 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6133 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6134 * An architecture can pass partial regs with ftrace_ops and still
6135 * set the ARCH_SUPPORTS_FTRACE_OPS.
6137 #if ARCH_SUPPORTS_FTRACE_OPS
6138 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
6139 struct ftrace_ops
*op
, struct pt_regs
*regs
)
6141 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, regs
);
6143 NOKPROBE_SYMBOL(ftrace_ops_list_func
);
6145 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
)
6147 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, NULL
);
6149 NOKPROBE_SYMBOL(ftrace_ops_no_ops
);
6153 * If there's only one function registered but it does not support
6154 * recursion, needs RCU protection and/or requires per cpu handling, then
6155 * this function will be called by the mcount trampoline.
6157 static void ftrace_ops_assist_func(unsigned long ip
, unsigned long parent_ip
,
6158 struct ftrace_ops
*op
, struct pt_regs
*regs
)
6162 if ((op
->flags
& FTRACE_OPS_FL_RCU
) && !rcu_is_watching())
6165 bit
= trace_test_and_set_recursion(TRACE_LIST_START
, TRACE_LIST_MAX
);
6169 preempt_disable_notrace();
6171 if (!(op
->flags
& FTRACE_OPS_FL_PER_CPU
) ||
6172 !ftrace_function_local_disabled(op
)) {
6173 op
->func(ip
, parent_ip
, op
, regs
);
6176 preempt_enable_notrace();
6177 trace_clear_recursion(bit
);
6179 NOKPROBE_SYMBOL(ftrace_ops_assist_func
);
6182 * ftrace_ops_get_func - get the function a trampoline should call
6183 * @ops: the ops to get the function for
6185 * Normally the mcount trampoline will call the ops->func, but there
6186 * are times that it should not. For example, if the ops does not
6187 * have its own recursion protection, then it should call the
6188 * ftrace_ops_assist_func() instead.
6190 * Returns the function that the trampoline should call for @ops.
6192 ftrace_func_t
ftrace_ops_get_func(struct ftrace_ops
*ops
)
6195 * If the function does not handle recursion, needs to be RCU safe,
6196 * or does per cpu logic, then we need to call the assist handler.
6198 if (!(ops
->flags
& FTRACE_OPS_FL_RECURSION_SAFE
) ||
6199 ops
->flags
& (FTRACE_OPS_FL_RCU
| FTRACE_OPS_FL_PER_CPU
))
6200 return ftrace_ops_assist_func
;
6206 ftrace_filter_pid_sched_switch_probe(void *data
, bool preempt
,
6207 struct task_struct
*prev
, struct task_struct
*next
)
6209 struct trace_array
*tr
= data
;
6210 struct trace_pid_list
*pid_list
;
6212 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6214 this_cpu_write(tr
->trace_buffer
.data
->ftrace_ignore_pid
,
6215 trace_ignore_this_task(pid_list
, next
));
6219 ftrace_pid_follow_sched_process_fork(void *data
,
6220 struct task_struct
*self
,
6221 struct task_struct
*task
)
6223 struct trace_pid_list
*pid_list
;
6224 struct trace_array
*tr
= data
;
6226 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6227 trace_filter_add_remove_task(pid_list
, self
, task
);
6231 ftrace_pid_follow_sched_process_exit(void *data
, struct task_struct
*task
)
6233 struct trace_pid_list
*pid_list
;
6234 struct trace_array
*tr
= data
;
6236 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6237 trace_filter_add_remove_task(pid_list
, NULL
, task
);
6240 void ftrace_pid_follow_fork(struct trace_array
*tr
, bool enable
)
6243 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork
,
6245 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit
,
6248 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork
,
6250 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit
,
6255 static void clear_ftrace_pids(struct trace_array
*tr
)
6257 struct trace_pid_list
*pid_list
;
6260 pid_list
= rcu_dereference_protected(tr
->function_pids
,
6261 lockdep_is_held(&ftrace_lock
));
6265 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe
, tr
);
6267 for_each_possible_cpu(cpu
)
6268 per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->ftrace_ignore_pid
= false;
6270 rcu_assign_pointer(tr
->function_pids
, NULL
);
6272 /* Wait till all users are no longer using pid filtering */
6273 synchronize_sched();
6275 trace_free_pid_list(pid_list
);
6278 void ftrace_clear_pids(struct trace_array
*tr
)
6280 mutex_lock(&ftrace_lock
);
6282 clear_ftrace_pids(tr
);
6284 mutex_unlock(&ftrace_lock
);
6287 static void ftrace_pid_reset(struct trace_array
*tr
)
6289 mutex_lock(&ftrace_lock
);
6290 clear_ftrace_pids(tr
);
6292 ftrace_update_pid_func();
6293 ftrace_startup_all(0);
6295 mutex_unlock(&ftrace_lock
);
6298 /* Greater than any max PID */
6299 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6301 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
6304 struct trace_pid_list
*pid_list
;
6305 struct trace_array
*tr
= m
->private;
6307 mutex_lock(&ftrace_lock
);
6308 rcu_read_lock_sched();
6310 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6313 return !(*pos
) ? FTRACE_NO_PIDS
: NULL
;
6315 return trace_pid_start(pid_list
, pos
);
6318 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
6320 struct trace_array
*tr
= m
->private;
6321 struct trace_pid_list
*pid_list
= rcu_dereference_sched(tr
->function_pids
);
6323 if (v
== FTRACE_NO_PIDS
) {
6327 return trace_pid_next(pid_list
, v
, pos
);
6330 static void fpid_stop(struct seq_file
*m
, void *p
)
6333 rcu_read_unlock_sched();
6334 mutex_unlock(&ftrace_lock
);
6337 static int fpid_show(struct seq_file
*m
, void *v
)
6339 if (v
== FTRACE_NO_PIDS
) {
6340 seq_puts(m
, "no pid\n");
6344 return trace_pid_show(m
, v
);
6347 static const struct seq_operations ftrace_pid_sops
= {
6348 .start
= fpid_start
,
6355 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
6357 struct trace_array
*tr
= inode
->i_private
;
6361 if (trace_array_get(tr
) < 0)
6364 if ((file
->f_mode
& FMODE_WRITE
) &&
6365 (file
->f_flags
& O_TRUNC
))
6366 ftrace_pid_reset(tr
);
6368 ret
= seq_open(file
, &ftrace_pid_sops
);
6370 trace_array_put(tr
);
6372 m
= file
->private_data
;
6373 /* copy tr over to seq ops */
6380 static void ignore_task_cpu(void *data
)
6382 struct trace_array
*tr
= data
;
6383 struct trace_pid_list
*pid_list
;
6386 * This function is called by on_each_cpu() while the
6387 * event_mutex is held.
6389 pid_list
= rcu_dereference_protected(tr
->function_pids
,
6390 mutex_is_locked(&ftrace_lock
));
6392 this_cpu_write(tr
->trace_buffer
.data
->ftrace_ignore_pid
,
6393 trace_ignore_this_task(pid_list
, current
));
6397 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
6398 size_t cnt
, loff_t
*ppos
)
6400 struct seq_file
*m
= filp
->private_data
;
6401 struct trace_array
*tr
= m
->private;
6402 struct trace_pid_list
*filtered_pids
= NULL
;
6403 struct trace_pid_list
*pid_list
;
6409 mutex_lock(&ftrace_lock
);
6411 filtered_pids
= rcu_dereference_protected(tr
->function_pids
,
6412 lockdep_is_held(&ftrace_lock
));
6414 ret
= trace_pid_write(filtered_pids
, &pid_list
, ubuf
, cnt
);
6418 rcu_assign_pointer(tr
->function_pids
, pid_list
);
6420 if (filtered_pids
) {
6421 synchronize_sched();
6422 trace_free_pid_list(filtered_pids
);
6423 } else if (pid_list
) {
6424 /* Register a probe to set whether to ignore the tracing of a task */
6425 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe
, tr
);
6429 * Ignoring of pids is done at task switch. But we have to
6430 * check for those tasks that are currently running.
6431 * Always do this in case a pid was appended or removed.
6433 on_each_cpu(ignore_task_cpu
, tr
, 1);
6435 ftrace_update_pid_func();
6436 ftrace_startup_all(0);
6438 mutex_unlock(&ftrace_lock
);
6447 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
6449 struct trace_array
*tr
= inode
->i_private
;
6451 trace_array_put(tr
);
6453 return seq_release(inode
, file
);
6456 static const struct file_operations ftrace_pid_fops
= {
6457 .open
= ftrace_pid_open
,
6458 .write
= ftrace_pid_write
,
6460 .llseek
= tracing_lseek
,
6461 .release
= ftrace_pid_release
,
6464 void ftrace_init_tracefs(struct trace_array
*tr
, struct dentry
*d_tracer
)
6466 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
6467 tr
, &ftrace_pid_fops
);
6470 void __init
ftrace_init_tracefs_toplevel(struct trace_array
*tr
,
6471 struct dentry
*d_tracer
)
6473 /* Only the top level directory has the dyn_tracefs and profile */
6474 WARN_ON(!(tr
->flags
& TRACE_ARRAY_FL_GLOBAL
));
6476 ftrace_init_dyn_tracefs(d_tracer
);
6477 ftrace_profile_tracefs(d_tracer
);
6481 * ftrace_kill - kill ftrace
6483 * This function should be used by panic code. It stops ftrace
6484 * but in a not so nice way. If you need to simply kill ftrace
6485 * from a non-atomic section, use ftrace_kill.
6487 void ftrace_kill(void)
6489 ftrace_disabled
= 1;
6491 clear_ftrace_function();
6495 * Test if ftrace is dead or not.
6497 int ftrace_is_dead(void)
6499 return ftrace_disabled
;
6503 * register_ftrace_function - register a function for profiling
6504 * @ops - ops structure that holds the function for profiling.
6506 * Register a function to be called by all functions in the
6509 * Note: @ops->func and all the functions it calls must be labeled
6510 * with "notrace", otherwise it will go into a
6513 int register_ftrace_function(struct ftrace_ops
*ops
)
6517 ftrace_ops_init(ops
);
6519 mutex_lock(&ftrace_lock
);
6521 ret
= ftrace_startup(ops
, 0);
6523 mutex_unlock(&ftrace_lock
);
6527 EXPORT_SYMBOL_GPL(register_ftrace_function
);
6530 * unregister_ftrace_function - unregister a function for profiling.
6531 * @ops - ops structure that holds the function to unregister
6533 * Unregister a function that was added to be called by ftrace profiling.
6535 int unregister_ftrace_function(struct ftrace_ops
*ops
)
6539 mutex_lock(&ftrace_lock
);
6540 ret
= ftrace_shutdown(ops
, 0);
6541 mutex_unlock(&ftrace_lock
);
6545 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
6548 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
6549 void __user
*buffer
, size_t *lenp
,
6554 mutex_lock(&ftrace_lock
);
6556 if (unlikely(ftrace_disabled
))
6559 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
6561 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
6564 last_ftrace_enabled
= !!ftrace_enabled
;
6566 if (ftrace_enabled
) {
6568 /* we are starting ftrace again */
6569 if (rcu_dereference_protected(ftrace_ops_list
,
6570 lockdep_is_held(&ftrace_lock
)) != &ftrace_list_end
)
6571 update_ftrace_function();
6573 ftrace_startup_sysctl();
6576 /* stopping ftrace calls (just send to ftrace_stub) */
6577 ftrace_trace_function
= ftrace_stub
;
6579 ftrace_shutdown_sysctl();
6583 mutex_unlock(&ftrace_lock
);
6587 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6589 static struct ftrace_ops graph_ops
= {
6590 .func
= ftrace_stub
,
6591 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
|
6592 FTRACE_OPS_FL_INITIALIZED
|
6595 #ifdef FTRACE_GRAPH_TRAMP_ADDR
6596 .trampoline
= FTRACE_GRAPH_TRAMP_ADDR
,
6597 /* trampoline_size is only needed for dynamically allocated tramps */
6599 ASSIGN_OPS_HASH(graph_ops
, &global_ops
.local_hash
)
6602 void ftrace_graph_sleep_time_control(bool enable
)
6604 fgraph_sleep_time
= enable
;
6607 void ftrace_graph_graph_time_control(bool enable
)
6609 fgraph_graph_time
= enable
;
6612 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
6617 /* The callbacks that hook a function */
6618 trace_func_graph_ret_t ftrace_graph_return
=
6619 (trace_func_graph_ret_t
)ftrace_stub
;
6620 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
6621 static trace_func_graph_ent_t __ftrace_graph_entry
= ftrace_graph_entry_stub
;
6623 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6624 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
6628 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
6629 struct task_struct
*g
, *t
;
6631 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
6632 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
6633 * sizeof(struct ftrace_ret_stack
),
6635 if (!ret_stack_list
[i
]) {
6643 read_lock(&tasklist_lock
);
6644 do_each_thread(g
, t
) {
6650 if (t
->ret_stack
== NULL
) {
6651 atomic_set(&t
->tracing_graph_pause
, 0);
6652 atomic_set(&t
->trace_overrun
, 0);
6653 t
->curr_ret_stack
= -1;
6654 /* Make sure the tasks see the -1 first: */
6656 t
->ret_stack
= ret_stack_list
[start
++];
6658 } while_each_thread(g
, t
);
6661 read_unlock(&tasklist_lock
);
6663 for (i
= start
; i
< end
; i
++)
6664 kfree(ret_stack_list
[i
]);
6669 ftrace_graph_probe_sched_switch(void *ignore
, bool preempt
,
6670 struct task_struct
*prev
, struct task_struct
*next
)
6672 unsigned long long timestamp
;
6676 * Does the user want to count the time a function was asleep.
6677 * If so, do not update the time stamps.
6679 if (fgraph_sleep_time
)
6682 timestamp
= trace_clock_local();
6684 prev
->ftrace_timestamp
= timestamp
;
6686 /* only process tasks that we timestamped */
6687 if (!next
->ftrace_timestamp
)
6691 * Update all the counters in next to make up for the
6692 * time next was sleeping.
6694 timestamp
-= next
->ftrace_timestamp
;
6696 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
6697 next
->ret_stack
[index
].calltime
+= timestamp
;
6700 /* Allocate a return stack for each task */
6701 static int start_graph_tracing(void)
6703 struct ftrace_ret_stack
**ret_stack_list
;
6706 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
6707 sizeof(struct ftrace_ret_stack
*),
6710 if (!ret_stack_list
)
6713 /* The cpu_boot init_task->ret_stack will never be freed */
6714 for_each_online_cpu(cpu
) {
6715 if (!idle_task(cpu
)->ret_stack
)
6716 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
6720 ret
= alloc_retstack_tasklist(ret_stack_list
);
6721 } while (ret
== -EAGAIN
);
6724 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
6726 pr_info("ftrace_graph: Couldn't activate tracepoint"
6727 " probe to kernel_sched_switch\n");
6730 kfree(ret_stack_list
);
6735 * Hibernation protection.
6736 * The state of the current task is too much unstable during
6737 * suspend/restore to disk. We want to protect against that.
6740 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
6744 case PM_HIBERNATION_PREPARE
:
6745 pause_graph_tracing();
6748 case PM_POST_HIBERNATION
:
6749 unpause_graph_tracing();
6755 static int ftrace_graph_entry_test(struct ftrace_graph_ent
*trace
)
6757 if (!ftrace_ops_test(&global_ops
, trace
->func
, NULL
))
6759 return __ftrace_graph_entry(trace
);
6763 * The function graph tracer should only trace the functions defined
6764 * by set_ftrace_filter and set_ftrace_notrace. If another function
6765 * tracer ops is registered, the graph tracer requires testing the
6766 * function against the global ops, and not just trace any function
6767 * that any ftrace_ops registered.
6769 static void update_function_graph_func(void)
6771 struct ftrace_ops
*op
;
6772 bool do_test
= false;
6775 * The graph and global ops share the same set of functions
6776 * to test. If any other ops is on the list, then
6777 * the graph tracing needs to test if its the function
6780 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
6781 if (op
!= &global_ops
&& op
!= &graph_ops
&&
6782 op
!= &ftrace_list_end
) {
6784 /* in double loop, break out with goto */
6787 } while_for_each_ftrace_op(op
);
6790 ftrace_graph_entry
= ftrace_graph_entry_test
;
6792 ftrace_graph_entry
= __ftrace_graph_entry
;
6795 static struct notifier_block ftrace_suspend_notifier
= {
6796 .notifier_call
= ftrace_suspend_notifier_call
,
6799 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
6800 trace_func_graph_ent_t entryfunc
)
6804 mutex_lock(&ftrace_lock
);
6806 /* we currently allow only one tracer registered at a time */
6807 if (ftrace_graph_active
) {
6812 register_pm_notifier(&ftrace_suspend_notifier
);
6814 ftrace_graph_active
++;
6815 ret
= start_graph_tracing();
6817 ftrace_graph_active
--;
6821 ftrace_graph_return
= retfunc
;
6824 * Update the indirect function to the entryfunc, and the
6825 * function that gets called to the entry_test first. Then
6826 * call the update fgraph entry function to determine if
6827 * the entryfunc should be called directly or not.
6829 __ftrace_graph_entry
= entryfunc
;
6830 ftrace_graph_entry
= ftrace_graph_entry_test
;
6831 update_function_graph_func();
6833 ret
= ftrace_startup(&graph_ops
, FTRACE_START_FUNC_RET
);
6835 mutex_unlock(&ftrace_lock
);
6839 void unregister_ftrace_graph(void)
6841 mutex_lock(&ftrace_lock
);
6843 if (unlikely(!ftrace_graph_active
))
6846 ftrace_graph_active
--;
6847 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
6848 ftrace_graph_entry
= ftrace_graph_entry_stub
;
6849 __ftrace_graph_entry
= ftrace_graph_entry_stub
;
6850 ftrace_shutdown(&graph_ops
, FTRACE_STOP_FUNC_RET
);
6851 unregister_pm_notifier(&ftrace_suspend_notifier
);
6852 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
6855 mutex_unlock(&ftrace_lock
);
6858 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
6861 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
6863 atomic_set(&t
->tracing_graph_pause
, 0);
6864 atomic_set(&t
->trace_overrun
, 0);
6865 t
->ftrace_timestamp
= 0;
6866 /* make curr_ret_stack visible before we add the ret_stack */
6868 t
->ret_stack
= ret_stack
;
6872 * Allocate a return stack for the idle task. May be the first
6873 * time through, or it may be done by CPU hotplug online.
6875 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
6877 t
->curr_ret_stack
= -1;
6879 * The idle task has no parent, it either has its own
6880 * stack or no stack at all.
6883 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
6885 if (ftrace_graph_active
) {
6886 struct ftrace_ret_stack
*ret_stack
;
6888 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
6890 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
6891 * sizeof(struct ftrace_ret_stack
),
6895 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
6897 graph_init_task(t
, ret_stack
);
6901 /* Allocate a return stack for newly created task */
6902 void ftrace_graph_init_task(struct task_struct
*t
)
6904 /* Make sure we do not use the parent ret_stack */
6905 t
->ret_stack
= NULL
;
6906 t
->curr_ret_stack
= -1;
6908 if (ftrace_graph_active
) {
6909 struct ftrace_ret_stack
*ret_stack
;
6911 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
6912 * sizeof(struct ftrace_ret_stack
),
6916 graph_init_task(t
, ret_stack
);
6920 void ftrace_graph_exit_task(struct task_struct
*t
)
6922 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
6924 t
->ret_stack
= NULL
;
6925 /* NULL must become visible to IRQs before we free it: */