2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/kprobes.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
32 #include <trace/events/sched.h>
34 #include <asm/ftrace.h>
35 #include <asm/setup.h>
37 #include "trace_output.h"
38 #include "trace_stat.h"
40 #define FTRACE_WARN_ON(cond) \
46 #define FTRACE_WARN_ON_ONCE(cond) \
48 if (WARN_ON_ONCE(cond)) \
52 /* hash bits for specific function selection */
53 #define FTRACE_HASH_BITS 7
54 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56 /* ftrace_enabled is a method to turn ftrace on or off */
57 int ftrace_enabled __read_mostly;
58 static int last_ftrace_enabled;
60 /* Quick disabling of function tracer. */
61 int function_trace_stop;
64 * ftrace_disabled is set when an anomaly is discovered.
65 * ftrace_disabled is much stronger than ftrace_enabled.
67 static int ftrace_disabled __read_mostly;
69 static DEFINE_MUTEX(ftrace_lock);
71 static struct ftrace_ops ftrace_list_end __read_mostly =
76 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
77 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
78 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
79 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
81 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
83 struct ftrace_ops *op = ftrace_list;
85 /* in case someone actually ports this to alpha! */
86 read_barrier_depends();
88 while (op != &ftrace_list_end) {
90 read_barrier_depends();
91 op->func(ip, parent_ip);
96 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
98 if (!test_tsk_trace_trace(current))
101 ftrace_pid_function(ip, parent_ip);
104 static void set_ftrace_pid_function(ftrace_func_t func)
106 /* do not set ftrace_pid_function to itself! */
107 if (func != ftrace_pid_func)
108 ftrace_pid_function = func;
112 * clear_ftrace_function - reset the ftrace function
114 * This NULLs the ftrace function and in essence stops
115 * tracing. There may be lag
117 void clear_ftrace_function(void)
119 ftrace_trace_function = ftrace_stub;
120 __ftrace_trace_function = ftrace_stub;
121 ftrace_pid_function = ftrace_stub;
124 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
126 * For those archs that do not test ftrace_trace_stop in their
127 * mcount call site, we need to do it from C.
129 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
131 if (function_trace_stop)
134 __ftrace_trace_function(ip, parent_ip);
138 static int __register_ftrace_function(struct ftrace_ops *ops)
140 ops->next = ftrace_list;
142 * We are entering ops into the ftrace_list but another
143 * CPU might be walking that list. We need to make sure
144 * the ops->next pointer is valid before another CPU sees
145 * the ops pointer included into the ftrace_list.
150 if (ftrace_enabled) {
153 if (ops->next == &ftrace_list_end)
156 func = ftrace_list_func;
158 if (ftrace_pid_trace) {
159 set_ftrace_pid_function(func);
160 func = ftrace_pid_func;
164 * For one func, simply call it directly.
165 * For more than one func, call the chain.
167 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
168 ftrace_trace_function = func;
170 __ftrace_trace_function = func;
171 ftrace_trace_function = ftrace_test_stop_func;
178 static int __unregister_ftrace_function(struct ftrace_ops *ops)
180 struct ftrace_ops **p;
183 * If we are removing the last function, then simply point
184 * to the ftrace_stub.
186 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
187 ftrace_trace_function = ftrace_stub;
188 ftrace_list = &ftrace_list_end;
192 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
201 if (ftrace_enabled) {
202 /* If we only have one func left, then call that directly */
203 if (ftrace_list->next == &ftrace_list_end) {
204 ftrace_func_t func = ftrace_list->func;
206 if (ftrace_pid_trace) {
207 set_ftrace_pid_function(func);
208 func = ftrace_pid_func;
210 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
211 ftrace_trace_function = func;
213 __ftrace_trace_function = func;
221 static void ftrace_update_pid_func(void)
225 if (ftrace_trace_function == ftrace_stub)
228 func = ftrace_trace_function;
230 if (ftrace_pid_trace) {
231 set_ftrace_pid_function(func);
232 func = ftrace_pid_func;
234 if (func == ftrace_pid_func)
235 func = ftrace_pid_function;
238 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
239 ftrace_trace_function = func;
241 __ftrace_trace_function = func;
245 #ifdef CONFIG_FUNCTION_PROFILER
246 struct ftrace_profile {
247 struct hlist_node node;
249 unsigned long counter;
250 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
251 unsigned long long time;
255 struct ftrace_profile_page {
256 struct ftrace_profile_page *next;
258 struct ftrace_profile records[];
261 struct ftrace_profile_stat {
263 struct hlist_head *hash;
264 struct ftrace_profile_page *pages;
265 struct ftrace_profile_page *start;
266 struct tracer_stat stat;
269 #define PROFILE_RECORDS_SIZE \
270 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
272 #define PROFILES_PER_PAGE \
273 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
275 static int ftrace_profile_bits __read_mostly;
276 static int ftrace_profile_enabled __read_mostly;
278 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
279 static DEFINE_MUTEX(ftrace_profile_lock);
281 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
283 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
286 function_stat_next(void *v, int idx)
288 struct ftrace_profile *rec = v;
289 struct ftrace_profile_page *pg;
291 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
295 if ((void *)rec >= (void *)&pg->records[pg->index]) {
299 rec = &pg->records[0];
307 static void *function_stat_start(struct tracer_stat *trace)
309 struct ftrace_profile_stat *stat =
310 container_of(trace, struct ftrace_profile_stat, stat);
312 if (!stat || !stat->start)
315 return function_stat_next(&stat->start->records[0], 0);
318 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
319 /* function graph compares on total time */
320 static int function_stat_cmp(void *p1, void *p2)
322 struct ftrace_profile *a = p1;
323 struct ftrace_profile *b = p2;
325 if (a->time < b->time)
327 if (a->time > b->time)
333 /* not function graph compares against hits */
334 static int function_stat_cmp(void *p1, void *p2)
336 struct ftrace_profile *a = p1;
337 struct ftrace_profile *b = p2;
339 if (a->counter < b->counter)
341 if (a->counter > b->counter)
348 static int function_stat_headers(struct seq_file *m)
350 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
351 seq_printf(m, " Function "
356 seq_printf(m, " Function Hit\n"
362 static int function_stat_show(struct seq_file *m, void *v)
364 struct ftrace_profile *rec = v;
365 char str[KSYM_SYMBOL_LEN];
366 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
367 static DEFINE_MUTEX(mutex);
368 static struct trace_seq s;
369 unsigned long long avg;
372 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
373 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
375 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
378 do_div(avg, rec->counter);
382 trace_print_graph_duration(rec->time, &s);
383 trace_seq_puts(&s, " ");
384 trace_print_graph_duration(avg, &s);
385 trace_print_seq(m, &s);
386 mutex_unlock(&mutex);
393 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
395 struct ftrace_profile_page *pg;
397 pg = stat->pages = stat->start;
400 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
405 memset(stat->hash, 0,
406 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
409 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
411 struct ftrace_profile_page *pg;
416 /* If we already allocated, do nothing */
420 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
424 #ifdef CONFIG_DYNAMIC_FTRACE
425 functions = ftrace_update_tot_cnt;
428 * We do not know the number of functions that exist because
429 * dynamic tracing is what counts them. With past experience
430 * we have around 20K functions. That should be more than enough.
431 * It is highly unlikely we will execute every function in
437 pg = stat->start = stat->pages;
439 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
441 for (i = 0; i < pages; i++) {
442 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
453 unsigned long tmp = (unsigned long)pg;
459 free_page((unsigned long)stat->pages);
466 static int ftrace_profile_init_cpu(int cpu)
468 struct ftrace_profile_stat *stat;
471 stat = &per_cpu(ftrace_profile_stats, cpu);
474 /* If the profile is already created, simply reset it */
475 ftrace_profile_reset(stat);
480 * We are profiling all functions, but usually only a few thousand
481 * functions are hit. We'll make a hash of 1024 items.
483 size = FTRACE_PROFILE_HASH_SIZE;
485 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
490 if (!ftrace_profile_bits) {
493 for (; size; size >>= 1)
494 ftrace_profile_bits++;
497 /* Preallocate the function profiling pages */
498 if (ftrace_profile_pages_init(stat) < 0) {
507 static int ftrace_profile_init(void)
512 for_each_online_cpu(cpu) {
513 ret = ftrace_profile_init_cpu(cpu);
521 /* interrupts must be disabled */
522 static struct ftrace_profile *
523 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
525 struct ftrace_profile *rec;
526 struct hlist_head *hhd;
527 struct hlist_node *n;
530 key = hash_long(ip, ftrace_profile_bits);
531 hhd = &stat->hash[key];
533 if (hlist_empty(hhd))
536 hlist_for_each_entry_rcu(rec, n, hhd, node) {
544 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
545 struct ftrace_profile *rec)
549 key = hash_long(rec->ip, ftrace_profile_bits);
550 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
554 * The memory is already allocated, this simply finds a new record to use.
556 static struct ftrace_profile *
557 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
559 struct ftrace_profile *rec = NULL;
561 /* prevent recursion (from NMIs) */
562 if (atomic_inc_return(&stat->disabled) != 1)
566 * Try to find the function again since an NMI
567 * could have added it
569 rec = ftrace_find_profiled_func(stat, ip);
573 if (stat->pages->index == PROFILES_PER_PAGE) {
574 if (!stat->pages->next)
576 stat->pages = stat->pages->next;
579 rec = &stat->pages->records[stat->pages->index++];
581 ftrace_add_profile(stat, rec);
584 atomic_dec(&stat->disabled);
590 function_profile_call(unsigned long ip, unsigned long parent_ip)
592 struct ftrace_profile_stat *stat;
593 struct ftrace_profile *rec;
596 if (!ftrace_profile_enabled)
599 local_irq_save(flags);
601 stat = &__get_cpu_var(ftrace_profile_stats);
605 rec = ftrace_find_profiled_func(stat, ip);
607 rec = ftrace_profile_alloc(stat, ip);
614 local_irq_restore(flags);
617 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
618 static int profile_graph_entry(struct ftrace_graph_ent *trace)
620 function_profile_call(trace->func, 0);
624 static void profile_graph_return(struct ftrace_graph_ret *trace)
626 struct ftrace_profile_stat *stat;
627 unsigned long long calltime;
628 struct ftrace_profile *rec;
631 local_irq_save(flags);
632 stat = &__get_cpu_var(ftrace_profile_stats);
636 calltime = trace->rettime - trace->calltime;
638 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
641 index = trace->depth;
643 /* Append this call time to the parent time to subtract */
645 current->ret_stack[index - 1].subtime += calltime;
647 if (current->ret_stack[index].subtime < calltime)
648 calltime -= current->ret_stack[index].subtime;
653 rec = ftrace_find_profiled_func(stat, trace->func);
655 rec->time += calltime;
658 local_irq_restore(flags);
661 static int register_ftrace_profiler(void)
663 return register_ftrace_graph(&profile_graph_return,
664 &profile_graph_entry);
667 static void unregister_ftrace_profiler(void)
669 unregister_ftrace_graph();
672 static struct ftrace_ops ftrace_profile_ops __read_mostly =
674 .func = function_profile_call,
677 static int register_ftrace_profiler(void)
679 return register_ftrace_function(&ftrace_profile_ops);
682 static void unregister_ftrace_profiler(void)
684 unregister_ftrace_function(&ftrace_profile_ops);
686 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
689 ftrace_profile_write(struct file *filp, const char __user *ubuf,
690 size_t cnt, loff_t *ppos)
693 char buf[64]; /* big enough to hold a number */
696 if (cnt >= sizeof(buf))
699 if (copy_from_user(&buf, ubuf, cnt))
704 ret = strict_strtoul(buf, 10, &val);
710 mutex_lock(&ftrace_profile_lock);
711 if (ftrace_profile_enabled ^ val) {
713 ret = ftrace_profile_init();
719 ret = register_ftrace_profiler();
724 ftrace_profile_enabled = 1;
726 ftrace_profile_enabled = 0;
727 unregister_ftrace_profiler();
731 mutex_unlock(&ftrace_profile_lock);
739 ftrace_profile_read(struct file *filp, char __user *ubuf,
740 size_t cnt, loff_t *ppos)
742 char buf[64]; /* big enough to hold a number */
745 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
746 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
749 static const struct file_operations ftrace_profile_fops = {
750 .open = tracing_open_generic,
751 .read = ftrace_profile_read,
752 .write = ftrace_profile_write,
755 /* used to initialize the real stat files */
756 static struct tracer_stat function_stats __initdata = {
758 .stat_start = function_stat_start,
759 .stat_next = function_stat_next,
760 .stat_cmp = function_stat_cmp,
761 .stat_headers = function_stat_headers,
762 .stat_show = function_stat_show
765 static void ftrace_profile_debugfs(struct dentry *d_tracer)
767 struct ftrace_profile_stat *stat;
768 struct dentry *entry;
773 for_each_possible_cpu(cpu) {
774 stat = &per_cpu(ftrace_profile_stats, cpu);
776 /* allocate enough for function name + cpu number */
777 name = kmalloc(32, GFP_KERNEL);
780 * The files created are permanent, if something happens
781 * we still do not free memory.
785 "Could not allocate stat file for cpu %d\n",
789 stat->stat = function_stats;
790 snprintf(name, 32, "function%d", cpu);
791 stat->stat.name = name;
792 ret = register_stat_tracer(&stat->stat);
795 "Could not register function stat for cpu %d\n",
802 entry = debugfs_create_file("function_profile_enabled", 0644,
803 d_tracer, NULL, &ftrace_profile_fops);
805 pr_warning("Could not create debugfs "
806 "'function_profile_enabled' entry\n");
809 #else /* CONFIG_FUNCTION_PROFILER */
810 static void ftrace_profile_debugfs(struct dentry *d_tracer)
813 #endif /* CONFIG_FUNCTION_PROFILER */
815 /* set when tracing only a pid */
816 struct pid *ftrace_pid_trace;
817 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
819 #ifdef CONFIG_DYNAMIC_FTRACE
821 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
822 # error Dynamic ftrace depends on MCOUNT_RECORD
825 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
827 struct ftrace_func_probe {
828 struct hlist_node node;
829 struct ftrace_probe_ops *ops;
837 FTRACE_ENABLE_CALLS = (1 << 0),
838 FTRACE_DISABLE_CALLS = (1 << 1),
839 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
840 FTRACE_ENABLE_MCOUNT = (1 << 3),
841 FTRACE_DISABLE_MCOUNT = (1 << 4),
842 FTRACE_START_FUNC_RET = (1 << 5),
843 FTRACE_STOP_FUNC_RET = (1 << 6),
846 static int ftrace_filtered;
848 static struct dyn_ftrace *ftrace_new_addrs;
850 static DEFINE_MUTEX(ftrace_regex_lock);
853 struct ftrace_page *next;
855 struct dyn_ftrace records[];
858 #define ENTRIES_PER_PAGE \
859 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
861 /* estimate from running different kernels */
862 #define NR_TO_INIT 10000
864 static struct ftrace_page *ftrace_pages_start;
865 static struct ftrace_page *ftrace_pages;
867 static struct dyn_ftrace *ftrace_free_records;
870 * This is a double for. Do not use 'break' to break out of the loop,
871 * you must use a goto.
873 #define do_for_each_ftrace_rec(pg, rec) \
874 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
876 for (_____i = 0; _____i < pg->index; _____i++) { \
877 rec = &pg->records[_____i];
879 #define while_for_each_ftrace_rec() \
883 #ifdef CONFIG_KPROBES
885 static int frozen_record_count;
887 static inline void freeze_record(struct dyn_ftrace *rec)
889 if (!(rec->flags & FTRACE_FL_FROZEN)) {
890 rec->flags |= FTRACE_FL_FROZEN;
891 frozen_record_count++;
895 static inline void unfreeze_record(struct dyn_ftrace *rec)
897 if (rec->flags & FTRACE_FL_FROZEN) {
898 rec->flags &= ~FTRACE_FL_FROZEN;
899 frozen_record_count--;
903 static inline int record_frozen(struct dyn_ftrace *rec)
905 return rec->flags & FTRACE_FL_FROZEN;
908 # define freeze_record(rec) ({ 0; })
909 # define unfreeze_record(rec) ({ 0; })
910 # define record_frozen(rec) ({ 0; })
911 #endif /* CONFIG_KPROBES */
913 static void ftrace_free_rec(struct dyn_ftrace *rec)
915 rec->freelist = ftrace_free_records;
916 ftrace_free_records = rec;
917 rec->flags |= FTRACE_FL_FREE;
920 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
922 struct dyn_ftrace *rec;
924 /* First check for freed records */
925 if (ftrace_free_records) {
926 rec = ftrace_free_records;
928 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
929 FTRACE_WARN_ON_ONCE(1);
930 ftrace_free_records = NULL;
934 ftrace_free_records = rec->freelist;
935 memset(rec, 0, sizeof(*rec));
939 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
940 if (!ftrace_pages->next) {
941 /* allocate another page */
943 (void *)get_zeroed_page(GFP_KERNEL);
944 if (!ftrace_pages->next)
947 ftrace_pages = ftrace_pages->next;
950 return &ftrace_pages->records[ftrace_pages->index++];
953 static struct dyn_ftrace *
954 ftrace_record_ip(unsigned long ip)
956 struct dyn_ftrace *rec;
961 rec = ftrace_alloc_dyn_node(ip);
966 rec->newlist = ftrace_new_addrs;
967 ftrace_new_addrs = rec;
972 static void print_ip_ins(const char *fmt, unsigned char *p)
976 printk(KERN_CONT "%s", fmt);
978 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
979 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
982 static void ftrace_bug(int failed, unsigned long ip)
986 FTRACE_WARN_ON_ONCE(1);
987 pr_info("ftrace faulted on modifying ");
991 FTRACE_WARN_ON_ONCE(1);
992 pr_info("ftrace failed to modify ");
994 print_ip_ins(" actual: ", (unsigned char *)ip);
995 printk(KERN_CONT "\n");
998 FTRACE_WARN_ON_ONCE(1);
999 pr_info("ftrace faulted on writing ");
1003 FTRACE_WARN_ON_ONCE(1);
1004 pr_info("ftrace faulted on unknown error ");
1011 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1013 unsigned long ftrace_addr;
1014 unsigned long ip, fl;
1016 ftrace_addr = (unsigned long)FTRACE_ADDR;
1021 * If this record is not to be traced and
1022 * it is not enabled then do nothing.
1024 * If this record is not to be traced and
1025 * it is enabled then disable it.
1028 if (rec->flags & FTRACE_FL_NOTRACE) {
1029 if (rec->flags & FTRACE_FL_ENABLED)
1030 rec->flags &= ~FTRACE_FL_ENABLED;
1034 } else if (ftrace_filtered && enable) {
1039 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
1041 /* Record is filtered and enabled, do nothing */
1042 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
1045 /* Record is not filtered or enabled, do nothing */
1049 /* Record is not filtered but enabled, disable it */
1050 if (fl == FTRACE_FL_ENABLED)
1051 rec->flags &= ~FTRACE_FL_ENABLED;
1053 /* Otherwise record is filtered but not enabled, enable it */
1054 rec->flags |= FTRACE_FL_ENABLED;
1056 /* Disable or not filtered */
1059 /* if record is enabled, do nothing */
1060 if (rec->flags & FTRACE_FL_ENABLED)
1063 rec->flags |= FTRACE_FL_ENABLED;
1067 /* if record is not enabled, do nothing */
1068 if (!(rec->flags & FTRACE_FL_ENABLED))
1071 rec->flags &= ~FTRACE_FL_ENABLED;
1075 if (rec->flags & FTRACE_FL_ENABLED)
1076 return ftrace_make_call(rec, ftrace_addr);
1078 return ftrace_make_nop(NULL, rec, ftrace_addr);
1081 static void ftrace_replace_code(int enable)
1083 struct dyn_ftrace *rec;
1084 struct ftrace_page *pg;
1087 do_for_each_ftrace_rec(pg, rec) {
1089 * Skip over free records, records that have
1090 * failed and not converted.
1092 if (rec->flags & FTRACE_FL_FREE ||
1093 rec->flags & FTRACE_FL_FAILED ||
1094 !(rec->flags & FTRACE_FL_CONVERTED))
1097 /* ignore updates to this record's mcount site */
1098 if (get_kprobe((void *)rec->ip)) {
1102 unfreeze_record(rec);
1105 failed = __ftrace_replace_code(rec, enable);
1107 rec->flags |= FTRACE_FL_FAILED;
1108 if ((system_state == SYSTEM_BOOTING) ||
1109 !core_kernel_text(rec->ip)) {
1110 ftrace_free_rec(rec);
1112 ftrace_bug(failed, rec->ip);
1113 /* Stop processing */
1117 } while_for_each_ftrace_rec();
1121 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1128 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1130 ftrace_bug(ret, ip);
1131 rec->flags |= FTRACE_FL_FAILED;
1138 * archs can override this function if they must do something
1139 * before the modifying code is performed.
1141 int __weak ftrace_arch_code_modify_prepare(void)
1147 * archs can override this function if they must do something
1148 * after the modifying code is performed.
1150 int __weak ftrace_arch_code_modify_post_process(void)
1155 static int __ftrace_modify_code(void *data)
1157 int *command = data;
1159 if (*command & FTRACE_ENABLE_CALLS)
1160 ftrace_replace_code(1);
1161 else if (*command & FTRACE_DISABLE_CALLS)
1162 ftrace_replace_code(0);
1164 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1165 ftrace_update_ftrace_func(ftrace_trace_function);
1167 if (*command & FTRACE_START_FUNC_RET)
1168 ftrace_enable_ftrace_graph_caller();
1169 else if (*command & FTRACE_STOP_FUNC_RET)
1170 ftrace_disable_ftrace_graph_caller();
1175 static void ftrace_run_update_code(int command)
1179 ret = ftrace_arch_code_modify_prepare();
1180 FTRACE_WARN_ON(ret);
1184 stop_machine(__ftrace_modify_code, &command, NULL);
1186 ret = ftrace_arch_code_modify_post_process();
1187 FTRACE_WARN_ON(ret);
1190 static ftrace_func_t saved_ftrace_func;
1191 static int ftrace_start_up;
1193 static void ftrace_startup_enable(int command)
1195 if (saved_ftrace_func != ftrace_trace_function) {
1196 saved_ftrace_func = ftrace_trace_function;
1197 command |= FTRACE_UPDATE_TRACE_FUNC;
1200 if (!command || !ftrace_enabled)
1203 ftrace_run_update_code(command);
1206 static void ftrace_startup(int command)
1208 if (unlikely(ftrace_disabled))
1212 command |= FTRACE_ENABLE_CALLS;
1214 ftrace_startup_enable(command);
1217 static void ftrace_shutdown(int command)
1219 if (unlikely(ftrace_disabled))
1223 if (!ftrace_start_up)
1224 command |= FTRACE_DISABLE_CALLS;
1226 if (saved_ftrace_func != ftrace_trace_function) {
1227 saved_ftrace_func = ftrace_trace_function;
1228 command |= FTRACE_UPDATE_TRACE_FUNC;
1231 if (!command || !ftrace_enabled)
1234 ftrace_run_update_code(command);
1237 static void ftrace_startup_sysctl(void)
1239 int command = FTRACE_ENABLE_MCOUNT;
1241 if (unlikely(ftrace_disabled))
1244 /* Force update next time */
1245 saved_ftrace_func = NULL;
1246 /* ftrace_start_up is true if we want ftrace running */
1247 if (ftrace_start_up)
1248 command |= FTRACE_ENABLE_CALLS;
1250 ftrace_run_update_code(command);
1253 static void ftrace_shutdown_sysctl(void)
1255 int command = FTRACE_DISABLE_MCOUNT;
1257 if (unlikely(ftrace_disabled))
1260 /* ftrace_start_up is true if ftrace is running */
1261 if (ftrace_start_up)
1262 command |= FTRACE_DISABLE_CALLS;
1264 ftrace_run_update_code(command);
1267 static cycle_t ftrace_update_time;
1268 static unsigned long ftrace_update_cnt;
1269 unsigned long ftrace_update_tot_cnt;
1271 static int ftrace_update_code(struct module *mod)
1273 struct dyn_ftrace *p;
1274 cycle_t start, stop;
1276 start = ftrace_now(raw_smp_processor_id());
1277 ftrace_update_cnt = 0;
1279 while (ftrace_new_addrs) {
1281 /* If something went wrong, bail without enabling anything */
1282 if (unlikely(ftrace_disabled))
1285 p = ftrace_new_addrs;
1286 ftrace_new_addrs = p->newlist;
1289 /* convert record (i.e, patch mcount-call with NOP) */
1290 if (ftrace_code_disable(mod, p)) {
1291 p->flags |= FTRACE_FL_CONVERTED;
1292 ftrace_update_cnt++;
1297 stop = ftrace_now(raw_smp_processor_id());
1298 ftrace_update_time = stop - start;
1299 ftrace_update_tot_cnt += ftrace_update_cnt;
1304 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1306 struct ftrace_page *pg;
1310 /* allocate a few pages */
1311 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1312 if (!ftrace_pages_start)
1316 * Allocate a few more pages.
1318 * TODO: have some parser search vmlinux before
1319 * final linking to find all calls to ftrace.
1321 * a) know how many pages to allocate.
1323 * b) set up the table then.
1325 * The dynamic code is still necessary for
1329 pg = ftrace_pages = ftrace_pages_start;
1331 cnt = num_to_init / ENTRIES_PER_PAGE;
1332 pr_info("ftrace: allocating %ld entries in %d pages\n",
1333 num_to_init, cnt + 1);
1335 for (i = 0; i < cnt; i++) {
1336 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1338 /* If we fail, we'll try later anyway */
1349 FTRACE_ITER_FILTER = (1 << 0),
1350 FTRACE_ITER_CONT = (1 << 1),
1351 FTRACE_ITER_NOTRACE = (1 << 2),
1352 FTRACE_ITER_FAILURES = (1 << 3),
1353 FTRACE_ITER_PRINTALL = (1 << 4),
1354 FTRACE_ITER_HASH = (1 << 5),
1357 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1359 struct ftrace_iterator {
1360 struct ftrace_page *pg;
1364 unsigned char buffer[FTRACE_BUFF_MAX+1];
1365 unsigned buffer_idx;
1370 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1372 struct ftrace_iterator *iter = m->private;
1373 struct hlist_node *hnd = v;
1374 struct hlist_head *hhd;
1376 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1381 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1384 hhd = &ftrace_func_hash[iter->hidx];
1386 if (hlist_empty(hhd)) {
1405 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1407 struct ftrace_iterator *iter = m->private;
1410 iter->flags |= FTRACE_ITER_HASH;
1412 return t_hash_next(m, p, pos);
1415 static int t_hash_show(struct seq_file *m, void *v)
1417 struct ftrace_func_probe *rec;
1418 struct hlist_node *hnd = v;
1419 char str[KSYM_SYMBOL_LEN];
1421 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1423 if (rec->ops->print)
1424 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1426 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1427 seq_printf(m, "%s:", str);
1429 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1430 seq_printf(m, "%s", str);
1433 seq_printf(m, ":%p", rec->data);
1440 t_next(struct seq_file *m, void *v, loff_t *pos)
1442 struct ftrace_iterator *iter = m->private;
1443 struct dyn_ftrace *rec = NULL;
1445 if (iter->flags & FTRACE_ITER_HASH)
1446 return t_hash_next(m, v, pos);
1450 if (iter->flags & FTRACE_ITER_PRINTALL)
1454 if (iter->idx >= iter->pg->index) {
1455 if (iter->pg->next) {
1456 iter->pg = iter->pg->next;
1463 rec = &iter->pg->records[iter->idx++];
1464 if ((rec->flags & FTRACE_FL_FREE) ||
1466 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1467 (rec->flags & FTRACE_FL_FAILED)) ||
1469 ((iter->flags & FTRACE_ITER_FAILURES) &&
1470 !(rec->flags & FTRACE_FL_FAILED)) ||
1472 ((iter->flags & FTRACE_ITER_FILTER) &&
1473 !(rec->flags & FTRACE_FL_FILTER)) ||
1475 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1476 !(rec->flags & FTRACE_FL_NOTRACE))) {
1485 static void *t_start(struct seq_file *m, loff_t *pos)
1487 struct ftrace_iterator *iter = m->private;
1490 mutex_lock(&ftrace_lock);
1492 * For set_ftrace_filter reading, if we have the filter
1493 * off, we can short cut and just print out that all
1494 * functions are enabled.
1496 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1498 return t_hash_start(m, pos);
1499 iter->flags |= FTRACE_ITER_PRINTALL;
1504 if (iter->flags & FTRACE_ITER_HASH)
1505 return t_hash_start(m, pos);
1514 p = t_next(m, p, pos);
1517 return t_hash_start(m, pos);
1522 static void t_stop(struct seq_file *m, void *p)
1524 mutex_unlock(&ftrace_lock);
1527 static int t_show(struct seq_file *m, void *v)
1529 struct ftrace_iterator *iter = m->private;
1530 struct dyn_ftrace *rec = v;
1531 char str[KSYM_SYMBOL_LEN];
1533 if (iter->flags & FTRACE_ITER_HASH)
1534 return t_hash_show(m, v);
1536 if (iter->flags & FTRACE_ITER_PRINTALL) {
1537 seq_printf(m, "#### all functions enabled ####\n");
1544 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1546 seq_printf(m, "%s\n", str);
1551 static struct seq_operations show_ftrace_seq_ops = {
1559 ftrace_avail_open(struct inode *inode, struct file *file)
1561 struct ftrace_iterator *iter;
1564 if (unlikely(ftrace_disabled))
1567 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1571 iter->pg = ftrace_pages_start;
1573 ret = seq_open(file, &show_ftrace_seq_ops);
1575 struct seq_file *m = file->private_data;
1585 int ftrace_avail_release(struct inode *inode, struct file *file)
1587 struct seq_file *m = (struct seq_file *)file->private_data;
1588 struct ftrace_iterator *iter = m->private;
1590 seq_release(inode, file);
1597 ftrace_failures_open(struct inode *inode, struct file *file)
1601 struct ftrace_iterator *iter;
1603 ret = ftrace_avail_open(inode, file);
1605 m = (struct seq_file *)file->private_data;
1606 iter = (struct ftrace_iterator *)m->private;
1607 iter->flags = FTRACE_ITER_FAILURES;
1614 static void ftrace_filter_reset(int enable)
1616 struct ftrace_page *pg;
1617 struct dyn_ftrace *rec;
1618 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1620 mutex_lock(&ftrace_lock);
1622 ftrace_filtered = 0;
1623 do_for_each_ftrace_rec(pg, rec) {
1624 if (rec->flags & FTRACE_FL_FAILED)
1626 rec->flags &= ~type;
1627 } while_for_each_ftrace_rec();
1628 mutex_unlock(&ftrace_lock);
1632 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1634 struct ftrace_iterator *iter;
1637 if (unlikely(ftrace_disabled))
1640 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1644 mutex_lock(&ftrace_regex_lock);
1645 if ((file->f_mode & FMODE_WRITE) &&
1646 !(file->f_flags & O_APPEND))
1647 ftrace_filter_reset(enable);
1649 if (file->f_mode & FMODE_READ) {
1650 iter->pg = ftrace_pages_start;
1651 iter->flags = enable ? FTRACE_ITER_FILTER :
1652 FTRACE_ITER_NOTRACE;
1654 ret = seq_open(file, &show_ftrace_seq_ops);
1656 struct seq_file *m = file->private_data;
1661 file->private_data = iter;
1662 mutex_unlock(&ftrace_regex_lock);
1668 ftrace_filter_open(struct inode *inode, struct file *file)
1670 return ftrace_regex_open(inode, file, 1);
1674 ftrace_notrace_open(struct inode *inode, struct file *file)
1676 return ftrace_regex_open(inode, file, 0);
1680 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1684 if (file->f_mode & FMODE_READ)
1685 ret = seq_lseek(file, offset, origin);
1687 file->f_pos = ret = 1;
1700 * (static function - no need for kernel doc)
1702 * Pass in a buffer containing a glob and this function will
1703 * set search to point to the search part of the buffer and
1704 * return the type of search it is (see enum above).
1705 * This does modify buff.
1707 * Returns enum type.
1708 * search returns the pointer to use for comparison.
1709 * not returns 1 if buff started with a '!'
1713 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1715 int type = MATCH_FULL;
1718 if (buff[0] == '!') {
1727 for (i = 0; i < len; i++) {
1728 if (buff[i] == '*') {
1731 type = MATCH_END_ONLY;
1733 if (type == MATCH_END_ONLY)
1734 type = MATCH_MIDDLE_ONLY;
1736 type = MATCH_FRONT_ONLY;
1746 static int ftrace_match(char *str, char *regex, int len, int type)
1753 if (strcmp(str, regex) == 0)
1756 case MATCH_FRONT_ONLY:
1757 if (strncmp(str, regex, len) == 0)
1760 case MATCH_MIDDLE_ONLY:
1761 if (strstr(str, regex))
1764 case MATCH_END_ONLY:
1765 ptr = strstr(str, regex);
1766 if (ptr && (ptr[len] == 0))
1775 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1777 char str[KSYM_SYMBOL_LEN];
1779 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1780 return ftrace_match(str, regex, len, type);
1783 static void ftrace_match_records(char *buff, int len, int enable)
1785 unsigned int search_len;
1786 struct ftrace_page *pg;
1787 struct dyn_ftrace *rec;
1793 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1794 type = ftrace_setup_glob(buff, len, &search, ¬);
1796 search_len = strlen(search);
1798 mutex_lock(&ftrace_lock);
1799 do_for_each_ftrace_rec(pg, rec) {
1801 if (rec->flags & FTRACE_FL_FAILED)
1804 if (ftrace_match_record(rec, search, search_len, type)) {
1806 rec->flags &= ~flag;
1811 * Only enable filtering if we have a function that
1814 if (enable && (rec->flags & FTRACE_FL_FILTER))
1815 ftrace_filtered = 1;
1816 } while_for_each_ftrace_rec();
1817 mutex_unlock(&ftrace_lock);
1821 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1822 char *regex, int len, int type)
1824 char str[KSYM_SYMBOL_LEN];
1827 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1829 if (!modname || strcmp(modname, mod))
1832 /* blank search means to match all funcs in the mod */
1834 return ftrace_match(str, regex, len, type);
1839 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1841 unsigned search_len = 0;
1842 struct ftrace_page *pg;
1843 struct dyn_ftrace *rec;
1844 int type = MATCH_FULL;
1845 char *search = buff;
1849 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1851 /* blank or '*' mean the same */
1852 if (strcmp(buff, "*") == 0)
1855 /* handle the case of 'dont filter this module' */
1856 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1862 type = ftrace_setup_glob(buff, strlen(buff), &search, ¬);
1863 search_len = strlen(search);
1866 mutex_lock(&ftrace_lock);
1867 do_for_each_ftrace_rec(pg, rec) {
1869 if (rec->flags & FTRACE_FL_FAILED)
1872 if (ftrace_match_module_record(rec, mod,
1873 search, search_len, type)) {
1875 rec->flags &= ~flag;
1879 if (enable && (rec->flags & FTRACE_FL_FILTER))
1880 ftrace_filtered = 1;
1882 } while_for_each_ftrace_rec();
1883 mutex_unlock(&ftrace_lock);
1887 * We register the module command as a template to show others how
1888 * to register the a command as well.
1892 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1897 * cmd == 'mod' because we only registered this func
1898 * for the 'mod' ftrace_func_command.
1899 * But if you register one func with multiple commands,
1900 * you can tell which command was used by the cmd
1904 /* we must have a module name */
1908 mod = strsep(¶m, ":");
1912 ftrace_match_module_records(func, mod, enable);
1916 static struct ftrace_func_command ftrace_mod_cmd = {
1918 .func = ftrace_mod_callback,
1921 static int __init ftrace_mod_cmd_init(void)
1923 return register_ftrace_command(&ftrace_mod_cmd);
1925 device_initcall(ftrace_mod_cmd_init);
1928 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1930 struct ftrace_func_probe *entry;
1931 struct hlist_head *hhd;
1932 struct hlist_node *n;
1936 key = hash_long(ip, FTRACE_HASH_BITS);
1938 hhd = &ftrace_func_hash[key];
1940 if (hlist_empty(hhd))
1944 * Disable preemption for these calls to prevent a RCU grace
1945 * period. This syncs the hash iteration and freeing of items
1946 * on the hash. rcu_read_lock is too dangerous here.
1948 resched = ftrace_preempt_disable();
1949 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1950 if (entry->ip == ip)
1951 entry->ops->func(ip, parent_ip, &entry->data);
1953 ftrace_preempt_enable(resched);
1956 static struct ftrace_ops trace_probe_ops __read_mostly =
1958 .func = function_trace_probe_call,
1961 static int ftrace_probe_registered;
1963 static void __enable_ftrace_function_probe(void)
1967 if (ftrace_probe_registered)
1970 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1971 struct hlist_head *hhd = &ftrace_func_hash[i];
1975 /* Nothing registered? */
1976 if (i == FTRACE_FUNC_HASHSIZE)
1979 __register_ftrace_function(&trace_probe_ops);
1981 ftrace_probe_registered = 1;
1984 static void __disable_ftrace_function_probe(void)
1988 if (!ftrace_probe_registered)
1991 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1992 struct hlist_head *hhd = &ftrace_func_hash[i];
1997 /* no more funcs left */
1998 __unregister_ftrace_function(&trace_probe_ops);
2000 ftrace_probe_registered = 0;
2004 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2006 struct ftrace_func_probe *entry =
2007 container_of(rhp, struct ftrace_func_probe, rcu);
2009 if (entry->ops->free)
2010 entry->ops->free(&entry->data);
2016 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2019 struct ftrace_func_probe *entry;
2020 struct ftrace_page *pg;
2021 struct dyn_ftrace *rec;
2027 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2028 len = strlen(search);
2030 /* we do not support '!' for function probes */
2034 mutex_lock(&ftrace_lock);
2035 do_for_each_ftrace_rec(pg, rec) {
2037 if (rec->flags & FTRACE_FL_FAILED)
2040 if (!ftrace_match_record(rec, search, len, type))
2043 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2045 /* If we did not process any, then return error */
2056 * The caller might want to do something special
2057 * for each function we find. We call the callback
2058 * to give the caller an opportunity to do so.
2060 if (ops->callback) {
2061 if (ops->callback(rec->ip, &entry->data) < 0) {
2062 /* caller does not like this func */
2069 entry->ip = rec->ip;
2071 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2072 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2074 } while_for_each_ftrace_rec();
2075 __enable_ftrace_function_probe();
2078 mutex_unlock(&ftrace_lock);
2084 PROBE_TEST_FUNC = 1,
2089 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2090 void *data, int flags)
2092 struct ftrace_func_probe *entry;
2093 struct hlist_node *n, *tmp;
2094 char str[KSYM_SYMBOL_LEN];
2095 int type = MATCH_FULL;
2099 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2104 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2105 len = strlen(search);
2107 /* we do not support '!' for function probes */
2112 mutex_lock(&ftrace_lock);
2113 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2114 struct hlist_head *hhd = &ftrace_func_hash[i];
2116 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2118 /* break up if statements for readability */
2119 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2122 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2125 /* do this last, since it is the most expensive */
2127 kallsyms_lookup(entry->ip, NULL, NULL,
2129 if (!ftrace_match(str, glob, len, type))
2133 hlist_del(&entry->node);
2134 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2137 __disable_ftrace_function_probe();
2138 mutex_unlock(&ftrace_lock);
2142 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2145 __unregister_ftrace_function_probe(glob, ops, data,
2146 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2150 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2152 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2155 void unregister_ftrace_function_probe_all(char *glob)
2157 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2160 static LIST_HEAD(ftrace_commands);
2161 static DEFINE_MUTEX(ftrace_cmd_mutex);
2163 int register_ftrace_command(struct ftrace_func_command *cmd)
2165 struct ftrace_func_command *p;
2168 mutex_lock(&ftrace_cmd_mutex);
2169 list_for_each_entry(p, &ftrace_commands, list) {
2170 if (strcmp(cmd->name, p->name) == 0) {
2175 list_add(&cmd->list, &ftrace_commands);
2177 mutex_unlock(&ftrace_cmd_mutex);
2182 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2184 struct ftrace_func_command *p, *n;
2187 mutex_lock(&ftrace_cmd_mutex);
2188 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2189 if (strcmp(cmd->name, p->name) == 0) {
2191 list_del_init(&p->list);
2196 mutex_unlock(&ftrace_cmd_mutex);
2201 static int ftrace_process_regex(char *buff, int len, int enable)
2203 char *func, *command, *next = buff;
2204 struct ftrace_func_command *p;
2207 func = strsep(&next, ":");
2210 ftrace_match_records(func, len, enable);
2216 command = strsep(&next, ":");
2218 mutex_lock(&ftrace_cmd_mutex);
2219 list_for_each_entry(p, &ftrace_commands, list) {
2220 if (strcmp(p->name, command) == 0) {
2221 ret = p->func(func, command, next, enable);
2226 mutex_unlock(&ftrace_cmd_mutex);
2232 ftrace_regex_write(struct file *file, const char __user *ubuf,
2233 size_t cnt, loff_t *ppos, int enable)
2235 struct ftrace_iterator *iter;
2240 if (!cnt || cnt < 0)
2243 mutex_lock(&ftrace_regex_lock);
2245 if (file->f_mode & FMODE_READ) {
2246 struct seq_file *m = file->private_data;
2249 iter = file->private_data;
2252 iter->flags &= ~FTRACE_ITER_CONT;
2253 iter->buffer_idx = 0;
2256 ret = get_user(ch, ubuf++);
2262 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2263 /* skip white space */
2264 while (cnt && isspace(ch)) {
2265 ret = get_user(ch, ubuf++);
2273 file->f_pos += read;
2278 iter->buffer_idx = 0;
2281 while (cnt && !isspace(ch)) {
2282 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2283 iter->buffer[iter->buffer_idx++] = ch;
2288 ret = get_user(ch, ubuf++);
2297 iter->buffer[iter->buffer_idx] = 0;
2298 ret = ftrace_process_regex(iter->buffer,
2299 iter->buffer_idx, enable);
2302 iter->buffer_idx = 0;
2304 iter->flags |= FTRACE_ITER_CONT;
2307 file->f_pos += read;
2311 mutex_unlock(&ftrace_regex_lock);
2317 ftrace_filter_write(struct file *file, const char __user *ubuf,
2318 size_t cnt, loff_t *ppos)
2320 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2324 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2325 size_t cnt, loff_t *ppos)
2327 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2331 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2333 if (unlikely(ftrace_disabled))
2336 mutex_lock(&ftrace_regex_lock);
2338 ftrace_filter_reset(enable);
2340 ftrace_match_records(buf, len, enable);
2341 mutex_unlock(&ftrace_regex_lock);
2345 * ftrace_set_filter - set a function to filter on in ftrace
2346 * @buf - the string that holds the function filter text.
2347 * @len - the length of the string.
2348 * @reset - non zero to reset all filters before applying this filter.
2350 * Filters denote which functions should be enabled when tracing is enabled.
2351 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2353 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2355 ftrace_set_regex(buf, len, reset, 1);
2359 * ftrace_set_notrace - set a function to not trace in ftrace
2360 * @buf - the string that holds the function notrace text.
2361 * @len - the length of the string.
2362 * @reset - non zero to reset all filters before applying this filter.
2364 * Notrace Filters denote which functions should not be enabled when tracing
2365 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2368 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2370 ftrace_set_regex(buf, len, reset, 0);
2374 * command line interface to allow users to set filters on boot up.
2376 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2377 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2378 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2380 static int __init set_ftrace_notrace(char *str)
2382 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2385 __setup("ftrace_notrace=", set_ftrace_notrace);
2387 static int __init set_ftrace_filter(char *str)
2389 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2392 __setup("ftrace_filter=", set_ftrace_filter);
2394 static void __init set_ftrace_early_filter(char *buf, int enable)
2399 func = strsep(&buf, ",");
2400 ftrace_set_regex(func, strlen(func), 0, enable);
2404 static void __init set_ftrace_early_filters(void)
2406 if (ftrace_filter_buf[0])
2407 set_ftrace_early_filter(ftrace_filter_buf, 1);
2408 if (ftrace_notrace_buf[0])
2409 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2413 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2415 struct seq_file *m = (struct seq_file *)file->private_data;
2416 struct ftrace_iterator *iter;
2418 mutex_lock(&ftrace_regex_lock);
2419 if (file->f_mode & FMODE_READ) {
2422 seq_release(inode, file);
2424 iter = file->private_data;
2426 if (iter->buffer_idx) {
2428 iter->buffer[iter->buffer_idx] = 0;
2429 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
2432 mutex_lock(&ftrace_lock);
2433 if (ftrace_start_up && ftrace_enabled)
2434 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2435 mutex_unlock(&ftrace_lock);
2438 mutex_unlock(&ftrace_regex_lock);
2443 ftrace_filter_release(struct inode *inode, struct file *file)
2445 return ftrace_regex_release(inode, file, 1);
2449 ftrace_notrace_release(struct inode *inode, struct file *file)
2451 return ftrace_regex_release(inode, file, 0);
2454 static const struct file_operations ftrace_avail_fops = {
2455 .open = ftrace_avail_open,
2457 .llseek = seq_lseek,
2458 .release = ftrace_avail_release,
2461 static const struct file_operations ftrace_failures_fops = {
2462 .open = ftrace_failures_open,
2464 .llseek = seq_lseek,
2465 .release = ftrace_avail_release,
2468 static const struct file_operations ftrace_filter_fops = {
2469 .open = ftrace_filter_open,
2471 .write = ftrace_filter_write,
2472 .llseek = ftrace_regex_lseek,
2473 .release = ftrace_filter_release,
2476 static const struct file_operations ftrace_notrace_fops = {
2477 .open = ftrace_notrace_open,
2479 .write = ftrace_notrace_write,
2480 .llseek = ftrace_regex_lseek,
2481 .release = ftrace_notrace_release,
2484 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2486 static DEFINE_MUTEX(graph_lock);
2488 int ftrace_graph_count;
2489 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2492 g_next(struct seq_file *m, void *v, loff_t *pos)
2494 unsigned long *array = m->private;
2499 if (index >= ftrace_graph_count)
2502 return &array[index];
2505 static void *g_start(struct seq_file *m, loff_t *pos)
2509 mutex_lock(&graph_lock);
2511 /* Nothing, tell g_show to print all functions are enabled */
2512 if (!ftrace_graph_count && !*pos)
2515 p = g_next(m, p, pos);
2520 static void g_stop(struct seq_file *m, void *p)
2522 mutex_unlock(&graph_lock);
2525 static int g_show(struct seq_file *m, void *v)
2527 unsigned long *ptr = v;
2528 char str[KSYM_SYMBOL_LEN];
2533 if (ptr == (unsigned long *)1) {
2534 seq_printf(m, "#### all functions enabled ####\n");
2538 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2540 seq_printf(m, "%s\n", str);
2545 static struct seq_operations ftrace_graph_seq_ops = {
2553 ftrace_graph_open(struct inode *inode, struct file *file)
2557 if (unlikely(ftrace_disabled))
2560 mutex_lock(&graph_lock);
2561 if ((file->f_mode & FMODE_WRITE) &&
2562 !(file->f_flags & O_APPEND)) {
2563 ftrace_graph_count = 0;
2564 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2567 if (file->f_mode & FMODE_READ) {
2568 ret = seq_open(file, &ftrace_graph_seq_ops);
2570 struct seq_file *m = file->private_data;
2571 m->private = ftrace_graph_funcs;
2574 file->private_data = ftrace_graph_funcs;
2575 mutex_unlock(&graph_lock);
2581 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2583 struct dyn_ftrace *rec;
2584 struct ftrace_page *pg;
2592 if (ftrace_disabled)
2596 type = ftrace_setup_glob(buffer, strlen(buffer), &search, ¬);
2600 search_len = strlen(search);
2602 mutex_lock(&ftrace_lock);
2603 do_for_each_ftrace_rec(pg, rec) {
2605 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2608 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2611 if (ftrace_match_record(rec, search, search_len, type)) {
2612 /* ensure it is not already in the array */
2614 for (i = 0; i < *idx; i++)
2615 if (array[i] == rec->ip) {
2620 array[(*idx)++] = rec->ip;
2624 } while_for_each_ftrace_rec();
2626 mutex_unlock(&ftrace_lock);
2628 return found ? 0 : -EINVAL;
2632 ftrace_graph_write(struct file *file, const char __user *ubuf,
2633 size_t cnt, loff_t *ppos)
2635 unsigned char buffer[FTRACE_BUFF_MAX+1];
2636 unsigned long *array;
2642 if (!cnt || cnt < 0)
2645 mutex_lock(&graph_lock);
2647 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2652 if (file->f_mode & FMODE_READ) {
2653 struct seq_file *m = file->private_data;
2656 array = file->private_data;
2658 ret = get_user(ch, ubuf++);
2664 /* skip white space */
2665 while (cnt && isspace(ch)) {
2666 ret = get_user(ch, ubuf++);
2679 while (cnt && !isspace(ch)) {
2680 if (index < FTRACE_BUFF_MAX)
2681 buffer[index++] = ch;
2686 ret = get_user(ch, ubuf++);
2694 /* we allow only one expression at a time */
2695 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2699 file->f_pos += read;
2703 mutex_unlock(&graph_lock);
2708 static const struct file_operations ftrace_graph_fops = {
2709 .open = ftrace_graph_open,
2711 .write = ftrace_graph_write,
2713 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2715 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2718 trace_create_file("available_filter_functions", 0444,
2719 d_tracer, NULL, &ftrace_avail_fops);
2721 trace_create_file("failures", 0444,
2722 d_tracer, NULL, &ftrace_failures_fops);
2724 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2725 NULL, &ftrace_filter_fops);
2727 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2728 NULL, &ftrace_notrace_fops);
2730 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2731 trace_create_file("set_graph_function", 0444, d_tracer,
2733 &ftrace_graph_fops);
2734 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2739 static int ftrace_convert_nops(struct module *mod,
2740 unsigned long *start,
2745 unsigned long flags;
2747 mutex_lock(&ftrace_lock);
2750 addr = ftrace_call_adjust(*p++);
2752 * Some architecture linkers will pad between
2753 * the different mcount_loc sections of different
2754 * object files to satisfy alignments.
2755 * Skip any NULL pointers.
2759 ftrace_record_ip(addr);
2762 /* disable interrupts to prevent kstop machine */
2763 local_irq_save(flags);
2764 ftrace_update_code(mod);
2765 local_irq_restore(flags);
2766 mutex_unlock(&ftrace_lock);
2771 #ifdef CONFIG_MODULES
2772 void ftrace_release(void *start, void *end)
2774 struct dyn_ftrace *rec;
2775 struct ftrace_page *pg;
2776 unsigned long s = (unsigned long)start;
2777 unsigned long e = (unsigned long)end;
2779 if (ftrace_disabled || !start || start == end)
2782 mutex_lock(&ftrace_lock);
2783 do_for_each_ftrace_rec(pg, rec) {
2784 if ((rec->ip >= s) && (rec->ip < e)) {
2786 * rec->ip is changed in ftrace_free_rec()
2787 * It should not between s and e if record was freed.
2789 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2790 ftrace_free_rec(rec);
2792 } while_for_each_ftrace_rec();
2793 mutex_unlock(&ftrace_lock);
2796 static void ftrace_init_module(struct module *mod,
2797 unsigned long *start, unsigned long *end)
2799 if (ftrace_disabled || start == end)
2801 ftrace_convert_nops(mod, start, end);
2804 static int ftrace_module_notify(struct notifier_block *self,
2805 unsigned long val, void *data)
2807 struct module *mod = data;
2810 case MODULE_STATE_COMING:
2811 ftrace_init_module(mod, mod->ftrace_callsites,
2812 mod->ftrace_callsites +
2813 mod->num_ftrace_callsites);
2815 case MODULE_STATE_GOING:
2816 ftrace_release(mod->ftrace_callsites,
2817 mod->ftrace_callsites +
2818 mod->num_ftrace_callsites);
2825 static int ftrace_module_notify(struct notifier_block *self,
2826 unsigned long val, void *data)
2830 #endif /* CONFIG_MODULES */
2832 struct notifier_block ftrace_module_nb = {
2833 .notifier_call = ftrace_module_notify,
2837 extern unsigned long __start_mcount_loc[];
2838 extern unsigned long __stop_mcount_loc[];
2840 void __init ftrace_init(void)
2842 unsigned long count, addr, flags;
2845 /* Keep the ftrace pointer to the stub */
2846 addr = (unsigned long)ftrace_stub;
2848 local_irq_save(flags);
2849 ftrace_dyn_arch_init(&addr);
2850 local_irq_restore(flags);
2852 /* ftrace_dyn_arch_init places the return code in addr */
2856 count = __stop_mcount_loc - __start_mcount_loc;
2858 ret = ftrace_dyn_table_alloc(count);
2862 last_ftrace_enabled = ftrace_enabled = 1;
2864 ret = ftrace_convert_nops(NULL,
2868 ret = register_module_notifier(&ftrace_module_nb);
2870 pr_warning("Failed to register trace ftrace module notifier\n");
2872 set_ftrace_early_filters();
2876 ftrace_disabled = 1;
2881 static int __init ftrace_nodyn_init(void)
2886 device_initcall(ftrace_nodyn_init);
2888 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2889 static inline void ftrace_startup_enable(int command) { }
2890 /* Keep as macros so we do not need to define the commands */
2891 # define ftrace_startup(command) do { } while (0)
2892 # define ftrace_shutdown(command) do { } while (0)
2893 # define ftrace_startup_sysctl() do { } while (0)
2894 # define ftrace_shutdown_sysctl() do { } while (0)
2895 #endif /* CONFIG_DYNAMIC_FTRACE */
2898 ftrace_pid_read(struct file *file, char __user *ubuf,
2899 size_t cnt, loff_t *ppos)
2904 if (ftrace_pid_trace == ftrace_swapper_pid)
2905 r = sprintf(buf, "swapper tasks\n");
2906 else if (ftrace_pid_trace)
2907 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2909 r = sprintf(buf, "no pid\n");
2911 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2914 static void clear_ftrace_swapper(void)
2916 struct task_struct *p;
2920 for_each_online_cpu(cpu) {
2922 clear_tsk_trace_trace(p);
2927 static void set_ftrace_swapper(void)
2929 struct task_struct *p;
2933 for_each_online_cpu(cpu) {
2935 set_tsk_trace_trace(p);
2940 static void clear_ftrace_pid(struct pid *pid)
2942 struct task_struct *p;
2945 do_each_pid_task(pid, PIDTYPE_PID, p) {
2946 clear_tsk_trace_trace(p);
2947 } while_each_pid_task(pid, PIDTYPE_PID, p);
2953 static void set_ftrace_pid(struct pid *pid)
2955 struct task_struct *p;
2958 do_each_pid_task(pid, PIDTYPE_PID, p) {
2959 set_tsk_trace_trace(p);
2960 } while_each_pid_task(pid, PIDTYPE_PID, p);
2964 static void clear_ftrace_pid_task(struct pid **pid)
2966 if (*pid == ftrace_swapper_pid)
2967 clear_ftrace_swapper();
2969 clear_ftrace_pid(*pid);
2974 static void set_ftrace_pid_task(struct pid *pid)
2976 if (pid == ftrace_swapper_pid)
2977 set_ftrace_swapper();
2979 set_ftrace_pid(pid);
2983 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2984 size_t cnt, loff_t *ppos)
2991 if (cnt >= sizeof(buf))
2994 if (copy_from_user(&buf, ubuf, cnt))
2999 ret = strict_strtol(buf, 10, &val);
3003 mutex_lock(&ftrace_lock);
3005 /* disable pid tracing */
3006 if (!ftrace_pid_trace)
3009 clear_ftrace_pid_task(&ftrace_pid_trace);
3012 /* swapper task is special */
3014 pid = ftrace_swapper_pid;
3015 if (pid == ftrace_pid_trace)
3018 pid = find_get_pid(val);
3020 if (pid == ftrace_pid_trace) {
3026 if (ftrace_pid_trace)
3027 clear_ftrace_pid_task(&ftrace_pid_trace);
3032 ftrace_pid_trace = pid;
3034 set_ftrace_pid_task(ftrace_pid_trace);
3037 /* update the function call */
3038 ftrace_update_pid_func();
3039 ftrace_startup_enable(0);
3042 mutex_unlock(&ftrace_lock);
3047 static const struct file_operations ftrace_pid_fops = {
3048 .read = ftrace_pid_read,
3049 .write = ftrace_pid_write,
3052 static __init int ftrace_init_debugfs(void)
3054 struct dentry *d_tracer;
3056 d_tracer = tracing_init_dentry();
3060 ftrace_init_dyn_debugfs(d_tracer);
3062 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3063 NULL, &ftrace_pid_fops);
3065 ftrace_profile_debugfs(d_tracer);
3069 fs_initcall(ftrace_init_debugfs);
3072 * ftrace_kill - kill ftrace
3074 * This function should be used by panic code. It stops ftrace
3075 * but in a not so nice way. If you need to simply kill ftrace
3076 * from a non-atomic section, use ftrace_kill.
3078 void ftrace_kill(void)
3080 ftrace_disabled = 1;
3082 clear_ftrace_function();
3086 * register_ftrace_function - register a function for profiling
3087 * @ops - ops structure that holds the function for profiling.
3089 * Register a function to be called by all functions in the
3092 * Note: @ops->func and all the functions it calls must be labeled
3093 * with "notrace", otherwise it will go into a
3096 int register_ftrace_function(struct ftrace_ops *ops)
3100 if (unlikely(ftrace_disabled))
3103 mutex_lock(&ftrace_lock);
3105 ret = __register_ftrace_function(ops);
3108 mutex_unlock(&ftrace_lock);
3113 * unregister_ftrace_function - unregister a function for profiling.
3114 * @ops - ops structure that holds the function to unregister
3116 * Unregister a function that was added to be called by ftrace profiling.
3118 int unregister_ftrace_function(struct ftrace_ops *ops)
3122 mutex_lock(&ftrace_lock);
3123 ret = __unregister_ftrace_function(ops);
3125 mutex_unlock(&ftrace_lock);
3131 ftrace_enable_sysctl(struct ctl_table *table, int write,
3132 struct file *file, void __user *buffer, size_t *lenp,
3137 if (unlikely(ftrace_disabled))
3140 mutex_lock(&ftrace_lock);
3142 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
3144 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
3147 last_ftrace_enabled = ftrace_enabled;
3149 if (ftrace_enabled) {
3151 ftrace_startup_sysctl();
3153 /* we are starting ftrace again */
3154 if (ftrace_list != &ftrace_list_end) {
3155 if (ftrace_list->next == &ftrace_list_end)
3156 ftrace_trace_function = ftrace_list->func;
3158 ftrace_trace_function = ftrace_list_func;
3162 /* stopping ftrace calls (just send to ftrace_stub) */
3163 ftrace_trace_function = ftrace_stub;
3165 ftrace_shutdown_sysctl();
3169 mutex_unlock(&ftrace_lock);
3173 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3175 static int ftrace_graph_active;
3176 static struct notifier_block ftrace_suspend_notifier;
3178 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3183 /* The callbacks that hook a function */
3184 trace_func_graph_ret_t ftrace_graph_return =
3185 (trace_func_graph_ret_t)ftrace_stub;
3186 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3188 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3189 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3193 unsigned long flags;
3194 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3195 struct task_struct *g, *t;
3197 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3198 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3199 * sizeof(struct ftrace_ret_stack),
3201 if (!ret_stack_list[i]) {
3209 read_lock_irqsave(&tasklist_lock, flags);
3210 do_each_thread(g, t) {
3216 if (t->ret_stack == NULL) {
3217 t->curr_ret_stack = -1;
3218 /* Make sure IRQs see the -1 first: */
3220 t->ret_stack = ret_stack_list[start++];
3221 atomic_set(&t->tracing_graph_pause, 0);
3222 atomic_set(&t->trace_overrun, 0);
3224 } while_each_thread(g, t);
3227 read_unlock_irqrestore(&tasklist_lock, flags);
3229 for (i = start; i < end; i++)
3230 kfree(ret_stack_list[i]);
3235 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3236 struct task_struct *next)
3238 unsigned long long timestamp;
3242 * Does the user want to count the time a function was asleep.
3243 * If so, do not update the time stamps.
3245 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3248 timestamp = trace_clock_local();
3250 prev->ftrace_timestamp = timestamp;
3252 /* only process tasks that we timestamped */
3253 if (!next->ftrace_timestamp)
3257 * Update all the counters in next to make up for the
3258 * time next was sleeping.
3260 timestamp -= next->ftrace_timestamp;
3262 for (index = next->curr_ret_stack; index >= 0; index--)
3263 next->ret_stack[index].calltime += timestamp;
3266 /* Allocate a return stack for each task */
3267 static int start_graph_tracing(void)
3269 struct ftrace_ret_stack **ret_stack_list;
3272 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3273 sizeof(struct ftrace_ret_stack *),
3276 if (!ret_stack_list)
3279 /* The cpu_boot init_task->ret_stack will never be freed */
3280 for_each_online_cpu(cpu)
3281 ftrace_graph_init_task(idle_task(cpu));
3284 ret = alloc_retstack_tasklist(ret_stack_list);
3285 } while (ret == -EAGAIN);
3288 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3290 pr_info("ftrace_graph: Couldn't activate tracepoint"
3291 " probe to kernel_sched_switch\n");
3294 kfree(ret_stack_list);
3299 * Hibernation protection.
3300 * The state of the current task is too much unstable during
3301 * suspend/restore to disk. We want to protect against that.
3304 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3308 case PM_HIBERNATION_PREPARE:
3309 pause_graph_tracing();
3312 case PM_POST_HIBERNATION:
3313 unpause_graph_tracing();
3319 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3320 trace_func_graph_ent_t entryfunc)
3324 mutex_lock(&ftrace_lock);
3326 /* we currently allow only one tracer registered at a time */
3327 if (ftrace_graph_active) {
3332 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3333 register_pm_notifier(&ftrace_suspend_notifier);
3335 ftrace_graph_active++;
3336 ret = start_graph_tracing();
3338 ftrace_graph_active--;
3342 ftrace_graph_return = retfunc;
3343 ftrace_graph_entry = entryfunc;
3345 ftrace_startup(FTRACE_START_FUNC_RET);
3348 mutex_unlock(&ftrace_lock);
3352 void unregister_ftrace_graph(void)
3354 mutex_lock(&ftrace_lock);
3356 if (unlikely(!ftrace_graph_active))
3359 ftrace_graph_active--;
3360 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3361 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3362 ftrace_graph_entry = ftrace_graph_entry_stub;
3363 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3364 unregister_pm_notifier(&ftrace_suspend_notifier);
3367 mutex_unlock(&ftrace_lock);
3370 /* Allocate a return stack for newly created task */
3371 void ftrace_graph_init_task(struct task_struct *t)
3373 if (ftrace_graph_active) {
3374 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3375 * sizeof(struct ftrace_ret_stack),
3379 t->curr_ret_stack = -1;
3380 atomic_set(&t->tracing_graph_pause, 0);
3381 atomic_set(&t->trace_overrun, 0);
3382 t->ftrace_timestamp = 0;
3384 t->ret_stack = NULL;
3387 void ftrace_graph_exit_task(struct task_struct *t)
3389 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3391 t->ret_stack = NULL;
3392 /* NULL must become visible to IRQs before we free it: */
3398 void ftrace_graph_stop(void)