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_ENABLE_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_move(struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1188 struct ftrace_func_entry
*entry
;
1189 struct hlist_node
*tp
, *tn
;
1190 struct hlist_head
*hhd
;
1191 struct ftrace_hash
*old_hash
;
1192 struct ftrace_hash
*new_hash
;
1194 int size
= src
->count
;
1199 * If the new source is empty, just free dst and assign it
1203 free_ftrace_hash_rcu(*dst
);
1204 rcu_assign_pointer(*dst
, EMPTY_HASH
);
1209 * Make the hash size about 1/2 the # found
1211 for (size
/= 2; size
; size
>>= 1)
1214 /* Don't allocate too much */
1215 if (bits
> FTRACE_HASH_MAX_BITS
)
1216 bits
= FTRACE_HASH_MAX_BITS
;
1218 new_hash
= alloc_ftrace_hash(bits
);
1222 size
= 1 << src
->size_bits
;
1223 for (i
= 0; i
< size
; i
++) {
1224 hhd
= &src
->buckets
[i
];
1225 hlist_for_each_entry_safe(entry
, tp
, tn
, hhd
, hlist
) {
1227 key
= hash_long(entry
->ip
, bits
);
1230 remove_hash_entry(src
, entry
);
1231 __add_hash_entry(new_hash
, entry
);
1236 rcu_assign_pointer(*dst
, new_hash
);
1237 free_ftrace_hash_rcu(old_hash
);
1243 * Test the hashes for this ops to see if we want to call
1244 * the ops->func or not.
1246 * It's a match if the ip is in the ops->filter_hash or
1247 * the filter_hash does not exist or is empty,
1249 * the ip is not in the ops->notrace_hash.
1251 * This needs to be called with preemption disabled as
1252 * the hashes are freed with call_rcu_sched().
1255 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
)
1257 struct ftrace_hash
*filter_hash
;
1258 struct ftrace_hash
*notrace_hash
;
1261 filter_hash
= rcu_dereference_raw(ops
->filter_hash
);
1262 notrace_hash
= rcu_dereference_raw(ops
->notrace_hash
);
1264 if ((!filter_hash
|| !filter_hash
->count
||
1265 ftrace_lookup_ip(filter_hash
, ip
)) &&
1266 (!notrace_hash
|| !notrace_hash
->count
||
1267 !ftrace_lookup_ip(notrace_hash
, ip
)))
1276 * This is a double for. Do not use 'break' to break out of the loop,
1277 * you must use a goto.
1279 #define do_for_each_ftrace_rec(pg, rec) \
1280 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1282 for (_____i = 0; _____i < pg->index; _____i++) { \
1283 rec = &pg->records[_____i];
1285 #define while_for_each_ftrace_rec() \
1289 static void __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1293 struct ftrace_hash
*hash
;
1294 struct ftrace_hash
*other_hash
;
1295 struct ftrace_page
*pg
;
1296 struct dyn_ftrace
*rec
;
1300 /* Only update if the ops has been registered */
1301 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1305 * In the filter_hash case:
1306 * If the count is zero, we update all records.
1307 * Otherwise we just update the items in the hash.
1309 * In the notrace_hash case:
1310 * We enable the update in the hash.
1311 * As disabling notrace means enabling the tracing,
1312 * and enabling notrace means disabling, the inc variable
1316 hash
= ops
->filter_hash
;
1317 other_hash
= ops
->notrace_hash
;
1318 if (!hash
|| !hash
->count
)
1322 hash
= ops
->notrace_hash
;
1323 other_hash
= ops
->filter_hash
;
1325 * If the notrace hash has no items,
1326 * then there's nothing to do.
1328 if (hash
&& !hash
->count
)
1332 do_for_each_ftrace_rec(pg
, rec
) {
1333 int in_other_hash
= 0;
1339 * Only the filter_hash affects all records.
1340 * Update if the record is not in the notrace hash.
1342 if (!other_hash
|| !ftrace_lookup_ip(other_hash
, rec
->ip
))
1345 in_hash
= hash
&& !!ftrace_lookup_ip(hash
, rec
->ip
);
1346 in_other_hash
= other_hash
&& !!ftrace_lookup_ip(other_hash
, rec
->ip
);
1351 if (filter_hash
&& in_hash
&& !in_other_hash
)
1353 else if (!filter_hash
&& in_hash
&&
1354 (in_other_hash
|| !other_hash
->count
))
1362 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == FTRACE_REF_MAX
))
1365 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == 0))
1370 /* Shortcut, if we handled all records, we are done. */
1371 if (!all
&& count
== hash
->count
)
1373 } while_for_each_ftrace_rec();
1376 static void ftrace_hash_rec_disable(struct ftrace_ops
*ops
,
1379 __ftrace_hash_rec_update(ops
, filter_hash
, 0);
1382 static void ftrace_hash_rec_enable(struct ftrace_ops
*ops
,
1385 __ftrace_hash_rec_update(ops
, filter_hash
, 1);
1388 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
1390 rec
->freelist
= ftrace_free_records
;
1391 ftrace_free_records
= rec
;
1392 rec
->flags
|= FTRACE_FL_FREE
;
1395 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
1397 struct dyn_ftrace
*rec
;
1399 /* First check for freed records */
1400 if (ftrace_free_records
) {
1401 rec
= ftrace_free_records
;
1403 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
1404 FTRACE_WARN_ON_ONCE(1);
1405 ftrace_free_records
= NULL
;
1409 ftrace_free_records
= rec
->freelist
;
1410 memset(rec
, 0, sizeof(*rec
));
1414 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
1415 if (!ftrace_pages
->next
) {
1416 /* allocate another page */
1417 ftrace_pages
->next
=
1418 (void *)get_zeroed_page(GFP_KERNEL
);
1419 if (!ftrace_pages
->next
)
1422 ftrace_pages
= ftrace_pages
->next
;
1425 return &ftrace_pages
->records
[ftrace_pages
->index
++];
1428 static struct dyn_ftrace
*
1429 ftrace_record_ip(unsigned long ip
)
1431 struct dyn_ftrace
*rec
;
1433 if (ftrace_disabled
)
1436 rec
= ftrace_alloc_dyn_node(ip
);
1441 rec
->newlist
= ftrace_new_addrs
;
1442 ftrace_new_addrs
= rec
;
1447 static void print_ip_ins(const char *fmt
, unsigned char *p
)
1451 printk(KERN_CONT
"%s", fmt
);
1453 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
1454 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
1457 static void ftrace_bug(int failed
, unsigned long ip
)
1461 FTRACE_WARN_ON_ONCE(1);
1462 pr_info("ftrace faulted on modifying ");
1466 FTRACE_WARN_ON_ONCE(1);
1467 pr_info("ftrace failed to modify ");
1469 print_ip_ins(" actual: ", (unsigned char *)ip
);
1470 printk(KERN_CONT
"\n");
1473 FTRACE_WARN_ON_ONCE(1);
1474 pr_info("ftrace faulted on writing ");
1478 FTRACE_WARN_ON_ONCE(1);
1479 pr_info("ftrace faulted on unknown error ");
1485 /* Return 1 if the address range is reserved for ftrace */
1486 int ftrace_text_reserved(void *start
, void *end
)
1488 struct dyn_ftrace
*rec
;
1489 struct ftrace_page
*pg
;
1491 do_for_each_ftrace_rec(pg
, rec
) {
1492 if (rec
->ip
<= (unsigned long)end
&&
1493 rec
->ip
+ MCOUNT_INSN_SIZE
> (unsigned long)start
)
1495 } while_for_each_ftrace_rec();
1501 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1503 unsigned long ftrace_addr
;
1504 unsigned long flag
= 0UL;
1506 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1509 * If we are enabling tracing:
1511 * If the record has a ref count, then we need to enable it
1512 * because someone is using it.
1514 * Otherwise we make sure its disabled.
1516 * If we are disabling tracing, then disable all records that
1519 if (enable
&& (rec
->flags
& ~FTRACE_FL_MASK
))
1520 flag
= FTRACE_FL_ENABLED
;
1522 /* If the state of this record hasn't changed, then do nothing */
1523 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1527 rec
->flags
|= FTRACE_FL_ENABLED
;
1528 return ftrace_make_call(rec
, ftrace_addr
);
1531 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1532 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1535 static void ftrace_replace_code(int enable
)
1537 struct dyn_ftrace
*rec
;
1538 struct ftrace_page
*pg
;
1541 if (unlikely(ftrace_disabled
))
1544 do_for_each_ftrace_rec(pg
, rec
) {
1545 /* Skip over free records */
1546 if (rec
->flags
& FTRACE_FL_FREE
)
1549 failed
= __ftrace_replace_code(rec
, enable
);
1551 ftrace_bug(failed
, rec
->ip
);
1552 /* Stop processing */
1555 } while_for_each_ftrace_rec();
1559 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1566 if (unlikely(ftrace_disabled
))
1569 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1571 ftrace_bug(ret
, ip
);
1578 * archs can override this function if they must do something
1579 * before the modifying code is performed.
1581 int __weak
ftrace_arch_code_modify_prepare(void)
1587 * archs can override this function if they must do something
1588 * after the modifying code is performed.
1590 int __weak
ftrace_arch_code_modify_post_process(void)
1595 static int __ftrace_modify_code(void *data
)
1597 int *command
= data
;
1599 if (*command
& FTRACE_ENABLE_CALLS
)
1600 ftrace_replace_code(1);
1601 else if (*command
& FTRACE_DISABLE_CALLS
)
1602 ftrace_replace_code(0);
1604 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1605 ftrace_update_ftrace_func(ftrace_trace_function
);
1607 if (*command
& FTRACE_START_FUNC_RET
)
1608 ftrace_enable_ftrace_graph_caller();
1609 else if (*command
& FTRACE_STOP_FUNC_RET
)
1610 ftrace_disable_ftrace_graph_caller();
1615 static void ftrace_run_update_code(int command
)
1619 ret
= ftrace_arch_code_modify_prepare();
1620 FTRACE_WARN_ON(ret
);
1624 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1626 ret
= ftrace_arch_code_modify_post_process();
1627 FTRACE_WARN_ON(ret
);
1630 static ftrace_func_t saved_ftrace_func
;
1631 static int ftrace_start_up
;
1632 static int global_start_up
;
1634 static void ftrace_startup_enable(int command
)
1636 if (saved_ftrace_func
!= ftrace_trace_function
) {
1637 saved_ftrace_func
= ftrace_trace_function
;
1638 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1641 if (!command
|| !ftrace_enabled
)
1644 ftrace_run_update_code(command
);
1647 static int ftrace_startup(struct ftrace_ops
*ops
, int command
)
1649 bool hash_enable
= true;
1651 if (unlikely(ftrace_disabled
))
1655 command
|= FTRACE_ENABLE_CALLS
;
1657 /* ops marked global share the filter hashes */
1658 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
1660 /* Don't update hash if global is already set */
1661 if (global_start_up
)
1662 hash_enable
= false;
1666 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
1668 ftrace_hash_rec_enable(ops
, 1);
1670 ftrace_startup_enable(command
);
1675 static void ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
1677 bool hash_disable
= true;
1679 if (unlikely(ftrace_disabled
))
1684 * Just warn in case of unbalance, no need to kill ftrace, it's not
1685 * critical but the ftrace_call callers may be never nopped again after
1686 * further ftrace uses.
1688 WARN_ON_ONCE(ftrace_start_up
< 0);
1690 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
1693 WARN_ON_ONCE(global_start_up
< 0);
1694 /* Don't update hash if global still has users */
1695 if (global_start_up
) {
1696 WARN_ON_ONCE(!ftrace_start_up
);
1697 hash_disable
= false;
1702 ftrace_hash_rec_disable(ops
, 1);
1704 if (ops
!= &global_ops
|| !global_start_up
)
1705 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
1707 if (!ftrace_start_up
)
1708 command
|= FTRACE_DISABLE_CALLS
;
1710 if (saved_ftrace_func
!= ftrace_trace_function
) {
1711 saved_ftrace_func
= ftrace_trace_function
;
1712 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1715 if (!command
|| !ftrace_enabled
)
1718 ftrace_run_update_code(command
);
1721 static void ftrace_startup_sysctl(void)
1723 if (unlikely(ftrace_disabled
))
1726 /* Force update next time */
1727 saved_ftrace_func
= NULL
;
1728 /* ftrace_start_up is true if we want ftrace running */
1729 if (ftrace_start_up
)
1730 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
1733 static void ftrace_shutdown_sysctl(void)
1735 if (unlikely(ftrace_disabled
))
1738 /* ftrace_start_up is true if ftrace is running */
1739 if (ftrace_start_up
)
1740 ftrace_run_update_code(FTRACE_DISABLE_CALLS
);
1743 static cycle_t ftrace_update_time
;
1744 static unsigned long ftrace_update_cnt
;
1745 unsigned long ftrace_update_tot_cnt
;
1747 static int ops_traces_mod(struct ftrace_ops
*ops
)
1749 struct ftrace_hash
*hash
;
1751 hash
= ops
->filter_hash
;
1752 return !!(!hash
|| !hash
->count
);
1755 static int ftrace_update_code(struct module
*mod
)
1757 struct dyn_ftrace
*p
;
1758 cycle_t start
, stop
;
1759 unsigned long ref
= 0;
1762 * When adding a module, we need to check if tracers are
1763 * currently enabled and if they are set to trace all functions.
1764 * If they are, we need to enable the module functions as well
1765 * as update the reference counts for those function records.
1768 struct ftrace_ops
*ops
;
1770 for (ops
= ftrace_ops_list
;
1771 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
1772 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
&&
1773 ops_traces_mod(ops
))
1778 start
= ftrace_now(raw_smp_processor_id());
1779 ftrace_update_cnt
= 0;
1781 while (ftrace_new_addrs
) {
1783 /* If something went wrong, bail without enabling anything */
1784 if (unlikely(ftrace_disabled
))
1787 p
= ftrace_new_addrs
;
1788 ftrace_new_addrs
= p
->newlist
;
1792 * Do the initial record conversion from mcount jump
1793 * to the NOP instructions.
1795 if (!ftrace_code_disable(mod
, p
)) {
1801 ftrace_update_cnt
++;
1804 * If the tracing is enabled, go ahead and enable the record.
1806 * The reason not to enable the record immediatelly is the
1807 * inherent check of ftrace_make_nop/ftrace_make_call for
1808 * correct previous instructions. Making first the NOP
1809 * conversion puts the module to the correct state, thus
1810 * passing the ftrace_make_call check.
1812 if (ftrace_start_up
&& ref
) {
1813 int failed
= __ftrace_replace_code(p
, 1);
1815 ftrace_bug(failed
, p
->ip
);
1821 stop
= ftrace_now(raw_smp_processor_id());
1822 ftrace_update_time
= stop
- start
;
1823 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1828 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1830 struct ftrace_page
*pg
;
1834 /* allocate a few pages */
1835 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1836 if (!ftrace_pages_start
)
1840 * Allocate a few more pages.
1842 * TODO: have some parser search vmlinux before
1843 * final linking to find all calls to ftrace.
1845 * a) know how many pages to allocate.
1847 * b) set up the table then.
1849 * The dynamic code is still necessary for
1853 pg
= ftrace_pages
= ftrace_pages_start
;
1855 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1856 pr_info("ftrace: allocating %ld entries in %d pages\n",
1857 num_to_init
, cnt
+ 1);
1859 for (i
= 0; i
< cnt
; i
++) {
1860 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1862 /* If we fail, we'll try later anyway */
1873 FTRACE_ITER_FILTER
= (1 << 0),
1874 FTRACE_ITER_NOTRACE
= (1 << 1),
1875 FTRACE_ITER_PRINTALL
= (1 << 2),
1876 FTRACE_ITER_HASH
= (1 << 3),
1877 FTRACE_ITER_ENABLED
= (1 << 4),
1880 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1882 struct ftrace_iterator
{
1885 struct ftrace_page
*pg
;
1886 struct dyn_ftrace
*func
;
1887 struct ftrace_func_probe
*probe
;
1888 struct trace_parser parser
;
1889 struct ftrace_hash
*hash
;
1890 struct ftrace_ops
*ops
;
1897 t_hash_next(struct seq_file
*m
, loff_t
*pos
)
1899 struct ftrace_iterator
*iter
= m
->private;
1900 struct hlist_node
*hnd
= NULL
;
1901 struct hlist_head
*hhd
;
1907 hnd
= &iter
->probe
->node
;
1909 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1912 hhd
= &ftrace_func_hash
[iter
->hidx
];
1914 if (hlist_empty(hhd
)) {
1930 if (WARN_ON_ONCE(!hnd
))
1933 iter
->probe
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1938 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1940 struct ftrace_iterator
*iter
= m
->private;
1944 if (iter
->func_pos
> *pos
)
1948 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
1949 p
= t_hash_next(m
, &l
);
1956 /* Only set this if we have an item */
1957 iter
->flags
|= FTRACE_ITER_HASH
;
1963 t_hash_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
1965 struct ftrace_func_probe
*rec
;
1968 if (WARN_ON_ONCE(!rec
))
1971 if (rec
->ops
->print
)
1972 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1974 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
1977 seq_printf(m
, ":%p", rec
->data
);
1984 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1986 struct ftrace_iterator
*iter
= m
->private;
1987 struct ftrace_ops
*ops
= &global_ops
;
1988 struct dyn_ftrace
*rec
= NULL
;
1990 if (unlikely(ftrace_disabled
))
1993 if (iter
->flags
& FTRACE_ITER_HASH
)
1994 return t_hash_next(m
, pos
);
1997 iter
->pos
= iter
->func_pos
= *pos
;
1999 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
2000 return t_hash_start(m
, pos
);
2003 if (iter
->idx
>= iter
->pg
->index
) {
2004 if (iter
->pg
->next
) {
2005 iter
->pg
= iter
->pg
->next
;
2010 rec
= &iter
->pg
->records
[iter
->idx
++];
2011 if ((rec
->flags
& FTRACE_FL_FREE
) ||
2013 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
2014 !(ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))) ||
2016 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
2017 !ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
)) ||
2019 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
2020 !(rec
->flags
& ~FTRACE_FL_MASK
))) {
2028 return t_hash_start(m
, pos
);
2035 static void reset_iter_read(struct ftrace_iterator
*iter
)
2039 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
& FTRACE_ITER_HASH
);
2042 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2044 struct ftrace_iterator
*iter
= m
->private;
2045 struct ftrace_ops
*ops
= &global_ops
;
2049 mutex_lock(&ftrace_lock
);
2051 if (unlikely(ftrace_disabled
))
2055 * If an lseek was done, then reset and start from beginning.
2057 if (*pos
< iter
->pos
)
2058 reset_iter_read(iter
);
2061 * For set_ftrace_filter reading, if we have the filter
2062 * off, we can short cut and just print out that all
2063 * functions are enabled.
2065 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ops
->filter_hash
->count
) {
2067 return t_hash_start(m
, pos
);
2068 iter
->flags
|= FTRACE_ITER_PRINTALL
;
2069 /* reset in case of seek/pread */
2070 iter
->flags
&= ~FTRACE_ITER_HASH
;
2074 if (iter
->flags
& FTRACE_ITER_HASH
)
2075 return t_hash_start(m
, pos
);
2078 * Unfortunately, we need to restart at ftrace_pages_start
2079 * every time we let go of the ftrace_mutex. This is because
2080 * those pointers can change without the lock.
2082 iter
->pg
= ftrace_pages_start
;
2084 for (l
= 0; l
<= *pos
; ) {
2085 p
= t_next(m
, p
, &l
);
2091 if (iter
->flags
& FTRACE_ITER_FILTER
)
2092 return t_hash_start(m
, pos
);
2100 static void t_stop(struct seq_file
*m
, void *p
)
2102 mutex_unlock(&ftrace_lock
);
2105 static int t_show(struct seq_file
*m
, void *v
)
2107 struct ftrace_iterator
*iter
= m
->private;
2108 struct dyn_ftrace
*rec
;
2110 if (iter
->flags
& FTRACE_ITER_HASH
)
2111 return t_hash_show(m
, iter
);
2113 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
2114 seq_printf(m
, "#### all functions enabled ####\n");
2123 seq_printf(m
, "%ps", (void *)rec
->ip
);
2124 if (iter
->flags
& FTRACE_ITER_ENABLED
)
2125 seq_printf(m
, " (%ld)",
2126 rec
->flags
& ~FTRACE_FL_MASK
);
2127 seq_printf(m
, "\n");
2132 static const struct seq_operations show_ftrace_seq_ops
= {
2140 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
2142 struct ftrace_iterator
*iter
;
2145 if (unlikely(ftrace_disabled
))
2148 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2152 iter
->pg
= ftrace_pages_start
;
2154 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2156 struct seq_file
*m
= file
->private_data
;
2167 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
2169 struct ftrace_iterator
*iter
;
2172 if (unlikely(ftrace_disabled
))
2175 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2179 iter
->pg
= ftrace_pages_start
;
2180 iter
->flags
= FTRACE_ITER_ENABLED
;
2182 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2184 struct seq_file
*m
= file
->private_data
;
2194 static void ftrace_filter_reset(struct ftrace_hash
*hash
)
2196 mutex_lock(&ftrace_lock
);
2197 ftrace_hash_clear(hash
);
2198 mutex_unlock(&ftrace_lock
);
2202 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
2203 struct inode
*inode
, struct file
*file
)
2205 struct ftrace_iterator
*iter
;
2206 struct ftrace_hash
*hash
;
2209 if (unlikely(ftrace_disabled
))
2212 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2216 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
2221 if (flag
& FTRACE_ITER_NOTRACE
)
2222 hash
= ops
->notrace_hash
;
2224 hash
= ops
->filter_hash
;
2229 if (file
->f_mode
& FMODE_WRITE
) {
2230 mutex_lock(&ftrace_lock
);
2231 iter
->hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, hash
);
2232 mutex_unlock(&ftrace_lock
);
2235 trace_parser_put(&iter
->parser
);
2241 mutex_lock(&ftrace_regex_lock
);
2243 if ((file
->f_mode
& FMODE_WRITE
) &&
2244 (file
->f_flags
& O_TRUNC
))
2245 ftrace_filter_reset(iter
->hash
);
2247 if (file
->f_mode
& FMODE_READ
) {
2248 iter
->pg
= ftrace_pages_start
;
2250 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2252 struct seq_file
*m
= file
->private_data
;
2256 free_ftrace_hash(iter
->hash
);
2257 trace_parser_put(&iter
->parser
);
2261 file
->private_data
= iter
;
2262 mutex_unlock(&ftrace_regex_lock
);
2268 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
2270 return ftrace_regex_open(&global_ops
, FTRACE_ITER_FILTER
,
2275 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
2277 return ftrace_regex_open(&global_ops
, FTRACE_ITER_NOTRACE
,
2282 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
2286 if (file
->f_mode
& FMODE_READ
)
2287 ret
= seq_lseek(file
, offset
, origin
);
2289 file
->f_pos
= ret
= 1;
2294 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
2301 if (strcmp(str
, regex
) == 0)
2304 case MATCH_FRONT_ONLY
:
2305 if (strncmp(str
, regex
, len
) == 0)
2308 case MATCH_MIDDLE_ONLY
:
2309 if (strstr(str
, regex
))
2312 case MATCH_END_ONLY
:
2314 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
2323 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int not)
2325 struct ftrace_func_entry
*entry
;
2328 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
2330 /* Do nothing if it doesn't exist */
2334 free_hash_entry(hash
, entry
);
2336 /* Do nothing if it exists */
2340 ret
= add_hash_entry(hash
, rec
->ip
);
2346 ftrace_match_record(struct dyn_ftrace
*rec
, char *mod
,
2347 char *regex
, int len
, int type
)
2349 char str
[KSYM_SYMBOL_LEN
];
2352 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
2355 /* module lookup requires matching the module */
2356 if (!modname
|| strcmp(modname
, mod
))
2359 /* blank search means to match all funcs in the mod */
2364 return ftrace_match(str
, regex
, len
, type
);
2368 match_records(struct ftrace_hash
*hash
, char *buff
,
2369 int len
, char *mod
, int not)
2371 unsigned search_len
= 0;
2372 struct ftrace_page
*pg
;
2373 struct dyn_ftrace
*rec
;
2374 int type
= MATCH_FULL
;
2375 char *search
= buff
;
2380 type
= filter_parse_regex(buff
, len
, &search
, ¬);
2381 search_len
= strlen(search
);
2384 mutex_lock(&ftrace_lock
);
2386 if (unlikely(ftrace_disabled
))
2389 do_for_each_ftrace_rec(pg
, rec
) {
2391 if (ftrace_match_record(rec
, mod
, search
, search_len
, type
)) {
2392 ret
= enter_record(hash
, rec
, not);
2399 } while_for_each_ftrace_rec();
2401 mutex_unlock(&ftrace_lock
);
2407 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
2409 return match_records(hash
, buff
, len
, NULL
, 0);
2413 ftrace_match_module_records(struct ftrace_hash
*hash
, char *buff
, char *mod
)
2417 /* blank or '*' mean the same */
2418 if (strcmp(buff
, "*") == 0)
2421 /* handle the case of 'dont filter this module' */
2422 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
2427 return match_records(hash
, buff
, strlen(buff
), mod
, not);
2431 * We register the module command as a template to show others how
2432 * to register the a command as well.
2436 ftrace_mod_callback(struct ftrace_hash
*hash
,
2437 char *func
, char *cmd
, char *param
, int enable
)
2443 * cmd == 'mod' because we only registered this func
2444 * for the 'mod' ftrace_func_command.
2445 * But if you register one func with multiple commands,
2446 * you can tell which command was used by the cmd
2450 /* we must have a module name */
2454 mod
= strsep(¶m
, ":");
2458 ret
= ftrace_match_module_records(hash
, func
, mod
);
2467 static struct ftrace_func_command ftrace_mod_cmd
= {
2469 .func
= ftrace_mod_callback
,
2472 static int __init
ftrace_mod_cmd_init(void)
2474 return register_ftrace_command(&ftrace_mod_cmd
);
2476 device_initcall(ftrace_mod_cmd_init
);
2479 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
2481 struct ftrace_func_probe
*entry
;
2482 struct hlist_head
*hhd
;
2483 struct hlist_node
*n
;
2486 key
= hash_long(ip
, FTRACE_HASH_BITS
);
2488 hhd
= &ftrace_func_hash
[key
];
2490 if (hlist_empty(hhd
))
2494 * Disable preemption for these calls to prevent a RCU grace
2495 * period. This syncs the hash iteration and freeing of items
2496 * on the hash. rcu_read_lock is too dangerous here.
2498 preempt_disable_notrace();
2499 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
2500 if (entry
->ip
== ip
)
2501 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
2503 preempt_enable_notrace();
2506 static struct ftrace_ops trace_probe_ops __read_mostly
=
2508 .func
= function_trace_probe_call
,
2511 static int ftrace_probe_registered
;
2513 static void __enable_ftrace_function_probe(void)
2518 if (ftrace_probe_registered
)
2521 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2522 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2526 /* Nothing registered? */
2527 if (i
== FTRACE_FUNC_HASHSIZE
)
2530 ret
= __register_ftrace_function(&trace_probe_ops
);
2532 ret
= ftrace_startup(&trace_probe_ops
, 0);
2534 ftrace_probe_registered
= 1;
2537 static void __disable_ftrace_function_probe(void)
2542 if (!ftrace_probe_registered
)
2545 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2546 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2551 /* no more funcs left */
2552 ret
= __unregister_ftrace_function(&trace_probe_ops
);
2554 ftrace_shutdown(&trace_probe_ops
, 0);
2556 ftrace_probe_registered
= 0;
2560 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
2562 struct ftrace_func_probe
*entry
=
2563 container_of(rhp
, struct ftrace_func_probe
, rcu
);
2565 if (entry
->ops
->free
)
2566 entry
->ops
->free(&entry
->data
);
2572 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2575 struct ftrace_func_probe
*entry
;
2576 struct ftrace_page
*pg
;
2577 struct dyn_ftrace
*rec
;
2583 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2584 len
= strlen(search
);
2586 /* we do not support '!' for function probes */
2590 mutex_lock(&ftrace_lock
);
2592 if (unlikely(ftrace_disabled
))
2595 do_for_each_ftrace_rec(pg
, rec
) {
2597 if (!ftrace_match_record(rec
, NULL
, search
, len
, type
))
2600 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
2602 /* If we did not process any, then return error */
2613 * The caller might want to do something special
2614 * for each function we find. We call the callback
2615 * to give the caller an opportunity to do so.
2617 if (ops
->callback
) {
2618 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
2619 /* caller does not like this func */
2626 entry
->ip
= rec
->ip
;
2628 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2629 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2631 } while_for_each_ftrace_rec();
2632 __enable_ftrace_function_probe();
2635 mutex_unlock(&ftrace_lock
);
2641 PROBE_TEST_FUNC
= 1,
2646 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2647 void *data
, int flags
)
2649 struct ftrace_func_probe
*entry
;
2650 struct hlist_node
*n
, *tmp
;
2651 char str
[KSYM_SYMBOL_LEN
];
2652 int type
= MATCH_FULL
;
2656 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
2661 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2662 len
= strlen(search
);
2664 /* we do not support '!' for function probes */
2669 mutex_lock(&ftrace_lock
);
2670 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2671 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2673 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2675 /* break up if statements for readability */
2676 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2679 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2682 /* do this last, since it is the most expensive */
2684 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2686 if (!ftrace_match(str
, glob
, len
, type
))
2690 hlist_del(&entry
->node
);
2691 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2694 __disable_ftrace_function_probe();
2695 mutex_unlock(&ftrace_lock
);
2699 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2702 __unregister_ftrace_function_probe(glob
, ops
, data
,
2703 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2707 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2709 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2712 void unregister_ftrace_function_probe_all(char *glob
)
2714 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2717 static LIST_HEAD(ftrace_commands
);
2718 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2720 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2722 struct ftrace_func_command
*p
;
2725 mutex_lock(&ftrace_cmd_mutex
);
2726 list_for_each_entry(p
, &ftrace_commands
, list
) {
2727 if (strcmp(cmd
->name
, p
->name
) == 0) {
2732 list_add(&cmd
->list
, &ftrace_commands
);
2734 mutex_unlock(&ftrace_cmd_mutex
);
2739 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2741 struct ftrace_func_command
*p
, *n
;
2744 mutex_lock(&ftrace_cmd_mutex
);
2745 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2746 if (strcmp(cmd
->name
, p
->name
) == 0) {
2748 list_del_init(&p
->list
);
2753 mutex_unlock(&ftrace_cmd_mutex
);
2758 static int ftrace_process_regex(struct ftrace_hash
*hash
,
2759 char *buff
, int len
, int enable
)
2761 char *func
, *command
, *next
= buff
;
2762 struct ftrace_func_command
*p
;
2765 func
= strsep(&next
, ":");
2768 ret
= ftrace_match_records(hash
, func
, len
);
2778 command
= strsep(&next
, ":");
2780 mutex_lock(&ftrace_cmd_mutex
);
2781 list_for_each_entry(p
, &ftrace_commands
, list
) {
2782 if (strcmp(p
->name
, command
) == 0) {
2783 ret
= p
->func(hash
, func
, command
, next
, enable
);
2788 mutex_unlock(&ftrace_cmd_mutex
);
2794 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2795 size_t cnt
, loff_t
*ppos
, int enable
)
2797 struct ftrace_iterator
*iter
;
2798 struct trace_parser
*parser
;
2804 mutex_lock(&ftrace_regex_lock
);
2807 if (unlikely(ftrace_disabled
))
2810 if (file
->f_mode
& FMODE_READ
) {
2811 struct seq_file
*m
= file
->private_data
;
2814 iter
= file
->private_data
;
2816 parser
= &iter
->parser
;
2817 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
2819 if (read
>= 0 && trace_parser_loaded(parser
) &&
2820 !trace_parser_cont(parser
)) {
2821 ret
= ftrace_process_regex(iter
->hash
, parser
->buffer
,
2822 parser
->idx
, enable
);
2823 trace_parser_clear(parser
);
2830 mutex_unlock(&ftrace_regex_lock
);
2836 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2837 size_t cnt
, loff_t
*ppos
)
2839 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2843 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2844 size_t cnt
, loff_t
*ppos
)
2846 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2850 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
2851 int reset
, int enable
)
2853 struct ftrace_hash
**orig_hash
;
2854 struct ftrace_hash
*hash
;
2857 /* All global ops uses the global ops filters */
2858 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
)
2861 if (unlikely(ftrace_disabled
))
2865 orig_hash
= &ops
->filter_hash
;
2867 orig_hash
= &ops
->notrace_hash
;
2869 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
2873 mutex_lock(&ftrace_regex_lock
);
2875 ftrace_filter_reset(hash
);
2877 ftrace_match_records(hash
, buf
, len
);
2879 mutex_lock(&ftrace_lock
);
2880 ret
= ftrace_hash_move(orig_hash
, hash
);
2881 mutex_unlock(&ftrace_lock
);
2883 mutex_unlock(&ftrace_regex_lock
);
2885 free_ftrace_hash(hash
);
2890 * ftrace_set_filter - set a function to filter on in ftrace
2891 * @ops - the ops to set the filter with
2892 * @buf - the string that holds the function filter text.
2893 * @len - the length of the string.
2894 * @reset - non zero to reset all filters before applying this filter.
2896 * Filters denote which functions should be enabled when tracing is enabled.
2897 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2899 void ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
2902 ftrace_set_regex(ops
, buf
, len
, reset
, 1);
2904 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
2907 * ftrace_set_notrace - set a function to not trace in ftrace
2908 * @ops - the ops to set the notrace filter with
2909 * @buf - the string that holds the function notrace text.
2910 * @len - the length of the string.
2911 * @reset - non zero to reset all filters before applying this filter.
2913 * Notrace Filters denote which functions should not be enabled when tracing
2914 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2917 void ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
2920 ftrace_set_regex(ops
, buf
, len
, reset
, 0);
2922 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
2924 * ftrace_set_filter - set a function to filter on in ftrace
2925 * @ops - the ops to set the filter with
2926 * @buf - the string that holds the function filter text.
2927 * @len - the length of the string.
2928 * @reset - non zero to reset all filters before applying this filter.
2930 * Filters denote which functions should be enabled when tracing is enabled.
2931 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2933 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
2935 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
2937 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
2940 * ftrace_set_notrace - set a function to not trace in ftrace
2941 * @ops - the ops to set the notrace filter with
2942 * @buf - the string that holds the function notrace text.
2943 * @len - the length of the string.
2944 * @reset - non zero to reset all filters before applying this filter.
2946 * Notrace Filters denote which functions should not be enabled when tracing
2947 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2950 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
2952 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
2954 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
2957 * command line interface to allow users to set filters on boot up.
2959 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2960 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2961 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2963 static int __init
set_ftrace_notrace(char *str
)
2965 strncpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
2968 __setup("ftrace_notrace=", set_ftrace_notrace
);
2970 static int __init
set_ftrace_filter(char *str
)
2972 strncpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
2975 __setup("ftrace_filter=", set_ftrace_filter
);
2977 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2978 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2979 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
2981 static int __init
set_graph_function(char *str
)
2983 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
2986 __setup("ftrace_graph_filter=", set_graph_function
);
2988 static void __init
set_ftrace_early_graph(char *buf
)
2994 func
= strsep(&buf
, ",");
2995 /* we allow only one expression at a time */
2996 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2999 printk(KERN_DEBUG
"ftrace: function %s not "
3000 "traceable\n", func
);
3003 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3006 set_ftrace_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
3011 func
= strsep(&buf
, ",");
3012 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
3016 static void __init
set_ftrace_early_filters(void)
3018 if (ftrace_filter_buf
[0])
3019 set_ftrace_early_filter(&global_ops
, ftrace_filter_buf
, 1);
3020 if (ftrace_notrace_buf
[0])
3021 set_ftrace_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
3022 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3023 if (ftrace_graph_buf
[0])
3024 set_ftrace_early_graph(ftrace_graph_buf
);
3025 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3029 ftrace_regex_release(struct inode
*inode
, struct file
*file
)
3031 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
3032 struct ftrace_iterator
*iter
;
3033 struct ftrace_hash
**orig_hash
;
3034 struct trace_parser
*parser
;
3038 mutex_lock(&ftrace_regex_lock
);
3039 if (file
->f_mode
& FMODE_READ
) {
3042 seq_release(inode
, file
);
3044 iter
= file
->private_data
;
3046 parser
= &iter
->parser
;
3047 if (trace_parser_loaded(parser
)) {
3048 parser
->buffer
[parser
->idx
] = 0;
3049 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
3052 trace_parser_put(parser
);
3054 if (file
->f_mode
& FMODE_WRITE
) {
3055 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
3058 orig_hash
= &iter
->ops
->filter_hash
;
3060 orig_hash
= &iter
->ops
->notrace_hash
;
3062 mutex_lock(&ftrace_lock
);
3064 * Remove the current set, update the hash and add
3067 ftrace_hash_rec_disable(iter
->ops
, filter_hash
);
3068 ret
= ftrace_hash_move(orig_hash
, iter
->hash
);
3070 ftrace_hash_rec_enable(iter
->ops
, filter_hash
);
3071 if (iter
->ops
->flags
& FTRACE_OPS_FL_ENABLED
3073 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
3075 mutex_unlock(&ftrace_lock
);
3077 free_ftrace_hash(iter
->hash
);
3080 mutex_unlock(&ftrace_regex_lock
);
3084 static const struct file_operations ftrace_avail_fops
= {
3085 .open
= ftrace_avail_open
,
3087 .llseek
= seq_lseek
,
3088 .release
= seq_release_private
,
3091 static const struct file_operations ftrace_enabled_fops
= {
3092 .open
= ftrace_enabled_open
,
3094 .llseek
= seq_lseek
,
3095 .release
= seq_release_private
,
3098 static const struct file_operations ftrace_filter_fops
= {
3099 .open
= ftrace_filter_open
,
3101 .write
= ftrace_filter_write
,
3102 .llseek
= ftrace_regex_lseek
,
3103 .release
= ftrace_regex_release
,
3106 static const struct file_operations ftrace_notrace_fops
= {
3107 .open
= ftrace_notrace_open
,
3109 .write
= ftrace_notrace_write
,
3110 .llseek
= ftrace_regex_lseek
,
3111 .release
= ftrace_regex_release
,
3114 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3116 static DEFINE_MUTEX(graph_lock
);
3118 int ftrace_graph_count
;
3119 int ftrace_graph_filter_enabled
;
3120 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
3123 __g_next(struct seq_file
*m
, loff_t
*pos
)
3125 if (*pos
>= ftrace_graph_count
)
3127 return &ftrace_graph_funcs
[*pos
];
3131 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3134 return __g_next(m
, pos
);
3137 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
3139 mutex_lock(&graph_lock
);
3141 /* Nothing, tell g_show to print all functions are enabled */
3142 if (!ftrace_graph_filter_enabled
&& !*pos
)
3145 return __g_next(m
, pos
);
3148 static void g_stop(struct seq_file
*m
, void *p
)
3150 mutex_unlock(&graph_lock
);
3153 static int g_show(struct seq_file
*m
, void *v
)
3155 unsigned long *ptr
= v
;
3160 if (ptr
== (unsigned long *)1) {
3161 seq_printf(m
, "#### all functions enabled ####\n");
3165 seq_printf(m
, "%ps\n", (void *)*ptr
);
3170 static const struct seq_operations ftrace_graph_seq_ops
= {
3178 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
3182 if (unlikely(ftrace_disabled
))
3185 mutex_lock(&graph_lock
);
3186 if ((file
->f_mode
& FMODE_WRITE
) &&
3187 (file
->f_flags
& O_TRUNC
)) {
3188 ftrace_graph_filter_enabled
= 0;
3189 ftrace_graph_count
= 0;
3190 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
3192 mutex_unlock(&graph_lock
);
3194 if (file
->f_mode
& FMODE_READ
)
3195 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
3201 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
3203 if (file
->f_mode
& FMODE_READ
)
3204 seq_release(inode
, file
);
3209 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
3211 struct dyn_ftrace
*rec
;
3212 struct ftrace_page
*pg
;
3221 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
3222 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3225 search_len
= strlen(search
);
3227 mutex_lock(&ftrace_lock
);
3229 if (unlikely(ftrace_disabled
)) {
3230 mutex_unlock(&ftrace_lock
);
3234 do_for_each_ftrace_rec(pg
, rec
) {
3236 if (rec
->flags
& FTRACE_FL_FREE
)
3239 if (ftrace_match_record(rec
, NULL
, search
, search_len
, type
)) {
3240 /* if it is in the array */
3242 for (i
= 0; i
< *idx
; i
++) {
3243 if (array
[i
] == rec
->ip
) {
3252 array
[(*idx
)++] = rec
->ip
;
3253 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3258 array
[i
] = array
[--(*idx
)];
3264 } while_for_each_ftrace_rec();
3266 mutex_unlock(&ftrace_lock
);
3271 ftrace_graph_filter_enabled
= 1;
3276 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
3277 size_t cnt
, loff_t
*ppos
)
3279 struct trace_parser parser
;
3285 mutex_lock(&graph_lock
);
3287 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
3292 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
3294 if (read
>= 0 && trace_parser_loaded((&parser
))) {
3295 parser
.buffer
[parser
.idx
] = 0;
3297 /* we allow only one expression at a time */
3298 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3307 trace_parser_put(&parser
);
3309 mutex_unlock(&graph_lock
);
3314 static const struct file_operations ftrace_graph_fops
= {
3315 .open
= ftrace_graph_open
,
3317 .write
= ftrace_graph_write
,
3318 .release
= ftrace_graph_release
,
3319 .llseek
= seq_lseek
,
3321 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3323 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
3326 trace_create_file("available_filter_functions", 0444,
3327 d_tracer
, NULL
, &ftrace_avail_fops
);
3329 trace_create_file("enabled_functions", 0444,
3330 d_tracer
, NULL
, &ftrace_enabled_fops
);
3332 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
3333 NULL
, &ftrace_filter_fops
);
3335 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
3336 NULL
, &ftrace_notrace_fops
);
3338 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3339 trace_create_file("set_graph_function", 0444, d_tracer
,
3341 &ftrace_graph_fops
);
3342 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3347 static int ftrace_process_locs(struct module
*mod
,
3348 unsigned long *start
,
3353 unsigned long flags
;
3355 mutex_lock(&ftrace_lock
);
3358 addr
= ftrace_call_adjust(*p
++);
3360 * Some architecture linkers will pad between
3361 * the different mcount_loc sections of different
3362 * object files to satisfy alignments.
3363 * Skip any NULL pointers.
3367 ftrace_record_ip(addr
);
3371 * Disable interrupts to prevent interrupts from executing
3372 * code that is being modified.
3374 local_irq_save(flags
);
3375 ftrace_update_code(mod
);
3376 local_irq_restore(flags
);
3377 mutex_unlock(&ftrace_lock
);
3382 #ifdef CONFIG_MODULES
3383 void ftrace_release_mod(struct module
*mod
)
3385 struct dyn_ftrace
*rec
;
3386 struct ftrace_page
*pg
;
3388 mutex_lock(&ftrace_lock
);
3390 if (ftrace_disabled
)
3393 do_for_each_ftrace_rec(pg
, rec
) {
3394 if (within_module_core(rec
->ip
, mod
)) {
3396 * rec->ip is changed in ftrace_free_rec()
3397 * It should not between s and e if record was freed.
3399 FTRACE_WARN_ON(rec
->flags
& FTRACE_FL_FREE
);
3400 ftrace_free_rec(rec
);
3402 } while_for_each_ftrace_rec();
3404 mutex_unlock(&ftrace_lock
);
3407 static void ftrace_init_module(struct module
*mod
,
3408 unsigned long *start
, unsigned long *end
)
3410 if (ftrace_disabled
|| start
== end
)
3412 ftrace_process_locs(mod
, start
, end
);
3415 static int ftrace_module_notify(struct notifier_block
*self
,
3416 unsigned long val
, void *data
)
3418 struct module
*mod
= data
;
3421 case MODULE_STATE_COMING
:
3422 ftrace_init_module(mod
, mod
->ftrace_callsites
,
3423 mod
->ftrace_callsites
+
3424 mod
->num_ftrace_callsites
);
3426 case MODULE_STATE_GOING
:
3427 ftrace_release_mod(mod
);
3434 static int ftrace_module_notify(struct notifier_block
*self
,
3435 unsigned long val
, void *data
)
3439 #endif /* CONFIG_MODULES */
3441 struct notifier_block ftrace_module_nb
= {
3442 .notifier_call
= ftrace_module_notify
,
3446 extern unsigned long __start_mcount_loc
[];
3447 extern unsigned long __stop_mcount_loc
[];
3449 void __init
ftrace_init(void)
3451 unsigned long count
, addr
, flags
;
3454 /* Keep the ftrace pointer to the stub */
3455 addr
= (unsigned long)ftrace_stub
;
3457 local_irq_save(flags
);
3458 ftrace_dyn_arch_init(&addr
);
3459 local_irq_restore(flags
);
3461 /* ftrace_dyn_arch_init places the return code in addr */
3465 count
= __stop_mcount_loc
- __start_mcount_loc
;
3467 ret
= ftrace_dyn_table_alloc(count
);
3471 last_ftrace_enabled
= ftrace_enabled
= 1;
3473 ret
= ftrace_process_locs(NULL
,
3477 ret
= register_module_notifier(&ftrace_module_nb
);
3479 pr_warning("Failed to register trace ftrace module notifier\n");
3481 set_ftrace_early_filters();
3485 ftrace_disabled
= 1;
3490 static struct ftrace_ops global_ops
= {
3491 .func
= ftrace_stub
,
3494 static int __init
ftrace_nodyn_init(void)
3499 device_initcall(ftrace_nodyn_init
);
3501 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
3502 static inline void ftrace_startup_enable(int command
) { }
3503 /* Keep as macros so we do not need to define the commands */
3504 # define ftrace_startup(ops, command) \
3506 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3509 # define ftrace_shutdown(ops, command) do { } while (0)
3510 # define ftrace_startup_sysctl() do { } while (0)
3511 # define ftrace_shutdown_sysctl() do { } while (0)
3514 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
)
3519 #endif /* CONFIG_DYNAMIC_FTRACE */
3522 ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
)
3524 struct ftrace_ops
*op
;
3526 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT
)))
3529 trace_recursion_set(TRACE_INTERNAL_BIT
);
3531 * Some of the ops may be dynamically allocated,
3532 * they must be freed after a synchronize_sched().
3534 preempt_disable_notrace();
3535 op
= rcu_dereference_raw(ftrace_ops_list
);
3536 while (op
!= &ftrace_list_end
) {
3537 if (ftrace_ops_test(op
, ip
))
3538 op
->func(ip
, parent_ip
);
3539 op
= rcu_dereference_raw(op
->next
);
3541 preempt_enable_notrace();
3542 trace_recursion_clear(TRACE_INTERNAL_BIT
);
3545 static void clear_ftrace_swapper(void)
3547 struct task_struct
*p
;
3551 for_each_online_cpu(cpu
) {
3553 clear_tsk_trace_trace(p
);
3558 static void set_ftrace_swapper(void)
3560 struct task_struct
*p
;
3564 for_each_online_cpu(cpu
) {
3566 set_tsk_trace_trace(p
);
3571 static void clear_ftrace_pid(struct pid
*pid
)
3573 struct task_struct
*p
;
3576 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
3577 clear_tsk_trace_trace(p
);
3578 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
3584 static void set_ftrace_pid(struct pid
*pid
)
3586 struct task_struct
*p
;
3589 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
3590 set_tsk_trace_trace(p
);
3591 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
3595 static void clear_ftrace_pid_task(struct pid
*pid
)
3597 if (pid
== ftrace_swapper_pid
)
3598 clear_ftrace_swapper();
3600 clear_ftrace_pid(pid
);
3603 static void set_ftrace_pid_task(struct pid
*pid
)
3605 if (pid
== ftrace_swapper_pid
)
3606 set_ftrace_swapper();
3608 set_ftrace_pid(pid
);
3611 static int ftrace_pid_add(int p
)
3614 struct ftrace_pid
*fpid
;
3617 mutex_lock(&ftrace_lock
);
3620 pid
= ftrace_swapper_pid
;
3622 pid
= find_get_pid(p
);
3629 list_for_each_entry(fpid
, &ftrace_pids
, list
)
3630 if (fpid
->pid
== pid
)
3635 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
3639 list_add(&fpid
->list
, &ftrace_pids
);
3642 set_ftrace_pid_task(pid
);
3644 ftrace_update_pid_func();
3645 ftrace_startup_enable(0);
3647 mutex_unlock(&ftrace_lock
);
3651 if (pid
!= ftrace_swapper_pid
)
3655 mutex_unlock(&ftrace_lock
);
3659 static void ftrace_pid_reset(void)
3661 struct ftrace_pid
*fpid
, *safe
;
3663 mutex_lock(&ftrace_lock
);
3664 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
3665 struct pid
*pid
= fpid
->pid
;
3667 clear_ftrace_pid_task(pid
);
3669 list_del(&fpid
->list
);
3673 ftrace_update_pid_func();
3674 ftrace_startup_enable(0);
3676 mutex_unlock(&ftrace_lock
);
3679 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
3681 mutex_lock(&ftrace_lock
);
3683 if (list_empty(&ftrace_pids
) && (!*pos
))
3686 return seq_list_start(&ftrace_pids
, *pos
);
3689 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3694 return seq_list_next(v
, &ftrace_pids
, pos
);
3697 static void fpid_stop(struct seq_file
*m
, void *p
)
3699 mutex_unlock(&ftrace_lock
);
3702 static int fpid_show(struct seq_file
*m
, void *v
)
3704 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
3706 if (v
== (void *)1) {
3707 seq_printf(m
, "no pid\n");
3711 if (fpid
->pid
== ftrace_swapper_pid
)
3712 seq_printf(m
, "swapper tasks\n");
3714 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
3719 static const struct seq_operations ftrace_pid_sops
= {
3720 .start
= fpid_start
,
3727 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
3731 if ((file
->f_mode
& FMODE_WRITE
) &&
3732 (file
->f_flags
& O_TRUNC
))
3735 if (file
->f_mode
& FMODE_READ
)
3736 ret
= seq_open(file
, &ftrace_pid_sops
);
3742 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
3743 size_t cnt
, loff_t
*ppos
)
3749 if (cnt
>= sizeof(buf
))
3752 if (copy_from_user(&buf
, ubuf
, cnt
))
3758 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3759 * to clean the filter quietly.
3761 tmp
= strstrip(buf
);
3762 if (strlen(tmp
) == 0)
3765 ret
= strict_strtol(tmp
, 10, &val
);
3769 ret
= ftrace_pid_add(val
);
3771 return ret
? ret
: cnt
;
3775 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
3777 if (file
->f_mode
& FMODE_READ
)
3778 seq_release(inode
, file
);
3783 static const struct file_operations ftrace_pid_fops
= {
3784 .open
= ftrace_pid_open
,
3785 .write
= ftrace_pid_write
,
3787 .llseek
= seq_lseek
,
3788 .release
= ftrace_pid_release
,
3791 static __init
int ftrace_init_debugfs(void)
3793 struct dentry
*d_tracer
;
3795 d_tracer
= tracing_init_dentry();
3799 ftrace_init_dyn_debugfs(d_tracer
);
3801 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
3802 NULL
, &ftrace_pid_fops
);
3804 ftrace_profile_debugfs(d_tracer
);
3808 fs_initcall(ftrace_init_debugfs
);
3811 * ftrace_kill - kill ftrace
3813 * This function should be used by panic code. It stops ftrace
3814 * but in a not so nice way. If you need to simply kill ftrace
3815 * from a non-atomic section, use ftrace_kill.
3817 void ftrace_kill(void)
3819 ftrace_disabled
= 1;
3821 clear_ftrace_function();
3825 * register_ftrace_function - register a function for profiling
3826 * @ops - ops structure that holds the function for profiling.
3828 * Register a function to be called by all functions in the
3831 * Note: @ops->func and all the functions it calls must be labeled
3832 * with "notrace", otherwise it will go into a
3835 int register_ftrace_function(struct ftrace_ops
*ops
)
3839 mutex_lock(&ftrace_lock
);
3841 if (unlikely(ftrace_disabled
))
3844 ret
= __register_ftrace_function(ops
);
3846 ret
= ftrace_startup(ops
, 0);
3850 mutex_unlock(&ftrace_lock
);
3853 EXPORT_SYMBOL_GPL(register_ftrace_function
);
3856 * unregister_ftrace_function - unregister a function for profiling.
3857 * @ops - ops structure that holds the function to unregister
3859 * Unregister a function that was added to be called by ftrace profiling.
3861 int unregister_ftrace_function(struct ftrace_ops
*ops
)
3865 mutex_lock(&ftrace_lock
);
3866 ret
= __unregister_ftrace_function(ops
);
3868 ftrace_shutdown(ops
, 0);
3869 mutex_unlock(&ftrace_lock
);
3873 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
3876 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
3877 void __user
*buffer
, size_t *lenp
,
3882 mutex_lock(&ftrace_lock
);
3884 if (unlikely(ftrace_disabled
))
3887 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3889 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
3892 last_ftrace_enabled
= !!ftrace_enabled
;
3894 if (ftrace_enabled
) {
3896 ftrace_startup_sysctl();
3898 /* we are starting ftrace again */
3899 if (ftrace_ops_list
!= &ftrace_list_end
) {
3900 if (ftrace_ops_list
->next
== &ftrace_list_end
)
3901 ftrace_trace_function
= ftrace_ops_list
->func
;
3903 ftrace_trace_function
= ftrace_ops_list_func
;
3907 /* stopping ftrace calls (just send to ftrace_stub) */
3908 ftrace_trace_function
= ftrace_stub
;
3910 ftrace_shutdown_sysctl();
3914 mutex_unlock(&ftrace_lock
);
3918 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3920 static int ftrace_graph_active
;
3921 static struct notifier_block ftrace_suspend_notifier
;
3923 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3928 /* The callbacks that hook a function */
3929 trace_func_graph_ret_t ftrace_graph_return
=
3930 (trace_func_graph_ret_t
)ftrace_stub
;
3931 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3933 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3934 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3938 unsigned long flags
;
3939 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3940 struct task_struct
*g
, *t
;
3942 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3943 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3944 * sizeof(struct ftrace_ret_stack
),
3946 if (!ret_stack_list
[i
]) {
3954 read_lock_irqsave(&tasklist_lock
, flags
);
3955 do_each_thread(g
, t
) {
3961 if (t
->ret_stack
== NULL
) {
3962 atomic_set(&t
->tracing_graph_pause
, 0);
3963 atomic_set(&t
->trace_overrun
, 0);
3964 t
->curr_ret_stack
= -1;
3965 /* Make sure the tasks see the -1 first: */
3967 t
->ret_stack
= ret_stack_list
[start
++];
3969 } while_each_thread(g
, t
);
3972 read_unlock_irqrestore(&tasklist_lock
, flags
);
3974 for (i
= start
; i
< end
; i
++)
3975 kfree(ret_stack_list
[i
]);
3980 ftrace_graph_probe_sched_switch(void *ignore
,
3981 struct task_struct
*prev
, struct task_struct
*next
)
3983 unsigned long long timestamp
;
3987 * Does the user want to count the time a function was asleep.
3988 * If so, do not update the time stamps.
3990 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
3993 timestamp
= trace_clock_local();
3995 prev
->ftrace_timestamp
= timestamp
;
3997 /* only process tasks that we timestamped */
3998 if (!next
->ftrace_timestamp
)
4002 * Update all the counters in next to make up for the
4003 * time next was sleeping.
4005 timestamp
-= next
->ftrace_timestamp
;
4007 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
4008 next
->ret_stack
[index
].calltime
+= timestamp
;
4011 /* Allocate a return stack for each task */
4012 static int start_graph_tracing(void)
4014 struct ftrace_ret_stack
**ret_stack_list
;
4017 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
4018 sizeof(struct ftrace_ret_stack
*),
4021 if (!ret_stack_list
)
4024 /* The cpu_boot init_task->ret_stack will never be freed */
4025 for_each_online_cpu(cpu
) {
4026 if (!idle_task(cpu
)->ret_stack
)
4027 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
4031 ret
= alloc_retstack_tasklist(ret_stack_list
);
4032 } while (ret
== -EAGAIN
);
4035 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4037 pr_info("ftrace_graph: Couldn't activate tracepoint"
4038 " probe to kernel_sched_switch\n");
4041 kfree(ret_stack_list
);
4046 * Hibernation protection.
4047 * The state of the current task is too much unstable during
4048 * suspend/restore to disk. We want to protect against that.
4051 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
4055 case PM_HIBERNATION_PREPARE
:
4056 pause_graph_tracing();
4059 case PM_POST_HIBERNATION
:
4060 unpause_graph_tracing();
4066 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
4067 trace_func_graph_ent_t entryfunc
)
4071 mutex_lock(&ftrace_lock
);
4073 /* we currently allow only one tracer registered at a time */
4074 if (ftrace_graph_active
) {
4079 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
4080 register_pm_notifier(&ftrace_suspend_notifier
);
4082 ftrace_graph_active
++;
4083 ret
= start_graph_tracing();
4085 ftrace_graph_active
--;
4089 ftrace_graph_return
= retfunc
;
4090 ftrace_graph_entry
= entryfunc
;
4092 ret
= ftrace_startup(&global_ops
, FTRACE_START_FUNC_RET
);
4095 mutex_unlock(&ftrace_lock
);
4099 void unregister_ftrace_graph(void)
4101 mutex_lock(&ftrace_lock
);
4103 if (unlikely(!ftrace_graph_active
))
4106 ftrace_graph_active
--;
4107 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
4108 ftrace_graph_entry
= ftrace_graph_entry_stub
;
4109 ftrace_shutdown(&global_ops
, FTRACE_STOP_FUNC_RET
);
4110 unregister_pm_notifier(&ftrace_suspend_notifier
);
4111 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4114 mutex_unlock(&ftrace_lock
);
4117 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
4120 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
4122 atomic_set(&t
->tracing_graph_pause
, 0);
4123 atomic_set(&t
->trace_overrun
, 0);
4124 t
->ftrace_timestamp
= 0;
4125 /* make curr_ret_stack visible before we add the ret_stack */
4127 t
->ret_stack
= ret_stack
;
4131 * Allocate a return stack for the idle task. May be the first
4132 * time through, or it may be done by CPU hotplug online.
4134 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
4136 t
->curr_ret_stack
= -1;
4138 * The idle task has no parent, it either has its own
4139 * stack or no stack at all.
4142 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
4144 if (ftrace_graph_active
) {
4145 struct ftrace_ret_stack
*ret_stack
;
4147 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
4149 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4150 * sizeof(struct ftrace_ret_stack
),
4154 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
4156 graph_init_task(t
, ret_stack
);
4160 /* Allocate a return stack for newly created task */
4161 void ftrace_graph_init_task(struct task_struct
*t
)
4163 /* Make sure we do not use the parent ret_stack */
4164 t
->ret_stack
= NULL
;
4165 t
->curr_ret_stack
= -1;
4167 if (ftrace_graph_active
) {
4168 struct ftrace_ret_stack
*ret_stack
;
4170 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4171 * sizeof(struct ftrace_ret_stack
),
4175 graph_init_task(t
, ret_stack
);
4179 void ftrace_graph_exit_task(struct task_struct
*t
)
4181 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
4183 t
->ret_stack
= NULL
;
4184 /* NULL must become visible to IRQs before we free it: */
4190 void ftrace_graph_stop(void)