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;
63 /* List for set_ftrace_pid's pids. */
64 LIST_HEAD(ftrace_pids);
66 struct list_head list;
71 * ftrace_disabled is set when an anomaly is discovered.
72 * ftrace_disabled is much stronger than ftrace_enabled.
74 static int ftrace_disabled __read_mostly;
76 static DEFINE_MUTEX(ftrace_lock);
78 static struct ftrace_ops ftrace_list_end __read_mostly =
83 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
84 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
85 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
86 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
88 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
90 struct ftrace_ops *op = ftrace_list;
92 /* in case someone actually ports this to alpha! */
93 read_barrier_depends();
95 while (op != &ftrace_list_end) {
97 read_barrier_depends();
98 op->func(ip, parent_ip);
103 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
105 if (!test_tsk_trace_trace(current))
108 ftrace_pid_function(ip, parent_ip);
111 static void set_ftrace_pid_function(ftrace_func_t func)
113 /* do not set ftrace_pid_function to itself! */
114 if (func != ftrace_pid_func)
115 ftrace_pid_function = func;
119 * clear_ftrace_function - reset the ftrace function
121 * This NULLs the ftrace function and in essence stops
122 * tracing. There may be lag
124 void clear_ftrace_function(void)
126 ftrace_trace_function = ftrace_stub;
127 __ftrace_trace_function = ftrace_stub;
128 ftrace_pid_function = ftrace_stub;
131 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
133 * For those archs that do not test ftrace_trace_stop in their
134 * mcount call site, we need to do it from C.
136 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
138 if (function_trace_stop)
141 __ftrace_trace_function(ip, parent_ip);
145 static int __register_ftrace_function(struct ftrace_ops *ops)
147 ops->next = ftrace_list;
149 * We are entering ops into the ftrace_list but another
150 * CPU might be walking that list. We need to make sure
151 * the ops->next pointer is valid before another CPU sees
152 * the ops pointer included into the ftrace_list.
157 if (ftrace_enabled) {
160 if (ops->next == &ftrace_list_end)
163 func = ftrace_list_func;
165 if (!list_empty(&ftrace_pids)) {
166 set_ftrace_pid_function(func);
167 func = ftrace_pid_func;
171 * For one func, simply call it directly.
172 * For more than one func, call the chain.
174 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
175 ftrace_trace_function = func;
177 __ftrace_trace_function = func;
178 ftrace_trace_function = ftrace_test_stop_func;
185 static int __unregister_ftrace_function(struct ftrace_ops *ops)
187 struct ftrace_ops **p;
190 * If we are removing the last function, then simply point
191 * to the ftrace_stub.
193 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
194 ftrace_trace_function = ftrace_stub;
195 ftrace_list = &ftrace_list_end;
199 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
208 if (ftrace_enabled) {
209 /* If we only have one func left, then call that directly */
210 if (ftrace_list->next == &ftrace_list_end) {
211 ftrace_func_t func = ftrace_list->func;
213 if (!list_empty(&ftrace_pids)) {
214 set_ftrace_pid_function(func);
215 func = ftrace_pid_func;
217 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
218 ftrace_trace_function = func;
220 __ftrace_trace_function = func;
228 static void ftrace_update_pid_func(void)
232 if (ftrace_trace_function == ftrace_stub)
235 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
236 func = ftrace_trace_function;
238 func = __ftrace_trace_function;
241 if (!list_empty(&ftrace_pids)) {
242 set_ftrace_pid_function(func);
243 func = ftrace_pid_func;
245 if (func == ftrace_pid_func)
246 func = ftrace_pid_function;
249 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
250 ftrace_trace_function = func;
252 __ftrace_trace_function = func;
256 #ifdef CONFIG_FUNCTION_PROFILER
257 struct ftrace_profile {
258 struct hlist_node node;
260 unsigned long counter;
261 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
262 unsigned long long time;
266 struct ftrace_profile_page {
267 struct ftrace_profile_page *next;
269 struct ftrace_profile records[];
272 struct ftrace_profile_stat {
274 struct hlist_head *hash;
275 struct ftrace_profile_page *pages;
276 struct ftrace_profile_page *start;
277 struct tracer_stat stat;
280 #define PROFILE_RECORDS_SIZE \
281 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
283 #define PROFILES_PER_PAGE \
284 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
286 static int ftrace_profile_bits __read_mostly;
287 static int ftrace_profile_enabled __read_mostly;
289 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
290 static DEFINE_MUTEX(ftrace_profile_lock);
292 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
294 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
297 function_stat_next(void *v, int idx)
299 struct ftrace_profile *rec = v;
300 struct ftrace_profile_page *pg;
302 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
308 if ((void *)rec >= (void *)&pg->records[pg->index]) {
312 rec = &pg->records[0];
320 static void *function_stat_start(struct tracer_stat *trace)
322 struct ftrace_profile_stat *stat =
323 container_of(trace, struct ftrace_profile_stat, stat);
325 if (!stat || !stat->start)
328 return function_stat_next(&stat->start->records[0], 0);
331 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
332 /* function graph compares on total time */
333 static int function_stat_cmp(void *p1, void *p2)
335 struct ftrace_profile *a = p1;
336 struct ftrace_profile *b = p2;
338 if (a->time < b->time)
340 if (a->time > b->time)
346 /* not function graph compares against hits */
347 static int function_stat_cmp(void *p1, void *p2)
349 struct ftrace_profile *a = p1;
350 struct ftrace_profile *b = p2;
352 if (a->counter < b->counter)
354 if (a->counter > b->counter)
361 static int function_stat_headers(struct seq_file *m)
363 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
364 seq_printf(m, " Function "
369 seq_printf(m, " Function Hit\n"
375 static int function_stat_show(struct seq_file *m, void *v)
377 struct ftrace_profile *rec = v;
378 char str[KSYM_SYMBOL_LEN];
379 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
380 static DEFINE_MUTEX(mutex);
381 static struct trace_seq s;
382 unsigned long long avg;
385 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
386 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
388 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
391 do_div(avg, rec->counter);
395 trace_print_graph_duration(rec->time, &s);
396 trace_seq_puts(&s, " ");
397 trace_print_graph_duration(avg, &s);
398 trace_print_seq(m, &s);
399 mutex_unlock(&mutex);
406 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
408 struct ftrace_profile_page *pg;
410 pg = stat->pages = stat->start;
413 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
418 memset(stat->hash, 0,
419 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
422 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
424 struct ftrace_profile_page *pg;
429 /* If we already allocated, do nothing */
433 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
437 #ifdef CONFIG_DYNAMIC_FTRACE
438 functions = ftrace_update_tot_cnt;
441 * We do not know the number of functions that exist because
442 * dynamic tracing is what counts them. With past experience
443 * we have around 20K functions. That should be more than enough.
444 * It is highly unlikely we will execute every function in
450 pg = stat->start = stat->pages;
452 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
454 for (i = 0; i < pages; i++) {
455 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
466 unsigned long tmp = (unsigned long)pg;
472 free_page((unsigned long)stat->pages);
479 static int ftrace_profile_init_cpu(int cpu)
481 struct ftrace_profile_stat *stat;
484 stat = &per_cpu(ftrace_profile_stats, cpu);
487 /* If the profile is already created, simply reset it */
488 ftrace_profile_reset(stat);
493 * We are profiling all functions, but usually only a few thousand
494 * functions are hit. We'll make a hash of 1024 items.
496 size = FTRACE_PROFILE_HASH_SIZE;
498 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
503 if (!ftrace_profile_bits) {
506 for (; size; size >>= 1)
507 ftrace_profile_bits++;
510 /* Preallocate the function profiling pages */
511 if (ftrace_profile_pages_init(stat) < 0) {
520 static int ftrace_profile_init(void)
525 for_each_online_cpu(cpu) {
526 ret = ftrace_profile_init_cpu(cpu);
534 /* interrupts must be disabled */
535 static struct ftrace_profile *
536 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
538 struct ftrace_profile *rec;
539 struct hlist_head *hhd;
540 struct hlist_node *n;
543 key = hash_long(ip, ftrace_profile_bits);
544 hhd = &stat->hash[key];
546 if (hlist_empty(hhd))
549 hlist_for_each_entry_rcu(rec, n, hhd, node) {
557 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
558 struct ftrace_profile *rec)
562 key = hash_long(rec->ip, ftrace_profile_bits);
563 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
567 * The memory is already allocated, this simply finds a new record to use.
569 static struct ftrace_profile *
570 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
572 struct ftrace_profile *rec = NULL;
574 /* prevent recursion (from NMIs) */
575 if (atomic_inc_return(&stat->disabled) != 1)
579 * Try to find the function again since an NMI
580 * could have added it
582 rec = ftrace_find_profiled_func(stat, ip);
586 if (stat->pages->index == PROFILES_PER_PAGE) {
587 if (!stat->pages->next)
589 stat->pages = stat->pages->next;
592 rec = &stat->pages->records[stat->pages->index++];
594 ftrace_add_profile(stat, rec);
597 atomic_dec(&stat->disabled);
603 function_profile_call(unsigned long ip, unsigned long parent_ip)
605 struct ftrace_profile_stat *stat;
606 struct ftrace_profile *rec;
609 if (!ftrace_profile_enabled)
612 local_irq_save(flags);
614 stat = &__get_cpu_var(ftrace_profile_stats);
615 if (!stat->hash || !ftrace_profile_enabled)
618 rec = ftrace_find_profiled_func(stat, ip);
620 rec = ftrace_profile_alloc(stat, ip);
627 local_irq_restore(flags);
630 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
631 static int profile_graph_entry(struct ftrace_graph_ent *trace)
633 function_profile_call(trace->func, 0);
637 static void profile_graph_return(struct ftrace_graph_ret *trace)
639 struct ftrace_profile_stat *stat;
640 unsigned long long calltime;
641 struct ftrace_profile *rec;
644 local_irq_save(flags);
645 stat = &__get_cpu_var(ftrace_profile_stats);
646 if (!stat->hash || !ftrace_profile_enabled)
649 calltime = trace->rettime - trace->calltime;
651 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
654 index = trace->depth;
656 /* Append this call time to the parent time to subtract */
658 current->ret_stack[index - 1].subtime += calltime;
660 if (current->ret_stack[index].subtime < calltime)
661 calltime -= current->ret_stack[index].subtime;
666 rec = ftrace_find_profiled_func(stat, trace->func);
668 rec->time += calltime;
671 local_irq_restore(flags);
674 static int register_ftrace_profiler(void)
676 return register_ftrace_graph(&profile_graph_return,
677 &profile_graph_entry);
680 static void unregister_ftrace_profiler(void)
682 unregister_ftrace_graph();
685 static struct ftrace_ops ftrace_profile_ops __read_mostly =
687 .func = function_profile_call,
690 static int register_ftrace_profiler(void)
692 return register_ftrace_function(&ftrace_profile_ops);
695 static void unregister_ftrace_profiler(void)
697 unregister_ftrace_function(&ftrace_profile_ops);
699 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
702 ftrace_profile_write(struct file *filp, const char __user *ubuf,
703 size_t cnt, loff_t *ppos)
706 char buf[64]; /* big enough to hold a number */
709 if (cnt >= sizeof(buf))
712 if (copy_from_user(&buf, ubuf, cnt))
717 ret = strict_strtoul(buf, 10, &val);
723 mutex_lock(&ftrace_profile_lock);
724 if (ftrace_profile_enabled ^ val) {
726 ret = ftrace_profile_init();
732 ret = register_ftrace_profiler();
737 ftrace_profile_enabled = 1;
739 ftrace_profile_enabled = 0;
741 * unregister_ftrace_profiler calls stop_machine
742 * so this acts like an synchronize_sched.
744 unregister_ftrace_profiler();
748 mutex_unlock(&ftrace_profile_lock);
756 ftrace_profile_read(struct file *filp, char __user *ubuf,
757 size_t cnt, loff_t *ppos)
759 char buf[64]; /* big enough to hold a number */
762 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
763 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
766 static const struct file_operations ftrace_profile_fops = {
767 .open = tracing_open_generic,
768 .read = ftrace_profile_read,
769 .write = ftrace_profile_write,
772 /* used to initialize the real stat files */
773 static struct tracer_stat function_stats __initdata = {
775 .stat_start = function_stat_start,
776 .stat_next = function_stat_next,
777 .stat_cmp = function_stat_cmp,
778 .stat_headers = function_stat_headers,
779 .stat_show = function_stat_show
782 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
784 struct ftrace_profile_stat *stat;
785 struct dentry *entry;
790 for_each_possible_cpu(cpu) {
791 stat = &per_cpu(ftrace_profile_stats, cpu);
793 /* allocate enough for function name + cpu number */
794 name = kmalloc(32, GFP_KERNEL);
797 * The files created are permanent, if something happens
798 * we still do not free memory.
801 "Could not allocate stat file for cpu %d\n",
805 stat->stat = function_stats;
806 snprintf(name, 32, "function%d", cpu);
807 stat->stat.name = name;
808 ret = register_stat_tracer(&stat->stat);
811 "Could not register function stat for cpu %d\n",
818 entry = debugfs_create_file("function_profile_enabled", 0644,
819 d_tracer, NULL, &ftrace_profile_fops);
821 pr_warning("Could not create debugfs "
822 "'function_profile_enabled' entry\n");
825 #else /* CONFIG_FUNCTION_PROFILER */
826 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
829 #endif /* CONFIG_FUNCTION_PROFILER */
831 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
833 #ifdef CONFIG_DYNAMIC_FTRACE
835 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
836 # error Dynamic ftrace depends on MCOUNT_RECORD
839 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
841 struct ftrace_func_probe {
842 struct hlist_node node;
843 struct ftrace_probe_ops *ops;
851 FTRACE_ENABLE_CALLS = (1 << 0),
852 FTRACE_DISABLE_CALLS = (1 << 1),
853 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
854 FTRACE_ENABLE_MCOUNT = (1 << 3),
855 FTRACE_DISABLE_MCOUNT = (1 << 4),
856 FTRACE_START_FUNC_RET = (1 << 5),
857 FTRACE_STOP_FUNC_RET = (1 << 6),
860 static int ftrace_filtered;
862 static struct dyn_ftrace *ftrace_new_addrs;
864 static DEFINE_MUTEX(ftrace_regex_lock);
867 struct ftrace_page *next;
869 struct dyn_ftrace records[];
872 #define ENTRIES_PER_PAGE \
873 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
875 /* estimate from running different kernels */
876 #define NR_TO_INIT 10000
878 static struct ftrace_page *ftrace_pages_start;
879 static struct ftrace_page *ftrace_pages;
881 static struct dyn_ftrace *ftrace_free_records;
884 * This is a double for. Do not use 'break' to break out of the loop,
885 * you must use a goto.
887 #define do_for_each_ftrace_rec(pg, rec) \
888 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
890 for (_____i = 0; _____i < pg->index; _____i++) { \
891 rec = &pg->records[_____i];
893 #define while_for_each_ftrace_rec() \
897 #ifdef CONFIG_KPROBES
899 static int frozen_record_count;
901 static inline void freeze_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 void unfreeze_record(struct dyn_ftrace *rec)
911 if (rec->flags & FTRACE_FL_FROZEN) {
912 rec->flags &= ~FTRACE_FL_FROZEN;
913 frozen_record_count--;
917 static inline int record_frozen(struct dyn_ftrace *rec)
919 return rec->flags & FTRACE_FL_FROZEN;
922 # define freeze_record(rec) ({ 0; })
923 # define unfreeze_record(rec) ({ 0; })
924 # define record_frozen(rec) ({ 0; })
925 #endif /* CONFIG_KPROBES */
927 static void ftrace_free_rec(struct dyn_ftrace *rec)
929 rec->freelist = ftrace_free_records;
930 ftrace_free_records = rec;
931 rec->flags |= FTRACE_FL_FREE;
934 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
936 struct dyn_ftrace *rec;
938 /* First check for freed records */
939 if (ftrace_free_records) {
940 rec = ftrace_free_records;
942 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
943 FTRACE_WARN_ON_ONCE(1);
944 ftrace_free_records = NULL;
948 ftrace_free_records = rec->freelist;
949 memset(rec, 0, sizeof(*rec));
953 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
954 if (!ftrace_pages->next) {
955 /* allocate another page */
957 (void *)get_zeroed_page(GFP_KERNEL);
958 if (!ftrace_pages->next)
961 ftrace_pages = ftrace_pages->next;
964 return &ftrace_pages->records[ftrace_pages->index++];
967 static struct dyn_ftrace *
968 ftrace_record_ip(unsigned long ip)
970 struct dyn_ftrace *rec;
975 rec = ftrace_alloc_dyn_node(ip);
980 rec->newlist = ftrace_new_addrs;
981 ftrace_new_addrs = rec;
986 static void print_ip_ins(const char *fmt, unsigned char *p)
990 printk(KERN_CONT "%s", fmt);
992 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
993 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
996 static void ftrace_bug(int failed, unsigned long ip)
1000 FTRACE_WARN_ON_ONCE(1);
1001 pr_info("ftrace faulted on modifying ");
1005 FTRACE_WARN_ON_ONCE(1);
1006 pr_info("ftrace failed to modify ");
1008 print_ip_ins(" actual: ", (unsigned char *)ip);
1009 printk(KERN_CONT "\n");
1012 FTRACE_WARN_ON_ONCE(1);
1013 pr_info("ftrace faulted on writing ");
1017 FTRACE_WARN_ON_ONCE(1);
1018 pr_info("ftrace faulted on unknown error ");
1025 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1027 unsigned long ftrace_addr;
1028 unsigned long flag = 0UL;
1030 ftrace_addr = (unsigned long)FTRACE_ADDR;
1033 * If this record is not to be traced or we want to disable it,
1036 * If we want to enable it and filtering is off, then enable it.
1038 * If we want to enable it and filtering is on, enable it only if
1041 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1042 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1043 flag = FTRACE_FL_ENABLED;
1046 /* If the state of this record hasn't changed, then do nothing */
1047 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1051 rec->flags |= FTRACE_FL_ENABLED;
1052 return ftrace_make_call(rec, ftrace_addr);
1055 rec->flags &= ~FTRACE_FL_ENABLED;
1056 return ftrace_make_nop(NULL, rec, ftrace_addr);
1059 static void ftrace_replace_code(int enable)
1061 struct dyn_ftrace *rec;
1062 struct ftrace_page *pg;
1065 do_for_each_ftrace_rec(pg, rec) {
1067 * Skip over free records, records that have
1068 * failed and not converted.
1070 if (rec->flags & FTRACE_FL_FREE ||
1071 rec->flags & FTRACE_FL_FAILED ||
1072 !(rec->flags & FTRACE_FL_CONVERTED))
1075 /* ignore updates to this record's mcount site */
1076 if (get_kprobe((void *)rec->ip)) {
1080 unfreeze_record(rec);
1083 failed = __ftrace_replace_code(rec, enable);
1085 rec->flags |= FTRACE_FL_FAILED;
1086 ftrace_bug(failed, rec->ip);
1087 /* Stop processing */
1090 } while_for_each_ftrace_rec();
1094 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1101 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1103 ftrace_bug(ret, ip);
1104 rec->flags |= FTRACE_FL_FAILED;
1111 * archs can override this function if they must do something
1112 * before the modifying code is performed.
1114 int __weak ftrace_arch_code_modify_prepare(void)
1120 * archs can override this function if they must do something
1121 * after the modifying code is performed.
1123 int __weak ftrace_arch_code_modify_post_process(void)
1128 static int __ftrace_modify_code(void *data)
1130 int *command = data;
1132 if (*command & FTRACE_ENABLE_CALLS)
1133 ftrace_replace_code(1);
1134 else if (*command & FTRACE_DISABLE_CALLS)
1135 ftrace_replace_code(0);
1137 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1138 ftrace_update_ftrace_func(ftrace_trace_function);
1140 if (*command & FTRACE_START_FUNC_RET)
1141 ftrace_enable_ftrace_graph_caller();
1142 else if (*command & FTRACE_STOP_FUNC_RET)
1143 ftrace_disable_ftrace_graph_caller();
1148 static void ftrace_run_update_code(int command)
1152 ret = ftrace_arch_code_modify_prepare();
1153 FTRACE_WARN_ON(ret);
1157 stop_machine(__ftrace_modify_code, &command, NULL);
1159 ret = ftrace_arch_code_modify_post_process();
1160 FTRACE_WARN_ON(ret);
1163 static ftrace_func_t saved_ftrace_func;
1164 static int ftrace_start_up;
1166 static void ftrace_startup_enable(int command)
1168 if (saved_ftrace_func != ftrace_trace_function) {
1169 saved_ftrace_func = ftrace_trace_function;
1170 command |= FTRACE_UPDATE_TRACE_FUNC;
1173 if (!command || !ftrace_enabled)
1176 ftrace_run_update_code(command);
1179 static void ftrace_startup(int command)
1181 if (unlikely(ftrace_disabled))
1185 command |= FTRACE_ENABLE_CALLS;
1187 ftrace_startup_enable(command);
1190 static void ftrace_shutdown(int command)
1192 if (unlikely(ftrace_disabled))
1197 * Just warn in case of unbalance, no need to kill ftrace, it's not
1198 * critical but the ftrace_call callers may be never nopped again after
1199 * further ftrace uses.
1201 WARN_ON_ONCE(ftrace_start_up < 0);
1203 if (!ftrace_start_up)
1204 command |= FTRACE_DISABLE_CALLS;
1206 if (saved_ftrace_func != ftrace_trace_function) {
1207 saved_ftrace_func = ftrace_trace_function;
1208 command |= FTRACE_UPDATE_TRACE_FUNC;
1211 if (!command || !ftrace_enabled)
1214 ftrace_run_update_code(command);
1217 static void ftrace_startup_sysctl(void)
1219 int command = FTRACE_ENABLE_MCOUNT;
1221 if (unlikely(ftrace_disabled))
1224 /* Force update next time */
1225 saved_ftrace_func = NULL;
1226 /* ftrace_start_up is true if we want ftrace running */
1227 if (ftrace_start_up)
1228 command |= FTRACE_ENABLE_CALLS;
1230 ftrace_run_update_code(command);
1233 static void ftrace_shutdown_sysctl(void)
1235 int command = FTRACE_DISABLE_MCOUNT;
1237 if (unlikely(ftrace_disabled))
1240 /* ftrace_start_up is true if ftrace is running */
1241 if (ftrace_start_up)
1242 command |= FTRACE_DISABLE_CALLS;
1244 ftrace_run_update_code(command);
1247 static cycle_t ftrace_update_time;
1248 static unsigned long ftrace_update_cnt;
1249 unsigned long ftrace_update_tot_cnt;
1251 static int ftrace_update_code(struct module *mod)
1253 struct dyn_ftrace *p;
1254 cycle_t start, stop;
1256 start = ftrace_now(raw_smp_processor_id());
1257 ftrace_update_cnt = 0;
1259 while (ftrace_new_addrs) {
1261 /* If something went wrong, bail without enabling anything */
1262 if (unlikely(ftrace_disabled))
1265 p = ftrace_new_addrs;
1266 ftrace_new_addrs = p->newlist;
1270 * Do the initial record convertion from mcount jump
1271 * to the NOP instructions.
1273 if (!ftrace_code_disable(mod, p)) {
1278 p->flags |= FTRACE_FL_CONVERTED;
1279 ftrace_update_cnt++;
1282 * If the tracing is enabled, go ahead and enable the record.
1284 * The reason not to enable the record immediatelly is the
1285 * inherent check of ftrace_make_nop/ftrace_make_call for
1286 * correct previous instructions. Making first the NOP
1287 * conversion puts the module to the correct state, thus
1288 * passing the ftrace_make_call check.
1290 if (ftrace_start_up) {
1291 int failed = __ftrace_replace_code(p, 1);
1293 ftrace_bug(failed, p->ip);
1299 stop = ftrace_now(raw_smp_processor_id());
1300 ftrace_update_time = stop - start;
1301 ftrace_update_tot_cnt += ftrace_update_cnt;
1306 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1308 struct ftrace_page *pg;
1312 /* allocate a few pages */
1313 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1314 if (!ftrace_pages_start)
1318 * Allocate a few more pages.
1320 * TODO: have some parser search vmlinux before
1321 * final linking to find all calls to ftrace.
1323 * a) know how many pages to allocate.
1325 * b) set up the table then.
1327 * The dynamic code is still necessary for
1331 pg = ftrace_pages = ftrace_pages_start;
1333 cnt = num_to_init / ENTRIES_PER_PAGE;
1334 pr_info("ftrace: allocating %ld entries in %d pages\n",
1335 num_to_init, cnt + 1);
1337 for (i = 0; i < cnt; i++) {
1338 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1340 /* If we fail, we'll try later anyway */
1351 FTRACE_ITER_FILTER = (1 << 0),
1352 FTRACE_ITER_NOTRACE = (1 << 1),
1353 FTRACE_ITER_FAILURES = (1 << 2),
1354 FTRACE_ITER_PRINTALL = (1 << 3),
1355 FTRACE_ITER_HASH = (1 << 4),
1358 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1360 struct ftrace_iterator {
1361 struct ftrace_page *pg;
1365 struct trace_parser parser;
1369 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1371 struct ftrace_iterator *iter = m->private;
1372 struct hlist_node *hnd = v;
1373 struct hlist_head *hhd;
1375 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1380 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1383 hhd = &ftrace_func_hash[iter->hidx];
1385 if (hlist_empty(hhd)) {
1404 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1406 struct ftrace_iterator *iter = m->private;
1410 if (!(iter->flags & FTRACE_ITER_HASH))
1413 iter->flags |= FTRACE_ITER_HASH;
1416 for (l = 0; l <= *pos; ) {
1417 p = t_hash_next(m, p, &l);
1424 static int t_hash_show(struct seq_file *m, void *v)
1426 struct ftrace_func_probe *rec;
1427 struct hlist_node *hnd = v;
1429 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1431 if (rec->ops->print)
1432 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1434 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1437 seq_printf(m, ":%p", rec->data);
1444 t_next(struct seq_file *m, void *v, loff_t *pos)
1446 struct ftrace_iterator *iter = m->private;
1447 struct dyn_ftrace *rec = NULL;
1449 if (iter->flags & FTRACE_ITER_HASH)
1450 return t_hash_next(m, v, pos);
1454 if (iter->flags & FTRACE_ITER_PRINTALL)
1458 if (iter->idx >= iter->pg->index) {
1459 if (iter->pg->next) {
1460 iter->pg = iter->pg->next;
1465 rec = &iter->pg->records[iter->idx++];
1466 if ((rec->flags & FTRACE_FL_FREE) ||
1468 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1469 (rec->flags & FTRACE_FL_FAILED)) ||
1471 ((iter->flags & FTRACE_ITER_FAILURES) &&
1472 !(rec->flags & FTRACE_FL_FAILED)) ||
1474 ((iter->flags & FTRACE_ITER_FILTER) &&
1475 !(rec->flags & FTRACE_FL_FILTER)) ||
1477 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1478 !(rec->flags & FTRACE_FL_NOTRACE))) {
1487 static void *t_start(struct seq_file *m, loff_t *pos)
1489 struct ftrace_iterator *iter = m->private;
1493 mutex_lock(&ftrace_lock);
1495 * For set_ftrace_filter reading, if we have the filter
1496 * off, we can short cut and just print out that all
1497 * functions are enabled.
1499 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1501 return t_hash_start(m, pos);
1502 iter->flags |= FTRACE_ITER_PRINTALL;
1506 if (iter->flags & FTRACE_ITER_HASH)
1507 return t_hash_start(m, pos);
1509 iter->pg = ftrace_pages_start;
1511 for (l = 0; l <= *pos; ) {
1512 p = t_next(m, p, &l);
1517 if (!p && iter->flags & FTRACE_ITER_FILTER)
1518 return t_hash_start(m, pos);
1523 static void t_stop(struct seq_file *m, void *p)
1525 mutex_unlock(&ftrace_lock);
1528 static int t_show(struct seq_file *m, void *v)
1530 struct ftrace_iterator *iter = m->private;
1531 struct dyn_ftrace *rec = v;
1533 if (iter->flags & FTRACE_ITER_HASH)
1534 return t_hash_show(m, v);
1536 if (iter->flags & FTRACE_ITER_PRINTALL) {
1537 seq_printf(m, "#### all functions enabled ####\n");
1544 seq_printf(m, "%ps\n", (void *)rec->ip);
1549 static const struct seq_operations show_ftrace_seq_ops = {
1557 ftrace_avail_open(struct inode *inode, struct file *file)
1559 struct ftrace_iterator *iter;
1562 if (unlikely(ftrace_disabled))
1565 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1569 iter->pg = ftrace_pages_start;
1571 ret = seq_open(file, &show_ftrace_seq_ops);
1573 struct seq_file *m = file->private_data;
1584 ftrace_failures_open(struct inode *inode, struct file *file)
1588 struct ftrace_iterator *iter;
1590 ret = ftrace_avail_open(inode, file);
1592 m = (struct seq_file *)file->private_data;
1593 iter = (struct ftrace_iterator *)m->private;
1594 iter->flags = FTRACE_ITER_FAILURES;
1601 static void ftrace_filter_reset(int enable)
1603 struct ftrace_page *pg;
1604 struct dyn_ftrace *rec;
1605 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1607 mutex_lock(&ftrace_lock);
1609 ftrace_filtered = 0;
1610 do_for_each_ftrace_rec(pg, rec) {
1611 if (rec->flags & FTRACE_FL_FAILED)
1613 rec->flags &= ~type;
1614 } while_for_each_ftrace_rec();
1615 mutex_unlock(&ftrace_lock);
1619 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1621 struct ftrace_iterator *iter;
1624 if (unlikely(ftrace_disabled))
1627 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1631 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1636 mutex_lock(&ftrace_regex_lock);
1637 if ((file->f_mode & FMODE_WRITE) &&
1638 (file->f_flags & O_TRUNC))
1639 ftrace_filter_reset(enable);
1641 if (file->f_mode & FMODE_READ) {
1642 iter->pg = ftrace_pages_start;
1643 iter->flags = enable ? FTRACE_ITER_FILTER :
1644 FTRACE_ITER_NOTRACE;
1646 ret = seq_open(file, &show_ftrace_seq_ops);
1648 struct seq_file *m = file->private_data;
1651 trace_parser_put(&iter->parser);
1655 file->private_data = iter;
1656 mutex_unlock(&ftrace_regex_lock);
1662 ftrace_filter_open(struct inode *inode, struct file *file)
1664 return ftrace_regex_open(inode, file, 1);
1668 ftrace_notrace_open(struct inode *inode, struct file *file)
1670 return ftrace_regex_open(inode, file, 0);
1674 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1678 if (file->f_mode & FMODE_READ)
1679 ret = seq_lseek(file, offset, origin);
1681 file->f_pos = ret = 1;
1686 static int ftrace_match(char *str, char *regex, int len, int type)
1693 if (strcmp(str, regex) == 0)
1696 case MATCH_FRONT_ONLY:
1697 if (strncmp(str, regex, len) == 0)
1700 case MATCH_MIDDLE_ONLY:
1701 if (strstr(str, regex))
1704 case MATCH_END_ONLY:
1706 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
1715 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1717 char str[KSYM_SYMBOL_LEN];
1719 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1720 return ftrace_match(str, regex, len, type);
1723 static int ftrace_match_records(char *buff, int len, int enable)
1725 unsigned int search_len;
1726 struct ftrace_page *pg;
1727 struct dyn_ftrace *rec;
1734 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1735 type = filter_parse_regex(buff, len, &search, ¬);
1737 search_len = strlen(search);
1739 mutex_lock(&ftrace_lock);
1740 do_for_each_ftrace_rec(pg, rec) {
1742 if (rec->flags & FTRACE_FL_FAILED)
1745 if (ftrace_match_record(rec, search, search_len, type)) {
1747 rec->flags &= ~flag;
1753 * Only enable filtering if we have a function that
1756 if (enable && (rec->flags & FTRACE_FL_FILTER))
1757 ftrace_filtered = 1;
1758 } while_for_each_ftrace_rec();
1759 mutex_unlock(&ftrace_lock);
1765 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1766 char *regex, int len, int type)
1768 char str[KSYM_SYMBOL_LEN];
1771 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1773 if (!modname || strcmp(modname, mod))
1776 /* blank search means to match all funcs in the mod */
1778 return ftrace_match(str, regex, len, type);
1783 static int ftrace_match_module_records(char *buff, char *mod, int enable)
1785 unsigned search_len = 0;
1786 struct ftrace_page *pg;
1787 struct dyn_ftrace *rec;
1788 int type = MATCH_FULL;
1789 char *search = buff;
1794 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1796 /* blank or '*' mean the same */
1797 if (strcmp(buff, "*") == 0)
1800 /* handle the case of 'dont filter this module' */
1801 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1807 type = filter_parse_regex(buff, strlen(buff), &search, ¬);
1808 search_len = strlen(search);
1811 mutex_lock(&ftrace_lock);
1812 do_for_each_ftrace_rec(pg, rec) {
1814 if (rec->flags & FTRACE_FL_FAILED)
1817 if (ftrace_match_module_record(rec, mod,
1818 search, search_len, type)) {
1820 rec->flags &= ~flag;
1825 if (enable && (rec->flags & FTRACE_FL_FILTER))
1826 ftrace_filtered = 1;
1828 } while_for_each_ftrace_rec();
1829 mutex_unlock(&ftrace_lock);
1835 * We register the module command as a template to show others how
1836 * to register the a command as well.
1840 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1845 * cmd == 'mod' because we only registered this func
1846 * for the 'mod' ftrace_func_command.
1847 * But if you register one func with multiple commands,
1848 * you can tell which command was used by the cmd
1852 /* we must have a module name */
1856 mod = strsep(¶m, ":");
1860 if (ftrace_match_module_records(func, mod, enable))
1865 static struct ftrace_func_command ftrace_mod_cmd = {
1867 .func = ftrace_mod_callback,
1870 static int __init ftrace_mod_cmd_init(void)
1872 return register_ftrace_command(&ftrace_mod_cmd);
1874 device_initcall(ftrace_mod_cmd_init);
1877 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1879 struct ftrace_func_probe *entry;
1880 struct hlist_head *hhd;
1881 struct hlist_node *n;
1885 key = hash_long(ip, FTRACE_HASH_BITS);
1887 hhd = &ftrace_func_hash[key];
1889 if (hlist_empty(hhd))
1893 * Disable preemption for these calls to prevent a RCU grace
1894 * period. This syncs the hash iteration and freeing of items
1895 * on the hash. rcu_read_lock is too dangerous here.
1897 resched = ftrace_preempt_disable();
1898 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1899 if (entry->ip == ip)
1900 entry->ops->func(ip, parent_ip, &entry->data);
1902 ftrace_preempt_enable(resched);
1905 static struct ftrace_ops trace_probe_ops __read_mostly =
1907 .func = function_trace_probe_call,
1910 static int ftrace_probe_registered;
1912 static void __enable_ftrace_function_probe(void)
1916 if (ftrace_probe_registered)
1919 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1920 struct hlist_head *hhd = &ftrace_func_hash[i];
1924 /* Nothing registered? */
1925 if (i == FTRACE_FUNC_HASHSIZE)
1928 __register_ftrace_function(&trace_probe_ops);
1930 ftrace_probe_registered = 1;
1933 static void __disable_ftrace_function_probe(void)
1937 if (!ftrace_probe_registered)
1940 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1941 struct hlist_head *hhd = &ftrace_func_hash[i];
1946 /* no more funcs left */
1947 __unregister_ftrace_function(&trace_probe_ops);
1949 ftrace_probe_registered = 0;
1953 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1955 struct ftrace_func_probe *entry =
1956 container_of(rhp, struct ftrace_func_probe, rcu);
1958 if (entry->ops->free)
1959 entry->ops->free(&entry->data);
1965 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1968 struct ftrace_func_probe *entry;
1969 struct ftrace_page *pg;
1970 struct dyn_ftrace *rec;
1976 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
1977 len = strlen(search);
1979 /* we do not support '!' for function probes */
1983 mutex_lock(&ftrace_lock);
1984 do_for_each_ftrace_rec(pg, rec) {
1986 if (rec->flags & FTRACE_FL_FAILED)
1989 if (!ftrace_match_record(rec, search, len, type))
1992 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1994 /* If we did not process any, then return error */
2005 * The caller might want to do something special
2006 * for each function we find. We call the callback
2007 * to give the caller an opportunity to do so.
2009 if (ops->callback) {
2010 if (ops->callback(rec->ip, &entry->data) < 0) {
2011 /* caller does not like this func */
2018 entry->ip = rec->ip;
2020 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2021 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2023 } while_for_each_ftrace_rec();
2024 __enable_ftrace_function_probe();
2027 mutex_unlock(&ftrace_lock);
2033 PROBE_TEST_FUNC = 1,
2038 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2039 void *data, int flags)
2041 struct ftrace_func_probe *entry;
2042 struct hlist_node *n, *tmp;
2043 char str[KSYM_SYMBOL_LEN];
2044 int type = MATCH_FULL;
2048 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2053 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2054 len = strlen(search);
2056 /* we do not support '!' for function probes */
2061 mutex_lock(&ftrace_lock);
2062 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2063 struct hlist_head *hhd = &ftrace_func_hash[i];
2065 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2067 /* break up if statements for readability */
2068 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2071 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2074 /* do this last, since it is the most expensive */
2076 kallsyms_lookup(entry->ip, NULL, NULL,
2078 if (!ftrace_match(str, glob, len, type))
2082 hlist_del(&entry->node);
2083 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2086 __disable_ftrace_function_probe();
2087 mutex_unlock(&ftrace_lock);
2091 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2094 __unregister_ftrace_function_probe(glob, ops, data,
2095 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2099 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2101 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2104 void unregister_ftrace_function_probe_all(char *glob)
2106 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2109 static LIST_HEAD(ftrace_commands);
2110 static DEFINE_MUTEX(ftrace_cmd_mutex);
2112 int register_ftrace_command(struct ftrace_func_command *cmd)
2114 struct ftrace_func_command *p;
2117 mutex_lock(&ftrace_cmd_mutex);
2118 list_for_each_entry(p, &ftrace_commands, list) {
2119 if (strcmp(cmd->name, p->name) == 0) {
2124 list_add(&cmd->list, &ftrace_commands);
2126 mutex_unlock(&ftrace_cmd_mutex);
2131 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2133 struct ftrace_func_command *p, *n;
2136 mutex_lock(&ftrace_cmd_mutex);
2137 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2138 if (strcmp(cmd->name, p->name) == 0) {
2140 list_del_init(&p->list);
2145 mutex_unlock(&ftrace_cmd_mutex);
2150 static int ftrace_process_regex(char *buff, int len, int enable)
2152 char *func, *command, *next = buff;
2153 struct ftrace_func_command *p;
2156 func = strsep(&next, ":");
2159 if (ftrace_match_records(func, len, enable))
2166 command = strsep(&next, ":");
2168 mutex_lock(&ftrace_cmd_mutex);
2169 list_for_each_entry(p, &ftrace_commands, list) {
2170 if (strcmp(p->name, command) == 0) {
2171 ret = p->func(func, command, next, enable);
2176 mutex_unlock(&ftrace_cmd_mutex);
2182 ftrace_regex_write(struct file *file, const char __user *ubuf,
2183 size_t cnt, loff_t *ppos, int enable)
2185 struct ftrace_iterator *iter;
2186 struct trace_parser *parser;
2192 mutex_lock(&ftrace_regex_lock);
2194 if (file->f_mode & FMODE_READ) {
2195 struct seq_file *m = file->private_data;
2198 iter = file->private_data;
2200 parser = &iter->parser;
2201 read = trace_get_user(parser, ubuf, cnt, ppos);
2203 if (read >= 0 && trace_parser_loaded(parser) &&
2204 !trace_parser_cont(parser)) {
2205 ret = ftrace_process_regex(parser->buffer,
2206 parser->idx, enable);
2207 trace_parser_clear(parser);
2214 mutex_unlock(&ftrace_regex_lock);
2220 ftrace_filter_write(struct file *file, const char __user *ubuf,
2221 size_t cnt, loff_t *ppos)
2223 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2227 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2228 size_t cnt, loff_t *ppos)
2230 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2234 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2236 if (unlikely(ftrace_disabled))
2239 mutex_lock(&ftrace_regex_lock);
2241 ftrace_filter_reset(enable);
2243 ftrace_match_records(buf, len, enable);
2244 mutex_unlock(&ftrace_regex_lock);
2248 * ftrace_set_filter - set a function to filter on in ftrace
2249 * @buf - the string that holds the function filter text.
2250 * @len - the length of the string.
2251 * @reset - non zero to reset all filters before applying this filter.
2253 * Filters denote which functions should be enabled when tracing is enabled.
2254 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2256 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2258 ftrace_set_regex(buf, len, reset, 1);
2262 * ftrace_set_notrace - set a function to not trace in ftrace
2263 * @buf - the string that holds the function notrace text.
2264 * @len - the length of the string.
2265 * @reset - non zero to reset all filters before applying this filter.
2267 * Notrace Filters denote which functions should not be enabled when tracing
2268 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2271 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2273 ftrace_set_regex(buf, len, reset, 0);
2277 * command line interface to allow users to set filters on boot up.
2279 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2280 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2281 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2283 static int __init set_ftrace_notrace(char *str)
2285 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2288 __setup("ftrace_notrace=", set_ftrace_notrace);
2290 static int __init set_ftrace_filter(char *str)
2292 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2295 __setup("ftrace_filter=", set_ftrace_filter);
2297 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2298 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2299 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2301 static int __init set_graph_function(char *str)
2303 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2306 __setup("ftrace_graph_filter=", set_graph_function);
2308 static void __init set_ftrace_early_graph(char *buf)
2314 func = strsep(&buf, ",");
2315 /* we allow only one expression at a time */
2316 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2319 printk(KERN_DEBUG "ftrace: function %s not "
2320 "traceable\n", func);
2323 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2325 static void __init set_ftrace_early_filter(char *buf, int enable)
2330 func = strsep(&buf, ",");
2331 ftrace_set_regex(func, strlen(func), 0, enable);
2335 static void __init set_ftrace_early_filters(void)
2337 if (ftrace_filter_buf[0])
2338 set_ftrace_early_filter(ftrace_filter_buf, 1);
2339 if (ftrace_notrace_buf[0])
2340 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2341 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2342 if (ftrace_graph_buf[0])
2343 set_ftrace_early_graph(ftrace_graph_buf);
2344 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2348 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2350 struct seq_file *m = (struct seq_file *)file->private_data;
2351 struct ftrace_iterator *iter;
2352 struct trace_parser *parser;
2354 mutex_lock(&ftrace_regex_lock);
2355 if (file->f_mode & FMODE_READ) {
2358 seq_release(inode, file);
2360 iter = file->private_data;
2362 parser = &iter->parser;
2363 if (trace_parser_loaded(parser)) {
2364 parser->buffer[parser->idx] = 0;
2365 ftrace_match_records(parser->buffer, parser->idx, enable);
2368 mutex_lock(&ftrace_lock);
2369 if (ftrace_start_up && ftrace_enabled)
2370 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2371 mutex_unlock(&ftrace_lock);
2373 trace_parser_put(parser);
2376 mutex_unlock(&ftrace_regex_lock);
2381 ftrace_filter_release(struct inode *inode, struct file *file)
2383 return ftrace_regex_release(inode, file, 1);
2387 ftrace_notrace_release(struct inode *inode, struct file *file)
2389 return ftrace_regex_release(inode, file, 0);
2392 static const struct file_operations ftrace_avail_fops = {
2393 .open = ftrace_avail_open,
2395 .llseek = seq_lseek,
2396 .release = seq_release_private,
2399 static const struct file_operations ftrace_failures_fops = {
2400 .open = ftrace_failures_open,
2402 .llseek = seq_lseek,
2403 .release = seq_release_private,
2406 static const struct file_operations ftrace_filter_fops = {
2407 .open = ftrace_filter_open,
2409 .write = ftrace_filter_write,
2410 .llseek = ftrace_regex_lseek,
2411 .release = ftrace_filter_release,
2414 static const struct file_operations ftrace_notrace_fops = {
2415 .open = ftrace_notrace_open,
2417 .write = ftrace_notrace_write,
2418 .llseek = ftrace_regex_lseek,
2419 .release = ftrace_notrace_release,
2422 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2424 static DEFINE_MUTEX(graph_lock);
2426 int ftrace_graph_count;
2427 int ftrace_graph_filter_enabled;
2428 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2431 __g_next(struct seq_file *m, loff_t *pos)
2433 if (*pos >= ftrace_graph_count)
2435 return &ftrace_graph_funcs[*pos];
2439 g_next(struct seq_file *m, void *v, loff_t *pos)
2442 return __g_next(m, pos);
2445 static void *g_start(struct seq_file *m, loff_t *pos)
2447 mutex_lock(&graph_lock);
2449 /* Nothing, tell g_show to print all functions are enabled */
2450 if (!ftrace_graph_filter_enabled && !*pos)
2453 return __g_next(m, pos);
2456 static void g_stop(struct seq_file *m, void *p)
2458 mutex_unlock(&graph_lock);
2461 static int g_show(struct seq_file *m, void *v)
2463 unsigned long *ptr = v;
2468 if (ptr == (unsigned long *)1) {
2469 seq_printf(m, "#### all functions enabled ####\n");
2473 seq_printf(m, "%ps\n", (void *)*ptr);
2478 static const struct seq_operations ftrace_graph_seq_ops = {
2486 ftrace_graph_open(struct inode *inode, struct file *file)
2490 if (unlikely(ftrace_disabled))
2493 mutex_lock(&graph_lock);
2494 if ((file->f_mode & FMODE_WRITE) &&
2495 (file->f_flags & O_TRUNC)) {
2496 ftrace_graph_filter_enabled = 0;
2497 ftrace_graph_count = 0;
2498 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2500 mutex_unlock(&graph_lock);
2502 if (file->f_mode & FMODE_READ)
2503 ret = seq_open(file, &ftrace_graph_seq_ops);
2509 ftrace_graph_release(struct inode *inode, struct file *file)
2511 if (file->f_mode & FMODE_READ)
2512 seq_release(inode, file);
2517 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2519 struct dyn_ftrace *rec;
2520 struct ftrace_page *pg;
2528 if (ftrace_disabled)
2532 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
2533 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2536 search_len = strlen(search);
2538 mutex_lock(&ftrace_lock);
2539 do_for_each_ftrace_rec(pg, rec) {
2541 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2544 if (ftrace_match_record(rec, search, search_len, type)) {
2545 /* if it is in the array */
2547 for (i = 0; i < *idx; i++) {
2548 if (array[i] == rec->ip) {
2557 array[(*idx)++] = rec->ip;
2558 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2563 array[i] = array[--(*idx)];
2569 } while_for_each_ftrace_rec();
2571 mutex_unlock(&ftrace_lock);
2576 ftrace_graph_filter_enabled = 1;
2581 ftrace_graph_write(struct file *file, const char __user *ubuf,
2582 size_t cnt, loff_t *ppos)
2584 struct trace_parser parser;
2590 mutex_lock(&graph_lock);
2592 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2597 read = trace_get_user(&parser, ubuf, cnt, ppos);
2599 if (read >= 0 && trace_parser_loaded((&parser))) {
2600 parser.buffer[parser.idx] = 0;
2602 /* we allow only one expression at a time */
2603 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2612 trace_parser_put(&parser);
2614 mutex_unlock(&graph_lock);
2619 static const struct file_operations ftrace_graph_fops = {
2620 .open = ftrace_graph_open,
2622 .write = ftrace_graph_write,
2623 .release = ftrace_graph_release,
2625 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2627 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2630 trace_create_file("available_filter_functions", 0444,
2631 d_tracer, NULL, &ftrace_avail_fops);
2633 trace_create_file("failures", 0444,
2634 d_tracer, NULL, &ftrace_failures_fops);
2636 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2637 NULL, &ftrace_filter_fops);
2639 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2640 NULL, &ftrace_notrace_fops);
2642 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2643 trace_create_file("set_graph_function", 0444, d_tracer,
2645 &ftrace_graph_fops);
2646 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2651 static int ftrace_process_locs(struct module *mod,
2652 unsigned long *start,
2657 unsigned long flags;
2659 mutex_lock(&ftrace_lock);
2662 addr = ftrace_call_adjust(*p++);
2664 * Some architecture linkers will pad between
2665 * the different mcount_loc sections of different
2666 * object files to satisfy alignments.
2667 * Skip any NULL pointers.
2671 ftrace_record_ip(addr);
2674 /* disable interrupts to prevent kstop machine */
2675 local_irq_save(flags);
2676 ftrace_update_code(mod);
2677 local_irq_restore(flags);
2678 mutex_unlock(&ftrace_lock);
2683 #ifdef CONFIG_MODULES
2684 void ftrace_release_mod(struct module *mod)
2686 struct dyn_ftrace *rec;
2687 struct ftrace_page *pg;
2689 if (ftrace_disabled)
2692 mutex_lock(&ftrace_lock);
2693 do_for_each_ftrace_rec(pg, rec) {
2694 if (within_module_core(rec->ip, mod)) {
2696 * rec->ip is changed in ftrace_free_rec()
2697 * It should not between s and e if record was freed.
2699 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2700 ftrace_free_rec(rec);
2702 } while_for_each_ftrace_rec();
2703 mutex_unlock(&ftrace_lock);
2706 static void ftrace_init_module(struct module *mod,
2707 unsigned long *start, unsigned long *end)
2709 if (ftrace_disabled || start == end)
2711 ftrace_process_locs(mod, start, end);
2714 static int ftrace_module_notify(struct notifier_block *self,
2715 unsigned long val, void *data)
2717 struct module *mod = data;
2720 case MODULE_STATE_COMING:
2721 ftrace_init_module(mod, mod->ftrace_callsites,
2722 mod->ftrace_callsites +
2723 mod->num_ftrace_callsites);
2725 case MODULE_STATE_GOING:
2726 ftrace_release_mod(mod);
2733 static int ftrace_module_notify(struct notifier_block *self,
2734 unsigned long val, void *data)
2738 #endif /* CONFIG_MODULES */
2740 struct notifier_block ftrace_module_nb = {
2741 .notifier_call = ftrace_module_notify,
2745 extern unsigned long __start_mcount_loc[];
2746 extern unsigned long __stop_mcount_loc[];
2748 void __init ftrace_init(void)
2750 unsigned long count, addr, flags;
2753 /* Keep the ftrace pointer to the stub */
2754 addr = (unsigned long)ftrace_stub;
2756 local_irq_save(flags);
2757 ftrace_dyn_arch_init(&addr);
2758 local_irq_restore(flags);
2760 /* ftrace_dyn_arch_init places the return code in addr */
2764 count = __stop_mcount_loc - __start_mcount_loc;
2766 ret = ftrace_dyn_table_alloc(count);
2770 last_ftrace_enabled = ftrace_enabled = 1;
2772 ret = ftrace_process_locs(NULL,
2776 ret = register_module_notifier(&ftrace_module_nb);
2778 pr_warning("Failed to register trace ftrace module notifier\n");
2780 set_ftrace_early_filters();
2784 ftrace_disabled = 1;
2789 static int __init ftrace_nodyn_init(void)
2794 device_initcall(ftrace_nodyn_init);
2796 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2797 static inline void ftrace_startup_enable(int command) { }
2798 /* Keep as macros so we do not need to define the commands */
2799 # define ftrace_startup(command) do { } while (0)
2800 # define ftrace_shutdown(command) do { } while (0)
2801 # define ftrace_startup_sysctl() do { } while (0)
2802 # define ftrace_shutdown_sysctl() do { } while (0)
2803 #endif /* CONFIG_DYNAMIC_FTRACE */
2805 static void clear_ftrace_swapper(void)
2807 struct task_struct *p;
2811 for_each_online_cpu(cpu) {
2813 clear_tsk_trace_trace(p);
2818 static void set_ftrace_swapper(void)
2820 struct task_struct *p;
2824 for_each_online_cpu(cpu) {
2826 set_tsk_trace_trace(p);
2831 static void clear_ftrace_pid(struct pid *pid)
2833 struct task_struct *p;
2836 do_each_pid_task(pid, PIDTYPE_PID, p) {
2837 clear_tsk_trace_trace(p);
2838 } while_each_pid_task(pid, PIDTYPE_PID, p);
2844 static void set_ftrace_pid(struct pid *pid)
2846 struct task_struct *p;
2849 do_each_pid_task(pid, PIDTYPE_PID, p) {
2850 set_tsk_trace_trace(p);
2851 } while_each_pid_task(pid, PIDTYPE_PID, p);
2855 static void clear_ftrace_pid_task(struct pid *pid)
2857 if (pid == ftrace_swapper_pid)
2858 clear_ftrace_swapper();
2860 clear_ftrace_pid(pid);
2863 static void set_ftrace_pid_task(struct pid *pid)
2865 if (pid == ftrace_swapper_pid)
2866 set_ftrace_swapper();
2868 set_ftrace_pid(pid);
2871 static int ftrace_pid_add(int p)
2874 struct ftrace_pid *fpid;
2877 mutex_lock(&ftrace_lock);
2880 pid = ftrace_swapper_pid;
2882 pid = find_get_pid(p);
2889 list_for_each_entry(fpid, &ftrace_pids, list)
2890 if (fpid->pid == pid)
2895 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2899 list_add(&fpid->list, &ftrace_pids);
2902 set_ftrace_pid_task(pid);
2904 ftrace_update_pid_func();
2905 ftrace_startup_enable(0);
2907 mutex_unlock(&ftrace_lock);
2911 if (pid != ftrace_swapper_pid)
2915 mutex_unlock(&ftrace_lock);
2919 static void ftrace_pid_reset(void)
2921 struct ftrace_pid *fpid, *safe;
2923 mutex_lock(&ftrace_lock);
2924 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2925 struct pid *pid = fpid->pid;
2927 clear_ftrace_pid_task(pid);
2929 list_del(&fpid->list);
2933 ftrace_update_pid_func();
2934 ftrace_startup_enable(0);
2936 mutex_unlock(&ftrace_lock);
2939 static void *fpid_start(struct seq_file *m, loff_t *pos)
2941 mutex_lock(&ftrace_lock);
2943 if (list_empty(&ftrace_pids) && (!*pos))
2946 return seq_list_start(&ftrace_pids, *pos);
2949 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2954 return seq_list_next(v, &ftrace_pids, pos);
2957 static void fpid_stop(struct seq_file *m, void *p)
2959 mutex_unlock(&ftrace_lock);
2962 static int fpid_show(struct seq_file *m, void *v)
2964 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2966 if (v == (void *)1) {
2967 seq_printf(m, "no pid\n");
2971 if (fpid->pid == ftrace_swapper_pid)
2972 seq_printf(m, "swapper tasks\n");
2974 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2979 static const struct seq_operations ftrace_pid_sops = {
2980 .start = fpid_start,
2987 ftrace_pid_open(struct inode *inode, struct file *file)
2991 if ((file->f_mode & FMODE_WRITE) &&
2992 (file->f_flags & O_TRUNC))
2995 if (file->f_mode & FMODE_READ)
2996 ret = seq_open(file, &ftrace_pid_sops);
3002 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3003 size_t cnt, loff_t *ppos)
3009 if (cnt >= sizeof(buf))
3012 if (copy_from_user(&buf, ubuf, cnt))
3018 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3019 * to clean the filter quietly.
3021 tmp = strstrip(buf);
3022 if (strlen(tmp) == 0)
3025 ret = strict_strtol(tmp, 10, &val);
3029 ret = ftrace_pid_add(val);
3031 return ret ? ret : cnt;
3035 ftrace_pid_release(struct inode *inode, struct file *file)
3037 if (file->f_mode & FMODE_READ)
3038 seq_release(inode, file);
3043 static const struct file_operations ftrace_pid_fops = {
3044 .open = ftrace_pid_open,
3045 .write = ftrace_pid_write,
3047 .llseek = seq_lseek,
3048 .release = ftrace_pid_release,
3051 static __init int ftrace_init_debugfs(void)
3053 struct dentry *d_tracer;
3055 d_tracer = tracing_init_dentry();
3059 ftrace_init_dyn_debugfs(d_tracer);
3061 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3062 NULL, &ftrace_pid_fops);
3064 ftrace_profile_debugfs(d_tracer);
3068 fs_initcall(ftrace_init_debugfs);
3071 * ftrace_kill - kill ftrace
3073 * This function should be used by panic code. It stops ftrace
3074 * but in a not so nice way. If you need to simply kill ftrace
3075 * from a non-atomic section, use ftrace_kill.
3077 void ftrace_kill(void)
3079 ftrace_disabled = 1;
3081 clear_ftrace_function();
3085 * register_ftrace_function - register a function for profiling
3086 * @ops - ops structure that holds the function for profiling.
3088 * Register a function to be called by all functions in the
3091 * Note: @ops->func and all the functions it calls must be labeled
3092 * with "notrace", otherwise it will go into a
3095 int register_ftrace_function(struct ftrace_ops *ops)
3099 if (unlikely(ftrace_disabled))
3102 mutex_lock(&ftrace_lock);
3104 ret = __register_ftrace_function(ops);
3107 mutex_unlock(&ftrace_lock);
3112 * unregister_ftrace_function - unregister a function for profiling.
3113 * @ops - ops structure that holds the function to unregister
3115 * Unregister a function that was added to be called by ftrace profiling.
3117 int unregister_ftrace_function(struct ftrace_ops *ops)
3121 mutex_lock(&ftrace_lock);
3122 ret = __unregister_ftrace_function(ops);
3124 mutex_unlock(&ftrace_lock);
3130 ftrace_enable_sysctl(struct ctl_table *table, int write,
3131 void __user *buffer, size_t *lenp,
3136 if (unlikely(ftrace_disabled))
3139 mutex_lock(&ftrace_lock);
3141 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3143 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3146 last_ftrace_enabled = !!ftrace_enabled;
3148 if (ftrace_enabled) {
3150 ftrace_startup_sysctl();
3152 /* we are starting ftrace again */
3153 if (ftrace_list != &ftrace_list_end) {
3154 if (ftrace_list->next == &ftrace_list_end)
3155 ftrace_trace_function = ftrace_list->func;
3157 ftrace_trace_function = ftrace_list_func;
3161 /* stopping ftrace calls (just send to ftrace_stub) */
3162 ftrace_trace_function = ftrace_stub;
3164 ftrace_shutdown_sysctl();
3168 mutex_unlock(&ftrace_lock);
3172 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3174 static int ftrace_graph_active;
3175 static struct notifier_block ftrace_suspend_notifier;
3177 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3182 /* The callbacks that hook a function */
3183 trace_func_graph_ret_t ftrace_graph_return =
3184 (trace_func_graph_ret_t)ftrace_stub;
3185 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3187 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3188 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3192 unsigned long flags;
3193 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3194 struct task_struct *g, *t;
3196 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3197 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3198 * sizeof(struct ftrace_ret_stack),
3200 if (!ret_stack_list[i]) {
3208 read_lock_irqsave(&tasklist_lock, flags);
3209 do_each_thread(g, t) {
3215 if (t->ret_stack == NULL) {
3216 atomic_set(&t->tracing_graph_pause, 0);
3217 atomic_set(&t->trace_overrun, 0);
3218 t->curr_ret_stack = -1;
3219 /* Make sure the tasks see the -1 first: */
3221 t->ret_stack = ret_stack_list[start++];
3223 } while_each_thread(g, t);
3226 read_unlock_irqrestore(&tasklist_lock, flags);
3228 for (i = start; i < end; i++)
3229 kfree(ret_stack_list[i]);
3234 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3235 struct task_struct *next)
3237 unsigned long long timestamp;
3241 * Does the user want to count the time a function was asleep.
3242 * If so, do not update the time stamps.
3244 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3247 timestamp = trace_clock_local();
3249 prev->ftrace_timestamp = timestamp;
3251 /* only process tasks that we timestamped */
3252 if (!next->ftrace_timestamp)
3256 * Update all the counters in next to make up for the
3257 * time next was sleeping.
3259 timestamp -= next->ftrace_timestamp;
3261 for (index = next->curr_ret_stack; index >= 0; index--)
3262 next->ret_stack[index].calltime += timestamp;
3265 /* Allocate a return stack for each task */
3266 static int start_graph_tracing(void)
3268 struct ftrace_ret_stack **ret_stack_list;
3271 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3272 sizeof(struct ftrace_ret_stack *),
3275 if (!ret_stack_list)
3278 /* The cpu_boot init_task->ret_stack will never be freed */
3279 for_each_online_cpu(cpu) {
3280 if (!idle_task(cpu)->ret_stack)
3281 ftrace_graph_init_task(idle_task(cpu));
3285 ret = alloc_retstack_tasklist(ret_stack_list);
3286 } while (ret == -EAGAIN);
3289 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3291 pr_info("ftrace_graph: Couldn't activate tracepoint"
3292 " probe to kernel_sched_switch\n");
3295 kfree(ret_stack_list);
3300 * Hibernation protection.
3301 * The state of the current task is too much unstable during
3302 * suspend/restore to disk. We want to protect against that.
3305 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3309 case PM_HIBERNATION_PREPARE:
3310 pause_graph_tracing();
3313 case PM_POST_HIBERNATION:
3314 unpause_graph_tracing();
3320 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3321 trace_func_graph_ent_t entryfunc)
3325 mutex_lock(&ftrace_lock);
3327 /* we currently allow only one tracer registered at a time */
3328 if (ftrace_graph_active) {
3333 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3334 register_pm_notifier(&ftrace_suspend_notifier);
3336 ftrace_graph_active++;
3337 ret = start_graph_tracing();
3339 ftrace_graph_active--;
3343 ftrace_graph_return = retfunc;
3344 ftrace_graph_entry = entryfunc;
3346 ftrace_startup(FTRACE_START_FUNC_RET);
3349 mutex_unlock(&ftrace_lock);
3353 void unregister_ftrace_graph(void)
3355 mutex_lock(&ftrace_lock);
3357 if (unlikely(!ftrace_graph_active))
3360 ftrace_graph_active--;
3361 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3362 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3363 ftrace_graph_entry = ftrace_graph_entry_stub;
3364 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3365 unregister_pm_notifier(&ftrace_suspend_notifier);
3368 mutex_unlock(&ftrace_lock);
3371 /* Allocate a return stack for newly created task */
3372 void ftrace_graph_init_task(struct task_struct *t)
3374 /* Make sure we do not use the parent ret_stack */
3375 t->ret_stack = NULL;
3377 if (ftrace_graph_active) {
3378 struct ftrace_ret_stack *ret_stack;
3380 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3381 * sizeof(struct ftrace_ret_stack),
3385 t->curr_ret_stack = -1;
3386 atomic_set(&t->tracing_graph_pause, 0);
3387 atomic_set(&t->trace_overrun, 0);
3388 t->ftrace_timestamp = 0;
3389 /* make curr_ret_stack visable before we add the ret_stack */
3391 t->ret_stack = ret_stack;
3395 void ftrace_graph_exit_task(struct task_struct *t)
3397 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3399 t->ret_stack = NULL;
3400 /* NULL must become visible to IRQs before we free it: */
3406 void ftrace_graph_stop(void)