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 William Lee Irwin III
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 /* ftrace_enabled is a method to turn ftrace on or off */
66 int ftrace_enabled __read_mostly
;
67 static int last_ftrace_enabled
;
69 /* Quick disabling of function tracer. */
70 int function_trace_stop
;
72 /* List for set_ftrace_pid's pids. */
73 LIST_HEAD(ftrace_pids
);
75 struct list_head list
;
80 * ftrace_disabled is set when an anomaly is discovered.
81 * ftrace_disabled is much stronger than ftrace_enabled.
83 static int ftrace_disabled __read_mostly
;
85 static DEFINE_MUTEX(ftrace_lock
);
87 static struct ftrace_ops ftrace_list_end __read_mostly
= {
91 static struct ftrace_ops
*ftrace_global_list __read_mostly
= &ftrace_list_end
;
92 static struct ftrace_ops
*ftrace_ops_list __read_mostly
= &ftrace_list_end
;
93 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
94 static ftrace_func_t __ftrace_trace_function_delay __read_mostly
= ftrace_stub
;
95 ftrace_func_t __ftrace_trace_function __read_mostly
= ftrace_stub
;
96 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
97 static struct ftrace_ops global_ops
;
100 ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
);
103 * Traverse the ftrace_global_list, invoking all entries. The reason that we
104 * can use rcu_dereference_raw() is that elements removed from this list
105 * are simply leaked, so there is no need to interact with a grace-period
106 * mechanism. The rcu_dereference_raw() calls are needed to handle
107 * concurrent insertions into the ftrace_global_list.
109 * Silly Alpha and silly pointer-speculation compiler optimizations!
111 static void ftrace_global_list_func(unsigned long ip
,
112 unsigned long parent_ip
)
114 struct ftrace_ops
*op
;
116 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT
)))
119 trace_recursion_set(TRACE_GLOBAL_BIT
);
120 op
= rcu_dereference_raw(ftrace_global_list
); /*see above*/
121 while (op
!= &ftrace_list_end
) {
122 op
->func(ip
, parent_ip
);
123 op
= rcu_dereference_raw(op
->next
); /*see above*/
125 trace_recursion_clear(TRACE_GLOBAL_BIT
);
128 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
)
130 if (!test_tsk_trace_trace(current
))
133 ftrace_pid_function(ip
, parent_ip
);
136 static void set_ftrace_pid_function(ftrace_func_t func
)
138 /* do not set ftrace_pid_function to itself! */
139 if (func
!= ftrace_pid_func
)
140 ftrace_pid_function
= func
;
144 * clear_ftrace_function - reset the ftrace function
146 * This NULLs the ftrace function and in essence stops
147 * tracing. There may be lag
149 void clear_ftrace_function(void)
151 ftrace_trace_function
= ftrace_stub
;
152 __ftrace_trace_function
= ftrace_stub
;
153 __ftrace_trace_function_delay
= ftrace_stub
;
154 ftrace_pid_function
= ftrace_stub
;
157 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
159 * For those archs that do not test ftrace_trace_stop in their
160 * mcount call site, we need to do it from C.
162 static void ftrace_test_stop_func(unsigned long ip
, unsigned long parent_ip
)
164 if (function_trace_stop
)
167 __ftrace_trace_function(ip
, parent_ip
);
171 static void update_global_ops(void)
176 * If there's only one function registered, then call that
177 * function directly. Otherwise, we need to iterate over the
178 * registered callers.
180 if (ftrace_global_list
== &ftrace_list_end
||
181 ftrace_global_list
->next
== &ftrace_list_end
)
182 func
= ftrace_global_list
->func
;
184 func
= ftrace_global_list_func
;
186 /* If we filter on pids, update to use the pid function */
187 if (!list_empty(&ftrace_pids
)) {
188 set_ftrace_pid_function(func
);
189 func
= ftrace_pid_func
;
192 global_ops
.func
= func
;
195 static void update_ftrace_function(void)
202 * If we are at the end of the list and this ops is
203 * not dynamic, then have the mcount trampoline call
204 * the function directly
206 if (ftrace_ops_list
== &ftrace_list_end
||
207 (ftrace_ops_list
->next
== &ftrace_list_end
&&
208 !(ftrace_ops_list
->flags
& FTRACE_OPS_FL_DYNAMIC
)))
209 func
= ftrace_ops_list
->func
;
211 func
= ftrace_ops_list_func
;
213 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
214 ftrace_trace_function
= func
;
216 #ifdef CONFIG_DYNAMIC_FTRACE
217 /* do not update till all functions have been modified */
218 __ftrace_trace_function_delay
= func
;
220 __ftrace_trace_function
= func
;
222 ftrace_trace_function
= ftrace_test_stop_func
;
226 static void add_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
230 * We are entering ops into the list but another
231 * CPU might be walking that list. We need to make sure
232 * the ops->next pointer is valid before another CPU sees
233 * the ops pointer included into the list.
235 rcu_assign_pointer(*list
, ops
);
238 static int remove_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
240 struct ftrace_ops
**p
;
243 * If we are removing the last function, then simply point
244 * to the ftrace_stub.
246 if (*list
== ops
&& ops
->next
== &ftrace_list_end
) {
247 *list
= &ftrace_list_end
;
251 for (p
= list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
262 static int __register_ftrace_function(struct ftrace_ops
*ops
)
267 if (FTRACE_WARN_ON(ops
== &global_ops
))
270 if (WARN_ON(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
273 if (!core_kernel_data((unsigned long)ops
))
274 ops
->flags
|= FTRACE_OPS_FL_DYNAMIC
;
276 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
277 int first
= ftrace_global_list
== &ftrace_list_end
;
278 add_ftrace_ops(&ftrace_global_list
, ops
);
279 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
281 add_ftrace_ops(&ftrace_ops_list
, &global_ops
);
283 add_ftrace_ops(&ftrace_ops_list
, ops
);
286 update_ftrace_function();
291 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
298 if (WARN_ON(!(ops
->flags
& FTRACE_OPS_FL_ENABLED
)))
301 if (FTRACE_WARN_ON(ops
== &global_ops
))
304 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
305 ret
= remove_ftrace_ops(&ftrace_global_list
, ops
);
306 if (!ret
&& ftrace_global_list
== &ftrace_list_end
)
307 ret
= remove_ftrace_ops(&ftrace_ops_list
, &global_ops
);
309 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
311 ret
= remove_ftrace_ops(&ftrace_ops_list
, ops
);
317 update_ftrace_function();
320 * Dynamic ops may be freed, we must make sure that all
321 * callers are done before leaving this function.
323 if (ops
->flags
& FTRACE_OPS_FL_DYNAMIC
)
329 static void ftrace_update_pid_func(void)
331 /* Only do something if we are tracing something */
332 if (ftrace_trace_function
== ftrace_stub
)
335 update_ftrace_function();
338 #ifdef CONFIG_FUNCTION_PROFILER
339 struct ftrace_profile
{
340 struct hlist_node node
;
342 unsigned long counter
;
343 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
344 unsigned long long time
;
345 unsigned long long time_squared
;
349 struct ftrace_profile_page
{
350 struct ftrace_profile_page
*next
;
352 struct ftrace_profile records
[];
355 struct ftrace_profile_stat
{
357 struct hlist_head
*hash
;
358 struct ftrace_profile_page
*pages
;
359 struct ftrace_profile_page
*start
;
360 struct tracer_stat stat
;
363 #define PROFILE_RECORDS_SIZE \
364 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
366 #define PROFILES_PER_PAGE \
367 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
369 static int ftrace_profile_bits __read_mostly
;
370 static int ftrace_profile_enabled __read_mostly
;
372 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
373 static DEFINE_MUTEX(ftrace_profile_lock
);
375 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
377 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
380 function_stat_next(void *v
, int idx
)
382 struct ftrace_profile
*rec
= v
;
383 struct ftrace_profile_page
*pg
;
385 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
391 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
395 rec
= &pg
->records
[0];
403 static void *function_stat_start(struct tracer_stat
*trace
)
405 struct ftrace_profile_stat
*stat
=
406 container_of(trace
, struct ftrace_profile_stat
, stat
);
408 if (!stat
|| !stat
->start
)
411 return function_stat_next(&stat
->start
->records
[0], 0);
414 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
415 /* function graph compares on total time */
416 static int function_stat_cmp(void *p1
, void *p2
)
418 struct ftrace_profile
*a
= p1
;
419 struct ftrace_profile
*b
= p2
;
421 if (a
->time
< b
->time
)
423 if (a
->time
> b
->time
)
429 /* not function graph compares against hits */
430 static int function_stat_cmp(void *p1
, void *p2
)
432 struct ftrace_profile
*a
= p1
;
433 struct ftrace_profile
*b
= p2
;
435 if (a
->counter
< b
->counter
)
437 if (a
->counter
> b
->counter
)
444 static int function_stat_headers(struct seq_file
*m
)
446 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
447 seq_printf(m
, " Function "
450 "--- ---- --- ---\n");
452 seq_printf(m
, " Function Hit\n"
458 static int function_stat_show(struct seq_file
*m
, void *v
)
460 struct ftrace_profile
*rec
= v
;
461 char str
[KSYM_SYMBOL_LEN
];
463 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
464 static struct trace_seq s
;
465 unsigned long long avg
;
466 unsigned long long stddev
;
468 mutex_lock(&ftrace_profile_lock
);
470 /* we raced with function_profile_reset() */
471 if (unlikely(rec
->counter
== 0)) {
476 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
477 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
479 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
482 do_div(avg
, rec
->counter
);
484 /* Sample standard deviation (s^2) */
485 if (rec
->counter
<= 1)
488 stddev
= rec
->time_squared
- rec
->counter
* avg
* avg
;
490 * Divide only 1000 for ns^2 -> us^2 conversion.
491 * trace_print_graph_duration will divide 1000 again.
493 do_div(stddev
, (rec
->counter
- 1) * 1000);
497 trace_print_graph_duration(rec
->time
, &s
);
498 trace_seq_puts(&s
, " ");
499 trace_print_graph_duration(avg
, &s
);
500 trace_seq_puts(&s
, " ");
501 trace_print_graph_duration(stddev
, &s
);
502 trace_print_seq(m
, &s
);
506 mutex_unlock(&ftrace_profile_lock
);
511 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
513 struct ftrace_profile_page
*pg
;
515 pg
= stat
->pages
= stat
->start
;
518 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
523 memset(stat
->hash
, 0,
524 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
527 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
529 struct ftrace_profile_page
*pg
;
534 /* If we already allocated, do nothing */
538 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
542 #ifdef CONFIG_DYNAMIC_FTRACE
543 functions
= ftrace_update_tot_cnt
;
546 * We do not know the number of functions that exist because
547 * dynamic tracing is what counts them. With past experience
548 * we have around 20K functions. That should be more than enough.
549 * It is highly unlikely we will execute every function in
555 pg
= stat
->start
= stat
->pages
;
557 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
559 for (i
= 0; i
< pages
; i
++) {
560 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
571 unsigned long tmp
= (unsigned long)pg
;
577 free_page((unsigned long)stat
->pages
);
584 static int ftrace_profile_init_cpu(int cpu
)
586 struct ftrace_profile_stat
*stat
;
589 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
592 /* If the profile is already created, simply reset it */
593 ftrace_profile_reset(stat
);
598 * We are profiling all functions, but usually only a few thousand
599 * functions are hit. We'll make a hash of 1024 items.
601 size
= FTRACE_PROFILE_HASH_SIZE
;
603 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
608 if (!ftrace_profile_bits
) {
611 for (; size
; size
>>= 1)
612 ftrace_profile_bits
++;
615 /* Preallocate the function profiling pages */
616 if (ftrace_profile_pages_init(stat
) < 0) {
625 static int ftrace_profile_init(void)
630 for_each_online_cpu(cpu
) {
631 ret
= ftrace_profile_init_cpu(cpu
);
639 /* interrupts must be disabled */
640 static struct ftrace_profile
*
641 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
643 struct ftrace_profile
*rec
;
644 struct hlist_head
*hhd
;
645 struct hlist_node
*n
;
648 key
= hash_long(ip
, ftrace_profile_bits
);
649 hhd
= &stat
->hash
[key
];
651 if (hlist_empty(hhd
))
654 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
662 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
663 struct ftrace_profile
*rec
)
667 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
668 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
672 * The memory is already allocated, this simply finds a new record to use.
674 static struct ftrace_profile
*
675 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
677 struct ftrace_profile
*rec
= NULL
;
679 /* prevent recursion (from NMIs) */
680 if (atomic_inc_return(&stat
->disabled
) != 1)
684 * Try to find the function again since an NMI
685 * could have added it
687 rec
= ftrace_find_profiled_func(stat
, ip
);
691 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
692 if (!stat
->pages
->next
)
694 stat
->pages
= stat
->pages
->next
;
697 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
699 ftrace_add_profile(stat
, rec
);
702 atomic_dec(&stat
->disabled
);
708 function_profile_call(unsigned long ip
, unsigned long parent_ip
)
710 struct ftrace_profile_stat
*stat
;
711 struct ftrace_profile
*rec
;
714 if (!ftrace_profile_enabled
)
717 local_irq_save(flags
);
719 stat
= &__get_cpu_var(ftrace_profile_stats
);
720 if (!stat
->hash
|| !ftrace_profile_enabled
)
723 rec
= ftrace_find_profiled_func(stat
, ip
);
725 rec
= ftrace_profile_alloc(stat
, ip
);
732 local_irq_restore(flags
);
735 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
736 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
738 function_profile_call(trace
->func
, 0);
742 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
744 struct ftrace_profile_stat
*stat
;
745 unsigned long long calltime
;
746 struct ftrace_profile
*rec
;
749 local_irq_save(flags
);
750 stat
= &__get_cpu_var(ftrace_profile_stats
);
751 if (!stat
->hash
|| !ftrace_profile_enabled
)
754 /* If the calltime was zero'd ignore it */
755 if (!trace
->calltime
)
758 calltime
= trace
->rettime
- trace
->calltime
;
760 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
763 index
= trace
->depth
;
765 /* Append this call time to the parent time to subtract */
767 current
->ret_stack
[index
- 1].subtime
+= calltime
;
769 if (current
->ret_stack
[index
].subtime
< calltime
)
770 calltime
-= current
->ret_stack
[index
].subtime
;
775 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
777 rec
->time
+= calltime
;
778 rec
->time_squared
+= calltime
* calltime
;
782 local_irq_restore(flags
);
785 static int register_ftrace_profiler(void)
787 return register_ftrace_graph(&profile_graph_return
,
788 &profile_graph_entry
);
791 static void unregister_ftrace_profiler(void)
793 unregister_ftrace_graph();
796 static struct ftrace_ops ftrace_profile_ops __read_mostly
= {
797 .func
= function_profile_call
,
800 static int register_ftrace_profiler(void)
802 return register_ftrace_function(&ftrace_profile_ops
);
805 static void unregister_ftrace_profiler(void)
807 unregister_ftrace_function(&ftrace_profile_ops
);
809 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
812 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
813 size_t cnt
, loff_t
*ppos
)
818 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
824 mutex_lock(&ftrace_profile_lock
);
825 if (ftrace_profile_enabled
^ val
) {
827 ret
= ftrace_profile_init();
833 ret
= register_ftrace_profiler();
838 ftrace_profile_enabled
= 1;
840 ftrace_profile_enabled
= 0;
842 * unregister_ftrace_profiler calls stop_machine
843 * so this acts like an synchronize_sched.
845 unregister_ftrace_profiler();
849 mutex_unlock(&ftrace_profile_lock
);
857 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
858 size_t cnt
, loff_t
*ppos
)
860 char buf
[64]; /* big enough to hold a number */
863 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
864 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
867 static const struct file_operations ftrace_profile_fops
= {
868 .open
= tracing_open_generic
,
869 .read
= ftrace_profile_read
,
870 .write
= ftrace_profile_write
,
871 .llseek
= default_llseek
,
874 /* used to initialize the real stat files */
875 static struct tracer_stat function_stats __initdata
= {
877 .stat_start
= function_stat_start
,
878 .stat_next
= function_stat_next
,
879 .stat_cmp
= function_stat_cmp
,
880 .stat_headers
= function_stat_headers
,
881 .stat_show
= function_stat_show
884 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
886 struct ftrace_profile_stat
*stat
;
887 struct dentry
*entry
;
892 for_each_possible_cpu(cpu
) {
893 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
895 /* allocate enough for function name + cpu number */
896 name
= kmalloc(32, GFP_KERNEL
);
899 * The files created are permanent, if something happens
900 * we still do not free memory.
903 "Could not allocate stat file for cpu %d\n",
907 stat
->stat
= function_stats
;
908 snprintf(name
, 32, "function%d", cpu
);
909 stat
->stat
.name
= name
;
910 ret
= register_stat_tracer(&stat
->stat
);
913 "Could not register function stat for cpu %d\n",
920 entry
= debugfs_create_file("function_profile_enabled", 0644,
921 d_tracer
, NULL
, &ftrace_profile_fops
);
923 pr_warning("Could not create debugfs "
924 "'function_profile_enabled' entry\n");
927 #else /* CONFIG_FUNCTION_PROFILER */
928 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
931 #endif /* CONFIG_FUNCTION_PROFILER */
933 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
935 #ifdef CONFIG_DYNAMIC_FTRACE
937 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
938 # error Dynamic ftrace depends on MCOUNT_RECORD
941 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
943 struct ftrace_func_probe
{
944 struct hlist_node node
;
945 struct ftrace_probe_ops
*ops
;
952 struct ftrace_func_entry
{
953 struct hlist_node hlist
;
958 unsigned long size_bits
;
959 struct hlist_head
*buckets
;
965 * We make these constant because no one should touch them,
966 * but they are used as the default "empty hash", to avoid allocating
967 * it all the time. These are in a read only section such that if
968 * anyone does try to modify it, it will cause an exception.
970 static const struct hlist_head empty_buckets
[1];
971 static const struct ftrace_hash empty_hash
= {
972 .buckets
= (struct hlist_head
*)empty_buckets
,
974 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
976 static struct ftrace_ops global_ops
= {
978 .notrace_hash
= EMPTY_HASH
,
979 .filter_hash
= EMPTY_HASH
,
982 static DEFINE_MUTEX(ftrace_regex_lock
);
985 struct ftrace_page
*next
;
986 struct dyn_ftrace
*records
;
991 static struct ftrace_page
*ftrace_new_pgs
;
993 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
994 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
996 /* estimate from running different kernels */
997 #define NR_TO_INIT 10000
999 static struct ftrace_page
*ftrace_pages_start
;
1000 static struct ftrace_page
*ftrace_pages
;
1002 static bool ftrace_hash_empty(struct ftrace_hash
*hash
)
1004 return !hash
|| !hash
->count
;
1007 static struct ftrace_func_entry
*
1008 ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1011 struct ftrace_func_entry
*entry
;
1012 struct hlist_head
*hhd
;
1013 struct hlist_node
*n
;
1015 if (ftrace_hash_empty(hash
))
1018 if (hash
->size_bits
> 0)
1019 key
= hash_long(ip
, hash
->size_bits
);
1023 hhd
= &hash
->buckets
[key
];
1025 hlist_for_each_entry_rcu(entry
, n
, hhd
, hlist
) {
1026 if (entry
->ip
== ip
)
1032 static void __add_hash_entry(struct ftrace_hash
*hash
,
1033 struct ftrace_func_entry
*entry
)
1035 struct hlist_head
*hhd
;
1038 if (hash
->size_bits
)
1039 key
= hash_long(entry
->ip
, hash
->size_bits
);
1043 hhd
= &hash
->buckets
[key
];
1044 hlist_add_head(&entry
->hlist
, hhd
);
1048 static int add_hash_entry(struct ftrace_hash
*hash
, unsigned long ip
)
1050 struct ftrace_func_entry
*entry
;
1052 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1057 __add_hash_entry(hash
, entry
);
1063 free_hash_entry(struct ftrace_hash
*hash
,
1064 struct ftrace_func_entry
*entry
)
1066 hlist_del(&entry
->hlist
);
1072 remove_hash_entry(struct ftrace_hash
*hash
,
1073 struct ftrace_func_entry
*entry
)
1075 hlist_del(&entry
->hlist
);
1079 static void ftrace_hash_clear(struct ftrace_hash
*hash
)
1081 struct hlist_head
*hhd
;
1082 struct hlist_node
*tp
, *tn
;
1083 struct ftrace_func_entry
*entry
;
1084 int size
= 1 << hash
->size_bits
;
1090 for (i
= 0; i
< size
; i
++) {
1091 hhd
= &hash
->buckets
[i
];
1092 hlist_for_each_entry_safe(entry
, tp
, tn
, hhd
, hlist
)
1093 free_hash_entry(hash
, entry
);
1095 FTRACE_WARN_ON(hash
->count
);
1098 static void free_ftrace_hash(struct ftrace_hash
*hash
)
1100 if (!hash
|| hash
== EMPTY_HASH
)
1102 ftrace_hash_clear(hash
);
1103 kfree(hash
->buckets
);
1107 static void __free_ftrace_hash_rcu(struct rcu_head
*rcu
)
1109 struct ftrace_hash
*hash
;
1111 hash
= container_of(rcu
, struct ftrace_hash
, rcu
);
1112 free_ftrace_hash(hash
);
1115 static void free_ftrace_hash_rcu(struct ftrace_hash
*hash
)
1117 if (!hash
|| hash
== EMPTY_HASH
)
1119 call_rcu_sched(&hash
->rcu
, __free_ftrace_hash_rcu
);
1122 static struct ftrace_hash
*alloc_ftrace_hash(int size_bits
)
1124 struct ftrace_hash
*hash
;
1127 hash
= kzalloc(sizeof(*hash
), GFP_KERNEL
);
1131 size
= 1 << size_bits
;
1132 hash
->buckets
= kzalloc(sizeof(*hash
->buckets
) * size
, GFP_KERNEL
);
1134 if (!hash
->buckets
) {
1139 hash
->size_bits
= size_bits
;
1144 static struct ftrace_hash
*
1145 alloc_and_copy_ftrace_hash(int size_bits
, struct ftrace_hash
*hash
)
1147 struct ftrace_func_entry
*entry
;
1148 struct ftrace_hash
*new_hash
;
1149 struct hlist_node
*tp
;
1154 new_hash
= alloc_ftrace_hash(size_bits
);
1159 if (ftrace_hash_empty(hash
))
1162 size
= 1 << hash
->size_bits
;
1163 for (i
= 0; i
< size
; i
++) {
1164 hlist_for_each_entry(entry
, tp
, &hash
->buckets
[i
], hlist
) {
1165 ret
= add_hash_entry(new_hash
, entry
->ip
);
1171 FTRACE_WARN_ON(new_hash
->count
!= hash
->count
);
1176 free_ftrace_hash(new_hash
);
1181 ftrace_hash_rec_disable(struct ftrace_ops
*ops
, int filter_hash
);
1183 ftrace_hash_rec_enable(struct ftrace_ops
*ops
, int filter_hash
);
1186 ftrace_hash_move(struct ftrace_ops
*ops
, int enable
,
1187 struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1189 struct ftrace_func_entry
*entry
;
1190 struct hlist_node
*tp
, *tn
;
1191 struct hlist_head
*hhd
;
1192 struct ftrace_hash
*old_hash
;
1193 struct ftrace_hash
*new_hash
;
1195 int size
= src
->count
;
1201 * Remove the current set, update the hash and add
1204 ftrace_hash_rec_disable(ops
, enable
);
1207 * If the new source is empty, just free dst and assign it
1211 free_ftrace_hash_rcu(*dst
);
1212 rcu_assign_pointer(*dst
, EMPTY_HASH
);
1213 /* still need to update the function records */
1219 * Make the hash size about 1/2 the # found
1221 for (size
/= 2; size
; size
>>= 1)
1224 /* Don't allocate too much */
1225 if (bits
> FTRACE_HASH_MAX_BITS
)
1226 bits
= FTRACE_HASH_MAX_BITS
;
1229 new_hash
= alloc_ftrace_hash(bits
);
1233 size
= 1 << src
->size_bits
;
1234 for (i
= 0; i
< size
; i
++) {
1235 hhd
= &src
->buckets
[i
];
1236 hlist_for_each_entry_safe(entry
, tp
, tn
, hhd
, hlist
) {
1238 key
= hash_long(entry
->ip
, bits
);
1241 remove_hash_entry(src
, entry
);
1242 __add_hash_entry(new_hash
, entry
);
1247 rcu_assign_pointer(*dst
, new_hash
);
1248 free_ftrace_hash_rcu(old_hash
);
1253 * Enable regardless of ret:
1254 * On success, we enable the new hash.
1255 * On failure, we re-enable the original hash.
1257 ftrace_hash_rec_enable(ops
, enable
);
1263 * Test the hashes for this ops to see if we want to call
1264 * the ops->func or not.
1266 * It's a match if the ip is in the ops->filter_hash or
1267 * the filter_hash does not exist or is empty,
1269 * the ip is not in the ops->notrace_hash.
1271 * This needs to be called with preemption disabled as
1272 * the hashes are freed with call_rcu_sched().
1275 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
)
1277 struct ftrace_hash
*filter_hash
;
1278 struct ftrace_hash
*notrace_hash
;
1281 filter_hash
= rcu_dereference_raw(ops
->filter_hash
);
1282 notrace_hash
= rcu_dereference_raw(ops
->notrace_hash
);
1284 if ((ftrace_hash_empty(filter_hash
) ||
1285 ftrace_lookup_ip(filter_hash
, ip
)) &&
1286 (ftrace_hash_empty(notrace_hash
) ||
1287 !ftrace_lookup_ip(notrace_hash
, ip
)))
1296 * This is a double for. Do not use 'break' to break out of the loop,
1297 * you must use a goto.
1299 #define do_for_each_ftrace_rec(pg, rec) \
1300 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1302 for (_____i = 0; _____i < pg->index; _____i++) { \
1303 rec = &pg->records[_____i];
1305 #define while_for_each_ftrace_rec() \
1310 static int ftrace_cmp_recs(const void *a
, const void *b
)
1312 const struct dyn_ftrace
*reca
= a
;
1313 const struct dyn_ftrace
*recb
= b
;
1315 if (reca
->ip
> recb
->ip
)
1317 if (reca
->ip
< recb
->ip
)
1323 * ftrace_location - return true if the ip giving is a traced location
1324 * @ip: the instruction pointer to check
1326 * Returns 1 if @ip given is a pointer to a ftrace location.
1327 * That is, the instruction that is either a NOP or call to
1328 * the function tracer. It checks the ftrace internal tables to
1329 * determine if the address belongs or not.
1331 int ftrace_location(unsigned long ip
)
1333 struct ftrace_page
*pg
;
1334 struct dyn_ftrace
*rec
;
1335 struct dyn_ftrace key
;
1339 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
1340 rec
= bsearch(&key
, pg
->records
, pg
->index
,
1341 sizeof(struct dyn_ftrace
),
1350 static void __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1354 struct ftrace_hash
*hash
;
1355 struct ftrace_hash
*other_hash
;
1356 struct ftrace_page
*pg
;
1357 struct dyn_ftrace
*rec
;
1361 /* Only update if the ops has been registered */
1362 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1366 * In the filter_hash case:
1367 * If the count is zero, we update all records.
1368 * Otherwise we just update the items in the hash.
1370 * In the notrace_hash case:
1371 * We enable the update in the hash.
1372 * As disabling notrace means enabling the tracing,
1373 * and enabling notrace means disabling, the inc variable
1377 hash
= ops
->filter_hash
;
1378 other_hash
= ops
->notrace_hash
;
1379 if (ftrace_hash_empty(hash
))
1383 hash
= ops
->notrace_hash
;
1384 other_hash
= ops
->filter_hash
;
1386 * If the notrace hash has no items,
1387 * then there's nothing to do.
1389 if (ftrace_hash_empty(hash
))
1393 do_for_each_ftrace_rec(pg
, rec
) {
1394 int in_other_hash
= 0;
1400 * Only the filter_hash affects all records.
1401 * Update if the record is not in the notrace hash.
1403 if (!other_hash
|| !ftrace_lookup_ip(other_hash
, rec
->ip
))
1406 in_hash
= !!ftrace_lookup_ip(hash
, rec
->ip
);
1407 in_other_hash
= !!ftrace_lookup_ip(other_hash
, rec
->ip
);
1412 if (filter_hash
&& in_hash
&& !in_other_hash
)
1414 else if (!filter_hash
&& in_hash
&&
1415 (in_other_hash
|| ftrace_hash_empty(other_hash
)))
1423 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == FTRACE_REF_MAX
))
1426 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == 0))
1431 /* Shortcut, if we handled all records, we are done. */
1432 if (!all
&& count
== hash
->count
)
1434 } while_for_each_ftrace_rec();
1437 static void ftrace_hash_rec_disable(struct ftrace_ops
*ops
,
1440 __ftrace_hash_rec_update(ops
, filter_hash
, 0);
1443 static void ftrace_hash_rec_enable(struct ftrace_ops
*ops
,
1446 __ftrace_hash_rec_update(ops
, filter_hash
, 1);
1449 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
1451 if (ftrace_pages
->index
== ftrace_pages
->size
) {
1452 /* We should have allocated enough */
1453 if (WARN_ON(!ftrace_pages
->next
))
1455 ftrace_pages
= ftrace_pages
->next
;
1458 return &ftrace_pages
->records
[ftrace_pages
->index
++];
1461 static struct dyn_ftrace
*
1462 ftrace_record_ip(unsigned long ip
)
1464 struct dyn_ftrace
*rec
;
1466 if (ftrace_disabled
)
1469 rec
= ftrace_alloc_dyn_node(ip
);
1478 static void print_ip_ins(const char *fmt
, unsigned char *p
)
1482 printk(KERN_CONT
"%s", fmt
);
1484 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
1485 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
1489 * ftrace_bug - report and shutdown function tracer
1490 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1491 * @ip: The address that failed
1493 * The arch code that enables or disables the function tracing
1494 * can call ftrace_bug() when it has detected a problem in
1495 * modifying the code. @failed should be one of either:
1496 * EFAULT - if the problem happens on reading the @ip address
1497 * EINVAL - if what is read at @ip is not what was expected
1498 * EPERM - if the problem happens on writting to the @ip address
1500 void ftrace_bug(int failed
, unsigned long ip
)
1504 FTRACE_WARN_ON_ONCE(1);
1505 pr_info("ftrace faulted on modifying ");
1509 FTRACE_WARN_ON_ONCE(1);
1510 pr_info("ftrace failed to modify ");
1512 print_ip_ins(" actual: ", (unsigned char *)ip
);
1513 printk(KERN_CONT
"\n");
1516 FTRACE_WARN_ON_ONCE(1);
1517 pr_info("ftrace faulted on writing ");
1521 FTRACE_WARN_ON_ONCE(1);
1522 pr_info("ftrace faulted on unknown error ");
1528 /* Return 1 if the address range is reserved for ftrace */
1529 int ftrace_text_reserved(void *start
, void *end
)
1531 struct dyn_ftrace
*rec
;
1532 struct ftrace_page
*pg
;
1534 do_for_each_ftrace_rec(pg
, rec
) {
1535 if (rec
->ip
<= (unsigned long)end
&&
1536 rec
->ip
+ MCOUNT_INSN_SIZE
> (unsigned long)start
)
1538 } while_for_each_ftrace_rec();
1542 static int ftrace_check_record(struct dyn_ftrace
*rec
, int enable
, int update
)
1544 unsigned long flag
= 0UL;
1547 * If we are updating calls:
1549 * If the record has a ref count, then we need to enable it
1550 * because someone is using it.
1552 * Otherwise we make sure its disabled.
1554 * If we are disabling calls, then disable all records that
1557 if (enable
&& (rec
->flags
& ~FTRACE_FL_MASK
))
1558 flag
= FTRACE_FL_ENABLED
;
1560 /* If the state of this record hasn't changed, then do nothing */
1561 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1562 return FTRACE_UPDATE_IGNORE
;
1566 rec
->flags
|= FTRACE_FL_ENABLED
;
1567 return FTRACE_UPDATE_MAKE_CALL
;
1571 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1573 return FTRACE_UPDATE_MAKE_NOP
;
1577 * ftrace_update_record, set a record that now is tracing or not
1578 * @rec: the record to update
1579 * @enable: set to 1 if the record is tracing, zero to force disable
1581 * The records that represent all functions that can be traced need
1582 * to be updated when tracing has been enabled.
1584 int ftrace_update_record(struct dyn_ftrace
*rec
, int enable
)
1586 return ftrace_check_record(rec
, enable
, 1);
1590 * ftrace_test_record, check if the record has been enabled or not
1591 * @rec: the record to test
1592 * @enable: set to 1 to check if enabled, 0 if it is disabled
1594 * The arch code may need to test if a record is already set to
1595 * tracing to determine how to modify the function code that it
1598 int ftrace_test_record(struct dyn_ftrace
*rec
, int enable
)
1600 return ftrace_check_record(rec
, enable
, 0);
1604 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1606 unsigned long ftrace_addr
;
1609 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1611 ret
= ftrace_update_record(rec
, enable
);
1614 case FTRACE_UPDATE_IGNORE
:
1617 case FTRACE_UPDATE_MAKE_CALL
:
1618 return ftrace_make_call(rec
, ftrace_addr
);
1620 case FTRACE_UPDATE_MAKE_NOP
:
1621 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1624 return -1; /* unknow ftrace bug */
1627 static void ftrace_replace_code(int update
)
1629 struct dyn_ftrace
*rec
;
1630 struct ftrace_page
*pg
;
1633 if (unlikely(ftrace_disabled
))
1636 do_for_each_ftrace_rec(pg
, rec
) {
1637 failed
= __ftrace_replace_code(rec
, update
);
1639 ftrace_bug(failed
, rec
->ip
);
1640 /* Stop processing */
1643 } while_for_each_ftrace_rec();
1646 struct ftrace_rec_iter
{
1647 struct ftrace_page
*pg
;
1652 * ftrace_rec_iter_start, start up iterating over traced functions
1654 * Returns an iterator handle that is used to iterate over all
1655 * the records that represent address locations where functions
1658 * May return NULL if no records are available.
1660 struct ftrace_rec_iter
*ftrace_rec_iter_start(void)
1663 * We only use a single iterator.
1664 * Protected by the ftrace_lock mutex.
1666 static struct ftrace_rec_iter ftrace_rec_iter
;
1667 struct ftrace_rec_iter
*iter
= &ftrace_rec_iter
;
1669 iter
->pg
= ftrace_pages_start
;
1672 /* Could have empty pages */
1673 while (iter
->pg
&& !iter
->pg
->index
)
1674 iter
->pg
= iter
->pg
->next
;
1683 * ftrace_rec_iter_next, get the next record to process.
1684 * @iter: The handle to the iterator.
1686 * Returns the next iterator after the given iterator @iter.
1688 struct ftrace_rec_iter
*ftrace_rec_iter_next(struct ftrace_rec_iter
*iter
)
1692 if (iter
->index
>= iter
->pg
->index
) {
1693 iter
->pg
= iter
->pg
->next
;
1696 /* Could have empty pages */
1697 while (iter
->pg
&& !iter
->pg
->index
)
1698 iter
->pg
= iter
->pg
->next
;
1708 * ftrace_rec_iter_record, get the record at the iterator location
1709 * @iter: The current iterator location
1711 * Returns the record that the current @iter is at.
1713 struct dyn_ftrace
*ftrace_rec_iter_record(struct ftrace_rec_iter
*iter
)
1715 return &iter
->pg
->records
[iter
->index
];
1719 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1726 if (unlikely(ftrace_disabled
))
1729 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1731 ftrace_bug(ret
, ip
);
1738 * archs can override this function if they must do something
1739 * before the modifying code is performed.
1741 int __weak
ftrace_arch_code_modify_prepare(void)
1747 * archs can override this function if they must do something
1748 * after the modifying code is performed.
1750 int __weak
ftrace_arch_code_modify_post_process(void)
1755 static int __ftrace_modify_code(void *data
)
1757 int *command
= data
;
1759 if (*command
& FTRACE_UPDATE_CALLS
)
1760 ftrace_replace_code(1);
1761 else if (*command
& FTRACE_DISABLE_CALLS
)
1762 ftrace_replace_code(0);
1764 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1765 ftrace_update_ftrace_func(ftrace_trace_function
);
1767 if (*command
& FTRACE_START_FUNC_RET
)
1768 ftrace_enable_ftrace_graph_caller();
1769 else if (*command
& FTRACE_STOP_FUNC_RET
)
1770 ftrace_disable_ftrace_graph_caller();
1776 * ftrace_run_stop_machine, go back to the stop machine method
1777 * @command: The command to tell ftrace what to do
1779 * If an arch needs to fall back to the stop machine method, the
1780 * it can call this function.
1782 void ftrace_run_stop_machine(int command
)
1784 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1788 * arch_ftrace_update_code, modify the code to trace or not trace
1789 * @command: The command that needs to be done
1791 * Archs can override this function if it does not need to
1792 * run stop_machine() to modify code.
1794 void __weak
arch_ftrace_update_code(int command
)
1796 ftrace_run_stop_machine(command
);
1799 static void ftrace_run_update_code(int command
)
1803 ret
= ftrace_arch_code_modify_prepare();
1804 FTRACE_WARN_ON(ret
);
1808 * Do not call function tracer while we update the code.
1809 * We are in stop machine.
1811 function_trace_stop
++;
1814 * By default we use stop_machine() to modify the code.
1815 * But archs can do what ever they want as long as it
1816 * is safe. The stop_machine() is the safest, but also
1817 * produces the most overhead.
1819 arch_ftrace_update_code(command
);
1821 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1823 * For archs that call ftrace_test_stop_func(), we must
1824 * wait till after we update all the function callers
1825 * before we update the callback. This keeps different
1826 * ops that record different functions from corrupting
1829 __ftrace_trace_function
= __ftrace_trace_function_delay
;
1831 function_trace_stop
--;
1833 ret
= ftrace_arch_code_modify_post_process();
1834 FTRACE_WARN_ON(ret
);
1837 static ftrace_func_t saved_ftrace_func
;
1838 static int ftrace_start_up
;
1839 static int global_start_up
;
1841 static void ftrace_startup_enable(int command
)
1843 if (saved_ftrace_func
!= ftrace_trace_function
) {
1844 saved_ftrace_func
= ftrace_trace_function
;
1845 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1848 if (!command
|| !ftrace_enabled
)
1851 ftrace_run_update_code(command
);
1854 static int ftrace_startup(struct ftrace_ops
*ops
, int command
)
1856 bool hash_enable
= true;
1858 if (unlikely(ftrace_disabled
))
1862 command
|= FTRACE_UPDATE_CALLS
;
1864 /* ops marked global share the filter hashes */
1865 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
1867 /* Don't update hash if global is already set */
1868 if (global_start_up
)
1869 hash_enable
= false;
1873 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
1875 ftrace_hash_rec_enable(ops
, 1);
1877 ftrace_startup_enable(command
);
1882 static void ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
1884 bool hash_disable
= true;
1886 if (unlikely(ftrace_disabled
))
1891 * Just warn in case of unbalance, no need to kill ftrace, it's not
1892 * critical but the ftrace_call callers may be never nopped again after
1893 * further ftrace uses.
1895 WARN_ON_ONCE(ftrace_start_up
< 0);
1897 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
1900 WARN_ON_ONCE(global_start_up
< 0);
1901 /* Don't update hash if global still has users */
1902 if (global_start_up
) {
1903 WARN_ON_ONCE(!ftrace_start_up
);
1904 hash_disable
= false;
1909 ftrace_hash_rec_disable(ops
, 1);
1911 if (ops
!= &global_ops
|| !global_start_up
)
1912 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
1914 command
|= FTRACE_UPDATE_CALLS
;
1916 if (saved_ftrace_func
!= ftrace_trace_function
) {
1917 saved_ftrace_func
= ftrace_trace_function
;
1918 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1921 if (!command
|| !ftrace_enabled
)
1924 ftrace_run_update_code(command
);
1927 static void ftrace_startup_sysctl(void)
1929 if (unlikely(ftrace_disabled
))
1932 /* Force update next time */
1933 saved_ftrace_func
= NULL
;
1934 /* ftrace_start_up is true if we want ftrace running */
1935 if (ftrace_start_up
)
1936 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
1939 static void ftrace_shutdown_sysctl(void)
1941 if (unlikely(ftrace_disabled
))
1944 /* ftrace_start_up is true if ftrace is running */
1945 if (ftrace_start_up
)
1946 ftrace_run_update_code(FTRACE_DISABLE_CALLS
);
1949 static cycle_t ftrace_update_time
;
1950 static unsigned long ftrace_update_cnt
;
1951 unsigned long ftrace_update_tot_cnt
;
1953 static int ops_traces_mod(struct ftrace_ops
*ops
)
1955 struct ftrace_hash
*hash
;
1957 hash
= ops
->filter_hash
;
1958 return ftrace_hash_empty(hash
);
1961 static int ftrace_update_code(struct module
*mod
)
1963 struct ftrace_page
*pg
;
1964 struct dyn_ftrace
*p
;
1965 cycle_t start
, stop
;
1966 unsigned long ref
= 0;
1970 * When adding a module, we need to check if tracers are
1971 * currently enabled and if they are set to trace all functions.
1972 * If they are, we need to enable the module functions as well
1973 * as update the reference counts for those function records.
1976 struct ftrace_ops
*ops
;
1978 for (ops
= ftrace_ops_list
;
1979 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
1980 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
&&
1981 ops_traces_mod(ops
))
1986 start
= ftrace_now(raw_smp_processor_id());
1987 ftrace_update_cnt
= 0;
1989 for (pg
= ftrace_new_pgs
; pg
; pg
= pg
->next
) {
1991 for (i
= 0; i
< pg
->index
; i
++) {
1992 /* If something went wrong, bail without enabling anything */
1993 if (unlikely(ftrace_disabled
))
1996 p
= &pg
->records
[i
];
2000 * Do the initial record conversion from mcount jump
2001 * to the NOP instructions.
2003 if (!ftrace_code_disable(mod
, p
))
2006 ftrace_update_cnt
++;
2009 * If the tracing is enabled, go ahead and enable the record.
2011 * The reason not to enable the record immediatelly is the
2012 * inherent check of ftrace_make_nop/ftrace_make_call for
2013 * correct previous instructions. Making first the NOP
2014 * conversion puts the module to the correct state, thus
2015 * passing the ftrace_make_call check.
2017 if (ftrace_start_up
&& ref
) {
2018 int failed
= __ftrace_replace_code(p
, 1);
2020 ftrace_bug(failed
, p
->ip
);
2025 ftrace_new_pgs
= NULL
;
2027 stop
= ftrace_now(raw_smp_processor_id());
2028 ftrace_update_time
= stop
- start
;
2029 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
2034 static int ftrace_allocate_records(struct ftrace_page
*pg
, int count
)
2039 if (WARN_ON(!count
))
2042 order
= get_count_order(DIV_ROUND_UP(count
, ENTRIES_PER_PAGE
));
2045 * We want to fill as much as possible. No more than a page
2048 while ((PAGE_SIZE
<< order
) / ENTRY_SIZE
>= count
+ ENTRIES_PER_PAGE
)
2052 pg
->records
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
2055 /* if we can't allocate this size, try something smaller */
2062 cnt
= (PAGE_SIZE
<< order
) / ENTRY_SIZE
;
2071 static struct ftrace_page
*
2072 ftrace_allocate_pages(unsigned long num_to_init
)
2074 struct ftrace_page
*start_pg
;
2075 struct ftrace_page
*pg
;
2082 start_pg
= pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2087 * Try to allocate as much as possible in one continues
2088 * location that fills in all of the space. We want to
2089 * waste as little space as possible.
2092 cnt
= ftrace_allocate_records(pg
, num_to_init
);
2100 pg
->next
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2111 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
2112 free_pages((unsigned long)pg
->records
, order
);
2113 start_pg
= pg
->next
;
2117 pr_info("ftrace: FAILED to allocate memory for functions\n");
2121 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
2126 pr_info("ftrace: No functions to be traced?\n");
2130 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
2131 pr_info("ftrace: allocating %ld entries in %d pages\n",
2132 num_to_init
, cnt
+ 1);
2137 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2139 struct ftrace_iterator
{
2142 struct ftrace_page
*pg
;
2143 struct dyn_ftrace
*func
;
2144 struct ftrace_func_probe
*probe
;
2145 struct trace_parser parser
;
2146 struct ftrace_hash
*hash
;
2147 struct ftrace_ops
*ops
;
2154 t_hash_next(struct seq_file
*m
, loff_t
*pos
)
2156 struct ftrace_iterator
*iter
= m
->private;
2157 struct hlist_node
*hnd
= NULL
;
2158 struct hlist_head
*hhd
;
2164 hnd
= &iter
->probe
->node
;
2166 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
2169 hhd
= &ftrace_func_hash
[iter
->hidx
];
2171 if (hlist_empty(hhd
)) {
2187 if (WARN_ON_ONCE(!hnd
))
2190 iter
->probe
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
2195 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
2197 struct ftrace_iterator
*iter
= m
->private;
2201 if (!(iter
->flags
& FTRACE_ITER_DO_HASH
))
2204 if (iter
->func_pos
> *pos
)
2208 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
2209 p
= t_hash_next(m
, &l
);
2216 /* Only set this if we have an item */
2217 iter
->flags
|= FTRACE_ITER_HASH
;
2223 t_hash_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
2225 struct ftrace_func_probe
*rec
;
2228 if (WARN_ON_ONCE(!rec
))
2231 if (rec
->ops
->print
)
2232 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
2234 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
2237 seq_printf(m
, ":%p", rec
->data
);
2244 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2246 struct ftrace_iterator
*iter
= m
->private;
2247 struct ftrace_ops
*ops
= iter
->ops
;
2248 struct dyn_ftrace
*rec
= NULL
;
2250 if (unlikely(ftrace_disabled
))
2253 if (iter
->flags
& FTRACE_ITER_HASH
)
2254 return t_hash_next(m
, pos
);
2257 iter
->pos
= iter
->func_pos
= *pos
;
2259 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
2260 return t_hash_start(m
, pos
);
2263 if (iter
->idx
>= iter
->pg
->index
) {
2264 if (iter
->pg
->next
) {
2265 iter
->pg
= iter
->pg
->next
;
2270 rec
= &iter
->pg
->records
[iter
->idx
++];
2271 if (((iter
->flags
& FTRACE_ITER_FILTER
) &&
2272 !(ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))) ||
2274 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
2275 !ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
)) ||
2277 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
2278 !(rec
->flags
& ~FTRACE_FL_MASK
))) {
2286 return t_hash_start(m
, pos
);
2293 static void reset_iter_read(struct ftrace_iterator
*iter
)
2297 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
& FTRACE_ITER_HASH
);
2300 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2302 struct ftrace_iterator
*iter
= m
->private;
2303 struct ftrace_ops
*ops
= iter
->ops
;
2307 mutex_lock(&ftrace_lock
);
2309 if (unlikely(ftrace_disabled
))
2313 * If an lseek was done, then reset and start from beginning.
2315 if (*pos
< iter
->pos
)
2316 reset_iter_read(iter
);
2319 * For set_ftrace_filter reading, if we have the filter
2320 * off, we can short cut and just print out that all
2321 * functions are enabled.
2323 if (iter
->flags
& FTRACE_ITER_FILTER
&&
2324 ftrace_hash_empty(ops
->filter_hash
)) {
2326 return t_hash_start(m
, pos
);
2327 iter
->flags
|= FTRACE_ITER_PRINTALL
;
2328 /* reset in case of seek/pread */
2329 iter
->flags
&= ~FTRACE_ITER_HASH
;
2333 if (iter
->flags
& FTRACE_ITER_HASH
)
2334 return t_hash_start(m
, pos
);
2337 * Unfortunately, we need to restart at ftrace_pages_start
2338 * every time we let go of the ftrace_mutex. This is because
2339 * those pointers can change without the lock.
2341 iter
->pg
= ftrace_pages_start
;
2343 for (l
= 0; l
<= *pos
; ) {
2344 p
= t_next(m
, p
, &l
);
2350 return t_hash_start(m
, pos
);
2355 static void t_stop(struct seq_file
*m
, void *p
)
2357 mutex_unlock(&ftrace_lock
);
2360 static int t_show(struct seq_file
*m
, void *v
)
2362 struct ftrace_iterator
*iter
= m
->private;
2363 struct dyn_ftrace
*rec
;
2365 if (iter
->flags
& FTRACE_ITER_HASH
)
2366 return t_hash_show(m
, iter
);
2368 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
2369 seq_printf(m
, "#### all functions enabled ####\n");
2378 seq_printf(m
, "%ps", (void *)rec
->ip
);
2379 if (iter
->flags
& FTRACE_ITER_ENABLED
)
2380 seq_printf(m
, " (%ld)",
2381 rec
->flags
& ~FTRACE_FL_MASK
);
2382 seq_printf(m
, "\n");
2387 static const struct seq_operations show_ftrace_seq_ops
= {
2395 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
2397 struct ftrace_iterator
*iter
;
2400 if (unlikely(ftrace_disabled
))
2403 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2407 iter
->pg
= ftrace_pages_start
;
2408 iter
->ops
= &global_ops
;
2410 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2412 struct seq_file
*m
= file
->private_data
;
2423 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
2425 struct ftrace_iterator
*iter
;
2428 if (unlikely(ftrace_disabled
))
2431 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2435 iter
->pg
= ftrace_pages_start
;
2436 iter
->flags
= FTRACE_ITER_ENABLED
;
2437 iter
->ops
= &global_ops
;
2439 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2441 struct seq_file
*m
= file
->private_data
;
2451 static void ftrace_filter_reset(struct ftrace_hash
*hash
)
2453 mutex_lock(&ftrace_lock
);
2454 ftrace_hash_clear(hash
);
2455 mutex_unlock(&ftrace_lock
);
2459 * ftrace_regex_open - initialize function tracer filter files
2460 * @ops: The ftrace_ops that hold the hash filters
2461 * @flag: The type of filter to process
2462 * @inode: The inode, usually passed in to your open routine
2463 * @file: The file, usually passed in to your open routine
2465 * ftrace_regex_open() initializes the filter files for the
2466 * @ops. Depending on @flag it may process the filter hash or
2467 * the notrace hash of @ops. With this called from the open
2468 * routine, you can use ftrace_filter_write() for the write
2469 * routine if @flag has FTRACE_ITER_FILTER set, or
2470 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2471 * ftrace_regex_lseek() should be used as the lseek routine, and
2472 * release must call ftrace_regex_release().
2475 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
2476 struct inode
*inode
, struct file
*file
)
2478 struct ftrace_iterator
*iter
;
2479 struct ftrace_hash
*hash
;
2482 if (unlikely(ftrace_disabled
))
2485 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2489 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
2494 if (flag
& FTRACE_ITER_NOTRACE
)
2495 hash
= ops
->notrace_hash
;
2497 hash
= ops
->filter_hash
;
2502 if (file
->f_mode
& FMODE_WRITE
) {
2503 mutex_lock(&ftrace_lock
);
2504 iter
->hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, hash
);
2505 mutex_unlock(&ftrace_lock
);
2508 trace_parser_put(&iter
->parser
);
2514 mutex_lock(&ftrace_regex_lock
);
2516 if ((file
->f_mode
& FMODE_WRITE
) &&
2517 (file
->f_flags
& O_TRUNC
))
2518 ftrace_filter_reset(iter
->hash
);
2520 if (file
->f_mode
& FMODE_READ
) {
2521 iter
->pg
= ftrace_pages_start
;
2523 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2525 struct seq_file
*m
= file
->private_data
;
2529 free_ftrace_hash(iter
->hash
);
2530 trace_parser_put(&iter
->parser
);
2534 file
->private_data
= iter
;
2535 mutex_unlock(&ftrace_regex_lock
);
2541 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
2543 return ftrace_regex_open(&global_ops
,
2544 FTRACE_ITER_FILTER
| FTRACE_ITER_DO_HASH
,
2549 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
2551 return ftrace_regex_open(&global_ops
, FTRACE_ITER_NOTRACE
,
2556 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
2560 if (file
->f_mode
& FMODE_READ
)
2561 ret
= seq_lseek(file
, offset
, origin
);
2563 file
->f_pos
= ret
= 1;
2568 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
2575 if (strcmp(str
, regex
) == 0)
2578 case MATCH_FRONT_ONLY
:
2579 if (strncmp(str
, regex
, len
) == 0)
2582 case MATCH_MIDDLE_ONLY
:
2583 if (strstr(str
, regex
))
2586 case MATCH_END_ONLY
:
2588 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
2597 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int not)
2599 struct ftrace_func_entry
*entry
;
2602 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
2604 /* Do nothing if it doesn't exist */
2608 free_hash_entry(hash
, entry
);
2610 /* Do nothing if it exists */
2614 ret
= add_hash_entry(hash
, rec
->ip
);
2620 ftrace_match_record(struct dyn_ftrace
*rec
, char *mod
,
2621 char *regex
, int len
, int type
)
2623 char str
[KSYM_SYMBOL_LEN
];
2626 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
2629 /* module lookup requires matching the module */
2630 if (!modname
|| strcmp(modname
, mod
))
2633 /* blank search means to match all funcs in the mod */
2638 return ftrace_match(str
, regex
, len
, type
);
2642 match_records(struct ftrace_hash
*hash
, char *buff
,
2643 int len
, char *mod
, int not)
2645 unsigned search_len
= 0;
2646 struct ftrace_page
*pg
;
2647 struct dyn_ftrace
*rec
;
2648 int type
= MATCH_FULL
;
2649 char *search
= buff
;
2654 type
= filter_parse_regex(buff
, len
, &search
, ¬);
2655 search_len
= strlen(search
);
2658 mutex_lock(&ftrace_lock
);
2660 if (unlikely(ftrace_disabled
))
2663 do_for_each_ftrace_rec(pg
, rec
) {
2664 if (ftrace_match_record(rec
, mod
, search
, search_len
, type
)) {
2665 ret
= enter_record(hash
, rec
, not);
2672 } while_for_each_ftrace_rec();
2674 mutex_unlock(&ftrace_lock
);
2680 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
2682 return match_records(hash
, buff
, len
, NULL
, 0);
2686 ftrace_match_module_records(struct ftrace_hash
*hash
, char *buff
, char *mod
)
2690 /* blank or '*' mean the same */
2691 if (strcmp(buff
, "*") == 0)
2694 /* handle the case of 'dont filter this module' */
2695 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
2700 return match_records(hash
, buff
, strlen(buff
), mod
, not);
2704 * We register the module command as a template to show others how
2705 * to register the a command as well.
2709 ftrace_mod_callback(struct ftrace_hash
*hash
,
2710 char *func
, char *cmd
, char *param
, int enable
)
2716 * cmd == 'mod' because we only registered this func
2717 * for the 'mod' ftrace_func_command.
2718 * But if you register one func with multiple commands,
2719 * you can tell which command was used by the cmd
2723 /* we must have a module name */
2727 mod
= strsep(¶m
, ":");
2731 ret
= ftrace_match_module_records(hash
, func
, mod
);
2740 static struct ftrace_func_command ftrace_mod_cmd
= {
2742 .func
= ftrace_mod_callback
,
2745 static int __init
ftrace_mod_cmd_init(void)
2747 return register_ftrace_command(&ftrace_mod_cmd
);
2749 device_initcall(ftrace_mod_cmd_init
);
2752 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
2754 struct ftrace_func_probe
*entry
;
2755 struct hlist_head
*hhd
;
2756 struct hlist_node
*n
;
2759 key
= hash_long(ip
, FTRACE_HASH_BITS
);
2761 hhd
= &ftrace_func_hash
[key
];
2763 if (hlist_empty(hhd
))
2767 * Disable preemption for these calls to prevent a RCU grace
2768 * period. This syncs the hash iteration and freeing of items
2769 * on the hash. rcu_read_lock is too dangerous here.
2771 preempt_disable_notrace();
2772 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
2773 if (entry
->ip
== ip
)
2774 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
2776 preempt_enable_notrace();
2779 static struct ftrace_ops trace_probe_ops __read_mostly
=
2781 .func
= function_trace_probe_call
,
2784 static int ftrace_probe_registered
;
2786 static void __enable_ftrace_function_probe(void)
2791 if (ftrace_probe_registered
)
2794 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2795 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2799 /* Nothing registered? */
2800 if (i
== FTRACE_FUNC_HASHSIZE
)
2803 ret
= __register_ftrace_function(&trace_probe_ops
);
2805 ret
= ftrace_startup(&trace_probe_ops
, 0);
2807 ftrace_probe_registered
= 1;
2810 static void __disable_ftrace_function_probe(void)
2815 if (!ftrace_probe_registered
)
2818 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2819 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2824 /* no more funcs left */
2825 ret
= __unregister_ftrace_function(&trace_probe_ops
);
2827 ftrace_shutdown(&trace_probe_ops
, 0);
2829 ftrace_probe_registered
= 0;
2833 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
2835 struct ftrace_func_probe
*entry
=
2836 container_of(rhp
, struct ftrace_func_probe
, rcu
);
2838 if (entry
->ops
->free
)
2839 entry
->ops
->free(&entry
->data
);
2845 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2848 struct ftrace_func_probe
*entry
;
2849 struct ftrace_page
*pg
;
2850 struct dyn_ftrace
*rec
;
2856 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2857 len
= strlen(search
);
2859 /* we do not support '!' for function probes */
2863 mutex_lock(&ftrace_lock
);
2865 if (unlikely(ftrace_disabled
))
2868 do_for_each_ftrace_rec(pg
, rec
) {
2870 if (!ftrace_match_record(rec
, NULL
, search
, len
, type
))
2873 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
2875 /* If we did not process any, then return error */
2886 * The caller might want to do something special
2887 * for each function we find. We call the callback
2888 * to give the caller an opportunity to do so.
2890 if (ops
->callback
) {
2891 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
2892 /* caller does not like this func */
2899 entry
->ip
= rec
->ip
;
2901 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2902 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2904 } while_for_each_ftrace_rec();
2905 __enable_ftrace_function_probe();
2908 mutex_unlock(&ftrace_lock
);
2914 PROBE_TEST_FUNC
= 1,
2919 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2920 void *data
, int flags
)
2922 struct ftrace_func_probe
*entry
;
2923 struct hlist_node
*n
, *tmp
;
2924 char str
[KSYM_SYMBOL_LEN
];
2925 int type
= MATCH_FULL
;
2929 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
2934 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2935 len
= strlen(search
);
2937 /* we do not support '!' for function probes */
2942 mutex_lock(&ftrace_lock
);
2943 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2944 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2946 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2948 /* break up if statements for readability */
2949 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2952 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2955 /* do this last, since it is the most expensive */
2957 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2959 if (!ftrace_match(str
, glob
, len
, type
))
2963 hlist_del(&entry
->node
);
2964 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2967 __disable_ftrace_function_probe();
2968 mutex_unlock(&ftrace_lock
);
2972 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2975 __unregister_ftrace_function_probe(glob
, ops
, data
,
2976 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2980 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2982 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2985 void unregister_ftrace_function_probe_all(char *glob
)
2987 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2990 static LIST_HEAD(ftrace_commands
);
2991 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2993 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2995 struct ftrace_func_command
*p
;
2998 mutex_lock(&ftrace_cmd_mutex
);
2999 list_for_each_entry(p
, &ftrace_commands
, list
) {
3000 if (strcmp(cmd
->name
, p
->name
) == 0) {
3005 list_add(&cmd
->list
, &ftrace_commands
);
3007 mutex_unlock(&ftrace_cmd_mutex
);
3012 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
3014 struct ftrace_func_command
*p
, *n
;
3017 mutex_lock(&ftrace_cmd_mutex
);
3018 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
3019 if (strcmp(cmd
->name
, p
->name
) == 0) {
3021 list_del_init(&p
->list
);
3026 mutex_unlock(&ftrace_cmd_mutex
);
3031 static int ftrace_process_regex(struct ftrace_hash
*hash
,
3032 char *buff
, int len
, int enable
)
3034 char *func
, *command
, *next
= buff
;
3035 struct ftrace_func_command
*p
;
3038 func
= strsep(&next
, ":");
3041 ret
= ftrace_match_records(hash
, func
, len
);
3051 command
= strsep(&next
, ":");
3053 mutex_lock(&ftrace_cmd_mutex
);
3054 list_for_each_entry(p
, &ftrace_commands
, list
) {
3055 if (strcmp(p
->name
, command
) == 0) {
3056 ret
= p
->func(hash
, func
, command
, next
, enable
);
3061 mutex_unlock(&ftrace_cmd_mutex
);
3067 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
3068 size_t cnt
, loff_t
*ppos
, int enable
)
3070 struct ftrace_iterator
*iter
;
3071 struct trace_parser
*parser
;
3077 mutex_lock(&ftrace_regex_lock
);
3080 if (unlikely(ftrace_disabled
))
3083 if (file
->f_mode
& FMODE_READ
) {
3084 struct seq_file
*m
= file
->private_data
;
3087 iter
= file
->private_data
;
3089 parser
= &iter
->parser
;
3090 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
3092 if (read
>= 0 && trace_parser_loaded(parser
) &&
3093 !trace_parser_cont(parser
)) {
3094 ret
= ftrace_process_regex(iter
->hash
, parser
->buffer
,
3095 parser
->idx
, enable
);
3096 trace_parser_clear(parser
);
3103 mutex_unlock(&ftrace_regex_lock
);
3109 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
3110 size_t cnt
, loff_t
*ppos
)
3112 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
3116 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
3117 size_t cnt
, loff_t
*ppos
)
3119 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
3123 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3124 int reset
, int enable
)
3126 struct ftrace_hash
**orig_hash
;
3127 struct ftrace_hash
*hash
;
3130 /* All global ops uses the global ops filters */
3131 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
)
3134 if (unlikely(ftrace_disabled
))
3138 orig_hash
= &ops
->filter_hash
;
3140 orig_hash
= &ops
->notrace_hash
;
3142 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3146 mutex_lock(&ftrace_regex_lock
);
3148 ftrace_filter_reset(hash
);
3150 ftrace_match_records(hash
, buf
, len
);
3152 mutex_lock(&ftrace_lock
);
3153 ret
= ftrace_hash_move(ops
, enable
, orig_hash
, hash
);
3154 if (!ret
&& ops
->flags
& FTRACE_OPS_FL_ENABLED
3156 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3158 mutex_unlock(&ftrace_lock
);
3160 mutex_unlock(&ftrace_regex_lock
);
3162 free_ftrace_hash(hash
);
3167 * ftrace_set_filter - set a function to filter on in ftrace
3168 * @ops - the ops to set the filter with
3169 * @buf - the string that holds the function filter text.
3170 * @len - the length of the string.
3171 * @reset - non zero to reset all filters before applying this filter.
3173 * Filters denote which functions should be enabled when tracing is enabled.
3174 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3176 void ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
3179 ftrace_set_regex(ops
, buf
, len
, reset
, 1);
3181 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
3184 * ftrace_set_notrace - set a function to not trace in ftrace
3185 * @ops - the ops to set the notrace filter with
3186 * @buf - the string that holds the function notrace text.
3187 * @len - the length of the string.
3188 * @reset - non zero to reset all filters before applying this filter.
3190 * Notrace Filters denote which functions should not be enabled when tracing
3191 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3194 void ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
3197 ftrace_set_regex(ops
, buf
, len
, reset
, 0);
3199 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
3201 * ftrace_set_filter - set a function to filter on in ftrace
3202 * @ops - the ops to set the filter with
3203 * @buf - the string that holds the function filter text.
3204 * @len - the length of the string.
3205 * @reset - non zero to reset all filters before applying this filter.
3207 * Filters denote which functions should be enabled when tracing is enabled.
3208 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3210 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
3212 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
3214 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
3217 * ftrace_set_notrace - set a function to not trace in ftrace
3218 * @ops - the ops to set the notrace filter with
3219 * @buf - the string that holds the function notrace text.
3220 * @len - the length of the string.
3221 * @reset - non zero to reset all filters before applying this filter.
3223 * Notrace Filters denote which functions should not be enabled when tracing
3224 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3227 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
3229 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
3231 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
3234 * command line interface to allow users to set filters on boot up.
3236 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3237 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3238 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3240 static int __init
set_ftrace_notrace(char *str
)
3242 strncpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
3245 __setup("ftrace_notrace=", set_ftrace_notrace
);
3247 static int __init
set_ftrace_filter(char *str
)
3249 strncpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
3252 __setup("ftrace_filter=", set_ftrace_filter
);
3254 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3255 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3256 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
3258 static int __init
set_graph_function(char *str
)
3260 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
3263 __setup("ftrace_graph_filter=", set_graph_function
);
3265 static void __init
set_ftrace_early_graph(char *buf
)
3271 func
= strsep(&buf
, ",");
3272 /* we allow only one expression at a time */
3273 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3276 printk(KERN_DEBUG
"ftrace: function %s not "
3277 "traceable\n", func
);
3280 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3283 ftrace_set_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
3288 func
= strsep(&buf
, ",");
3289 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
3293 static void __init
set_ftrace_early_filters(void)
3295 if (ftrace_filter_buf
[0])
3296 ftrace_set_early_filter(&global_ops
, ftrace_filter_buf
, 1);
3297 if (ftrace_notrace_buf
[0])
3298 ftrace_set_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
3299 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3300 if (ftrace_graph_buf
[0])
3301 set_ftrace_early_graph(ftrace_graph_buf
);
3302 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3305 int ftrace_regex_release(struct inode
*inode
, struct file
*file
)
3307 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
3308 struct ftrace_iterator
*iter
;
3309 struct ftrace_hash
**orig_hash
;
3310 struct trace_parser
*parser
;
3314 mutex_lock(&ftrace_regex_lock
);
3315 if (file
->f_mode
& FMODE_READ
) {
3318 seq_release(inode
, file
);
3320 iter
= file
->private_data
;
3322 parser
= &iter
->parser
;
3323 if (trace_parser_loaded(parser
)) {
3324 parser
->buffer
[parser
->idx
] = 0;
3325 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
3328 trace_parser_put(parser
);
3330 if (file
->f_mode
& FMODE_WRITE
) {
3331 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
3334 orig_hash
= &iter
->ops
->filter_hash
;
3336 orig_hash
= &iter
->ops
->notrace_hash
;
3338 mutex_lock(&ftrace_lock
);
3339 ret
= ftrace_hash_move(iter
->ops
, filter_hash
,
3340 orig_hash
, iter
->hash
);
3341 if (!ret
&& (iter
->ops
->flags
& FTRACE_OPS_FL_ENABLED
)
3343 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3345 mutex_unlock(&ftrace_lock
);
3347 free_ftrace_hash(iter
->hash
);
3350 mutex_unlock(&ftrace_regex_lock
);
3354 static const struct file_operations ftrace_avail_fops
= {
3355 .open
= ftrace_avail_open
,
3357 .llseek
= seq_lseek
,
3358 .release
= seq_release_private
,
3361 static const struct file_operations ftrace_enabled_fops
= {
3362 .open
= ftrace_enabled_open
,
3364 .llseek
= seq_lseek
,
3365 .release
= seq_release_private
,
3368 static const struct file_operations ftrace_filter_fops
= {
3369 .open
= ftrace_filter_open
,
3371 .write
= ftrace_filter_write
,
3372 .llseek
= ftrace_regex_lseek
,
3373 .release
= ftrace_regex_release
,
3376 static const struct file_operations ftrace_notrace_fops
= {
3377 .open
= ftrace_notrace_open
,
3379 .write
= ftrace_notrace_write
,
3380 .llseek
= ftrace_regex_lseek
,
3381 .release
= ftrace_regex_release
,
3384 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3386 static DEFINE_MUTEX(graph_lock
);
3388 int ftrace_graph_count
;
3389 int ftrace_graph_filter_enabled
;
3390 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
3393 __g_next(struct seq_file
*m
, loff_t
*pos
)
3395 if (*pos
>= ftrace_graph_count
)
3397 return &ftrace_graph_funcs
[*pos
];
3401 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3404 return __g_next(m
, pos
);
3407 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
3409 mutex_lock(&graph_lock
);
3411 /* Nothing, tell g_show to print all functions are enabled */
3412 if (!ftrace_graph_filter_enabled
&& !*pos
)
3415 return __g_next(m
, pos
);
3418 static void g_stop(struct seq_file
*m
, void *p
)
3420 mutex_unlock(&graph_lock
);
3423 static int g_show(struct seq_file
*m
, void *v
)
3425 unsigned long *ptr
= v
;
3430 if (ptr
== (unsigned long *)1) {
3431 seq_printf(m
, "#### all functions enabled ####\n");
3435 seq_printf(m
, "%ps\n", (void *)*ptr
);
3440 static const struct seq_operations ftrace_graph_seq_ops
= {
3448 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
3452 if (unlikely(ftrace_disabled
))
3455 mutex_lock(&graph_lock
);
3456 if ((file
->f_mode
& FMODE_WRITE
) &&
3457 (file
->f_flags
& O_TRUNC
)) {
3458 ftrace_graph_filter_enabled
= 0;
3459 ftrace_graph_count
= 0;
3460 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
3462 mutex_unlock(&graph_lock
);
3464 if (file
->f_mode
& FMODE_READ
)
3465 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
3471 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
3473 if (file
->f_mode
& FMODE_READ
)
3474 seq_release(inode
, file
);
3479 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
3481 struct dyn_ftrace
*rec
;
3482 struct ftrace_page
*pg
;
3491 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
3492 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3495 search_len
= strlen(search
);
3497 mutex_lock(&ftrace_lock
);
3499 if (unlikely(ftrace_disabled
)) {
3500 mutex_unlock(&ftrace_lock
);
3504 do_for_each_ftrace_rec(pg
, rec
) {
3506 if (ftrace_match_record(rec
, NULL
, search
, search_len
, type
)) {
3507 /* if it is in the array */
3509 for (i
= 0; i
< *idx
; i
++) {
3510 if (array
[i
] == rec
->ip
) {
3519 array
[(*idx
)++] = rec
->ip
;
3520 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3525 array
[i
] = array
[--(*idx
)];
3531 } while_for_each_ftrace_rec();
3533 mutex_unlock(&ftrace_lock
);
3538 ftrace_graph_filter_enabled
= 1;
3543 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
3544 size_t cnt
, loff_t
*ppos
)
3546 struct trace_parser parser
;
3552 mutex_lock(&graph_lock
);
3554 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
3559 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
3561 if (read
>= 0 && trace_parser_loaded((&parser
))) {
3562 parser
.buffer
[parser
.idx
] = 0;
3564 /* we allow only one expression at a time */
3565 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3574 trace_parser_put(&parser
);
3576 mutex_unlock(&graph_lock
);
3581 static const struct file_operations ftrace_graph_fops
= {
3582 .open
= ftrace_graph_open
,
3584 .write
= ftrace_graph_write
,
3585 .release
= ftrace_graph_release
,
3586 .llseek
= seq_lseek
,
3588 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3590 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
3593 trace_create_file("available_filter_functions", 0444,
3594 d_tracer
, NULL
, &ftrace_avail_fops
);
3596 trace_create_file("enabled_functions", 0444,
3597 d_tracer
, NULL
, &ftrace_enabled_fops
);
3599 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
3600 NULL
, &ftrace_filter_fops
);
3602 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
3603 NULL
, &ftrace_notrace_fops
);
3605 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3606 trace_create_file("set_graph_function", 0444, d_tracer
,
3608 &ftrace_graph_fops
);
3609 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3614 static void ftrace_swap_recs(void *a
, void *b
, int size
)
3616 struct dyn_ftrace
*reca
= a
;
3617 struct dyn_ftrace
*recb
= b
;
3618 struct dyn_ftrace t
;
3625 static int ftrace_process_locs(struct module
*mod
,
3626 unsigned long *start
,
3629 struct ftrace_page
*pg
;
3630 unsigned long count
;
3633 unsigned long flags
= 0; /* Shut up gcc */
3636 count
= end
- start
;
3641 pg
= ftrace_allocate_pages(count
);
3645 mutex_lock(&ftrace_lock
);
3648 * Core and each module needs their own pages, as
3649 * modules will free them when they are removed.
3650 * Force a new page to be allocated for modules.
3653 WARN_ON(ftrace_pages
|| ftrace_pages_start
);
3654 /* First initialization */
3655 ftrace_pages
= ftrace_pages_start
= pg
;
3660 if (WARN_ON(ftrace_pages
->next
)) {
3661 /* Hmm, we have free pages? */
3662 while (ftrace_pages
->next
)
3663 ftrace_pages
= ftrace_pages
->next
;
3666 ftrace_pages
->next
= pg
;
3672 addr
= ftrace_call_adjust(*p
++);
3674 * Some architecture linkers will pad between
3675 * the different mcount_loc sections of different
3676 * object files to satisfy alignments.
3677 * Skip any NULL pointers.
3681 if (!ftrace_record_ip(addr
))
3685 /* These new locations need to be initialized */
3686 ftrace_new_pgs
= pg
;
3688 /* Make each individual set of pages sorted by ips */
3689 for (; pg
; pg
= pg
->next
)
3690 sort(pg
->records
, pg
->index
, sizeof(struct dyn_ftrace
),
3691 ftrace_cmp_recs
, ftrace_swap_recs
);
3694 * We only need to disable interrupts on start up
3695 * because we are modifying code that an interrupt
3696 * may execute, and the modification is not atomic.
3697 * But for modules, nothing runs the code we modify
3698 * until we are finished with it, and there's no
3699 * reason to cause large interrupt latencies while we do it.
3702 local_irq_save(flags
);
3703 ftrace_update_code(mod
);
3705 local_irq_restore(flags
);
3708 mutex_unlock(&ftrace_lock
);
3713 #ifdef CONFIG_MODULES
3715 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3717 void ftrace_release_mod(struct module
*mod
)
3719 struct dyn_ftrace
*rec
;
3720 struct ftrace_page
**last_pg
;
3721 struct ftrace_page
*pg
;
3724 mutex_lock(&ftrace_lock
);
3726 if (ftrace_disabled
)
3730 * Each module has its own ftrace_pages, remove
3731 * them from the list.
3733 last_pg
= &ftrace_pages_start
;
3734 for (pg
= ftrace_pages_start
; pg
; pg
= *last_pg
) {
3735 rec
= &pg
->records
[0];
3736 if (within_module_core(rec
->ip
, mod
)) {
3738 * As core pages are first, the first
3739 * page should never be a module page.
3741 if (WARN_ON(pg
== ftrace_pages_start
))
3744 /* Check if we are deleting the last page */
3745 if (pg
== ftrace_pages
)
3746 ftrace_pages
= next_to_ftrace_page(last_pg
);
3748 *last_pg
= pg
->next
;
3749 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
3750 free_pages((unsigned long)pg
->records
, order
);
3753 last_pg
= &pg
->next
;
3756 mutex_unlock(&ftrace_lock
);
3759 static void ftrace_init_module(struct module
*mod
,
3760 unsigned long *start
, unsigned long *end
)
3762 if (ftrace_disabled
|| start
== end
)
3764 ftrace_process_locs(mod
, start
, end
);
3767 static int ftrace_module_notify(struct notifier_block
*self
,
3768 unsigned long val
, void *data
)
3770 struct module
*mod
= data
;
3773 case MODULE_STATE_COMING
:
3774 ftrace_init_module(mod
, mod
->ftrace_callsites
,
3775 mod
->ftrace_callsites
+
3776 mod
->num_ftrace_callsites
);
3778 case MODULE_STATE_GOING
:
3779 ftrace_release_mod(mod
);
3786 static int ftrace_module_notify(struct notifier_block
*self
,
3787 unsigned long val
, void *data
)
3791 #endif /* CONFIG_MODULES */
3793 struct notifier_block ftrace_module_nb
= {
3794 .notifier_call
= ftrace_module_notify
,
3798 extern unsigned long __start_mcount_loc
[];
3799 extern unsigned long __stop_mcount_loc
[];
3801 void __init
ftrace_init(void)
3803 unsigned long count
, addr
, flags
;
3806 /* Keep the ftrace pointer to the stub */
3807 addr
= (unsigned long)ftrace_stub
;
3809 local_irq_save(flags
);
3810 ftrace_dyn_arch_init(&addr
);
3811 local_irq_restore(flags
);
3813 /* ftrace_dyn_arch_init places the return code in addr */
3817 count
= __stop_mcount_loc
- __start_mcount_loc
;
3819 ret
= ftrace_dyn_table_alloc(count
);
3823 last_ftrace_enabled
= ftrace_enabled
= 1;
3825 ret
= ftrace_process_locs(NULL
,
3829 ret
= register_module_notifier(&ftrace_module_nb
);
3831 pr_warning("Failed to register trace ftrace module notifier\n");
3833 set_ftrace_early_filters();
3837 ftrace_disabled
= 1;
3842 static struct ftrace_ops global_ops
= {
3843 .func
= ftrace_stub
,
3846 static int __init
ftrace_nodyn_init(void)
3851 device_initcall(ftrace_nodyn_init
);
3853 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
3854 static inline void ftrace_startup_enable(int command
) { }
3855 /* Keep as macros so we do not need to define the commands */
3856 # define ftrace_startup(ops, command) \
3858 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3861 # define ftrace_shutdown(ops, command) do { } while (0)
3862 # define ftrace_startup_sysctl() do { } while (0)
3863 # define ftrace_shutdown_sysctl() do { } while (0)
3866 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
)
3871 #endif /* CONFIG_DYNAMIC_FTRACE */
3874 ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
)
3876 struct ftrace_ops
*op
;
3878 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT
)))
3881 trace_recursion_set(TRACE_INTERNAL_BIT
);
3883 * Some of the ops may be dynamically allocated,
3884 * they must be freed after a synchronize_sched().
3886 preempt_disable_notrace();
3887 op
= rcu_dereference_raw(ftrace_ops_list
);
3888 while (op
!= &ftrace_list_end
) {
3889 if (ftrace_ops_test(op
, ip
))
3890 op
->func(ip
, parent_ip
);
3891 op
= rcu_dereference_raw(op
->next
);
3893 preempt_enable_notrace();
3894 trace_recursion_clear(TRACE_INTERNAL_BIT
);
3897 static void clear_ftrace_swapper(void)
3899 struct task_struct
*p
;
3903 for_each_online_cpu(cpu
) {
3905 clear_tsk_trace_trace(p
);
3910 static void set_ftrace_swapper(void)
3912 struct task_struct
*p
;
3916 for_each_online_cpu(cpu
) {
3918 set_tsk_trace_trace(p
);
3923 static void clear_ftrace_pid(struct pid
*pid
)
3925 struct task_struct
*p
;
3928 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
3929 clear_tsk_trace_trace(p
);
3930 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
3936 static void set_ftrace_pid(struct pid
*pid
)
3938 struct task_struct
*p
;
3941 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
3942 set_tsk_trace_trace(p
);
3943 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
3947 static void clear_ftrace_pid_task(struct pid
*pid
)
3949 if (pid
== ftrace_swapper_pid
)
3950 clear_ftrace_swapper();
3952 clear_ftrace_pid(pid
);
3955 static void set_ftrace_pid_task(struct pid
*pid
)
3957 if (pid
== ftrace_swapper_pid
)
3958 set_ftrace_swapper();
3960 set_ftrace_pid(pid
);
3963 static int ftrace_pid_add(int p
)
3966 struct ftrace_pid
*fpid
;
3969 mutex_lock(&ftrace_lock
);
3972 pid
= ftrace_swapper_pid
;
3974 pid
= find_get_pid(p
);
3981 list_for_each_entry(fpid
, &ftrace_pids
, list
)
3982 if (fpid
->pid
== pid
)
3987 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
3991 list_add(&fpid
->list
, &ftrace_pids
);
3994 set_ftrace_pid_task(pid
);
3996 ftrace_update_pid_func();
3997 ftrace_startup_enable(0);
3999 mutex_unlock(&ftrace_lock
);
4003 if (pid
!= ftrace_swapper_pid
)
4007 mutex_unlock(&ftrace_lock
);
4011 static void ftrace_pid_reset(void)
4013 struct ftrace_pid
*fpid
, *safe
;
4015 mutex_lock(&ftrace_lock
);
4016 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
4017 struct pid
*pid
= fpid
->pid
;
4019 clear_ftrace_pid_task(pid
);
4021 list_del(&fpid
->list
);
4025 ftrace_update_pid_func();
4026 ftrace_startup_enable(0);
4028 mutex_unlock(&ftrace_lock
);
4031 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
4033 mutex_lock(&ftrace_lock
);
4035 if (list_empty(&ftrace_pids
) && (!*pos
))
4038 return seq_list_start(&ftrace_pids
, *pos
);
4041 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
4046 return seq_list_next(v
, &ftrace_pids
, pos
);
4049 static void fpid_stop(struct seq_file
*m
, void *p
)
4051 mutex_unlock(&ftrace_lock
);
4054 static int fpid_show(struct seq_file
*m
, void *v
)
4056 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
4058 if (v
== (void *)1) {
4059 seq_printf(m
, "no pid\n");
4063 if (fpid
->pid
== ftrace_swapper_pid
)
4064 seq_printf(m
, "swapper tasks\n");
4066 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
4071 static const struct seq_operations ftrace_pid_sops
= {
4072 .start
= fpid_start
,
4079 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
4083 if ((file
->f_mode
& FMODE_WRITE
) &&
4084 (file
->f_flags
& O_TRUNC
))
4087 if (file
->f_mode
& FMODE_READ
)
4088 ret
= seq_open(file
, &ftrace_pid_sops
);
4094 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
4095 size_t cnt
, loff_t
*ppos
)
4101 if (cnt
>= sizeof(buf
))
4104 if (copy_from_user(&buf
, ubuf
, cnt
))
4110 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4111 * to clean the filter quietly.
4113 tmp
= strstrip(buf
);
4114 if (strlen(tmp
) == 0)
4117 ret
= strict_strtol(tmp
, 10, &val
);
4121 ret
= ftrace_pid_add(val
);
4123 return ret
? ret
: cnt
;
4127 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
4129 if (file
->f_mode
& FMODE_READ
)
4130 seq_release(inode
, file
);
4135 static const struct file_operations ftrace_pid_fops
= {
4136 .open
= ftrace_pid_open
,
4137 .write
= ftrace_pid_write
,
4139 .llseek
= seq_lseek
,
4140 .release
= ftrace_pid_release
,
4143 static __init
int ftrace_init_debugfs(void)
4145 struct dentry
*d_tracer
;
4147 d_tracer
= tracing_init_dentry();
4151 ftrace_init_dyn_debugfs(d_tracer
);
4153 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
4154 NULL
, &ftrace_pid_fops
);
4156 ftrace_profile_debugfs(d_tracer
);
4160 fs_initcall(ftrace_init_debugfs
);
4163 * ftrace_kill - kill ftrace
4165 * This function should be used by panic code. It stops ftrace
4166 * but in a not so nice way. If you need to simply kill ftrace
4167 * from a non-atomic section, use ftrace_kill.
4169 void ftrace_kill(void)
4171 ftrace_disabled
= 1;
4173 clear_ftrace_function();
4177 * Test if ftrace is dead or not.
4179 int ftrace_is_dead(void)
4181 return ftrace_disabled
;
4185 * register_ftrace_function - register a function for profiling
4186 * @ops - ops structure that holds the function for profiling.
4188 * Register a function to be called by all functions in the
4191 * Note: @ops->func and all the functions it calls must be labeled
4192 * with "notrace", otherwise it will go into a
4195 int register_ftrace_function(struct ftrace_ops
*ops
)
4199 mutex_lock(&ftrace_lock
);
4201 if (unlikely(ftrace_disabled
))
4204 ret
= __register_ftrace_function(ops
);
4206 ret
= ftrace_startup(ops
, 0);
4210 mutex_unlock(&ftrace_lock
);
4213 EXPORT_SYMBOL_GPL(register_ftrace_function
);
4216 * unregister_ftrace_function - unregister a function for profiling.
4217 * @ops - ops structure that holds the function to unregister
4219 * Unregister a function that was added to be called by ftrace profiling.
4221 int unregister_ftrace_function(struct ftrace_ops
*ops
)
4225 mutex_lock(&ftrace_lock
);
4226 ret
= __unregister_ftrace_function(ops
);
4228 ftrace_shutdown(ops
, 0);
4229 mutex_unlock(&ftrace_lock
);
4233 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
4236 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
4237 void __user
*buffer
, size_t *lenp
,
4242 mutex_lock(&ftrace_lock
);
4244 if (unlikely(ftrace_disabled
))
4247 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
4249 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
4252 last_ftrace_enabled
= !!ftrace_enabled
;
4254 if (ftrace_enabled
) {
4256 ftrace_startup_sysctl();
4258 /* we are starting ftrace again */
4259 if (ftrace_ops_list
!= &ftrace_list_end
) {
4260 if (ftrace_ops_list
->next
== &ftrace_list_end
)
4261 ftrace_trace_function
= ftrace_ops_list
->func
;
4263 ftrace_trace_function
= ftrace_ops_list_func
;
4267 /* stopping ftrace calls (just send to ftrace_stub) */
4268 ftrace_trace_function
= ftrace_stub
;
4270 ftrace_shutdown_sysctl();
4274 mutex_unlock(&ftrace_lock
);
4278 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4280 static int ftrace_graph_active
;
4281 static struct notifier_block ftrace_suspend_notifier
;
4283 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
4288 /* The callbacks that hook a function */
4289 trace_func_graph_ret_t ftrace_graph_return
=
4290 (trace_func_graph_ret_t
)ftrace_stub
;
4291 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
4293 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4294 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
4298 unsigned long flags
;
4299 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
4300 struct task_struct
*g
, *t
;
4302 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
4303 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
4304 * sizeof(struct ftrace_ret_stack
),
4306 if (!ret_stack_list
[i
]) {
4314 read_lock_irqsave(&tasklist_lock
, flags
);
4315 do_each_thread(g
, t
) {
4321 if (t
->ret_stack
== NULL
) {
4322 atomic_set(&t
->tracing_graph_pause
, 0);
4323 atomic_set(&t
->trace_overrun
, 0);
4324 t
->curr_ret_stack
= -1;
4325 /* Make sure the tasks see the -1 first: */
4327 t
->ret_stack
= ret_stack_list
[start
++];
4329 } while_each_thread(g
, t
);
4332 read_unlock_irqrestore(&tasklist_lock
, flags
);
4334 for (i
= start
; i
< end
; i
++)
4335 kfree(ret_stack_list
[i
]);
4340 ftrace_graph_probe_sched_switch(void *ignore
,
4341 struct task_struct
*prev
, struct task_struct
*next
)
4343 unsigned long long timestamp
;
4347 * Does the user want to count the time a function was asleep.
4348 * If so, do not update the time stamps.
4350 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
4353 timestamp
= trace_clock_local();
4355 prev
->ftrace_timestamp
= timestamp
;
4357 /* only process tasks that we timestamped */
4358 if (!next
->ftrace_timestamp
)
4362 * Update all the counters in next to make up for the
4363 * time next was sleeping.
4365 timestamp
-= next
->ftrace_timestamp
;
4367 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
4368 next
->ret_stack
[index
].calltime
+= timestamp
;
4371 /* Allocate a return stack for each task */
4372 static int start_graph_tracing(void)
4374 struct ftrace_ret_stack
**ret_stack_list
;
4377 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
4378 sizeof(struct ftrace_ret_stack
*),
4381 if (!ret_stack_list
)
4384 /* The cpu_boot init_task->ret_stack will never be freed */
4385 for_each_online_cpu(cpu
) {
4386 if (!idle_task(cpu
)->ret_stack
)
4387 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
4391 ret
= alloc_retstack_tasklist(ret_stack_list
);
4392 } while (ret
== -EAGAIN
);
4395 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4397 pr_info("ftrace_graph: Couldn't activate tracepoint"
4398 " probe to kernel_sched_switch\n");
4401 kfree(ret_stack_list
);
4406 * Hibernation protection.
4407 * The state of the current task is too much unstable during
4408 * suspend/restore to disk. We want to protect against that.
4411 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
4415 case PM_HIBERNATION_PREPARE
:
4416 pause_graph_tracing();
4419 case PM_POST_HIBERNATION
:
4420 unpause_graph_tracing();
4426 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
4427 trace_func_graph_ent_t entryfunc
)
4431 mutex_lock(&ftrace_lock
);
4433 /* we currently allow only one tracer registered at a time */
4434 if (ftrace_graph_active
) {
4439 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
4440 register_pm_notifier(&ftrace_suspend_notifier
);
4442 ftrace_graph_active
++;
4443 ret
= start_graph_tracing();
4445 ftrace_graph_active
--;
4449 ftrace_graph_return
= retfunc
;
4450 ftrace_graph_entry
= entryfunc
;
4452 ret
= ftrace_startup(&global_ops
, FTRACE_START_FUNC_RET
);
4455 mutex_unlock(&ftrace_lock
);
4459 void unregister_ftrace_graph(void)
4461 mutex_lock(&ftrace_lock
);
4463 if (unlikely(!ftrace_graph_active
))
4466 ftrace_graph_active
--;
4467 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
4468 ftrace_graph_entry
= ftrace_graph_entry_stub
;
4469 ftrace_shutdown(&global_ops
, FTRACE_STOP_FUNC_RET
);
4470 unregister_pm_notifier(&ftrace_suspend_notifier
);
4471 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4474 mutex_unlock(&ftrace_lock
);
4477 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
4480 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
4482 atomic_set(&t
->tracing_graph_pause
, 0);
4483 atomic_set(&t
->trace_overrun
, 0);
4484 t
->ftrace_timestamp
= 0;
4485 /* make curr_ret_stack visible before we add the ret_stack */
4487 t
->ret_stack
= ret_stack
;
4491 * Allocate a return stack for the idle task. May be the first
4492 * time through, or it may be done by CPU hotplug online.
4494 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
4496 t
->curr_ret_stack
= -1;
4498 * The idle task has no parent, it either has its own
4499 * stack or no stack at all.
4502 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
4504 if (ftrace_graph_active
) {
4505 struct ftrace_ret_stack
*ret_stack
;
4507 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
4509 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4510 * sizeof(struct ftrace_ret_stack
),
4514 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
4516 graph_init_task(t
, ret_stack
);
4520 /* Allocate a return stack for newly created task */
4521 void ftrace_graph_init_task(struct task_struct
*t
)
4523 /* Make sure we do not use the parent ret_stack */
4524 t
->ret_stack
= NULL
;
4525 t
->curr_ret_stack
= -1;
4527 if (ftrace_graph_active
) {
4528 struct ftrace_ret_stack
*ret_stack
;
4530 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4531 * sizeof(struct ftrace_ret_stack
),
4535 graph_init_task(t
, ret_stack
);
4539 void ftrace_graph_exit_task(struct task_struct
*t
)
4541 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
4543 t
->ret_stack
= NULL
;
4544 /* NULL must become visible to IRQs before we free it: */
4550 void ftrace_graph_stop(void)