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/events/sched.h>
34 #include <asm/ftrace.h>
35 #include <asm/setup.h>
37 #include "trace_output.h"
38 #include "trace_stat.h"
40 #define FTRACE_WARN_ON(cond) \
46 #define FTRACE_WARN_ON_ONCE(cond) \
48 if (WARN_ON_ONCE(cond)) \
52 /* hash bits for specific function selection */
53 #define FTRACE_HASH_BITS 7
54 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56 /* ftrace_enabled is a method to turn ftrace on or off */
57 int ftrace_enabled __read_mostly;
58 static int last_ftrace_enabled;
60 /* Quick disabling of function tracer. */
61 int function_trace_stop;
64 * ftrace_disabled is set when an anomaly is discovered.
65 * ftrace_disabled is much stronger than ftrace_enabled.
67 static int ftrace_disabled __read_mostly;
69 static DEFINE_MUTEX(ftrace_lock);
71 static struct ftrace_ops ftrace_list_end __read_mostly =
76 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
77 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
78 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
79 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
81 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
83 struct ftrace_ops *op = ftrace_list;
85 /* in case someone actually ports this to alpha! */
86 read_barrier_depends();
88 while (op != &ftrace_list_end) {
90 read_barrier_depends();
91 op->func(ip, parent_ip);
96 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
98 if (!test_tsk_trace_trace(current))
101 ftrace_pid_function(ip, parent_ip);
104 static void set_ftrace_pid_function(ftrace_func_t func)
106 /* do not set ftrace_pid_function to itself! */
107 if (func != ftrace_pid_func)
108 ftrace_pid_function = func;
112 * clear_ftrace_function - reset the ftrace function
114 * This NULLs the ftrace function and in essence stops
115 * tracing. There may be lag
117 void clear_ftrace_function(void)
119 ftrace_trace_function = ftrace_stub;
120 __ftrace_trace_function = ftrace_stub;
121 ftrace_pid_function = ftrace_stub;
124 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
126 * For those archs that do not test ftrace_trace_stop in their
127 * mcount call site, we need to do it from C.
129 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
131 if (function_trace_stop)
134 __ftrace_trace_function(ip, parent_ip);
138 static int __register_ftrace_function(struct ftrace_ops *ops)
140 ops->next = ftrace_list;
142 * We are entering ops into the ftrace_list but another
143 * CPU might be walking that list. We need to make sure
144 * the ops->next pointer is valid before another CPU sees
145 * the ops pointer included into the ftrace_list.
150 if (ftrace_enabled) {
153 if (ops->next == &ftrace_list_end)
156 func = ftrace_list_func;
158 if (ftrace_pid_trace) {
159 set_ftrace_pid_function(func);
160 func = ftrace_pid_func;
164 * For one func, simply call it directly.
165 * For more than one func, call the chain.
167 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
168 ftrace_trace_function = func;
170 __ftrace_trace_function = func;
171 ftrace_trace_function = ftrace_test_stop_func;
178 static int __unregister_ftrace_function(struct ftrace_ops *ops)
180 struct ftrace_ops **p;
183 * If we are removing the last function, then simply point
184 * to the ftrace_stub.
186 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
187 ftrace_trace_function = ftrace_stub;
188 ftrace_list = &ftrace_list_end;
192 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
201 if (ftrace_enabled) {
202 /* If we only have one func left, then call that directly */
203 if (ftrace_list->next == &ftrace_list_end) {
204 ftrace_func_t func = ftrace_list->func;
206 if (ftrace_pid_trace) {
207 set_ftrace_pid_function(func);
208 func = ftrace_pid_func;
210 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
211 ftrace_trace_function = func;
213 __ftrace_trace_function = func;
221 static void ftrace_update_pid_func(void)
225 if (ftrace_trace_function == ftrace_stub)
228 func = ftrace_trace_function;
230 if (ftrace_pid_trace) {
231 set_ftrace_pid_function(func);
232 func = ftrace_pid_func;
234 if (func == ftrace_pid_func)
235 func = ftrace_pid_function;
238 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
239 ftrace_trace_function = func;
241 __ftrace_trace_function = func;
245 #ifdef CONFIG_FUNCTION_PROFILER
246 struct ftrace_profile {
247 struct hlist_node node;
249 unsigned long counter;
250 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
251 unsigned long long time;
255 struct ftrace_profile_page {
256 struct ftrace_profile_page *next;
258 struct ftrace_profile records[];
261 struct ftrace_profile_stat {
263 struct hlist_head *hash;
264 struct ftrace_profile_page *pages;
265 struct ftrace_profile_page *start;
266 struct tracer_stat stat;
269 #define PROFILE_RECORDS_SIZE \
270 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
272 #define PROFILES_PER_PAGE \
273 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
275 static int ftrace_profile_bits __read_mostly;
276 static int ftrace_profile_enabled __read_mostly;
278 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
279 static DEFINE_MUTEX(ftrace_profile_lock);
281 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
283 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
286 function_stat_next(void *v, int idx)
288 struct ftrace_profile *rec = v;
289 struct ftrace_profile_page *pg;
291 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
297 if ((void *)rec >= (void *)&pg->records[pg->index]) {
301 rec = &pg->records[0];
309 static void *function_stat_start(struct tracer_stat *trace)
311 struct ftrace_profile_stat *stat =
312 container_of(trace, struct ftrace_profile_stat, stat);
314 if (!stat || !stat->start)
317 return function_stat_next(&stat->start->records[0], 0);
320 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
321 /* function graph compares on total time */
322 static int function_stat_cmp(void *p1, void *p2)
324 struct ftrace_profile *a = p1;
325 struct ftrace_profile *b = p2;
327 if (a->time < b->time)
329 if (a->time > b->time)
335 /* not function graph compares against hits */
336 static int function_stat_cmp(void *p1, void *p2)
338 struct ftrace_profile *a = p1;
339 struct ftrace_profile *b = p2;
341 if (a->counter < b->counter)
343 if (a->counter > b->counter)
350 static int function_stat_headers(struct seq_file *m)
352 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
353 seq_printf(m, " Function "
358 seq_printf(m, " Function Hit\n"
364 static int function_stat_show(struct seq_file *m, void *v)
366 struct ftrace_profile *rec = v;
367 char str[KSYM_SYMBOL_LEN];
368 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
369 static DEFINE_MUTEX(mutex);
370 static struct trace_seq s;
371 unsigned long long avg;
374 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
375 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
377 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
380 do_div(avg, rec->counter);
384 trace_print_graph_duration(rec->time, &s);
385 trace_seq_puts(&s, " ");
386 trace_print_graph_duration(avg, &s);
387 trace_print_seq(m, &s);
388 mutex_unlock(&mutex);
395 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
397 struct ftrace_profile_page *pg;
399 pg = stat->pages = stat->start;
402 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
407 memset(stat->hash, 0,
408 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
411 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
413 struct ftrace_profile_page *pg;
418 /* If we already allocated, do nothing */
422 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
426 #ifdef CONFIG_DYNAMIC_FTRACE
427 functions = ftrace_update_tot_cnt;
430 * We do not know the number of functions that exist because
431 * dynamic tracing is what counts them. With past experience
432 * we have around 20K functions. That should be more than enough.
433 * It is highly unlikely we will execute every function in
439 pg = stat->start = stat->pages;
441 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
443 for (i = 0; i < pages; i++) {
444 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
455 unsigned long tmp = (unsigned long)pg;
461 free_page((unsigned long)stat->pages);
468 static int ftrace_profile_init_cpu(int cpu)
470 struct ftrace_profile_stat *stat;
473 stat = &per_cpu(ftrace_profile_stats, cpu);
476 /* If the profile is already created, simply reset it */
477 ftrace_profile_reset(stat);
482 * We are profiling all functions, but usually only a few thousand
483 * functions are hit. We'll make a hash of 1024 items.
485 size = FTRACE_PROFILE_HASH_SIZE;
487 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
492 if (!ftrace_profile_bits) {
495 for (; size; size >>= 1)
496 ftrace_profile_bits++;
499 /* Preallocate the function profiling pages */
500 if (ftrace_profile_pages_init(stat) < 0) {
509 static int ftrace_profile_init(void)
514 for_each_online_cpu(cpu) {
515 ret = ftrace_profile_init_cpu(cpu);
523 /* interrupts must be disabled */
524 static struct ftrace_profile *
525 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
527 struct ftrace_profile *rec;
528 struct hlist_head *hhd;
529 struct hlist_node *n;
532 key = hash_long(ip, ftrace_profile_bits);
533 hhd = &stat->hash[key];
535 if (hlist_empty(hhd))
538 hlist_for_each_entry_rcu(rec, n, hhd, node) {
546 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
547 struct ftrace_profile *rec)
551 key = hash_long(rec->ip, ftrace_profile_bits);
552 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
556 * The memory is already allocated, this simply finds a new record to use.
558 static struct ftrace_profile *
559 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
561 struct ftrace_profile *rec = NULL;
563 /* prevent recursion (from NMIs) */
564 if (atomic_inc_return(&stat->disabled) != 1)
568 * Try to find the function again since an NMI
569 * could have added it
571 rec = ftrace_find_profiled_func(stat, ip);
575 if (stat->pages->index == PROFILES_PER_PAGE) {
576 if (!stat->pages->next)
578 stat->pages = stat->pages->next;
581 rec = &stat->pages->records[stat->pages->index++];
583 ftrace_add_profile(stat, rec);
586 atomic_dec(&stat->disabled);
592 function_profile_call(unsigned long ip, unsigned long parent_ip)
594 struct ftrace_profile_stat *stat;
595 struct ftrace_profile *rec;
598 if (!ftrace_profile_enabled)
601 local_irq_save(flags);
603 stat = &__get_cpu_var(ftrace_profile_stats);
604 if (!stat->hash || !ftrace_profile_enabled)
607 rec = ftrace_find_profiled_func(stat, ip);
609 rec = ftrace_profile_alloc(stat, ip);
616 local_irq_restore(flags);
619 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
620 static int profile_graph_entry(struct ftrace_graph_ent *trace)
622 function_profile_call(trace->func, 0);
626 static void profile_graph_return(struct ftrace_graph_ret *trace)
628 struct ftrace_profile_stat *stat;
629 unsigned long long calltime;
630 struct ftrace_profile *rec;
633 local_irq_save(flags);
634 stat = &__get_cpu_var(ftrace_profile_stats);
635 if (!stat->hash || !ftrace_profile_enabled)
638 calltime = trace->rettime - trace->calltime;
640 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
643 index = trace->depth;
645 /* Append this call time to the parent time to subtract */
647 current->ret_stack[index - 1].subtime += calltime;
649 if (current->ret_stack[index].subtime < calltime)
650 calltime -= current->ret_stack[index].subtime;
655 rec = ftrace_find_profiled_func(stat, trace->func);
657 rec->time += calltime;
660 local_irq_restore(flags);
663 static int register_ftrace_profiler(void)
665 return register_ftrace_graph(&profile_graph_return,
666 &profile_graph_entry);
669 static void unregister_ftrace_profiler(void)
671 unregister_ftrace_graph();
674 static struct ftrace_ops ftrace_profile_ops __read_mostly =
676 .func = function_profile_call,
679 static int register_ftrace_profiler(void)
681 return register_ftrace_function(&ftrace_profile_ops);
684 static void unregister_ftrace_profiler(void)
686 unregister_ftrace_function(&ftrace_profile_ops);
688 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
691 ftrace_profile_write(struct file *filp, const char __user *ubuf,
692 size_t cnt, loff_t *ppos)
695 char buf[64]; /* big enough to hold a number */
698 if (cnt >= sizeof(buf))
701 if (copy_from_user(&buf, ubuf, cnt))
706 ret = strict_strtoul(buf, 10, &val);
712 mutex_lock(&ftrace_profile_lock);
713 if (ftrace_profile_enabled ^ val) {
715 ret = ftrace_profile_init();
721 ret = register_ftrace_profiler();
726 ftrace_profile_enabled = 1;
728 ftrace_profile_enabled = 0;
730 * unregister_ftrace_profiler calls stop_machine
731 * so this acts like an synchronize_sched.
733 unregister_ftrace_profiler();
737 mutex_unlock(&ftrace_profile_lock);
745 ftrace_profile_read(struct file *filp, char __user *ubuf,
746 size_t cnt, loff_t *ppos)
748 char buf[64]; /* big enough to hold a number */
751 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
752 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
755 static const struct file_operations ftrace_profile_fops = {
756 .open = tracing_open_generic,
757 .read = ftrace_profile_read,
758 .write = ftrace_profile_write,
761 /* used to initialize the real stat files */
762 static struct tracer_stat function_stats __initdata = {
764 .stat_start = function_stat_start,
765 .stat_next = function_stat_next,
766 .stat_cmp = function_stat_cmp,
767 .stat_headers = function_stat_headers,
768 .stat_show = function_stat_show
771 static void ftrace_profile_debugfs(struct dentry *d_tracer)
773 struct ftrace_profile_stat *stat;
774 struct dentry *entry;
779 for_each_possible_cpu(cpu) {
780 stat = &per_cpu(ftrace_profile_stats, cpu);
782 /* allocate enough for function name + cpu number */
783 name = kmalloc(32, GFP_KERNEL);
786 * The files created are permanent, if something happens
787 * we still do not free memory.
791 "Could not allocate stat file for cpu %d\n",
795 stat->stat = function_stats;
796 snprintf(name, 32, "function%d", cpu);
797 stat->stat.name = name;
798 ret = register_stat_tracer(&stat->stat);
801 "Could not register function stat for cpu %d\n",
808 entry = debugfs_create_file("function_profile_enabled", 0644,
809 d_tracer, NULL, &ftrace_profile_fops);
811 pr_warning("Could not create debugfs "
812 "'function_profile_enabled' entry\n");
815 #else /* CONFIG_FUNCTION_PROFILER */
816 static void ftrace_profile_debugfs(struct dentry *d_tracer)
819 #endif /* CONFIG_FUNCTION_PROFILER */
821 /* set when tracing only a pid */
822 struct pid *ftrace_pid_trace;
823 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
825 #ifdef CONFIG_DYNAMIC_FTRACE
827 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
828 # error Dynamic ftrace depends on MCOUNT_RECORD
831 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
833 struct ftrace_func_probe {
834 struct hlist_node node;
835 struct ftrace_probe_ops *ops;
843 FTRACE_ENABLE_CALLS = (1 << 0),
844 FTRACE_DISABLE_CALLS = (1 << 1),
845 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
846 FTRACE_ENABLE_MCOUNT = (1 << 3),
847 FTRACE_DISABLE_MCOUNT = (1 << 4),
848 FTRACE_START_FUNC_RET = (1 << 5),
849 FTRACE_STOP_FUNC_RET = (1 << 6),
852 static int ftrace_filtered;
854 static struct dyn_ftrace *ftrace_new_addrs;
856 static DEFINE_MUTEX(ftrace_regex_lock);
859 struct ftrace_page *next;
861 struct dyn_ftrace records[];
864 #define ENTRIES_PER_PAGE \
865 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
867 /* estimate from running different kernels */
868 #define NR_TO_INIT 10000
870 static struct ftrace_page *ftrace_pages_start;
871 static struct ftrace_page *ftrace_pages;
873 static struct dyn_ftrace *ftrace_free_records;
876 * This is a double for. Do not use 'break' to break out of the loop,
877 * you must use a goto.
879 #define do_for_each_ftrace_rec(pg, rec) \
880 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
882 for (_____i = 0; _____i < pg->index; _____i++) { \
883 rec = &pg->records[_____i];
885 #define while_for_each_ftrace_rec() \
889 #ifdef CONFIG_KPROBES
891 static int frozen_record_count;
893 static inline void freeze_record(struct dyn_ftrace *rec)
895 if (!(rec->flags & FTRACE_FL_FROZEN)) {
896 rec->flags |= FTRACE_FL_FROZEN;
897 frozen_record_count++;
901 static inline void unfreeze_record(struct dyn_ftrace *rec)
903 if (rec->flags & FTRACE_FL_FROZEN) {
904 rec->flags &= ~FTRACE_FL_FROZEN;
905 frozen_record_count--;
909 static inline int record_frozen(struct dyn_ftrace *rec)
911 return rec->flags & FTRACE_FL_FROZEN;
914 # define freeze_record(rec) ({ 0; })
915 # define unfreeze_record(rec) ({ 0; })
916 # define record_frozen(rec) ({ 0; })
917 #endif /* CONFIG_KPROBES */
919 static void ftrace_free_rec(struct dyn_ftrace *rec)
921 rec->freelist = ftrace_free_records;
922 ftrace_free_records = rec;
923 rec->flags |= FTRACE_FL_FREE;
926 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
928 struct dyn_ftrace *rec;
930 /* First check for freed records */
931 if (ftrace_free_records) {
932 rec = ftrace_free_records;
934 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
935 FTRACE_WARN_ON_ONCE(1);
936 ftrace_free_records = NULL;
940 ftrace_free_records = rec->freelist;
941 memset(rec, 0, sizeof(*rec));
945 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
946 if (!ftrace_pages->next) {
947 /* allocate another page */
949 (void *)get_zeroed_page(GFP_KERNEL);
950 if (!ftrace_pages->next)
953 ftrace_pages = ftrace_pages->next;
956 return &ftrace_pages->records[ftrace_pages->index++];
959 static struct dyn_ftrace *
960 ftrace_record_ip(unsigned long ip)
962 struct dyn_ftrace *rec;
967 rec = ftrace_alloc_dyn_node(ip);
972 rec->newlist = ftrace_new_addrs;
973 ftrace_new_addrs = rec;
978 static void print_ip_ins(const char *fmt, unsigned char *p)
982 printk(KERN_CONT "%s", fmt);
984 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
985 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
988 static void ftrace_bug(int failed, unsigned long ip)
992 FTRACE_WARN_ON_ONCE(1);
993 pr_info("ftrace faulted on modifying ");
997 FTRACE_WARN_ON_ONCE(1);
998 pr_info("ftrace failed to modify ");
1000 print_ip_ins(" actual: ", (unsigned char *)ip);
1001 printk(KERN_CONT "\n");
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on writing ");
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace faulted on unknown error ");
1017 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1019 unsigned long ftrace_addr;
1020 unsigned long flag = 0UL;
1022 ftrace_addr = (unsigned long)FTRACE_ADDR;
1025 * If this record is not to be traced or we want to disable it,
1028 * If we want to enable it and filtering is off, then enable it.
1030 * If we want to enable it and filtering is on, enable it only if
1033 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1034 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1035 flag = FTRACE_FL_ENABLED;
1038 /* If the state of this record hasn't changed, then do nothing */
1039 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1043 rec->flags |= FTRACE_FL_ENABLED;
1044 return ftrace_make_call(rec, ftrace_addr);
1047 rec->flags &= ~FTRACE_FL_ENABLED;
1048 return ftrace_make_nop(NULL, rec, ftrace_addr);
1051 static void ftrace_replace_code(int enable)
1053 struct dyn_ftrace *rec;
1054 struct ftrace_page *pg;
1057 do_for_each_ftrace_rec(pg, rec) {
1059 * Skip over free records, records that have
1060 * failed and not converted.
1062 if (rec->flags & FTRACE_FL_FREE ||
1063 rec->flags & FTRACE_FL_FAILED ||
1064 !(rec->flags & FTRACE_FL_CONVERTED))
1067 /* ignore updates to this record's mcount site */
1068 if (get_kprobe((void *)rec->ip)) {
1072 unfreeze_record(rec);
1075 failed = __ftrace_replace_code(rec, enable);
1077 rec->flags |= FTRACE_FL_FAILED;
1078 if ((system_state == SYSTEM_BOOTING) ||
1079 !core_kernel_text(rec->ip)) {
1080 ftrace_free_rec(rec);
1082 ftrace_bug(failed, rec->ip);
1083 /* Stop processing */
1087 } while_for_each_ftrace_rec();
1091 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1098 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1100 ftrace_bug(ret, ip);
1101 rec->flags |= FTRACE_FL_FAILED;
1108 * archs can override this function if they must do something
1109 * before the modifying code is performed.
1111 int __weak ftrace_arch_code_modify_prepare(void)
1117 * archs can override this function if they must do something
1118 * after the modifying code is performed.
1120 int __weak ftrace_arch_code_modify_post_process(void)
1125 static int __ftrace_modify_code(void *data)
1127 int *command = data;
1129 if (*command & FTRACE_ENABLE_CALLS)
1130 ftrace_replace_code(1);
1131 else if (*command & FTRACE_DISABLE_CALLS)
1132 ftrace_replace_code(0);
1134 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1135 ftrace_update_ftrace_func(ftrace_trace_function);
1137 if (*command & FTRACE_START_FUNC_RET)
1138 ftrace_enable_ftrace_graph_caller();
1139 else if (*command & FTRACE_STOP_FUNC_RET)
1140 ftrace_disable_ftrace_graph_caller();
1145 static void ftrace_run_update_code(int command)
1149 ret = ftrace_arch_code_modify_prepare();
1150 FTRACE_WARN_ON(ret);
1154 stop_machine(__ftrace_modify_code, &command, NULL);
1156 ret = ftrace_arch_code_modify_post_process();
1157 FTRACE_WARN_ON(ret);
1160 static ftrace_func_t saved_ftrace_func;
1161 static int ftrace_start_up;
1163 static void ftrace_startup_enable(int command)
1165 if (saved_ftrace_func != ftrace_trace_function) {
1166 saved_ftrace_func = ftrace_trace_function;
1167 command |= FTRACE_UPDATE_TRACE_FUNC;
1170 if (!command || !ftrace_enabled)
1173 ftrace_run_update_code(command);
1176 static void ftrace_startup(int command)
1178 if (unlikely(ftrace_disabled))
1182 command |= FTRACE_ENABLE_CALLS;
1184 ftrace_startup_enable(command);
1187 static void ftrace_shutdown(int command)
1189 if (unlikely(ftrace_disabled))
1194 * Just warn in case of unbalance, no need to kill ftrace, it's not
1195 * critical but the ftrace_call callers may be never nopped again after
1196 * further ftrace uses.
1198 WARN_ON_ONCE(ftrace_start_up < 0);
1200 if (!ftrace_start_up)
1201 command |= FTRACE_DISABLE_CALLS;
1203 if (saved_ftrace_func != ftrace_trace_function) {
1204 saved_ftrace_func = ftrace_trace_function;
1205 command |= FTRACE_UPDATE_TRACE_FUNC;
1208 if (!command || !ftrace_enabled)
1211 ftrace_run_update_code(command);
1214 static void ftrace_startup_sysctl(void)
1216 int command = FTRACE_ENABLE_MCOUNT;
1218 if (unlikely(ftrace_disabled))
1221 /* Force update next time */
1222 saved_ftrace_func = NULL;
1223 /* ftrace_start_up is true if we want ftrace running */
1224 if (ftrace_start_up)
1225 command |= FTRACE_ENABLE_CALLS;
1227 ftrace_run_update_code(command);
1230 static void ftrace_shutdown_sysctl(void)
1232 int command = FTRACE_DISABLE_MCOUNT;
1234 if (unlikely(ftrace_disabled))
1237 /* ftrace_start_up is true if ftrace is running */
1238 if (ftrace_start_up)
1239 command |= FTRACE_DISABLE_CALLS;
1241 ftrace_run_update_code(command);
1244 static cycle_t ftrace_update_time;
1245 static unsigned long ftrace_update_cnt;
1246 unsigned long ftrace_update_tot_cnt;
1248 static int ftrace_update_code(struct module *mod)
1250 struct dyn_ftrace *p;
1251 cycle_t start, stop;
1253 start = ftrace_now(raw_smp_processor_id());
1254 ftrace_update_cnt = 0;
1256 while (ftrace_new_addrs) {
1258 /* If something went wrong, bail without enabling anything */
1259 if (unlikely(ftrace_disabled))
1262 p = ftrace_new_addrs;
1263 ftrace_new_addrs = p->newlist;
1266 /* convert record (i.e, patch mcount-call with NOP) */
1267 if (ftrace_code_disable(mod, p)) {
1268 p->flags |= FTRACE_FL_CONVERTED;
1269 ftrace_update_cnt++;
1274 stop = ftrace_now(raw_smp_processor_id());
1275 ftrace_update_time = stop - start;
1276 ftrace_update_tot_cnt += ftrace_update_cnt;
1281 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1283 struct ftrace_page *pg;
1287 /* allocate a few pages */
1288 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1289 if (!ftrace_pages_start)
1293 * Allocate a few more pages.
1295 * TODO: have some parser search vmlinux before
1296 * final linking to find all calls to ftrace.
1298 * a) know how many pages to allocate.
1300 * b) set up the table then.
1302 * The dynamic code is still necessary for
1306 pg = ftrace_pages = ftrace_pages_start;
1308 cnt = num_to_init / ENTRIES_PER_PAGE;
1309 pr_info("ftrace: allocating %ld entries in %d pages\n",
1310 num_to_init, cnt + 1);
1312 for (i = 0; i < cnt; i++) {
1313 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1315 /* If we fail, we'll try later anyway */
1326 FTRACE_ITER_FILTER = (1 << 0),
1327 FTRACE_ITER_CONT = (1 << 1),
1328 FTRACE_ITER_NOTRACE = (1 << 2),
1329 FTRACE_ITER_FAILURES = (1 << 3),
1330 FTRACE_ITER_PRINTALL = (1 << 4),
1331 FTRACE_ITER_HASH = (1 << 5),
1334 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1336 struct ftrace_iterator {
1337 struct ftrace_page *pg;
1341 unsigned char buffer[FTRACE_BUFF_MAX+1];
1342 unsigned buffer_idx;
1347 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1349 struct ftrace_iterator *iter = m->private;
1350 struct hlist_node *hnd = v;
1351 struct hlist_head *hhd;
1353 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1358 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1361 hhd = &ftrace_func_hash[iter->hidx];
1363 if (hlist_empty(hhd)) {
1382 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1384 struct ftrace_iterator *iter = m->private;
1388 if (!(iter->flags & FTRACE_ITER_HASH))
1391 iter->flags |= FTRACE_ITER_HASH;
1394 for (l = 0; l <= *pos; ) {
1395 p = t_hash_next(m, p, &l);
1402 static int t_hash_show(struct seq_file *m, void *v)
1404 struct ftrace_func_probe *rec;
1405 struct hlist_node *hnd = v;
1407 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1409 if (rec->ops->print)
1410 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1412 seq_printf(m, "%pf:%pf", (void *)rec->ip, (void *)rec->ops->func);
1415 seq_printf(m, ":%p", rec->data);
1422 t_next(struct seq_file *m, void *v, loff_t *pos)
1424 struct ftrace_iterator *iter = m->private;
1425 struct dyn_ftrace *rec = NULL;
1427 if (iter->flags & FTRACE_ITER_HASH)
1428 return t_hash_next(m, v, pos);
1432 if (iter->flags & FTRACE_ITER_PRINTALL)
1436 if (iter->idx >= iter->pg->index) {
1437 if (iter->pg->next) {
1438 iter->pg = iter->pg->next;
1443 rec = &iter->pg->records[iter->idx++];
1444 if ((rec->flags & FTRACE_FL_FREE) ||
1446 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1447 (rec->flags & FTRACE_FL_FAILED)) ||
1449 ((iter->flags & FTRACE_ITER_FAILURES) &&
1450 !(rec->flags & FTRACE_FL_FAILED)) ||
1452 ((iter->flags & FTRACE_ITER_FILTER) &&
1453 !(rec->flags & FTRACE_FL_FILTER)) ||
1455 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1456 !(rec->flags & FTRACE_FL_NOTRACE))) {
1465 static void *t_start(struct seq_file *m, loff_t *pos)
1467 struct ftrace_iterator *iter = m->private;
1471 mutex_lock(&ftrace_lock);
1473 * For set_ftrace_filter reading, if we have the filter
1474 * off, we can short cut and just print out that all
1475 * functions are enabled.
1477 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1479 return t_hash_start(m, pos);
1480 iter->flags |= FTRACE_ITER_PRINTALL;
1484 if (iter->flags & FTRACE_ITER_HASH)
1485 return t_hash_start(m, pos);
1487 iter->pg = ftrace_pages_start;
1489 for (l = 0; l <= *pos; ) {
1490 p = t_next(m, p, &l);
1495 if (!p && iter->flags & FTRACE_ITER_FILTER)
1496 return t_hash_start(m, pos);
1501 static void t_stop(struct seq_file *m, void *p)
1503 mutex_unlock(&ftrace_lock);
1506 static int t_show(struct seq_file *m, void *v)
1508 struct ftrace_iterator *iter = m->private;
1509 struct dyn_ftrace *rec = v;
1511 if (iter->flags & FTRACE_ITER_HASH)
1512 return t_hash_show(m, v);
1514 if (iter->flags & FTRACE_ITER_PRINTALL) {
1515 seq_printf(m, "#### all functions enabled ####\n");
1522 seq_printf(m, "%pf\n", (void *)rec->ip);
1527 static struct seq_operations show_ftrace_seq_ops = {
1535 ftrace_avail_open(struct inode *inode, struct file *file)
1537 struct ftrace_iterator *iter;
1540 if (unlikely(ftrace_disabled))
1543 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1547 iter->pg = ftrace_pages_start;
1549 ret = seq_open(file, &show_ftrace_seq_ops);
1551 struct seq_file *m = file->private_data;
1561 int ftrace_avail_release(struct inode *inode, struct file *file)
1563 struct seq_file *m = (struct seq_file *)file->private_data;
1564 struct ftrace_iterator *iter = m->private;
1566 seq_release(inode, file);
1573 ftrace_failures_open(struct inode *inode, struct file *file)
1577 struct ftrace_iterator *iter;
1579 ret = ftrace_avail_open(inode, file);
1581 m = (struct seq_file *)file->private_data;
1582 iter = (struct ftrace_iterator *)m->private;
1583 iter->flags = FTRACE_ITER_FAILURES;
1590 static void ftrace_filter_reset(int enable)
1592 struct ftrace_page *pg;
1593 struct dyn_ftrace *rec;
1594 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1596 mutex_lock(&ftrace_lock);
1598 ftrace_filtered = 0;
1599 do_for_each_ftrace_rec(pg, rec) {
1600 if (rec->flags & FTRACE_FL_FAILED)
1602 rec->flags &= ~type;
1603 } while_for_each_ftrace_rec();
1604 mutex_unlock(&ftrace_lock);
1608 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1610 struct ftrace_iterator *iter;
1613 if (unlikely(ftrace_disabled))
1616 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1620 mutex_lock(&ftrace_regex_lock);
1621 if ((file->f_mode & FMODE_WRITE) &&
1622 !(file->f_flags & O_APPEND))
1623 ftrace_filter_reset(enable);
1625 if (file->f_mode & FMODE_READ) {
1626 iter->pg = ftrace_pages_start;
1627 iter->flags = enable ? FTRACE_ITER_FILTER :
1628 FTRACE_ITER_NOTRACE;
1630 ret = seq_open(file, &show_ftrace_seq_ops);
1632 struct seq_file *m = file->private_data;
1637 file->private_data = iter;
1638 mutex_unlock(&ftrace_regex_lock);
1644 ftrace_filter_open(struct inode *inode, struct file *file)
1646 return ftrace_regex_open(inode, file, 1);
1650 ftrace_notrace_open(struct inode *inode, struct file *file)
1652 return ftrace_regex_open(inode, file, 0);
1656 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1660 if (file->f_mode & FMODE_READ)
1661 ret = seq_lseek(file, offset, origin);
1663 file->f_pos = ret = 1;
1676 * (static function - no need for kernel doc)
1678 * Pass in a buffer containing a glob and this function will
1679 * set search to point to the search part of the buffer and
1680 * return the type of search it is (see enum above).
1681 * This does modify buff.
1683 * Returns enum type.
1684 * search returns the pointer to use for comparison.
1685 * not returns 1 if buff started with a '!'
1689 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1691 int type = MATCH_FULL;
1694 if (buff[0] == '!') {
1703 for (i = 0; i < len; i++) {
1704 if (buff[i] == '*') {
1707 type = MATCH_END_ONLY;
1709 if (type == MATCH_END_ONLY)
1710 type = MATCH_MIDDLE_ONLY;
1712 type = MATCH_FRONT_ONLY;
1722 static int ftrace_match(char *str, char *regex, int len, int type)
1729 if (strcmp(str, regex) == 0)
1732 case MATCH_FRONT_ONLY:
1733 if (strncmp(str, regex, len) == 0)
1736 case MATCH_MIDDLE_ONLY:
1737 if (strstr(str, regex))
1740 case MATCH_END_ONLY:
1741 ptr = strstr(str, regex);
1742 if (ptr && (ptr[len] == 0))
1751 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1753 char str[KSYM_SYMBOL_LEN];
1755 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1756 return ftrace_match(str, regex, len, type);
1759 static void ftrace_match_records(char *buff, int len, int enable)
1761 unsigned int search_len;
1762 struct ftrace_page *pg;
1763 struct dyn_ftrace *rec;
1769 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1770 type = ftrace_setup_glob(buff, len, &search, ¬);
1772 search_len = strlen(search);
1774 mutex_lock(&ftrace_lock);
1775 do_for_each_ftrace_rec(pg, rec) {
1777 if (rec->flags & FTRACE_FL_FAILED)
1780 if (ftrace_match_record(rec, search, search_len, type)) {
1782 rec->flags &= ~flag;
1787 * Only enable filtering if we have a function that
1790 if (enable && (rec->flags & FTRACE_FL_FILTER))
1791 ftrace_filtered = 1;
1792 } while_for_each_ftrace_rec();
1793 mutex_unlock(&ftrace_lock);
1797 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1798 char *regex, int len, int type)
1800 char str[KSYM_SYMBOL_LEN];
1803 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1805 if (!modname || strcmp(modname, mod))
1808 /* blank search means to match all funcs in the mod */
1810 return ftrace_match(str, regex, len, type);
1815 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1817 unsigned search_len = 0;
1818 struct ftrace_page *pg;
1819 struct dyn_ftrace *rec;
1820 int type = MATCH_FULL;
1821 char *search = buff;
1825 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1827 /* blank or '*' mean the same */
1828 if (strcmp(buff, "*") == 0)
1831 /* handle the case of 'dont filter this module' */
1832 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1838 type = ftrace_setup_glob(buff, strlen(buff), &search, ¬);
1839 search_len = strlen(search);
1842 mutex_lock(&ftrace_lock);
1843 do_for_each_ftrace_rec(pg, rec) {
1845 if (rec->flags & FTRACE_FL_FAILED)
1848 if (ftrace_match_module_record(rec, mod,
1849 search, search_len, type)) {
1851 rec->flags &= ~flag;
1855 if (enable && (rec->flags & FTRACE_FL_FILTER))
1856 ftrace_filtered = 1;
1858 } while_for_each_ftrace_rec();
1859 mutex_unlock(&ftrace_lock);
1863 * We register the module command as a template to show others how
1864 * to register the a command as well.
1868 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1873 * cmd == 'mod' because we only registered this func
1874 * for the 'mod' ftrace_func_command.
1875 * But if you register one func with multiple commands,
1876 * you can tell which command was used by the cmd
1880 /* we must have a module name */
1884 mod = strsep(¶m, ":");
1888 ftrace_match_module_records(func, mod, enable);
1892 static struct ftrace_func_command ftrace_mod_cmd = {
1894 .func = ftrace_mod_callback,
1897 static int __init ftrace_mod_cmd_init(void)
1899 return register_ftrace_command(&ftrace_mod_cmd);
1901 device_initcall(ftrace_mod_cmd_init);
1904 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1906 struct ftrace_func_probe *entry;
1907 struct hlist_head *hhd;
1908 struct hlist_node *n;
1912 key = hash_long(ip, FTRACE_HASH_BITS);
1914 hhd = &ftrace_func_hash[key];
1916 if (hlist_empty(hhd))
1920 * Disable preemption for these calls to prevent a RCU grace
1921 * period. This syncs the hash iteration and freeing of items
1922 * on the hash. rcu_read_lock is too dangerous here.
1924 resched = ftrace_preempt_disable();
1925 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1926 if (entry->ip == ip)
1927 entry->ops->func(ip, parent_ip, &entry->data);
1929 ftrace_preempt_enable(resched);
1932 static struct ftrace_ops trace_probe_ops __read_mostly =
1934 .func = function_trace_probe_call,
1937 static int ftrace_probe_registered;
1939 static void __enable_ftrace_function_probe(void)
1943 if (ftrace_probe_registered)
1946 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1947 struct hlist_head *hhd = &ftrace_func_hash[i];
1951 /* Nothing registered? */
1952 if (i == FTRACE_FUNC_HASHSIZE)
1955 __register_ftrace_function(&trace_probe_ops);
1957 ftrace_probe_registered = 1;
1960 static void __disable_ftrace_function_probe(void)
1964 if (!ftrace_probe_registered)
1967 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1968 struct hlist_head *hhd = &ftrace_func_hash[i];
1973 /* no more funcs left */
1974 __unregister_ftrace_function(&trace_probe_ops);
1976 ftrace_probe_registered = 0;
1980 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1982 struct ftrace_func_probe *entry =
1983 container_of(rhp, struct ftrace_func_probe, rcu);
1985 if (entry->ops->free)
1986 entry->ops->free(&entry->data);
1992 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1995 struct ftrace_func_probe *entry;
1996 struct ftrace_page *pg;
1997 struct dyn_ftrace *rec;
2003 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2004 len = strlen(search);
2006 /* we do not support '!' for function probes */
2010 mutex_lock(&ftrace_lock);
2011 do_for_each_ftrace_rec(pg, rec) {
2013 if (rec->flags & FTRACE_FL_FAILED)
2016 if (!ftrace_match_record(rec, search, len, type))
2019 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2021 /* If we did not process any, then return error */
2032 * The caller might want to do something special
2033 * for each function we find. We call the callback
2034 * to give the caller an opportunity to do so.
2036 if (ops->callback) {
2037 if (ops->callback(rec->ip, &entry->data) < 0) {
2038 /* caller does not like this func */
2045 entry->ip = rec->ip;
2047 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2048 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2050 } while_for_each_ftrace_rec();
2051 __enable_ftrace_function_probe();
2054 mutex_unlock(&ftrace_lock);
2060 PROBE_TEST_FUNC = 1,
2065 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2066 void *data, int flags)
2068 struct ftrace_func_probe *entry;
2069 struct hlist_node *n, *tmp;
2070 char str[KSYM_SYMBOL_LEN];
2071 int type = MATCH_FULL;
2075 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2080 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2081 len = strlen(search);
2083 /* we do not support '!' for function probes */
2088 mutex_lock(&ftrace_lock);
2089 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2090 struct hlist_head *hhd = &ftrace_func_hash[i];
2092 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2094 /* break up if statements for readability */
2095 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2098 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2101 /* do this last, since it is the most expensive */
2103 kallsyms_lookup(entry->ip, NULL, NULL,
2105 if (!ftrace_match(str, glob, len, type))
2109 hlist_del(&entry->node);
2110 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2113 __disable_ftrace_function_probe();
2114 mutex_unlock(&ftrace_lock);
2118 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2121 __unregister_ftrace_function_probe(glob, ops, data,
2122 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2126 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2128 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2131 void unregister_ftrace_function_probe_all(char *glob)
2133 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2136 static LIST_HEAD(ftrace_commands);
2137 static DEFINE_MUTEX(ftrace_cmd_mutex);
2139 int register_ftrace_command(struct ftrace_func_command *cmd)
2141 struct ftrace_func_command *p;
2144 mutex_lock(&ftrace_cmd_mutex);
2145 list_for_each_entry(p, &ftrace_commands, list) {
2146 if (strcmp(cmd->name, p->name) == 0) {
2151 list_add(&cmd->list, &ftrace_commands);
2153 mutex_unlock(&ftrace_cmd_mutex);
2158 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2160 struct ftrace_func_command *p, *n;
2163 mutex_lock(&ftrace_cmd_mutex);
2164 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2165 if (strcmp(cmd->name, p->name) == 0) {
2167 list_del_init(&p->list);
2172 mutex_unlock(&ftrace_cmd_mutex);
2177 static int ftrace_process_regex(char *buff, int len, int enable)
2179 char *func, *command, *next = buff;
2180 struct ftrace_func_command *p;
2183 func = strsep(&next, ":");
2186 ftrace_match_records(func, len, enable);
2192 command = strsep(&next, ":");
2194 mutex_lock(&ftrace_cmd_mutex);
2195 list_for_each_entry(p, &ftrace_commands, list) {
2196 if (strcmp(p->name, command) == 0) {
2197 ret = p->func(func, command, next, enable);
2202 mutex_unlock(&ftrace_cmd_mutex);
2208 ftrace_regex_write(struct file *file, const char __user *ubuf,
2209 size_t cnt, loff_t *ppos, int enable)
2211 struct ftrace_iterator *iter;
2216 if (!cnt || cnt < 0)
2219 mutex_lock(&ftrace_regex_lock);
2221 if (file->f_mode & FMODE_READ) {
2222 struct seq_file *m = file->private_data;
2225 iter = file->private_data;
2228 iter->flags &= ~FTRACE_ITER_CONT;
2229 iter->buffer_idx = 0;
2232 ret = get_user(ch, ubuf++);
2238 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2239 /* skip white space */
2240 while (cnt && isspace(ch)) {
2241 ret = get_user(ch, ubuf++);
2249 file->f_pos += read;
2254 iter->buffer_idx = 0;
2257 while (cnt && !isspace(ch)) {
2258 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2259 iter->buffer[iter->buffer_idx++] = ch;
2264 ret = get_user(ch, ubuf++);
2273 iter->buffer[iter->buffer_idx] = 0;
2274 ret = ftrace_process_regex(iter->buffer,
2275 iter->buffer_idx, enable);
2278 iter->buffer_idx = 0;
2280 iter->flags |= FTRACE_ITER_CONT;
2283 file->f_pos += read;
2287 mutex_unlock(&ftrace_regex_lock);
2293 ftrace_filter_write(struct file *file, const char __user *ubuf,
2294 size_t cnt, loff_t *ppos)
2296 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2300 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2301 size_t cnt, loff_t *ppos)
2303 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2307 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2309 if (unlikely(ftrace_disabled))
2312 mutex_lock(&ftrace_regex_lock);
2314 ftrace_filter_reset(enable);
2316 ftrace_match_records(buf, len, enable);
2317 mutex_unlock(&ftrace_regex_lock);
2321 * ftrace_set_filter - set a function to filter on in ftrace
2322 * @buf - the string that holds the function filter text.
2323 * @len - the length of the string.
2324 * @reset - non zero to reset all filters before applying this filter.
2326 * Filters denote which functions should be enabled when tracing is enabled.
2327 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2329 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2331 ftrace_set_regex(buf, len, reset, 1);
2335 * ftrace_set_notrace - set a function to not trace in ftrace
2336 * @buf - the string that holds the function notrace text.
2337 * @len - the length of the string.
2338 * @reset - non zero to reset all filters before applying this filter.
2340 * Notrace Filters denote which functions should not be enabled when tracing
2341 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2344 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2346 ftrace_set_regex(buf, len, reset, 0);
2350 * command line interface to allow users to set filters on boot up.
2352 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2353 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2354 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2356 static int __init set_ftrace_notrace(char *str)
2358 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2361 __setup("ftrace_notrace=", set_ftrace_notrace);
2363 static int __init set_ftrace_filter(char *str)
2365 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2368 __setup("ftrace_filter=", set_ftrace_filter);
2370 static void __init set_ftrace_early_filter(char *buf, int enable)
2375 func = strsep(&buf, ",");
2376 ftrace_set_regex(func, strlen(func), 0, enable);
2380 static void __init set_ftrace_early_filters(void)
2382 if (ftrace_filter_buf[0])
2383 set_ftrace_early_filter(ftrace_filter_buf, 1);
2384 if (ftrace_notrace_buf[0])
2385 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2389 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2391 struct seq_file *m = (struct seq_file *)file->private_data;
2392 struct ftrace_iterator *iter;
2394 mutex_lock(&ftrace_regex_lock);
2395 if (file->f_mode & FMODE_READ) {
2398 seq_release(inode, file);
2400 iter = file->private_data;
2402 if (iter->buffer_idx) {
2404 iter->buffer[iter->buffer_idx] = 0;
2405 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
2408 mutex_lock(&ftrace_lock);
2409 if (ftrace_start_up && ftrace_enabled)
2410 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2411 mutex_unlock(&ftrace_lock);
2414 mutex_unlock(&ftrace_regex_lock);
2419 ftrace_filter_release(struct inode *inode, struct file *file)
2421 return ftrace_regex_release(inode, file, 1);
2425 ftrace_notrace_release(struct inode *inode, struct file *file)
2427 return ftrace_regex_release(inode, file, 0);
2430 static const struct file_operations ftrace_avail_fops = {
2431 .open = ftrace_avail_open,
2433 .llseek = seq_lseek,
2434 .release = ftrace_avail_release,
2437 static const struct file_operations ftrace_failures_fops = {
2438 .open = ftrace_failures_open,
2440 .llseek = seq_lseek,
2441 .release = ftrace_avail_release,
2444 static const struct file_operations ftrace_filter_fops = {
2445 .open = ftrace_filter_open,
2447 .write = ftrace_filter_write,
2448 .llseek = ftrace_regex_lseek,
2449 .release = ftrace_filter_release,
2452 static const struct file_operations ftrace_notrace_fops = {
2453 .open = ftrace_notrace_open,
2455 .write = ftrace_notrace_write,
2456 .llseek = ftrace_regex_lseek,
2457 .release = ftrace_notrace_release,
2460 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2462 static DEFINE_MUTEX(graph_lock);
2464 int ftrace_graph_count;
2465 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2468 __g_next(struct seq_file *m, loff_t *pos)
2470 unsigned long *array = m->private;
2472 if (*pos >= ftrace_graph_count)
2474 return &array[*pos];
2478 g_next(struct seq_file *m, void *v, loff_t *pos)
2481 return __g_next(m, pos);
2484 static void *g_start(struct seq_file *m, loff_t *pos)
2486 mutex_lock(&graph_lock);
2488 /* Nothing, tell g_show to print all functions are enabled */
2489 if (!ftrace_graph_count && !*pos)
2492 return __g_next(m, pos);
2495 static void g_stop(struct seq_file *m, void *p)
2497 mutex_unlock(&graph_lock);
2500 static int g_show(struct seq_file *m, void *v)
2502 unsigned long *ptr = v;
2507 if (ptr == (unsigned long *)1) {
2508 seq_printf(m, "#### all functions enabled ####\n");
2512 seq_printf(m, "%pf\n", v);
2517 static struct seq_operations ftrace_graph_seq_ops = {
2525 ftrace_graph_open(struct inode *inode, struct file *file)
2529 if (unlikely(ftrace_disabled))
2532 mutex_lock(&graph_lock);
2533 if ((file->f_mode & FMODE_WRITE) &&
2534 !(file->f_flags & O_APPEND)) {
2535 ftrace_graph_count = 0;
2536 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2539 if (file->f_mode & FMODE_READ) {
2540 ret = seq_open(file, &ftrace_graph_seq_ops);
2542 struct seq_file *m = file->private_data;
2543 m->private = ftrace_graph_funcs;
2546 file->private_data = ftrace_graph_funcs;
2547 mutex_unlock(&graph_lock);
2553 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2555 struct dyn_ftrace *rec;
2556 struct ftrace_page *pg;
2564 if (ftrace_disabled)
2568 type = ftrace_setup_glob(buffer, strlen(buffer), &search, ¬);
2572 search_len = strlen(search);
2574 mutex_lock(&ftrace_lock);
2575 do_for_each_ftrace_rec(pg, rec) {
2577 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2580 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2583 if (ftrace_match_record(rec, search, search_len, type)) {
2584 /* ensure it is not already in the array */
2586 for (i = 0; i < *idx; i++)
2587 if (array[i] == rec->ip) {
2592 array[(*idx)++] = rec->ip;
2596 } while_for_each_ftrace_rec();
2598 mutex_unlock(&ftrace_lock);
2600 return found ? 0 : -EINVAL;
2604 ftrace_graph_write(struct file *file, const char __user *ubuf,
2605 size_t cnt, loff_t *ppos)
2607 unsigned char buffer[FTRACE_BUFF_MAX+1];
2608 unsigned long *array;
2614 if (!cnt || cnt < 0)
2617 mutex_lock(&graph_lock);
2619 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2624 if (file->f_mode & FMODE_READ) {
2625 struct seq_file *m = file->private_data;
2628 array = file->private_data;
2630 ret = get_user(ch, ubuf++);
2636 /* skip white space */
2637 while (cnt && isspace(ch)) {
2638 ret = get_user(ch, ubuf++);
2651 while (cnt && !isspace(ch)) {
2652 if (index < FTRACE_BUFF_MAX)
2653 buffer[index++] = ch;
2658 ret = get_user(ch, ubuf++);
2666 /* we allow only one expression at a time */
2667 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2671 file->f_pos += read;
2675 mutex_unlock(&graph_lock);
2680 static const struct file_operations ftrace_graph_fops = {
2681 .open = ftrace_graph_open,
2683 .write = ftrace_graph_write,
2685 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2687 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2690 trace_create_file("available_filter_functions", 0444,
2691 d_tracer, NULL, &ftrace_avail_fops);
2693 trace_create_file("failures", 0444,
2694 d_tracer, NULL, &ftrace_failures_fops);
2696 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2697 NULL, &ftrace_filter_fops);
2699 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2700 NULL, &ftrace_notrace_fops);
2702 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2703 trace_create_file("set_graph_function", 0444, d_tracer,
2705 &ftrace_graph_fops);
2706 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2711 static int ftrace_convert_nops(struct module *mod,
2712 unsigned long *start,
2717 unsigned long flags;
2719 mutex_lock(&ftrace_lock);
2722 addr = ftrace_call_adjust(*p++);
2724 * Some architecture linkers will pad between
2725 * the different mcount_loc sections of different
2726 * object files to satisfy alignments.
2727 * Skip any NULL pointers.
2731 ftrace_record_ip(addr);
2734 /* disable interrupts to prevent kstop machine */
2735 local_irq_save(flags);
2736 ftrace_update_code(mod);
2737 local_irq_restore(flags);
2738 mutex_unlock(&ftrace_lock);
2743 #ifdef CONFIG_MODULES
2744 void ftrace_release(void *start, void *end)
2746 struct dyn_ftrace *rec;
2747 struct ftrace_page *pg;
2748 unsigned long s = (unsigned long)start;
2749 unsigned long e = (unsigned long)end;
2751 if (ftrace_disabled || !start || start == end)
2754 mutex_lock(&ftrace_lock);
2755 do_for_each_ftrace_rec(pg, rec) {
2756 if ((rec->ip >= s) && (rec->ip < e)) {
2758 * rec->ip is changed in ftrace_free_rec()
2759 * It should not between s and e if record was freed.
2761 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2762 ftrace_free_rec(rec);
2764 } while_for_each_ftrace_rec();
2765 mutex_unlock(&ftrace_lock);
2768 static void ftrace_init_module(struct module *mod,
2769 unsigned long *start, unsigned long *end)
2771 if (ftrace_disabled || start == end)
2773 ftrace_convert_nops(mod, start, end);
2776 static int ftrace_module_notify(struct notifier_block *self,
2777 unsigned long val, void *data)
2779 struct module *mod = data;
2782 case MODULE_STATE_COMING:
2783 ftrace_init_module(mod, mod->ftrace_callsites,
2784 mod->ftrace_callsites +
2785 mod->num_ftrace_callsites);
2787 case MODULE_STATE_GOING:
2788 ftrace_release(mod->ftrace_callsites,
2789 mod->ftrace_callsites +
2790 mod->num_ftrace_callsites);
2797 static int ftrace_module_notify(struct notifier_block *self,
2798 unsigned long val, void *data)
2802 #endif /* CONFIG_MODULES */
2804 struct notifier_block ftrace_module_nb = {
2805 .notifier_call = ftrace_module_notify,
2809 extern unsigned long __start_mcount_loc[];
2810 extern unsigned long __stop_mcount_loc[];
2812 void __init ftrace_init(void)
2814 unsigned long count, addr, flags;
2817 /* Keep the ftrace pointer to the stub */
2818 addr = (unsigned long)ftrace_stub;
2820 local_irq_save(flags);
2821 ftrace_dyn_arch_init(&addr);
2822 local_irq_restore(flags);
2824 /* ftrace_dyn_arch_init places the return code in addr */
2828 count = __stop_mcount_loc - __start_mcount_loc;
2830 ret = ftrace_dyn_table_alloc(count);
2834 last_ftrace_enabled = ftrace_enabled = 1;
2836 ret = ftrace_convert_nops(NULL,
2840 ret = register_module_notifier(&ftrace_module_nb);
2842 pr_warning("Failed to register trace ftrace module notifier\n");
2844 set_ftrace_early_filters();
2848 ftrace_disabled = 1;
2853 static int __init ftrace_nodyn_init(void)
2858 device_initcall(ftrace_nodyn_init);
2860 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2861 static inline void ftrace_startup_enable(int command) { }
2862 /* Keep as macros so we do not need to define the commands */
2863 # define ftrace_startup(command) do { } while (0)
2864 # define ftrace_shutdown(command) do { } while (0)
2865 # define ftrace_startup_sysctl() do { } while (0)
2866 # define ftrace_shutdown_sysctl() do { } while (0)
2867 #endif /* CONFIG_DYNAMIC_FTRACE */
2870 ftrace_pid_read(struct file *file, char __user *ubuf,
2871 size_t cnt, loff_t *ppos)
2876 if (ftrace_pid_trace == ftrace_swapper_pid)
2877 r = sprintf(buf, "swapper tasks\n");
2878 else if (ftrace_pid_trace)
2879 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2881 r = sprintf(buf, "no pid\n");
2883 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2886 static void clear_ftrace_swapper(void)
2888 struct task_struct *p;
2892 for_each_online_cpu(cpu) {
2894 clear_tsk_trace_trace(p);
2899 static void set_ftrace_swapper(void)
2901 struct task_struct *p;
2905 for_each_online_cpu(cpu) {
2907 set_tsk_trace_trace(p);
2912 static void clear_ftrace_pid(struct pid *pid)
2914 struct task_struct *p;
2917 do_each_pid_task(pid, PIDTYPE_PID, p) {
2918 clear_tsk_trace_trace(p);
2919 } while_each_pid_task(pid, PIDTYPE_PID, p);
2925 static void set_ftrace_pid(struct pid *pid)
2927 struct task_struct *p;
2930 do_each_pid_task(pid, PIDTYPE_PID, p) {
2931 set_tsk_trace_trace(p);
2932 } while_each_pid_task(pid, PIDTYPE_PID, p);
2936 static void clear_ftrace_pid_task(struct pid **pid)
2938 if (*pid == ftrace_swapper_pid)
2939 clear_ftrace_swapper();
2941 clear_ftrace_pid(*pid);
2946 static void set_ftrace_pid_task(struct pid *pid)
2948 if (pid == ftrace_swapper_pid)
2949 set_ftrace_swapper();
2951 set_ftrace_pid(pid);
2955 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2956 size_t cnt, loff_t *ppos)
2963 if (cnt >= sizeof(buf))
2966 if (copy_from_user(&buf, ubuf, cnt))
2971 ret = strict_strtol(buf, 10, &val);
2975 mutex_lock(&ftrace_lock);
2977 /* disable pid tracing */
2978 if (!ftrace_pid_trace)
2981 clear_ftrace_pid_task(&ftrace_pid_trace);
2984 /* swapper task is special */
2986 pid = ftrace_swapper_pid;
2987 if (pid == ftrace_pid_trace)
2990 pid = find_get_pid(val);
2992 if (pid == ftrace_pid_trace) {
2998 if (ftrace_pid_trace)
2999 clear_ftrace_pid_task(&ftrace_pid_trace);
3004 ftrace_pid_trace = pid;
3006 set_ftrace_pid_task(ftrace_pid_trace);
3009 /* update the function call */
3010 ftrace_update_pid_func();
3011 ftrace_startup_enable(0);
3014 mutex_unlock(&ftrace_lock);
3019 static const struct file_operations ftrace_pid_fops = {
3020 .read = ftrace_pid_read,
3021 .write = ftrace_pid_write,
3024 static __init int ftrace_init_debugfs(void)
3026 struct dentry *d_tracer;
3028 d_tracer = tracing_init_dentry();
3032 ftrace_init_dyn_debugfs(d_tracer);
3034 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3035 NULL, &ftrace_pid_fops);
3037 ftrace_profile_debugfs(d_tracer);
3041 fs_initcall(ftrace_init_debugfs);
3044 * ftrace_kill - kill ftrace
3046 * This function should be used by panic code. It stops ftrace
3047 * but in a not so nice way. If you need to simply kill ftrace
3048 * from a non-atomic section, use ftrace_kill.
3050 void ftrace_kill(void)
3052 ftrace_disabled = 1;
3054 clear_ftrace_function();
3058 * register_ftrace_function - register a function for profiling
3059 * @ops - ops structure that holds the function for profiling.
3061 * Register a function to be called by all functions in the
3064 * Note: @ops->func and all the functions it calls must be labeled
3065 * with "notrace", otherwise it will go into a
3068 int register_ftrace_function(struct ftrace_ops *ops)
3072 if (unlikely(ftrace_disabled))
3075 mutex_lock(&ftrace_lock);
3077 ret = __register_ftrace_function(ops);
3080 mutex_unlock(&ftrace_lock);
3085 * unregister_ftrace_function - unregister a function for profiling.
3086 * @ops - ops structure that holds the function to unregister
3088 * Unregister a function that was added to be called by ftrace profiling.
3090 int unregister_ftrace_function(struct ftrace_ops *ops)
3094 mutex_lock(&ftrace_lock);
3095 ret = __unregister_ftrace_function(ops);
3097 mutex_unlock(&ftrace_lock);
3103 ftrace_enable_sysctl(struct ctl_table *table, int write,
3104 struct file *file, void __user *buffer, size_t *lenp,
3109 if (unlikely(ftrace_disabled))
3112 mutex_lock(&ftrace_lock);
3114 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
3116 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3119 last_ftrace_enabled = !!ftrace_enabled;
3121 if (ftrace_enabled) {
3123 ftrace_startup_sysctl();
3125 /* we are starting ftrace again */
3126 if (ftrace_list != &ftrace_list_end) {
3127 if (ftrace_list->next == &ftrace_list_end)
3128 ftrace_trace_function = ftrace_list->func;
3130 ftrace_trace_function = ftrace_list_func;
3134 /* stopping ftrace calls (just send to ftrace_stub) */
3135 ftrace_trace_function = ftrace_stub;
3137 ftrace_shutdown_sysctl();
3141 mutex_unlock(&ftrace_lock);
3145 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3147 static int ftrace_graph_active;
3148 static struct notifier_block ftrace_suspend_notifier;
3150 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3155 /* The callbacks that hook a function */
3156 trace_func_graph_ret_t ftrace_graph_return =
3157 (trace_func_graph_ret_t)ftrace_stub;
3158 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3160 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3161 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3165 unsigned long flags;
3166 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3167 struct task_struct *g, *t;
3169 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3170 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3171 * sizeof(struct ftrace_ret_stack),
3173 if (!ret_stack_list[i]) {
3181 read_lock_irqsave(&tasklist_lock, flags);
3182 do_each_thread(g, t) {
3188 if (t->ret_stack == NULL) {
3189 atomic_set(&t->tracing_graph_pause, 0);
3190 atomic_set(&t->trace_overrun, 0);
3191 t->curr_ret_stack = -1;
3192 /* Make sure the tasks see the -1 first: */
3194 t->ret_stack = ret_stack_list[start++];
3196 } while_each_thread(g, t);
3199 read_unlock_irqrestore(&tasklist_lock, flags);
3201 for (i = start; i < end; i++)
3202 kfree(ret_stack_list[i]);
3207 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3208 struct task_struct *next)
3210 unsigned long long timestamp;
3214 * Does the user want to count the time a function was asleep.
3215 * If so, do not update the time stamps.
3217 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3220 timestamp = trace_clock_local();
3222 prev->ftrace_timestamp = timestamp;
3224 /* only process tasks that we timestamped */
3225 if (!next->ftrace_timestamp)
3229 * Update all the counters in next to make up for the
3230 * time next was sleeping.
3232 timestamp -= next->ftrace_timestamp;
3234 for (index = next->curr_ret_stack; index >= 0; index--)
3235 next->ret_stack[index].calltime += timestamp;
3238 /* Allocate a return stack for each task */
3239 static int start_graph_tracing(void)
3241 struct ftrace_ret_stack **ret_stack_list;
3244 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3245 sizeof(struct ftrace_ret_stack *),
3248 if (!ret_stack_list)
3251 /* The cpu_boot init_task->ret_stack will never be freed */
3252 for_each_online_cpu(cpu) {
3253 if (!idle_task(cpu)->ret_stack)
3254 ftrace_graph_init_task(idle_task(cpu));
3258 ret = alloc_retstack_tasklist(ret_stack_list);
3259 } while (ret == -EAGAIN);
3262 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3264 pr_info("ftrace_graph: Couldn't activate tracepoint"
3265 " probe to kernel_sched_switch\n");
3268 kfree(ret_stack_list);
3273 * Hibernation protection.
3274 * The state of the current task is too much unstable during
3275 * suspend/restore to disk. We want to protect against that.
3278 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3282 case PM_HIBERNATION_PREPARE:
3283 pause_graph_tracing();
3286 case PM_POST_HIBERNATION:
3287 unpause_graph_tracing();
3293 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3294 trace_func_graph_ent_t entryfunc)
3298 mutex_lock(&ftrace_lock);
3300 /* we currently allow only one tracer registered at a time */
3301 if (ftrace_graph_active) {
3306 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3307 register_pm_notifier(&ftrace_suspend_notifier);
3309 ftrace_graph_active++;
3310 ret = start_graph_tracing();
3312 ftrace_graph_active--;
3316 ftrace_graph_return = retfunc;
3317 ftrace_graph_entry = entryfunc;
3319 ftrace_startup(FTRACE_START_FUNC_RET);
3322 mutex_unlock(&ftrace_lock);
3326 void unregister_ftrace_graph(void)
3328 mutex_lock(&ftrace_lock);
3330 if (unlikely(!ftrace_graph_active))
3333 ftrace_graph_active--;
3334 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3335 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3336 ftrace_graph_entry = ftrace_graph_entry_stub;
3337 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3338 unregister_pm_notifier(&ftrace_suspend_notifier);
3341 mutex_unlock(&ftrace_lock);
3344 /* Allocate a return stack for newly created task */
3345 void ftrace_graph_init_task(struct task_struct *t)
3347 /* Make sure we do not use the parent ret_stack */
3348 t->ret_stack = NULL;
3350 if (ftrace_graph_active) {
3351 struct ftrace_ret_stack *ret_stack;
3353 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3354 * sizeof(struct ftrace_ret_stack),
3358 t->curr_ret_stack = -1;
3359 atomic_set(&t->tracing_graph_pause, 0);
3360 atomic_set(&t->trace_overrun, 0);
3361 t->ftrace_timestamp = 0;
3362 /* make curr_ret_stack visable before we add the ret_stack */
3364 t->ret_stack = ret_stack;
3368 void ftrace_graph_exit_task(struct task_struct *t)
3370 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3372 t->ret_stack = NULL;
3373 /* NULL must become visible to IRQs before we free it: */
3379 void ftrace_graph_stop(void)