2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/kprobes.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
32 #include <trace/sched.h>
34 #include <asm/ftrace.h>
37 #include "trace_stat.h"
39 #define FTRACE_WARN_ON(cond) \
45 #define FTRACE_WARN_ON_ONCE(cond) \
47 if (WARN_ON_ONCE(cond)) \
51 /* hash bits for specific function selection */
52 #define FTRACE_HASH_BITS 7
53 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
55 /* ftrace_enabled is a method to turn ftrace on or off */
56 int ftrace_enabled __read_mostly;
57 static int last_ftrace_enabled;
59 /* Quick disabling of function tracer. */
60 int function_trace_stop;
63 * ftrace_disabled is set when an anomaly is discovered.
64 * ftrace_disabled is much stronger than ftrace_enabled.
66 static int ftrace_disabled __read_mostly;
68 static DEFINE_MUTEX(ftrace_lock);
70 static struct ftrace_ops ftrace_list_end __read_mostly =
75 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
76 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
77 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
78 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
80 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
82 struct ftrace_ops *op = ftrace_list;
84 /* in case someone actually ports this to alpha! */
85 read_barrier_depends();
87 while (op != &ftrace_list_end) {
89 read_barrier_depends();
90 op->func(ip, parent_ip);
95 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
97 if (!test_tsk_trace_trace(current))
100 ftrace_pid_function(ip, parent_ip);
103 static void set_ftrace_pid_function(ftrace_func_t func)
105 /* do not set ftrace_pid_function to itself! */
106 if (func != ftrace_pid_func)
107 ftrace_pid_function = func;
111 * clear_ftrace_function - reset the ftrace function
113 * This NULLs the ftrace function and in essence stops
114 * tracing. There may be lag
116 void clear_ftrace_function(void)
118 ftrace_trace_function = ftrace_stub;
119 __ftrace_trace_function = ftrace_stub;
120 ftrace_pid_function = ftrace_stub;
123 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
125 * For those archs that do not test ftrace_trace_stop in their
126 * mcount call site, we need to do it from C.
128 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
130 if (function_trace_stop)
133 __ftrace_trace_function(ip, parent_ip);
137 static int __register_ftrace_function(struct ftrace_ops *ops)
139 ops->next = ftrace_list;
141 * We are entering ops into the ftrace_list but another
142 * CPU might be walking that list. We need to make sure
143 * the ops->next pointer is valid before another CPU sees
144 * the ops pointer included into the ftrace_list.
149 if (ftrace_enabled) {
152 if (ops->next == &ftrace_list_end)
155 func = ftrace_list_func;
157 if (ftrace_pid_trace) {
158 set_ftrace_pid_function(func);
159 func = ftrace_pid_func;
163 * For one func, simply call it directly.
164 * For more than one func, call the chain.
166 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
167 ftrace_trace_function = func;
169 __ftrace_trace_function = func;
170 ftrace_trace_function = ftrace_test_stop_func;
177 static int __unregister_ftrace_function(struct ftrace_ops *ops)
179 struct ftrace_ops **p;
182 * If we are removing the last function, then simply point
183 * to the ftrace_stub.
185 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
186 ftrace_trace_function = ftrace_stub;
187 ftrace_list = &ftrace_list_end;
191 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
200 if (ftrace_enabled) {
201 /* If we only have one func left, then call that directly */
202 if (ftrace_list->next == &ftrace_list_end) {
203 ftrace_func_t func = ftrace_list->func;
205 if (ftrace_pid_trace) {
206 set_ftrace_pid_function(func);
207 func = ftrace_pid_func;
209 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
210 ftrace_trace_function = func;
212 __ftrace_trace_function = func;
220 static void ftrace_update_pid_func(void)
224 if (ftrace_trace_function == ftrace_stub)
227 func = ftrace_trace_function;
229 if (ftrace_pid_trace) {
230 set_ftrace_pid_function(func);
231 func = ftrace_pid_func;
233 if (func == ftrace_pid_func)
234 func = ftrace_pid_function;
237 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
238 ftrace_trace_function = func;
240 __ftrace_trace_function = func;
244 /* set when tracing only a pid */
245 struct pid *ftrace_pid_trace;
246 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
248 #ifdef CONFIG_DYNAMIC_FTRACE
250 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
251 # error Dynamic ftrace depends on MCOUNT_RECORD
254 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
256 struct ftrace_func_probe {
257 struct hlist_node node;
258 struct ftrace_probe_ops *ops;
266 FTRACE_ENABLE_CALLS = (1 << 0),
267 FTRACE_DISABLE_CALLS = (1 << 1),
268 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
269 FTRACE_ENABLE_MCOUNT = (1 << 3),
270 FTRACE_DISABLE_MCOUNT = (1 << 4),
271 FTRACE_START_FUNC_RET = (1 << 5),
272 FTRACE_STOP_FUNC_RET = (1 << 6),
275 static int ftrace_filtered;
277 static struct dyn_ftrace *ftrace_new_addrs;
279 static DEFINE_MUTEX(ftrace_regex_lock);
282 struct ftrace_page *next;
284 struct dyn_ftrace records[];
287 #define ENTRIES_PER_PAGE \
288 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
290 /* estimate from running different kernels */
291 #define NR_TO_INIT 10000
293 static struct ftrace_page *ftrace_pages_start;
294 static struct ftrace_page *ftrace_pages;
296 static struct dyn_ftrace *ftrace_free_records;
299 * This is a double for. Do not use 'break' to break out of the loop,
300 * you must use a goto.
302 #define do_for_each_ftrace_rec(pg, rec) \
303 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
305 for (_____i = 0; _____i < pg->index; _____i++) { \
306 rec = &pg->records[_____i];
308 #define while_for_each_ftrace_rec() \
312 #ifdef CONFIG_FUNCTION_PROFILER
313 static struct hlist_head *ftrace_profile_hash;
314 static int ftrace_profile_bits;
315 static int ftrace_profile_enabled;
316 static DEFINE_MUTEX(ftrace_profile_lock);
319 function_stat_next(void *v, int idx)
321 struct dyn_ftrace *rec = v;
322 struct ftrace_page *pg;
324 pg = (struct ftrace_page *)((unsigned long)rec & PAGE_MASK);
328 if ((void *)rec >= (void *)&pg->records[pg->index]) {
332 rec = &pg->records[0];
335 if (rec->flags & FTRACE_FL_FREE ||
336 rec->flags & FTRACE_FL_FAILED ||
337 !(rec->flags & FTRACE_FL_CONVERTED) ||
338 /* ignore non hit functions */
345 static void *function_stat_start(struct tracer_stat *trace)
347 return function_stat_next(&ftrace_pages_start->records[0], 0);
350 static int function_stat_cmp(void *p1, void *p2)
352 struct dyn_ftrace *a = p1;
353 struct dyn_ftrace *b = p2;
355 if (a->counter < b->counter)
357 if (a->counter > b->counter)
363 static int function_stat_headers(struct seq_file *m)
365 seq_printf(m, " Function Hit\n"
370 static int function_stat_show(struct seq_file *m, void *v)
372 struct dyn_ftrace *rec = v;
373 char str[KSYM_SYMBOL_LEN];
375 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
377 seq_printf(m, " %-30.30s %10lu\n", str, rec->counter);
381 static struct tracer_stat function_stats = {
383 .stat_start = function_stat_start,
384 .stat_next = function_stat_next,
385 .stat_cmp = function_stat_cmp,
386 .stat_headers = function_stat_headers,
387 .stat_show = function_stat_show
390 static void ftrace_profile_init(int nr_funcs)
397 * We are profiling all functions, lets make it 1/4th of the
398 * number of functions that are in core kernel. So we have to
401 order = (sizeof(struct hlist_head) * nr_funcs) / 4;
402 order = get_order(order);
403 size = 1 << (PAGE_SHIFT + order);
405 pr_info("Allocating %d KB for profiler hash\n", size >> 10);
407 addr = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
409 pr_warning("Could not allocate function profiler hash\n");
413 ftrace_profile_hash = (void *)addr;
416 * struct hlist_head should be a pointer of 4 or 8 bytes.
417 * And a simple bit manipulation can be done, but if for
418 * some reason struct hlist_head is not a mulitple of 2,
419 * then we play it safe, and simply count. This function
420 * is done once at boot up, so it is not that critical in
425 size /= sizeof(struct hlist_head);
427 for (; size; size >>= 1)
428 ftrace_profile_bits++;
430 pr_info("Function profiler has %d hash buckets\n",
431 1 << ftrace_profile_bits);
437 ftrace_profile_read(struct file *filp, char __user *ubuf,
438 size_t cnt, loff_t *ppos)
443 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
444 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
447 static void ftrace_profile_reset(void)
449 struct dyn_ftrace *rec;
450 struct ftrace_page *pg;
452 do_for_each_ftrace_rec(pg, rec) {
454 } while_for_each_ftrace_rec();
457 static struct dyn_ftrace *ftrace_find_profiled_func(unsigned long ip)
459 struct dyn_ftrace *rec;
460 struct hlist_head *hhd;
461 struct hlist_node *n;
465 if (!ftrace_profile_hash)
468 key = hash_long(ip, ftrace_profile_bits);
469 hhd = &ftrace_profile_hash[key];
471 if (hlist_empty(hhd))
474 local_irq_save(flags);
475 hlist_for_each_entry_rcu(rec, n, hhd, node) {
481 local_irq_restore(flags);
487 function_profile_call(unsigned long ip, unsigned long parent_ip)
489 struct dyn_ftrace *rec;
492 if (!ftrace_profile_enabled)
495 local_irq_save(flags);
496 rec = ftrace_find_profiled_func(ip);
502 local_irq_restore(flags);
505 static struct ftrace_ops ftrace_profile_ops __read_mostly =
507 .func = function_profile_call,
511 ftrace_profile_write(struct file *filp, const char __user *ubuf,
512 size_t cnt, loff_t *ppos)
518 if (!ftrace_profile_hash) {
519 pr_info("Can not enable hash due to earlier problems\n");
523 if (cnt >= sizeof(buf))
526 if (copy_from_user(&buf, ubuf, cnt))
531 ret = strict_strtoul(buf, 10, &val);
537 mutex_lock(&ftrace_profile_lock);
538 if (ftrace_profile_enabled ^ val) {
540 ftrace_profile_reset();
541 register_ftrace_function(&ftrace_profile_ops);
542 ftrace_profile_enabled = 1;
544 ftrace_profile_enabled = 0;
545 unregister_ftrace_function(&ftrace_profile_ops);
548 mutex_unlock(&ftrace_profile_lock);
555 static const struct file_operations ftrace_profile_fops = {
556 .open = tracing_open_generic,
557 .read = ftrace_profile_read,
558 .write = ftrace_profile_write,
561 static void ftrace_profile_debugfs(struct dentry *d_tracer)
563 struct dentry *entry;
566 ret = register_stat_tracer(&function_stats);
568 pr_warning("Warning: could not register "
573 entry = debugfs_create_file("function_profile_enabled", 0644,
574 d_tracer, NULL, &ftrace_profile_fops);
576 pr_warning("Could not create debugfs "
577 "'function_profile_enabled' entry\n");
580 static void ftrace_add_profile(struct dyn_ftrace *rec)
584 if (!ftrace_profile_hash)
587 key = hash_long(rec->ip, ftrace_profile_bits);
588 hlist_add_head_rcu(&rec->node, &ftrace_profile_hash[key]);
591 static void ftrace_profile_release(struct dyn_ftrace *rec)
593 mutex_lock(&ftrace_profile_lock);
594 hlist_del(&rec->node);
595 mutex_unlock(&ftrace_profile_lock);
598 #else /* CONFIG_FUNCTION_PROFILER */
599 static void ftrace_profile_init(int nr_funcs)
602 static void ftrace_add_profile(struct dyn_ftrace *rec)
605 static void ftrace_profile_debugfs(struct dentry *d_tracer)
608 static void ftrace_profile_release(struct dyn_ftrace *rec)
611 #endif /* CONFIG_FUNCTION_PROFILER */
613 #ifdef CONFIG_KPROBES
615 static int frozen_record_count;
617 static inline void freeze_record(struct dyn_ftrace *rec)
619 if (!(rec->flags & FTRACE_FL_FROZEN)) {
620 rec->flags |= FTRACE_FL_FROZEN;
621 frozen_record_count++;
625 static inline void unfreeze_record(struct dyn_ftrace *rec)
627 if (rec->flags & FTRACE_FL_FROZEN) {
628 rec->flags &= ~FTRACE_FL_FROZEN;
629 frozen_record_count--;
633 static inline int record_frozen(struct dyn_ftrace *rec)
635 return rec->flags & FTRACE_FL_FROZEN;
638 # define freeze_record(rec) ({ 0; })
639 # define unfreeze_record(rec) ({ 0; })
640 # define record_frozen(rec) ({ 0; })
641 #endif /* CONFIG_KPROBES */
643 static void ftrace_free_rec(struct dyn_ftrace *rec)
645 rec->freelist = ftrace_free_records;
646 ftrace_free_records = rec;
647 rec->flags |= FTRACE_FL_FREE;
650 void ftrace_release(void *start, unsigned long size)
652 struct dyn_ftrace *rec;
653 struct ftrace_page *pg;
654 unsigned long s = (unsigned long)start;
655 unsigned long e = s + size;
657 if (ftrace_disabled || !start)
660 mutex_lock(&ftrace_lock);
661 do_for_each_ftrace_rec(pg, rec) {
662 if ((rec->ip >= s) && (rec->ip < e) &&
663 !(rec->flags & FTRACE_FL_FREE)) {
664 ftrace_free_rec(rec);
665 ftrace_profile_release(rec);
667 } while_for_each_ftrace_rec();
668 mutex_unlock(&ftrace_lock);
671 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
673 struct dyn_ftrace *rec;
675 /* First check for freed records */
676 if (ftrace_free_records) {
677 rec = ftrace_free_records;
679 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
680 FTRACE_WARN_ON_ONCE(1);
681 ftrace_free_records = NULL;
685 ftrace_free_records = rec->freelist;
686 memset(rec, 0, sizeof(*rec));
690 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
691 if (!ftrace_pages->next) {
692 /* allocate another page */
694 (void *)get_zeroed_page(GFP_KERNEL);
695 if (!ftrace_pages->next)
698 ftrace_pages = ftrace_pages->next;
701 return &ftrace_pages->records[ftrace_pages->index++];
704 static struct dyn_ftrace *
705 ftrace_record_ip(unsigned long ip)
707 struct dyn_ftrace *rec;
712 rec = ftrace_alloc_dyn_node(ip);
717 rec->newlist = ftrace_new_addrs;
718 ftrace_new_addrs = rec;
720 ftrace_add_profile(rec);
725 static void print_ip_ins(const char *fmt, unsigned char *p)
729 printk(KERN_CONT "%s", fmt);
731 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
732 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
735 static void ftrace_bug(int failed, unsigned long ip)
739 FTRACE_WARN_ON_ONCE(1);
740 pr_info("ftrace faulted on modifying ");
744 FTRACE_WARN_ON_ONCE(1);
745 pr_info("ftrace failed to modify ");
747 print_ip_ins(" actual: ", (unsigned char *)ip);
748 printk(KERN_CONT "\n");
751 FTRACE_WARN_ON_ONCE(1);
752 pr_info("ftrace faulted on writing ");
756 FTRACE_WARN_ON_ONCE(1);
757 pr_info("ftrace faulted on unknown error ");
764 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
766 unsigned long ftrace_addr;
767 unsigned long ip, fl;
769 ftrace_addr = (unsigned long)FTRACE_ADDR;
774 * If this record is not to be traced and
775 * it is not enabled then do nothing.
777 * If this record is not to be traced and
778 * it is enabled then disable it.
781 if (rec->flags & FTRACE_FL_NOTRACE) {
782 if (rec->flags & FTRACE_FL_ENABLED)
783 rec->flags &= ~FTRACE_FL_ENABLED;
787 } else if (ftrace_filtered && enable) {
792 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
794 /* Record is filtered and enabled, do nothing */
795 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
798 /* Record is not filtered or enabled, do nothing */
802 /* Record is not filtered but enabled, disable it */
803 if (fl == FTRACE_FL_ENABLED)
804 rec->flags &= ~FTRACE_FL_ENABLED;
806 /* Otherwise record is filtered but not enabled, enable it */
807 rec->flags |= FTRACE_FL_ENABLED;
809 /* Disable or not filtered */
812 /* if record is enabled, do nothing */
813 if (rec->flags & FTRACE_FL_ENABLED)
816 rec->flags |= FTRACE_FL_ENABLED;
820 /* if record is not enabled, do nothing */
821 if (!(rec->flags & FTRACE_FL_ENABLED))
824 rec->flags &= ~FTRACE_FL_ENABLED;
828 if (rec->flags & FTRACE_FL_ENABLED)
829 return ftrace_make_call(rec, ftrace_addr);
831 return ftrace_make_nop(NULL, rec, ftrace_addr);
834 static void ftrace_replace_code(int enable)
836 struct dyn_ftrace *rec;
837 struct ftrace_page *pg;
840 do_for_each_ftrace_rec(pg, rec) {
842 * Skip over free records, records that have
843 * failed and not converted.
845 if (rec->flags & FTRACE_FL_FREE ||
846 rec->flags & FTRACE_FL_FAILED ||
847 !(rec->flags & FTRACE_FL_CONVERTED))
850 /* ignore updates to this record's mcount site */
851 if (get_kprobe((void *)rec->ip)) {
855 unfreeze_record(rec);
858 failed = __ftrace_replace_code(rec, enable);
860 rec->flags |= FTRACE_FL_FAILED;
861 if ((system_state == SYSTEM_BOOTING) ||
862 !core_kernel_text(rec->ip)) {
863 ftrace_free_rec(rec);
865 ftrace_bug(failed, rec->ip);
866 /* Stop processing */
870 } while_for_each_ftrace_rec();
874 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
881 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
884 rec->flags |= FTRACE_FL_FAILED;
891 * archs can override this function if they must do something
892 * before the modifying code is performed.
894 int __weak ftrace_arch_code_modify_prepare(void)
900 * archs can override this function if they must do something
901 * after the modifying code is performed.
903 int __weak ftrace_arch_code_modify_post_process(void)
908 static int __ftrace_modify_code(void *data)
912 if (*command & FTRACE_ENABLE_CALLS)
913 ftrace_replace_code(1);
914 else if (*command & FTRACE_DISABLE_CALLS)
915 ftrace_replace_code(0);
917 if (*command & FTRACE_UPDATE_TRACE_FUNC)
918 ftrace_update_ftrace_func(ftrace_trace_function);
920 if (*command & FTRACE_START_FUNC_RET)
921 ftrace_enable_ftrace_graph_caller();
922 else if (*command & FTRACE_STOP_FUNC_RET)
923 ftrace_disable_ftrace_graph_caller();
928 static void ftrace_run_update_code(int command)
932 ret = ftrace_arch_code_modify_prepare();
937 stop_machine(__ftrace_modify_code, &command, NULL);
939 ret = ftrace_arch_code_modify_post_process();
943 static ftrace_func_t saved_ftrace_func;
944 static int ftrace_start_up;
946 static void ftrace_startup_enable(int command)
948 if (saved_ftrace_func != ftrace_trace_function) {
949 saved_ftrace_func = ftrace_trace_function;
950 command |= FTRACE_UPDATE_TRACE_FUNC;
953 if (!command || !ftrace_enabled)
956 ftrace_run_update_code(command);
959 static void ftrace_startup(int command)
961 if (unlikely(ftrace_disabled))
965 command |= FTRACE_ENABLE_CALLS;
967 ftrace_startup_enable(command);
970 static void ftrace_shutdown(int command)
972 if (unlikely(ftrace_disabled))
976 if (!ftrace_start_up)
977 command |= FTRACE_DISABLE_CALLS;
979 if (saved_ftrace_func != ftrace_trace_function) {
980 saved_ftrace_func = ftrace_trace_function;
981 command |= FTRACE_UPDATE_TRACE_FUNC;
984 if (!command || !ftrace_enabled)
987 ftrace_run_update_code(command);
990 static void ftrace_startup_sysctl(void)
992 int command = FTRACE_ENABLE_MCOUNT;
994 if (unlikely(ftrace_disabled))
997 /* Force update next time */
998 saved_ftrace_func = NULL;
999 /* ftrace_start_up is true if we want ftrace running */
1000 if (ftrace_start_up)
1001 command |= FTRACE_ENABLE_CALLS;
1003 ftrace_run_update_code(command);
1006 static void ftrace_shutdown_sysctl(void)
1008 int command = FTRACE_DISABLE_MCOUNT;
1010 if (unlikely(ftrace_disabled))
1013 /* ftrace_start_up is true if ftrace is running */
1014 if (ftrace_start_up)
1015 command |= FTRACE_DISABLE_CALLS;
1017 ftrace_run_update_code(command);
1020 static cycle_t ftrace_update_time;
1021 static unsigned long ftrace_update_cnt;
1022 unsigned long ftrace_update_tot_cnt;
1024 static int ftrace_update_code(struct module *mod)
1026 struct dyn_ftrace *p;
1027 cycle_t start, stop;
1029 start = ftrace_now(raw_smp_processor_id());
1030 ftrace_update_cnt = 0;
1032 while (ftrace_new_addrs) {
1034 /* If something went wrong, bail without enabling anything */
1035 if (unlikely(ftrace_disabled))
1038 p = ftrace_new_addrs;
1039 ftrace_new_addrs = p->newlist;
1042 /* convert record (i.e, patch mcount-call with NOP) */
1043 if (ftrace_code_disable(mod, p)) {
1044 p->flags |= FTRACE_FL_CONVERTED;
1045 ftrace_update_cnt++;
1050 stop = ftrace_now(raw_smp_processor_id());
1051 ftrace_update_time = stop - start;
1052 ftrace_update_tot_cnt += ftrace_update_cnt;
1057 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1059 struct ftrace_page *pg;
1063 /* allocate a few pages */
1064 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1065 if (!ftrace_pages_start)
1069 * Allocate a few more pages.
1071 * TODO: have some parser search vmlinux before
1072 * final linking to find all calls to ftrace.
1074 * a) know how many pages to allocate.
1076 * b) set up the table then.
1078 * The dynamic code is still necessary for
1082 pg = ftrace_pages = ftrace_pages_start;
1084 cnt = num_to_init / ENTRIES_PER_PAGE;
1085 pr_info("ftrace: allocating %ld entries in %d pages\n",
1086 num_to_init, cnt + 1);
1088 for (i = 0; i < cnt; i++) {
1089 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1091 /* If we fail, we'll try later anyway */
1102 FTRACE_ITER_FILTER = (1 << 0),
1103 FTRACE_ITER_CONT = (1 << 1),
1104 FTRACE_ITER_NOTRACE = (1 << 2),
1105 FTRACE_ITER_FAILURES = (1 << 3),
1106 FTRACE_ITER_PRINTALL = (1 << 4),
1107 FTRACE_ITER_HASH = (1 << 5),
1110 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1112 struct ftrace_iterator {
1113 struct ftrace_page *pg;
1117 unsigned char buffer[FTRACE_BUFF_MAX+1];
1118 unsigned buffer_idx;
1123 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1125 struct ftrace_iterator *iter = m->private;
1126 struct hlist_node *hnd = v;
1127 struct hlist_head *hhd;
1129 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1134 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1137 hhd = &ftrace_func_hash[iter->hidx];
1139 if (hlist_empty(hhd)) {
1158 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1160 struct ftrace_iterator *iter = m->private;
1163 iter->flags |= FTRACE_ITER_HASH;
1165 return t_hash_next(m, p, pos);
1168 static int t_hash_show(struct seq_file *m, void *v)
1170 struct ftrace_func_probe *rec;
1171 struct hlist_node *hnd = v;
1172 char str[KSYM_SYMBOL_LEN];
1174 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1176 if (rec->ops->print)
1177 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1179 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1180 seq_printf(m, "%s:", str);
1182 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1183 seq_printf(m, "%s", str);
1186 seq_printf(m, ":%p", rec->data);
1193 t_next(struct seq_file *m, void *v, loff_t *pos)
1195 struct ftrace_iterator *iter = m->private;
1196 struct dyn_ftrace *rec = NULL;
1198 if (iter->flags & FTRACE_ITER_HASH)
1199 return t_hash_next(m, v, pos);
1203 if (iter->flags & FTRACE_ITER_PRINTALL)
1207 if (iter->idx >= iter->pg->index) {
1208 if (iter->pg->next) {
1209 iter->pg = iter->pg->next;
1216 rec = &iter->pg->records[iter->idx++];
1217 if ((rec->flags & FTRACE_FL_FREE) ||
1219 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1220 (rec->flags & FTRACE_FL_FAILED)) ||
1222 ((iter->flags & FTRACE_ITER_FAILURES) &&
1223 !(rec->flags & FTRACE_FL_FAILED)) ||
1225 ((iter->flags & FTRACE_ITER_FILTER) &&
1226 !(rec->flags & FTRACE_FL_FILTER)) ||
1228 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1229 !(rec->flags & FTRACE_FL_NOTRACE))) {
1238 static void *t_start(struct seq_file *m, loff_t *pos)
1240 struct ftrace_iterator *iter = m->private;
1243 mutex_lock(&ftrace_lock);
1245 * For set_ftrace_filter reading, if we have the filter
1246 * off, we can short cut and just print out that all
1247 * functions are enabled.
1249 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1251 return t_hash_start(m, pos);
1252 iter->flags |= FTRACE_ITER_PRINTALL;
1257 if (iter->flags & FTRACE_ITER_HASH)
1258 return t_hash_start(m, pos);
1267 p = t_next(m, p, pos);
1270 return t_hash_start(m, pos);
1275 static void t_stop(struct seq_file *m, void *p)
1277 mutex_unlock(&ftrace_lock);
1280 static int t_show(struct seq_file *m, void *v)
1282 struct ftrace_iterator *iter = m->private;
1283 struct dyn_ftrace *rec = v;
1284 char str[KSYM_SYMBOL_LEN];
1286 if (iter->flags & FTRACE_ITER_HASH)
1287 return t_hash_show(m, v);
1289 if (iter->flags & FTRACE_ITER_PRINTALL) {
1290 seq_printf(m, "#### all functions enabled ####\n");
1297 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1299 seq_printf(m, "%s\n", str);
1304 static struct seq_operations show_ftrace_seq_ops = {
1312 ftrace_avail_open(struct inode *inode, struct file *file)
1314 struct ftrace_iterator *iter;
1317 if (unlikely(ftrace_disabled))
1320 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1324 iter->pg = ftrace_pages_start;
1326 ret = seq_open(file, &show_ftrace_seq_ops);
1328 struct seq_file *m = file->private_data;
1338 int ftrace_avail_release(struct inode *inode, struct file *file)
1340 struct seq_file *m = (struct seq_file *)file->private_data;
1341 struct ftrace_iterator *iter = m->private;
1343 seq_release(inode, file);
1350 ftrace_failures_open(struct inode *inode, struct file *file)
1354 struct ftrace_iterator *iter;
1356 ret = ftrace_avail_open(inode, file);
1358 m = (struct seq_file *)file->private_data;
1359 iter = (struct ftrace_iterator *)m->private;
1360 iter->flags = FTRACE_ITER_FAILURES;
1367 static void ftrace_filter_reset(int enable)
1369 struct ftrace_page *pg;
1370 struct dyn_ftrace *rec;
1371 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1373 mutex_lock(&ftrace_lock);
1375 ftrace_filtered = 0;
1376 do_for_each_ftrace_rec(pg, rec) {
1377 if (rec->flags & FTRACE_FL_FAILED)
1379 rec->flags &= ~type;
1380 } while_for_each_ftrace_rec();
1381 mutex_unlock(&ftrace_lock);
1385 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1387 struct ftrace_iterator *iter;
1390 if (unlikely(ftrace_disabled))
1393 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1397 mutex_lock(&ftrace_regex_lock);
1398 if ((file->f_mode & FMODE_WRITE) &&
1399 !(file->f_flags & O_APPEND))
1400 ftrace_filter_reset(enable);
1402 if (file->f_mode & FMODE_READ) {
1403 iter->pg = ftrace_pages_start;
1404 iter->flags = enable ? FTRACE_ITER_FILTER :
1405 FTRACE_ITER_NOTRACE;
1407 ret = seq_open(file, &show_ftrace_seq_ops);
1409 struct seq_file *m = file->private_data;
1414 file->private_data = iter;
1415 mutex_unlock(&ftrace_regex_lock);
1421 ftrace_filter_open(struct inode *inode, struct file *file)
1423 return ftrace_regex_open(inode, file, 1);
1427 ftrace_notrace_open(struct inode *inode, struct file *file)
1429 return ftrace_regex_open(inode, file, 0);
1433 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1437 if (file->f_mode & FMODE_READ)
1438 ret = seq_lseek(file, offset, origin);
1440 file->f_pos = ret = 1;
1453 * (static function - no need for kernel doc)
1455 * Pass in a buffer containing a glob and this function will
1456 * set search to point to the search part of the buffer and
1457 * return the type of search it is (see enum above).
1458 * This does modify buff.
1460 * Returns enum type.
1461 * search returns the pointer to use for comparison.
1462 * not returns 1 if buff started with a '!'
1466 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1468 int type = MATCH_FULL;
1471 if (buff[0] == '!') {
1480 for (i = 0; i < len; i++) {
1481 if (buff[i] == '*') {
1484 type = MATCH_END_ONLY;
1486 if (type == MATCH_END_ONLY)
1487 type = MATCH_MIDDLE_ONLY;
1489 type = MATCH_FRONT_ONLY;
1499 static int ftrace_match(char *str, char *regex, int len, int type)
1506 if (strcmp(str, regex) == 0)
1509 case MATCH_FRONT_ONLY:
1510 if (strncmp(str, regex, len) == 0)
1513 case MATCH_MIDDLE_ONLY:
1514 if (strstr(str, regex))
1517 case MATCH_END_ONLY:
1518 ptr = strstr(str, regex);
1519 if (ptr && (ptr[len] == 0))
1528 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1530 char str[KSYM_SYMBOL_LEN];
1532 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1533 return ftrace_match(str, regex, len, type);
1536 static void ftrace_match_records(char *buff, int len, int enable)
1538 unsigned int search_len;
1539 struct ftrace_page *pg;
1540 struct dyn_ftrace *rec;
1546 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1547 type = ftrace_setup_glob(buff, len, &search, ¬);
1549 search_len = strlen(search);
1551 mutex_lock(&ftrace_lock);
1552 do_for_each_ftrace_rec(pg, rec) {
1554 if (rec->flags & FTRACE_FL_FAILED)
1557 if (ftrace_match_record(rec, search, search_len, type)) {
1559 rec->flags &= ~flag;
1564 * Only enable filtering if we have a function that
1567 if (enable && (rec->flags & FTRACE_FL_FILTER))
1568 ftrace_filtered = 1;
1569 } while_for_each_ftrace_rec();
1570 mutex_unlock(&ftrace_lock);
1574 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1575 char *regex, int len, int type)
1577 char str[KSYM_SYMBOL_LEN];
1580 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1582 if (!modname || strcmp(modname, mod))
1585 /* blank search means to match all funcs in the mod */
1587 return ftrace_match(str, regex, len, type);
1592 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1594 unsigned search_len = 0;
1595 struct ftrace_page *pg;
1596 struct dyn_ftrace *rec;
1597 int type = MATCH_FULL;
1598 char *search = buff;
1602 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1604 /* blank or '*' mean the same */
1605 if (strcmp(buff, "*") == 0)
1608 /* handle the case of 'dont filter this module' */
1609 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1615 type = ftrace_setup_glob(buff, strlen(buff), &search, ¬);
1616 search_len = strlen(search);
1619 mutex_lock(&ftrace_lock);
1620 do_for_each_ftrace_rec(pg, rec) {
1622 if (rec->flags & FTRACE_FL_FAILED)
1625 if (ftrace_match_module_record(rec, mod,
1626 search, search_len, type)) {
1628 rec->flags &= ~flag;
1632 if (enable && (rec->flags & FTRACE_FL_FILTER))
1633 ftrace_filtered = 1;
1635 } while_for_each_ftrace_rec();
1636 mutex_unlock(&ftrace_lock);
1640 * We register the module command as a template to show others how
1641 * to register the a command as well.
1645 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1650 * cmd == 'mod' because we only registered this func
1651 * for the 'mod' ftrace_func_command.
1652 * But if you register one func with multiple commands,
1653 * you can tell which command was used by the cmd
1657 /* we must have a module name */
1661 mod = strsep(¶m, ":");
1665 ftrace_match_module_records(func, mod, enable);
1669 static struct ftrace_func_command ftrace_mod_cmd = {
1671 .func = ftrace_mod_callback,
1674 static int __init ftrace_mod_cmd_init(void)
1676 return register_ftrace_command(&ftrace_mod_cmd);
1678 device_initcall(ftrace_mod_cmd_init);
1681 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1683 struct ftrace_func_probe *entry;
1684 struct hlist_head *hhd;
1685 struct hlist_node *n;
1689 key = hash_long(ip, FTRACE_HASH_BITS);
1691 hhd = &ftrace_func_hash[key];
1693 if (hlist_empty(hhd))
1697 * Disable preemption for these calls to prevent a RCU grace
1698 * period. This syncs the hash iteration and freeing of items
1699 * on the hash. rcu_read_lock is too dangerous here.
1701 resched = ftrace_preempt_disable();
1702 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1703 if (entry->ip == ip)
1704 entry->ops->func(ip, parent_ip, &entry->data);
1706 ftrace_preempt_enable(resched);
1709 static struct ftrace_ops trace_probe_ops __read_mostly =
1711 .func = function_trace_probe_call,
1714 static int ftrace_probe_registered;
1716 static void __enable_ftrace_function_probe(void)
1720 if (ftrace_probe_registered)
1723 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1724 struct hlist_head *hhd = &ftrace_func_hash[i];
1728 /* Nothing registered? */
1729 if (i == FTRACE_FUNC_HASHSIZE)
1732 __register_ftrace_function(&trace_probe_ops);
1734 ftrace_probe_registered = 1;
1737 static void __disable_ftrace_function_probe(void)
1741 if (!ftrace_probe_registered)
1744 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1745 struct hlist_head *hhd = &ftrace_func_hash[i];
1750 /* no more funcs left */
1751 __unregister_ftrace_function(&trace_probe_ops);
1753 ftrace_probe_registered = 0;
1757 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1759 struct ftrace_func_probe *entry =
1760 container_of(rhp, struct ftrace_func_probe, rcu);
1762 if (entry->ops->free)
1763 entry->ops->free(&entry->data);
1769 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1772 struct ftrace_func_probe *entry;
1773 struct ftrace_page *pg;
1774 struct dyn_ftrace *rec;
1780 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
1781 len = strlen(search);
1783 /* we do not support '!' for function probes */
1787 mutex_lock(&ftrace_lock);
1788 do_for_each_ftrace_rec(pg, rec) {
1790 if (rec->flags & FTRACE_FL_FAILED)
1793 if (!ftrace_match_record(rec, search, len, type))
1796 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1798 /* If we did not process any, then return error */
1809 * The caller might want to do something special
1810 * for each function we find. We call the callback
1811 * to give the caller an opportunity to do so.
1813 if (ops->callback) {
1814 if (ops->callback(rec->ip, &entry->data) < 0) {
1815 /* caller does not like this func */
1822 entry->ip = rec->ip;
1824 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1825 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1827 } while_for_each_ftrace_rec();
1828 __enable_ftrace_function_probe();
1831 mutex_unlock(&ftrace_lock);
1837 PROBE_TEST_FUNC = 1,
1842 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1843 void *data, int flags)
1845 struct ftrace_func_probe *entry;
1846 struct hlist_node *n, *tmp;
1847 char str[KSYM_SYMBOL_LEN];
1848 int type = MATCH_FULL;
1852 if (glob && (strcmp(glob, "*") || !strlen(glob)))
1857 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
1858 len = strlen(search);
1860 /* we do not support '!' for function probes */
1865 mutex_lock(&ftrace_lock);
1866 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1867 struct hlist_head *hhd = &ftrace_func_hash[i];
1869 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
1871 /* break up if statements for readability */
1872 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
1875 if ((flags & PROBE_TEST_DATA) && entry->data != data)
1878 /* do this last, since it is the most expensive */
1880 kallsyms_lookup(entry->ip, NULL, NULL,
1882 if (!ftrace_match(str, glob, len, type))
1886 hlist_del(&entry->node);
1887 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
1890 __disable_ftrace_function_probe();
1891 mutex_unlock(&ftrace_lock);
1895 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1898 __unregister_ftrace_function_probe(glob, ops, data,
1899 PROBE_TEST_FUNC | PROBE_TEST_DATA);
1903 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
1905 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
1908 void unregister_ftrace_function_probe_all(char *glob)
1910 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
1913 static LIST_HEAD(ftrace_commands);
1914 static DEFINE_MUTEX(ftrace_cmd_mutex);
1916 int register_ftrace_command(struct ftrace_func_command *cmd)
1918 struct ftrace_func_command *p;
1921 mutex_lock(&ftrace_cmd_mutex);
1922 list_for_each_entry(p, &ftrace_commands, list) {
1923 if (strcmp(cmd->name, p->name) == 0) {
1928 list_add(&cmd->list, &ftrace_commands);
1930 mutex_unlock(&ftrace_cmd_mutex);
1935 int unregister_ftrace_command(struct ftrace_func_command *cmd)
1937 struct ftrace_func_command *p, *n;
1940 mutex_lock(&ftrace_cmd_mutex);
1941 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
1942 if (strcmp(cmd->name, p->name) == 0) {
1944 list_del_init(&p->list);
1949 mutex_unlock(&ftrace_cmd_mutex);
1954 static int ftrace_process_regex(char *buff, int len, int enable)
1956 char *func, *command, *next = buff;
1957 struct ftrace_func_command *p;
1960 func = strsep(&next, ":");
1963 ftrace_match_records(func, len, enable);
1969 command = strsep(&next, ":");
1971 mutex_lock(&ftrace_cmd_mutex);
1972 list_for_each_entry(p, &ftrace_commands, list) {
1973 if (strcmp(p->name, command) == 0) {
1974 ret = p->func(func, command, next, enable);
1979 mutex_unlock(&ftrace_cmd_mutex);
1985 ftrace_regex_write(struct file *file, const char __user *ubuf,
1986 size_t cnt, loff_t *ppos, int enable)
1988 struct ftrace_iterator *iter;
1993 if (!cnt || cnt < 0)
1996 mutex_lock(&ftrace_regex_lock);
1998 if (file->f_mode & FMODE_READ) {
1999 struct seq_file *m = file->private_data;
2002 iter = file->private_data;
2005 iter->flags &= ~FTRACE_ITER_CONT;
2006 iter->buffer_idx = 0;
2009 ret = get_user(ch, ubuf++);
2015 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2016 /* skip white space */
2017 while (cnt && isspace(ch)) {
2018 ret = get_user(ch, ubuf++);
2026 file->f_pos += read;
2031 iter->buffer_idx = 0;
2034 while (cnt && !isspace(ch)) {
2035 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2036 iter->buffer[iter->buffer_idx++] = ch;
2041 ret = get_user(ch, ubuf++);
2050 iter->buffer[iter->buffer_idx] = 0;
2051 ret = ftrace_process_regex(iter->buffer,
2052 iter->buffer_idx, enable);
2055 iter->buffer_idx = 0;
2057 iter->flags |= FTRACE_ITER_CONT;
2060 file->f_pos += read;
2064 mutex_unlock(&ftrace_regex_lock);
2070 ftrace_filter_write(struct file *file, const char __user *ubuf,
2071 size_t cnt, loff_t *ppos)
2073 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2077 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2078 size_t cnt, loff_t *ppos)
2080 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2084 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2086 if (unlikely(ftrace_disabled))
2089 mutex_lock(&ftrace_regex_lock);
2091 ftrace_filter_reset(enable);
2093 ftrace_match_records(buf, len, enable);
2094 mutex_unlock(&ftrace_regex_lock);
2098 * ftrace_set_filter - set a function to filter on in ftrace
2099 * @buf - the string that holds the function filter text.
2100 * @len - the length of the string.
2101 * @reset - non zero to reset all filters before applying this filter.
2103 * Filters denote which functions should be enabled when tracing is enabled.
2104 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2106 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2108 ftrace_set_regex(buf, len, reset, 1);
2112 * ftrace_set_notrace - set a function to not trace in ftrace
2113 * @buf - the string that holds the function notrace text.
2114 * @len - the length of the string.
2115 * @reset - non zero to reset all filters before applying this filter.
2117 * Notrace Filters denote which functions should not be enabled when tracing
2118 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2121 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2123 ftrace_set_regex(buf, len, reset, 0);
2127 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2129 struct seq_file *m = (struct seq_file *)file->private_data;
2130 struct ftrace_iterator *iter;
2132 mutex_lock(&ftrace_regex_lock);
2133 if (file->f_mode & FMODE_READ) {
2136 seq_release(inode, file);
2138 iter = file->private_data;
2140 if (iter->buffer_idx) {
2142 iter->buffer[iter->buffer_idx] = 0;
2143 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
2146 mutex_lock(&ftrace_lock);
2147 if (ftrace_start_up && ftrace_enabled)
2148 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2149 mutex_unlock(&ftrace_lock);
2152 mutex_unlock(&ftrace_regex_lock);
2157 ftrace_filter_release(struct inode *inode, struct file *file)
2159 return ftrace_regex_release(inode, file, 1);
2163 ftrace_notrace_release(struct inode *inode, struct file *file)
2165 return ftrace_regex_release(inode, file, 0);
2168 static const struct file_operations ftrace_avail_fops = {
2169 .open = ftrace_avail_open,
2171 .llseek = seq_lseek,
2172 .release = ftrace_avail_release,
2175 static const struct file_operations ftrace_failures_fops = {
2176 .open = ftrace_failures_open,
2178 .llseek = seq_lseek,
2179 .release = ftrace_avail_release,
2182 static const struct file_operations ftrace_filter_fops = {
2183 .open = ftrace_filter_open,
2185 .write = ftrace_filter_write,
2186 .llseek = ftrace_regex_lseek,
2187 .release = ftrace_filter_release,
2190 static const struct file_operations ftrace_notrace_fops = {
2191 .open = ftrace_notrace_open,
2193 .write = ftrace_notrace_write,
2194 .llseek = ftrace_regex_lseek,
2195 .release = ftrace_notrace_release,
2198 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2200 static DEFINE_MUTEX(graph_lock);
2202 int ftrace_graph_count;
2203 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2206 g_next(struct seq_file *m, void *v, loff_t *pos)
2208 unsigned long *array = m->private;
2213 if (index >= ftrace_graph_count)
2216 return &array[index];
2219 static void *g_start(struct seq_file *m, loff_t *pos)
2223 mutex_lock(&graph_lock);
2225 /* Nothing, tell g_show to print all functions are enabled */
2226 if (!ftrace_graph_count && !*pos)
2229 p = g_next(m, p, pos);
2234 static void g_stop(struct seq_file *m, void *p)
2236 mutex_unlock(&graph_lock);
2239 static int g_show(struct seq_file *m, void *v)
2241 unsigned long *ptr = v;
2242 char str[KSYM_SYMBOL_LEN];
2247 if (ptr == (unsigned long *)1) {
2248 seq_printf(m, "#### all functions enabled ####\n");
2252 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2254 seq_printf(m, "%s\n", str);
2259 static struct seq_operations ftrace_graph_seq_ops = {
2267 ftrace_graph_open(struct inode *inode, struct file *file)
2271 if (unlikely(ftrace_disabled))
2274 mutex_lock(&graph_lock);
2275 if ((file->f_mode & FMODE_WRITE) &&
2276 !(file->f_flags & O_APPEND)) {
2277 ftrace_graph_count = 0;
2278 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2281 if (file->f_mode & FMODE_READ) {
2282 ret = seq_open(file, &ftrace_graph_seq_ops);
2284 struct seq_file *m = file->private_data;
2285 m->private = ftrace_graph_funcs;
2288 file->private_data = ftrace_graph_funcs;
2289 mutex_unlock(&graph_lock);
2295 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2297 struct dyn_ftrace *rec;
2298 struct ftrace_page *pg;
2306 if (ftrace_disabled)
2310 type = ftrace_setup_glob(buffer, strlen(buffer), &search, ¬);
2314 search_len = strlen(search);
2316 mutex_lock(&ftrace_lock);
2317 do_for_each_ftrace_rec(pg, rec) {
2319 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2322 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2325 if (ftrace_match_record(rec, search, search_len, type)) {
2326 /* ensure it is not already in the array */
2328 for (i = 0; i < *idx; i++)
2329 if (array[i] == rec->ip) {
2334 array[(*idx)++] = rec->ip;
2338 } while_for_each_ftrace_rec();
2340 mutex_unlock(&ftrace_lock);
2342 return found ? 0 : -EINVAL;
2346 ftrace_graph_write(struct file *file, const char __user *ubuf,
2347 size_t cnt, loff_t *ppos)
2349 unsigned char buffer[FTRACE_BUFF_MAX+1];
2350 unsigned long *array;
2356 if (!cnt || cnt < 0)
2359 mutex_lock(&graph_lock);
2361 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2366 if (file->f_mode & FMODE_READ) {
2367 struct seq_file *m = file->private_data;
2370 array = file->private_data;
2372 ret = get_user(ch, ubuf++);
2378 /* skip white space */
2379 while (cnt && isspace(ch)) {
2380 ret = get_user(ch, ubuf++);
2393 while (cnt && !isspace(ch)) {
2394 if (index < FTRACE_BUFF_MAX)
2395 buffer[index++] = ch;
2400 ret = get_user(ch, ubuf++);
2408 /* we allow only one expression at a time */
2409 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2413 file->f_pos += read;
2417 mutex_unlock(&graph_lock);
2422 static const struct file_operations ftrace_graph_fops = {
2423 .open = ftrace_graph_open,
2425 .write = ftrace_graph_write,
2427 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2429 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2431 struct dentry *entry;
2433 entry = debugfs_create_file("available_filter_functions", 0444,
2434 d_tracer, NULL, &ftrace_avail_fops);
2436 pr_warning("Could not create debugfs "
2437 "'available_filter_functions' entry\n");
2439 entry = debugfs_create_file("failures", 0444,
2440 d_tracer, NULL, &ftrace_failures_fops);
2442 pr_warning("Could not create debugfs 'failures' entry\n");
2444 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
2445 NULL, &ftrace_filter_fops);
2447 pr_warning("Could not create debugfs "
2448 "'set_ftrace_filter' entry\n");
2450 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
2451 NULL, &ftrace_notrace_fops);
2453 pr_warning("Could not create debugfs "
2454 "'set_ftrace_notrace' entry\n");
2456 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2457 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
2459 &ftrace_graph_fops);
2461 pr_warning("Could not create debugfs "
2462 "'set_graph_function' entry\n");
2463 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2465 ftrace_profile_debugfs(d_tracer);
2470 static int ftrace_convert_nops(struct module *mod,
2471 unsigned long *start,
2476 unsigned long flags;
2478 mutex_lock(&ftrace_lock);
2481 addr = ftrace_call_adjust(*p++);
2483 * Some architecture linkers will pad between
2484 * the different mcount_loc sections of different
2485 * object files to satisfy alignments.
2486 * Skip any NULL pointers.
2490 ftrace_record_ip(addr);
2493 /* disable interrupts to prevent kstop machine */
2494 local_irq_save(flags);
2495 ftrace_update_code(mod);
2496 local_irq_restore(flags);
2497 mutex_unlock(&ftrace_lock);
2502 void ftrace_init_module(struct module *mod,
2503 unsigned long *start, unsigned long *end)
2505 if (ftrace_disabled || start == end)
2507 ftrace_convert_nops(mod, start, end);
2510 extern unsigned long __start_mcount_loc[];
2511 extern unsigned long __stop_mcount_loc[];
2513 void __init ftrace_init(void)
2515 unsigned long count, addr, flags;
2518 /* Keep the ftrace pointer to the stub */
2519 addr = (unsigned long)ftrace_stub;
2521 local_irq_save(flags);
2522 ftrace_dyn_arch_init(&addr);
2523 local_irq_restore(flags);
2525 /* ftrace_dyn_arch_init places the return code in addr */
2529 count = __stop_mcount_loc - __start_mcount_loc;
2531 ret = ftrace_dyn_table_alloc(count);
2535 ftrace_profile_init(count);
2537 last_ftrace_enabled = ftrace_enabled = 1;
2539 ret = ftrace_convert_nops(NULL,
2545 ftrace_disabled = 1;
2550 static int __init ftrace_nodyn_init(void)
2555 device_initcall(ftrace_nodyn_init);
2557 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2558 static inline void ftrace_startup_enable(int command) { }
2559 /* Keep as macros so we do not need to define the commands */
2560 # define ftrace_startup(command) do { } while (0)
2561 # define ftrace_shutdown(command) do { } while (0)
2562 # define ftrace_startup_sysctl() do { } while (0)
2563 # define ftrace_shutdown_sysctl() do { } while (0)
2564 #endif /* CONFIG_DYNAMIC_FTRACE */
2567 ftrace_pid_read(struct file *file, char __user *ubuf,
2568 size_t cnt, loff_t *ppos)
2573 if (ftrace_pid_trace == ftrace_swapper_pid)
2574 r = sprintf(buf, "swapper tasks\n");
2575 else if (ftrace_pid_trace)
2576 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2578 r = sprintf(buf, "no pid\n");
2580 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2583 static void clear_ftrace_swapper(void)
2585 struct task_struct *p;
2589 for_each_online_cpu(cpu) {
2591 clear_tsk_trace_trace(p);
2596 static void set_ftrace_swapper(void)
2598 struct task_struct *p;
2602 for_each_online_cpu(cpu) {
2604 set_tsk_trace_trace(p);
2609 static void clear_ftrace_pid(struct pid *pid)
2611 struct task_struct *p;
2614 do_each_pid_task(pid, PIDTYPE_PID, p) {
2615 clear_tsk_trace_trace(p);
2616 } while_each_pid_task(pid, PIDTYPE_PID, p);
2622 static void set_ftrace_pid(struct pid *pid)
2624 struct task_struct *p;
2627 do_each_pid_task(pid, PIDTYPE_PID, p) {
2628 set_tsk_trace_trace(p);
2629 } while_each_pid_task(pid, PIDTYPE_PID, p);
2633 static void clear_ftrace_pid_task(struct pid **pid)
2635 if (*pid == ftrace_swapper_pid)
2636 clear_ftrace_swapper();
2638 clear_ftrace_pid(*pid);
2643 static void set_ftrace_pid_task(struct pid *pid)
2645 if (pid == ftrace_swapper_pid)
2646 set_ftrace_swapper();
2648 set_ftrace_pid(pid);
2652 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2653 size_t cnt, loff_t *ppos)
2660 if (cnt >= sizeof(buf))
2663 if (copy_from_user(&buf, ubuf, cnt))
2668 ret = strict_strtol(buf, 10, &val);
2672 mutex_lock(&ftrace_lock);
2674 /* disable pid tracing */
2675 if (!ftrace_pid_trace)
2678 clear_ftrace_pid_task(&ftrace_pid_trace);
2681 /* swapper task is special */
2683 pid = ftrace_swapper_pid;
2684 if (pid == ftrace_pid_trace)
2687 pid = find_get_pid(val);
2689 if (pid == ftrace_pid_trace) {
2695 if (ftrace_pid_trace)
2696 clear_ftrace_pid_task(&ftrace_pid_trace);
2701 ftrace_pid_trace = pid;
2703 set_ftrace_pid_task(ftrace_pid_trace);
2706 /* update the function call */
2707 ftrace_update_pid_func();
2708 ftrace_startup_enable(0);
2711 mutex_unlock(&ftrace_lock);
2716 static const struct file_operations ftrace_pid_fops = {
2717 .read = ftrace_pid_read,
2718 .write = ftrace_pid_write,
2721 static __init int ftrace_init_debugfs(void)
2723 struct dentry *d_tracer;
2724 struct dentry *entry;
2726 d_tracer = tracing_init_dentry();
2730 ftrace_init_dyn_debugfs(d_tracer);
2732 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2733 NULL, &ftrace_pid_fops);
2735 pr_warning("Could not create debugfs "
2736 "'set_ftrace_pid' entry\n");
2739 fs_initcall(ftrace_init_debugfs);
2742 * ftrace_kill - kill ftrace
2744 * This function should be used by panic code. It stops ftrace
2745 * but in a not so nice way. If you need to simply kill ftrace
2746 * from a non-atomic section, use ftrace_kill.
2748 void ftrace_kill(void)
2750 ftrace_disabled = 1;
2752 clear_ftrace_function();
2756 * register_ftrace_function - register a function for profiling
2757 * @ops - ops structure that holds the function for profiling.
2759 * Register a function to be called by all functions in the
2762 * Note: @ops->func and all the functions it calls must be labeled
2763 * with "notrace", otherwise it will go into a
2766 int register_ftrace_function(struct ftrace_ops *ops)
2770 if (unlikely(ftrace_disabled))
2773 mutex_lock(&ftrace_lock);
2775 ret = __register_ftrace_function(ops);
2778 mutex_unlock(&ftrace_lock);
2783 * unregister_ftrace_function - unregister a function for profiling.
2784 * @ops - ops structure that holds the function to unregister
2786 * Unregister a function that was added to be called by ftrace profiling.
2788 int unregister_ftrace_function(struct ftrace_ops *ops)
2792 mutex_lock(&ftrace_lock);
2793 ret = __unregister_ftrace_function(ops);
2795 mutex_unlock(&ftrace_lock);
2801 ftrace_enable_sysctl(struct ctl_table *table, int write,
2802 struct file *file, void __user *buffer, size_t *lenp,
2807 if (unlikely(ftrace_disabled))
2810 mutex_lock(&ftrace_lock);
2812 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
2814 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
2817 last_ftrace_enabled = ftrace_enabled;
2819 if (ftrace_enabled) {
2821 ftrace_startup_sysctl();
2823 /* we are starting ftrace again */
2824 if (ftrace_list != &ftrace_list_end) {
2825 if (ftrace_list->next == &ftrace_list_end)
2826 ftrace_trace_function = ftrace_list->func;
2828 ftrace_trace_function = ftrace_list_func;
2832 /* stopping ftrace calls (just send to ftrace_stub) */
2833 ftrace_trace_function = ftrace_stub;
2835 ftrace_shutdown_sysctl();
2839 mutex_unlock(&ftrace_lock);
2843 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2845 static atomic_t ftrace_graph_active;
2846 static struct notifier_block ftrace_suspend_notifier;
2848 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
2853 /* The callbacks that hook a function */
2854 trace_func_graph_ret_t ftrace_graph_return =
2855 (trace_func_graph_ret_t)ftrace_stub;
2856 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
2858 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
2859 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
2863 unsigned long flags;
2864 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
2865 struct task_struct *g, *t;
2867 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
2868 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
2869 * sizeof(struct ftrace_ret_stack),
2871 if (!ret_stack_list[i]) {
2879 read_lock_irqsave(&tasklist_lock, flags);
2880 do_each_thread(g, t) {
2886 if (t->ret_stack == NULL) {
2887 t->curr_ret_stack = -1;
2888 /* Make sure IRQs see the -1 first: */
2890 t->ret_stack = ret_stack_list[start++];
2891 atomic_set(&t->tracing_graph_pause, 0);
2892 atomic_set(&t->trace_overrun, 0);
2894 } while_each_thread(g, t);
2897 read_unlock_irqrestore(&tasklist_lock, flags);
2899 for (i = start; i < end; i++)
2900 kfree(ret_stack_list[i]);
2905 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
2906 struct task_struct *next)
2908 unsigned long long timestamp;
2912 * Does the user want to count the time a function was asleep.
2913 * If so, do not update the time stamps.
2915 if (trace_flags & TRACE_ITER_SLEEP_TIME)
2918 timestamp = trace_clock_local();
2920 prev->ftrace_timestamp = timestamp;
2922 /* only process tasks that we timestamped */
2923 if (!next->ftrace_timestamp)
2927 * Update all the counters in next to make up for the
2928 * time next was sleeping.
2930 timestamp -= next->ftrace_timestamp;
2932 for (index = next->curr_ret_stack; index >= 0; index--)
2933 next->ret_stack[index].calltime += timestamp;
2936 /* Allocate a return stack for each task */
2937 static int start_graph_tracing(void)
2939 struct ftrace_ret_stack **ret_stack_list;
2942 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
2943 sizeof(struct ftrace_ret_stack *),
2946 if (!ret_stack_list)
2949 /* The cpu_boot init_task->ret_stack will never be freed */
2950 for_each_online_cpu(cpu)
2951 ftrace_graph_init_task(idle_task(cpu));
2954 ret = alloc_retstack_tasklist(ret_stack_list);
2955 } while (ret == -EAGAIN);
2958 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
2960 pr_info("ftrace_graph: Couldn't activate tracepoint"
2961 " probe to kernel_sched_switch\n");
2964 kfree(ret_stack_list);
2969 * Hibernation protection.
2970 * The state of the current task is too much unstable during
2971 * suspend/restore to disk. We want to protect against that.
2974 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
2978 case PM_HIBERNATION_PREPARE:
2979 pause_graph_tracing();
2982 case PM_POST_HIBERNATION:
2983 unpause_graph_tracing();
2989 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
2990 trace_func_graph_ent_t entryfunc)
2994 mutex_lock(&ftrace_lock);
2996 /* we currently allow only one tracer registered at a time */
2997 if (atomic_read(&ftrace_graph_active)) {
3002 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3003 register_pm_notifier(&ftrace_suspend_notifier);
3005 atomic_inc(&ftrace_graph_active);
3006 ret = start_graph_tracing();
3008 atomic_dec(&ftrace_graph_active);
3012 ftrace_graph_return = retfunc;
3013 ftrace_graph_entry = entryfunc;
3015 ftrace_startup(FTRACE_START_FUNC_RET);
3018 mutex_unlock(&ftrace_lock);
3022 void unregister_ftrace_graph(void)
3024 mutex_lock(&ftrace_lock);
3026 atomic_dec(&ftrace_graph_active);
3027 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3028 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3029 ftrace_graph_entry = ftrace_graph_entry_stub;
3030 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3031 unregister_pm_notifier(&ftrace_suspend_notifier);
3033 mutex_unlock(&ftrace_lock);
3036 /* Allocate a return stack for newly created task */
3037 void ftrace_graph_init_task(struct task_struct *t)
3039 if (atomic_read(&ftrace_graph_active)) {
3040 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3041 * sizeof(struct ftrace_ret_stack),
3045 t->curr_ret_stack = -1;
3046 atomic_set(&t->tracing_graph_pause, 0);
3047 atomic_set(&t->trace_overrun, 0);
3048 t->ftrace_timestamp = 0;
3050 t->ret_stack = NULL;
3053 void ftrace_graph_exit_task(struct task_struct *t)
3055 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3057 t->ret_stack = NULL;
3058 /* NULL must become visible to IRQs before we free it: */
3064 void ftrace_graph_stop(void)