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/ctype.h>
28 #include <linux/list.h>
29 #include <linux/hash.h>
31 #include <trace/events/sched.h>
33 #include <asm/ftrace.h>
34 #include <asm/setup.h>
36 #include "trace_output.h"
37 #include "trace_stat.h"
39 #define FTRACE_WARN_ON(cond) \
45 #define FTRACE_WARN_ON_ONCE(cond) \
47 if (WARN_ON_ONCE(cond)) \
51 /* hash bits for specific function selection */
52 #define FTRACE_HASH_BITS 7
53 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
55 /* ftrace_enabled is a method to turn ftrace on or off */
56 int ftrace_enabled __read_mostly;
57 static int last_ftrace_enabled;
59 /* Quick disabling of function tracer. */
60 int function_trace_stop;
62 /* List for set_ftrace_pid's pids. */
63 LIST_HEAD(ftrace_pids);
65 struct list_head list;
70 * ftrace_disabled is set when an anomaly is discovered.
71 * ftrace_disabled is much stronger than ftrace_enabled.
73 static int ftrace_disabled __read_mostly;
75 static DEFINE_MUTEX(ftrace_lock);
77 static struct ftrace_ops ftrace_list_end __read_mostly =
82 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
83 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
84 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
85 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
87 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
89 struct ftrace_ops *op = ftrace_list;
91 /* in case someone actually ports this to alpha! */
92 read_barrier_depends();
94 while (op != &ftrace_list_end) {
96 read_barrier_depends();
97 op->func(ip, parent_ip);
102 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
104 if (!test_tsk_trace_trace(current))
107 ftrace_pid_function(ip, parent_ip);
110 static void set_ftrace_pid_function(ftrace_func_t func)
112 /* do not set ftrace_pid_function to itself! */
113 if (func != ftrace_pid_func)
114 ftrace_pid_function = func;
118 * clear_ftrace_function - reset the ftrace function
120 * This NULLs the ftrace function and in essence stops
121 * tracing. There may be lag
123 void clear_ftrace_function(void)
125 ftrace_trace_function = ftrace_stub;
126 __ftrace_trace_function = ftrace_stub;
127 ftrace_pid_function = ftrace_stub;
130 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
132 * For those archs that do not test ftrace_trace_stop in their
133 * mcount call site, we need to do it from C.
135 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
137 if (function_trace_stop)
140 __ftrace_trace_function(ip, parent_ip);
144 static int __register_ftrace_function(struct ftrace_ops *ops)
146 ops->next = ftrace_list;
148 * We are entering ops into the ftrace_list but another
149 * CPU might be walking that list. We need to make sure
150 * the ops->next pointer is valid before another CPU sees
151 * the ops pointer included into the ftrace_list.
156 if (ftrace_enabled) {
159 if (ops->next == &ftrace_list_end)
162 func = ftrace_list_func;
164 if (!list_empty(&ftrace_pids)) {
165 set_ftrace_pid_function(func);
166 func = ftrace_pid_func;
170 * For one func, simply call it directly.
171 * For more than one func, call the chain.
173 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
174 ftrace_trace_function = func;
176 __ftrace_trace_function = func;
177 ftrace_trace_function = ftrace_test_stop_func;
184 static int __unregister_ftrace_function(struct ftrace_ops *ops)
186 struct ftrace_ops **p;
189 * If we are removing the last function, then simply point
190 * to the ftrace_stub.
192 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
193 ftrace_trace_function = ftrace_stub;
194 ftrace_list = &ftrace_list_end;
198 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
207 if (ftrace_enabled) {
208 /* If we only have one func left, then call that directly */
209 if (ftrace_list->next == &ftrace_list_end) {
210 ftrace_func_t func = ftrace_list->func;
212 if (!list_empty(&ftrace_pids)) {
213 set_ftrace_pid_function(func);
214 func = ftrace_pid_func;
216 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
217 ftrace_trace_function = func;
219 __ftrace_trace_function = func;
227 static void ftrace_update_pid_func(void)
231 if (ftrace_trace_function == ftrace_stub)
234 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
235 func = ftrace_trace_function;
237 func = __ftrace_trace_function;
240 if (!list_empty(&ftrace_pids)) {
241 set_ftrace_pid_function(func);
242 func = ftrace_pid_func;
244 if (func == ftrace_pid_func)
245 func = ftrace_pid_function;
248 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
249 ftrace_trace_function = func;
251 __ftrace_trace_function = func;
255 #ifdef CONFIG_FUNCTION_PROFILER
256 struct ftrace_profile {
257 struct hlist_node node;
259 unsigned long counter;
260 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
261 unsigned long long time;
265 struct ftrace_profile_page {
266 struct ftrace_profile_page *next;
268 struct ftrace_profile records[];
271 struct ftrace_profile_stat {
273 struct hlist_head *hash;
274 struct ftrace_profile_page *pages;
275 struct ftrace_profile_page *start;
276 struct tracer_stat stat;
279 #define PROFILE_RECORDS_SIZE \
280 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
282 #define PROFILES_PER_PAGE \
283 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
285 static int ftrace_profile_bits __read_mostly;
286 static int ftrace_profile_enabled __read_mostly;
288 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
289 static DEFINE_MUTEX(ftrace_profile_lock);
291 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
293 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
296 function_stat_next(void *v, int idx)
298 struct ftrace_profile *rec = v;
299 struct ftrace_profile_page *pg;
301 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
307 if ((void *)rec >= (void *)&pg->records[pg->index]) {
311 rec = &pg->records[0];
319 static void *function_stat_start(struct tracer_stat *trace)
321 struct ftrace_profile_stat *stat =
322 container_of(trace, struct ftrace_profile_stat, stat);
324 if (!stat || !stat->start)
327 return function_stat_next(&stat->start->records[0], 0);
330 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
331 /* function graph compares on total time */
332 static int function_stat_cmp(void *p1, void *p2)
334 struct ftrace_profile *a = p1;
335 struct ftrace_profile *b = p2;
337 if (a->time < b->time)
339 if (a->time > b->time)
345 /* not function graph compares against hits */
346 static int function_stat_cmp(void *p1, void *p2)
348 struct ftrace_profile *a = p1;
349 struct ftrace_profile *b = p2;
351 if (a->counter < b->counter)
353 if (a->counter > b->counter)
360 static int function_stat_headers(struct seq_file *m)
362 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
363 seq_printf(m, " Function "
368 seq_printf(m, " Function Hit\n"
374 static int function_stat_show(struct seq_file *m, void *v)
376 struct ftrace_profile *rec = v;
377 char str[KSYM_SYMBOL_LEN];
378 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
379 static DEFINE_MUTEX(mutex);
380 static struct trace_seq s;
381 unsigned long long avg;
384 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
385 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
387 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
390 do_div(avg, rec->counter);
394 trace_print_graph_duration(rec->time, &s);
395 trace_seq_puts(&s, " ");
396 trace_print_graph_duration(avg, &s);
397 trace_print_seq(m, &s);
398 mutex_unlock(&mutex);
405 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
407 struct ftrace_profile_page *pg;
409 pg = stat->pages = stat->start;
412 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
417 memset(stat->hash, 0,
418 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
421 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
423 struct ftrace_profile_page *pg;
428 /* If we already allocated, do nothing */
432 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
436 #ifdef CONFIG_DYNAMIC_FTRACE
437 functions = ftrace_update_tot_cnt;
440 * We do not know the number of functions that exist because
441 * dynamic tracing is what counts them. With past experience
442 * we have around 20K functions. That should be more than enough.
443 * It is highly unlikely we will execute every function in
449 pg = stat->start = stat->pages;
451 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
453 for (i = 0; i < pages; i++) {
454 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
465 unsigned long tmp = (unsigned long)pg;
471 free_page((unsigned long)stat->pages);
478 static int ftrace_profile_init_cpu(int cpu)
480 struct ftrace_profile_stat *stat;
483 stat = &per_cpu(ftrace_profile_stats, cpu);
486 /* If the profile is already created, simply reset it */
487 ftrace_profile_reset(stat);
492 * We are profiling all functions, but usually only a few thousand
493 * functions are hit. We'll make a hash of 1024 items.
495 size = FTRACE_PROFILE_HASH_SIZE;
497 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
502 if (!ftrace_profile_bits) {
505 for (; size; size >>= 1)
506 ftrace_profile_bits++;
509 /* Preallocate the function profiling pages */
510 if (ftrace_profile_pages_init(stat) < 0) {
519 static int ftrace_profile_init(void)
524 for_each_online_cpu(cpu) {
525 ret = ftrace_profile_init_cpu(cpu);
533 /* interrupts must be disabled */
534 static struct ftrace_profile *
535 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
537 struct ftrace_profile *rec;
538 struct hlist_head *hhd;
539 struct hlist_node *n;
542 key = hash_long(ip, ftrace_profile_bits);
543 hhd = &stat->hash[key];
545 if (hlist_empty(hhd))
548 hlist_for_each_entry_rcu(rec, n, hhd, node) {
556 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
557 struct ftrace_profile *rec)
561 key = hash_long(rec->ip, ftrace_profile_bits);
562 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
566 * The memory is already allocated, this simply finds a new record to use.
568 static struct ftrace_profile *
569 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
571 struct ftrace_profile *rec = NULL;
573 /* prevent recursion (from NMIs) */
574 if (atomic_inc_return(&stat->disabled) != 1)
578 * Try to find the function again since an NMI
579 * could have added it
581 rec = ftrace_find_profiled_func(stat, ip);
585 if (stat->pages->index == PROFILES_PER_PAGE) {
586 if (!stat->pages->next)
588 stat->pages = stat->pages->next;
591 rec = &stat->pages->records[stat->pages->index++];
593 ftrace_add_profile(stat, rec);
596 atomic_dec(&stat->disabled);
602 function_profile_call(unsigned long ip, unsigned long parent_ip)
604 struct ftrace_profile_stat *stat;
605 struct ftrace_profile *rec;
608 if (!ftrace_profile_enabled)
611 local_irq_save(flags);
613 stat = &__get_cpu_var(ftrace_profile_stats);
614 if (!stat->hash || !ftrace_profile_enabled)
617 rec = ftrace_find_profiled_func(stat, ip);
619 rec = ftrace_profile_alloc(stat, ip);
626 local_irq_restore(flags);
629 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
630 static int profile_graph_entry(struct ftrace_graph_ent *trace)
632 function_profile_call(trace->func, 0);
636 static void profile_graph_return(struct ftrace_graph_ret *trace)
638 struct ftrace_profile_stat *stat;
639 unsigned long long calltime;
640 struct ftrace_profile *rec;
643 local_irq_save(flags);
644 stat = &__get_cpu_var(ftrace_profile_stats);
645 if (!stat->hash || !ftrace_profile_enabled)
648 calltime = trace->rettime - trace->calltime;
650 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
653 index = trace->depth;
655 /* Append this call time to the parent time to subtract */
657 current->ret_stack[index - 1].subtime += calltime;
659 if (current->ret_stack[index].subtime < calltime)
660 calltime -= current->ret_stack[index].subtime;
665 rec = ftrace_find_profiled_func(stat, trace->func);
667 rec->time += calltime;
670 local_irq_restore(flags);
673 static int register_ftrace_profiler(void)
675 return register_ftrace_graph(&profile_graph_return,
676 &profile_graph_entry);
679 static void unregister_ftrace_profiler(void)
681 unregister_ftrace_graph();
684 static struct ftrace_ops ftrace_profile_ops __read_mostly =
686 .func = function_profile_call,
689 static int register_ftrace_profiler(void)
691 return register_ftrace_function(&ftrace_profile_ops);
694 static void unregister_ftrace_profiler(void)
696 unregister_ftrace_function(&ftrace_profile_ops);
698 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
701 ftrace_profile_write(struct file *filp, const char __user *ubuf,
702 size_t cnt, loff_t *ppos)
705 char buf[64]; /* big enough to hold a number */
708 if (cnt >= sizeof(buf))
711 if (copy_from_user(&buf, ubuf, cnt))
716 ret = strict_strtoul(buf, 10, &val);
722 mutex_lock(&ftrace_profile_lock);
723 if (ftrace_profile_enabled ^ val) {
725 ret = ftrace_profile_init();
731 ret = register_ftrace_profiler();
736 ftrace_profile_enabled = 1;
738 ftrace_profile_enabled = 0;
740 * unregister_ftrace_profiler calls stop_machine
741 * so this acts like an synchronize_sched.
743 unregister_ftrace_profiler();
747 mutex_unlock(&ftrace_profile_lock);
755 ftrace_profile_read(struct file *filp, char __user *ubuf,
756 size_t cnt, loff_t *ppos)
758 char buf[64]; /* big enough to hold a number */
761 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
762 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
765 static const struct file_operations ftrace_profile_fops = {
766 .open = tracing_open_generic,
767 .read = ftrace_profile_read,
768 .write = ftrace_profile_write,
771 /* used to initialize the real stat files */
772 static struct tracer_stat function_stats __initdata = {
774 .stat_start = function_stat_start,
775 .stat_next = function_stat_next,
776 .stat_cmp = function_stat_cmp,
777 .stat_headers = function_stat_headers,
778 .stat_show = function_stat_show
781 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
783 struct ftrace_profile_stat *stat;
784 struct dentry *entry;
789 for_each_possible_cpu(cpu) {
790 stat = &per_cpu(ftrace_profile_stats, cpu);
792 /* allocate enough for function name + cpu number */
793 name = kmalloc(32, GFP_KERNEL);
796 * The files created are permanent, if something happens
797 * we still do not free memory.
800 "Could not allocate stat file for cpu %d\n",
804 stat->stat = function_stats;
805 snprintf(name, 32, "function%d", cpu);
806 stat->stat.name = name;
807 ret = register_stat_tracer(&stat->stat);
810 "Could not register function stat for cpu %d\n",
817 entry = debugfs_create_file("function_profile_enabled", 0644,
818 d_tracer, NULL, &ftrace_profile_fops);
820 pr_warning("Could not create debugfs "
821 "'function_profile_enabled' entry\n");
824 #else /* CONFIG_FUNCTION_PROFILER */
825 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
828 #endif /* CONFIG_FUNCTION_PROFILER */
830 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
832 #ifdef CONFIG_DYNAMIC_FTRACE
834 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
835 # error Dynamic ftrace depends on MCOUNT_RECORD
838 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
840 struct ftrace_func_probe {
841 struct hlist_node node;
842 struct ftrace_probe_ops *ops;
850 FTRACE_ENABLE_CALLS = (1 << 0),
851 FTRACE_DISABLE_CALLS = (1 << 1),
852 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
853 FTRACE_ENABLE_MCOUNT = (1 << 3),
854 FTRACE_DISABLE_MCOUNT = (1 << 4),
855 FTRACE_START_FUNC_RET = (1 << 5),
856 FTRACE_STOP_FUNC_RET = (1 << 6),
859 static int ftrace_filtered;
861 static struct dyn_ftrace *ftrace_new_addrs;
863 static DEFINE_MUTEX(ftrace_regex_lock);
866 struct ftrace_page *next;
868 struct dyn_ftrace records[];
871 #define ENTRIES_PER_PAGE \
872 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
874 /* estimate from running different kernels */
875 #define NR_TO_INIT 10000
877 static struct ftrace_page *ftrace_pages_start;
878 static struct ftrace_page *ftrace_pages;
880 static struct dyn_ftrace *ftrace_free_records;
883 * This is a double for. Do not use 'break' to break out of the loop,
884 * you must use a goto.
886 #define do_for_each_ftrace_rec(pg, rec) \
887 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
889 for (_____i = 0; _____i < pg->index; _____i++) { \
890 rec = &pg->records[_____i];
892 #define while_for_each_ftrace_rec() \
896 static void ftrace_free_rec(struct dyn_ftrace *rec)
898 rec->freelist = ftrace_free_records;
899 ftrace_free_records = rec;
900 rec->flags |= FTRACE_FL_FREE;
903 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
905 struct dyn_ftrace *rec;
907 /* First check for freed records */
908 if (ftrace_free_records) {
909 rec = ftrace_free_records;
911 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
912 FTRACE_WARN_ON_ONCE(1);
913 ftrace_free_records = NULL;
917 ftrace_free_records = rec->freelist;
918 memset(rec, 0, sizeof(*rec));
922 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
923 if (!ftrace_pages->next) {
924 /* allocate another page */
926 (void *)get_zeroed_page(GFP_KERNEL);
927 if (!ftrace_pages->next)
930 ftrace_pages = ftrace_pages->next;
933 return &ftrace_pages->records[ftrace_pages->index++];
936 static struct dyn_ftrace *
937 ftrace_record_ip(unsigned long ip)
939 struct dyn_ftrace *rec;
944 rec = ftrace_alloc_dyn_node(ip);
949 rec->newlist = ftrace_new_addrs;
950 ftrace_new_addrs = rec;
955 static void print_ip_ins(const char *fmt, unsigned char *p)
959 printk(KERN_CONT "%s", fmt);
961 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
962 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
965 static void ftrace_bug(int failed, unsigned long ip)
969 FTRACE_WARN_ON_ONCE(1);
970 pr_info("ftrace faulted on modifying ");
974 FTRACE_WARN_ON_ONCE(1);
975 pr_info("ftrace failed to modify ");
977 print_ip_ins(" actual: ", (unsigned char *)ip);
978 printk(KERN_CONT "\n");
981 FTRACE_WARN_ON_ONCE(1);
982 pr_info("ftrace faulted on writing ");
986 FTRACE_WARN_ON_ONCE(1);
987 pr_info("ftrace faulted on unknown error ");
993 /* Return 1 if the address range is reserved for ftrace */
994 int ftrace_text_reserved(void *start, void *end)
996 struct dyn_ftrace *rec;
997 struct ftrace_page *pg;
999 do_for_each_ftrace_rec(pg, rec) {
1000 if (rec->ip <= (unsigned long)end &&
1001 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1003 } while_for_each_ftrace_rec();
1009 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1011 unsigned long ftrace_addr;
1012 unsigned long flag = 0UL;
1014 ftrace_addr = (unsigned long)FTRACE_ADDR;
1017 * If this record is not to be traced or we want to disable it,
1020 * If we want to enable it and filtering is off, then enable it.
1022 * If we want to enable it and filtering is on, enable it only if
1025 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1026 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1027 flag = FTRACE_FL_ENABLED;
1030 /* If the state of this record hasn't changed, then do nothing */
1031 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1035 rec->flags |= FTRACE_FL_ENABLED;
1036 return ftrace_make_call(rec, ftrace_addr);
1039 rec->flags &= ~FTRACE_FL_ENABLED;
1040 return ftrace_make_nop(NULL, rec, ftrace_addr);
1043 static void ftrace_replace_code(int enable)
1045 struct dyn_ftrace *rec;
1046 struct ftrace_page *pg;
1049 do_for_each_ftrace_rec(pg, rec) {
1051 * Skip over free records, records that have
1052 * failed and not converted.
1054 if (rec->flags & FTRACE_FL_FREE ||
1055 rec->flags & FTRACE_FL_FAILED ||
1056 !(rec->flags & FTRACE_FL_CONVERTED))
1059 failed = __ftrace_replace_code(rec, enable);
1061 rec->flags |= FTRACE_FL_FAILED;
1062 ftrace_bug(failed, rec->ip);
1063 /* Stop processing */
1066 } while_for_each_ftrace_rec();
1070 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1077 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1079 ftrace_bug(ret, ip);
1080 rec->flags |= FTRACE_FL_FAILED;
1087 * archs can override this function if they must do something
1088 * before the modifying code is performed.
1090 int __weak ftrace_arch_code_modify_prepare(void)
1096 * archs can override this function if they must do something
1097 * after the modifying code is performed.
1099 int __weak ftrace_arch_code_modify_post_process(void)
1104 static int __ftrace_modify_code(void *data)
1106 int *command = data;
1108 if (*command & FTRACE_ENABLE_CALLS)
1109 ftrace_replace_code(1);
1110 else if (*command & FTRACE_DISABLE_CALLS)
1111 ftrace_replace_code(0);
1113 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1114 ftrace_update_ftrace_func(ftrace_trace_function);
1116 if (*command & FTRACE_START_FUNC_RET)
1117 ftrace_enable_ftrace_graph_caller();
1118 else if (*command & FTRACE_STOP_FUNC_RET)
1119 ftrace_disable_ftrace_graph_caller();
1124 static void ftrace_run_update_code(int command)
1128 ret = ftrace_arch_code_modify_prepare();
1129 FTRACE_WARN_ON(ret);
1133 stop_machine(__ftrace_modify_code, &command, NULL);
1135 ret = ftrace_arch_code_modify_post_process();
1136 FTRACE_WARN_ON(ret);
1139 static ftrace_func_t saved_ftrace_func;
1140 static int ftrace_start_up;
1142 static void ftrace_startup_enable(int command)
1144 if (saved_ftrace_func != ftrace_trace_function) {
1145 saved_ftrace_func = ftrace_trace_function;
1146 command |= FTRACE_UPDATE_TRACE_FUNC;
1149 if (!command || !ftrace_enabled)
1152 ftrace_run_update_code(command);
1155 static void ftrace_startup(int command)
1157 if (unlikely(ftrace_disabled))
1161 command |= FTRACE_ENABLE_CALLS;
1163 ftrace_startup_enable(command);
1166 static void ftrace_shutdown(int command)
1168 if (unlikely(ftrace_disabled))
1173 * Just warn in case of unbalance, no need to kill ftrace, it's not
1174 * critical but the ftrace_call callers may be never nopped again after
1175 * further ftrace uses.
1177 WARN_ON_ONCE(ftrace_start_up < 0);
1179 if (!ftrace_start_up)
1180 command |= FTRACE_DISABLE_CALLS;
1182 if (saved_ftrace_func != ftrace_trace_function) {
1183 saved_ftrace_func = ftrace_trace_function;
1184 command |= FTRACE_UPDATE_TRACE_FUNC;
1187 if (!command || !ftrace_enabled)
1190 ftrace_run_update_code(command);
1193 static void ftrace_startup_sysctl(void)
1195 int command = FTRACE_ENABLE_MCOUNT;
1197 if (unlikely(ftrace_disabled))
1200 /* Force update next time */
1201 saved_ftrace_func = NULL;
1202 /* ftrace_start_up is true if we want ftrace running */
1203 if (ftrace_start_up)
1204 command |= FTRACE_ENABLE_CALLS;
1206 ftrace_run_update_code(command);
1209 static void ftrace_shutdown_sysctl(void)
1211 int command = FTRACE_DISABLE_MCOUNT;
1213 if (unlikely(ftrace_disabled))
1216 /* ftrace_start_up is true if ftrace is running */
1217 if (ftrace_start_up)
1218 command |= FTRACE_DISABLE_CALLS;
1220 ftrace_run_update_code(command);
1223 static cycle_t ftrace_update_time;
1224 static unsigned long ftrace_update_cnt;
1225 unsigned long ftrace_update_tot_cnt;
1227 static int ftrace_update_code(struct module *mod)
1229 struct dyn_ftrace *p;
1230 cycle_t start, stop;
1232 start = ftrace_now(raw_smp_processor_id());
1233 ftrace_update_cnt = 0;
1235 while (ftrace_new_addrs) {
1237 /* If something went wrong, bail without enabling anything */
1238 if (unlikely(ftrace_disabled))
1241 p = ftrace_new_addrs;
1242 ftrace_new_addrs = p->newlist;
1246 * Do the initial record convertion from mcount jump
1247 * to the NOP instructions.
1249 if (!ftrace_code_disable(mod, p)) {
1254 p->flags |= FTRACE_FL_CONVERTED;
1255 ftrace_update_cnt++;
1258 * If the tracing is enabled, go ahead and enable the record.
1260 * The reason not to enable the record immediatelly is the
1261 * inherent check of ftrace_make_nop/ftrace_make_call for
1262 * correct previous instructions. Making first the NOP
1263 * conversion puts the module to the correct state, thus
1264 * passing the ftrace_make_call check.
1266 if (ftrace_start_up) {
1267 int failed = __ftrace_replace_code(p, 1);
1269 ftrace_bug(failed, p->ip);
1275 stop = ftrace_now(raw_smp_processor_id());
1276 ftrace_update_time = stop - start;
1277 ftrace_update_tot_cnt += ftrace_update_cnt;
1282 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1284 struct ftrace_page *pg;
1288 /* allocate a few pages */
1289 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1290 if (!ftrace_pages_start)
1294 * Allocate a few more pages.
1296 * TODO: have some parser search vmlinux before
1297 * final linking to find all calls to ftrace.
1299 * a) know how many pages to allocate.
1301 * b) set up the table then.
1303 * The dynamic code is still necessary for
1307 pg = ftrace_pages = ftrace_pages_start;
1309 cnt = num_to_init / ENTRIES_PER_PAGE;
1310 pr_info("ftrace: allocating %ld entries in %d pages\n",
1311 num_to_init, cnt + 1);
1313 for (i = 0; i < cnt; i++) {
1314 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1316 /* If we fail, we'll try later anyway */
1327 FTRACE_ITER_FILTER = (1 << 0),
1328 FTRACE_ITER_NOTRACE = (1 << 1),
1329 FTRACE_ITER_FAILURES = (1 << 2),
1330 FTRACE_ITER_PRINTALL = (1 << 3),
1331 FTRACE_ITER_HASH = (1 << 4),
1334 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1336 struct ftrace_iterator {
1337 struct ftrace_page *pg;
1341 struct trace_parser parser;
1345 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1347 struct ftrace_iterator *iter = m->private;
1348 struct hlist_node *hnd = v;
1349 struct hlist_head *hhd;
1351 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1356 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1359 hhd = &ftrace_func_hash[iter->hidx];
1361 if (hlist_empty(hhd)) {
1380 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1382 struct ftrace_iterator *iter = m->private;
1386 if (!(iter->flags & FTRACE_ITER_HASH))
1389 iter->flags |= FTRACE_ITER_HASH;
1392 for (l = 0; l <= *pos; ) {
1393 p = t_hash_next(m, p, &l);
1400 static int t_hash_show(struct seq_file *m, void *v)
1402 struct ftrace_func_probe *rec;
1403 struct hlist_node *hnd = v;
1405 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1407 if (rec->ops->print)
1408 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1410 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1413 seq_printf(m, ":%p", rec->data);
1420 t_next(struct seq_file *m, void *v, loff_t *pos)
1422 struct ftrace_iterator *iter = m->private;
1423 struct dyn_ftrace *rec = NULL;
1425 if (iter->flags & FTRACE_ITER_HASH)
1426 return t_hash_next(m, v, pos);
1430 if (iter->flags & FTRACE_ITER_PRINTALL)
1434 if (iter->idx >= iter->pg->index) {
1435 if (iter->pg->next) {
1436 iter->pg = iter->pg->next;
1441 rec = &iter->pg->records[iter->idx++];
1442 if ((rec->flags & FTRACE_FL_FREE) ||
1444 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1445 (rec->flags & FTRACE_FL_FAILED)) ||
1447 ((iter->flags & FTRACE_ITER_FAILURES) &&
1448 !(rec->flags & FTRACE_FL_FAILED)) ||
1450 ((iter->flags & FTRACE_ITER_FILTER) &&
1451 !(rec->flags & FTRACE_FL_FILTER)) ||
1453 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1454 !(rec->flags & FTRACE_FL_NOTRACE))) {
1463 static void *t_start(struct seq_file *m, loff_t *pos)
1465 struct ftrace_iterator *iter = m->private;
1469 mutex_lock(&ftrace_lock);
1471 * For set_ftrace_filter reading, if we have the filter
1472 * off, we can short cut and just print out that all
1473 * functions are enabled.
1475 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1477 return t_hash_start(m, pos);
1478 iter->flags |= FTRACE_ITER_PRINTALL;
1482 if (iter->flags & FTRACE_ITER_HASH)
1483 return t_hash_start(m, pos);
1485 iter->pg = ftrace_pages_start;
1487 for (l = 0; l <= *pos; ) {
1488 p = t_next(m, p, &l);
1493 if (!p && iter->flags & FTRACE_ITER_FILTER)
1494 return t_hash_start(m, pos);
1499 static void t_stop(struct seq_file *m, void *p)
1501 mutex_unlock(&ftrace_lock);
1504 static int t_show(struct seq_file *m, void *v)
1506 struct ftrace_iterator *iter = m->private;
1507 struct dyn_ftrace *rec = v;
1509 if (iter->flags & FTRACE_ITER_HASH)
1510 return t_hash_show(m, v);
1512 if (iter->flags & FTRACE_ITER_PRINTALL) {
1513 seq_printf(m, "#### all functions enabled ####\n");
1520 seq_printf(m, "%ps\n", (void *)rec->ip);
1525 static const struct seq_operations show_ftrace_seq_ops = {
1533 ftrace_avail_open(struct inode *inode, struct file *file)
1535 struct ftrace_iterator *iter;
1538 if (unlikely(ftrace_disabled))
1541 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1545 iter->pg = ftrace_pages_start;
1547 ret = seq_open(file, &show_ftrace_seq_ops);
1549 struct seq_file *m = file->private_data;
1560 ftrace_failures_open(struct inode *inode, struct file *file)
1564 struct ftrace_iterator *iter;
1566 ret = ftrace_avail_open(inode, file);
1568 m = (struct seq_file *)file->private_data;
1569 iter = (struct ftrace_iterator *)m->private;
1570 iter->flags = FTRACE_ITER_FAILURES;
1577 static void ftrace_filter_reset(int enable)
1579 struct ftrace_page *pg;
1580 struct dyn_ftrace *rec;
1581 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1583 mutex_lock(&ftrace_lock);
1585 ftrace_filtered = 0;
1586 do_for_each_ftrace_rec(pg, rec) {
1587 if (rec->flags & FTRACE_FL_FAILED)
1589 rec->flags &= ~type;
1590 } while_for_each_ftrace_rec();
1591 mutex_unlock(&ftrace_lock);
1595 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1597 struct ftrace_iterator *iter;
1600 if (unlikely(ftrace_disabled))
1603 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1607 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1612 mutex_lock(&ftrace_regex_lock);
1613 if ((file->f_mode & FMODE_WRITE) &&
1614 (file->f_flags & O_TRUNC))
1615 ftrace_filter_reset(enable);
1617 if (file->f_mode & FMODE_READ) {
1618 iter->pg = ftrace_pages_start;
1619 iter->flags = enable ? FTRACE_ITER_FILTER :
1620 FTRACE_ITER_NOTRACE;
1622 ret = seq_open(file, &show_ftrace_seq_ops);
1624 struct seq_file *m = file->private_data;
1627 trace_parser_put(&iter->parser);
1631 file->private_data = iter;
1632 mutex_unlock(&ftrace_regex_lock);
1638 ftrace_filter_open(struct inode *inode, struct file *file)
1640 return ftrace_regex_open(inode, file, 1);
1644 ftrace_notrace_open(struct inode *inode, struct file *file)
1646 return ftrace_regex_open(inode, file, 0);
1650 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1654 if (file->f_mode & FMODE_READ)
1655 ret = seq_lseek(file, offset, origin);
1657 file->f_pos = ret = 1;
1662 static int ftrace_match(char *str, char *regex, int len, int type)
1669 if (strcmp(str, regex) == 0)
1672 case MATCH_FRONT_ONLY:
1673 if (strncmp(str, regex, len) == 0)
1676 case MATCH_MIDDLE_ONLY:
1677 if (strstr(str, regex))
1680 case MATCH_END_ONLY:
1682 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
1691 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1693 char str[KSYM_SYMBOL_LEN];
1695 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1696 return ftrace_match(str, regex, len, type);
1699 static int ftrace_match_records(char *buff, int len, int enable)
1701 unsigned int search_len;
1702 struct ftrace_page *pg;
1703 struct dyn_ftrace *rec;
1710 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1711 type = filter_parse_regex(buff, len, &search, ¬);
1713 search_len = strlen(search);
1715 mutex_lock(&ftrace_lock);
1716 do_for_each_ftrace_rec(pg, rec) {
1718 if (rec->flags & FTRACE_FL_FAILED)
1721 if (ftrace_match_record(rec, search, search_len, type)) {
1723 rec->flags &= ~flag;
1729 * Only enable filtering if we have a function that
1732 if (enable && (rec->flags & FTRACE_FL_FILTER))
1733 ftrace_filtered = 1;
1734 } while_for_each_ftrace_rec();
1735 mutex_unlock(&ftrace_lock);
1741 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1742 char *regex, int len, int type)
1744 char str[KSYM_SYMBOL_LEN];
1747 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1749 if (!modname || strcmp(modname, mod))
1752 /* blank search means to match all funcs in the mod */
1754 return ftrace_match(str, regex, len, type);
1759 static int ftrace_match_module_records(char *buff, char *mod, int enable)
1761 unsigned search_len = 0;
1762 struct ftrace_page *pg;
1763 struct dyn_ftrace *rec;
1764 int type = MATCH_FULL;
1765 char *search = buff;
1770 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1772 /* blank or '*' mean the same */
1773 if (strcmp(buff, "*") == 0)
1776 /* handle the case of 'dont filter this module' */
1777 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1783 type = filter_parse_regex(buff, strlen(buff), &search, ¬);
1784 search_len = strlen(search);
1787 mutex_lock(&ftrace_lock);
1788 do_for_each_ftrace_rec(pg, rec) {
1790 if (rec->flags & FTRACE_FL_FAILED)
1793 if (ftrace_match_module_record(rec, mod,
1794 search, search_len, type)) {
1796 rec->flags &= ~flag;
1801 if (enable && (rec->flags & FTRACE_FL_FILTER))
1802 ftrace_filtered = 1;
1804 } while_for_each_ftrace_rec();
1805 mutex_unlock(&ftrace_lock);
1811 * We register the module command as a template to show others how
1812 * to register the a command as well.
1816 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1821 * cmd == 'mod' because we only registered this func
1822 * for the 'mod' ftrace_func_command.
1823 * But if you register one func with multiple commands,
1824 * you can tell which command was used by the cmd
1828 /* we must have a module name */
1832 mod = strsep(¶m, ":");
1836 if (ftrace_match_module_records(func, mod, enable))
1841 static struct ftrace_func_command ftrace_mod_cmd = {
1843 .func = ftrace_mod_callback,
1846 static int __init ftrace_mod_cmd_init(void)
1848 return register_ftrace_command(&ftrace_mod_cmd);
1850 device_initcall(ftrace_mod_cmd_init);
1853 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1855 struct ftrace_func_probe *entry;
1856 struct hlist_head *hhd;
1857 struct hlist_node *n;
1861 key = hash_long(ip, FTRACE_HASH_BITS);
1863 hhd = &ftrace_func_hash[key];
1865 if (hlist_empty(hhd))
1869 * Disable preemption for these calls to prevent a RCU grace
1870 * period. This syncs the hash iteration and freeing of items
1871 * on the hash. rcu_read_lock is too dangerous here.
1873 resched = ftrace_preempt_disable();
1874 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1875 if (entry->ip == ip)
1876 entry->ops->func(ip, parent_ip, &entry->data);
1878 ftrace_preempt_enable(resched);
1881 static struct ftrace_ops trace_probe_ops __read_mostly =
1883 .func = function_trace_probe_call,
1886 static int ftrace_probe_registered;
1888 static void __enable_ftrace_function_probe(void)
1892 if (ftrace_probe_registered)
1895 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1896 struct hlist_head *hhd = &ftrace_func_hash[i];
1900 /* Nothing registered? */
1901 if (i == FTRACE_FUNC_HASHSIZE)
1904 __register_ftrace_function(&trace_probe_ops);
1906 ftrace_probe_registered = 1;
1909 static void __disable_ftrace_function_probe(void)
1913 if (!ftrace_probe_registered)
1916 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1917 struct hlist_head *hhd = &ftrace_func_hash[i];
1922 /* no more funcs left */
1923 __unregister_ftrace_function(&trace_probe_ops);
1925 ftrace_probe_registered = 0;
1929 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1931 struct ftrace_func_probe *entry =
1932 container_of(rhp, struct ftrace_func_probe, rcu);
1934 if (entry->ops->free)
1935 entry->ops->free(&entry->data);
1941 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1944 struct ftrace_func_probe *entry;
1945 struct ftrace_page *pg;
1946 struct dyn_ftrace *rec;
1952 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
1953 len = strlen(search);
1955 /* we do not support '!' for function probes */
1959 mutex_lock(&ftrace_lock);
1960 do_for_each_ftrace_rec(pg, rec) {
1962 if (rec->flags & FTRACE_FL_FAILED)
1965 if (!ftrace_match_record(rec, search, len, type))
1968 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1970 /* If we did not process any, then return error */
1981 * The caller might want to do something special
1982 * for each function we find. We call the callback
1983 * to give the caller an opportunity to do so.
1985 if (ops->callback) {
1986 if (ops->callback(rec->ip, &entry->data) < 0) {
1987 /* caller does not like this func */
1994 entry->ip = rec->ip;
1996 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1997 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1999 } while_for_each_ftrace_rec();
2000 __enable_ftrace_function_probe();
2003 mutex_unlock(&ftrace_lock);
2009 PROBE_TEST_FUNC = 1,
2014 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2015 void *data, int flags)
2017 struct ftrace_func_probe *entry;
2018 struct hlist_node *n, *tmp;
2019 char str[KSYM_SYMBOL_LEN];
2020 int type = MATCH_FULL;
2024 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2029 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2030 len = strlen(search);
2032 /* we do not support '!' for function probes */
2037 mutex_lock(&ftrace_lock);
2038 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2039 struct hlist_head *hhd = &ftrace_func_hash[i];
2041 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2043 /* break up if statements for readability */
2044 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2047 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2050 /* do this last, since it is the most expensive */
2052 kallsyms_lookup(entry->ip, NULL, NULL,
2054 if (!ftrace_match(str, glob, len, type))
2058 hlist_del(&entry->node);
2059 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2062 __disable_ftrace_function_probe();
2063 mutex_unlock(&ftrace_lock);
2067 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2070 __unregister_ftrace_function_probe(glob, ops, data,
2071 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2075 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2077 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2080 void unregister_ftrace_function_probe_all(char *glob)
2082 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2085 static LIST_HEAD(ftrace_commands);
2086 static DEFINE_MUTEX(ftrace_cmd_mutex);
2088 int register_ftrace_command(struct ftrace_func_command *cmd)
2090 struct ftrace_func_command *p;
2093 mutex_lock(&ftrace_cmd_mutex);
2094 list_for_each_entry(p, &ftrace_commands, list) {
2095 if (strcmp(cmd->name, p->name) == 0) {
2100 list_add(&cmd->list, &ftrace_commands);
2102 mutex_unlock(&ftrace_cmd_mutex);
2107 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2109 struct ftrace_func_command *p, *n;
2112 mutex_lock(&ftrace_cmd_mutex);
2113 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2114 if (strcmp(cmd->name, p->name) == 0) {
2116 list_del_init(&p->list);
2121 mutex_unlock(&ftrace_cmd_mutex);
2126 static int ftrace_process_regex(char *buff, int len, int enable)
2128 char *func, *command, *next = buff;
2129 struct ftrace_func_command *p;
2132 func = strsep(&next, ":");
2135 if (ftrace_match_records(func, len, enable))
2142 command = strsep(&next, ":");
2144 mutex_lock(&ftrace_cmd_mutex);
2145 list_for_each_entry(p, &ftrace_commands, list) {
2146 if (strcmp(p->name, command) == 0) {
2147 ret = p->func(func, command, next, enable);
2152 mutex_unlock(&ftrace_cmd_mutex);
2158 ftrace_regex_write(struct file *file, const char __user *ubuf,
2159 size_t cnt, loff_t *ppos, int enable)
2161 struct ftrace_iterator *iter;
2162 struct trace_parser *parser;
2168 mutex_lock(&ftrace_regex_lock);
2170 if (file->f_mode & FMODE_READ) {
2171 struct seq_file *m = file->private_data;
2174 iter = file->private_data;
2176 parser = &iter->parser;
2177 read = trace_get_user(parser, ubuf, cnt, ppos);
2179 if (read >= 0 && trace_parser_loaded(parser) &&
2180 !trace_parser_cont(parser)) {
2181 ret = ftrace_process_regex(parser->buffer,
2182 parser->idx, enable);
2183 trace_parser_clear(parser);
2190 mutex_unlock(&ftrace_regex_lock);
2196 ftrace_filter_write(struct file *file, const char __user *ubuf,
2197 size_t cnt, loff_t *ppos)
2199 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2203 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2204 size_t cnt, loff_t *ppos)
2206 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2210 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2212 if (unlikely(ftrace_disabled))
2215 mutex_lock(&ftrace_regex_lock);
2217 ftrace_filter_reset(enable);
2219 ftrace_match_records(buf, len, enable);
2220 mutex_unlock(&ftrace_regex_lock);
2224 * ftrace_set_filter - set a function to filter on in ftrace
2225 * @buf - the string that holds the function filter text.
2226 * @len - the length of the string.
2227 * @reset - non zero to reset all filters before applying this filter.
2229 * Filters denote which functions should be enabled when tracing is enabled.
2230 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2232 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2234 ftrace_set_regex(buf, len, reset, 1);
2238 * ftrace_set_notrace - set a function to not trace in ftrace
2239 * @buf - the string that holds the function notrace text.
2240 * @len - the length of the string.
2241 * @reset - non zero to reset all filters before applying this filter.
2243 * Notrace Filters denote which functions should not be enabled when tracing
2244 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2247 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2249 ftrace_set_regex(buf, len, reset, 0);
2253 * command line interface to allow users to set filters on boot up.
2255 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2256 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2257 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2259 static int __init set_ftrace_notrace(char *str)
2261 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2264 __setup("ftrace_notrace=", set_ftrace_notrace);
2266 static int __init set_ftrace_filter(char *str)
2268 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2271 __setup("ftrace_filter=", set_ftrace_filter);
2273 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2274 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2275 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2277 static int __init set_graph_function(char *str)
2279 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2282 __setup("ftrace_graph_filter=", set_graph_function);
2284 static void __init set_ftrace_early_graph(char *buf)
2290 func = strsep(&buf, ",");
2291 /* we allow only one expression at a time */
2292 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2295 printk(KERN_DEBUG "ftrace: function %s not "
2296 "traceable\n", func);
2299 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2301 static void __init set_ftrace_early_filter(char *buf, int enable)
2306 func = strsep(&buf, ",");
2307 ftrace_set_regex(func, strlen(func), 0, enable);
2311 static void __init set_ftrace_early_filters(void)
2313 if (ftrace_filter_buf[0])
2314 set_ftrace_early_filter(ftrace_filter_buf, 1);
2315 if (ftrace_notrace_buf[0])
2316 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2317 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2318 if (ftrace_graph_buf[0])
2319 set_ftrace_early_graph(ftrace_graph_buf);
2320 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2324 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2326 struct seq_file *m = (struct seq_file *)file->private_data;
2327 struct ftrace_iterator *iter;
2328 struct trace_parser *parser;
2330 mutex_lock(&ftrace_regex_lock);
2331 if (file->f_mode & FMODE_READ) {
2334 seq_release(inode, file);
2336 iter = file->private_data;
2338 parser = &iter->parser;
2339 if (trace_parser_loaded(parser)) {
2340 parser->buffer[parser->idx] = 0;
2341 ftrace_match_records(parser->buffer, parser->idx, enable);
2344 mutex_lock(&ftrace_lock);
2345 if (ftrace_start_up && ftrace_enabled)
2346 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2347 mutex_unlock(&ftrace_lock);
2349 trace_parser_put(parser);
2352 mutex_unlock(&ftrace_regex_lock);
2357 ftrace_filter_release(struct inode *inode, struct file *file)
2359 return ftrace_regex_release(inode, file, 1);
2363 ftrace_notrace_release(struct inode *inode, struct file *file)
2365 return ftrace_regex_release(inode, file, 0);
2368 static const struct file_operations ftrace_avail_fops = {
2369 .open = ftrace_avail_open,
2371 .llseek = seq_lseek,
2372 .release = seq_release_private,
2375 static const struct file_operations ftrace_failures_fops = {
2376 .open = ftrace_failures_open,
2378 .llseek = seq_lseek,
2379 .release = seq_release_private,
2382 static const struct file_operations ftrace_filter_fops = {
2383 .open = ftrace_filter_open,
2385 .write = ftrace_filter_write,
2386 .llseek = ftrace_regex_lseek,
2387 .release = ftrace_filter_release,
2390 static const struct file_operations ftrace_notrace_fops = {
2391 .open = ftrace_notrace_open,
2393 .write = ftrace_notrace_write,
2394 .llseek = ftrace_regex_lseek,
2395 .release = ftrace_notrace_release,
2398 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2400 static DEFINE_MUTEX(graph_lock);
2402 int ftrace_graph_count;
2403 int ftrace_graph_filter_enabled;
2404 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2407 __g_next(struct seq_file *m, loff_t *pos)
2409 if (*pos >= ftrace_graph_count)
2411 return &ftrace_graph_funcs[*pos];
2415 g_next(struct seq_file *m, void *v, loff_t *pos)
2418 return __g_next(m, pos);
2421 static void *g_start(struct seq_file *m, loff_t *pos)
2423 mutex_lock(&graph_lock);
2425 /* Nothing, tell g_show to print all functions are enabled */
2426 if (!ftrace_graph_filter_enabled && !*pos)
2429 return __g_next(m, pos);
2432 static void g_stop(struct seq_file *m, void *p)
2434 mutex_unlock(&graph_lock);
2437 static int g_show(struct seq_file *m, void *v)
2439 unsigned long *ptr = v;
2444 if (ptr == (unsigned long *)1) {
2445 seq_printf(m, "#### all functions enabled ####\n");
2449 seq_printf(m, "%ps\n", (void *)*ptr);
2454 static const struct seq_operations ftrace_graph_seq_ops = {
2462 ftrace_graph_open(struct inode *inode, struct file *file)
2466 if (unlikely(ftrace_disabled))
2469 mutex_lock(&graph_lock);
2470 if ((file->f_mode & FMODE_WRITE) &&
2471 (file->f_flags & O_TRUNC)) {
2472 ftrace_graph_filter_enabled = 0;
2473 ftrace_graph_count = 0;
2474 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2476 mutex_unlock(&graph_lock);
2478 if (file->f_mode & FMODE_READ)
2479 ret = seq_open(file, &ftrace_graph_seq_ops);
2485 ftrace_graph_release(struct inode *inode, struct file *file)
2487 if (file->f_mode & FMODE_READ)
2488 seq_release(inode, file);
2493 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2495 struct dyn_ftrace *rec;
2496 struct ftrace_page *pg;
2504 if (ftrace_disabled)
2508 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
2509 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2512 search_len = strlen(search);
2514 mutex_lock(&ftrace_lock);
2515 do_for_each_ftrace_rec(pg, rec) {
2517 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2520 if (ftrace_match_record(rec, search, search_len, type)) {
2521 /* if it is in the array */
2523 for (i = 0; i < *idx; i++) {
2524 if (array[i] == rec->ip) {
2533 array[(*idx)++] = rec->ip;
2534 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2539 array[i] = array[--(*idx)];
2545 } while_for_each_ftrace_rec();
2547 mutex_unlock(&ftrace_lock);
2552 ftrace_graph_filter_enabled = 1;
2557 ftrace_graph_write(struct file *file, const char __user *ubuf,
2558 size_t cnt, loff_t *ppos)
2560 struct trace_parser parser;
2566 mutex_lock(&graph_lock);
2568 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2573 read = trace_get_user(&parser, ubuf, cnt, ppos);
2575 if (read >= 0 && trace_parser_loaded((&parser))) {
2576 parser.buffer[parser.idx] = 0;
2578 /* we allow only one expression at a time */
2579 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2588 trace_parser_put(&parser);
2590 mutex_unlock(&graph_lock);
2595 static const struct file_operations ftrace_graph_fops = {
2596 .open = ftrace_graph_open,
2598 .write = ftrace_graph_write,
2599 .release = ftrace_graph_release,
2601 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2603 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2606 trace_create_file("available_filter_functions", 0444,
2607 d_tracer, NULL, &ftrace_avail_fops);
2609 trace_create_file("failures", 0444,
2610 d_tracer, NULL, &ftrace_failures_fops);
2612 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2613 NULL, &ftrace_filter_fops);
2615 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2616 NULL, &ftrace_notrace_fops);
2618 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2619 trace_create_file("set_graph_function", 0444, d_tracer,
2621 &ftrace_graph_fops);
2622 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2627 static int ftrace_process_locs(struct module *mod,
2628 unsigned long *start,
2633 unsigned long flags;
2635 mutex_lock(&ftrace_lock);
2638 addr = ftrace_call_adjust(*p++);
2640 * Some architecture linkers will pad between
2641 * the different mcount_loc sections of different
2642 * object files to satisfy alignments.
2643 * Skip any NULL pointers.
2647 ftrace_record_ip(addr);
2650 /* disable interrupts to prevent kstop machine */
2651 local_irq_save(flags);
2652 ftrace_update_code(mod);
2653 local_irq_restore(flags);
2654 mutex_unlock(&ftrace_lock);
2659 #ifdef CONFIG_MODULES
2660 void ftrace_release_mod(struct module *mod)
2662 struct dyn_ftrace *rec;
2663 struct ftrace_page *pg;
2665 if (ftrace_disabled)
2668 mutex_lock(&ftrace_lock);
2669 do_for_each_ftrace_rec(pg, rec) {
2670 if (within_module_core(rec->ip, mod)) {
2672 * rec->ip is changed in ftrace_free_rec()
2673 * It should not between s and e if record was freed.
2675 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2676 ftrace_free_rec(rec);
2678 } while_for_each_ftrace_rec();
2679 mutex_unlock(&ftrace_lock);
2682 static void ftrace_init_module(struct module *mod,
2683 unsigned long *start, unsigned long *end)
2685 if (ftrace_disabled || start == end)
2687 ftrace_process_locs(mod, start, end);
2690 static int ftrace_module_notify(struct notifier_block *self,
2691 unsigned long val, void *data)
2693 struct module *mod = data;
2696 case MODULE_STATE_COMING:
2697 ftrace_init_module(mod, mod->ftrace_callsites,
2698 mod->ftrace_callsites +
2699 mod->num_ftrace_callsites);
2701 case MODULE_STATE_GOING:
2702 ftrace_release_mod(mod);
2709 static int ftrace_module_notify(struct notifier_block *self,
2710 unsigned long val, void *data)
2714 #endif /* CONFIG_MODULES */
2716 struct notifier_block ftrace_module_nb = {
2717 .notifier_call = ftrace_module_notify,
2721 extern unsigned long __start_mcount_loc[];
2722 extern unsigned long __stop_mcount_loc[];
2724 void __init ftrace_init(void)
2726 unsigned long count, addr, flags;
2729 /* Keep the ftrace pointer to the stub */
2730 addr = (unsigned long)ftrace_stub;
2732 local_irq_save(flags);
2733 ftrace_dyn_arch_init(&addr);
2734 local_irq_restore(flags);
2736 /* ftrace_dyn_arch_init places the return code in addr */
2740 count = __stop_mcount_loc - __start_mcount_loc;
2742 ret = ftrace_dyn_table_alloc(count);
2746 last_ftrace_enabled = ftrace_enabled = 1;
2748 ret = ftrace_process_locs(NULL,
2752 ret = register_module_notifier(&ftrace_module_nb);
2754 pr_warning("Failed to register trace ftrace module notifier\n");
2756 set_ftrace_early_filters();
2760 ftrace_disabled = 1;
2765 static int __init ftrace_nodyn_init(void)
2770 device_initcall(ftrace_nodyn_init);
2772 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2773 static inline void ftrace_startup_enable(int command) { }
2774 /* Keep as macros so we do not need to define the commands */
2775 # define ftrace_startup(command) do { } while (0)
2776 # define ftrace_shutdown(command) do { } while (0)
2777 # define ftrace_startup_sysctl() do { } while (0)
2778 # define ftrace_shutdown_sysctl() do { } while (0)
2779 #endif /* CONFIG_DYNAMIC_FTRACE */
2781 static void clear_ftrace_swapper(void)
2783 struct task_struct *p;
2787 for_each_online_cpu(cpu) {
2789 clear_tsk_trace_trace(p);
2794 static void set_ftrace_swapper(void)
2796 struct task_struct *p;
2800 for_each_online_cpu(cpu) {
2802 set_tsk_trace_trace(p);
2807 static void clear_ftrace_pid(struct pid *pid)
2809 struct task_struct *p;
2812 do_each_pid_task(pid, PIDTYPE_PID, p) {
2813 clear_tsk_trace_trace(p);
2814 } while_each_pid_task(pid, PIDTYPE_PID, p);
2820 static void set_ftrace_pid(struct pid *pid)
2822 struct task_struct *p;
2825 do_each_pid_task(pid, PIDTYPE_PID, p) {
2826 set_tsk_trace_trace(p);
2827 } while_each_pid_task(pid, PIDTYPE_PID, p);
2831 static void clear_ftrace_pid_task(struct pid *pid)
2833 if (pid == ftrace_swapper_pid)
2834 clear_ftrace_swapper();
2836 clear_ftrace_pid(pid);
2839 static void set_ftrace_pid_task(struct pid *pid)
2841 if (pid == ftrace_swapper_pid)
2842 set_ftrace_swapper();
2844 set_ftrace_pid(pid);
2847 static int ftrace_pid_add(int p)
2850 struct ftrace_pid *fpid;
2853 mutex_lock(&ftrace_lock);
2856 pid = ftrace_swapper_pid;
2858 pid = find_get_pid(p);
2865 list_for_each_entry(fpid, &ftrace_pids, list)
2866 if (fpid->pid == pid)
2871 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2875 list_add(&fpid->list, &ftrace_pids);
2878 set_ftrace_pid_task(pid);
2880 ftrace_update_pid_func();
2881 ftrace_startup_enable(0);
2883 mutex_unlock(&ftrace_lock);
2887 if (pid != ftrace_swapper_pid)
2891 mutex_unlock(&ftrace_lock);
2895 static void ftrace_pid_reset(void)
2897 struct ftrace_pid *fpid, *safe;
2899 mutex_lock(&ftrace_lock);
2900 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2901 struct pid *pid = fpid->pid;
2903 clear_ftrace_pid_task(pid);
2905 list_del(&fpid->list);
2909 ftrace_update_pid_func();
2910 ftrace_startup_enable(0);
2912 mutex_unlock(&ftrace_lock);
2915 static void *fpid_start(struct seq_file *m, loff_t *pos)
2917 mutex_lock(&ftrace_lock);
2919 if (list_empty(&ftrace_pids) && (!*pos))
2922 return seq_list_start(&ftrace_pids, *pos);
2925 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2930 return seq_list_next(v, &ftrace_pids, pos);
2933 static void fpid_stop(struct seq_file *m, void *p)
2935 mutex_unlock(&ftrace_lock);
2938 static int fpid_show(struct seq_file *m, void *v)
2940 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2942 if (v == (void *)1) {
2943 seq_printf(m, "no pid\n");
2947 if (fpid->pid == ftrace_swapper_pid)
2948 seq_printf(m, "swapper tasks\n");
2950 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2955 static const struct seq_operations ftrace_pid_sops = {
2956 .start = fpid_start,
2963 ftrace_pid_open(struct inode *inode, struct file *file)
2967 if ((file->f_mode & FMODE_WRITE) &&
2968 (file->f_flags & O_TRUNC))
2971 if (file->f_mode & FMODE_READ)
2972 ret = seq_open(file, &ftrace_pid_sops);
2978 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2979 size_t cnt, loff_t *ppos)
2985 if (cnt >= sizeof(buf))
2988 if (copy_from_user(&buf, ubuf, cnt))
2994 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
2995 * to clean the filter quietly.
2997 tmp = strstrip(buf);
2998 if (strlen(tmp) == 0)
3001 ret = strict_strtol(tmp, 10, &val);
3005 ret = ftrace_pid_add(val);
3007 return ret ? ret : cnt;
3011 ftrace_pid_release(struct inode *inode, struct file *file)
3013 if (file->f_mode & FMODE_READ)
3014 seq_release(inode, file);
3019 static const struct file_operations ftrace_pid_fops = {
3020 .open = ftrace_pid_open,
3021 .write = ftrace_pid_write,
3023 .llseek = seq_lseek,
3024 .release = ftrace_pid_release,
3027 static __init int ftrace_init_debugfs(void)
3029 struct dentry *d_tracer;
3031 d_tracer = tracing_init_dentry();
3035 ftrace_init_dyn_debugfs(d_tracer);
3037 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3038 NULL, &ftrace_pid_fops);
3040 ftrace_profile_debugfs(d_tracer);
3044 fs_initcall(ftrace_init_debugfs);
3047 * ftrace_kill - kill ftrace
3049 * This function should be used by panic code. It stops ftrace
3050 * but in a not so nice way. If you need to simply kill ftrace
3051 * from a non-atomic section, use ftrace_kill.
3053 void ftrace_kill(void)
3055 ftrace_disabled = 1;
3057 clear_ftrace_function();
3061 * register_ftrace_function - register a function for profiling
3062 * @ops - ops structure that holds the function for profiling.
3064 * Register a function to be called by all functions in the
3067 * Note: @ops->func and all the functions it calls must be labeled
3068 * with "notrace", otherwise it will go into a
3071 int register_ftrace_function(struct ftrace_ops *ops)
3075 if (unlikely(ftrace_disabled))
3078 mutex_lock(&ftrace_lock);
3080 ret = __register_ftrace_function(ops);
3083 mutex_unlock(&ftrace_lock);
3088 * unregister_ftrace_function - unregister a function for profiling.
3089 * @ops - ops structure that holds the function to unregister
3091 * Unregister a function that was added to be called by ftrace profiling.
3093 int unregister_ftrace_function(struct ftrace_ops *ops)
3097 mutex_lock(&ftrace_lock);
3098 ret = __unregister_ftrace_function(ops);
3100 mutex_unlock(&ftrace_lock);
3106 ftrace_enable_sysctl(struct ctl_table *table, int write,
3107 void __user *buffer, size_t *lenp,
3112 if (unlikely(ftrace_disabled))
3115 mutex_lock(&ftrace_lock);
3117 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3119 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3122 last_ftrace_enabled = !!ftrace_enabled;
3124 if (ftrace_enabled) {
3126 ftrace_startup_sysctl();
3128 /* we are starting ftrace again */
3129 if (ftrace_list != &ftrace_list_end) {
3130 if (ftrace_list->next == &ftrace_list_end)
3131 ftrace_trace_function = ftrace_list->func;
3133 ftrace_trace_function = ftrace_list_func;
3137 /* stopping ftrace calls (just send to ftrace_stub) */
3138 ftrace_trace_function = ftrace_stub;
3140 ftrace_shutdown_sysctl();
3144 mutex_unlock(&ftrace_lock);
3148 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3150 static int ftrace_graph_active;
3151 static struct notifier_block ftrace_suspend_notifier;
3153 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3158 /* The callbacks that hook a function */
3159 trace_func_graph_ret_t ftrace_graph_return =
3160 (trace_func_graph_ret_t)ftrace_stub;
3161 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3163 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3164 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3168 unsigned long flags;
3169 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3170 struct task_struct *g, *t;
3172 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3173 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3174 * sizeof(struct ftrace_ret_stack),
3176 if (!ret_stack_list[i]) {
3184 read_lock_irqsave(&tasklist_lock, flags);
3185 do_each_thread(g, t) {
3191 if (t->ret_stack == NULL) {
3192 atomic_set(&t->tracing_graph_pause, 0);
3193 atomic_set(&t->trace_overrun, 0);
3194 t->curr_ret_stack = -1;
3195 /* Make sure the tasks see the -1 first: */
3197 t->ret_stack = ret_stack_list[start++];
3199 } while_each_thread(g, t);
3202 read_unlock_irqrestore(&tasklist_lock, flags);
3204 for (i = start; i < end; i++)
3205 kfree(ret_stack_list[i]);
3210 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3211 struct task_struct *next)
3213 unsigned long long timestamp;
3217 * Does the user want to count the time a function was asleep.
3218 * If so, do not update the time stamps.
3220 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3223 timestamp = trace_clock_local();
3225 prev->ftrace_timestamp = timestamp;
3227 /* only process tasks that we timestamped */
3228 if (!next->ftrace_timestamp)
3232 * Update all the counters in next to make up for the
3233 * time next was sleeping.
3235 timestamp -= next->ftrace_timestamp;
3237 for (index = next->curr_ret_stack; index >= 0; index--)
3238 next->ret_stack[index].calltime += timestamp;
3241 /* Allocate a return stack for each task */
3242 static int start_graph_tracing(void)
3244 struct ftrace_ret_stack **ret_stack_list;
3247 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3248 sizeof(struct ftrace_ret_stack *),
3251 if (!ret_stack_list)
3254 /* The cpu_boot init_task->ret_stack will never be freed */
3255 for_each_online_cpu(cpu) {
3256 if (!idle_task(cpu)->ret_stack)
3257 ftrace_graph_init_task(idle_task(cpu));
3261 ret = alloc_retstack_tasklist(ret_stack_list);
3262 } while (ret == -EAGAIN);
3265 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3267 pr_info("ftrace_graph: Couldn't activate tracepoint"
3268 " probe to kernel_sched_switch\n");
3271 kfree(ret_stack_list);
3276 * Hibernation protection.
3277 * The state of the current task is too much unstable during
3278 * suspend/restore to disk. We want to protect against that.
3281 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3285 case PM_HIBERNATION_PREPARE:
3286 pause_graph_tracing();
3289 case PM_POST_HIBERNATION:
3290 unpause_graph_tracing();
3296 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3297 trace_func_graph_ent_t entryfunc)
3301 mutex_lock(&ftrace_lock);
3303 /* we currently allow only one tracer registered at a time */
3304 if (ftrace_graph_active) {
3309 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3310 register_pm_notifier(&ftrace_suspend_notifier);
3312 ftrace_graph_active++;
3313 ret = start_graph_tracing();
3315 ftrace_graph_active--;
3319 ftrace_graph_return = retfunc;
3320 ftrace_graph_entry = entryfunc;
3322 ftrace_startup(FTRACE_START_FUNC_RET);
3325 mutex_unlock(&ftrace_lock);
3329 void unregister_ftrace_graph(void)
3331 mutex_lock(&ftrace_lock);
3333 if (unlikely(!ftrace_graph_active))
3336 ftrace_graph_active--;
3337 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3338 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3339 ftrace_graph_entry = ftrace_graph_entry_stub;
3340 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3341 unregister_pm_notifier(&ftrace_suspend_notifier);
3344 mutex_unlock(&ftrace_lock);
3347 /* Allocate a return stack for newly created task */
3348 void ftrace_graph_init_task(struct task_struct *t)
3350 /* Make sure we do not use the parent ret_stack */
3351 t->ret_stack = NULL;
3352 t->curr_ret_stack = -1;
3354 if (ftrace_graph_active) {
3355 struct ftrace_ret_stack *ret_stack;
3357 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3358 * sizeof(struct ftrace_ret_stack),
3362 atomic_set(&t->tracing_graph_pause, 0);
3363 atomic_set(&t->trace_overrun, 0);
3364 t->ftrace_timestamp = 0;
3365 /* make curr_ret_stack visable before we add the ret_stack */
3367 t->ret_stack = ret_stack;
3371 void ftrace_graph_exit_task(struct task_struct *t)
3373 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3375 t->ret_stack = NULL;
3376 /* NULL must become visible to IRQs before we free it: */
3382 void ftrace_graph_stop(void)