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 calltime = trace->rettime - trace->calltime;
671 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
674 index = trace->depth;
676 /* Append this call time to the parent time to subtract */
678 current->ret_stack[index - 1].subtime += calltime;
680 if (current->ret_stack[index].subtime < calltime)
681 calltime -= current->ret_stack[index].subtime;
686 rec = ftrace_find_profiled_func(stat, trace->func);
688 rec->time += calltime;
689 rec->time_squared += calltime * calltime;
693 local_irq_restore(flags);
696 static int register_ftrace_profiler(void)
698 return register_ftrace_graph(&profile_graph_return,
699 &profile_graph_entry);
702 static void unregister_ftrace_profiler(void)
704 unregister_ftrace_graph();
707 static struct ftrace_ops ftrace_profile_ops __read_mostly =
709 .func = function_profile_call,
712 static int register_ftrace_profiler(void)
714 return register_ftrace_function(&ftrace_profile_ops);
717 static void unregister_ftrace_profiler(void)
719 unregister_ftrace_function(&ftrace_profile_ops);
721 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
724 ftrace_profile_write(struct file *filp, const char __user *ubuf,
725 size_t cnt, loff_t *ppos)
728 char buf[64]; /* big enough to hold a number */
731 if (cnt >= sizeof(buf))
734 if (copy_from_user(&buf, ubuf, cnt))
739 ret = strict_strtoul(buf, 10, &val);
745 mutex_lock(&ftrace_profile_lock);
746 if (ftrace_profile_enabled ^ val) {
748 ret = ftrace_profile_init();
754 ret = register_ftrace_profiler();
759 ftrace_profile_enabled = 1;
761 ftrace_profile_enabled = 0;
763 * unregister_ftrace_profiler calls stop_machine
764 * so this acts like an synchronize_sched.
766 unregister_ftrace_profiler();
770 mutex_unlock(&ftrace_profile_lock);
778 ftrace_profile_read(struct file *filp, char __user *ubuf,
779 size_t cnt, loff_t *ppos)
781 char buf[64]; /* big enough to hold a number */
784 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
785 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
788 static const struct file_operations ftrace_profile_fops = {
789 .open = tracing_open_generic,
790 .read = ftrace_profile_read,
791 .write = ftrace_profile_write,
794 /* used to initialize the real stat files */
795 static struct tracer_stat function_stats __initdata = {
797 .stat_start = function_stat_start,
798 .stat_next = function_stat_next,
799 .stat_cmp = function_stat_cmp,
800 .stat_headers = function_stat_headers,
801 .stat_show = function_stat_show
804 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
806 struct ftrace_profile_stat *stat;
807 struct dentry *entry;
812 for_each_possible_cpu(cpu) {
813 stat = &per_cpu(ftrace_profile_stats, cpu);
815 /* allocate enough for function name + cpu number */
816 name = kmalloc(32, GFP_KERNEL);
819 * The files created are permanent, if something happens
820 * we still do not free memory.
823 "Could not allocate stat file for cpu %d\n",
827 stat->stat = function_stats;
828 snprintf(name, 32, "function%d", cpu);
829 stat->stat.name = name;
830 ret = register_stat_tracer(&stat->stat);
833 "Could not register function stat for cpu %d\n",
840 entry = debugfs_create_file("function_profile_enabled", 0644,
841 d_tracer, NULL, &ftrace_profile_fops);
843 pr_warning("Could not create debugfs "
844 "'function_profile_enabled' entry\n");
847 #else /* CONFIG_FUNCTION_PROFILER */
848 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
851 #endif /* CONFIG_FUNCTION_PROFILER */
853 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
855 #ifdef CONFIG_DYNAMIC_FTRACE
857 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
858 # error Dynamic ftrace depends on MCOUNT_RECORD
861 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
863 struct ftrace_func_probe {
864 struct hlist_node node;
865 struct ftrace_probe_ops *ops;
873 FTRACE_ENABLE_CALLS = (1 << 0),
874 FTRACE_DISABLE_CALLS = (1 << 1),
875 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
876 FTRACE_ENABLE_MCOUNT = (1 << 3),
877 FTRACE_DISABLE_MCOUNT = (1 << 4),
878 FTRACE_START_FUNC_RET = (1 << 5),
879 FTRACE_STOP_FUNC_RET = (1 << 6),
882 static int ftrace_filtered;
884 static struct dyn_ftrace *ftrace_new_addrs;
886 static DEFINE_MUTEX(ftrace_regex_lock);
889 struct ftrace_page *next;
891 struct dyn_ftrace records[];
894 #define ENTRIES_PER_PAGE \
895 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
897 /* estimate from running different kernels */
898 #define NR_TO_INIT 10000
900 static struct ftrace_page *ftrace_pages_start;
901 static struct ftrace_page *ftrace_pages;
903 static struct dyn_ftrace *ftrace_free_records;
906 * This is a double for. Do not use 'break' to break out of the loop,
907 * you must use a goto.
909 #define do_for_each_ftrace_rec(pg, rec) \
910 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
912 for (_____i = 0; _____i < pg->index; _____i++) { \
913 rec = &pg->records[_____i];
915 #define while_for_each_ftrace_rec() \
919 static void ftrace_free_rec(struct dyn_ftrace *rec)
921 rec->freelist = ftrace_free_records;
922 ftrace_free_records = rec;
923 rec->flags |= FTRACE_FL_FREE;
926 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
928 struct dyn_ftrace *rec;
930 /* First check for freed records */
931 if (ftrace_free_records) {
932 rec = ftrace_free_records;
934 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
935 FTRACE_WARN_ON_ONCE(1);
936 ftrace_free_records = NULL;
940 ftrace_free_records = rec->freelist;
941 memset(rec, 0, sizeof(*rec));
945 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
946 if (!ftrace_pages->next) {
947 /* allocate another page */
949 (void *)get_zeroed_page(GFP_KERNEL);
950 if (!ftrace_pages->next)
953 ftrace_pages = ftrace_pages->next;
956 return &ftrace_pages->records[ftrace_pages->index++];
959 static struct dyn_ftrace *
960 ftrace_record_ip(unsigned long ip)
962 struct dyn_ftrace *rec;
967 rec = ftrace_alloc_dyn_node(ip);
972 rec->newlist = ftrace_new_addrs;
973 ftrace_new_addrs = rec;
978 static void print_ip_ins(const char *fmt, unsigned char *p)
982 printk(KERN_CONT "%s", fmt);
984 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
985 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
988 static void ftrace_bug(int failed, unsigned long ip)
992 FTRACE_WARN_ON_ONCE(1);
993 pr_info("ftrace faulted on modifying ");
997 FTRACE_WARN_ON_ONCE(1);
998 pr_info("ftrace failed to modify ");
1000 print_ip_ins(" actual: ", (unsigned char *)ip);
1001 printk(KERN_CONT "\n");
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on writing ");
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace faulted on unknown error ");
1016 /* Return 1 if the address range is reserved for ftrace */
1017 int ftrace_text_reserved(void *start, void *end)
1019 struct dyn_ftrace *rec;
1020 struct ftrace_page *pg;
1022 do_for_each_ftrace_rec(pg, rec) {
1023 if (rec->ip <= (unsigned long)end &&
1024 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1026 } while_for_each_ftrace_rec();
1032 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1034 unsigned long ftrace_addr;
1035 unsigned long flag = 0UL;
1037 ftrace_addr = (unsigned long)FTRACE_ADDR;
1040 * If this record is not to be traced or we want to disable it,
1043 * If we want to enable it and filtering is off, then enable it.
1045 * If we want to enable it and filtering is on, enable it only if
1048 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1049 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1050 flag = FTRACE_FL_ENABLED;
1053 /* If the state of this record hasn't changed, then do nothing */
1054 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1058 rec->flags |= FTRACE_FL_ENABLED;
1059 return ftrace_make_call(rec, ftrace_addr);
1062 rec->flags &= ~FTRACE_FL_ENABLED;
1063 return ftrace_make_nop(NULL, rec, ftrace_addr);
1066 static void ftrace_replace_code(int enable)
1068 struct dyn_ftrace *rec;
1069 struct ftrace_page *pg;
1072 do_for_each_ftrace_rec(pg, rec) {
1074 * Skip over free records, records that have
1075 * failed and not converted.
1077 if (rec->flags & FTRACE_FL_FREE ||
1078 rec->flags & FTRACE_FL_FAILED ||
1079 !(rec->flags & FTRACE_FL_CONVERTED))
1082 failed = __ftrace_replace_code(rec, enable);
1084 rec->flags |= FTRACE_FL_FAILED;
1085 ftrace_bug(failed, rec->ip);
1086 /* Stop processing */
1089 } while_for_each_ftrace_rec();
1093 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1100 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1102 ftrace_bug(ret, ip);
1103 rec->flags |= FTRACE_FL_FAILED;
1110 * archs can override this function if they must do something
1111 * before the modifying code is performed.
1113 int __weak ftrace_arch_code_modify_prepare(void)
1119 * archs can override this function if they must do something
1120 * after the modifying code is performed.
1122 int __weak ftrace_arch_code_modify_post_process(void)
1127 static int __ftrace_modify_code(void *data)
1129 int *command = data;
1131 if (*command & FTRACE_ENABLE_CALLS)
1132 ftrace_replace_code(1);
1133 else if (*command & FTRACE_DISABLE_CALLS)
1134 ftrace_replace_code(0);
1136 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1137 ftrace_update_ftrace_func(ftrace_trace_function);
1139 if (*command & FTRACE_START_FUNC_RET)
1140 ftrace_enable_ftrace_graph_caller();
1141 else if (*command & FTRACE_STOP_FUNC_RET)
1142 ftrace_disable_ftrace_graph_caller();
1147 static void ftrace_run_update_code(int command)
1151 ret = ftrace_arch_code_modify_prepare();
1152 FTRACE_WARN_ON(ret);
1156 stop_machine(__ftrace_modify_code, &command, NULL);
1158 ret = ftrace_arch_code_modify_post_process();
1159 FTRACE_WARN_ON(ret);
1162 static ftrace_func_t saved_ftrace_func;
1163 static int ftrace_start_up;
1165 static void ftrace_startup_enable(int command)
1167 if (saved_ftrace_func != ftrace_trace_function) {
1168 saved_ftrace_func = ftrace_trace_function;
1169 command |= FTRACE_UPDATE_TRACE_FUNC;
1172 if (!command || !ftrace_enabled)
1175 ftrace_run_update_code(command);
1178 static void ftrace_startup(int command)
1180 if (unlikely(ftrace_disabled))
1184 command |= FTRACE_ENABLE_CALLS;
1186 ftrace_startup_enable(command);
1189 static void ftrace_shutdown(int command)
1191 if (unlikely(ftrace_disabled))
1196 * Just warn in case of unbalance, no need to kill ftrace, it's not
1197 * critical but the ftrace_call callers may be never nopped again after
1198 * further ftrace uses.
1200 WARN_ON_ONCE(ftrace_start_up < 0);
1202 if (!ftrace_start_up)
1203 command |= FTRACE_DISABLE_CALLS;
1205 if (saved_ftrace_func != ftrace_trace_function) {
1206 saved_ftrace_func = ftrace_trace_function;
1207 command |= FTRACE_UPDATE_TRACE_FUNC;
1210 if (!command || !ftrace_enabled)
1213 ftrace_run_update_code(command);
1216 static void ftrace_startup_sysctl(void)
1218 int command = FTRACE_ENABLE_MCOUNT;
1220 if (unlikely(ftrace_disabled))
1223 /* Force update next time */
1224 saved_ftrace_func = NULL;
1225 /* ftrace_start_up is true if we want ftrace running */
1226 if (ftrace_start_up)
1227 command |= FTRACE_ENABLE_CALLS;
1229 ftrace_run_update_code(command);
1232 static void ftrace_shutdown_sysctl(void)
1234 int command = FTRACE_DISABLE_MCOUNT;
1236 if (unlikely(ftrace_disabled))
1239 /* ftrace_start_up is true if ftrace is running */
1240 if (ftrace_start_up)
1241 command |= FTRACE_DISABLE_CALLS;
1243 ftrace_run_update_code(command);
1246 static cycle_t ftrace_update_time;
1247 static unsigned long ftrace_update_cnt;
1248 unsigned long ftrace_update_tot_cnt;
1250 static int ftrace_update_code(struct module *mod)
1252 struct dyn_ftrace *p;
1253 cycle_t start, stop;
1255 start = ftrace_now(raw_smp_processor_id());
1256 ftrace_update_cnt = 0;
1258 while (ftrace_new_addrs) {
1260 /* If something went wrong, bail without enabling anything */
1261 if (unlikely(ftrace_disabled))
1264 p = ftrace_new_addrs;
1265 ftrace_new_addrs = p->newlist;
1269 * Do the initial record convertion from mcount jump
1270 * to the NOP instructions.
1272 if (!ftrace_code_disable(mod, p)) {
1277 p->flags |= FTRACE_FL_CONVERTED;
1278 ftrace_update_cnt++;
1281 * If the tracing is enabled, go ahead and enable the record.
1283 * The reason not to enable the record immediatelly is the
1284 * inherent check of ftrace_make_nop/ftrace_make_call for
1285 * correct previous instructions. Making first the NOP
1286 * conversion puts the module to the correct state, thus
1287 * passing the ftrace_make_call check.
1289 if (ftrace_start_up) {
1290 int failed = __ftrace_replace_code(p, 1);
1292 ftrace_bug(failed, p->ip);
1298 stop = ftrace_now(raw_smp_processor_id());
1299 ftrace_update_time = stop - start;
1300 ftrace_update_tot_cnt += ftrace_update_cnt;
1305 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1307 struct ftrace_page *pg;
1311 /* allocate a few pages */
1312 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1313 if (!ftrace_pages_start)
1317 * Allocate a few more pages.
1319 * TODO: have some parser search vmlinux before
1320 * final linking to find all calls to ftrace.
1322 * a) know how many pages to allocate.
1324 * b) set up the table then.
1326 * The dynamic code is still necessary for
1330 pg = ftrace_pages = ftrace_pages_start;
1332 cnt = num_to_init / ENTRIES_PER_PAGE;
1333 pr_info("ftrace: allocating %ld entries in %d pages\n",
1334 num_to_init, cnt + 1);
1336 for (i = 0; i < cnt; i++) {
1337 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1339 /* If we fail, we'll try later anyway */
1350 FTRACE_ITER_FILTER = (1 << 0),
1351 FTRACE_ITER_NOTRACE = (1 << 1),
1352 FTRACE_ITER_FAILURES = (1 << 2),
1353 FTRACE_ITER_PRINTALL = (1 << 3),
1354 FTRACE_ITER_HASH = (1 << 4),
1357 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1359 struct ftrace_iterator {
1360 struct ftrace_page *pg;
1364 struct trace_parser parser;
1368 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1370 struct ftrace_iterator *iter = m->private;
1371 struct hlist_node *hnd = v;
1372 struct hlist_head *hhd;
1374 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1379 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1382 hhd = &ftrace_func_hash[iter->hidx];
1384 if (hlist_empty(hhd)) {
1403 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1405 struct ftrace_iterator *iter = m->private;
1409 if (!(iter->flags & FTRACE_ITER_HASH))
1412 iter->flags |= FTRACE_ITER_HASH;
1415 for (l = 0; l <= *pos; ) {
1416 p = t_hash_next(m, p, &l);
1423 static int t_hash_show(struct seq_file *m, void *v)
1425 struct ftrace_func_probe *rec;
1426 struct hlist_node *hnd = v;
1428 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1430 if (rec->ops->print)
1431 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1433 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1436 seq_printf(m, ":%p", rec->data);
1443 t_next(struct seq_file *m, void *v, loff_t *pos)
1445 struct ftrace_iterator *iter = m->private;
1446 struct dyn_ftrace *rec = NULL;
1448 if (iter->flags & FTRACE_ITER_HASH)
1449 return t_hash_next(m, v, pos);
1453 if (iter->flags & FTRACE_ITER_PRINTALL)
1457 if (iter->idx >= iter->pg->index) {
1458 if (iter->pg->next) {
1459 iter->pg = iter->pg->next;
1464 rec = &iter->pg->records[iter->idx++];
1465 if ((rec->flags & FTRACE_FL_FREE) ||
1467 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1468 (rec->flags & FTRACE_FL_FAILED)) ||
1470 ((iter->flags & FTRACE_ITER_FAILURES) &&
1471 !(rec->flags & FTRACE_FL_FAILED)) ||
1473 ((iter->flags & FTRACE_ITER_FILTER) &&
1474 !(rec->flags & FTRACE_FL_FILTER)) ||
1476 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1477 !(rec->flags & FTRACE_FL_NOTRACE))) {
1486 static void *t_start(struct seq_file *m, loff_t *pos)
1488 struct ftrace_iterator *iter = m->private;
1492 mutex_lock(&ftrace_lock);
1494 * For set_ftrace_filter reading, if we have the filter
1495 * off, we can short cut and just print out that all
1496 * functions are enabled.
1498 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1500 return t_hash_start(m, pos);
1501 iter->flags |= FTRACE_ITER_PRINTALL;
1505 if (iter->flags & FTRACE_ITER_HASH)
1506 return t_hash_start(m, pos);
1508 iter->pg = ftrace_pages_start;
1510 for (l = 0; l <= *pos; ) {
1511 p = t_next(m, p, &l);
1516 if (!p && iter->flags & FTRACE_ITER_FILTER)
1517 return t_hash_start(m, pos);
1522 static void t_stop(struct seq_file *m, void *p)
1524 mutex_unlock(&ftrace_lock);
1527 static int t_show(struct seq_file *m, void *v)
1529 struct ftrace_iterator *iter = m->private;
1530 struct dyn_ftrace *rec = v;
1532 if (iter->flags & FTRACE_ITER_HASH)
1533 return t_hash_show(m, v);
1535 if (iter->flags & FTRACE_ITER_PRINTALL) {
1536 seq_printf(m, "#### all functions enabled ####\n");
1543 seq_printf(m, "%ps\n", (void *)rec->ip);
1548 static const struct seq_operations show_ftrace_seq_ops = {
1556 ftrace_avail_open(struct inode *inode, struct file *file)
1558 struct ftrace_iterator *iter;
1561 if (unlikely(ftrace_disabled))
1564 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1568 iter->pg = ftrace_pages_start;
1570 ret = seq_open(file, &show_ftrace_seq_ops);
1572 struct seq_file *m = file->private_data;
1583 ftrace_failures_open(struct inode *inode, struct file *file)
1587 struct ftrace_iterator *iter;
1589 ret = ftrace_avail_open(inode, file);
1591 m = (struct seq_file *)file->private_data;
1592 iter = (struct ftrace_iterator *)m->private;
1593 iter->flags = FTRACE_ITER_FAILURES;
1600 static void ftrace_filter_reset(int enable)
1602 struct ftrace_page *pg;
1603 struct dyn_ftrace *rec;
1604 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1606 mutex_lock(&ftrace_lock);
1608 ftrace_filtered = 0;
1609 do_for_each_ftrace_rec(pg, rec) {
1610 if (rec->flags & FTRACE_FL_FAILED)
1612 rec->flags &= ~type;
1613 } while_for_each_ftrace_rec();
1614 mutex_unlock(&ftrace_lock);
1618 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1620 struct ftrace_iterator *iter;
1623 if (unlikely(ftrace_disabled))
1626 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1630 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1635 mutex_lock(&ftrace_regex_lock);
1636 if ((file->f_mode & FMODE_WRITE) &&
1637 (file->f_flags & O_TRUNC))
1638 ftrace_filter_reset(enable);
1640 if (file->f_mode & FMODE_READ) {
1641 iter->pg = ftrace_pages_start;
1642 iter->flags = enable ? FTRACE_ITER_FILTER :
1643 FTRACE_ITER_NOTRACE;
1645 ret = seq_open(file, &show_ftrace_seq_ops);
1647 struct seq_file *m = file->private_data;
1650 trace_parser_put(&iter->parser);
1654 file->private_data = iter;
1655 mutex_unlock(&ftrace_regex_lock);
1661 ftrace_filter_open(struct inode *inode, struct file *file)
1663 return ftrace_regex_open(inode, file, 1);
1667 ftrace_notrace_open(struct inode *inode, struct file *file)
1669 return ftrace_regex_open(inode, file, 0);
1673 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1677 if (file->f_mode & FMODE_READ)
1678 ret = seq_lseek(file, offset, origin);
1680 file->f_pos = ret = 1;
1685 static int ftrace_match(char *str, char *regex, int len, int type)
1692 if (strcmp(str, regex) == 0)
1695 case MATCH_FRONT_ONLY:
1696 if (strncmp(str, regex, len) == 0)
1699 case MATCH_MIDDLE_ONLY:
1700 if (strstr(str, regex))
1703 case MATCH_END_ONLY:
1705 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
1714 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1716 char str[KSYM_SYMBOL_LEN];
1718 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1719 return ftrace_match(str, regex, len, type);
1722 static int ftrace_match_records(char *buff, int len, int enable)
1724 unsigned int search_len;
1725 struct ftrace_page *pg;
1726 struct dyn_ftrace *rec;
1733 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1734 type = filter_parse_regex(buff, len, &search, ¬);
1736 search_len = strlen(search);
1738 mutex_lock(&ftrace_lock);
1739 do_for_each_ftrace_rec(pg, rec) {
1741 if (rec->flags & FTRACE_FL_FAILED)
1744 if (ftrace_match_record(rec, search, search_len, type)) {
1746 rec->flags &= ~flag;
1752 * Only enable filtering if we have a function that
1755 if (enable && (rec->flags & FTRACE_FL_FILTER))
1756 ftrace_filtered = 1;
1757 } while_for_each_ftrace_rec();
1758 mutex_unlock(&ftrace_lock);
1764 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1765 char *regex, int len, int type)
1767 char str[KSYM_SYMBOL_LEN];
1770 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1772 if (!modname || strcmp(modname, mod))
1775 /* blank search means to match all funcs in the mod */
1777 return ftrace_match(str, regex, len, type);
1782 static int ftrace_match_module_records(char *buff, char *mod, int enable)
1784 unsigned search_len = 0;
1785 struct ftrace_page *pg;
1786 struct dyn_ftrace *rec;
1787 int type = MATCH_FULL;
1788 char *search = buff;
1793 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1795 /* blank or '*' mean the same */
1796 if (strcmp(buff, "*") == 0)
1799 /* handle the case of 'dont filter this module' */
1800 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1806 type = filter_parse_regex(buff, strlen(buff), &search, ¬);
1807 search_len = strlen(search);
1810 mutex_lock(&ftrace_lock);
1811 do_for_each_ftrace_rec(pg, rec) {
1813 if (rec->flags & FTRACE_FL_FAILED)
1816 if (ftrace_match_module_record(rec, mod,
1817 search, search_len, type)) {
1819 rec->flags &= ~flag;
1824 if (enable && (rec->flags & FTRACE_FL_FILTER))
1825 ftrace_filtered = 1;
1827 } while_for_each_ftrace_rec();
1828 mutex_unlock(&ftrace_lock);
1834 * We register the module command as a template to show others how
1835 * to register the a command as well.
1839 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1844 * cmd == 'mod' because we only registered this func
1845 * for the 'mod' ftrace_func_command.
1846 * But if you register one func with multiple commands,
1847 * you can tell which command was used by the cmd
1851 /* we must have a module name */
1855 mod = strsep(¶m, ":");
1859 if (ftrace_match_module_records(func, mod, enable))
1864 static struct ftrace_func_command ftrace_mod_cmd = {
1866 .func = ftrace_mod_callback,
1869 static int __init ftrace_mod_cmd_init(void)
1871 return register_ftrace_command(&ftrace_mod_cmd);
1873 device_initcall(ftrace_mod_cmd_init);
1876 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1878 struct ftrace_func_probe *entry;
1879 struct hlist_head *hhd;
1880 struct hlist_node *n;
1884 key = hash_long(ip, FTRACE_HASH_BITS);
1886 hhd = &ftrace_func_hash[key];
1888 if (hlist_empty(hhd))
1892 * Disable preemption for these calls to prevent a RCU grace
1893 * period. This syncs the hash iteration and freeing of items
1894 * on the hash. rcu_read_lock is too dangerous here.
1896 resched = ftrace_preempt_disable();
1897 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1898 if (entry->ip == ip)
1899 entry->ops->func(ip, parent_ip, &entry->data);
1901 ftrace_preempt_enable(resched);
1904 static struct ftrace_ops trace_probe_ops __read_mostly =
1906 .func = function_trace_probe_call,
1909 static int ftrace_probe_registered;
1911 static void __enable_ftrace_function_probe(void)
1915 if (ftrace_probe_registered)
1918 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1919 struct hlist_head *hhd = &ftrace_func_hash[i];
1923 /* Nothing registered? */
1924 if (i == FTRACE_FUNC_HASHSIZE)
1927 __register_ftrace_function(&trace_probe_ops);
1929 ftrace_probe_registered = 1;
1932 static void __disable_ftrace_function_probe(void)
1936 if (!ftrace_probe_registered)
1939 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1940 struct hlist_head *hhd = &ftrace_func_hash[i];
1945 /* no more funcs left */
1946 __unregister_ftrace_function(&trace_probe_ops);
1948 ftrace_probe_registered = 0;
1952 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1954 struct ftrace_func_probe *entry =
1955 container_of(rhp, struct ftrace_func_probe, rcu);
1957 if (entry->ops->free)
1958 entry->ops->free(&entry->data);
1964 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1967 struct ftrace_func_probe *entry;
1968 struct ftrace_page *pg;
1969 struct dyn_ftrace *rec;
1975 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
1976 len = strlen(search);
1978 /* we do not support '!' for function probes */
1982 mutex_lock(&ftrace_lock);
1983 do_for_each_ftrace_rec(pg, rec) {
1985 if (rec->flags & FTRACE_FL_FAILED)
1988 if (!ftrace_match_record(rec, search, len, type))
1991 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1993 /* If we did not process any, then return error */
2004 * The caller might want to do something special
2005 * for each function we find. We call the callback
2006 * to give the caller an opportunity to do so.
2008 if (ops->callback) {
2009 if (ops->callback(rec->ip, &entry->data) < 0) {
2010 /* caller does not like this func */
2017 entry->ip = rec->ip;
2019 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2020 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2022 } while_for_each_ftrace_rec();
2023 __enable_ftrace_function_probe();
2026 mutex_unlock(&ftrace_lock);
2032 PROBE_TEST_FUNC = 1,
2037 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2038 void *data, int flags)
2040 struct ftrace_func_probe *entry;
2041 struct hlist_node *n, *tmp;
2042 char str[KSYM_SYMBOL_LEN];
2043 int type = MATCH_FULL;
2047 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2052 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2053 len = strlen(search);
2055 /* we do not support '!' for function probes */
2060 mutex_lock(&ftrace_lock);
2061 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2062 struct hlist_head *hhd = &ftrace_func_hash[i];
2064 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2066 /* break up if statements for readability */
2067 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2070 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2073 /* do this last, since it is the most expensive */
2075 kallsyms_lookup(entry->ip, NULL, NULL,
2077 if (!ftrace_match(str, glob, len, type))
2081 hlist_del(&entry->node);
2082 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2085 __disable_ftrace_function_probe();
2086 mutex_unlock(&ftrace_lock);
2090 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2093 __unregister_ftrace_function_probe(glob, ops, data,
2094 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2098 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2100 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2103 void unregister_ftrace_function_probe_all(char *glob)
2105 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2108 static LIST_HEAD(ftrace_commands);
2109 static DEFINE_MUTEX(ftrace_cmd_mutex);
2111 int register_ftrace_command(struct ftrace_func_command *cmd)
2113 struct ftrace_func_command *p;
2116 mutex_lock(&ftrace_cmd_mutex);
2117 list_for_each_entry(p, &ftrace_commands, list) {
2118 if (strcmp(cmd->name, p->name) == 0) {
2123 list_add(&cmd->list, &ftrace_commands);
2125 mutex_unlock(&ftrace_cmd_mutex);
2130 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2132 struct ftrace_func_command *p, *n;
2135 mutex_lock(&ftrace_cmd_mutex);
2136 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2137 if (strcmp(cmd->name, p->name) == 0) {
2139 list_del_init(&p->list);
2144 mutex_unlock(&ftrace_cmd_mutex);
2149 static int ftrace_process_regex(char *buff, int len, int enable)
2151 char *func, *command, *next = buff;
2152 struct ftrace_func_command *p;
2155 func = strsep(&next, ":");
2158 if (ftrace_match_records(func, len, enable))
2165 command = strsep(&next, ":");
2167 mutex_lock(&ftrace_cmd_mutex);
2168 list_for_each_entry(p, &ftrace_commands, list) {
2169 if (strcmp(p->name, command) == 0) {
2170 ret = p->func(func, command, next, enable);
2175 mutex_unlock(&ftrace_cmd_mutex);
2181 ftrace_regex_write(struct file *file, const char __user *ubuf,
2182 size_t cnt, loff_t *ppos, int enable)
2184 struct ftrace_iterator *iter;
2185 struct trace_parser *parser;
2191 mutex_lock(&ftrace_regex_lock);
2193 if (file->f_mode & FMODE_READ) {
2194 struct seq_file *m = file->private_data;
2197 iter = file->private_data;
2199 parser = &iter->parser;
2200 read = trace_get_user(parser, ubuf, cnt, ppos);
2202 if (read >= 0 && trace_parser_loaded(parser) &&
2203 !trace_parser_cont(parser)) {
2204 ret = ftrace_process_regex(parser->buffer,
2205 parser->idx, enable);
2206 trace_parser_clear(parser);
2213 mutex_unlock(&ftrace_regex_lock);
2219 ftrace_filter_write(struct file *file, const char __user *ubuf,
2220 size_t cnt, loff_t *ppos)
2222 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2226 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2227 size_t cnt, loff_t *ppos)
2229 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2233 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2235 if (unlikely(ftrace_disabled))
2238 mutex_lock(&ftrace_regex_lock);
2240 ftrace_filter_reset(enable);
2242 ftrace_match_records(buf, len, enable);
2243 mutex_unlock(&ftrace_regex_lock);
2247 * ftrace_set_filter - set a function to filter on in ftrace
2248 * @buf - the string that holds the function filter text.
2249 * @len - the length of the string.
2250 * @reset - non zero to reset all filters before applying this filter.
2252 * Filters denote which functions should be enabled when tracing is enabled.
2253 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2255 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2257 ftrace_set_regex(buf, len, reset, 1);
2261 * ftrace_set_notrace - set a function to not trace in ftrace
2262 * @buf - the string that holds the function notrace text.
2263 * @len - the length of the string.
2264 * @reset - non zero to reset all filters before applying this filter.
2266 * Notrace Filters denote which functions should not be enabled when tracing
2267 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2270 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2272 ftrace_set_regex(buf, len, reset, 0);
2276 * command line interface to allow users to set filters on boot up.
2278 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2279 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2280 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2282 static int __init set_ftrace_notrace(char *str)
2284 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2287 __setup("ftrace_notrace=", set_ftrace_notrace);
2289 static int __init set_ftrace_filter(char *str)
2291 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2294 __setup("ftrace_filter=", set_ftrace_filter);
2296 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2297 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2298 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2300 static int __init set_graph_function(char *str)
2302 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2305 __setup("ftrace_graph_filter=", set_graph_function);
2307 static void __init set_ftrace_early_graph(char *buf)
2313 func = strsep(&buf, ",");
2314 /* we allow only one expression at a time */
2315 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2318 printk(KERN_DEBUG "ftrace: function %s not "
2319 "traceable\n", func);
2322 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2324 static void __init set_ftrace_early_filter(char *buf, int enable)
2329 func = strsep(&buf, ",");
2330 ftrace_set_regex(func, strlen(func), 0, enable);
2334 static void __init set_ftrace_early_filters(void)
2336 if (ftrace_filter_buf[0])
2337 set_ftrace_early_filter(ftrace_filter_buf, 1);
2338 if (ftrace_notrace_buf[0])
2339 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2340 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2341 if (ftrace_graph_buf[0])
2342 set_ftrace_early_graph(ftrace_graph_buf);
2343 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2347 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2349 struct seq_file *m = (struct seq_file *)file->private_data;
2350 struct ftrace_iterator *iter;
2351 struct trace_parser *parser;
2353 mutex_lock(&ftrace_regex_lock);
2354 if (file->f_mode & FMODE_READ) {
2357 seq_release(inode, file);
2359 iter = file->private_data;
2361 parser = &iter->parser;
2362 if (trace_parser_loaded(parser)) {
2363 parser->buffer[parser->idx] = 0;
2364 ftrace_match_records(parser->buffer, parser->idx, enable);
2367 mutex_lock(&ftrace_lock);
2368 if (ftrace_start_up && ftrace_enabled)
2369 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2370 mutex_unlock(&ftrace_lock);
2372 trace_parser_put(parser);
2375 mutex_unlock(&ftrace_regex_lock);
2380 ftrace_filter_release(struct inode *inode, struct file *file)
2382 return ftrace_regex_release(inode, file, 1);
2386 ftrace_notrace_release(struct inode *inode, struct file *file)
2388 return ftrace_regex_release(inode, file, 0);
2391 static const struct file_operations ftrace_avail_fops = {
2392 .open = ftrace_avail_open,
2394 .llseek = seq_lseek,
2395 .release = seq_release_private,
2398 static const struct file_operations ftrace_failures_fops = {
2399 .open = ftrace_failures_open,
2401 .llseek = seq_lseek,
2402 .release = seq_release_private,
2405 static const struct file_operations ftrace_filter_fops = {
2406 .open = ftrace_filter_open,
2408 .write = ftrace_filter_write,
2409 .llseek = ftrace_regex_lseek,
2410 .release = ftrace_filter_release,
2413 static const struct file_operations ftrace_notrace_fops = {
2414 .open = ftrace_notrace_open,
2416 .write = ftrace_notrace_write,
2417 .llseek = ftrace_regex_lseek,
2418 .release = ftrace_notrace_release,
2421 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2423 static DEFINE_MUTEX(graph_lock);
2425 int ftrace_graph_count;
2426 int ftrace_graph_filter_enabled;
2427 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2430 __g_next(struct seq_file *m, loff_t *pos)
2432 if (*pos >= ftrace_graph_count)
2434 return &ftrace_graph_funcs[*pos];
2438 g_next(struct seq_file *m, void *v, loff_t *pos)
2441 return __g_next(m, pos);
2444 static void *g_start(struct seq_file *m, loff_t *pos)
2446 mutex_lock(&graph_lock);
2448 /* Nothing, tell g_show to print all functions are enabled */
2449 if (!ftrace_graph_filter_enabled && !*pos)
2452 return __g_next(m, pos);
2455 static void g_stop(struct seq_file *m, void *p)
2457 mutex_unlock(&graph_lock);
2460 static int g_show(struct seq_file *m, void *v)
2462 unsigned long *ptr = v;
2467 if (ptr == (unsigned long *)1) {
2468 seq_printf(m, "#### all functions enabled ####\n");
2472 seq_printf(m, "%ps\n", (void *)*ptr);
2477 static const struct seq_operations ftrace_graph_seq_ops = {
2485 ftrace_graph_open(struct inode *inode, struct file *file)
2489 if (unlikely(ftrace_disabled))
2492 mutex_lock(&graph_lock);
2493 if ((file->f_mode & FMODE_WRITE) &&
2494 (file->f_flags & O_TRUNC)) {
2495 ftrace_graph_filter_enabled = 0;
2496 ftrace_graph_count = 0;
2497 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2499 mutex_unlock(&graph_lock);
2501 if (file->f_mode & FMODE_READ)
2502 ret = seq_open(file, &ftrace_graph_seq_ops);
2508 ftrace_graph_release(struct inode *inode, struct file *file)
2510 if (file->f_mode & FMODE_READ)
2511 seq_release(inode, file);
2516 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2518 struct dyn_ftrace *rec;
2519 struct ftrace_page *pg;
2527 if (ftrace_disabled)
2531 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
2532 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2535 search_len = strlen(search);
2537 mutex_lock(&ftrace_lock);
2538 do_for_each_ftrace_rec(pg, rec) {
2540 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2543 if (ftrace_match_record(rec, search, search_len, type)) {
2544 /* if it is in the array */
2546 for (i = 0; i < *idx; i++) {
2547 if (array[i] == rec->ip) {
2556 array[(*idx)++] = rec->ip;
2557 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2562 array[i] = array[--(*idx)];
2568 } while_for_each_ftrace_rec();
2570 mutex_unlock(&ftrace_lock);
2575 ftrace_graph_filter_enabled = 1;
2580 ftrace_graph_write(struct file *file, const char __user *ubuf,
2581 size_t cnt, loff_t *ppos)
2583 struct trace_parser parser;
2589 mutex_lock(&graph_lock);
2591 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2596 read = trace_get_user(&parser, ubuf, cnt, ppos);
2598 if (read >= 0 && trace_parser_loaded((&parser))) {
2599 parser.buffer[parser.idx] = 0;
2601 /* we allow only one expression at a time */
2602 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2611 trace_parser_put(&parser);
2613 mutex_unlock(&graph_lock);
2618 static const struct file_operations ftrace_graph_fops = {
2619 .open = ftrace_graph_open,
2621 .write = ftrace_graph_write,
2622 .release = ftrace_graph_release,
2624 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2626 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2629 trace_create_file("available_filter_functions", 0444,
2630 d_tracer, NULL, &ftrace_avail_fops);
2632 trace_create_file("failures", 0444,
2633 d_tracer, NULL, &ftrace_failures_fops);
2635 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2636 NULL, &ftrace_filter_fops);
2638 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2639 NULL, &ftrace_notrace_fops);
2641 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2642 trace_create_file("set_graph_function", 0444, d_tracer,
2644 &ftrace_graph_fops);
2645 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2650 static int ftrace_process_locs(struct module *mod,
2651 unsigned long *start,
2656 unsigned long flags;
2658 mutex_lock(&ftrace_lock);
2661 addr = ftrace_call_adjust(*p++);
2663 * Some architecture linkers will pad between
2664 * the different mcount_loc sections of different
2665 * object files to satisfy alignments.
2666 * Skip any NULL pointers.
2670 ftrace_record_ip(addr);
2673 /* disable interrupts to prevent kstop machine */
2674 local_irq_save(flags);
2675 ftrace_update_code(mod);
2676 local_irq_restore(flags);
2677 mutex_unlock(&ftrace_lock);
2682 #ifdef CONFIG_MODULES
2683 void ftrace_release_mod(struct module *mod)
2685 struct dyn_ftrace *rec;
2686 struct ftrace_page *pg;
2688 if (ftrace_disabled)
2691 mutex_lock(&ftrace_lock);
2692 do_for_each_ftrace_rec(pg, rec) {
2693 if (within_module_core(rec->ip, mod)) {
2695 * rec->ip is changed in ftrace_free_rec()
2696 * It should not between s and e if record was freed.
2698 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2699 ftrace_free_rec(rec);
2701 } while_for_each_ftrace_rec();
2702 mutex_unlock(&ftrace_lock);
2705 static void ftrace_init_module(struct module *mod,
2706 unsigned long *start, unsigned long *end)
2708 if (ftrace_disabled || start == end)
2710 ftrace_process_locs(mod, start, end);
2713 static int ftrace_module_notify(struct notifier_block *self,
2714 unsigned long val, void *data)
2716 struct module *mod = data;
2719 case MODULE_STATE_COMING:
2720 ftrace_init_module(mod, mod->ftrace_callsites,
2721 mod->ftrace_callsites +
2722 mod->num_ftrace_callsites);
2724 case MODULE_STATE_GOING:
2725 ftrace_release_mod(mod);
2732 static int ftrace_module_notify(struct notifier_block *self,
2733 unsigned long val, void *data)
2737 #endif /* CONFIG_MODULES */
2739 struct notifier_block ftrace_module_nb = {
2740 .notifier_call = ftrace_module_notify,
2744 extern unsigned long __start_mcount_loc[];
2745 extern unsigned long __stop_mcount_loc[];
2747 void __init ftrace_init(void)
2749 unsigned long count, addr, flags;
2752 /* Keep the ftrace pointer to the stub */
2753 addr = (unsigned long)ftrace_stub;
2755 local_irq_save(flags);
2756 ftrace_dyn_arch_init(&addr);
2757 local_irq_restore(flags);
2759 /* ftrace_dyn_arch_init places the return code in addr */
2763 count = __stop_mcount_loc - __start_mcount_loc;
2765 ret = ftrace_dyn_table_alloc(count);
2769 last_ftrace_enabled = ftrace_enabled = 1;
2771 ret = ftrace_process_locs(NULL,
2775 ret = register_module_notifier(&ftrace_module_nb);
2777 pr_warning("Failed to register trace ftrace module notifier\n");
2779 set_ftrace_early_filters();
2783 ftrace_disabled = 1;
2788 static int __init ftrace_nodyn_init(void)
2793 device_initcall(ftrace_nodyn_init);
2795 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2796 static inline void ftrace_startup_enable(int command) { }
2797 /* Keep as macros so we do not need to define the commands */
2798 # define ftrace_startup(command) do { } while (0)
2799 # define ftrace_shutdown(command) do { } while (0)
2800 # define ftrace_startup_sysctl() do { } while (0)
2801 # define ftrace_shutdown_sysctl() do { } while (0)
2802 #endif /* CONFIG_DYNAMIC_FTRACE */
2804 static void clear_ftrace_swapper(void)
2806 struct task_struct *p;
2810 for_each_online_cpu(cpu) {
2812 clear_tsk_trace_trace(p);
2817 static void set_ftrace_swapper(void)
2819 struct task_struct *p;
2823 for_each_online_cpu(cpu) {
2825 set_tsk_trace_trace(p);
2830 static void clear_ftrace_pid(struct pid *pid)
2832 struct task_struct *p;
2835 do_each_pid_task(pid, PIDTYPE_PID, p) {
2836 clear_tsk_trace_trace(p);
2837 } while_each_pid_task(pid, PIDTYPE_PID, p);
2843 static void set_ftrace_pid(struct pid *pid)
2845 struct task_struct *p;
2848 do_each_pid_task(pid, PIDTYPE_PID, p) {
2849 set_tsk_trace_trace(p);
2850 } while_each_pid_task(pid, PIDTYPE_PID, p);
2854 static void clear_ftrace_pid_task(struct pid *pid)
2856 if (pid == ftrace_swapper_pid)
2857 clear_ftrace_swapper();
2859 clear_ftrace_pid(pid);
2862 static void set_ftrace_pid_task(struct pid *pid)
2864 if (pid == ftrace_swapper_pid)
2865 set_ftrace_swapper();
2867 set_ftrace_pid(pid);
2870 static int ftrace_pid_add(int p)
2873 struct ftrace_pid *fpid;
2876 mutex_lock(&ftrace_lock);
2879 pid = ftrace_swapper_pid;
2881 pid = find_get_pid(p);
2888 list_for_each_entry(fpid, &ftrace_pids, list)
2889 if (fpid->pid == pid)
2894 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2898 list_add(&fpid->list, &ftrace_pids);
2901 set_ftrace_pid_task(pid);
2903 ftrace_update_pid_func();
2904 ftrace_startup_enable(0);
2906 mutex_unlock(&ftrace_lock);
2910 if (pid != ftrace_swapper_pid)
2914 mutex_unlock(&ftrace_lock);
2918 static void ftrace_pid_reset(void)
2920 struct ftrace_pid *fpid, *safe;
2922 mutex_lock(&ftrace_lock);
2923 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2924 struct pid *pid = fpid->pid;
2926 clear_ftrace_pid_task(pid);
2928 list_del(&fpid->list);
2932 ftrace_update_pid_func();
2933 ftrace_startup_enable(0);
2935 mutex_unlock(&ftrace_lock);
2938 static void *fpid_start(struct seq_file *m, loff_t *pos)
2940 mutex_lock(&ftrace_lock);
2942 if (list_empty(&ftrace_pids) && (!*pos))
2945 return seq_list_start(&ftrace_pids, *pos);
2948 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2953 return seq_list_next(v, &ftrace_pids, pos);
2956 static void fpid_stop(struct seq_file *m, void *p)
2958 mutex_unlock(&ftrace_lock);
2961 static int fpid_show(struct seq_file *m, void *v)
2963 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2965 if (v == (void *)1) {
2966 seq_printf(m, "no pid\n");
2970 if (fpid->pid == ftrace_swapper_pid)
2971 seq_printf(m, "swapper tasks\n");
2973 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2978 static const struct seq_operations ftrace_pid_sops = {
2979 .start = fpid_start,
2986 ftrace_pid_open(struct inode *inode, struct file *file)
2990 if ((file->f_mode & FMODE_WRITE) &&
2991 (file->f_flags & O_TRUNC))
2994 if (file->f_mode & FMODE_READ)
2995 ret = seq_open(file, &ftrace_pid_sops);
3001 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3002 size_t cnt, loff_t *ppos)
3008 if (cnt >= sizeof(buf))
3011 if (copy_from_user(&buf, ubuf, cnt))
3017 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3018 * to clean the filter quietly.
3020 tmp = strstrip(buf);
3021 if (strlen(tmp) == 0)
3024 ret = strict_strtol(tmp, 10, &val);
3028 ret = ftrace_pid_add(val);
3030 return ret ? ret : cnt;
3034 ftrace_pid_release(struct inode *inode, struct file *file)
3036 if (file->f_mode & FMODE_READ)
3037 seq_release(inode, file);
3042 static const struct file_operations ftrace_pid_fops = {
3043 .open = ftrace_pid_open,
3044 .write = ftrace_pid_write,
3046 .llseek = seq_lseek,
3047 .release = ftrace_pid_release,
3050 static __init int ftrace_init_debugfs(void)
3052 struct dentry *d_tracer;
3054 d_tracer = tracing_init_dentry();
3058 ftrace_init_dyn_debugfs(d_tracer);
3060 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3061 NULL, &ftrace_pid_fops);
3063 ftrace_profile_debugfs(d_tracer);
3067 fs_initcall(ftrace_init_debugfs);
3070 * ftrace_kill - kill ftrace
3072 * This function should be used by panic code. It stops ftrace
3073 * but in a not so nice way. If you need to simply kill ftrace
3074 * from a non-atomic section, use ftrace_kill.
3076 void ftrace_kill(void)
3078 ftrace_disabled = 1;
3080 clear_ftrace_function();
3084 * register_ftrace_function - register a function for profiling
3085 * @ops - ops structure that holds the function for profiling.
3087 * Register a function to be called by all functions in the
3090 * Note: @ops->func and all the functions it calls must be labeled
3091 * with "notrace", otherwise it will go into a
3094 int register_ftrace_function(struct ftrace_ops *ops)
3098 if (unlikely(ftrace_disabled))
3101 mutex_lock(&ftrace_lock);
3103 ret = __register_ftrace_function(ops);
3106 mutex_unlock(&ftrace_lock);
3111 * unregister_ftrace_function - unregister a function for profiling.
3112 * @ops - ops structure that holds the function to unregister
3114 * Unregister a function that was added to be called by ftrace profiling.
3116 int unregister_ftrace_function(struct ftrace_ops *ops)
3120 mutex_lock(&ftrace_lock);
3121 ret = __unregister_ftrace_function(ops);
3123 mutex_unlock(&ftrace_lock);
3129 ftrace_enable_sysctl(struct ctl_table *table, int write,
3130 void __user *buffer, size_t *lenp,
3135 if (unlikely(ftrace_disabled))
3138 mutex_lock(&ftrace_lock);
3140 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3142 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3145 last_ftrace_enabled = !!ftrace_enabled;
3147 if (ftrace_enabled) {
3149 ftrace_startup_sysctl();
3151 /* we are starting ftrace again */
3152 if (ftrace_list != &ftrace_list_end) {
3153 if (ftrace_list->next == &ftrace_list_end)
3154 ftrace_trace_function = ftrace_list->func;
3156 ftrace_trace_function = ftrace_list_func;
3160 /* stopping ftrace calls (just send to ftrace_stub) */
3161 ftrace_trace_function = ftrace_stub;
3163 ftrace_shutdown_sysctl();
3167 mutex_unlock(&ftrace_lock);
3171 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3173 static int ftrace_graph_active;
3174 static struct notifier_block ftrace_suspend_notifier;
3176 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3181 /* The callbacks that hook a function */
3182 trace_func_graph_ret_t ftrace_graph_return =
3183 (trace_func_graph_ret_t)ftrace_stub;
3184 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3186 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3187 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3191 unsigned long flags;
3192 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3193 struct task_struct *g, *t;
3195 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3196 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3197 * sizeof(struct ftrace_ret_stack),
3199 if (!ret_stack_list[i]) {
3207 read_lock_irqsave(&tasklist_lock, flags);
3208 do_each_thread(g, t) {
3214 if (t->ret_stack == NULL) {
3215 atomic_set(&t->tracing_graph_pause, 0);
3216 atomic_set(&t->trace_overrun, 0);
3217 t->curr_ret_stack = -1;
3218 /* Make sure the tasks see the -1 first: */
3220 t->ret_stack = ret_stack_list[start++];
3222 } while_each_thread(g, t);
3225 read_unlock_irqrestore(&tasklist_lock, flags);
3227 for (i = start; i < end; i++)
3228 kfree(ret_stack_list[i]);
3233 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3234 struct task_struct *next)
3236 unsigned long long timestamp;
3240 * Does the user want to count the time a function was asleep.
3241 * If so, do not update the time stamps.
3243 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3246 timestamp = trace_clock_local();
3248 prev->ftrace_timestamp = timestamp;
3250 /* only process tasks that we timestamped */
3251 if (!next->ftrace_timestamp)
3255 * Update all the counters in next to make up for the
3256 * time next was sleeping.
3258 timestamp -= next->ftrace_timestamp;
3260 for (index = next->curr_ret_stack; index >= 0; index--)
3261 next->ret_stack[index].calltime += timestamp;
3264 /* Allocate a return stack for each task */
3265 static int start_graph_tracing(void)
3267 struct ftrace_ret_stack **ret_stack_list;
3270 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3271 sizeof(struct ftrace_ret_stack *),
3274 if (!ret_stack_list)
3277 /* The cpu_boot init_task->ret_stack will never be freed */
3278 for_each_online_cpu(cpu) {
3279 if (!idle_task(cpu)->ret_stack)
3280 ftrace_graph_init_task(idle_task(cpu));
3284 ret = alloc_retstack_tasklist(ret_stack_list);
3285 } while (ret == -EAGAIN);
3288 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3290 pr_info("ftrace_graph: Couldn't activate tracepoint"
3291 " probe to kernel_sched_switch\n");
3294 kfree(ret_stack_list);
3299 * Hibernation protection.
3300 * The state of the current task is too much unstable during
3301 * suspend/restore to disk. We want to protect against that.
3304 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3308 case PM_HIBERNATION_PREPARE:
3309 pause_graph_tracing();
3312 case PM_POST_HIBERNATION:
3313 unpause_graph_tracing();
3319 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3320 trace_func_graph_ent_t entryfunc)
3324 mutex_lock(&ftrace_lock);
3326 /* we currently allow only one tracer registered at a time */
3327 if (ftrace_graph_active) {
3332 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3333 register_pm_notifier(&ftrace_suspend_notifier);
3335 ftrace_graph_active++;
3336 ret = start_graph_tracing();
3338 ftrace_graph_active--;
3342 ftrace_graph_return = retfunc;
3343 ftrace_graph_entry = entryfunc;
3345 ftrace_startup(FTRACE_START_FUNC_RET);
3348 mutex_unlock(&ftrace_lock);
3352 void unregister_ftrace_graph(void)
3354 mutex_lock(&ftrace_lock);
3356 if (unlikely(!ftrace_graph_active))
3359 ftrace_graph_active--;
3360 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3361 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3362 ftrace_graph_entry = ftrace_graph_entry_stub;
3363 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3364 unregister_pm_notifier(&ftrace_suspend_notifier);
3367 mutex_unlock(&ftrace_lock);
3370 /* Allocate a return stack for newly created task */
3371 void ftrace_graph_init_task(struct task_struct *t)
3373 /* Make sure we do not use the parent ret_stack */
3374 t->ret_stack = NULL;
3375 t->curr_ret_stack = -1;
3377 if (ftrace_graph_active) {
3378 struct ftrace_ret_stack *ret_stack;
3380 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3381 * sizeof(struct ftrace_ret_stack),
3385 atomic_set(&t->tracing_graph_pause, 0);
3386 atomic_set(&t->trace_overrun, 0);
3387 t->ftrace_timestamp = 0;
3388 /* make curr_ret_stack visable before we add the ret_stack */
3390 t->ret_stack = ret_stack;
3394 void ftrace_graph_exit_task(struct task_struct *t)
3396 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3398 t->ret_stack = NULL;
3399 /* NULL must become visible to IRQs before we free it: */
3405 void ftrace_graph_stop(void)