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/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/slab.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
31 #include <linux/rcupdate.h>
33 #include <trace/events/sched.h>
35 #include <asm/ftrace.h>
36 #include <asm/setup.h>
38 #include "trace_output.h"
39 #include "trace_stat.h"
41 #define FTRACE_WARN_ON(cond) \
49 #define FTRACE_WARN_ON_ONCE(cond) \
52 if (WARN_ON_ONCE(___r)) \
57 /* hash bits for specific function selection */
58 #define FTRACE_HASH_BITS 7
59 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
61 /* ftrace_enabled is a method to turn ftrace on or off */
62 int ftrace_enabled __read_mostly;
63 static int last_ftrace_enabled;
65 /* Quick disabling of function tracer. */
66 int function_trace_stop;
68 /* List for set_ftrace_pid's pids. */
69 LIST_HEAD(ftrace_pids);
71 struct list_head list;
76 * ftrace_disabled is set when an anomaly is discovered.
77 * ftrace_disabled is much stronger than ftrace_enabled.
79 static int ftrace_disabled __read_mostly;
81 static DEFINE_MUTEX(ftrace_lock);
83 static struct ftrace_ops ftrace_list_end __read_mostly =
88 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
89 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
90 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
91 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
94 * Traverse the ftrace_list, invoking all entries. The reason that we
95 * can use rcu_dereference_raw() is that elements removed from this list
96 * are simply leaked, so there is no need to interact with a grace-period
97 * mechanism. The rcu_dereference_raw() calls are needed to handle
98 * concurrent insertions into the ftrace_list.
100 * Silly Alpha and silly pointer-speculation compiler optimizations!
102 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
104 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
106 while (op != &ftrace_list_end) {
107 op->func(ip, parent_ip);
108 op = rcu_dereference_raw(op->next); /*see above*/
112 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
114 if (!test_tsk_trace_trace(current))
117 ftrace_pid_function(ip, parent_ip);
120 static void set_ftrace_pid_function(ftrace_func_t func)
122 /* do not set ftrace_pid_function to itself! */
123 if (func != ftrace_pid_func)
124 ftrace_pid_function = func;
128 * clear_ftrace_function - reset the ftrace function
130 * This NULLs the ftrace function and in essence stops
131 * tracing. There may be lag
133 void clear_ftrace_function(void)
135 ftrace_trace_function = ftrace_stub;
136 __ftrace_trace_function = ftrace_stub;
137 ftrace_pid_function = ftrace_stub;
140 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
142 * For those archs that do not test ftrace_trace_stop in their
143 * mcount call site, we need to do it from C.
145 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
147 if (function_trace_stop)
150 __ftrace_trace_function(ip, parent_ip);
154 static void update_ftrace_function(void)
159 * If there's only one function registered, then call that
160 * function directly. Otherwise, we need to iterate over the
161 * registered callers.
163 if (ftrace_list == &ftrace_list_end ||
164 ftrace_list->next == &ftrace_list_end)
165 func = ftrace_list->func;
167 func = ftrace_list_func;
169 /* If we filter on pids, update to use the pid function */
170 if (!list_empty(&ftrace_pids)) {
171 set_ftrace_pid_function(func);
172 func = ftrace_pid_func;
174 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
175 ftrace_trace_function = func;
177 __ftrace_trace_function = func;
178 ftrace_trace_function = ftrace_test_stop_func;
182 static int __register_ftrace_function(struct ftrace_ops *ops)
184 ops->next = ftrace_list;
186 * We are entering ops into the ftrace_list but another
187 * CPU might be walking that list. We need to make sure
188 * the ops->next pointer is valid before another CPU sees
189 * the ops pointer included into the ftrace_list.
191 rcu_assign_pointer(ftrace_list, ops);
194 update_ftrace_function();
199 static int __unregister_ftrace_function(struct ftrace_ops *ops)
201 struct ftrace_ops **p;
204 * If we are removing the last function, then simply point
205 * to the ftrace_stub.
207 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
208 ftrace_trace_function = ftrace_stub;
209 ftrace_list = &ftrace_list_end;
213 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
223 update_ftrace_function();
228 static void ftrace_update_pid_func(void)
230 /* Only do something if we are tracing something */
231 if (ftrace_trace_function == ftrace_stub)
234 update_ftrace_function();
237 #ifdef CONFIG_FUNCTION_PROFILER
238 struct ftrace_profile {
239 struct hlist_node node;
241 unsigned long counter;
242 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
243 unsigned long long time;
244 unsigned long long time_squared;
248 struct ftrace_profile_page {
249 struct ftrace_profile_page *next;
251 struct ftrace_profile records[];
254 struct ftrace_profile_stat {
256 struct hlist_head *hash;
257 struct ftrace_profile_page *pages;
258 struct ftrace_profile_page *start;
259 struct tracer_stat stat;
262 #define PROFILE_RECORDS_SIZE \
263 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
265 #define PROFILES_PER_PAGE \
266 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
268 static int ftrace_profile_bits __read_mostly;
269 static int ftrace_profile_enabled __read_mostly;
271 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
272 static DEFINE_MUTEX(ftrace_profile_lock);
274 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
276 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
279 function_stat_next(void *v, int idx)
281 struct ftrace_profile *rec = v;
282 struct ftrace_profile_page *pg;
284 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
290 if ((void *)rec >= (void *)&pg->records[pg->index]) {
294 rec = &pg->records[0];
302 static void *function_stat_start(struct tracer_stat *trace)
304 struct ftrace_profile_stat *stat =
305 container_of(trace, struct ftrace_profile_stat, stat);
307 if (!stat || !stat->start)
310 return function_stat_next(&stat->start->records[0], 0);
313 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
314 /* function graph compares on total time */
315 static int function_stat_cmp(void *p1, void *p2)
317 struct ftrace_profile *a = p1;
318 struct ftrace_profile *b = p2;
320 if (a->time < b->time)
322 if (a->time > b->time)
328 /* not function graph compares against hits */
329 static int function_stat_cmp(void *p1, void *p2)
331 struct ftrace_profile *a = p1;
332 struct ftrace_profile *b = p2;
334 if (a->counter < b->counter)
336 if (a->counter > b->counter)
343 static int function_stat_headers(struct seq_file *m)
345 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
346 seq_printf(m, " Function "
349 "--- ---- --- ---\n");
351 seq_printf(m, " Function Hit\n"
357 static int function_stat_show(struct seq_file *m, void *v)
359 struct ftrace_profile *rec = v;
360 char str[KSYM_SYMBOL_LEN];
362 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
363 static struct trace_seq s;
364 unsigned long long avg;
365 unsigned long long stddev;
367 mutex_lock(&ftrace_profile_lock);
369 /* we raced with function_profile_reset() */
370 if (unlikely(rec->counter == 0)) {
375 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
376 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
378 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
381 do_div(avg, rec->counter);
383 /* Sample standard deviation (s^2) */
384 if (rec->counter <= 1)
387 stddev = rec->time_squared - rec->counter * avg * avg;
389 * Divide only 1000 for ns^2 -> us^2 conversion.
390 * trace_print_graph_duration will divide 1000 again.
392 do_div(stddev, (rec->counter - 1) * 1000);
396 trace_print_graph_duration(rec->time, &s);
397 trace_seq_puts(&s, " ");
398 trace_print_graph_duration(avg, &s);
399 trace_seq_puts(&s, " ");
400 trace_print_graph_duration(stddev, &s);
401 trace_print_seq(m, &s);
405 mutex_unlock(&ftrace_profile_lock);
410 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
412 struct ftrace_profile_page *pg;
414 pg = stat->pages = stat->start;
417 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
422 memset(stat->hash, 0,
423 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
426 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
428 struct ftrace_profile_page *pg;
433 /* If we already allocated, do nothing */
437 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
441 #ifdef CONFIG_DYNAMIC_FTRACE
442 functions = ftrace_update_tot_cnt;
445 * We do not know the number of functions that exist because
446 * dynamic tracing is what counts them. With past experience
447 * we have around 20K functions. That should be more than enough.
448 * It is highly unlikely we will execute every function in
454 pg = stat->start = stat->pages;
456 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
458 for (i = 0; i < pages; i++) {
459 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
470 unsigned long tmp = (unsigned long)pg;
476 free_page((unsigned long)stat->pages);
483 static int ftrace_profile_init_cpu(int cpu)
485 struct ftrace_profile_stat *stat;
488 stat = &per_cpu(ftrace_profile_stats, cpu);
491 /* If the profile is already created, simply reset it */
492 ftrace_profile_reset(stat);
497 * We are profiling all functions, but usually only a few thousand
498 * functions are hit. We'll make a hash of 1024 items.
500 size = FTRACE_PROFILE_HASH_SIZE;
502 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
507 if (!ftrace_profile_bits) {
510 for (; size; size >>= 1)
511 ftrace_profile_bits++;
514 /* Preallocate the function profiling pages */
515 if (ftrace_profile_pages_init(stat) < 0) {
524 static int ftrace_profile_init(void)
529 for_each_online_cpu(cpu) {
530 ret = ftrace_profile_init_cpu(cpu);
538 /* interrupts must be disabled */
539 static struct ftrace_profile *
540 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
542 struct ftrace_profile *rec;
543 struct hlist_head *hhd;
544 struct hlist_node *n;
547 key = hash_long(ip, ftrace_profile_bits);
548 hhd = &stat->hash[key];
550 if (hlist_empty(hhd))
553 hlist_for_each_entry_rcu(rec, n, hhd, node) {
561 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
562 struct ftrace_profile *rec)
566 key = hash_long(rec->ip, ftrace_profile_bits);
567 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
571 * The memory is already allocated, this simply finds a new record to use.
573 static struct ftrace_profile *
574 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
576 struct ftrace_profile *rec = NULL;
578 /* prevent recursion (from NMIs) */
579 if (atomic_inc_return(&stat->disabled) != 1)
583 * Try to find the function again since an NMI
584 * could have added it
586 rec = ftrace_find_profiled_func(stat, ip);
590 if (stat->pages->index == PROFILES_PER_PAGE) {
591 if (!stat->pages->next)
593 stat->pages = stat->pages->next;
596 rec = &stat->pages->records[stat->pages->index++];
598 ftrace_add_profile(stat, rec);
601 atomic_dec(&stat->disabled);
607 function_profile_call(unsigned long ip, unsigned long parent_ip)
609 struct ftrace_profile_stat *stat;
610 struct ftrace_profile *rec;
613 if (!ftrace_profile_enabled)
616 local_irq_save(flags);
618 stat = &__get_cpu_var(ftrace_profile_stats);
619 if (!stat->hash || !ftrace_profile_enabled)
622 rec = ftrace_find_profiled_func(stat, ip);
624 rec = ftrace_profile_alloc(stat, ip);
631 local_irq_restore(flags);
634 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
635 static int profile_graph_entry(struct ftrace_graph_ent *trace)
637 function_profile_call(trace->func, 0);
641 static void profile_graph_return(struct ftrace_graph_ret *trace)
643 struct ftrace_profile_stat *stat;
644 unsigned long long calltime;
645 struct ftrace_profile *rec;
648 local_irq_save(flags);
649 stat = &__get_cpu_var(ftrace_profile_stats);
650 if (!stat->hash || !ftrace_profile_enabled)
653 /* If the calltime was zero'd ignore it */
654 if (!trace->calltime)
657 calltime = trace->rettime - trace->calltime;
659 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
662 index = trace->depth;
664 /* Append this call time to the parent time to subtract */
666 current->ret_stack[index - 1].subtime += calltime;
668 if (current->ret_stack[index].subtime < calltime)
669 calltime -= current->ret_stack[index].subtime;
674 rec = ftrace_find_profiled_func(stat, trace->func);
676 rec->time += calltime;
677 rec->time_squared += calltime * calltime;
681 local_irq_restore(flags);
684 static int register_ftrace_profiler(void)
686 return register_ftrace_graph(&profile_graph_return,
687 &profile_graph_entry);
690 static void unregister_ftrace_profiler(void)
692 unregister_ftrace_graph();
695 static struct ftrace_ops ftrace_profile_ops __read_mostly =
697 .func = function_profile_call,
700 static int register_ftrace_profiler(void)
702 return register_ftrace_function(&ftrace_profile_ops);
705 static void unregister_ftrace_profiler(void)
707 unregister_ftrace_function(&ftrace_profile_ops);
709 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
712 ftrace_profile_write(struct file *filp, const char __user *ubuf,
713 size_t cnt, loff_t *ppos)
716 char buf[64]; /* big enough to hold a number */
719 if (cnt >= sizeof(buf))
722 if (copy_from_user(&buf, ubuf, cnt))
727 ret = strict_strtoul(buf, 10, &val);
733 mutex_lock(&ftrace_profile_lock);
734 if (ftrace_profile_enabled ^ val) {
736 ret = ftrace_profile_init();
742 ret = register_ftrace_profiler();
747 ftrace_profile_enabled = 1;
749 ftrace_profile_enabled = 0;
751 * unregister_ftrace_profiler calls stop_machine
752 * so this acts like an synchronize_sched.
754 unregister_ftrace_profiler();
758 mutex_unlock(&ftrace_profile_lock);
766 ftrace_profile_read(struct file *filp, char __user *ubuf,
767 size_t cnt, loff_t *ppos)
769 char buf[64]; /* big enough to hold a number */
772 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
773 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
776 static const struct file_operations ftrace_profile_fops = {
777 .open = tracing_open_generic,
778 .read = ftrace_profile_read,
779 .write = ftrace_profile_write,
780 .llseek = default_llseek,
783 /* used to initialize the real stat files */
784 static struct tracer_stat function_stats __initdata = {
786 .stat_start = function_stat_start,
787 .stat_next = function_stat_next,
788 .stat_cmp = function_stat_cmp,
789 .stat_headers = function_stat_headers,
790 .stat_show = function_stat_show
793 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
795 struct ftrace_profile_stat *stat;
796 struct dentry *entry;
801 for_each_possible_cpu(cpu) {
802 stat = &per_cpu(ftrace_profile_stats, cpu);
804 /* allocate enough for function name + cpu number */
805 name = kmalloc(32, GFP_KERNEL);
808 * The files created are permanent, if something happens
809 * we still do not free memory.
812 "Could not allocate stat file for cpu %d\n",
816 stat->stat = function_stats;
817 snprintf(name, 32, "function%d", cpu);
818 stat->stat.name = name;
819 ret = register_stat_tracer(&stat->stat);
822 "Could not register function stat for cpu %d\n",
829 entry = debugfs_create_file("function_profile_enabled", 0644,
830 d_tracer, NULL, &ftrace_profile_fops);
832 pr_warning("Could not create debugfs "
833 "'function_profile_enabled' entry\n");
836 #else /* CONFIG_FUNCTION_PROFILER */
837 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
840 #endif /* CONFIG_FUNCTION_PROFILER */
842 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
844 #ifdef CONFIG_DYNAMIC_FTRACE
846 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
847 # error Dynamic ftrace depends on MCOUNT_RECORD
850 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
852 struct ftrace_func_probe {
853 struct hlist_node node;
854 struct ftrace_probe_ops *ops;
862 FTRACE_ENABLE_CALLS = (1 << 0),
863 FTRACE_DISABLE_CALLS = (1 << 1),
864 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
865 FTRACE_START_FUNC_RET = (1 << 3),
866 FTRACE_STOP_FUNC_RET = (1 << 4),
869 static int ftrace_filtered;
871 static struct dyn_ftrace *ftrace_new_addrs;
873 static DEFINE_MUTEX(ftrace_regex_lock);
876 struct ftrace_page *next;
878 struct dyn_ftrace records[];
881 #define ENTRIES_PER_PAGE \
882 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
884 /* estimate from running different kernels */
885 #define NR_TO_INIT 10000
887 static struct ftrace_page *ftrace_pages_start;
888 static struct ftrace_page *ftrace_pages;
890 static struct dyn_ftrace *ftrace_free_records;
893 * This is a double for. Do not use 'break' to break out of the loop,
894 * you must use a goto.
896 #define do_for_each_ftrace_rec(pg, rec) \
897 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
899 for (_____i = 0; _____i < pg->index; _____i++) { \
900 rec = &pg->records[_____i];
902 #define while_for_each_ftrace_rec() \
906 static void ftrace_free_rec(struct dyn_ftrace *rec)
908 rec->freelist = ftrace_free_records;
909 ftrace_free_records = rec;
910 rec->flags |= FTRACE_FL_FREE;
913 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
915 struct dyn_ftrace *rec;
917 /* First check for freed records */
918 if (ftrace_free_records) {
919 rec = ftrace_free_records;
921 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
922 FTRACE_WARN_ON_ONCE(1);
923 ftrace_free_records = NULL;
927 ftrace_free_records = rec->freelist;
928 memset(rec, 0, sizeof(*rec));
932 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
933 if (!ftrace_pages->next) {
934 /* allocate another page */
936 (void *)get_zeroed_page(GFP_KERNEL);
937 if (!ftrace_pages->next)
940 ftrace_pages = ftrace_pages->next;
943 return &ftrace_pages->records[ftrace_pages->index++];
946 static struct dyn_ftrace *
947 ftrace_record_ip(unsigned long ip)
949 struct dyn_ftrace *rec;
954 rec = ftrace_alloc_dyn_node(ip);
959 rec->newlist = ftrace_new_addrs;
960 ftrace_new_addrs = rec;
965 static void print_ip_ins(const char *fmt, unsigned char *p)
969 printk(KERN_CONT "%s", fmt);
971 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
972 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
975 static void ftrace_bug(int failed, unsigned long ip)
979 FTRACE_WARN_ON_ONCE(1);
980 pr_info("ftrace faulted on modifying ");
984 FTRACE_WARN_ON_ONCE(1);
985 pr_info("ftrace failed to modify ");
987 print_ip_ins(" actual: ", (unsigned char *)ip);
988 printk(KERN_CONT "\n");
991 FTRACE_WARN_ON_ONCE(1);
992 pr_info("ftrace faulted on writing ");
996 FTRACE_WARN_ON_ONCE(1);
997 pr_info("ftrace faulted on unknown error ");
1003 /* Return 1 if the address range is reserved for ftrace */
1004 int ftrace_text_reserved(void *start, void *end)
1006 struct dyn_ftrace *rec;
1007 struct ftrace_page *pg;
1009 do_for_each_ftrace_rec(pg, rec) {
1010 if (rec->ip <= (unsigned long)end &&
1011 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1013 } while_for_each_ftrace_rec();
1019 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1021 unsigned long ftrace_addr;
1022 unsigned long flag = 0UL;
1024 ftrace_addr = (unsigned long)FTRACE_ADDR;
1027 * If this record is not to be traced or we want to disable it,
1030 * If we want to enable it and filtering is off, then enable it.
1032 * If we want to enable it and filtering is on, enable it only if
1035 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1036 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1037 flag = FTRACE_FL_ENABLED;
1040 /* If the state of this record hasn't changed, then do nothing */
1041 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1045 rec->flags |= FTRACE_FL_ENABLED;
1046 return ftrace_make_call(rec, ftrace_addr);
1049 rec->flags &= ~FTRACE_FL_ENABLED;
1050 return ftrace_make_nop(NULL, rec, ftrace_addr);
1053 static void ftrace_replace_code(int enable)
1055 struct dyn_ftrace *rec;
1056 struct ftrace_page *pg;
1059 if (unlikely(ftrace_disabled))
1062 do_for_each_ftrace_rec(pg, rec) {
1063 /* Skip over free records */
1064 if (rec->flags & FTRACE_FL_FREE)
1067 failed = __ftrace_replace_code(rec, enable);
1069 ftrace_bug(failed, rec->ip);
1070 /* Stop processing */
1073 } while_for_each_ftrace_rec();
1077 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1084 if (unlikely(ftrace_disabled))
1087 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1089 ftrace_bug(ret, ip);
1096 * archs can override this function if they must do something
1097 * before the modifying code is performed.
1099 int __weak ftrace_arch_code_modify_prepare(void)
1105 * archs can override this function if they must do something
1106 * after the modifying code is performed.
1108 int __weak ftrace_arch_code_modify_post_process(void)
1113 static int __ftrace_modify_code(void *data)
1115 int *command = data;
1117 if (*command & FTRACE_ENABLE_CALLS)
1118 ftrace_replace_code(1);
1119 else if (*command & FTRACE_DISABLE_CALLS)
1120 ftrace_replace_code(0);
1122 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1123 ftrace_update_ftrace_func(ftrace_trace_function);
1125 if (*command & FTRACE_START_FUNC_RET)
1126 ftrace_enable_ftrace_graph_caller();
1127 else if (*command & FTRACE_STOP_FUNC_RET)
1128 ftrace_disable_ftrace_graph_caller();
1133 static void ftrace_run_update_code(int command)
1137 ret = ftrace_arch_code_modify_prepare();
1138 FTRACE_WARN_ON(ret);
1142 stop_machine(__ftrace_modify_code, &command, NULL);
1144 ret = ftrace_arch_code_modify_post_process();
1145 FTRACE_WARN_ON(ret);
1148 static ftrace_func_t saved_ftrace_func;
1149 static int ftrace_start_up;
1151 static void ftrace_startup_enable(int command)
1153 if (saved_ftrace_func != ftrace_trace_function) {
1154 saved_ftrace_func = ftrace_trace_function;
1155 command |= FTRACE_UPDATE_TRACE_FUNC;
1158 if (!command || !ftrace_enabled)
1161 ftrace_run_update_code(command);
1164 static void ftrace_startup(int command)
1166 if (unlikely(ftrace_disabled))
1170 command |= FTRACE_ENABLE_CALLS;
1172 ftrace_startup_enable(command);
1175 static void ftrace_shutdown(int command)
1177 if (unlikely(ftrace_disabled))
1182 * Just warn in case of unbalance, no need to kill ftrace, it's not
1183 * critical but the ftrace_call callers may be never nopped again after
1184 * further ftrace uses.
1186 WARN_ON_ONCE(ftrace_start_up < 0);
1188 if (!ftrace_start_up)
1189 command |= FTRACE_DISABLE_CALLS;
1191 if (saved_ftrace_func != ftrace_trace_function) {
1192 saved_ftrace_func = ftrace_trace_function;
1193 command |= FTRACE_UPDATE_TRACE_FUNC;
1196 if (!command || !ftrace_enabled)
1199 ftrace_run_update_code(command);
1202 static void ftrace_startup_sysctl(void)
1204 if (unlikely(ftrace_disabled))
1207 /* Force update next time */
1208 saved_ftrace_func = NULL;
1209 /* ftrace_start_up is true if we want ftrace running */
1210 if (ftrace_start_up)
1211 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1214 static void ftrace_shutdown_sysctl(void)
1216 if (unlikely(ftrace_disabled))
1219 /* ftrace_start_up is true if ftrace is running */
1220 if (ftrace_start_up)
1221 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1224 static cycle_t ftrace_update_time;
1225 static unsigned long ftrace_update_cnt;
1226 unsigned long ftrace_update_tot_cnt;
1228 static int ftrace_update_code(struct module *mod)
1230 struct dyn_ftrace *p;
1231 cycle_t start, stop;
1233 start = ftrace_now(raw_smp_processor_id());
1234 ftrace_update_cnt = 0;
1236 while (ftrace_new_addrs) {
1238 /* If something went wrong, bail without enabling anything */
1239 if (unlikely(ftrace_disabled))
1242 p = ftrace_new_addrs;
1243 ftrace_new_addrs = p->newlist;
1247 * Do the initial record conversion from mcount jump
1248 * to the NOP instructions.
1250 if (!ftrace_code_disable(mod, p)) {
1256 ftrace_update_cnt++;
1259 * If the tracing is enabled, go ahead and enable the record.
1261 * The reason not to enable the record immediatelly is the
1262 * inherent check of ftrace_make_nop/ftrace_make_call for
1263 * correct previous instructions. Making first the NOP
1264 * conversion puts the module to the correct state, thus
1265 * passing the ftrace_make_call check.
1267 if (ftrace_start_up) {
1268 int failed = __ftrace_replace_code(p, 1);
1270 ftrace_bug(failed, p->ip);
1276 stop = ftrace_now(raw_smp_processor_id());
1277 ftrace_update_time = stop - start;
1278 ftrace_update_tot_cnt += ftrace_update_cnt;
1283 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1285 struct ftrace_page *pg;
1289 /* allocate a few pages */
1290 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1291 if (!ftrace_pages_start)
1295 * Allocate a few more pages.
1297 * TODO: have some parser search vmlinux before
1298 * final linking to find all calls to ftrace.
1300 * a) know how many pages to allocate.
1302 * b) set up the table then.
1304 * The dynamic code is still necessary for
1308 pg = ftrace_pages = ftrace_pages_start;
1310 cnt = num_to_init / ENTRIES_PER_PAGE;
1311 pr_info("ftrace: allocating %ld entries in %d pages\n",
1312 num_to_init, cnt + 1);
1314 for (i = 0; i < cnt; i++) {
1315 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1317 /* If we fail, we'll try later anyway */
1328 FTRACE_ITER_FILTER = (1 << 0),
1329 FTRACE_ITER_NOTRACE = (1 << 1),
1330 FTRACE_ITER_PRINTALL = (1 << 2),
1331 FTRACE_ITER_HASH = (1 << 3),
1334 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1336 struct ftrace_iterator {
1339 struct ftrace_page *pg;
1340 struct dyn_ftrace *func;
1341 struct ftrace_func_probe *probe;
1342 struct trace_parser parser;
1349 t_hash_next(struct seq_file *m, loff_t *pos)
1351 struct ftrace_iterator *iter = m->private;
1352 struct hlist_node *hnd = NULL;
1353 struct hlist_head *hhd;
1359 hnd = &iter->probe->node;
1361 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1364 hhd = &ftrace_func_hash[iter->hidx];
1366 if (hlist_empty(hhd)) {
1382 if (WARN_ON_ONCE(!hnd))
1385 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1390 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1392 struct ftrace_iterator *iter = m->private;
1396 if (iter->func_pos > *pos)
1400 for (l = 0; l <= (*pos - iter->func_pos); ) {
1401 p = t_hash_next(m, &l);
1408 /* Only set this if we have an item */
1409 iter->flags |= FTRACE_ITER_HASH;
1415 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
1417 struct ftrace_func_probe *rec;
1420 if (WARN_ON_ONCE(!rec))
1423 if (rec->ops->print)
1424 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1426 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1429 seq_printf(m, ":%p", rec->data);
1436 t_next(struct seq_file *m, void *v, loff_t *pos)
1438 struct ftrace_iterator *iter = m->private;
1439 struct dyn_ftrace *rec = NULL;
1441 if (unlikely(ftrace_disabled))
1444 if (iter->flags & FTRACE_ITER_HASH)
1445 return t_hash_next(m, pos);
1448 iter->pos = iter->func_pos = *pos;
1450 if (iter->flags & FTRACE_ITER_PRINTALL)
1451 return t_hash_start(m, pos);
1454 if (iter->idx >= iter->pg->index) {
1455 if (iter->pg->next) {
1456 iter->pg = iter->pg->next;
1461 rec = &iter->pg->records[iter->idx++];
1462 if ((rec->flags & FTRACE_FL_FREE) ||
1464 ((iter->flags & FTRACE_ITER_FILTER) &&
1465 !(rec->flags & FTRACE_FL_FILTER)) ||
1467 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1468 !(rec->flags & FTRACE_FL_NOTRACE))) {
1475 return t_hash_start(m, pos);
1482 static void reset_iter_read(struct ftrace_iterator *iter)
1486 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
1489 static void *t_start(struct seq_file *m, loff_t *pos)
1491 struct ftrace_iterator *iter = m->private;
1495 mutex_lock(&ftrace_lock);
1497 if (unlikely(ftrace_disabled))
1501 * If an lseek was done, then reset and start from beginning.
1503 if (*pos < iter->pos)
1504 reset_iter_read(iter);
1507 * For set_ftrace_filter reading, if we have the filter
1508 * off, we can short cut and just print out that all
1509 * functions are enabled.
1511 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1513 return t_hash_start(m, pos);
1514 iter->flags |= FTRACE_ITER_PRINTALL;
1515 /* reset in case of seek/pread */
1516 iter->flags &= ~FTRACE_ITER_HASH;
1520 if (iter->flags & FTRACE_ITER_HASH)
1521 return t_hash_start(m, pos);
1524 * Unfortunately, we need to restart at ftrace_pages_start
1525 * every time we let go of the ftrace_mutex. This is because
1526 * those pointers can change without the lock.
1528 iter->pg = ftrace_pages_start;
1530 for (l = 0; l <= *pos; ) {
1531 p = t_next(m, p, &l);
1537 if (iter->flags & FTRACE_ITER_FILTER)
1538 return t_hash_start(m, pos);
1546 static void t_stop(struct seq_file *m, void *p)
1548 mutex_unlock(&ftrace_lock);
1551 static int t_show(struct seq_file *m, void *v)
1553 struct ftrace_iterator *iter = m->private;
1554 struct dyn_ftrace *rec;
1556 if (iter->flags & FTRACE_ITER_HASH)
1557 return t_hash_show(m, iter);
1559 if (iter->flags & FTRACE_ITER_PRINTALL) {
1560 seq_printf(m, "#### all functions enabled ####\n");
1569 seq_printf(m, "%ps\n", (void *)rec->ip);
1574 static const 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 static void ftrace_filter_reset(int enable)
1610 struct ftrace_page *pg;
1611 struct dyn_ftrace *rec;
1612 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1614 mutex_lock(&ftrace_lock);
1616 ftrace_filtered = 0;
1617 do_for_each_ftrace_rec(pg, rec) {
1618 rec->flags &= ~type;
1619 } while_for_each_ftrace_rec();
1620 mutex_unlock(&ftrace_lock);
1624 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1626 struct ftrace_iterator *iter;
1629 if (unlikely(ftrace_disabled))
1632 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1636 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1641 mutex_lock(&ftrace_regex_lock);
1642 if ((file->f_mode & FMODE_WRITE) &&
1643 (file->f_flags & O_TRUNC))
1644 ftrace_filter_reset(enable);
1646 if (file->f_mode & FMODE_READ) {
1647 iter->pg = ftrace_pages_start;
1648 iter->flags = enable ? FTRACE_ITER_FILTER :
1649 FTRACE_ITER_NOTRACE;
1651 ret = seq_open(file, &show_ftrace_seq_ops);
1653 struct seq_file *m = file->private_data;
1656 trace_parser_put(&iter->parser);
1660 file->private_data = iter;
1661 mutex_unlock(&ftrace_regex_lock);
1667 ftrace_filter_open(struct inode *inode, struct file *file)
1669 return ftrace_regex_open(inode, file, 1);
1673 ftrace_notrace_open(struct inode *inode, struct file *file)
1675 return ftrace_regex_open(inode, file, 0);
1679 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1683 if (file->f_mode & FMODE_READ)
1684 ret = seq_lseek(file, offset, origin);
1686 file->f_pos = ret = 1;
1691 static int ftrace_match(char *str, char *regex, int len, int type)
1698 if (strcmp(str, regex) == 0)
1701 case MATCH_FRONT_ONLY:
1702 if (strncmp(str, regex, len) == 0)
1705 case MATCH_MIDDLE_ONLY:
1706 if (strstr(str, regex))
1709 case MATCH_END_ONLY:
1711 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
1720 update_record(struct dyn_ftrace *rec, unsigned long flag, int not)
1723 rec->flags &= ~flag;
1729 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
1730 char *regex, int len, int type)
1732 char str[KSYM_SYMBOL_LEN];
1735 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1738 /* module lookup requires matching the module */
1739 if (!modname || strcmp(modname, mod))
1742 /* blank search means to match all funcs in the mod */
1747 return ftrace_match(str, regex, len, type);
1750 static int match_records(char *buff, int len, char *mod, int enable, int not)
1752 unsigned search_len = 0;
1753 struct ftrace_page *pg;
1754 struct dyn_ftrace *rec;
1755 int type = MATCH_FULL;
1756 char *search = buff;
1761 type = filter_parse_regex(buff, len, &search, ¬);
1762 search_len = strlen(search);
1765 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1767 mutex_lock(&ftrace_lock);
1769 if (unlikely(ftrace_disabled))
1772 do_for_each_ftrace_rec(pg, rec) {
1774 if (ftrace_match_record(rec, mod, search, search_len, type)) {
1775 update_record(rec, flag, not);
1779 * Only enable filtering if we have a function that
1782 if (enable && (rec->flags & FTRACE_FL_FILTER))
1783 ftrace_filtered = 1;
1785 } while_for_each_ftrace_rec();
1787 mutex_unlock(&ftrace_lock);
1793 ftrace_match_records(char *buff, int len, int enable)
1795 return match_records(buff, len, NULL, enable, 0);
1798 static int ftrace_match_module_records(char *buff, char *mod, int enable)
1802 /* blank or '*' mean the same */
1803 if (strcmp(buff, "*") == 0)
1806 /* handle the case of 'dont filter this module' */
1807 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1812 return match_records(buff, strlen(buff), mod, enable, not);
1816 * We register the module command as a template to show others how
1817 * to register the a command as well.
1821 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1826 * cmd == 'mod' because we only registered this func
1827 * for the 'mod' ftrace_func_command.
1828 * But if you register one func with multiple commands,
1829 * you can tell which command was used by the cmd
1833 /* we must have a module name */
1837 mod = strsep(¶m, ":");
1841 if (ftrace_match_module_records(func, mod, enable))
1846 static struct ftrace_func_command ftrace_mod_cmd = {
1848 .func = ftrace_mod_callback,
1851 static int __init ftrace_mod_cmd_init(void)
1853 return register_ftrace_command(&ftrace_mod_cmd);
1855 device_initcall(ftrace_mod_cmd_init);
1858 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1860 struct ftrace_func_probe *entry;
1861 struct hlist_head *hhd;
1862 struct hlist_node *n;
1865 key = hash_long(ip, FTRACE_HASH_BITS);
1867 hhd = &ftrace_func_hash[key];
1869 if (hlist_empty(hhd))
1873 * Disable preemption for these calls to prevent a RCU grace
1874 * period. This syncs the hash iteration and freeing of items
1875 * on the hash. rcu_read_lock is too dangerous here.
1877 preempt_disable_notrace();
1878 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1879 if (entry->ip == ip)
1880 entry->ops->func(ip, parent_ip, &entry->data);
1882 preempt_enable_notrace();
1885 static struct ftrace_ops trace_probe_ops __read_mostly =
1887 .func = function_trace_probe_call,
1890 static int ftrace_probe_registered;
1892 static void __enable_ftrace_function_probe(void)
1896 if (ftrace_probe_registered)
1899 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1900 struct hlist_head *hhd = &ftrace_func_hash[i];
1904 /* Nothing registered? */
1905 if (i == FTRACE_FUNC_HASHSIZE)
1908 __register_ftrace_function(&trace_probe_ops);
1910 ftrace_probe_registered = 1;
1913 static void __disable_ftrace_function_probe(void)
1917 if (!ftrace_probe_registered)
1920 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1921 struct hlist_head *hhd = &ftrace_func_hash[i];
1926 /* no more funcs left */
1927 __unregister_ftrace_function(&trace_probe_ops);
1929 ftrace_probe_registered = 0;
1933 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1935 struct ftrace_func_probe *entry =
1936 container_of(rhp, struct ftrace_func_probe, rcu);
1938 if (entry->ops->free)
1939 entry->ops->free(&entry->data);
1945 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1948 struct ftrace_func_probe *entry;
1949 struct ftrace_page *pg;
1950 struct dyn_ftrace *rec;
1956 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
1957 len = strlen(search);
1959 /* we do not support '!' for function probes */
1963 mutex_lock(&ftrace_lock);
1965 if (unlikely(ftrace_disabled))
1968 do_for_each_ftrace_rec(pg, rec) {
1970 if (!ftrace_match_record(rec, NULL, search, len, type))
1973 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1975 /* If we did not process any, then return error */
1986 * The caller might want to do something special
1987 * for each function we find. We call the callback
1988 * to give the caller an opportunity to do so.
1990 if (ops->callback) {
1991 if (ops->callback(rec->ip, &entry->data) < 0) {
1992 /* caller does not like this func */
1999 entry->ip = rec->ip;
2001 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2002 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2004 } while_for_each_ftrace_rec();
2005 __enable_ftrace_function_probe();
2008 mutex_unlock(&ftrace_lock);
2014 PROBE_TEST_FUNC = 1,
2019 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2020 void *data, int flags)
2022 struct ftrace_func_probe *entry;
2023 struct hlist_node *n, *tmp;
2024 char str[KSYM_SYMBOL_LEN];
2025 int type = MATCH_FULL;
2029 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2034 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2035 len = strlen(search);
2037 /* we do not support '!' for function probes */
2042 mutex_lock(&ftrace_lock);
2043 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2044 struct hlist_head *hhd = &ftrace_func_hash[i];
2046 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2048 /* break up if statements for readability */
2049 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2052 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2055 /* do this last, since it is the most expensive */
2057 kallsyms_lookup(entry->ip, NULL, NULL,
2059 if (!ftrace_match(str, glob, len, type))
2063 hlist_del(&entry->node);
2064 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2067 __disable_ftrace_function_probe();
2068 mutex_unlock(&ftrace_lock);
2072 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2075 __unregister_ftrace_function_probe(glob, ops, data,
2076 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2080 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2082 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2085 void unregister_ftrace_function_probe_all(char *glob)
2087 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2090 static LIST_HEAD(ftrace_commands);
2091 static DEFINE_MUTEX(ftrace_cmd_mutex);
2093 int register_ftrace_command(struct ftrace_func_command *cmd)
2095 struct ftrace_func_command *p;
2098 mutex_lock(&ftrace_cmd_mutex);
2099 list_for_each_entry(p, &ftrace_commands, list) {
2100 if (strcmp(cmd->name, p->name) == 0) {
2105 list_add(&cmd->list, &ftrace_commands);
2107 mutex_unlock(&ftrace_cmd_mutex);
2112 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2114 struct ftrace_func_command *p, *n;
2117 mutex_lock(&ftrace_cmd_mutex);
2118 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2119 if (strcmp(cmd->name, p->name) == 0) {
2121 list_del_init(&p->list);
2126 mutex_unlock(&ftrace_cmd_mutex);
2131 static int ftrace_process_regex(char *buff, int len, int enable)
2133 char *func, *command, *next = buff;
2134 struct ftrace_func_command *p;
2137 func = strsep(&next, ":");
2140 if (ftrace_match_records(func, len, enable))
2147 command = strsep(&next, ":");
2149 mutex_lock(&ftrace_cmd_mutex);
2150 list_for_each_entry(p, &ftrace_commands, list) {
2151 if (strcmp(p->name, command) == 0) {
2152 ret = p->func(func, command, next, enable);
2157 mutex_unlock(&ftrace_cmd_mutex);
2163 ftrace_regex_write(struct file *file, const char __user *ubuf,
2164 size_t cnt, loff_t *ppos, int enable)
2166 struct ftrace_iterator *iter;
2167 struct trace_parser *parser;
2173 mutex_lock(&ftrace_regex_lock);
2176 if (unlikely(ftrace_disabled))
2179 if (file->f_mode & FMODE_READ) {
2180 struct seq_file *m = file->private_data;
2183 iter = file->private_data;
2185 parser = &iter->parser;
2186 read = trace_get_user(parser, ubuf, cnt, ppos);
2188 if (read >= 0 && trace_parser_loaded(parser) &&
2189 !trace_parser_cont(parser)) {
2190 ret = ftrace_process_regex(parser->buffer,
2191 parser->idx, enable);
2192 trace_parser_clear(parser);
2199 mutex_unlock(&ftrace_regex_lock);
2205 ftrace_filter_write(struct file *file, const char __user *ubuf,
2206 size_t cnt, loff_t *ppos)
2208 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2212 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2213 size_t cnt, loff_t *ppos)
2215 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2219 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2221 if (unlikely(ftrace_disabled))
2224 mutex_lock(&ftrace_regex_lock);
2226 ftrace_filter_reset(enable);
2228 ftrace_match_records(buf, len, enable);
2229 mutex_unlock(&ftrace_regex_lock);
2233 * ftrace_set_filter - set a function to filter on in ftrace
2234 * @buf - the string that holds the function filter text.
2235 * @len - the length of the string.
2236 * @reset - non zero to reset all filters before applying this filter.
2238 * Filters denote which functions should be enabled when tracing is enabled.
2239 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2241 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2243 ftrace_set_regex(buf, len, reset, 1);
2247 * ftrace_set_notrace - set a function to not trace in ftrace
2248 * @buf - the string that holds the function notrace text.
2249 * @len - the length of the string.
2250 * @reset - non zero to reset all filters before applying this filter.
2252 * Notrace Filters denote which functions should not be enabled when tracing
2253 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2256 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2258 ftrace_set_regex(buf, len, reset, 0);
2262 * command line interface to allow users to set filters on boot up.
2264 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2265 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2266 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2268 static int __init set_ftrace_notrace(char *str)
2270 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2273 __setup("ftrace_notrace=", set_ftrace_notrace);
2275 static int __init set_ftrace_filter(char *str)
2277 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2280 __setup("ftrace_filter=", set_ftrace_filter);
2282 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2283 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2284 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2286 static int __init set_graph_function(char *str)
2288 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2291 __setup("ftrace_graph_filter=", set_graph_function);
2293 static void __init set_ftrace_early_graph(char *buf)
2299 func = strsep(&buf, ",");
2300 /* we allow only one expression at a time */
2301 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2304 printk(KERN_DEBUG "ftrace: function %s not "
2305 "traceable\n", func);
2308 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2310 static void __init set_ftrace_early_filter(char *buf, int enable)
2315 func = strsep(&buf, ",");
2316 ftrace_set_regex(func, strlen(func), 0, enable);
2320 static void __init set_ftrace_early_filters(void)
2322 if (ftrace_filter_buf[0])
2323 set_ftrace_early_filter(ftrace_filter_buf, 1);
2324 if (ftrace_notrace_buf[0])
2325 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2326 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2327 if (ftrace_graph_buf[0])
2328 set_ftrace_early_graph(ftrace_graph_buf);
2329 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2333 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2335 struct seq_file *m = (struct seq_file *)file->private_data;
2336 struct ftrace_iterator *iter;
2337 struct trace_parser *parser;
2339 mutex_lock(&ftrace_regex_lock);
2340 if (file->f_mode & FMODE_READ) {
2343 seq_release(inode, file);
2345 iter = file->private_data;
2347 parser = &iter->parser;
2348 if (trace_parser_loaded(parser)) {
2349 parser->buffer[parser->idx] = 0;
2350 ftrace_match_records(parser->buffer, parser->idx, enable);
2353 trace_parser_put(parser);
2356 if (file->f_mode & FMODE_WRITE) {
2357 mutex_lock(&ftrace_lock);
2358 if (ftrace_start_up && ftrace_enabled)
2359 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2360 mutex_unlock(&ftrace_lock);
2363 mutex_unlock(&ftrace_regex_lock);
2368 ftrace_filter_release(struct inode *inode, struct file *file)
2370 return ftrace_regex_release(inode, file, 1);
2374 ftrace_notrace_release(struct inode *inode, struct file *file)
2376 return ftrace_regex_release(inode, file, 0);
2379 static const struct file_operations ftrace_avail_fops = {
2380 .open = ftrace_avail_open,
2382 .llseek = seq_lseek,
2383 .release = seq_release_private,
2386 static const struct file_operations ftrace_filter_fops = {
2387 .open = ftrace_filter_open,
2389 .write = ftrace_filter_write,
2390 .llseek = ftrace_regex_lseek,
2391 .release = ftrace_filter_release,
2394 static const struct file_operations ftrace_notrace_fops = {
2395 .open = ftrace_notrace_open,
2397 .write = ftrace_notrace_write,
2398 .llseek = ftrace_regex_lseek,
2399 .release = ftrace_notrace_release,
2402 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2404 static DEFINE_MUTEX(graph_lock);
2406 int ftrace_graph_count;
2407 int ftrace_graph_filter_enabled;
2408 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2411 __g_next(struct seq_file *m, loff_t *pos)
2413 if (*pos >= ftrace_graph_count)
2415 return &ftrace_graph_funcs[*pos];
2419 g_next(struct seq_file *m, void *v, loff_t *pos)
2422 return __g_next(m, pos);
2425 static void *g_start(struct seq_file *m, loff_t *pos)
2427 mutex_lock(&graph_lock);
2429 /* Nothing, tell g_show to print all functions are enabled */
2430 if (!ftrace_graph_filter_enabled && !*pos)
2433 return __g_next(m, pos);
2436 static void g_stop(struct seq_file *m, void *p)
2438 mutex_unlock(&graph_lock);
2441 static int g_show(struct seq_file *m, void *v)
2443 unsigned long *ptr = v;
2448 if (ptr == (unsigned long *)1) {
2449 seq_printf(m, "#### all functions enabled ####\n");
2453 seq_printf(m, "%ps\n", (void *)*ptr);
2458 static const struct seq_operations ftrace_graph_seq_ops = {
2466 ftrace_graph_open(struct inode *inode, struct file *file)
2470 if (unlikely(ftrace_disabled))
2473 mutex_lock(&graph_lock);
2474 if ((file->f_mode & FMODE_WRITE) &&
2475 (file->f_flags & O_TRUNC)) {
2476 ftrace_graph_filter_enabled = 0;
2477 ftrace_graph_count = 0;
2478 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2480 mutex_unlock(&graph_lock);
2482 if (file->f_mode & FMODE_READ)
2483 ret = seq_open(file, &ftrace_graph_seq_ops);
2489 ftrace_graph_release(struct inode *inode, struct file *file)
2491 if (file->f_mode & FMODE_READ)
2492 seq_release(inode, file);
2497 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2499 struct dyn_ftrace *rec;
2500 struct ftrace_page *pg;
2509 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
2510 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2513 search_len = strlen(search);
2515 mutex_lock(&ftrace_lock);
2517 if (unlikely(ftrace_disabled)) {
2518 mutex_unlock(&ftrace_lock);
2522 do_for_each_ftrace_rec(pg, rec) {
2524 if (rec->flags & FTRACE_FL_FREE)
2527 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
2528 /* if it is in the array */
2530 for (i = 0; i < *idx; i++) {
2531 if (array[i] == rec->ip) {
2540 array[(*idx)++] = rec->ip;
2541 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2546 array[i] = array[--(*idx)];
2552 } while_for_each_ftrace_rec();
2554 mutex_unlock(&ftrace_lock);
2559 ftrace_graph_filter_enabled = 1;
2564 ftrace_graph_write(struct file *file, const char __user *ubuf,
2565 size_t cnt, loff_t *ppos)
2567 struct trace_parser parser;
2573 mutex_lock(&graph_lock);
2575 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2580 read = trace_get_user(&parser, ubuf, cnt, ppos);
2582 if (read >= 0 && trace_parser_loaded((&parser))) {
2583 parser.buffer[parser.idx] = 0;
2585 /* we allow only one expression at a time */
2586 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2595 trace_parser_put(&parser);
2597 mutex_unlock(&graph_lock);
2602 static const struct file_operations ftrace_graph_fops = {
2603 .open = ftrace_graph_open,
2605 .write = ftrace_graph_write,
2606 .release = ftrace_graph_release,
2607 .llseek = seq_lseek,
2609 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2611 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2614 trace_create_file("available_filter_functions", 0444,
2615 d_tracer, NULL, &ftrace_avail_fops);
2617 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2618 NULL, &ftrace_filter_fops);
2620 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2621 NULL, &ftrace_notrace_fops);
2623 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2624 trace_create_file("set_graph_function", 0444, d_tracer,
2626 &ftrace_graph_fops);
2627 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2632 static int ftrace_process_locs(struct module *mod,
2633 unsigned long *start,
2639 mutex_lock(&ftrace_lock);
2642 addr = ftrace_call_adjust(*p++);
2644 * Some architecture linkers will pad between
2645 * the different mcount_loc sections of different
2646 * object files to satisfy alignments.
2647 * Skip any NULL pointers.
2651 ftrace_record_ip(addr);
2654 ftrace_update_code(mod);
2655 mutex_unlock(&ftrace_lock);
2660 #ifdef CONFIG_MODULES
2661 void ftrace_release_mod(struct module *mod)
2663 struct dyn_ftrace *rec;
2664 struct ftrace_page *pg;
2666 mutex_lock(&ftrace_lock);
2668 if (ftrace_disabled)
2671 do_for_each_ftrace_rec(pg, rec) {
2672 if (within_module_core(rec->ip, mod)) {
2674 * rec->ip is changed in ftrace_free_rec()
2675 * It should not between s and e if record was freed.
2677 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2678 ftrace_free_rec(rec);
2680 } while_for_each_ftrace_rec();
2682 mutex_unlock(&ftrace_lock);
2685 static void ftrace_init_module(struct module *mod,
2686 unsigned long *start, unsigned long *end)
2688 if (ftrace_disabled || start == end)
2690 ftrace_process_locs(mod, start, end);
2693 static int ftrace_module_notify(struct notifier_block *self,
2694 unsigned long val, void *data)
2696 struct module *mod = data;
2699 case MODULE_STATE_COMING:
2700 ftrace_init_module(mod, mod->ftrace_callsites,
2701 mod->ftrace_callsites +
2702 mod->num_ftrace_callsites);
2704 case MODULE_STATE_GOING:
2705 ftrace_release_mod(mod);
2712 static int ftrace_module_notify(struct notifier_block *self,
2713 unsigned long val, void *data)
2717 #endif /* CONFIG_MODULES */
2719 struct notifier_block ftrace_module_nb = {
2720 .notifier_call = ftrace_module_notify,
2724 extern unsigned long __start_mcount_loc[];
2725 extern unsigned long __stop_mcount_loc[];
2727 void __init ftrace_init(void)
2729 unsigned long count, addr, flags;
2732 /* Keep the ftrace pointer to the stub */
2733 addr = (unsigned long)ftrace_stub;
2735 local_irq_save(flags);
2736 ftrace_dyn_arch_init(&addr);
2737 local_irq_restore(flags);
2739 /* ftrace_dyn_arch_init places the return code in addr */
2743 count = __stop_mcount_loc - __start_mcount_loc;
2745 ret = ftrace_dyn_table_alloc(count);
2749 last_ftrace_enabled = ftrace_enabled = 1;
2751 ret = ftrace_process_locs(NULL,
2755 ret = register_module_notifier(&ftrace_module_nb);
2757 pr_warning("Failed to register trace ftrace module notifier\n");
2759 set_ftrace_early_filters();
2763 ftrace_disabled = 1;
2768 static int __init ftrace_nodyn_init(void)
2773 device_initcall(ftrace_nodyn_init);
2775 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2776 static inline void ftrace_startup_enable(int command) { }
2777 /* Keep as macros so we do not need to define the commands */
2778 # define ftrace_startup(command) do { } while (0)
2779 # define ftrace_shutdown(command) do { } while (0)
2780 # define ftrace_startup_sysctl() do { } while (0)
2781 # define ftrace_shutdown_sysctl() do { } while (0)
2782 #endif /* CONFIG_DYNAMIC_FTRACE */
2784 static void clear_ftrace_swapper(void)
2786 struct task_struct *p;
2790 for_each_online_cpu(cpu) {
2792 clear_tsk_trace_trace(p);
2797 static void set_ftrace_swapper(void)
2799 struct task_struct *p;
2803 for_each_online_cpu(cpu) {
2805 set_tsk_trace_trace(p);
2810 static void clear_ftrace_pid(struct pid *pid)
2812 struct task_struct *p;
2815 do_each_pid_task(pid, PIDTYPE_PID, p) {
2816 clear_tsk_trace_trace(p);
2817 } while_each_pid_task(pid, PIDTYPE_PID, p);
2823 static void set_ftrace_pid(struct pid *pid)
2825 struct task_struct *p;
2828 do_each_pid_task(pid, PIDTYPE_PID, p) {
2829 set_tsk_trace_trace(p);
2830 } while_each_pid_task(pid, PIDTYPE_PID, p);
2834 static void clear_ftrace_pid_task(struct pid *pid)
2836 if (pid == ftrace_swapper_pid)
2837 clear_ftrace_swapper();
2839 clear_ftrace_pid(pid);
2842 static void set_ftrace_pid_task(struct pid *pid)
2844 if (pid == ftrace_swapper_pid)
2845 set_ftrace_swapper();
2847 set_ftrace_pid(pid);
2850 static int ftrace_pid_add(int p)
2853 struct ftrace_pid *fpid;
2856 mutex_lock(&ftrace_lock);
2859 pid = ftrace_swapper_pid;
2861 pid = find_get_pid(p);
2868 list_for_each_entry(fpid, &ftrace_pids, list)
2869 if (fpid->pid == pid)
2874 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2878 list_add(&fpid->list, &ftrace_pids);
2881 set_ftrace_pid_task(pid);
2883 ftrace_update_pid_func();
2884 ftrace_startup_enable(0);
2886 mutex_unlock(&ftrace_lock);
2890 if (pid != ftrace_swapper_pid)
2894 mutex_unlock(&ftrace_lock);
2898 static void ftrace_pid_reset(void)
2900 struct ftrace_pid *fpid, *safe;
2902 mutex_lock(&ftrace_lock);
2903 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2904 struct pid *pid = fpid->pid;
2906 clear_ftrace_pid_task(pid);
2908 list_del(&fpid->list);
2912 ftrace_update_pid_func();
2913 ftrace_startup_enable(0);
2915 mutex_unlock(&ftrace_lock);
2918 static void *fpid_start(struct seq_file *m, loff_t *pos)
2920 mutex_lock(&ftrace_lock);
2922 if (list_empty(&ftrace_pids) && (!*pos))
2925 return seq_list_start(&ftrace_pids, *pos);
2928 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2933 return seq_list_next(v, &ftrace_pids, pos);
2936 static void fpid_stop(struct seq_file *m, void *p)
2938 mutex_unlock(&ftrace_lock);
2941 static int fpid_show(struct seq_file *m, void *v)
2943 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2945 if (v == (void *)1) {
2946 seq_printf(m, "no pid\n");
2950 if (fpid->pid == ftrace_swapper_pid)
2951 seq_printf(m, "swapper tasks\n");
2953 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2958 static const struct seq_operations ftrace_pid_sops = {
2959 .start = fpid_start,
2966 ftrace_pid_open(struct inode *inode, struct file *file)
2970 if ((file->f_mode & FMODE_WRITE) &&
2971 (file->f_flags & O_TRUNC))
2974 if (file->f_mode & FMODE_READ)
2975 ret = seq_open(file, &ftrace_pid_sops);
2981 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2982 size_t cnt, loff_t *ppos)
2988 if (cnt >= sizeof(buf))
2991 if (copy_from_user(&buf, ubuf, cnt))
2997 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
2998 * to clean the filter quietly.
3000 tmp = strstrip(buf);
3001 if (strlen(tmp) == 0)
3004 ret = strict_strtol(tmp, 10, &val);
3008 ret = ftrace_pid_add(val);
3010 return ret ? ret : cnt;
3014 ftrace_pid_release(struct inode *inode, struct file *file)
3016 if (file->f_mode & FMODE_READ)
3017 seq_release(inode, file);
3022 static const struct file_operations ftrace_pid_fops = {
3023 .open = ftrace_pid_open,
3024 .write = ftrace_pid_write,
3026 .llseek = seq_lseek,
3027 .release = ftrace_pid_release,
3030 static __init int ftrace_init_debugfs(void)
3032 struct dentry *d_tracer;
3034 d_tracer = tracing_init_dentry();
3038 ftrace_init_dyn_debugfs(d_tracer);
3040 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3041 NULL, &ftrace_pid_fops);
3043 ftrace_profile_debugfs(d_tracer);
3047 fs_initcall(ftrace_init_debugfs);
3050 * ftrace_kill - kill ftrace
3052 * This function should be used by panic code. It stops ftrace
3053 * but in a not so nice way. If you need to simply kill ftrace
3054 * from a non-atomic section, use ftrace_kill.
3056 void ftrace_kill(void)
3058 ftrace_disabled = 1;
3060 clear_ftrace_function();
3064 * register_ftrace_function - register a function for profiling
3065 * @ops - ops structure that holds the function for profiling.
3067 * Register a function to be called by all functions in the
3070 * Note: @ops->func and all the functions it calls must be labeled
3071 * with "notrace", otherwise it will go into a
3074 int register_ftrace_function(struct ftrace_ops *ops)
3078 mutex_lock(&ftrace_lock);
3080 if (unlikely(ftrace_disabled))
3083 ret = __register_ftrace_function(ops);
3087 mutex_unlock(&ftrace_lock);
3092 * unregister_ftrace_function - unregister a function for profiling.
3093 * @ops - ops structure that holds the function to unregister
3095 * Unregister a function that was added to be called by ftrace profiling.
3097 int unregister_ftrace_function(struct ftrace_ops *ops)
3101 mutex_lock(&ftrace_lock);
3102 ret = __unregister_ftrace_function(ops);
3104 mutex_unlock(&ftrace_lock);
3110 ftrace_enable_sysctl(struct ctl_table *table, int write,
3111 void __user *buffer, size_t *lenp,
3116 mutex_lock(&ftrace_lock);
3118 if (unlikely(ftrace_disabled))
3121 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3123 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3126 last_ftrace_enabled = !!ftrace_enabled;
3128 if (ftrace_enabled) {
3130 ftrace_startup_sysctl();
3132 /* we are starting ftrace again */
3133 if (ftrace_list != &ftrace_list_end) {
3134 if (ftrace_list->next == &ftrace_list_end)
3135 ftrace_trace_function = ftrace_list->func;
3137 ftrace_trace_function = ftrace_list_func;
3141 /* stopping ftrace calls (just send to ftrace_stub) */
3142 ftrace_trace_function = ftrace_stub;
3144 ftrace_shutdown_sysctl();
3148 mutex_unlock(&ftrace_lock);
3152 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3154 static int ftrace_graph_active;
3155 static struct notifier_block ftrace_suspend_notifier;
3157 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3162 /* The callbacks that hook a function */
3163 trace_func_graph_ret_t ftrace_graph_return =
3164 (trace_func_graph_ret_t)ftrace_stub;
3165 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3167 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3168 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3172 unsigned long flags;
3173 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3174 struct task_struct *g, *t;
3176 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3177 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3178 * sizeof(struct ftrace_ret_stack),
3180 if (!ret_stack_list[i]) {
3188 read_lock_irqsave(&tasklist_lock, flags);
3189 do_each_thread(g, t) {
3195 if (t->ret_stack == NULL) {
3196 atomic_set(&t->tracing_graph_pause, 0);
3197 atomic_set(&t->trace_overrun, 0);
3198 t->curr_ret_stack = -1;
3199 /* Make sure the tasks see the -1 first: */
3201 t->ret_stack = ret_stack_list[start++];
3203 } while_each_thread(g, t);
3206 read_unlock_irqrestore(&tasklist_lock, flags);
3208 for (i = start; i < end; i++)
3209 kfree(ret_stack_list[i]);
3214 ftrace_graph_probe_sched_switch(void *ignore,
3215 struct task_struct *prev, struct task_struct *next)
3217 unsigned long long timestamp;
3221 * Does the user want to count the time a function was asleep.
3222 * If so, do not update the time stamps.
3224 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3227 timestamp = trace_clock_local();
3229 prev->ftrace_timestamp = timestamp;
3231 /* only process tasks that we timestamped */
3232 if (!next->ftrace_timestamp)
3236 * Update all the counters in next to make up for the
3237 * time next was sleeping.
3239 timestamp -= next->ftrace_timestamp;
3241 for (index = next->curr_ret_stack; index >= 0; index--)
3242 next->ret_stack[index].calltime += timestamp;
3245 /* Allocate a return stack for each task */
3246 static int start_graph_tracing(void)
3248 struct ftrace_ret_stack **ret_stack_list;
3251 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3252 sizeof(struct ftrace_ret_stack *),
3255 if (!ret_stack_list)
3258 /* The cpu_boot init_task->ret_stack will never be freed */
3259 for_each_online_cpu(cpu) {
3260 if (!idle_task(cpu)->ret_stack)
3261 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
3265 ret = alloc_retstack_tasklist(ret_stack_list);
3266 } while (ret == -EAGAIN);
3269 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3271 pr_info("ftrace_graph: Couldn't activate tracepoint"
3272 " probe to kernel_sched_switch\n");
3275 kfree(ret_stack_list);
3280 * Hibernation protection.
3281 * The state of the current task is too much unstable during
3282 * suspend/restore to disk. We want to protect against that.
3285 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3289 case PM_HIBERNATION_PREPARE:
3290 pause_graph_tracing();
3293 case PM_POST_HIBERNATION:
3294 unpause_graph_tracing();
3300 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3301 trace_func_graph_ent_t entryfunc)
3305 mutex_lock(&ftrace_lock);
3307 /* we currently allow only one tracer registered at a time */
3308 if (ftrace_graph_active) {
3313 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3314 register_pm_notifier(&ftrace_suspend_notifier);
3316 ftrace_graph_active++;
3317 ret = start_graph_tracing();
3319 ftrace_graph_active--;
3323 ftrace_graph_return = retfunc;
3324 ftrace_graph_entry = entryfunc;
3326 ftrace_startup(FTRACE_START_FUNC_RET);
3329 mutex_unlock(&ftrace_lock);
3333 void unregister_ftrace_graph(void)
3335 mutex_lock(&ftrace_lock);
3337 if (unlikely(!ftrace_graph_active))
3340 ftrace_graph_active--;
3341 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3342 ftrace_graph_entry = ftrace_graph_entry_stub;
3343 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3344 unregister_pm_notifier(&ftrace_suspend_notifier);
3345 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3348 mutex_unlock(&ftrace_lock);
3351 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
3354 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
3356 atomic_set(&t->tracing_graph_pause, 0);
3357 atomic_set(&t->trace_overrun, 0);
3358 t->ftrace_timestamp = 0;
3359 /* make curr_ret_stack visible before we add the ret_stack */
3361 t->ret_stack = ret_stack;
3365 * Allocate a return stack for the idle task. May be the first
3366 * time through, or it may be done by CPU hotplug online.
3368 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
3370 t->curr_ret_stack = -1;
3372 * The idle task has no parent, it either has its own
3373 * stack or no stack at all.
3376 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
3378 if (ftrace_graph_active) {
3379 struct ftrace_ret_stack *ret_stack;
3381 ret_stack = per_cpu(idle_ret_stack, cpu);
3383 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3384 * sizeof(struct ftrace_ret_stack),
3388 per_cpu(idle_ret_stack, cpu) = ret_stack;
3390 graph_init_task(t, ret_stack);
3394 /* Allocate a return stack for newly created task */
3395 void ftrace_graph_init_task(struct task_struct *t)
3397 /* Make sure we do not use the parent ret_stack */
3398 t->ret_stack = NULL;
3399 t->curr_ret_stack = -1;
3401 if (ftrace_graph_active) {
3402 struct ftrace_ret_stack *ret_stack;
3404 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3405 * sizeof(struct ftrace_ret_stack),
3409 graph_init_task(t, ret_stack);
3413 void ftrace_graph_exit_task(struct task_struct *t)
3415 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3417 t->ret_stack = NULL;
3418 /* NULL must become visible to IRQs before we free it: */
3424 void ftrace_graph_stop(void)