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 ftrace_update_code(struct module
*mod
)
1749 struct dyn_ftrace
*p
;
1750 cycle_t start
, stop
;
1752 start
= ftrace_now(raw_smp_processor_id());
1753 ftrace_update_cnt
= 0;
1755 while (ftrace_new_addrs
) {
1757 /* If something went wrong, bail without enabling anything */
1758 if (unlikely(ftrace_disabled
))
1761 p
= ftrace_new_addrs
;
1762 ftrace_new_addrs
= p
->newlist
;
1766 * Do the initial record conversion from mcount jump
1767 * to the NOP instructions.
1769 if (!ftrace_code_disable(mod
, p
)) {
1775 ftrace_update_cnt
++;
1778 * If the tracing is enabled, go ahead and enable the record.
1780 * The reason not to enable the record immediatelly is the
1781 * inherent check of ftrace_make_nop/ftrace_make_call for
1782 * correct previous instructions. Making first the NOP
1783 * conversion puts the module to the correct state, thus
1784 * passing the ftrace_make_call check.
1786 if (ftrace_start_up
) {
1787 int failed
= __ftrace_replace_code(p
, 1);
1789 ftrace_bug(failed
, p
->ip
);
1795 stop
= ftrace_now(raw_smp_processor_id());
1796 ftrace_update_time
= stop
- start
;
1797 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1802 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1804 struct ftrace_page
*pg
;
1808 /* allocate a few pages */
1809 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1810 if (!ftrace_pages_start
)
1814 * Allocate a few more pages.
1816 * TODO: have some parser search vmlinux before
1817 * final linking to find all calls to ftrace.
1819 * a) know how many pages to allocate.
1821 * b) set up the table then.
1823 * The dynamic code is still necessary for
1827 pg
= ftrace_pages
= ftrace_pages_start
;
1829 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1830 pr_info("ftrace: allocating %ld entries in %d pages\n",
1831 num_to_init
, cnt
+ 1);
1833 for (i
= 0; i
< cnt
; i
++) {
1834 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1836 /* If we fail, we'll try later anyway */
1847 FTRACE_ITER_FILTER
= (1 << 0),
1848 FTRACE_ITER_NOTRACE
= (1 << 1),
1849 FTRACE_ITER_PRINTALL
= (1 << 2),
1850 FTRACE_ITER_HASH
= (1 << 3),
1851 FTRACE_ITER_ENABLED
= (1 << 4),
1854 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1856 struct ftrace_iterator
{
1859 struct ftrace_page
*pg
;
1860 struct dyn_ftrace
*func
;
1861 struct ftrace_func_probe
*probe
;
1862 struct trace_parser parser
;
1863 struct ftrace_hash
*hash
;
1864 struct ftrace_ops
*ops
;
1871 t_hash_next(struct seq_file
*m
, loff_t
*pos
)
1873 struct ftrace_iterator
*iter
= m
->private;
1874 struct hlist_node
*hnd
= NULL
;
1875 struct hlist_head
*hhd
;
1881 hnd
= &iter
->probe
->node
;
1883 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1886 hhd
= &ftrace_func_hash
[iter
->hidx
];
1888 if (hlist_empty(hhd
)) {
1904 if (WARN_ON_ONCE(!hnd
))
1907 iter
->probe
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1912 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1914 struct ftrace_iterator
*iter
= m
->private;
1918 if (iter
->func_pos
> *pos
)
1922 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
1923 p
= t_hash_next(m
, &l
);
1930 /* Only set this if we have an item */
1931 iter
->flags
|= FTRACE_ITER_HASH
;
1937 t_hash_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
1939 struct ftrace_func_probe
*rec
;
1942 if (WARN_ON_ONCE(!rec
))
1945 if (rec
->ops
->print
)
1946 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1948 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
1951 seq_printf(m
, ":%p", rec
->data
);
1958 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1960 struct ftrace_iterator
*iter
= m
->private;
1961 struct ftrace_ops
*ops
= &global_ops
;
1962 struct dyn_ftrace
*rec
= NULL
;
1964 if (unlikely(ftrace_disabled
))
1967 if (iter
->flags
& FTRACE_ITER_HASH
)
1968 return t_hash_next(m
, pos
);
1971 iter
->pos
= iter
->func_pos
= *pos
;
1973 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
1974 return t_hash_start(m
, pos
);
1977 if (iter
->idx
>= iter
->pg
->index
) {
1978 if (iter
->pg
->next
) {
1979 iter
->pg
= iter
->pg
->next
;
1984 rec
= &iter
->pg
->records
[iter
->idx
++];
1985 if ((rec
->flags
& FTRACE_FL_FREE
) ||
1987 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
1988 !(ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))) ||
1990 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
1991 !ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
)) ||
1993 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
1994 !(rec
->flags
& ~FTRACE_FL_MASK
))) {
2002 return t_hash_start(m
, pos
);
2009 static void reset_iter_read(struct ftrace_iterator
*iter
)
2013 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
& FTRACE_ITER_HASH
);
2016 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2018 struct ftrace_iterator
*iter
= m
->private;
2019 struct ftrace_ops
*ops
= &global_ops
;
2023 mutex_lock(&ftrace_lock
);
2025 if (unlikely(ftrace_disabled
))
2029 * If an lseek was done, then reset and start from beginning.
2031 if (*pos
< iter
->pos
)
2032 reset_iter_read(iter
);
2035 * For set_ftrace_filter reading, if we have the filter
2036 * off, we can short cut and just print out that all
2037 * functions are enabled.
2039 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ops
->filter_hash
->count
) {
2041 return t_hash_start(m
, pos
);
2042 iter
->flags
|= FTRACE_ITER_PRINTALL
;
2043 /* reset in case of seek/pread */
2044 iter
->flags
&= ~FTRACE_ITER_HASH
;
2048 if (iter
->flags
& FTRACE_ITER_HASH
)
2049 return t_hash_start(m
, pos
);
2052 * Unfortunately, we need to restart at ftrace_pages_start
2053 * every time we let go of the ftrace_mutex. This is because
2054 * those pointers can change without the lock.
2056 iter
->pg
= ftrace_pages_start
;
2058 for (l
= 0; l
<= *pos
; ) {
2059 p
= t_next(m
, p
, &l
);
2065 if (iter
->flags
& FTRACE_ITER_FILTER
)
2066 return t_hash_start(m
, pos
);
2074 static void t_stop(struct seq_file
*m
, void *p
)
2076 mutex_unlock(&ftrace_lock
);
2079 static int t_show(struct seq_file
*m
, void *v
)
2081 struct ftrace_iterator
*iter
= m
->private;
2082 struct dyn_ftrace
*rec
;
2084 if (iter
->flags
& FTRACE_ITER_HASH
)
2085 return t_hash_show(m
, iter
);
2087 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
2088 seq_printf(m
, "#### all functions enabled ####\n");
2097 seq_printf(m
, "%ps", (void *)rec
->ip
);
2098 if (iter
->flags
& FTRACE_ITER_ENABLED
)
2099 seq_printf(m
, " (%ld)",
2100 rec
->flags
& ~FTRACE_FL_MASK
);
2101 seq_printf(m
, "\n");
2106 static const struct seq_operations show_ftrace_seq_ops
= {
2114 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
2116 struct ftrace_iterator
*iter
;
2119 if (unlikely(ftrace_disabled
))
2122 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2126 iter
->pg
= ftrace_pages_start
;
2128 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2130 struct seq_file
*m
= file
->private_data
;
2141 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
2143 struct ftrace_iterator
*iter
;
2146 if (unlikely(ftrace_disabled
))
2149 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2153 iter
->pg
= ftrace_pages_start
;
2154 iter
->flags
= FTRACE_ITER_ENABLED
;
2156 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2158 struct seq_file
*m
= file
->private_data
;
2168 static void ftrace_filter_reset(struct ftrace_hash
*hash
)
2170 mutex_lock(&ftrace_lock
);
2171 ftrace_hash_clear(hash
);
2172 mutex_unlock(&ftrace_lock
);
2176 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
2177 struct inode
*inode
, struct file
*file
)
2179 struct ftrace_iterator
*iter
;
2180 struct ftrace_hash
*hash
;
2183 if (unlikely(ftrace_disabled
))
2186 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2190 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
2195 if (flag
& FTRACE_ITER_NOTRACE
)
2196 hash
= ops
->notrace_hash
;
2198 hash
= ops
->filter_hash
;
2203 if (file
->f_mode
& FMODE_WRITE
) {
2204 mutex_lock(&ftrace_lock
);
2205 iter
->hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, hash
);
2206 mutex_unlock(&ftrace_lock
);
2209 trace_parser_put(&iter
->parser
);
2215 mutex_lock(&ftrace_regex_lock
);
2217 if ((file
->f_mode
& FMODE_WRITE
) &&
2218 (file
->f_flags
& O_TRUNC
))
2219 ftrace_filter_reset(iter
->hash
);
2221 if (file
->f_mode
& FMODE_READ
) {
2222 iter
->pg
= ftrace_pages_start
;
2224 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2226 struct seq_file
*m
= file
->private_data
;
2230 free_ftrace_hash(iter
->hash
);
2231 trace_parser_put(&iter
->parser
);
2235 file
->private_data
= iter
;
2236 mutex_unlock(&ftrace_regex_lock
);
2242 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
2244 return ftrace_regex_open(&global_ops
, FTRACE_ITER_FILTER
,
2249 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
2251 return ftrace_regex_open(&global_ops
, FTRACE_ITER_NOTRACE
,
2256 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
2260 if (file
->f_mode
& FMODE_READ
)
2261 ret
= seq_lseek(file
, offset
, origin
);
2263 file
->f_pos
= ret
= 1;
2268 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
2275 if (strcmp(str
, regex
) == 0)
2278 case MATCH_FRONT_ONLY
:
2279 if (strncmp(str
, regex
, len
) == 0)
2282 case MATCH_MIDDLE_ONLY
:
2283 if (strstr(str
, regex
))
2286 case MATCH_END_ONLY
:
2288 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
2297 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int not)
2299 struct ftrace_func_entry
*entry
;
2302 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
2304 /* Do nothing if it doesn't exist */
2308 free_hash_entry(hash
, entry
);
2310 /* Do nothing if it exists */
2314 ret
= add_hash_entry(hash
, rec
->ip
);
2320 ftrace_match_record(struct dyn_ftrace
*rec
, char *mod
,
2321 char *regex
, int len
, int type
)
2323 char str
[KSYM_SYMBOL_LEN
];
2326 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
2329 /* module lookup requires matching the module */
2330 if (!modname
|| strcmp(modname
, mod
))
2333 /* blank search means to match all funcs in the mod */
2338 return ftrace_match(str
, regex
, len
, type
);
2342 match_records(struct ftrace_hash
*hash
, char *buff
,
2343 int len
, char *mod
, int not)
2345 unsigned search_len
= 0;
2346 struct ftrace_page
*pg
;
2347 struct dyn_ftrace
*rec
;
2348 int type
= MATCH_FULL
;
2349 char *search
= buff
;
2354 type
= filter_parse_regex(buff
, len
, &search
, ¬);
2355 search_len
= strlen(search
);
2358 mutex_lock(&ftrace_lock
);
2360 if (unlikely(ftrace_disabled
))
2363 do_for_each_ftrace_rec(pg
, rec
) {
2365 if (ftrace_match_record(rec
, mod
, search
, search_len
, type
)) {
2366 ret
= enter_record(hash
, rec
, not);
2373 } while_for_each_ftrace_rec();
2375 mutex_unlock(&ftrace_lock
);
2381 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
2383 return match_records(hash
, buff
, len
, NULL
, 0);
2387 ftrace_match_module_records(struct ftrace_hash
*hash
, char *buff
, char *mod
)
2391 /* blank or '*' mean the same */
2392 if (strcmp(buff
, "*") == 0)
2395 /* handle the case of 'dont filter this module' */
2396 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
2401 return match_records(hash
, buff
, strlen(buff
), mod
, not);
2405 * We register the module command as a template to show others how
2406 * to register the a command as well.
2410 ftrace_mod_callback(char *func
, char *cmd
, char *param
, int enable
)
2412 struct ftrace_ops
*ops
= &global_ops
;
2413 struct ftrace_hash
*hash
;
2418 * cmd == 'mod' because we only registered this func
2419 * for the 'mod' ftrace_func_command.
2420 * But if you register one func with multiple commands,
2421 * you can tell which command was used by the cmd
2425 /* we must have a module name */
2429 mod
= strsep(¶m
, ":");
2434 hash
= ops
->filter_hash
;
2436 hash
= ops
->notrace_hash
;
2438 ret
= ftrace_match_module_records(hash
, func
, mod
);
2447 static struct ftrace_func_command ftrace_mod_cmd
= {
2449 .func
= ftrace_mod_callback
,
2452 static int __init
ftrace_mod_cmd_init(void)
2454 return register_ftrace_command(&ftrace_mod_cmd
);
2456 device_initcall(ftrace_mod_cmd_init
);
2459 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
2461 struct ftrace_func_probe
*entry
;
2462 struct hlist_head
*hhd
;
2463 struct hlist_node
*n
;
2466 key
= hash_long(ip
, FTRACE_HASH_BITS
);
2468 hhd
= &ftrace_func_hash
[key
];
2470 if (hlist_empty(hhd
))
2474 * Disable preemption for these calls to prevent a RCU grace
2475 * period. This syncs the hash iteration and freeing of items
2476 * on the hash. rcu_read_lock is too dangerous here.
2478 preempt_disable_notrace();
2479 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
2480 if (entry
->ip
== ip
)
2481 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
2483 preempt_enable_notrace();
2486 static struct ftrace_ops trace_probe_ops __read_mostly
=
2488 .func
= function_trace_probe_call
,
2491 static int ftrace_probe_registered
;
2493 static void __enable_ftrace_function_probe(void)
2498 if (ftrace_probe_registered
)
2501 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2502 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2506 /* Nothing registered? */
2507 if (i
== FTRACE_FUNC_HASHSIZE
)
2510 ret
= __register_ftrace_function(&trace_probe_ops
);
2512 ret
= ftrace_startup(&trace_probe_ops
, 0);
2514 ftrace_probe_registered
= 1;
2517 static void __disable_ftrace_function_probe(void)
2522 if (!ftrace_probe_registered
)
2525 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2526 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2531 /* no more funcs left */
2532 ret
= __unregister_ftrace_function(&trace_probe_ops
);
2534 ftrace_shutdown(&trace_probe_ops
, 0);
2536 ftrace_probe_registered
= 0;
2540 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
2542 struct ftrace_func_probe
*entry
=
2543 container_of(rhp
, struct ftrace_func_probe
, rcu
);
2545 if (entry
->ops
->free
)
2546 entry
->ops
->free(&entry
->data
);
2552 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2555 struct ftrace_func_probe
*entry
;
2556 struct ftrace_page
*pg
;
2557 struct dyn_ftrace
*rec
;
2563 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2564 len
= strlen(search
);
2566 /* we do not support '!' for function probes */
2570 mutex_lock(&ftrace_lock
);
2572 if (unlikely(ftrace_disabled
))
2575 do_for_each_ftrace_rec(pg
, rec
) {
2577 if (!ftrace_match_record(rec
, NULL
, search
, len
, type
))
2580 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
2582 /* If we did not process any, then return error */
2593 * The caller might want to do something special
2594 * for each function we find. We call the callback
2595 * to give the caller an opportunity to do so.
2597 if (ops
->callback
) {
2598 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
2599 /* caller does not like this func */
2606 entry
->ip
= rec
->ip
;
2608 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2609 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2611 } while_for_each_ftrace_rec();
2612 __enable_ftrace_function_probe();
2615 mutex_unlock(&ftrace_lock
);
2621 PROBE_TEST_FUNC
= 1,
2626 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2627 void *data
, int flags
)
2629 struct ftrace_func_probe
*entry
;
2630 struct hlist_node
*n
, *tmp
;
2631 char str
[KSYM_SYMBOL_LEN
];
2632 int type
= MATCH_FULL
;
2636 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
2641 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2642 len
= strlen(search
);
2644 /* we do not support '!' for function probes */
2649 mutex_lock(&ftrace_lock
);
2650 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2651 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2653 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2655 /* break up if statements for readability */
2656 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2659 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2662 /* do this last, since it is the most expensive */
2664 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2666 if (!ftrace_match(str
, glob
, len
, type
))
2670 hlist_del(&entry
->node
);
2671 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2674 __disable_ftrace_function_probe();
2675 mutex_unlock(&ftrace_lock
);
2679 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2682 __unregister_ftrace_function_probe(glob
, ops
, data
,
2683 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2687 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2689 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2692 void unregister_ftrace_function_probe_all(char *glob
)
2694 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2697 static LIST_HEAD(ftrace_commands
);
2698 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2700 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2702 struct ftrace_func_command
*p
;
2705 mutex_lock(&ftrace_cmd_mutex
);
2706 list_for_each_entry(p
, &ftrace_commands
, list
) {
2707 if (strcmp(cmd
->name
, p
->name
) == 0) {
2712 list_add(&cmd
->list
, &ftrace_commands
);
2714 mutex_unlock(&ftrace_cmd_mutex
);
2719 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2721 struct ftrace_func_command
*p
, *n
;
2724 mutex_lock(&ftrace_cmd_mutex
);
2725 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2726 if (strcmp(cmd
->name
, p
->name
) == 0) {
2728 list_del_init(&p
->list
);
2733 mutex_unlock(&ftrace_cmd_mutex
);
2738 static int ftrace_process_regex(struct ftrace_hash
*hash
,
2739 char *buff
, int len
, int enable
)
2741 char *func
, *command
, *next
= buff
;
2742 struct ftrace_func_command
*p
;
2745 func
= strsep(&next
, ":");
2748 ret
= ftrace_match_records(hash
, func
, len
);
2758 command
= strsep(&next
, ":");
2760 mutex_lock(&ftrace_cmd_mutex
);
2761 list_for_each_entry(p
, &ftrace_commands
, list
) {
2762 if (strcmp(p
->name
, command
) == 0) {
2763 ret
= p
->func(func
, command
, next
, enable
);
2768 mutex_unlock(&ftrace_cmd_mutex
);
2774 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2775 size_t cnt
, loff_t
*ppos
, int enable
)
2777 struct ftrace_iterator
*iter
;
2778 struct trace_parser
*parser
;
2784 mutex_lock(&ftrace_regex_lock
);
2787 if (unlikely(ftrace_disabled
))
2790 if (file
->f_mode
& FMODE_READ
) {
2791 struct seq_file
*m
= file
->private_data
;
2794 iter
= file
->private_data
;
2796 parser
= &iter
->parser
;
2797 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
2799 if (read
>= 0 && trace_parser_loaded(parser
) &&
2800 !trace_parser_cont(parser
)) {
2801 ret
= ftrace_process_regex(iter
->hash
, parser
->buffer
,
2802 parser
->idx
, enable
);
2803 trace_parser_clear(parser
);
2810 mutex_unlock(&ftrace_regex_lock
);
2816 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2817 size_t cnt
, loff_t
*ppos
)
2819 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2823 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2824 size_t cnt
, loff_t
*ppos
)
2826 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2830 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
2831 int reset
, int enable
)
2833 struct ftrace_hash
**orig_hash
;
2834 struct ftrace_hash
*hash
;
2837 /* All global ops uses the global ops filters */
2838 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
)
2841 if (unlikely(ftrace_disabled
))
2845 orig_hash
= &ops
->filter_hash
;
2847 orig_hash
= &ops
->notrace_hash
;
2849 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
2853 mutex_lock(&ftrace_regex_lock
);
2855 ftrace_filter_reset(hash
);
2857 ftrace_match_records(hash
, buf
, len
);
2859 mutex_lock(&ftrace_lock
);
2860 ret
= ftrace_hash_move(orig_hash
, hash
);
2861 mutex_unlock(&ftrace_lock
);
2863 mutex_unlock(&ftrace_regex_lock
);
2865 free_ftrace_hash(hash
);
2870 * ftrace_set_filter - set a function to filter on in ftrace
2871 * @ops - the ops to set the filter with
2872 * @buf - the string that holds the function filter text.
2873 * @len - the length of the string.
2874 * @reset - non zero to reset all filters before applying this filter.
2876 * Filters denote which functions should be enabled when tracing is enabled.
2877 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2879 void ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
2882 ftrace_set_regex(ops
, buf
, len
, reset
, 1);
2884 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
2887 * ftrace_set_notrace - set a function to not trace in ftrace
2888 * @ops - the ops to set the notrace filter with
2889 * @buf - the string that holds the function notrace text.
2890 * @len - the length of the string.
2891 * @reset - non zero to reset all filters before applying this filter.
2893 * Notrace Filters denote which functions should not be enabled when tracing
2894 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2897 void ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
2900 ftrace_set_regex(ops
, buf
, len
, reset
, 0);
2902 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
2904 * ftrace_set_filter - set a function to filter on in ftrace
2905 * @ops - the ops to set the filter with
2906 * @buf - the string that holds the function filter text.
2907 * @len - the length of the string.
2908 * @reset - non zero to reset all filters before applying this filter.
2910 * Filters denote which functions should be enabled when tracing is enabled.
2911 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2913 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
2915 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
2917 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
2920 * ftrace_set_notrace - set a function to not trace in ftrace
2921 * @ops - the ops to set the notrace filter with
2922 * @buf - the string that holds the function notrace text.
2923 * @len - the length of the string.
2924 * @reset - non zero to reset all filters before applying this filter.
2926 * Notrace Filters denote which functions should not be enabled when tracing
2927 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2930 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
2932 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
2934 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
2937 * command line interface to allow users to set filters on boot up.
2939 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2940 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2941 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2943 static int __init
set_ftrace_notrace(char *str
)
2945 strncpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
2948 __setup("ftrace_notrace=", set_ftrace_notrace
);
2950 static int __init
set_ftrace_filter(char *str
)
2952 strncpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
2955 __setup("ftrace_filter=", set_ftrace_filter
);
2957 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2958 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2959 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
2961 static int __init
set_graph_function(char *str
)
2963 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
2966 __setup("ftrace_graph_filter=", set_graph_function
);
2968 static void __init
set_ftrace_early_graph(char *buf
)
2974 func
= strsep(&buf
, ",");
2975 /* we allow only one expression at a time */
2976 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2979 printk(KERN_DEBUG
"ftrace: function %s not "
2980 "traceable\n", func
);
2983 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2986 set_ftrace_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
2991 func
= strsep(&buf
, ",");
2992 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
2996 static void __init
set_ftrace_early_filters(void)
2998 if (ftrace_filter_buf
[0])
2999 set_ftrace_early_filter(&global_ops
, ftrace_filter_buf
, 1);
3000 if (ftrace_notrace_buf
[0])
3001 set_ftrace_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
3002 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3003 if (ftrace_graph_buf
[0])
3004 set_ftrace_early_graph(ftrace_graph_buf
);
3005 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3009 ftrace_regex_release(struct inode
*inode
, struct file
*file
)
3011 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
3012 struct ftrace_iterator
*iter
;
3013 struct ftrace_hash
**orig_hash
;
3014 struct trace_parser
*parser
;
3018 mutex_lock(&ftrace_regex_lock
);
3019 if (file
->f_mode
& FMODE_READ
) {
3022 seq_release(inode
, file
);
3024 iter
= file
->private_data
;
3026 parser
= &iter
->parser
;
3027 if (trace_parser_loaded(parser
)) {
3028 parser
->buffer
[parser
->idx
] = 0;
3029 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
3032 trace_parser_put(parser
);
3034 if (file
->f_mode
& FMODE_WRITE
) {
3035 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
3038 orig_hash
= &iter
->ops
->filter_hash
;
3040 orig_hash
= &iter
->ops
->notrace_hash
;
3042 mutex_lock(&ftrace_lock
);
3044 * Remove the current set, update the hash and add
3047 ftrace_hash_rec_disable(iter
->ops
, filter_hash
);
3048 ret
= ftrace_hash_move(orig_hash
, iter
->hash
);
3050 ftrace_hash_rec_enable(iter
->ops
, filter_hash
);
3051 if (iter
->ops
->flags
& FTRACE_OPS_FL_ENABLED
3053 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
3055 mutex_unlock(&ftrace_lock
);
3057 free_ftrace_hash(iter
->hash
);
3060 mutex_unlock(&ftrace_regex_lock
);
3064 static const struct file_operations ftrace_avail_fops
= {
3065 .open
= ftrace_avail_open
,
3067 .llseek
= seq_lseek
,
3068 .release
= seq_release_private
,
3071 static const struct file_operations ftrace_enabled_fops
= {
3072 .open
= ftrace_enabled_open
,
3074 .llseek
= seq_lseek
,
3075 .release
= seq_release_private
,
3078 static const struct file_operations ftrace_filter_fops
= {
3079 .open
= ftrace_filter_open
,
3081 .write
= ftrace_filter_write
,
3082 .llseek
= ftrace_regex_lseek
,
3083 .release
= ftrace_regex_release
,
3086 static const struct file_operations ftrace_notrace_fops
= {
3087 .open
= ftrace_notrace_open
,
3089 .write
= ftrace_notrace_write
,
3090 .llseek
= ftrace_regex_lseek
,
3091 .release
= ftrace_regex_release
,
3094 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3096 static DEFINE_MUTEX(graph_lock
);
3098 int ftrace_graph_count
;
3099 int ftrace_graph_filter_enabled
;
3100 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
3103 __g_next(struct seq_file
*m
, loff_t
*pos
)
3105 if (*pos
>= ftrace_graph_count
)
3107 return &ftrace_graph_funcs
[*pos
];
3111 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3114 return __g_next(m
, pos
);
3117 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
3119 mutex_lock(&graph_lock
);
3121 /* Nothing, tell g_show to print all functions are enabled */
3122 if (!ftrace_graph_filter_enabled
&& !*pos
)
3125 return __g_next(m
, pos
);
3128 static void g_stop(struct seq_file
*m
, void *p
)
3130 mutex_unlock(&graph_lock
);
3133 static int g_show(struct seq_file
*m
, void *v
)
3135 unsigned long *ptr
= v
;
3140 if (ptr
== (unsigned long *)1) {
3141 seq_printf(m
, "#### all functions enabled ####\n");
3145 seq_printf(m
, "%ps\n", (void *)*ptr
);
3150 static const struct seq_operations ftrace_graph_seq_ops
= {
3158 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
3162 if (unlikely(ftrace_disabled
))
3165 mutex_lock(&graph_lock
);
3166 if ((file
->f_mode
& FMODE_WRITE
) &&
3167 (file
->f_flags
& O_TRUNC
)) {
3168 ftrace_graph_filter_enabled
= 0;
3169 ftrace_graph_count
= 0;
3170 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
3172 mutex_unlock(&graph_lock
);
3174 if (file
->f_mode
& FMODE_READ
)
3175 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
3181 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
3183 if (file
->f_mode
& FMODE_READ
)
3184 seq_release(inode
, file
);
3189 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
3191 struct dyn_ftrace
*rec
;
3192 struct ftrace_page
*pg
;
3201 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
3202 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3205 search_len
= strlen(search
);
3207 mutex_lock(&ftrace_lock
);
3209 if (unlikely(ftrace_disabled
)) {
3210 mutex_unlock(&ftrace_lock
);
3214 do_for_each_ftrace_rec(pg
, rec
) {
3216 if (rec
->flags
& FTRACE_FL_FREE
)
3219 if (ftrace_match_record(rec
, NULL
, search
, search_len
, type
)) {
3220 /* if it is in the array */
3222 for (i
= 0; i
< *idx
; i
++) {
3223 if (array
[i
] == rec
->ip
) {
3232 array
[(*idx
)++] = rec
->ip
;
3233 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3238 array
[i
] = array
[--(*idx
)];
3244 } while_for_each_ftrace_rec();
3246 mutex_unlock(&ftrace_lock
);
3251 ftrace_graph_filter_enabled
= 1;
3256 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
3257 size_t cnt
, loff_t
*ppos
)
3259 struct trace_parser parser
;
3265 mutex_lock(&graph_lock
);
3267 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
3272 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
3274 if (read
>= 0 && trace_parser_loaded((&parser
))) {
3275 parser
.buffer
[parser
.idx
] = 0;
3277 /* we allow only one expression at a time */
3278 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3287 trace_parser_put(&parser
);
3289 mutex_unlock(&graph_lock
);
3294 static const struct file_operations ftrace_graph_fops
= {
3295 .open
= ftrace_graph_open
,
3297 .write
= ftrace_graph_write
,
3298 .release
= ftrace_graph_release
,
3299 .llseek
= seq_lseek
,
3301 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3303 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
3306 trace_create_file("available_filter_functions", 0444,
3307 d_tracer
, NULL
, &ftrace_avail_fops
);
3309 trace_create_file("enabled_functions", 0444,
3310 d_tracer
, NULL
, &ftrace_enabled_fops
);
3312 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
3313 NULL
, &ftrace_filter_fops
);
3315 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
3316 NULL
, &ftrace_notrace_fops
);
3318 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3319 trace_create_file("set_graph_function", 0444, d_tracer
,
3321 &ftrace_graph_fops
);
3322 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3327 static int ftrace_process_locs(struct module
*mod
,
3328 unsigned long *start
,
3334 mutex_lock(&ftrace_lock
);
3337 addr
= ftrace_call_adjust(*p
++);
3339 * Some architecture linkers will pad between
3340 * the different mcount_loc sections of different
3341 * object files to satisfy alignments.
3342 * Skip any NULL pointers.
3346 ftrace_record_ip(addr
);
3349 ftrace_update_code(mod
);
3350 mutex_unlock(&ftrace_lock
);
3355 #ifdef CONFIG_MODULES
3356 void ftrace_release_mod(struct module
*mod
)
3358 struct dyn_ftrace
*rec
;
3359 struct ftrace_page
*pg
;
3361 mutex_lock(&ftrace_lock
);
3363 if (ftrace_disabled
)
3366 do_for_each_ftrace_rec(pg
, rec
) {
3367 if (within_module_core(rec
->ip
, mod
)) {
3369 * rec->ip is changed in ftrace_free_rec()
3370 * It should not between s and e if record was freed.
3372 FTRACE_WARN_ON(rec
->flags
& FTRACE_FL_FREE
);
3373 ftrace_free_rec(rec
);
3375 } while_for_each_ftrace_rec();
3377 mutex_unlock(&ftrace_lock
);
3380 static void ftrace_init_module(struct module
*mod
,
3381 unsigned long *start
, unsigned long *end
)
3383 if (ftrace_disabled
|| start
== end
)
3385 ftrace_process_locs(mod
, start
, end
);
3388 static int ftrace_module_notify(struct notifier_block
*self
,
3389 unsigned long val
, void *data
)
3391 struct module
*mod
= data
;
3394 case MODULE_STATE_COMING
:
3395 ftrace_init_module(mod
, mod
->ftrace_callsites
,
3396 mod
->ftrace_callsites
+
3397 mod
->num_ftrace_callsites
);
3399 case MODULE_STATE_GOING
:
3400 ftrace_release_mod(mod
);
3407 static int ftrace_module_notify(struct notifier_block
*self
,
3408 unsigned long val
, void *data
)
3412 #endif /* CONFIG_MODULES */
3414 struct notifier_block ftrace_module_nb
= {
3415 .notifier_call
= ftrace_module_notify
,
3419 extern unsigned long __start_mcount_loc
[];
3420 extern unsigned long __stop_mcount_loc
[];
3422 void __init
ftrace_init(void)
3424 unsigned long count
, addr
, flags
;
3427 /* Keep the ftrace pointer to the stub */
3428 addr
= (unsigned long)ftrace_stub
;
3430 local_irq_save(flags
);
3431 ftrace_dyn_arch_init(&addr
);
3432 local_irq_restore(flags
);
3434 /* ftrace_dyn_arch_init places the return code in addr */
3438 count
= __stop_mcount_loc
- __start_mcount_loc
;
3440 ret
= ftrace_dyn_table_alloc(count
);
3444 last_ftrace_enabled
= ftrace_enabled
= 1;
3446 ret
= ftrace_process_locs(NULL
,
3450 ret
= register_module_notifier(&ftrace_module_nb
);
3452 pr_warning("Failed to register trace ftrace module notifier\n");
3454 set_ftrace_early_filters();
3458 ftrace_disabled
= 1;
3463 static struct ftrace_ops global_ops
= {
3464 .func
= ftrace_stub
,
3467 static int __init
ftrace_nodyn_init(void)
3472 device_initcall(ftrace_nodyn_init
);
3474 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
3475 static inline void ftrace_startup_enable(int command
) { }
3476 /* Keep as macros so we do not need to define the commands */
3477 # define ftrace_startup(ops, command) \
3479 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3482 # define ftrace_shutdown(ops, command) do { } while (0)
3483 # define ftrace_startup_sysctl() do { } while (0)
3484 # define ftrace_shutdown_sysctl() do { } while (0)
3487 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
)
3492 #endif /* CONFIG_DYNAMIC_FTRACE */
3495 ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
)
3497 struct ftrace_ops
*op
;
3499 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT
)))
3502 trace_recursion_set(TRACE_INTERNAL_BIT
);
3504 * Some of the ops may be dynamically allocated,
3505 * they must be freed after a synchronize_sched().
3507 preempt_disable_notrace();
3508 op
= rcu_dereference_raw(ftrace_ops_list
);
3509 while (op
!= &ftrace_list_end
) {
3510 if (ftrace_ops_test(op
, ip
))
3511 op
->func(ip
, parent_ip
);
3512 op
= rcu_dereference_raw(op
->next
);
3514 preempt_enable_notrace();
3515 trace_recursion_clear(TRACE_INTERNAL_BIT
);
3518 static void clear_ftrace_swapper(void)
3520 struct task_struct
*p
;
3524 for_each_online_cpu(cpu
) {
3526 clear_tsk_trace_trace(p
);
3531 static void set_ftrace_swapper(void)
3533 struct task_struct
*p
;
3537 for_each_online_cpu(cpu
) {
3539 set_tsk_trace_trace(p
);
3544 static void clear_ftrace_pid(struct pid
*pid
)
3546 struct task_struct
*p
;
3549 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
3550 clear_tsk_trace_trace(p
);
3551 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
3557 static void set_ftrace_pid(struct pid
*pid
)
3559 struct task_struct
*p
;
3562 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
3563 set_tsk_trace_trace(p
);
3564 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
3568 static void clear_ftrace_pid_task(struct pid
*pid
)
3570 if (pid
== ftrace_swapper_pid
)
3571 clear_ftrace_swapper();
3573 clear_ftrace_pid(pid
);
3576 static void set_ftrace_pid_task(struct pid
*pid
)
3578 if (pid
== ftrace_swapper_pid
)
3579 set_ftrace_swapper();
3581 set_ftrace_pid(pid
);
3584 static int ftrace_pid_add(int p
)
3587 struct ftrace_pid
*fpid
;
3590 mutex_lock(&ftrace_lock
);
3593 pid
= ftrace_swapper_pid
;
3595 pid
= find_get_pid(p
);
3602 list_for_each_entry(fpid
, &ftrace_pids
, list
)
3603 if (fpid
->pid
== pid
)
3608 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
3612 list_add(&fpid
->list
, &ftrace_pids
);
3615 set_ftrace_pid_task(pid
);
3617 ftrace_update_pid_func();
3618 ftrace_startup_enable(0);
3620 mutex_unlock(&ftrace_lock
);
3624 if (pid
!= ftrace_swapper_pid
)
3628 mutex_unlock(&ftrace_lock
);
3632 static void ftrace_pid_reset(void)
3634 struct ftrace_pid
*fpid
, *safe
;
3636 mutex_lock(&ftrace_lock
);
3637 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
3638 struct pid
*pid
= fpid
->pid
;
3640 clear_ftrace_pid_task(pid
);
3642 list_del(&fpid
->list
);
3646 ftrace_update_pid_func();
3647 ftrace_startup_enable(0);
3649 mutex_unlock(&ftrace_lock
);
3652 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
3654 mutex_lock(&ftrace_lock
);
3656 if (list_empty(&ftrace_pids
) && (!*pos
))
3659 return seq_list_start(&ftrace_pids
, *pos
);
3662 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3667 return seq_list_next(v
, &ftrace_pids
, pos
);
3670 static void fpid_stop(struct seq_file
*m
, void *p
)
3672 mutex_unlock(&ftrace_lock
);
3675 static int fpid_show(struct seq_file
*m
, void *v
)
3677 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
3679 if (v
== (void *)1) {
3680 seq_printf(m
, "no pid\n");
3684 if (fpid
->pid
== ftrace_swapper_pid
)
3685 seq_printf(m
, "swapper tasks\n");
3687 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
3692 static const struct seq_operations ftrace_pid_sops
= {
3693 .start
= fpid_start
,
3700 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
3704 if ((file
->f_mode
& FMODE_WRITE
) &&
3705 (file
->f_flags
& O_TRUNC
))
3708 if (file
->f_mode
& FMODE_READ
)
3709 ret
= seq_open(file
, &ftrace_pid_sops
);
3715 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
3716 size_t cnt
, loff_t
*ppos
)
3722 if (cnt
>= sizeof(buf
))
3725 if (copy_from_user(&buf
, ubuf
, cnt
))
3731 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3732 * to clean the filter quietly.
3734 tmp
= strstrip(buf
);
3735 if (strlen(tmp
) == 0)
3738 ret
= strict_strtol(tmp
, 10, &val
);
3742 ret
= ftrace_pid_add(val
);
3744 return ret
? ret
: cnt
;
3748 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
3750 if (file
->f_mode
& FMODE_READ
)
3751 seq_release(inode
, file
);
3756 static const struct file_operations ftrace_pid_fops
= {
3757 .open
= ftrace_pid_open
,
3758 .write
= ftrace_pid_write
,
3760 .llseek
= seq_lseek
,
3761 .release
= ftrace_pid_release
,
3764 static __init
int ftrace_init_debugfs(void)
3766 struct dentry
*d_tracer
;
3768 d_tracer
= tracing_init_dentry();
3772 ftrace_init_dyn_debugfs(d_tracer
);
3774 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
3775 NULL
, &ftrace_pid_fops
);
3777 ftrace_profile_debugfs(d_tracer
);
3781 fs_initcall(ftrace_init_debugfs
);
3784 * ftrace_kill - kill ftrace
3786 * This function should be used by panic code. It stops ftrace
3787 * but in a not so nice way. If you need to simply kill ftrace
3788 * from a non-atomic section, use ftrace_kill.
3790 void ftrace_kill(void)
3792 ftrace_disabled
= 1;
3794 clear_ftrace_function();
3798 * register_ftrace_function - register a function for profiling
3799 * @ops - ops structure that holds the function for profiling.
3801 * Register a function to be called by all functions in the
3804 * Note: @ops->func and all the functions it calls must be labeled
3805 * with "notrace", otherwise it will go into a
3808 int register_ftrace_function(struct ftrace_ops
*ops
)
3812 mutex_lock(&ftrace_lock
);
3814 if (unlikely(ftrace_disabled
))
3817 ret
= __register_ftrace_function(ops
);
3819 ret
= ftrace_startup(ops
, 0);
3823 mutex_unlock(&ftrace_lock
);
3826 EXPORT_SYMBOL_GPL(register_ftrace_function
);
3829 * unregister_ftrace_function - unregister a function for profiling.
3830 * @ops - ops structure that holds the function to unregister
3832 * Unregister a function that was added to be called by ftrace profiling.
3834 int unregister_ftrace_function(struct ftrace_ops
*ops
)
3838 mutex_lock(&ftrace_lock
);
3839 ret
= __unregister_ftrace_function(ops
);
3841 ftrace_shutdown(ops
, 0);
3842 mutex_unlock(&ftrace_lock
);
3846 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
3849 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
3850 void __user
*buffer
, size_t *lenp
,
3855 mutex_lock(&ftrace_lock
);
3857 if (unlikely(ftrace_disabled
))
3860 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3862 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
3865 last_ftrace_enabled
= !!ftrace_enabled
;
3867 if (ftrace_enabled
) {
3869 ftrace_startup_sysctl();
3871 /* we are starting ftrace again */
3872 if (ftrace_ops_list
!= &ftrace_list_end
) {
3873 if (ftrace_ops_list
->next
== &ftrace_list_end
)
3874 ftrace_trace_function
= ftrace_ops_list
->func
;
3876 ftrace_trace_function
= ftrace_ops_list_func
;
3880 /* stopping ftrace calls (just send to ftrace_stub) */
3881 ftrace_trace_function
= ftrace_stub
;
3883 ftrace_shutdown_sysctl();
3887 mutex_unlock(&ftrace_lock
);
3891 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3893 static int ftrace_graph_active
;
3894 static struct notifier_block ftrace_suspend_notifier
;
3896 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3901 /* The callbacks that hook a function */
3902 trace_func_graph_ret_t ftrace_graph_return
=
3903 (trace_func_graph_ret_t
)ftrace_stub
;
3904 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3906 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3907 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3911 unsigned long flags
;
3912 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3913 struct task_struct
*g
, *t
;
3915 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3916 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3917 * sizeof(struct ftrace_ret_stack
),
3919 if (!ret_stack_list
[i
]) {
3927 read_lock_irqsave(&tasklist_lock
, flags
);
3928 do_each_thread(g
, t
) {
3934 if (t
->ret_stack
== NULL
) {
3935 atomic_set(&t
->tracing_graph_pause
, 0);
3936 atomic_set(&t
->trace_overrun
, 0);
3937 t
->curr_ret_stack
= -1;
3938 /* Make sure the tasks see the -1 first: */
3940 t
->ret_stack
= ret_stack_list
[start
++];
3942 } while_each_thread(g
, t
);
3945 read_unlock_irqrestore(&tasklist_lock
, flags
);
3947 for (i
= start
; i
< end
; i
++)
3948 kfree(ret_stack_list
[i
]);
3953 ftrace_graph_probe_sched_switch(void *ignore
,
3954 struct task_struct
*prev
, struct task_struct
*next
)
3956 unsigned long long timestamp
;
3960 * Does the user want to count the time a function was asleep.
3961 * If so, do not update the time stamps.
3963 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
3966 timestamp
= trace_clock_local();
3968 prev
->ftrace_timestamp
= timestamp
;
3970 /* only process tasks that we timestamped */
3971 if (!next
->ftrace_timestamp
)
3975 * Update all the counters in next to make up for the
3976 * time next was sleeping.
3978 timestamp
-= next
->ftrace_timestamp
;
3980 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
3981 next
->ret_stack
[index
].calltime
+= timestamp
;
3984 /* Allocate a return stack for each task */
3985 static int start_graph_tracing(void)
3987 struct ftrace_ret_stack
**ret_stack_list
;
3990 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
3991 sizeof(struct ftrace_ret_stack
*),
3994 if (!ret_stack_list
)
3997 /* The cpu_boot init_task->ret_stack will never be freed */
3998 for_each_online_cpu(cpu
) {
3999 if (!idle_task(cpu
)->ret_stack
)
4000 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
4004 ret
= alloc_retstack_tasklist(ret_stack_list
);
4005 } while (ret
== -EAGAIN
);
4008 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4010 pr_info("ftrace_graph: Couldn't activate tracepoint"
4011 " probe to kernel_sched_switch\n");
4014 kfree(ret_stack_list
);
4019 * Hibernation protection.
4020 * The state of the current task is too much unstable during
4021 * suspend/restore to disk. We want to protect against that.
4024 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
4028 case PM_HIBERNATION_PREPARE
:
4029 pause_graph_tracing();
4032 case PM_POST_HIBERNATION
:
4033 unpause_graph_tracing();
4039 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
4040 trace_func_graph_ent_t entryfunc
)
4044 mutex_lock(&ftrace_lock
);
4046 /* we currently allow only one tracer registered at a time */
4047 if (ftrace_graph_active
) {
4052 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
4053 register_pm_notifier(&ftrace_suspend_notifier
);
4055 ftrace_graph_active
++;
4056 ret
= start_graph_tracing();
4058 ftrace_graph_active
--;
4062 ftrace_graph_return
= retfunc
;
4063 ftrace_graph_entry
= entryfunc
;
4065 ret
= ftrace_startup(&global_ops
, FTRACE_START_FUNC_RET
);
4068 mutex_unlock(&ftrace_lock
);
4072 void unregister_ftrace_graph(void)
4074 mutex_lock(&ftrace_lock
);
4076 if (unlikely(!ftrace_graph_active
))
4079 ftrace_graph_active
--;
4080 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
4081 ftrace_graph_entry
= ftrace_graph_entry_stub
;
4082 ftrace_shutdown(&global_ops
, FTRACE_STOP_FUNC_RET
);
4083 unregister_pm_notifier(&ftrace_suspend_notifier
);
4084 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4087 mutex_unlock(&ftrace_lock
);
4090 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
4093 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
4095 atomic_set(&t
->tracing_graph_pause
, 0);
4096 atomic_set(&t
->trace_overrun
, 0);
4097 t
->ftrace_timestamp
= 0;
4098 /* make curr_ret_stack visible before we add the ret_stack */
4100 t
->ret_stack
= ret_stack
;
4104 * Allocate a return stack for the idle task. May be the first
4105 * time through, or it may be done by CPU hotplug online.
4107 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
4109 t
->curr_ret_stack
= -1;
4111 * The idle task has no parent, it either has its own
4112 * stack or no stack at all.
4115 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
4117 if (ftrace_graph_active
) {
4118 struct ftrace_ret_stack
*ret_stack
;
4120 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
4122 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4123 * sizeof(struct ftrace_ret_stack
),
4127 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
4129 graph_init_task(t
, ret_stack
);
4133 /* Allocate a return stack for newly created task */
4134 void ftrace_graph_init_task(struct task_struct
*t
)
4136 /* Make sure we do not use the parent ret_stack */
4137 t
->ret_stack
= NULL
;
4138 t
->curr_ret_stack
= -1;
4140 if (ftrace_graph_active
) {
4141 struct ftrace_ret_stack
*ret_stack
;
4143 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4144 * sizeof(struct ftrace_ret_stack
),
4148 graph_init_task(t
, ret_stack
);
4152 void ftrace_graph_exit_task(struct task_struct
*t
)
4154 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
4156 t
->ret_stack
= NULL
;
4157 /* NULL must become visible to IRQs before we free it: */
4163 void ftrace_graph_stop(void)