2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/slab.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
31 #include <linux/rcupdate.h>
33 #include <trace/events/sched.h>
35 #include <asm/ftrace.h>
36 #include <asm/setup.h>
38 #include "trace_output.h"
39 #include "trace_stat.h"
41 #define FTRACE_WARN_ON(cond) \
47 #define FTRACE_WARN_ON_ONCE(cond) \
49 if (WARN_ON_ONCE(cond)) \
53 /* hash bits for specific function selection */
54 #define FTRACE_HASH_BITS 7
55 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
57 /* ftrace_enabled is a method to turn ftrace on or off */
58 int ftrace_enabled __read_mostly;
59 static int last_ftrace_enabled;
61 /* Quick disabling of function tracer. */
62 int function_trace_stop;
64 /* List for set_ftrace_pid's pids. */
65 LIST_HEAD(ftrace_pids);
67 struct list_head list;
72 * ftrace_disabled is set when an anomaly is discovered.
73 * ftrace_disabled is much stronger than ftrace_enabled.
75 static int ftrace_disabled __read_mostly;
77 static DEFINE_MUTEX(ftrace_lock);
79 static struct ftrace_ops ftrace_list_end __read_mostly =
84 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
85 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
86 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
87 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
90 * Traverse the ftrace_list, invoking all entries. The reason that we
91 * can use rcu_dereference_raw() is that elements removed from this list
92 * are simply leaked, so there is no need to interact with a grace-period
93 * mechanism. The rcu_dereference_raw() calls are needed to handle
94 * concurrent insertions into the ftrace_list.
96 * Silly Alpha and silly pointer-speculation compiler optimizations!
98 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
100 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
102 while (op != &ftrace_list_end) {
103 op->func(ip, parent_ip);
104 op = rcu_dereference_raw(op->next); /*see above*/
108 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
110 if (!test_tsk_trace_trace(current))
113 ftrace_pid_function(ip, parent_ip);
116 static void set_ftrace_pid_function(ftrace_func_t func)
118 /* do not set ftrace_pid_function to itself! */
119 if (func != ftrace_pid_func)
120 ftrace_pid_function = func;
124 * clear_ftrace_function - reset the ftrace function
126 * This NULLs the ftrace function and in essence stops
127 * tracing. There may be lag
129 void clear_ftrace_function(void)
131 ftrace_trace_function = ftrace_stub;
132 __ftrace_trace_function = ftrace_stub;
133 ftrace_pid_function = ftrace_stub;
136 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
138 * For those archs that do not test ftrace_trace_stop in their
139 * mcount call site, we need to do it from C.
141 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
143 if (function_trace_stop)
146 __ftrace_trace_function(ip, parent_ip);
150 static int __register_ftrace_function(struct ftrace_ops *ops)
152 ops->next = ftrace_list;
154 * We are entering ops into the ftrace_list but another
155 * CPU might be walking that list. We need to make sure
156 * the ops->next pointer is valid before another CPU sees
157 * the ops pointer included into the ftrace_list.
159 rcu_assign_pointer(ftrace_list, ops);
161 if (ftrace_enabled) {
164 if (ops->next == &ftrace_list_end)
167 func = ftrace_list_func;
169 if (!list_empty(&ftrace_pids)) {
170 set_ftrace_pid_function(func);
171 func = ftrace_pid_func;
175 * For one func, simply call it directly.
176 * For more than one func, call the chain.
178 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
179 ftrace_trace_function = func;
181 __ftrace_trace_function = func;
182 ftrace_trace_function = ftrace_test_stop_func;
189 static int __unregister_ftrace_function(struct ftrace_ops *ops)
191 struct ftrace_ops **p;
194 * If we are removing the last function, then simply point
195 * to the ftrace_stub.
197 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
198 ftrace_trace_function = ftrace_stub;
199 ftrace_list = &ftrace_list_end;
203 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
212 if (ftrace_enabled) {
213 /* If we only have one func left, then call that directly */
214 if (ftrace_list->next == &ftrace_list_end) {
215 ftrace_func_t func = ftrace_list->func;
217 if (!list_empty(&ftrace_pids)) {
218 set_ftrace_pid_function(func);
219 func = ftrace_pid_func;
221 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
222 ftrace_trace_function = func;
224 __ftrace_trace_function = func;
232 static void ftrace_update_pid_func(void)
236 if (ftrace_trace_function == ftrace_stub)
239 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
240 func = ftrace_trace_function;
242 func = __ftrace_trace_function;
245 if (!list_empty(&ftrace_pids)) {
246 set_ftrace_pid_function(func);
247 func = ftrace_pid_func;
249 if (func == ftrace_pid_func)
250 func = ftrace_pid_function;
253 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
254 ftrace_trace_function = func;
256 __ftrace_trace_function = func;
260 #ifdef CONFIG_FUNCTION_PROFILER
261 struct ftrace_profile {
262 struct hlist_node node;
264 unsigned long counter;
265 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
266 unsigned long long time;
267 unsigned long long time_squared;
271 struct ftrace_profile_page {
272 struct ftrace_profile_page *next;
274 struct ftrace_profile records[];
277 struct ftrace_profile_stat {
279 struct hlist_head *hash;
280 struct ftrace_profile_page *pages;
281 struct ftrace_profile_page *start;
282 struct tracer_stat stat;
285 #define PROFILE_RECORDS_SIZE \
286 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
288 #define PROFILES_PER_PAGE \
289 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
291 static int ftrace_profile_bits __read_mostly;
292 static int ftrace_profile_enabled __read_mostly;
294 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
295 static DEFINE_MUTEX(ftrace_profile_lock);
297 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
299 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
302 function_stat_next(void *v, int idx)
304 struct ftrace_profile *rec = v;
305 struct ftrace_profile_page *pg;
307 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
313 if ((void *)rec >= (void *)&pg->records[pg->index]) {
317 rec = &pg->records[0];
325 static void *function_stat_start(struct tracer_stat *trace)
327 struct ftrace_profile_stat *stat =
328 container_of(trace, struct ftrace_profile_stat, stat);
330 if (!stat || !stat->start)
333 return function_stat_next(&stat->start->records[0], 0);
336 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
337 /* function graph compares on total time */
338 static int function_stat_cmp(void *p1, void *p2)
340 struct ftrace_profile *a = p1;
341 struct ftrace_profile *b = p2;
343 if (a->time < b->time)
345 if (a->time > b->time)
351 /* not function graph compares against hits */
352 static int function_stat_cmp(void *p1, void *p2)
354 struct ftrace_profile *a = p1;
355 struct ftrace_profile *b = p2;
357 if (a->counter < b->counter)
359 if (a->counter > b->counter)
366 static int function_stat_headers(struct seq_file *m)
368 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
369 seq_printf(m, " Function "
372 "--- ---- --- ---\n");
374 seq_printf(m, " Function Hit\n"
380 static int function_stat_show(struct seq_file *m, void *v)
382 struct ftrace_profile *rec = v;
383 char str[KSYM_SYMBOL_LEN];
385 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
386 static struct trace_seq s;
387 unsigned long long avg;
388 unsigned long long stddev;
390 mutex_lock(&ftrace_profile_lock);
392 /* we raced with function_profile_reset() */
393 if (unlikely(rec->counter == 0)) {
398 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
399 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
401 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
404 do_div(avg, rec->counter);
406 /* Sample standard deviation (s^2) */
407 if (rec->counter <= 1)
410 stddev = rec->time_squared - rec->counter * avg * avg;
412 * Divide only 1000 for ns^2 -> us^2 conversion.
413 * trace_print_graph_duration will divide 1000 again.
415 do_div(stddev, (rec->counter - 1) * 1000);
419 trace_print_graph_duration(rec->time, &s);
420 trace_seq_puts(&s, " ");
421 trace_print_graph_duration(avg, &s);
422 trace_seq_puts(&s, " ");
423 trace_print_graph_duration(stddev, &s);
424 trace_print_seq(m, &s);
428 mutex_unlock(&ftrace_profile_lock);
433 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
435 struct ftrace_profile_page *pg;
437 pg = stat->pages = stat->start;
440 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
445 memset(stat->hash, 0,
446 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
449 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
451 struct ftrace_profile_page *pg;
456 /* If we already allocated, do nothing */
460 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
464 #ifdef CONFIG_DYNAMIC_FTRACE
465 functions = ftrace_update_tot_cnt;
468 * We do not know the number of functions that exist because
469 * dynamic tracing is what counts them. With past experience
470 * we have around 20K functions. That should be more than enough.
471 * It is highly unlikely we will execute every function in
477 pg = stat->start = stat->pages;
479 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
481 for (i = 0; i < pages; i++) {
482 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
493 unsigned long tmp = (unsigned long)pg;
499 free_page((unsigned long)stat->pages);
506 static int ftrace_profile_init_cpu(int cpu)
508 struct ftrace_profile_stat *stat;
511 stat = &per_cpu(ftrace_profile_stats, cpu);
514 /* If the profile is already created, simply reset it */
515 ftrace_profile_reset(stat);
520 * We are profiling all functions, but usually only a few thousand
521 * functions are hit. We'll make a hash of 1024 items.
523 size = FTRACE_PROFILE_HASH_SIZE;
525 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
530 if (!ftrace_profile_bits) {
533 for (; size; size >>= 1)
534 ftrace_profile_bits++;
537 /* Preallocate the function profiling pages */
538 if (ftrace_profile_pages_init(stat) < 0) {
547 static int ftrace_profile_init(void)
552 for_each_online_cpu(cpu) {
553 ret = ftrace_profile_init_cpu(cpu);
561 /* interrupts must be disabled */
562 static struct ftrace_profile *
563 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
565 struct ftrace_profile *rec;
566 struct hlist_head *hhd;
567 struct hlist_node *n;
570 key = hash_long(ip, ftrace_profile_bits);
571 hhd = &stat->hash[key];
573 if (hlist_empty(hhd))
576 hlist_for_each_entry_rcu(rec, n, hhd, node) {
584 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
585 struct ftrace_profile *rec)
589 key = hash_long(rec->ip, ftrace_profile_bits);
590 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
594 * The memory is already allocated, this simply finds a new record to use.
596 static struct ftrace_profile *
597 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
599 struct ftrace_profile *rec = NULL;
601 /* prevent recursion (from NMIs) */
602 if (atomic_inc_return(&stat->disabled) != 1)
606 * Try to find the function again since an NMI
607 * could have added it
609 rec = ftrace_find_profiled_func(stat, ip);
613 if (stat->pages->index == PROFILES_PER_PAGE) {
614 if (!stat->pages->next)
616 stat->pages = stat->pages->next;
619 rec = &stat->pages->records[stat->pages->index++];
621 ftrace_add_profile(stat, rec);
624 atomic_dec(&stat->disabled);
630 function_profile_call(unsigned long ip, unsigned long parent_ip)
632 struct ftrace_profile_stat *stat;
633 struct ftrace_profile *rec;
636 if (!ftrace_profile_enabled)
639 local_irq_save(flags);
641 stat = &__get_cpu_var(ftrace_profile_stats);
642 if (!stat->hash || !ftrace_profile_enabled)
645 rec = ftrace_find_profiled_func(stat, ip);
647 rec = ftrace_profile_alloc(stat, ip);
654 local_irq_restore(flags);
657 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
658 static int profile_graph_entry(struct ftrace_graph_ent *trace)
660 function_profile_call(trace->func, 0);
664 static void profile_graph_return(struct ftrace_graph_ret *trace)
666 struct ftrace_profile_stat *stat;
667 unsigned long long calltime;
668 struct ftrace_profile *rec;
671 local_irq_save(flags);
672 stat = &__get_cpu_var(ftrace_profile_stats);
673 if (!stat->hash || !ftrace_profile_enabled)
676 /* If the calltime was zero'd ignore it */
677 if (!trace->calltime)
680 calltime = trace->rettime - trace->calltime;
682 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
685 index = trace->depth;
687 /* Append this call time to the parent time to subtract */
689 current->ret_stack[index - 1].subtime += calltime;
691 if (current->ret_stack[index].subtime < calltime)
692 calltime -= current->ret_stack[index].subtime;
697 rec = ftrace_find_profiled_func(stat, trace->func);
699 rec->time += calltime;
700 rec->time_squared += calltime * calltime;
704 local_irq_restore(flags);
707 static int register_ftrace_profiler(void)
709 return register_ftrace_graph(&profile_graph_return,
710 &profile_graph_entry);
713 static void unregister_ftrace_profiler(void)
715 unregister_ftrace_graph();
718 static struct ftrace_ops ftrace_profile_ops __read_mostly =
720 .func = function_profile_call,
723 static int register_ftrace_profiler(void)
725 return register_ftrace_function(&ftrace_profile_ops);
728 static void unregister_ftrace_profiler(void)
730 unregister_ftrace_function(&ftrace_profile_ops);
732 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
735 ftrace_profile_write(struct file *filp, const char __user *ubuf,
736 size_t cnt, loff_t *ppos)
739 char buf[64]; /* big enough to hold a number */
742 if (cnt >= sizeof(buf))
745 if (copy_from_user(&buf, ubuf, cnt))
750 ret = strict_strtoul(buf, 10, &val);
756 mutex_lock(&ftrace_profile_lock);
757 if (ftrace_profile_enabled ^ val) {
759 ret = ftrace_profile_init();
765 ret = register_ftrace_profiler();
770 ftrace_profile_enabled = 1;
772 ftrace_profile_enabled = 0;
774 * unregister_ftrace_profiler calls stop_machine
775 * so this acts like an synchronize_sched.
777 unregister_ftrace_profiler();
781 mutex_unlock(&ftrace_profile_lock);
789 ftrace_profile_read(struct file *filp, char __user *ubuf,
790 size_t cnt, loff_t *ppos)
792 char buf[64]; /* big enough to hold a number */
795 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
796 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
799 static const struct file_operations ftrace_profile_fops = {
800 .open = tracing_open_generic,
801 .read = ftrace_profile_read,
802 .write = ftrace_profile_write,
805 /* used to initialize the real stat files */
806 static struct tracer_stat function_stats __initdata = {
808 .stat_start = function_stat_start,
809 .stat_next = function_stat_next,
810 .stat_cmp = function_stat_cmp,
811 .stat_headers = function_stat_headers,
812 .stat_show = function_stat_show
815 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
817 struct ftrace_profile_stat *stat;
818 struct dentry *entry;
823 for_each_possible_cpu(cpu) {
824 stat = &per_cpu(ftrace_profile_stats, cpu);
826 /* allocate enough for function name + cpu number */
827 name = kmalloc(32, GFP_KERNEL);
830 * The files created are permanent, if something happens
831 * we still do not free memory.
834 "Could not allocate stat file for cpu %d\n",
838 stat->stat = function_stats;
839 snprintf(name, 32, "function%d", cpu);
840 stat->stat.name = name;
841 ret = register_stat_tracer(&stat->stat);
844 "Could not register function stat for cpu %d\n",
851 entry = debugfs_create_file("function_profile_enabled", 0644,
852 d_tracer, NULL, &ftrace_profile_fops);
854 pr_warning("Could not create debugfs "
855 "'function_profile_enabled' entry\n");
858 #else /* CONFIG_FUNCTION_PROFILER */
859 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
862 #endif /* CONFIG_FUNCTION_PROFILER */
864 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
866 #ifdef CONFIG_DYNAMIC_FTRACE
868 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
869 # error Dynamic ftrace depends on MCOUNT_RECORD
872 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
874 struct ftrace_func_probe {
875 struct hlist_node node;
876 struct ftrace_probe_ops *ops;
884 FTRACE_ENABLE_CALLS = (1 << 0),
885 FTRACE_DISABLE_CALLS = (1 << 1),
886 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
887 FTRACE_ENABLE_MCOUNT = (1 << 3),
888 FTRACE_DISABLE_MCOUNT = (1 << 4),
889 FTRACE_START_FUNC_RET = (1 << 5),
890 FTRACE_STOP_FUNC_RET = (1 << 6),
893 static int ftrace_filtered;
895 static struct dyn_ftrace *ftrace_new_addrs;
897 static DEFINE_MUTEX(ftrace_regex_lock);
900 struct ftrace_page *next;
902 struct dyn_ftrace records[];
905 #define ENTRIES_PER_PAGE \
906 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
908 /* estimate from running different kernels */
909 #define NR_TO_INIT 10000
911 static struct ftrace_page *ftrace_pages_start;
912 static struct ftrace_page *ftrace_pages;
914 static struct dyn_ftrace *ftrace_free_records;
917 * This is a double for. Do not use 'break' to break out of the loop,
918 * you must use a goto.
920 #define do_for_each_ftrace_rec(pg, rec) \
921 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
923 for (_____i = 0; _____i < pg->index; _____i++) { \
924 rec = &pg->records[_____i];
926 #define while_for_each_ftrace_rec() \
930 static void ftrace_free_rec(struct dyn_ftrace *rec)
932 rec->freelist = ftrace_free_records;
933 ftrace_free_records = rec;
934 rec->flags |= FTRACE_FL_FREE;
937 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
939 struct dyn_ftrace *rec;
941 /* First check for freed records */
942 if (ftrace_free_records) {
943 rec = ftrace_free_records;
945 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
946 FTRACE_WARN_ON_ONCE(1);
947 ftrace_free_records = NULL;
951 ftrace_free_records = rec->freelist;
952 memset(rec, 0, sizeof(*rec));
956 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
957 if (!ftrace_pages->next) {
958 /* allocate another page */
960 (void *)get_zeroed_page(GFP_KERNEL);
961 if (!ftrace_pages->next)
964 ftrace_pages = ftrace_pages->next;
967 return &ftrace_pages->records[ftrace_pages->index++];
970 static struct dyn_ftrace *
971 ftrace_record_ip(unsigned long ip)
973 struct dyn_ftrace *rec;
978 rec = ftrace_alloc_dyn_node(ip);
983 rec->newlist = ftrace_new_addrs;
984 ftrace_new_addrs = rec;
989 static void print_ip_ins(const char *fmt, unsigned char *p)
993 printk(KERN_CONT "%s", fmt);
995 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
996 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
999 static void ftrace_bug(int failed, unsigned long ip)
1003 FTRACE_WARN_ON_ONCE(1);
1004 pr_info("ftrace faulted on modifying ");
1008 FTRACE_WARN_ON_ONCE(1);
1009 pr_info("ftrace failed to modify ");
1011 print_ip_ins(" actual: ", (unsigned char *)ip);
1012 printk(KERN_CONT "\n");
1015 FTRACE_WARN_ON_ONCE(1);
1016 pr_info("ftrace faulted on writing ");
1020 FTRACE_WARN_ON_ONCE(1);
1021 pr_info("ftrace faulted on unknown error ");
1027 /* Return 1 if the address range is reserved for ftrace */
1028 int ftrace_text_reserved(void *start, void *end)
1030 struct dyn_ftrace *rec;
1031 struct ftrace_page *pg;
1033 do_for_each_ftrace_rec(pg, rec) {
1034 if (rec->ip <= (unsigned long)end &&
1035 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1037 } while_for_each_ftrace_rec();
1043 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1045 unsigned long ftrace_addr;
1046 unsigned long flag = 0UL;
1048 ftrace_addr = (unsigned long)FTRACE_ADDR;
1051 * If this record is not to be traced or we want to disable it,
1054 * If we want to enable it and filtering is off, then enable it.
1056 * If we want to enable it and filtering is on, enable it only if
1059 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1060 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1061 flag = FTRACE_FL_ENABLED;
1064 /* If the state of this record hasn't changed, then do nothing */
1065 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1069 rec->flags |= FTRACE_FL_ENABLED;
1070 return ftrace_make_call(rec, ftrace_addr);
1073 rec->flags &= ~FTRACE_FL_ENABLED;
1074 return ftrace_make_nop(NULL, rec, ftrace_addr);
1077 static void ftrace_replace_code(int enable)
1079 struct dyn_ftrace *rec;
1080 struct ftrace_page *pg;
1083 do_for_each_ftrace_rec(pg, rec) {
1085 * Skip over free records, records that have
1086 * failed and not converted.
1088 if (rec->flags & FTRACE_FL_FREE ||
1089 rec->flags & FTRACE_FL_FAILED ||
1090 !(rec->flags & FTRACE_FL_CONVERTED))
1093 failed = __ftrace_replace_code(rec, enable);
1095 rec->flags |= FTRACE_FL_FAILED;
1096 ftrace_bug(failed, rec->ip);
1097 /* Stop processing */
1100 } while_for_each_ftrace_rec();
1104 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1111 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1113 ftrace_bug(ret, ip);
1114 rec->flags |= FTRACE_FL_FAILED;
1121 * archs can override this function if they must do something
1122 * before the modifying code is performed.
1124 int __weak ftrace_arch_code_modify_prepare(void)
1130 * archs can override this function if they must do something
1131 * after the modifying code is performed.
1133 int __weak ftrace_arch_code_modify_post_process(void)
1138 static int __ftrace_modify_code(void *data)
1140 int *command = data;
1142 if (*command & FTRACE_ENABLE_CALLS)
1143 ftrace_replace_code(1);
1144 else if (*command & FTRACE_DISABLE_CALLS)
1145 ftrace_replace_code(0);
1147 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1148 ftrace_update_ftrace_func(ftrace_trace_function);
1150 if (*command & FTRACE_START_FUNC_RET)
1151 ftrace_enable_ftrace_graph_caller();
1152 else if (*command & FTRACE_STOP_FUNC_RET)
1153 ftrace_disable_ftrace_graph_caller();
1158 static void ftrace_run_update_code(int command)
1162 ret = ftrace_arch_code_modify_prepare();
1163 FTRACE_WARN_ON(ret);
1167 stop_machine(__ftrace_modify_code, &command, NULL);
1169 ret = ftrace_arch_code_modify_post_process();
1170 FTRACE_WARN_ON(ret);
1173 static ftrace_func_t saved_ftrace_func;
1174 static int ftrace_start_up;
1176 static void ftrace_startup_enable(int command)
1178 if (saved_ftrace_func != ftrace_trace_function) {
1179 saved_ftrace_func = ftrace_trace_function;
1180 command |= FTRACE_UPDATE_TRACE_FUNC;
1183 if (!command || !ftrace_enabled)
1186 ftrace_run_update_code(command);
1189 static void ftrace_startup(int command)
1191 if (unlikely(ftrace_disabled))
1195 command |= FTRACE_ENABLE_CALLS;
1197 ftrace_startup_enable(command);
1200 static void ftrace_shutdown(int command)
1202 if (unlikely(ftrace_disabled))
1207 * Just warn in case of unbalance, no need to kill ftrace, it's not
1208 * critical but the ftrace_call callers may be never nopped again after
1209 * further ftrace uses.
1211 WARN_ON_ONCE(ftrace_start_up < 0);
1213 if (!ftrace_start_up)
1214 command |= FTRACE_DISABLE_CALLS;
1216 if (saved_ftrace_func != ftrace_trace_function) {
1217 saved_ftrace_func = ftrace_trace_function;
1218 command |= FTRACE_UPDATE_TRACE_FUNC;
1221 if (!command || !ftrace_enabled)
1224 ftrace_run_update_code(command);
1227 static void ftrace_startup_sysctl(void)
1229 int command = FTRACE_ENABLE_MCOUNT;
1231 if (unlikely(ftrace_disabled))
1234 /* Force update next time */
1235 saved_ftrace_func = NULL;
1236 /* ftrace_start_up is true if we want ftrace running */
1237 if (ftrace_start_up)
1238 command |= FTRACE_ENABLE_CALLS;
1240 ftrace_run_update_code(command);
1243 static void ftrace_shutdown_sysctl(void)
1245 int command = FTRACE_DISABLE_MCOUNT;
1247 if (unlikely(ftrace_disabled))
1250 /* ftrace_start_up is true if ftrace is running */
1251 if (ftrace_start_up)
1252 command |= FTRACE_DISABLE_CALLS;
1254 ftrace_run_update_code(command);
1257 static cycle_t ftrace_update_time;
1258 static unsigned long ftrace_update_cnt;
1259 unsigned long ftrace_update_tot_cnt;
1261 static int ftrace_update_code(struct module *mod)
1263 struct dyn_ftrace *p;
1264 cycle_t start, stop;
1266 start = ftrace_now(raw_smp_processor_id());
1267 ftrace_update_cnt = 0;
1269 while (ftrace_new_addrs) {
1271 /* If something went wrong, bail without enabling anything */
1272 if (unlikely(ftrace_disabled))
1275 p = ftrace_new_addrs;
1276 ftrace_new_addrs = p->newlist;
1280 * Do the initial record convertion from mcount jump
1281 * to the NOP instructions.
1283 if (!ftrace_code_disable(mod, p)) {
1288 p->flags |= FTRACE_FL_CONVERTED;
1289 ftrace_update_cnt++;
1292 * If the tracing is enabled, go ahead and enable the record.
1294 * The reason not to enable the record immediatelly is the
1295 * inherent check of ftrace_make_nop/ftrace_make_call for
1296 * correct previous instructions. Making first the NOP
1297 * conversion puts the module to the correct state, thus
1298 * passing the ftrace_make_call check.
1300 if (ftrace_start_up) {
1301 int failed = __ftrace_replace_code(p, 1);
1303 ftrace_bug(failed, p->ip);
1309 stop = ftrace_now(raw_smp_processor_id());
1310 ftrace_update_time = stop - start;
1311 ftrace_update_tot_cnt += ftrace_update_cnt;
1316 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1318 struct ftrace_page *pg;
1322 /* allocate a few pages */
1323 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1324 if (!ftrace_pages_start)
1328 * Allocate a few more pages.
1330 * TODO: have some parser search vmlinux before
1331 * final linking to find all calls to ftrace.
1333 * a) know how many pages to allocate.
1335 * b) set up the table then.
1337 * The dynamic code is still necessary for
1341 pg = ftrace_pages = ftrace_pages_start;
1343 cnt = num_to_init / ENTRIES_PER_PAGE;
1344 pr_info("ftrace: allocating %ld entries in %d pages\n",
1345 num_to_init, cnt + 1);
1347 for (i = 0; i < cnt; i++) {
1348 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1350 /* If we fail, we'll try later anyway */
1361 FTRACE_ITER_FILTER = (1 << 0),
1362 FTRACE_ITER_NOTRACE = (1 << 1),
1363 FTRACE_ITER_FAILURES = (1 << 2),
1364 FTRACE_ITER_PRINTALL = (1 << 3),
1365 FTRACE_ITER_HASH = (1 << 4),
1368 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1370 struct ftrace_iterator {
1372 struct ftrace_page *pg;
1376 struct trace_parser parser;
1380 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1382 struct ftrace_iterator *iter = m->private;
1383 struct hlist_node *hnd = v;
1384 struct hlist_head *hhd;
1386 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1391 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1394 hhd = &ftrace_func_hash[iter->hidx];
1396 if (hlist_empty(hhd)) {
1415 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1417 struct ftrace_iterator *iter = m->private;
1421 if (!(iter->flags & FTRACE_ITER_HASH))
1422 iter->func_pos = *pos;
1424 if (iter->func_pos > *pos)
1427 iter->flags |= FTRACE_ITER_HASH;
1430 for (l = 0; l <= (*pos - iter->func_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;
1443 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1445 if (rec->ops->print)
1446 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1448 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1451 seq_printf(m, ":%p", rec->data);
1458 t_next(struct seq_file *m, void *v, loff_t *pos)
1460 struct ftrace_iterator *iter = m->private;
1461 struct dyn_ftrace *rec = NULL;
1463 if (iter->flags & FTRACE_ITER_HASH)
1464 return t_hash_next(m, v, pos);
1468 if (iter->flags & FTRACE_ITER_PRINTALL)
1472 if (iter->idx >= iter->pg->index) {
1473 if (iter->pg->next) {
1474 iter->pg = iter->pg->next;
1479 rec = &iter->pg->records[iter->idx++];
1480 if ((rec->flags & FTRACE_FL_FREE) ||
1482 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1483 (rec->flags & FTRACE_FL_FAILED)) ||
1485 ((iter->flags & FTRACE_ITER_FAILURES) &&
1486 !(rec->flags & FTRACE_FL_FAILED)) ||
1488 ((iter->flags & FTRACE_ITER_FILTER) &&
1489 !(rec->flags & FTRACE_FL_FILTER)) ||
1491 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1492 !(rec->flags & FTRACE_FL_NOTRACE))) {
1501 static void *t_start(struct seq_file *m, loff_t *pos)
1503 struct ftrace_iterator *iter = m->private;
1507 mutex_lock(&ftrace_lock);
1509 * For set_ftrace_filter reading, if we have the filter
1510 * off, we can short cut and just print out that all
1511 * functions are enabled.
1513 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1515 return t_hash_start(m, pos);
1516 iter->flags |= FTRACE_ITER_PRINTALL;
1517 /* reset in case of seek/pread */
1518 iter->flags &= ~FTRACE_ITER_HASH;
1522 if (iter->flags & FTRACE_ITER_HASH)
1523 return t_hash_start(m, pos);
1525 iter->pg = ftrace_pages_start;
1527 for (l = 0; l <= *pos; ) {
1528 p = t_next(m, p, &l);
1533 if (!p && iter->flags & FTRACE_ITER_FILTER)
1534 return t_hash_start(m, pos);
1539 static void t_stop(struct seq_file *m, void *p)
1541 mutex_unlock(&ftrace_lock);
1544 static int t_show(struct seq_file *m, void *v)
1546 struct ftrace_iterator *iter = m->private;
1547 struct dyn_ftrace *rec = v;
1549 if (iter->flags & FTRACE_ITER_HASH)
1550 return t_hash_show(m, v);
1552 if (iter->flags & FTRACE_ITER_PRINTALL) {
1553 seq_printf(m, "#### all functions enabled ####\n");
1560 seq_printf(m, "%ps\n", (void *)rec->ip);
1565 static const struct seq_operations show_ftrace_seq_ops = {
1573 ftrace_avail_open(struct inode *inode, struct file *file)
1575 struct ftrace_iterator *iter;
1578 if (unlikely(ftrace_disabled))
1581 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1585 iter->pg = ftrace_pages_start;
1587 ret = seq_open(file, &show_ftrace_seq_ops);
1589 struct seq_file *m = file->private_data;
1600 ftrace_failures_open(struct inode *inode, struct file *file)
1604 struct ftrace_iterator *iter;
1606 ret = ftrace_avail_open(inode, file);
1608 m = (struct seq_file *)file->private_data;
1609 iter = (struct ftrace_iterator *)m->private;
1610 iter->flags = FTRACE_ITER_FAILURES;
1617 static void ftrace_filter_reset(int enable)
1619 struct ftrace_page *pg;
1620 struct dyn_ftrace *rec;
1621 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1623 mutex_lock(&ftrace_lock);
1625 ftrace_filtered = 0;
1626 do_for_each_ftrace_rec(pg, rec) {
1627 if (rec->flags & FTRACE_FL_FAILED)
1629 rec->flags &= ~type;
1630 } while_for_each_ftrace_rec();
1631 mutex_unlock(&ftrace_lock);
1635 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1637 struct ftrace_iterator *iter;
1640 if (unlikely(ftrace_disabled))
1643 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1647 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1652 mutex_lock(&ftrace_regex_lock);
1653 if ((file->f_mode & FMODE_WRITE) &&
1654 (file->f_flags & O_TRUNC))
1655 ftrace_filter_reset(enable);
1657 if (file->f_mode & FMODE_READ) {
1658 iter->pg = ftrace_pages_start;
1659 iter->flags = enable ? FTRACE_ITER_FILTER :
1660 FTRACE_ITER_NOTRACE;
1662 ret = seq_open(file, &show_ftrace_seq_ops);
1664 struct seq_file *m = file->private_data;
1667 trace_parser_put(&iter->parser);
1671 file->private_data = iter;
1672 mutex_unlock(&ftrace_regex_lock);
1678 ftrace_filter_open(struct inode *inode, struct file *file)
1680 return ftrace_regex_open(inode, file, 1);
1684 ftrace_notrace_open(struct inode *inode, struct file *file)
1686 return ftrace_regex_open(inode, file, 0);
1690 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1694 if (file->f_mode & FMODE_READ)
1695 ret = seq_lseek(file, offset, origin);
1697 file->f_pos = ret = 1;
1702 static int ftrace_match(char *str, char *regex, int len, int type)
1709 if (strcmp(str, regex) == 0)
1712 case MATCH_FRONT_ONLY:
1713 if (strncmp(str, regex, len) == 0)
1716 case MATCH_MIDDLE_ONLY:
1717 if (strstr(str, regex))
1720 case MATCH_END_ONLY:
1722 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
1731 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1733 char str[KSYM_SYMBOL_LEN];
1735 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1736 return ftrace_match(str, regex, len, type);
1739 static int ftrace_match_records(char *buff, int len, int enable)
1741 unsigned int search_len;
1742 struct ftrace_page *pg;
1743 struct dyn_ftrace *rec;
1750 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1751 type = filter_parse_regex(buff, len, &search, ¬);
1753 search_len = strlen(search);
1755 mutex_lock(&ftrace_lock);
1756 do_for_each_ftrace_rec(pg, rec) {
1758 if (rec->flags & FTRACE_FL_FAILED)
1761 if (ftrace_match_record(rec, search, search_len, type)) {
1763 rec->flags &= ~flag;
1769 * Only enable filtering if we have a function that
1772 if (enable && (rec->flags & FTRACE_FL_FILTER))
1773 ftrace_filtered = 1;
1774 } while_for_each_ftrace_rec();
1775 mutex_unlock(&ftrace_lock);
1781 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1782 char *regex, int len, int type)
1784 char str[KSYM_SYMBOL_LEN];
1787 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1789 if (!modname || strcmp(modname, mod))
1792 /* blank search means to match all funcs in the mod */
1794 return ftrace_match(str, regex, len, type);
1799 static int ftrace_match_module_records(char *buff, char *mod, int enable)
1801 unsigned search_len = 0;
1802 struct ftrace_page *pg;
1803 struct dyn_ftrace *rec;
1804 int type = MATCH_FULL;
1805 char *search = buff;
1810 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1812 /* blank or '*' mean the same */
1813 if (strcmp(buff, "*") == 0)
1816 /* handle the case of 'dont filter this module' */
1817 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1823 type = filter_parse_regex(buff, strlen(buff), &search, ¬);
1824 search_len = strlen(search);
1827 mutex_lock(&ftrace_lock);
1828 do_for_each_ftrace_rec(pg, rec) {
1830 if (rec->flags & FTRACE_FL_FAILED)
1833 if (ftrace_match_module_record(rec, mod,
1834 search, search_len, type)) {
1836 rec->flags &= ~flag;
1841 if (enable && (rec->flags & FTRACE_FL_FILTER))
1842 ftrace_filtered = 1;
1844 } while_for_each_ftrace_rec();
1845 mutex_unlock(&ftrace_lock);
1851 * We register the module command as a template to show others how
1852 * to register the a command as well.
1856 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1861 * cmd == 'mod' because we only registered this func
1862 * for the 'mod' ftrace_func_command.
1863 * But if you register one func with multiple commands,
1864 * you can tell which command was used by the cmd
1868 /* we must have a module name */
1872 mod = strsep(¶m, ":");
1876 if (ftrace_match_module_records(func, mod, enable))
1881 static struct ftrace_func_command ftrace_mod_cmd = {
1883 .func = ftrace_mod_callback,
1886 static int __init ftrace_mod_cmd_init(void)
1888 return register_ftrace_command(&ftrace_mod_cmd);
1890 device_initcall(ftrace_mod_cmd_init);
1893 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1895 struct ftrace_func_probe *entry;
1896 struct hlist_head *hhd;
1897 struct hlist_node *n;
1900 key = hash_long(ip, FTRACE_HASH_BITS);
1902 hhd = &ftrace_func_hash[key];
1904 if (hlist_empty(hhd))
1908 * Disable preemption for these calls to prevent a RCU grace
1909 * period. This syncs the hash iteration and freeing of items
1910 * on the hash. rcu_read_lock is too dangerous here.
1912 preempt_disable_notrace();
1913 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1914 if (entry->ip == ip)
1915 entry->ops->func(ip, parent_ip, &entry->data);
1917 preempt_enable_notrace();
1920 static struct ftrace_ops trace_probe_ops __read_mostly =
1922 .func = function_trace_probe_call,
1925 static int ftrace_probe_registered;
1927 static void __enable_ftrace_function_probe(void)
1931 if (ftrace_probe_registered)
1934 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1935 struct hlist_head *hhd = &ftrace_func_hash[i];
1939 /* Nothing registered? */
1940 if (i == FTRACE_FUNC_HASHSIZE)
1943 __register_ftrace_function(&trace_probe_ops);
1945 ftrace_probe_registered = 1;
1948 static void __disable_ftrace_function_probe(void)
1952 if (!ftrace_probe_registered)
1955 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1956 struct hlist_head *hhd = &ftrace_func_hash[i];
1961 /* no more funcs left */
1962 __unregister_ftrace_function(&trace_probe_ops);
1964 ftrace_probe_registered = 0;
1968 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1970 struct ftrace_func_probe *entry =
1971 container_of(rhp, struct ftrace_func_probe, rcu);
1973 if (entry->ops->free)
1974 entry->ops->free(&entry->data);
1980 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1983 struct ftrace_func_probe *entry;
1984 struct ftrace_page *pg;
1985 struct dyn_ftrace *rec;
1991 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
1992 len = strlen(search);
1994 /* we do not support '!' for function probes */
1998 mutex_lock(&ftrace_lock);
1999 do_for_each_ftrace_rec(pg, rec) {
2001 if (rec->flags & FTRACE_FL_FAILED)
2004 if (!ftrace_match_record(rec, search, len, type))
2007 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2009 /* If we did not process any, then return error */
2020 * The caller might want to do something special
2021 * for each function we find. We call the callback
2022 * to give the caller an opportunity to do so.
2024 if (ops->callback) {
2025 if (ops->callback(rec->ip, &entry->data) < 0) {
2026 /* caller does not like this func */
2033 entry->ip = rec->ip;
2035 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2036 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2038 } while_for_each_ftrace_rec();
2039 __enable_ftrace_function_probe();
2042 mutex_unlock(&ftrace_lock);
2048 PROBE_TEST_FUNC = 1,
2053 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2054 void *data, int flags)
2056 struct ftrace_func_probe *entry;
2057 struct hlist_node *n, *tmp;
2058 char str[KSYM_SYMBOL_LEN];
2059 int type = MATCH_FULL;
2063 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2068 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2069 len = strlen(search);
2071 /* we do not support '!' for function probes */
2076 mutex_lock(&ftrace_lock);
2077 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2078 struct hlist_head *hhd = &ftrace_func_hash[i];
2080 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2082 /* break up if statements for readability */
2083 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2086 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2089 /* do this last, since it is the most expensive */
2091 kallsyms_lookup(entry->ip, NULL, NULL,
2093 if (!ftrace_match(str, glob, len, type))
2097 hlist_del(&entry->node);
2098 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2101 __disable_ftrace_function_probe();
2102 mutex_unlock(&ftrace_lock);
2106 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2109 __unregister_ftrace_function_probe(glob, ops, data,
2110 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2114 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2116 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2119 void unregister_ftrace_function_probe_all(char *glob)
2121 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2124 static LIST_HEAD(ftrace_commands);
2125 static DEFINE_MUTEX(ftrace_cmd_mutex);
2127 int register_ftrace_command(struct ftrace_func_command *cmd)
2129 struct ftrace_func_command *p;
2132 mutex_lock(&ftrace_cmd_mutex);
2133 list_for_each_entry(p, &ftrace_commands, list) {
2134 if (strcmp(cmd->name, p->name) == 0) {
2139 list_add(&cmd->list, &ftrace_commands);
2141 mutex_unlock(&ftrace_cmd_mutex);
2146 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2148 struct ftrace_func_command *p, *n;
2151 mutex_lock(&ftrace_cmd_mutex);
2152 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2153 if (strcmp(cmd->name, p->name) == 0) {
2155 list_del_init(&p->list);
2160 mutex_unlock(&ftrace_cmd_mutex);
2165 static int ftrace_process_regex(char *buff, int len, int enable)
2167 char *func, *command, *next = buff;
2168 struct ftrace_func_command *p;
2171 func = strsep(&next, ":");
2174 if (ftrace_match_records(func, len, enable))
2181 command = strsep(&next, ":");
2183 mutex_lock(&ftrace_cmd_mutex);
2184 list_for_each_entry(p, &ftrace_commands, list) {
2185 if (strcmp(p->name, command) == 0) {
2186 ret = p->func(func, command, next, enable);
2191 mutex_unlock(&ftrace_cmd_mutex);
2197 ftrace_regex_write(struct file *file, const char __user *ubuf,
2198 size_t cnt, loff_t *ppos, int enable)
2200 struct ftrace_iterator *iter;
2201 struct trace_parser *parser;
2207 mutex_lock(&ftrace_regex_lock);
2209 if (file->f_mode & FMODE_READ) {
2210 struct seq_file *m = file->private_data;
2213 iter = file->private_data;
2215 parser = &iter->parser;
2216 read = trace_get_user(parser, ubuf, cnt, ppos);
2218 if (read >= 0 && trace_parser_loaded(parser) &&
2219 !trace_parser_cont(parser)) {
2220 ret = ftrace_process_regex(parser->buffer,
2221 parser->idx, enable);
2222 trace_parser_clear(parser);
2229 mutex_unlock(&ftrace_regex_lock);
2235 ftrace_filter_write(struct file *file, const char __user *ubuf,
2236 size_t cnt, loff_t *ppos)
2238 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2242 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2243 size_t cnt, loff_t *ppos)
2245 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2249 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2251 if (unlikely(ftrace_disabled))
2254 mutex_lock(&ftrace_regex_lock);
2256 ftrace_filter_reset(enable);
2258 ftrace_match_records(buf, len, enable);
2259 mutex_unlock(&ftrace_regex_lock);
2263 * ftrace_set_filter - set a function to filter on in ftrace
2264 * @buf - the string that holds the function filter text.
2265 * @len - the length of the string.
2266 * @reset - non zero to reset all filters before applying this filter.
2268 * Filters denote which functions should be enabled when tracing is enabled.
2269 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2271 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2273 ftrace_set_regex(buf, len, reset, 1);
2277 * ftrace_set_notrace - set a function to not trace in ftrace
2278 * @buf - the string that holds the function notrace text.
2279 * @len - the length of the string.
2280 * @reset - non zero to reset all filters before applying this filter.
2282 * Notrace Filters denote which functions should not be enabled when tracing
2283 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2286 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2288 ftrace_set_regex(buf, len, reset, 0);
2292 * command line interface to allow users to set filters on boot up.
2294 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2295 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2296 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2298 static int __init set_ftrace_notrace(char *str)
2300 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2303 __setup("ftrace_notrace=", set_ftrace_notrace);
2305 static int __init set_ftrace_filter(char *str)
2307 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2310 __setup("ftrace_filter=", set_ftrace_filter);
2312 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2313 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2314 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2316 static int __init set_graph_function(char *str)
2318 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2321 __setup("ftrace_graph_filter=", set_graph_function);
2323 static void __init set_ftrace_early_graph(char *buf)
2329 func = strsep(&buf, ",");
2330 /* we allow only one expression at a time */
2331 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2334 printk(KERN_DEBUG "ftrace: function %s not "
2335 "traceable\n", func);
2338 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2340 static void __init set_ftrace_early_filter(char *buf, int enable)
2345 func = strsep(&buf, ",");
2346 ftrace_set_regex(func, strlen(func), 0, enable);
2350 static void __init set_ftrace_early_filters(void)
2352 if (ftrace_filter_buf[0])
2353 set_ftrace_early_filter(ftrace_filter_buf, 1);
2354 if (ftrace_notrace_buf[0])
2355 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2356 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2357 if (ftrace_graph_buf[0])
2358 set_ftrace_early_graph(ftrace_graph_buf);
2359 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2363 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2365 struct seq_file *m = (struct seq_file *)file->private_data;
2366 struct ftrace_iterator *iter;
2367 struct trace_parser *parser;
2369 mutex_lock(&ftrace_regex_lock);
2370 if (file->f_mode & FMODE_READ) {
2373 seq_release(inode, file);
2375 iter = file->private_data;
2377 parser = &iter->parser;
2378 if (trace_parser_loaded(parser)) {
2379 parser->buffer[parser->idx] = 0;
2380 ftrace_match_records(parser->buffer, parser->idx, enable);
2383 mutex_lock(&ftrace_lock);
2384 if (ftrace_start_up && ftrace_enabled)
2385 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2386 mutex_unlock(&ftrace_lock);
2388 trace_parser_put(parser);
2391 mutex_unlock(&ftrace_regex_lock);
2396 ftrace_filter_release(struct inode *inode, struct file *file)
2398 return ftrace_regex_release(inode, file, 1);
2402 ftrace_notrace_release(struct inode *inode, struct file *file)
2404 return ftrace_regex_release(inode, file, 0);
2407 static const struct file_operations ftrace_avail_fops = {
2408 .open = ftrace_avail_open,
2410 .llseek = seq_lseek,
2411 .release = seq_release_private,
2414 static const struct file_operations ftrace_failures_fops = {
2415 .open = ftrace_failures_open,
2417 .llseek = seq_lseek,
2418 .release = seq_release_private,
2421 static const struct file_operations ftrace_filter_fops = {
2422 .open = ftrace_filter_open,
2424 .write = ftrace_filter_write,
2425 .llseek = no_llseek,
2426 .release = ftrace_filter_release,
2429 static const struct file_operations ftrace_notrace_fops = {
2430 .open = ftrace_notrace_open,
2432 .write = ftrace_notrace_write,
2433 .llseek = ftrace_regex_lseek,
2434 .release = ftrace_notrace_release,
2437 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2439 static DEFINE_MUTEX(graph_lock);
2441 int ftrace_graph_count;
2442 int ftrace_graph_filter_enabled;
2443 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2446 __g_next(struct seq_file *m, loff_t *pos)
2448 if (*pos >= ftrace_graph_count)
2450 return &ftrace_graph_funcs[*pos];
2454 g_next(struct seq_file *m, void *v, loff_t *pos)
2457 return __g_next(m, pos);
2460 static void *g_start(struct seq_file *m, loff_t *pos)
2462 mutex_lock(&graph_lock);
2464 /* Nothing, tell g_show to print all functions are enabled */
2465 if (!ftrace_graph_filter_enabled && !*pos)
2468 return __g_next(m, pos);
2471 static void g_stop(struct seq_file *m, void *p)
2473 mutex_unlock(&graph_lock);
2476 static int g_show(struct seq_file *m, void *v)
2478 unsigned long *ptr = v;
2483 if (ptr == (unsigned long *)1) {
2484 seq_printf(m, "#### all functions enabled ####\n");
2488 seq_printf(m, "%ps\n", (void *)*ptr);
2493 static const struct seq_operations ftrace_graph_seq_ops = {
2501 ftrace_graph_open(struct inode *inode, struct file *file)
2505 if (unlikely(ftrace_disabled))
2508 mutex_lock(&graph_lock);
2509 if ((file->f_mode & FMODE_WRITE) &&
2510 (file->f_flags & O_TRUNC)) {
2511 ftrace_graph_filter_enabled = 0;
2512 ftrace_graph_count = 0;
2513 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2515 mutex_unlock(&graph_lock);
2517 if (file->f_mode & FMODE_READ)
2518 ret = seq_open(file, &ftrace_graph_seq_ops);
2524 ftrace_graph_release(struct inode *inode, struct file *file)
2526 if (file->f_mode & FMODE_READ)
2527 seq_release(inode, file);
2532 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2534 struct dyn_ftrace *rec;
2535 struct ftrace_page *pg;
2543 if (ftrace_disabled)
2547 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
2548 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2551 search_len = strlen(search);
2553 mutex_lock(&ftrace_lock);
2554 do_for_each_ftrace_rec(pg, rec) {
2556 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2559 if (ftrace_match_record(rec, search, search_len, type)) {
2560 /* if it is in the array */
2562 for (i = 0; i < *idx; i++) {
2563 if (array[i] == rec->ip) {
2572 array[(*idx)++] = rec->ip;
2573 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2578 array[i] = array[--(*idx)];
2584 } while_for_each_ftrace_rec();
2586 mutex_unlock(&ftrace_lock);
2591 ftrace_graph_filter_enabled = 1;
2596 ftrace_graph_write(struct file *file, const char __user *ubuf,
2597 size_t cnt, loff_t *ppos)
2599 struct trace_parser parser;
2605 mutex_lock(&graph_lock);
2607 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2612 read = trace_get_user(&parser, ubuf, cnt, ppos);
2614 if (read >= 0 && trace_parser_loaded((&parser))) {
2615 parser.buffer[parser.idx] = 0;
2617 /* we allow only one expression at a time */
2618 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2627 trace_parser_put(&parser);
2629 mutex_unlock(&graph_lock);
2634 static const struct file_operations ftrace_graph_fops = {
2635 .open = ftrace_graph_open,
2637 .write = ftrace_graph_write,
2638 .release = ftrace_graph_release,
2640 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2642 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2645 trace_create_file("available_filter_functions", 0444,
2646 d_tracer, NULL, &ftrace_avail_fops);
2648 trace_create_file("failures", 0444,
2649 d_tracer, NULL, &ftrace_failures_fops);
2651 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2652 NULL, &ftrace_filter_fops);
2654 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2655 NULL, &ftrace_notrace_fops);
2657 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2658 trace_create_file("set_graph_function", 0444, d_tracer,
2660 &ftrace_graph_fops);
2661 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2666 static int ftrace_process_locs(struct module *mod,
2667 unsigned long *start,
2672 unsigned long flags;
2674 mutex_lock(&ftrace_lock);
2677 addr = ftrace_call_adjust(*p++);
2679 * Some architecture linkers will pad between
2680 * the different mcount_loc sections of different
2681 * object files to satisfy alignments.
2682 * Skip any NULL pointers.
2686 ftrace_record_ip(addr);
2689 /* disable interrupts to prevent kstop machine */
2690 local_irq_save(flags);
2691 ftrace_update_code(mod);
2692 local_irq_restore(flags);
2693 mutex_unlock(&ftrace_lock);
2698 #ifdef CONFIG_MODULES
2699 void ftrace_release_mod(struct module *mod)
2701 struct dyn_ftrace *rec;
2702 struct ftrace_page *pg;
2704 if (ftrace_disabled)
2707 mutex_lock(&ftrace_lock);
2708 do_for_each_ftrace_rec(pg, rec) {
2709 if (within_module_core(rec->ip, mod)) {
2711 * rec->ip is changed in ftrace_free_rec()
2712 * It should not between s and e if record was freed.
2714 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2715 ftrace_free_rec(rec);
2717 } while_for_each_ftrace_rec();
2718 mutex_unlock(&ftrace_lock);
2721 static void ftrace_init_module(struct module *mod,
2722 unsigned long *start, unsigned long *end)
2724 if (ftrace_disabled || start == end)
2726 ftrace_process_locs(mod, start, end);
2729 static int ftrace_module_notify(struct notifier_block *self,
2730 unsigned long val, void *data)
2732 struct module *mod = data;
2735 case MODULE_STATE_COMING:
2736 ftrace_init_module(mod, mod->ftrace_callsites,
2737 mod->ftrace_callsites +
2738 mod->num_ftrace_callsites);
2740 case MODULE_STATE_GOING:
2741 ftrace_release_mod(mod);
2748 static int ftrace_module_notify(struct notifier_block *self,
2749 unsigned long val, void *data)
2753 #endif /* CONFIG_MODULES */
2755 struct notifier_block ftrace_module_nb = {
2756 .notifier_call = ftrace_module_notify,
2760 extern unsigned long __start_mcount_loc[];
2761 extern unsigned long __stop_mcount_loc[];
2763 void __init ftrace_init(void)
2765 unsigned long count, addr, flags;
2768 /* Keep the ftrace pointer to the stub */
2769 addr = (unsigned long)ftrace_stub;
2771 local_irq_save(flags);
2772 ftrace_dyn_arch_init(&addr);
2773 local_irq_restore(flags);
2775 /* ftrace_dyn_arch_init places the return code in addr */
2779 count = __stop_mcount_loc - __start_mcount_loc;
2781 ret = ftrace_dyn_table_alloc(count);
2785 last_ftrace_enabled = ftrace_enabled = 1;
2787 ret = ftrace_process_locs(NULL,
2791 ret = register_module_notifier(&ftrace_module_nb);
2793 pr_warning("Failed to register trace ftrace module notifier\n");
2795 set_ftrace_early_filters();
2799 ftrace_disabled = 1;
2804 static int __init ftrace_nodyn_init(void)
2809 device_initcall(ftrace_nodyn_init);
2811 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2812 static inline void ftrace_startup_enable(int command) { }
2813 /* Keep as macros so we do not need to define the commands */
2814 # define ftrace_startup(command) do { } while (0)
2815 # define ftrace_shutdown(command) do { } while (0)
2816 # define ftrace_startup_sysctl() do { } while (0)
2817 # define ftrace_shutdown_sysctl() do { } while (0)
2818 #endif /* CONFIG_DYNAMIC_FTRACE */
2820 static void clear_ftrace_swapper(void)
2822 struct task_struct *p;
2826 for_each_online_cpu(cpu) {
2828 clear_tsk_trace_trace(p);
2833 static void set_ftrace_swapper(void)
2835 struct task_struct *p;
2839 for_each_online_cpu(cpu) {
2841 set_tsk_trace_trace(p);
2846 static void clear_ftrace_pid(struct pid *pid)
2848 struct task_struct *p;
2851 do_each_pid_task(pid, PIDTYPE_PID, p) {
2852 clear_tsk_trace_trace(p);
2853 } while_each_pid_task(pid, PIDTYPE_PID, p);
2859 static void set_ftrace_pid(struct pid *pid)
2861 struct task_struct *p;
2864 do_each_pid_task(pid, PIDTYPE_PID, p) {
2865 set_tsk_trace_trace(p);
2866 } while_each_pid_task(pid, PIDTYPE_PID, p);
2870 static void clear_ftrace_pid_task(struct pid *pid)
2872 if (pid == ftrace_swapper_pid)
2873 clear_ftrace_swapper();
2875 clear_ftrace_pid(pid);
2878 static void set_ftrace_pid_task(struct pid *pid)
2880 if (pid == ftrace_swapper_pid)
2881 set_ftrace_swapper();
2883 set_ftrace_pid(pid);
2886 static int ftrace_pid_add(int p)
2889 struct ftrace_pid *fpid;
2892 mutex_lock(&ftrace_lock);
2895 pid = ftrace_swapper_pid;
2897 pid = find_get_pid(p);
2904 list_for_each_entry(fpid, &ftrace_pids, list)
2905 if (fpid->pid == pid)
2910 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2914 list_add(&fpid->list, &ftrace_pids);
2917 set_ftrace_pid_task(pid);
2919 ftrace_update_pid_func();
2920 ftrace_startup_enable(0);
2922 mutex_unlock(&ftrace_lock);
2926 if (pid != ftrace_swapper_pid)
2930 mutex_unlock(&ftrace_lock);
2934 static void ftrace_pid_reset(void)
2936 struct ftrace_pid *fpid, *safe;
2938 mutex_lock(&ftrace_lock);
2939 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2940 struct pid *pid = fpid->pid;
2942 clear_ftrace_pid_task(pid);
2944 list_del(&fpid->list);
2948 ftrace_update_pid_func();
2949 ftrace_startup_enable(0);
2951 mutex_unlock(&ftrace_lock);
2954 static void *fpid_start(struct seq_file *m, loff_t *pos)
2956 mutex_lock(&ftrace_lock);
2958 if (list_empty(&ftrace_pids) && (!*pos))
2961 return seq_list_start(&ftrace_pids, *pos);
2964 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2969 return seq_list_next(v, &ftrace_pids, pos);
2972 static void fpid_stop(struct seq_file *m, void *p)
2974 mutex_unlock(&ftrace_lock);
2977 static int fpid_show(struct seq_file *m, void *v)
2979 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2981 if (v == (void *)1) {
2982 seq_printf(m, "no pid\n");
2986 if (fpid->pid == ftrace_swapper_pid)
2987 seq_printf(m, "swapper tasks\n");
2989 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2994 static const struct seq_operations ftrace_pid_sops = {
2995 .start = fpid_start,
3002 ftrace_pid_open(struct inode *inode, struct file *file)
3006 if ((file->f_mode & FMODE_WRITE) &&
3007 (file->f_flags & O_TRUNC))
3010 if (file->f_mode & FMODE_READ)
3011 ret = seq_open(file, &ftrace_pid_sops);
3017 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3018 size_t cnt, loff_t *ppos)
3024 if (cnt >= sizeof(buf))
3027 if (copy_from_user(&buf, ubuf, cnt))
3033 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3034 * to clean the filter quietly.
3036 tmp = strstrip(buf);
3037 if (strlen(tmp) == 0)
3040 ret = strict_strtol(tmp, 10, &val);
3044 ret = ftrace_pid_add(val);
3046 return ret ? ret : cnt;
3050 ftrace_pid_release(struct inode *inode, struct file *file)
3052 if (file->f_mode & FMODE_READ)
3053 seq_release(inode, file);
3058 static const struct file_operations ftrace_pid_fops = {
3059 .open = ftrace_pid_open,
3060 .write = ftrace_pid_write,
3062 .llseek = seq_lseek,
3063 .release = ftrace_pid_release,
3066 static __init int ftrace_init_debugfs(void)
3068 struct dentry *d_tracer;
3070 d_tracer = tracing_init_dentry();
3074 ftrace_init_dyn_debugfs(d_tracer);
3076 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3077 NULL, &ftrace_pid_fops);
3079 ftrace_profile_debugfs(d_tracer);
3083 fs_initcall(ftrace_init_debugfs);
3086 * ftrace_kill - kill ftrace
3088 * This function should be used by panic code. It stops ftrace
3089 * but in a not so nice way. If you need to simply kill ftrace
3090 * from a non-atomic section, use ftrace_kill.
3092 void ftrace_kill(void)
3094 ftrace_disabled = 1;
3096 clear_ftrace_function();
3100 * register_ftrace_function - register a function for profiling
3101 * @ops - ops structure that holds the function for profiling.
3103 * Register a function to be called by all functions in the
3106 * Note: @ops->func and all the functions it calls must be labeled
3107 * with "notrace", otherwise it will go into a
3110 int register_ftrace_function(struct ftrace_ops *ops)
3114 if (unlikely(ftrace_disabled))
3117 mutex_lock(&ftrace_lock);
3119 ret = __register_ftrace_function(ops);
3122 mutex_unlock(&ftrace_lock);
3127 * unregister_ftrace_function - unregister a function for profiling.
3128 * @ops - ops structure that holds the function to unregister
3130 * Unregister a function that was added to be called by ftrace profiling.
3132 int unregister_ftrace_function(struct ftrace_ops *ops)
3136 mutex_lock(&ftrace_lock);
3137 ret = __unregister_ftrace_function(ops);
3139 mutex_unlock(&ftrace_lock);
3145 ftrace_enable_sysctl(struct ctl_table *table, int write,
3146 void __user *buffer, size_t *lenp,
3151 if (unlikely(ftrace_disabled))
3154 mutex_lock(&ftrace_lock);
3156 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3158 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3161 last_ftrace_enabled = !!ftrace_enabled;
3163 if (ftrace_enabled) {
3165 ftrace_startup_sysctl();
3167 /* we are starting ftrace again */
3168 if (ftrace_list != &ftrace_list_end) {
3169 if (ftrace_list->next == &ftrace_list_end)
3170 ftrace_trace_function = ftrace_list->func;
3172 ftrace_trace_function = ftrace_list_func;
3176 /* stopping ftrace calls (just send to ftrace_stub) */
3177 ftrace_trace_function = ftrace_stub;
3179 ftrace_shutdown_sysctl();
3183 mutex_unlock(&ftrace_lock);
3187 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3189 static int ftrace_graph_active;
3190 static struct notifier_block ftrace_suspend_notifier;
3192 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3197 /* The callbacks that hook a function */
3198 trace_func_graph_ret_t ftrace_graph_return =
3199 (trace_func_graph_ret_t)ftrace_stub;
3200 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3202 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3203 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3207 unsigned long flags;
3208 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3209 struct task_struct *g, *t;
3211 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3212 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3213 * sizeof(struct ftrace_ret_stack),
3215 if (!ret_stack_list[i]) {
3223 read_lock_irqsave(&tasklist_lock, flags);
3224 do_each_thread(g, t) {
3230 if (t->ret_stack == NULL) {
3231 atomic_set(&t->tracing_graph_pause, 0);
3232 atomic_set(&t->trace_overrun, 0);
3233 t->curr_ret_stack = -1;
3234 /* Make sure the tasks see the -1 first: */
3236 t->ret_stack = ret_stack_list[start++];
3238 } while_each_thread(g, t);
3241 read_unlock_irqrestore(&tasklist_lock, flags);
3243 for (i = start; i < end; i++)
3244 kfree(ret_stack_list[i]);
3249 ftrace_graph_probe_sched_switch(void *ignore,
3250 struct task_struct *prev, struct task_struct *next)
3252 unsigned long long timestamp;
3256 * Does the user want to count the time a function was asleep.
3257 * If so, do not update the time stamps.
3259 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3262 timestamp = trace_clock_local();
3264 prev->ftrace_timestamp = timestamp;
3266 /* only process tasks that we timestamped */
3267 if (!next->ftrace_timestamp)
3271 * Update all the counters in next to make up for the
3272 * time next was sleeping.
3274 timestamp -= next->ftrace_timestamp;
3276 for (index = next->curr_ret_stack; index >= 0; index--)
3277 next->ret_stack[index].calltime += timestamp;
3280 /* Allocate a return stack for each task */
3281 static int start_graph_tracing(void)
3283 struct ftrace_ret_stack **ret_stack_list;
3286 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3287 sizeof(struct ftrace_ret_stack *),
3290 if (!ret_stack_list)
3293 /* The cpu_boot init_task->ret_stack will never be freed */
3294 for_each_online_cpu(cpu) {
3295 if (!idle_task(cpu)->ret_stack)
3296 ftrace_graph_init_task(idle_task(cpu));
3300 ret = alloc_retstack_tasklist(ret_stack_list);
3301 } while (ret == -EAGAIN);
3304 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3306 pr_info("ftrace_graph: Couldn't activate tracepoint"
3307 " probe to kernel_sched_switch\n");
3310 kfree(ret_stack_list);
3315 * Hibernation protection.
3316 * The state of the current task is too much unstable during
3317 * suspend/restore to disk. We want to protect against that.
3320 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3324 case PM_HIBERNATION_PREPARE:
3325 pause_graph_tracing();
3328 case PM_POST_HIBERNATION:
3329 unpause_graph_tracing();
3335 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3336 trace_func_graph_ent_t entryfunc)
3340 mutex_lock(&ftrace_lock);
3342 /* we currently allow only one tracer registered at a time */
3343 if (ftrace_graph_active) {
3348 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3349 register_pm_notifier(&ftrace_suspend_notifier);
3351 ftrace_graph_active++;
3352 ret = start_graph_tracing();
3354 ftrace_graph_active--;
3358 ftrace_graph_return = retfunc;
3359 ftrace_graph_entry = entryfunc;
3361 ftrace_startup(FTRACE_START_FUNC_RET);
3364 mutex_unlock(&ftrace_lock);
3368 void unregister_ftrace_graph(void)
3370 mutex_lock(&ftrace_lock);
3372 if (unlikely(!ftrace_graph_active))
3375 ftrace_graph_active--;
3376 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3377 ftrace_graph_entry = ftrace_graph_entry_stub;
3378 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3379 unregister_pm_notifier(&ftrace_suspend_notifier);
3380 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3383 mutex_unlock(&ftrace_lock);
3386 /* Allocate a return stack for newly created task */
3387 void ftrace_graph_init_task(struct task_struct *t)
3389 /* Make sure we do not use the parent ret_stack */
3390 t->ret_stack = NULL;
3391 t->curr_ret_stack = -1;
3393 if (ftrace_graph_active) {
3394 struct ftrace_ret_stack *ret_stack;
3396 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3397 * sizeof(struct ftrace_ret_stack),
3401 atomic_set(&t->tracing_graph_pause, 0);
3402 atomic_set(&t->trace_overrun, 0);
3403 t->ftrace_timestamp = 0;
3404 /* make curr_ret_stack visable before we add the ret_stack */
3406 t->ret_stack = ret_stack;
3410 void ftrace_graph_exit_task(struct task_struct *t)
3412 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3414 t->ret_stack = NULL;
3415 /* NULL must become visible to IRQs before we free it: */
3421 void ftrace_graph_stop(void)