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);
602 if (!stat->hash || !ftrace_profile_enabled)
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);
633 if (!stat->hash || !ftrace_profile_enabled)
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;
728 * unregister_ftrace_profiler calls stop_machine
729 * so this acts like an synchronize_sched.
731 unregister_ftrace_profiler();
735 mutex_unlock(&ftrace_profile_lock);
743 ftrace_profile_read(struct file *filp, char __user *ubuf,
744 size_t cnt, loff_t *ppos)
746 char buf[64]; /* big enough to hold a number */
749 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
750 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
753 static const struct file_operations ftrace_profile_fops = {
754 .open = tracing_open_generic,
755 .read = ftrace_profile_read,
756 .write = ftrace_profile_write,
759 /* used to initialize the real stat files */
760 static struct tracer_stat function_stats __initdata = {
762 .stat_start = function_stat_start,
763 .stat_next = function_stat_next,
764 .stat_cmp = function_stat_cmp,
765 .stat_headers = function_stat_headers,
766 .stat_show = function_stat_show
769 static void ftrace_profile_debugfs(struct dentry *d_tracer)
771 struct ftrace_profile_stat *stat;
772 struct dentry *entry;
777 for_each_possible_cpu(cpu) {
778 stat = &per_cpu(ftrace_profile_stats, cpu);
780 /* allocate enough for function name + cpu number */
781 name = kmalloc(32, GFP_KERNEL);
784 * The files created are permanent, if something happens
785 * we still do not free memory.
789 "Could not allocate stat file for cpu %d\n",
793 stat->stat = function_stats;
794 snprintf(name, 32, "function%d", cpu);
795 stat->stat.name = name;
796 ret = register_stat_tracer(&stat->stat);
799 "Could not register function stat for cpu %d\n",
806 entry = debugfs_create_file("function_profile_enabled", 0644,
807 d_tracer, NULL, &ftrace_profile_fops);
809 pr_warning("Could not create debugfs "
810 "'function_profile_enabled' entry\n");
813 #else /* CONFIG_FUNCTION_PROFILER */
814 static void ftrace_profile_debugfs(struct dentry *d_tracer)
817 #endif /* CONFIG_FUNCTION_PROFILER */
819 /* set when tracing only a pid */
820 struct pid *ftrace_pid_trace;
821 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
823 #ifdef CONFIG_DYNAMIC_FTRACE
825 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
826 # error Dynamic ftrace depends on MCOUNT_RECORD
829 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
831 struct ftrace_func_probe {
832 struct hlist_node node;
833 struct ftrace_probe_ops *ops;
841 FTRACE_ENABLE_CALLS = (1 << 0),
842 FTRACE_DISABLE_CALLS = (1 << 1),
843 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
844 FTRACE_ENABLE_MCOUNT = (1 << 3),
845 FTRACE_DISABLE_MCOUNT = (1 << 4),
846 FTRACE_START_FUNC_RET = (1 << 5),
847 FTRACE_STOP_FUNC_RET = (1 << 6),
850 static int ftrace_filtered;
852 static struct dyn_ftrace *ftrace_new_addrs;
854 static DEFINE_MUTEX(ftrace_regex_lock);
857 struct ftrace_page *next;
859 struct dyn_ftrace records[];
862 #define ENTRIES_PER_PAGE \
863 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
865 /* estimate from running different kernels */
866 #define NR_TO_INIT 10000
868 static struct ftrace_page *ftrace_pages_start;
869 static struct ftrace_page *ftrace_pages;
871 static struct dyn_ftrace *ftrace_free_records;
874 * This is a double for. Do not use 'break' to break out of the loop,
875 * you must use a goto.
877 #define do_for_each_ftrace_rec(pg, rec) \
878 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
880 for (_____i = 0; _____i < pg->index; _____i++) { \
881 rec = &pg->records[_____i];
883 #define while_for_each_ftrace_rec() \
887 #ifdef CONFIG_KPROBES
889 static int frozen_record_count;
891 static inline void freeze_record(struct dyn_ftrace *rec)
893 if (!(rec->flags & FTRACE_FL_FROZEN)) {
894 rec->flags |= FTRACE_FL_FROZEN;
895 frozen_record_count++;
899 static inline void unfreeze_record(struct dyn_ftrace *rec)
901 if (rec->flags & FTRACE_FL_FROZEN) {
902 rec->flags &= ~FTRACE_FL_FROZEN;
903 frozen_record_count--;
907 static inline int record_frozen(struct dyn_ftrace *rec)
909 return rec->flags & FTRACE_FL_FROZEN;
912 # define freeze_record(rec) ({ 0; })
913 # define unfreeze_record(rec) ({ 0; })
914 # define record_frozen(rec) ({ 0; })
915 #endif /* CONFIG_KPROBES */
917 static void ftrace_free_rec(struct dyn_ftrace *rec)
919 rec->freelist = ftrace_free_records;
920 ftrace_free_records = rec;
921 rec->flags |= FTRACE_FL_FREE;
924 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
926 struct dyn_ftrace *rec;
928 /* First check for freed records */
929 if (ftrace_free_records) {
930 rec = ftrace_free_records;
932 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
933 FTRACE_WARN_ON_ONCE(1);
934 ftrace_free_records = NULL;
938 ftrace_free_records = rec->freelist;
939 memset(rec, 0, sizeof(*rec));
943 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
944 if (!ftrace_pages->next) {
945 /* allocate another page */
947 (void *)get_zeroed_page(GFP_KERNEL);
948 if (!ftrace_pages->next)
951 ftrace_pages = ftrace_pages->next;
954 return &ftrace_pages->records[ftrace_pages->index++];
957 static struct dyn_ftrace *
958 ftrace_record_ip(unsigned long ip)
960 struct dyn_ftrace *rec;
965 rec = ftrace_alloc_dyn_node(ip);
970 rec->newlist = ftrace_new_addrs;
971 ftrace_new_addrs = rec;
976 static void print_ip_ins(const char *fmt, unsigned char *p)
980 printk(KERN_CONT "%s", fmt);
982 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
983 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
986 static void ftrace_bug(int failed, unsigned long ip)
990 FTRACE_WARN_ON_ONCE(1);
991 pr_info("ftrace faulted on modifying ");
995 FTRACE_WARN_ON_ONCE(1);
996 pr_info("ftrace failed to modify ");
998 print_ip_ins(" actual: ", (unsigned char *)ip);
999 printk(KERN_CONT "\n");
1002 FTRACE_WARN_ON_ONCE(1);
1003 pr_info("ftrace faulted on writing ");
1007 FTRACE_WARN_ON_ONCE(1);
1008 pr_info("ftrace faulted on unknown error ");
1015 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1017 unsigned long ftrace_addr;
1018 unsigned long ip, fl;
1020 ftrace_addr = (unsigned long)FTRACE_ADDR;
1025 * If this record is not to be traced and
1026 * it is not enabled then do nothing.
1028 * If this record is not to be traced and
1029 * it is enabled then disable it.
1032 if (rec->flags & FTRACE_FL_NOTRACE) {
1033 if (rec->flags & FTRACE_FL_ENABLED)
1034 rec->flags &= ~FTRACE_FL_ENABLED;
1038 } else if (ftrace_filtered && enable) {
1043 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
1045 /* Record is filtered and enabled, do nothing */
1046 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
1049 /* Record is not filtered or enabled, do nothing */
1053 /* Record is not filtered but enabled, disable it */
1054 if (fl == FTRACE_FL_ENABLED)
1055 rec->flags &= ~FTRACE_FL_ENABLED;
1057 /* Otherwise record is filtered but not enabled, enable it */
1058 rec->flags |= FTRACE_FL_ENABLED;
1060 /* Disable or not filtered */
1063 /* if record is enabled, do nothing */
1064 if (rec->flags & FTRACE_FL_ENABLED)
1067 rec->flags |= FTRACE_FL_ENABLED;
1071 /* if record is not enabled, do nothing */
1072 if (!(rec->flags & FTRACE_FL_ENABLED))
1075 rec->flags &= ~FTRACE_FL_ENABLED;
1079 if (rec->flags & FTRACE_FL_ENABLED)
1080 return ftrace_make_call(rec, ftrace_addr);
1082 return ftrace_make_nop(NULL, rec, ftrace_addr);
1085 static void ftrace_replace_code(int enable)
1087 struct dyn_ftrace *rec;
1088 struct ftrace_page *pg;
1091 do_for_each_ftrace_rec(pg, rec) {
1093 * Skip over free records, records that have
1094 * failed and not converted.
1096 if (rec->flags & FTRACE_FL_FREE ||
1097 rec->flags & FTRACE_FL_FAILED ||
1098 !(rec->flags & FTRACE_FL_CONVERTED))
1101 /* ignore updates to this record's mcount site */
1102 if (get_kprobe((void *)rec->ip)) {
1106 unfreeze_record(rec);
1109 failed = __ftrace_replace_code(rec, enable);
1111 rec->flags |= FTRACE_FL_FAILED;
1112 if ((system_state == SYSTEM_BOOTING) ||
1113 !core_kernel_text(rec->ip)) {
1114 ftrace_free_rec(rec);
1116 ftrace_bug(failed, rec->ip);
1117 /* Stop processing */
1121 } while_for_each_ftrace_rec();
1125 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1132 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1134 ftrace_bug(ret, ip);
1135 rec->flags |= FTRACE_FL_FAILED;
1142 * archs can override this function if they must do something
1143 * before the modifying code is performed.
1145 int __weak ftrace_arch_code_modify_prepare(void)
1151 * archs can override this function if they must do something
1152 * after the modifying code is performed.
1154 int __weak ftrace_arch_code_modify_post_process(void)
1159 static int __ftrace_modify_code(void *data)
1161 int *command = data;
1163 if (*command & FTRACE_ENABLE_CALLS)
1164 ftrace_replace_code(1);
1165 else if (*command & FTRACE_DISABLE_CALLS)
1166 ftrace_replace_code(0);
1168 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1169 ftrace_update_ftrace_func(ftrace_trace_function);
1171 if (*command & FTRACE_START_FUNC_RET)
1172 ftrace_enable_ftrace_graph_caller();
1173 else if (*command & FTRACE_STOP_FUNC_RET)
1174 ftrace_disable_ftrace_graph_caller();
1179 static void ftrace_run_update_code(int command)
1183 ret = ftrace_arch_code_modify_prepare();
1184 FTRACE_WARN_ON(ret);
1188 stop_machine(__ftrace_modify_code, &command, NULL);
1190 ret = ftrace_arch_code_modify_post_process();
1191 FTRACE_WARN_ON(ret);
1194 static ftrace_func_t saved_ftrace_func;
1195 static int ftrace_start_up;
1197 static void ftrace_startup_enable(int command)
1199 if (saved_ftrace_func != ftrace_trace_function) {
1200 saved_ftrace_func = ftrace_trace_function;
1201 command |= FTRACE_UPDATE_TRACE_FUNC;
1204 if (!command || !ftrace_enabled)
1207 ftrace_run_update_code(command);
1210 static void ftrace_startup(int command)
1212 if (unlikely(ftrace_disabled))
1216 command |= FTRACE_ENABLE_CALLS;
1218 ftrace_startup_enable(command);
1221 static void ftrace_shutdown(int command)
1223 if (unlikely(ftrace_disabled))
1228 * Just warn in case of unbalance, no need to kill ftrace, it's not
1229 * critical but the ftrace_call callers may be never nopped again after
1230 * further ftrace uses.
1232 WARN_ON_ONCE(ftrace_start_up < 0);
1234 if (!ftrace_start_up)
1235 command |= FTRACE_DISABLE_CALLS;
1237 if (saved_ftrace_func != ftrace_trace_function) {
1238 saved_ftrace_func = ftrace_trace_function;
1239 command |= FTRACE_UPDATE_TRACE_FUNC;
1242 if (!command || !ftrace_enabled)
1245 ftrace_run_update_code(command);
1248 static void ftrace_startup_sysctl(void)
1250 int command = FTRACE_ENABLE_MCOUNT;
1252 if (unlikely(ftrace_disabled))
1255 /* Force update next time */
1256 saved_ftrace_func = NULL;
1257 /* ftrace_start_up is true if we want ftrace running */
1258 if (ftrace_start_up)
1259 command |= FTRACE_ENABLE_CALLS;
1261 ftrace_run_update_code(command);
1264 static void ftrace_shutdown_sysctl(void)
1266 int command = FTRACE_DISABLE_MCOUNT;
1268 if (unlikely(ftrace_disabled))
1271 /* ftrace_start_up is true if ftrace is running */
1272 if (ftrace_start_up)
1273 command |= FTRACE_DISABLE_CALLS;
1275 ftrace_run_update_code(command);
1278 static cycle_t ftrace_update_time;
1279 static unsigned long ftrace_update_cnt;
1280 unsigned long ftrace_update_tot_cnt;
1282 static int ftrace_update_code(struct module *mod)
1284 struct dyn_ftrace *p;
1285 cycle_t start, stop;
1287 start = ftrace_now(raw_smp_processor_id());
1288 ftrace_update_cnt = 0;
1290 while (ftrace_new_addrs) {
1292 /* If something went wrong, bail without enabling anything */
1293 if (unlikely(ftrace_disabled))
1296 p = ftrace_new_addrs;
1297 ftrace_new_addrs = p->newlist;
1300 /* convert record (i.e, patch mcount-call with NOP) */
1301 if (ftrace_code_disable(mod, p)) {
1302 p->flags |= FTRACE_FL_CONVERTED;
1303 ftrace_update_cnt++;
1308 stop = ftrace_now(raw_smp_processor_id());
1309 ftrace_update_time = stop - start;
1310 ftrace_update_tot_cnt += ftrace_update_cnt;
1315 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1317 struct ftrace_page *pg;
1321 /* allocate a few pages */
1322 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1323 if (!ftrace_pages_start)
1327 * Allocate a few more pages.
1329 * TODO: have some parser search vmlinux before
1330 * final linking to find all calls to ftrace.
1332 * a) know how many pages to allocate.
1334 * b) set up the table then.
1336 * The dynamic code is still necessary for
1340 pg = ftrace_pages = ftrace_pages_start;
1342 cnt = num_to_init / ENTRIES_PER_PAGE;
1343 pr_info("ftrace: allocating %ld entries in %d pages\n",
1344 num_to_init, cnt + 1);
1346 for (i = 0; i < cnt; i++) {
1347 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1349 /* If we fail, we'll try later anyway */
1360 FTRACE_ITER_FILTER = (1 << 0),
1361 FTRACE_ITER_CONT = (1 << 1),
1362 FTRACE_ITER_NOTRACE = (1 << 2),
1363 FTRACE_ITER_FAILURES = (1 << 3),
1364 FTRACE_ITER_PRINTALL = (1 << 4),
1365 FTRACE_ITER_HASH = (1 << 5),
1368 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1370 struct ftrace_iterator {
1371 struct ftrace_page *pg;
1375 unsigned char buffer[FTRACE_BUFF_MAX+1];
1376 unsigned buffer_idx;
1381 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1383 struct ftrace_iterator *iter = m->private;
1384 struct hlist_node *hnd = v;
1385 struct hlist_head *hhd;
1387 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1392 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1395 hhd = &ftrace_func_hash[iter->hidx];
1397 if (hlist_empty(hhd)) {
1416 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1418 struct ftrace_iterator *iter = m->private;
1422 if (!(iter->flags & FTRACE_ITER_HASH))
1425 iter->flags |= FTRACE_ITER_HASH;
1428 for (l = 0; l <= *pos; ) {
1429 p = t_hash_next(m, p, &l);
1436 static int t_hash_show(struct seq_file *m, void *v)
1438 struct ftrace_func_probe *rec;
1439 struct hlist_node *hnd = v;
1440 char str[KSYM_SYMBOL_LEN];
1442 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1444 if (rec->ops->print)
1445 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1447 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1448 seq_printf(m, "%s:", str);
1450 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1451 seq_printf(m, "%s", str);
1454 seq_printf(m, ":%p", rec->data);
1461 t_next(struct seq_file *m, void *v, loff_t *pos)
1463 struct ftrace_iterator *iter = m->private;
1464 struct dyn_ftrace *rec = NULL;
1466 if (iter->flags & FTRACE_ITER_HASH)
1467 return t_hash_next(m, v, pos);
1471 if (iter->flags & FTRACE_ITER_PRINTALL)
1475 if (iter->idx >= iter->pg->index) {
1476 if (iter->pg->next) {
1477 iter->pg = iter->pg->next;
1482 rec = &iter->pg->records[iter->idx++];
1483 if ((rec->flags & FTRACE_FL_FREE) ||
1485 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1486 (rec->flags & FTRACE_FL_FAILED)) ||
1488 ((iter->flags & FTRACE_ITER_FAILURES) &&
1489 !(rec->flags & FTRACE_FL_FAILED)) ||
1491 ((iter->flags & FTRACE_ITER_FILTER) &&
1492 !(rec->flags & FTRACE_FL_FILTER)) ||
1494 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1495 !(rec->flags & FTRACE_FL_NOTRACE))) {
1504 static void *t_start(struct seq_file *m, loff_t *pos)
1506 struct ftrace_iterator *iter = m->private;
1510 mutex_lock(&ftrace_lock);
1512 * For set_ftrace_filter reading, if we have the filter
1513 * off, we can short cut and just print out that all
1514 * functions are enabled.
1516 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1518 return t_hash_start(m, pos);
1519 iter->flags |= FTRACE_ITER_PRINTALL;
1523 if (iter->flags & FTRACE_ITER_HASH)
1524 return t_hash_start(m, pos);
1526 iter->pg = ftrace_pages_start;
1528 for (l = 0; l <= *pos; ) {
1529 p = t_next(m, p, &l);
1534 if (!p && iter->flags & FTRACE_ITER_FILTER)
1535 return t_hash_start(m, pos);
1540 static void t_stop(struct seq_file *m, void *p)
1542 mutex_unlock(&ftrace_lock);
1545 static int t_show(struct seq_file *m, void *v)
1547 struct ftrace_iterator *iter = m->private;
1548 struct dyn_ftrace *rec = v;
1549 char str[KSYM_SYMBOL_LEN];
1551 if (iter->flags & FTRACE_ITER_HASH)
1552 return t_hash_show(m, v);
1554 if (iter->flags & FTRACE_ITER_PRINTALL) {
1555 seq_printf(m, "#### all functions enabled ####\n");
1562 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1564 seq_printf(m, "%s\n", str);
1569 static struct seq_operations show_ftrace_seq_ops = {
1577 ftrace_avail_open(struct inode *inode, struct file *file)
1579 struct ftrace_iterator *iter;
1582 if (unlikely(ftrace_disabled))
1585 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1589 iter->pg = ftrace_pages_start;
1591 ret = seq_open(file, &show_ftrace_seq_ops);
1593 struct seq_file *m = file->private_data;
1603 int ftrace_avail_release(struct inode *inode, struct file *file)
1605 struct seq_file *m = (struct seq_file *)file->private_data;
1606 struct ftrace_iterator *iter = m->private;
1608 seq_release(inode, file);
1615 ftrace_failures_open(struct inode *inode, struct file *file)
1619 struct ftrace_iterator *iter;
1621 ret = ftrace_avail_open(inode, file);
1623 m = (struct seq_file *)file->private_data;
1624 iter = (struct ftrace_iterator *)m->private;
1625 iter->flags = FTRACE_ITER_FAILURES;
1632 static void ftrace_filter_reset(int enable)
1634 struct ftrace_page *pg;
1635 struct dyn_ftrace *rec;
1636 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1638 mutex_lock(&ftrace_lock);
1640 ftrace_filtered = 0;
1641 do_for_each_ftrace_rec(pg, rec) {
1642 if (rec->flags & FTRACE_FL_FAILED)
1644 rec->flags &= ~type;
1645 } while_for_each_ftrace_rec();
1646 mutex_unlock(&ftrace_lock);
1650 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1652 struct ftrace_iterator *iter;
1655 if (unlikely(ftrace_disabled))
1658 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1662 mutex_lock(&ftrace_regex_lock);
1663 if ((file->f_mode & FMODE_WRITE) &&
1664 !(file->f_flags & O_APPEND))
1665 ftrace_filter_reset(enable);
1667 if (file->f_mode & FMODE_READ) {
1668 iter->pg = ftrace_pages_start;
1669 iter->flags = enable ? FTRACE_ITER_FILTER :
1670 FTRACE_ITER_NOTRACE;
1672 ret = seq_open(file, &show_ftrace_seq_ops);
1674 struct seq_file *m = file->private_data;
1679 file->private_data = iter;
1680 mutex_unlock(&ftrace_regex_lock);
1686 ftrace_filter_open(struct inode *inode, struct file *file)
1688 return ftrace_regex_open(inode, file, 1);
1692 ftrace_notrace_open(struct inode *inode, struct file *file)
1694 return ftrace_regex_open(inode, file, 0);
1698 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1702 if (file->f_mode & FMODE_READ)
1703 ret = seq_lseek(file, offset, origin);
1705 file->f_pos = ret = 1;
1718 * (static function - no need for kernel doc)
1720 * Pass in a buffer containing a glob and this function will
1721 * set search to point to the search part of the buffer and
1722 * return the type of search it is (see enum above).
1723 * This does modify buff.
1725 * Returns enum type.
1726 * search returns the pointer to use for comparison.
1727 * not returns 1 if buff started with a '!'
1731 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1733 int type = MATCH_FULL;
1736 if (buff[0] == '!') {
1745 for (i = 0; i < len; i++) {
1746 if (buff[i] == '*') {
1749 type = MATCH_END_ONLY;
1751 if (type == MATCH_END_ONLY)
1752 type = MATCH_MIDDLE_ONLY;
1754 type = MATCH_FRONT_ONLY;
1764 static int ftrace_match(char *str, char *regex, int len, int type)
1771 if (strcmp(str, regex) == 0)
1774 case MATCH_FRONT_ONLY:
1775 if (strncmp(str, regex, len) == 0)
1778 case MATCH_MIDDLE_ONLY:
1779 if (strstr(str, regex))
1782 case MATCH_END_ONLY:
1783 ptr = strstr(str, regex);
1784 if (ptr && (ptr[len] == 0))
1793 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1795 char str[KSYM_SYMBOL_LEN];
1797 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1798 return ftrace_match(str, regex, len, type);
1801 static void ftrace_match_records(char *buff, int len, int enable)
1803 unsigned int search_len;
1804 struct ftrace_page *pg;
1805 struct dyn_ftrace *rec;
1811 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1812 type = ftrace_setup_glob(buff, len, &search, ¬);
1814 search_len = strlen(search);
1816 mutex_lock(&ftrace_lock);
1817 do_for_each_ftrace_rec(pg, rec) {
1819 if (rec->flags & FTRACE_FL_FAILED)
1822 if (ftrace_match_record(rec, search, search_len, type)) {
1824 rec->flags &= ~flag;
1829 * Only enable filtering if we have a function that
1832 if (enable && (rec->flags & FTRACE_FL_FILTER))
1833 ftrace_filtered = 1;
1834 } while_for_each_ftrace_rec();
1835 mutex_unlock(&ftrace_lock);
1839 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1840 char *regex, int len, int type)
1842 char str[KSYM_SYMBOL_LEN];
1845 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1847 if (!modname || strcmp(modname, mod))
1850 /* blank search means to match all funcs in the mod */
1852 return ftrace_match(str, regex, len, type);
1857 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1859 unsigned search_len = 0;
1860 struct ftrace_page *pg;
1861 struct dyn_ftrace *rec;
1862 int type = MATCH_FULL;
1863 char *search = buff;
1867 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1869 /* blank or '*' mean the same */
1870 if (strcmp(buff, "*") == 0)
1873 /* handle the case of 'dont filter this module' */
1874 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1880 type = ftrace_setup_glob(buff, strlen(buff), &search, ¬);
1881 search_len = strlen(search);
1884 mutex_lock(&ftrace_lock);
1885 do_for_each_ftrace_rec(pg, rec) {
1887 if (rec->flags & FTRACE_FL_FAILED)
1890 if (ftrace_match_module_record(rec, mod,
1891 search, search_len, type)) {
1893 rec->flags &= ~flag;
1897 if (enable && (rec->flags & FTRACE_FL_FILTER))
1898 ftrace_filtered = 1;
1900 } while_for_each_ftrace_rec();
1901 mutex_unlock(&ftrace_lock);
1905 * We register the module command as a template to show others how
1906 * to register the a command as well.
1910 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1915 * cmd == 'mod' because we only registered this func
1916 * for the 'mod' ftrace_func_command.
1917 * But if you register one func with multiple commands,
1918 * you can tell which command was used by the cmd
1922 /* we must have a module name */
1926 mod = strsep(¶m, ":");
1930 ftrace_match_module_records(func, mod, enable);
1934 static struct ftrace_func_command ftrace_mod_cmd = {
1936 .func = ftrace_mod_callback,
1939 static int __init ftrace_mod_cmd_init(void)
1941 return register_ftrace_command(&ftrace_mod_cmd);
1943 device_initcall(ftrace_mod_cmd_init);
1946 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1948 struct ftrace_func_probe *entry;
1949 struct hlist_head *hhd;
1950 struct hlist_node *n;
1954 key = hash_long(ip, FTRACE_HASH_BITS);
1956 hhd = &ftrace_func_hash[key];
1958 if (hlist_empty(hhd))
1962 * Disable preemption for these calls to prevent a RCU grace
1963 * period. This syncs the hash iteration and freeing of items
1964 * on the hash. rcu_read_lock is too dangerous here.
1966 resched = ftrace_preempt_disable();
1967 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1968 if (entry->ip == ip)
1969 entry->ops->func(ip, parent_ip, &entry->data);
1971 ftrace_preempt_enable(resched);
1974 static struct ftrace_ops trace_probe_ops __read_mostly =
1976 .func = function_trace_probe_call,
1979 static int ftrace_probe_registered;
1981 static void __enable_ftrace_function_probe(void)
1985 if (ftrace_probe_registered)
1988 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1989 struct hlist_head *hhd = &ftrace_func_hash[i];
1993 /* Nothing registered? */
1994 if (i == FTRACE_FUNC_HASHSIZE)
1997 __register_ftrace_function(&trace_probe_ops);
1999 ftrace_probe_registered = 1;
2002 static void __disable_ftrace_function_probe(void)
2006 if (!ftrace_probe_registered)
2009 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2010 struct hlist_head *hhd = &ftrace_func_hash[i];
2015 /* no more funcs left */
2016 __unregister_ftrace_function(&trace_probe_ops);
2018 ftrace_probe_registered = 0;
2022 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2024 struct ftrace_func_probe *entry =
2025 container_of(rhp, struct ftrace_func_probe, rcu);
2027 if (entry->ops->free)
2028 entry->ops->free(&entry->data);
2034 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2037 struct ftrace_func_probe *entry;
2038 struct ftrace_page *pg;
2039 struct dyn_ftrace *rec;
2045 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2046 len = strlen(search);
2048 /* we do not support '!' for function probes */
2052 mutex_lock(&ftrace_lock);
2053 do_for_each_ftrace_rec(pg, rec) {
2055 if (rec->flags & FTRACE_FL_FAILED)
2058 if (!ftrace_match_record(rec, search, len, type))
2061 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2063 /* If we did not process any, then return error */
2074 * The caller might want to do something special
2075 * for each function we find. We call the callback
2076 * to give the caller an opportunity to do so.
2078 if (ops->callback) {
2079 if (ops->callback(rec->ip, &entry->data) < 0) {
2080 /* caller does not like this func */
2087 entry->ip = rec->ip;
2089 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2090 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2092 } while_for_each_ftrace_rec();
2093 __enable_ftrace_function_probe();
2096 mutex_unlock(&ftrace_lock);
2102 PROBE_TEST_FUNC = 1,
2107 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2108 void *data, int flags)
2110 struct ftrace_func_probe *entry;
2111 struct hlist_node *n, *tmp;
2112 char str[KSYM_SYMBOL_LEN];
2113 int type = MATCH_FULL;
2117 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2122 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2123 len = strlen(search);
2125 /* we do not support '!' for function probes */
2130 mutex_lock(&ftrace_lock);
2131 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2132 struct hlist_head *hhd = &ftrace_func_hash[i];
2134 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2136 /* break up if statements for readability */
2137 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2140 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2143 /* do this last, since it is the most expensive */
2145 kallsyms_lookup(entry->ip, NULL, NULL,
2147 if (!ftrace_match(str, glob, len, type))
2151 hlist_del(&entry->node);
2152 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2155 __disable_ftrace_function_probe();
2156 mutex_unlock(&ftrace_lock);
2160 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2163 __unregister_ftrace_function_probe(glob, ops, data,
2164 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2168 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2170 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2173 void unregister_ftrace_function_probe_all(char *glob)
2175 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2178 static LIST_HEAD(ftrace_commands);
2179 static DEFINE_MUTEX(ftrace_cmd_mutex);
2181 int register_ftrace_command(struct ftrace_func_command *cmd)
2183 struct ftrace_func_command *p;
2186 mutex_lock(&ftrace_cmd_mutex);
2187 list_for_each_entry(p, &ftrace_commands, list) {
2188 if (strcmp(cmd->name, p->name) == 0) {
2193 list_add(&cmd->list, &ftrace_commands);
2195 mutex_unlock(&ftrace_cmd_mutex);
2200 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2202 struct ftrace_func_command *p, *n;
2205 mutex_lock(&ftrace_cmd_mutex);
2206 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2207 if (strcmp(cmd->name, p->name) == 0) {
2209 list_del_init(&p->list);
2214 mutex_unlock(&ftrace_cmd_mutex);
2219 static int ftrace_process_regex(char *buff, int len, int enable)
2221 char *func, *command, *next = buff;
2222 struct ftrace_func_command *p;
2225 func = strsep(&next, ":");
2228 ftrace_match_records(func, len, enable);
2234 command = strsep(&next, ":");
2236 mutex_lock(&ftrace_cmd_mutex);
2237 list_for_each_entry(p, &ftrace_commands, list) {
2238 if (strcmp(p->name, command) == 0) {
2239 ret = p->func(func, command, next, enable);
2244 mutex_unlock(&ftrace_cmd_mutex);
2250 ftrace_regex_write(struct file *file, const char __user *ubuf,
2251 size_t cnt, loff_t *ppos, int enable)
2253 struct ftrace_iterator *iter;
2258 if (!cnt || cnt < 0)
2261 mutex_lock(&ftrace_regex_lock);
2263 if (file->f_mode & FMODE_READ) {
2264 struct seq_file *m = file->private_data;
2267 iter = file->private_data;
2270 iter->flags &= ~FTRACE_ITER_CONT;
2271 iter->buffer_idx = 0;
2274 ret = get_user(ch, ubuf++);
2280 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2281 /* skip white space */
2282 while (cnt && isspace(ch)) {
2283 ret = get_user(ch, ubuf++);
2291 file->f_pos += read;
2296 iter->buffer_idx = 0;
2299 while (cnt && !isspace(ch)) {
2300 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2301 iter->buffer[iter->buffer_idx++] = ch;
2306 ret = get_user(ch, ubuf++);
2315 iter->buffer[iter->buffer_idx] = 0;
2316 ret = ftrace_process_regex(iter->buffer,
2317 iter->buffer_idx, enable);
2320 iter->buffer_idx = 0;
2322 iter->flags |= FTRACE_ITER_CONT;
2325 file->f_pos += read;
2329 mutex_unlock(&ftrace_regex_lock);
2335 ftrace_filter_write(struct file *file, const char __user *ubuf,
2336 size_t cnt, loff_t *ppos)
2338 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2342 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2343 size_t cnt, loff_t *ppos)
2345 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2349 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2351 if (unlikely(ftrace_disabled))
2354 mutex_lock(&ftrace_regex_lock);
2356 ftrace_filter_reset(enable);
2358 ftrace_match_records(buf, len, enable);
2359 mutex_unlock(&ftrace_regex_lock);
2363 * ftrace_set_filter - set a function to filter on in ftrace
2364 * @buf - the string that holds the function filter text.
2365 * @len - the length of the string.
2366 * @reset - non zero to reset all filters before applying this filter.
2368 * Filters denote which functions should be enabled when tracing is enabled.
2369 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2371 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2373 ftrace_set_regex(buf, len, reset, 1);
2377 * ftrace_set_notrace - set a function to not trace in ftrace
2378 * @buf - the string that holds the function notrace text.
2379 * @len - the length of the string.
2380 * @reset - non zero to reset all filters before applying this filter.
2382 * Notrace Filters denote which functions should not be enabled when tracing
2383 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2386 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2388 ftrace_set_regex(buf, len, reset, 0);
2392 * command line interface to allow users to set filters on boot up.
2394 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2395 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2396 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2398 static int __init set_ftrace_notrace(char *str)
2400 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2403 __setup("ftrace_notrace=", set_ftrace_notrace);
2405 static int __init set_ftrace_filter(char *str)
2407 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2410 __setup("ftrace_filter=", set_ftrace_filter);
2412 static void __init set_ftrace_early_filter(char *buf, int enable)
2417 func = strsep(&buf, ",");
2418 ftrace_set_regex(func, strlen(func), 0, enable);
2422 static void __init set_ftrace_early_filters(void)
2424 if (ftrace_filter_buf[0])
2425 set_ftrace_early_filter(ftrace_filter_buf, 1);
2426 if (ftrace_notrace_buf[0])
2427 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2431 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2433 struct seq_file *m = (struct seq_file *)file->private_data;
2434 struct ftrace_iterator *iter;
2436 mutex_lock(&ftrace_regex_lock);
2437 if (file->f_mode & FMODE_READ) {
2440 seq_release(inode, file);
2442 iter = file->private_data;
2444 if (iter->buffer_idx) {
2446 iter->buffer[iter->buffer_idx] = 0;
2447 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
2450 mutex_lock(&ftrace_lock);
2451 if (ftrace_start_up && ftrace_enabled)
2452 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2453 mutex_unlock(&ftrace_lock);
2456 mutex_unlock(&ftrace_regex_lock);
2461 ftrace_filter_release(struct inode *inode, struct file *file)
2463 return ftrace_regex_release(inode, file, 1);
2467 ftrace_notrace_release(struct inode *inode, struct file *file)
2469 return ftrace_regex_release(inode, file, 0);
2472 static const struct file_operations ftrace_avail_fops = {
2473 .open = ftrace_avail_open,
2475 .llseek = seq_lseek,
2476 .release = ftrace_avail_release,
2479 static const struct file_operations ftrace_failures_fops = {
2480 .open = ftrace_failures_open,
2482 .llseek = seq_lseek,
2483 .release = ftrace_avail_release,
2486 static const struct file_operations ftrace_filter_fops = {
2487 .open = ftrace_filter_open,
2489 .write = ftrace_filter_write,
2490 .llseek = ftrace_regex_lseek,
2491 .release = ftrace_filter_release,
2494 static const struct file_operations ftrace_notrace_fops = {
2495 .open = ftrace_notrace_open,
2497 .write = ftrace_notrace_write,
2498 .llseek = ftrace_regex_lseek,
2499 .release = ftrace_notrace_release,
2502 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2504 static DEFINE_MUTEX(graph_lock);
2506 int ftrace_graph_count;
2507 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2510 __g_next(struct seq_file *m, loff_t *pos)
2512 unsigned long *array = m->private;
2514 if (*pos >= ftrace_graph_count)
2516 return &array[*pos];
2520 g_next(struct seq_file *m, void *v, loff_t *pos)
2523 return __g_next(m, pos);
2526 static void *g_start(struct seq_file *m, loff_t *pos)
2528 mutex_lock(&graph_lock);
2530 /* Nothing, tell g_show to print all functions are enabled */
2531 if (!ftrace_graph_count && !*pos)
2534 return __g_next(m, pos);
2537 static void g_stop(struct seq_file *m, void *p)
2539 mutex_unlock(&graph_lock);
2542 static int g_show(struct seq_file *m, void *v)
2544 unsigned long *ptr = v;
2545 char str[KSYM_SYMBOL_LEN];
2550 if (ptr == (unsigned long *)1) {
2551 seq_printf(m, "#### all functions enabled ####\n");
2555 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2557 seq_printf(m, "%s\n", str);
2562 static struct seq_operations ftrace_graph_seq_ops = {
2570 ftrace_graph_open(struct inode *inode, struct file *file)
2574 if (unlikely(ftrace_disabled))
2577 mutex_lock(&graph_lock);
2578 if ((file->f_mode & FMODE_WRITE) &&
2579 !(file->f_flags & O_APPEND)) {
2580 ftrace_graph_count = 0;
2581 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2584 if (file->f_mode & FMODE_READ) {
2585 ret = seq_open(file, &ftrace_graph_seq_ops);
2587 struct seq_file *m = file->private_data;
2588 m->private = ftrace_graph_funcs;
2591 file->private_data = ftrace_graph_funcs;
2592 mutex_unlock(&graph_lock);
2598 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2600 struct dyn_ftrace *rec;
2601 struct ftrace_page *pg;
2609 if (ftrace_disabled)
2613 type = ftrace_setup_glob(buffer, strlen(buffer), &search, ¬);
2617 search_len = strlen(search);
2619 mutex_lock(&ftrace_lock);
2620 do_for_each_ftrace_rec(pg, rec) {
2622 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2625 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2628 if (ftrace_match_record(rec, search, search_len, type)) {
2629 /* ensure it is not already in the array */
2631 for (i = 0; i < *idx; i++)
2632 if (array[i] == rec->ip) {
2637 array[(*idx)++] = rec->ip;
2641 } while_for_each_ftrace_rec();
2643 mutex_unlock(&ftrace_lock);
2645 return found ? 0 : -EINVAL;
2649 ftrace_graph_write(struct file *file, const char __user *ubuf,
2650 size_t cnt, loff_t *ppos)
2652 unsigned char buffer[FTRACE_BUFF_MAX+1];
2653 unsigned long *array;
2659 if (!cnt || cnt < 0)
2662 mutex_lock(&graph_lock);
2664 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2669 if (file->f_mode & FMODE_READ) {
2670 struct seq_file *m = file->private_data;
2673 array = file->private_data;
2675 ret = get_user(ch, ubuf++);
2681 /* skip white space */
2682 while (cnt && isspace(ch)) {
2683 ret = get_user(ch, ubuf++);
2696 while (cnt && !isspace(ch)) {
2697 if (index < FTRACE_BUFF_MAX)
2698 buffer[index++] = ch;
2703 ret = get_user(ch, ubuf++);
2711 /* we allow only one expression at a time */
2712 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2716 file->f_pos += read;
2720 mutex_unlock(&graph_lock);
2725 static const struct file_operations ftrace_graph_fops = {
2726 .open = ftrace_graph_open,
2728 .write = ftrace_graph_write,
2730 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2732 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2735 trace_create_file("available_filter_functions", 0444,
2736 d_tracer, NULL, &ftrace_avail_fops);
2738 trace_create_file("failures", 0444,
2739 d_tracer, NULL, &ftrace_failures_fops);
2741 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2742 NULL, &ftrace_filter_fops);
2744 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2745 NULL, &ftrace_notrace_fops);
2747 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2748 trace_create_file("set_graph_function", 0444, d_tracer,
2750 &ftrace_graph_fops);
2751 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2756 static int ftrace_convert_nops(struct module *mod,
2757 unsigned long *start,
2762 unsigned long flags;
2764 mutex_lock(&ftrace_lock);
2767 addr = ftrace_call_adjust(*p++);
2769 * Some architecture linkers will pad between
2770 * the different mcount_loc sections of different
2771 * object files to satisfy alignments.
2772 * Skip any NULL pointers.
2776 ftrace_record_ip(addr);
2779 /* disable interrupts to prevent kstop machine */
2780 local_irq_save(flags);
2781 ftrace_update_code(mod);
2782 local_irq_restore(flags);
2783 mutex_unlock(&ftrace_lock);
2788 #ifdef CONFIG_MODULES
2789 void ftrace_release(void *start, void *end)
2791 struct dyn_ftrace *rec;
2792 struct ftrace_page *pg;
2793 unsigned long s = (unsigned long)start;
2794 unsigned long e = (unsigned long)end;
2796 if (ftrace_disabled || !start || start == end)
2799 mutex_lock(&ftrace_lock);
2800 do_for_each_ftrace_rec(pg, rec) {
2801 if ((rec->ip >= s) && (rec->ip < e)) {
2803 * rec->ip is changed in ftrace_free_rec()
2804 * It should not between s and e if record was freed.
2806 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2807 ftrace_free_rec(rec);
2809 } while_for_each_ftrace_rec();
2810 mutex_unlock(&ftrace_lock);
2813 static void ftrace_init_module(struct module *mod,
2814 unsigned long *start, unsigned long *end)
2816 if (ftrace_disabled || start == end)
2818 ftrace_convert_nops(mod, start, end);
2821 static int ftrace_module_notify(struct notifier_block *self,
2822 unsigned long val, void *data)
2824 struct module *mod = data;
2827 case MODULE_STATE_COMING:
2828 ftrace_init_module(mod, mod->ftrace_callsites,
2829 mod->ftrace_callsites +
2830 mod->num_ftrace_callsites);
2832 case MODULE_STATE_GOING:
2833 ftrace_release(mod->ftrace_callsites,
2834 mod->ftrace_callsites +
2835 mod->num_ftrace_callsites);
2842 static int ftrace_module_notify(struct notifier_block *self,
2843 unsigned long val, void *data)
2847 #endif /* CONFIG_MODULES */
2849 struct notifier_block ftrace_module_nb = {
2850 .notifier_call = ftrace_module_notify,
2854 extern unsigned long __start_mcount_loc[];
2855 extern unsigned long __stop_mcount_loc[];
2857 void __init ftrace_init(void)
2859 unsigned long count, addr, flags;
2862 /* Keep the ftrace pointer to the stub */
2863 addr = (unsigned long)ftrace_stub;
2865 local_irq_save(flags);
2866 ftrace_dyn_arch_init(&addr);
2867 local_irq_restore(flags);
2869 /* ftrace_dyn_arch_init places the return code in addr */
2873 count = __stop_mcount_loc - __start_mcount_loc;
2875 ret = ftrace_dyn_table_alloc(count);
2879 last_ftrace_enabled = ftrace_enabled = 1;
2881 ret = ftrace_convert_nops(NULL,
2885 ret = register_module_notifier(&ftrace_module_nb);
2887 pr_warning("Failed to register trace ftrace module notifier\n");
2889 set_ftrace_early_filters();
2893 ftrace_disabled = 1;
2898 static int __init ftrace_nodyn_init(void)
2903 device_initcall(ftrace_nodyn_init);
2905 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2906 static inline void ftrace_startup_enable(int command) { }
2907 /* Keep as macros so we do not need to define the commands */
2908 # define ftrace_startup(command) do { } while (0)
2909 # define ftrace_shutdown(command) do { } while (0)
2910 # define ftrace_startup_sysctl() do { } while (0)
2911 # define ftrace_shutdown_sysctl() do { } while (0)
2912 #endif /* CONFIG_DYNAMIC_FTRACE */
2915 ftrace_pid_read(struct file *file, char __user *ubuf,
2916 size_t cnt, loff_t *ppos)
2921 if (ftrace_pid_trace == ftrace_swapper_pid)
2922 r = sprintf(buf, "swapper tasks\n");
2923 else if (ftrace_pid_trace)
2924 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2926 r = sprintf(buf, "no pid\n");
2928 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2931 static void clear_ftrace_swapper(void)
2933 struct task_struct *p;
2937 for_each_online_cpu(cpu) {
2939 clear_tsk_trace_trace(p);
2944 static void set_ftrace_swapper(void)
2946 struct task_struct *p;
2950 for_each_online_cpu(cpu) {
2952 set_tsk_trace_trace(p);
2957 static void clear_ftrace_pid(struct pid *pid)
2959 struct task_struct *p;
2962 do_each_pid_task(pid, PIDTYPE_PID, p) {
2963 clear_tsk_trace_trace(p);
2964 } while_each_pid_task(pid, PIDTYPE_PID, p);
2970 static void set_ftrace_pid(struct pid *pid)
2972 struct task_struct *p;
2975 do_each_pid_task(pid, PIDTYPE_PID, p) {
2976 set_tsk_trace_trace(p);
2977 } while_each_pid_task(pid, PIDTYPE_PID, p);
2981 static void clear_ftrace_pid_task(struct pid **pid)
2983 if (*pid == ftrace_swapper_pid)
2984 clear_ftrace_swapper();
2986 clear_ftrace_pid(*pid);
2991 static void set_ftrace_pid_task(struct pid *pid)
2993 if (pid == ftrace_swapper_pid)
2994 set_ftrace_swapper();
2996 set_ftrace_pid(pid);
3000 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3001 size_t cnt, loff_t *ppos)
3008 if (cnt >= sizeof(buf))
3011 if (copy_from_user(&buf, ubuf, cnt))
3016 ret = strict_strtol(buf, 10, &val);
3020 mutex_lock(&ftrace_lock);
3022 /* disable pid tracing */
3023 if (!ftrace_pid_trace)
3026 clear_ftrace_pid_task(&ftrace_pid_trace);
3029 /* swapper task is special */
3031 pid = ftrace_swapper_pid;
3032 if (pid == ftrace_pid_trace)
3035 pid = find_get_pid(val);
3037 if (pid == ftrace_pid_trace) {
3043 if (ftrace_pid_trace)
3044 clear_ftrace_pid_task(&ftrace_pid_trace);
3049 ftrace_pid_trace = pid;
3051 set_ftrace_pid_task(ftrace_pid_trace);
3054 /* update the function call */
3055 ftrace_update_pid_func();
3056 ftrace_startup_enable(0);
3059 mutex_unlock(&ftrace_lock);
3064 static const struct file_operations ftrace_pid_fops = {
3065 .read = ftrace_pid_read,
3066 .write = ftrace_pid_write,
3069 static __init int ftrace_init_debugfs(void)
3071 struct dentry *d_tracer;
3073 d_tracer = tracing_init_dentry();
3077 ftrace_init_dyn_debugfs(d_tracer);
3079 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3080 NULL, &ftrace_pid_fops);
3082 ftrace_profile_debugfs(d_tracer);
3086 fs_initcall(ftrace_init_debugfs);
3089 * ftrace_kill - kill ftrace
3091 * This function should be used by panic code. It stops ftrace
3092 * but in a not so nice way. If you need to simply kill ftrace
3093 * from a non-atomic section, use ftrace_kill.
3095 void ftrace_kill(void)
3097 ftrace_disabled = 1;
3099 clear_ftrace_function();
3103 * register_ftrace_function - register a function for profiling
3104 * @ops - ops structure that holds the function for profiling.
3106 * Register a function to be called by all functions in the
3109 * Note: @ops->func and all the functions it calls must be labeled
3110 * with "notrace", otherwise it will go into a
3113 int register_ftrace_function(struct ftrace_ops *ops)
3117 if (unlikely(ftrace_disabled))
3120 mutex_lock(&ftrace_lock);
3122 ret = __register_ftrace_function(ops);
3125 mutex_unlock(&ftrace_lock);
3130 * unregister_ftrace_function - unregister a function for profiling.
3131 * @ops - ops structure that holds the function to unregister
3133 * Unregister a function that was added to be called by ftrace profiling.
3135 int unregister_ftrace_function(struct ftrace_ops *ops)
3139 mutex_lock(&ftrace_lock);
3140 ret = __unregister_ftrace_function(ops);
3142 mutex_unlock(&ftrace_lock);
3148 ftrace_enable_sysctl(struct ctl_table *table, int write,
3149 struct file *file, void __user *buffer, size_t *lenp,
3154 if (unlikely(ftrace_disabled))
3157 mutex_lock(&ftrace_lock);
3159 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
3161 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
3164 last_ftrace_enabled = ftrace_enabled;
3166 if (ftrace_enabled) {
3168 ftrace_startup_sysctl();
3170 /* we are starting ftrace again */
3171 if (ftrace_list != &ftrace_list_end) {
3172 if (ftrace_list->next == &ftrace_list_end)
3173 ftrace_trace_function = ftrace_list->func;
3175 ftrace_trace_function = ftrace_list_func;
3179 /* stopping ftrace calls (just send to ftrace_stub) */
3180 ftrace_trace_function = ftrace_stub;
3182 ftrace_shutdown_sysctl();
3186 mutex_unlock(&ftrace_lock);
3190 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3192 static int ftrace_graph_active;
3193 static struct notifier_block ftrace_suspend_notifier;
3195 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3200 /* The callbacks that hook a function */
3201 trace_func_graph_ret_t ftrace_graph_return =
3202 (trace_func_graph_ret_t)ftrace_stub;
3203 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3205 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3206 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3210 unsigned long flags;
3211 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3212 struct task_struct *g, *t;
3214 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3215 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3216 * sizeof(struct ftrace_ret_stack),
3218 if (!ret_stack_list[i]) {
3226 read_lock_irqsave(&tasklist_lock, flags);
3227 do_each_thread(g, t) {
3233 if (t->ret_stack == NULL) {
3234 atomic_set(&t->tracing_graph_pause, 0);
3235 atomic_set(&t->trace_overrun, 0);
3236 t->curr_ret_stack = -1;
3237 /* Make sure the tasks see the -1 first: */
3239 t->ret_stack = ret_stack_list[start++];
3241 } while_each_thread(g, t);
3244 read_unlock_irqrestore(&tasklist_lock, flags);
3246 for (i = start; i < end; i++)
3247 kfree(ret_stack_list[i]);
3252 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3253 struct task_struct *next)
3255 unsigned long long timestamp;
3259 * Does the user want to count the time a function was asleep.
3260 * If so, do not update the time stamps.
3262 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3265 timestamp = trace_clock_local();
3267 prev->ftrace_timestamp = timestamp;
3269 /* only process tasks that we timestamped */
3270 if (!next->ftrace_timestamp)
3274 * Update all the counters in next to make up for the
3275 * time next was sleeping.
3277 timestamp -= next->ftrace_timestamp;
3279 for (index = next->curr_ret_stack; index >= 0; index--)
3280 next->ret_stack[index].calltime += timestamp;
3283 /* Allocate a return stack for each task */
3284 static int start_graph_tracing(void)
3286 struct ftrace_ret_stack **ret_stack_list;
3289 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3290 sizeof(struct ftrace_ret_stack *),
3293 if (!ret_stack_list)
3296 /* The cpu_boot init_task->ret_stack will never be freed */
3297 for_each_online_cpu(cpu) {
3298 if (!idle_task(cpu)->ret_stack)
3299 ftrace_graph_init_task(idle_task(cpu));
3303 ret = alloc_retstack_tasklist(ret_stack_list);
3304 } while (ret == -EAGAIN);
3307 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3309 pr_info("ftrace_graph: Couldn't activate tracepoint"
3310 " probe to kernel_sched_switch\n");
3313 kfree(ret_stack_list);
3318 * Hibernation protection.
3319 * The state of the current task is too much unstable during
3320 * suspend/restore to disk. We want to protect against that.
3323 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3327 case PM_HIBERNATION_PREPARE:
3328 pause_graph_tracing();
3331 case PM_POST_HIBERNATION:
3332 unpause_graph_tracing();
3338 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3339 trace_func_graph_ent_t entryfunc)
3343 mutex_lock(&ftrace_lock);
3345 /* we currently allow only one tracer registered at a time */
3346 if (ftrace_graph_active) {
3351 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3352 register_pm_notifier(&ftrace_suspend_notifier);
3354 ftrace_graph_active++;
3355 ret = start_graph_tracing();
3357 ftrace_graph_active--;
3361 ftrace_graph_return = retfunc;
3362 ftrace_graph_entry = entryfunc;
3364 ftrace_startup(FTRACE_START_FUNC_RET);
3367 mutex_unlock(&ftrace_lock);
3371 void unregister_ftrace_graph(void)
3373 mutex_lock(&ftrace_lock);
3375 if (unlikely(!ftrace_graph_active))
3378 ftrace_graph_active--;
3379 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3380 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3381 ftrace_graph_entry = ftrace_graph_entry_stub;
3382 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3383 unregister_pm_notifier(&ftrace_suspend_notifier);
3386 mutex_unlock(&ftrace_lock);
3389 /* Allocate a return stack for newly created task */
3390 void ftrace_graph_init_task(struct task_struct *t)
3392 /* Make sure we do not use the parent ret_stack */
3393 t->ret_stack = NULL;
3395 if (ftrace_graph_active) {
3396 struct ftrace_ret_stack *ret_stack;
3398 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3399 * sizeof(struct ftrace_ret_stack),
3403 t->curr_ret_stack = -1;
3404 atomic_set(&t->tracing_graph_pause, 0);
3405 atomic_set(&t->trace_overrun, 0);
3406 t->ftrace_timestamp = 0;
3407 /* make curr_ret_stack visable before we add the ret_stack */
3409 t->ret_stack = ret_stack;
3413 void ftrace_graph_exit_task(struct task_struct *t)
3415 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3417 t->ret_stack = NULL;
3418 /* NULL must become visible to IRQs before we free it: */
3424 void ftrace_graph_stop(void)