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);
297 if ((void *)rec >= (void *)&pg->records[pg->index]) {
301 rec = &pg->records[0];
309 static void *function_stat_start(struct tracer_stat *trace)
311 struct ftrace_profile_stat *stat =
312 container_of(trace, struct ftrace_profile_stat, stat);
314 if (!stat || !stat->start)
317 return function_stat_next(&stat->start->records[0], 0);
320 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
321 /* function graph compares on total time */
322 static int function_stat_cmp(void *p1, void *p2)
324 struct ftrace_profile *a = p1;
325 struct ftrace_profile *b = p2;
327 if (a->time < b->time)
329 if (a->time > b->time)
335 /* not function graph compares against hits */
336 static int function_stat_cmp(void *p1, void *p2)
338 struct ftrace_profile *a = p1;
339 struct ftrace_profile *b = p2;
341 if (a->counter < b->counter)
343 if (a->counter > b->counter)
350 static int function_stat_headers(struct seq_file *m)
352 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
353 seq_printf(m, " Function "
358 seq_printf(m, " Function Hit\n"
364 static int function_stat_show(struct seq_file *m, void *v)
366 struct ftrace_profile *rec = v;
367 char str[KSYM_SYMBOL_LEN];
368 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
369 static DEFINE_MUTEX(mutex);
370 static struct trace_seq s;
371 unsigned long long avg;
374 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
375 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
377 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
380 do_div(avg, rec->counter);
384 trace_print_graph_duration(rec->time, &s);
385 trace_seq_puts(&s, " ");
386 trace_print_graph_duration(avg, &s);
387 trace_print_seq(m, &s);
388 mutex_unlock(&mutex);
395 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
397 struct ftrace_profile_page *pg;
399 pg = stat->pages = stat->start;
402 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
407 memset(stat->hash, 0,
408 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
411 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
413 struct ftrace_profile_page *pg;
418 /* If we already allocated, do nothing */
422 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
426 #ifdef CONFIG_DYNAMIC_FTRACE
427 functions = ftrace_update_tot_cnt;
430 * We do not know the number of functions that exist because
431 * dynamic tracing is what counts them. With past experience
432 * we have around 20K functions. That should be more than enough.
433 * It is highly unlikely we will execute every function in
439 pg = stat->start = stat->pages;
441 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
443 for (i = 0; i < pages; i++) {
444 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
455 unsigned long tmp = (unsigned long)pg;
461 free_page((unsigned long)stat->pages);
468 static int ftrace_profile_init_cpu(int cpu)
470 struct ftrace_profile_stat *stat;
473 stat = &per_cpu(ftrace_profile_stats, cpu);
476 /* If the profile is already created, simply reset it */
477 ftrace_profile_reset(stat);
482 * We are profiling all functions, but usually only a few thousand
483 * functions are hit. We'll make a hash of 1024 items.
485 size = FTRACE_PROFILE_HASH_SIZE;
487 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
492 if (!ftrace_profile_bits) {
495 for (; size; size >>= 1)
496 ftrace_profile_bits++;
499 /* Preallocate the function profiling pages */
500 if (ftrace_profile_pages_init(stat) < 0) {
509 static int ftrace_profile_init(void)
514 for_each_online_cpu(cpu) {
515 ret = ftrace_profile_init_cpu(cpu);
523 /* interrupts must be disabled */
524 static struct ftrace_profile *
525 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
527 struct ftrace_profile *rec;
528 struct hlist_head *hhd;
529 struct hlist_node *n;
532 key = hash_long(ip, ftrace_profile_bits);
533 hhd = &stat->hash[key];
535 if (hlist_empty(hhd))
538 hlist_for_each_entry_rcu(rec, n, hhd, node) {
546 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
547 struct ftrace_profile *rec)
551 key = hash_long(rec->ip, ftrace_profile_bits);
552 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
556 * The memory is already allocated, this simply finds a new record to use.
558 static struct ftrace_profile *
559 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
561 struct ftrace_profile *rec = NULL;
563 /* prevent recursion (from NMIs) */
564 if (atomic_inc_return(&stat->disabled) != 1)
568 * Try to find the function again since an NMI
569 * could have added it
571 rec = ftrace_find_profiled_func(stat, ip);
575 if (stat->pages->index == PROFILES_PER_PAGE) {
576 if (!stat->pages->next)
578 stat->pages = stat->pages->next;
581 rec = &stat->pages->records[stat->pages->index++];
583 ftrace_add_profile(stat, rec);
586 atomic_dec(&stat->disabled);
592 function_profile_call(unsigned long ip, unsigned long parent_ip)
594 struct ftrace_profile_stat *stat;
595 struct ftrace_profile *rec;
598 if (!ftrace_profile_enabled)
601 local_irq_save(flags);
603 stat = &__get_cpu_var(ftrace_profile_stats);
604 if (!stat->hash || !ftrace_profile_enabled)
607 rec = ftrace_find_profiled_func(stat, ip);
609 rec = ftrace_profile_alloc(stat, ip);
616 local_irq_restore(flags);
619 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
620 static int profile_graph_entry(struct ftrace_graph_ent *trace)
622 function_profile_call(trace->func, 0);
626 static void profile_graph_return(struct ftrace_graph_ret *trace)
628 struct ftrace_profile_stat *stat;
629 unsigned long long calltime;
630 struct ftrace_profile *rec;
633 local_irq_save(flags);
634 stat = &__get_cpu_var(ftrace_profile_stats);
635 if (!stat->hash || !ftrace_profile_enabled)
638 calltime = trace->rettime - trace->calltime;
640 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
643 index = trace->depth;
645 /* Append this call time to the parent time to subtract */
647 current->ret_stack[index - 1].subtime += calltime;
649 if (current->ret_stack[index].subtime < calltime)
650 calltime -= current->ret_stack[index].subtime;
655 rec = ftrace_find_profiled_func(stat, trace->func);
657 rec->time += calltime;
660 local_irq_restore(flags);
663 static int register_ftrace_profiler(void)
665 return register_ftrace_graph(&profile_graph_return,
666 &profile_graph_entry);
669 static void unregister_ftrace_profiler(void)
671 unregister_ftrace_graph();
674 static struct ftrace_ops ftrace_profile_ops __read_mostly =
676 .func = function_profile_call,
679 static int register_ftrace_profiler(void)
681 return register_ftrace_function(&ftrace_profile_ops);
684 static void unregister_ftrace_profiler(void)
686 unregister_ftrace_function(&ftrace_profile_ops);
688 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
691 ftrace_profile_write(struct file *filp, const char __user *ubuf,
692 size_t cnt, loff_t *ppos)
695 char buf[64]; /* big enough to hold a number */
698 if (cnt >= sizeof(buf))
701 if (copy_from_user(&buf, ubuf, cnt))
706 ret = strict_strtoul(buf, 10, &val);
712 mutex_lock(&ftrace_profile_lock);
713 if (ftrace_profile_enabled ^ val) {
715 ret = ftrace_profile_init();
721 ret = register_ftrace_profiler();
726 ftrace_profile_enabled = 1;
728 ftrace_profile_enabled = 0;
730 * unregister_ftrace_profiler calls stop_machine
731 * so this acts like an synchronize_sched.
733 unregister_ftrace_profiler();
737 mutex_unlock(&ftrace_profile_lock);
745 ftrace_profile_read(struct file *filp, char __user *ubuf,
746 size_t cnt, loff_t *ppos)
748 char buf[64]; /* big enough to hold a number */
751 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
752 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
755 static const struct file_operations ftrace_profile_fops = {
756 .open = tracing_open_generic,
757 .read = ftrace_profile_read,
758 .write = ftrace_profile_write,
761 /* used to initialize the real stat files */
762 static struct tracer_stat function_stats __initdata = {
764 .stat_start = function_stat_start,
765 .stat_next = function_stat_next,
766 .stat_cmp = function_stat_cmp,
767 .stat_headers = function_stat_headers,
768 .stat_show = function_stat_show
771 static void ftrace_profile_debugfs(struct dentry *d_tracer)
773 struct ftrace_profile_stat *stat;
774 struct dentry *entry;
779 for_each_possible_cpu(cpu) {
780 stat = &per_cpu(ftrace_profile_stats, cpu);
782 /* allocate enough for function name + cpu number */
783 name = kmalloc(32, GFP_KERNEL);
786 * The files created are permanent, if something happens
787 * we still do not free memory.
791 "Could not allocate stat file for cpu %d\n",
795 stat->stat = function_stats;
796 snprintf(name, 32, "function%d", cpu);
797 stat->stat.name = name;
798 ret = register_stat_tracer(&stat->stat);
801 "Could not register function stat for cpu %d\n",
808 entry = debugfs_create_file("function_profile_enabled", 0644,
809 d_tracer, NULL, &ftrace_profile_fops);
811 pr_warning("Could not create debugfs "
812 "'function_profile_enabled' entry\n");
815 #else /* CONFIG_FUNCTION_PROFILER */
816 static void ftrace_profile_debugfs(struct dentry *d_tracer)
819 #endif /* CONFIG_FUNCTION_PROFILER */
821 /* set when tracing only a pid */
822 struct pid *ftrace_pid_trace;
823 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
825 #ifdef CONFIG_DYNAMIC_FTRACE
827 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
828 # error Dynamic ftrace depends on MCOUNT_RECORD
831 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
833 struct ftrace_func_probe {
834 struct hlist_node node;
835 struct ftrace_probe_ops *ops;
843 FTRACE_ENABLE_CALLS = (1 << 0),
844 FTRACE_DISABLE_CALLS = (1 << 1),
845 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
846 FTRACE_ENABLE_MCOUNT = (1 << 3),
847 FTRACE_DISABLE_MCOUNT = (1 << 4),
848 FTRACE_START_FUNC_RET = (1 << 5),
849 FTRACE_STOP_FUNC_RET = (1 << 6),
852 static int ftrace_filtered;
854 static struct dyn_ftrace *ftrace_new_addrs;
856 static DEFINE_MUTEX(ftrace_regex_lock);
859 struct ftrace_page *next;
861 struct dyn_ftrace records[];
864 #define ENTRIES_PER_PAGE \
865 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
867 /* estimate from running different kernels */
868 #define NR_TO_INIT 10000
870 static struct ftrace_page *ftrace_pages_start;
871 static struct ftrace_page *ftrace_pages;
873 static struct dyn_ftrace *ftrace_free_records;
876 * This is a double for. Do not use 'break' to break out of the loop,
877 * you must use a goto.
879 #define do_for_each_ftrace_rec(pg, rec) \
880 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
882 for (_____i = 0; _____i < pg->index; _____i++) { \
883 rec = &pg->records[_____i];
885 #define while_for_each_ftrace_rec() \
889 #ifdef CONFIG_KPROBES
891 static int frozen_record_count;
893 static inline void freeze_record(struct dyn_ftrace *rec)
895 if (!(rec->flags & FTRACE_FL_FROZEN)) {
896 rec->flags |= FTRACE_FL_FROZEN;
897 frozen_record_count++;
901 static inline void unfreeze_record(struct dyn_ftrace *rec)
903 if (rec->flags & FTRACE_FL_FROZEN) {
904 rec->flags &= ~FTRACE_FL_FROZEN;
905 frozen_record_count--;
909 static inline int record_frozen(struct dyn_ftrace *rec)
911 return rec->flags & FTRACE_FL_FROZEN;
914 # define freeze_record(rec) ({ 0; })
915 # define unfreeze_record(rec) ({ 0; })
916 # define record_frozen(rec) ({ 0; })
917 #endif /* CONFIG_KPROBES */
919 static void ftrace_free_rec(struct dyn_ftrace *rec)
921 rec->freelist = ftrace_free_records;
922 ftrace_free_records = rec;
923 rec->flags |= FTRACE_FL_FREE;
926 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
928 struct dyn_ftrace *rec;
930 /* First check for freed records */
931 if (ftrace_free_records) {
932 rec = ftrace_free_records;
934 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
935 FTRACE_WARN_ON_ONCE(1);
936 ftrace_free_records = NULL;
940 ftrace_free_records = rec->freelist;
941 memset(rec, 0, sizeof(*rec));
945 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
946 if (!ftrace_pages->next) {
947 /* allocate another page */
949 (void *)get_zeroed_page(GFP_KERNEL);
950 if (!ftrace_pages->next)
953 ftrace_pages = ftrace_pages->next;
956 return &ftrace_pages->records[ftrace_pages->index++];
959 static struct dyn_ftrace *
960 ftrace_record_ip(unsigned long ip)
962 struct dyn_ftrace *rec;
967 rec = ftrace_alloc_dyn_node(ip);
972 rec->newlist = ftrace_new_addrs;
973 ftrace_new_addrs = rec;
978 static void print_ip_ins(const char *fmt, unsigned char *p)
982 printk(KERN_CONT "%s", fmt);
984 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
985 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
988 static void ftrace_bug(int failed, unsigned long ip)
992 FTRACE_WARN_ON_ONCE(1);
993 pr_info("ftrace faulted on modifying ");
997 FTRACE_WARN_ON_ONCE(1);
998 pr_info("ftrace failed to modify ");
1000 print_ip_ins(" actual: ", (unsigned char *)ip);
1001 printk(KERN_CONT "\n");
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on writing ");
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace faulted on unknown error ");
1017 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1019 unsigned long ftrace_addr;
1020 unsigned long ip, fl;
1022 ftrace_addr = (unsigned long)FTRACE_ADDR;
1027 * If this record is not to be traced and
1028 * it is not enabled then do nothing.
1030 * If this record is not to be traced and
1031 * it is enabled then disable it.
1034 if (rec->flags & FTRACE_FL_NOTRACE) {
1035 if (rec->flags & FTRACE_FL_ENABLED)
1036 rec->flags &= ~FTRACE_FL_ENABLED;
1040 } else if (ftrace_filtered && enable) {
1045 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
1047 /* Record is filtered and enabled, do nothing */
1048 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
1051 /* Record is not filtered or enabled, do nothing */
1055 /* Record is not filtered but enabled, disable it */
1056 if (fl == FTRACE_FL_ENABLED)
1057 rec->flags &= ~FTRACE_FL_ENABLED;
1059 /* Otherwise record is filtered but not enabled, enable it */
1060 rec->flags |= FTRACE_FL_ENABLED;
1062 /* Disable or not filtered */
1065 /* if record is enabled, do nothing */
1066 if (rec->flags & FTRACE_FL_ENABLED)
1069 rec->flags |= FTRACE_FL_ENABLED;
1073 /* if record is not enabled, do nothing */
1074 if (!(rec->flags & FTRACE_FL_ENABLED))
1077 rec->flags &= ~FTRACE_FL_ENABLED;
1081 if (rec->flags & FTRACE_FL_ENABLED)
1082 return ftrace_make_call(rec, ftrace_addr);
1084 return ftrace_make_nop(NULL, rec, ftrace_addr);
1087 static void ftrace_replace_code(int enable)
1089 struct dyn_ftrace *rec;
1090 struct ftrace_page *pg;
1093 do_for_each_ftrace_rec(pg, rec) {
1095 * Skip over free records, records that have
1096 * failed and not converted.
1098 if (rec->flags & FTRACE_FL_FREE ||
1099 rec->flags & FTRACE_FL_FAILED ||
1100 !(rec->flags & FTRACE_FL_CONVERTED))
1103 /* ignore updates to this record's mcount site */
1104 if (get_kprobe((void *)rec->ip)) {
1108 unfreeze_record(rec);
1111 failed = __ftrace_replace_code(rec, enable);
1113 rec->flags |= FTRACE_FL_FAILED;
1114 if ((system_state == SYSTEM_BOOTING) ||
1115 !core_kernel_text(rec->ip)) {
1116 ftrace_free_rec(rec);
1118 ftrace_bug(failed, rec->ip);
1119 /* Stop processing */
1123 } while_for_each_ftrace_rec();
1127 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1134 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1136 ftrace_bug(ret, ip);
1137 rec->flags |= FTRACE_FL_FAILED;
1144 * archs can override this function if they must do something
1145 * before the modifying code is performed.
1147 int __weak ftrace_arch_code_modify_prepare(void)
1153 * archs can override this function if they must do something
1154 * after the modifying code is performed.
1156 int __weak ftrace_arch_code_modify_post_process(void)
1161 static int __ftrace_modify_code(void *data)
1163 int *command = data;
1165 if (*command & FTRACE_ENABLE_CALLS)
1166 ftrace_replace_code(1);
1167 else if (*command & FTRACE_DISABLE_CALLS)
1168 ftrace_replace_code(0);
1170 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1171 ftrace_update_ftrace_func(ftrace_trace_function);
1173 if (*command & FTRACE_START_FUNC_RET)
1174 ftrace_enable_ftrace_graph_caller();
1175 else if (*command & FTRACE_STOP_FUNC_RET)
1176 ftrace_disable_ftrace_graph_caller();
1181 static void ftrace_run_update_code(int command)
1185 ret = ftrace_arch_code_modify_prepare();
1186 FTRACE_WARN_ON(ret);
1190 stop_machine(__ftrace_modify_code, &command, NULL);
1192 ret = ftrace_arch_code_modify_post_process();
1193 FTRACE_WARN_ON(ret);
1196 static ftrace_func_t saved_ftrace_func;
1197 static int ftrace_start_up;
1199 static void ftrace_startup_enable(int command)
1201 if (saved_ftrace_func != ftrace_trace_function) {
1202 saved_ftrace_func = ftrace_trace_function;
1203 command |= FTRACE_UPDATE_TRACE_FUNC;
1206 if (!command || !ftrace_enabled)
1209 ftrace_run_update_code(command);
1212 static void ftrace_startup(int command)
1214 if (unlikely(ftrace_disabled))
1218 command |= FTRACE_ENABLE_CALLS;
1220 ftrace_startup_enable(command);
1223 static void ftrace_shutdown(int command)
1225 if (unlikely(ftrace_disabled))
1230 * Just warn in case of unbalance, no need to kill ftrace, it's not
1231 * critical but the ftrace_call callers may be never nopped again after
1232 * further ftrace uses.
1234 WARN_ON_ONCE(ftrace_start_up < 0);
1236 if (!ftrace_start_up)
1237 command |= FTRACE_DISABLE_CALLS;
1239 if (saved_ftrace_func != ftrace_trace_function) {
1240 saved_ftrace_func = ftrace_trace_function;
1241 command |= FTRACE_UPDATE_TRACE_FUNC;
1244 if (!command || !ftrace_enabled)
1247 ftrace_run_update_code(command);
1250 static void ftrace_startup_sysctl(void)
1252 int command = FTRACE_ENABLE_MCOUNT;
1254 if (unlikely(ftrace_disabled))
1257 /* Force update next time */
1258 saved_ftrace_func = NULL;
1259 /* ftrace_start_up is true if we want ftrace running */
1260 if (ftrace_start_up)
1261 command |= FTRACE_ENABLE_CALLS;
1263 ftrace_run_update_code(command);
1266 static void ftrace_shutdown_sysctl(void)
1268 int command = FTRACE_DISABLE_MCOUNT;
1270 if (unlikely(ftrace_disabled))
1273 /* ftrace_start_up is true if ftrace is running */
1274 if (ftrace_start_up)
1275 command |= FTRACE_DISABLE_CALLS;
1277 ftrace_run_update_code(command);
1280 static cycle_t ftrace_update_time;
1281 static unsigned long ftrace_update_cnt;
1282 unsigned long ftrace_update_tot_cnt;
1284 static int ftrace_update_code(struct module *mod)
1286 struct dyn_ftrace *p;
1287 cycle_t start, stop;
1289 start = ftrace_now(raw_smp_processor_id());
1290 ftrace_update_cnt = 0;
1292 while (ftrace_new_addrs) {
1294 /* If something went wrong, bail without enabling anything */
1295 if (unlikely(ftrace_disabled))
1298 p = ftrace_new_addrs;
1299 ftrace_new_addrs = p->newlist;
1302 /* convert record (i.e, patch mcount-call with NOP) */
1303 if (ftrace_code_disable(mod, p)) {
1304 p->flags |= FTRACE_FL_CONVERTED;
1305 ftrace_update_cnt++;
1310 stop = ftrace_now(raw_smp_processor_id());
1311 ftrace_update_time = stop - start;
1312 ftrace_update_tot_cnt += ftrace_update_cnt;
1317 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1319 struct ftrace_page *pg;
1323 /* allocate a few pages */
1324 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1325 if (!ftrace_pages_start)
1329 * Allocate a few more pages.
1331 * TODO: have some parser search vmlinux before
1332 * final linking to find all calls to ftrace.
1334 * a) know how many pages to allocate.
1336 * b) set up the table then.
1338 * The dynamic code is still necessary for
1342 pg = ftrace_pages = ftrace_pages_start;
1344 cnt = num_to_init / ENTRIES_PER_PAGE;
1345 pr_info("ftrace: allocating %ld entries in %d pages\n",
1346 num_to_init, cnt + 1);
1348 for (i = 0; i < cnt; i++) {
1349 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1351 /* If we fail, we'll try later anyway */
1362 FTRACE_ITER_FILTER = (1 << 0),
1363 FTRACE_ITER_CONT = (1 << 1),
1364 FTRACE_ITER_NOTRACE = (1 << 2),
1365 FTRACE_ITER_FAILURES = (1 << 3),
1366 FTRACE_ITER_PRINTALL = (1 << 4),
1367 FTRACE_ITER_HASH = (1 << 5),
1370 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1372 struct ftrace_iterator {
1373 struct ftrace_page *pg;
1377 unsigned char buffer[FTRACE_BUFF_MAX+1];
1378 unsigned buffer_idx;
1383 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1385 struct ftrace_iterator *iter = m->private;
1386 struct hlist_node *hnd = v;
1387 struct hlist_head *hhd;
1389 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1394 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1397 hhd = &ftrace_func_hash[iter->hidx];
1399 if (hlist_empty(hhd)) {
1418 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1420 struct ftrace_iterator *iter = m->private;
1424 if (!(iter->flags & FTRACE_ITER_HASH))
1427 iter->flags |= FTRACE_ITER_HASH;
1430 for (l = 0; l <= *pos; ) {
1431 p = t_hash_next(m, p, &l);
1438 static int t_hash_show(struct seq_file *m, void *v)
1440 struct ftrace_func_probe *rec;
1441 struct hlist_node *hnd = v;
1442 char str[KSYM_SYMBOL_LEN];
1444 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1446 if (rec->ops->print)
1447 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1449 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1450 seq_printf(m, "%s:", str);
1452 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1453 seq_printf(m, "%s", str);
1456 seq_printf(m, ":%p", rec->data);
1463 t_next(struct seq_file *m, void *v, loff_t *pos)
1465 struct ftrace_iterator *iter = m->private;
1466 struct dyn_ftrace *rec = NULL;
1468 if (iter->flags & FTRACE_ITER_HASH)
1469 return t_hash_next(m, v, pos);
1473 if (iter->flags & FTRACE_ITER_PRINTALL)
1477 if (iter->idx >= iter->pg->index) {
1478 if (iter->pg->next) {
1479 iter->pg = iter->pg->next;
1484 rec = &iter->pg->records[iter->idx++];
1485 if ((rec->flags & FTRACE_FL_FREE) ||
1487 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1488 (rec->flags & FTRACE_FL_FAILED)) ||
1490 ((iter->flags & FTRACE_ITER_FAILURES) &&
1491 !(rec->flags & FTRACE_FL_FAILED)) ||
1493 ((iter->flags & FTRACE_ITER_FILTER) &&
1494 !(rec->flags & FTRACE_FL_FILTER)) ||
1496 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1497 !(rec->flags & FTRACE_FL_NOTRACE))) {
1506 static void *t_start(struct seq_file *m, loff_t *pos)
1508 struct ftrace_iterator *iter = m->private;
1512 mutex_lock(&ftrace_lock);
1514 * For set_ftrace_filter reading, if we have the filter
1515 * off, we can short cut and just print out that all
1516 * functions are enabled.
1518 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1520 return t_hash_start(m, pos);
1521 iter->flags |= FTRACE_ITER_PRINTALL;
1525 if (iter->flags & FTRACE_ITER_HASH)
1526 return t_hash_start(m, pos);
1528 iter->pg = ftrace_pages_start;
1530 for (l = 0; l <= *pos; ) {
1531 p = t_next(m, p, &l);
1536 if (!p && iter->flags & FTRACE_ITER_FILTER)
1537 return t_hash_start(m, pos);
1542 static void t_stop(struct seq_file *m, void *p)
1544 mutex_unlock(&ftrace_lock);
1547 static int t_show(struct seq_file *m, void *v)
1549 struct ftrace_iterator *iter = m->private;
1550 struct dyn_ftrace *rec = v;
1551 char str[KSYM_SYMBOL_LEN];
1553 if (iter->flags & FTRACE_ITER_HASH)
1554 return t_hash_show(m, v);
1556 if (iter->flags & FTRACE_ITER_PRINTALL) {
1557 seq_printf(m, "#### all functions enabled ####\n");
1564 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1566 seq_printf(m, "%s\n", str);
1571 static struct seq_operations show_ftrace_seq_ops = {
1579 ftrace_avail_open(struct inode *inode, struct file *file)
1581 struct ftrace_iterator *iter;
1584 if (unlikely(ftrace_disabled))
1587 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1591 iter->pg = ftrace_pages_start;
1593 ret = seq_open(file, &show_ftrace_seq_ops);
1595 struct seq_file *m = file->private_data;
1605 int ftrace_avail_release(struct inode *inode, struct file *file)
1607 struct seq_file *m = (struct seq_file *)file->private_data;
1608 struct ftrace_iterator *iter = m->private;
1610 seq_release(inode, file);
1617 ftrace_failures_open(struct inode *inode, struct file *file)
1621 struct ftrace_iterator *iter;
1623 ret = ftrace_avail_open(inode, file);
1625 m = (struct seq_file *)file->private_data;
1626 iter = (struct ftrace_iterator *)m->private;
1627 iter->flags = FTRACE_ITER_FAILURES;
1634 static void ftrace_filter_reset(int enable)
1636 struct ftrace_page *pg;
1637 struct dyn_ftrace *rec;
1638 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1640 mutex_lock(&ftrace_lock);
1642 ftrace_filtered = 0;
1643 do_for_each_ftrace_rec(pg, rec) {
1644 if (rec->flags & FTRACE_FL_FAILED)
1646 rec->flags &= ~type;
1647 } while_for_each_ftrace_rec();
1648 mutex_unlock(&ftrace_lock);
1652 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1654 struct ftrace_iterator *iter;
1657 if (unlikely(ftrace_disabled))
1660 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1664 mutex_lock(&ftrace_regex_lock);
1665 if ((file->f_mode & FMODE_WRITE) &&
1666 !(file->f_flags & O_APPEND))
1667 ftrace_filter_reset(enable);
1669 if (file->f_mode & FMODE_READ) {
1670 iter->pg = ftrace_pages_start;
1671 iter->flags = enable ? FTRACE_ITER_FILTER :
1672 FTRACE_ITER_NOTRACE;
1674 ret = seq_open(file, &show_ftrace_seq_ops);
1676 struct seq_file *m = file->private_data;
1681 file->private_data = iter;
1682 mutex_unlock(&ftrace_regex_lock);
1688 ftrace_filter_open(struct inode *inode, struct file *file)
1690 return ftrace_regex_open(inode, file, 1);
1694 ftrace_notrace_open(struct inode *inode, struct file *file)
1696 return ftrace_regex_open(inode, file, 0);
1700 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1704 if (file->f_mode & FMODE_READ)
1705 ret = seq_lseek(file, offset, origin);
1707 file->f_pos = ret = 1;
1720 * (static function - no need for kernel doc)
1722 * Pass in a buffer containing a glob and this function will
1723 * set search to point to the search part of the buffer and
1724 * return the type of search it is (see enum above).
1725 * This does modify buff.
1727 * Returns enum type.
1728 * search returns the pointer to use for comparison.
1729 * not returns 1 if buff started with a '!'
1733 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1735 int type = MATCH_FULL;
1738 if (buff[0] == '!') {
1747 for (i = 0; i < len; i++) {
1748 if (buff[i] == '*') {
1751 type = MATCH_END_ONLY;
1753 if (type == MATCH_END_ONLY)
1754 type = MATCH_MIDDLE_ONLY;
1756 type = MATCH_FRONT_ONLY;
1766 static int ftrace_match(char *str, char *regex, int len, int type)
1773 if (strcmp(str, regex) == 0)
1776 case MATCH_FRONT_ONLY:
1777 if (strncmp(str, regex, len) == 0)
1780 case MATCH_MIDDLE_ONLY:
1781 if (strstr(str, regex))
1784 case MATCH_END_ONLY:
1785 ptr = strstr(str, regex);
1786 if (ptr && (ptr[len] == 0))
1795 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1797 char str[KSYM_SYMBOL_LEN];
1799 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1800 return ftrace_match(str, regex, len, type);
1803 static void ftrace_match_records(char *buff, int len, int enable)
1805 unsigned int search_len;
1806 struct ftrace_page *pg;
1807 struct dyn_ftrace *rec;
1813 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1814 type = ftrace_setup_glob(buff, len, &search, ¬);
1816 search_len = strlen(search);
1818 mutex_lock(&ftrace_lock);
1819 do_for_each_ftrace_rec(pg, rec) {
1821 if (rec->flags & FTRACE_FL_FAILED)
1824 if (ftrace_match_record(rec, search, search_len, type)) {
1826 rec->flags &= ~flag;
1831 * Only enable filtering if we have a function that
1834 if (enable && (rec->flags & FTRACE_FL_FILTER))
1835 ftrace_filtered = 1;
1836 } while_for_each_ftrace_rec();
1837 mutex_unlock(&ftrace_lock);
1841 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1842 char *regex, int len, int type)
1844 char str[KSYM_SYMBOL_LEN];
1847 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1849 if (!modname || strcmp(modname, mod))
1852 /* blank search means to match all funcs in the mod */
1854 return ftrace_match(str, regex, len, type);
1859 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1861 unsigned search_len = 0;
1862 struct ftrace_page *pg;
1863 struct dyn_ftrace *rec;
1864 int type = MATCH_FULL;
1865 char *search = buff;
1869 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1871 /* blank or '*' mean the same */
1872 if (strcmp(buff, "*") == 0)
1875 /* handle the case of 'dont filter this module' */
1876 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1882 type = ftrace_setup_glob(buff, strlen(buff), &search, ¬);
1883 search_len = strlen(search);
1886 mutex_lock(&ftrace_lock);
1887 do_for_each_ftrace_rec(pg, rec) {
1889 if (rec->flags & FTRACE_FL_FAILED)
1892 if (ftrace_match_module_record(rec, mod,
1893 search, search_len, type)) {
1895 rec->flags &= ~flag;
1899 if (enable && (rec->flags & FTRACE_FL_FILTER))
1900 ftrace_filtered = 1;
1902 } while_for_each_ftrace_rec();
1903 mutex_unlock(&ftrace_lock);
1907 * We register the module command as a template to show others how
1908 * to register the a command as well.
1912 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1917 * cmd == 'mod' because we only registered this func
1918 * for the 'mod' ftrace_func_command.
1919 * But if you register one func with multiple commands,
1920 * you can tell which command was used by the cmd
1924 /* we must have a module name */
1928 mod = strsep(¶m, ":");
1932 ftrace_match_module_records(func, mod, enable);
1936 static struct ftrace_func_command ftrace_mod_cmd = {
1938 .func = ftrace_mod_callback,
1941 static int __init ftrace_mod_cmd_init(void)
1943 return register_ftrace_command(&ftrace_mod_cmd);
1945 device_initcall(ftrace_mod_cmd_init);
1948 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1950 struct ftrace_func_probe *entry;
1951 struct hlist_head *hhd;
1952 struct hlist_node *n;
1956 key = hash_long(ip, FTRACE_HASH_BITS);
1958 hhd = &ftrace_func_hash[key];
1960 if (hlist_empty(hhd))
1964 * Disable preemption for these calls to prevent a RCU grace
1965 * period. This syncs the hash iteration and freeing of items
1966 * on the hash. rcu_read_lock is too dangerous here.
1968 resched = ftrace_preempt_disable();
1969 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1970 if (entry->ip == ip)
1971 entry->ops->func(ip, parent_ip, &entry->data);
1973 ftrace_preempt_enable(resched);
1976 static struct ftrace_ops trace_probe_ops __read_mostly =
1978 .func = function_trace_probe_call,
1981 static int ftrace_probe_registered;
1983 static void __enable_ftrace_function_probe(void)
1987 if (ftrace_probe_registered)
1990 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1991 struct hlist_head *hhd = &ftrace_func_hash[i];
1995 /* Nothing registered? */
1996 if (i == FTRACE_FUNC_HASHSIZE)
1999 __register_ftrace_function(&trace_probe_ops);
2001 ftrace_probe_registered = 1;
2004 static void __disable_ftrace_function_probe(void)
2008 if (!ftrace_probe_registered)
2011 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2012 struct hlist_head *hhd = &ftrace_func_hash[i];
2017 /* no more funcs left */
2018 __unregister_ftrace_function(&trace_probe_ops);
2020 ftrace_probe_registered = 0;
2024 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2026 struct ftrace_func_probe *entry =
2027 container_of(rhp, struct ftrace_func_probe, rcu);
2029 if (entry->ops->free)
2030 entry->ops->free(&entry->data);
2036 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2039 struct ftrace_func_probe *entry;
2040 struct ftrace_page *pg;
2041 struct dyn_ftrace *rec;
2047 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2048 len = strlen(search);
2050 /* we do not support '!' for function probes */
2054 mutex_lock(&ftrace_lock);
2055 do_for_each_ftrace_rec(pg, rec) {
2057 if (rec->flags & FTRACE_FL_FAILED)
2060 if (!ftrace_match_record(rec, search, len, type))
2063 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2065 /* If we did not process any, then return error */
2076 * The caller might want to do something special
2077 * for each function we find. We call the callback
2078 * to give the caller an opportunity to do so.
2080 if (ops->callback) {
2081 if (ops->callback(rec->ip, &entry->data) < 0) {
2082 /* caller does not like this func */
2089 entry->ip = rec->ip;
2091 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2092 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2094 } while_for_each_ftrace_rec();
2095 __enable_ftrace_function_probe();
2098 mutex_unlock(&ftrace_lock);
2104 PROBE_TEST_FUNC = 1,
2109 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2110 void *data, int flags)
2112 struct ftrace_func_probe *entry;
2113 struct hlist_node *n, *tmp;
2114 char str[KSYM_SYMBOL_LEN];
2115 int type = MATCH_FULL;
2119 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2124 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
2125 len = strlen(search);
2127 /* we do not support '!' for function probes */
2132 mutex_lock(&ftrace_lock);
2133 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2134 struct hlist_head *hhd = &ftrace_func_hash[i];
2136 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2138 /* break up if statements for readability */
2139 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2142 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2145 /* do this last, since it is the most expensive */
2147 kallsyms_lookup(entry->ip, NULL, NULL,
2149 if (!ftrace_match(str, glob, len, type))
2153 hlist_del(&entry->node);
2154 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2157 __disable_ftrace_function_probe();
2158 mutex_unlock(&ftrace_lock);
2162 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2165 __unregister_ftrace_function_probe(glob, ops, data,
2166 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2170 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2172 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2175 void unregister_ftrace_function_probe_all(char *glob)
2177 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2180 static LIST_HEAD(ftrace_commands);
2181 static DEFINE_MUTEX(ftrace_cmd_mutex);
2183 int register_ftrace_command(struct ftrace_func_command *cmd)
2185 struct ftrace_func_command *p;
2188 mutex_lock(&ftrace_cmd_mutex);
2189 list_for_each_entry(p, &ftrace_commands, list) {
2190 if (strcmp(cmd->name, p->name) == 0) {
2195 list_add(&cmd->list, &ftrace_commands);
2197 mutex_unlock(&ftrace_cmd_mutex);
2202 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2204 struct ftrace_func_command *p, *n;
2207 mutex_lock(&ftrace_cmd_mutex);
2208 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2209 if (strcmp(cmd->name, p->name) == 0) {
2211 list_del_init(&p->list);
2216 mutex_unlock(&ftrace_cmd_mutex);
2221 static int ftrace_process_regex(char *buff, int len, int enable)
2223 char *func, *command, *next = buff;
2224 struct ftrace_func_command *p;
2227 func = strsep(&next, ":");
2230 ftrace_match_records(func, len, enable);
2236 command = strsep(&next, ":");
2238 mutex_lock(&ftrace_cmd_mutex);
2239 list_for_each_entry(p, &ftrace_commands, list) {
2240 if (strcmp(p->name, command) == 0) {
2241 ret = p->func(func, command, next, enable);
2246 mutex_unlock(&ftrace_cmd_mutex);
2252 ftrace_regex_write(struct file *file, const char __user *ubuf,
2253 size_t cnt, loff_t *ppos, int enable)
2255 struct ftrace_iterator *iter;
2260 if (!cnt || cnt < 0)
2263 mutex_lock(&ftrace_regex_lock);
2265 if (file->f_mode & FMODE_READ) {
2266 struct seq_file *m = file->private_data;
2269 iter = file->private_data;
2272 iter->flags &= ~FTRACE_ITER_CONT;
2273 iter->buffer_idx = 0;
2276 ret = get_user(ch, ubuf++);
2282 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2283 /* skip white space */
2284 while (cnt && isspace(ch)) {
2285 ret = get_user(ch, ubuf++);
2293 file->f_pos += read;
2298 iter->buffer_idx = 0;
2301 while (cnt && !isspace(ch)) {
2302 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2303 iter->buffer[iter->buffer_idx++] = ch;
2308 ret = get_user(ch, ubuf++);
2317 iter->buffer[iter->buffer_idx] = 0;
2318 ret = ftrace_process_regex(iter->buffer,
2319 iter->buffer_idx, enable);
2322 iter->buffer_idx = 0;
2324 iter->flags |= FTRACE_ITER_CONT;
2327 file->f_pos += read;
2331 mutex_unlock(&ftrace_regex_lock);
2337 ftrace_filter_write(struct file *file, const char __user *ubuf,
2338 size_t cnt, loff_t *ppos)
2340 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2344 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2345 size_t cnt, loff_t *ppos)
2347 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2351 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2353 if (unlikely(ftrace_disabled))
2356 mutex_lock(&ftrace_regex_lock);
2358 ftrace_filter_reset(enable);
2360 ftrace_match_records(buf, len, enable);
2361 mutex_unlock(&ftrace_regex_lock);
2365 * ftrace_set_filter - set a function to filter on in ftrace
2366 * @buf - the string that holds the function filter text.
2367 * @len - the length of the string.
2368 * @reset - non zero to reset all filters before applying this filter.
2370 * Filters denote which functions should be enabled when tracing is enabled.
2371 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2373 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2375 ftrace_set_regex(buf, len, reset, 1);
2379 * ftrace_set_notrace - set a function to not trace in ftrace
2380 * @buf - the string that holds the function notrace text.
2381 * @len - the length of the string.
2382 * @reset - non zero to reset all filters before applying this filter.
2384 * Notrace Filters denote which functions should not be enabled when tracing
2385 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2388 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2390 ftrace_set_regex(buf, len, reset, 0);
2394 * command line interface to allow users to set filters on boot up.
2396 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2397 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2398 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2400 static int __init set_ftrace_notrace(char *str)
2402 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2405 __setup("ftrace_notrace=", set_ftrace_notrace);
2407 static int __init set_ftrace_filter(char *str)
2409 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2412 __setup("ftrace_filter=", set_ftrace_filter);
2414 static void __init set_ftrace_early_filter(char *buf, int enable)
2419 func = strsep(&buf, ",");
2420 ftrace_set_regex(func, strlen(func), 0, enable);
2424 static void __init set_ftrace_early_filters(void)
2426 if (ftrace_filter_buf[0])
2427 set_ftrace_early_filter(ftrace_filter_buf, 1);
2428 if (ftrace_notrace_buf[0])
2429 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2433 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2435 struct seq_file *m = (struct seq_file *)file->private_data;
2436 struct ftrace_iterator *iter;
2438 mutex_lock(&ftrace_regex_lock);
2439 if (file->f_mode & FMODE_READ) {
2442 seq_release(inode, file);
2444 iter = file->private_data;
2446 if (iter->buffer_idx) {
2448 iter->buffer[iter->buffer_idx] = 0;
2449 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
2452 mutex_lock(&ftrace_lock);
2453 if (ftrace_start_up && ftrace_enabled)
2454 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2455 mutex_unlock(&ftrace_lock);
2458 mutex_unlock(&ftrace_regex_lock);
2463 ftrace_filter_release(struct inode *inode, struct file *file)
2465 return ftrace_regex_release(inode, file, 1);
2469 ftrace_notrace_release(struct inode *inode, struct file *file)
2471 return ftrace_regex_release(inode, file, 0);
2474 static const struct file_operations ftrace_avail_fops = {
2475 .open = ftrace_avail_open,
2477 .llseek = seq_lseek,
2478 .release = ftrace_avail_release,
2481 static const struct file_operations ftrace_failures_fops = {
2482 .open = ftrace_failures_open,
2484 .llseek = seq_lseek,
2485 .release = ftrace_avail_release,
2488 static const struct file_operations ftrace_filter_fops = {
2489 .open = ftrace_filter_open,
2491 .write = ftrace_filter_write,
2492 .llseek = ftrace_regex_lseek,
2493 .release = ftrace_filter_release,
2496 static const struct file_operations ftrace_notrace_fops = {
2497 .open = ftrace_notrace_open,
2499 .write = ftrace_notrace_write,
2500 .llseek = ftrace_regex_lseek,
2501 .release = ftrace_notrace_release,
2504 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2506 static DEFINE_MUTEX(graph_lock);
2508 int ftrace_graph_count;
2509 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2512 __g_next(struct seq_file *m, loff_t *pos)
2514 unsigned long *array = m->private;
2516 if (*pos >= ftrace_graph_count)
2518 return &array[*pos];
2522 g_next(struct seq_file *m, void *v, loff_t *pos)
2525 return __g_next(m, pos);
2528 static void *g_start(struct seq_file *m, loff_t *pos)
2530 mutex_lock(&graph_lock);
2532 /* Nothing, tell g_show to print all functions are enabled */
2533 if (!ftrace_graph_count && !*pos)
2536 return __g_next(m, pos);
2539 static void g_stop(struct seq_file *m, void *p)
2541 mutex_unlock(&graph_lock);
2544 static int g_show(struct seq_file *m, void *v)
2546 unsigned long *ptr = v;
2547 char str[KSYM_SYMBOL_LEN];
2552 if (ptr == (unsigned long *)1) {
2553 seq_printf(m, "#### all functions enabled ####\n");
2557 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2559 seq_printf(m, "%s\n", str);
2564 static struct seq_operations ftrace_graph_seq_ops = {
2572 ftrace_graph_open(struct inode *inode, struct file *file)
2576 if (unlikely(ftrace_disabled))
2579 mutex_lock(&graph_lock);
2580 if ((file->f_mode & FMODE_WRITE) &&
2581 !(file->f_flags & O_APPEND)) {
2582 ftrace_graph_count = 0;
2583 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2586 if (file->f_mode & FMODE_READ) {
2587 ret = seq_open(file, &ftrace_graph_seq_ops);
2589 struct seq_file *m = file->private_data;
2590 m->private = ftrace_graph_funcs;
2593 file->private_data = ftrace_graph_funcs;
2594 mutex_unlock(&graph_lock);
2600 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2602 struct dyn_ftrace *rec;
2603 struct ftrace_page *pg;
2611 if (ftrace_disabled)
2615 type = ftrace_setup_glob(buffer, strlen(buffer), &search, ¬);
2619 search_len = strlen(search);
2621 mutex_lock(&ftrace_lock);
2622 do_for_each_ftrace_rec(pg, rec) {
2624 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2627 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2630 if (ftrace_match_record(rec, search, search_len, type)) {
2631 /* ensure it is not already in the array */
2633 for (i = 0; i < *idx; i++)
2634 if (array[i] == rec->ip) {
2639 array[(*idx)++] = rec->ip;
2643 } while_for_each_ftrace_rec();
2645 mutex_unlock(&ftrace_lock);
2647 return found ? 0 : -EINVAL;
2651 ftrace_graph_write(struct file *file, const char __user *ubuf,
2652 size_t cnt, loff_t *ppos)
2654 unsigned char buffer[FTRACE_BUFF_MAX+1];
2655 unsigned long *array;
2661 if (!cnt || cnt < 0)
2664 mutex_lock(&graph_lock);
2666 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2671 if (file->f_mode & FMODE_READ) {
2672 struct seq_file *m = file->private_data;
2675 array = file->private_data;
2677 ret = get_user(ch, ubuf++);
2683 /* skip white space */
2684 while (cnt && isspace(ch)) {
2685 ret = get_user(ch, ubuf++);
2698 while (cnt && !isspace(ch)) {
2699 if (index < FTRACE_BUFF_MAX)
2700 buffer[index++] = ch;
2705 ret = get_user(ch, ubuf++);
2713 /* we allow only one expression at a time */
2714 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2718 file->f_pos += read;
2722 mutex_unlock(&graph_lock);
2727 static const struct file_operations ftrace_graph_fops = {
2728 .open = ftrace_graph_open,
2730 .write = ftrace_graph_write,
2732 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2734 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2737 trace_create_file("available_filter_functions", 0444,
2738 d_tracer, NULL, &ftrace_avail_fops);
2740 trace_create_file("failures", 0444,
2741 d_tracer, NULL, &ftrace_failures_fops);
2743 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2744 NULL, &ftrace_filter_fops);
2746 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2747 NULL, &ftrace_notrace_fops);
2749 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2750 trace_create_file("set_graph_function", 0444, d_tracer,
2752 &ftrace_graph_fops);
2753 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2758 static int ftrace_convert_nops(struct module *mod,
2759 unsigned long *start,
2764 unsigned long flags;
2766 mutex_lock(&ftrace_lock);
2769 addr = ftrace_call_adjust(*p++);
2771 * Some architecture linkers will pad between
2772 * the different mcount_loc sections of different
2773 * object files to satisfy alignments.
2774 * Skip any NULL pointers.
2778 ftrace_record_ip(addr);
2781 /* disable interrupts to prevent kstop machine */
2782 local_irq_save(flags);
2783 ftrace_update_code(mod);
2784 local_irq_restore(flags);
2785 mutex_unlock(&ftrace_lock);
2790 #ifdef CONFIG_MODULES
2791 void ftrace_release(void *start, void *end)
2793 struct dyn_ftrace *rec;
2794 struct ftrace_page *pg;
2795 unsigned long s = (unsigned long)start;
2796 unsigned long e = (unsigned long)end;
2798 if (ftrace_disabled || !start || start == end)
2801 mutex_lock(&ftrace_lock);
2802 do_for_each_ftrace_rec(pg, rec) {
2803 if ((rec->ip >= s) && (rec->ip < e)) {
2805 * rec->ip is changed in ftrace_free_rec()
2806 * It should not between s and e if record was freed.
2808 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2809 ftrace_free_rec(rec);
2811 } while_for_each_ftrace_rec();
2812 mutex_unlock(&ftrace_lock);
2815 static void ftrace_init_module(struct module *mod,
2816 unsigned long *start, unsigned long *end)
2818 if (ftrace_disabled || start == end)
2820 ftrace_convert_nops(mod, start, end);
2823 static int ftrace_module_notify(struct notifier_block *self,
2824 unsigned long val, void *data)
2826 struct module *mod = data;
2829 case MODULE_STATE_COMING:
2830 ftrace_init_module(mod, mod->ftrace_callsites,
2831 mod->ftrace_callsites +
2832 mod->num_ftrace_callsites);
2834 case MODULE_STATE_GOING:
2835 ftrace_release(mod->ftrace_callsites,
2836 mod->ftrace_callsites +
2837 mod->num_ftrace_callsites);
2844 static int ftrace_module_notify(struct notifier_block *self,
2845 unsigned long val, void *data)
2849 #endif /* CONFIG_MODULES */
2851 struct notifier_block ftrace_module_nb = {
2852 .notifier_call = ftrace_module_notify,
2856 extern unsigned long __start_mcount_loc[];
2857 extern unsigned long __stop_mcount_loc[];
2859 void __init ftrace_init(void)
2861 unsigned long count, addr, flags;
2864 /* Keep the ftrace pointer to the stub */
2865 addr = (unsigned long)ftrace_stub;
2867 local_irq_save(flags);
2868 ftrace_dyn_arch_init(&addr);
2869 local_irq_restore(flags);
2871 /* ftrace_dyn_arch_init places the return code in addr */
2875 count = __stop_mcount_loc - __start_mcount_loc;
2877 ret = ftrace_dyn_table_alloc(count);
2881 last_ftrace_enabled = ftrace_enabled = 1;
2883 ret = ftrace_convert_nops(NULL,
2887 ret = register_module_notifier(&ftrace_module_nb);
2889 pr_warning("Failed to register trace ftrace module notifier\n");
2891 set_ftrace_early_filters();
2895 ftrace_disabled = 1;
2900 static int __init ftrace_nodyn_init(void)
2905 device_initcall(ftrace_nodyn_init);
2907 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2908 static inline void ftrace_startup_enable(int command) { }
2909 /* Keep as macros so we do not need to define the commands */
2910 # define ftrace_startup(command) do { } while (0)
2911 # define ftrace_shutdown(command) do { } while (0)
2912 # define ftrace_startup_sysctl() do { } while (0)
2913 # define ftrace_shutdown_sysctl() do { } while (0)
2914 #endif /* CONFIG_DYNAMIC_FTRACE */
2917 ftrace_pid_read(struct file *file, char __user *ubuf,
2918 size_t cnt, loff_t *ppos)
2923 if (ftrace_pid_trace == ftrace_swapper_pid)
2924 r = sprintf(buf, "swapper tasks\n");
2925 else if (ftrace_pid_trace)
2926 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2928 r = sprintf(buf, "no pid\n");
2930 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2933 static void clear_ftrace_swapper(void)
2935 struct task_struct *p;
2939 for_each_online_cpu(cpu) {
2941 clear_tsk_trace_trace(p);
2946 static void set_ftrace_swapper(void)
2948 struct task_struct *p;
2952 for_each_online_cpu(cpu) {
2954 set_tsk_trace_trace(p);
2959 static void clear_ftrace_pid(struct pid *pid)
2961 struct task_struct *p;
2964 do_each_pid_task(pid, PIDTYPE_PID, p) {
2965 clear_tsk_trace_trace(p);
2966 } while_each_pid_task(pid, PIDTYPE_PID, p);
2972 static void set_ftrace_pid(struct pid *pid)
2974 struct task_struct *p;
2977 do_each_pid_task(pid, PIDTYPE_PID, p) {
2978 set_tsk_trace_trace(p);
2979 } while_each_pid_task(pid, PIDTYPE_PID, p);
2983 static void clear_ftrace_pid_task(struct pid **pid)
2985 if (*pid == ftrace_swapper_pid)
2986 clear_ftrace_swapper();
2988 clear_ftrace_pid(*pid);
2993 static void set_ftrace_pid_task(struct pid *pid)
2995 if (pid == ftrace_swapper_pid)
2996 set_ftrace_swapper();
2998 set_ftrace_pid(pid);
3002 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3003 size_t cnt, loff_t *ppos)
3010 if (cnt >= sizeof(buf))
3013 if (copy_from_user(&buf, ubuf, cnt))
3018 ret = strict_strtol(buf, 10, &val);
3022 mutex_lock(&ftrace_lock);
3024 /* disable pid tracing */
3025 if (!ftrace_pid_trace)
3028 clear_ftrace_pid_task(&ftrace_pid_trace);
3031 /* swapper task is special */
3033 pid = ftrace_swapper_pid;
3034 if (pid == ftrace_pid_trace)
3037 pid = find_get_pid(val);
3039 if (pid == ftrace_pid_trace) {
3045 if (ftrace_pid_trace)
3046 clear_ftrace_pid_task(&ftrace_pid_trace);
3051 ftrace_pid_trace = pid;
3053 set_ftrace_pid_task(ftrace_pid_trace);
3056 /* update the function call */
3057 ftrace_update_pid_func();
3058 ftrace_startup_enable(0);
3061 mutex_unlock(&ftrace_lock);
3066 static const struct file_operations ftrace_pid_fops = {
3067 .read = ftrace_pid_read,
3068 .write = ftrace_pid_write,
3071 static __init int ftrace_init_debugfs(void)
3073 struct dentry *d_tracer;
3075 d_tracer = tracing_init_dentry();
3079 ftrace_init_dyn_debugfs(d_tracer);
3081 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3082 NULL, &ftrace_pid_fops);
3084 ftrace_profile_debugfs(d_tracer);
3088 fs_initcall(ftrace_init_debugfs);
3091 * ftrace_kill - kill ftrace
3093 * This function should be used by panic code. It stops ftrace
3094 * but in a not so nice way. If you need to simply kill ftrace
3095 * from a non-atomic section, use ftrace_kill.
3097 void ftrace_kill(void)
3099 ftrace_disabled = 1;
3101 clear_ftrace_function();
3105 * register_ftrace_function - register a function for profiling
3106 * @ops - ops structure that holds the function for profiling.
3108 * Register a function to be called by all functions in the
3111 * Note: @ops->func and all the functions it calls must be labeled
3112 * with "notrace", otherwise it will go into a
3115 int register_ftrace_function(struct ftrace_ops *ops)
3119 if (unlikely(ftrace_disabled))
3122 mutex_lock(&ftrace_lock);
3124 ret = __register_ftrace_function(ops);
3127 mutex_unlock(&ftrace_lock);
3132 * unregister_ftrace_function - unregister a function for profiling.
3133 * @ops - ops structure that holds the function to unregister
3135 * Unregister a function that was added to be called by ftrace profiling.
3137 int unregister_ftrace_function(struct ftrace_ops *ops)
3141 mutex_lock(&ftrace_lock);
3142 ret = __unregister_ftrace_function(ops);
3144 mutex_unlock(&ftrace_lock);
3150 ftrace_enable_sysctl(struct ctl_table *table, int write,
3151 struct file *file, void __user *buffer, size_t *lenp,
3156 if (unlikely(ftrace_disabled))
3159 mutex_lock(&ftrace_lock);
3161 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
3163 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
3166 last_ftrace_enabled = ftrace_enabled;
3168 if (ftrace_enabled) {
3170 ftrace_startup_sysctl();
3172 /* we are starting ftrace again */
3173 if (ftrace_list != &ftrace_list_end) {
3174 if (ftrace_list->next == &ftrace_list_end)
3175 ftrace_trace_function = ftrace_list->func;
3177 ftrace_trace_function = ftrace_list_func;
3181 /* stopping ftrace calls (just send to ftrace_stub) */
3182 ftrace_trace_function = ftrace_stub;
3184 ftrace_shutdown_sysctl();
3188 mutex_unlock(&ftrace_lock);
3192 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3194 static int ftrace_graph_active;
3195 static struct notifier_block ftrace_suspend_notifier;
3197 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3202 /* The callbacks that hook a function */
3203 trace_func_graph_ret_t ftrace_graph_return =
3204 (trace_func_graph_ret_t)ftrace_stub;
3205 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3207 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3208 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3212 unsigned long flags;
3213 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3214 struct task_struct *g, *t;
3216 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3217 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3218 * sizeof(struct ftrace_ret_stack),
3220 if (!ret_stack_list[i]) {
3228 read_lock_irqsave(&tasklist_lock, flags);
3229 do_each_thread(g, t) {
3235 if (t->ret_stack == NULL) {
3236 atomic_set(&t->tracing_graph_pause, 0);
3237 atomic_set(&t->trace_overrun, 0);
3238 t->curr_ret_stack = -1;
3239 /* Make sure the tasks see the -1 first: */
3241 t->ret_stack = ret_stack_list[start++];
3243 } while_each_thread(g, t);
3246 read_unlock_irqrestore(&tasklist_lock, flags);
3248 for (i = start; i < end; i++)
3249 kfree(ret_stack_list[i]);
3254 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3255 struct task_struct *next)
3257 unsigned long long timestamp;
3261 * Does the user want to count the time a function was asleep.
3262 * If so, do not update the time stamps.
3264 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3267 timestamp = trace_clock_local();
3269 prev->ftrace_timestamp = timestamp;
3271 /* only process tasks that we timestamped */
3272 if (!next->ftrace_timestamp)
3276 * Update all the counters in next to make up for the
3277 * time next was sleeping.
3279 timestamp -= next->ftrace_timestamp;
3281 for (index = next->curr_ret_stack; index >= 0; index--)
3282 next->ret_stack[index].calltime += timestamp;
3285 /* Allocate a return stack for each task */
3286 static int start_graph_tracing(void)
3288 struct ftrace_ret_stack **ret_stack_list;
3291 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3292 sizeof(struct ftrace_ret_stack *),
3295 if (!ret_stack_list)
3298 /* The cpu_boot init_task->ret_stack will never be freed */
3299 for_each_online_cpu(cpu) {
3300 if (!idle_task(cpu)->ret_stack)
3301 ftrace_graph_init_task(idle_task(cpu));
3305 ret = alloc_retstack_tasklist(ret_stack_list);
3306 } while (ret == -EAGAIN);
3309 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3311 pr_info("ftrace_graph: Couldn't activate tracepoint"
3312 " probe to kernel_sched_switch\n");
3315 kfree(ret_stack_list);
3320 * Hibernation protection.
3321 * The state of the current task is too much unstable during
3322 * suspend/restore to disk. We want to protect against that.
3325 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3329 case PM_HIBERNATION_PREPARE:
3330 pause_graph_tracing();
3333 case PM_POST_HIBERNATION:
3334 unpause_graph_tracing();
3340 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3341 trace_func_graph_ent_t entryfunc)
3345 mutex_lock(&ftrace_lock);
3347 /* we currently allow only one tracer registered at a time */
3348 if (ftrace_graph_active) {
3353 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3354 register_pm_notifier(&ftrace_suspend_notifier);
3356 ftrace_graph_active++;
3357 ret = start_graph_tracing();
3359 ftrace_graph_active--;
3363 ftrace_graph_return = retfunc;
3364 ftrace_graph_entry = entryfunc;
3366 ftrace_startup(FTRACE_START_FUNC_RET);
3369 mutex_unlock(&ftrace_lock);
3373 void unregister_ftrace_graph(void)
3375 mutex_lock(&ftrace_lock);
3377 if (unlikely(!ftrace_graph_active))
3380 ftrace_graph_active--;
3381 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3382 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3383 ftrace_graph_entry = ftrace_graph_entry_stub;
3384 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3385 unregister_pm_notifier(&ftrace_suspend_notifier);
3388 mutex_unlock(&ftrace_lock);
3391 /* Allocate a return stack for newly created task */
3392 void ftrace_graph_init_task(struct task_struct *t)
3394 /* Make sure we do not use the parent ret_stack */
3395 t->ret_stack = NULL;
3397 if (ftrace_graph_active) {
3398 struct ftrace_ret_stack *ret_stack;
3400 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3401 * sizeof(struct ftrace_ret_stack),
3405 t->curr_ret_stack = -1;
3406 atomic_set(&t->tracing_graph_pause, 0);
3407 atomic_set(&t->trace_overrun, 0);
3408 t->ftrace_timestamp = 0;
3409 /* make curr_ret_stack visable before we add the ret_stack */
3411 t->ret_stack = ret_stack;
3415 void ftrace_graph_exit_task(struct task_struct *t)
3417 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3419 t->ret_stack = NULL;
3420 /* NULL must become visible to IRQs before we free it: */
3426 void ftrace_graph_stop(void)