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/kprobes.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
32 #include <trace/sched.h>
34 #include <asm/ftrace.h>
36 #include "trace_output.h"
37 #include "trace_stat.h"
39 #define FTRACE_WARN_ON(cond) \
45 #define FTRACE_WARN_ON_ONCE(cond) \
47 if (WARN_ON_ONCE(cond)) \
51 /* hash bits for specific function selection */
52 #define FTRACE_HASH_BITS 7
53 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
55 /* ftrace_enabled is a method to turn ftrace on or off */
56 int ftrace_enabled __read_mostly
;
57 static int last_ftrace_enabled
;
59 /* Quick disabling of function tracer. */
60 int function_trace_stop
;
63 * ftrace_disabled is set when an anomaly is discovered.
64 * ftrace_disabled is much stronger than ftrace_enabled.
66 static int ftrace_disabled __read_mostly
;
68 static DEFINE_MUTEX(ftrace_lock
);
70 static struct ftrace_ops ftrace_list_end __read_mostly
=
75 static struct ftrace_ops
*ftrace_list __read_mostly
= &ftrace_list_end
;
76 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
77 ftrace_func_t __ftrace_trace_function __read_mostly
= ftrace_stub
;
78 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
80 static void ftrace_list_func(unsigned long ip
, unsigned long parent_ip
)
82 struct ftrace_ops
*op
= ftrace_list
;
84 /* in case someone actually ports this to alpha! */
85 read_barrier_depends();
87 while (op
!= &ftrace_list_end
) {
89 read_barrier_depends();
90 op
->func(ip
, parent_ip
);
95 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
)
97 if (!test_tsk_trace_trace(current
))
100 ftrace_pid_function(ip
, parent_ip
);
103 static void set_ftrace_pid_function(ftrace_func_t func
)
105 /* do not set ftrace_pid_function to itself! */
106 if (func
!= ftrace_pid_func
)
107 ftrace_pid_function
= func
;
111 * clear_ftrace_function - reset the ftrace function
113 * This NULLs the ftrace function and in essence stops
114 * tracing. There may be lag
116 void clear_ftrace_function(void)
118 ftrace_trace_function
= ftrace_stub
;
119 __ftrace_trace_function
= ftrace_stub
;
120 ftrace_pid_function
= ftrace_stub
;
123 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
125 * For those archs that do not test ftrace_trace_stop in their
126 * mcount call site, we need to do it from C.
128 static void ftrace_test_stop_func(unsigned long ip
, unsigned long parent_ip
)
130 if (function_trace_stop
)
133 __ftrace_trace_function(ip
, parent_ip
);
137 static int __register_ftrace_function(struct ftrace_ops
*ops
)
139 ops
->next
= ftrace_list
;
141 * We are entering ops into the ftrace_list but another
142 * CPU might be walking that list. We need to make sure
143 * the ops->next pointer is valid before another CPU sees
144 * the ops pointer included into the ftrace_list.
149 if (ftrace_enabled
) {
152 if (ops
->next
== &ftrace_list_end
)
155 func
= ftrace_list_func
;
157 if (ftrace_pid_trace
) {
158 set_ftrace_pid_function(func
);
159 func
= ftrace_pid_func
;
163 * For one func, simply call it directly.
164 * For more than one func, call the chain.
166 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
167 ftrace_trace_function
= func
;
169 __ftrace_trace_function
= func
;
170 ftrace_trace_function
= ftrace_test_stop_func
;
177 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
179 struct ftrace_ops
**p
;
182 * If we are removing the last function, then simply point
183 * to the ftrace_stub.
185 if (ftrace_list
== ops
&& ops
->next
== &ftrace_list_end
) {
186 ftrace_trace_function
= ftrace_stub
;
187 ftrace_list
= &ftrace_list_end
;
191 for (p
= &ftrace_list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
200 if (ftrace_enabled
) {
201 /* If we only have one func left, then call that directly */
202 if (ftrace_list
->next
== &ftrace_list_end
) {
203 ftrace_func_t func
= ftrace_list
->func
;
205 if (ftrace_pid_trace
) {
206 set_ftrace_pid_function(func
);
207 func
= ftrace_pid_func
;
209 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
210 ftrace_trace_function
= func
;
212 __ftrace_trace_function
= func
;
220 static void ftrace_update_pid_func(void)
224 if (ftrace_trace_function
== ftrace_stub
)
227 func
= ftrace_trace_function
;
229 if (ftrace_pid_trace
) {
230 set_ftrace_pid_function(func
);
231 func
= ftrace_pid_func
;
233 if (func
== ftrace_pid_func
)
234 func
= ftrace_pid_function
;
237 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
238 ftrace_trace_function
= func
;
240 __ftrace_trace_function
= func
;
244 #ifdef CONFIG_FUNCTION_PROFILER
245 struct ftrace_profile
{
246 struct hlist_node node
;
248 unsigned long counter
;
249 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
250 unsigned long long time
;
254 struct ftrace_profile_page
{
255 struct ftrace_profile_page
*next
;
257 struct ftrace_profile records
[];
260 struct ftrace_profile_stat
{
262 struct hlist_head
*hash
;
263 struct ftrace_profile_page
*pages
;
264 struct ftrace_profile_page
*start
;
265 struct tracer_stat stat
;
268 #define PROFILE_RECORDS_SIZE \
269 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
271 #define PROFILES_PER_PAGE \
272 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
274 static int ftrace_profile_bits __read_mostly
;
275 static int ftrace_profile_enabled __read_mostly
;
277 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
278 static DEFINE_MUTEX(ftrace_profile_lock
);
280 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
282 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
285 function_stat_next(void *v
, int idx
)
287 struct ftrace_profile
*rec
= v
;
288 struct ftrace_profile_page
*pg
;
290 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
294 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
298 rec
= &pg
->records
[0];
306 static void *function_stat_start(struct tracer_stat
*trace
)
308 struct ftrace_profile_stat
*stat
=
309 container_of(trace
, struct ftrace_profile_stat
, stat
);
311 if (!stat
|| !stat
->start
)
314 return function_stat_next(&stat
->start
->records
[0], 0);
317 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
318 /* function graph compares on total time */
319 static int function_stat_cmp(void *p1
, void *p2
)
321 struct ftrace_profile
*a
= p1
;
322 struct ftrace_profile
*b
= p2
;
324 if (a
->time
< b
->time
)
326 if (a
->time
> b
->time
)
332 /* not function graph compares against hits */
333 static int function_stat_cmp(void *p1
, void *p2
)
335 struct ftrace_profile
*a
= p1
;
336 struct ftrace_profile
*b
= p2
;
338 if (a
->counter
< b
->counter
)
340 if (a
->counter
> b
->counter
)
347 static int function_stat_headers(struct seq_file
*m
)
349 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
350 seq_printf(m
, " Function Hit Time\n"
351 " -------- --- ----\n");
353 seq_printf(m
, " Function Hit\n"
359 static int function_stat_show(struct seq_file
*m
, void *v
)
361 struct ftrace_profile
*rec
= v
;
362 char str
[KSYM_SYMBOL_LEN
];
363 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
364 static struct trace_seq s
;
365 static DEFINE_MUTEX(mutex
);
369 trace_print_graph_duration(rec
->time
, &s
);
372 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
373 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
375 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
377 trace_print_seq(m
, &s
);
378 mutex_unlock(&mutex
);
385 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
387 struct ftrace_profile_page
*pg
;
389 pg
= stat
->pages
= stat
->start
;
392 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
397 memset(stat
->hash
, 0,
398 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
401 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
403 struct ftrace_profile_page
*pg
;
408 /* If we already allocated, do nothing */
412 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
416 #ifdef CONFIG_DYNAMIC_FTRACE
417 functions
= ftrace_update_tot_cnt
;
420 * We do not know the number of functions that exist because
421 * dynamic tracing is what counts them. With past experience
422 * we have around 20K functions. That should be more than enough.
423 * It is highly unlikely we will execute every function in
429 pg
= stat
->start
= stat
->pages
;
431 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
433 for (i
= 0; i
< pages
; i
++) {
434 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
445 unsigned long tmp
= (unsigned long)pg
;
451 free_page((unsigned long)stat
->pages
);
458 static int ftrace_profile_init_cpu(int cpu
)
460 struct ftrace_profile_stat
*stat
;
463 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
466 /* If the profile is already created, simply reset it */
467 ftrace_profile_reset(stat
);
472 * We are profiling all functions, but usually only a few thousand
473 * functions are hit. We'll make a hash of 1024 items.
475 size
= FTRACE_PROFILE_HASH_SIZE
;
477 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
482 if (!ftrace_profile_bits
) {
485 for (; size
; size
>>= 1)
486 ftrace_profile_bits
++;
489 /* Preallocate the function profiling pages */
490 if (ftrace_profile_pages_init(stat
) < 0) {
499 static int ftrace_profile_init(void)
504 for_each_online_cpu(cpu
) {
505 ret
= ftrace_profile_init_cpu(cpu
);
513 /* interrupts must be disabled */
514 static struct ftrace_profile
*
515 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
517 struct ftrace_profile
*rec
;
518 struct hlist_head
*hhd
;
519 struct hlist_node
*n
;
522 key
= hash_long(ip
, ftrace_profile_bits
);
523 hhd
= &stat
->hash
[key
];
525 if (hlist_empty(hhd
))
528 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
536 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
537 struct ftrace_profile
*rec
)
541 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
542 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
546 * The memory is already allocated, this simply finds a new record to use.
548 static struct ftrace_profile
*
549 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
551 struct ftrace_profile
*rec
= NULL
;
553 /* prevent recursion (from NMIs) */
554 if (atomic_inc_return(&stat
->disabled
) != 1)
558 * Try to find the function again since an NMI
559 * could have added it
561 rec
= ftrace_find_profiled_func(stat
, ip
);
565 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
566 if (!stat
->pages
->next
)
568 stat
->pages
= stat
->pages
->next
;
571 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
573 ftrace_add_profile(stat
, rec
);
576 atomic_dec(&stat
->disabled
);
582 function_profile_call(unsigned long ip
, unsigned long parent_ip
)
584 struct ftrace_profile_stat
*stat
;
585 struct ftrace_profile
*rec
;
588 if (!ftrace_profile_enabled
)
591 local_irq_save(flags
);
593 stat
= &__get_cpu_var(ftrace_profile_stats
);
597 rec
= ftrace_find_profiled_func(stat
, ip
);
599 rec
= ftrace_profile_alloc(stat
, ip
);
606 local_irq_restore(flags
);
609 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
610 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
612 function_profile_call(trace
->func
, 0);
616 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
618 struct ftrace_profile_stat
*stat
;
619 unsigned long long calltime
;
620 struct ftrace_profile
*rec
;
623 local_irq_save(flags
);
624 stat
= &__get_cpu_var(ftrace_profile_stats
);
628 calltime
= trace
->rettime
- trace
->calltime
;
630 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
633 index
= trace
->depth
;
635 /* Append this call time to the parent time to subtract */
637 current
->ret_stack
[index
- 1].subtime
+= calltime
;
639 if (current
->ret_stack
[index
].subtime
< calltime
)
640 calltime
-= current
->ret_stack
[index
].subtime
;
645 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
647 rec
->time
+= calltime
;
650 local_irq_restore(flags
);
653 static int register_ftrace_profiler(void)
655 return register_ftrace_graph(&profile_graph_return
,
656 &profile_graph_entry
);
659 static void unregister_ftrace_profiler(void)
661 unregister_ftrace_graph();
664 static struct ftrace_ops ftrace_profile_ops __read_mostly
=
666 .func
= function_profile_call
,
669 static int register_ftrace_profiler(void)
671 return register_ftrace_function(&ftrace_profile_ops
);
674 static void unregister_ftrace_profiler(void)
676 unregister_ftrace_function(&ftrace_profile_ops
);
678 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
681 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
682 size_t cnt
, loff_t
*ppos
)
685 char buf
[64]; /* big enough to hold a number */
688 if (cnt
>= sizeof(buf
))
691 if (copy_from_user(&buf
, ubuf
, cnt
))
696 ret
= strict_strtoul(buf
, 10, &val
);
702 mutex_lock(&ftrace_profile_lock
);
703 if (ftrace_profile_enabled
^ val
) {
705 ret
= ftrace_profile_init();
711 ret
= register_ftrace_profiler();
716 ftrace_profile_enabled
= 1;
718 ftrace_profile_enabled
= 0;
719 unregister_ftrace_profiler();
723 mutex_unlock(&ftrace_profile_lock
);
731 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
732 size_t cnt
, loff_t
*ppos
)
734 char buf
[64]; /* big enough to hold a number */
737 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
738 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
741 static const struct file_operations ftrace_profile_fops
= {
742 .open
= tracing_open_generic
,
743 .read
= ftrace_profile_read
,
744 .write
= ftrace_profile_write
,
747 /* used to initialize the real stat files */
748 static struct tracer_stat function_stats __initdata
= {
750 .stat_start
= function_stat_start
,
751 .stat_next
= function_stat_next
,
752 .stat_cmp
= function_stat_cmp
,
753 .stat_headers
= function_stat_headers
,
754 .stat_show
= function_stat_show
757 static void ftrace_profile_debugfs(struct dentry
*d_tracer
)
759 struct ftrace_profile_stat
*stat
;
760 struct dentry
*entry
;
765 for_each_possible_cpu(cpu
) {
766 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
768 /* allocate enough for function name + cpu number */
769 name
= kmalloc(32, GFP_KERNEL
);
772 * The files created are permanent, if something happens
773 * we still do not free memory.
777 "Could not allocate stat file for cpu %d\n",
781 stat
->stat
= function_stats
;
782 snprintf(name
, 32, "function%d", cpu
);
783 stat
->stat
.name
= name
;
784 ret
= register_stat_tracer(&stat
->stat
);
787 "Could not register function stat for cpu %d\n",
794 entry
= debugfs_create_file("function_profile_enabled", 0644,
795 d_tracer
, NULL
, &ftrace_profile_fops
);
797 pr_warning("Could not create debugfs "
798 "'function_profile_enabled' entry\n");
801 #else /* CONFIG_FUNCTION_PROFILER */
802 static void ftrace_profile_debugfs(struct dentry
*d_tracer
)
805 #endif /* CONFIG_FUNCTION_PROFILER */
807 /* set when tracing only a pid */
808 struct pid
*ftrace_pid_trace
;
809 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
811 #ifdef CONFIG_DYNAMIC_FTRACE
813 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
814 # error Dynamic ftrace depends on MCOUNT_RECORD
817 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
819 struct ftrace_func_probe
{
820 struct hlist_node node
;
821 struct ftrace_probe_ops
*ops
;
829 FTRACE_ENABLE_CALLS
= (1 << 0),
830 FTRACE_DISABLE_CALLS
= (1 << 1),
831 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
832 FTRACE_ENABLE_MCOUNT
= (1 << 3),
833 FTRACE_DISABLE_MCOUNT
= (1 << 4),
834 FTRACE_START_FUNC_RET
= (1 << 5),
835 FTRACE_STOP_FUNC_RET
= (1 << 6),
838 static int ftrace_filtered
;
840 static struct dyn_ftrace
*ftrace_new_addrs
;
842 static DEFINE_MUTEX(ftrace_regex_lock
);
845 struct ftrace_page
*next
;
847 struct dyn_ftrace records
[];
850 #define ENTRIES_PER_PAGE \
851 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
853 /* estimate from running different kernels */
854 #define NR_TO_INIT 10000
856 static struct ftrace_page
*ftrace_pages_start
;
857 static struct ftrace_page
*ftrace_pages
;
859 static struct dyn_ftrace
*ftrace_free_records
;
862 * This is a double for. Do not use 'break' to break out of the loop,
863 * you must use a goto.
865 #define do_for_each_ftrace_rec(pg, rec) \
866 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
868 for (_____i = 0; _____i < pg->index; _____i++) { \
869 rec = &pg->records[_____i];
871 #define while_for_each_ftrace_rec() \
875 #ifdef CONFIG_KPROBES
877 static int frozen_record_count
;
879 static inline void freeze_record(struct dyn_ftrace
*rec
)
881 if (!(rec
->flags
& FTRACE_FL_FROZEN
)) {
882 rec
->flags
|= FTRACE_FL_FROZEN
;
883 frozen_record_count
++;
887 static inline void unfreeze_record(struct dyn_ftrace
*rec
)
889 if (rec
->flags
& FTRACE_FL_FROZEN
) {
890 rec
->flags
&= ~FTRACE_FL_FROZEN
;
891 frozen_record_count
--;
895 static inline int record_frozen(struct dyn_ftrace
*rec
)
897 return rec
->flags
& FTRACE_FL_FROZEN
;
900 # define freeze_record(rec) ({ 0; })
901 # define unfreeze_record(rec) ({ 0; })
902 # define record_frozen(rec) ({ 0; })
903 #endif /* CONFIG_KPROBES */
905 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
907 rec
->freelist
= ftrace_free_records
;
908 ftrace_free_records
= rec
;
909 rec
->flags
|= FTRACE_FL_FREE
;
912 void ftrace_release(void *start
, unsigned long size
)
914 struct dyn_ftrace
*rec
;
915 struct ftrace_page
*pg
;
916 unsigned long s
= (unsigned long)start
;
917 unsigned long e
= s
+ size
;
919 if (ftrace_disabled
|| !start
)
922 mutex_lock(&ftrace_lock
);
923 do_for_each_ftrace_rec(pg
, rec
) {
924 if ((rec
->ip
>= s
) && (rec
->ip
< e
) &&
925 !(rec
->flags
& FTRACE_FL_FREE
))
926 ftrace_free_rec(rec
);
927 } while_for_each_ftrace_rec();
928 mutex_unlock(&ftrace_lock
);
931 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
933 struct dyn_ftrace
*rec
;
935 /* First check for freed records */
936 if (ftrace_free_records
) {
937 rec
= ftrace_free_records
;
939 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
940 FTRACE_WARN_ON_ONCE(1);
941 ftrace_free_records
= NULL
;
945 ftrace_free_records
= rec
->freelist
;
946 memset(rec
, 0, sizeof(*rec
));
950 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
951 if (!ftrace_pages
->next
) {
952 /* allocate another page */
954 (void *)get_zeroed_page(GFP_KERNEL
);
955 if (!ftrace_pages
->next
)
958 ftrace_pages
= ftrace_pages
->next
;
961 return &ftrace_pages
->records
[ftrace_pages
->index
++];
964 static struct dyn_ftrace
*
965 ftrace_record_ip(unsigned long ip
)
967 struct dyn_ftrace
*rec
;
972 rec
= ftrace_alloc_dyn_node(ip
);
977 rec
->newlist
= ftrace_new_addrs
;
978 ftrace_new_addrs
= rec
;
983 static void print_ip_ins(const char *fmt
, unsigned char *p
)
987 printk(KERN_CONT
"%s", fmt
);
989 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
990 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
993 static void ftrace_bug(int failed
, unsigned long ip
)
997 FTRACE_WARN_ON_ONCE(1);
998 pr_info("ftrace faulted on modifying ");
1002 FTRACE_WARN_ON_ONCE(1);
1003 pr_info("ftrace failed to modify ");
1005 print_ip_ins(" actual: ", (unsigned char *)ip
);
1006 printk(KERN_CONT
"\n");
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace faulted on writing ");
1014 FTRACE_WARN_ON_ONCE(1);
1015 pr_info("ftrace faulted on unknown error ");
1022 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1024 unsigned long ftrace_addr
;
1025 unsigned long ip
, fl
;
1027 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1032 * If this record is not to be traced and
1033 * it is not enabled then do nothing.
1035 * If this record is not to be traced and
1036 * it is enabled then disable it.
1039 if (rec
->flags
& FTRACE_FL_NOTRACE
) {
1040 if (rec
->flags
& FTRACE_FL_ENABLED
)
1041 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1045 } else if (ftrace_filtered
&& enable
) {
1050 fl
= rec
->flags
& (FTRACE_FL_FILTER
| FTRACE_FL_ENABLED
);
1052 /* Record is filtered and enabled, do nothing */
1053 if (fl
== (FTRACE_FL_FILTER
| FTRACE_FL_ENABLED
))
1056 /* Record is not filtered or enabled, do nothing */
1060 /* Record is not filtered but enabled, disable it */
1061 if (fl
== FTRACE_FL_ENABLED
)
1062 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1064 /* Otherwise record is filtered but not enabled, enable it */
1065 rec
->flags
|= FTRACE_FL_ENABLED
;
1067 /* Disable or not filtered */
1070 /* if record is enabled, do nothing */
1071 if (rec
->flags
& FTRACE_FL_ENABLED
)
1074 rec
->flags
|= FTRACE_FL_ENABLED
;
1078 /* if record is not enabled, do nothing */
1079 if (!(rec
->flags
& FTRACE_FL_ENABLED
))
1082 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1086 if (rec
->flags
& FTRACE_FL_ENABLED
)
1087 return ftrace_make_call(rec
, ftrace_addr
);
1089 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1092 static void ftrace_replace_code(int enable
)
1094 struct dyn_ftrace
*rec
;
1095 struct ftrace_page
*pg
;
1098 do_for_each_ftrace_rec(pg
, rec
) {
1100 * Skip over free records, records that have
1101 * failed and not converted.
1103 if (rec
->flags
& FTRACE_FL_FREE
||
1104 rec
->flags
& FTRACE_FL_FAILED
||
1105 !(rec
->flags
& FTRACE_FL_CONVERTED
))
1108 /* ignore updates to this record's mcount site */
1109 if (get_kprobe((void *)rec
->ip
)) {
1113 unfreeze_record(rec
);
1116 failed
= __ftrace_replace_code(rec
, enable
);
1118 rec
->flags
|= FTRACE_FL_FAILED
;
1119 if ((system_state
== SYSTEM_BOOTING
) ||
1120 !core_kernel_text(rec
->ip
)) {
1121 ftrace_free_rec(rec
);
1123 ftrace_bug(failed
, rec
->ip
);
1124 /* Stop processing */
1128 } while_for_each_ftrace_rec();
1132 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1139 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1141 ftrace_bug(ret
, ip
);
1142 rec
->flags
|= FTRACE_FL_FAILED
;
1149 * archs can override this function if they must do something
1150 * before the modifying code is performed.
1152 int __weak
ftrace_arch_code_modify_prepare(void)
1158 * archs can override this function if they must do something
1159 * after the modifying code is performed.
1161 int __weak
ftrace_arch_code_modify_post_process(void)
1166 static int __ftrace_modify_code(void *data
)
1168 int *command
= data
;
1170 if (*command
& FTRACE_ENABLE_CALLS
)
1171 ftrace_replace_code(1);
1172 else if (*command
& FTRACE_DISABLE_CALLS
)
1173 ftrace_replace_code(0);
1175 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1176 ftrace_update_ftrace_func(ftrace_trace_function
);
1178 if (*command
& FTRACE_START_FUNC_RET
)
1179 ftrace_enable_ftrace_graph_caller();
1180 else if (*command
& FTRACE_STOP_FUNC_RET
)
1181 ftrace_disable_ftrace_graph_caller();
1186 static void ftrace_run_update_code(int command
)
1190 ret
= ftrace_arch_code_modify_prepare();
1191 FTRACE_WARN_ON(ret
);
1195 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1197 ret
= ftrace_arch_code_modify_post_process();
1198 FTRACE_WARN_ON(ret
);
1201 static ftrace_func_t saved_ftrace_func
;
1202 static int ftrace_start_up
;
1204 static void ftrace_startup_enable(int command
)
1206 if (saved_ftrace_func
!= ftrace_trace_function
) {
1207 saved_ftrace_func
= ftrace_trace_function
;
1208 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1211 if (!command
|| !ftrace_enabled
)
1214 ftrace_run_update_code(command
);
1217 static void ftrace_startup(int command
)
1219 if (unlikely(ftrace_disabled
))
1223 command
|= FTRACE_ENABLE_CALLS
;
1225 ftrace_startup_enable(command
);
1228 static void ftrace_shutdown(int command
)
1230 if (unlikely(ftrace_disabled
))
1234 if (!ftrace_start_up
)
1235 command
|= FTRACE_DISABLE_CALLS
;
1237 if (saved_ftrace_func
!= ftrace_trace_function
) {
1238 saved_ftrace_func
= ftrace_trace_function
;
1239 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1242 if (!command
|| !ftrace_enabled
)
1245 ftrace_run_update_code(command
);
1248 static void ftrace_startup_sysctl(void)
1250 int command
= FTRACE_ENABLE_MCOUNT
;
1252 if (unlikely(ftrace_disabled
))
1255 /* Force update next time */
1256 saved_ftrace_func
= NULL
;
1257 /* ftrace_start_up is true if we want ftrace running */
1258 if (ftrace_start_up
)
1259 command
|= FTRACE_ENABLE_CALLS
;
1261 ftrace_run_update_code(command
);
1264 static void ftrace_shutdown_sysctl(void)
1266 int command
= FTRACE_DISABLE_MCOUNT
;
1268 if (unlikely(ftrace_disabled
))
1271 /* ftrace_start_up is true if ftrace is running */
1272 if (ftrace_start_up
)
1273 command
|= FTRACE_DISABLE_CALLS
;
1275 ftrace_run_update_code(command
);
1278 static cycle_t ftrace_update_time
;
1279 static unsigned long ftrace_update_cnt
;
1280 unsigned long ftrace_update_tot_cnt
;
1282 static int ftrace_update_code(struct module
*mod
)
1284 struct dyn_ftrace
*p
;
1285 cycle_t start
, stop
;
1287 start
= ftrace_now(raw_smp_processor_id());
1288 ftrace_update_cnt
= 0;
1290 while (ftrace_new_addrs
) {
1292 /* If something went wrong, bail without enabling anything */
1293 if (unlikely(ftrace_disabled
))
1296 p
= ftrace_new_addrs
;
1297 ftrace_new_addrs
= p
->newlist
;
1300 /* convert record (i.e, patch mcount-call with NOP) */
1301 if (ftrace_code_disable(mod
, p
)) {
1302 p
->flags
|= FTRACE_FL_CONVERTED
;
1303 ftrace_update_cnt
++;
1308 stop
= ftrace_now(raw_smp_processor_id());
1309 ftrace_update_time
= stop
- start
;
1310 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1315 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1317 struct ftrace_page
*pg
;
1321 /* allocate a few pages */
1322 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1323 if (!ftrace_pages_start
)
1327 * Allocate a few more pages.
1329 * TODO: have some parser search vmlinux before
1330 * final linking to find all calls to ftrace.
1332 * a) know how many pages to allocate.
1334 * b) set up the table then.
1336 * The dynamic code is still necessary for
1340 pg
= ftrace_pages
= ftrace_pages_start
;
1342 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1343 pr_info("ftrace: allocating %ld entries in %d pages\n",
1344 num_to_init
, cnt
+ 1);
1346 for (i
= 0; i
< cnt
; i
++) {
1347 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1349 /* If we fail, we'll try later anyway */
1360 FTRACE_ITER_FILTER
= (1 << 0),
1361 FTRACE_ITER_CONT
= (1 << 1),
1362 FTRACE_ITER_NOTRACE
= (1 << 2),
1363 FTRACE_ITER_FAILURES
= (1 << 3),
1364 FTRACE_ITER_PRINTALL
= (1 << 4),
1365 FTRACE_ITER_HASH
= (1 << 5),
1368 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1370 struct ftrace_iterator
{
1371 struct ftrace_page
*pg
;
1375 unsigned char buffer
[FTRACE_BUFF_MAX
+1];
1376 unsigned buffer_idx
;
1381 t_hash_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1383 struct ftrace_iterator
*iter
= m
->private;
1384 struct hlist_node
*hnd
= v
;
1385 struct hlist_head
*hhd
;
1387 WARN_ON(!(iter
->flags
& FTRACE_ITER_HASH
));
1392 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1395 hhd
= &ftrace_func_hash
[iter
->hidx
];
1397 if (hlist_empty(hhd
)) {
1416 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1418 struct ftrace_iterator
*iter
= m
->private;
1421 iter
->flags
|= FTRACE_ITER_HASH
;
1423 return t_hash_next(m
, p
, pos
);
1426 static int t_hash_show(struct seq_file
*m
, void *v
)
1428 struct ftrace_func_probe
*rec
;
1429 struct hlist_node
*hnd
= v
;
1430 char str
[KSYM_SYMBOL_LEN
];
1432 rec
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1434 if (rec
->ops
->print
)
1435 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1437 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1438 seq_printf(m
, "%s:", str
);
1440 kallsyms_lookup((unsigned long)rec
->ops
->func
, NULL
, NULL
, NULL
, str
);
1441 seq_printf(m
, "%s", str
);
1444 seq_printf(m
, ":%p", rec
->data
);
1451 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1453 struct ftrace_iterator
*iter
= m
->private;
1454 struct dyn_ftrace
*rec
= NULL
;
1456 if (iter
->flags
& FTRACE_ITER_HASH
)
1457 return t_hash_next(m
, v
, pos
);
1461 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
1465 if (iter
->idx
>= iter
->pg
->index
) {
1466 if (iter
->pg
->next
) {
1467 iter
->pg
= iter
->pg
->next
;
1474 rec
= &iter
->pg
->records
[iter
->idx
++];
1475 if ((rec
->flags
& FTRACE_FL_FREE
) ||
1477 (!(iter
->flags
& FTRACE_ITER_FAILURES
) &&
1478 (rec
->flags
& FTRACE_FL_FAILED
)) ||
1480 ((iter
->flags
& FTRACE_ITER_FAILURES
) &&
1481 !(rec
->flags
& FTRACE_FL_FAILED
)) ||
1483 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
1484 !(rec
->flags
& FTRACE_FL_FILTER
)) ||
1486 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
1487 !(rec
->flags
& FTRACE_FL_NOTRACE
))) {
1496 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1498 struct ftrace_iterator
*iter
= m
->private;
1501 mutex_lock(&ftrace_lock
);
1503 * For set_ftrace_filter reading, if we have the filter
1504 * off, we can short cut and just print out that all
1505 * functions are enabled.
1507 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ftrace_filtered
) {
1509 return t_hash_start(m
, pos
);
1510 iter
->flags
|= FTRACE_ITER_PRINTALL
;
1515 if (iter
->flags
& FTRACE_ITER_HASH
)
1516 return t_hash_start(m
, pos
);
1525 p
= t_next(m
, p
, pos
);
1528 return t_hash_start(m
, pos
);
1533 static void t_stop(struct seq_file
*m
, void *p
)
1535 mutex_unlock(&ftrace_lock
);
1538 static int t_show(struct seq_file
*m
, void *v
)
1540 struct ftrace_iterator
*iter
= m
->private;
1541 struct dyn_ftrace
*rec
= v
;
1542 char str
[KSYM_SYMBOL_LEN
];
1544 if (iter
->flags
& FTRACE_ITER_HASH
)
1545 return t_hash_show(m
, v
);
1547 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
1548 seq_printf(m
, "#### all functions enabled ####\n");
1555 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1557 seq_printf(m
, "%s\n", str
);
1562 static struct seq_operations show_ftrace_seq_ops
= {
1570 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
1572 struct ftrace_iterator
*iter
;
1575 if (unlikely(ftrace_disabled
))
1578 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1582 iter
->pg
= ftrace_pages_start
;
1584 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1586 struct seq_file
*m
= file
->private_data
;
1596 int ftrace_avail_release(struct inode
*inode
, struct file
*file
)
1598 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1599 struct ftrace_iterator
*iter
= m
->private;
1601 seq_release(inode
, file
);
1608 ftrace_failures_open(struct inode
*inode
, struct file
*file
)
1612 struct ftrace_iterator
*iter
;
1614 ret
= ftrace_avail_open(inode
, file
);
1616 m
= (struct seq_file
*)file
->private_data
;
1617 iter
= (struct ftrace_iterator
*)m
->private;
1618 iter
->flags
= FTRACE_ITER_FAILURES
;
1625 static void ftrace_filter_reset(int enable
)
1627 struct ftrace_page
*pg
;
1628 struct dyn_ftrace
*rec
;
1629 unsigned long type
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1631 mutex_lock(&ftrace_lock
);
1633 ftrace_filtered
= 0;
1634 do_for_each_ftrace_rec(pg
, rec
) {
1635 if (rec
->flags
& FTRACE_FL_FAILED
)
1637 rec
->flags
&= ~type
;
1638 } while_for_each_ftrace_rec();
1639 mutex_unlock(&ftrace_lock
);
1643 ftrace_regex_open(struct inode
*inode
, struct file
*file
, int enable
)
1645 struct ftrace_iterator
*iter
;
1648 if (unlikely(ftrace_disabled
))
1651 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1655 mutex_lock(&ftrace_regex_lock
);
1656 if ((file
->f_mode
& FMODE_WRITE
) &&
1657 !(file
->f_flags
& O_APPEND
))
1658 ftrace_filter_reset(enable
);
1660 if (file
->f_mode
& FMODE_READ
) {
1661 iter
->pg
= ftrace_pages_start
;
1662 iter
->flags
= enable
? FTRACE_ITER_FILTER
:
1663 FTRACE_ITER_NOTRACE
;
1665 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1667 struct seq_file
*m
= file
->private_data
;
1672 file
->private_data
= iter
;
1673 mutex_unlock(&ftrace_regex_lock
);
1679 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
1681 return ftrace_regex_open(inode
, file
, 1);
1685 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
1687 return ftrace_regex_open(inode
, file
, 0);
1691 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
1695 if (file
->f_mode
& FMODE_READ
)
1696 ret
= seq_lseek(file
, offset
, origin
);
1698 file
->f_pos
= ret
= 1;
1711 * (static function - no need for kernel doc)
1713 * Pass in a buffer containing a glob and this function will
1714 * set search to point to the search part of the buffer and
1715 * return the type of search it is (see enum above).
1716 * This does modify buff.
1718 * Returns enum type.
1719 * search returns the pointer to use for comparison.
1720 * not returns 1 if buff started with a '!'
1724 ftrace_setup_glob(char *buff
, int len
, char **search
, int *not)
1726 int type
= MATCH_FULL
;
1729 if (buff
[0] == '!') {
1738 for (i
= 0; i
< len
; i
++) {
1739 if (buff
[i
] == '*') {
1742 type
= MATCH_END_ONLY
;
1744 if (type
== MATCH_END_ONLY
)
1745 type
= MATCH_MIDDLE_ONLY
;
1747 type
= MATCH_FRONT_ONLY
;
1757 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
1764 if (strcmp(str
, regex
) == 0)
1767 case MATCH_FRONT_ONLY
:
1768 if (strncmp(str
, regex
, len
) == 0)
1771 case MATCH_MIDDLE_ONLY
:
1772 if (strstr(str
, regex
))
1775 case MATCH_END_ONLY
:
1776 ptr
= strstr(str
, regex
);
1777 if (ptr
&& (ptr
[len
] == 0))
1786 ftrace_match_record(struct dyn_ftrace
*rec
, char *regex
, int len
, int type
)
1788 char str
[KSYM_SYMBOL_LEN
];
1790 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1791 return ftrace_match(str
, regex
, len
, type
);
1794 static void ftrace_match_records(char *buff
, int len
, int enable
)
1796 unsigned int search_len
;
1797 struct ftrace_page
*pg
;
1798 struct dyn_ftrace
*rec
;
1804 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1805 type
= ftrace_setup_glob(buff
, len
, &search
, ¬);
1807 search_len
= strlen(search
);
1809 mutex_lock(&ftrace_lock
);
1810 do_for_each_ftrace_rec(pg
, rec
) {
1812 if (rec
->flags
& FTRACE_FL_FAILED
)
1815 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
1817 rec
->flags
&= ~flag
;
1822 * Only enable filtering if we have a function that
1825 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1826 ftrace_filtered
= 1;
1827 } while_for_each_ftrace_rec();
1828 mutex_unlock(&ftrace_lock
);
1832 ftrace_match_module_record(struct dyn_ftrace
*rec
, char *mod
,
1833 char *regex
, int len
, int type
)
1835 char str
[KSYM_SYMBOL_LEN
];
1838 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
1840 if (!modname
|| strcmp(modname
, mod
))
1843 /* blank search means to match all funcs in the mod */
1845 return ftrace_match(str
, regex
, len
, type
);
1850 static void ftrace_match_module_records(char *buff
, char *mod
, int enable
)
1852 unsigned search_len
= 0;
1853 struct ftrace_page
*pg
;
1854 struct dyn_ftrace
*rec
;
1855 int type
= MATCH_FULL
;
1856 char *search
= buff
;
1860 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1862 /* blank or '*' mean the same */
1863 if (strcmp(buff
, "*") == 0)
1866 /* handle the case of 'dont filter this module' */
1867 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
1873 type
= ftrace_setup_glob(buff
, strlen(buff
), &search
, ¬);
1874 search_len
= strlen(search
);
1877 mutex_lock(&ftrace_lock
);
1878 do_for_each_ftrace_rec(pg
, rec
) {
1880 if (rec
->flags
& FTRACE_FL_FAILED
)
1883 if (ftrace_match_module_record(rec
, mod
,
1884 search
, search_len
, type
)) {
1886 rec
->flags
&= ~flag
;
1890 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1891 ftrace_filtered
= 1;
1893 } while_for_each_ftrace_rec();
1894 mutex_unlock(&ftrace_lock
);
1898 * We register the module command as a template to show others how
1899 * to register the a command as well.
1903 ftrace_mod_callback(char *func
, char *cmd
, char *param
, int enable
)
1908 * cmd == 'mod' because we only registered this func
1909 * for the 'mod' ftrace_func_command.
1910 * But if you register one func with multiple commands,
1911 * you can tell which command was used by the cmd
1915 /* we must have a module name */
1919 mod
= strsep(¶m
, ":");
1923 ftrace_match_module_records(func
, mod
, enable
);
1927 static struct ftrace_func_command ftrace_mod_cmd
= {
1929 .func
= ftrace_mod_callback
,
1932 static int __init
ftrace_mod_cmd_init(void)
1934 return register_ftrace_command(&ftrace_mod_cmd
);
1936 device_initcall(ftrace_mod_cmd_init
);
1939 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
1941 struct ftrace_func_probe
*entry
;
1942 struct hlist_head
*hhd
;
1943 struct hlist_node
*n
;
1947 key
= hash_long(ip
, FTRACE_HASH_BITS
);
1949 hhd
= &ftrace_func_hash
[key
];
1951 if (hlist_empty(hhd
))
1955 * Disable preemption for these calls to prevent a RCU grace
1956 * period. This syncs the hash iteration and freeing of items
1957 * on the hash. rcu_read_lock is too dangerous here.
1959 resched
= ftrace_preempt_disable();
1960 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
1961 if (entry
->ip
== ip
)
1962 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
1964 ftrace_preempt_enable(resched
);
1967 static struct ftrace_ops trace_probe_ops __read_mostly
=
1969 .func
= function_trace_probe_call
,
1972 static int ftrace_probe_registered
;
1974 static void __enable_ftrace_function_probe(void)
1978 if (ftrace_probe_registered
)
1981 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1982 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1986 /* Nothing registered? */
1987 if (i
== FTRACE_FUNC_HASHSIZE
)
1990 __register_ftrace_function(&trace_probe_ops
);
1992 ftrace_probe_registered
= 1;
1995 static void __disable_ftrace_function_probe(void)
1999 if (!ftrace_probe_registered
)
2002 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2003 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2008 /* no more funcs left */
2009 __unregister_ftrace_function(&trace_probe_ops
);
2011 ftrace_probe_registered
= 0;
2015 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
2017 struct ftrace_func_probe
*entry
=
2018 container_of(rhp
, struct ftrace_func_probe
, rcu
);
2020 if (entry
->ops
->free
)
2021 entry
->ops
->free(&entry
->data
);
2027 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2030 struct ftrace_func_probe
*entry
;
2031 struct ftrace_page
*pg
;
2032 struct dyn_ftrace
*rec
;
2038 type
= ftrace_setup_glob(glob
, strlen(glob
), &search
, ¬);
2039 len
= strlen(search
);
2041 /* we do not support '!' for function probes */
2045 mutex_lock(&ftrace_lock
);
2046 do_for_each_ftrace_rec(pg
, rec
) {
2048 if (rec
->flags
& FTRACE_FL_FAILED
)
2051 if (!ftrace_match_record(rec
, search
, len
, type
))
2054 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
2056 /* If we did not process any, then return error */
2067 * The caller might want to do something special
2068 * for each function we find. We call the callback
2069 * to give the caller an opportunity to do so.
2071 if (ops
->callback
) {
2072 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
2073 /* caller does not like this func */
2080 entry
->ip
= rec
->ip
;
2082 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2083 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2085 } while_for_each_ftrace_rec();
2086 __enable_ftrace_function_probe();
2089 mutex_unlock(&ftrace_lock
);
2095 PROBE_TEST_FUNC
= 1,
2100 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2101 void *data
, int flags
)
2103 struct ftrace_func_probe
*entry
;
2104 struct hlist_node
*n
, *tmp
;
2105 char str
[KSYM_SYMBOL_LEN
];
2106 int type
= MATCH_FULL
;
2110 if (glob
&& (strcmp(glob
, "*") || !strlen(glob
)))
2115 type
= ftrace_setup_glob(glob
, strlen(glob
), &search
, ¬);
2116 len
= strlen(search
);
2118 /* we do not support '!' for function probes */
2123 mutex_lock(&ftrace_lock
);
2124 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2125 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2127 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2129 /* break up if statements for readability */
2130 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2133 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2136 /* do this last, since it is the most expensive */
2138 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2140 if (!ftrace_match(str
, glob
, len
, type
))
2144 hlist_del(&entry
->node
);
2145 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2148 __disable_ftrace_function_probe();
2149 mutex_unlock(&ftrace_lock
);
2153 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2156 __unregister_ftrace_function_probe(glob
, ops
, data
,
2157 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2161 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2163 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2166 void unregister_ftrace_function_probe_all(char *glob
)
2168 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2171 static LIST_HEAD(ftrace_commands
);
2172 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2174 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2176 struct ftrace_func_command
*p
;
2179 mutex_lock(&ftrace_cmd_mutex
);
2180 list_for_each_entry(p
, &ftrace_commands
, list
) {
2181 if (strcmp(cmd
->name
, p
->name
) == 0) {
2186 list_add(&cmd
->list
, &ftrace_commands
);
2188 mutex_unlock(&ftrace_cmd_mutex
);
2193 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2195 struct ftrace_func_command
*p
, *n
;
2198 mutex_lock(&ftrace_cmd_mutex
);
2199 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2200 if (strcmp(cmd
->name
, p
->name
) == 0) {
2202 list_del_init(&p
->list
);
2207 mutex_unlock(&ftrace_cmd_mutex
);
2212 static int ftrace_process_regex(char *buff
, int len
, int enable
)
2214 char *func
, *command
, *next
= buff
;
2215 struct ftrace_func_command
*p
;
2218 func
= strsep(&next
, ":");
2221 ftrace_match_records(func
, len
, enable
);
2227 command
= strsep(&next
, ":");
2229 mutex_lock(&ftrace_cmd_mutex
);
2230 list_for_each_entry(p
, &ftrace_commands
, list
) {
2231 if (strcmp(p
->name
, command
) == 0) {
2232 ret
= p
->func(func
, command
, next
, enable
);
2237 mutex_unlock(&ftrace_cmd_mutex
);
2243 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2244 size_t cnt
, loff_t
*ppos
, int enable
)
2246 struct ftrace_iterator
*iter
;
2251 if (!cnt
|| cnt
< 0)
2254 mutex_lock(&ftrace_regex_lock
);
2256 if (file
->f_mode
& FMODE_READ
) {
2257 struct seq_file
*m
= file
->private_data
;
2260 iter
= file
->private_data
;
2263 iter
->flags
&= ~FTRACE_ITER_CONT
;
2264 iter
->buffer_idx
= 0;
2267 ret
= get_user(ch
, ubuf
++);
2273 if (!(iter
->flags
& ~FTRACE_ITER_CONT
)) {
2274 /* skip white space */
2275 while (cnt
&& isspace(ch
)) {
2276 ret
= get_user(ch
, ubuf
++);
2284 file
->f_pos
+= read
;
2289 iter
->buffer_idx
= 0;
2292 while (cnt
&& !isspace(ch
)) {
2293 if (iter
->buffer_idx
< FTRACE_BUFF_MAX
)
2294 iter
->buffer
[iter
->buffer_idx
++] = ch
;
2299 ret
= get_user(ch
, ubuf
++);
2308 iter
->buffer
[iter
->buffer_idx
] = 0;
2309 ret
= ftrace_process_regex(iter
->buffer
,
2310 iter
->buffer_idx
, enable
);
2313 iter
->buffer_idx
= 0;
2315 iter
->flags
|= FTRACE_ITER_CONT
;
2318 file
->f_pos
+= read
;
2322 mutex_unlock(&ftrace_regex_lock
);
2328 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2329 size_t cnt
, loff_t
*ppos
)
2331 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2335 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2336 size_t cnt
, loff_t
*ppos
)
2338 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2342 ftrace_set_regex(unsigned char *buf
, int len
, int reset
, int enable
)
2344 if (unlikely(ftrace_disabled
))
2347 mutex_lock(&ftrace_regex_lock
);
2349 ftrace_filter_reset(enable
);
2351 ftrace_match_records(buf
, len
, enable
);
2352 mutex_unlock(&ftrace_regex_lock
);
2356 * ftrace_set_filter - set a function to filter on in ftrace
2357 * @buf - the string that holds the function filter text.
2358 * @len - the length of the string.
2359 * @reset - non zero to reset all filters before applying this filter.
2361 * Filters denote which functions should be enabled when tracing is enabled.
2362 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2364 void ftrace_set_filter(unsigned char *buf
, int len
, int reset
)
2366 ftrace_set_regex(buf
, len
, reset
, 1);
2370 * ftrace_set_notrace - set a function to not trace in ftrace
2371 * @buf - the string that holds the function notrace text.
2372 * @len - the length of the string.
2373 * @reset - non zero to reset all filters before applying this filter.
2375 * Notrace Filters denote which functions should not be enabled when tracing
2376 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2379 void ftrace_set_notrace(unsigned char *buf
, int len
, int reset
)
2381 ftrace_set_regex(buf
, len
, reset
, 0);
2385 ftrace_regex_release(struct inode
*inode
, struct file
*file
, int enable
)
2387 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2388 struct ftrace_iterator
*iter
;
2390 mutex_lock(&ftrace_regex_lock
);
2391 if (file
->f_mode
& FMODE_READ
) {
2394 seq_release(inode
, file
);
2396 iter
= file
->private_data
;
2398 if (iter
->buffer_idx
) {
2400 iter
->buffer
[iter
->buffer_idx
] = 0;
2401 ftrace_match_records(iter
->buffer
, iter
->buffer_idx
, enable
);
2404 mutex_lock(&ftrace_lock
);
2405 if (ftrace_start_up
&& ftrace_enabled
)
2406 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
2407 mutex_unlock(&ftrace_lock
);
2410 mutex_unlock(&ftrace_regex_lock
);
2415 ftrace_filter_release(struct inode
*inode
, struct file
*file
)
2417 return ftrace_regex_release(inode
, file
, 1);
2421 ftrace_notrace_release(struct inode
*inode
, struct file
*file
)
2423 return ftrace_regex_release(inode
, file
, 0);
2426 static const struct file_operations ftrace_avail_fops
= {
2427 .open
= ftrace_avail_open
,
2429 .llseek
= seq_lseek
,
2430 .release
= ftrace_avail_release
,
2433 static const struct file_operations ftrace_failures_fops
= {
2434 .open
= ftrace_failures_open
,
2436 .llseek
= seq_lseek
,
2437 .release
= ftrace_avail_release
,
2440 static const struct file_operations ftrace_filter_fops
= {
2441 .open
= ftrace_filter_open
,
2443 .write
= ftrace_filter_write
,
2444 .llseek
= ftrace_regex_lseek
,
2445 .release
= ftrace_filter_release
,
2448 static const struct file_operations ftrace_notrace_fops
= {
2449 .open
= ftrace_notrace_open
,
2451 .write
= ftrace_notrace_write
,
2452 .llseek
= ftrace_regex_lseek
,
2453 .release
= ftrace_notrace_release
,
2456 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2458 static DEFINE_MUTEX(graph_lock
);
2460 int ftrace_graph_count
;
2461 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
2464 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2466 unsigned long *array
= m
->private;
2471 if (index
>= ftrace_graph_count
)
2474 return &array
[index
];
2477 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
2481 mutex_lock(&graph_lock
);
2483 /* Nothing, tell g_show to print all functions are enabled */
2484 if (!ftrace_graph_count
&& !*pos
)
2487 p
= g_next(m
, p
, pos
);
2492 static void g_stop(struct seq_file
*m
, void *p
)
2494 mutex_unlock(&graph_lock
);
2497 static int g_show(struct seq_file
*m
, void *v
)
2499 unsigned long *ptr
= v
;
2500 char str
[KSYM_SYMBOL_LEN
];
2505 if (ptr
== (unsigned long *)1) {
2506 seq_printf(m
, "#### all functions enabled ####\n");
2510 kallsyms_lookup(*ptr
, NULL
, NULL
, NULL
, str
);
2512 seq_printf(m
, "%s\n", str
);
2517 static struct seq_operations ftrace_graph_seq_ops
= {
2525 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
2529 if (unlikely(ftrace_disabled
))
2532 mutex_lock(&graph_lock
);
2533 if ((file
->f_mode
& FMODE_WRITE
) &&
2534 !(file
->f_flags
& O_APPEND
)) {
2535 ftrace_graph_count
= 0;
2536 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
2539 if (file
->f_mode
& FMODE_READ
) {
2540 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
2542 struct seq_file
*m
= file
->private_data
;
2543 m
->private = ftrace_graph_funcs
;
2546 file
->private_data
= ftrace_graph_funcs
;
2547 mutex_unlock(&graph_lock
);
2553 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
2555 struct dyn_ftrace
*rec
;
2556 struct ftrace_page
*pg
;
2564 if (ftrace_disabled
)
2568 type
= ftrace_setup_glob(buffer
, strlen(buffer
), &search
, ¬);
2572 search_len
= strlen(search
);
2574 mutex_lock(&ftrace_lock
);
2575 do_for_each_ftrace_rec(pg
, rec
) {
2577 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2580 if (rec
->flags
& (FTRACE_FL_FAILED
| FTRACE_FL_FREE
))
2583 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
2584 /* ensure it is not already in the array */
2586 for (i
= 0; i
< *idx
; i
++)
2587 if (array
[i
] == rec
->ip
) {
2592 array
[(*idx
)++] = rec
->ip
;
2596 } while_for_each_ftrace_rec();
2598 mutex_unlock(&ftrace_lock
);
2600 return found
? 0 : -EINVAL
;
2604 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
2605 size_t cnt
, loff_t
*ppos
)
2607 unsigned char buffer
[FTRACE_BUFF_MAX
+1];
2608 unsigned long *array
;
2614 if (!cnt
|| cnt
< 0)
2617 mutex_lock(&graph_lock
);
2619 if (ftrace_graph_count
>= FTRACE_GRAPH_MAX_FUNCS
) {
2624 if (file
->f_mode
& FMODE_READ
) {
2625 struct seq_file
*m
= file
->private_data
;
2628 array
= file
->private_data
;
2630 ret
= get_user(ch
, ubuf
++);
2636 /* skip white space */
2637 while (cnt
&& isspace(ch
)) {
2638 ret
= get_user(ch
, ubuf
++);
2651 while (cnt
&& !isspace(ch
)) {
2652 if (index
< FTRACE_BUFF_MAX
)
2653 buffer
[index
++] = ch
;
2658 ret
= get_user(ch
, ubuf
++);
2666 /* we allow only one expression at a time */
2667 ret
= ftrace_set_func(array
, &ftrace_graph_count
, buffer
);
2671 file
->f_pos
+= read
;
2675 mutex_unlock(&graph_lock
);
2680 static const struct file_operations ftrace_graph_fops
= {
2681 .open
= ftrace_graph_open
,
2683 .write
= ftrace_graph_write
,
2685 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2687 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
2689 struct dentry
*entry
;
2691 entry
= debugfs_create_file("available_filter_functions", 0444,
2692 d_tracer
, NULL
, &ftrace_avail_fops
);
2694 pr_warning("Could not create debugfs "
2695 "'available_filter_functions' entry\n");
2697 entry
= debugfs_create_file("failures", 0444,
2698 d_tracer
, NULL
, &ftrace_failures_fops
);
2700 pr_warning("Could not create debugfs 'failures' entry\n");
2702 entry
= debugfs_create_file("set_ftrace_filter", 0644, d_tracer
,
2703 NULL
, &ftrace_filter_fops
);
2705 pr_warning("Could not create debugfs "
2706 "'set_ftrace_filter' entry\n");
2708 entry
= debugfs_create_file("set_ftrace_notrace", 0644, d_tracer
,
2709 NULL
, &ftrace_notrace_fops
);
2711 pr_warning("Could not create debugfs "
2712 "'set_ftrace_notrace' entry\n");
2714 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2715 entry
= debugfs_create_file("set_graph_function", 0444, d_tracer
,
2717 &ftrace_graph_fops
);
2719 pr_warning("Could not create debugfs "
2720 "'set_graph_function' entry\n");
2721 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2726 static int ftrace_convert_nops(struct module
*mod
,
2727 unsigned long *start
,
2732 unsigned long flags
;
2734 mutex_lock(&ftrace_lock
);
2737 addr
= ftrace_call_adjust(*p
++);
2739 * Some architecture linkers will pad between
2740 * the different mcount_loc sections of different
2741 * object files to satisfy alignments.
2742 * Skip any NULL pointers.
2746 ftrace_record_ip(addr
);
2749 /* disable interrupts to prevent kstop machine */
2750 local_irq_save(flags
);
2751 ftrace_update_code(mod
);
2752 local_irq_restore(flags
);
2753 mutex_unlock(&ftrace_lock
);
2758 void ftrace_init_module(struct module
*mod
,
2759 unsigned long *start
, unsigned long *end
)
2761 if (ftrace_disabled
|| start
== end
)
2763 ftrace_convert_nops(mod
, start
, end
);
2766 extern unsigned long __start_mcount_loc
[];
2767 extern unsigned long __stop_mcount_loc
[];
2769 void __init
ftrace_init(void)
2771 unsigned long count
, addr
, flags
;
2774 /* Keep the ftrace pointer to the stub */
2775 addr
= (unsigned long)ftrace_stub
;
2777 local_irq_save(flags
);
2778 ftrace_dyn_arch_init(&addr
);
2779 local_irq_restore(flags
);
2781 /* ftrace_dyn_arch_init places the return code in addr */
2785 count
= __stop_mcount_loc
- __start_mcount_loc
;
2787 ret
= ftrace_dyn_table_alloc(count
);
2791 last_ftrace_enabled
= ftrace_enabled
= 1;
2793 ret
= ftrace_convert_nops(NULL
,
2799 ftrace_disabled
= 1;
2804 static int __init
ftrace_nodyn_init(void)
2809 device_initcall(ftrace_nodyn_init
);
2811 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
2812 static inline void ftrace_startup_enable(int command
) { }
2813 /* Keep as macros so we do not need to define the commands */
2814 # define ftrace_startup(command) do { } while (0)
2815 # define ftrace_shutdown(command) do { } while (0)
2816 # define ftrace_startup_sysctl() do { } while (0)
2817 # define ftrace_shutdown_sysctl() do { } while (0)
2818 #endif /* CONFIG_DYNAMIC_FTRACE */
2821 ftrace_pid_read(struct file
*file
, char __user
*ubuf
,
2822 size_t cnt
, loff_t
*ppos
)
2827 if (ftrace_pid_trace
== ftrace_swapper_pid
)
2828 r
= sprintf(buf
, "swapper tasks\n");
2829 else if (ftrace_pid_trace
)
2830 r
= sprintf(buf
, "%u\n", pid_vnr(ftrace_pid_trace
));
2832 r
= sprintf(buf
, "no pid\n");
2834 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2837 static void clear_ftrace_swapper(void)
2839 struct task_struct
*p
;
2843 for_each_online_cpu(cpu
) {
2845 clear_tsk_trace_trace(p
);
2850 static void set_ftrace_swapper(void)
2852 struct task_struct
*p
;
2856 for_each_online_cpu(cpu
) {
2858 set_tsk_trace_trace(p
);
2863 static void clear_ftrace_pid(struct pid
*pid
)
2865 struct task_struct
*p
;
2868 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2869 clear_tsk_trace_trace(p
);
2870 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2876 static void set_ftrace_pid(struct pid
*pid
)
2878 struct task_struct
*p
;
2881 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2882 set_tsk_trace_trace(p
);
2883 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2887 static void clear_ftrace_pid_task(struct pid
**pid
)
2889 if (*pid
== ftrace_swapper_pid
)
2890 clear_ftrace_swapper();
2892 clear_ftrace_pid(*pid
);
2897 static void set_ftrace_pid_task(struct pid
*pid
)
2899 if (pid
== ftrace_swapper_pid
)
2900 set_ftrace_swapper();
2902 set_ftrace_pid(pid
);
2906 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
2907 size_t cnt
, loff_t
*ppos
)
2914 if (cnt
>= sizeof(buf
))
2917 if (copy_from_user(&buf
, ubuf
, cnt
))
2922 ret
= strict_strtol(buf
, 10, &val
);
2926 mutex_lock(&ftrace_lock
);
2928 /* disable pid tracing */
2929 if (!ftrace_pid_trace
)
2932 clear_ftrace_pid_task(&ftrace_pid_trace
);
2935 /* swapper task is special */
2937 pid
= ftrace_swapper_pid
;
2938 if (pid
== ftrace_pid_trace
)
2941 pid
= find_get_pid(val
);
2943 if (pid
== ftrace_pid_trace
) {
2949 if (ftrace_pid_trace
)
2950 clear_ftrace_pid_task(&ftrace_pid_trace
);
2955 ftrace_pid_trace
= pid
;
2957 set_ftrace_pid_task(ftrace_pid_trace
);
2960 /* update the function call */
2961 ftrace_update_pid_func();
2962 ftrace_startup_enable(0);
2965 mutex_unlock(&ftrace_lock
);
2970 static const struct file_operations ftrace_pid_fops
= {
2971 .read
= ftrace_pid_read
,
2972 .write
= ftrace_pid_write
,
2975 static __init
int ftrace_init_debugfs(void)
2977 struct dentry
*d_tracer
;
2978 struct dentry
*entry
;
2980 d_tracer
= tracing_init_dentry();
2984 ftrace_init_dyn_debugfs(d_tracer
);
2986 entry
= debugfs_create_file("set_ftrace_pid", 0644, d_tracer
,
2987 NULL
, &ftrace_pid_fops
);
2989 pr_warning("Could not create debugfs "
2990 "'set_ftrace_pid' entry\n");
2992 ftrace_profile_debugfs(d_tracer
);
2996 fs_initcall(ftrace_init_debugfs
);
2999 * ftrace_kill - kill ftrace
3001 * This function should be used by panic code. It stops ftrace
3002 * but in a not so nice way. If you need to simply kill ftrace
3003 * from a non-atomic section, use ftrace_kill.
3005 void ftrace_kill(void)
3007 ftrace_disabled
= 1;
3009 clear_ftrace_function();
3013 * register_ftrace_function - register a function for profiling
3014 * @ops - ops structure that holds the function for profiling.
3016 * Register a function to be called by all functions in the
3019 * Note: @ops->func and all the functions it calls must be labeled
3020 * with "notrace", otherwise it will go into a
3023 int register_ftrace_function(struct ftrace_ops
*ops
)
3027 if (unlikely(ftrace_disabled
))
3030 mutex_lock(&ftrace_lock
);
3032 ret
= __register_ftrace_function(ops
);
3035 mutex_unlock(&ftrace_lock
);
3040 * unregister_ftrace_function - unregister a function for profiling.
3041 * @ops - ops structure that holds the function to unregister
3043 * Unregister a function that was added to be called by ftrace profiling.
3045 int unregister_ftrace_function(struct ftrace_ops
*ops
)
3049 mutex_lock(&ftrace_lock
);
3050 ret
= __unregister_ftrace_function(ops
);
3052 mutex_unlock(&ftrace_lock
);
3058 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
3059 struct file
*file
, void __user
*buffer
, size_t *lenp
,
3064 if (unlikely(ftrace_disabled
))
3067 mutex_lock(&ftrace_lock
);
3069 ret
= proc_dointvec(table
, write
, file
, buffer
, lenp
, ppos
);
3071 if (ret
|| !write
|| (last_ftrace_enabled
== ftrace_enabled
))
3074 last_ftrace_enabled
= ftrace_enabled
;
3076 if (ftrace_enabled
) {
3078 ftrace_startup_sysctl();
3080 /* we are starting ftrace again */
3081 if (ftrace_list
!= &ftrace_list_end
) {
3082 if (ftrace_list
->next
== &ftrace_list_end
)
3083 ftrace_trace_function
= ftrace_list
->func
;
3085 ftrace_trace_function
= ftrace_list_func
;
3089 /* stopping ftrace calls (just send to ftrace_stub) */
3090 ftrace_trace_function
= ftrace_stub
;
3092 ftrace_shutdown_sysctl();
3096 mutex_unlock(&ftrace_lock
);
3100 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3102 static atomic_t ftrace_graph_active
;
3103 static struct notifier_block ftrace_suspend_notifier
;
3105 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3110 /* The callbacks that hook a function */
3111 trace_func_graph_ret_t ftrace_graph_return
=
3112 (trace_func_graph_ret_t
)ftrace_stub
;
3113 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3115 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3116 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3120 unsigned long flags
;
3121 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3122 struct task_struct
*g
, *t
;
3124 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3125 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3126 * sizeof(struct ftrace_ret_stack
),
3128 if (!ret_stack_list
[i
]) {
3136 read_lock_irqsave(&tasklist_lock
, flags
);
3137 do_each_thread(g
, t
) {
3143 if (t
->ret_stack
== NULL
) {
3144 t
->curr_ret_stack
= -1;
3145 /* Make sure IRQs see the -1 first: */
3147 t
->ret_stack
= ret_stack_list
[start
++];
3148 atomic_set(&t
->tracing_graph_pause
, 0);
3149 atomic_set(&t
->trace_overrun
, 0);
3151 } while_each_thread(g
, t
);
3154 read_unlock_irqrestore(&tasklist_lock
, flags
);
3156 for (i
= start
; i
< end
; i
++)
3157 kfree(ret_stack_list
[i
]);
3162 ftrace_graph_probe_sched_switch(struct rq
*__rq
, struct task_struct
*prev
,
3163 struct task_struct
*next
)
3165 unsigned long long timestamp
;
3169 * Does the user want to count the time a function was asleep.
3170 * If so, do not update the time stamps.
3172 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
3175 timestamp
= trace_clock_local();
3177 prev
->ftrace_timestamp
= timestamp
;
3179 /* only process tasks that we timestamped */
3180 if (!next
->ftrace_timestamp
)
3184 * Update all the counters in next to make up for the
3185 * time next was sleeping.
3187 timestamp
-= next
->ftrace_timestamp
;
3189 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
3190 next
->ret_stack
[index
].calltime
+= timestamp
;
3193 /* Allocate a return stack for each task */
3194 static int start_graph_tracing(void)
3196 struct ftrace_ret_stack
**ret_stack_list
;
3199 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
3200 sizeof(struct ftrace_ret_stack
*),
3203 if (!ret_stack_list
)
3206 /* The cpu_boot init_task->ret_stack will never be freed */
3207 for_each_online_cpu(cpu
)
3208 ftrace_graph_init_task(idle_task(cpu
));
3211 ret
= alloc_retstack_tasklist(ret_stack_list
);
3212 } while (ret
== -EAGAIN
);
3215 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3217 pr_info("ftrace_graph: Couldn't activate tracepoint"
3218 " probe to kernel_sched_switch\n");
3221 kfree(ret_stack_list
);
3226 * Hibernation protection.
3227 * The state of the current task is too much unstable during
3228 * suspend/restore to disk. We want to protect against that.
3231 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
3235 case PM_HIBERNATION_PREPARE
:
3236 pause_graph_tracing();
3239 case PM_POST_HIBERNATION
:
3240 unpause_graph_tracing();
3246 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
3247 trace_func_graph_ent_t entryfunc
)
3251 mutex_lock(&ftrace_lock
);
3253 /* we currently allow only one tracer registered at a time */
3254 if (atomic_read(&ftrace_graph_active
)) {
3259 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
3260 register_pm_notifier(&ftrace_suspend_notifier
);
3262 atomic_inc(&ftrace_graph_active
);
3263 ret
= start_graph_tracing();
3265 atomic_dec(&ftrace_graph_active
);
3269 ftrace_graph_return
= retfunc
;
3270 ftrace_graph_entry
= entryfunc
;
3272 ftrace_startup(FTRACE_START_FUNC_RET
);
3275 mutex_unlock(&ftrace_lock
);
3279 void unregister_ftrace_graph(void)
3281 mutex_lock(&ftrace_lock
);
3283 atomic_dec(&ftrace_graph_active
);
3284 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3285 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
3286 ftrace_graph_entry
= ftrace_graph_entry_stub
;
3287 ftrace_shutdown(FTRACE_STOP_FUNC_RET
);
3288 unregister_pm_notifier(&ftrace_suspend_notifier
);
3290 mutex_unlock(&ftrace_lock
);
3293 /* Allocate a return stack for newly created task */
3294 void ftrace_graph_init_task(struct task_struct
*t
)
3296 if (atomic_read(&ftrace_graph_active
)) {
3297 t
->ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
3298 * sizeof(struct ftrace_ret_stack
),
3302 t
->curr_ret_stack
= -1;
3303 atomic_set(&t
->tracing_graph_pause
, 0);
3304 atomic_set(&t
->trace_overrun
, 0);
3305 t
->ftrace_timestamp
= 0;
3307 t
->ret_stack
= NULL
;
3310 void ftrace_graph_exit_task(struct task_struct
*t
)
3312 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
3314 t
->ret_stack
= NULL
;
3315 /* NULL must become visible to IRQs before we free it: */
3321 void ftrace_graph_stop(void)