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)) {
933 * rec->ip is changed in ftrace_free_rec()
934 * It should not between s and e if record was freed.
936 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
937 ftrace_free_rec(rec);
939 } while_for_each_ftrace_rec();
940 mutex_unlock(&ftrace_lock);
943 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
945 struct dyn_ftrace *rec;
947 /* First check for freed records */
948 if (ftrace_free_records) {
949 rec = ftrace_free_records;
951 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
952 FTRACE_WARN_ON_ONCE(1);
953 ftrace_free_records = NULL;
957 ftrace_free_records = rec->freelist;
958 memset(rec, 0, sizeof(*rec));
962 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
963 if (!ftrace_pages->next) {
964 /* allocate another page */
966 (void *)get_zeroed_page(GFP_KERNEL);
967 if (!ftrace_pages->next)
970 ftrace_pages = ftrace_pages->next;
973 return &ftrace_pages->records[ftrace_pages->index++];
976 static struct dyn_ftrace *
977 ftrace_record_ip(unsigned long ip)
979 struct dyn_ftrace *rec;
984 rec = ftrace_alloc_dyn_node(ip);
989 rec->newlist = ftrace_new_addrs;
990 ftrace_new_addrs = rec;
995 static void print_ip_ins(const char *fmt, unsigned char *p)
999 printk(KERN_CONT "%s", fmt);
1001 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1002 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1005 static void ftrace_bug(int failed, unsigned long ip)
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace faulted on modifying ");
1014 FTRACE_WARN_ON_ONCE(1);
1015 pr_info("ftrace failed to modify ");
1017 print_ip_ins(" actual: ", (unsigned char *)ip);
1018 printk(KERN_CONT "\n");
1021 FTRACE_WARN_ON_ONCE(1);
1022 pr_info("ftrace faulted on writing ");
1026 FTRACE_WARN_ON_ONCE(1);
1027 pr_info("ftrace faulted on unknown error ");
1034 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1036 unsigned long ftrace_addr;
1037 unsigned long ip, fl;
1039 ftrace_addr = (unsigned long)FTRACE_ADDR;
1044 * If this record is not to be traced and
1045 * it is not enabled then do nothing.
1047 * If this record is not to be traced and
1048 * it is enabled then disable it.
1051 if (rec->flags & FTRACE_FL_NOTRACE) {
1052 if (rec->flags & FTRACE_FL_ENABLED)
1053 rec->flags &= ~FTRACE_FL_ENABLED;
1057 } else if (ftrace_filtered && enable) {
1062 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
1064 /* Record is filtered and enabled, do nothing */
1065 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
1068 /* Record is not filtered or enabled, do nothing */
1072 /* Record is not filtered but enabled, disable it */
1073 if (fl == FTRACE_FL_ENABLED)
1074 rec->flags &= ~FTRACE_FL_ENABLED;
1076 /* Otherwise record is filtered but not enabled, enable it */
1077 rec->flags |= FTRACE_FL_ENABLED;
1079 /* Disable or not filtered */
1082 /* if record is enabled, do nothing */
1083 if (rec->flags & FTRACE_FL_ENABLED)
1086 rec->flags |= FTRACE_FL_ENABLED;
1090 /* if record is not enabled, do nothing */
1091 if (!(rec->flags & FTRACE_FL_ENABLED))
1094 rec->flags &= ~FTRACE_FL_ENABLED;
1098 if (rec->flags & FTRACE_FL_ENABLED)
1099 return ftrace_make_call(rec, ftrace_addr);
1101 return ftrace_make_nop(NULL, rec, ftrace_addr);
1104 static void ftrace_replace_code(int enable)
1106 struct dyn_ftrace *rec;
1107 struct ftrace_page *pg;
1110 do_for_each_ftrace_rec(pg, rec) {
1112 * Skip over free records, records that have
1113 * failed and not converted.
1115 if (rec->flags & FTRACE_FL_FREE ||
1116 rec->flags & FTRACE_FL_FAILED ||
1117 !(rec->flags & FTRACE_FL_CONVERTED))
1120 /* ignore updates to this record's mcount site */
1121 if (get_kprobe((void *)rec->ip)) {
1125 unfreeze_record(rec);
1128 failed = __ftrace_replace_code(rec, enable);
1130 rec->flags |= FTRACE_FL_FAILED;
1131 if ((system_state == SYSTEM_BOOTING) ||
1132 !core_kernel_text(rec->ip)) {
1133 ftrace_free_rec(rec);
1135 ftrace_bug(failed, rec->ip);
1136 /* Stop processing */
1140 } while_for_each_ftrace_rec();
1144 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1151 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1153 ftrace_bug(ret, ip);
1154 rec->flags |= FTRACE_FL_FAILED;
1161 * archs can override this function if they must do something
1162 * before the modifying code is performed.
1164 int __weak ftrace_arch_code_modify_prepare(void)
1170 * archs can override this function if they must do something
1171 * after the modifying code is performed.
1173 int __weak ftrace_arch_code_modify_post_process(void)
1178 static int __ftrace_modify_code(void *data)
1180 int *command = data;
1182 if (*command & FTRACE_ENABLE_CALLS)
1183 ftrace_replace_code(1);
1184 else if (*command & FTRACE_DISABLE_CALLS)
1185 ftrace_replace_code(0);
1187 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1188 ftrace_update_ftrace_func(ftrace_trace_function);
1190 if (*command & FTRACE_START_FUNC_RET)
1191 ftrace_enable_ftrace_graph_caller();
1192 else if (*command & FTRACE_STOP_FUNC_RET)
1193 ftrace_disable_ftrace_graph_caller();
1198 static void ftrace_run_update_code(int command)
1202 ret = ftrace_arch_code_modify_prepare();
1203 FTRACE_WARN_ON(ret);
1207 stop_machine(__ftrace_modify_code, &command, NULL);
1209 ret = ftrace_arch_code_modify_post_process();
1210 FTRACE_WARN_ON(ret);
1213 static ftrace_func_t saved_ftrace_func;
1214 static int ftrace_start_up;
1216 static void ftrace_startup_enable(int command)
1218 if (saved_ftrace_func != ftrace_trace_function) {
1219 saved_ftrace_func = ftrace_trace_function;
1220 command |= FTRACE_UPDATE_TRACE_FUNC;
1223 if (!command || !ftrace_enabled)
1226 ftrace_run_update_code(command);
1229 static void ftrace_startup(int command)
1231 if (unlikely(ftrace_disabled))
1235 command |= FTRACE_ENABLE_CALLS;
1237 ftrace_startup_enable(command);
1240 static void ftrace_shutdown(int command)
1242 if (unlikely(ftrace_disabled))
1246 if (!ftrace_start_up)
1247 command |= FTRACE_DISABLE_CALLS;
1249 if (saved_ftrace_func != ftrace_trace_function) {
1250 saved_ftrace_func = ftrace_trace_function;
1251 command |= FTRACE_UPDATE_TRACE_FUNC;
1254 if (!command || !ftrace_enabled)
1257 ftrace_run_update_code(command);
1260 static void ftrace_startup_sysctl(void)
1262 int command = FTRACE_ENABLE_MCOUNT;
1264 if (unlikely(ftrace_disabled))
1267 /* Force update next time */
1268 saved_ftrace_func = NULL;
1269 /* ftrace_start_up is true if we want ftrace running */
1270 if (ftrace_start_up)
1271 command |= FTRACE_ENABLE_CALLS;
1273 ftrace_run_update_code(command);
1276 static void ftrace_shutdown_sysctl(void)
1278 int command = FTRACE_DISABLE_MCOUNT;
1280 if (unlikely(ftrace_disabled))
1283 /* ftrace_start_up is true if ftrace is running */
1284 if (ftrace_start_up)
1285 command |= FTRACE_DISABLE_CALLS;
1287 ftrace_run_update_code(command);
1290 static cycle_t ftrace_update_time;
1291 static unsigned long ftrace_update_cnt;
1292 unsigned long ftrace_update_tot_cnt;
1294 static int ftrace_update_code(struct module *mod)
1296 struct dyn_ftrace *p;
1297 cycle_t start, stop;
1299 start = ftrace_now(raw_smp_processor_id());
1300 ftrace_update_cnt = 0;
1302 while (ftrace_new_addrs) {
1304 /* If something went wrong, bail without enabling anything */
1305 if (unlikely(ftrace_disabled))
1308 p = ftrace_new_addrs;
1309 ftrace_new_addrs = p->newlist;
1312 /* convert record (i.e, patch mcount-call with NOP) */
1313 if (ftrace_code_disable(mod, p)) {
1314 p->flags |= FTRACE_FL_CONVERTED;
1315 ftrace_update_cnt++;
1320 stop = ftrace_now(raw_smp_processor_id());
1321 ftrace_update_time = stop - start;
1322 ftrace_update_tot_cnt += ftrace_update_cnt;
1327 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1329 struct ftrace_page *pg;
1333 /* allocate a few pages */
1334 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1335 if (!ftrace_pages_start)
1339 * Allocate a few more pages.
1341 * TODO: have some parser search vmlinux before
1342 * final linking to find all calls to ftrace.
1344 * a) know how many pages to allocate.
1346 * b) set up the table then.
1348 * The dynamic code is still necessary for
1352 pg = ftrace_pages = ftrace_pages_start;
1354 cnt = num_to_init / ENTRIES_PER_PAGE;
1355 pr_info("ftrace: allocating %ld entries in %d pages\n",
1356 num_to_init, cnt + 1);
1358 for (i = 0; i < cnt; i++) {
1359 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1361 /* If we fail, we'll try later anyway */
1372 FTRACE_ITER_FILTER = (1 << 0),
1373 FTRACE_ITER_CONT = (1 << 1),
1374 FTRACE_ITER_NOTRACE = (1 << 2),
1375 FTRACE_ITER_FAILURES = (1 << 3),
1376 FTRACE_ITER_PRINTALL = (1 << 4),
1377 FTRACE_ITER_HASH = (1 << 5),
1380 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1382 struct ftrace_iterator {
1383 struct ftrace_page *pg;
1387 unsigned char buffer[FTRACE_BUFF_MAX+1];
1388 unsigned buffer_idx;
1393 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1395 struct ftrace_iterator *iter = m->private;
1396 struct hlist_node *hnd = v;
1397 struct hlist_head *hhd;
1399 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1404 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1407 hhd = &ftrace_func_hash[iter->hidx];
1409 if (hlist_empty(hhd)) {
1428 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1430 struct ftrace_iterator *iter = m->private;
1433 iter->flags |= FTRACE_ITER_HASH;
1435 return t_hash_next(m, p, pos);
1438 static int t_hash_show(struct seq_file *m, void *v)
1440 struct ftrace_func_probe *rec;
1441 struct hlist_node *hnd = v;
1442 char str[KSYM_SYMBOL_LEN];
1444 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1446 if (rec->ops->print)
1447 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1449 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1450 seq_printf(m, "%s:", str);
1452 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1453 seq_printf(m, "%s", str);
1456 seq_printf(m, ":%p", rec->data);
1463 t_next(struct seq_file *m, void *v, loff_t *pos)
1465 struct ftrace_iterator *iter = m->private;
1466 struct dyn_ftrace *rec = NULL;
1468 if (iter->flags & FTRACE_ITER_HASH)
1469 return t_hash_next(m, v, pos);
1473 if (iter->flags & FTRACE_ITER_PRINTALL)
1477 if (iter->idx >= iter->pg->index) {
1478 if (iter->pg->next) {
1479 iter->pg = iter->pg->next;
1486 rec = &iter->pg->records[iter->idx++];
1487 if ((rec->flags & FTRACE_FL_FREE) ||
1489 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1490 (rec->flags & FTRACE_FL_FAILED)) ||
1492 ((iter->flags & FTRACE_ITER_FAILURES) &&
1493 !(rec->flags & FTRACE_FL_FAILED)) ||
1495 ((iter->flags & FTRACE_ITER_FILTER) &&
1496 !(rec->flags & FTRACE_FL_FILTER)) ||
1498 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1499 !(rec->flags & FTRACE_FL_NOTRACE))) {
1508 static void *t_start(struct seq_file *m, loff_t *pos)
1510 struct ftrace_iterator *iter = m->private;
1513 mutex_lock(&ftrace_lock);
1515 * For set_ftrace_filter reading, if we have the filter
1516 * off, we can short cut and just print out that all
1517 * functions are enabled.
1519 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1521 return t_hash_start(m, pos);
1522 iter->flags |= FTRACE_ITER_PRINTALL;
1527 if (iter->flags & FTRACE_ITER_HASH)
1528 return t_hash_start(m, pos);
1537 p = t_next(m, p, pos);
1540 return t_hash_start(m, pos);
1545 static void t_stop(struct seq_file *m, void *p)
1547 mutex_unlock(&ftrace_lock);
1550 static int t_show(struct seq_file *m, void *v)
1552 struct ftrace_iterator *iter = m->private;
1553 struct dyn_ftrace *rec = v;
1554 char str[KSYM_SYMBOL_LEN];
1556 if (iter->flags & FTRACE_ITER_HASH)
1557 return t_hash_show(m, v);
1559 if (iter->flags & FTRACE_ITER_PRINTALL) {
1560 seq_printf(m, "#### all functions enabled ####\n");
1567 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1569 seq_printf(m, "%s\n", str);
1574 static struct seq_operations show_ftrace_seq_ops = {
1582 ftrace_avail_open(struct inode *inode, struct file *file)
1584 struct ftrace_iterator *iter;
1587 if (unlikely(ftrace_disabled))
1590 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1594 iter->pg = ftrace_pages_start;
1596 ret = seq_open(file, &show_ftrace_seq_ops);
1598 struct seq_file *m = file->private_data;
1608 int ftrace_avail_release(struct inode *inode, struct file *file)
1610 struct seq_file *m = (struct seq_file *)file->private_data;
1611 struct ftrace_iterator *iter = m->private;
1613 seq_release(inode, file);
1620 ftrace_failures_open(struct inode *inode, struct file *file)
1624 struct ftrace_iterator *iter;
1626 ret = ftrace_avail_open(inode, file);
1628 m = (struct seq_file *)file->private_data;
1629 iter = (struct ftrace_iterator *)m->private;
1630 iter->flags = FTRACE_ITER_FAILURES;
1637 static void ftrace_filter_reset(int enable)
1639 struct ftrace_page *pg;
1640 struct dyn_ftrace *rec;
1641 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1643 mutex_lock(&ftrace_lock);
1645 ftrace_filtered = 0;
1646 do_for_each_ftrace_rec(pg, rec) {
1647 if (rec->flags & FTRACE_FL_FAILED)
1649 rec->flags &= ~type;
1650 } while_for_each_ftrace_rec();
1651 mutex_unlock(&ftrace_lock);
1655 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1657 struct ftrace_iterator *iter;
1660 if (unlikely(ftrace_disabled))
1663 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1667 mutex_lock(&ftrace_regex_lock);
1668 if ((file->f_mode & FMODE_WRITE) &&
1669 !(file->f_flags & O_APPEND))
1670 ftrace_filter_reset(enable);
1672 if (file->f_mode & FMODE_READ) {
1673 iter->pg = ftrace_pages_start;
1674 iter->flags = enable ? FTRACE_ITER_FILTER :
1675 FTRACE_ITER_NOTRACE;
1677 ret = seq_open(file, &show_ftrace_seq_ops);
1679 struct seq_file *m = file->private_data;
1684 file->private_data = iter;
1685 mutex_unlock(&ftrace_regex_lock);
1691 ftrace_filter_open(struct inode *inode, struct file *file)
1693 return ftrace_regex_open(inode, file, 1);
1697 ftrace_notrace_open(struct inode *inode, struct file *file)
1699 return ftrace_regex_open(inode, file, 0);
1703 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1707 if (file->f_mode & FMODE_READ)
1708 ret = seq_lseek(file, offset, origin);
1710 file->f_pos = ret = 1;
1723 * (static function - no need for kernel doc)
1725 * Pass in a buffer containing a glob and this function will
1726 * set search to point to the search part of the buffer and
1727 * return the type of search it is (see enum above).
1728 * This does modify buff.
1730 * Returns enum type.
1731 * search returns the pointer to use for comparison.
1732 * not returns 1 if buff started with a '!'
1736 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1738 int type = MATCH_FULL;
1741 if (buff[0] == '!') {
1750 for (i = 0; i < len; i++) {
1751 if (buff[i] == '*') {
1754 type = MATCH_END_ONLY;
1756 if (type == MATCH_END_ONLY)
1757 type = MATCH_MIDDLE_ONLY;
1759 type = MATCH_FRONT_ONLY;
1769 static int ftrace_match(char *str, char *regex, int len, int type)
1776 if (strcmp(str, regex) == 0)
1779 case MATCH_FRONT_ONLY:
1780 if (strncmp(str, regex, len) == 0)
1783 case MATCH_MIDDLE_ONLY:
1784 if (strstr(str, regex))
1787 case MATCH_END_ONLY:
1788 ptr = strstr(str, regex);
1789 if (ptr && (ptr[len] == 0))
1798 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1800 char str[KSYM_SYMBOL_LEN];
1802 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1803 return ftrace_match(str, regex, len, type);
1806 static void ftrace_match_records(char *buff, int len, int enable)
1808 unsigned int search_len;
1809 struct ftrace_page *pg;
1810 struct dyn_ftrace *rec;
1816 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1817 type = ftrace_setup_glob(buff, len, &search, ¬);
1819 search_len = strlen(search);
1821 mutex_lock(&ftrace_lock);
1822 do_for_each_ftrace_rec(pg, rec) {
1824 if (rec->flags & FTRACE_FL_FAILED)
1827 if (ftrace_match_record(rec, search, search_len, type)) {
1829 rec->flags &= ~flag;
1834 * Only enable filtering if we have a function that
1837 if (enable && (rec->flags & FTRACE_FL_FILTER))
1838 ftrace_filtered = 1;
1839 } while_for_each_ftrace_rec();
1840 mutex_unlock(&ftrace_lock);
1844 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1845 char *regex, int len, int type)
1847 char str[KSYM_SYMBOL_LEN];
1850 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1852 if (!modname || strcmp(modname, mod))
1855 /* blank search means to match all funcs in the mod */
1857 return ftrace_match(str, regex, len, type);
1862 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1864 unsigned search_len = 0;
1865 struct ftrace_page *pg;
1866 struct dyn_ftrace *rec;
1867 int type = MATCH_FULL;
1868 char *search = buff;
1872 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1874 /* blank or '*' mean the same */
1875 if (strcmp(buff, "*") == 0)
1878 /* handle the case of 'dont filter this module' */
1879 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1885 type = ftrace_setup_glob(buff, strlen(buff), &search, ¬);
1886 search_len = strlen(search);
1889 mutex_lock(&ftrace_lock);
1890 do_for_each_ftrace_rec(pg, rec) {
1892 if (rec->flags & FTRACE_FL_FAILED)
1895 if (ftrace_match_module_record(rec, mod,
1896 search, search_len, type)) {
1898 rec->flags &= ~flag;
1902 if (enable && (rec->flags & FTRACE_FL_FILTER))
1903 ftrace_filtered = 1;
1905 } while_for_each_ftrace_rec();
1906 mutex_unlock(&ftrace_lock);
1910 * We register the module command as a template to show others how
1911 * to register the a command as well.
1915 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1920 * cmd == 'mod' because we only registered this func
1921 * for the 'mod' ftrace_func_command.
1922 * But if you register one func with multiple commands,
1923 * you can tell which command was used by the cmd
1927 /* we must have a module name */
1931 mod = strsep(¶m, ":");
1935 ftrace_match_module_records(func, mod, enable);
1939 static struct ftrace_func_command ftrace_mod_cmd = {
1941 .func = ftrace_mod_callback,
1944 static int __init ftrace_mod_cmd_init(void)
1946 return register_ftrace_command(&ftrace_mod_cmd);
1948 device_initcall(ftrace_mod_cmd_init);
1951 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1953 struct ftrace_func_probe *entry;
1954 struct hlist_head *hhd;
1955 struct hlist_node *n;
1959 key = hash_long(ip, FTRACE_HASH_BITS);
1961 hhd = &ftrace_func_hash[key];
1963 if (hlist_empty(hhd))
1967 * Disable preemption for these calls to prevent a RCU grace
1968 * period. This syncs the hash iteration and freeing of items
1969 * on the hash. rcu_read_lock is too dangerous here.
1971 resched = ftrace_preempt_disable();
1972 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1973 if (entry->ip == ip)
1974 entry->ops->func(ip, parent_ip, &entry->data);
1976 ftrace_preempt_enable(resched);
1979 static struct ftrace_ops trace_probe_ops __read_mostly =
1981 .func = function_trace_probe_call,
1984 static int ftrace_probe_registered;
1986 static void __enable_ftrace_function_probe(void)
1990 if (ftrace_probe_registered)
1993 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1994 struct hlist_head *hhd = &ftrace_func_hash[i];
1998 /* Nothing registered? */
1999 if (i == FTRACE_FUNC_HASHSIZE)
2002 __register_ftrace_function(&trace_probe_ops);
2004 ftrace_probe_registered = 1;
2007 static void __disable_ftrace_function_probe(void)
2011 if (!ftrace_probe_registered)
2014 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2015 struct hlist_head *hhd = &ftrace_func_hash[i];
2020 /* no more funcs left */
2021 __unregister_ftrace_function(&trace_probe_ops);
2023 ftrace_probe_registered = 0;
2027 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2029 struct ftrace_func_probe *entry =
2030 container_of(rhp, struct ftrace_func_probe, rcu);
2032 if (entry->ops->free)
2033 entry->ops->free(&entry->data);
2039 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2042 struct ftrace_func_probe *entry;
2043 struct ftrace_page *pg;
2044 struct dyn_ftrace *rec;
2050 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2051 len = strlen(search);
2053 /* we do not support '!' for function probes */
2057 mutex_lock(&ftrace_lock);
2058 do_for_each_ftrace_rec(pg, rec) {
2060 if (rec->flags & FTRACE_FL_FAILED)
2063 if (!ftrace_match_record(rec, search, len, type))
2066 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2068 /* If we did not process any, then return error */
2079 * The caller might want to do something special
2080 * for each function we find. We call the callback
2081 * to give the caller an opportunity to do so.
2083 if (ops->callback) {
2084 if (ops->callback(rec->ip, &entry->data) < 0) {
2085 /* caller does not like this func */
2092 entry->ip = rec->ip;
2094 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2095 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2097 } while_for_each_ftrace_rec();
2098 __enable_ftrace_function_probe();
2101 mutex_unlock(&ftrace_lock);
2107 PROBE_TEST_FUNC = 1,
2112 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2113 void *data, int flags)
2115 struct ftrace_func_probe *entry;
2116 struct hlist_node *n, *tmp;
2117 char str[KSYM_SYMBOL_LEN];
2118 int type = MATCH_FULL;
2122 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2127 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2128 len = strlen(search);
2130 /* we do not support '!' for function probes */
2135 mutex_lock(&ftrace_lock);
2136 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2137 struct hlist_head *hhd = &ftrace_func_hash[i];
2139 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2141 /* break up if statements for readability */
2142 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2145 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2148 /* do this last, since it is the most expensive */
2150 kallsyms_lookup(entry->ip, NULL, NULL,
2152 if (!ftrace_match(str, glob, len, type))
2156 hlist_del(&entry->node);
2157 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2160 __disable_ftrace_function_probe();
2161 mutex_unlock(&ftrace_lock);
2165 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2168 __unregister_ftrace_function_probe(glob, ops, data,
2169 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2173 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2175 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2178 void unregister_ftrace_function_probe_all(char *glob)
2180 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2183 static LIST_HEAD(ftrace_commands);
2184 static DEFINE_MUTEX(ftrace_cmd_mutex);
2186 int register_ftrace_command(struct ftrace_func_command *cmd)
2188 struct ftrace_func_command *p;
2191 mutex_lock(&ftrace_cmd_mutex);
2192 list_for_each_entry(p, &ftrace_commands, list) {
2193 if (strcmp(cmd->name, p->name) == 0) {
2198 list_add(&cmd->list, &ftrace_commands);
2200 mutex_unlock(&ftrace_cmd_mutex);
2205 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2207 struct ftrace_func_command *p, *n;
2210 mutex_lock(&ftrace_cmd_mutex);
2211 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2212 if (strcmp(cmd->name, p->name) == 0) {
2214 list_del_init(&p->list);
2219 mutex_unlock(&ftrace_cmd_mutex);
2224 static int ftrace_process_regex(char *buff, int len, int enable)
2226 char *func, *command, *next = buff;
2227 struct ftrace_func_command *p;
2230 func = strsep(&next, ":");
2233 ftrace_match_records(func, len, enable);
2239 command = strsep(&next, ":");
2241 mutex_lock(&ftrace_cmd_mutex);
2242 list_for_each_entry(p, &ftrace_commands, list) {
2243 if (strcmp(p->name, command) == 0) {
2244 ret = p->func(func, command, next, enable);
2249 mutex_unlock(&ftrace_cmd_mutex);
2255 ftrace_regex_write(struct file *file, const char __user *ubuf,
2256 size_t cnt, loff_t *ppos, int enable)
2258 struct ftrace_iterator *iter;
2263 if (!cnt || cnt < 0)
2266 mutex_lock(&ftrace_regex_lock);
2268 if (file->f_mode & FMODE_READ) {
2269 struct seq_file *m = file->private_data;
2272 iter = file->private_data;
2275 iter->flags &= ~FTRACE_ITER_CONT;
2276 iter->buffer_idx = 0;
2279 ret = get_user(ch, ubuf++);
2285 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2286 /* skip white space */
2287 while (cnt && isspace(ch)) {
2288 ret = get_user(ch, ubuf++);
2296 file->f_pos += read;
2301 iter->buffer_idx = 0;
2304 while (cnt && !isspace(ch)) {
2305 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2306 iter->buffer[iter->buffer_idx++] = ch;
2311 ret = get_user(ch, ubuf++);
2320 iter->buffer[iter->buffer_idx] = 0;
2321 ret = ftrace_process_regex(iter->buffer,
2322 iter->buffer_idx, enable);
2325 iter->buffer_idx = 0;
2327 iter->flags |= FTRACE_ITER_CONT;
2330 file->f_pos += read;
2334 mutex_unlock(&ftrace_regex_lock);
2340 ftrace_filter_write(struct file *file, const char __user *ubuf,
2341 size_t cnt, loff_t *ppos)
2343 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2347 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2348 size_t cnt, loff_t *ppos)
2350 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2354 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2356 if (unlikely(ftrace_disabled))
2359 mutex_lock(&ftrace_regex_lock);
2361 ftrace_filter_reset(enable);
2363 ftrace_match_records(buf, len, enable);
2364 mutex_unlock(&ftrace_regex_lock);
2368 * ftrace_set_filter - set a function to filter on in ftrace
2369 * @buf - the string that holds the function filter text.
2370 * @len - the length of the string.
2371 * @reset - non zero to reset all filters before applying this filter.
2373 * Filters denote which functions should be enabled when tracing is enabled.
2374 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2376 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2378 ftrace_set_regex(buf, len, reset, 1);
2382 * ftrace_set_notrace - set a function to not trace in ftrace
2383 * @buf - the string that holds the function notrace text.
2384 * @len - the length of the string.
2385 * @reset - non zero to reset all filters before applying this filter.
2387 * Notrace Filters denote which functions should not be enabled when tracing
2388 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2391 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2393 ftrace_set_regex(buf, len, reset, 0);
2397 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2399 struct seq_file *m = (struct seq_file *)file->private_data;
2400 struct ftrace_iterator *iter;
2402 mutex_lock(&ftrace_regex_lock);
2403 if (file->f_mode & FMODE_READ) {
2406 seq_release(inode, file);
2408 iter = file->private_data;
2410 if (iter->buffer_idx) {
2412 iter->buffer[iter->buffer_idx] = 0;
2413 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
2416 mutex_lock(&ftrace_lock);
2417 if (ftrace_start_up && ftrace_enabled)
2418 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2419 mutex_unlock(&ftrace_lock);
2422 mutex_unlock(&ftrace_regex_lock);
2427 ftrace_filter_release(struct inode *inode, struct file *file)
2429 return ftrace_regex_release(inode, file, 1);
2433 ftrace_notrace_release(struct inode *inode, struct file *file)
2435 return ftrace_regex_release(inode, file, 0);
2438 static const struct file_operations ftrace_avail_fops = {
2439 .open = ftrace_avail_open,
2441 .llseek = seq_lseek,
2442 .release = ftrace_avail_release,
2445 static const struct file_operations ftrace_failures_fops = {
2446 .open = ftrace_failures_open,
2448 .llseek = seq_lseek,
2449 .release = ftrace_avail_release,
2452 static const struct file_operations ftrace_filter_fops = {
2453 .open = ftrace_filter_open,
2455 .write = ftrace_filter_write,
2456 .llseek = ftrace_regex_lseek,
2457 .release = ftrace_filter_release,
2460 static const struct file_operations ftrace_notrace_fops = {
2461 .open = ftrace_notrace_open,
2463 .write = ftrace_notrace_write,
2464 .llseek = ftrace_regex_lseek,
2465 .release = ftrace_notrace_release,
2468 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2470 static DEFINE_MUTEX(graph_lock);
2472 int ftrace_graph_count;
2473 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2476 g_next(struct seq_file *m, void *v, loff_t *pos)
2478 unsigned long *array = m->private;
2483 if (index >= ftrace_graph_count)
2486 return &array[index];
2489 static void *g_start(struct seq_file *m, loff_t *pos)
2493 mutex_lock(&graph_lock);
2495 /* Nothing, tell g_show to print all functions are enabled */
2496 if (!ftrace_graph_count && !*pos)
2499 p = g_next(m, p, pos);
2504 static void g_stop(struct seq_file *m, void *p)
2506 mutex_unlock(&graph_lock);
2509 static int g_show(struct seq_file *m, void *v)
2511 unsigned long *ptr = v;
2512 char str[KSYM_SYMBOL_LEN];
2517 if (ptr == (unsigned long *)1) {
2518 seq_printf(m, "#### all functions enabled ####\n");
2522 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2524 seq_printf(m, "%s\n", str);
2529 static struct seq_operations ftrace_graph_seq_ops = {
2537 ftrace_graph_open(struct inode *inode, struct file *file)
2541 if (unlikely(ftrace_disabled))
2544 mutex_lock(&graph_lock);
2545 if ((file->f_mode & FMODE_WRITE) &&
2546 !(file->f_flags & O_APPEND)) {
2547 ftrace_graph_count = 0;
2548 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2551 if (file->f_mode & FMODE_READ) {
2552 ret = seq_open(file, &ftrace_graph_seq_ops);
2554 struct seq_file *m = file->private_data;
2555 m->private = ftrace_graph_funcs;
2558 file->private_data = ftrace_graph_funcs;
2559 mutex_unlock(&graph_lock);
2565 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2567 struct dyn_ftrace *rec;
2568 struct ftrace_page *pg;
2576 if (ftrace_disabled)
2580 type = ftrace_setup_glob(buffer, strlen(buffer), &search, ¬);
2584 search_len = strlen(search);
2586 mutex_lock(&ftrace_lock);
2587 do_for_each_ftrace_rec(pg, rec) {
2589 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2592 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2595 if (ftrace_match_record(rec, search, search_len, type)) {
2596 /* ensure it is not already in the array */
2598 for (i = 0; i < *idx; i++)
2599 if (array[i] == rec->ip) {
2604 array[(*idx)++] = rec->ip;
2608 } while_for_each_ftrace_rec();
2610 mutex_unlock(&ftrace_lock);
2612 return found ? 0 : -EINVAL;
2616 ftrace_graph_write(struct file *file, const char __user *ubuf,
2617 size_t cnt, loff_t *ppos)
2619 unsigned char buffer[FTRACE_BUFF_MAX+1];
2620 unsigned long *array;
2626 if (!cnt || cnt < 0)
2629 mutex_lock(&graph_lock);
2631 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2636 if (file->f_mode & FMODE_READ) {
2637 struct seq_file *m = file->private_data;
2640 array = file->private_data;
2642 ret = get_user(ch, ubuf++);
2648 /* skip white space */
2649 while (cnt && isspace(ch)) {
2650 ret = get_user(ch, ubuf++);
2663 while (cnt && !isspace(ch)) {
2664 if (index < FTRACE_BUFF_MAX)
2665 buffer[index++] = ch;
2670 ret = get_user(ch, ubuf++);
2678 /* we allow only one expression at a time */
2679 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2683 file->f_pos += read;
2687 mutex_unlock(&graph_lock);
2692 static const struct file_operations ftrace_graph_fops = {
2693 .open = ftrace_graph_open,
2695 .write = ftrace_graph_write,
2697 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2699 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2701 struct dentry *entry;
2703 entry = debugfs_create_file("available_filter_functions", 0444,
2704 d_tracer, NULL, &ftrace_avail_fops);
2706 pr_warning("Could not create debugfs "
2707 "'available_filter_functions' entry\n");
2709 entry = debugfs_create_file("failures", 0444,
2710 d_tracer, NULL, &ftrace_failures_fops);
2712 pr_warning("Could not create debugfs 'failures' entry\n");
2714 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
2715 NULL, &ftrace_filter_fops);
2717 pr_warning("Could not create debugfs "
2718 "'set_ftrace_filter' entry\n");
2720 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
2721 NULL, &ftrace_notrace_fops);
2723 pr_warning("Could not create debugfs "
2724 "'set_ftrace_notrace' entry\n");
2726 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2727 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
2729 &ftrace_graph_fops);
2731 pr_warning("Could not create debugfs "
2732 "'set_graph_function' entry\n");
2733 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2738 static int ftrace_convert_nops(struct module *mod,
2739 unsigned long *start,
2744 unsigned long flags;
2746 mutex_lock(&ftrace_lock);
2749 addr = ftrace_call_adjust(*p++);
2751 * Some architecture linkers will pad between
2752 * the different mcount_loc sections of different
2753 * object files to satisfy alignments.
2754 * Skip any NULL pointers.
2758 ftrace_record_ip(addr);
2761 /* disable interrupts to prevent kstop machine */
2762 local_irq_save(flags);
2763 ftrace_update_code(mod);
2764 local_irq_restore(flags);
2765 mutex_unlock(&ftrace_lock);
2770 void ftrace_init_module(struct module *mod,
2771 unsigned long *start, unsigned long *end)
2773 if (ftrace_disabled || start == end)
2775 ftrace_convert_nops(mod, start, end);
2778 extern unsigned long __start_mcount_loc[];
2779 extern unsigned long __stop_mcount_loc[];
2781 void __init ftrace_init(void)
2783 unsigned long count, addr, flags;
2786 /* Keep the ftrace pointer to the stub */
2787 addr = (unsigned long)ftrace_stub;
2789 local_irq_save(flags);
2790 ftrace_dyn_arch_init(&addr);
2791 local_irq_restore(flags);
2793 /* ftrace_dyn_arch_init places the return code in addr */
2797 count = __stop_mcount_loc - __start_mcount_loc;
2799 ret = ftrace_dyn_table_alloc(count);
2803 last_ftrace_enabled = ftrace_enabled = 1;
2805 ret = ftrace_convert_nops(NULL,
2811 ftrace_disabled = 1;
2816 static int __init ftrace_nodyn_init(void)
2821 device_initcall(ftrace_nodyn_init);
2823 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2824 static inline void ftrace_startup_enable(int command) { }
2825 /* Keep as macros so we do not need to define the commands */
2826 # define ftrace_startup(command) do { } while (0)
2827 # define ftrace_shutdown(command) do { } while (0)
2828 # define ftrace_startup_sysctl() do { } while (0)
2829 # define ftrace_shutdown_sysctl() do { } while (0)
2830 #endif /* CONFIG_DYNAMIC_FTRACE */
2833 ftrace_pid_read(struct file *file, char __user *ubuf,
2834 size_t cnt, loff_t *ppos)
2839 if (ftrace_pid_trace == ftrace_swapper_pid)
2840 r = sprintf(buf, "swapper tasks\n");
2841 else if (ftrace_pid_trace)
2842 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2844 r = sprintf(buf, "no pid\n");
2846 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2849 static void clear_ftrace_swapper(void)
2851 struct task_struct *p;
2855 for_each_online_cpu(cpu) {
2857 clear_tsk_trace_trace(p);
2862 static void set_ftrace_swapper(void)
2864 struct task_struct *p;
2868 for_each_online_cpu(cpu) {
2870 set_tsk_trace_trace(p);
2875 static void clear_ftrace_pid(struct pid *pid)
2877 struct task_struct *p;
2880 do_each_pid_task(pid, PIDTYPE_PID, p) {
2881 clear_tsk_trace_trace(p);
2882 } while_each_pid_task(pid, PIDTYPE_PID, p);
2888 static void set_ftrace_pid(struct pid *pid)
2890 struct task_struct *p;
2893 do_each_pid_task(pid, PIDTYPE_PID, p) {
2894 set_tsk_trace_trace(p);
2895 } while_each_pid_task(pid, PIDTYPE_PID, p);
2899 static void clear_ftrace_pid_task(struct pid **pid)
2901 if (*pid == ftrace_swapper_pid)
2902 clear_ftrace_swapper();
2904 clear_ftrace_pid(*pid);
2909 static void set_ftrace_pid_task(struct pid *pid)
2911 if (pid == ftrace_swapper_pid)
2912 set_ftrace_swapper();
2914 set_ftrace_pid(pid);
2918 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2919 size_t cnt, loff_t *ppos)
2926 if (cnt >= sizeof(buf))
2929 if (copy_from_user(&buf, ubuf, cnt))
2934 ret = strict_strtol(buf, 10, &val);
2938 mutex_lock(&ftrace_lock);
2940 /* disable pid tracing */
2941 if (!ftrace_pid_trace)
2944 clear_ftrace_pid_task(&ftrace_pid_trace);
2947 /* swapper task is special */
2949 pid = ftrace_swapper_pid;
2950 if (pid == ftrace_pid_trace)
2953 pid = find_get_pid(val);
2955 if (pid == ftrace_pid_trace) {
2961 if (ftrace_pid_trace)
2962 clear_ftrace_pid_task(&ftrace_pid_trace);
2967 ftrace_pid_trace = pid;
2969 set_ftrace_pid_task(ftrace_pid_trace);
2972 /* update the function call */
2973 ftrace_update_pid_func();
2974 ftrace_startup_enable(0);
2977 mutex_unlock(&ftrace_lock);
2982 static const struct file_operations ftrace_pid_fops = {
2983 .read = ftrace_pid_read,
2984 .write = ftrace_pid_write,
2987 static __init int ftrace_init_debugfs(void)
2989 struct dentry *d_tracer;
2990 struct dentry *entry;
2992 d_tracer = tracing_init_dentry();
2996 ftrace_init_dyn_debugfs(d_tracer);
2998 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2999 NULL, &ftrace_pid_fops);
3001 pr_warning("Could not create debugfs "
3002 "'set_ftrace_pid' entry\n");
3004 ftrace_profile_debugfs(d_tracer);
3008 fs_initcall(ftrace_init_debugfs);
3011 * ftrace_kill - kill ftrace
3013 * This function should be used by panic code. It stops ftrace
3014 * but in a not so nice way. If you need to simply kill ftrace
3015 * from a non-atomic section, use ftrace_kill.
3017 void ftrace_kill(void)
3019 ftrace_disabled = 1;
3021 clear_ftrace_function();
3025 * register_ftrace_function - register a function for profiling
3026 * @ops - ops structure that holds the function for profiling.
3028 * Register a function to be called by all functions in the
3031 * Note: @ops->func and all the functions it calls must be labeled
3032 * with "notrace", otherwise it will go into a
3035 int register_ftrace_function(struct ftrace_ops *ops)
3039 if (unlikely(ftrace_disabled))
3042 mutex_lock(&ftrace_lock);
3044 ret = __register_ftrace_function(ops);
3047 mutex_unlock(&ftrace_lock);
3052 * unregister_ftrace_function - unregister a function for profiling.
3053 * @ops - ops structure that holds the function to unregister
3055 * Unregister a function that was added to be called by ftrace profiling.
3057 int unregister_ftrace_function(struct ftrace_ops *ops)
3061 mutex_lock(&ftrace_lock);
3062 ret = __unregister_ftrace_function(ops);
3064 mutex_unlock(&ftrace_lock);
3070 ftrace_enable_sysctl(struct ctl_table *table, int write,
3071 struct file *file, void __user *buffer, size_t *lenp,
3076 if (unlikely(ftrace_disabled))
3079 mutex_lock(&ftrace_lock);
3081 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
3083 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
3086 last_ftrace_enabled = ftrace_enabled;
3088 if (ftrace_enabled) {
3090 ftrace_startup_sysctl();
3092 /* we are starting ftrace again */
3093 if (ftrace_list != &ftrace_list_end) {
3094 if (ftrace_list->next == &ftrace_list_end)
3095 ftrace_trace_function = ftrace_list->func;
3097 ftrace_trace_function = ftrace_list_func;
3101 /* stopping ftrace calls (just send to ftrace_stub) */
3102 ftrace_trace_function = ftrace_stub;
3104 ftrace_shutdown_sysctl();
3108 mutex_unlock(&ftrace_lock);
3112 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3114 static atomic_t ftrace_graph_active;
3115 static struct notifier_block ftrace_suspend_notifier;
3117 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3122 /* The callbacks that hook a function */
3123 trace_func_graph_ret_t ftrace_graph_return =
3124 (trace_func_graph_ret_t)ftrace_stub;
3125 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3127 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3128 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3132 unsigned long flags;
3133 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3134 struct task_struct *g, *t;
3136 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3137 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3138 * sizeof(struct ftrace_ret_stack),
3140 if (!ret_stack_list[i]) {
3148 read_lock_irqsave(&tasklist_lock, flags);
3149 do_each_thread(g, t) {
3155 if (t->ret_stack == NULL) {
3156 t->curr_ret_stack = -1;
3157 /* Make sure IRQs see the -1 first: */
3159 t->ret_stack = ret_stack_list[start++];
3160 atomic_set(&t->tracing_graph_pause, 0);
3161 atomic_set(&t->trace_overrun, 0);
3163 } while_each_thread(g, t);
3166 read_unlock_irqrestore(&tasklist_lock, flags);
3168 for (i = start; i < end; i++)
3169 kfree(ret_stack_list[i]);
3174 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3175 struct task_struct *next)
3177 unsigned long long timestamp;
3181 * Does the user want to count the time a function was asleep.
3182 * If so, do not update the time stamps.
3184 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3187 timestamp = trace_clock_local();
3189 prev->ftrace_timestamp = timestamp;
3191 /* only process tasks that we timestamped */
3192 if (!next->ftrace_timestamp)
3196 * Update all the counters in next to make up for the
3197 * time next was sleeping.
3199 timestamp -= next->ftrace_timestamp;
3201 for (index = next->curr_ret_stack; index >= 0; index--)
3202 next->ret_stack[index].calltime += timestamp;
3205 /* Allocate a return stack for each task */
3206 static int start_graph_tracing(void)
3208 struct ftrace_ret_stack **ret_stack_list;
3211 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3212 sizeof(struct ftrace_ret_stack *),
3215 if (!ret_stack_list)
3218 /* The cpu_boot init_task->ret_stack will never be freed */
3219 for_each_online_cpu(cpu)
3220 ftrace_graph_init_task(idle_task(cpu));
3223 ret = alloc_retstack_tasklist(ret_stack_list);
3224 } while (ret == -EAGAIN);
3227 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3229 pr_info("ftrace_graph: Couldn't activate tracepoint"
3230 " probe to kernel_sched_switch\n");
3233 kfree(ret_stack_list);
3238 * Hibernation protection.
3239 * The state of the current task is too much unstable during
3240 * suspend/restore to disk. We want to protect against that.
3243 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3247 case PM_HIBERNATION_PREPARE:
3248 pause_graph_tracing();
3251 case PM_POST_HIBERNATION:
3252 unpause_graph_tracing();
3258 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3259 trace_func_graph_ent_t entryfunc)
3263 mutex_lock(&ftrace_lock);
3265 /* we currently allow only one tracer registered at a time */
3266 if (atomic_read(&ftrace_graph_active)) {
3271 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3272 register_pm_notifier(&ftrace_suspend_notifier);
3274 atomic_inc(&ftrace_graph_active);
3275 ret = start_graph_tracing();
3277 atomic_dec(&ftrace_graph_active);
3281 ftrace_graph_return = retfunc;
3282 ftrace_graph_entry = entryfunc;
3284 ftrace_startup(FTRACE_START_FUNC_RET);
3287 mutex_unlock(&ftrace_lock);
3291 void unregister_ftrace_graph(void)
3293 mutex_lock(&ftrace_lock);
3295 if (!unlikely(atomic_read(&ftrace_graph_active)))
3298 atomic_dec(&ftrace_graph_active);
3299 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3300 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3301 ftrace_graph_entry = ftrace_graph_entry_stub;
3302 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3303 unregister_pm_notifier(&ftrace_suspend_notifier);
3306 mutex_unlock(&ftrace_lock);
3309 /* Allocate a return stack for newly created task */
3310 void ftrace_graph_init_task(struct task_struct *t)
3312 if (atomic_read(&ftrace_graph_active)) {
3313 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3314 * sizeof(struct ftrace_ret_stack),
3318 t->curr_ret_stack = -1;
3319 atomic_set(&t->tracing_graph_pause, 0);
3320 atomic_set(&t->trace_overrun, 0);
3321 t->ftrace_timestamp = 0;
3323 t->ret_stack = NULL;
3326 void ftrace_graph_exit_task(struct task_struct *t)
3328 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3330 t->ret_stack = NULL;
3331 /* NULL must become visible to IRQs before we free it: */
3337 void ftrace_graph_stop(void)