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/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
36 #include <trace/events/sched.h>
38 #include <asm/setup.h>
40 #include "trace_output.h"
41 #include "trace_stat.h"
43 #define FTRACE_WARN_ON(cond) \
51 #define FTRACE_WARN_ON_ONCE(cond) \
54 if (WARN_ON_ONCE(___r)) \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
71 #define INIT_REGEX_LOCK(opsname)
74 static struct ftrace_ops ftrace_list_end __read_mostly
= {
76 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_STUB
,
79 /* ftrace_enabled is a method to turn ftrace on or off */
80 int ftrace_enabled __read_mostly
;
81 static int last_ftrace_enabled
;
83 /* Quick disabling of function tracer. */
84 int function_trace_stop __read_mostly
;
86 /* Current function tracing op */
87 struct ftrace_ops
*function_trace_op __read_mostly
= &ftrace_list_end
;
88 /* What to set function_trace_op to */
89 static struct ftrace_ops
*set_function_trace_op
;
91 /* List for set_ftrace_pid's pids. */
92 LIST_HEAD(ftrace_pids
);
94 struct list_head list
;
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
102 static int ftrace_disabled __read_mostly
;
104 static DEFINE_MUTEX(ftrace_lock
);
106 static struct ftrace_ops
*ftrace_global_list __read_mostly
= &ftrace_list_end
;
107 static struct ftrace_ops
*ftrace_control_list __read_mostly
= &ftrace_list_end
;
108 static struct ftrace_ops
*ftrace_ops_list __read_mostly
= &ftrace_list_end
;
109 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
110 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
111 static struct ftrace_ops global_ops
;
112 static struct ftrace_ops control_ops
;
114 #if ARCH_SUPPORTS_FTRACE_OPS
115 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
116 struct ftrace_ops
*op
, struct pt_regs
*regs
);
118 /* See comment below, where ftrace_ops_list_func is defined */
119 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
);
120 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
124 * Traverse the ftrace_global_list, invoking all entries. The reason that we
125 * can use rcu_dereference_raw_notrace() is that elements removed from this list
126 * are simply leaked, so there is no need to interact with a grace-period
127 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
128 * concurrent insertions into the ftrace_global_list.
130 * Silly Alpha and silly pointer-speculation compiler optimizations!
132 #define do_for_each_ftrace_op(op, list) \
133 op = rcu_dereference_raw_notrace(list); \
137 * Optimized for just a single item in the list (as that is the normal case).
139 #define while_for_each_ftrace_op(op) \
140 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
141 unlikely((op) != &ftrace_list_end))
143 static inline void ftrace_ops_init(struct ftrace_ops
*ops
)
145 #ifdef CONFIG_DYNAMIC_FTRACE
146 if (!(ops
->flags
& FTRACE_OPS_FL_INITIALIZED
)) {
147 mutex_init(&ops
->regex_lock
);
148 ops
->flags
|= FTRACE_OPS_FL_INITIALIZED
;
154 * ftrace_nr_registered_ops - return number of ops registered
156 * Returns the number of ftrace_ops registered and tracing functions
158 int ftrace_nr_registered_ops(void)
160 struct ftrace_ops
*ops
;
163 mutex_lock(&ftrace_lock
);
165 for (ops
= ftrace_ops_list
;
166 ops
!= &ftrace_list_end
; ops
= ops
->next
)
169 mutex_unlock(&ftrace_lock
);
175 ftrace_global_list_func(unsigned long ip
, unsigned long parent_ip
,
176 struct ftrace_ops
*op
, struct pt_regs
*regs
)
180 bit
= trace_test_and_set_recursion(TRACE_GLOBAL_START
, TRACE_GLOBAL_MAX
);
184 do_for_each_ftrace_op(op
, ftrace_global_list
) {
185 op
->func(ip
, parent_ip
, op
, regs
);
186 } while_for_each_ftrace_op(op
);
188 trace_clear_recursion(bit
);
191 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
,
192 struct ftrace_ops
*op
, struct pt_regs
*regs
)
194 if (!test_tsk_trace_trace(current
))
197 ftrace_pid_function(ip
, parent_ip
, op
, regs
);
200 static void set_ftrace_pid_function(ftrace_func_t func
)
202 /* do not set ftrace_pid_function to itself! */
203 if (func
!= ftrace_pid_func
)
204 ftrace_pid_function
= func
;
208 * clear_ftrace_function - reset the ftrace function
210 * This NULLs the ftrace function and in essence stops
211 * tracing. There may be lag
213 void clear_ftrace_function(void)
215 ftrace_trace_function
= ftrace_stub
;
216 ftrace_pid_function
= ftrace_stub
;
219 static void control_ops_disable_all(struct ftrace_ops
*ops
)
223 for_each_possible_cpu(cpu
)
224 *per_cpu_ptr(ops
->disabled
, cpu
) = 1;
227 static int control_ops_alloc(struct ftrace_ops
*ops
)
229 int __percpu
*disabled
;
231 disabled
= alloc_percpu(int);
235 ops
->disabled
= disabled
;
236 control_ops_disable_all(ops
);
240 static void control_ops_free(struct ftrace_ops
*ops
)
242 free_percpu(ops
->disabled
);
245 static void update_global_ops(void)
250 * If there's only one function registered, then call that
251 * function directly. Otherwise, we need to iterate over the
252 * registered callers.
254 if (ftrace_global_list
== &ftrace_list_end
||
255 ftrace_global_list
->next
== &ftrace_list_end
) {
256 func
= ftrace_global_list
->func
;
258 * As we are calling the function directly.
259 * If it does not have recursion protection,
260 * the function_trace_op needs to be updated
263 if (ftrace_global_list
->flags
& FTRACE_OPS_FL_RECURSION_SAFE
)
264 global_ops
.flags
|= FTRACE_OPS_FL_RECURSION_SAFE
;
266 global_ops
.flags
&= ~FTRACE_OPS_FL_RECURSION_SAFE
;
268 func
= ftrace_global_list_func
;
269 /* The list has its own recursion protection. */
270 global_ops
.flags
|= FTRACE_OPS_FL_RECURSION_SAFE
;
274 /* If we filter on pids, update to use the pid function */
275 if (!list_empty(&ftrace_pids
)) {
276 set_ftrace_pid_function(func
);
277 func
= ftrace_pid_func
;
280 global_ops
.func
= func
;
283 static void ftrace_sync(struct work_struct
*work
)
286 * This function is just a stub to implement a hard force
287 * of synchronize_sched(). This requires synchronizing
288 * tasks even in userspace and idle.
290 * Yes, function tracing is rude.
294 static void ftrace_sync_ipi(void *data
)
296 /* Probably not needed, but do it anyway */
300 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
301 static void update_function_graph_func(void);
303 static inline void update_function_graph_func(void) { }
306 static void update_ftrace_function(void)
313 * If we are at the end of the list and this ops is
314 * recursion safe and not dynamic and the arch supports passing ops,
315 * then have the mcount trampoline call the function directly.
317 if (ftrace_ops_list
== &ftrace_list_end
||
318 (ftrace_ops_list
->next
== &ftrace_list_end
&&
319 !(ftrace_ops_list
->flags
& FTRACE_OPS_FL_DYNAMIC
) &&
320 (ftrace_ops_list
->flags
& FTRACE_OPS_FL_RECURSION_SAFE
) &&
321 !FTRACE_FORCE_LIST_FUNC
)) {
322 /* Set the ftrace_ops that the arch callback uses */
323 if (ftrace_ops_list
== &global_ops
)
324 set_function_trace_op
= ftrace_global_list
;
326 set_function_trace_op
= ftrace_ops_list
;
327 func
= ftrace_ops_list
->func
;
329 /* Just use the default ftrace_ops */
330 set_function_trace_op
= &ftrace_list_end
;
331 func
= ftrace_ops_list_func
;
334 update_function_graph_func();
336 /* If there's no change, then do nothing more here */
337 if (ftrace_trace_function
== func
)
341 * If we are using the list function, it doesn't care
342 * about the function_trace_ops.
344 if (func
== ftrace_ops_list_func
) {
345 ftrace_trace_function
= func
;
347 * Don't even bother setting function_trace_ops,
348 * it would be racy to do so anyway.
353 #ifndef CONFIG_DYNAMIC_FTRACE
355 * For static tracing, we need to be a bit more careful.
356 * The function change takes affect immediately. Thus,
357 * we need to coorditate the setting of the function_trace_ops
358 * with the setting of the ftrace_trace_function.
360 * Set the function to the list ops, which will call the
361 * function we want, albeit indirectly, but it handles the
362 * ftrace_ops and doesn't depend on function_trace_op.
364 ftrace_trace_function
= ftrace_ops_list_func
;
366 * Make sure all CPUs see this. Yes this is slow, but static
367 * tracing is slow and nasty to have enabled.
369 schedule_on_each_cpu(ftrace_sync
);
370 /* Now all cpus are using the list ops. */
371 function_trace_op
= set_function_trace_op
;
372 /* Make sure the function_trace_op is visible on all CPUs */
374 /* Nasty way to force a rmb on all cpus */
375 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
376 /* OK, we are all set to update the ftrace_trace_function now! */
377 #endif /* !CONFIG_DYNAMIC_FTRACE */
379 ftrace_trace_function
= func
;
382 static void add_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
386 * We are entering ops into the list but another
387 * CPU might be walking that list. We need to make sure
388 * the ops->next pointer is valid before another CPU sees
389 * the ops pointer included into the list.
391 rcu_assign_pointer(*list
, ops
);
394 static int remove_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
396 struct ftrace_ops
**p
;
399 * If we are removing the last function, then simply point
400 * to the ftrace_stub.
402 if (*list
== ops
&& ops
->next
== &ftrace_list_end
) {
403 *list
= &ftrace_list_end
;
407 for (p
= list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
418 static void add_ftrace_list_ops(struct ftrace_ops
**list
,
419 struct ftrace_ops
*main_ops
,
420 struct ftrace_ops
*ops
)
422 int first
= *list
== &ftrace_list_end
;
423 add_ftrace_ops(list
, ops
);
425 add_ftrace_ops(&ftrace_ops_list
, main_ops
);
428 static int remove_ftrace_list_ops(struct ftrace_ops
**list
,
429 struct ftrace_ops
*main_ops
,
430 struct ftrace_ops
*ops
)
432 int ret
= remove_ftrace_ops(list
, ops
);
433 if (!ret
&& *list
== &ftrace_list_end
)
434 ret
= remove_ftrace_ops(&ftrace_ops_list
, main_ops
);
438 static int __register_ftrace_function(struct ftrace_ops
*ops
)
440 if (FTRACE_WARN_ON(ops
== &global_ops
))
443 if (WARN_ON(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
446 /* We don't support both control and global flags set. */
447 if ((ops
->flags
& FL_GLOBAL_CONTROL_MASK
) == FL_GLOBAL_CONTROL_MASK
)
450 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
452 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
453 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
454 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
456 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
&&
457 !(ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
))
460 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
)
461 ops
->flags
|= FTRACE_OPS_FL_SAVE_REGS
;
464 if (!core_kernel_data((unsigned long)ops
))
465 ops
->flags
|= FTRACE_OPS_FL_DYNAMIC
;
467 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
468 add_ftrace_list_ops(&ftrace_global_list
, &global_ops
, ops
);
469 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
470 } else if (ops
->flags
& FTRACE_OPS_FL_CONTROL
) {
471 if (control_ops_alloc(ops
))
473 add_ftrace_list_ops(&ftrace_control_list
, &control_ops
, ops
);
475 add_ftrace_ops(&ftrace_ops_list
, ops
);
478 update_ftrace_function();
483 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
487 if (WARN_ON(!(ops
->flags
& FTRACE_OPS_FL_ENABLED
)))
490 if (FTRACE_WARN_ON(ops
== &global_ops
))
493 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
494 ret
= remove_ftrace_list_ops(&ftrace_global_list
,
497 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
498 } else if (ops
->flags
& FTRACE_OPS_FL_CONTROL
) {
499 ret
= remove_ftrace_list_ops(&ftrace_control_list
,
502 ret
= remove_ftrace_ops(&ftrace_ops_list
, ops
);
508 update_ftrace_function();
513 static void ftrace_update_pid_func(void)
515 /* Only do something if we are tracing something */
516 if (ftrace_trace_function
== ftrace_stub
)
519 update_ftrace_function();
522 #ifdef CONFIG_FUNCTION_PROFILER
523 struct ftrace_profile
{
524 struct hlist_node node
;
526 unsigned long counter
;
527 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
528 unsigned long long time
;
529 unsigned long long time_squared
;
533 struct ftrace_profile_page
{
534 struct ftrace_profile_page
*next
;
536 struct ftrace_profile records
[];
539 struct ftrace_profile_stat
{
541 struct hlist_head
*hash
;
542 struct ftrace_profile_page
*pages
;
543 struct ftrace_profile_page
*start
;
544 struct tracer_stat stat
;
547 #define PROFILE_RECORDS_SIZE \
548 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
550 #define PROFILES_PER_PAGE \
551 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
553 static int ftrace_profile_enabled __read_mostly
;
555 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
556 static DEFINE_MUTEX(ftrace_profile_lock
);
558 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
560 #define FTRACE_PROFILE_HASH_BITS 10
561 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
564 function_stat_next(void *v
, int idx
)
566 struct ftrace_profile
*rec
= v
;
567 struct ftrace_profile_page
*pg
;
569 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
575 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
579 rec
= &pg
->records
[0];
587 static void *function_stat_start(struct tracer_stat
*trace
)
589 struct ftrace_profile_stat
*stat
=
590 container_of(trace
, struct ftrace_profile_stat
, stat
);
592 if (!stat
|| !stat
->start
)
595 return function_stat_next(&stat
->start
->records
[0], 0);
598 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
599 /* function graph compares on total time */
600 static int function_stat_cmp(void *p1
, void *p2
)
602 struct ftrace_profile
*a
= p1
;
603 struct ftrace_profile
*b
= p2
;
605 if (a
->time
< b
->time
)
607 if (a
->time
> b
->time
)
613 /* not function graph compares against hits */
614 static int function_stat_cmp(void *p1
, void *p2
)
616 struct ftrace_profile
*a
= p1
;
617 struct ftrace_profile
*b
= p2
;
619 if (a
->counter
< b
->counter
)
621 if (a
->counter
> b
->counter
)
628 static int function_stat_headers(struct seq_file
*m
)
630 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
631 seq_printf(m
, " Function "
634 "--- ---- --- ---\n");
636 seq_printf(m
, " Function Hit\n"
642 static int function_stat_show(struct seq_file
*m
, void *v
)
644 struct ftrace_profile
*rec
= v
;
645 char str
[KSYM_SYMBOL_LEN
];
647 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
648 static struct trace_seq s
;
649 unsigned long long avg
;
650 unsigned long long stddev
;
652 mutex_lock(&ftrace_profile_lock
);
654 /* we raced with function_profile_reset() */
655 if (unlikely(rec
->counter
== 0)) {
660 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
661 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
663 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
666 do_div(avg
, rec
->counter
);
668 /* Sample standard deviation (s^2) */
669 if (rec
->counter
<= 1)
673 * Apply Welford's method:
674 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
676 stddev
= rec
->counter
* rec
->time_squared
-
677 rec
->time
* rec
->time
;
680 * Divide only 1000 for ns^2 -> us^2 conversion.
681 * trace_print_graph_duration will divide 1000 again.
683 do_div(stddev
, rec
->counter
* (rec
->counter
- 1) * 1000);
687 trace_print_graph_duration(rec
->time
, &s
);
688 trace_seq_puts(&s
, " ");
689 trace_print_graph_duration(avg
, &s
);
690 trace_seq_puts(&s
, " ");
691 trace_print_graph_duration(stddev
, &s
);
692 trace_print_seq(m
, &s
);
696 mutex_unlock(&ftrace_profile_lock
);
701 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
703 struct ftrace_profile_page
*pg
;
705 pg
= stat
->pages
= stat
->start
;
708 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
713 memset(stat
->hash
, 0,
714 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
717 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
719 struct ftrace_profile_page
*pg
;
724 /* If we already allocated, do nothing */
728 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
732 #ifdef CONFIG_DYNAMIC_FTRACE
733 functions
= ftrace_update_tot_cnt
;
736 * We do not know the number of functions that exist because
737 * dynamic tracing is what counts them. With past experience
738 * we have around 20K functions. That should be more than enough.
739 * It is highly unlikely we will execute every function in
745 pg
= stat
->start
= stat
->pages
;
747 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
749 for (i
= 1; i
< pages
; i
++) {
750 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
761 unsigned long tmp
= (unsigned long)pg
;
773 static int ftrace_profile_init_cpu(int cpu
)
775 struct ftrace_profile_stat
*stat
;
778 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
781 /* If the profile is already created, simply reset it */
782 ftrace_profile_reset(stat
);
787 * We are profiling all functions, but usually only a few thousand
788 * functions are hit. We'll make a hash of 1024 items.
790 size
= FTRACE_PROFILE_HASH_SIZE
;
792 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
797 /* Preallocate the function profiling pages */
798 if (ftrace_profile_pages_init(stat
) < 0) {
807 static int ftrace_profile_init(void)
812 for_each_possible_cpu(cpu
) {
813 ret
= ftrace_profile_init_cpu(cpu
);
821 /* interrupts must be disabled */
822 static struct ftrace_profile
*
823 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
825 struct ftrace_profile
*rec
;
826 struct hlist_head
*hhd
;
829 key
= hash_long(ip
, FTRACE_PROFILE_HASH_BITS
);
830 hhd
= &stat
->hash
[key
];
832 if (hlist_empty(hhd
))
835 hlist_for_each_entry_rcu_notrace(rec
, hhd
, node
) {
843 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
844 struct ftrace_profile
*rec
)
848 key
= hash_long(rec
->ip
, FTRACE_PROFILE_HASH_BITS
);
849 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
853 * The memory is already allocated, this simply finds a new record to use.
855 static struct ftrace_profile
*
856 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
858 struct ftrace_profile
*rec
= NULL
;
860 /* prevent recursion (from NMIs) */
861 if (atomic_inc_return(&stat
->disabled
) != 1)
865 * Try to find the function again since an NMI
866 * could have added it
868 rec
= ftrace_find_profiled_func(stat
, ip
);
872 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
873 if (!stat
->pages
->next
)
875 stat
->pages
= stat
->pages
->next
;
878 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
880 ftrace_add_profile(stat
, rec
);
883 atomic_dec(&stat
->disabled
);
889 function_profile_call(unsigned long ip
, unsigned long parent_ip
,
890 struct ftrace_ops
*ops
, struct pt_regs
*regs
)
892 struct ftrace_profile_stat
*stat
;
893 struct ftrace_profile
*rec
;
896 if (!ftrace_profile_enabled
)
899 local_irq_save(flags
);
901 stat
= &__get_cpu_var(ftrace_profile_stats
);
902 if (!stat
->hash
|| !ftrace_profile_enabled
)
905 rec
= ftrace_find_profiled_func(stat
, ip
);
907 rec
= ftrace_profile_alloc(stat
, ip
);
914 local_irq_restore(flags
);
917 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
918 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
920 function_profile_call(trace
->func
, 0, NULL
, NULL
);
924 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
926 struct ftrace_profile_stat
*stat
;
927 unsigned long long calltime
;
928 struct ftrace_profile
*rec
;
931 local_irq_save(flags
);
932 stat
= &__get_cpu_var(ftrace_profile_stats
);
933 if (!stat
->hash
|| !ftrace_profile_enabled
)
936 /* If the calltime was zero'd ignore it */
937 if (!trace
->calltime
)
940 calltime
= trace
->rettime
- trace
->calltime
;
942 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
945 index
= trace
->depth
;
947 /* Append this call time to the parent time to subtract */
949 current
->ret_stack
[index
- 1].subtime
+= calltime
;
951 if (current
->ret_stack
[index
].subtime
< calltime
)
952 calltime
-= current
->ret_stack
[index
].subtime
;
957 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
959 rec
->time
+= calltime
;
960 rec
->time_squared
+= calltime
* calltime
;
964 local_irq_restore(flags
);
967 static int register_ftrace_profiler(void)
969 return register_ftrace_graph(&profile_graph_return
,
970 &profile_graph_entry
);
973 static void unregister_ftrace_profiler(void)
975 unregister_ftrace_graph();
978 static struct ftrace_ops ftrace_profile_ops __read_mostly
= {
979 .func
= function_profile_call
,
980 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
981 INIT_REGEX_LOCK(ftrace_profile_ops
)
984 static int register_ftrace_profiler(void)
986 return register_ftrace_function(&ftrace_profile_ops
);
989 static void unregister_ftrace_profiler(void)
991 unregister_ftrace_function(&ftrace_profile_ops
);
993 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
996 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
997 size_t cnt
, loff_t
*ppos
)
1002 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
1008 mutex_lock(&ftrace_profile_lock
);
1009 if (ftrace_profile_enabled
^ val
) {
1011 ret
= ftrace_profile_init();
1017 ret
= register_ftrace_profiler();
1022 ftrace_profile_enabled
= 1;
1024 ftrace_profile_enabled
= 0;
1026 * unregister_ftrace_profiler calls stop_machine
1027 * so this acts like an synchronize_sched.
1029 unregister_ftrace_profiler();
1033 mutex_unlock(&ftrace_profile_lock
);
1041 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
1042 size_t cnt
, loff_t
*ppos
)
1044 char buf
[64]; /* big enough to hold a number */
1047 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
1048 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1051 static const struct file_operations ftrace_profile_fops
= {
1052 .open
= tracing_open_generic
,
1053 .read
= ftrace_profile_read
,
1054 .write
= ftrace_profile_write
,
1055 .llseek
= default_llseek
,
1058 /* used to initialize the real stat files */
1059 static struct tracer_stat function_stats __initdata
= {
1060 .name
= "functions",
1061 .stat_start
= function_stat_start
,
1062 .stat_next
= function_stat_next
,
1063 .stat_cmp
= function_stat_cmp
,
1064 .stat_headers
= function_stat_headers
,
1065 .stat_show
= function_stat_show
1068 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
1070 struct ftrace_profile_stat
*stat
;
1071 struct dentry
*entry
;
1076 for_each_possible_cpu(cpu
) {
1077 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
1079 /* allocate enough for function name + cpu number */
1080 name
= kmalloc(32, GFP_KERNEL
);
1083 * The files created are permanent, if something happens
1084 * we still do not free memory.
1087 "Could not allocate stat file for cpu %d\n",
1091 stat
->stat
= function_stats
;
1092 snprintf(name
, 32, "function%d", cpu
);
1093 stat
->stat
.name
= name
;
1094 ret
= register_stat_tracer(&stat
->stat
);
1097 "Could not register function stat for cpu %d\n",
1104 entry
= debugfs_create_file("function_profile_enabled", 0644,
1105 d_tracer
, NULL
, &ftrace_profile_fops
);
1107 pr_warning("Could not create debugfs "
1108 "'function_profile_enabled' entry\n");
1111 #else /* CONFIG_FUNCTION_PROFILER */
1112 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
1115 #endif /* CONFIG_FUNCTION_PROFILER */
1117 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
1120 ftrace_filter_lseek(struct file
*file
, loff_t offset
, int whence
)
1124 if (file
->f_mode
& FMODE_READ
)
1125 ret
= seq_lseek(file
, offset
, whence
);
1127 file
->f_pos
= ret
= 1;
1132 #ifdef CONFIG_DYNAMIC_FTRACE
1134 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1135 # error Dynamic ftrace depends on MCOUNT_RECORD
1138 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
1140 struct ftrace_func_probe
{
1141 struct hlist_node node
;
1142 struct ftrace_probe_ops
*ops
;
1143 unsigned long flags
;
1146 struct list_head free_list
;
1149 struct ftrace_func_entry
{
1150 struct hlist_node hlist
;
1154 struct ftrace_hash
{
1155 unsigned long size_bits
;
1156 struct hlist_head
*buckets
;
1157 unsigned long count
;
1158 struct rcu_head rcu
;
1162 * We make these constant because no one should touch them,
1163 * but they are used as the default "empty hash", to avoid allocating
1164 * it all the time. These are in a read only section such that if
1165 * anyone does try to modify it, it will cause an exception.
1167 static const struct hlist_head empty_buckets
[1];
1168 static const struct ftrace_hash empty_hash
= {
1169 .buckets
= (struct hlist_head
*)empty_buckets
,
1171 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1173 static struct ftrace_ops global_ops
= {
1174 .func
= ftrace_stub
,
1175 .notrace_hash
= EMPTY_HASH
,
1176 .filter_hash
= EMPTY_HASH
,
1177 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
1178 INIT_REGEX_LOCK(global_ops
)
1181 struct ftrace_page
{
1182 struct ftrace_page
*next
;
1183 struct dyn_ftrace
*records
;
1188 static struct ftrace_page
*ftrace_new_pgs
;
1190 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1191 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1193 /* estimate from running different kernels */
1194 #define NR_TO_INIT 10000
1196 static struct ftrace_page
*ftrace_pages_start
;
1197 static struct ftrace_page
*ftrace_pages
;
1199 static bool ftrace_hash_empty(struct ftrace_hash
*hash
)
1201 return !hash
|| !hash
->count
;
1204 static struct ftrace_func_entry
*
1205 ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1208 struct ftrace_func_entry
*entry
;
1209 struct hlist_head
*hhd
;
1211 if (ftrace_hash_empty(hash
))
1214 if (hash
->size_bits
> 0)
1215 key
= hash_long(ip
, hash
->size_bits
);
1219 hhd
= &hash
->buckets
[key
];
1221 hlist_for_each_entry_rcu_notrace(entry
, hhd
, hlist
) {
1222 if (entry
->ip
== ip
)
1228 static void __add_hash_entry(struct ftrace_hash
*hash
,
1229 struct ftrace_func_entry
*entry
)
1231 struct hlist_head
*hhd
;
1234 if (hash
->size_bits
)
1235 key
= hash_long(entry
->ip
, hash
->size_bits
);
1239 hhd
= &hash
->buckets
[key
];
1240 hlist_add_head(&entry
->hlist
, hhd
);
1244 static int add_hash_entry(struct ftrace_hash
*hash
, unsigned long ip
)
1246 struct ftrace_func_entry
*entry
;
1248 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1253 __add_hash_entry(hash
, entry
);
1259 free_hash_entry(struct ftrace_hash
*hash
,
1260 struct ftrace_func_entry
*entry
)
1262 hlist_del(&entry
->hlist
);
1268 remove_hash_entry(struct ftrace_hash
*hash
,
1269 struct ftrace_func_entry
*entry
)
1271 hlist_del(&entry
->hlist
);
1275 static void ftrace_hash_clear(struct ftrace_hash
*hash
)
1277 struct hlist_head
*hhd
;
1278 struct hlist_node
*tn
;
1279 struct ftrace_func_entry
*entry
;
1280 int size
= 1 << hash
->size_bits
;
1286 for (i
= 0; i
< size
; i
++) {
1287 hhd
= &hash
->buckets
[i
];
1288 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
)
1289 free_hash_entry(hash
, entry
);
1291 FTRACE_WARN_ON(hash
->count
);
1294 static void free_ftrace_hash(struct ftrace_hash
*hash
)
1296 if (!hash
|| hash
== EMPTY_HASH
)
1298 ftrace_hash_clear(hash
);
1299 kfree(hash
->buckets
);
1303 static void __free_ftrace_hash_rcu(struct rcu_head
*rcu
)
1305 struct ftrace_hash
*hash
;
1307 hash
= container_of(rcu
, struct ftrace_hash
, rcu
);
1308 free_ftrace_hash(hash
);
1311 static void free_ftrace_hash_rcu(struct ftrace_hash
*hash
)
1313 if (!hash
|| hash
== EMPTY_HASH
)
1315 call_rcu_sched(&hash
->rcu
, __free_ftrace_hash_rcu
);
1318 void ftrace_free_filter(struct ftrace_ops
*ops
)
1320 ftrace_ops_init(ops
);
1321 free_ftrace_hash(ops
->filter_hash
);
1322 free_ftrace_hash(ops
->notrace_hash
);
1325 static struct ftrace_hash
*alloc_ftrace_hash(int size_bits
)
1327 struct ftrace_hash
*hash
;
1330 hash
= kzalloc(sizeof(*hash
), GFP_KERNEL
);
1334 size
= 1 << size_bits
;
1335 hash
->buckets
= kcalloc(size
, sizeof(*hash
->buckets
), GFP_KERNEL
);
1337 if (!hash
->buckets
) {
1342 hash
->size_bits
= size_bits
;
1347 static struct ftrace_hash
*
1348 alloc_and_copy_ftrace_hash(int size_bits
, struct ftrace_hash
*hash
)
1350 struct ftrace_func_entry
*entry
;
1351 struct ftrace_hash
*new_hash
;
1356 new_hash
= alloc_ftrace_hash(size_bits
);
1361 if (ftrace_hash_empty(hash
))
1364 size
= 1 << hash
->size_bits
;
1365 for (i
= 0; i
< size
; i
++) {
1366 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
1367 ret
= add_hash_entry(new_hash
, entry
->ip
);
1373 FTRACE_WARN_ON(new_hash
->count
!= hash
->count
);
1378 free_ftrace_hash(new_hash
);
1383 ftrace_hash_rec_disable(struct ftrace_ops
*ops
, int filter_hash
);
1385 ftrace_hash_rec_enable(struct ftrace_ops
*ops
, int filter_hash
);
1388 ftrace_hash_move(struct ftrace_ops
*ops
, int enable
,
1389 struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1391 struct ftrace_func_entry
*entry
;
1392 struct hlist_node
*tn
;
1393 struct hlist_head
*hhd
;
1394 struct ftrace_hash
*old_hash
;
1395 struct ftrace_hash
*new_hash
;
1396 int size
= src
->count
;
1402 * Remove the current set, update the hash and add
1405 ftrace_hash_rec_disable(ops
, enable
);
1408 * If the new source is empty, just free dst and assign it
1412 free_ftrace_hash_rcu(*dst
);
1413 rcu_assign_pointer(*dst
, EMPTY_HASH
);
1414 /* still need to update the function records */
1420 * Make the hash size about 1/2 the # found
1422 for (size
/= 2; size
; size
>>= 1)
1425 /* Don't allocate too much */
1426 if (bits
> FTRACE_HASH_MAX_BITS
)
1427 bits
= FTRACE_HASH_MAX_BITS
;
1430 new_hash
= alloc_ftrace_hash(bits
);
1434 size
= 1 << src
->size_bits
;
1435 for (i
= 0; i
< size
; i
++) {
1436 hhd
= &src
->buckets
[i
];
1437 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
) {
1438 remove_hash_entry(src
, entry
);
1439 __add_hash_entry(new_hash
, entry
);
1444 rcu_assign_pointer(*dst
, new_hash
);
1445 free_ftrace_hash_rcu(old_hash
);
1450 * Enable regardless of ret:
1451 * On success, we enable the new hash.
1452 * On failure, we re-enable the original hash.
1454 ftrace_hash_rec_enable(ops
, enable
);
1460 * Test the hashes for this ops to see if we want to call
1461 * the ops->func or not.
1463 * It's a match if the ip is in the ops->filter_hash or
1464 * the filter_hash does not exist or is empty,
1466 * the ip is not in the ops->notrace_hash.
1468 * This needs to be called with preemption disabled as
1469 * the hashes are freed with call_rcu_sched().
1472 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
1474 struct ftrace_hash
*filter_hash
;
1475 struct ftrace_hash
*notrace_hash
;
1478 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1480 * There's a small race when adding ops that the ftrace handler
1481 * that wants regs, may be called without them. We can not
1482 * allow that handler to be called if regs is NULL.
1484 if (regs
== NULL
&& (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
))
1488 filter_hash
= rcu_dereference_raw_notrace(ops
->filter_hash
);
1489 notrace_hash
= rcu_dereference_raw_notrace(ops
->notrace_hash
);
1491 if ((ftrace_hash_empty(filter_hash
) ||
1492 ftrace_lookup_ip(filter_hash
, ip
)) &&
1493 (ftrace_hash_empty(notrace_hash
) ||
1494 !ftrace_lookup_ip(notrace_hash
, ip
)))
1503 * This is a double for. Do not use 'break' to break out of the loop,
1504 * you must use a goto.
1506 #define do_for_each_ftrace_rec(pg, rec) \
1507 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1509 for (_____i = 0; _____i < pg->index; _____i++) { \
1510 rec = &pg->records[_____i];
1512 #define while_for_each_ftrace_rec() \
1517 static int ftrace_cmp_recs(const void *a
, const void *b
)
1519 const struct dyn_ftrace
*key
= a
;
1520 const struct dyn_ftrace
*rec
= b
;
1522 if (key
->flags
< rec
->ip
)
1524 if (key
->ip
>= rec
->ip
+ MCOUNT_INSN_SIZE
)
1529 static unsigned long ftrace_location_range(unsigned long start
, unsigned long end
)
1531 struct ftrace_page
*pg
;
1532 struct dyn_ftrace
*rec
;
1533 struct dyn_ftrace key
;
1536 key
.flags
= end
; /* overload flags, as it is unsigned long */
1538 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
1539 if (end
< pg
->records
[0].ip
||
1540 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
1542 rec
= bsearch(&key
, pg
->records
, pg
->index
,
1543 sizeof(struct dyn_ftrace
),
1553 * ftrace_location - return true if the ip giving is a traced location
1554 * @ip: the instruction pointer to check
1556 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1557 * That is, the instruction that is either a NOP or call to
1558 * the function tracer. It checks the ftrace internal tables to
1559 * determine if the address belongs or not.
1561 unsigned long ftrace_location(unsigned long ip
)
1563 return ftrace_location_range(ip
, ip
);
1567 * ftrace_text_reserved - return true if range contains an ftrace location
1568 * @start: start of range to search
1569 * @end: end of range to search (inclusive). @end points to the last byte to check.
1571 * Returns 1 if @start and @end contains a ftrace location.
1572 * That is, the instruction that is either a NOP or call to
1573 * the function tracer. It checks the ftrace internal tables to
1574 * determine if the address belongs or not.
1576 int ftrace_text_reserved(void *start
, void *end
)
1580 ret
= ftrace_location_range((unsigned long)start
,
1581 (unsigned long)end
);
1586 static void __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1590 struct ftrace_hash
*hash
;
1591 struct ftrace_hash
*other_hash
;
1592 struct ftrace_page
*pg
;
1593 struct dyn_ftrace
*rec
;
1597 /* Only update if the ops has been registered */
1598 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1602 * In the filter_hash case:
1603 * If the count is zero, we update all records.
1604 * Otherwise we just update the items in the hash.
1606 * In the notrace_hash case:
1607 * We enable the update in the hash.
1608 * As disabling notrace means enabling the tracing,
1609 * and enabling notrace means disabling, the inc variable
1613 hash
= ops
->filter_hash
;
1614 other_hash
= ops
->notrace_hash
;
1615 if (ftrace_hash_empty(hash
))
1619 hash
= ops
->notrace_hash
;
1620 other_hash
= ops
->filter_hash
;
1622 * If the notrace hash has no items,
1623 * then there's nothing to do.
1625 if (ftrace_hash_empty(hash
))
1629 do_for_each_ftrace_rec(pg
, rec
) {
1630 int in_other_hash
= 0;
1636 * Only the filter_hash affects all records.
1637 * Update if the record is not in the notrace hash.
1639 if (!other_hash
|| !ftrace_lookup_ip(other_hash
, rec
->ip
))
1642 in_hash
= !!ftrace_lookup_ip(hash
, rec
->ip
);
1643 in_other_hash
= !!ftrace_lookup_ip(other_hash
, rec
->ip
);
1648 if (filter_hash
&& in_hash
&& !in_other_hash
)
1650 else if (!filter_hash
&& in_hash
&&
1651 (in_other_hash
|| ftrace_hash_empty(other_hash
)))
1659 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == FTRACE_REF_MAX
))
1662 * If any ops wants regs saved for this function
1663 * then all ops will get saved regs.
1665 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
1666 rec
->flags
|= FTRACE_FL_REGS
;
1668 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == 0))
1673 /* Shortcut, if we handled all records, we are done. */
1674 if (!all
&& count
== hash
->count
)
1676 } while_for_each_ftrace_rec();
1679 static void ftrace_hash_rec_disable(struct ftrace_ops
*ops
,
1682 __ftrace_hash_rec_update(ops
, filter_hash
, 0);
1685 static void ftrace_hash_rec_enable(struct ftrace_ops
*ops
,
1688 __ftrace_hash_rec_update(ops
, filter_hash
, 1);
1691 static void print_ip_ins(const char *fmt
, unsigned char *p
)
1695 printk(KERN_CONT
"%s", fmt
);
1697 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
1698 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
1702 * ftrace_bug - report and shutdown function tracer
1703 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1704 * @ip: The address that failed
1706 * The arch code that enables or disables the function tracing
1707 * can call ftrace_bug() when it has detected a problem in
1708 * modifying the code. @failed should be one of either:
1709 * EFAULT - if the problem happens on reading the @ip address
1710 * EINVAL - if what is read at @ip is not what was expected
1711 * EPERM - if the problem happens on writting to the @ip address
1713 void ftrace_bug(int failed
, unsigned long ip
)
1717 FTRACE_WARN_ON_ONCE(1);
1718 pr_info("ftrace faulted on modifying ");
1722 FTRACE_WARN_ON_ONCE(1);
1723 pr_info("ftrace failed to modify ");
1725 print_ip_ins(" actual: ", (unsigned char *)ip
);
1726 printk(KERN_CONT
"\n");
1729 FTRACE_WARN_ON_ONCE(1);
1730 pr_info("ftrace faulted on writing ");
1734 FTRACE_WARN_ON_ONCE(1);
1735 pr_info("ftrace faulted on unknown error ");
1740 static int ftrace_check_record(struct dyn_ftrace
*rec
, int enable
, int update
)
1742 unsigned long flag
= 0UL;
1745 * If we are updating calls:
1747 * If the record has a ref count, then we need to enable it
1748 * because someone is using it.
1750 * Otherwise we make sure its disabled.
1752 * If we are disabling calls, then disable all records that
1755 if (enable
&& (rec
->flags
& ~FTRACE_FL_MASK
))
1756 flag
= FTRACE_FL_ENABLED
;
1759 * If enabling and the REGS flag does not match the REGS_EN, then
1760 * do not ignore this record. Set flags to fail the compare against
1764 (!(rec
->flags
& FTRACE_FL_REGS
) != !(rec
->flags
& FTRACE_FL_REGS_EN
)))
1765 flag
|= FTRACE_FL_REGS
;
1767 /* If the state of this record hasn't changed, then do nothing */
1768 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1769 return FTRACE_UPDATE_IGNORE
;
1772 /* Save off if rec is being enabled (for return value) */
1773 flag
^= rec
->flags
& FTRACE_FL_ENABLED
;
1776 rec
->flags
|= FTRACE_FL_ENABLED
;
1777 if (flag
& FTRACE_FL_REGS
) {
1778 if (rec
->flags
& FTRACE_FL_REGS
)
1779 rec
->flags
|= FTRACE_FL_REGS_EN
;
1781 rec
->flags
&= ~FTRACE_FL_REGS_EN
;
1786 * If this record is being updated from a nop, then
1787 * return UPDATE_MAKE_CALL.
1788 * Otherwise, if the EN flag is set, then return
1789 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1790 * from the non-save regs, to a save regs function.
1792 * return UPDATE_MODIFY_CALL to tell the caller to convert
1793 * from the save regs, to a non-save regs function.
1795 if (flag
& FTRACE_FL_ENABLED
)
1796 return FTRACE_UPDATE_MAKE_CALL
;
1797 else if (rec
->flags
& FTRACE_FL_REGS_EN
)
1798 return FTRACE_UPDATE_MODIFY_CALL_REGS
;
1800 return FTRACE_UPDATE_MODIFY_CALL
;
1804 /* If there's no more users, clear all flags */
1805 if (!(rec
->flags
& ~FTRACE_FL_MASK
))
1808 /* Just disable the record (keep REGS state) */
1809 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1812 return FTRACE_UPDATE_MAKE_NOP
;
1816 * ftrace_update_record, set a record that now is tracing or not
1817 * @rec: the record to update
1818 * @enable: set to 1 if the record is tracing, zero to force disable
1820 * The records that represent all functions that can be traced need
1821 * to be updated when tracing has been enabled.
1823 int ftrace_update_record(struct dyn_ftrace
*rec
, int enable
)
1825 return ftrace_check_record(rec
, enable
, 1);
1829 * ftrace_test_record, check if the record has been enabled or not
1830 * @rec: the record to test
1831 * @enable: set to 1 to check if enabled, 0 if it is disabled
1833 * The arch code may need to test if a record is already set to
1834 * tracing to determine how to modify the function code that it
1837 int ftrace_test_record(struct dyn_ftrace
*rec
, int enable
)
1839 return ftrace_check_record(rec
, enable
, 0);
1843 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1845 unsigned long ftrace_old_addr
;
1846 unsigned long ftrace_addr
;
1849 ret
= ftrace_update_record(rec
, enable
);
1851 if (rec
->flags
& FTRACE_FL_REGS
)
1852 ftrace_addr
= (unsigned long)FTRACE_REGS_ADDR
;
1854 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1857 case FTRACE_UPDATE_IGNORE
:
1860 case FTRACE_UPDATE_MAKE_CALL
:
1861 return ftrace_make_call(rec
, ftrace_addr
);
1863 case FTRACE_UPDATE_MAKE_NOP
:
1864 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1866 case FTRACE_UPDATE_MODIFY_CALL_REGS
:
1867 case FTRACE_UPDATE_MODIFY_CALL
:
1868 if (rec
->flags
& FTRACE_FL_REGS
)
1869 ftrace_old_addr
= (unsigned long)FTRACE_ADDR
;
1871 ftrace_old_addr
= (unsigned long)FTRACE_REGS_ADDR
;
1873 return ftrace_modify_call(rec
, ftrace_old_addr
, ftrace_addr
);
1876 return -1; /* unknow ftrace bug */
1879 void __weak
ftrace_replace_code(int enable
)
1881 struct dyn_ftrace
*rec
;
1882 struct ftrace_page
*pg
;
1885 if (unlikely(ftrace_disabled
))
1888 do_for_each_ftrace_rec(pg
, rec
) {
1889 failed
= __ftrace_replace_code(rec
, enable
);
1891 ftrace_bug(failed
, rec
->ip
);
1892 /* Stop processing */
1895 } while_for_each_ftrace_rec();
1898 struct ftrace_rec_iter
{
1899 struct ftrace_page
*pg
;
1904 * ftrace_rec_iter_start, start up iterating over traced functions
1906 * Returns an iterator handle that is used to iterate over all
1907 * the records that represent address locations where functions
1910 * May return NULL if no records are available.
1912 struct ftrace_rec_iter
*ftrace_rec_iter_start(void)
1915 * We only use a single iterator.
1916 * Protected by the ftrace_lock mutex.
1918 static struct ftrace_rec_iter ftrace_rec_iter
;
1919 struct ftrace_rec_iter
*iter
= &ftrace_rec_iter
;
1921 iter
->pg
= ftrace_pages_start
;
1924 /* Could have empty pages */
1925 while (iter
->pg
&& !iter
->pg
->index
)
1926 iter
->pg
= iter
->pg
->next
;
1935 * ftrace_rec_iter_next, get the next record to process.
1936 * @iter: The handle to the iterator.
1938 * Returns the next iterator after the given iterator @iter.
1940 struct ftrace_rec_iter
*ftrace_rec_iter_next(struct ftrace_rec_iter
*iter
)
1944 if (iter
->index
>= iter
->pg
->index
) {
1945 iter
->pg
= iter
->pg
->next
;
1948 /* Could have empty pages */
1949 while (iter
->pg
&& !iter
->pg
->index
)
1950 iter
->pg
= iter
->pg
->next
;
1960 * ftrace_rec_iter_record, get the record at the iterator location
1961 * @iter: The current iterator location
1963 * Returns the record that the current @iter is at.
1965 struct dyn_ftrace
*ftrace_rec_iter_record(struct ftrace_rec_iter
*iter
)
1967 return &iter
->pg
->records
[iter
->index
];
1971 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1978 if (unlikely(ftrace_disabled
))
1981 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1983 ftrace_bug(ret
, ip
);
1990 * archs can override this function if they must do something
1991 * before the modifying code is performed.
1993 int __weak
ftrace_arch_code_modify_prepare(void)
1999 * archs can override this function if they must do something
2000 * after the modifying code is performed.
2002 int __weak
ftrace_arch_code_modify_post_process(void)
2007 void ftrace_modify_all_code(int command
)
2009 int update
= command
& FTRACE_UPDATE_TRACE_FUNC
;
2012 * If the ftrace_caller calls a ftrace_ops func directly,
2013 * we need to make sure that it only traces functions it
2014 * expects to trace. When doing the switch of functions,
2015 * we need to update to the ftrace_ops_list_func first
2016 * before the transition between old and new calls are set,
2017 * as the ftrace_ops_list_func will check the ops hashes
2018 * to make sure the ops are having the right functions
2022 ftrace_update_ftrace_func(ftrace_ops_list_func
);
2024 if (command
& FTRACE_UPDATE_CALLS
)
2025 ftrace_replace_code(1);
2026 else if (command
& FTRACE_DISABLE_CALLS
)
2027 ftrace_replace_code(0);
2029 if (update
&& ftrace_trace_function
!= ftrace_ops_list_func
) {
2030 function_trace_op
= set_function_trace_op
;
2032 /* If irqs are disabled, we are in stop machine */
2033 if (!irqs_disabled())
2034 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
2035 ftrace_update_ftrace_func(ftrace_trace_function
);
2038 if (command
& FTRACE_START_FUNC_RET
)
2039 ftrace_enable_ftrace_graph_caller();
2040 else if (command
& FTRACE_STOP_FUNC_RET
)
2041 ftrace_disable_ftrace_graph_caller();
2044 static int __ftrace_modify_code(void *data
)
2046 int *command
= data
;
2048 ftrace_modify_all_code(*command
);
2054 * ftrace_run_stop_machine, go back to the stop machine method
2055 * @command: The command to tell ftrace what to do
2057 * If an arch needs to fall back to the stop machine method, the
2058 * it can call this function.
2060 void ftrace_run_stop_machine(int command
)
2062 stop_machine(__ftrace_modify_code
, &command
, NULL
);
2066 * arch_ftrace_update_code, modify the code to trace or not trace
2067 * @command: The command that needs to be done
2069 * Archs can override this function if it does not need to
2070 * run stop_machine() to modify code.
2072 void __weak
arch_ftrace_update_code(int command
)
2074 ftrace_run_stop_machine(command
);
2077 static void ftrace_run_update_code(int command
)
2081 ret
= ftrace_arch_code_modify_prepare();
2082 FTRACE_WARN_ON(ret
);
2086 * Do not call function tracer while we update the code.
2087 * We are in stop machine.
2089 function_trace_stop
++;
2092 * By default we use stop_machine() to modify the code.
2093 * But archs can do what ever they want as long as it
2094 * is safe. The stop_machine() is the safest, but also
2095 * produces the most overhead.
2097 arch_ftrace_update_code(command
);
2099 function_trace_stop
--;
2101 ret
= ftrace_arch_code_modify_post_process();
2102 FTRACE_WARN_ON(ret
);
2105 static ftrace_func_t saved_ftrace_func
;
2106 static int ftrace_start_up
;
2107 static int global_start_up
;
2109 static void ftrace_startup_enable(int command
)
2111 if (saved_ftrace_func
!= ftrace_trace_function
) {
2112 saved_ftrace_func
= ftrace_trace_function
;
2113 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2116 if (!command
|| !ftrace_enabled
)
2119 ftrace_run_update_code(command
);
2122 static int ftrace_startup(struct ftrace_ops
*ops
, int command
)
2124 bool hash_enable
= true;
2127 if (unlikely(ftrace_disabled
))
2130 ret
= __register_ftrace_function(ops
);
2135 command
|= FTRACE_UPDATE_CALLS
;
2137 /* ops marked global share the filter hashes */
2138 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
2140 /* Don't update hash if global is already set */
2141 if (global_start_up
)
2142 hash_enable
= false;
2146 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
2148 ftrace_hash_rec_enable(ops
, 1);
2150 ftrace_startup_enable(command
);
2155 static int ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
2157 bool hash_disable
= true;
2160 if (unlikely(ftrace_disabled
))
2163 ret
= __unregister_ftrace_function(ops
);
2169 * Just warn in case of unbalance, no need to kill ftrace, it's not
2170 * critical but the ftrace_call callers may be never nopped again after
2171 * further ftrace uses.
2173 WARN_ON_ONCE(ftrace_start_up
< 0);
2175 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
2178 WARN_ON_ONCE(global_start_up
< 0);
2179 /* Don't update hash if global still has users */
2180 if (global_start_up
) {
2181 WARN_ON_ONCE(!ftrace_start_up
);
2182 hash_disable
= false;
2187 ftrace_hash_rec_disable(ops
, 1);
2189 if (ops
!= &global_ops
|| !global_start_up
)
2190 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
2192 command
|= FTRACE_UPDATE_CALLS
;
2194 if (saved_ftrace_func
!= ftrace_trace_function
) {
2195 saved_ftrace_func
= ftrace_trace_function
;
2196 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2199 if (!command
|| !ftrace_enabled
) {
2201 * If these are control ops, they still need their
2202 * per_cpu field freed. Since, function tracing is
2203 * not currently active, we can just free them
2204 * without synchronizing all CPUs.
2206 if (ops
->flags
& FTRACE_OPS_FL_CONTROL
)
2207 control_ops_free(ops
);
2211 ftrace_run_update_code(command
);
2214 * Dynamic ops may be freed, we must make sure that all
2215 * callers are done before leaving this function.
2216 * The same goes for freeing the per_cpu data of the control
2219 * Again, normal synchronize_sched() is not good enough.
2220 * We need to do a hard force of sched synchronization.
2221 * This is because we use preempt_disable() to do RCU, but
2222 * the function tracers can be called where RCU is not watching
2223 * (like before user_exit()). We can not rely on the RCU
2224 * infrastructure to do the synchronization, thus we must do it
2227 if (ops
->flags
& (FTRACE_OPS_FL_DYNAMIC
| FTRACE_OPS_FL_CONTROL
)) {
2228 schedule_on_each_cpu(ftrace_sync
);
2230 if (ops
->flags
& FTRACE_OPS_FL_CONTROL
)
2231 control_ops_free(ops
);
2237 static void ftrace_startup_sysctl(void)
2239 if (unlikely(ftrace_disabled
))
2242 /* Force update next time */
2243 saved_ftrace_func
= NULL
;
2244 /* ftrace_start_up is true if we want ftrace running */
2245 if (ftrace_start_up
)
2246 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
2249 static void ftrace_shutdown_sysctl(void)
2251 if (unlikely(ftrace_disabled
))
2254 /* ftrace_start_up is true if ftrace is running */
2255 if (ftrace_start_up
)
2256 ftrace_run_update_code(FTRACE_DISABLE_CALLS
);
2259 static cycle_t ftrace_update_time
;
2260 static unsigned long ftrace_update_cnt
;
2261 unsigned long ftrace_update_tot_cnt
;
2263 static inline int ops_traces_mod(struct ftrace_ops
*ops
)
2266 * Filter_hash being empty will default to trace module.
2267 * But notrace hash requires a test of individual module functions.
2269 return ftrace_hash_empty(ops
->filter_hash
) &&
2270 ftrace_hash_empty(ops
->notrace_hash
);
2274 * Check if the current ops references the record.
2276 * If the ops traces all functions, then it was already accounted for.
2277 * If the ops does not trace the current record function, skip it.
2278 * If the ops ignores the function via notrace filter, skip it.
2281 ops_references_rec(struct ftrace_ops
*ops
, struct dyn_ftrace
*rec
)
2283 /* If ops isn't enabled, ignore it */
2284 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
2287 /* If ops traces all mods, we already accounted for it */
2288 if (ops_traces_mod(ops
))
2291 /* The function must be in the filter */
2292 if (!ftrace_hash_empty(ops
->filter_hash
) &&
2293 !ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))
2296 /* If in notrace hash, we ignore it too */
2297 if (ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
))
2303 static int referenced_filters(struct dyn_ftrace
*rec
)
2305 struct ftrace_ops
*ops
;
2308 for (ops
= ftrace_ops_list
; ops
!= &ftrace_list_end
; ops
= ops
->next
) {
2309 if (ops_references_rec(ops
, rec
))
2316 static int ftrace_update_code(struct module
*mod
)
2318 struct ftrace_page
*pg
;
2319 struct dyn_ftrace
*p
;
2320 cycle_t start
, stop
;
2321 unsigned long ref
= 0;
2326 * When adding a module, we need to check if tracers are
2327 * currently enabled and if they are set to trace all functions.
2328 * If they are, we need to enable the module functions as well
2329 * as update the reference counts for those function records.
2332 struct ftrace_ops
*ops
;
2334 for (ops
= ftrace_ops_list
;
2335 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
2336 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
) {
2337 if (ops_traces_mod(ops
))
2345 start
= ftrace_now(raw_smp_processor_id());
2346 ftrace_update_cnt
= 0;
2348 for (pg
= ftrace_new_pgs
; pg
; pg
= pg
->next
) {
2350 for (i
= 0; i
< pg
->index
; i
++) {
2353 /* If something went wrong, bail without enabling anything */
2354 if (unlikely(ftrace_disabled
))
2357 p
= &pg
->records
[i
];
2359 cnt
+= referenced_filters(p
);
2363 * Do the initial record conversion from mcount jump
2364 * to the NOP instructions.
2366 if (!ftrace_code_disable(mod
, p
))
2369 ftrace_update_cnt
++;
2372 * If the tracing is enabled, go ahead and enable the record.
2374 * The reason not to enable the record immediatelly is the
2375 * inherent check of ftrace_make_nop/ftrace_make_call for
2376 * correct previous instructions. Making first the NOP
2377 * conversion puts the module to the correct state, thus
2378 * passing the ftrace_make_call check.
2380 if (ftrace_start_up
&& cnt
) {
2381 int failed
= __ftrace_replace_code(p
, 1);
2383 ftrace_bug(failed
, p
->ip
);
2388 ftrace_new_pgs
= NULL
;
2390 stop
= ftrace_now(raw_smp_processor_id());
2391 ftrace_update_time
= stop
- start
;
2392 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
2397 static int ftrace_allocate_records(struct ftrace_page
*pg
, int count
)
2402 if (WARN_ON(!count
))
2405 order
= get_count_order(DIV_ROUND_UP(count
, ENTRIES_PER_PAGE
));
2408 * We want to fill as much as possible. No more than a page
2411 while ((PAGE_SIZE
<< order
) / ENTRY_SIZE
>= count
+ ENTRIES_PER_PAGE
)
2415 pg
->records
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
2418 /* if we can't allocate this size, try something smaller */
2425 cnt
= (PAGE_SIZE
<< order
) / ENTRY_SIZE
;
2434 static struct ftrace_page
*
2435 ftrace_allocate_pages(unsigned long num_to_init
)
2437 struct ftrace_page
*start_pg
;
2438 struct ftrace_page
*pg
;
2445 start_pg
= pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2450 * Try to allocate as much as possible in one continues
2451 * location that fills in all of the space. We want to
2452 * waste as little space as possible.
2455 cnt
= ftrace_allocate_records(pg
, num_to_init
);
2463 pg
->next
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2474 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
2475 free_pages((unsigned long)pg
->records
, order
);
2476 start_pg
= pg
->next
;
2480 pr_info("ftrace: FAILED to allocate memory for functions\n");
2484 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
2489 pr_info("ftrace: No functions to be traced?\n");
2493 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
2494 pr_info("ftrace: allocating %ld entries in %d pages\n",
2495 num_to_init
, cnt
+ 1);
2500 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2502 struct ftrace_iterator
{
2505 struct ftrace_page
*pg
;
2506 struct dyn_ftrace
*func
;
2507 struct ftrace_func_probe
*probe
;
2508 struct trace_parser parser
;
2509 struct ftrace_hash
*hash
;
2510 struct ftrace_ops
*ops
;
2517 t_hash_next(struct seq_file
*m
, loff_t
*pos
)
2519 struct ftrace_iterator
*iter
= m
->private;
2520 struct hlist_node
*hnd
= NULL
;
2521 struct hlist_head
*hhd
;
2527 hnd
= &iter
->probe
->node
;
2529 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
2532 hhd
= &ftrace_func_hash
[iter
->hidx
];
2534 if (hlist_empty(hhd
)) {
2550 if (WARN_ON_ONCE(!hnd
))
2553 iter
->probe
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
2558 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
2560 struct ftrace_iterator
*iter
= m
->private;
2564 if (!(iter
->flags
& FTRACE_ITER_DO_HASH
))
2567 if (iter
->func_pos
> *pos
)
2571 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
2572 p
= t_hash_next(m
, &l
);
2579 /* Only set this if we have an item */
2580 iter
->flags
|= FTRACE_ITER_HASH
;
2586 t_hash_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
2588 struct ftrace_func_probe
*rec
;
2591 if (WARN_ON_ONCE(!rec
))
2594 if (rec
->ops
->print
)
2595 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
2597 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
2600 seq_printf(m
, ":%p", rec
->data
);
2607 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2609 struct ftrace_iterator
*iter
= m
->private;
2610 struct ftrace_ops
*ops
= iter
->ops
;
2611 struct dyn_ftrace
*rec
= NULL
;
2613 if (unlikely(ftrace_disabled
))
2616 if (iter
->flags
& FTRACE_ITER_HASH
)
2617 return t_hash_next(m
, pos
);
2620 iter
->pos
= iter
->func_pos
= *pos
;
2622 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
2623 return t_hash_start(m
, pos
);
2626 if (iter
->idx
>= iter
->pg
->index
) {
2627 if (iter
->pg
->next
) {
2628 iter
->pg
= iter
->pg
->next
;
2633 rec
= &iter
->pg
->records
[iter
->idx
++];
2634 if (((iter
->flags
& FTRACE_ITER_FILTER
) &&
2635 !(ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))) ||
2637 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
2638 !ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
)) ||
2640 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
2641 !(rec
->flags
& FTRACE_FL_ENABLED
))) {
2649 return t_hash_start(m
, pos
);
2656 static void reset_iter_read(struct ftrace_iterator
*iter
)
2660 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
| FTRACE_ITER_HASH
);
2663 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2665 struct ftrace_iterator
*iter
= m
->private;
2666 struct ftrace_ops
*ops
= iter
->ops
;
2670 mutex_lock(&ftrace_lock
);
2672 if (unlikely(ftrace_disabled
))
2676 * If an lseek was done, then reset and start from beginning.
2678 if (*pos
< iter
->pos
)
2679 reset_iter_read(iter
);
2682 * For set_ftrace_filter reading, if we have the filter
2683 * off, we can short cut and just print out that all
2684 * functions are enabled.
2686 if (iter
->flags
& FTRACE_ITER_FILTER
&&
2687 ftrace_hash_empty(ops
->filter_hash
)) {
2689 return t_hash_start(m
, pos
);
2690 iter
->flags
|= FTRACE_ITER_PRINTALL
;
2691 /* reset in case of seek/pread */
2692 iter
->flags
&= ~FTRACE_ITER_HASH
;
2696 if (iter
->flags
& FTRACE_ITER_HASH
)
2697 return t_hash_start(m
, pos
);
2700 * Unfortunately, we need to restart at ftrace_pages_start
2701 * every time we let go of the ftrace_mutex. This is because
2702 * those pointers can change without the lock.
2704 iter
->pg
= ftrace_pages_start
;
2706 for (l
= 0; l
<= *pos
; ) {
2707 p
= t_next(m
, p
, &l
);
2713 return t_hash_start(m
, pos
);
2718 static void t_stop(struct seq_file
*m
, void *p
)
2720 mutex_unlock(&ftrace_lock
);
2723 static int t_show(struct seq_file
*m
, void *v
)
2725 struct ftrace_iterator
*iter
= m
->private;
2726 struct dyn_ftrace
*rec
;
2728 if (iter
->flags
& FTRACE_ITER_HASH
)
2729 return t_hash_show(m
, iter
);
2731 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
2732 seq_printf(m
, "#### all functions enabled ####\n");
2741 seq_printf(m
, "%ps", (void *)rec
->ip
);
2742 if (iter
->flags
& FTRACE_ITER_ENABLED
)
2743 seq_printf(m
, " (%ld)%s",
2744 rec
->flags
& ~FTRACE_FL_MASK
,
2745 rec
->flags
& FTRACE_FL_REGS
? " R" : "");
2746 seq_printf(m
, "\n");
2751 static const struct seq_operations show_ftrace_seq_ops
= {
2759 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
2761 struct ftrace_iterator
*iter
;
2763 if (unlikely(ftrace_disabled
))
2766 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
2768 iter
->pg
= ftrace_pages_start
;
2769 iter
->ops
= &global_ops
;
2772 return iter
? 0 : -ENOMEM
;
2776 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
2778 struct ftrace_iterator
*iter
;
2780 if (unlikely(ftrace_disabled
))
2783 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
2785 iter
->pg
= ftrace_pages_start
;
2786 iter
->flags
= FTRACE_ITER_ENABLED
;
2787 iter
->ops
= &global_ops
;
2790 return iter
? 0 : -ENOMEM
;
2793 static void ftrace_filter_reset(struct ftrace_hash
*hash
)
2795 mutex_lock(&ftrace_lock
);
2796 ftrace_hash_clear(hash
);
2797 mutex_unlock(&ftrace_lock
);
2801 * ftrace_regex_open - initialize function tracer filter files
2802 * @ops: The ftrace_ops that hold the hash filters
2803 * @flag: The type of filter to process
2804 * @inode: The inode, usually passed in to your open routine
2805 * @file: The file, usually passed in to your open routine
2807 * ftrace_regex_open() initializes the filter files for the
2808 * @ops. Depending on @flag it may process the filter hash or
2809 * the notrace hash of @ops. With this called from the open
2810 * routine, you can use ftrace_filter_write() for the write
2811 * routine if @flag has FTRACE_ITER_FILTER set, or
2812 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2813 * ftrace_filter_lseek() should be used as the lseek routine, and
2814 * release must call ftrace_regex_release().
2817 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
2818 struct inode
*inode
, struct file
*file
)
2820 struct ftrace_iterator
*iter
;
2821 struct ftrace_hash
*hash
;
2824 ftrace_ops_init(ops
);
2826 if (unlikely(ftrace_disabled
))
2829 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2833 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
2841 mutex_lock(&ops
->regex_lock
);
2843 if (flag
& FTRACE_ITER_NOTRACE
)
2844 hash
= ops
->notrace_hash
;
2846 hash
= ops
->filter_hash
;
2848 if (file
->f_mode
& FMODE_WRITE
) {
2849 iter
->hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, hash
);
2851 trace_parser_put(&iter
->parser
);
2858 if ((file
->f_mode
& FMODE_WRITE
) &&
2859 (file
->f_flags
& O_TRUNC
))
2860 ftrace_filter_reset(iter
->hash
);
2862 if (file
->f_mode
& FMODE_READ
) {
2863 iter
->pg
= ftrace_pages_start
;
2865 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2867 struct seq_file
*m
= file
->private_data
;
2871 free_ftrace_hash(iter
->hash
);
2872 trace_parser_put(&iter
->parser
);
2876 file
->private_data
= iter
;
2879 mutex_unlock(&ops
->regex_lock
);
2885 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
2887 return ftrace_regex_open(&global_ops
,
2888 FTRACE_ITER_FILTER
| FTRACE_ITER_DO_HASH
,
2893 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
2895 return ftrace_regex_open(&global_ops
, FTRACE_ITER_NOTRACE
,
2899 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
2906 if (strcmp(str
, regex
) == 0)
2909 case MATCH_FRONT_ONLY
:
2910 if (strncmp(str
, regex
, len
) == 0)
2913 case MATCH_MIDDLE_ONLY
:
2914 if (strstr(str
, regex
))
2917 case MATCH_END_ONLY
:
2919 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
2928 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int not)
2930 struct ftrace_func_entry
*entry
;
2933 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
2935 /* Do nothing if it doesn't exist */
2939 free_hash_entry(hash
, entry
);
2941 /* Do nothing if it exists */
2945 ret
= add_hash_entry(hash
, rec
->ip
);
2951 ftrace_match_record(struct dyn_ftrace
*rec
, char *mod
,
2952 char *regex
, int len
, int type
)
2954 char str
[KSYM_SYMBOL_LEN
];
2957 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
2960 /* module lookup requires matching the module */
2961 if (!modname
|| strcmp(modname
, mod
))
2964 /* blank search means to match all funcs in the mod */
2969 return ftrace_match(str
, regex
, len
, type
);
2973 match_records(struct ftrace_hash
*hash
, char *buff
,
2974 int len
, char *mod
, int not)
2976 unsigned search_len
= 0;
2977 struct ftrace_page
*pg
;
2978 struct dyn_ftrace
*rec
;
2979 int type
= MATCH_FULL
;
2980 char *search
= buff
;
2985 type
= filter_parse_regex(buff
, len
, &search
, ¬);
2986 search_len
= strlen(search
);
2989 mutex_lock(&ftrace_lock
);
2991 if (unlikely(ftrace_disabled
))
2994 do_for_each_ftrace_rec(pg
, rec
) {
2995 if (ftrace_match_record(rec
, mod
, search
, search_len
, type
)) {
2996 ret
= enter_record(hash
, rec
, not);
3003 } while_for_each_ftrace_rec();
3005 mutex_unlock(&ftrace_lock
);
3011 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
3013 return match_records(hash
, buff
, len
, NULL
, 0);
3017 ftrace_match_module_records(struct ftrace_hash
*hash
, char *buff
, char *mod
)
3021 /* blank or '*' mean the same */
3022 if (strcmp(buff
, "*") == 0)
3025 /* handle the case of 'dont filter this module' */
3026 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
3031 return match_records(hash
, buff
, strlen(buff
), mod
, not);
3035 * We register the module command as a template to show others how
3036 * to register the a command as well.
3040 ftrace_mod_callback(struct ftrace_hash
*hash
,
3041 char *func
, char *cmd
, char *param
, int enable
)
3047 * cmd == 'mod' because we only registered this func
3048 * for the 'mod' ftrace_func_command.
3049 * But if you register one func with multiple commands,
3050 * you can tell which command was used by the cmd
3054 /* we must have a module name */
3058 mod
= strsep(¶m
, ":");
3062 ret
= ftrace_match_module_records(hash
, func
, mod
);
3071 static struct ftrace_func_command ftrace_mod_cmd
= {
3073 .func
= ftrace_mod_callback
,
3076 static int __init
ftrace_mod_cmd_init(void)
3078 return register_ftrace_command(&ftrace_mod_cmd
);
3080 core_initcall(ftrace_mod_cmd_init
);
3082 static void function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
,
3083 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
3085 struct ftrace_func_probe
*entry
;
3086 struct hlist_head
*hhd
;
3089 key
= hash_long(ip
, FTRACE_HASH_BITS
);
3091 hhd
= &ftrace_func_hash
[key
];
3093 if (hlist_empty(hhd
))
3097 * Disable preemption for these calls to prevent a RCU grace
3098 * period. This syncs the hash iteration and freeing of items
3099 * on the hash. rcu_read_lock is too dangerous here.
3101 preempt_disable_notrace();
3102 hlist_for_each_entry_rcu_notrace(entry
, hhd
, node
) {
3103 if (entry
->ip
== ip
)
3104 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
3106 preempt_enable_notrace();
3109 static struct ftrace_ops trace_probe_ops __read_mostly
=
3111 .func
= function_trace_probe_call
,
3112 .flags
= FTRACE_OPS_FL_INITIALIZED
,
3113 INIT_REGEX_LOCK(trace_probe_ops
)
3116 static int ftrace_probe_registered
;
3118 static void __enable_ftrace_function_probe(void)
3123 if (ftrace_probe_registered
) {
3124 /* still need to update the function call sites */
3126 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3130 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
3131 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
3135 /* Nothing registered? */
3136 if (i
== FTRACE_FUNC_HASHSIZE
)
3139 ret
= ftrace_startup(&trace_probe_ops
, 0);
3141 ftrace_probe_registered
= 1;
3144 static void __disable_ftrace_function_probe(void)
3148 if (!ftrace_probe_registered
)
3151 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
3152 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
3157 /* no more funcs left */
3158 ftrace_shutdown(&trace_probe_ops
, 0);
3160 ftrace_probe_registered
= 0;
3164 static void ftrace_free_entry(struct ftrace_func_probe
*entry
)
3166 if (entry
->ops
->free
)
3167 entry
->ops
->free(entry
->ops
, entry
->ip
, &entry
->data
);
3172 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3175 struct ftrace_func_probe
*entry
;
3176 struct ftrace_hash
**orig_hash
= &trace_probe_ops
.filter_hash
;
3177 struct ftrace_hash
*hash
;
3178 struct ftrace_page
*pg
;
3179 struct dyn_ftrace
*rec
;
3186 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
3187 len
= strlen(search
);
3189 /* we do not support '!' for function probes */
3193 mutex_lock(&trace_probe_ops
.regex_lock
);
3195 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3201 if (unlikely(ftrace_disabled
)) {
3206 mutex_lock(&ftrace_lock
);
3208 do_for_each_ftrace_rec(pg
, rec
) {
3210 if (!ftrace_match_record(rec
, NULL
, search
, len
, type
))
3213 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
3215 /* If we did not process any, then return error */
3226 * The caller might want to do something special
3227 * for each function we find. We call the callback
3228 * to give the caller an opportunity to do so.
3231 if (ops
->init(ops
, rec
->ip
, &entry
->data
) < 0) {
3232 /* caller does not like this func */
3238 ret
= enter_record(hash
, rec
, 0);
3246 entry
->ip
= rec
->ip
;
3248 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
3249 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
3251 } while_for_each_ftrace_rec();
3253 ret
= ftrace_hash_move(&trace_probe_ops
, 1, orig_hash
, hash
);
3257 __enable_ftrace_function_probe();
3260 mutex_unlock(&ftrace_lock
);
3262 mutex_unlock(&trace_probe_ops
.regex_lock
);
3263 free_ftrace_hash(hash
);
3269 PROBE_TEST_FUNC
= 1,
3274 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3275 void *data
, int flags
)
3277 struct ftrace_func_entry
*rec_entry
;
3278 struct ftrace_func_probe
*entry
;
3279 struct ftrace_func_probe
*p
;
3280 struct ftrace_hash
**orig_hash
= &trace_probe_ops
.filter_hash
;
3281 struct list_head free_list
;
3282 struct ftrace_hash
*hash
;
3283 struct hlist_node
*tmp
;
3284 char str
[KSYM_SYMBOL_LEN
];
3285 int type
= MATCH_FULL
;
3289 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
3294 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
3295 len
= strlen(search
);
3297 /* we do not support '!' for function probes */
3302 mutex_lock(&trace_probe_ops
.regex_lock
);
3304 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3306 /* Hmm, should report this somehow */
3309 INIT_LIST_HEAD(&free_list
);
3311 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
3312 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
3314 hlist_for_each_entry_safe(entry
, tmp
, hhd
, node
) {
3316 /* break up if statements for readability */
3317 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
3320 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
3323 /* do this last, since it is the most expensive */
3325 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
3327 if (!ftrace_match(str
, glob
, len
, type
))
3331 rec_entry
= ftrace_lookup_ip(hash
, entry
->ip
);
3332 /* It is possible more than one entry had this ip */
3334 free_hash_entry(hash
, rec_entry
);
3336 hlist_del_rcu(&entry
->node
);
3337 list_add(&entry
->free_list
, &free_list
);
3340 mutex_lock(&ftrace_lock
);
3341 __disable_ftrace_function_probe();
3343 * Remove after the disable is called. Otherwise, if the last
3344 * probe is removed, a null hash means *all enabled*.
3346 ftrace_hash_move(&trace_probe_ops
, 1, orig_hash
, hash
);
3347 synchronize_sched();
3348 list_for_each_entry_safe(entry
, p
, &free_list
, free_list
) {
3349 list_del(&entry
->free_list
);
3350 ftrace_free_entry(entry
);
3352 mutex_unlock(&ftrace_lock
);
3355 mutex_unlock(&trace_probe_ops
.regex_lock
);
3356 free_ftrace_hash(hash
);
3360 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3363 __unregister_ftrace_function_probe(glob
, ops
, data
,
3364 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
3368 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
3370 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
3373 void unregister_ftrace_function_probe_all(char *glob
)
3375 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
3378 static LIST_HEAD(ftrace_commands
);
3379 static DEFINE_MUTEX(ftrace_cmd_mutex
);
3381 int register_ftrace_command(struct ftrace_func_command
*cmd
)
3383 struct ftrace_func_command
*p
;
3386 mutex_lock(&ftrace_cmd_mutex
);
3387 list_for_each_entry(p
, &ftrace_commands
, list
) {
3388 if (strcmp(cmd
->name
, p
->name
) == 0) {
3393 list_add(&cmd
->list
, &ftrace_commands
);
3395 mutex_unlock(&ftrace_cmd_mutex
);
3400 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
3402 struct ftrace_func_command
*p
, *n
;
3405 mutex_lock(&ftrace_cmd_mutex
);
3406 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
3407 if (strcmp(cmd
->name
, p
->name
) == 0) {
3409 list_del_init(&p
->list
);
3414 mutex_unlock(&ftrace_cmd_mutex
);
3419 static int ftrace_process_regex(struct ftrace_hash
*hash
,
3420 char *buff
, int len
, int enable
)
3422 char *func
, *command
, *next
= buff
;
3423 struct ftrace_func_command
*p
;
3426 func
= strsep(&next
, ":");
3429 ret
= ftrace_match_records(hash
, func
, len
);
3439 command
= strsep(&next
, ":");
3441 mutex_lock(&ftrace_cmd_mutex
);
3442 list_for_each_entry(p
, &ftrace_commands
, list
) {
3443 if (strcmp(p
->name
, command
) == 0) {
3444 ret
= p
->func(hash
, func
, command
, next
, enable
);
3449 mutex_unlock(&ftrace_cmd_mutex
);
3455 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
3456 size_t cnt
, loff_t
*ppos
, int enable
)
3458 struct ftrace_iterator
*iter
;
3459 struct trace_parser
*parser
;
3465 if (file
->f_mode
& FMODE_READ
) {
3466 struct seq_file
*m
= file
->private_data
;
3469 iter
= file
->private_data
;
3471 if (unlikely(ftrace_disabled
))
3474 /* iter->hash is a local copy, so we don't need regex_lock */
3476 parser
= &iter
->parser
;
3477 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
3479 if (read
>= 0 && trace_parser_loaded(parser
) &&
3480 !trace_parser_cont(parser
)) {
3481 ret
= ftrace_process_regex(iter
->hash
, parser
->buffer
,
3482 parser
->idx
, enable
);
3483 trace_parser_clear(parser
);
3494 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
3495 size_t cnt
, loff_t
*ppos
)
3497 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
3501 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
3502 size_t cnt
, loff_t
*ppos
)
3504 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
3508 ftrace_match_addr(struct ftrace_hash
*hash
, unsigned long ip
, int remove
)
3510 struct ftrace_func_entry
*entry
;
3512 if (!ftrace_location(ip
))
3516 entry
= ftrace_lookup_ip(hash
, ip
);
3519 free_hash_entry(hash
, entry
);
3523 return add_hash_entry(hash
, ip
);
3526 static void ftrace_ops_update_code(struct ftrace_ops
*ops
)
3528 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
&& ftrace_enabled
)
3529 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3533 ftrace_set_hash(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3534 unsigned long ip
, int remove
, int reset
, int enable
)
3536 struct ftrace_hash
**orig_hash
;
3537 struct ftrace_hash
*hash
;
3540 /* All global ops uses the global ops filters */
3541 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
)
3544 if (unlikely(ftrace_disabled
))
3547 mutex_lock(&ops
->regex_lock
);
3550 orig_hash
= &ops
->filter_hash
;
3552 orig_hash
= &ops
->notrace_hash
;
3554 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3557 goto out_regex_unlock
;
3561 ftrace_filter_reset(hash
);
3562 if (buf
&& !ftrace_match_records(hash
, buf
, len
)) {
3564 goto out_regex_unlock
;
3567 ret
= ftrace_match_addr(hash
, ip
, remove
);
3569 goto out_regex_unlock
;
3572 mutex_lock(&ftrace_lock
);
3573 ret
= ftrace_hash_move(ops
, enable
, orig_hash
, hash
);
3575 ftrace_ops_update_code(ops
);
3577 mutex_unlock(&ftrace_lock
);
3580 mutex_unlock(&ops
->regex_lock
);
3582 free_ftrace_hash(hash
);
3587 ftrace_set_addr(struct ftrace_ops
*ops
, unsigned long ip
, int remove
,
3588 int reset
, int enable
)
3590 return ftrace_set_hash(ops
, 0, 0, ip
, remove
, reset
, enable
);
3594 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3595 * @ops - the ops to set the filter with
3596 * @ip - the address to add to or remove from the filter.
3597 * @remove - non zero to remove the ip from the filter
3598 * @reset - non zero to reset all filters before applying this filter.
3600 * Filters denote which functions should be enabled when tracing is enabled
3601 * If @ip is NULL, it failes to update filter.
3603 int ftrace_set_filter_ip(struct ftrace_ops
*ops
, unsigned long ip
,
3604 int remove
, int reset
)
3606 ftrace_ops_init(ops
);
3607 return ftrace_set_addr(ops
, ip
, remove
, reset
, 1);
3609 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip
);
3612 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3613 int reset
, int enable
)
3615 return ftrace_set_hash(ops
, buf
, len
, 0, 0, reset
, enable
);
3619 * ftrace_set_filter - set a function to filter on in ftrace
3620 * @ops - the ops to set the filter with
3621 * @buf - the string that holds the function filter text.
3622 * @len - the length of the string.
3623 * @reset - non zero to reset all filters before applying this filter.
3625 * Filters denote which functions should be enabled when tracing is enabled.
3626 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3628 int ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
3631 ftrace_ops_init(ops
);
3632 return ftrace_set_regex(ops
, buf
, len
, reset
, 1);
3634 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
3637 * ftrace_set_notrace - set a function to not trace in ftrace
3638 * @ops - the ops to set the notrace filter with
3639 * @buf - the string that holds the function notrace text.
3640 * @len - the length of the string.
3641 * @reset - non zero to reset all filters before applying this filter.
3643 * Notrace Filters denote which functions should not be enabled when tracing
3644 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3647 int ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
3650 ftrace_ops_init(ops
);
3651 return ftrace_set_regex(ops
, buf
, len
, reset
, 0);
3653 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
3655 * ftrace_set_filter - set a function to filter on in ftrace
3656 * @ops - the ops to set the filter with
3657 * @buf - the string that holds the function filter text.
3658 * @len - the length of the string.
3659 * @reset - non zero to reset all filters before applying this filter.
3661 * Filters denote which functions should be enabled when tracing is enabled.
3662 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3664 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
3666 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
3668 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
3671 * ftrace_set_notrace - set a function to not trace in ftrace
3672 * @ops - the ops to set the notrace filter with
3673 * @buf - the string that holds the function notrace text.
3674 * @len - the length of the string.
3675 * @reset - non zero to reset all filters before applying this filter.
3677 * Notrace Filters denote which functions should not be enabled when tracing
3678 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3681 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
3683 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
3685 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
3688 * command line interface to allow users to set filters on boot up.
3690 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3691 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3692 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3694 /* Used by function selftest to not test if filter is set */
3695 bool ftrace_filter_param __initdata
;
3697 static int __init
set_ftrace_notrace(char *str
)
3699 ftrace_filter_param
= true;
3700 strlcpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
3703 __setup("ftrace_notrace=", set_ftrace_notrace
);
3705 static int __init
set_ftrace_filter(char *str
)
3707 ftrace_filter_param
= true;
3708 strlcpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
3711 __setup("ftrace_filter=", set_ftrace_filter
);
3713 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3714 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3715 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
3717 static int __init
set_graph_function(char *str
)
3719 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
3722 __setup("ftrace_graph_filter=", set_graph_function
);
3724 static void __init
set_ftrace_early_graph(char *buf
)
3730 func
= strsep(&buf
, ",");
3731 /* we allow only one expression at a time */
3732 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3735 printk(KERN_DEBUG
"ftrace: function %s not "
3736 "traceable\n", func
);
3739 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3742 ftrace_set_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
3746 ftrace_ops_init(ops
);
3749 func
= strsep(&buf
, ",");
3750 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
3754 static void __init
set_ftrace_early_filters(void)
3756 if (ftrace_filter_buf
[0])
3757 ftrace_set_early_filter(&global_ops
, ftrace_filter_buf
, 1);
3758 if (ftrace_notrace_buf
[0])
3759 ftrace_set_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
3760 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3761 if (ftrace_graph_buf
[0])
3762 set_ftrace_early_graph(ftrace_graph_buf
);
3763 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3766 int ftrace_regex_release(struct inode
*inode
, struct file
*file
)
3768 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
3769 struct ftrace_iterator
*iter
;
3770 struct ftrace_hash
**orig_hash
;
3771 struct trace_parser
*parser
;
3775 if (file
->f_mode
& FMODE_READ
) {
3777 seq_release(inode
, file
);
3779 iter
= file
->private_data
;
3781 parser
= &iter
->parser
;
3782 if (trace_parser_loaded(parser
)) {
3783 parser
->buffer
[parser
->idx
] = 0;
3784 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
3787 trace_parser_put(parser
);
3789 mutex_lock(&iter
->ops
->regex_lock
);
3791 if (file
->f_mode
& FMODE_WRITE
) {
3792 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
3795 orig_hash
= &iter
->ops
->filter_hash
;
3797 orig_hash
= &iter
->ops
->notrace_hash
;
3799 mutex_lock(&ftrace_lock
);
3800 ret
= ftrace_hash_move(iter
->ops
, filter_hash
,
3801 orig_hash
, iter
->hash
);
3803 ftrace_ops_update_code(iter
->ops
);
3805 mutex_unlock(&ftrace_lock
);
3808 mutex_unlock(&iter
->ops
->regex_lock
);
3809 free_ftrace_hash(iter
->hash
);
3815 static const struct file_operations ftrace_avail_fops
= {
3816 .open
= ftrace_avail_open
,
3818 .llseek
= seq_lseek
,
3819 .release
= seq_release_private
,
3822 static const struct file_operations ftrace_enabled_fops
= {
3823 .open
= ftrace_enabled_open
,
3825 .llseek
= seq_lseek
,
3826 .release
= seq_release_private
,
3829 static const struct file_operations ftrace_filter_fops
= {
3830 .open
= ftrace_filter_open
,
3832 .write
= ftrace_filter_write
,
3833 .llseek
= ftrace_filter_lseek
,
3834 .release
= ftrace_regex_release
,
3837 static const struct file_operations ftrace_notrace_fops
= {
3838 .open
= ftrace_notrace_open
,
3840 .write
= ftrace_notrace_write
,
3841 .llseek
= ftrace_filter_lseek
,
3842 .release
= ftrace_regex_release
,
3845 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3847 static DEFINE_MUTEX(graph_lock
);
3849 int ftrace_graph_count
;
3850 int ftrace_graph_filter_enabled
;
3851 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
3854 __g_next(struct seq_file
*m
, loff_t
*pos
)
3856 if (*pos
>= ftrace_graph_count
)
3858 return &ftrace_graph_funcs
[*pos
];
3862 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3865 return __g_next(m
, pos
);
3868 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
3870 mutex_lock(&graph_lock
);
3872 /* Nothing, tell g_show to print all functions are enabled */
3873 if (!ftrace_graph_filter_enabled
&& !*pos
)
3876 return __g_next(m
, pos
);
3879 static void g_stop(struct seq_file
*m
, void *p
)
3881 mutex_unlock(&graph_lock
);
3884 static int g_show(struct seq_file
*m
, void *v
)
3886 unsigned long *ptr
= v
;
3891 if (ptr
== (unsigned long *)1) {
3892 seq_printf(m
, "#### all functions enabled ####\n");
3896 seq_printf(m
, "%ps\n", (void *)*ptr
);
3901 static const struct seq_operations ftrace_graph_seq_ops
= {
3909 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
3913 if (unlikely(ftrace_disabled
))
3916 mutex_lock(&graph_lock
);
3917 if ((file
->f_mode
& FMODE_WRITE
) &&
3918 (file
->f_flags
& O_TRUNC
)) {
3919 ftrace_graph_filter_enabled
= 0;
3920 ftrace_graph_count
= 0;
3921 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
3923 mutex_unlock(&graph_lock
);
3925 if (file
->f_mode
& FMODE_READ
)
3926 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
3932 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
3934 if (file
->f_mode
& FMODE_READ
)
3935 seq_release(inode
, file
);
3940 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
3942 struct dyn_ftrace
*rec
;
3943 struct ftrace_page
*pg
;
3952 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
3953 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3956 search_len
= strlen(search
);
3958 mutex_lock(&ftrace_lock
);
3960 if (unlikely(ftrace_disabled
)) {
3961 mutex_unlock(&ftrace_lock
);
3965 do_for_each_ftrace_rec(pg
, rec
) {
3967 if (ftrace_match_record(rec
, NULL
, search
, search_len
, type
)) {
3968 /* if it is in the array */
3970 for (i
= 0; i
< *idx
; i
++) {
3971 if (array
[i
] == rec
->ip
) {
3980 array
[(*idx
)++] = rec
->ip
;
3981 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3986 array
[i
] = array
[--(*idx
)];
3992 } while_for_each_ftrace_rec();
3994 mutex_unlock(&ftrace_lock
);
3999 ftrace_graph_filter_enabled
= !!(*idx
);
4005 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
4006 size_t cnt
, loff_t
*ppos
)
4008 struct trace_parser parser
;
4014 mutex_lock(&graph_lock
);
4016 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
4021 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
4023 if (read
>= 0 && trace_parser_loaded((&parser
))) {
4024 parser
.buffer
[parser
.idx
] = 0;
4026 /* we allow only one expression at a time */
4027 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
4036 trace_parser_put(&parser
);
4038 mutex_unlock(&graph_lock
);
4043 static const struct file_operations ftrace_graph_fops
= {
4044 .open
= ftrace_graph_open
,
4046 .write
= ftrace_graph_write
,
4047 .llseek
= ftrace_filter_lseek
,
4048 .release
= ftrace_graph_release
,
4050 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4052 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
4055 trace_create_file("available_filter_functions", 0444,
4056 d_tracer
, NULL
, &ftrace_avail_fops
);
4058 trace_create_file("enabled_functions", 0444,
4059 d_tracer
, NULL
, &ftrace_enabled_fops
);
4061 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
4062 NULL
, &ftrace_filter_fops
);
4064 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
4065 NULL
, &ftrace_notrace_fops
);
4067 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4068 trace_create_file("set_graph_function", 0444, d_tracer
,
4070 &ftrace_graph_fops
);
4071 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4076 static int ftrace_cmp_ips(const void *a
, const void *b
)
4078 const unsigned long *ipa
= a
;
4079 const unsigned long *ipb
= b
;
4088 static void ftrace_swap_ips(void *a
, void *b
, int size
)
4090 unsigned long *ipa
= a
;
4091 unsigned long *ipb
= b
;
4099 static int ftrace_process_locs(struct module
*mod
,
4100 unsigned long *start
,
4103 struct ftrace_page
*start_pg
;
4104 struct ftrace_page
*pg
;
4105 struct dyn_ftrace
*rec
;
4106 unsigned long count
;
4109 unsigned long flags
= 0; /* Shut up gcc */
4112 count
= end
- start
;
4117 sort(start
, count
, sizeof(*start
),
4118 ftrace_cmp_ips
, ftrace_swap_ips
);
4120 start_pg
= ftrace_allocate_pages(count
);
4124 mutex_lock(&ftrace_lock
);
4127 * Core and each module needs their own pages, as
4128 * modules will free them when they are removed.
4129 * Force a new page to be allocated for modules.
4132 WARN_ON(ftrace_pages
|| ftrace_pages_start
);
4133 /* First initialization */
4134 ftrace_pages
= ftrace_pages_start
= start_pg
;
4139 if (WARN_ON(ftrace_pages
->next
)) {
4140 /* Hmm, we have free pages? */
4141 while (ftrace_pages
->next
)
4142 ftrace_pages
= ftrace_pages
->next
;
4145 ftrace_pages
->next
= start_pg
;
4151 addr
= ftrace_call_adjust(*p
++);
4153 * Some architecture linkers will pad between
4154 * the different mcount_loc sections of different
4155 * object files to satisfy alignments.
4156 * Skip any NULL pointers.
4161 if (pg
->index
== pg
->size
) {
4162 /* We should have allocated enough */
4163 if (WARN_ON(!pg
->next
))
4168 rec
= &pg
->records
[pg
->index
++];
4172 /* We should have used all pages */
4175 /* Assign the last page to ftrace_pages */
4178 /* These new locations need to be initialized */
4179 ftrace_new_pgs
= start_pg
;
4182 * We only need to disable interrupts on start up
4183 * because we are modifying code that an interrupt
4184 * may execute, and the modification is not atomic.
4185 * But for modules, nothing runs the code we modify
4186 * until we are finished with it, and there's no
4187 * reason to cause large interrupt latencies while we do it.
4190 local_irq_save(flags
);
4191 ftrace_update_code(mod
);
4193 local_irq_restore(flags
);
4196 mutex_unlock(&ftrace_lock
);
4201 #ifdef CONFIG_MODULES
4203 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4205 void ftrace_release_mod(struct module
*mod
)
4207 struct dyn_ftrace
*rec
;
4208 struct ftrace_page
**last_pg
;
4209 struct ftrace_page
*pg
;
4212 mutex_lock(&ftrace_lock
);
4214 if (ftrace_disabled
)
4218 * Each module has its own ftrace_pages, remove
4219 * them from the list.
4221 last_pg
= &ftrace_pages_start
;
4222 for (pg
= ftrace_pages_start
; pg
; pg
= *last_pg
) {
4223 rec
= &pg
->records
[0];
4224 if (within_module_core(rec
->ip
, mod
)) {
4226 * As core pages are first, the first
4227 * page should never be a module page.
4229 if (WARN_ON(pg
== ftrace_pages_start
))
4232 /* Check if we are deleting the last page */
4233 if (pg
== ftrace_pages
)
4234 ftrace_pages
= next_to_ftrace_page(last_pg
);
4236 *last_pg
= pg
->next
;
4237 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
4238 free_pages((unsigned long)pg
->records
, order
);
4241 last_pg
= &pg
->next
;
4244 mutex_unlock(&ftrace_lock
);
4247 static void ftrace_init_module(struct module
*mod
,
4248 unsigned long *start
, unsigned long *end
)
4250 if (ftrace_disabled
|| start
== end
)
4252 ftrace_process_locs(mod
, start
, end
);
4255 void ftrace_module_init(struct module
*mod
)
4257 ftrace_init_module(mod
, mod
->ftrace_callsites
,
4258 mod
->ftrace_callsites
+
4259 mod
->num_ftrace_callsites
);
4262 static int ftrace_module_notify_exit(struct notifier_block
*self
,
4263 unsigned long val
, void *data
)
4265 struct module
*mod
= data
;
4267 if (val
== MODULE_STATE_GOING
)
4268 ftrace_release_mod(mod
);
4273 static int ftrace_module_notify_exit(struct notifier_block
*self
,
4274 unsigned long val
, void *data
)
4278 #endif /* CONFIG_MODULES */
4280 struct notifier_block ftrace_module_exit_nb
= {
4281 .notifier_call
= ftrace_module_notify_exit
,
4282 .priority
= INT_MIN
, /* Run after anything that can remove kprobes */
4285 extern unsigned long __start_mcount_loc
[];
4286 extern unsigned long __stop_mcount_loc
[];
4288 void __init
ftrace_init(void)
4290 unsigned long count
, addr
, flags
;
4293 /* Keep the ftrace pointer to the stub */
4294 addr
= (unsigned long)ftrace_stub
;
4296 local_irq_save(flags
);
4297 ftrace_dyn_arch_init(&addr
);
4298 local_irq_restore(flags
);
4300 /* ftrace_dyn_arch_init places the return code in addr */
4304 count
= __stop_mcount_loc
- __start_mcount_loc
;
4306 ret
= ftrace_dyn_table_alloc(count
);
4310 last_ftrace_enabled
= ftrace_enabled
= 1;
4312 ret
= ftrace_process_locs(NULL
,
4316 ret
= register_module_notifier(&ftrace_module_exit_nb
);
4318 pr_warning("Failed to register trace ftrace module exit notifier\n");
4320 set_ftrace_early_filters();
4324 ftrace_disabled
= 1;
4329 static struct ftrace_ops global_ops
= {
4330 .func
= ftrace_stub
,
4331 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
4332 INIT_REGEX_LOCK(global_ops
)
4335 static int __init
ftrace_nodyn_init(void)
4340 core_initcall(ftrace_nodyn_init
);
4342 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
4343 static inline void ftrace_startup_enable(int command
) { }
4344 /* Keep as macros so we do not need to define the commands */
4345 # define ftrace_startup(ops, command) \
4347 int ___ret = __register_ftrace_function(ops); \
4349 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4352 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4354 # define ftrace_startup_sysctl() do { } while (0)
4355 # define ftrace_shutdown_sysctl() do { } while (0)
4358 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
4363 #endif /* CONFIG_DYNAMIC_FTRACE */
4366 ftrace_ops_control_func(unsigned long ip
, unsigned long parent_ip
,
4367 struct ftrace_ops
*op
, struct pt_regs
*regs
)
4369 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT
)))
4373 * Some of the ops may be dynamically allocated,
4374 * they must be freed after a synchronize_sched().
4376 preempt_disable_notrace();
4377 trace_recursion_set(TRACE_CONTROL_BIT
);
4378 do_for_each_ftrace_op(op
, ftrace_control_list
) {
4379 if (!(op
->flags
& FTRACE_OPS_FL_STUB
) &&
4380 !ftrace_function_local_disabled(op
) &&
4381 ftrace_ops_test(op
, ip
, regs
))
4382 op
->func(ip
, parent_ip
, op
, regs
);
4383 } while_for_each_ftrace_op(op
);
4384 trace_recursion_clear(TRACE_CONTROL_BIT
);
4385 preempt_enable_notrace();
4388 static struct ftrace_ops control_ops
= {
4389 .func
= ftrace_ops_control_func
,
4390 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
4391 INIT_REGEX_LOCK(control_ops
)
4395 __ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
4396 struct ftrace_ops
*ignored
, struct pt_regs
*regs
)
4398 struct ftrace_ops
*op
;
4401 if (function_trace_stop
)
4404 bit
= trace_test_and_set_recursion(TRACE_LIST_START
, TRACE_LIST_MAX
);
4409 * Some of the ops may be dynamically allocated,
4410 * they must be freed after a synchronize_sched().
4412 preempt_disable_notrace();
4413 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
4414 if (ftrace_ops_test(op
, ip
, regs
))
4415 op
->func(ip
, parent_ip
, op
, regs
);
4416 } while_for_each_ftrace_op(op
);
4417 preempt_enable_notrace();
4418 trace_clear_recursion(bit
);
4422 * Some archs only support passing ip and parent_ip. Even though
4423 * the list function ignores the op parameter, we do not want any
4424 * C side effects, where a function is called without the caller
4425 * sending a third parameter.
4426 * Archs are to support both the regs and ftrace_ops at the same time.
4427 * If they support ftrace_ops, it is assumed they support regs.
4428 * If call backs want to use regs, they must either check for regs
4429 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4430 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4431 * An architecture can pass partial regs with ftrace_ops and still
4432 * set the ARCH_SUPPORT_FTARCE_OPS.
4434 #if ARCH_SUPPORTS_FTRACE_OPS
4435 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
4436 struct ftrace_ops
*op
, struct pt_regs
*regs
)
4438 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, regs
);
4441 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
)
4443 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, NULL
);
4447 static void clear_ftrace_swapper(void)
4449 struct task_struct
*p
;
4453 for_each_online_cpu(cpu
) {
4455 clear_tsk_trace_trace(p
);
4460 static void set_ftrace_swapper(void)
4462 struct task_struct
*p
;
4466 for_each_online_cpu(cpu
) {
4468 set_tsk_trace_trace(p
);
4473 static void clear_ftrace_pid(struct pid
*pid
)
4475 struct task_struct
*p
;
4478 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
4479 clear_tsk_trace_trace(p
);
4480 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
4486 static void set_ftrace_pid(struct pid
*pid
)
4488 struct task_struct
*p
;
4491 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
4492 set_tsk_trace_trace(p
);
4493 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
4497 static void clear_ftrace_pid_task(struct pid
*pid
)
4499 if (pid
== ftrace_swapper_pid
)
4500 clear_ftrace_swapper();
4502 clear_ftrace_pid(pid
);
4505 static void set_ftrace_pid_task(struct pid
*pid
)
4507 if (pid
== ftrace_swapper_pid
)
4508 set_ftrace_swapper();
4510 set_ftrace_pid(pid
);
4513 static int ftrace_pid_add(int p
)
4516 struct ftrace_pid
*fpid
;
4519 mutex_lock(&ftrace_lock
);
4522 pid
= ftrace_swapper_pid
;
4524 pid
= find_get_pid(p
);
4531 list_for_each_entry(fpid
, &ftrace_pids
, list
)
4532 if (fpid
->pid
== pid
)
4537 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
4541 list_add(&fpid
->list
, &ftrace_pids
);
4544 set_ftrace_pid_task(pid
);
4546 ftrace_update_pid_func();
4547 ftrace_startup_enable(0);
4549 mutex_unlock(&ftrace_lock
);
4553 if (pid
!= ftrace_swapper_pid
)
4557 mutex_unlock(&ftrace_lock
);
4561 static void ftrace_pid_reset(void)
4563 struct ftrace_pid
*fpid
, *safe
;
4565 mutex_lock(&ftrace_lock
);
4566 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
4567 struct pid
*pid
= fpid
->pid
;
4569 clear_ftrace_pid_task(pid
);
4571 list_del(&fpid
->list
);
4575 ftrace_update_pid_func();
4576 ftrace_startup_enable(0);
4578 mutex_unlock(&ftrace_lock
);
4581 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
4583 mutex_lock(&ftrace_lock
);
4585 if (list_empty(&ftrace_pids
) && (!*pos
))
4588 return seq_list_start(&ftrace_pids
, *pos
);
4591 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
4596 return seq_list_next(v
, &ftrace_pids
, pos
);
4599 static void fpid_stop(struct seq_file
*m
, void *p
)
4601 mutex_unlock(&ftrace_lock
);
4604 static int fpid_show(struct seq_file
*m
, void *v
)
4606 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
4608 if (v
== (void *)1) {
4609 seq_printf(m
, "no pid\n");
4613 if (fpid
->pid
== ftrace_swapper_pid
)
4614 seq_printf(m
, "swapper tasks\n");
4616 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
4621 static const struct seq_operations ftrace_pid_sops
= {
4622 .start
= fpid_start
,
4629 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
4633 if ((file
->f_mode
& FMODE_WRITE
) &&
4634 (file
->f_flags
& O_TRUNC
))
4637 if (file
->f_mode
& FMODE_READ
)
4638 ret
= seq_open(file
, &ftrace_pid_sops
);
4644 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
4645 size_t cnt
, loff_t
*ppos
)
4651 if (cnt
>= sizeof(buf
))
4654 if (copy_from_user(&buf
, ubuf
, cnt
))
4660 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4661 * to clean the filter quietly.
4663 tmp
= strstrip(buf
);
4664 if (strlen(tmp
) == 0)
4667 ret
= kstrtol(tmp
, 10, &val
);
4671 ret
= ftrace_pid_add(val
);
4673 return ret
? ret
: cnt
;
4677 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
4679 if (file
->f_mode
& FMODE_READ
)
4680 seq_release(inode
, file
);
4685 static const struct file_operations ftrace_pid_fops
= {
4686 .open
= ftrace_pid_open
,
4687 .write
= ftrace_pid_write
,
4689 .llseek
= ftrace_filter_lseek
,
4690 .release
= ftrace_pid_release
,
4693 static __init
int ftrace_init_debugfs(void)
4695 struct dentry
*d_tracer
;
4697 d_tracer
= tracing_init_dentry();
4701 ftrace_init_dyn_debugfs(d_tracer
);
4703 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
4704 NULL
, &ftrace_pid_fops
);
4706 ftrace_profile_debugfs(d_tracer
);
4710 fs_initcall(ftrace_init_debugfs
);
4713 * ftrace_kill - kill ftrace
4715 * This function should be used by panic code. It stops ftrace
4716 * but in a not so nice way. If you need to simply kill ftrace
4717 * from a non-atomic section, use ftrace_kill.
4719 void ftrace_kill(void)
4721 ftrace_disabled
= 1;
4723 clear_ftrace_function();
4727 * Test if ftrace is dead or not.
4729 int ftrace_is_dead(void)
4731 return ftrace_disabled
;
4735 * register_ftrace_function - register a function for profiling
4736 * @ops - ops structure that holds the function for profiling.
4738 * Register a function to be called by all functions in the
4741 * Note: @ops->func and all the functions it calls must be labeled
4742 * with "notrace", otherwise it will go into a
4745 int register_ftrace_function(struct ftrace_ops
*ops
)
4749 ftrace_ops_init(ops
);
4751 mutex_lock(&ftrace_lock
);
4753 ret
= ftrace_startup(ops
, 0);
4755 mutex_unlock(&ftrace_lock
);
4759 EXPORT_SYMBOL_GPL(register_ftrace_function
);
4762 * unregister_ftrace_function - unregister a function for profiling.
4763 * @ops - ops structure that holds the function to unregister
4765 * Unregister a function that was added to be called by ftrace profiling.
4767 int unregister_ftrace_function(struct ftrace_ops
*ops
)
4771 mutex_lock(&ftrace_lock
);
4772 ret
= ftrace_shutdown(ops
, 0);
4773 mutex_unlock(&ftrace_lock
);
4777 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
4780 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
4781 void __user
*buffer
, size_t *lenp
,
4786 mutex_lock(&ftrace_lock
);
4788 if (unlikely(ftrace_disabled
))
4791 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
4793 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
4796 last_ftrace_enabled
= !!ftrace_enabled
;
4798 if (ftrace_enabled
) {
4800 ftrace_startup_sysctl();
4802 /* we are starting ftrace again */
4803 if (ftrace_ops_list
!= &ftrace_list_end
)
4804 update_ftrace_function();
4807 /* stopping ftrace calls (just send to ftrace_stub) */
4808 ftrace_trace_function
= ftrace_stub
;
4810 ftrace_shutdown_sysctl();
4814 mutex_unlock(&ftrace_lock
);
4818 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4820 static int ftrace_graph_active
;
4821 static struct notifier_block ftrace_suspend_notifier
;
4823 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
4828 /* The callbacks that hook a function */
4829 trace_func_graph_ret_t ftrace_graph_return
=
4830 (trace_func_graph_ret_t
)ftrace_stub
;
4831 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
4832 static trace_func_graph_ent_t __ftrace_graph_entry
= ftrace_graph_entry_stub
;
4834 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4835 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
4839 unsigned long flags
;
4840 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
4841 struct task_struct
*g
, *t
;
4843 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
4844 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
4845 * sizeof(struct ftrace_ret_stack
),
4847 if (!ret_stack_list
[i
]) {
4855 read_lock_irqsave(&tasklist_lock
, flags
);
4856 do_each_thread(g
, t
) {
4862 if (t
->ret_stack
== NULL
) {
4863 atomic_set(&t
->tracing_graph_pause
, 0);
4864 atomic_set(&t
->trace_overrun
, 0);
4865 t
->curr_ret_stack
= -1;
4866 /* Make sure the tasks see the -1 first: */
4868 t
->ret_stack
= ret_stack_list
[start
++];
4870 } while_each_thread(g
, t
);
4873 read_unlock_irqrestore(&tasklist_lock
, flags
);
4875 for (i
= start
; i
< end
; i
++)
4876 kfree(ret_stack_list
[i
]);
4881 ftrace_graph_probe_sched_switch(void *ignore
,
4882 struct task_struct
*prev
, struct task_struct
*next
)
4884 unsigned long long timestamp
;
4888 * Does the user want to count the time a function was asleep.
4889 * If so, do not update the time stamps.
4891 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
4894 timestamp
= trace_clock_local();
4896 prev
->ftrace_timestamp
= timestamp
;
4898 /* only process tasks that we timestamped */
4899 if (!next
->ftrace_timestamp
)
4903 * Update all the counters in next to make up for the
4904 * time next was sleeping.
4906 timestamp
-= next
->ftrace_timestamp
;
4908 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
4909 next
->ret_stack
[index
].calltime
+= timestamp
;
4912 /* Allocate a return stack for each task */
4913 static int start_graph_tracing(void)
4915 struct ftrace_ret_stack
**ret_stack_list
;
4918 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
4919 sizeof(struct ftrace_ret_stack
*),
4922 if (!ret_stack_list
)
4925 /* The cpu_boot init_task->ret_stack will never be freed */
4926 for_each_online_cpu(cpu
) {
4927 if (!idle_task(cpu
)->ret_stack
)
4928 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
4932 ret
= alloc_retstack_tasklist(ret_stack_list
);
4933 } while (ret
== -EAGAIN
);
4936 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4938 pr_info("ftrace_graph: Couldn't activate tracepoint"
4939 " probe to kernel_sched_switch\n");
4942 kfree(ret_stack_list
);
4947 * Hibernation protection.
4948 * The state of the current task is too much unstable during
4949 * suspend/restore to disk. We want to protect against that.
4952 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
4956 case PM_HIBERNATION_PREPARE
:
4957 pause_graph_tracing();
4960 case PM_POST_HIBERNATION
:
4961 unpause_graph_tracing();
4967 /* Just a place holder for function graph */
4968 static struct ftrace_ops fgraph_ops __read_mostly
= {
4969 .func
= ftrace_stub
,
4970 .flags
= FTRACE_OPS_FL_STUB
| FTRACE_OPS_FL_GLOBAL
|
4971 FTRACE_OPS_FL_RECURSION_SAFE
,
4974 static int ftrace_graph_entry_test(struct ftrace_graph_ent
*trace
)
4976 if (!ftrace_ops_test(&global_ops
, trace
->func
, NULL
))
4978 return __ftrace_graph_entry(trace
);
4982 * The function graph tracer should only trace the functions defined
4983 * by set_ftrace_filter and set_ftrace_notrace. If another function
4984 * tracer ops is registered, the graph tracer requires testing the
4985 * function against the global ops, and not just trace any function
4986 * that any ftrace_ops registered.
4988 static void update_function_graph_func(void)
4990 if (ftrace_ops_list
== &ftrace_list_end
||
4991 (ftrace_ops_list
== &global_ops
&&
4992 global_ops
.next
== &ftrace_list_end
))
4993 ftrace_graph_entry
= __ftrace_graph_entry
;
4995 ftrace_graph_entry
= ftrace_graph_entry_test
;
4998 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
4999 trace_func_graph_ent_t entryfunc
)
5003 mutex_lock(&ftrace_lock
);
5005 /* we currently allow only one tracer registered at a time */
5006 if (ftrace_graph_active
) {
5011 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
5012 register_pm_notifier(&ftrace_suspend_notifier
);
5014 ftrace_graph_active
++;
5015 ret
= start_graph_tracing();
5017 ftrace_graph_active
--;
5021 ftrace_graph_return
= retfunc
;
5024 * Update the indirect function to the entryfunc, and the
5025 * function that gets called to the entry_test first. Then
5026 * call the update fgraph entry function to determine if
5027 * the entryfunc should be called directly or not.
5029 __ftrace_graph_entry
= entryfunc
;
5030 ftrace_graph_entry
= ftrace_graph_entry_test
;
5031 update_function_graph_func();
5033 ret
= ftrace_startup(&fgraph_ops
, FTRACE_START_FUNC_RET
);
5036 mutex_unlock(&ftrace_lock
);
5040 void unregister_ftrace_graph(void)
5042 mutex_lock(&ftrace_lock
);
5044 if (unlikely(!ftrace_graph_active
))
5047 ftrace_graph_active
--;
5048 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
5049 ftrace_graph_entry
= ftrace_graph_entry_stub
;
5050 __ftrace_graph_entry
= ftrace_graph_entry_stub
;
5051 ftrace_shutdown(&fgraph_ops
, FTRACE_STOP_FUNC_RET
);
5052 unregister_pm_notifier(&ftrace_suspend_notifier
);
5053 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
5056 mutex_unlock(&ftrace_lock
);
5059 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
5062 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
5064 atomic_set(&t
->tracing_graph_pause
, 0);
5065 atomic_set(&t
->trace_overrun
, 0);
5066 t
->ftrace_timestamp
= 0;
5067 /* make curr_ret_stack visible before we add the ret_stack */
5069 t
->ret_stack
= ret_stack
;
5073 * Allocate a return stack for the idle task. May be the first
5074 * time through, or it may be done by CPU hotplug online.
5076 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
5078 t
->curr_ret_stack
= -1;
5080 * The idle task has no parent, it either has its own
5081 * stack or no stack at all.
5084 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
5086 if (ftrace_graph_active
) {
5087 struct ftrace_ret_stack
*ret_stack
;
5089 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
5091 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
5092 * sizeof(struct ftrace_ret_stack
),
5096 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
5098 graph_init_task(t
, ret_stack
);
5102 /* Allocate a return stack for newly created task */
5103 void ftrace_graph_init_task(struct task_struct
*t
)
5105 /* Make sure we do not use the parent ret_stack */
5106 t
->ret_stack
= NULL
;
5107 t
->curr_ret_stack
= -1;
5109 if (ftrace_graph_active
) {
5110 struct ftrace_ret_stack
*ret_stack
;
5112 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
5113 * sizeof(struct ftrace_ret_stack
),
5117 graph_init_task(t
, ret_stack
);
5121 void ftrace_graph_exit_task(struct task_struct
*t
)
5123 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
5125 t
->ret_stack
= NULL
;
5126 /* NULL must become visible to IRQs before we free it: */
5132 void ftrace_graph_stop(void)