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) \
47 #define FTRACE_WARN_ON_ONCE(cond) \
49 if (WARN_ON_ONCE(cond)) \
53 /* hash bits for specific function selection */
54 #define FTRACE_HASH_BITS 7
55 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
57 /* ftrace_enabled is a method to turn ftrace on or off */
58 int ftrace_enabled __read_mostly
;
59 static int last_ftrace_enabled
;
61 /* Quick disabling of function tracer. */
62 int function_trace_stop
;
64 /* List for set_ftrace_pid's pids. */
65 LIST_HEAD(ftrace_pids
);
67 struct list_head list
;
72 * ftrace_disabled is set when an anomaly is discovered.
73 * ftrace_disabled is much stronger than ftrace_enabled.
75 static int ftrace_disabled __read_mostly
;
77 static DEFINE_MUTEX(ftrace_lock
);
79 static struct ftrace_ops ftrace_list_end __read_mostly
=
84 static struct ftrace_ops
*ftrace_list __read_mostly
= &ftrace_list_end
;
85 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
86 ftrace_func_t __ftrace_trace_function __read_mostly
= ftrace_stub
;
87 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
90 * Traverse the ftrace_list, invoking all entries. The reason that we
91 * can use rcu_dereference_raw() is that elements removed from this list
92 * are simply leaked, so there is no need to interact with a grace-period
93 * mechanism. The rcu_dereference_raw() calls are needed to handle
94 * concurrent insertions into the ftrace_list.
96 * Silly Alpha and silly pointer-speculation compiler optimizations!
98 static void ftrace_list_func(unsigned long ip
, unsigned long parent_ip
)
100 struct ftrace_ops
*op
= rcu_dereference_raw(ftrace_list
); /*see above*/
102 while (op
!= &ftrace_list_end
) {
103 op
->func(ip
, parent_ip
);
104 op
= rcu_dereference_raw(op
->next
); /*see above*/
108 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
)
110 if (!test_tsk_trace_trace(current
))
113 ftrace_pid_function(ip
, parent_ip
);
116 static void set_ftrace_pid_function(ftrace_func_t func
)
118 /* do not set ftrace_pid_function to itself! */
119 if (func
!= ftrace_pid_func
)
120 ftrace_pid_function
= func
;
124 * clear_ftrace_function - reset the ftrace function
126 * This NULLs the ftrace function and in essence stops
127 * tracing. There may be lag
129 void clear_ftrace_function(void)
131 ftrace_trace_function
= ftrace_stub
;
132 __ftrace_trace_function
= ftrace_stub
;
133 ftrace_pid_function
= ftrace_stub
;
136 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
138 * For those archs that do not test ftrace_trace_stop in their
139 * mcount call site, we need to do it from C.
141 static void ftrace_test_stop_func(unsigned long ip
, unsigned long parent_ip
)
143 if (function_trace_stop
)
146 __ftrace_trace_function(ip
, parent_ip
);
150 static int __register_ftrace_function(struct ftrace_ops
*ops
)
152 ops
->next
= ftrace_list
;
154 * We are entering ops into the ftrace_list but another
155 * CPU might be walking that list. We need to make sure
156 * the ops->next pointer is valid before another CPU sees
157 * the ops pointer included into the ftrace_list.
159 rcu_assign_pointer(ftrace_list
, ops
);
161 if (ftrace_enabled
) {
164 if (ops
->next
== &ftrace_list_end
)
167 func
= ftrace_list_func
;
169 if (!list_empty(&ftrace_pids
)) {
170 set_ftrace_pid_function(func
);
171 func
= ftrace_pid_func
;
175 * For one func, simply call it directly.
176 * For more than one func, call the chain.
178 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
179 ftrace_trace_function
= func
;
181 __ftrace_trace_function
= func
;
182 ftrace_trace_function
= ftrace_test_stop_func
;
189 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
191 struct ftrace_ops
**p
;
194 * If we are removing the last function, then simply point
195 * to the ftrace_stub.
197 if (ftrace_list
== ops
&& ops
->next
== &ftrace_list_end
) {
198 ftrace_trace_function
= ftrace_stub
;
199 ftrace_list
= &ftrace_list_end
;
203 for (p
= &ftrace_list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
212 if (ftrace_enabled
) {
213 /* If we only have one func left, then call that directly */
214 if (ftrace_list
->next
== &ftrace_list_end
) {
215 ftrace_func_t func
= ftrace_list
->func
;
217 if (!list_empty(&ftrace_pids
)) {
218 set_ftrace_pid_function(func
);
219 func
= ftrace_pid_func
;
221 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
222 ftrace_trace_function
= func
;
224 __ftrace_trace_function
= func
;
232 static void ftrace_update_pid_func(void)
236 if (ftrace_trace_function
== ftrace_stub
)
239 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
240 func
= ftrace_trace_function
;
242 func
= __ftrace_trace_function
;
245 if (!list_empty(&ftrace_pids
)) {
246 set_ftrace_pid_function(func
);
247 func
= ftrace_pid_func
;
249 if (func
== ftrace_pid_func
)
250 func
= ftrace_pid_function
;
253 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
254 ftrace_trace_function
= func
;
256 __ftrace_trace_function
= func
;
260 #ifdef CONFIG_FUNCTION_PROFILER
261 struct ftrace_profile
{
262 struct hlist_node node
;
264 unsigned long counter
;
265 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
266 unsigned long long time
;
267 unsigned long long time_squared
;
271 struct ftrace_profile_page
{
272 struct ftrace_profile_page
*next
;
274 struct ftrace_profile records
[];
277 struct ftrace_profile_stat
{
279 struct hlist_head
*hash
;
280 struct ftrace_profile_page
*pages
;
281 struct ftrace_profile_page
*start
;
282 struct tracer_stat stat
;
285 #define PROFILE_RECORDS_SIZE \
286 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
288 #define PROFILES_PER_PAGE \
289 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
291 static int ftrace_profile_bits __read_mostly
;
292 static int ftrace_profile_enabled __read_mostly
;
294 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
295 static DEFINE_MUTEX(ftrace_profile_lock
);
297 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
299 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
302 function_stat_next(void *v
, int idx
)
304 struct ftrace_profile
*rec
= v
;
305 struct ftrace_profile_page
*pg
;
307 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
313 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
317 rec
= &pg
->records
[0];
325 static void *function_stat_start(struct tracer_stat
*trace
)
327 struct ftrace_profile_stat
*stat
=
328 container_of(trace
, struct ftrace_profile_stat
, stat
);
330 if (!stat
|| !stat
->start
)
333 return function_stat_next(&stat
->start
->records
[0], 0);
336 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
337 /* function graph compares on total time */
338 static int function_stat_cmp(void *p1
, void *p2
)
340 struct ftrace_profile
*a
= p1
;
341 struct ftrace_profile
*b
= p2
;
343 if (a
->time
< b
->time
)
345 if (a
->time
> b
->time
)
351 /* not function graph compares against hits */
352 static int function_stat_cmp(void *p1
, void *p2
)
354 struct ftrace_profile
*a
= p1
;
355 struct ftrace_profile
*b
= p2
;
357 if (a
->counter
< b
->counter
)
359 if (a
->counter
> b
->counter
)
366 static int function_stat_headers(struct seq_file
*m
)
368 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
369 seq_printf(m
, " Function "
372 "--- ---- --- ---\n");
374 seq_printf(m
, " Function Hit\n"
380 static int function_stat_show(struct seq_file
*m
, void *v
)
382 struct ftrace_profile
*rec
= v
;
383 char str
[KSYM_SYMBOL_LEN
];
385 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
386 static struct trace_seq s
;
387 unsigned long long avg
;
388 unsigned long long stddev
;
390 mutex_lock(&ftrace_profile_lock
);
392 /* we raced with function_profile_reset() */
393 if (unlikely(rec
->counter
== 0)) {
398 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
399 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
401 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
404 do_div(avg
, rec
->counter
);
406 /* Sample standard deviation (s^2) */
407 if (rec
->counter
<= 1)
410 stddev
= rec
->time_squared
- rec
->counter
* avg
* avg
;
412 * Divide only 1000 for ns^2 -> us^2 conversion.
413 * trace_print_graph_duration will divide 1000 again.
415 do_div(stddev
, (rec
->counter
- 1) * 1000);
419 trace_print_graph_duration(rec
->time
, &s
);
420 trace_seq_puts(&s
, " ");
421 trace_print_graph_duration(avg
, &s
);
422 trace_seq_puts(&s
, " ");
423 trace_print_graph_duration(stddev
, &s
);
424 trace_print_seq(m
, &s
);
428 mutex_unlock(&ftrace_profile_lock
);
433 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
435 struct ftrace_profile_page
*pg
;
437 pg
= stat
->pages
= stat
->start
;
440 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
445 memset(stat
->hash
, 0,
446 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
449 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
451 struct ftrace_profile_page
*pg
;
456 /* If we already allocated, do nothing */
460 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
464 #ifdef CONFIG_DYNAMIC_FTRACE
465 functions
= ftrace_update_tot_cnt
;
468 * We do not know the number of functions that exist because
469 * dynamic tracing is what counts them. With past experience
470 * we have around 20K functions. That should be more than enough.
471 * It is highly unlikely we will execute every function in
477 pg
= stat
->start
= stat
->pages
;
479 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
481 for (i
= 0; i
< pages
; i
++) {
482 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
493 unsigned long tmp
= (unsigned long)pg
;
499 free_page((unsigned long)stat
->pages
);
506 static int ftrace_profile_init_cpu(int cpu
)
508 struct ftrace_profile_stat
*stat
;
511 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
514 /* If the profile is already created, simply reset it */
515 ftrace_profile_reset(stat
);
520 * We are profiling all functions, but usually only a few thousand
521 * functions are hit. We'll make a hash of 1024 items.
523 size
= FTRACE_PROFILE_HASH_SIZE
;
525 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
530 if (!ftrace_profile_bits
) {
533 for (; size
; size
>>= 1)
534 ftrace_profile_bits
++;
537 /* Preallocate the function profiling pages */
538 if (ftrace_profile_pages_init(stat
) < 0) {
547 static int ftrace_profile_init(void)
552 for_each_online_cpu(cpu
) {
553 ret
= ftrace_profile_init_cpu(cpu
);
561 /* interrupts must be disabled */
562 static struct ftrace_profile
*
563 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
565 struct ftrace_profile
*rec
;
566 struct hlist_head
*hhd
;
567 struct hlist_node
*n
;
570 key
= hash_long(ip
, ftrace_profile_bits
);
571 hhd
= &stat
->hash
[key
];
573 if (hlist_empty(hhd
))
576 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
584 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
585 struct ftrace_profile
*rec
)
589 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
590 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
594 * The memory is already allocated, this simply finds a new record to use.
596 static struct ftrace_profile
*
597 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
599 struct ftrace_profile
*rec
= NULL
;
601 /* prevent recursion (from NMIs) */
602 if (atomic_inc_return(&stat
->disabled
) != 1)
606 * Try to find the function again since an NMI
607 * could have added it
609 rec
= ftrace_find_profiled_func(stat
, ip
);
613 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
614 if (!stat
->pages
->next
)
616 stat
->pages
= stat
->pages
->next
;
619 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
621 ftrace_add_profile(stat
, rec
);
624 atomic_dec(&stat
->disabled
);
630 function_profile_call(unsigned long ip
, unsigned long parent_ip
)
632 struct ftrace_profile_stat
*stat
;
633 struct ftrace_profile
*rec
;
636 if (!ftrace_profile_enabled
)
639 local_irq_save(flags
);
641 stat
= &__get_cpu_var(ftrace_profile_stats
);
642 if (!stat
->hash
|| !ftrace_profile_enabled
)
645 rec
= ftrace_find_profiled_func(stat
, ip
);
647 rec
= ftrace_profile_alloc(stat
, ip
);
654 local_irq_restore(flags
);
657 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
658 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
660 function_profile_call(trace
->func
, 0);
664 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
666 struct ftrace_profile_stat
*stat
;
667 unsigned long long calltime
;
668 struct ftrace_profile
*rec
;
671 local_irq_save(flags
);
672 stat
= &__get_cpu_var(ftrace_profile_stats
);
673 if (!stat
->hash
|| !ftrace_profile_enabled
)
676 /* If the calltime was zero'd ignore it */
677 if (!trace
->calltime
)
680 calltime
= trace
->rettime
- trace
->calltime
;
682 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
685 index
= trace
->depth
;
687 /* Append this call time to the parent time to subtract */
689 current
->ret_stack
[index
- 1].subtime
+= calltime
;
691 if (current
->ret_stack
[index
].subtime
< calltime
)
692 calltime
-= current
->ret_stack
[index
].subtime
;
697 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
699 rec
->time
+= calltime
;
700 rec
->time_squared
+= calltime
* calltime
;
704 local_irq_restore(flags
);
707 static int register_ftrace_profiler(void)
709 return register_ftrace_graph(&profile_graph_return
,
710 &profile_graph_entry
);
713 static void unregister_ftrace_profiler(void)
715 unregister_ftrace_graph();
718 static struct ftrace_ops ftrace_profile_ops __read_mostly
=
720 .func
= function_profile_call
,
723 static int register_ftrace_profiler(void)
725 return register_ftrace_function(&ftrace_profile_ops
);
728 static void unregister_ftrace_profiler(void)
730 unregister_ftrace_function(&ftrace_profile_ops
);
732 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
735 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
736 size_t cnt
, loff_t
*ppos
)
739 char buf
[64]; /* big enough to hold a number */
742 if (cnt
>= sizeof(buf
))
745 if (copy_from_user(&buf
, ubuf
, cnt
))
750 ret
= strict_strtoul(buf
, 10, &val
);
756 mutex_lock(&ftrace_profile_lock
);
757 if (ftrace_profile_enabled
^ val
) {
759 ret
= ftrace_profile_init();
765 ret
= register_ftrace_profiler();
770 ftrace_profile_enabled
= 1;
772 ftrace_profile_enabled
= 0;
774 * unregister_ftrace_profiler calls stop_machine
775 * so this acts like an synchronize_sched.
777 unregister_ftrace_profiler();
781 mutex_unlock(&ftrace_profile_lock
);
789 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
790 size_t cnt
, loff_t
*ppos
)
792 char buf
[64]; /* big enough to hold a number */
795 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
796 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
799 static const struct file_operations ftrace_profile_fops
= {
800 .open
= tracing_open_generic
,
801 .read
= ftrace_profile_read
,
802 .write
= ftrace_profile_write
,
803 .llseek
= default_llseek
,
806 /* used to initialize the real stat files */
807 static struct tracer_stat function_stats __initdata
= {
809 .stat_start
= function_stat_start
,
810 .stat_next
= function_stat_next
,
811 .stat_cmp
= function_stat_cmp
,
812 .stat_headers
= function_stat_headers
,
813 .stat_show
= function_stat_show
816 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
818 struct ftrace_profile_stat
*stat
;
819 struct dentry
*entry
;
824 for_each_possible_cpu(cpu
) {
825 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
827 /* allocate enough for function name + cpu number */
828 name
= kmalloc(32, GFP_KERNEL
);
831 * The files created are permanent, if something happens
832 * we still do not free memory.
835 "Could not allocate stat file for cpu %d\n",
839 stat
->stat
= function_stats
;
840 snprintf(name
, 32, "function%d", cpu
);
841 stat
->stat
.name
= name
;
842 ret
= register_stat_tracer(&stat
->stat
);
845 "Could not register function stat for cpu %d\n",
852 entry
= debugfs_create_file("function_profile_enabled", 0644,
853 d_tracer
, NULL
, &ftrace_profile_fops
);
855 pr_warning("Could not create debugfs "
856 "'function_profile_enabled' entry\n");
859 #else /* CONFIG_FUNCTION_PROFILER */
860 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
863 #endif /* CONFIG_FUNCTION_PROFILER */
865 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
867 #ifdef CONFIG_DYNAMIC_FTRACE
869 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
870 # error Dynamic ftrace depends on MCOUNT_RECORD
873 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
875 struct ftrace_func_probe
{
876 struct hlist_node node
;
877 struct ftrace_probe_ops
*ops
;
885 FTRACE_ENABLE_CALLS
= (1 << 0),
886 FTRACE_DISABLE_CALLS
= (1 << 1),
887 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
888 FTRACE_START_FUNC_RET
= (1 << 3),
889 FTRACE_STOP_FUNC_RET
= (1 << 4),
892 static int ftrace_filtered
;
894 static struct dyn_ftrace
*ftrace_new_addrs
;
896 static DEFINE_MUTEX(ftrace_regex_lock
);
899 struct ftrace_page
*next
;
901 struct dyn_ftrace records
[];
904 #define ENTRIES_PER_PAGE \
905 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
907 /* estimate from running different kernels */
908 #define NR_TO_INIT 10000
910 static struct ftrace_page
*ftrace_pages_start
;
911 static struct ftrace_page
*ftrace_pages
;
913 static struct dyn_ftrace
*ftrace_free_records
;
916 * This is a double for. Do not use 'break' to break out of the loop,
917 * you must use a goto.
919 #define do_for_each_ftrace_rec(pg, rec) \
920 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
922 for (_____i = 0; _____i < pg->index; _____i++) { \
923 rec = &pg->records[_____i];
925 #define while_for_each_ftrace_rec() \
929 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
931 rec
->freelist
= ftrace_free_records
;
932 ftrace_free_records
= rec
;
933 rec
->flags
|= FTRACE_FL_FREE
;
936 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
938 struct dyn_ftrace
*rec
;
940 /* First check for freed records */
941 if (ftrace_free_records
) {
942 rec
= ftrace_free_records
;
944 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
945 FTRACE_WARN_ON_ONCE(1);
946 ftrace_free_records
= NULL
;
950 ftrace_free_records
= rec
->freelist
;
951 memset(rec
, 0, sizeof(*rec
));
955 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
956 if (!ftrace_pages
->next
) {
957 /* allocate another page */
959 (void *)get_zeroed_page(GFP_KERNEL
);
960 if (!ftrace_pages
->next
)
963 ftrace_pages
= ftrace_pages
->next
;
966 return &ftrace_pages
->records
[ftrace_pages
->index
++];
969 static struct dyn_ftrace
*
970 ftrace_record_ip(unsigned long ip
)
972 struct dyn_ftrace
*rec
;
977 rec
= ftrace_alloc_dyn_node(ip
);
982 rec
->newlist
= ftrace_new_addrs
;
983 ftrace_new_addrs
= rec
;
988 static void print_ip_ins(const char *fmt
, unsigned char *p
)
992 printk(KERN_CONT
"%s", fmt
);
994 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
995 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
998 static void ftrace_bug(int failed
, unsigned long ip
)
1002 FTRACE_WARN_ON_ONCE(1);
1003 pr_info("ftrace faulted on modifying ");
1007 FTRACE_WARN_ON_ONCE(1);
1008 pr_info("ftrace failed to modify ");
1010 print_ip_ins(" actual: ", (unsigned char *)ip
);
1011 printk(KERN_CONT
"\n");
1014 FTRACE_WARN_ON_ONCE(1);
1015 pr_info("ftrace faulted on writing ");
1019 FTRACE_WARN_ON_ONCE(1);
1020 pr_info("ftrace faulted on unknown error ");
1026 /* Return 1 if the address range is reserved for ftrace */
1027 int ftrace_text_reserved(void *start
, void *end
)
1029 struct dyn_ftrace
*rec
;
1030 struct ftrace_page
*pg
;
1032 do_for_each_ftrace_rec(pg
, rec
) {
1033 if (rec
->ip
<= (unsigned long)end
&&
1034 rec
->ip
+ MCOUNT_INSN_SIZE
> (unsigned long)start
)
1036 } while_for_each_ftrace_rec();
1042 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1044 unsigned long ftrace_addr
;
1045 unsigned long flag
= 0UL;
1047 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1050 * If this record is not to be traced or we want to disable it,
1053 * If we want to enable it and filtering is off, then enable it.
1055 * If we want to enable it and filtering is on, enable it only if
1058 if (enable
&& !(rec
->flags
& FTRACE_FL_NOTRACE
)) {
1059 if (!ftrace_filtered
|| (rec
->flags
& FTRACE_FL_FILTER
))
1060 flag
= FTRACE_FL_ENABLED
;
1063 /* If the state of this record hasn't changed, then do nothing */
1064 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1068 rec
->flags
|= FTRACE_FL_ENABLED
;
1069 return ftrace_make_call(rec
, ftrace_addr
);
1072 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1073 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1076 static void ftrace_replace_code(int enable
)
1078 struct dyn_ftrace
*rec
;
1079 struct ftrace_page
*pg
;
1082 do_for_each_ftrace_rec(pg
, rec
) {
1084 * Skip over free records, records that have
1085 * failed and not converted.
1087 if (rec
->flags
& FTRACE_FL_FREE
||
1088 rec
->flags
& FTRACE_FL_FAILED
||
1089 !(rec
->flags
& FTRACE_FL_CONVERTED
))
1092 failed
= __ftrace_replace_code(rec
, enable
);
1094 rec
->flags
|= FTRACE_FL_FAILED
;
1095 ftrace_bug(failed
, rec
->ip
);
1096 /* Stop processing */
1099 } while_for_each_ftrace_rec();
1103 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1110 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1112 ftrace_bug(ret
, ip
);
1113 rec
->flags
|= FTRACE_FL_FAILED
;
1120 * archs can override this function if they must do something
1121 * before the modifying code is performed.
1123 int __weak
ftrace_arch_code_modify_prepare(void)
1129 * archs can override this function if they must do something
1130 * after the modifying code is performed.
1132 int __weak
ftrace_arch_code_modify_post_process(void)
1137 static int __ftrace_modify_code(void *data
)
1139 int *command
= data
;
1141 if (*command
& FTRACE_ENABLE_CALLS
)
1142 ftrace_replace_code(1);
1143 else if (*command
& FTRACE_DISABLE_CALLS
)
1144 ftrace_replace_code(0);
1146 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1147 ftrace_update_ftrace_func(ftrace_trace_function
);
1149 if (*command
& FTRACE_START_FUNC_RET
)
1150 ftrace_enable_ftrace_graph_caller();
1151 else if (*command
& FTRACE_STOP_FUNC_RET
)
1152 ftrace_disable_ftrace_graph_caller();
1157 static void ftrace_run_update_code(int command
)
1161 ret
= ftrace_arch_code_modify_prepare();
1162 FTRACE_WARN_ON(ret
);
1166 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1168 ret
= ftrace_arch_code_modify_post_process();
1169 FTRACE_WARN_ON(ret
);
1172 static ftrace_func_t saved_ftrace_func
;
1173 static int ftrace_start_up
;
1175 static void ftrace_startup_enable(int command
)
1177 if (saved_ftrace_func
!= ftrace_trace_function
) {
1178 saved_ftrace_func
= ftrace_trace_function
;
1179 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1182 if (!command
|| !ftrace_enabled
)
1185 ftrace_run_update_code(command
);
1188 static void ftrace_startup(int command
)
1190 if (unlikely(ftrace_disabled
))
1194 command
|= FTRACE_ENABLE_CALLS
;
1196 ftrace_startup_enable(command
);
1199 static void ftrace_shutdown(int command
)
1201 if (unlikely(ftrace_disabled
))
1206 * Just warn in case of unbalance, no need to kill ftrace, it's not
1207 * critical but the ftrace_call callers may be never nopped again after
1208 * further ftrace uses.
1210 WARN_ON_ONCE(ftrace_start_up
< 0);
1212 if (!ftrace_start_up
)
1213 command
|= FTRACE_DISABLE_CALLS
;
1215 if (saved_ftrace_func
!= ftrace_trace_function
) {
1216 saved_ftrace_func
= ftrace_trace_function
;
1217 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1220 if (!command
|| !ftrace_enabled
)
1223 ftrace_run_update_code(command
);
1226 static void ftrace_startup_sysctl(void)
1228 if (unlikely(ftrace_disabled
))
1231 /* Force update next time */
1232 saved_ftrace_func
= NULL
;
1233 /* ftrace_start_up is true if we want ftrace running */
1234 if (ftrace_start_up
)
1235 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
1238 static void ftrace_shutdown_sysctl(void)
1240 if (unlikely(ftrace_disabled
))
1243 /* ftrace_start_up is true if ftrace is running */
1244 if (ftrace_start_up
)
1245 ftrace_run_update_code(FTRACE_DISABLE_CALLS
);
1248 static cycle_t ftrace_update_time
;
1249 static unsigned long ftrace_update_cnt
;
1250 unsigned long ftrace_update_tot_cnt
;
1252 static int ftrace_update_code(struct module
*mod
)
1254 struct dyn_ftrace
*p
;
1255 cycle_t start
, stop
;
1257 start
= ftrace_now(raw_smp_processor_id());
1258 ftrace_update_cnt
= 0;
1260 while (ftrace_new_addrs
) {
1262 /* If something went wrong, bail without enabling anything */
1263 if (unlikely(ftrace_disabled
))
1266 p
= ftrace_new_addrs
;
1267 ftrace_new_addrs
= p
->newlist
;
1271 * Do the initial record conversion from mcount jump
1272 * to the NOP instructions.
1274 if (!ftrace_code_disable(mod
, p
)) {
1279 p
->flags
|= FTRACE_FL_CONVERTED
;
1280 ftrace_update_cnt
++;
1283 * If the tracing is enabled, go ahead and enable the record.
1285 * The reason not to enable the record immediatelly is the
1286 * inherent check of ftrace_make_nop/ftrace_make_call for
1287 * correct previous instructions. Making first the NOP
1288 * conversion puts the module to the correct state, thus
1289 * passing the ftrace_make_call check.
1291 if (ftrace_start_up
) {
1292 int failed
= __ftrace_replace_code(p
, 1);
1294 ftrace_bug(failed
, p
->ip
);
1300 stop
= ftrace_now(raw_smp_processor_id());
1301 ftrace_update_time
= stop
- start
;
1302 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1307 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1309 struct ftrace_page
*pg
;
1313 /* allocate a few pages */
1314 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1315 if (!ftrace_pages_start
)
1319 * Allocate a few more pages.
1321 * TODO: have some parser search vmlinux before
1322 * final linking to find all calls to ftrace.
1324 * a) know how many pages to allocate.
1326 * b) set up the table then.
1328 * The dynamic code is still necessary for
1332 pg
= ftrace_pages
= ftrace_pages_start
;
1334 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1335 pr_info("ftrace: allocating %ld entries in %d pages\n",
1336 num_to_init
, cnt
+ 1);
1338 for (i
= 0; i
< cnt
; i
++) {
1339 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1341 /* If we fail, we'll try later anyway */
1352 FTRACE_ITER_FILTER
= (1 << 0),
1353 FTRACE_ITER_NOTRACE
= (1 << 1),
1354 FTRACE_ITER_FAILURES
= (1 << 2),
1355 FTRACE_ITER_PRINTALL
= (1 << 3),
1356 FTRACE_ITER_HASH
= (1 << 4),
1359 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1361 struct ftrace_iterator
{
1364 struct ftrace_page
*pg
;
1365 struct dyn_ftrace
*func
;
1366 struct ftrace_func_probe
*probe
;
1367 struct trace_parser parser
;
1374 t_hash_next(struct seq_file
*m
, loff_t
*pos
)
1376 struct ftrace_iterator
*iter
= m
->private;
1377 struct hlist_node
*hnd
= NULL
;
1378 struct hlist_head
*hhd
;
1384 hnd
= &iter
->probe
->node
;
1386 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1389 hhd
= &ftrace_func_hash
[iter
->hidx
];
1391 if (hlist_empty(hhd
)) {
1407 if (WARN_ON_ONCE(!hnd
))
1410 iter
->probe
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1415 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1417 struct ftrace_iterator
*iter
= m
->private;
1421 if (iter
->func_pos
> *pos
)
1425 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
1426 p
= t_hash_next(m
, &l
);
1433 /* Only set this if we have an item */
1434 iter
->flags
|= FTRACE_ITER_HASH
;
1440 t_hash_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
1442 struct ftrace_func_probe
*rec
;
1445 if (WARN_ON_ONCE(!rec
))
1448 if (rec
->ops
->print
)
1449 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1451 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
1454 seq_printf(m
, ":%p", rec
->data
);
1461 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1463 struct ftrace_iterator
*iter
= m
->private;
1464 struct dyn_ftrace
*rec
= NULL
;
1466 if (iter
->flags
& FTRACE_ITER_HASH
)
1467 return t_hash_next(m
, pos
);
1470 iter
->pos
= iter
->func_pos
= *pos
;
1472 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
1473 return t_hash_start(m
, pos
);
1476 if (iter
->idx
>= iter
->pg
->index
) {
1477 if (iter
->pg
->next
) {
1478 iter
->pg
= iter
->pg
->next
;
1483 rec
= &iter
->pg
->records
[iter
->idx
++];
1484 if ((rec
->flags
& FTRACE_FL_FREE
) ||
1486 (!(iter
->flags
& FTRACE_ITER_FAILURES
) &&
1487 (rec
->flags
& FTRACE_FL_FAILED
)) ||
1489 ((iter
->flags
& FTRACE_ITER_FAILURES
) &&
1490 !(rec
->flags
& FTRACE_FL_FAILED
)) ||
1492 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
1493 !(rec
->flags
& FTRACE_FL_FILTER
)) ||
1495 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
1496 !(rec
->flags
& FTRACE_FL_NOTRACE
))) {
1503 return t_hash_start(m
, pos
);
1510 static void reset_iter_read(struct ftrace_iterator
*iter
)
1514 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
& FTRACE_ITER_HASH
);
1517 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1519 struct ftrace_iterator
*iter
= m
->private;
1523 mutex_lock(&ftrace_lock
);
1525 * If an lseek was done, then reset and start from beginning.
1527 if (*pos
< iter
->pos
)
1528 reset_iter_read(iter
);
1531 * For set_ftrace_filter reading, if we have the filter
1532 * off, we can short cut and just print out that all
1533 * functions are enabled.
1535 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ftrace_filtered
) {
1537 return t_hash_start(m
, pos
);
1538 iter
->flags
|= FTRACE_ITER_PRINTALL
;
1539 /* reset in case of seek/pread */
1540 iter
->flags
&= ~FTRACE_ITER_HASH
;
1544 if (iter
->flags
& FTRACE_ITER_HASH
)
1545 return t_hash_start(m
, pos
);
1548 * Unfortunately, we need to restart at ftrace_pages_start
1549 * every time we let go of the ftrace_mutex. This is because
1550 * those pointers can change without the lock.
1552 iter
->pg
= ftrace_pages_start
;
1554 for (l
= 0; l
<= *pos
; ) {
1555 p
= t_next(m
, p
, &l
);
1561 if (iter
->flags
& FTRACE_ITER_FILTER
)
1562 return t_hash_start(m
, pos
);
1570 static void t_stop(struct seq_file
*m
, void *p
)
1572 mutex_unlock(&ftrace_lock
);
1575 static int t_show(struct seq_file
*m
, void *v
)
1577 struct ftrace_iterator
*iter
= m
->private;
1578 struct dyn_ftrace
*rec
;
1580 if (iter
->flags
& FTRACE_ITER_HASH
)
1581 return t_hash_show(m
, iter
);
1583 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
1584 seq_printf(m
, "#### all functions enabled ####\n");
1593 seq_printf(m
, "%ps\n", (void *)rec
->ip
);
1598 static const struct seq_operations show_ftrace_seq_ops
= {
1606 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
1608 struct ftrace_iterator
*iter
;
1611 if (unlikely(ftrace_disabled
))
1614 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1618 iter
->pg
= ftrace_pages_start
;
1620 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1622 struct seq_file
*m
= file
->private_data
;
1633 ftrace_failures_open(struct inode
*inode
, struct file
*file
)
1637 struct ftrace_iterator
*iter
;
1639 ret
= ftrace_avail_open(inode
, file
);
1641 m
= file
->private_data
;
1643 iter
->flags
= FTRACE_ITER_FAILURES
;
1650 static void ftrace_filter_reset(int enable
)
1652 struct ftrace_page
*pg
;
1653 struct dyn_ftrace
*rec
;
1654 unsigned long type
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1656 mutex_lock(&ftrace_lock
);
1658 ftrace_filtered
= 0;
1659 do_for_each_ftrace_rec(pg
, rec
) {
1660 if (rec
->flags
& FTRACE_FL_FAILED
)
1662 rec
->flags
&= ~type
;
1663 } while_for_each_ftrace_rec();
1664 mutex_unlock(&ftrace_lock
);
1668 ftrace_regex_open(struct inode
*inode
, struct file
*file
, int enable
)
1670 struct ftrace_iterator
*iter
;
1673 if (unlikely(ftrace_disabled
))
1676 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1680 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
1685 mutex_lock(&ftrace_regex_lock
);
1686 if ((file
->f_mode
& FMODE_WRITE
) &&
1687 (file
->f_flags
& O_TRUNC
))
1688 ftrace_filter_reset(enable
);
1690 if (file
->f_mode
& FMODE_READ
) {
1691 iter
->pg
= ftrace_pages_start
;
1692 iter
->flags
= enable
? FTRACE_ITER_FILTER
:
1693 FTRACE_ITER_NOTRACE
;
1695 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1697 struct seq_file
*m
= file
->private_data
;
1700 trace_parser_put(&iter
->parser
);
1704 file
->private_data
= iter
;
1705 mutex_unlock(&ftrace_regex_lock
);
1711 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
1713 return ftrace_regex_open(inode
, file
, 1);
1717 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
1719 return ftrace_regex_open(inode
, file
, 0);
1723 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
1727 if (file
->f_mode
& FMODE_READ
)
1728 ret
= seq_lseek(file
, offset
, origin
);
1730 file
->f_pos
= ret
= 1;
1735 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
1742 if (strcmp(str
, regex
) == 0)
1745 case MATCH_FRONT_ONLY
:
1746 if (strncmp(str
, regex
, len
) == 0)
1749 case MATCH_MIDDLE_ONLY
:
1750 if (strstr(str
, regex
))
1753 case MATCH_END_ONLY
:
1755 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
1764 ftrace_match_record(struct dyn_ftrace
*rec
, char *regex
, int len
, int type
)
1766 char str
[KSYM_SYMBOL_LEN
];
1768 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1769 return ftrace_match(str
, regex
, len
, type
);
1772 static int ftrace_match_records(char *buff
, int len
, int enable
)
1774 unsigned int search_len
;
1775 struct ftrace_page
*pg
;
1776 struct dyn_ftrace
*rec
;
1783 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1784 type
= filter_parse_regex(buff
, len
, &search
, ¬);
1786 search_len
= strlen(search
);
1788 mutex_lock(&ftrace_lock
);
1789 do_for_each_ftrace_rec(pg
, rec
) {
1791 if (rec
->flags
& FTRACE_FL_FAILED
)
1794 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
1796 rec
->flags
&= ~flag
;
1802 * Only enable filtering if we have a function that
1805 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1806 ftrace_filtered
= 1;
1807 } while_for_each_ftrace_rec();
1808 mutex_unlock(&ftrace_lock
);
1814 ftrace_match_module_record(struct dyn_ftrace
*rec
, char *mod
,
1815 char *regex
, int len
, int type
)
1817 char str
[KSYM_SYMBOL_LEN
];
1820 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
1822 if (!modname
|| strcmp(modname
, mod
))
1825 /* blank search means to match all funcs in the mod */
1827 return ftrace_match(str
, regex
, len
, type
);
1832 static int ftrace_match_module_records(char *buff
, char *mod
, int enable
)
1834 unsigned search_len
= 0;
1835 struct ftrace_page
*pg
;
1836 struct dyn_ftrace
*rec
;
1837 int type
= MATCH_FULL
;
1838 char *search
= buff
;
1843 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1845 /* blank or '*' mean the same */
1846 if (strcmp(buff
, "*") == 0)
1849 /* handle the case of 'dont filter this module' */
1850 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
1856 type
= filter_parse_regex(buff
, strlen(buff
), &search
, ¬);
1857 search_len
= strlen(search
);
1860 mutex_lock(&ftrace_lock
);
1861 do_for_each_ftrace_rec(pg
, rec
) {
1863 if (rec
->flags
& FTRACE_FL_FAILED
)
1866 if (ftrace_match_module_record(rec
, mod
,
1867 search
, search_len
, type
)) {
1869 rec
->flags
&= ~flag
;
1874 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1875 ftrace_filtered
= 1;
1877 } while_for_each_ftrace_rec();
1878 mutex_unlock(&ftrace_lock
);
1884 * We register the module command as a template to show others how
1885 * to register the a command as well.
1889 ftrace_mod_callback(char *func
, char *cmd
, char *param
, int enable
)
1894 * cmd == 'mod' because we only registered this func
1895 * for the 'mod' ftrace_func_command.
1896 * But if you register one func with multiple commands,
1897 * you can tell which command was used by the cmd
1901 /* we must have a module name */
1905 mod
= strsep(¶m
, ":");
1909 if (ftrace_match_module_records(func
, mod
, enable
))
1914 static struct ftrace_func_command ftrace_mod_cmd
= {
1916 .func
= ftrace_mod_callback
,
1919 static int __init
ftrace_mod_cmd_init(void)
1921 return register_ftrace_command(&ftrace_mod_cmd
);
1923 device_initcall(ftrace_mod_cmd_init
);
1926 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
1928 struct ftrace_func_probe
*entry
;
1929 struct hlist_head
*hhd
;
1930 struct hlist_node
*n
;
1933 key
= hash_long(ip
, FTRACE_HASH_BITS
);
1935 hhd
= &ftrace_func_hash
[key
];
1937 if (hlist_empty(hhd
))
1941 * Disable preemption for these calls to prevent a RCU grace
1942 * period. This syncs the hash iteration and freeing of items
1943 * on the hash. rcu_read_lock is too dangerous here.
1945 preempt_disable_notrace();
1946 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
1947 if (entry
->ip
== ip
)
1948 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
1950 preempt_enable_notrace();
1953 static struct ftrace_ops trace_probe_ops __read_mostly
=
1955 .func
= function_trace_probe_call
,
1958 static int ftrace_probe_registered
;
1960 static void __enable_ftrace_function_probe(void)
1964 if (ftrace_probe_registered
)
1967 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1968 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1972 /* Nothing registered? */
1973 if (i
== FTRACE_FUNC_HASHSIZE
)
1976 __register_ftrace_function(&trace_probe_ops
);
1978 ftrace_probe_registered
= 1;
1981 static void __disable_ftrace_function_probe(void)
1985 if (!ftrace_probe_registered
)
1988 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1989 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1994 /* no more funcs left */
1995 __unregister_ftrace_function(&trace_probe_ops
);
1997 ftrace_probe_registered
= 0;
2001 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
2003 struct ftrace_func_probe
*entry
=
2004 container_of(rhp
, struct ftrace_func_probe
, rcu
);
2006 if (entry
->ops
->free
)
2007 entry
->ops
->free(&entry
->data
);
2013 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2016 struct ftrace_func_probe
*entry
;
2017 struct ftrace_page
*pg
;
2018 struct dyn_ftrace
*rec
;
2024 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2025 len
= strlen(search
);
2027 /* we do not support '!' for function probes */
2031 mutex_lock(&ftrace_lock
);
2032 do_for_each_ftrace_rec(pg
, rec
) {
2034 if (rec
->flags
& FTRACE_FL_FAILED
)
2037 if (!ftrace_match_record(rec
, search
, len
, type
))
2040 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
2042 /* If we did not process any, then return error */
2053 * The caller might want to do something special
2054 * for each function we find. We call the callback
2055 * to give the caller an opportunity to do so.
2057 if (ops
->callback
) {
2058 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
2059 /* caller does not like this func */
2066 entry
->ip
= rec
->ip
;
2068 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2069 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2071 } while_for_each_ftrace_rec();
2072 __enable_ftrace_function_probe();
2075 mutex_unlock(&ftrace_lock
);
2081 PROBE_TEST_FUNC
= 1,
2086 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2087 void *data
, int flags
)
2089 struct ftrace_func_probe
*entry
;
2090 struct hlist_node
*n
, *tmp
;
2091 char str
[KSYM_SYMBOL_LEN
];
2092 int type
= MATCH_FULL
;
2096 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
2101 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2102 len
= strlen(search
);
2104 /* we do not support '!' for function probes */
2109 mutex_lock(&ftrace_lock
);
2110 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2111 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2113 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2115 /* break up if statements for readability */
2116 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2119 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2122 /* do this last, since it is the most expensive */
2124 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2126 if (!ftrace_match(str
, glob
, len
, type
))
2130 hlist_del(&entry
->node
);
2131 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2134 __disable_ftrace_function_probe();
2135 mutex_unlock(&ftrace_lock
);
2139 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2142 __unregister_ftrace_function_probe(glob
, ops
, data
,
2143 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2147 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2149 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2152 void unregister_ftrace_function_probe_all(char *glob
)
2154 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2157 static LIST_HEAD(ftrace_commands
);
2158 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2160 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2162 struct ftrace_func_command
*p
;
2165 mutex_lock(&ftrace_cmd_mutex
);
2166 list_for_each_entry(p
, &ftrace_commands
, list
) {
2167 if (strcmp(cmd
->name
, p
->name
) == 0) {
2172 list_add(&cmd
->list
, &ftrace_commands
);
2174 mutex_unlock(&ftrace_cmd_mutex
);
2179 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2181 struct ftrace_func_command
*p
, *n
;
2184 mutex_lock(&ftrace_cmd_mutex
);
2185 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2186 if (strcmp(cmd
->name
, p
->name
) == 0) {
2188 list_del_init(&p
->list
);
2193 mutex_unlock(&ftrace_cmd_mutex
);
2198 static int ftrace_process_regex(char *buff
, int len
, int enable
)
2200 char *func
, *command
, *next
= buff
;
2201 struct ftrace_func_command
*p
;
2204 func
= strsep(&next
, ":");
2207 if (ftrace_match_records(func
, len
, enable
))
2214 command
= strsep(&next
, ":");
2216 mutex_lock(&ftrace_cmd_mutex
);
2217 list_for_each_entry(p
, &ftrace_commands
, list
) {
2218 if (strcmp(p
->name
, command
) == 0) {
2219 ret
= p
->func(func
, command
, next
, enable
);
2224 mutex_unlock(&ftrace_cmd_mutex
);
2230 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2231 size_t cnt
, loff_t
*ppos
, int enable
)
2233 struct ftrace_iterator
*iter
;
2234 struct trace_parser
*parser
;
2240 mutex_lock(&ftrace_regex_lock
);
2242 if (file
->f_mode
& FMODE_READ
) {
2243 struct seq_file
*m
= file
->private_data
;
2246 iter
= file
->private_data
;
2248 parser
= &iter
->parser
;
2249 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
2251 if (read
>= 0 && trace_parser_loaded(parser
) &&
2252 !trace_parser_cont(parser
)) {
2253 ret
= ftrace_process_regex(parser
->buffer
,
2254 parser
->idx
, enable
);
2255 trace_parser_clear(parser
);
2262 mutex_unlock(&ftrace_regex_lock
);
2268 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2269 size_t cnt
, loff_t
*ppos
)
2271 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2275 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2276 size_t cnt
, loff_t
*ppos
)
2278 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2282 ftrace_set_regex(unsigned char *buf
, int len
, int reset
, int enable
)
2284 if (unlikely(ftrace_disabled
))
2287 mutex_lock(&ftrace_regex_lock
);
2289 ftrace_filter_reset(enable
);
2291 ftrace_match_records(buf
, len
, enable
);
2292 mutex_unlock(&ftrace_regex_lock
);
2296 * ftrace_set_filter - set a function to filter on in ftrace
2297 * @buf - the string that holds the function filter text.
2298 * @len - the length of the string.
2299 * @reset - non zero to reset all filters before applying this filter.
2301 * Filters denote which functions should be enabled when tracing is enabled.
2302 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2304 void ftrace_set_filter(unsigned char *buf
, int len
, int reset
)
2306 ftrace_set_regex(buf
, len
, reset
, 1);
2310 * ftrace_set_notrace - set a function to not trace in ftrace
2311 * @buf - the string that holds the function notrace text.
2312 * @len - the length of the string.
2313 * @reset - non zero to reset all filters before applying this filter.
2315 * Notrace Filters denote which functions should not be enabled when tracing
2316 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2319 void ftrace_set_notrace(unsigned char *buf
, int len
, int reset
)
2321 ftrace_set_regex(buf
, len
, reset
, 0);
2325 * command line interface to allow users to set filters on boot up.
2327 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2328 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2329 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2331 static int __init
set_ftrace_notrace(char *str
)
2333 strncpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
2336 __setup("ftrace_notrace=", set_ftrace_notrace
);
2338 static int __init
set_ftrace_filter(char *str
)
2340 strncpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
2343 __setup("ftrace_filter=", set_ftrace_filter
);
2345 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2346 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2347 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
2349 static int __init
set_graph_function(char *str
)
2351 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
2354 __setup("ftrace_graph_filter=", set_graph_function
);
2356 static void __init
set_ftrace_early_graph(char *buf
)
2362 func
= strsep(&buf
, ",");
2363 /* we allow only one expression at a time */
2364 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2367 printk(KERN_DEBUG
"ftrace: function %s not "
2368 "traceable\n", func
);
2371 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2373 static void __init
set_ftrace_early_filter(char *buf
, int enable
)
2378 func
= strsep(&buf
, ",");
2379 ftrace_set_regex(func
, strlen(func
), 0, enable
);
2383 static void __init
set_ftrace_early_filters(void)
2385 if (ftrace_filter_buf
[0])
2386 set_ftrace_early_filter(ftrace_filter_buf
, 1);
2387 if (ftrace_notrace_buf
[0])
2388 set_ftrace_early_filter(ftrace_notrace_buf
, 0);
2389 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2390 if (ftrace_graph_buf
[0])
2391 set_ftrace_early_graph(ftrace_graph_buf
);
2392 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2396 ftrace_regex_release(struct inode
*inode
, struct file
*file
, int enable
)
2398 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2399 struct ftrace_iterator
*iter
;
2400 struct trace_parser
*parser
;
2402 mutex_lock(&ftrace_regex_lock
);
2403 if (file
->f_mode
& FMODE_READ
) {
2406 seq_release(inode
, file
);
2408 iter
= file
->private_data
;
2410 parser
= &iter
->parser
;
2411 if (trace_parser_loaded(parser
)) {
2412 parser
->buffer
[parser
->idx
] = 0;
2413 ftrace_match_records(parser
->buffer
, parser
->idx
, enable
);
2416 mutex_lock(&ftrace_lock
);
2417 if (ftrace_start_up
&& ftrace_enabled
)
2418 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
2419 mutex_unlock(&ftrace_lock
);
2421 trace_parser_put(parser
);
2424 mutex_unlock(&ftrace_regex_lock
);
2429 ftrace_filter_release(struct inode
*inode
, struct file
*file
)
2431 return ftrace_regex_release(inode
, file
, 1);
2435 ftrace_notrace_release(struct inode
*inode
, struct file
*file
)
2437 return ftrace_regex_release(inode
, file
, 0);
2440 static const struct file_operations ftrace_avail_fops
= {
2441 .open
= ftrace_avail_open
,
2443 .llseek
= seq_lseek
,
2444 .release
= seq_release_private
,
2447 static const struct file_operations ftrace_failures_fops
= {
2448 .open
= ftrace_failures_open
,
2450 .llseek
= seq_lseek
,
2451 .release
= seq_release_private
,
2454 static const struct file_operations ftrace_filter_fops
= {
2455 .open
= ftrace_filter_open
,
2457 .write
= ftrace_filter_write
,
2458 .llseek
= ftrace_regex_lseek
,
2459 .release
= ftrace_filter_release
,
2462 static const struct file_operations ftrace_notrace_fops
= {
2463 .open
= ftrace_notrace_open
,
2465 .write
= ftrace_notrace_write
,
2466 .llseek
= ftrace_regex_lseek
,
2467 .release
= ftrace_notrace_release
,
2470 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2472 static DEFINE_MUTEX(graph_lock
);
2474 int ftrace_graph_count
;
2475 int ftrace_graph_filter_enabled
;
2476 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
2479 __g_next(struct seq_file
*m
, loff_t
*pos
)
2481 if (*pos
>= ftrace_graph_count
)
2483 return &ftrace_graph_funcs
[*pos
];
2487 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2490 return __g_next(m
, pos
);
2493 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
2495 mutex_lock(&graph_lock
);
2497 /* Nothing, tell g_show to print all functions are enabled */
2498 if (!ftrace_graph_filter_enabled
&& !*pos
)
2501 return __g_next(m
, pos
);
2504 static void g_stop(struct seq_file
*m
, void *p
)
2506 mutex_unlock(&graph_lock
);
2509 static int g_show(struct seq_file
*m
, void *v
)
2511 unsigned long *ptr
= v
;
2516 if (ptr
== (unsigned long *)1) {
2517 seq_printf(m
, "#### all functions enabled ####\n");
2521 seq_printf(m
, "%ps\n", (void *)*ptr
);
2526 static const struct seq_operations ftrace_graph_seq_ops
= {
2534 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
2538 if (unlikely(ftrace_disabled
))
2541 mutex_lock(&graph_lock
);
2542 if ((file
->f_mode
& FMODE_WRITE
) &&
2543 (file
->f_flags
& O_TRUNC
)) {
2544 ftrace_graph_filter_enabled
= 0;
2545 ftrace_graph_count
= 0;
2546 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
2548 mutex_unlock(&graph_lock
);
2550 if (file
->f_mode
& FMODE_READ
)
2551 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
2557 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
2559 if (file
->f_mode
& FMODE_READ
)
2560 seq_release(inode
, file
);
2565 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
2567 struct dyn_ftrace
*rec
;
2568 struct ftrace_page
*pg
;
2576 if (ftrace_disabled
)
2580 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
2581 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2584 search_len
= strlen(search
);
2586 mutex_lock(&ftrace_lock
);
2587 do_for_each_ftrace_rec(pg
, rec
) {
2589 if (rec
->flags
& (FTRACE_FL_FAILED
| FTRACE_FL_FREE
))
2592 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
2593 /* if it is in the array */
2595 for (i
= 0; i
< *idx
; i
++) {
2596 if (array
[i
] == rec
->ip
) {
2605 array
[(*idx
)++] = rec
->ip
;
2606 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2611 array
[i
] = array
[--(*idx
)];
2617 } while_for_each_ftrace_rec();
2619 mutex_unlock(&ftrace_lock
);
2624 ftrace_graph_filter_enabled
= 1;
2629 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
2630 size_t cnt
, loff_t
*ppos
)
2632 struct trace_parser parser
;
2638 mutex_lock(&graph_lock
);
2640 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
2645 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
2647 if (read
>= 0 && trace_parser_loaded((&parser
))) {
2648 parser
.buffer
[parser
.idx
] = 0;
2650 /* we allow only one expression at a time */
2651 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2660 trace_parser_put(&parser
);
2662 mutex_unlock(&graph_lock
);
2667 static const struct file_operations ftrace_graph_fops
= {
2668 .open
= ftrace_graph_open
,
2670 .write
= ftrace_graph_write
,
2671 .release
= ftrace_graph_release
,
2672 .llseek
= seq_lseek
,
2674 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2676 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
2679 trace_create_file("available_filter_functions", 0444,
2680 d_tracer
, NULL
, &ftrace_avail_fops
);
2682 trace_create_file("failures", 0444,
2683 d_tracer
, NULL
, &ftrace_failures_fops
);
2685 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
2686 NULL
, &ftrace_filter_fops
);
2688 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
2689 NULL
, &ftrace_notrace_fops
);
2691 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2692 trace_create_file("set_graph_function", 0444, d_tracer
,
2694 &ftrace_graph_fops
);
2695 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2700 static int ftrace_process_locs(struct module
*mod
,
2701 unsigned long *start
,
2706 unsigned long flags
;
2708 mutex_lock(&ftrace_lock
);
2711 addr
= ftrace_call_adjust(*p
++);
2713 * Some architecture linkers will pad between
2714 * the different mcount_loc sections of different
2715 * object files to satisfy alignments.
2716 * Skip any NULL pointers.
2720 ftrace_record_ip(addr
);
2723 /* disable interrupts to prevent kstop machine */
2724 local_irq_save(flags
);
2725 ftrace_update_code(mod
);
2726 local_irq_restore(flags
);
2727 mutex_unlock(&ftrace_lock
);
2732 #ifdef CONFIG_MODULES
2733 void ftrace_release_mod(struct module
*mod
)
2735 struct dyn_ftrace
*rec
;
2736 struct ftrace_page
*pg
;
2738 if (ftrace_disabled
)
2741 mutex_lock(&ftrace_lock
);
2742 do_for_each_ftrace_rec(pg
, rec
) {
2743 if (within_module_core(rec
->ip
, mod
)) {
2745 * rec->ip is changed in ftrace_free_rec()
2746 * It should not between s and e if record was freed.
2748 FTRACE_WARN_ON(rec
->flags
& FTRACE_FL_FREE
);
2749 ftrace_free_rec(rec
);
2751 } while_for_each_ftrace_rec();
2752 mutex_unlock(&ftrace_lock
);
2755 static void ftrace_init_module(struct module
*mod
,
2756 unsigned long *start
, unsigned long *end
)
2758 if (ftrace_disabled
|| start
== end
)
2760 ftrace_process_locs(mod
, start
, end
);
2763 static int ftrace_module_notify(struct notifier_block
*self
,
2764 unsigned long val
, void *data
)
2766 struct module
*mod
= data
;
2769 case MODULE_STATE_COMING
:
2770 ftrace_init_module(mod
, mod
->ftrace_callsites
,
2771 mod
->ftrace_callsites
+
2772 mod
->num_ftrace_callsites
);
2774 case MODULE_STATE_GOING
:
2775 ftrace_release_mod(mod
);
2782 static int ftrace_module_notify(struct notifier_block
*self
,
2783 unsigned long val
, void *data
)
2787 #endif /* CONFIG_MODULES */
2789 struct notifier_block ftrace_module_nb
= {
2790 .notifier_call
= ftrace_module_notify
,
2794 extern unsigned long __start_mcount_loc
[];
2795 extern unsigned long __stop_mcount_loc
[];
2797 void __init
ftrace_init(void)
2799 unsigned long count
, addr
, flags
;
2802 /* Keep the ftrace pointer to the stub */
2803 addr
= (unsigned long)ftrace_stub
;
2805 local_irq_save(flags
);
2806 ftrace_dyn_arch_init(&addr
);
2807 local_irq_restore(flags
);
2809 /* ftrace_dyn_arch_init places the return code in addr */
2813 count
= __stop_mcount_loc
- __start_mcount_loc
;
2815 ret
= ftrace_dyn_table_alloc(count
);
2819 last_ftrace_enabled
= ftrace_enabled
= 1;
2821 ret
= ftrace_process_locs(NULL
,
2825 ret
= register_module_notifier(&ftrace_module_nb
);
2827 pr_warning("Failed to register trace ftrace module notifier\n");
2829 set_ftrace_early_filters();
2833 ftrace_disabled
= 1;
2838 static int __init
ftrace_nodyn_init(void)
2843 device_initcall(ftrace_nodyn_init
);
2845 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
2846 static inline void ftrace_startup_enable(int command
) { }
2847 /* Keep as macros so we do not need to define the commands */
2848 # define ftrace_startup(command) do { } while (0)
2849 # define ftrace_shutdown(command) do { } while (0)
2850 # define ftrace_startup_sysctl() do { } while (0)
2851 # define ftrace_shutdown_sysctl() do { } while (0)
2852 #endif /* CONFIG_DYNAMIC_FTRACE */
2854 static void clear_ftrace_swapper(void)
2856 struct task_struct
*p
;
2860 for_each_online_cpu(cpu
) {
2862 clear_tsk_trace_trace(p
);
2867 static void set_ftrace_swapper(void)
2869 struct task_struct
*p
;
2873 for_each_online_cpu(cpu
) {
2875 set_tsk_trace_trace(p
);
2880 static void clear_ftrace_pid(struct pid
*pid
)
2882 struct task_struct
*p
;
2885 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2886 clear_tsk_trace_trace(p
);
2887 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2893 static void set_ftrace_pid(struct pid
*pid
)
2895 struct task_struct
*p
;
2898 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2899 set_tsk_trace_trace(p
);
2900 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2904 static void clear_ftrace_pid_task(struct pid
*pid
)
2906 if (pid
== ftrace_swapper_pid
)
2907 clear_ftrace_swapper();
2909 clear_ftrace_pid(pid
);
2912 static void set_ftrace_pid_task(struct pid
*pid
)
2914 if (pid
== ftrace_swapper_pid
)
2915 set_ftrace_swapper();
2917 set_ftrace_pid(pid
);
2920 static int ftrace_pid_add(int p
)
2923 struct ftrace_pid
*fpid
;
2926 mutex_lock(&ftrace_lock
);
2929 pid
= ftrace_swapper_pid
;
2931 pid
= find_get_pid(p
);
2938 list_for_each_entry(fpid
, &ftrace_pids
, list
)
2939 if (fpid
->pid
== pid
)
2944 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
2948 list_add(&fpid
->list
, &ftrace_pids
);
2951 set_ftrace_pid_task(pid
);
2953 ftrace_update_pid_func();
2954 ftrace_startup_enable(0);
2956 mutex_unlock(&ftrace_lock
);
2960 if (pid
!= ftrace_swapper_pid
)
2964 mutex_unlock(&ftrace_lock
);
2968 static void ftrace_pid_reset(void)
2970 struct ftrace_pid
*fpid
, *safe
;
2972 mutex_lock(&ftrace_lock
);
2973 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
2974 struct pid
*pid
= fpid
->pid
;
2976 clear_ftrace_pid_task(pid
);
2978 list_del(&fpid
->list
);
2982 ftrace_update_pid_func();
2983 ftrace_startup_enable(0);
2985 mutex_unlock(&ftrace_lock
);
2988 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
2990 mutex_lock(&ftrace_lock
);
2992 if (list_empty(&ftrace_pids
) && (!*pos
))
2995 return seq_list_start(&ftrace_pids
, *pos
);
2998 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3003 return seq_list_next(v
, &ftrace_pids
, pos
);
3006 static void fpid_stop(struct seq_file
*m
, void *p
)
3008 mutex_unlock(&ftrace_lock
);
3011 static int fpid_show(struct seq_file
*m
, void *v
)
3013 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
3015 if (v
== (void *)1) {
3016 seq_printf(m
, "no pid\n");
3020 if (fpid
->pid
== ftrace_swapper_pid
)
3021 seq_printf(m
, "swapper tasks\n");
3023 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
3028 static const struct seq_operations ftrace_pid_sops
= {
3029 .start
= fpid_start
,
3036 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
3040 if ((file
->f_mode
& FMODE_WRITE
) &&
3041 (file
->f_flags
& O_TRUNC
))
3044 if (file
->f_mode
& FMODE_READ
)
3045 ret
= seq_open(file
, &ftrace_pid_sops
);
3051 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
3052 size_t cnt
, loff_t
*ppos
)
3058 if (cnt
>= sizeof(buf
))
3061 if (copy_from_user(&buf
, ubuf
, cnt
))
3067 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3068 * to clean the filter quietly.
3070 tmp
= strstrip(buf
);
3071 if (strlen(tmp
) == 0)
3074 ret
= strict_strtol(tmp
, 10, &val
);
3078 ret
= ftrace_pid_add(val
);
3080 return ret
? ret
: cnt
;
3084 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
3086 if (file
->f_mode
& FMODE_READ
)
3087 seq_release(inode
, file
);
3092 static const struct file_operations ftrace_pid_fops
= {
3093 .open
= ftrace_pid_open
,
3094 .write
= ftrace_pid_write
,
3096 .llseek
= seq_lseek
,
3097 .release
= ftrace_pid_release
,
3100 static __init
int ftrace_init_debugfs(void)
3102 struct dentry
*d_tracer
;
3104 d_tracer
= tracing_init_dentry();
3108 ftrace_init_dyn_debugfs(d_tracer
);
3110 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
3111 NULL
, &ftrace_pid_fops
);
3113 ftrace_profile_debugfs(d_tracer
);
3117 fs_initcall(ftrace_init_debugfs
);
3120 * ftrace_kill - kill ftrace
3122 * This function should be used by panic code. It stops ftrace
3123 * but in a not so nice way. If you need to simply kill ftrace
3124 * from a non-atomic section, use ftrace_kill.
3126 void ftrace_kill(void)
3128 ftrace_disabled
= 1;
3130 clear_ftrace_function();
3134 * register_ftrace_function - register a function for profiling
3135 * @ops - ops structure that holds the function for profiling.
3137 * Register a function to be called by all functions in the
3140 * Note: @ops->func and all the functions it calls must be labeled
3141 * with "notrace", otherwise it will go into a
3144 int register_ftrace_function(struct ftrace_ops
*ops
)
3148 if (unlikely(ftrace_disabled
))
3151 mutex_lock(&ftrace_lock
);
3153 ret
= __register_ftrace_function(ops
);
3156 mutex_unlock(&ftrace_lock
);
3161 * unregister_ftrace_function - unregister a function for profiling.
3162 * @ops - ops structure that holds the function to unregister
3164 * Unregister a function that was added to be called by ftrace profiling.
3166 int unregister_ftrace_function(struct ftrace_ops
*ops
)
3170 mutex_lock(&ftrace_lock
);
3171 ret
= __unregister_ftrace_function(ops
);
3173 mutex_unlock(&ftrace_lock
);
3179 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
3180 void __user
*buffer
, size_t *lenp
,
3185 if (unlikely(ftrace_disabled
))
3188 mutex_lock(&ftrace_lock
);
3190 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3192 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
3195 last_ftrace_enabled
= !!ftrace_enabled
;
3197 if (ftrace_enabled
) {
3199 ftrace_startup_sysctl();
3201 /* we are starting ftrace again */
3202 if (ftrace_list
!= &ftrace_list_end
) {
3203 if (ftrace_list
->next
== &ftrace_list_end
)
3204 ftrace_trace_function
= ftrace_list
->func
;
3206 ftrace_trace_function
= ftrace_list_func
;
3210 /* stopping ftrace calls (just send to ftrace_stub) */
3211 ftrace_trace_function
= ftrace_stub
;
3213 ftrace_shutdown_sysctl();
3217 mutex_unlock(&ftrace_lock
);
3221 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3223 static int ftrace_graph_active
;
3224 static struct notifier_block ftrace_suspend_notifier
;
3226 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3231 /* The callbacks that hook a function */
3232 trace_func_graph_ret_t ftrace_graph_return
=
3233 (trace_func_graph_ret_t
)ftrace_stub
;
3234 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3236 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3237 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3241 unsigned long flags
;
3242 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3243 struct task_struct
*g
, *t
;
3245 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3246 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3247 * sizeof(struct ftrace_ret_stack
),
3249 if (!ret_stack_list
[i
]) {
3257 read_lock_irqsave(&tasklist_lock
, flags
);
3258 do_each_thread(g
, t
) {
3264 if (t
->ret_stack
== NULL
) {
3265 atomic_set(&t
->tracing_graph_pause
, 0);
3266 atomic_set(&t
->trace_overrun
, 0);
3267 t
->curr_ret_stack
= -1;
3268 /* Make sure the tasks see the -1 first: */
3270 t
->ret_stack
= ret_stack_list
[start
++];
3272 } while_each_thread(g
, t
);
3275 read_unlock_irqrestore(&tasklist_lock
, flags
);
3277 for (i
= start
; i
< end
; i
++)
3278 kfree(ret_stack_list
[i
]);
3283 ftrace_graph_probe_sched_switch(void *ignore
,
3284 struct task_struct
*prev
, struct task_struct
*next
)
3286 unsigned long long timestamp
;
3290 * Does the user want to count the time a function was asleep.
3291 * If so, do not update the time stamps.
3293 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
3296 timestamp
= trace_clock_local();
3298 prev
->ftrace_timestamp
= timestamp
;
3300 /* only process tasks that we timestamped */
3301 if (!next
->ftrace_timestamp
)
3305 * Update all the counters in next to make up for the
3306 * time next was sleeping.
3308 timestamp
-= next
->ftrace_timestamp
;
3310 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
3311 next
->ret_stack
[index
].calltime
+= timestamp
;
3314 /* Allocate a return stack for each task */
3315 static int start_graph_tracing(void)
3317 struct ftrace_ret_stack
**ret_stack_list
;
3320 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
3321 sizeof(struct ftrace_ret_stack
*),
3324 if (!ret_stack_list
)
3327 /* The cpu_boot init_task->ret_stack will never be freed */
3328 for_each_online_cpu(cpu
) {
3329 if (!idle_task(cpu
)->ret_stack
)
3330 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
3334 ret
= alloc_retstack_tasklist(ret_stack_list
);
3335 } while (ret
== -EAGAIN
);
3338 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
3340 pr_info("ftrace_graph: Couldn't activate tracepoint"
3341 " probe to kernel_sched_switch\n");
3344 kfree(ret_stack_list
);
3349 * Hibernation protection.
3350 * The state of the current task is too much unstable during
3351 * suspend/restore to disk. We want to protect against that.
3354 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
3358 case PM_HIBERNATION_PREPARE
:
3359 pause_graph_tracing();
3362 case PM_POST_HIBERNATION
:
3363 unpause_graph_tracing();
3369 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
3370 trace_func_graph_ent_t entryfunc
)
3374 mutex_lock(&ftrace_lock
);
3376 /* we currently allow only one tracer registered at a time */
3377 if (ftrace_graph_active
) {
3382 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
3383 register_pm_notifier(&ftrace_suspend_notifier
);
3385 ftrace_graph_active
++;
3386 ret
= start_graph_tracing();
3388 ftrace_graph_active
--;
3392 ftrace_graph_return
= retfunc
;
3393 ftrace_graph_entry
= entryfunc
;
3395 ftrace_startup(FTRACE_START_FUNC_RET
);
3398 mutex_unlock(&ftrace_lock
);
3402 void unregister_ftrace_graph(void)
3404 mutex_lock(&ftrace_lock
);
3406 if (unlikely(!ftrace_graph_active
))
3409 ftrace_graph_active
--;
3410 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
3411 ftrace_graph_entry
= ftrace_graph_entry_stub
;
3412 ftrace_shutdown(FTRACE_STOP_FUNC_RET
);
3413 unregister_pm_notifier(&ftrace_suspend_notifier
);
3414 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
3417 mutex_unlock(&ftrace_lock
);
3420 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
3423 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
3425 atomic_set(&t
->tracing_graph_pause
, 0);
3426 atomic_set(&t
->trace_overrun
, 0);
3427 t
->ftrace_timestamp
= 0;
3428 /* make curr_ret_stack visible before we add the ret_stack */
3430 t
->ret_stack
= ret_stack
;
3434 * Allocate a return stack for the idle task. May be the first
3435 * time through, or it may be done by CPU hotplug online.
3437 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
3439 t
->curr_ret_stack
= -1;
3441 * The idle task has no parent, it either has its own
3442 * stack or no stack at all.
3445 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
3447 if (ftrace_graph_active
) {
3448 struct ftrace_ret_stack
*ret_stack
;
3450 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
3452 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
3453 * sizeof(struct ftrace_ret_stack
),
3457 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
3459 graph_init_task(t
, ret_stack
);
3463 /* Allocate a return stack for newly created task */
3464 void ftrace_graph_init_task(struct task_struct
*t
)
3466 /* Make sure we do not use the parent ret_stack */
3467 t
->ret_stack
= NULL
;
3468 t
->curr_ret_stack
= -1;
3470 if (ftrace_graph_active
) {
3471 struct ftrace_ret_stack
*ret_stack
;
3473 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
3474 * sizeof(struct ftrace_ret_stack
),
3478 graph_init_task(t
, ret_stack
);
3482 void ftrace_graph_exit_task(struct task_struct
*t
)
3484 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
3486 t
->ret_stack
= NULL
;
3487 /* NULL must become visible to IRQs before we free it: */
3493 void ftrace_graph_stop(void)