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 "
355 seq_printf(m, " Function Hit\n"
361 static int function_stat_show(struct seq_file *m, void *v)
363 struct ftrace_profile *rec = v;
364 char str[KSYM_SYMBOL_LEN];
365 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
366 static DEFINE_MUTEX(mutex);
367 static struct trace_seq s;
368 unsigned long long avg;
371 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
372 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
374 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
377 do_div(avg, rec->counter);
381 trace_print_graph_duration(rec->time, &s);
382 trace_seq_puts(&s, " ");
383 trace_print_graph_duration(avg, &s);
384 trace_print_seq(m, &s);
385 mutex_unlock(&mutex);
392 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
394 struct ftrace_profile_page *pg;
396 pg = stat->pages = stat->start;
399 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
404 memset(stat->hash, 0,
405 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
408 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
410 struct ftrace_profile_page *pg;
415 /* If we already allocated, do nothing */
419 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
423 #ifdef CONFIG_DYNAMIC_FTRACE
424 functions = ftrace_update_tot_cnt;
427 * We do not know the number of functions that exist because
428 * dynamic tracing is what counts them. With past experience
429 * we have around 20K functions. That should be more than enough.
430 * It is highly unlikely we will execute every function in
436 pg = stat->start = stat->pages;
438 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
440 for (i = 0; i < pages; i++) {
441 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
452 unsigned long tmp = (unsigned long)pg;
458 free_page((unsigned long)stat->pages);
465 static int ftrace_profile_init_cpu(int cpu)
467 struct ftrace_profile_stat *stat;
470 stat = &per_cpu(ftrace_profile_stats, cpu);
473 /* If the profile is already created, simply reset it */
474 ftrace_profile_reset(stat);
479 * We are profiling all functions, but usually only a few thousand
480 * functions are hit. We'll make a hash of 1024 items.
482 size = FTRACE_PROFILE_HASH_SIZE;
484 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
489 if (!ftrace_profile_bits) {
492 for (; size; size >>= 1)
493 ftrace_profile_bits++;
496 /* Preallocate the function profiling pages */
497 if (ftrace_profile_pages_init(stat) < 0) {
506 static int ftrace_profile_init(void)
511 for_each_online_cpu(cpu) {
512 ret = ftrace_profile_init_cpu(cpu);
520 /* interrupts must be disabled */
521 static struct ftrace_profile *
522 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
524 struct ftrace_profile *rec;
525 struct hlist_head *hhd;
526 struct hlist_node *n;
529 key = hash_long(ip, ftrace_profile_bits);
530 hhd = &stat->hash[key];
532 if (hlist_empty(hhd))
535 hlist_for_each_entry_rcu(rec, n, hhd, node) {
543 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
544 struct ftrace_profile *rec)
548 key = hash_long(rec->ip, ftrace_profile_bits);
549 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
553 * The memory is already allocated, this simply finds a new record to use.
555 static struct ftrace_profile *
556 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
558 struct ftrace_profile *rec = NULL;
560 /* prevent recursion (from NMIs) */
561 if (atomic_inc_return(&stat->disabled) != 1)
565 * Try to find the function again since an NMI
566 * could have added it
568 rec = ftrace_find_profiled_func(stat, ip);
572 if (stat->pages->index == PROFILES_PER_PAGE) {
573 if (!stat->pages->next)
575 stat->pages = stat->pages->next;
578 rec = &stat->pages->records[stat->pages->index++];
580 ftrace_add_profile(stat, rec);
583 atomic_dec(&stat->disabled);
589 function_profile_call(unsigned long ip, unsigned long parent_ip)
591 struct ftrace_profile_stat *stat;
592 struct ftrace_profile *rec;
595 if (!ftrace_profile_enabled)
598 local_irq_save(flags);
600 stat = &__get_cpu_var(ftrace_profile_stats);
604 rec = ftrace_find_profiled_func(stat, ip);
606 rec = ftrace_profile_alloc(stat, ip);
613 local_irq_restore(flags);
616 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
617 static int profile_graph_entry(struct ftrace_graph_ent *trace)
619 function_profile_call(trace->func, 0);
623 static void profile_graph_return(struct ftrace_graph_ret *trace)
625 struct ftrace_profile_stat *stat;
626 unsigned long long calltime;
627 struct ftrace_profile *rec;
630 local_irq_save(flags);
631 stat = &__get_cpu_var(ftrace_profile_stats);
635 calltime = trace->rettime - trace->calltime;
637 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
640 index = trace->depth;
642 /* Append this call time to the parent time to subtract */
644 current->ret_stack[index - 1].subtime += calltime;
646 if (current->ret_stack[index].subtime < calltime)
647 calltime -= current->ret_stack[index].subtime;
652 rec = ftrace_find_profiled_func(stat, trace->func);
654 rec->time += calltime;
657 local_irq_restore(flags);
660 static int register_ftrace_profiler(void)
662 return register_ftrace_graph(&profile_graph_return,
663 &profile_graph_entry);
666 static void unregister_ftrace_profiler(void)
668 unregister_ftrace_graph();
671 static struct ftrace_ops ftrace_profile_ops __read_mostly =
673 .func = function_profile_call,
676 static int register_ftrace_profiler(void)
678 return register_ftrace_function(&ftrace_profile_ops);
681 static void unregister_ftrace_profiler(void)
683 unregister_ftrace_function(&ftrace_profile_ops);
685 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
688 ftrace_profile_write(struct file *filp, const char __user *ubuf,
689 size_t cnt, loff_t *ppos)
692 char buf[64]; /* big enough to hold a number */
695 if (cnt >= sizeof(buf))
698 if (copy_from_user(&buf, ubuf, cnt))
703 ret = strict_strtoul(buf, 10, &val);
709 mutex_lock(&ftrace_profile_lock);
710 if (ftrace_profile_enabled ^ val) {
712 ret = ftrace_profile_init();
718 ret = register_ftrace_profiler();
723 ftrace_profile_enabled = 1;
725 ftrace_profile_enabled = 0;
726 unregister_ftrace_profiler();
730 mutex_unlock(&ftrace_profile_lock);
738 ftrace_profile_read(struct file *filp, char __user *ubuf,
739 size_t cnt, loff_t *ppos)
741 char buf[64]; /* big enough to hold a number */
744 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
745 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
748 static const struct file_operations ftrace_profile_fops = {
749 .open = tracing_open_generic,
750 .read = ftrace_profile_read,
751 .write = ftrace_profile_write,
754 /* used to initialize the real stat files */
755 static struct tracer_stat function_stats __initdata = {
757 .stat_start = function_stat_start,
758 .stat_next = function_stat_next,
759 .stat_cmp = function_stat_cmp,
760 .stat_headers = function_stat_headers,
761 .stat_show = function_stat_show
764 static void ftrace_profile_debugfs(struct dentry *d_tracer)
766 struct ftrace_profile_stat *stat;
767 struct dentry *entry;
772 for_each_possible_cpu(cpu) {
773 stat = &per_cpu(ftrace_profile_stats, cpu);
775 /* allocate enough for function name + cpu number */
776 name = kmalloc(32, GFP_KERNEL);
779 * The files created are permanent, if something happens
780 * we still do not free memory.
784 "Could not allocate stat file for cpu %d\n",
788 stat->stat = function_stats;
789 snprintf(name, 32, "function%d", cpu);
790 stat->stat.name = name;
791 ret = register_stat_tracer(&stat->stat);
794 "Could not register function stat for cpu %d\n",
801 entry = debugfs_create_file("function_profile_enabled", 0644,
802 d_tracer, NULL, &ftrace_profile_fops);
804 pr_warning("Could not create debugfs "
805 "'function_profile_enabled' entry\n");
808 #else /* CONFIG_FUNCTION_PROFILER */
809 static void ftrace_profile_debugfs(struct dentry *d_tracer)
812 #endif /* CONFIG_FUNCTION_PROFILER */
814 /* set when tracing only a pid */
815 struct pid *ftrace_pid_trace;
816 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
818 #ifdef CONFIG_DYNAMIC_FTRACE
820 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
821 # error Dynamic ftrace depends on MCOUNT_RECORD
824 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
826 struct ftrace_func_probe {
827 struct hlist_node node;
828 struct ftrace_probe_ops *ops;
836 FTRACE_ENABLE_CALLS = (1 << 0),
837 FTRACE_DISABLE_CALLS = (1 << 1),
838 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
839 FTRACE_ENABLE_MCOUNT = (1 << 3),
840 FTRACE_DISABLE_MCOUNT = (1 << 4),
841 FTRACE_START_FUNC_RET = (1 << 5),
842 FTRACE_STOP_FUNC_RET = (1 << 6),
845 static int ftrace_filtered;
847 static struct dyn_ftrace *ftrace_new_addrs;
849 static DEFINE_MUTEX(ftrace_regex_lock);
852 struct ftrace_page *next;
854 struct dyn_ftrace records[];
857 #define ENTRIES_PER_PAGE \
858 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
860 /* estimate from running different kernels */
861 #define NR_TO_INIT 10000
863 static struct ftrace_page *ftrace_pages_start;
864 static struct ftrace_page *ftrace_pages;
866 static struct dyn_ftrace *ftrace_free_records;
869 * This is a double for. Do not use 'break' to break out of the loop,
870 * you must use a goto.
872 #define do_for_each_ftrace_rec(pg, rec) \
873 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
875 for (_____i = 0; _____i < pg->index; _____i++) { \
876 rec = &pg->records[_____i];
878 #define while_for_each_ftrace_rec() \
882 #ifdef CONFIG_KPROBES
884 static int frozen_record_count;
886 static inline void freeze_record(struct dyn_ftrace *rec)
888 if (!(rec->flags & FTRACE_FL_FROZEN)) {
889 rec->flags |= FTRACE_FL_FROZEN;
890 frozen_record_count++;
894 static inline void unfreeze_record(struct dyn_ftrace *rec)
896 if (rec->flags & FTRACE_FL_FROZEN) {
897 rec->flags &= ~FTRACE_FL_FROZEN;
898 frozen_record_count--;
902 static inline int record_frozen(struct dyn_ftrace *rec)
904 return rec->flags & FTRACE_FL_FROZEN;
907 # define freeze_record(rec) ({ 0; })
908 # define unfreeze_record(rec) ({ 0; })
909 # define record_frozen(rec) ({ 0; })
910 #endif /* CONFIG_KPROBES */
912 static void ftrace_free_rec(struct dyn_ftrace *rec)
914 rec->freelist = ftrace_free_records;
915 ftrace_free_records = rec;
916 rec->flags |= FTRACE_FL_FREE;
919 void ftrace_release(void *start, unsigned long size)
921 struct dyn_ftrace *rec;
922 struct ftrace_page *pg;
923 unsigned long s = (unsigned long)start;
924 unsigned long e = s + size;
926 if (ftrace_disabled || !start)
929 mutex_lock(&ftrace_lock);
930 do_for_each_ftrace_rec(pg, rec) {
931 if ((rec->ip >= s) && (rec->ip < e) &&
932 !(rec->flags & FTRACE_FL_FREE))
933 ftrace_free_rec(rec);
934 } while_for_each_ftrace_rec();
935 mutex_unlock(&ftrace_lock);
938 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
940 struct dyn_ftrace *rec;
942 /* First check for freed records */
943 if (ftrace_free_records) {
944 rec = ftrace_free_records;
946 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
947 FTRACE_WARN_ON_ONCE(1);
948 ftrace_free_records = NULL;
952 ftrace_free_records = rec->freelist;
953 memset(rec, 0, sizeof(*rec));
957 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
958 if (!ftrace_pages->next) {
959 /* allocate another page */
961 (void *)get_zeroed_page(GFP_KERNEL);
962 if (!ftrace_pages->next)
965 ftrace_pages = ftrace_pages->next;
968 return &ftrace_pages->records[ftrace_pages->index++];
971 static struct dyn_ftrace *
972 ftrace_record_ip(unsigned long ip)
974 struct dyn_ftrace *rec;
979 rec = ftrace_alloc_dyn_node(ip);
984 rec->newlist = ftrace_new_addrs;
985 ftrace_new_addrs = rec;
990 static void print_ip_ins(const char *fmt, unsigned char *p)
994 printk(KERN_CONT "%s", fmt);
996 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
997 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1000 static void ftrace_bug(int failed, unsigned long ip)
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on modifying ");
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace failed to modify ");
1012 print_ip_ins(" actual: ", (unsigned char *)ip);
1013 printk(KERN_CONT "\n");
1016 FTRACE_WARN_ON_ONCE(1);
1017 pr_info("ftrace faulted on writing ");
1021 FTRACE_WARN_ON_ONCE(1);
1022 pr_info("ftrace faulted on unknown error ");
1029 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1031 unsigned long ftrace_addr;
1032 unsigned long ip, fl;
1034 ftrace_addr = (unsigned long)FTRACE_ADDR;
1039 * If this record is not to be traced and
1040 * it is not enabled then do nothing.
1042 * If this record is not to be traced and
1043 * it is enabled then disable it.
1046 if (rec->flags & FTRACE_FL_NOTRACE) {
1047 if (rec->flags & FTRACE_FL_ENABLED)
1048 rec->flags &= ~FTRACE_FL_ENABLED;
1052 } else if (ftrace_filtered && enable) {
1057 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
1059 /* Record is filtered and enabled, do nothing */
1060 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
1063 /* Record is not filtered or enabled, do nothing */
1067 /* Record is not filtered but enabled, disable it */
1068 if (fl == FTRACE_FL_ENABLED)
1069 rec->flags &= ~FTRACE_FL_ENABLED;
1071 /* Otherwise record is filtered but not enabled, enable it */
1072 rec->flags |= FTRACE_FL_ENABLED;
1074 /* Disable or not filtered */
1077 /* if record is enabled, do nothing */
1078 if (rec->flags & FTRACE_FL_ENABLED)
1081 rec->flags |= FTRACE_FL_ENABLED;
1085 /* if record is not enabled, do nothing */
1086 if (!(rec->flags & FTRACE_FL_ENABLED))
1089 rec->flags &= ~FTRACE_FL_ENABLED;
1093 if (rec->flags & FTRACE_FL_ENABLED)
1094 return ftrace_make_call(rec, ftrace_addr);
1096 return ftrace_make_nop(NULL, rec, ftrace_addr);
1099 static void ftrace_replace_code(int enable)
1101 struct dyn_ftrace *rec;
1102 struct ftrace_page *pg;
1105 do_for_each_ftrace_rec(pg, rec) {
1107 * Skip over free records, records that have
1108 * failed and not converted.
1110 if (rec->flags & FTRACE_FL_FREE ||
1111 rec->flags & FTRACE_FL_FAILED ||
1112 !(rec->flags & FTRACE_FL_CONVERTED))
1115 /* ignore updates to this record's mcount site */
1116 if (get_kprobe((void *)rec->ip)) {
1120 unfreeze_record(rec);
1123 failed = __ftrace_replace_code(rec, enable);
1125 rec->flags |= FTRACE_FL_FAILED;
1126 if ((system_state == SYSTEM_BOOTING) ||
1127 !core_kernel_text(rec->ip)) {
1128 ftrace_free_rec(rec);
1130 ftrace_bug(failed, rec->ip);
1131 /* Stop processing */
1135 } while_for_each_ftrace_rec();
1139 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1146 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1148 ftrace_bug(ret, ip);
1149 rec->flags |= FTRACE_FL_FAILED;
1156 * archs can override this function if they must do something
1157 * before the modifying code is performed.
1159 int __weak ftrace_arch_code_modify_prepare(void)
1165 * archs can override this function if they must do something
1166 * after the modifying code is performed.
1168 int __weak ftrace_arch_code_modify_post_process(void)
1173 static int __ftrace_modify_code(void *data)
1175 int *command = data;
1177 if (*command & FTRACE_ENABLE_CALLS)
1178 ftrace_replace_code(1);
1179 else if (*command & FTRACE_DISABLE_CALLS)
1180 ftrace_replace_code(0);
1182 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1183 ftrace_update_ftrace_func(ftrace_trace_function);
1185 if (*command & FTRACE_START_FUNC_RET)
1186 ftrace_enable_ftrace_graph_caller();
1187 else if (*command & FTRACE_STOP_FUNC_RET)
1188 ftrace_disable_ftrace_graph_caller();
1193 static void ftrace_run_update_code(int command)
1197 ret = ftrace_arch_code_modify_prepare();
1198 FTRACE_WARN_ON(ret);
1202 stop_machine(__ftrace_modify_code, &command, NULL);
1204 ret = ftrace_arch_code_modify_post_process();
1205 FTRACE_WARN_ON(ret);
1208 static ftrace_func_t saved_ftrace_func;
1209 static int ftrace_start_up;
1211 static void ftrace_startup_enable(int command)
1213 if (saved_ftrace_func != ftrace_trace_function) {
1214 saved_ftrace_func = ftrace_trace_function;
1215 command |= FTRACE_UPDATE_TRACE_FUNC;
1218 if (!command || !ftrace_enabled)
1221 ftrace_run_update_code(command);
1224 static void ftrace_startup(int command)
1226 if (unlikely(ftrace_disabled))
1230 command |= FTRACE_ENABLE_CALLS;
1232 ftrace_startup_enable(command);
1235 static void ftrace_shutdown(int command)
1237 if (unlikely(ftrace_disabled))
1241 if (!ftrace_start_up)
1242 command |= FTRACE_DISABLE_CALLS;
1244 if (saved_ftrace_func != ftrace_trace_function) {
1245 saved_ftrace_func = ftrace_trace_function;
1246 command |= FTRACE_UPDATE_TRACE_FUNC;
1249 if (!command || !ftrace_enabled)
1252 ftrace_run_update_code(command);
1255 static void ftrace_startup_sysctl(void)
1257 int command = FTRACE_ENABLE_MCOUNT;
1259 if (unlikely(ftrace_disabled))
1262 /* Force update next time */
1263 saved_ftrace_func = NULL;
1264 /* ftrace_start_up is true if we want ftrace running */
1265 if (ftrace_start_up)
1266 command |= FTRACE_ENABLE_CALLS;
1268 ftrace_run_update_code(command);
1271 static void ftrace_shutdown_sysctl(void)
1273 int command = FTRACE_DISABLE_MCOUNT;
1275 if (unlikely(ftrace_disabled))
1278 /* ftrace_start_up is true if ftrace is running */
1279 if (ftrace_start_up)
1280 command |= FTRACE_DISABLE_CALLS;
1282 ftrace_run_update_code(command);
1285 static cycle_t ftrace_update_time;
1286 static unsigned long ftrace_update_cnt;
1287 unsigned long ftrace_update_tot_cnt;
1289 static int ftrace_update_code(struct module *mod)
1291 struct dyn_ftrace *p;
1292 cycle_t start, stop;
1294 start = ftrace_now(raw_smp_processor_id());
1295 ftrace_update_cnt = 0;
1297 while (ftrace_new_addrs) {
1299 /* If something went wrong, bail without enabling anything */
1300 if (unlikely(ftrace_disabled))
1303 p = ftrace_new_addrs;
1304 ftrace_new_addrs = p->newlist;
1307 /* convert record (i.e, patch mcount-call with NOP) */
1308 if (ftrace_code_disable(mod, p)) {
1309 p->flags |= FTRACE_FL_CONVERTED;
1310 ftrace_update_cnt++;
1315 stop = ftrace_now(raw_smp_processor_id());
1316 ftrace_update_time = stop - start;
1317 ftrace_update_tot_cnt += ftrace_update_cnt;
1322 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1324 struct ftrace_page *pg;
1328 /* allocate a few pages */
1329 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1330 if (!ftrace_pages_start)
1334 * Allocate a few more pages.
1336 * TODO: have some parser search vmlinux before
1337 * final linking to find all calls to ftrace.
1339 * a) know how many pages to allocate.
1341 * b) set up the table then.
1343 * The dynamic code is still necessary for
1347 pg = ftrace_pages = ftrace_pages_start;
1349 cnt = num_to_init / ENTRIES_PER_PAGE;
1350 pr_info("ftrace: allocating %ld entries in %d pages\n",
1351 num_to_init, cnt + 1);
1353 for (i = 0; i < cnt; i++) {
1354 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1356 /* If we fail, we'll try later anyway */
1367 FTRACE_ITER_FILTER = (1 << 0),
1368 FTRACE_ITER_CONT = (1 << 1),
1369 FTRACE_ITER_NOTRACE = (1 << 2),
1370 FTRACE_ITER_FAILURES = (1 << 3),
1371 FTRACE_ITER_PRINTALL = (1 << 4),
1372 FTRACE_ITER_HASH = (1 << 5),
1375 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1377 struct ftrace_iterator {
1378 struct ftrace_page *pg;
1382 unsigned char buffer[FTRACE_BUFF_MAX+1];
1383 unsigned buffer_idx;
1388 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1390 struct ftrace_iterator *iter = m->private;
1391 struct hlist_node *hnd = v;
1392 struct hlist_head *hhd;
1394 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1399 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1402 hhd = &ftrace_func_hash[iter->hidx];
1404 if (hlist_empty(hhd)) {
1423 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1425 struct ftrace_iterator *iter = m->private;
1428 iter->flags |= FTRACE_ITER_HASH;
1430 return t_hash_next(m, p, pos);
1433 static int t_hash_show(struct seq_file *m, void *v)
1435 struct ftrace_func_probe *rec;
1436 struct hlist_node *hnd = v;
1437 char str[KSYM_SYMBOL_LEN];
1439 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1441 if (rec->ops->print)
1442 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1444 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1445 seq_printf(m, "%s:", str);
1447 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1448 seq_printf(m, "%s", str);
1451 seq_printf(m, ":%p", rec->data);
1458 t_next(struct seq_file *m, void *v, loff_t *pos)
1460 struct ftrace_iterator *iter = m->private;
1461 struct dyn_ftrace *rec = NULL;
1463 if (iter->flags & FTRACE_ITER_HASH)
1464 return t_hash_next(m, v, pos);
1468 if (iter->flags & FTRACE_ITER_PRINTALL)
1472 if (iter->idx >= iter->pg->index) {
1473 if (iter->pg->next) {
1474 iter->pg = iter->pg->next;
1481 rec = &iter->pg->records[iter->idx++];
1482 if ((rec->flags & FTRACE_FL_FREE) ||
1484 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1485 (rec->flags & FTRACE_FL_FAILED)) ||
1487 ((iter->flags & FTRACE_ITER_FAILURES) &&
1488 !(rec->flags & FTRACE_FL_FAILED)) ||
1490 ((iter->flags & FTRACE_ITER_FILTER) &&
1491 !(rec->flags & FTRACE_FL_FILTER)) ||
1493 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1494 !(rec->flags & FTRACE_FL_NOTRACE))) {
1503 static void *t_start(struct seq_file *m, loff_t *pos)
1505 struct ftrace_iterator *iter = m->private;
1508 mutex_lock(&ftrace_lock);
1510 * For set_ftrace_filter reading, if we have the filter
1511 * off, we can short cut and just print out that all
1512 * functions are enabled.
1514 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1516 return t_hash_start(m, pos);
1517 iter->flags |= FTRACE_ITER_PRINTALL;
1522 if (iter->flags & FTRACE_ITER_HASH)
1523 return t_hash_start(m, pos);
1532 p = t_next(m, p, pos);
1535 return t_hash_start(m, pos);
1540 static void t_stop(struct seq_file *m, void *p)
1542 mutex_unlock(&ftrace_lock);
1545 static int t_show(struct seq_file *m, void *v)
1547 struct ftrace_iterator *iter = m->private;
1548 struct dyn_ftrace *rec = v;
1549 char str[KSYM_SYMBOL_LEN];
1551 if (iter->flags & FTRACE_ITER_HASH)
1552 return t_hash_show(m, v);
1554 if (iter->flags & FTRACE_ITER_PRINTALL) {
1555 seq_printf(m, "#### all functions enabled ####\n");
1562 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1564 seq_printf(m, "%s\n", str);
1569 static struct seq_operations show_ftrace_seq_ops = {
1577 ftrace_avail_open(struct inode *inode, struct file *file)
1579 struct ftrace_iterator *iter;
1582 if (unlikely(ftrace_disabled))
1585 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1589 iter->pg = ftrace_pages_start;
1591 ret = seq_open(file, &show_ftrace_seq_ops);
1593 struct seq_file *m = file->private_data;
1603 int ftrace_avail_release(struct inode *inode, struct file *file)
1605 struct seq_file *m = (struct seq_file *)file->private_data;
1606 struct ftrace_iterator *iter = m->private;
1608 seq_release(inode, file);
1615 ftrace_failures_open(struct inode *inode, struct file *file)
1619 struct ftrace_iterator *iter;
1621 ret = ftrace_avail_open(inode, file);
1623 m = (struct seq_file *)file->private_data;
1624 iter = (struct ftrace_iterator *)m->private;
1625 iter->flags = FTRACE_ITER_FAILURES;
1632 static void ftrace_filter_reset(int enable)
1634 struct ftrace_page *pg;
1635 struct dyn_ftrace *rec;
1636 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1638 mutex_lock(&ftrace_lock);
1640 ftrace_filtered = 0;
1641 do_for_each_ftrace_rec(pg, rec) {
1642 if (rec->flags & FTRACE_FL_FAILED)
1644 rec->flags &= ~type;
1645 } while_for_each_ftrace_rec();
1646 mutex_unlock(&ftrace_lock);
1650 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1652 struct ftrace_iterator *iter;
1655 if (unlikely(ftrace_disabled))
1658 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1662 mutex_lock(&ftrace_regex_lock);
1663 if ((file->f_mode & FMODE_WRITE) &&
1664 !(file->f_flags & O_APPEND))
1665 ftrace_filter_reset(enable);
1667 if (file->f_mode & FMODE_READ) {
1668 iter->pg = ftrace_pages_start;
1669 iter->flags = enable ? FTRACE_ITER_FILTER :
1670 FTRACE_ITER_NOTRACE;
1672 ret = seq_open(file, &show_ftrace_seq_ops);
1674 struct seq_file *m = file->private_data;
1679 file->private_data = iter;
1680 mutex_unlock(&ftrace_regex_lock);
1686 ftrace_filter_open(struct inode *inode, struct file *file)
1688 return ftrace_regex_open(inode, file, 1);
1692 ftrace_notrace_open(struct inode *inode, struct file *file)
1694 return ftrace_regex_open(inode, file, 0);
1698 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1702 if (file->f_mode & FMODE_READ)
1703 ret = seq_lseek(file, offset, origin);
1705 file->f_pos = ret = 1;
1718 * (static function - no need for kernel doc)
1720 * Pass in a buffer containing a glob and this function will
1721 * set search to point to the search part of the buffer and
1722 * return the type of search it is (see enum above).
1723 * This does modify buff.
1725 * Returns enum type.
1726 * search returns the pointer to use for comparison.
1727 * not returns 1 if buff started with a '!'
1731 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1733 int type = MATCH_FULL;
1736 if (buff[0] == '!') {
1745 for (i = 0; i < len; i++) {
1746 if (buff[i] == '*') {
1749 type = MATCH_END_ONLY;
1751 if (type == MATCH_END_ONLY)
1752 type = MATCH_MIDDLE_ONLY;
1754 type = MATCH_FRONT_ONLY;
1764 static int ftrace_match(char *str, char *regex, int len, int type)
1771 if (strcmp(str, regex) == 0)
1774 case MATCH_FRONT_ONLY:
1775 if (strncmp(str, regex, len) == 0)
1778 case MATCH_MIDDLE_ONLY:
1779 if (strstr(str, regex))
1782 case MATCH_END_ONLY:
1783 ptr = strstr(str, regex);
1784 if (ptr && (ptr[len] == 0))
1793 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1795 char str[KSYM_SYMBOL_LEN];
1797 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1798 return ftrace_match(str, regex, len, type);
1801 static void ftrace_match_records(char *buff, int len, int enable)
1803 unsigned int search_len;
1804 struct ftrace_page *pg;
1805 struct dyn_ftrace *rec;
1811 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1812 type = ftrace_setup_glob(buff, len, &search, ¬);
1814 search_len = strlen(search);
1816 mutex_lock(&ftrace_lock);
1817 do_for_each_ftrace_rec(pg, rec) {
1819 if (rec->flags & FTRACE_FL_FAILED)
1822 if (ftrace_match_record(rec, search, search_len, type)) {
1824 rec->flags &= ~flag;
1829 * Only enable filtering if we have a function that
1832 if (enable && (rec->flags & FTRACE_FL_FILTER))
1833 ftrace_filtered = 1;
1834 } while_for_each_ftrace_rec();
1835 mutex_unlock(&ftrace_lock);
1839 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1840 char *regex, int len, int type)
1842 char str[KSYM_SYMBOL_LEN];
1845 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1847 if (!modname || strcmp(modname, mod))
1850 /* blank search means to match all funcs in the mod */
1852 return ftrace_match(str, regex, len, type);
1857 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1859 unsigned search_len = 0;
1860 struct ftrace_page *pg;
1861 struct dyn_ftrace *rec;
1862 int type = MATCH_FULL;
1863 char *search = buff;
1867 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1869 /* blank or '*' mean the same */
1870 if (strcmp(buff, "*") == 0)
1873 /* handle the case of 'dont filter this module' */
1874 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1880 type = ftrace_setup_glob(buff, strlen(buff), &search, ¬);
1881 search_len = strlen(search);
1884 mutex_lock(&ftrace_lock);
1885 do_for_each_ftrace_rec(pg, rec) {
1887 if (rec->flags & FTRACE_FL_FAILED)
1890 if (ftrace_match_module_record(rec, mod,
1891 search, search_len, type)) {
1893 rec->flags &= ~flag;
1897 if (enable && (rec->flags & FTRACE_FL_FILTER))
1898 ftrace_filtered = 1;
1900 } while_for_each_ftrace_rec();
1901 mutex_unlock(&ftrace_lock);
1905 * We register the module command as a template to show others how
1906 * to register the a command as well.
1910 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1915 * cmd == 'mod' because we only registered this func
1916 * for the 'mod' ftrace_func_command.
1917 * But if you register one func with multiple commands,
1918 * you can tell which command was used by the cmd
1922 /* we must have a module name */
1926 mod = strsep(¶m, ":");
1930 ftrace_match_module_records(func, mod, enable);
1934 static struct ftrace_func_command ftrace_mod_cmd = {
1936 .func = ftrace_mod_callback,
1939 static int __init ftrace_mod_cmd_init(void)
1941 return register_ftrace_command(&ftrace_mod_cmd);
1943 device_initcall(ftrace_mod_cmd_init);
1946 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1948 struct ftrace_func_probe *entry;
1949 struct hlist_head *hhd;
1950 struct hlist_node *n;
1954 key = hash_long(ip, FTRACE_HASH_BITS);
1956 hhd = &ftrace_func_hash[key];
1958 if (hlist_empty(hhd))
1962 * Disable preemption for these calls to prevent a RCU grace
1963 * period. This syncs the hash iteration and freeing of items
1964 * on the hash. rcu_read_lock is too dangerous here.
1966 resched = ftrace_preempt_disable();
1967 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1968 if (entry->ip == ip)
1969 entry->ops->func(ip, parent_ip, &entry->data);
1971 ftrace_preempt_enable(resched);
1974 static struct ftrace_ops trace_probe_ops __read_mostly =
1976 .func = function_trace_probe_call,
1979 static int ftrace_probe_registered;
1981 static void __enable_ftrace_function_probe(void)
1985 if (ftrace_probe_registered)
1988 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1989 struct hlist_head *hhd = &ftrace_func_hash[i];
1993 /* Nothing registered? */
1994 if (i == FTRACE_FUNC_HASHSIZE)
1997 __register_ftrace_function(&trace_probe_ops);
1999 ftrace_probe_registered = 1;
2002 static void __disable_ftrace_function_probe(void)
2006 if (!ftrace_probe_registered)
2009 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2010 struct hlist_head *hhd = &ftrace_func_hash[i];
2015 /* no more funcs left */
2016 __unregister_ftrace_function(&trace_probe_ops);
2018 ftrace_probe_registered = 0;
2022 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2024 struct ftrace_func_probe *entry =
2025 container_of(rhp, struct ftrace_func_probe, rcu);
2027 if (entry->ops->free)
2028 entry->ops->free(&entry->data);
2034 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2037 struct ftrace_func_probe *entry;
2038 struct ftrace_page *pg;
2039 struct dyn_ftrace *rec;
2045 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2046 len = strlen(search);
2048 /* we do not support '!' for function probes */
2052 mutex_lock(&ftrace_lock);
2053 do_for_each_ftrace_rec(pg, rec) {
2055 if (rec->flags & FTRACE_FL_FAILED)
2058 if (!ftrace_match_record(rec, search, len, type))
2061 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2063 /* If we did not process any, then return error */
2074 * The caller might want to do something special
2075 * for each function we find. We call the callback
2076 * to give the caller an opportunity to do so.
2078 if (ops->callback) {
2079 if (ops->callback(rec->ip, &entry->data) < 0) {
2080 /* caller does not like this func */
2087 entry->ip = rec->ip;
2089 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2090 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2092 } while_for_each_ftrace_rec();
2093 __enable_ftrace_function_probe();
2096 mutex_unlock(&ftrace_lock);
2102 PROBE_TEST_FUNC = 1,
2107 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2108 void *data, int flags)
2110 struct ftrace_func_probe *entry;
2111 struct hlist_node *n, *tmp;
2112 char str[KSYM_SYMBOL_LEN];
2113 int type = MATCH_FULL;
2117 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2122 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2123 len = strlen(search);
2125 /* we do not support '!' for function probes */
2130 mutex_lock(&ftrace_lock);
2131 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2132 struct hlist_head *hhd = &ftrace_func_hash[i];
2134 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2136 /* break up if statements for readability */
2137 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2140 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2143 /* do this last, since it is the most expensive */
2145 kallsyms_lookup(entry->ip, NULL, NULL,
2147 if (!ftrace_match(str, glob, len, type))
2151 hlist_del(&entry->node);
2152 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2155 __disable_ftrace_function_probe();
2156 mutex_unlock(&ftrace_lock);
2160 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2163 __unregister_ftrace_function_probe(glob, ops, data,
2164 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2168 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2170 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2173 void unregister_ftrace_function_probe_all(char *glob)
2175 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2178 static LIST_HEAD(ftrace_commands);
2179 static DEFINE_MUTEX(ftrace_cmd_mutex);
2181 int register_ftrace_command(struct ftrace_func_command *cmd)
2183 struct ftrace_func_command *p;
2186 mutex_lock(&ftrace_cmd_mutex);
2187 list_for_each_entry(p, &ftrace_commands, list) {
2188 if (strcmp(cmd->name, p->name) == 0) {
2193 list_add(&cmd->list, &ftrace_commands);
2195 mutex_unlock(&ftrace_cmd_mutex);
2200 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2202 struct ftrace_func_command *p, *n;
2205 mutex_lock(&ftrace_cmd_mutex);
2206 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2207 if (strcmp(cmd->name, p->name) == 0) {
2209 list_del_init(&p->list);
2214 mutex_unlock(&ftrace_cmd_mutex);
2219 static int ftrace_process_regex(char *buff, int len, int enable)
2221 char *func, *command, *next = buff;
2222 struct ftrace_func_command *p;
2225 func = strsep(&next, ":");
2228 ftrace_match_records(func, len, enable);
2234 command = strsep(&next, ":");
2236 mutex_lock(&ftrace_cmd_mutex);
2237 list_for_each_entry(p, &ftrace_commands, list) {
2238 if (strcmp(p->name, command) == 0) {
2239 ret = p->func(func, command, next, enable);
2244 mutex_unlock(&ftrace_cmd_mutex);
2250 ftrace_regex_write(struct file *file, const char __user *ubuf,
2251 size_t cnt, loff_t *ppos, int enable)
2253 struct ftrace_iterator *iter;
2258 if (!cnt || cnt < 0)
2261 mutex_lock(&ftrace_regex_lock);
2263 if (file->f_mode & FMODE_READ) {
2264 struct seq_file *m = file->private_data;
2267 iter = file->private_data;
2270 iter->flags &= ~FTRACE_ITER_CONT;
2271 iter->buffer_idx = 0;
2274 ret = get_user(ch, ubuf++);
2280 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2281 /* skip white space */
2282 while (cnt && isspace(ch)) {
2283 ret = get_user(ch, ubuf++);
2291 file->f_pos += read;
2296 iter->buffer_idx = 0;
2299 while (cnt && !isspace(ch)) {
2300 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2301 iter->buffer[iter->buffer_idx++] = ch;
2306 ret = get_user(ch, ubuf++);
2315 iter->buffer[iter->buffer_idx] = 0;
2316 ret = ftrace_process_regex(iter->buffer,
2317 iter->buffer_idx, enable);
2320 iter->buffer_idx = 0;
2322 iter->flags |= FTRACE_ITER_CONT;
2325 file->f_pos += read;
2329 mutex_unlock(&ftrace_regex_lock);
2335 ftrace_filter_write(struct file *file, const char __user *ubuf,
2336 size_t cnt, loff_t *ppos)
2338 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2342 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2343 size_t cnt, loff_t *ppos)
2345 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2349 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2351 if (unlikely(ftrace_disabled))
2354 mutex_lock(&ftrace_regex_lock);
2356 ftrace_filter_reset(enable);
2358 ftrace_match_records(buf, len, enable);
2359 mutex_unlock(&ftrace_regex_lock);
2363 * ftrace_set_filter - set a function to filter on in ftrace
2364 * @buf - the string that holds the function filter text.
2365 * @len - the length of the string.
2366 * @reset - non zero to reset all filters before applying this filter.
2368 * Filters denote which functions should be enabled when tracing is enabled.
2369 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2371 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2373 ftrace_set_regex(buf, len, reset, 1);
2377 * ftrace_set_notrace - set a function to not trace in ftrace
2378 * @buf - the string that holds the function notrace text.
2379 * @len - the length of the string.
2380 * @reset - non zero to reset all filters before applying this filter.
2382 * Notrace Filters denote which functions should not be enabled when tracing
2383 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2386 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2388 ftrace_set_regex(buf, len, reset, 0);
2392 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2394 struct seq_file *m = (struct seq_file *)file->private_data;
2395 struct ftrace_iterator *iter;
2397 mutex_lock(&ftrace_regex_lock);
2398 if (file->f_mode & FMODE_READ) {
2401 seq_release(inode, file);
2403 iter = file->private_data;
2405 if (iter->buffer_idx) {
2407 iter->buffer[iter->buffer_idx] = 0;
2408 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
2411 mutex_lock(&ftrace_lock);
2412 if (ftrace_start_up && ftrace_enabled)
2413 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2414 mutex_unlock(&ftrace_lock);
2417 mutex_unlock(&ftrace_regex_lock);
2422 ftrace_filter_release(struct inode *inode, struct file *file)
2424 return ftrace_regex_release(inode, file, 1);
2428 ftrace_notrace_release(struct inode *inode, struct file *file)
2430 return ftrace_regex_release(inode, file, 0);
2433 static const struct file_operations ftrace_avail_fops = {
2434 .open = ftrace_avail_open,
2436 .llseek = seq_lseek,
2437 .release = ftrace_avail_release,
2440 static const struct file_operations ftrace_failures_fops = {
2441 .open = ftrace_failures_open,
2443 .llseek = seq_lseek,
2444 .release = ftrace_avail_release,
2447 static const struct file_operations ftrace_filter_fops = {
2448 .open = ftrace_filter_open,
2450 .write = ftrace_filter_write,
2451 .llseek = ftrace_regex_lseek,
2452 .release = ftrace_filter_release,
2455 static const struct file_operations ftrace_notrace_fops = {
2456 .open = ftrace_notrace_open,
2458 .write = ftrace_notrace_write,
2459 .llseek = ftrace_regex_lseek,
2460 .release = ftrace_notrace_release,
2463 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2465 static DEFINE_MUTEX(graph_lock);
2467 int ftrace_graph_count;
2468 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2471 g_next(struct seq_file *m, void *v, loff_t *pos)
2473 unsigned long *array = m->private;
2478 if (index >= ftrace_graph_count)
2481 return &array[index];
2484 static void *g_start(struct seq_file *m, loff_t *pos)
2488 mutex_lock(&graph_lock);
2490 /* Nothing, tell g_show to print all functions are enabled */
2491 if (!ftrace_graph_count && !*pos)
2494 p = g_next(m, p, pos);
2499 static void g_stop(struct seq_file *m, void *p)
2501 mutex_unlock(&graph_lock);
2504 static int g_show(struct seq_file *m, void *v)
2506 unsigned long *ptr = v;
2507 char str[KSYM_SYMBOL_LEN];
2512 if (ptr == (unsigned long *)1) {
2513 seq_printf(m, "#### all functions enabled ####\n");
2517 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2519 seq_printf(m, "%s\n", str);
2524 static struct seq_operations ftrace_graph_seq_ops = {
2532 ftrace_graph_open(struct inode *inode, struct file *file)
2536 if (unlikely(ftrace_disabled))
2539 mutex_lock(&graph_lock);
2540 if ((file->f_mode & FMODE_WRITE) &&
2541 !(file->f_flags & O_APPEND)) {
2542 ftrace_graph_count = 0;
2543 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2546 if (file->f_mode & FMODE_READ) {
2547 ret = seq_open(file, &ftrace_graph_seq_ops);
2549 struct seq_file *m = file->private_data;
2550 m->private = ftrace_graph_funcs;
2553 file->private_data = ftrace_graph_funcs;
2554 mutex_unlock(&graph_lock);
2560 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2562 struct dyn_ftrace *rec;
2563 struct ftrace_page *pg;
2571 if (ftrace_disabled)
2575 type = ftrace_setup_glob(buffer, strlen(buffer), &search, ¬);
2579 search_len = strlen(search);
2581 mutex_lock(&ftrace_lock);
2582 do_for_each_ftrace_rec(pg, rec) {
2584 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2587 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2590 if (ftrace_match_record(rec, search, search_len, type)) {
2591 /* ensure it is not already in the array */
2593 for (i = 0; i < *idx; i++)
2594 if (array[i] == rec->ip) {
2599 array[(*idx)++] = rec->ip;
2603 } while_for_each_ftrace_rec();
2605 mutex_unlock(&ftrace_lock);
2607 return found ? 0 : -EINVAL;
2611 ftrace_graph_write(struct file *file, const char __user *ubuf,
2612 size_t cnt, loff_t *ppos)
2614 unsigned char buffer[FTRACE_BUFF_MAX+1];
2615 unsigned long *array;
2621 if (!cnt || cnt < 0)
2624 mutex_lock(&graph_lock);
2626 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2631 if (file->f_mode & FMODE_READ) {
2632 struct seq_file *m = file->private_data;
2635 array = file->private_data;
2637 ret = get_user(ch, ubuf++);
2643 /* skip white space */
2644 while (cnt && isspace(ch)) {
2645 ret = get_user(ch, ubuf++);
2658 while (cnt && !isspace(ch)) {
2659 if (index < FTRACE_BUFF_MAX)
2660 buffer[index++] = ch;
2665 ret = get_user(ch, ubuf++);
2673 /* we allow only one expression at a time */
2674 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2678 file->f_pos += read;
2682 mutex_unlock(&graph_lock);
2687 static const struct file_operations ftrace_graph_fops = {
2688 .open = ftrace_graph_open,
2690 .write = ftrace_graph_write,
2692 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2694 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2696 struct dentry *entry;
2698 entry = debugfs_create_file("available_filter_functions", 0444,
2699 d_tracer, NULL, &ftrace_avail_fops);
2701 pr_warning("Could not create debugfs "
2702 "'available_filter_functions' entry\n");
2704 entry = debugfs_create_file("failures", 0444,
2705 d_tracer, NULL, &ftrace_failures_fops);
2707 pr_warning("Could not create debugfs 'failures' entry\n");
2709 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
2710 NULL, &ftrace_filter_fops);
2712 pr_warning("Could not create debugfs "
2713 "'set_ftrace_filter' entry\n");
2715 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
2716 NULL, &ftrace_notrace_fops);
2718 pr_warning("Could not create debugfs "
2719 "'set_ftrace_notrace' entry\n");
2721 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2722 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
2724 &ftrace_graph_fops);
2726 pr_warning("Could not create debugfs "
2727 "'set_graph_function' entry\n");
2728 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2733 static int ftrace_convert_nops(struct module *mod,
2734 unsigned long *start,
2739 unsigned long flags;
2741 mutex_lock(&ftrace_lock);
2744 addr = ftrace_call_adjust(*p++);
2746 * Some architecture linkers will pad between
2747 * the different mcount_loc sections of different
2748 * object files to satisfy alignments.
2749 * Skip any NULL pointers.
2753 ftrace_record_ip(addr);
2756 /* disable interrupts to prevent kstop machine */
2757 local_irq_save(flags);
2758 ftrace_update_code(mod);
2759 local_irq_restore(flags);
2760 mutex_unlock(&ftrace_lock);
2765 void ftrace_init_module(struct module *mod,
2766 unsigned long *start, unsigned long *end)
2768 if (ftrace_disabled || start == end)
2770 ftrace_convert_nops(mod, start, end);
2773 extern unsigned long __start_mcount_loc[];
2774 extern unsigned long __stop_mcount_loc[];
2776 void __init ftrace_init(void)
2778 unsigned long count, addr, flags;
2781 /* Keep the ftrace pointer to the stub */
2782 addr = (unsigned long)ftrace_stub;
2784 local_irq_save(flags);
2785 ftrace_dyn_arch_init(&addr);
2786 local_irq_restore(flags);
2788 /* ftrace_dyn_arch_init places the return code in addr */
2792 count = __stop_mcount_loc - __start_mcount_loc;
2794 ret = ftrace_dyn_table_alloc(count);
2798 last_ftrace_enabled = ftrace_enabled = 1;
2800 ret = ftrace_convert_nops(NULL,
2806 ftrace_disabled = 1;
2811 static int __init ftrace_nodyn_init(void)
2816 device_initcall(ftrace_nodyn_init);
2818 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2819 static inline void ftrace_startup_enable(int command) { }
2820 /* Keep as macros so we do not need to define the commands */
2821 # define ftrace_startup(command) do { } while (0)
2822 # define ftrace_shutdown(command) do { } while (0)
2823 # define ftrace_startup_sysctl() do { } while (0)
2824 # define ftrace_shutdown_sysctl() do { } while (0)
2825 #endif /* CONFIG_DYNAMIC_FTRACE */
2828 ftrace_pid_read(struct file *file, char __user *ubuf,
2829 size_t cnt, loff_t *ppos)
2834 if (ftrace_pid_trace == ftrace_swapper_pid)
2835 r = sprintf(buf, "swapper tasks\n");
2836 else if (ftrace_pid_trace)
2837 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2839 r = sprintf(buf, "no pid\n");
2841 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2844 static void clear_ftrace_swapper(void)
2846 struct task_struct *p;
2850 for_each_online_cpu(cpu) {
2852 clear_tsk_trace_trace(p);
2857 static void set_ftrace_swapper(void)
2859 struct task_struct *p;
2863 for_each_online_cpu(cpu) {
2865 set_tsk_trace_trace(p);
2870 static void clear_ftrace_pid(struct pid *pid)
2872 struct task_struct *p;
2875 do_each_pid_task(pid, PIDTYPE_PID, p) {
2876 clear_tsk_trace_trace(p);
2877 } while_each_pid_task(pid, PIDTYPE_PID, p);
2883 static void set_ftrace_pid(struct pid *pid)
2885 struct task_struct *p;
2888 do_each_pid_task(pid, PIDTYPE_PID, p) {
2889 set_tsk_trace_trace(p);
2890 } while_each_pid_task(pid, PIDTYPE_PID, p);
2894 static void clear_ftrace_pid_task(struct pid **pid)
2896 if (*pid == ftrace_swapper_pid)
2897 clear_ftrace_swapper();
2899 clear_ftrace_pid(*pid);
2904 static void set_ftrace_pid_task(struct pid *pid)
2906 if (pid == ftrace_swapper_pid)
2907 set_ftrace_swapper();
2909 set_ftrace_pid(pid);
2913 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2914 size_t cnt, loff_t *ppos)
2921 if (cnt >= sizeof(buf))
2924 if (copy_from_user(&buf, ubuf, cnt))
2929 ret = strict_strtol(buf, 10, &val);
2933 mutex_lock(&ftrace_lock);
2935 /* disable pid tracing */
2936 if (!ftrace_pid_trace)
2939 clear_ftrace_pid_task(&ftrace_pid_trace);
2942 /* swapper task is special */
2944 pid = ftrace_swapper_pid;
2945 if (pid == ftrace_pid_trace)
2948 pid = find_get_pid(val);
2950 if (pid == ftrace_pid_trace) {
2956 if (ftrace_pid_trace)
2957 clear_ftrace_pid_task(&ftrace_pid_trace);
2962 ftrace_pid_trace = pid;
2964 set_ftrace_pid_task(ftrace_pid_trace);
2967 /* update the function call */
2968 ftrace_update_pid_func();
2969 ftrace_startup_enable(0);
2972 mutex_unlock(&ftrace_lock);
2977 static const struct file_operations ftrace_pid_fops = {
2978 .read = ftrace_pid_read,
2979 .write = ftrace_pid_write,
2982 static __init int ftrace_init_debugfs(void)
2984 struct dentry *d_tracer;
2985 struct dentry *entry;
2987 d_tracer = tracing_init_dentry();
2991 ftrace_init_dyn_debugfs(d_tracer);
2993 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2994 NULL, &ftrace_pid_fops);
2996 pr_warning("Could not create debugfs "
2997 "'set_ftrace_pid' entry\n");
2999 ftrace_profile_debugfs(d_tracer);
3003 fs_initcall(ftrace_init_debugfs);
3006 * ftrace_kill - kill ftrace
3008 * This function should be used by panic code. It stops ftrace
3009 * but in a not so nice way. If you need to simply kill ftrace
3010 * from a non-atomic section, use ftrace_kill.
3012 void ftrace_kill(void)
3014 ftrace_disabled = 1;
3016 clear_ftrace_function();
3020 * register_ftrace_function - register a function for profiling
3021 * @ops - ops structure that holds the function for profiling.
3023 * Register a function to be called by all functions in the
3026 * Note: @ops->func and all the functions it calls must be labeled
3027 * with "notrace", otherwise it will go into a
3030 int register_ftrace_function(struct ftrace_ops *ops)
3034 if (unlikely(ftrace_disabled))
3037 mutex_lock(&ftrace_lock);
3039 ret = __register_ftrace_function(ops);
3042 mutex_unlock(&ftrace_lock);
3047 * unregister_ftrace_function - unregister a function for profiling.
3048 * @ops - ops structure that holds the function to unregister
3050 * Unregister a function that was added to be called by ftrace profiling.
3052 int unregister_ftrace_function(struct ftrace_ops *ops)
3056 mutex_lock(&ftrace_lock);
3057 ret = __unregister_ftrace_function(ops);
3059 mutex_unlock(&ftrace_lock);
3065 ftrace_enable_sysctl(struct ctl_table *table, int write,
3066 struct file *file, void __user *buffer, size_t *lenp,
3071 if (unlikely(ftrace_disabled))
3074 mutex_lock(&ftrace_lock);
3076 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
3078 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
3081 last_ftrace_enabled = ftrace_enabled;
3083 if (ftrace_enabled) {
3085 ftrace_startup_sysctl();
3087 /* we are starting ftrace again */
3088 if (ftrace_list != &ftrace_list_end) {
3089 if (ftrace_list->next == &ftrace_list_end)
3090 ftrace_trace_function = ftrace_list->func;
3092 ftrace_trace_function = ftrace_list_func;
3096 /* stopping ftrace calls (just send to ftrace_stub) */
3097 ftrace_trace_function = ftrace_stub;
3099 ftrace_shutdown_sysctl();
3103 mutex_unlock(&ftrace_lock);
3107 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3109 static atomic_t ftrace_graph_active;
3110 static struct notifier_block ftrace_suspend_notifier;
3112 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3117 /* The callbacks that hook a function */
3118 trace_func_graph_ret_t ftrace_graph_return =
3119 (trace_func_graph_ret_t)ftrace_stub;
3120 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3122 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3123 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3127 unsigned long flags;
3128 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3129 struct task_struct *g, *t;
3131 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3132 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3133 * sizeof(struct ftrace_ret_stack),
3135 if (!ret_stack_list[i]) {
3143 read_lock_irqsave(&tasklist_lock, flags);
3144 do_each_thread(g, t) {
3150 if (t->ret_stack == NULL) {
3151 t->curr_ret_stack = -1;
3152 /* Make sure IRQs see the -1 first: */
3154 t->ret_stack = ret_stack_list[start++];
3155 atomic_set(&t->tracing_graph_pause, 0);
3156 atomic_set(&t->trace_overrun, 0);
3158 } while_each_thread(g, t);
3161 read_unlock_irqrestore(&tasklist_lock, flags);
3163 for (i = start; i < end; i++)
3164 kfree(ret_stack_list[i]);
3169 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3170 struct task_struct *next)
3172 unsigned long long timestamp;
3176 * Does the user want to count the time a function was asleep.
3177 * If so, do not update the time stamps.
3179 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3182 timestamp = trace_clock_local();
3184 prev->ftrace_timestamp = timestamp;
3186 /* only process tasks that we timestamped */
3187 if (!next->ftrace_timestamp)
3191 * Update all the counters in next to make up for the
3192 * time next was sleeping.
3194 timestamp -= next->ftrace_timestamp;
3196 for (index = next->curr_ret_stack; index >= 0; index--)
3197 next->ret_stack[index].calltime += timestamp;
3200 /* Allocate a return stack for each task */
3201 static int start_graph_tracing(void)
3203 struct ftrace_ret_stack **ret_stack_list;
3206 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3207 sizeof(struct ftrace_ret_stack *),
3210 if (!ret_stack_list)
3213 /* The cpu_boot init_task->ret_stack will never be freed */
3214 for_each_online_cpu(cpu)
3215 ftrace_graph_init_task(idle_task(cpu));
3218 ret = alloc_retstack_tasklist(ret_stack_list);
3219 } while (ret == -EAGAIN);
3222 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3224 pr_info("ftrace_graph: Couldn't activate tracepoint"
3225 " probe to kernel_sched_switch\n");
3228 kfree(ret_stack_list);
3233 * Hibernation protection.
3234 * The state of the current task is too much unstable during
3235 * suspend/restore to disk. We want to protect against that.
3238 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3242 case PM_HIBERNATION_PREPARE:
3243 pause_graph_tracing();
3246 case PM_POST_HIBERNATION:
3247 unpause_graph_tracing();
3253 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3254 trace_func_graph_ent_t entryfunc)
3258 mutex_lock(&ftrace_lock);
3260 /* we currently allow only one tracer registered at a time */
3261 if (atomic_read(&ftrace_graph_active)) {
3266 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3267 register_pm_notifier(&ftrace_suspend_notifier);
3269 atomic_inc(&ftrace_graph_active);
3270 ret = start_graph_tracing();
3272 atomic_dec(&ftrace_graph_active);
3276 ftrace_graph_return = retfunc;
3277 ftrace_graph_entry = entryfunc;
3279 ftrace_startup(FTRACE_START_FUNC_RET);
3282 mutex_unlock(&ftrace_lock);
3286 void unregister_ftrace_graph(void)
3288 mutex_lock(&ftrace_lock);
3290 atomic_dec(&ftrace_graph_active);
3291 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3292 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3293 ftrace_graph_entry = ftrace_graph_entry_stub;
3294 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3295 unregister_pm_notifier(&ftrace_suspend_notifier);
3297 mutex_unlock(&ftrace_lock);
3300 /* Allocate a return stack for newly created task */
3301 void ftrace_graph_init_task(struct task_struct *t)
3303 if (atomic_read(&ftrace_graph_active)) {
3304 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3305 * sizeof(struct ftrace_ret_stack),
3309 t->curr_ret_stack = -1;
3310 atomic_set(&t->tracing_graph_pause, 0);
3311 atomic_set(&t->trace_overrun, 0);
3312 t->ftrace_timestamp = 0;
3314 t->ret_stack = NULL;
3317 void ftrace_graph_exit_task(struct task_struct *t)
3319 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3321 t->ret_stack = NULL;
3322 /* NULL must become visible to IRQs before we free it: */
3328 void ftrace_graph_stop(void)