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
;
275 static int ftrace_profile_enabled
;
276 static DEFINE_MUTEX(ftrace_profile_lock
);
278 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
280 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
283 function_stat_next(void *v
, int idx
)
285 struct ftrace_profile
*rec
= v
;
286 struct ftrace_profile_page
*pg
;
288 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
292 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
296 rec
= &pg
->records
[0];
304 static void *function_stat_start(struct tracer_stat
*trace
)
306 struct ftrace_profile_stat
*stat
=
307 container_of(trace
, struct ftrace_profile_stat
, stat
);
309 if (!stat
|| !stat
->start
)
312 return function_stat_next(&stat
->start
->records
[0], 0);
315 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
316 /* function graph compares on total time */
317 static int function_stat_cmp(void *p1
, void *p2
)
319 struct ftrace_profile
*a
= p1
;
320 struct ftrace_profile
*b
= p2
;
322 if (a
->time
< b
->time
)
324 if (a
->time
> b
->time
)
330 /* not function graph compares against hits */
331 static int function_stat_cmp(void *p1
, void *p2
)
333 struct ftrace_profile
*a
= p1
;
334 struct ftrace_profile
*b
= p2
;
336 if (a
->counter
< b
->counter
)
338 if (a
->counter
> b
->counter
)
345 static int function_stat_headers(struct seq_file
*m
)
347 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
348 seq_printf(m
, " Function Hit Time\n"
349 " -------- --- ----\n");
351 seq_printf(m
, " Function Hit\n"
357 static int function_stat_show(struct seq_file
*m
, void *v
)
359 struct ftrace_profile
*rec
= v
;
360 char str
[KSYM_SYMBOL_LEN
];
361 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
362 static struct trace_seq s
;
363 static DEFINE_MUTEX(mutex
);
367 trace_print_graph_duration(rec
->time
, &s
);
370 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
371 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
373 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
375 trace_print_seq(m
, &s
);
376 mutex_unlock(&mutex
);
383 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
385 struct ftrace_profile_page
*pg
;
387 pg
= stat
->pages
= stat
->start
;
390 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
395 memset(stat
->hash
, 0,
396 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
399 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
401 struct ftrace_profile_page
*pg
;
404 /* If we already allocated, do nothing */
408 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
412 pg
= stat
->start
= stat
->pages
;
414 /* allocate 10 more pages to start */
415 for (i
= 0; i
< 10; i
++) {
416 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
418 * We only care about allocating profile_pages, if
419 * we failed to allocate here, hopefully we will allocate
430 static int ftrace_profile_init_cpu(int cpu
)
432 struct ftrace_profile_stat
*stat
;
435 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
438 /* If the profile is already created, simply reset it */
439 ftrace_profile_reset(stat
);
444 * We are profiling all functions, but usually only a few thousand
445 * functions are hit. We'll make a hash of 1024 items.
447 size
= FTRACE_PROFILE_HASH_SIZE
;
449 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
454 if (!ftrace_profile_bits
) {
457 for (; size
; size
>>= 1)
458 ftrace_profile_bits
++;
461 /* Preallocate a few pages */
462 if (ftrace_profile_pages_init(stat
) < 0) {
471 static int ftrace_profile_init(void)
476 for_each_online_cpu(cpu
) {
477 ret
= ftrace_profile_init_cpu(cpu
);
485 /* interrupts must be disabled */
486 static struct ftrace_profile
*
487 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
489 struct ftrace_profile
*rec
;
490 struct hlist_head
*hhd
;
491 struct hlist_node
*n
;
494 key
= hash_long(ip
, ftrace_profile_bits
);
495 hhd
= &stat
->hash
[key
];
497 if (hlist_empty(hhd
))
500 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
508 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
509 struct ftrace_profile
*rec
)
513 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
514 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
517 /* Interrupts must be disabled calling this */
518 static struct ftrace_profile
*
519 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
,
520 unsigned long ip
, bool alloc_safe
)
522 struct ftrace_profile
*rec
= NULL
;
524 /* prevent recursion */
525 if (atomic_inc_return(&stat
->disabled
) != 1)
528 /* Try to always keep another page available */
529 if (!stat
->pages
->next
&& alloc_safe
)
530 stat
->pages
->next
= (void *)get_zeroed_page(GFP_ATOMIC
);
533 * Try to find the function again since another
534 * task on another CPU could have added it
536 rec
= ftrace_find_profiled_func(stat
, ip
);
540 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
541 if (!stat
->pages
->next
)
543 stat
->pages
= stat
->pages
->next
;
546 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
548 ftrace_add_profile(stat
, rec
);
551 atomic_dec(&stat
->disabled
);
557 * If we are not in an interrupt, or softirq and
558 * and interrupts are disabled and preemption is not enabled
559 * (not in a spinlock) then it should be safe to allocate memory.
561 static bool ftrace_safe_to_allocate(void)
563 return !in_interrupt() && irqs_disabled() && !preempt_count();
567 function_profile_call(unsigned long ip
, unsigned long parent_ip
)
569 struct ftrace_profile_stat
*stat
;
570 struct ftrace_profile
*rec
;
574 if (!ftrace_profile_enabled
)
577 alloc_safe
= ftrace_safe_to_allocate();
579 local_irq_save(flags
);
581 stat
= &__get_cpu_var(ftrace_profile_stats
);
585 rec
= ftrace_find_profiled_func(stat
, ip
);
587 rec
= ftrace_profile_alloc(stat
, ip
, alloc_safe
);
594 local_irq_restore(flags
);
597 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
598 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
600 function_profile_call(trace
->func
, 0);
604 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
606 struct ftrace_profile_stat
*stat
;
607 unsigned long long calltime
;
608 struct ftrace_profile
*rec
;
611 local_irq_save(flags
);
612 stat
= &__get_cpu_var(ftrace_profile_stats
);
616 calltime
= trace
->rettime
- trace
->calltime
;
618 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
621 index
= trace
->depth
;
623 /* Append this call time to the parent time to subtract */
625 current
->ret_stack
[index
- 1].subtime
+= calltime
;
627 if (current
->ret_stack
[index
].subtime
< calltime
)
628 calltime
-= current
->ret_stack
[index
].subtime
;
633 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
635 rec
->time
+= calltime
;
638 local_irq_restore(flags
);
641 static int register_ftrace_profiler(void)
643 return register_ftrace_graph(&profile_graph_return
,
644 &profile_graph_entry
);
647 static void unregister_ftrace_profiler(void)
649 unregister_ftrace_graph();
652 static struct ftrace_ops ftrace_profile_ops __read_mostly
=
654 .func
= function_profile_call
,
657 static int register_ftrace_profiler(void)
659 return register_ftrace_function(&ftrace_profile_ops
);
662 static void unregister_ftrace_profiler(void)
664 unregister_ftrace_function(&ftrace_profile_ops
);
666 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
669 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
670 size_t cnt
, loff_t
*ppos
)
676 if (cnt
>= sizeof(buf
))
679 if (copy_from_user(&buf
, ubuf
, cnt
))
684 ret
= strict_strtoul(buf
, 10, &val
);
690 mutex_lock(&ftrace_profile_lock
);
691 if (ftrace_profile_enabled
^ val
) {
693 ret
= ftrace_profile_init();
699 ret
= register_ftrace_profiler();
704 ftrace_profile_enabled
= 1;
706 ftrace_profile_enabled
= 0;
707 unregister_ftrace_profiler();
711 mutex_unlock(&ftrace_profile_lock
);
719 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
720 size_t cnt
, loff_t
*ppos
)
725 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
726 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
729 static const struct file_operations ftrace_profile_fops
= {
730 .open
= tracing_open_generic
,
731 .read
= ftrace_profile_read
,
732 .write
= ftrace_profile_write
,
735 /* used to initialize the real stat files */
736 static struct tracer_stat function_stats __initdata
= {
738 .stat_start
= function_stat_start
,
739 .stat_next
= function_stat_next
,
740 .stat_cmp
= function_stat_cmp
,
741 .stat_headers
= function_stat_headers
,
742 .stat_show
= function_stat_show
745 static void ftrace_profile_debugfs(struct dentry
*d_tracer
)
747 struct ftrace_profile_stat
*stat
;
748 struct dentry
*entry
;
753 for_each_possible_cpu(cpu
) {
754 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
756 /* allocate enough for function name + cpu number */
757 name
= kmalloc(32, GFP_KERNEL
);
760 * The files created are permanent, if something happens
761 * we still do not free memory.
765 "Could not allocate stat file for cpu %d\n",
769 stat
->stat
= function_stats
;
770 snprintf(name
, 32, "function%d", cpu
);
771 stat
->stat
.name
= name
;
772 ret
= register_stat_tracer(&stat
->stat
);
775 "Could not register function stat for cpu %d\n",
782 entry
= debugfs_create_file("function_profile_enabled", 0644,
783 d_tracer
, NULL
, &ftrace_profile_fops
);
785 pr_warning("Could not create debugfs "
786 "'function_profile_enabled' entry\n");
789 #else /* CONFIG_FUNCTION_PROFILER */
790 static void ftrace_profile_debugfs(struct dentry
*d_tracer
)
793 #endif /* CONFIG_FUNCTION_PROFILER */
795 /* set when tracing only a pid */
796 struct pid
*ftrace_pid_trace
;
797 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
799 #ifdef CONFIG_DYNAMIC_FTRACE
801 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
802 # error Dynamic ftrace depends on MCOUNT_RECORD
805 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
807 struct ftrace_func_probe
{
808 struct hlist_node node
;
809 struct ftrace_probe_ops
*ops
;
817 FTRACE_ENABLE_CALLS
= (1 << 0),
818 FTRACE_DISABLE_CALLS
= (1 << 1),
819 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
820 FTRACE_ENABLE_MCOUNT
= (1 << 3),
821 FTRACE_DISABLE_MCOUNT
= (1 << 4),
822 FTRACE_START_FUNC_RET
= (1 << 5),
823 FTRACE_STOP_FUNC_RET
= (1 << 6),
826 static int ftrace_filtered
;
828 static struct dyn_ftrace
*ftrace_new_addrs
;
830 static DEFINE_MUTEX(ftrace_regex_lock
);
833 struct ftrace_page
*next
;
835 struct dyn_ftrace records
[];
838 #define ENTRIES_PER_PAGE \
839 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
841 /* estimate from running different kernels */
842 #define NR_TO_INIT 10000
844 static struct ftrace_page
*ftrace_pages_start
;
845 static struct ftrace_page
*ftrace_pages
;
847 static struct dyn_ftrace
*ftrace_free_records
;
850 * This is a double for. Do not use 'break' to break out of the loop,
851 * you must use a goto.
853 #define do_for_each_ftrace_rec(pg, rec) \
854 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
856 for (_____i = 0; _____i < pg->index; _____i++) { \
857 rec = &pg->records[_____i];
859 #define while_for_each_ftrace_rec() \
863 #ifdef CONFIG_KPROBES
865 static int frozen_record_count
;
867 static inline void freeze_record(struct dyn_ftrace
*rec
)
869 if (!(rec
->flags
& FTRACE_FL_FROZEN
)) {
870 rec
->flags
|= FTRACE_FL_FROZEN
;
871 frozen_record_count
++;
875 static inline void unfreeze_record(struct dyn_ftrace
*rec
)
877 if (rec
->flags
& FTRACE_FL_FROZEN
) {
878 rec
->flags
&= ~FTRACE_FL_FROZEN
;
879 frozen_record_count
--;
883 static inline int record_frozen(struct dyn_ftrace
*rec
)
885 return rec
->flags
& FTRACE_FL_FROZEN
;
888 # define freeze_record(rec) ({ 0; })
889 # define unfreeze_record(rec) ({ 0; })
890 # define record_frozen(rec) ({ 0; })
891 #endif /* CONFIG_KPROBES */
893 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
895 rec
->freelist
= ftrace_free_records
;
896 ftrace_free_records
= rec
;
897 rec
->flags
|= FTRACE_FL_FREE
;
900 void ftrace_release(void *start
, unsigned long size
)
902 struct dyn_ftrace
*rec
;
903 struct ftrace_page
*pg
;
904 unsigned long s
= (unsigned long)start
;
905 unsigned long e
= s
+ size
;
907 if (ftrace_disabled
|| !start
)
910 mutex_lock(&ftrace_lock
);
911 do_for_each_ftrace_rec(pg
, rec
) {
912 if ((rec
->ip
>= s
) && (rec
->ip
< e
) &&
913 !(rec
->flags
& FTRACE_FL_FREE
))
914 ftrace_free_rec(rec
);
915 } while_for_each_ftrace_rec();
916 mutex_unlock(&ftrace_lock
);
919 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
921 struct dyn_ftrace
*rec
;
923 /* First check for freed records */
924 if (ftrace_free_records
) {
925 rec
= ftrace_free_records
;
927 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
928 FTRACE_WARN_ON_ONCE(1);
929 ftrace_free_records
= NULL
;
933 ftrace_free_records
= rec
->freelist
;
934 memset(rec
, 0, sizeof(*rec
));
938 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
939 if (!ftrace_pages
->next
) {
940 /* allocate another page */
942 (void *)get_zeroed_page(GFP_KERNEL
);
943 if (!ftrace_pages
->next
)
946 ftrace_pages
= ftrace_pages
->next
;
949 return &ftrace_pages
->records
[ftrace_pages
->index
++];
952 static struct dyn_ftrace
*
953 ftrace_record_ip(unsigned long ip
)
955 struct dyn_ftrace
*rec
;
960 rec
= ftrace_alloc_dyn_node(ip
);
965 rec
->newlist
= ftrace_new_addrs
;
966 ftrace_new_addrs
= rec
;
971 static void print_ip_ins(const char *fmt
, unsigned char *p
)
975 printk(KERN_CONT
"%s", fmt
);
977 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
978 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
981 static void ftrace_bug(int failed
, unsigned long ip
)
985 FTRACE_WARN_ON_ONCE(1);
986 pr_info("ftrace faulted on modifying ");
990 FTRACE_WARN_ON_ONCE(1);
991 pr_info("ftrace failed to modify ");
993 print_ip_ins(" actual: ", (unsigned char *)ip
);
994 printk(KERN_CONT
"\n");
997 FTRACE_WARN_ON_ONCE(1);
998 pr_info("ftrace faulted on writing ");
1002 FTRACE_WARN_ON_ONCE(1);
1003 pr_info("ftrace faulted on unknown error ");
1010 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1012 unsigned long ftrace_addr
;
1013 unsigned long ip
, fl
;
1015 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1020 * If this record is not to be traced and
1021 * it is not enabled then do nothing.
1023 * If this record is not to be traced and
1024 * it is enabled then disable it.
1027 if (rec
->flags
& FTRACE_FL_NOTRACE
) {
1028 if (rec
->flags
& FTRACE_FL_ENABLED
)
1029 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1033 } else if (ftrace_filtered
&& enable
) {
1038 fl
= rec
->flags
& (FTRACE_FL_FILTER
| FTRACE_FL_ENABLED
);
1040 /* Record is filtered and enabled, do nothing */
1041 if (fl
== (FTRACE_FL_FILTER
| FTRACE_FL_ENABLED
))
1044 /* Record is not filtered or enabled, do nothing */
1048 /* Record is not filtered but enabled, disable it */
1049 if (fl
== FTRACE_FL_ENABLED
)
1050 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1052 /* Otherwise record is filtered but not enabled, enable it */
1053 rec
->flags
|= FTRACE_FL_ENABLED
;
1055 /* Disable or not filtered */
1058 /* if record is enabled, do nothing */
1059 if (rec
->flags
& FTRACE_FL_ENABLED
)
1062 rec
->flags
|= FTRACE_FL_ENABLED
;
1066 /* if record is not enabled, do nothing */
1067 if (!(rec
->flags
& FTRACE_FL_ENABLED
))
1070 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1074 if (rec
->flags
& FTRACE_FL_ENABLED
)
1075 return ftrace_make_call(rec
, ftrace_addr
);
1077 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1080 static void ftrace_replace_code(int enable
)
1082 struct dyn_ftrace
*rec
;
1083 struct ftrace_page
*pg
;
1086 do_for_each_ftrace_rec(pg
, rec
) {
1088 * Skip over free records, records that have
1089 * failed and not converted.
1091 if (rec
->flags
& FTRACE_FL_FREE
||
1092 rec
->flags
& FTRACE_FL_FAILED
||
1093 !(rec
->flags
& FTRACE_FL_CONVERTED
))
1096 /* ignore updates to this record's mcount site */
1097 if (get_kprobe((void *)rec
->ip
)) {
1101 unfreeze_record(rec
);
1104 failed
= __ftrace_replace_code(rec
, enable
);
1106 rec
->flags
|= FTRACE_FL_FAILED
;
1107 if ((system_state
== SYSTEM_BOOTING
) ||
1108 !core_kernel_text(rec
->ip
)) {
1109 ftrace_free_rec(rec
);
1111 ftrace_bug(failed
, rec
->ip
);
1112 /* Stop processing */
1116 } while_for_each_ftrace_rec();
1120 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1127 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1129 ftrace_bug(ret
, ip
);
1130 rec
->flags
|= FTRACE_FL_FAILED
;
1137 * archs can override this function if they must do something
1138 * before the modifying code is performed.
1140 int __weak
ftrace_arch_code_modify_prepare(void)
1146 * archs can override this function if they must do something
1147 * after the modifying code is performed.
1149 int __weak
ftrace_arch_code_modify_post_process(void)
1154 static int __ftrace_modify_code(void *data
)
1156 int *command
= data
;
1158 if (*command
& FTRACE_ENABLE_CALLS
)
1159 ftrace_replace_code(1);
1160 else if (*command
& FTRACE_DISABLE_CALLS
)
1161 ftrace_replace_code(0);
1163 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1164 ftrace_update_ftrace_func(ftrace_trace_function
);
1166 if (*command
& FTRACE_START_FUNC_RET
)
1167 ftrace_enable_ftrace_graph_caller();
1168 else if (*command
& FTRACE_STOP_FUNC_RET
)
1169 ftrace_disable_ftrace_graph_caller();
1174 static void ftrace_run_update_code(int command
)
1178 ret
= ftrace_arch_code_modify_prepare();
1179 FTRACE_WARN_ON(ret
);
1183 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1185 ret
= ftrace_arch_code_modify_post_process();
1186 FTRACE_WARN_ON(ret
);
1189 static ftrace_func_t saved_ftrace_func
;
1190 static int ftrace_start_up
;
1192 static void ftrace_startup_enable(int command
)
1194 if (saved_ftrace_func
!= ftrace_trace_function
) {
1195 saved_ftrace_func
= ftrace_trace_function
;
1196 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1199 if (!command
|| !ftrace_enabled
)
1202 ftrace_run_update_code(command
);
1205 static void ftrace_startup(int command
)
1207 if (unlikely(ftrace_disabled
))
1211 command
|= FTRACE_ENABLE_CALLS
;
1213 ftrace_startup_enable(command
);
1216 static void ftrace_shutdown(int command
)
1218 if (unlikely(ftrace_disabled
))
1222 if (!ftrace_start_up
)
1223 command
|= FTRACE_DISABLE_CALLS
;
1225 if (saved_ftrace_func
!= ftrace_trace_function
) {
1226 saved_ftrace_func
= ftrace_trace_function
;
1227 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1230 if (!command
|| !ftrace_enabled
)
1233 ftrace_run_update_code(command
);
1236 static void ftrace_startup_sysctl(void)
1238 int command
= FTRACE_ENABLE_MCOUNT
;
1240 if (unlikely(ftrace_disabled
))
1243 /* Force update next time */
1244 saved_ftrace_func
= NULL
;
1245 /* ftrace_start_up is true if we want ftrace running */
1246 if (ftrace_start_up
)
1247 command
|= FTRACE_ENABLE_CALLS
;
1249 ftrace_run_update_code(command
);
1252 static void ftrace_shutdown_sysctl(void)
1254 int command
= FTRACE_DISABLE_MCOUNT
;
1256 if (unlikely(ftrace_disabled
))
1259 /* ftrace_start_up is true if ftrace is running */
1260 if (ftrace_start_up
)
1261 command
|= FTRACE_DISABLE_CALLS
;
1263 ftrace_run_update_code(command
);
1266 static cycle_t ftrace_update_time
;
1267 static unsigned long ftrace_update_cnt
;
1268 unsigned long ftrace_update_tot_cnt
;
1270 static int ftrace_update_code(struct module
*mod
)
1272 struct dyn_ftrace
*p
;
1273 cycle_t start
, stop
;
1275 start
= ftrace_now(raw_smp_processor_id());
1276 ftrace_update_cnt
= 0;
1278 while (ftrace_new_addrs
) {
1280 /* If something went wrong, bail without enabling anything */
1281 if (unlikely(ftrace_disabled
))
1284 p
= ftrace_new_addrs
;
1285 ftrace_new_addrs
= p
->newlist
;
1288 /* convert record (i.e, patch mcount-call with NOP) */
1289 if (ftrace_code_disable(mod
, p
)) {
1290 p
->flags
|= FTRACE_FL_CONVERTED
;
1291 ftrace_update_cnt
++;
1296 stop
= ftrace_now(raw_smp_processor_id());
1297 ftrace_update_time
= stop
- start
;
1298 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1303 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1305 struct ftrace_page
*pg
;
1309 /* allocate a few pages */
1310 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1311 if (!ftrace_pages_start
)
1315 * Allocate a few more pages.
1317 * TODO: have some parser search vmlinux before
1318 * final linking to find all calls to ftrace.
1320 * a) know how many pages to allocate.
1322 * b) set up the table then.
1324 * The dynamic code is still necessary for
1328 pg
= ftrace_pages
= ftrace_pages_start
;
1330 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1331 pr_info("ftrace: allocating %ld entries in %d pages\n",
1332 num_to_init
, cnt
+ 1);
1334 for (i
= 0; i
< cnt
; i
++) {
1335 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1337 /* If we fail, we'll try later anyway */
1348 FTRACE_ITER_FILTER
= (1 << 0),
1349 FTRACE_ITER_CONT
= (1 << 1),
1350 FTRACE_ITER_NOTRACE
= (1 << 2),
1351 FTRACE_ITER_FAILURES
= (1 << 3),
1352 FTRACE_ITER_PRINTALL
= (1 << 4),
1353 FTRACE_ITER_HASH
= (1 << 5),
1356 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1358 struct ftrace_iterator
{
1359 struct ftrace_page
*pg
;
1363 unsigned char buffer
[FTRACE_BUFF_MAX
+1];
1364 unsigned buffer_idx
;
1369 t_hash_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1371 struct ftrace_iterator
*iter
= m
->private;
1372 struct hlist_node
*hnd
= v
;
1373 struct hlist_head
*hhd
;
1375 WARN_ON(!(iter
->flags
& FTRACE_ITER_HASH
));
1380 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1383 hhd
= &ftrace_func_hash
[iter
->hidx
];
1385 if (hlist_empty(hhd
)) {
1404 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1406 struct ftrace_iterator
*iter
= m
->private;
1409 iter
->flags
|= FTRACE_ITER_HASH
;
1411 return t_hash_next(m
, p
, pos
);
1414 static int t_hash_show(struct seq_file
*m
, void *v
)
1416 struct ftrace_func_probe
*rec
;
1417 struct hlist_node
*hnd
= v
;
1418 char str
[KSYM_SYMBOL_LEN
];
1420 rec
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1422 if (rec
->ops
->print
)
1423 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1425 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1426 seq_printf(m
, "%s:", str
);
1428 kallsyms_lookup((unsigned long)rec
->ops
->func
, NULL
, NULL
, NULL
, str
);
1429 seq_printf(m
, "%s", str
);
1432 seq_printf(m
, ":%p", rec
->data
);
1439 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1441 struct ftrace_iterator
*iter
= m
->private;
1442 struct dyn_ftrace
*rec
= NULL
;
1444 if (iter
->flags
& FTRACE_ITER_HASH
)
1445 return t_hash_next(m
, v
, pos
);
1449 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
1453 if (iter
->idx
>= iter
->pg
->index
) {
1454 if (iter
->pg
->next
) {
1455 iter
->pg
= iter
->pg
->next
;
1462 rec
= &iter
->pg
->records
[iter
->idx
++];
1463 if ((rec
->flags
& FTRACE_FL_FREE
) ||
1465 (!(iter
->flags
& FTRACE_ITER_FAILURES
) &&
1466 (rec
->flags
& FTRACE_FL_FAILED
)) ||
1468 ((iter
->flags
& FTRACE_ITER_FAILURES
) &&
1469 !(rec
->flags
& FTRACE_FL_FAILED
)) ||
1471 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
1472 !(rec
->flags
& FTRACE_FL_FILTER
)) ||
1474 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
1475 !(rec
->flags
& FTRACE_FL_NOTRACE
))) {
1484 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1486 struct ftrace_iterator
*iter
= m
->private;
1489 mutex_lock(&ftrace_lock
);
1491 * For set_ftrace_filter reading, if we have the filter
1492 * off, we can short cut and just print out that all
1493 * functions are enabled.
1495 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ftrace_filtered
) {
1497 return t_hash_start(m
, pos
);
1498 iter
->flags
|= FTRACE_ITER_PRINTALL
;
1503 if (iter
->flags
& FTRACE_ITER_HASH
)
1504 return t_hash_start(m
, pos
);
1513 p
= t_next(m
, p
, pos
);
1516 return t_hash_start(m
, pos
);
1521 static void t_stop(struct seq_file
*m
, void *p
)
1523 mutex_unlock(&ftrace_lock
);
1526 static int t_show(struct seq_file
*m
, void *v
)
1528 struct ftrace_iterator
*iter
= m
->private;
1529 struct dyn_ftrace
*rec
= v
;
1530 char str
[KSYM_SYMBOL_LEN
];
1532 if (iter
->flags
& FTRACE_ITER_HASH
)
1533 return t_hash_show(m
, v
);
1535 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
1536 seq_printf(m
, "#### all functions enabled ####\n");
1543 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1545 seq_printf(m
, "%s\n", str
);
1550 static struct seq_operations show_ftrace_seq_ops
= {
1558 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
1560 struct ftrace_iterator
*iter
;
1563 if (unlikely(ftrace_disabled
))
1566 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1570 iter
->pg
= ftrace_pages_start
;
1572 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1574 struct seq_file
*m
= file
->private_data
;
1584 int ftrace_avail_release(struct inode
*inode
, struct file
*file
)
1586 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1587 struct ftrace_iterator
*iter
= m
->private;
1589 seq_release(inode
, file
);
1596 ftrace_failures_open(struct inode
*inode
, struct file
*file
)
1600 struct ftrace_iterator
*iter
;
1602 ret
= ftrace_avail_open(inode
, file
);
1604 m
= (struct seq_file
*)file
->private_data
;
1605 iter
= (struct ftrace_iterator
*)m
->private;
1606 iter
->flags
= FTRACE_ITER_FAILURES
;
1613 static void ftrace_filter_reset(int enable
)
1615 struct ftrace_page
*pg
;
1616 struct dyn_ftrace
*rec
;
1617 unsigned long type
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1619 mutex_lock(&ftrace_lock
);
1621 ftrace_filtered
= 0;
1622 do_for_each_ftrace_rec(pg
, rec
) {
1623 if (rec
->flags
& FTRACE_FL_FAILED
)
1625 rec
->flags
&= ~type
;
1626 } while_for_each_ftrace_rec();
1627 mutex_unlock(&ftrace_lock
);
1631 ftrace_regex_open(struct inode
*inode
, struct file
*file
, int enable
)
1633 struct ftrace_iterator
*iter
;
1636 if (unlikely(ftrace_disabled
))
1639 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1643 mutex_lock(&ftrace_regex_lock
);
1644 if ((file
->f_mode
& FMODE_WRITE
) &&
1645 !(file
->f_flags
& O_APPEND
))
1646 ftrace_filter_reset(enable
);
1648 if (file
->f_mode
& FMODE_READ
) {
1649 iter
->pg
= ftrace_pages_start
;
1650 iter
->flags
= enable
? FTRACE_ITER_FILTER
:
1651 FTRACE_ITER_NOTRACE
;
1653 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1655 struct seq_file
*m
= file
->private_data
;
1660 file
->private_data
= iter
;
1661 mutex_unlock(&ftrace_regex_lock
);
1667 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
1669 return ftrace_regex_open(inode
, file
, 1);
1673 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
1675 return ftrace_regex_open(inode
, file
, 0);
1679 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
1683 if (file
->f_mode
& FMODE_READ
)
1684 ret
= seq_lseek(file
, offset
, origin
);
1686 file
->f_pos
= ret
= 1;
1699 * (static function - no need for kernel doc)
1701 * Pass in a buffer containing a glob and this function will
1702 * set search to point to the search part of the buffer and
1703 * return the type of search it is (see enum above).
1704 * This does modify buff.
1706 * Returns enum type.
1707 * search returns the pointer to use for comparison.
1708 * not returns 1 if buff started with a '!'
1712 ftrace_setup_glob(char *buff
, int len
, char **search
, int *not)
1714 int type
= MATCH_FULL
;
1717 if (buff
[0] == '!') {
1726 for (i
= 0; i
< len
; i
++) {
1727 if (buff
[i
] == '*') {
1730 type
= MATCH_END_ONLY
;
1732 if (type
== MATCH_END_ONLY
)
1733 type
= MATCH_MIDDLE_ONLY
;
1735 type
= MATCH_FRONT_ONLY
;
1745 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
1752 if (strcmp(str
, regex
) == 0)
1755 case MATCH_FRONT_ONLY
:
1756 if (strncmp(str
, regex
, len
) == 0)
1759 case MATCH_MIDDLE_ONLY
:
1760 if (strstr(str
, regex
))
1763 case MATCH_END_ONLY
:
1764 ptr
= strstr(str
, regex
);
1765 if (ptr
&& (ptr
[len
] == 0))
1774 ftrace_match_record(struct dyn_ftrace
*rec
, char *regex
, int len
, int type
)
1776 char str
[KSYM_SYMBOL_LEN
];
1778 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1779 return ftrace_match(str
, regex
, len
, type
);
1782 static void ftrace_match_records(char *buff
, int len
, int enable
)
1784 unsigned int search_len
;
1785 struct ftrace_page
*pg
;
1786 struct dyn_ftrace
*rec
;
1792 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1793 type
= ftrace_setup_glob(buff
, len
, &search
, ¬);
1795 search_len
= strlen(search
);
1797 mutex_lock(&ftrace_lock
);
1798 do_for_each_ftrace_rec(pg
, rec
) {
1800 if (rec
->flags
& FTRACE_FL_FAILED
)
1803 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
1805 rec
->flags
&= ~flag
;
1810 * Only enable filtering if we have a function that
1813 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1814 ftrace_filtered
= 1;
1815 } while_for_each_ftrace_rec();
1816 mutex_unlock(&ftrace_lock
);
1820 ftrace_match_module_record(struct dyn_ftrace
*rec
, char *mod
,
1821 char *regex
, int len
, int type
)
1823 char str
[KSYM_SYMBOL_LEN
];
1826 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
1828 if (!modname
|| strcmp(modname
, mod
))
1831 /* blank search means to match all funcs in the mod */
1833 return ftrace_match(str
, regex
, len
, type
);
1838 static void ftrace_match_module_records(char *buff
, char *mod
, int enable
)
1840 unsigned search_len
= 0;
1841 struct ftrace_page
*pg
;
1842 struct dyn_ftrace
*rec
;
1843 int type
= MATCH_FULL
;
1844 char *search
= buff
;
1848 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1850 /* blank or '*' mean the same */
1851 if (strcmp(buff
, "*") == 0)
1854 /* handle the case of 'dont filter this module' */
1855 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
1861 type
= ftrace_setup_glob(buff
, strlen(buff
), &search
, ¬);
1862 search_len
= strlen(search
);
1865 mutex_lock(&ftrace_lock
);
1866 do_for_each_ftrace_rec(pg
, rec
) {
1868 if (rec
->flags
& FTRACE_FL_FAILED
)
1871 if (ftrace_match_module_record(rec
, mod
,
1872 search
, search_len
, type
)) {
1874 rec
->flags
&= ~flag
;
1878 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1879 ftrace_filtered
= 1;
1881 } while_for_each_ftrace_rec();
1882 mutex_unlock(&ftrace_lock
);
1886 * We register the module command as a template to show others how
1887 * to register the a command as well.
1891 ftrace_mod_callback(char *func
, char *cmd
, char *param
, int enable
)
1896 * cmd == 'mod' because we only registered this func
1897 * for the 'mod' ftrace_func_command.
1898 * But if you register one func with multiple commands,
1899 * you can tell which command was used by the cmd
1903 /* we must have a module name */
1907 mod
= strsep(¶m
, ":");
1911 ftrace_match_module_records(func
, mod
, enable
);
1915 static struct ftrace_func_command ftrace_mod_cmd
= {
1917 .func
= ftrace_mod_callback
,
1920 static int __init
ftrace_mod_cmd_init(void)
1922 return register_ftrace_command(&ftrace_mod_cmd
);
1924 device_initcall(ftrace_mod_cmd_init
);
1927 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
1929 struct ftrace_func_probe
*entry
;
1930 struct hlist_head
*hhd
;
1931 struct hlist_node
*n
;
1935 key
= hash_long(ip
, FTRACE_HASH_BITS
);
1937 hhd
= &ftrace_func_hash
[key
];
1939 if (hlist_empty(hhd
))
1943 * Disable preemption for these calls to prevent a RCU grace
1944 * period. This syncs the hash iteration and freeing of items
1945 * on the hash. rcu_read_lock is too dangerous here.
1947 resched
= ftrace_preempt_disable();
1948 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
1949 if (entry
->ip
== ip
)
1950 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
1952 ftrace_preempt_enable(resched
);
1955 static struct ftrace_ops trace_probe_ops __read_mostly
=
1957 .func
= function_trace_probe_call
,
1960 static int ftrace_probe_registered
;
1962 static void __enable_ftrace_function_probe(void)
1966 if (ftrace_probe_registered
)
1969 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1970 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1974 /* Nothing registered? */
1975 if (i
== FTRACE_FUNC_HASHSIZE
)
1978 __register_ftrace_function(&trace_probe_ops
);
1980 ftrace_probe_registered
= 1;
1983 static void __disable_ftrace_function_probe(void)
1987 if (!ftrace_probe_registered
)
1990 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1991 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1996 /* no more funcs left */
1997 __unregister_ftrace_function(&trace_probe_ops
);
1999 ftrace_probe_registered
= 0;
2003 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
2005 struct ftrace_func_probe
*entry
=
2006 container_of(rhp
, struct ftrace_func_probe
, rcu
);
2008 if (entry
->ops
->free
)
2009 entry
->ops
->free(&entry
->data
);
2015 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2018 struct ftrace_func_probe
*entry
;
2019 struct ftrace_page
*pg
;
2020 struct dyn_ftrace
*rec
;
2026 type
= ftrace_setup_glob(glob
, strlen(glob
), &search
, ¬);
2027 len
= strlen(search
);
2029 /* we do not support '!' for function probes */
2033 mutex_lock(&ftrace_lock
);
2034 do_for_each_ftrace_rec(pg
, rec
) {
2036 if (rec
->flags
& FTRACE_FL_FAILED
)
2039 if (!ftrace_match_record(rec
, search
, len
, type
))
2042 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
2044 /* If we did not process any, then return error */
2055 * The caller might want to do something special
2056 * for each function we find. We call the callback
2057 * to give the caller an opportunity to do so.
2059 if (ops
->callback
) {
2060 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
2061 /* caller does not like this func */
2068 entry
->ip
= rec
->ip
;
2070 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2071 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2073 } while_for_each_ftrace_rec();
2074 __enable_ftrace_function_probe();
2077 mutex_unlock(&ftrace_lock
);
2083 PROBE_TEST_FUNC
= 1,
2088 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2089 void *data
, int flags
)
2091 struct ftrace_func_probe
*entry
;
2092 struct hlist_node
*n
, *tmp
;
2093 char str
[KSYM_SYMBOL_LEN
];
2094 int type
= MATCH_FULL
;
2098 if (glob
&& (strcmp(glob
, "*") || !strlen(glob
)))
2103 type
= ftrace_setup_glob(glob
, strlen(glob
), &search
, ¬);
2104 len
= strlen(search
);
2106 /* we do not support '!' for function probes */
2111 mutex_lock(&ftrace_lock
);
2112 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2113 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2115 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2117 /* break up if statements for readability */
2118 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2121 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2124 /* do this last, since it is the most expensive */
2126 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2128 if (!ftrace_match(str
, glob
, len
, type
))
2132 hlist_del(&entry
->node
);
2133 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2136 __disable_ftrace_function_probe();
2137 mutex_unlock(&ftrace_lock
);
2141 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2144 __unregister_ftrace_function_probe(glob
, ops
, data
,
2145 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2149 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2151 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2154 void unregister_ftrace_function_probe_all(char *glob
)
2156 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2159 static LIST_HEAD(ftrace_commands
);
2160 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2162 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2164 struct ftrace_func_command
*p
;
2167 mutex_lock(&ftrace_cmd_mutex
);
2168 list_for_each_entry(p
, &ftrace_commands
, list
) {
2169 if (strcmp(cmd
->name
, p
->name
) == 0) {
2174 list_add(&cmd
->list
, &ftrace_commands
);
2176 mutex_unlock(&ftrace_cmd_mutex
);
2181 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2183 struct ftrace_func_command
*p
, *n
;
2186 mutex_lock(&ftrace_cmd_mutex
);
2187 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2188 if (strcmp(cmd
->name
, p
->name
) == 0) {
2190 list_del_init(&p
->list
);
2195 mutex_unlock(&ftrace_cmd_mutex
);
2200 static int ftrace_process_regex(char *buff
, int len
, int enable
)
2202 char *func
, *command
, *next
= buff
;
2203 struct ftrace_func_command
*p
;
2206 func
= strsep(&next
, ":");
2209 ftrace_match_records(func
, len
, enable
);
2215 command
= strsep(&next
, ":");
2217 mutex_lock(&ftrace_cmd_mutex
);
2218 list_for_each_entry(p
, &ftrace_commands
, list
) {
2219 if (strcmp(p
->name
, command
) == 0) {
2220 ret
= p
->func(func
, command
, next
, enable
);
2225 mutex_unlock(&ftrace_cmd_mutex
);
2231 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2232 size_t cnt
, loff_t
*ppos
, int enable
)
2234 struct ftrace_iterator
*iter
;
2239 if (!cnt
|| cnt
< 0)
2242 mutex_lock(&ftrace_regex_lock
);
2244 if (file
->f_mode
& FMODE_READ
) {
2245 struct seq_file
*m
= file
->private_data
;
2248 iter
= file
->private_data
;
2251 iter
->flags
&= ~FTRACE_ITER_CONT
;
2252 iter
->buffer_idx
= 0;
2255 ret
= get_user(ch
, ubuf
++);
2261 if (!(iter
->flags
& ~FTRACE_ITER_CONT
)) {
2262 /* skip white space */
2263 while (cnt
&& isspace(ch
)) {
2264 ret
= get_user(ch
, ubuf
++);
2272 file
->f_pos
+= read
;
2277 iter
->buffer_idx
= 0;
2280 while (cnt
&& !isspace(ch
)) {
2281 if (iter
->buffer_idx
< FTRACE_BUFF_MAX
)
2282 iter
->buffer
[iter
->buffer_idx
++] = ch
;
2287 ret
= get_user(ch
, ubuf
++);
2296 iter
->buffer
[iter
->buffer_idx
] = 0;
2297 ret
= ftrace_process_regex(iter
->buffer
,
2298 iter
->buffer_idx
, enable
);
2301 iter
->buffer_idx
= 0;
2303 iter
->flags
|= FTRACE_ITER_CONT
;
2306 file
->f_pos
+= read
;
2310 mutex_unlock(&ftrace_regex_lock
);
2316 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2317 size_t cnt
, loff_t
*ppos
)
2319 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2323 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2324 size_t cnt
, loff_t
*ppos
)
2326 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2330 ftrace_set_regex(unsigned char *buf
, int len
, int reset
, int enable
)
2332 if (unlikely(ftrace_disabled
))
2335 mutex_lock(&ftrace_regex_lock
);
2337 ftrace_filter_reset(enable
);
2339 ftrace_match_records(buf
, len
, enable
);
2340 mutex_unlock(&ftrace_regex_lock
);
2344 * ftrace_set_filter - set a function to filter on in ftrace
2345 * @buf - the string that holds the function filter text.
2346 * @len - the length of the string.
2347 * @reset - non zero to reset all filters before applying this filter.
2349 * Filters denote which functions should be enabled when tracing is enabled.
2350 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2352 void ftrace_set_filter(unsigned char *buf
, int len
, int reset
)
2354 ftrace_set_regex(buf
, len
, reset
, 1);
2358 * ftrace_set_notrace - set a function to not trace in ftrace
2359 * @buf - the string that holds the function notrace text.
2360 * @len - the length of the string.
2361 * @reset - non zero to reset all filters before applying this filter.
2363 * Notrace Filters denote which functions should not be enabled when tracing
2364 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2367 void ftrace_set_notrace(unsigned char *buf
, int len
, int reset
)
2369 ftrace_set_regex(buf
, len
, reset
, 0);
2373 ftrace_regex_release(struct inode
*inode
, struct file
*file
, int enable
)
2375 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2376 struct ftrace_iterator
*iter
;
2378 mutex_lock(&ftrace_regex_lock
);
2379 if (file
->f_mode
& FMODE_READ
) {
2382 seq_release(inode
, file
);
2384 iter
= file
->private_data
;
2386 if (iter
->buffer_idx
) {
2388 iter
->buffer
[iter
->buffer_idx
] = 0;
2389 ftrace_match_records(iter
->buffer
, iter
->buffer_idx
, enable
);
2392 mutex_lock(&ftrace_lock
);
2393 if (ftrace_start_up
&& ftrace_enabled
)
2394 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
2395 mutex_unlock(&ftrace_lock
);
2398 mutex_unlock(&ftrace_regex_lock
);
2403 ftrace_filter_release(struct inode
*inode
, struct file
*file
)
2405 return ftrace_regex_release(inode
, file
, 1);
2409 ftrace_notrace_release(struct inode
*inode
, struct file
*file
)
2411 return ftrace_regex_release(inode
, file
, 0);
2414 static const struct file_operations ftrace_avail_fops
= {
2415 .open
= ftrace_avail_open
,
2417 .llseek
= seq_lseek
,
2418 .release
= ftrace_avail_release
,
2421 static const struct file_operations ftrace_failures_fops
= {
2422 .open
= ftrace_failures_open
,
2424 .llseek
= seq_lseek
,
2425 .release
= ftrace_avail_release
,
2428 static const struct file_operations ftrace_filter_fops
= {
2429 .open
= ftrace_filter_open
,
2431 .write
= ftrace_filter_write
,
2432 .llseek
= ftrace_regex_lseek
,
2433 .release
= ftrace_filter_release
,
2436 static const struct file_operations ftrace_notrace_fops
= {
2437 .open
= ftrace_notrace_open
,
2439 .write
= ftrace_notrace_write
,
2440 .llseek
= ftrace_regex_lseek
,
2441 .release
= ftrace_notrace_release
,
2444 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2446 static DEFINE_MUTEX(graph_lock
);
2448 int ftrace_graph_count
;
2449 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
2452 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2454 unsigned long *array
= m
->private;
2459 if (index
>= ftrace_graph_count
)
2462 return &array
[index
];
2465 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
2469 mutex_lock(&graph_lock
);
2471 /* Nothing, tell g_show to print all functions are enabled */
2472 if (!ftrace_graph_count
&& !*pos
)
2475 p
= g_next(m
, p
, pos
);
2480 static void g_stop(struct seq_file
*m
, void *p
)
2482 mutex_unlock(&graph_lock
);
2485 static int g_show(struct seq_file
*m
, void *v
)
2487 unsigned long *ptr
= v
;
2488 char str
[KSYM_SYMBOL_LEN
];
2493 if (ptr
== (unsigned long *)1) {
2494 seq_printf(m
, "#### all functions enabled ####\n");
2498 kallsyms_lookup(*ptr
, NULL
, NULL
, NULL
, str
);
2500 seq_printf(m
, "%s\n", str
);
2505 static struct seq_operations ftrace_graph_seq_ops
= {
2513 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
2517 if (unlikely(ftrace_disabled
))
2520 mutex_lock(&graph_lock
);
2521 if ((file
->f_mode
& FMODE_WRITE
) &&
2522 !(file
->f_flags
& O_APPEND
)) {
2523 ftrace_graph_count
= 0;
2524 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
2527 if (file
->f_mode
& FMODE_READ
) {
2528 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
2530 struct seq_file
*m
= file
->private_data
;
2531 m
->private = ftrace_graph_funcs
;
2534 file
->private_data
= ftrace_graph_funcs
;
2535 mutex_unlock(&graph_lock
);
2541 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
2543 struct dyn_ftrace
*rec
;
2544 struct ftrace_page
*pg
;
2552 if (ftrace_disabled
)
2556 type
= ftrace_setup_glob(buffer
, strlen(buffer
), &search
, ¬);
2560 search_len
= strlen(search
);
2562 mutex_lock(&ftrace_lock
);
2563 do_for_each_ftrace_rec(pg
, rec
) {
2565 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2568 if (rec
->flags
& (FTRACE_FL_FAILED
| FTRACE_FL_FREE
))
2571 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
2572 /* ensure it is not already in the array */
2574 for (i
= 0; i
< *idx
; i
++)
2575 if (array
[i
] == rec
->ip
) {
2580 array
[(*idx
)++] = rec
->ip
;
2584 } while_for_each_ftrace_rec();
2586 mutex_unlock(&ftrace_lock
);
2588 return found
? 0 : -EINVAL
;
2592 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
2593 size_t cnt
, loff_t
*ppos
)
2595 unsigned char buffer
[FTRACE_BUFF_MAX
+1];
2596 unsigned long *array
;
2602 if (!cnt
|| cnt
< 0)
2605 mutex_lock(&graph_lock
);
2607 if (ftrace_graph_count
>= FTRACE_GRAPH_MAX_FUNCS
) {
2612 if (file
->f_mode
& FMODE_READ
) {
2613 struct seq_file
*m
= file
->private_data
;
2616 array
= file
->private_data
;
2618 ret
= get_user(ch
, ubuf
++);
2624 /* skip white space */
2625 while (cnt
&& isspace(ch
)) {
2626 ret
= get_user(ch
, ubuf
++);
2639 while (cnt
&& !isspace(ch
)) {
2640 if (index
< FTRACE_BUFF_MAX
)
2641 buffer
[index
++] = ch
;
2646 ret
= get_user(ch
, ubuf
++);
2654 /* we allow only one expression at a time */
2655 ret
= ftrace_set_func(array
, &ftrace_graph_count
, buffer
);
2659 file
->f_pos
+= read
;
2663 mutex_unlock(&graph_lock
);
2668 static const struct file_operations ftrace_graph_fops
= {
2669 .open
= ftrace_graph_open
,
2671 .write
= ftrace_graph_write
,
2673 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2675 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
2677 struct dentry
*entry
;
2679 entry
= debugfs_create_file("available_filter_functions", 0444,
2680 d_tracer
, NULL
, &ftrace_avail_fops
);
2682 pr_warning("Could not create debugfs "
2683 "'available_filter_functions' entry\n");
2685 entry
= debugfs_create_file("failures", 0444,
2686 d_tracer
, NULL
, &ftrace_failures_fops
);
2688 pr_warning("Could not create debugfs 'failures' entry\n");
2690 entry
= debugfs_create_file("set_ftrace_filter", 0644, d_tracer
,
2691 NULL
, &ftrace_filter_fops
);
2693 pr_warning("Could not create debugfs "
2694 "'set_ftrace_filter' entry\n");
2696 entry
= debugfs_create_file("set_ftrace_notrace", 0644, d_tracer
,
2697 NULL
, &ftrace_notrace_fops
);
2699 pr_warning("Could not create debugfs "
2700 "'set_ftrace_notrace' entry\n");
2702 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2703 entry
= debugfs_create_file("set_graph_function", 0444, d_tracer
,
2705 &ftrace_graph_fops
);
2707 pr_warning("Could not create debugfs "
2708 "'set_graph_function' entry\n");
2709 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2714 static int ftrace_convert_nops(struct module
*mod
,
2715 unsigned long *start
,
2720 unsigned long flags
;
2722 mutex_lock(&ftrace_lock
);
2725 addr
= ftrace_call_adjust(*p
++);
2727 * Some architecture linkers will pad between
2728 * the different mcount_loc sections of different
2729 * object files to satisfy alignments.
2730 * Skip any NULL pointers.
2734 ftrace_record_ip(addr
);
2737 /* disable interrupts to prevent kstop machine */
2738 local_irq_save(flags
);
2739 ftrace_update_code(mod
);
2740 local_irq_restore(flags
);
2741 mutex_unlock(&ftrace_lock
);
2746 void ftrace_init_module(struct module
*mod
,
2747 unsigned long *start
, unsigned long *end
)
2749 if (ftrace_disabled
|| start
== end
)
2751 ftrace_convert_nops(mod
, start
, end
);
2754 extern unsigned long __start_mcount_loc
[];
2755 extern unsigned long __stop_mcount_loc
[];
2757 void __init
ftrace_init(void)
2759 unsigned long count
, addr
, flags
;
2762 /* Keep the ftrace pointer to the stub */
2763 addr
= (unsigned long)ftrace_stub
;
2765 local_irq_save(flags
);
2766 ftrace_dyn_arch_init(&addr
);
2767 local_irq_restore(flags
);
2769 /* ftrace_dyn_arch_init places the return code in addr */
2773 count
= __stop_mcount_loc
- __start_mcount_loc
;
2775 ret
= ftrace_dyn_table_alloc(count
);
2779 last_ftrace_enabled
= ftrace_enabled
= 1;
2781 ret
= ftrace_convert_nops(NULL
,
2787 ftrace_disabled
= 1;
2792 static int __init
ftrace_nodyn_init(void)
2797 device_initcall(ftrace_nodyn_init
);
2799 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
2800 static inline void ftrace_startup_enable(int command
) { }
2801 /* Keep as macros so we do not need to define the commands */
2802 # define ftrace_startup(command) do { } while (0)
2803 # define ftrace_shutdown(command) do { } while (0)
2804 # define ftrace_startup_sysctl() do { } while (0)
2805 # define ftrace_shutdown_sysctl() do { } while (0)
2806 #endif /* CONFIG_DYNAMIC_FTRACE */
2809 ftrace_pid_read(struct file
*file
, char __user
*ubuf
,
2810 size_t cnt
, loff_t
*ppos
)
2815 if (ftrace_pid_trace
== ftrace_swapper_pid
)
2816 r
= sprintf(buf
, "swapper tasks\n");
2817 else if (ftrace_pid_trace
)
2818 r
= sprintf(buf
, "%u\n", pid_vnr(ftrace_pid_trace
));
2820 r
= sprintf(buf
, "no pid\n");
2822 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2825 static void clear_ftrace_swapper(void)
2827 struct task_struct
*p
;
2831 for_each_online_cpu(cpu
) {
2833 clear_tsk_trace_trace(p
);
2838 static void set_ftrace_swapper(void)
2840 struct task_struct
*p
;
2844 for_each_online_cpu(cpu
) {
2846 set_tsk_trace_trace(p
);
2851 static void clear_ftrace_pid(struct pid
*pid
)
2853 struct task_struct
*p
;
2856 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2857 clear_tsk_trace_trace(p
);
2858 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2864 static void set_ftrace_pid(struct pid
*pid
)
2866 struct task_struct
*p
;
2869 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2870 set_tsk_trace_trace(p
);
2871 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2875 static void clear_ftrace_pid_task(struct pid
**pid
)
2877 if (*pid
== ftrace_swapper_pid
)
2878 clear_ftrace_swapper();
2880 clear_ftrace_pid(*pid
);
2885 static void set_ftrace_pid_task(struct pid
*pid
)
2887 if (pid
== ftrace_swapper_pid
)
2888 set_ftrace_swapper();
2890 set_ftrace_pid(pid
);
2894 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
2895 size_t cnt
, loff_t
*ppos
)
2902 if (cnt
>= sizeof(buf
))
2905 if (copy_from_user(&buf
, ubuf
, cnt
))
2910 ret
= strict_strtol(buf
, 10, &val
);
2914 mutex_lock(&ftrace_lock
);
2916 /* disable pid tracing */
2917 if (!ftrace_pid_trace
)
2920 clear_ftrace_pid_task(&ftrace_pid_trace
);
2923 /* swapper task is special */
2925 pid
= ftrace_swapper_pid
;
2926 if (pid
== ftrace_pid_trace
)
2929 pid
= find_get_pid(val
);
2931 if (pid
== ftrace_pid_trace
) {
2937 if (ftrace_pid_trace
)
2938 clear_ftrace_pid_task(&ftrace_pid_trace
);
2943 ftrace_pid_trace
= pid
;
2945 set_ftrace_pid_task(ftrace_pid_trace
);
2948 /* update the function call */
2949 ftrace_update_pid_func();
2950 ftrace_startup_enable(0);
2953 mutex_unlock(&ftrace_lock
);
2958 static const struct file_operations ftrace_pid_fops
= {
2959 .read
= ftrace_pid_read
,
2960 .write
= ftrace_pid_write
,
2963 static __init
int ftrace_init_debugfs(void)
2965 struct dentry
*d_tracer
;
2966 struct dentry
*entry
;
2968 d_tracer
= tracing_init_dentry();
2972 ftrace_init_dyn_debugfs(d_tracer
);
2974 entry
= debugfs_create_file("set_ftrace_pid", 0644, d_tracer
,
2975 NULL
, &ftrace_pid_fops
);
2977 pr_warning("Could not create debugfs "
2978 "'set_ftrace_pid' entry\n");
2980 ftrace_profile_debugfs(d_tracer
);
2984 fs_initcall(ftrace_init_debugfs
);
2987 * ftrace_kill - kill ftrace
2989 * This function should be used by panic code. It stops ftrace
2990 * but in a not so nice way. If you need to simply kill ftrace
2991 * from a non-atomic section, use ftrace_kill.
2993 void ftrace_kill(void)
2995 ftrace_disabled
= 1;
2997 clear_ftrace_function();
3001 * register_ftrace_function - register a function for profiling
3002 * @ops - ops structure that holds the function for profiling.
3004 * Register a function to be called by all functions in the
3007 * Note: @ops->func and all the functions it calls must be labeled
3008 * with "notrace", otherwise it will go into a
3011 int register_ftrace_function(struct ftrace_ops
*ops
)
3015 if (unlikely(ftrace_disabled
))
3018 mutex_lock(&ftrace_lock
);
3020 ret
= __register_ftrace_function(ops
);
3023 mutex_unlock(&ftrace_lock
);
3028 * unregister_ftrace_function - unregister a function for profiling.
3029 * @ops - ops structure that holds the function to unregister
3031 * Unregister a function that was added to be called by ftrace profiling.
3033 int unregister_ftrace_function(struct ftrace_ops
*ops
)
3037 mutex_lock(&ftrace_lock
);
3038 ret
= __unregister_ftrace_function(ops
);
3040 mutex_unlock(&ftrace_lock
);
3046 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
3047 struct file
*file
, void __user
*buffer
, size_t *lenp
,
3052 if (unlikely(ftrace_disabled
))
3055 mutex_lock(&ftrace_lock
);
3057 ret
= proc_dointvec(table
, write
, file
, buffer
, lenp
, ppos
);
3059 if (ret
|| !write
|| (last_ftrace_enabled
== ftrace_enabled
))
3062 last_ftrace_enabled
= ftrace_enabled
;
3064 if (ftrace_enabled
) {
3066 ftrace_startup_sysctl();
3068 /* we are starting ftrace again */
3069 if (ftrace_list
!= &ftrace_list_end
) {
3070 if (ftrace_list
->next
== &ftrace_list_end
)
3071 ftrace_trace_function
= ftrace_list
->func
;
3073 ftrace_trace_function
= ftrace_list_func
;
3077 /* stopping ftrace calls (just send to ftrace_stub) */
3078 ftrace_trace_function
= ftrace_stub
;
3080 ftrace_shutdown_sysctl();
3084 mutex_unlock(&ftrace_lock
);
3088 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3090 static atomic_t ftrace_graph_active
;
3091 static struct notifier_block ftrace_suspend_notifier
;
3093 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3098 /* The callbacks that hook a function */
3099 trace_func_graph_ret_t ftrace_graph_return
=
3100 (trace_func_graph_ret_t
)ftrace_stub
;
3101 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3103 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3104 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3108 unsigned long flags
;
3109 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3110 struct task_struct
*g
, *t
;
3112 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3113 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3114 * sizeof(struct ftrace_ret_stack
),
3116 if (!ret_stack_list
[i
]) {
3124 read_lock_irqsave(&tasklist_lock
, flags
);
3125 do_each_thread(g
, t
) {
3131 if (t
->ret_stack
== NULL
) {
3132 t
->curr_ret_stack
= -1;
3133 /* Make sure IRQs see the -1 first: */
3135 t
->ret_stack
= ret_stack_list
[start
++];
3136 atomic_set(&t
->tracing_graph_pause
, 0);
3137 atomic_set(&t
->trace_overrun
, 0);
3139 } while_each_thread(g
, t
);
3142 read_unlock_irqrestore(&tasklist_lock
, flags
);
3144 for (i
= start
; i
< end
; i
++)
3145 kfree(ret_stack_list
[i
]);
3150 ftrace_graph_probe_sched_switch(struct rq
*__rq
, struct task_struct
*prev
,
3151 struct task_struct
*next
)
3153 unsigned long long timestamp
;
3157 * Does the user want to count the time a function was asleep.
3158 * If so, do not update the time stamps.
3160 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
3163 timestamp
= trace_clock_local();
3165 prev
->ftrace_timestamp
= timestamp
;
3167 /* only process tasks that we timestamped */
3168 if (!next
->ftrace_timestamp
)
3172 * Update all the counters in next to make up for the
3173 * time next was sleeping.
3175 timestamp
-= next
->ftrace_timestamp
;
3177 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
3178 next
->ret_stack
[index
].calltime
+= timestamp
;
3181 /* Allocate a return stack for each task */
3182 static int start_graph_tracing(void)
3184 struct ftrace_ret_stack
**ret_stack_list
;
3187 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
3188 sizeof(struct ftrace_ret_stack
*),
3191 if (!ret_stack_list
)
3194 /* The cpu_boot init_task->ret_stack will never be freed */
3195 for_each_online_cpu(cpu
)
3196 ftrace_graph_init_task(idle_task(cpu
));
3199 ret
= alloc_retstack_tasklist(ret_stack_list
);
3200 } while (ret
== -EAGAIN
);
3203 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3205 pr_info("ftrace_graph: Couldn't activate tracepoint"
3206 " probe to kernel_sched_switch\n");
3209 kfree(ret_stack_list
);
3214 * Hibernation protection.
3215 * The state of the current task is too much unstable during
3216 * suspend/restore to disk. We want to protect against that.
3219 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
3223 case PM_HIBERNATION_PREPARE
:
3224 pause_graph_tracing();
3227 case PM_POST_HIBERNATION
:
3228 unpause_graph_tracing();
3234 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
3235 trace_func_graph_ent_t entryfunc
)
3239 mutex_lock(&ftrace_lock
);
3241 /* we currently allow only one tracer registered at a time */
3242 if (atomic_read(&ftrace_graph_active
)) {
3247 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
3248 register_pm_notifier(&ftrace_suspend_notifier
);
3250 atomic_inc(&ftrace_graph_active
);
3251 ret
= start_graph_tracing();
3253 atomic_dec(&ftrace_graph_active
);
3257 ftrace_graph_return
= retfunc
;
3258 ftrace_graph_entry
= entryfunc
;
3260 ftrace_startup(FTRACE_START_FUNC_RET
);
3263 mutex_unlock(&ftrace_lock
);
3267 void unregister_ftrace_graph(void)
3269 mutex_lock(&ftrace_lock
);
3271 atomic_dec(&ftrace_graph_active
);
3272 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3273 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
3274 ftrace_graph_entry
= ftrace_graph_entry_stub
;
3275 ftrace_shutdown(FTRACE_STOP_FUNC_RET
);
3276 unregister_pm_notifier(&ftrace_suspend_notifier
);
3278 mutex_unlock(&ftrace_lock
);
3281 /* Allocate a return stack for newly created task */
3282 void ftrace_graph_init_task(struct task_struct
*t
)
3284 if (atomic_read(&ftrace_graph_active
)) {
3285 t
->ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
3286 * sizeof(struct ftrace_ret_stack
),
3290 t
->curr_ret_stack
= -1;
3291 atomic_set(&t
->tracing_graph_pause
, 0);
3292 atomic_set(&t
->trace_overrun
, 0);
3293 t
->ftrace_timestamp
= 0;
3295 t
->ret_stack
= NULL
;
3298 void ftrace_graph_exit_task(struct task_struct
*t
)
3300 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
3302 t
->ret_stack
= NULL
;
3303 /* NULL must become visible to IRQs before we free it: */
3309 void ftrace_graph_stop(void)