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/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/slab.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
31 #include <linux/rcupdate.h>
33 #include <trace/events/sched.h>
35 #include <asm/ftrace.h>
36 #include <asm/setup.h>
38 #include "trace_output.h"
39 #include "trace_stat.h"
41 #define FTRACE_WARN_ON(cond) \
49 #define FTRACE_WARN_ON_ONCE(cond) \
52 if (WARN_ON_ONCE(___r)) \
57 /* hash bits for specific function selection */
58 #define FTRACE_HASH_BITS 7
59 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
60 #define FTRACE_HASH_DEFAULT_BITS 10
61 #define FTRACE_HASH_MAX_BITS 12
63 /* ftrace_enabled is a method to turn ftrace on or off */
64 int ftrace_enabled __read_mostly
;
65 static int last_ftrace_enabled
;
67 /* Quick disabling of function tracer. */
68 int function_trace_stop
;
70 /* List for set_ftrace_pid's pids. */
71 LIST_HEAD(ftrace_pids
);
73 struct list_head list
;
78 * ftrace_disabled is set when an anomaly is discovered.
79 * ftrace_disabled is much stronger than ftrace_enabled.
81 static int ftrace_disabled __read_mostly
;
83 static DEFINE_MUTEX(ftrace_lock
);
85 static struct ftrace_ops ftrace_list_end __read_mostly
=
90 static struct ftrace_ops
*ftrace_global_list __read_mostly
= &ftrace_list_end
;
91 static struct ftrace_ops
*ftrace_ops_list __read_mostly
= &ftrace_list_end
;
92 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
93 ftrace_func_t __ftrace_trace_function __read_mostly
= ftrace_stub
;
94 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
95 static struct ftrace_ops global_ops
;
98 ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
);
101 * Traverse the ftrace_global_list, invoking all entries. The reason that we
102 * can use rcu_dereference_raw() is that elements removed from this list
103 * are simply leaked, so there is no need to interact with a grace-period
104 * mechanism. The rcu_dereference_raw() calls are needed to handle
105 * concurrent insertions into the ftrace_global_list.
107 * Silly Alpha and silly pointer-speculation compiler optimizations!
109 static void ftrace_global_list_func(unsigned long ip
,
110 unsigned long parent_ip
)
112 struct ftrace_ops
*op
;
114 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT
)))
117 trace_recursion_set(TRACE_GLOBAL_BIT
);
118 op
= rcu_dereference_raw(ftrace_global_list
); /*see above*/
119 while (op
!= &ftrace_list_end
) {
120 op
->func(ip
, parent_ip
);
121 op
= rcu_dereference_raw(op
->next
); /*see above*/
123 trace_recursion_clear(TRACE_GLOBAL_BIT
);
126 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
)
128 if (!test_tsk_trace_trace(current
))
131 ftrace_pid_function(ip
, parent_ip
);
134 static void set_ftrace_pid_function(ftrace_func_t func
)
136 /* do not set ftrace_pid_function to itself! */
137 if (func
!= ftrace_pid_func
)
138 ftrace_pid_function
= func
;
142 * clear_ftrace_function - reset the ftrace function
144 * This NULLs the ftrace function and in essence stops
145 * tracing. There may be lag
147 void clear_ftrace_function(void)
149 ftrace_trace_function
= ftrace_stub
;
150 __ftrace_trace_function
= ftrace_stub
;
151 ftrace_pid_function
= ftrace_stub
;
154 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
156 * For those archs that do not test ftrace_trace_stop in their
157 * mcount call site, we need to do it from C.
159 static void ftrace_test_stop_func(unsigned long ip
, unsigned long parent_ip
)
161 if (function_trace_stop
)
164 __ftrace_trace_function(ip
, parent_ip
);
168 static void update_global_ops(void)
173 * If there's only one function registered, then call that
174 * function directly. Otherwise, we need to iterate over the
175 * registered callers.
177 if (ftrace_global_list
== &ftrace_list_end
||
178 ftrace_global_list
->next
== &ftrace_list_end
)
179 func
= ftrace_global_list
->func
;
181 func
= ftrace_global_list_func
;
183 /* If we filter on pids, update to use the pid function */
184 if (!list_empty(&ftrace_pids
)) {
185 set_ftrace_pid_function(func
);
186 func
= ftrace_pid_func
;
189 global_ops
.func
= func
;
192 static void update_ftrace_function(void)
199 * If we are at the end of the list and this ops is
200 * not dynamic, then have the mcount trampoline call
201 * the function directly
203 if (ftrace_ops_list
== &ftrace_list_end
||
204 (ftrace_ops_list
->next
== &ftrace_list_end
&&
205 !(ftrace_ops_list
->flags
& FTRACE_OPS_FL_DYNAMIC
)))
206 func
= ftrace_ops_list
->func
;
208 func
= ftrace_ops_list_func
;
210 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
211 ftrace_trace_function
= func
;
213 __ftrace_trace_function
= func
;
214 ftrace_trace_function
= ftrace_test_stop_func
;
218 static void add_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
222 * We are entering ops into the list but another
223 * CPU might be walking that list. We need to make sure
224 * the ops->next pointer is valid before another CPU sees
225 * the ops pointer included into the list.
227 rcu_assign_pointer(*list
, ops
);
230 static int remove_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
232 struct ftrace_ops
**p
;
235 * If we are removing the last function, then simply point
236 * to the ftrace_stub.
238 if (*list
== ops
&& ops
->next
== &ftrace_list_end
) {
239 *list
= &ftrace_list_end
;
243 for (p
= list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
254 static int __register_ftrace_function(struct ftrace_ops
*ops
)
259 if (FTRACE_WARN_ON(ops
== &global_ops
))
262 if (WARN_ON(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
265 if (!core_kernel_data((unsigned long)ops
))
266 ops
->flags
|= FTRACE_OPS_FL_DYNAMIC
;
268 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
269 int first
= ftrace_global_list
== &ftrace_list_end
;
270 add_ftrace_ops(&ftrace_global_list
, ops
);
271 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
273 add_ftrace_ops(&ftrace_ops_list
, &global_ops
);
275 add_ftrace_ops(&ftrace_ops_list
, ops
);
278 update_ftrace_function();
283 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
290 if (WARN_ON(!(ops
->flags
& FTRACE_OPS_FL_ENABLED
)))
293 if (FTRACE_WARN_ON(ops
== &global_ops
))
296 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
297 ret
= remove_ftrace_ops(&ftrace_global_list
, ops
);
298 if (!ret
&& ftrace_global_list
== &ftrace_list_end
)
299 ret
= remove_ftrace_ops(&ftrace_ops_list
, &global_ops
);
301 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
303 ret
= remove_ftrace_ops(&ftrace_ops_list
, ops
);
309 update_ftrace_function();
312 * Dynamic ops may be freed, we must make sure that all
313 * callers are done before leaving this function.
315 if (ops
->flags
& FTRACE_OPS_FL_DYNAMIC
)
321 static void ftrace_update_pid_func(void)
323 /* Only do something if we are tracing something */
324 if (ftrace_trace_function
== ftrace_stub
)
327 update_ftrace_function();
330 #ifdef CONFIG_FUNCTION_PROFILER
331 struct ftrace_profile
{
332 struct hlist_node node
;
334 unsigned long counter
;
335 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
336 unsigned long long time
;
337 unsigned long long time_squared
;
341 struct ftrace_profile_page
{
342 struct ftrace_profile_page
*next
;
344 struct ftrace_profile records
[];
347 struct ftrace_profile_stat
{
349 struct hlist_head
*hash
;
350 struct ftrace_profile_page
*pages
;
351 struct ftrace_profile_page
*start
;
352 struct tracer_stat stat
;
355 #define PROFILE_RECORDS_SIZE \
356 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
358 #define PROFILES_PER_PAGE \
359 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
361 static int ftrace_profile_bits __read_mostly
;
362 static int ftrace_profile_enabled __read_mostly
;
364 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
365 static DEFINE_MUTEX(ftrace_profile_lock
);
367 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
369 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
372 function_stat_next(void *v
, int idx
)
374 struct ftrace_profile
*rec
= v
;
375 struct ftrace_profile_page
*pg
;
377 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
383 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
387 rec
= &pg
->records
[0];
395 static void *function_stat_start(struct tracer_stat
*trace
)
397 struct ftrace_profile_stat
*stat
=
398 container_of(trace
, struct ftrace_profile_stat
, stat
);
400 if (!stat
|| !stat
->start
)
403 return function_stat_next(&stat
->start
->records
[0], 0);
406 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
407 /* function graph compares on total time */
408 static int function_stat_cmp(void *p1
, void *p2
)
410 struct ftrace_profile
*a
= p1
;
411 struct ftrace_profile
*b
= p2
;
413 if (a
->time
< b
->time
)
415 if (a
->time
> b
->time
)
421 /* not function graph compares against hits */
422 static int function_stat_cmp(void *p1
, void *p2
)
424 struct ftrace_profile
*a
= p1
;
425 struct ftrace_profile
*b
= p2
;
427 if (a
->counter
< b
->counter
)
429 if (a
->counter
> b
->counter
)
436 static int function_stat_headers(struct seq_file
*m
)
438 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
439 seq_printf(m
, " Function "
442 "--- ---- --- ---\n");
444 seq_printf(m
, " Function Hit\n"
450 static int function_stat_show(struct seq_file
*m
, void *v
)
452 struct ftrace_profile
*rec
= v
;
453 char str
[KSYM_SYMBOL_LEN
];
455 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
456 static struct trace_seq s
;
457 unsigned long long avg
;
458 unsigned long long stddev
;
460 mutex_lock(&ftrace_profile_lock
);
462 /* we raced with function_profile_reset() */
463 if (unlikely(rec
->counter
== 0)) {
468 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
469 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
471 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
474 do_div(avg
, rec
->counter
);
476 /* Sample standard deviation (s^2) */
477 if (rec
->counter
<= 1)
480 stddev
= rec
->time_squared
- rec
->counter
* avg
* avg
;
482 * Divide only 1000 for ns^2 -> us^2 conversion.
483 * trace_print_graph_duration will divide 1000 again.
485 do_div(stddev
, (rec
->counter
- 1) * 1000);
489 trace_print_graph_duration(rec
->time
, &s
);
490 trace_seq_puts(&s
, " ");
491 trace_print_graph_duration(avg
, &s
);
492 trace_seq_puts(&s
, " ");
493 trace_print_graph_duration(stddev
, &s
);
494 trace_print_seq(m
, &s
);
498 mutex_unlock(&ftrace_profile_lock
);
503 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
505 struct ftrace_profile_page
*pg
;
507 pg
= stat
->pages
= stat
->start
;
510 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
515 memset(stat
->hash
, 0,
516 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
519 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
521 struct ftrace_profile_page
*pg
;
526 /* If we already allocated, do nothing */
530 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
534 #ifdef CONFIG_DYNAMIC_FTRACE
535 functions
= ftrace_update_tot_cnt
;
538 * We do not know the number of functions that exist because
539 * dynamic tracing is what counts them. With past experience
540 * we have around 20K functions. That should be more than enough.
541 * It is highly unlikely we will execute every function in
547 pg
= stat
->start
= stat
->pages
;
549 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
551 for (i
= 0; i
< pages
; i
++) {
552 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
563 unsigned long tmp
= (unsigned long)pg
;
569 free_page((unsigned long)stat
->pages
);
576 static int ftrace_profile_init_cpu(int cpu
)
578 struct ftrace_profile_stat
*stat
;
581 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
584 /* If the profile is already created, simply reset it */
585 ftrace_profile_reset(stat
);
590 * We are profiling all functions, but usually only a few thousand
591 * functions are hit. We'll make a hash of 1024 items.
593 size
= FTRACE_PROFILE_HASH_SIZE
;
595 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
600 if (!ftrace_profile_bits
) {
603 for (; size
; size
>>= 1)
604 ftrace_profile_bits
++;
607 /* Preallocate the function profiling pages */
608 if (ftrace_profile_pages_init(stat
) < 0) {
617 static int ftrace_profile_init(void)
622 for_each_online_cpu(cpu
) {
623 ret
= ftrace_profile_init_cpu(cpu
);
631 /* interrupts must be disabled */
632 static struct ftrace_profile
*
633 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
635 struct ftrace_profile
*rec
;
636 struct hlist_head
*hhd
;
637 struct hlist_node
*n
;
640 key
= hash_long(ip
, ftrace_profile_bits
);
641 hhd
= &stat
->hash
[key
];
643 if (hlist_empty(hhd
))
646 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
654 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
655 struct ftrace_profile
*rec
)
659 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
660 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
664 * The memory is already allocated, this simply finds a new record to use.
666 static struct ftrace_profile
*
667 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
669 struct ftrace_profile
*rec
= NULL
;
671 /* prevent recursion (from NMIs) */
672 if (atomic_inc_return(&stat
->disabled
) != 1)
676 * Try to find the function again since an NMI
677 * could have added it
679 rec
= ftrace_find_profiled_func(stat
, ip
);
683 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
684 if (!stat
->pages
->next
)
686 stat
->pages
= stat
->pages
->next
;
689 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
691 ftrace_add_profile(stat
, rec
);
694 atomic_dec(&stat
->disabled
);
700 function_profile_call(unsigned long ip
, unsigned long parent_ip
)
702 struct ftrace_profile_stat
*stat
;
703 struct ftrace_profile
*rec
;
706 if (!ftrace_profile_enabled
)
709 local_irq_save(flags
);
711 stat
= &__get_cpu_var(ftrace_profile_stats
);
712 if (!stat
->hash
|| !ftrace_profile_enabled
)
715 rec
= ftrace_find_profiled_func(stat
, ip
);
717 rec
= ftrace_profile_alloc(stat
, ip
);
724 local_irq_restore(flags
);
727 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
728 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
730 function_profile_call(trace
->func
, 0);
734 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
736 struct ftrace_profile_stat
*stat
;
737 unsigned long long calltime
;
738 struct ftrace_profile
*rec
;
741 local_irq_save(flags
);
742 stat
= &__get_cpu_var(ftrace_profile_stats
);
743 if (!stat
->hash
|| !ftrace_profile_enabled
)
746 /* If the calltime was zero'd ignore it */
747 if (!trace
->calltime
)
750 calltime
= trace
->rettime
- trace
->calltime
;
752 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
755 index
= trace
->depth
;
757 /* Append this call time to the parent time to subtract */
759 current
->ret_stack
[index
- 1].subtime
+= calltime
;
761 if (current
->ret_stack
[index
].subtime
< calltime
)
762 calltime
-= current
->ret_stack
[index
].subtime
;
767 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
769 rec
->time
+= calltime
;
770 rec
->time_squared
+= calltime
* calltime
;
774 local_irq_restore(flags
);
777 static int register_ftrace_profiler(void)
779 return register_ftrace_graph(&profile_graph_return
,
780 &profile_graph_entry
);
783 static void unregister_ftrace_profiler(void)
785 unregister_ftrace_graph();
788 static struct ftrace_ops ftrace_profile_ops __read_mostly
=
790 .func
= function_profile_call
,
793 static int register_ftrace_profiler(void)
795 return register_ftrace_function(&ftrace_profile_ops
);
798 static void unregister_ftrace_profiler(void)
800 unregister_ftrace_function(&ftrace_profile_ops
);
802 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
805 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
806 size_t cnt
, loff_t
*ppos
)
809 char buf
[64]; /* big enough to hold a number */
812 if (cnt
>= sizeof(buf
))
815 if (copy_from_user(&buf
, ubuf
, cnt
))
820 ret
= strict_strtoul(buf
, 10, &val
);
826 mutex_lock(&ftrace_profile_lock
);
827 if (ftrace_profile_enabled
^ val
) {
829 ret
= ftrace_profile_init();
835 ret
= register_ftrace_profiler();
840 ftrace_profile_enabled
= 1;
842 ftrace_profile_enabled
= 0;
844 * unregister_ftrace_profiler calls stop_machine
845 * so this acts like an synchronize_sched.
847 unregister_ftrace_profiler();
851 mutex_unlock(&ftrace_profile_lock
);
859 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
860 size_t cnt
, loff_t
*ppos
)
862 char buf
[64]; /* big enough to hold a number */
865 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
866 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
869 static const struct file_operations ftrace_profile_fops
= {
870 .open
= tracing_open_generic
,
871 .read
= ftrace_profile_read
,
872 .write
= ftrace_profile_write
,
873 .llseek
= default_llseek
,
876 /* used to initialize the real stat files */
877 static struct tracer_stat function_stats __initdata
= {
879 .stat_start
= function_stat_start
,
880 .stat_next
= function_stat_next
,
881 .stat_cmp
= function_stat_cmp
,
882 .stat_headers
= function_stat_headers
,
883 .stat_show
= function_stat_show
886 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
888 struct ftrace_profile_stat
*stat
;
889 struct dentry
*entry
;
894 for_each_possible_cpu(cpu
) {
895 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
897 /* allocate enough for function name + cpu number */
898 name
= kmalloc(32, GFP_KERNEL
);
901 * The files created are permanent, if something happens
902 * we still do not free memory.
905 "Could not allocate stat file for cpu %d\n",
909 stat
->stat
= function_stats
;
910 snprintf(name
, 32, "function%d", cpu
);
911 stat
->stat
.name
= name
;
912 ret
= register_stat_tracer(&stat
->stat
);
915 "Could not register function stat for cpu %d\n",
922 entry
= debugfs_create_file("function_profile_enabled", 0644,
923 d_tracer
, NULL
, &ftrace_profile_fops
);
925 pr_warning("Could not create debugfs "
926 "'function_profile_enabled' entry\n");
929 #else /* CONFIG_FUNCTION_PROFILER */
930 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
933 #endif /* CONFIG_FUNCTION_PROFILER */
935 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
937 #ifdef CONFIG_DYNAMIC_FTRACE
939 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
940 # error Dynamic ftrace depends on MCOUNT_RECORD
943 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
945 struct ftrace_func_probe
{
946 struct hlist_node node
;
947 struct ftrace_probe_ops
*ops
;
955 FTRACE_UPDATE_CALLS
= (1 << 0),
956 FTRACE_DISABLE_CALLS
= (1 << 1),
957 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
958 FTRACE_START_FUNC_RET
= (1 << 3),
959 FTRACE_STOP_FUNC_RET
= (1 << 4),
961 struct ftrace_func_entry
{
962 struct hlist_node hlist
;
967 unsigned long size_bits
;
968 struct hlist_head
*buckets
;
974 * We make these constant because no one should touch them,
975 * but they are used as the default "empty hash", to avoid allocating
976 * it all the time. These are in a read only section such that if
977 * anyone does try to modify it, it will cause an exception.
979 static const struct hlist_head empty_buckets
[1];
980 static const struct ftrace_hash empty_hash
= {
981 .buckets
= (struct hlist_head
*)empty_buckets
,
983 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
985 static struct ftrace_ops global_ops
= {
987 .notrace_hash
= EMPTY_HASH
,
988 .filter_hash
= EMPTY_HASH
,
991 static struct dyn_ftrace
*ftrace_new_addrs
;
993 static DEFINE_MUTEX(ftrace_regex_lock
);
996 struct ftrace_page
*next
;
998 struct dyn_ftrace records
[];
1001 #define ENTRIES_PER_PAGE \
1002 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
1004 /* estimate from running different kernels */
1005 #define NR_TO_INIT 10000
1007 static struct ftrace_page
*ftrace_pages_start
;
1008 static struct ftrace_page
*ftrace_pages
;
1010 static struct dyn_ftrace
*ftrace_free_records
;
1012 static struct ftrace_func_entry
*
1013 ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1016 struct ftrace_func_entry
*entry
;
1017 struct hlist_head
*hhd
;
1018 struct hlist_node
*n
;
1023 if (hash
->size_bits
> 0)
1024 key
= hash_long(ip
, hash
->size_bits
);
1028 hhd
= &hash
->buckets
[key
];
1030 hlist_for_each_entry_rcu(entry
, n
, hhd
, hlist
) {
1031 if (entry
->ip
== ip
)
1037 static void __add_hash_entry(struct ftrace_hash
*hash
,
1038 struct ftrace_func_entry
*entry
)
1040 struct hlist_head
*hhd
;
1043 if (hash
->size_bits
)
1044 key
= hash_long(entry
->ip
, hash
->size_bits
);
1048 hhd
= &hash
->buckets
[key
];
1049 hlist_add_head(&entry
->hlist
, hhd
);
1053 static int add_hash_entry(struct ftrace_hash
*hash
, unsigned long ip
)
1055 struct ftrace_func_entry
*entry
;
1057 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1062 __add_hash_entry(hash
, entry
);
1068 free_hash_entry(struct ftrace_hash
*hash
,
1069 struct ftrace_func_entry
*entry
)
1071 hlist_del(&entry
->hlist
);
1077 remove_hash_entry(struct ftrace_hash
*hash
,
1078 struct ftrace_func_entry
*entry
)
1080 hlist_del(&entry
->hlist
);
1084 static void ftrace_hash_clear(struct ftrace_hash
*hash
)
1086 struct hlist_head
*hhd
;
1087 struct hlist_node
*tp
, *tn
;
1088 struct ftrace_func_entry
*entry
;
1089 int size
= 1 << hash
->size_bits
;
1095 for (i
= 0; i
< size
; i
++) {
1096 hhd
= &hash
->buckets
[i
];
1097 hlist_for_each_entry_safe(entry
, tp
, tn
, hhd
, hlist
)
1098 free_hash_entry(hash
, entry
);
1100 FTRACE_WARN_ON(hash
->count
);
1103 static void free_ftrace_hash(struct ftrace_hash
*hash
)
1105 if (!hash
|| hash
== EMPTY_HASH
)
1107 ftrace_hash_clear(hash
);
1108 kfree(hash
->buckets
);
1112 static void __free_ftrace_hash_rcu(struct rcu_head
*rcu
)
1114 struct ftrace_hash
*hash
;
1116 hash
= container_of(rcu
, struct ftrace_hash
, rcu
);
1117 free_ftrace_hash(hash
);
1120 static void free_ftrace_hash_rcu(struct ftrace_hash
*hash
)
1122 if (!hash
|| hash
== EMPTY_HASH
)
1124 call_rcu_sched(&hash
->rcu
, __free_ftrace_hash_rcu
);
1127 static struct ftrace_hash
*alloc_ftrace_hash(int size_bits
)
1129 struct ftrace_hash
*hash
;
1132 hash
= kzalloc(sizeof(*hash
), GFP_KERNEL
);
1136 size
= 1 << size_bits
;
1137 hash
->buckets
= kzalloc(sizeof(*hash
->buckets
) * size
, GFP_KERNEL
);
1139 if (!hash
->buckets
) {
1144 hash
->size_bits
= size_bits
;
1149 static struct ftrace_hash
*
1150 alloc_and_copy_ftrace_hash(int size_bits
, struct ftrace_hash
*hash
)
1152 struct ftrace_func_entry
*entry
;
1153 struct ftrace_hash
*new_hash
;
1154 struct hlist_node
*tp
;
1159 new_hash
= alloc_ftrace_hash(size_bits
);
1164 if (!hash
|| !hash
->count
)
1167 size
= 1 << hash
->size_bits
;
1168 for (i
= 0; i
< size
; i
++) {
1169 hlist_for_each_entry(entry
, tp
, &hash
->buckets
[i
], hlist
) {
1170 ret
= add_hash_entry(new_hash
, entry
->ip
);
1176 FTRACE_WARN_ON(new_hash
->count
!= hash
->count
);
1181 free_ftrace_hash(new_hash
);
1186 ftrace_hash_rec_disable(struct ftrace_ops
*ops
, int filter_hash
);
1188 ftrace_hash_rec_enable(struct ftrace_ops
*ops
, int filter_hash
);
1191 ftrace_hash_move(struct ftrace_ops
*ops
, int enable
,
1192 struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1194 struct ftrace_func_entry
*entry
;
1195 struct hlist_node
*tp
, *tn
;
1196 struct hlist_head
*hhd
;
1197 struct ftrace_hash
*old_hash
;
1198 struct ftrace_hash
*new_hash
;
1200 int size
= src
->count
;
1206 * Remove the current set, update the hash and add
1209 ftrace_hash_rec_disable(ops
, enable
);
1212 * If the new source is empty, just free dst and assign it
1216 free_ftrace_hash_rcu(*dst
);
1217 rcu_assign_pointer(*dst
, EMPTY_HASH
);
1222 * Make the hash size about 1/2 the # found
1224 for (size
/= 2; size
; size
>>= 1)
1227 /* Don't allocate too much */
1228 if (bits
> FTRACE_HASH_MAX_BITS
)
1229 bits
= FTRACE_HASH_MAX_BITS
;
1232 new_hash
= alloc_ftrace_hash(bits
);
1236 size
= 1 << src
->size_bits
;
1237 for (i
= 0; i
< size
; i
++) {
1238 hhd
= &src
->buckets
[i
];
1239 hlist_for_each_entry_safe(entry
, tp
, tn
, hhd
, hlist
) {
1241 key
= hash_long(entry
->ip
, bits
);
1244 remove_hash_entry(src
, entry
);
1245 __add_hash_entry(new_hash
, entry
);
1250 rcu_assign_pointer(*dst
, new_hash
);
1251 free_ftrace_hash_rcu(old_hash
);
1256 * Enable regardless of ret:
1257 * On success, we enable the new hash.
1258 * On failure, we re-enable the original hash.
1260 ftrace_hash_rec_enable(ops
, enable
);
1266 * Test the hashes for this ops to see if we want to call
1267 * the ops->func or not.
1269 * It's a match if the ip is in the ops->filter_hash or
1270 * the filter_hash does not exist or is empty,
1272 * the ip is not in the ops->notrace_hash.
1274 * This needs to be called with preemption disabled as
1275 * the hashes are freed with call_rcu_sched().
1278 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
)
1280 struct ftrace_hash
*filter_hash
;
1281 struct ftrace_hash
*notrace_hash
;
1284 filter_hash
= rcu_dereference_raw(ops
->filter_hash
);
1285 notrace_hash
= rcu_dereference_raw(ops
->notrace_hash
);
1287 if ((!filter_hash
|| !filter_hash
->count
||
1288 ftrace_lookup_ip(filter_hash
, ip
)) &&
1289 (!notrace_hash
|| !notrace_hash
->count
||
1290 !ftrace_lookup_ip(notrace_hash
, ip
)))
1299 * This is a double for. Do not use 'break' to break out of the loop,
1300 * you must use a goto.
1302 #define do_for_each_ftrace_rec(pg, rec) \
1303 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1305 for (_____i = 0; _____i < pg->index; _____i++) { \
1306 rec = &pg->records[_____i];
1308 #define while_for_each_ftrace_rec() \
1312 static void __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1316 struct ftrace_hash
*hash
;
1317 struct ftrace_hash
*other_hash
;
1318 struct ftrace_page
*pg
;
1319 struct dyn_ftrace
*rec
;
1323 /* Only update if the ops has been registered */
1324 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1328 * In the filter_hash case:
1329 * If the count is zero, we update all records.
1330 * Otherwise we just update the items in the hash.
1332 * In the notrace_hash case:
1333 * We enable the update in the hash.
1334 * As disabling notrace means enabling the tracing,
1335 * and enabling notrace means disabling, the inc variable
1339 hash
= ops
->filter_hash
;
1340 other_hash
= ops
->notrace_hash
;
1341 if (!hash
|| !hash
->count
)
1345 hash
= ops
->notrace_hash
;
1346 other_hash
= ops
->filter_hash
;
1348 * If the notrace hash has no items,
1349 * then there's nothing to do.
1351 if (hash
&& !hash
->count
)
1355 do_for_each_ftrace_rec(pg
, rec
) {
1356 int in_other_hash
= 0;
1362 * Only the filter_hash affects all records.
1363 * Update if the record is not in the notrace hash.
1365 if (!other_hash
|| !ftrace_lookup_ip(other_hash
, rec
->ip
))
1368 in_hash
= hash
&& !!ftrace_lookup_ip(hash
, rec
->ip
);
1369 in_other_hash
= other_hash
&& !!ftrace_lookup_ip(other_hash
, rec
->ip
);
1374 if (filter_hash
&& in_hash
&& !in_other_hash
)
1376 else if (!filter_hash
&& in_hash
&&
1377 (in_other_hash
|| !other_hash
->count
))
1385 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == FTRACE_REF_MAX
))
1388 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == 0))
1393 /* Shortcut, if we handled all records, we are done. */
1394 if (!all
&& count
== hash
->count
)
1396 } while_for_each_ftrace_rec();
1399 static void ftrace_hash_rec_disable(struct ftrace_ops
*ops
,
1402 __ftrace_hash_rec_update(ops
, filter_hash
, 0);
1405 static void ftrace_hash_rec_enable(struct ftrace_ops
*ops
,
1408 __ftrace_hash_rec_update(ops
, filter_hash
, 1);
1411 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
1413 rec
->freelist
= ftrace_free_records
;
1414 ftrace_free_records
= rec
;
1415 rec
->flags
|= FTRACE_FL_FREE
;
1418 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
1420 struct dyn_ftrace
*rec
;
1422 /* First check for freed records */
1423 if (ftrace_free_records
) {
1424 rec
= ftrace_free_records
;
1426 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
1427 FTRACE_WARN_ON_ONCE(1);
1428 ftrace_free_records
= NULL
;
1432 ftrace_free_records
= rec
->freelist
;
1433 memset(rec
, 0, sizeof(*rec
));
1437 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
1438 if (!ftrace_pages
->next
) {
1439 /* allocate another page */
1440 ftrace_pages
->next
=
1441 (void *)get_zeroed_page(GFP_KERNEL
);
1442 if (!ftrace_pages
->next
)
1445 ftrace_pages
= ftrace_pages
->next
;
1448 return &ftrace_pages
->records
[ftrace_pages
->index
++];
1451 static struct dyn_ftrace
*
1452 ftrace_record_ip(unsigned long ip
)
1454 struct dyn_ftrace
*rec
;
1456 if (ftrace_disabled
)
1459 rec
= ftrace_alloc_dyn_node(ip
);
1464 rec
->newlist
= ftrace_new_addrs
;
1465 ftrace_new_addrs
= rec
;
1470 static void print_ip_ins(const char *fmt
, unsigned char *p
)
1474 printk(KERN_CONT
"%s", fmt
);
1476 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
1477 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
1480 static void ftrace_bug(int failed
, unsigned long ip
)
1484 FTRACE_WARN_ON_ONCE(1);
1485 pr_info("ftrace faulted on modifying ");
1489 FTRACE_WARN_ON_ONCE(1);
1490 pr_info("ftrace failed to modify ");
1492 print_ip_ins(" actual: ", (unsigned char *)ip
);
1493 printk(KERN_CONT
"\n");
1496 FTRACE_WARN_ON_ONCE(1);
1497 pr_info("ftrace faulted on writing ");
1501 FTRACE_WARN_ON_ONCE(1);
1502 pr_info("ftrace faulted on unknown error ");
1508 /* Return 1 if the address range is reserved for ftrace */
1509 int ftrace_text_reserved(void *start
, void *end
)
1511 struct dyn_ftrace
*rec
;
1512 struct ftrace_page
*pg
;
1514 do_for_each_ftrace_rec(pg
, rec
) {
1515 if (rec
->ip
<= (unsigned long)end
&&
1516 rec
->ip
+ MCOUNT_INSN_SIZE
> (unsigned long)start
)
1518 } while_for_each_ftrace_rec();
1524 __ftrace_replace_code(struct dyn_ftrace
*rec
, int update
)
1526 unsigned long ftrace_addr
;
1527 unsigned long flag
= 0UL;
1529 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1532 * If we are updating calls:
1534 * If the record has a ref count, then we need to enable it
1535 * because someone is using it.
1537 * Otherwise we make sure its disabled.
1539 * If we are disabling calls, then disable all records that
1542 if (update
&& (rec
->flags
& ~FTRACE_FL_MASK
))
1543 flag
= FTRACE_FL_ENABLED
;
1545 /* If the state of this record hasn't changed, then do nothing */
1546 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1550 rec
->flags
|= FTRACE_FL_ENABLED
;
1551 return ftrace_make_call(rec
, ftrace_addr
);
1554 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1555 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1558 static void ftrace_replace_code(int update
)
1560 struct dyn_ftrace
*rec
;
1561 struct ftrace_page
*pg
;
1564 if (unlikely(ftrace_disabled
))
1567 do_for_each_ftrace_rec(pg
, rec
) {
1568 /* Skip over free records */
1569 if (rec
->flags
& FTRACE_FL_FREE
)
1572 failed
= __ftrace_replace_code(rec
, update
);
1574 ftrace_bug(failed
, rec
->ip
);
1575 /* Stop processing */
1578 } while_for_each_ftrace_rec();
1582 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1589 if (unlikely(ftrace_disabled
))
1592 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1594 ftrace_bug(ret
, ip
);
1601 * archs can override this function if they must do something
1602 * before the modifying code is performed.
1604 int __weak
ftrace_arch_code_modify_prepare(void)
1610 * archs can override this function if they must do something
1611 * after the modifying code is performed.
1613 int __weak
ftrace_arch_code_modify_post_process(void)
1618 static int __ftrace_modify_code(void *data
)
1620 int *command
= data
;
1622 if (*command
& FTRACE_UPDATE_CALLS
)
1623 ftrace_replace_code(1);
1624 else if (*command
& FTRACE_DISABLE_CALLS
)
1625 ftrace_replace_code(0);
1627 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1628 ftrace_update_ftrace_func(ftrace_trace_function
);
1630 if (*command
& FTRACE_START_FUNC_RET
)
1631 ftrace_enable_ftrace_graph_caller();
1632 else if (*command
& FTRACE_STOP_FUNC_RET
)
1633 ftrace_disable_ftrace_graph_caller();
1638 static void ftrace_run_update_code(int command
)
1642 ret
= ftrace_arch_code_modify_prepare();
1643 FTRACE_WARN_ON(ret
);
1647 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1649 ret
= ftrace_arch_code_modify_post_process();
1650 FTRACE_WARN_ON(ret
);
1653 static ftrace_func_t saved_ftrace_func
;
1654 static int ftrace_start_up
;
1655 static int global_start_up
;
1657 static void ftrace_startup_enable(int command
)
1659 if (saved_ftrace_func
!= ftrace_trace_function
) {
1660 saved_ftrace_func
= ftrace_trace_function
;
1661 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1664 if (!command
|| !ftrace_enabled
)
1667 ftrace_run_update_code(command
);
1670 static int ftrace_startup(struct ftrace_ops
*ops
, int command
)
1672 bool hash_enable
= true;
1674 if (unlikely(ftrace_disabled
))
1678 command
|= FTRACE_UPDATE_CALLS
;
1680 /* ops marked global share the filter hashes */
1681 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
1683 /* Don't update hash if global is already set */
1684 if (global_start_up
)
1685 hash_enable
= false;
1689 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
1691 ftrace_hash_rec_enable(ops
, 1);
1693 ftrace_startup_enable(command
);
1698 static void ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
1700 bool hash_disable
= true;
1702 if (unlikely(ftrace_disabled
))
1707 * Just warn in case of unbalance, no need to kill ftrace, it's not
1708 * critical but the ftrace_call callers may be never nopped again after
1709 * further ftrace uses.
1711 WARN_ON_ONCE(ftrace_start_up
< 0);
1713 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
1716 WARN_ON_ONCE(global_start_up
< 0);
1717 /* Don't update hash if global still has users */
1718 if (global_start_up
) {
1719 WARN_ON_ONCE(!ftrace_start_up
);
1720 hash_disable
= false;
1725 ftrace_hash_rec_disable(ops
, 1);
1727 if (ops
!= &global_ops
|| !global_start_up
)
1728 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
1730 command
|= FTRACE_UPDATE_CALLS
;
1732 if (saved_ftrace_func
!= ftrace_trace_function
) {
1733 saved_ftrace_func
= ftrace_trace_function
;
1734 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1737 if (!command
|| !ftrace_enabled
)
1740 ftrace_run_update_code(command
);
1743 static void ftrace_startup_sysctl(void)
1745 if (unlikely(ftrace_disabled
))
1748 /* Force update next time */
1749 saved_ftrace_func
= NULL
;
1750 /* ftrace_start_up is true if we want ftrace running */
1751 if (ftrace_start_up
)
1752 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
1755 static void ftrace_shutdown_sysctl(void)
1757 if (unlikely(ftrace_disabled
))
1760 /* ftrace_start_up is true if ftrace is running */
1761 if (ftrace_start_up
)
1762 ftrace_run_update_code(FTRACE_DISABLE_CALLS
);
1765 static cycle_t ftrace_update_time
;
1766 static unsigned long ftrace_update_cnt
;
1767 unsigned long ftrace_update_tot_cnt
;
1769 static int ops_traces_mod(struct ftrace_ops
*ops
)
1771 struct ftrace_hash
*hash
;
1773 hash
= ops
->filter_hash
;
1774 return !!(!hash
|| !hash
->count
);
1777 static int ftrace_update_code(struct module
*mod
)
1779 struct dyn_ftrace
*p
;
1780 cycle_t start
, stop
;
1781 unsigned long ref
= 0;
1784 * When adding a module, we need to check if tracers are
1785 * currently enabled and if they are set to trace all functions.
1786 * If they are, we need to enable the module functions as well
1787 * as update the reference counts for those function records.
1790 struct ftrace_ops
*ops
;
1792 for (ops
= ftrace_ops_list
;
1793 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
1794 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
&&
1795 ops_traces_mod(ops
))
1800 start
= ftrace_now(raw_smp_processor_id());
1801 ftrace_update_cnt
= 0;
1803 while (ftrace_new_addrs
) {
1805 /* If something went wrong, bail without enabling anything */
1806 if (unlikely(ftrace_disabled
))
1809 p
= ftrace_new_addrs
;
1810 ftrace_new_addrs
= p
->newlist
;
1814 * Do the initial record conversion from mcount jump
1815 * to the NOP instructions.
1817 if (!ftrace_code_disable(mod
, p
)) {
1823 ftrace_update_cnt
++;
1826 * If the tracing is enabled, go ahead and enable the record.
1828 * The reason not to enable the record immediatelly is the
1829 * inherent check of ftrace_make_nop/ftrace_make_call for
1830 * correct previous instructions. Making first the NOP
1831 * conversion puts the module to the correct state, thus
1832 * passing the ftrace_make_call check.
1834 if (ftrace_start_up
&& ref
) {
1835 int failed
= __ftrace_replace_code(p
, 1);
1837 ftrace_bug(failed
, p
->ip
);
1843 stop
= ftrace_now(raw_smp_processor_id());
1844 ftrace_update_time
= stop
- start
;
1845 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1850 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1852 struct ftrace_page
*pg
;
1856 /* allocate a few pages */
1857 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1858 if (!ftrace_pages_start
)
1862 * Allocate a few more pages.
1864 * TODO: have some parser search vmlinux before
1865 * final linking to find all calls to ftrace.
1867 * a) know how many pages to allocate.
1869 * b) set up the table then.
1871 * The dynamic code is still necessary for
1875 pg
= ftrace_pages
= ftrace_pages_start
;
1877 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1878 pr_info("ftrace: allocating %ld entries in %d pages\n",
1879 num_to_init
, cnt
+ 1);
1881 for (i
= 0; i
< cnt
; i
++) {
1882 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1884 /* If we fail, we'll try later anyway */
1895 FTRACE_ITER_FILTER
= (1 << 0),
1896 FTRACE_ITER_NOTRACE
= (1 << 1),
1897 FTRACE_ITER_PRINTALL
= (1 << 2),
1898 FTRACE_ITER_HASH
= (1 << 3),
1899 FTRACE_ITER_ENABLED
= (1 << 4),
1902 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1904 struct ftrace_iterator
{
1907 struct ftrace_page
*pg
;
1908 struct dyn_ftrace
*func
;
1909 struct ftrace_func_probe
*probe
;
1910 struct trace_parser parser
;
1911 struct ftrace_hash
*hash
;
1912 struct ftrace_ops
*ops
;
1919 t_hash_next(struct seq_file
*m
, loff_t
*pos
)
1921 struct ftrace_iterator
*iter
= m
->private;
1922 struct hlist_node
*hnd
= NULL
;
1923 struct hlist_head
*hhd
;
1929 hnd
= &iter
->probe
->node
;
1931 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1934 hhd
= &ftrace_func_hash
[iter
->hidx
];
1936 if (hlist_empty(hhd
)) {
1952 if (WARN_ON_ONCE(!hnd
))
1955 iter
->probe
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1960 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1962 struct ftrace_iterator
*iter
= m
->private;
1966 if (iter
->func_pos
> *pos
)
1970 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
1971 p
= t_hash_next(m
, &l
);
1978 /* Only set this if we have an item */
1979 iter
->flags
|= FTRACE_ITER_HASH
;
1985 t_hash_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
1987 struct ftrace_func_probe
*rec
;
1990 if (WARN_ON_ONCE(!rec
))
1993 if (rec
->ops
->print
)
1994 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1996 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
1999 seq_printf(m
, ":%p", rec
->data
);
2006 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2008 struct ftrace_iterator
*iter
= m
->private;
2009 struct ftrace_ops
*ops
= &global_ops
;
2010 struct dyn_ftrace
*rec
= NULL
;
2012 if (unlikely(ftrace_disabled
))
2015 if (iter
->flags
& FTRACE_ITER_HASH
)
2016 return t_hash_next(m
, pos
);
2019 iter
->pos
= iter
->func_pos
= *pos
;
2021 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
2022 return t_hash_start(m
, pos
);
2025 if (iter
->idx
>= iter
->pg
->index
) {
2026 if (iter
->pg
->next
) {
2027 iter
->pg
= iter
->pg
->next
;
2032 rec
= &iter
->pg
->records
[iter
->idx
++];
2033 if ((rec
->flags
& FTRACE_FL_FREE
) ||
2035 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
2036 !(ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))) ||
2038 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
2039 !ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
)) ||
2041 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
2042 !(rec
->flags
& ~FTRACE_FL_MASK
))) {
2050 return t_hash_start(m
, pos
);
2057 static void reset_iter_read(struct ftrace_iterator
*iter
)
2061 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
& FTRACE_ITER_HASH
);
2064 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2066 struct ftrace_iterator
*iter
= m
->private;
2067 struct ftrace_ops
*ops
= &global_ops
;
2071 mutex_lock(&ftrace_lock
);
2073 if (unlikely(ftrace_disabled
))
2077 * If an lseek was done, then reset and start from beginning.
2079 if (*pos
< iter
->pos
)
2080 reset_iter_read(iter
);
2083 * For set_ftrace_filter reading, if we have the filter
2084 * off, we can short cut and just print out that all
2085 * functions are enabled.
2087 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ops
->filter_hash
->count
) {
2089 return t_hash_start(m
, pos
);
2090 iter
->flags
|= FTRACE_ITER_PRINTALL
;
2091 /* reset in case of seek/pread */
2092 iter
->flags
&= ~FTRACE_ITER_HASH
;
2096 if (iter
->flags
& FTRACE_ITER_HASH
)
2097 return t_hash_start(m
, pos
);
2100 * Unfortunately, we need to restart at ftrace_pages_start
2101 * every time we let go of the ftrace_mutex. This is because
2102 * those pointers can change without the lock.
2104 iter
->pg
= ftrace_pages_start
;
2106 for (l
= 0; l
<= *pos
; ) {
2107 p
= t_next(m
, p
, &l
);
2113 if (iter
->flags
& FTRACE_ITER_FILTER
)
2114 return t_hash_start(m
, pos
);
2122 static void t_stop(struct seq_file
*m
, void *p
)
2124 mutex_unlock(&ftrace_lock
);
2127 static int t_show(struct seq_file
*m
, void *v
)
2129 struct ftrace_iterator
*iter
= m
->private;
2130 struct dyn_ftrace
*rec
;
2132 if (iter
->flags
& FTRACE_ITER_HASH
)
2133 return t_hash_show(m
, iter
);
2135 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
2136 seq_printf(m
, "#### all functions enabled ####\n");
2145 seq_printf(m
, "%ps", (void *)rec
->ip
);
2146 if (iter
->flags
& FTRACE_ITER_ENABLED
)
2147 seq_printf(m
, " (%ld)",
2148 rec
->flags
& ~FTRACE_FL_MASK
);
2149 seq_printf(m
, "\n");
2154 static const struct seq_operations show_ftrace_seq_ops
= {
2162 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
2164 struct ftrace_iterator
*iter
;
2167 if (unlikely(ftrace_disabled
))
2170 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2174 iter
->pg
= ftrace_pages_start
;
2176 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2178 struct seq_file
*m
= file
->private_data
;
2189 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
2191 struct ftrace_iterator
*iter
;
2194 if (unlikely(ftrace_disabled
))
2197 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2201 iter
->pg
= ftrace_pages_start
;
2202 iter
->flags
= FTRACE_ITER_ENABLED
;
2204 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2206 struct seq_file
*m
= file
->private_data
;
2216 static void ftrace_filter_reset(struct ftrace_hash
*hash
)
2218 mutex_lock(&ftrace_lock
);
2219 ftrace_hash_clear(hash
);
2220 mutex_unlock(&ftrace_lock
);
2224 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
2225 struct inode
*inode
, struct file
*file
)
2227 struct ftrace_iterator
*iter
;
2228 struct ftrace_hash
*hash
;
2231 if (unlikely(ftrace_disabled
))
2234 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2238 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
2243 if (flag
& FTRACE_ITER_NOTRACE
)
2244 hash
= ops
->notrace_hash
;
2246 hash
= ops
->filter_hash
;
2251 if (file
->f_mode
& FMODE_WRITE
) {
2252 mutex_lock(&ftrace_lock
);
2253 iter
->hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, hash
);
2254 mutex_unlock(&ftrace_lock
);
2257 trace_parser_put(&iter
->parser
);
2263 mutex_lock(&ftrace_regex_lock
);
2265 if ((file
->f_mode
& FMODE_WRITE
) &&
2266 (file
->f_flags
& O_TRUNC
))
2267 ftrace_filter_reset(iter
->hash
);
2269 if (file
->f_mode
& FMODE_READ
) {
2270 iter
->pg
= ftrace_pages_start
;
2272 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2274 struct seq_file
*m
= file
->private_data
;
2278 free_ftrace_hash(iter
->hash
);
2279 trace_parser_put(&iter
->parser
);
2283 file
->private_data
= iter
;
2284 mutex_unlock(&ftrace_regex_lock
);
2290 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
2292 return ftrace_regex_open(&global_ops
, FTRACE_ITER_FILTER
,
2297 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
2299 return ftrace_regex_open(&global_ops
, FTRACE_ITER_NOTRACE
,
2304 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
2308 if (file
->f_mode
& FMODE_READ
)
2309 ret
= seq_lseek(file
, offset
, origin
);
2311 file
->f_pos
= ret
= 1;
2316 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
2323 if (strcmp(str
, regex
) == 0)
2326 case MATCH_FRONT_ONLY
:
2327 if (strncmp(str
, regex
, len
) == 0)
2330 case MATCH_MIDDLE_ONLY
:
2331 if (strstr(str
, regex
))
2334 case MATCH_END_ONLY
:
2336 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
2345 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int not)
2347 struct ftrace_func_entry
*entry
;
2350 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
2352 /* Do nothing if it doesn't exist */
2356 free_hash_entry(hash
, entry
);
2358 /* Do nothing if it exists */
2362 ret
= add_hash_entry(hash
, rec
->ip
);
2368 ftrace_match_record(struct dyn_ftrace
*rec
, char *mod
,
2369 char *regex
, int len
, int type
)
2371 char str
[KSYM_SYMBOL_LEN
];
2374 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
2377 /* module lookup requires matching the module */
2378 if (!modname
|| strcmp(modname
, mod
))
2381 /* blank search means to match all funcs in the mod */
2386 return ftrace_match(str
, regex
, len
, type
);
2390 match_records(struct ftrace_hash
*hash
, char *buff
,
2391 int len
, char *mod
, int not)
2393 unsigned search_len
= 0;
2394 struct ftrace_page
*pg
;
2395 struct dyn_ftrace
*rec
;
2396 int type
= MATCH_FULL
;
2397 char *search
= buff
;
2402 type
= filter_parse_regex(buff
, len
, &search
, ¬);
2403 search_len
= strlen(search
);
2406 mutex_lock(&ftrace_lock
);
2408 if (unlikely(ftrace_disabled
))
2411 do_for_each_ftrace_rec(pg
, rec
) {
2413 if (ftrace_match_record(rec
, mod
, search
, search_len
, type
)) {
2414 ret
= enter_record(hash
, rec
, not);
2421 } while_for_each_ftrace_rec();
2423 mutex_unlock(&ftrace_lock
);
2429 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
2431 return match_records(hash
, buff
, len
, NULL
, 0);
2435 ftrace_match_module_records(struct ftrace_hash
*hash
, char *buff
, char *mod
)
2439 /* blank or '*' mean the same */
2440 if (strcmp(buff
, "*") == 0)
2443 /* handle the case of 'dont filter this module' */
2444 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
2449 return match_records(hash
, buff
, strlen(buff
), mod
, not);
2453 * We register the module command as a template to show others how
2454 * to register the a command as well.
2458 ftrace_mod_callback(struct ftrace_hash
*hash
,
2459 char *func
, char *cmd
, char *param
, int enable
)
2465 * cmd == 'mod' because we only registered this func
2466 * for the 'mod' ftrace_func_command.
2467 * But if you register one func with multiple commands,
2468 * you can tell which command was used by the cmd
2472 /* we must have a module name */
2476 mod
= strsep(¶m
, ":");
2480 ret
= ftrace_match_module_records(hash
, func
, mod
);
2489 static struct ftrace_func_command ftrace_mod_cmd
= {
2491 .func
= ftrace_mod_callback
,
2494 static int __init
ftrace_mod_cmd_init(void)
2496 return register_ftrace_command(&ftrace_mod_cmd
);
2498 device_initcall(ftrace_mod_cmd_init
);
2501 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
2503 struct ftrace_func_probe
*entry
;
2504 struct hlist_head
*hhd
;
2505 struct hlist_node
*n
;
2508 key
= hash_long(ip
, FTRACE_HASH_BITS
);
2510 hhd
= &ftrace_func_hash
[key
];
2512 if (hlist_empty(hhd
))
2516 * Disable preemption for these calls to prevent a RCU grace
2517 * period. This syncs the hash iteration and freeing of items
2518 * on the hash. rcu_read_lock is too dangerous here.
2520 preempt_disable_notrace();
2521 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
2522 if (entry
->ip
== ip
)
2523 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
2525 preempt_enable_notrace();
2528 static struct ftrace_ops trace_probe_ops __read_mostly
=
2530 .func
= function_trace_probe_call
,
2533 static int ftrace_probe_registered
;
2535 static void __enable_ftrace_function_probe(void)
2540 if (ftrace_probe_registered
)
2543 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2544 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2548 /* Nothing registered? */
2549 if (i
== FTRACE_FUNC_HASHSIZE
)
2552 ret
= __register_ftrace_function(&trace_probe_ops
);
2554 ret
= ftrace_startup(&trace_probe_ops
, 0);
2556 ftrace_probe_registered
= 1;
2559 static void __disable_ftrace_function_probe(void)
2564 if (!ftrace_probe_registered
)
2567 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2568 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2573 /* no more funcs left */
2574 ret
= __unregister_ftrace_function(&trace_probe_ops
);
2576 ftrace_shutdown(&trace_probe_ops
, 0);
2578 ftrace_probe_registered
= 0;
2582 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
2584 struct ftrace_func_probe
*entry
=
2585 container_of(rhp
, struct ftrace_func_probe
, rcu
);
2587 if (entry
->ops
->free
)
2588 entry
->ops
->free(&entry
->data
);
2594 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2597 struct ftrace_func_probe
*entry
;
2598 struct ftrace_page
*pg
;
2599 struct dyn_ftrace
*rec
;
2605 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2606 len
= strlen(search
);
2608 /* we do not support '!' for function probes */
2612 mutex_lock(&ftrace_lock
);
2614 if (unlikely(ftrace_disabled
))
2617 do_for_each_ftrace_rec(pg
, rec
) {
2619 if (!ftrace_match_record(rec
, NULL
, search
, len
, type
))
2622 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
2624 /* If we did not process any, then return error */
2635 * The caller might want to do something special
2636 * for each function we find. We call the callback
2637 * to give the caller an opportunity to do so.
2639 if (ops
->callback
) {
2640 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
2641 /* caller does not like this func */
2648 entry
->ip
= rec
->ip
;
2650 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2651 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2653 } while_for_each_ftrace_rec();
2654 __enable_ftrace_function_probe();
2657 mutex_unlock(&ftrace_lock
);
2663 PROBE_TEST_FUNC
= 1,
2668 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2669 void *data
, int flags
)
2671 struct ftrace_func_probe
*entry
;
2672 struct hlist_node
*n
, *tmp
;
2673 char str
[KSYM_SYMBOL_LEN
];
2674 int type
= MATCH_FULL
;
2678 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
2683 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2684 len
= strlen(search
);
2686 /* we do not support '!' for function probes */
2691 mutex_lock(&ftrace_lock
);
2692 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2693 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2695 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2697 /* break up if statements for readability */
2698 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2701 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2704 /* do this last, since it is the most expensive */
2706 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2708 if (!ftrace_match(str
, glob
, len
, type
))
2712 hlist_del(&entry
->node
);
2713 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2716 __disable_ftrace_function_probe();
2717 mutex_unlock(&ftrace_lock
);
2721 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2724 __unregister_ftrace_function_probe(glob
, ops
, data
,
2725 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2729 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2731 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2734 void unregister_ftrace_function_probe_all(char *glob
)
2736 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2739 static LIST_HEAD(ftrace_commands
);
2740 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2742 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2744 struct ftrace_func_command
*p
;
2747 mutex_lock(&ftrace_cmd_mutex
);
2748 list_for_each_entry(p
, &ftrace_commands
, list
) {
2749 if (strcmp(cmd
->name
, p
->name
) == 0) {
2754 list_add(&cmd
->list
, &ftrace_commands
);
2756 mutex_unlock(&ftrace_cmd_mutex
);
2761 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2763 struct ftrace_func_command
*p
, *n
;
2766 mutex_lock(&ftrace_cmd_mutex
);
2767 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2768 if (strcmp(cmd
->name
, p
->name
) == 0) {
2770 list_del_init(&p
->list
);
2775 mutex_unlock(&ftrace_cmd_mutex
);
2780 static int ftrace_process_regex(struct ftrace_hash
*hash
,
2781 char *buff
, int len
, int enable
)
2783 char *func
, *command
, *next
= buff
;
2784 struct ftrace_func_command
*p
;
2787 func
= strsep(&next
, ":");
2790 ret
= ftrace_match_records(hash
, func
, len
);
2800 command
= strsep(&next
, ":");
2802 mutex_lock(&ftrace_cmd_mutex
);
2803 list_for_each_entry(p
, &ftrace_commands
, list
) {
2804 if (strcmp(p
->name
, command
) == 0) {
2805 ret
= p
->func(hash
, func
, command
, next
, enable
);
2810 mutex_unlock(&ftrace_cmd_mutex
);
2816 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2817 size_t cnt
, loff_t
*ppos
, int enable
)
2819 struct ftrace_iterator
*iter
;
2820 struct trace_parser
*parser
;
2826 mutex_lock(&ftrace_regex_lock
);
2829 if (unlikely(ftrace_disabled
))
2832 if (file
->f_mode
& FMODE_READ
) {
2833 struct seq_file
*m
= file
->private_data
;
2836 iter
= file
->private_data
;
2838 parser
= &iter
->parser
;
2839 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
2841 if (read
>= 0 && trace_parser_loaded(parser
) &&
2842 !trace_parser_cont(parser
)) {
2843 ret
= ftrace_process_regex(iter
->hash
, parser
->buffer
,
2844 parser
->idx
, enable
);
2845 trace_parser_clear(parser
);
2852 mutex_unlock(&ftrace_regex_lock
);
2858 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2859 size_t cnt
, loff_t
*ppos
)
2861 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2865 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2866 size_t cnt
, loff_t
*ppos
)
2868 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2872 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
2873 int reset
, int enable
)
2875 struct ftrace_hash
**orig_hash
;
2876 struct ftrace_hash
*hash
;
2879 /* All global ops uses the global ops filters */
2880 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
)
2883 if (unlikely(ftrace_disabled
))
2887 orig_hash
= &ops
->filter_hash
;
2889 orig_hash
= &ops
->notrace_hash
;
2891 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
2895 mutex_lock(&ftrace_regex_lock
);
2897 ftrace_filter_reset(hash
);
2899 ftrace_match_records(hash
, buf
, len
);
2901 mutex_lock(&ftrace_lock
);
2902 ret
= ftrace_hash_move(ops
, enable
, orig_hash
, hash
);
2903 if (!ret
&& ops
->flags
& FTRACE_OPS_FL_ENABLED
2905 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
2907 mutex_unlock(&ftrace_lock
);
2909 mutex_unlock(&ftrace_regex_lock
);
2911 free_ftrace_hash(hash
);
2916 * ftrace_set_filter - set a function to filter on in ftrace
2917 * @ops - the ops to set the filter with
2918 * @buf - the string that holds the function filter text.
2919 * @len - the length of the string.
2920 * @reset - non zero to reset all filters before applying this filter.
2922 * Filters denote which functions should be enabled when tracing is enabled.
2923 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2925 void ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
2928 ftrace_set_regex(ops
, buf
, len
, reset
, 1);
2930 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
2933 * ftrace_set_notrace - set a function to not trace in ftrace
2934 * @ops - the ops to set the notrace filter with
2935 * @buf - the string that holds the function notrace text.
2936 * @len - the length of the string.
2937 * @reset - non zero to reset all filters before applying this filter.
2939 * Notrace Filters denote which functions should not be enabled when tracing
2940 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2943 void ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
2946 ftrace_set_regex(ops
, buf
, len
, reset
, 0);
2948 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
2950 * ftrace_set_filter - set a function to filter on in ftrace
2951 * @ops - the ops to set the filter with
2952 * @buf - the string that holds the function filter text.
2953 * @len - the length of the string.
2954 * @reset - non zero to reset all filters before applying this filter.
2956 * Filters denote which functions should be enabled when tracing is enabled.
2957 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2959 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
2961 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
2963 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
2966 * ftrace_set_notrace - set a function to not trace in ftrace
2967 * @ops - the ops to set the notrace filter with
2968 * @buf - the string that holds the function notrace text.
2969 * @len - the length of the string.
2970 * @reset - non zero to reset all filters before applying this filter.
2972 * Notrace Filters denote which functions should not be enabled when tracing
2973 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2976 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
2978 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
2980 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
2983 * command line interface to allow users to set filters on boot up.
2985 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2986 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2987 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2989 static int __init
set_ftrace_notrace(char *str
)
2991 strncpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
2994 __setup("ftrace_notrace=", set_ftrace_notrace
);
2996 static int __init
set_ftrace_filter(char *str
)
2998 strncpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
3001 __setup("ftrace_filter=", set_ftrace_filter
);
3003 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3004 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3005 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
3007 static int __init
set_graph_function(char *str
)
3009 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
3012 __setup("ftrace_graph_filter=", set_graph_function
);
3014 static void __init
set_ftrace_early_graph(char *buf
)
3020 func
= strsep(&buf
, ",");
3021 /* we allow only one expression at a time */
3022 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3025 printk(KERN_DEBUG
"ftrace: function %s not "
3026 "traceable\n", func
);
3029 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3032 set_ftrace_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
3037 func
= strsep(&buf
, ",");
3038 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
3042 static void __init
set_ftrace_early_filters(void)
3044 if (ftrace_filter_buf
[0])
3045 set_ftrace_early_filter(&global_ops
, ftrace_filter_buf
, 1);
3046 if (ftrace_notrace_buf
[0])
3047 set_ftrace_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
3048 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3049 if (ftrace_graph_buf
[0])
3050 set_ftrace_early_graph(ftrace_graph_buf
);
3051 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3055 ftrace_regex_release(struct inode
*inode
, struct file
*file
)
3057 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
3058 struct ftrace_iterator
*iter
;
3059 struct ftrace_hash
**orig_hash
;
3060 struct trace_parser
*parser
;
3064 mutex_lock(&ftrace_regex_lock
);
3065 if (file
->f_mode
& FMODE_READ
) {
3068 seq_release(inode
, file
);
3070 iter
= file
->private_data
;
3072 parser
= &iter
->parser
;
3073 if (trace_parser_loaded(parser
)) {
3074 parser
->buffer
[parser
->idx
] = 0;
3075 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
3078 trace_parser_put(parser
);
3080 if (file
->f_mode
& FMODE_WRITE
) {
3081 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
3084 orig_hash
= &iter
->ops
->filter_hash
;
3086 orig_hash
= &iter
->ops
->notrace_hash
;
3088 mutex_lock(&ftrace_lock
);
3089 ret
= ftrace_hash_move(iter
->ops
, filter_hash
,
3090 orig_hash
, iter
->hash
);
3091 if (!ret
&& (iter
->ops
->flags
& FTRACE_OPS_FL_ENABLED
)
3093 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3095 mutex_unlock(&ftrace_lock
);
3097 free_ftrace_hash(iter
->hash
);
3100 mutex_unlock(&ftrace_regex_lock
);
3104 static const struct file_operations ftrace_avail_fops
= {
3105 .open
= ftrace_avail_open
,
3107 .llseek
= seq_lseek
,
3108 .release
= seq_release_private
,
3111 static const struct file_operations ftrace_enabled_fops
= {
3112 .open
= ftrace_enabled_open
,
3114 .llseek
= seq_lseek
,
3115 .release
= seq_release_private
,
3118 static const struct file_operations ftrace_filter_fops
= {
3119 .open
= ftrace_filter_open
,
3121 .write
= ftrace_filter_write
,
3122 .llseek
= ftrace_regex_lseek
,
3123 .release
= ftrace_regex_release
,
3126 static const struct file_operations ftrace_notrace_fops
= {
3127 .open
= ftrace_notrace_open
,
3129 .write
= ftrace_notrace_write
,
3130 .llseek
= ftrace_regex_lseek
,
3131 .release
= ftrace_regex_release
,
3134 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3136 static DEFINE_MUTEX(graph_lock
);
3138 int ftrace_graph_count
;
3139 int ftrace_graph_filter_enabled
;
3140 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
3143 __g_next(struct seq_file
*m
, loff_t
*pos
)
3145 if (*pos
>= ftrace_graph_count
)
3147 return &ftrace_graph_funcs
[*pos
];
3151 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3154 return __g_next(m
, pos
);
3157 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
3159 mutex_lock(&graph_lock
);
3161 /* Nothing, tell g_show to print all functions are enabled */
3162 if (!ftrace_graph_filter_enabled
&& !*pos
)
3165 return __g_next(m
, pos
);
3168 static void g_stop(struct seq_file
*m
, void *p
)
3170 mutex_unlock(&graph_lock
);
3173 static int g_show(struct seq_file
*m
, void *v
)
3175 unsigned long *ptr
= v
;
3180 if (ptr
== (unsigned long *)1) {
3181 seq_printf(m
, "#### all functions enabled ####\n");
3185 seq_printf(m
, "%ps\n", (void *)*ptr
);
3190 static const struct seq_operations ftrace_graph_seq_ops
= {
3198 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
3202 if (unlikely(ftrace_disabled
))
3205 mutex_lock(&graph_lock
);
3206 if ((file
->f_mode
& FMODE_WRITE
) &&
3207 (file
->f_flags
& O_TRUNC
)) {
3208 ftrace_graph_filter_enabled
= 0;
3209 ftrace_graph_count
= 0;
3210 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
3212 mutex_unlock(&graph_lock
);
3214 if (file
->f_mode
& FMODE_READ
)
3215 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
3221 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
3223 if (file
->f_mode
& FMODE_READ
)
3224 seq_release(inode
, file
);
3229 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
3231 struct dyn_ftrace
*rec
;
3232 struct ftrace_page
*pg
;
3241 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
3242 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3245 search_len
= strlen(search
);
3247 mutex_lock(&ftrace_lock
);
3249 if (unlikely(ftrace_disabled
)) {
3250 mutex_unlock(&ftrace_lock
);
3254 do_for_each_ftrace_rec(pg
, rec
) {
3256 if (rec
->flags
& FTRACE_FL_FREE
)
3259 if (ftrace_match_record(rec
, NULL
, search
, search_len
, type
)) {
3260 /* if it is in the array */
3262 for (i
= 0; i
< *idx
; i
++) {
3263 if (array
[i
] == rec
->ip
) {
3272 array
[(*idx
)++] = rec
->ip
;
3273 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3278 array
[i
] = array
[--(*idx
)];
3284 } while_for_each_ftrace_rec();
3286 mutex_unlock(&ftrace_lock
);
3291 ftrace_graph_filter_enabled
= 1;
3296 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
3297 size_t cnt
, loff_t
*ppos
)
3299 struct trace_parser parser
;
3305 mutex_lock(&graph_lock
);
3307 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
3312 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
3314 if (read
>= 0 && trace_parser_loaded((&parser
))) {
3315 parser
.buffer
[parser
.idx
] = 0;
3317 /* we allow only one expression at a time */
3318 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3327 trace_parser_put(&parser
);
3329 mutex_unlock(&graph_lock
);
3334 static const struct file_operations ftrace_graph_fops
= {
3335 .open
= ftrace_graph_open
,
3337 .write
= ftrace_graph_write
,
3338 .release
= ftrace_graph_release
,
3339 .llseek
= seq_lseek
,
3341 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3343 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
3346 trace_create_file("available_filter_functions", 0444,
3347 d_tracer
, NULL
, &ftrace_avail_fops
);
3349 trace_create_file("enabled_functions", 0444,
3350 d_tracer
, NULL
, &ftrace_enabled_fops
);
3352 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
3353 NULL
, &ftrace_filter_fops
);
3355 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
3356 NULL
, &ftrace_notrace_fops
);
3358 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3359 trace_create_file("set_graph_function", 0444, d_tracer
,
3361 &ftrace_graph_fops
);
3362 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3367 static int ftrace_process_locs(struct module
*mod
,
3368 unsigned long *start
,
3373 unsigned long flags
;
3375 mutex_lock(&ftrace_lock
);
3378 addr
= ftrace_call_adjust(*p
++);
3380 * Some architecture linkers will pad between
3381 * the different mcount_loc sections of different
3382 * object files to satisfy alignments.
3383 * Skip any NULL pointers.
3387 ftrace_record_ip(addr
);
3391 * Disable interrupts to prevent interrupts from executing
3392 * code that is being modified.
3394 local_irq_save(flags
);
3395 ftrace_update_code(mod
);
3396 local_irq_restore(flags
);
3397 mutex_unlock(&ftrace_lock
);
3402 #ifdef CONFIG_MODULES
3403 void ftrace_release_mod(struct module
*mod
)
3405 struct dyn_ftrace
*rec
;
3406 struct ftrace_page
*pg
;
3408 mutex_lock(&ftrace_lock
);
3410 if (ftrace_disabled
)
3413 do_for_each_ftrace_rec(pg
, rec
) {
3414 if (within_module_core(rec
->ip
, mod
)) {
3416 * rec->ip is changed in ftrace_free_rec()
3417 * It should not between s and e if record was freed.
3419 FTRACE_WARN_ON(rec
->flags
& FTRACE_FL_FREE
);
3420 ftrace_free_rec(rec
);
3422 } while_for_each_ftrace_rec();
3424 mutex_unlock(&ftrace_lock
);
3427 static void ftrace_init_module(struct module
*mod
,
3428 unsigned long *start
, unsigned long *end
)
3430 if (ftrace_disabled
|| start
== end
)
3432 ftrace_process_locs(mod
, start
, end
);
3435 static int ftrace_module_notify(struct notifier_block
*self
,
3436 unsigned long val
, void *data
)
3438 struct module
*mod
= data
;
3441 case MODULE_STATE_COMING
:
3442 ftrace_init_module(mod
, mod
->ftrace_callsites
,
3443 mod
->ftrace_callsites
+
3444 mod
->num_ftrace_callsites
);
3446 case MODULE_STATE_GOING
:
3447 ftrace_release_mod(mod
);
3454 static int ftrace_module_notify(struct notifier_block
*self
,
3455 unsigned long val
, void *data
)
3459 #endif /* CONFIG_MODULES */
3461 struct notifier_block ftrace_module_nb
= {
3462 .notifier_call
= ftrace_module_notify
,
3466 extern unsigned long __start_mcount_loc
[];
3467 extern unsigned long __stop_mcount_loc
[];
3469 void __init
ftrace_init(void)
3471 unsigned long count
, addr
, flags
;
3474 /* Keep the ftrace pointer to the stub */
3475 addr
= (unsigned long)ftrace_stub
;
3477 local_irq_save(flags
);
3478 ftrace_dyn_arch_init(&addr
);
3479 local_irq_restore(flags
);
3481 /* ftrace_dyn_arch_init places the return code in addr */
3485 count
= __stop_mcount_loc
- __start_mcount_loc
;
3487 ret
= ftrace_dyn_table_alloc(count
);
3491 last_ftrace_enabled
= ftrace_enabled
= 1;
3493 ret
= ftrace_process_locs(NULL
,
3497 ret
= register_module_notifier(&ftrace_module_nb
);
3499 pr_warning("Failed to register trace ftrace module notifier\n");
3501 set_ftrace_early_filters();
3505 ftrace_disabled
= 1;
3510 static struct ftrace_ops global_ops
= {
3511 .func
= ftrace_stub
,
3514 static int __init
ftrace_nodyn_init(void)
3519 device_initcall(ftrace_nodyn_init
);
3521 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
3522 static inline void ftrace_startup_enable(int command
) { }
3523 /* Keep as macros so we do not need to define the commands */
3524 # define ftrace_startup(ops, command) \
3526 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3529 # define ftrace_shutdown(ops, command) do { } while (0)
3530 # define ftrace_startup_sysctl() do { } while (0)
3531 # define ftrace_shutdown_sysctl() do { } while (0)
3534 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
)
3539 #endif /* CONFIG_DYNAMIC_FTRACE */
3542 ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
)
3544 struct ftrace_ops
*op
;
3546 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT
)))
3549 trace_recursion_set(TRACE_INTERNAL_BIT
);
3551 * Some of the ops may be dynamically allocated,
3552 * they must be freed after a synchronize_sched().
3554 preempt_disable_notrace();
3555 op
= rcu_dereference_raw(ftrace_ops_list
);
3556 while (op
!= &ftrace_list_end
) {
3557 if (ftrace_ops_test(op
, ip
))
3558 op
->func(ip
, parent_ip
);
3559 op
= rcu_dereference_raw(op
->next
);
3561 preempt_enable_notrace();
3562 trace_recursion_clear(TRACE_INTERNAL_BIT
);
3565 static void clear_ftrace_swapper(void)
3567 struct task_struct
*p
;
3571 for_each_online_cpu(cpu
) {
3573 clear_tsk_trace_trace(p
);
3578 static void set_ftrace_swapper(void)
3580 struct task_struct
*p
;
3584 for_each_online_cpu(cpu
) {
3586 set_tsk_trace_trace(p
);
3591 static void clear_ftrace_pid(struct pid
*pid
)
3593 struct task_struct
*p
;
3596 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
3597 clear_tsk_trace_trace(p
);
3598 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
3604 static void set_ftrace_pid(struct pid
*pid
)
3606 struct task_struct
*p
;
3609 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
3610 set_tsk_trace_trace(p
);
3611 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
3615 static void clear_ftrace_pid_task(struct pid
*pid
)
3617 if (pid
== ftrace_swapper_pid
)
3618 clear_ftrace_swapper();
3620 clear_ftrace_pid(pid
);
3623 static void set_ftrace_pid_task(struct pid
*pid
)
3625 if (pid
== ftrace_swapper_pid
)
3626 set_ftrace_swapper();
3628 set_ftrace_pid(pid
);
3631 static int ftrace_pid_add(int p
)
3634 struct ftrace_pid
*fpid
;
3637 mutex_lock(&ftrace_lock
);
3640 pid
= ftrace_swapper_pid
;
3642 pid
= find_get_pid(p
);
3649 list_for_each_entry(fpid
, &ftrace_pids
, list
)
3650 if (fpid
->pid
== pid
)
3655 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
3659 list_add(&fpid
->list
, &ftrace_pids
);
3662 set_ftrace_pid_task(pid
);
3664 ftrace_update_pid_func();
3665 ftrace_startup_enable(0);
3667 mutex_unlock(&ftrace_lock
);
3671 if (pid
!= ftrace_swapper_pid
)
3675 mutex_unlock(&ftrace_lock
);
3679 static void ftrace_pid_reset(void)
3681 struct ftrace_pid
*fpid
, *safe
;
3683 mutex_lock(&ftrace_lock
);
3684 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
3685 struct pid
*pid
= fpid
->pid
;
3687 clear_ftrace_pid_task(pid
);
3689 list_del(&fpid
->list
);
3693 ftrace_update_pid_func();
3694 ftrace_startup_enable(0);
3696 mutex_unlock(&ftrace_lock
);
3699 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
3701 mutex_lock(&ftrace_lock
);
3703 if (list_empty(&ftrace_pids
) && (!*pos
))
3706 return seq_list_start(&ftrace_pids
, *pos
);
3709 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3714 return seq_list_next(v
, &ftrace_pids
, pos
);
3717 static void fpid_stop(struct seq_file
*m
, void *p
)
3719 mutex_unlock(&ftrace_lock
);
3722 static int fpid_show(struct seq_file
*m
, void *v
)
3724 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
3726 if (v
== (void *)1) {
3727 seq_printf(m
, "no pid\n");
3731 if (fpid
->pid
== ftrace_swapper_pid
)
3732 seq_printf(m
, "swapper tasks\n");
3734 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
3739 static const struct seq_operations ftrace_pid_sops
= {
3740 .start
= fpid_start
,
3747 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
3751 if ((file
->f_mode
& FMODE_WRITE
) &&
3752 (file
->f_flags
& O_TRUNC
))
3755 if (file
->f_mode
& FMODE_READ
)
3756 ret
= seq_open(file
, &ftrace_pid_sops
);
3762 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
3763 size_t cnt
, loff_t
*ppos
)
3769 if (cnt
>= sizeof(buf
))
3772 if (copy_from_user(&buf
, ubuf
, cnt
))
3778 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3779 * to clean the filter quietly.
3781 tmp
= strstrip(buf
);
3782 if (strlen(tmp
) == 0)
3785 ret
= strict_strtol(tmp
, 10, &val
);
3789 ret
= ftrace_pid_add(val
);
3791 return ret
? ret
: cnt
;
3795 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
3797 if (file
->f_mode
& FMODE_READ
)
3798 seq_release(inode
, file
);
3803 static const struct file_operations ftrace_pid_fops
= {
3804 .open
= ftrace_pid_open
,
3805 .write
= ftrace_pid_write
,
3807 .llseek
= seq_lseek
,
3808 .release
= ftrace_pid_release
,
3811 static __init
int ftrace_init_debugfs(void)
3813 struct dentry
*d_tracer
;
3815 d_tracer
= tracing_init_dentry();
3819 ftrace_init_dyn_debugfs(d_tracer
);
3821 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
3822 NULL
, &ftrace_pid_fops
);
3824 ftrace_profile_debugfs(d_tracer
);
3828 fs_initcall(ftrace_init_debugfs
);
3831 * ftrace_kill - kill ftrace
3833 * This function should be used by panic code. It stops ftrace
3834 * but in a not so nice way. If you need to simply kill ftrace
3835 * from a non-atomic section, use ftrace_kill.
3837 void ftrace_kill(void)
3839 ftrace_disabled
= 1;
3841 clear_ftrace_function();
3845 * register_ftrace_function - register a function for profiling
3846 * @ops - ops structure that holds the function for profiling.
3848 * Register a function to be called by all functions in the
3851 * Note: @ops->func and all the functions it calls must be labeled
3852 * with "notrace", otherwise it will go into a
3855 int register_ftrace_function(struct ftrace_ops
*ops
)
3859 mutex_lock(&ftrace_lock
);
3861 if (unlikely(ftrace_disabled
))
3864 ret
= __register_ftrace_function(ops
);
3866 ret
= ftrace_startup(ops
, 0);
3870 mutex_unlock(&ftrace_lock
);
3873 EXPORT_SYMBOL_GPL(register_ftrace_function
);
3876 * unregister_ftrace_function - unregister a function for profiling.
3877 * @ops - ops structure that holds the function to unregister
3879 * Unregister a function that was added to be called by ftrace profiling.
3881 int unregister_ftrace_function(struct ftrace_ops
*ops
)
3885 mutex_lock(&ftrace_lock
);
3886 ret
= __unregister_ftrace_function(ops
);
3888 ftrace_shutdown(ops
, 0);
3889 mutex_unlock(&ftrace_lock
);
3893 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
3896 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
3897 void __user
*buffer
, size_t *lenp
,
3902 mutex_lock(&ftrace_lock
);
3904 if (unlikely(ftrace_disabled
))
3907 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3909 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
3912 last_ftrace_enabled
= !!ftrace_enabled
;
3914 if (ftrace_enabled
) {
3916 ftrace_startup_sysctl();
3918 /* we are starting ftrace again */
3919 if (ftrace_ops_list
!= &ftrace_list_end
) {
3920 if (ftrace_ops_list
->next
== &ftrace_list_end
)
3921 ftrace_trace_function
= ftrace_ops_list
->func
;
3923 ftrace_trace_function
= ftrace_ops_list_func
;
3927 /* stopping ftrace calls (just send to ftrace_stub) */
3928 ftrace_trace_function
= ftrace_stub
;
3930 ftrace_shutdown_sysctl();
3934 mutex_unlock(&ftrace_lock
);
3938 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3940 static int ftrace_graph_active
;
3941 static struct notifier_block ftrace_suspend_notifier
;
3943 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3948 /* The callbacks that hook a function */
3949 trace_func_graph_ret_t ftrace_graph_return
=
3950 (trace_func_graph_ret_t
)ftrace_stub
;
3951 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3953 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3954 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3958 unsigned long flags
;
3959 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3960 struct task_struct
*g
, *t
;
3962 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3963 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3964 * sizeof(struct ftrace_ret_stack
),
3966 if (!ret_stack_list
[i
]) {
3974 read_lock_irqsave(&tasklist_lock
, flags
);
3975 do_each_thread(g
, t
) {
3981 if (t
->ret_stack
== NULL
) {
3982 atomic_set(&t
->tracing_graph_pause
, 0);
3983 atomic_set(&t
->trace_overrun
, 0);
3984 t
->curr_ret_stack
= -1;
3985 /* Make sure the tasks see the -1 first: */
3987 t
->ret_stack
= ret_stack_list
[start
++];
3989 } while_each_thread(g
, t
);
3992 read_unlock_irqrestore(&tasklist_lock
, flags
);
3994 for (i
= start
; i
< end
; i
++)
3995 kfree(ret_stack_list
[i
]);
4000 ftrace_graph_probe_sched_switch(void *ignore
,
4001 struct task_struct
*prev
, struct task_struct
*next
)
4003 unsigned long long timestamp
;
4007 * Does the user want to count the time a function was asleep.
4008 * If so, do not update the time stamps.
4010 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
4013 timestamp
= trace_clock_local();
4015 prev
->ftrace_timestamp
= timestamp
;
4017 /* only process tasks that we timestamped */
4018 if (!next
->ftrace_timestamp
)
4022 * Update all the counters in next to make up for the
4023 * time next was sleeping.
4025 timestamp
-= next
->ftrace_timestamp
;
4027 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
4028 next
->ret_stack
[index
].calltime
+= timestamp
;
4031 /* Allocate a return stack for each task */
4032 static int start_graph_tracing(void)
4034 struct ftrace_ret_stack
**ret_stack_list
;
4037 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
4038 sizeof(struct ftrace_ret_stack
*),
4041 if (!ret_stack_list
)
4044 /* The cpu_boot init_task->ret_stack will never be freed */
4045 for_each_online_cpu(cpu
) {
4046 if (!idle_task(cpu
)->ret_stack
)
4047 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
4051 ret
= alloc_retstack_tasklist(ret_stack_list
);
4052 } while (ret
== -EAGAIN
);
4055 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4057 pr_info("ftrace_graph: Couldn't activate tracepoint"
4058 " probe to kernel_sched_switch\n");
4061 kfree(ret_stack_list
);
4066 * Hibernation protection.
4067 * The state of the current task is too much unstable during
4068 * suspend/restore to disk. We want to protect against that.
4071 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
4075 case PM_HIBERNATION_PREPARE
:
4076 pause_graph_tracing();
4079 case PM_POST_HIBERNATION
:
4080 unpause_graph_tracing();
4086 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
4087 trace_func_graph_ent_t entryfunc
)
4091 mutex_lock(&ftrace_lock
);
4093 /* we currently allow only one tracer registered at a time */
4094 if (ftrace_graph_active
) {
4099 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
4100 register_pm_notifier(&ftrace_suspend_notifier
);
4102 ftrace_graph_active
++;
4103 ret
= start_graph_tracing();
4105 ftrace_graph_active
--;
4109 ftrace_graph_return
= retfunc
;
4110 ftrace_graph_entry
= entryfunc
;
4112 ret
= ftrace_startup(&global_ops
, FTRACE_START_FUNC_RET
);
4115 mutex_unlock(&ftrace_lock
);
4119 void unregister_ftrace_graph(void)
4121 mutex_lock(&ftrace_lock
);
4123 if (unlikely(!ftrace_graph_active
))
4126 ftrace_graph_active
--;
4127 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
4128 ftrace_graph_entry
= ftrace_graph_entry_stub
;
4129 ftrace_shutdown(&global_ops
, FTRACE_STOP_FUNC_RET
);
4130 unregister_pm_notifier(&ftrace_suspend_notifier
);
4131 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4134 mutex_unlock(&ftrace_lock
);
4137 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
4140 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
4142 atomic_set(&t
->tracing_graph_pause
, 0);
4143 atomic_set(&t
->trace_overrun
, 0);
4144 t
->ftrace_timestamp
= 0;
4145 /* make curr_ret_stack visible before we add the ret_stack */
4147 t
->ret_stack
= ret_stack
;
4151 * Allocate a return stack for the idle task. May be the first
4152 * time through, or it may be done by CPU hotplug online.
4154 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
4156 t
->curr_ret_stack
= -1;
4158 * The idle task has no parent, it either has its own
4159 * stack or no stack at all.
4162 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
4164 if (ftrace_graph_active
) {
4165 struct ftrace_ret_stack
*ret_stack
;
4167 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
4169 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4170 * sizeof(struct ftrace_ret_stack
),
4174 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
4176 graph_init_task(t
, ret_stack
);
4180 /* Allocate a return stack for newly created task */
4181 void ftrace_graph_init_task(struct task_struct
*t
)
4183 /* Make sure we do not use the parent ret_stack */
4184 t
->ret_stack
= NULL
;
4185 t
->curr_ret_stack
= -1;
4187 if (ftrace_graph_active
) {
4188 struct ftrace_ret_stack
*ret_stack
;
4190 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4191 * sizeof(struct ftrace_ret_stack
),
4195 graph_init_task(t
, ret_stack
);
4199 void ftrace_graph_exit_task(struct task_struct
*t
)
4201 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
4203 t
->ret_stack
= NULL
;
4204 /* NULL must become visible to IRQs before we free it: */
4210 void ftrace_graph_stop(void)