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];
384 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
385 static DEFINE_MUTEX(mutex);
386 static struct trace_seq s;
387 unsigned long long avg;
388 unsigned long long stddev;
391 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
392 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
394 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
397 do_div(avg, rec->counter);
399 /* Sample standard deviation (s^2) */
400 if (rec->counter <= 1)
403 stddev = rec->time_squared - rec->counter * avg * avg;
405 * Divide only 1000 for ns^2 -> us^2 conversion.
406 * trace_print_graph_duration will divide 1000 again.
408 do_div(stddev, (rec->counter - 1) * 1000);
413 trace_print_graph_duration(rec->time, &s);
414 trace_seq_puts(&s, " ");
415 trace_print_graph_duration(avg, &s);
416 trace_seq_puts(&s, " ");
417 trace_print_graph_duration(stddev, &s);
418 trace_print_seq(m, &s);
419 mutex_unlock(&mutex);
426 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
428 struct ftrace_profile_page *pg;
430 pg = stat->pages = stat->start;
433 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
438 memset(stat->hash, 0,
439 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
442 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
444 struct ftrace_profile_page *pg;
449 /* If we already allocated, do nothing */
453 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
457 #ifdef CONFIG_DYNAMIC_FTRACE
458 functions = ftrace_update_tot_cnt;
461 * We do not know the number of functions that exist because
462 * dynamic tracing is what counts them. With past experience
463 * we have around 20K functions. That should be more than enough.
464 * It is highly unlikely we will execute every function in
470 pg = stat->start = stat->pages;
472 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
474 for (i = 0; i < pages; i++) {
475 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
486 unsigned long tmp = (unsigned long)pg;
492 free_page((unsigned long)stat->pages);
499 static int ftrace_profile_init_cpu(int cpu)
501 struct ftrace_profile_stat *stat;
504 stat = &per_cpu(ftrace_profile_stats, cpu);
507 /* If the profile is already created, simply reset it */
508 ftrace_profile_reset(stat);
513 * We are profiling all functions, but usually only a few thousand
514 * functions are hit. We'll make a hash of 1024 items.
516 size = FTRACE_PROFILE_HASH_SIZE;
518 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
523 if (!ftrace_profile_bits) {
526 for (; size; size >>= 1)
527 ftrace_profile_bits++;
530 /* Preallocate the function profiling pages */
531 if (ftrace_profile_pages_init(stat) < 0) {
540 static int ftrace_profile_init(void)
545 for_each_online_cpu(cpu) {
546 ret = ftrace_profile_init_cpu(cpu);
554 /* interrupts must be disabled */
555 static struct ftrace_profile *
556 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
558 struct ftrace_profile *rec;
559 struct hlist_head *hhd;
560 struct hlist_node *n;
563 key = hash_long(ip, ftrace_profile_bits);
564 hhd = &stat->hash[key];
566 if (hlist_empty(hhd))
569 hlist_for_each_entry_rcu(rec, n, hhd, node) {
577 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
578 struct ftrace_profile *rec)
582 key = hash_long(rec->ip, ftrace_profile_bits);
583 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
587 * The memory is already allocated, this simply finds a new record to use.
589 static struct ftrace_profile *
590 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
592 struct ftrace_profile *rec = NULL;
594 /* prevent recursion (from NMIs) */
595 if (atomic_inc_return(&stat->disabled) != 1)
599 * Try to find the function again since an NMI
600 * could have added it
602 rec = ftrace_find_profiled_func(stat, ip);
606 if (stat->pages->index == PROFILES_PER_PAGE) {
607 if (!stat->pages->next)
609 stat->pages = stat->pages->next;
612 rec = &stat->pages->records[stat->pages->index++];
614 ftrace_add_profile(stat, rec);
617 atomic_dec(&stat->disabled);
623 function_profile_call(unsigned long ip, unsigned long parent_ip)
625 struct ftrace_profile_stat *stat;
626 struct ftrace_profile *rec;
629 if (!ftrace_profile_enabled)
632 local_irq_save(flags);
634 stat = &__get_cpu_var(ftrace_profile_stats);
635 if (!stat->hash || !ftrace_profile_enabled)
638 rec = ftrace_find_profiled_func(stat, ip);
640 rec = ftrace_profile_alloc(stat, ip);
647 local_irq_restore(flags);
650 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
651 static int profile_graph_entry(struct ftrace_graph_ent *trace)
653 function_profile_call(trace->func, 0);
657 static void profile_graph_return(struct ftrace_graph_ret *trace)
659 struct ftrace_profile_stat *stat;
660 unsigned long long calltime;
661 struct ftrace_profile *rec;
664 local_irq_save(flags);
665 stat = &__get_cpu_var(ftrace_profile_stats);
666 if (!stat->hash || !ftrace_profile_enabled)
669 /* If the calltime was zero'd ignore it */
670 if (!trace->calltime)
673 calltime = trace->rettime - trace->calltime;
675 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
678 index = trace->depth;
680 /* Append this call time to the parent time to subtract */
682 current->ret_stack[index - 1].subtime += calltime;
684 if (current->ret_stack[index].subtime < calltime)
685 calltime -= current->ret_stack[index].subtime;
690 rec = ftrace_find_profiled_func(stat, trace->func);
692 rec->time += calltime;
693 rec->time_squared += calltime * calltime;
697 local_irq_restore(flags);
700 static int register_ftrace_profiler(void)
702 return register_ftrace_graph(&profile_graph_return,
703 &profile_graph_entry);
706 static void unregister_ftrace_profiler(void)
708 unregister_ftrace_graph();
711 static struct ftrace_ops ftrace_profile_ops __read_mostly =
713 .func = function_profile_call,
716 static int register_ftrace_profiler(void)
718 return register_ftrace_function(&ftrace_profile_ops);
721 static void unregister_ftrace_profiler(void)
723 unregister_ftrace_function(&ftrace_profile_ops);
725 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
728 ftrace_profile_write(struct file *filp, const char __user *ubuf,
729 size_t cnt, loff_t *ppos)
732 char buf[64]; /* big enough to hold a number */
735 if (cnt >= sizeof(buf))
738 if (copy_from_user(&buf, ubuf, cnt))
743 ret = strict_strtoul(buf, 10, &val);
749 mutex_lock(&ftrace_profile_lock);
750 if (ftrace_profile_enabled ^ val) {
752 ret = ftrace_profile_init();
758 ret = register_ftrace_profiler();
763 ftrace_profile_enabled = 1;
765 ftrace_profile_enabled = 0;
767 * unregister_ftrace_profiler calls stop_machine
768 * so this acts like an synchronize_sched.
770 unregister_ftrace_profiler();
774 mutex_unlock(&ftrace_profile_lock);
782 ftrace_profile_read(struct file *filp, char __user *ubuf,
783 size_t cnt, loff_t *ppos)
785 char buf[64]; /* big enough to hold a number */
788 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
789 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
792 static const struct file_operations ftrace_profile_fops = {
793 .open = tracing_open_generic,
794 .read = ftrace_profile_read,
795 .write = ftrace_profile_write,
798 /* used to initialize the real stat files */
799 static struct tracer_stat function_stats __initdata = {
801 .stat_start = function_stat_start,
802 .stat_next = function_stat_next,
803 .stat_cmp = function_stat_cmp,
804 .stat_headers = function_stat_headers,
805 .stat_show = function_stat_show
808 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
810 struct ftrace_profile_stat *stat;
811 struct dentry *entry;
816 for_each_possible_cpu(cpu) {
817 stat = &per_cpu(ftrace_profile_stats, cpu);
819 /* allocate enough for function name + cpu number */
820 name = kmalloc(32, GFP_KERNEL);
823 * The files created are permanent, if something happens
824 * we still do not free memory.
827 "Could not allocate stat file for cpu %d\n",
831 stat->stat = function_stats;
832 snprintf(name, 32, "function%d", cpu);
833 stat->stat.name = name;
834 ret = register_stat_tracer(&stat->stat);
837 "Could not register function stat for cpu %d\n",
844 entry = debugfs_create_file("function_profile_enabled", 0644,
845 d_tracer, NULL, &ftrace_profile_fops);
847 pr_warning("Could not create debugfs "
848 "'function_profile_enabled' entry\n");
851 #else /* CONFIG_FUNCTION_PROFILER */
852 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
855 #endif /* CONFIG_FUNCTION_PROFILER */
857 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
859 #ifdef CONFIG_DYNAMIC_FTRACE
861 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
862 # error Dynamic ftrace depends on MCOUNT_RECORD
865 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
867 struct ftrace_func_probe {
868 struct hlist_node node;
869 struct ftrace_probe_ops *ops;
877 FTRACE_ENABLE_CALLS = (1 << 0),
878 FTRACE_DISABLE_CALLS = (1 << 1),
879 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
880 FTRACE_ENABLE_MCOUNT = (1 << 3),
881 FTRACE_DISABLE_MCOUNT = (1 << 4),
882 FTRACE_START_FUNC_RET = (1 << 5),
883 FTRACE_STOP_FUNC_RET = (1 << 6),
886 static int ftrace_filtered;
888 static struct dyn_ftrace *ftrace_new_addrs;
890 static DEFINE_MUTEX(ftrace_regex_lock);
893 struct ftrace_page *next;
895 struct dyn_ftrace records[];
898 #define ENTRIES_PER_PAGE \
899 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
901 /* estimate from running different kernels */
902 #define NR_TO_INIT 10000
904 static struct ftrace_page *ftrace_pages_start;
905 static struct ftrace_page *ftrace_pages;
907 static struct dyn_ftrace *ftrace_free_records;
910 * This is a double for. Do not use 'break' to break out of the loop,
911 * you must use a goto.
913 #define do_for_each_ftrace_rec(pg, rec) \
914 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
916 for (_____i = 0; _____i < pg->index; _____i++) { \
917 rec = &pg->records[_____i];
919 #define while_for_each_ftrace_rec() \
923 static void ftrace_free_rec(struct dyn_ftrace *rec)
925 rec->freelist = ftrace_free_records;
926 ftrace_free_records = rec;
927 rec->flags |= FTRACE_FL_FREE;
930 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
932 struct dyn_ftrace *rec;
934 /* First check for freed records */
935 if (ftrace_free_records) {
936 rec = ftrace_free_records;
938 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
939 FTRACE_WARN_ON_ONCE(1);
940 ftrace_free_records = NULL;
944 ftrace_free_records = rec->freelist;
945 memset(rec, 0, sizeof(*rec));
949 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
950 if (!ftrace_pages->next) {
951 /* allocate another page */
953 (void *)get_zeroed_page(GFP_KERNEL);
954 if (!ftrace_pages->next)
957 ftrace_pages = ftrace_pages->next;
960 return &ftrace_pages->records[ftrace_pages->index++];
963 static struct dyn_ftrace *
964 ftrace_record_ip(unsigned long ip)
966 struct dyn_ftrace *rec;
971 rec = ftrace_alloc_dyn_node(ip);
976 rec->newlist = ftrace_new_addrs;
977 ftrace_new_addrs = rec;
982 static void print_ip_ins(const char *fmt, unsigned char *p)
986 printk(KERN_CONT "%s", fmt);
988 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
989 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
992 static void ftrace_bug(int failed, unsigned long ip)
996 FTRACE_WARN_ON_ONCE(1);
997 pr_info("ftrace faulted on modifying ");
1001 FTRACE_WARN_ON_ONCE(1);
1002 pr_info("ftrace failed to modify ");
1004 print_ip_ins(" actual: ", (unsigned char *)ip);
1005 printk(KERN_CONT "\n");
1008 FTRACE_WARN_ON_ONCE(1);
1009 pr_info("ftrace faulted on writing ");
1013 FTRACE_WARN_ON_ONCE(1);
1014 pr_info("ftrace faulted on unknown error ");
1020 /* Return 1 if the address range is reserved for ftrace */
1021 int ftrace_text_reserved(void *start, void *end)
1023 struct dyn_ftrace *rec;
1024 struct ftrace_page *pg;
1026 do_for_each_ftrace_rec(pg, rec) {
1027 if (rec->ip <= (unsigned long)end &&
1028 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1030 } while_for_each_ftrace_rec();
1036 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1038 unsigned long ftrace_addr;
1039 unsigned long flag = 0UL;
1041 ftrace_addr = (unsigned long)FTRACE_ADDR;
1044 * If this record is not to be traced or we want to disable it,
1047 * If we want to enable it and filtering is off, then enable it.
1049 * If we want to enable it and filtering is on, enable it only if
1052 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1053 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1054 flag = FTRACE_FL_ENABLED;
1057 /* If the state of this record hasn't changed, then do nothing */
1058 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1062 rec->flags |= FTRACE_FL_ENABLED;
1063 return ftrace_make_call(rec, ftrace_addr);
1066 rec->flags &= ~FTRACE_FL_ENABLED;
1067 return ftrace_make_nop(NULL, rec, ftrace_addr);
1070 static void ftrace_replace_code(int enable)
1072 struct dyn_ftrace *rec;
1073 struct ftrace_page *pg;
1076 do_for_each_ftrace_rec(pg, rec) {
1078 * Skip over free records, records that have
1079 * failed and not converted.
1081 if (rec->flags & FTRACE_FL_FREE ||
1082 rec->flags & FTRACE_FL_FAILED ||
1083 !(rec->flags & FTRACE_FL_CONVERTED))
1086 failed = __ftrace_replace_code(rec, enable);
1088 rec->flags |= FTRACE_FL_FAILED;
1089 ftrace_bug(failed, rec->ip);
1090 /* Stop processing */
1093 } while_for_each_ftrace_rec();
1097 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1104 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1106 ftrace_bug(ret, ip);
1107 rec->flags |= FTRACE_FL_FAILED;
1114 * archs can override this function if they must do something
1115 * before the modifying code is performed.
1117 int __weak ftrace_arch_code_modify_prepare(void)
1123 * archs can override this function if they must do something
1124 * after the modifying code is performed.
1126 int __weak ftrace_arch_code_modify_post_process(void)
1131 static int __ftrace_modify_code(void *data)
1133 int *command = data;
1135 if (*command & FTRACE_ENABLE_CALLS)
1136 ftrace_replace_code(1);
1137 else if (*command & FTRACE_DISABLE_CALLS)
1138 ftrace_replace_code(0);
1140 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1141 ftrace_update_ftrace_func(ftrace_trace_function);
1143 if (*command & FTRACE_START_FUNC_RET)
1144 ftrace_enable_ftrace_graph_caller();
1145 else if (*command & FTRACE_STOP_FUNC_RET)
1146 ftrace_disable_ftrace_graph_caller();
1151 static void ftrace_run_update_code(int command)
1155 ret = ftrace_arch_code_modify_prepare();
1156 FTRACE_WARN_ON(ret);
1160 stop_machine(__ftrace_modify_code, &command, NULL);
1162 ret = ftrace_arch_code_modify_post_process();
1163 FTRACE_WARN_ON(ret);
1166 static ftrace_func_t saved_ftrace_func;
1167 static int ftrace_start_up;
1169 static void ftrace_startup_enable(int command)
1171 if (saved_ftrace_func != ftrace_trace_function) {
1172 saved_ftrace_func = ftrace_trace_function;
1173 command |= FTRACE_UPDATE_TRACE_FUNC;
1176 if (!command || !ftrace_enabled)
1179 ftrace_run_update_code(command);
1182 static void ftrace_startup(int command)
1184 if (unlikely(ftrace_disabled))
1188 command |= FTRACE_ENABLE_CALLS;
1190 ftrace_startup_enable(command);
1193 static void ftrace_shutdown(int command)
1195 if (unlikely(ftrace_disabled))
1200 * Just warn in case of unbalance, no need to kill ftrace, it's not
1201 * critical but the ftrace_call callers may be never nopped again after
1202 * further ftrace uses.
1204 WARN_ON_ONCE(ftrace_start_up < 0);
1206 if (!ftrace_start_up)
1207 command |= FTRACE_DISABLE_CALLS;
1209 if (saved_ftrace_func != ftrace_trace_function) {
1210 saved_ftrace_func = ftrace_trace_function;
1211 command |= FTRACE_UPDATE_TRACE_FUNC;
1214 if (!command || !ftrace_enabled)
1217 ftrace_run_update_code(command);
1220 static void ftrace_startup_sysctl(void)
1222 int command = FTRACE_ENABLE_MCOUNT;
1224 if (unlikely(ftrace_disabled))
1227 /* Force update next time */
1228 saved_ftrace_func = NULL;
1229 /* ftrace_start_up is true if we want ftrace running */
1230 if (ftrace_start_up)
1231 command |= FTRACE_ENABLE_CALLS;
1233 ftrace_run_update_code(command);
1236 static void ftrace_shutdown_sysctl(void)
1238 int command = FTRACE_DISABLE_MCOUNT;
1240 if (unlikely(ftrace_disabled))
1243 /* ftrace_start_up is true if ftrace is running */
1244 if (ftrace_start_up)
1245 command |= FTRACE_DISABLE_CALLS;
1247 ftrace_run_update_code(command);
1250 static cycle_t ftrace_update_time;
1251 static unsigned long ftrace_update_cnt;
1252 unsigned long ftrace_update_tot_cnt;
1254 static int ftrace_update_code(struct module *mod)
1256 struct dyn_ftrace *p;
1257 cycle_t start, stop;
1259 start = ftrace_now(raw_smp_processor_id());
1260 ftrace_update_cnt = 0;
1262 while (ftrace_new_addrs) {
1264 /* If something went wrong, bail without enabling anything */
1265 if (unlikely(ftrace_disabled))
1268 p = ftrace_new_addrs;
1269 ftrace_new_addrs = p->newlist;
1273 * Do the initial record convertion from mcount jump
1274 * to the NOP instructions.
1276 if (!ftrace_code_disable(mod, p)) {
1281 p->flags |= FTRACE_FL_CONVERTED;
1282 ftrace_update_cnt++;
1285 * If the tracing is enabled, go ahead and enable the record.
1287 * The reason not to enable the record immediatelly is the
1288 * inherent check of ftrace_make_nop/ftrace_make_call for
1289 * correct previous instructions. Making first the NOP
1290 * conversion puts the module to the correct state, thus
1291 * passing the ftrace_make_call check.
1293 if (ftrace_start_up) {
1294 int failed = __ftrace_replace_code(p, 1);
1296 ftrace_bug(failed, p->ip);
1302 stop = ftrace_now(raw_smp_processor_id());
1303 ftrace_update_time = stop - start;
1304 ftrace_update_tot_cnt += ftrace_update_cnt;
1309 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1311 struct ftrace_page *pg;
1315 /* allocate a few pages */
1316 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1317 if (!ftrace_pages_start)
1321 * Allocate a few more pages.
1323 * TODO: have some parser search vmlinux before
1324 * final linking to find all calls to ftrace.
1326 * a) know how many pages to allocate.
1328 * b) set up the table then.
1330 * The dynamic code is still necessary for
1334 pg = ftrace_pages = ftrace_pages_start;
1336 cnt = num_to_init / ENTRIES_PER_PAGE;
1337 pr_info("ftrace: allocating %ld entries in %d pages\n",
1338 num_to_init, cnt + 1);
1340 for (i = 0; i < cnt; i++) {
1341 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1343 /* If we fail, we'll try later anyway */
1354 FTRACE_ITER_FILTER = (1 << 0),
1355 FTRACE_ITER_NOTRACE = (1 << 1),
1356 FTRACE_ITER_FAILURES = (1 << 2),
1357 FTRACE_ITER_PRINTALL = (1 << 3),
1358 FTRACE_ITER_HASH = (1 << 4),
1361 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1363 struct ftrace_iterator {
1364 struct ftrace_page *pg;
1368 struct trace_parser parser;
1372 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1374 struct ftrace_iterator *iter = m->private;
1375 struct hlist_node *hnd = v;
1376 struct hlist_head *hhd;
1378 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1383 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1386 hhd = &ftrace_func_hash[iter->hidx];
1388 if (hlist_empty(hhd)) {
1407 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1409 struct ftrace_iterator *iter = m->private;
1413 if (!(iter->flags & FTRACE_ITER_HASH))
1416 iter->flags |= FTRACE_ITER_HASH;
1419 for (l = 0; l <= *pos; ) {
1420 p = t_hash_next(m, p, &l);
1427 static int t_hash_show(struct seq_file *m, void *v)
1429 struct ftrace_func_probe *rec;
1430 struct hlist_node *hnd = v;
1432 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1434 if (rec->ops->print)
1435 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1437 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1440 seq_printf(m, ":%p", rec->data);
1447 t_next(struct seq_file *m, void *v, loff_t *pos)
1449 struct ftrace_iterator *iter = m->private;
1450 struct dyn_ftrace *rec = NULL;
1452 if (iter->flags & FTRACE_ITER_HASH)
1453 return t_hash_next(m, v, pos);
1457 if (iter->flags & FTRACE_ITER_PRINTALL)
1461 if (iter->idx >= iter->pg->index) {
1462 if (iter->pg->next) {
1463 iter->pg = iter->pg->next;
1468 rec = &iter->pg->records[iter->idx++];
1469 if ((rec->flags & FTRACE_FL_FREE) ||
1471 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1472 (rec->flags & FTRACE_FL_FAILED)) ||
1474 ((iter->flags & FTRACE_ITER_FAILURES) &&
1475 !(rec->flags & FTRACE_FL_FAILED)) ||
1477 ((iter->flags & FTRACE_ITER_FILTER) &&
1478 !(rec->flags & FTRACE_FL_FILTER)) ||
1480 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1481 !(rec->flags & FTRACE_FL_NOTRACE))) {
1490 static void *t_start(struct seq_file *m, loff_t *pos)
1492 struct ftrace_iterator *iter = m->private;
1496 mutex_lock(&ftrace_lock);
1498 * For set_ftrace_filter reading, if we have the filter
1499 * off, we can short cut and just print out that all
1500 * functions are enabled.
1502 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1504 return t_hash_start(m, pos);
1505 iter->flags |= FTRACE_ITER_PRINTALL;
1509 if (iter->flags & FTRACE_ITER_HASH)
1510 return t_hash_start(m, pos);
1512 iter->pg = ftrace_pages_start;
1514 for (l = 0; l <= *pos; ) {
1515 p = t_next(m, p, &l);
1520 if (!p && iter->flags & FTRACE_ITER_FILTER)
1521 return t_hash_start(m, pos);
1526 static void t_stop(struct seq_file *m, void *p)
1528 mutex_unlock(&ftrace_lock);
1531 static int t_show(struct seq_file *m, void *v)
1533 struct ftrace_iterator *iter = m->private;
1534 struct dyn_ftrace *rec = v;
1536 if (iter->flags & FTRACE_ITER_HASH)
1537 return t_hash_show(m, v);
1539 if (iter->flags & FTRACE_ITER_PRINTALL) {
1540 seq_printf(m, "#### all functions enabled ####\n");
1547 seq_printf(m, "%ps\n", (void *)rec->ip);
1552 static const struct seq_operations show_ftrace_seq_ops = {
1560 ftrace_avail_open(struct inode *inode, struct file *file)
1562 struct ftrace_iterator *iter;
1565 if (unlikely(ftrace_disabled))
1568 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1572 iter->pg = ftrace_pages_start;
1574 ret = seq_open(file, &show_ftrace_seq_ops);
1576 struct seq_file *m = file->private_data;
1587 ftrace_failures_open(struct inode *inode, struct file *file)
1591 struct ftrace_iterator *iter;
1593 ret = ftrace_avail_open(inode, file);
1595 m = (struct seq_file *)file->private_data;
1596 iter = (struct ftrace_iterator *)m->private;
1597 iter->flags = FTRACE_ITER_FAILURES;
1604 static void ftrace_filter_reset(int enable)
1606 struct ftrace_page *pg;
1607 struct dyn_ftrace *rec;
1608 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1610 mutex_lock(&ftrace_lock);
1612 ftrace_filtered = 0;
1613 do_for_each_ftrace_rec(pg, rec) {
1614 if (rec->flags & FTRACE_FL_FAILED)
1616 rec->flags &= ~type;
1617 } while_for_each_ftrace_rec();
1618 mutex_unlock(&ftrace_lock);
1622 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1624 struct ftrace_iterator *iter;
1627 if (unlikely(ftrace_disabled))
1630 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1634 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1639 mutex_lock(&ftrace_regex_lock);
1640 if ((file->f_mode & FMODE_WRITE) &&
1641 (file->f_flags & O_TRUNC))
1642 ftrace_filter_reset(enable);
1644 if (file->f_mode & FMODE_READ) {
1645 iter->pg = ftrace_pages_start;
1646 iter->flags = enable ? FTRACE_ITER_FILTER :
1647 FTRACE_ITER_NOTRACE;
1649 ret = seq_open(file, &show_ftrace_seq_ops);
1651 struct seq_file *m = file->private_data;
1654 trace_parser_put(&iter->parser);
1658 file->private_data = iter;
1659 mutex_unlock(&ftrace_regex_lock);
1665 ftrace_filter_open(struct inode *inode, struct file *file)
1667 return ftrace_regex_open(inode, file, 1);
1671 ftrace_notrace_open(struct inode *inode, struct file *file)
1673 return ftrace_regex_open(inode, file, 0);
1677 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1681 if (file->f_mode & FMODE_READ)
1682 ret = seq_lseek(file, offset, origin);
1684 file->f_pos = ret = 1;
1689 static int ftrace_match(char *str, char *regex, int len, int type)
1696 if (strcmp(str, regex) == 0)
1699 case MATCH_FRONT_ONLY:
1700 if (strncmp(str, regex, len) == 0)
1703 case MATCH_MIDDLE_ONLY:
1704 if (strstr(str, regex))
1707 case MATCH_END_ONLY:
1709 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
1718 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1720 char str[KSYM_SYMBOL_LEN];
1722 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1723 return ftrace_match(str, regex, len, type);
1726 static int ftrace_match_records(char *buff, int len, int enable)
1728 unsigned int search_len;
1729 struct ftrace_page *pg;
1730 struct dyn_ftrace *rec;
1737 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1738 type = filter_parse_regex(buff, len, &search, ¬);
1740 search_len = strlen(search);
1742 mutex_lock(&ftrace_lock);
1743 do_for_each_ftrace_rec(pg, rec) {
1745 if (rec->flags & FTRACE_FL_FAILED)
1748 if (ftrace_match_record(rec, search, search_len, type)) {
1750 rec->flags &= ~flag;
1756 * Only enable filtering if we have a function that
1759 if (enable && (rec->flags & FTRACE_FL_FILTER))
1760 ftrace_filtered = 1;
1761 } while_for_each_ftrace_rec();
1762 mutex_unlock(&ftrace_lock);
1768 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1769 char *regex, int len, int type)
1771 char str[KSYM_SYMBOL_LEN];
1774 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1776 if (!modname || strcmp(modname, mod))
1779 /* blank search means to match all funcs in the mod */
1781 return ftrace_match(str, regex, len, type);
1786 static int ftrace_match_module_records(char *buff, char *mod, int enable)
1788 unsigned search_len = 0;
1789 struct ftrace_page *pg;
1790 struct dyn_ftrace *rec;
1791 int type = MATCH_FULL;
1792 char *search = buff;
1797 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1799 /* blank or '*' mean the same */
1800 if (strcmp(buff, "*") == 0)
1803 /* handle the case of 'dont filter this module' */
1804 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1810 type = filter_parse_regex(buff, strlen(buff), &search, ¬);
1811 search_len = strlen(search);
1814 mutex_lock(&ftrace_lock);
1815 do_for_each_ftrace_rec(pg, rec) {
1817 if (rec->flags & FTRACE_FL_FAILED)
1820 if (ftrace_match_module_record(rec, mod,
1821 search, search_len, type)) {
1823 rec->flags &= ~flag;
1828 if (enable && (rec->flags & FTRACE_FL_FILTER))
1829 ftrace_filtered = 1;
1831 } while_for_each_ftrace_rec();
1832 mutex_unlock(&ftrace_lock);
1838 * We register the module command as a template to show others how
1839 * to register the a command as well.
1843 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1848 * cmd == 'mod' because we only registered this func
1849 * for the 'mod' ftrace_func_command.
1850 * But if you register one func with multiple commands,
1851 * you can tell which command was used by the cmd
1855 /* we must have a module name */
1859 mod = strsep(¶m, ":");
1863 if (ftrace_match_module_records(func, mod, enable))
1868 static struct ftrace_func_command ftrace_mod_cmd = {
1870 .func = ftrace_mod_callback,
1873 static int __init ftrace_mod_cmd_init(void)
1875 return register_ftrace_command(&ftrace_mod_cmd);
1877 device_initcall(ftrace_mod_cmd_init);
1880 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1882 struct ftrace_func_probe *entry;
1883 struct hlist_head *hhd;
1884 struct hlist_node *n;
1887 key = hash_long(ip, FTRACE_HASH_BITS);
1889 hhd = &ftrace_func_hash[key];
1891 if (hlist_empty(hhd))
1895 * Disable preemption for these calls to prevent a RCU grace
1896 * period. This syncs the hash iteration and freeing of items
1897 * on the hash. rcu_read_lock is too dangerous here.
1899 preempt_disable_notrace();
1900 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1901 if (entry->ip == ip)
1902 entry->ops->func(ip, parent_ip, &entry->data);
1904 preempt_enable_notrace();
1907 static struct ftrace_ops trace_probe_ops __read_mostly =
1909 .func = function_trace_probe_call,
1912 static int ftrace_probe_registered;
1914 static void __enable_ftrace_function_probe(void)
1918 if (ftrace_probe_registered)
1921 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1922 struct hlist_head *hhd = &ftrace_func_hash[i];
1926 /* Nothing registered? */
1927 if (i == FTRACE_FUNC_HASHSIZE)
1930 __register_ftrace_function(&trace_probe_ops);
1932 ftrace_probe_registered = 1;
1935 static void __disable_ftrace_function_probe(void)
1939 if (!ftrace_probe_registered)
1942 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1943 struct hlist_head *hhd = &ftrace_func_hash[i];
1948 /* no more funcs left */
1949 __unregister_ftrace_function(&trace_probe_ops);
1951 ftrace_probe_registered = 0;
1955 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1957 struct ftrace_func_probe *entry =
1958 container_of(rhp, struct ftrace_func_probe, rcu);
1960 if (entry->ops->free)
1961 entry->ops->free(&entry->data);
1967 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1970 struct ftrace_func_probe *entry;
1971 struct ftrace_page *pg;
1972 struct dyn_ftrace *rec;
1978 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
1979 len = strlen(search);
1981 /* we do not support '!' for function probes */
1985 mutex_lock(&ftrace_lock);
1986 do_for_each_ftrace_rec(pg, rec) {
1988 if (rec->flags & FTRACE_FL_FAILED)
1991 if (!ftrace_match_record(rec, search, len, type))
1994 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1996 /* If we did not process any, then return error */
2007 * The caller might want to do something special
2008 * for each function we find. We call the callback
2009 * to give the caller an opportunity to do so.
2011 if (ops->callback) {
2012 if (ops->callback(rec->ip, &entry->data) < 0) {
2013 /* caller does not like this func */
2020 entry->ip = rec->ip;
2022 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2023 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2025 } while_for_each_ftrace_rec();
2026 __enable_ftrace_function_probe();
2029 mutex_unlock(&ftrace_lock);
2035 PROBE_TEST_FUNC = 1,
2040 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2041 void *data, int flags)
2043 struct ftrace_func_probe *entry;
2044 struct hlist_node *n, *tmp;
2045 char str[KSYM_SYMBOL_LEN];
2046 int type = MATCH_FULL;
2050 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2055 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2056 len = strlen(search);
2058 /* we do not support '!' for function probes */
2063 mutex_lock(&ftrace_lock);
2064 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2065 struct hlist_head *hhd = &ftrace_func_hash[i];
2067 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2069 /* break up if statements for readability */
2070 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2073 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2076 /* do this last, since it is the most expensive */
2078 kallsyms_lookup(entry->ip, NULL, NULL,
2080 if (!ftrace_match(str, glob, len, type))
2084 hlist_del(&entry->node);
2085 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2088 __disable_ftrace_function_probe();
2089 mutex_unlock(&ftrace_lock);
2093 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2096 __unregister_ftrace_function_probe(glob, ops, data,
2097 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2101 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2103 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2106 void unregister_ftrace_function_probe_all(char *glob)
2108 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2111 static LIST_HEAD(ftrace_commands);
2112 static DEFINE_MUTEX(ftrace_cmd_mutex);
2114 int register_ftrace_command(struct ftrace_func_command *cmd)
2116 struct ftrace_func_command *p;
2119 mutex_lock(&ftrace_cmd_mutex);
2120 list_for_each_entry(p, &ftrace_commands, list) {
2121 if (strcmp(cmd->name, p->name) == 0) {
2126 list_add(&cmd->list, &ftrace_commands);
2128 mutex_unlock(&ftrace_cmd_mutex);
2133 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2135 struct ftrace_func_command *p, *n;
2138 mutex_lock(&ftrace_cmd_mutex);
2139 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2140 if (strcmp(cmd->name, p->name) == 0) {
2142 list_del_init(&p->list);
2147 mutex_unlock(&ftrace_cmd_mutex);
2152 static int ftrace_process_regex(char *buff, int len, int enable)
2154 char *func, *command, *next = buff;
2155 struct ftrace_func_command *p;
2158 func = strsep(&next, ":");
2161 if (ftrace_match_records(func, len, enable))
2168 command = strsep(&next, ":");
2170 mutex_lock(&ftrace_cmd_mutex);
2171 list_for_each_entry(p, &ftrace_commands, list) {
2172 if (strcmp(p->name, command) == 0) {
2173 ret = p->func(func, command, next, enable);
2178 mutex_unlock(&ftrace_cmd_mutex);
2184 ftrace_regex_write(struct file *file, const char __user *ubuf,
2185 size_t cnt, loff_t *ppos, int enable)
2187 struct ftrace_iterator *iter;
2188 struct trace_parser *parser;
2194 mutex_lock(&ftrace_regex_lock);
2196 if (file->f_mode & FMODE_READ) {
2197 struct seq_file *m = file->private_data;
2200 iter = file->private_data;
2202 parser = &iter->parser;
2203 read = trace_get_user(parser, ubuf, cnt, ppos);
2205 if (read >= 0 && trace_parser_loaded(parser) &&
2206 !trace_parser_cont(parser)) {
2207 ret = ftrace_process_regex(parser->buffer,
2208 parser->idx, enable);
2209 trace_parser_clear(parser);
2216 mutex_unlock(&ftrace_regex_lock);
2222 ftrace_filter_write(struct file *file, const char __user *ubuf,
2223 size_t cnt, loff_t *ppos)
2225 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2229 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2230 size_t cnt, loff_t *ppos)
2232 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2236 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2238 if (unlikely(ftrace_disabled))
2241 mutex_lock(&ftrace_regex_lock);
2243 ftrace_filter_reset(enable);
2245 ftrace_match_records(buf, len, enable);
2246 mutex_unlock(&ftrace_regex_lock);
2250 * ftrace_set_filter - set a function to filter on in ftrace
2251 * @buf - the string that holds the function filter text.
2252 * @len - the length of the string.
2253 * @reset - non zero to reset all filters before applying this filter.
2255 * Filters denote which functions should be enabled when tracing is enabled.
2256 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2258 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2260 ftrace_set_regex(buf, len, reset, 1);
2264 * ftrace_set_notrace - set a function to not trace in ftrace
2265 * @buf - the string that holds the function notrace text.
2266 * @len - the length of the string.
2267 * @reset - non zero to reset all filters before applying this filter.
2269 * Notrace Filters denote which functions should not be enabled when tracing
2270 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2273 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2275 ftrace_set_regex(buf, len, reset, 0);
2279 * command line interface to allow users to set filters on boot up.
2281 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2282 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2283 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2285 static int __init set_ftrace_notrace(char *str)
2287 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2290 __setup("ftrace_notrace=", set_ftrace_notrace);
2292 static int __init set_ftrace_filter(char *str)
2294 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2297 __setup("ftrace_filter=", set_ftrace_filter);
2299 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2300 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2301 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2303 static int __init set_graph_function(char *str)
2305 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2308 __setup("ftrace_graph_filter=", set_graph_function);
2310 static void __init set_ftrace_early_graph(char *buf)
2316 func = strsep(&buf, ",");
2317 /* we allow only one expression at a time */
2318 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2321 printk(KERN_DEBUG "ftrace: function %s not "
2322 "traceable\n", func);
2325 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2327 static void __init set_ftrace_early_filter(char *buf, int enable)
2332 func = strsep(&buf, ",");
2333 ftrace_set_regex(func, strlen(func), 0, enable);
2337 static void __init set_ftrace_early_filters(void)
2339 if (ftrace_filter_buf[0])
2340 set_ftrace_early_filter(ftrace_filter_buf, 1);
2341 if (ftrace_notrace_buf[0])
2342 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2343 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2344 if (ftrace_graph_buf[0])
2345 set_ftrace_early_graph(ftrace_graph_buf);
2346 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2350 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2352 struct seq_file *m = (struct seq_file *)file->private_data;
2353 struct ftrace_iterator *iter;
2354 struct trace_parser *parser;
2356 mutex_lock(&ftrace_regex_lock);
2357 if (file->f_mode & FMODE_READ) {
2360 seq_release(inode, file);
2362 iter = file->private_data;
2364 parser = &iter->parser;
2365 if (trace_parser_loaded(parser)) {
2366 parser->buffer[parser->idx] = 0;
2367 ftrace_match_records(parser->buffer, parser->idx, enable);
2370 mutex_lock(&ftrace_lock);
2371 if (ftrace_start_up && ftrace_enabled)
2372 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2373 mutex_unlock(&ftrace_lock);
2375 trace_parser_put(parser);
2378 mutex_unlock(&ftrace_regex_lock);
2383 ftrace_filter_release(struct inode *inode, struct file *file)
2385 return ftrace_regex_release(inode, file, 1);
2389 ftrace_notrace_release(struct inode *inode, struct file *file)
2391 return ftrace_regex_release(inode, file, 0);
2394 static const struct file_operations ftrace_avail_fops = {
2395 .open = ftrace_avail_open,
2397 .llseek = seq_lseek,
2398 .release = seq_release_private,
2401 static const struct file_operations ftrace_failures_fops = {
2402 .open = ftrace_failures_open,
2404 .llseek = seq_lseek,
2405 .release = seq_release_private,
2408 static const struct file_operations ftrace_filter_fops = {
2409 .open = ftrace_filter_open,
2411 .write = ftrace_filter_write,
2412 .llseek = ftrace_regex_lseek,
2413 .release = ftrace_filter_release,
2416 static const struct file_operations ftrace_notrace_fops = {
2417 .open = ftrace_notrace_open,
2419 .write = ftrace_notrace_write,
2420 .llseek = ftrace_regex_lseek,
2421 .release = ftrace_notrace_release,
2424 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2426 static DEFINE_MUTEX(graph_lock);
2428 int ftrace_graph_count;
2429 int ftrace_graph_filter_enabled;
2430 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2433 __g_next(struct seq_file *m, loff_t *pos)
2435 if (*pos >= ftrace_graph_count)
2437 return &ftrace_graph_funcs[*pos];
2441 g_next(struct seq_file *m, void *v, loff_t *pos)
2444 return __g_next(m, pos);
2447 static void *g_start(struct seq_file *m, loff_t *pos)
2449 mutex_lock(&graph_lock);
2451 /* Nothing, tell g_show to print all functions are enabled */
2452 if (!ftrace_graph_filter_enabled && !*pos)
2455 return __g_next(m, pos);
2458 static void g_stop(struct seq_file *m, void *p)
2460 mutex_unlock(&graph_lock);
2463 static int g_show(struct seq_file *m, void *v)
2465 unsigned long *ptr = v;
2470 if (ptr == (unsigned long *)1) {
2471 seq_printf(m, "#### all functions enabled ####\n");
2475 seq_printf(m, "%ps\n", (void *)*ptr);
2480 static const struct seq_operations ftrace_graph_seq_ops = {
2488 ftrace_graph_open(struct inode *inode, struct file *file)
2492 if (unlikely(ftrace_disabled))
2495 mutex_lock(&graph_lock);
2496 if ((file->f_mode & FMODE_WRITE) &&
2497 (file->f_flags & O_TRUNC)) {
2498 ftrace_graph_filter_enabled = 0;
2499 ftrace_graph_count = 0;
2500 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2502 mutex_unlock(&graph_lock);
2504 if (file->f_mode & FMODE_READ)
2505 ret = seq_open(file, &ftrace_graph_seq_ops);
2511 ftrace_graph_release(struct inode *inode, struct file *file)
2513 if (file->f_mode & FMODE_READ)
2514 seq_release(inode, file);
2519 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2521 struct dyn_ftrace *rec;
2522 struct ftrace_page *pg;
2530 if (ftrace_disabled)
2534 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
2535 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2538 search_len = strlen(search);
2540 mutex_lock(&ftrace_lock);
2541 do_for_each_ftrace_rec(pg, rec) {
2543 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2546 if (ftrace_match_record(rec, search, search_len, type)) {
2547 /* if it is in the array */
2549 for (i = 0; i < *idx; i++) {
2550 if (array[i] == rec->ip) {
2559 array[(*idx)++] = rec->ip;
2560 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2565 array[i] = array[--(*idx)];
2571 } while_for_each_ftrace_rec();
2573 mutex_unlock(&ftrace_lock);
2578 ftrace_graph_filter_enabled = 1;
2583 ftrace_graph_write(struct file *file, const char __user *ubuf,
2584 size_t cnt, loff_t *ppos)
2586 struct trace_parser parser;
2592 mutex_lock(&graph_lock);
2594 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2599 read = trace_get_user(&parser, ubuf, cnt, ppos);
2601 if (read >= 0 && trace_parser_loaded((&parser))) {
2602 parser.buffer[parser.idx] = 0;
2604 /* we allow only one expression at a time */
2605 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2614 trace_parser_put(&parser);
2616 mutex_unlock(&graph_lock);
2621 static const struct file_operations ftrace_graph_fops = {
2622 .open = ftrace_graph_open,
2624 .write = ftrace_graph_write,
2625 .release = ftrace_graph_release,
2627 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2629 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2632 trace_create_file("available_filter_functions", 0444,
2633 d_tracer, NULL, &ftrace_avail_fops);
2635 trace_create_file("failures", 0444,
2636 d_tracer, NULL, &ftrace_failures_fops);
2638 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2639 NULL, &ftrace_filter_fops);
2641 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2642 NULL, &ftrace_notrace_fops);
2644 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2645 trace_create_file("set_graph_function", 0444, d_tracer,
2647 &ftrace_graph_fops);
2648 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2653 static int ftrace_process_locs(struct module *mod,
2654 unsigned long *start,
2659 unsigned long flags;
2661 mutex_lock(&ftrace_lock);
2664 addr = ftrace_call_adjust(*p++);
2666 * Some architecture linkers will pad between
2667 * the different mcount_loc sections of different
2668 * object files to satisfy alignments.
2669 * Skip any NULL pointers.
2673 ftrace_record_ip(addr);
2676 /* disable interrupts to prevent kstop machine */
2677 local_irq_save(flags);
2678 ftrace_update_code(mod);
2679 local_irq_restore(flags);
2680 mutex_unlock(&ftrace_lock);
2685 #ifdef CONFIG_MODULES
2686 void ftrace_release_mod(struct module *mod)
2688 struct dyn_ftrace *rec;
2689 struct ftrace_page *pg;
2691 if (ftrace_disabled)
2694 mutex_lock(&ftrace_lock);
2695 do_for_each_ftrace_rec(pg, rec) {
2696 if (within_module_core(rec->ip, mod)) {
2698 * rec->ip is changed in ftrace_free_rec()
2699 * It should not between s and e if record was freed.
2701 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2702 ftrace_free_rec(rec);
2704 } while_for_each_ftrace_rec();
2705 mutex_unlock(&ftrace_lock);
2708 static void ftrace_init_module(struct module *mod,
2709 unsigned long *start, unsigned long *end)
2711 if (ftrace_disabled || start == end)
2713 ftrace_process_locs(mod, start, end);
2716 static int ftrace_module_notify(struct notifier_block *self,
2717 unsigned long val, void *data)
2719 struct module *mod = data;
2722 case MODULE_STATE_COMING:
2723 ftrace_init_module(mod, mod->ftrace_callsites,
2724 mod->ftrace_callsites +
2725 mod->num_ftrace_callsites);
2727 case MODULE_STATE_GOING:
2728 ftrace_release_mod(mod);
2735 static int ftrace_module_notify(struct notifier_block *self,
2736 unsigned long val, void *data)
2740 #endif /* CONFIG_MODULES */
2742 struct notifier_block ftrace_module_nb = {
2743 .notifier_call = ftrace_module_notify,
2747 extern unsigned long __start_mcount_loc[];
2748 extern unsigned long __stop_mcount_loc[];
2750 void __init ftrace_init(void)
2752 unsigned long count, addr, flags;
2755 /* Keep the ftrace pointer to the stub */
2756 addr = (unsigned long)ftrace_stub;
2758 local_irq_save(flags);
2759 ftrace_dyn_arch_init(&addr);
2760 local_irq_restore(flags);
2762 /* ftrace_dyn_arch_init places the return code in addr */
2766 count = __stop_mcount_loc - __start_mcount_loc;
2768 ret = ftrace_dyn_table_alloc(count);
2772 last_ftrace_enabled = ftrace_enabled = 1;
2774 ret = ftrace_process_locs(NULL,
2778 ret = register_module_notifier(&ftrace_module_nb);
2780 pr_warning("Failed to register trace ftrace module notifier\n");
2782 set_ftrace_early_filters();
2786 ftrace_disabled = 1;
2791 static int __init ftrace_nodyn_init(void)
2796 device_initcall(ftrace_nodyn_init);
2798 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2799 static inline void ftrace_startup_enable(int command) { }
2800 /* Keep as macros so we do not need to define the commands */
2801 # define ftrace_startup(command) do { } while (0)
2802 # define ftrace_shutdown(command) do { } while (0)
2803 # define ftrace_startup_sysctl() do { } while (0)
2804 # define ftrace_shutdown_sysctl() do { } while (0)
2805 #endif /* CONFIG_DYNAMIC_FTRACE */
2807 static void clear_ftrace_swapper(void)
2809 struct task_struct *p;
2813 for_each_online_cpu(cpu) {
2815 clear_tsk_trace_trace(p);
2820 static void set_ftrace_swapper(void)
2822 struct task_struct *p;
2826 for_each_online_cpu(cpu) {
2828 set_tsk_trace_trace(p);
2833 static void clear_ftrace_pid(struct pid *pid)
2835 struct task_struct *p;
2838 do_each_pid_task(pid, PIDTYPE_PID, p) {
2839 clear_tsk_trace_trace(p);
2840 } while_each_pid_task(pid, PIDTYPE_PID, p);
2846 static void set_ftrace_pid(struct pid *pid)
2848 struct task_struct *p;
2851 do_each_pid_task(pid, PIDTYPE_PID, p) {
2852 set_tsk_trace_trace(p);
2853 } while_each_pid_task(pid, PIDTYPE_PID, p);
2857 static void clear_ftrace_pid_task(struct pid *pid)
2859 if (pid == ftrace_swapper_pid)
2860 clear_ftrace_swapper();
2862 clear_ftrace_pid(pid);
2865 static void set_ftrace_pid_task(struct pid *pid)
2867 if (pid == ftrace_swapper_pid)
2868 set_ftrace_swapper();
2870 set_ftrace_pid(pid);
2873 static int ftrace_pid_add(int p)
2876 struct ftrace_pid *fpid;
2879 mutex_lock(&ftrace_lock);
2882 pid = ftrace_swapper_pid;
2884 pid = find_get_pid(p);
2891 list_for_each_entry(fpid, &ftrace_pids, list)
2892 if (fpid->pid == pid)
2897 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2901 list_add(&fpid->list, &ftrace_pids);
2904 set_ftrace_pid_task(pid);
2906 ftrace_update_pid_func();
2907 ftrace_startup_enable(0);
2909 mutex_unlock(&ftrace_lock);
2913 if (pid != ftrace_swapper_pid)
2917 mutex_unlock(&ftrace_lock);
2921 static void ftrace_pid_reset(void)
2923 struct ftrace_pid *fpid, *safe;
2925 mutex_lock(&ftrace_lock);
2926 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2927 struct pid *pid = fpid->pid;
2929 clear_ftrace_pid_task(pid);
2931 list_del(&fpid->list);
2935 ftrace_update_pid_func();
2936 ftrace_startup_enable(0);
2938 mutex_unlock(&ftrace_lock);
2941 static void *fpid_start(struct seq_file *m, loff_t *pos)
2943 mutex_lock(&ftrace_lock);
2945 if (list_empty(&ftrace_pids) && (!*pos))
2948 return seq_list_start(&ftrace_pids, *pos);
2951 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2956 return seq_list_next(v, &ftrace_pids, pos);
2959 static void fpid_stop(struct seq_file *m, void *p)
2961 mutex_unlock(&ftrace_lock);
2964 static int fpid_show(struct seq_file *m, void *v)
2966 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2968 if (v == (void *)1) {
2969 seq_printf(m, "no pid\n");
2973 if (fpid->pid == ftrace_swapper_pid)
2974 seq_printf(m, "swapper tasks\n");
2976 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2981 static const struct seq_operations ftrace_pid_sops = {
2982 .start = fpid_start,
2989 ftrace_pid_open(struct inode *inode, struct file *file)
2993 if ((file->f_mode & FMODE_WRITE) &&
2994 (file->f_flags & O_TRUNC))
2997 if (file->f_mode & FMODE_READ)
2998 ret = seq_open(file, &ftrace_pid_sops);
3004 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3005 size_t cnt, loff_t *ppos)
3011 if (cnt >= sizeof(buf))
3014 if (copy_from_user(&buf, ubuf, cnt))
3020 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3021 * to clean the filter quietly.
3023 tmp = strstrip(buf);
3024 if (strlen(tmp) == 0)
3027 ret = strict_strtol(tmp, 10, &val);
3031 ret = ftrace_pid_add(val);
3033 return ret ? ret : cnt;
3037 ftrace_pid_release(struct inode *inode, struct file *file)
3039 if (file->f_mode & FMODE_READ)
3040 seq_release(inode, file);
3045 static const struct file_operations ftrace_pid_fops = {
3046 .open = ftrace_pid_open,
3047 .write = ftrace_pid_write,
3049 .llseek = seq_lseek,
3050 .release = ftrace_pid_release,
3053 static __init int ftrace_init_debugfs(void)
3055 struct dentry *d_tracer;
3057 d_tracer = tracing_init_dentry();
3061 ftrace_init_dyn_debugfs(d_tracer);
3063 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3064 NULL, &ftrace_pid_fops);
3066 ftrace_profile_debugfs(d_tracer);
3070 fs_initcall(ftrace_init_debugfs);
3073 * ftrace_kill - kill ftrace
3075 * This function should be used by panic code. It stops ftrace
3076 * but in a not so nice way. If you need to simply kill ftrace
3077 * from a non-atomic section, use ftrace_kill.
3079 void ftrace_kill(void)
3081 ftrace_disabled = 1;
3083 clear_ftrace_function();
3087 * register_ftrace_function - register a function for profiling
3088 * @ops - ops structure that holds the function for profiling.
3090 * Register a function to be called by all functions in the
3093 * Note: @ops->func and all the functions it calls must be labeled
3094 * with "notrace", otherwise it will go into a
3097 int register_ftrace_function(struct ftrace_ops *ops)
3101 if (unlikely(ftrace_disabled))
3104 mutex_lock(&ftrace_lock);
3106 ret = __register_ftrace_function(ops);
3109 mutex_unlock(&ftrace_lock);
3114 * unregister_ftrace_function - unregister a function for profiling.
3115 * @ops - ops structure that holds the function to unregister
3117 * Unregister a function that was added to be called by ftrace profiling.
3119 int unregister_ftrace_function(struct ftrace_ops *ops)
3123 mutex_lock(&ftrace_lock);
3124 ret = __unregister_ftrace_function(ops);
3126 mutex_unlock(&ftrace_lock);
3132 ftrace_enable_sysctl(struct ctl_table *table, int write,
3133 void __user *buffer, size_t *lenp,
3138 if (unlikely(ftrace_disabled))
3141 mutex_lock(&ftrace_lock);
3143 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3145 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3148 last_ftrace_enabled = !!ftrace_enabled;
3150 if (ftrace_enabled) {
3152 ftrace_startup_sysctl();
3154 /* we are starting ftrace again */
3155 if (ftrace_list != &ftrace_list_end) {
3156 if (ftrace_list->next == &ftrace_list_end)
3157 ftrace_trace_function = ftrace_list->func;
3159 ftrace_trace_function = ftrace_list_func;
3163 /* stopping ftrace calls (just send to ftrace_stub) */
3164 ftrace_trace_function = ftrace_stub;
3166 ftrace_shutdown_sysctl();
3170 mutex_unlock(&ftrace_lock);
3174 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3176 static int ftrace_graph_active;
3177 static struct notifier_block ftrace_suspend_notifier;
3179 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3184 /* The callbacks that hook a function */
3185 trace_func_graph_ret_t ftrace_graph_return =
3186 (trace_func_graph_ret_t)ftrace_stub;
3187 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3189 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3190 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3194 unsigned long flags;
3195 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3196 struct task_struct *g, *t;
3198 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3199 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3200 * sizeof(struct ftrace_ret_stack),
3202 if (!ret_stack_list[i]) {
3210 read_lock_irqsave(&tasklist_lock, flags);
3211 do_each_thread(g, t) {
3217 if (t->ret_stack == NULL) {
3218 atomic_set(&t->tracing_graph_pause, 0);
3219 atomic_set(&t->trace_overrun, 0);
3220 t->curr_ret_stack = -1;
3221 /* Make sure the tasks see the -1 first: */
3223 t->ret_stack = ret_stack_list[start++];
3225 } while_each_thread(g, t);
3228 read_unlock_irqrestore(&tasklist_lock, flags);
3230 for (i = start; i < end; i++)
3231 kfree(ret_stack_list[i]);
3236 ftrace_graph_probe_sched_switch(void *ignore,
3237 struct task_struct *prev, struct task_struct *next)
3239 unsigned long long timestamp;
3243 * Does the user want to count the time a function was asleep.
3244 * If so, do not update the time stamps.
3246 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3249 timestamp = trace_clock_local();
3251 prev->ftrace_timestamp = timestamp;
3253 /* only process tasks that we timestamped */
3254 if (!next->ftrace_timestamp)
3258 * Update all the counters in next to make up for the
3259 * time next was sleeping.
3261 timestamp -= next->ftrace_timestamp;
3263 for (index = next->curr_ret_stack; index >= 0; index--)
3264 next->ret_stack[index].calltime += timestamp;
3267 /* Allocate a return stack for each task */
3268 static int start_graph_tracing(void)
3270 struct ftrace_ret_stack **ret_stack_list;
3273 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3274 sizeof(struct ftrace_ret_stack *),
3277 if (!ret_stack_list)
3280 /* The cpu_boot init_task->ret_stack will never be freed */
3281 for_each_online_cpu(cpu) {
3282 if (!idle_task(cpu)->ret_stack)
3283 ftrace_graph_init_task(idle_task(cpu));
3287 ret = alloc_retstack_tasklist(ret_stack_list);
3288 } while (ret == -EAGAIN);
3291 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3293 pr_info("ftrace_graph: Couldn't activate tracepoint"
3294 " probe to kernel_sched_switch\n");
3297 kfree(ret_stack_list);
3302 * Hibernation protection.
3303 * The state of the current task is too much unstable during
3304 * suspend/restore to disk. We want to protect against that.
3307 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3311 case PM_HIBERNATION_PREPARE:
3312 pause_graph_tracing();
3315 case PM_POST_HIBERNATION:
3316 unpause_graph_tracing();
3322 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3323 trace_func_graph_ent_t entryfunc)
3327 mutex_lock(&ftrace_lock);
3329 /* we currently allow only one tracer registered at a time */
3330 if (ftrace_graph_active) {
3335 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3336 register_pm_notifier(&ftrace_suspend_notifier);
3338 ftrace_graph_active++;
3339 ret = start_graph_tracing();
3341 ftrace_graph_active--;
3345 ftrace_graph_return = retfunc;
3346 ftrace_graph_entry = entryfunc;
3348 ftrace_startup(FTRACE_START_FUNC_RET);
3351 mutex_unlock(&ftrace_lock);
3355 void unregister_ftrace_graph(void)
3357 mutex_lock(&ftrace_lock);
3359 if (unlikely(!ftrace_graph_active))
3362 ftrace_graph_active--;
3363 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3364 ftrace_graph_entry = ftrace_graph_entry_stub;
3365 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3366 unregister_pm_notifier(&ftrace_suspend_notifier);
3367 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3370 mutex_unlock(&ftrace_lock);
3373 /* Allocate a return stack for newly created task */
3374 void ftrace_graph_init_task(struct task_struct *t)
3376 /* Make sure we do not use the parent ret_stack */
3377 t->ret_stack = NULL;
3378 t->curr_ret_stack = -1;
3380 if (ftrace_graph_active) {
3381 struct ftrace_ret_stack *ret_stack;
3383 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3384 * sizeof(struct ftrace_ret_stack),
3388 atomic_set(&t->tracing_graph_pause, 0);
3389 atomic_set(&t->trace_overrun, 0);
3390 t->ftrace_timestamp = 0;
3391 /* make curr_ret_stack visable before we add the ret_stack */
3393 t->ret_stack = ret_stack;
3397 void ftrace_graph_exit_task(struct task_struct *t)
3399 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3401 t->ret_stack = NULL;
3402 /* NULL must become visible to IRQs before we free it: */
3408 void ftrace_graph_stop(void)