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/ctype.h>
28 #include <linux/list.h>
29 #include <linux/hash.h>
30 #include <linux/rcupdate.h>
32 #include <trace/events/sched.h>
34 #include <asm/ftrace.h>
35 #include <asm/setup.h>
37 #include "trace_output.h"
38 #include "trace_stat.h"
40 #define FTRACE_WARN_ON(cond) \
46 #define FTRACE_WARN_ON_ONCE(cond) \
48 if (WARN_ON_ONCE(cond)) \
52 /* hash bits for specific function selection */
53 #define FTRACE_HASH_BITS 7
54 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56 /* ftrace_enabled is a method to turn ftrace on or off */
57 int ftrace_enabled __read_mostly;
58 static int last_ftrace_enabled;
60 /* Quick disabling of function tracer. */
61 int function_trace_stop;
63 /* List for set_ftrace_pid's pids. */
64 LIST_HEAD(ftrace_pids);
66 struct list_head list;
71 * ftrace_disabled is set when an anomaly is discovered.
72 * ftrace_disabled is much stronger than ftrace_enabled.
74 static int ftrace_disabled __read_mostly;
76 static DEFINE_MUTEX(ftrace_lock);
78 static struct ftrace_ops ftrace_list_end __read_mostly =
83 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
84 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
85 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
86 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
88 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
89 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
93 * Traverse the ftrace_list, invoking all entries. The reason that we
94 * can use rcu_dereference_raw() is that elements removed from this list
95 * are simply leaked, so there is no need to interact with a grace-period
96 * mechanism. The rcu_dereference_raw() calls are needed to handle
97 * concurrent insertions into the ftrace_list.
99 * Silly Alpha and silly pointer-speculation compiler optimizations!
101 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
103 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
105 while (op != &ftrace_list_end) {
106 op->func(ip, parent_ip);
107 op = rcu_dereference_raw(op->next); /*see above*/
111 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
113 if (!test_tsk_trace_trace(current))
116 ftrace_pid_function(ip, parent_ip);
119 static void set_ftrace_pid_function(ftrace_func_t func)
121 /* do not set ftrace_pid_function to itself! */
122 if (func != ftrace_pid_func)
123 ftrace_pid_function = func;
127 * clear_ftrace_function - reset the ftrace function
129 * This NULLs the ftrace function and in essence stops
130 * tracing. There may be lag
132 void clear_ftrace_function(void)
134 ftrace_trace_function = ftrace_stub;
135 __ftrace_trace_function = ftrace_stub;
136 ftrace_pid_function = ftrace_stub;
139 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
141 * For those archs that do not test ftrace_trace_stop in their
142 * mcount call site, we need to do it from C.
144 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
146 if (function_trace_stop)
149 __ftrace_trace_function(ip, parent_ip);
153 static int __register_ftrace_function(struct ftrace_ops *ops)
155 ops->next = ftrace_list;
157 * We are entering ops into the ftrace_list but another
158 * CPU might be walking that list. We need to make sure
159 * the ops->next pointer is valid before another CPU sees
160 * the ops pointer included into the ftrace_list.
162 rcu_assign_pointer(ftrace_list, ops);
164 if (ftrace_enabled) {
167 if (ops->next == &ftrace_list_end)
170 func = ftrace_list_func;
172 if (!list_empty(&ftrace_pids)) {
173 set_ftrace_pid_function(func);
174 func = ftrace_pid_func;
178 * For one func, simply call it directly.
179 * For more than one func, call the chain.
181 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
182 ftrace_trace_function = func;
184 __ftrace_trace_function = func;
185 ftrace_trace_function = ftrace_test_stop_func;
192 static int __unregister_ftrace_function(struct ftrace_ops *ops)
194 struct ftrace_ops **p;
197 * If we are removing the last function, then simply point
198 * to the ftrace_stub.
200 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
201 ftrace_trace_function = ftrace_stub;
202 ftrace_list = &ftrace_list_end;
206 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
215 if (ftrace_enabled) {
216 /* If we only have one func left, then call that directly */
217 if (ftrace_list->next == &ftrace_list_end) {
218 ftrace_func_t func = ftrace_list->func;
220 if (!list_empty(&ftrace_pids)) {
221 set_ftrace_pid_function(func);
222 func = ftrace_pid_func;
224 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
225 ftrace_trace_function = func;
227 __ftrace_trace_function = func;
235 static void ftrace_update_pid_func(void)
239 if (ftrace_trace_function == ftrace_stub)
242 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
243 func = ftrace_trace_function;
245 func = __ftrace_trace_function;
248 if (!list_empty(&ftrace_pids)) {
249 set_ftrace_pid_function(func);
250 func = ftrace_pid_func;
252 if (func == ftrace_pid_func)
253 func = ftrace_pid_function;
256 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
257 ftrace_trace_function = func;
259 __ftrace_trace_function = func;
263 #ifdef CONFIG_FUNCTION_PROFILER
264 struct ftrace_profile {
265 struct hlist_node node;
267 unsigned long counter;
268 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
269 unsigned long long time;
273 struct ftrace_profile_page {
274 struct ftrace_profile_page *next;
276 struct ftrace_profile records[];
279 struct ftrace_profile_stat {
281 struct hlist_head *hash;
282 struct ftrace_profile_page *pages;
283 struct ftrace_profile_page *start;
284 struct tracer_stat stat;
287 #define PROFILE_RECORDS_SIZE \
288 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
290 #define PROFILES_PER_PAGE \
291 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
293 static int ftrace_profile_bits __read_mostly;
294 static int ftrace_profile_enabled __read_mostly;
296 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
297 static DEFINE_MUTEX(ftrace_profile_lock);
299 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
301 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
304 function_stat_next(void *v, int idx)
306 struct ftrace_profile *rec = v;
307 struct ftrace_profile_page *pg;
309 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
315 if ((void *)rec >= (void *)&pg->records[pg->index]) {
319 rec = &pg->records[0];
327 static void *function_stat_start(struct tracer_stat *trace)
329 struct ftrace_profile_stat *stat =
330 container_of(trace, struct ftrace_profile_stat, stat);
332 if (!stat || !stat->start)
335 return function_stat_next(&stat->start->records[0], 0);
338 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
339 /* function graph compares on total time */
340 static int function_stat_cmp(void *p1, void *p2)
342 struct ftrace_profile *a = p1;
343 struct ftrace_profile *b = p2;
345 if (a->time < b->time)
347 if (a->time > b->time)
353 /* not function graph compares against hits */
354 static int function_stat_cmp(void *p1, void *p2)
356 struct ftrace_profile *a = p1;
357 struct ftrace_profile *b = p2;
359 if (a->counter < b->counter)
361 if (a->counter > b->counter)
368 static int function_stat_headers(struct seq_file *m)
370 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
371 seq_printf(m, " Function "
376 seq_printf(m, " Function Hit\n"
382 static int function_stat_show(struct seq_file *m, void *v)
384 struct ftrace_profile *rec = v;
385 char str[KSYM_SYMBOL_LEN];
386 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
387 static DEFINE_MUTEX(mutex);
388 static struct trace_seq s;
389 unsigned long long avg;
392 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
393 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
395 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
398 do_div(avg, rec->counter);
402 trace_print_graph_duration(rec->time, &s);
403 trace_seq_puts(&s, " ");
404 trace_print_graph_duration(avg, &s);
405 trace_print_seq(m, &s);
406 mutex_unlock(&mutex);
413 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
415 struct ftrace_profile_page *pg;
417 pg = stat->pages = stat->start;
420 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
425 memset(stat->hash, 0,
426 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
429 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
431 struct ftrace_profile_page *pg;
436 /* If we already allocated, do nothing */
440 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
444 #ifdef CONFIG_DYNAMIC_FTRACE
445 functions = ftrace_update_tot_cnt;
448 * We do not know the number of functions that exist because
449 * dynamic tracing is what counts them. With past experience
450 * we have around 20K functions. That should be more than enough.
451 * It is highly unlikely we will execute every function in
457 pg = stat->start = stat->pages;
459 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
461 for (i = 0; i < pages; i++) {
462 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
473 unsigned long tmp = (unsigned long)pg;
479 free_page((unsigned long)stat->pages);
486 static int ftrace_profile_init_cpu(int cpu)
488 struct ftrace_profile_stat *stat;
491 stat = &per_cpu(ftrace_profile_stats, cpu);
494 /* If the profile is already created, simply reset it */
495 ftrace_profile_reset(stat);
500 * We are profiling all functions, but usually only a few thousand
501 * functions are hit. We'll make a hash of 1024 items.
503 size = FTRACE_PROFILE_HASH_SIZE;
505 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
510 if (!ftrace_profile_bits) {
513 for (; size; size >>= 1)
514 ftrace_profile_bits++;
517 /* Preallocate the function profiling pages */
518 if (ftrace_profile_pages_init(stat) < 0) {
527 static int ftrace_profile_init(void)
532 for_each_online_cpu(cpu) {
533 ret = ftrace_profile_init_cpu(cpu);
541 /* interrupts must be disabled */
542 static struct ftrace_profile *
543 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
545 struct ftrace_profile *rec;
546 struct hlist_head *hhd;
547 struct hlist_node *n;
550 key = hash_long(ip, ftrace_profile_bits);
551 hhd = &stat->hash[key];
553 if (hlist_empty(hhd))
556 hlist_for_each_entry_rcu(rec, n, hhd, node) {
564 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
565 struct ftrace_profile *rec)
569 key = hash_long(rec->ip, ftrace_profile_bits);
570 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
574 * The memory is already allocated, this simply finds a new record to use.
576 static struct ftrace_profile *
577 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
579 struct ftrace_profile *rec = NULL;
581 /* prevent recursion (from NMIs) */
582 if (atomic_inc_return(&stat->disabled) != 1)
586 * Try to find the function again since an NMI
587 * could have added it
589 rec = ftrace_find_profiled_func(stat, ip);
593 if (stat->pages->index == PROFILES_PER_PAGE) {
594 if (!stat->pages->next)
596 stat->pages = stat->pages->next;
599 rec = &stat->pages->records[stat->pages->index++];
601 ftrace_add_profile(stat, rec);
604 atomic_dec(&stat->disabled);
610 function_profile_call(unsigned long ip, unsigned long parent_ip)
612 struct ftrace_profile_stat *stat;
613 struct ftrace_profile *rec;
616 if (!ftrace_profile_enabled)
619 local_irq_save(flags);
621 stat = &__get_cpu_var(ftrace_profile_stats);
622 if (!stat->hash || !ftrace_profile_enabled)
625 rec = ftrace_find_profiled_func(stat, ip);
627 rec = ftrace_profile_alloc(stat, ip);
634 local_irq_restore(flags);
637 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
638 static int profile_graph_entry(struct ftrace_graph_ent *trace)
640 function_profile_call(trace->func, 0);
644 static void profile_graph_return(struct ftrace_graph_ret *trace)
646 struct ftrace_profile_stat *stat;
647 unsigned long long calltime;
648 struct ftrace_profile *rec;
651 local_irq_save(flags);
652 stat = &__get_cpu_var(ftrace_profile_stats);
653 if (!stat->hash || !ftrace_profile_enabled)
656 calltime = trace->rettime - trace->calltime;
658 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
661 index = trace->depth;
663 /* Append this call time to the parent time to subtract */
665 current->ret_stack[index - 1].subtime += calltime;
667 if (current->ret_stack[index].subtime < calltime)
668 calltime -= current->ret_stack[index].subtime;
673 rec = ftrace_find_profiled_func(stat, trace->func);
675 rec->time += calltime;
678 local_irq_restore(flags);
681 static int register_ftrace_profiler(void)
683 return register_ftrace_graph(&profile_graph_return,
684 &profile_graph_entry);
687 static void unregister_ftrace_profiler(void)
689 unregister_ftrace_graph();
692 static struct ftrace_ops ftrace_profile_ops __read_mostly =
694 .func = function_profile_call,
697 static int register_ftrace_profiler(void)
699 return register_ftrace_function(&ftrace_profile_ops);
702 static void unregister_ftrace_profiler(void)
704 unregister_ftrace_function(&ftrace_profile_ops);
706 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
709 ftrace_profile_write(struct file *filp, const char __user *ubuf,
710 size_t cnt, loff_t *ppos)
713 char buf[64]; /* big enough to hold a number */
716 if (cnt >= sizeof(buf))
719 if (copy_from_user(&buf, ubuf, cnt))
724 ret = strict_strtoul(buf, 10, &val);
730 mutex_lock(&ftrace_profile_lock);
731 if (ftrace_profile_enabled ^ val) {
733 ret = ftrace_profile_init();
739 ret = register_ftrace_profiler();
744 ftrace_profile_enabled = 1;
746 ftrace_profile_enabled = 0;
748 * unregister_ftrace_profiler calls stop_machine
749 * so this acts like an synchronize_sched.
751 unregister_ftrace_profiler();
755 mutex_unlock(&ftrace_profile_lock);
763 ftrace_profile_read(struct file *filp, char __user *ubuf,
764 size_t cnt, loff_t *ppos)
766 char buf[64]; /* big enough to hold a number */
769 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
770 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
773 static const struct file_operations ftrace_profile_fops = {
774 .open = tracing_open_generic,
775 .read = ftrace_profile_read,
776 .write = ftrace_profile_write,
779 /* used to initialize the real stat files */
780 static struct tracer_stat function_stats __initdata = {
782 .stat_start = function_stat_start,
783 .stat_next = function_stat_next,
784 .stat_cmp = function_stat_cmp,
785 .stat_headers = function_stat_headers,
786 .stat_show = function_stat_show
789 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
791 struct ftrace_profile_stat *stat;
792 struct dentry *entry;
797 for_each_possible_cpu(cpu) {
798 stat = &per_cpu(ftrace_profile_stats, cpu);
800 /* allocate enough for function name + cpu number */
801 name = kmalloc(32, GFP_KERNEL);
804 * The files created are permanent, if something happens
805 * we still do not free memory.
808 "Could not allocate stat file for cpu %d\n",
812 stat->stat = function_stats;
813 snprintf(name, 32, "function%d", cpu);
814 stat->stat.name = name;
815 ret = register_stat_tracer(&stat->stat);
818 "Could not register function stat for cpu %d\n",
825 entry = debugfs_create_file("function_profile_enabled", 0644,
826 d_tracer, NULL, &ftrace_profile_fops);
828 pr_warning("Could not create debugfs "
829 "'function_profile_enabled' entry\n");
832 #else /* CONFIG_FUNCTION_PROFILER */
833 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
836 #endif /* CONFIG_FUNCTION_PROFILER */
838 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
840 #ifdef CONFIG_DYNAMIC_FTRACE
842 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
843 # error Dynamic ftrace depends on MCOUNT_RECORD
846 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
848 struct ftrace_func_probe {
849 struct hlist_node node;
850 struct ftrace_probe_ops *ops;
858 FTRACE_ENABLE_CALLS = (1 << 0),
859 FTRACE_DISABLE_CALLS = (1 << 1),
860 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
861 FTRACE_ENABLE_MCOUNT = (1 << 3),
862 FTRACE_DISABLE_MCOUNT = (1 << 4),
863 FTRACE_START_FUNC_RET = (1 << 5),
864 FTRACE_STOP_FUNC_RET = (1 << 6),
867 static int ftrace_filtered;
869 static struct dyn_ftrace *ftrace_new_addrs;
871 static DEFINE_MUTEX(ftrace_regex_lock);
874 struct ftrace_page *next;
876 struct dyn_ftrace records[];
879 #define ENTRIES_PER_PAGE \
880 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
882 /* estimate from running different kernels */
883 #define NR_TO_INIT 10000
885 static struct ftrace_page *ftrace_pages_start;
886 static struct ftrace_page *ftrace_pages;
888 static struct dyn_ftrace *ftrace_free_records;
891 * This is a double for. Do not use 'break' to break out of the loop,
892 * you must use a goto.
894 #define do_for_each_ftrace_rec(pg, rec) \
895 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
897 for (_____i = 0; _____i < pg->index; _____i++) { \
898 rec = &pg->records[_____i];
900 #define while_for_each_ftrace_rec() \
904 static void ftrace_free_rec(struct dyn_ftrace *rec)
906 rec->freelist = ftrace_free_records;
907 ftrace_free_records = rec;
908 rec->flags |= FTRACE_FL_FREE;
911 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
913 struct dyn_ftrace *rec;
915 /* First check for freed records */
916 if (ftrace_free_records) {
917 rec = ftrace_free_records;
919 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
920 FTRACE_WARN_ON_ONCE(1);
921 ftrace_free_records = NULL;
925 ftrace_free_records = rec->freelist;
926 memset(rec, 0, sizeof(*rec));
930 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
931 if (!ftrace_pages->next) {
932 /* allocate another page */
934 (void *)get_zeroed_page(GFP_KERNEL);
935 if (!ftrace_pages->next)
938 ftrace_pages = ftrace_pages->next;
941 return &ftrace_pages->records[ftrace_pages->index++];
944 static struct dyn_ftrace *
945 ftrace_record_ip(unsigned long ip)
947 struct dyn_ftrace *rec;
952 rec = ftrace_alloc_dyn_node(ip);
957 rec->newlist = ftrace_new_addrs;
958 ftrace_new_addrs = rec;
963 static void print_ip_ins(const char *fmt, unsigned char *p)
967 printk(KERN_CONT "%s", fmt);
969 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
970 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
973 static void ftrace_bug(int failed, unsigned long ip)
977 FTRACE_WARN_ON_ONCE(1);
978 pr_info("ftrace faulted on modifying ");
982 FTRACE_WARN_ON_ONCE(1);
983 pr_info("ftrace failed to modify ");
985 print_ip_ins(" actual: ", (unsigned char *)ip);
986 printk(KERN_CONT "\n");
989 FTRACE_WARN_ON_ONCE(1);
990 pr_info("ftrace faulted on writing ");
994 FTRACE_WARN_ON_ONCE(1);
995 pr_info("ftrace faulted on unknown error ");
1001 /* Return 1 if the address range is reserved for ftrace */
1002 int ftrace_text_reserved(void *start, void *end)
1004 struct dyn_ftrace *rec;
1005 struct ftrace_page *pg;
1007 do_for_each_ftrace_rec(pg, rec) {
1008 if (rec->ip <= (unsigned long)end &&
1009 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1011 } while_for_each_ftrace_rec();
1017 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1019 unsigned long ftrace_addr;
1020 unsigned long flag = 0UL;
1022 ftrace_addr = (unsigned long)FTRACE_ADDR;
1025 * If this record is not to be traced or we want to disable it,
1028 * If we want to enable it and filtering is off, then enable it.
1030 * If we want to enable it and filtering is on, enable it only if
1033 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1034 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1035 flag = FTRACE_FL_ENABLED;
1038 /* If the state of this record hasn't changed, then do nothing */
1039 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1043 rec->flags |= FTRACE_FL_ENABLED;
1044 return ftrace_make_call(rec, ftrace_addr);
1047 rec->flags &= ~FTRACE_FL_ENABLED;
1048 return ftrace_make_nop(NULL, rec, ftrace_addr);
1051 static void ftrace_replace_code(int enable)
1053 struct dyn_ftrace *rec;
1054 struct ftrace_page *pg;
1057 do_for_each_ftrace_rec(pg, rec) {
1059 * Skip over free records, records that have
1060 * failed and not converted.
1062 if (rec->flags & FTRACE_FL_FREE ||
1063 rec->flags & FTRACE_FL_FAILED ||
1064 !(rec->flags & FTRACE_FL_CONVERTED))
1067 failed = __ftrace_replace_code(rec, enable);
1069 rec->flags |= FTRACE_FL_FAILED;
1070 ftrace_bug(failed, rec->ip);
1071 /* Stop processing */
1074 } while_for_each_ftrace_rec();
1078 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1085 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1087 ftrace_bug(ret, ip);
1088 rec->flags |= FTRACE_FL_FAILED;
1095 * archs can override this function if they must do something
1096 * before the modifying code is performed.
1098 int __weak ftrace_arch_code_modify_prepare(void)
1104 * archs can override this function if they must do something
1105 * after the modifying code is performed.
1107 int __weak ftrace_arch_code_modify_post_process(void)
1112 static int __ftrace_modify_code(void *data)
1114 int *command = data;
1116 if (*command & FTRACE_ENABLE_CALLS)
1117 ftrace_replace_code(1);
1118 else if (*command & FTRACE_DISABLE_CALLS)
1119 ftrace_replace_code(0);
1121 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1122 ftrace_update_ftrace_func(ftrace_trace_function);
1124 if (*command & FTRACE_START_FUNC_RET)
1125 ftrace_enable_ftrace_graph_caller();
1126 else if (*command & FTRACE_STOP_FUNC_RET)
1127 ftrace_disable_ftrace_graph_caller();
1132 static void ftrace_run_update_code(int command)
1136 ret = ftrace_arch_code_modify_prepare();
1137 FTRACE_WARN_ON(ret);
1141 stop_machine(__ftrace_modify_code, &command, NULL);
1143 ret = ftrace_arch_code_modify_post_process();
1144 FTRACE_WARN_ON(ret);
1147 static ftrace_func_t saved_ftrace_func;
1148 static int ftrace_start_up;
1150 static void ftrace_startup_enable(int command)
1152 if (saved_ftrace_func != ftrace_trace_function) {
1153 saved_ftrace_func = ftrace_trace_function;
1154 command |= FTRACE_UPDATE_TRACE_FUNC;
1157 if (!command || !ftrace_enabled)
1160 ftrace_run_update_code(command);
1163 static void ftrace_startup(int command)
1165 if (unlikely(ftrace_disabled))
1169 command |= FTRACE_ENABLE_CALLS;
1171 ftrace_startup_enable(command);
1174 static void ftrace_shutdown(int command)
1176 if (unlikely(ftrace_disabled))
1181 * Just warn in case of unbalance, no need to kill ftrace, it's not
1182 * critical but the ftrace_call callers may be never nopped again after
1183 * further ftrace uses.
1185 WARN_ON_ONCE(ftrace_start_up < 0);
1187 if (!ftrace_start_up)
1188 command |= FTRACE_DISABLE_CALLS;
1190 if (saved_ftrace_func != ftrace_trace_function) {
1191 saved_ftrace_func = ftrace_trace_function;
1192 command |= FTRACE_UPDATE_TRACE_FUNC;
1195 if (!command || !ftrace_enabled)
1198 ftrace_run_update_code(command);
1201 static void ftrace_startup_sysctl(void)
1203 int command = FTRACE_ENABLE_MCOUNT;
1205 if (unlikely(ftrace_disabled))
1208 /* Force update next time */
1209 saved_ftrace_func = NULL;
1210 /* ftrace_start_up is true if we want ftrace running */
1211 if (ftrace_start_up)
1212 command |= FTRACE_ENABLE_CALLS;
1214 ftrace_run_update_code(command);
1217 static void ftrace_shutdown_sysctl(void)
1219 int command = FTRACE_DISABLE_MCOUNT;
1221 if (unlikely(ftrace_disabled))
1224 /* ftrace_start_up is true if ftrace is running */
1225 if (ftrace_start_up)
1226 command |= FTRACE_DISABLE_CALLS;
1228 ftrace_run_update_code(command);
1231 static cycle_t ftrace_update_time;
1232 static unsigned long ftrace_update_cnt;
1233 unsigned long ftrace_update_tot_cnt;
1235 static int ftrace_update_code(struct module *mod)
1237 struct dyn_ftrace *p;
1238 cycle_t start, stop;
1240 start = ftrace_now(raw_smp_processor_id());
1241 ftrace_update_cnt = 0;
1243 while (ftrace_new_addrs) {
1245 /* If something went wrong, bail without enabling anything */
1246 if (unlikely(ftrace_disabled))
1249 p = ftrace_new_addrs;
1250 ftrace_new_addrs = p->newlist;
1254 * Do the initial record convertion from mcount jump
1255 * to the NOP instructions.
1257 if (!ftrace_code_disable(mod, p)) {
1262 p->flags |= FTRACE_FL_CONVERTED;
1263 ftrace_update_cnt++;
1266 * If the tracing is enabled, go ahead and enable the record.
1268 * The reason not to enable the record immediatelly is the
1269 * inherent check of ftrace_make_nop/ftrace_make_call for
1270 * correct previous instructions. Making first the NOP
1271 * conversion puts the module to the correct state, thus
1272 * passing the ftrace_make_call check.
1274 if (ftrace_start_up) {
1275 int failed = __ftrace_replace_code(p, 1);
1277 ftrace_bug(failed, p->ip);
1283 stop = ftrace_now(raw_smp_processor_id());
1284 ftrace_update_time = stop - start;
1285 ftrace_update_tot_cnt += ftrace_update_cnt;
1290 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1292 struct ftrace_page *pg;
1296 /* allocate a few pages */
1297 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1298 if (!ftrace_pages_start)
1302 * Allocate a few more pages.
1304 * TODO: have some parser search vmlinux before
1305 * final linking to find all calls to ftrace.
1307 * a) know how many pages to allocate.
1309 * b) set up the table then.
1311 * The dynamic code is still necessary for
1315 pg = ftrace_pages = ftrace_pages_start;
1317 cnt = num_to_init / ENTRIES_PER_PAGE;
1318 pr_info("ftrace: allocating %ld entries in %d pages\n",
1319 num_to_init, cnt + 1);
1321 for (i = 0; i < cnt; i++) {
1322 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1324 /* If we fail, we'll try later anyway */
1335 FTRACE_ITER_FILTER = (1 << 0),
1336 FTRACE_ITER_NOTRACE = (1 << 1),
1337 FTRACE_ITER_FAILURES = (1 << 2),
1338 FTRACE_ITER_PRINTALL = (1 << 3),
1339 FTRACE_ITER_HASH = (1 << 4),
1342 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1344 struct ftrace_iterator {
1345 struct ftrace_page *pg;
1349 struct trace_parser parser;
1353 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1355 struct ftrace_iterator *iter = m->private;
1356 struct hlist_node *hnd = v;
1357 struct hlist_head *hhd;
1359 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1364 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1367 hhd = &ftrace_func_hash[iter->hidx];
1369 if (hlist_empty(hhd)) {
1388 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1390 struct ftrace_iterator *iter = m->private;
1394 if (!(iter->flags & FTRACE_ITER_HASH))
1397 iter->flags |= FTRACE_ITER_HASH;
1400 for (l = 0; l <= *pos; ) {
1401 p = t_hash_next(m, p, &l);
1408 static int t_hash_show(struct seq_file *m, void *v)
1410 struct ftrace_func_probe *rec;
1411 struct hlist_node *hnd = v;
1413 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1415 if (rec->ops->print)
1416 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1418 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1421 seq_printf(m, ":%p", rec->data);
1428 t_next(struct seq_file *m, void *v, loff_t *pos)
1430 struct ftrace_iterator *iter = m->private;
1431 struct dyn_ftrace *rec = NULL;
1433 if (iter->flags & FTRACE_ITER_HASH)
1434 return t_hash_next(m, v, pos);
1438 if (iter->flags & FTRACE_ITER_PRINTALL)
1442 if (iter->idx >= iter->pg->index) {
1443 if (iter->pg->next) {
1444 iter->pg = iter->pg->next;
1449 rec = &iter->pg->records[iter->idx++];
1450 if ((rec->flags & FTRACE_FL_FREE) ||
1452 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1453 (rec->flags & FTRACE_FL_FAILED)) ||
1455 ((iter->flags & FTRACE_ITER_FAILURES) &&
1456 !(rec->flags & FTRACE_FL_FAILED)) ||
1458 ((iter->flags & FTRACE_ITER_FILTER) &&
1459 !(rec->flags & FTRACE_FL_FILTER)) ||
1461 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1462 !(rec->flags & FTRACE_FL_NOTRACE))) {
1471 static void *t_start(struct seq_file *m, loff_t *pos)
1473 struct ftrace_iterator *iter = m->private;
1477 mutex_lock(&ftrace_lock);
1479 * For set_ftrace_filter reading, if we have the filter
1480 * off, we can short cut and just print out that all
1481 * functions are enabled.
1483 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1485 return t_hash_start(m, pos);
1486 iter->flags |= FTRACE_ITER_PRINTALL;
1490 if (iter->flags & FTRACE_ITER_HASH)
1491 return t_hash_start(m, pos);
1493 iter->pg = ftrace_pages_start;
1495 for (l = 0; l <= *pos; ) {
1496 p = t_next(m, p, &l);
1501 if (!p && iter->flags & FTRACE_ITER_FILTER)
1502 return t_hash_start(m, pos);
1507 static void t_stop(struct seq_file *m, void *p)
1509 mutex_unlock(&ftrace_lock);
1512 static int t_show(struct seq_file *m, void *v)
1514 struct ftrace_iterator *iter = m->private;
1515 struct dyn_ftrace *rec = v;
1517 if (iter->flags & FTRACE_ITER_HASH)
1518 return t_hash_show(m, v);
1520 if (iter->flags & FTRACE_ITER_PRINTALL) {
1521 seq_printf(m, "#### all functions enabled ####\n");
1528 seq_printf(m, "%ps\n", (void *)rec->ip);
1533 static const struct seq_operations show_ftrace_seq_ops = {
1541 ftrace_avail_open(struct inode *inode, struct file *file)
1543 struct ftrace_iterator *iter;
1546 if (unlikely(ftrace_disabled))
1549 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1553 iter->pg = ftrace_pages_start;
1555 ret = seq_open(file, &show_ftrace_seq_ops);
1557 struct seq_file *m = file->private_data;
1568 ftrace_failures_open(struct inode *inode, struct file *file)
1572 struct ftrace_iterator *iter;
1574 ret = ftrace_avail_open(inode, file);
1576 m = (struct seq_file *)file->private_data;
1577 iter = (struct ftrace_iterator *)m->private;
1578 iter->flags = FTRACE_ITER_FAILURES;
1585 static void ftrace_filter_reset(int enable)
1587 struct ftrace_page *pg;
1588 struct dyn_ftrace *rec;
1589 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1591 mutex_lock(&ftrace_lock);
1593 ftrace_filtered = 0;
1594 do_for_each_ftrace_rec(pg, rec) {
1595 if (rec->flags & FTRACE_FL_FAILED)
1597 rec->flags &= ~type;
1598 } while_for_each_ftrace_rec();
1599 mutex_unlock(&ftrace_lock);
1603 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1605 struct ftrace_iterator *iter;
1608 if (unlikely(ftrace_disabled))
1611 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1615 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1620 mutex_lock(&ftrace_regex_lock);
1621 if ((file->f_mode & FMODE_WRITE) &&
1622 (file->f_flags & O_TRUNC))
1623 ftrace_filter_reset(enable);
1625 if (file->f_mode & FMODE_READ) {
1626 iter->pg = ftrace_pages_start;
1627 iter->flags = enable ? FTRACE_ITER_FILTER :
1628 FTRACE_ITER_NOTRACE;
1630 ret = seq_open(file, &show_ftrace_seq_ops);
1632 struct seq_file *m = file->private_data;
1635 trace_parser_put(&iter->parser);
1639 file->private_data = iter;
1640 mutex_unlock(&ftrace_regex_lock);
1646 ftrace_filter_open(struct inode *inode, struct file *file)
1648 return ftrace_regex_open(inode, file, 1);
1652 ftrace_notrace_open(struct inode *inode, struct file *file)
1654 return ftrace_regex_open(inode, file, 0);
1658 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1662 if (file->f_mode & FMODE_READ)
1663 ret = seq_lseek(file, offset, origin);
1665 file->f_pos = ret = 1;
1670 static int ftrace_match(char *str, char *regex, int len, int type)
1677 if (strcmp(str, regex) == 0)
1680 case MATCH_FRONT_ONLY:
1681 if (strncmp(str, regex, len) == 0)
1684 case MATCH_MIDDLE_ONLY:
1685 if (strstr(str, regex))
1688 case MATCH_END_ONLY:
1690 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
1699 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1701 char str[KSYM_SYMBOL_LEN];
1703 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1704 return ftrace_match(str, regex, len, type);
1707 static int ftrace_match_records(char *buff, int len, int enable)
1709 unsigned int search_len;
1710 struct ftrace_page *pg;
1711 struct dyn_ftrace *rec;
1718 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1719 type = filter_parse_regex(buff, len, &search, ¬);
1721 search_len = strlen(search);
1723 mutex_lock(&ftrace_lock);
1724 do_for_each_ftrace_rec(pg, rec) {
1726 if (rec->flags & FTRACE_FL_FAILED)
1729 if (ftrace_match_record(rec, search, search_len, type)) {
1731 rec->flags &= ~flag;
1737 * Only enable filtering if we have a function that
1740 if (enable && (rec->flags & FTRACE_FL_FILTER))
1741 ftrace_filtered = 1;
1742 } while_for_each_ftrace_rec();
1743 mutex_unlock(&ftrace_lock);
1749 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1750 char *regex, int len, int type)
1752 char str[KSYM_SYMBOL_LEN];
1755 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1757 if (!modname || strcmp(modname, mod))
1760 /* blank search means to match all funcs in the mod */
1762 return ftrace_match(str, regex, len, type);
1767 static int ftrace_match_module_records(char *buff, char *mod, int enable)
1769 unsigned search_len = 0;
1770 struct ftrace_page *pg;
1771 struct dyn_ftrace *rec;
1772 int type = MATCH_FULL;
1773 char *search = buff;
1778 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1780 /* blank or '*' mean the same */
1781 if (strcmp(buff, "*") == 0)
1784 /* handle the case of 'dont filter this module' */
1785 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1791 type = filter_parse_regex(buff, strlen(buff), &search, ¬);
1792 search_len = strlen(search);
1795 mutex_lock(&ftrace_lock);
1796 do_for_each_ftrace_rec(pg, rec) {
1798 if (rec->flags & FTRACE_FL_FAILED)
1801 if (ftrace_match_module_record(rec, mod,
1802 search, search_len, type)) {
1804 rec->flags &= ~flag;
1809 if (enable && (rec->flags & FTRACE_FL_FILTER))
1810 ftrace_filtered = 1;
1812 } while_for_each_ftrace_rec();
1813 mutex_unlock(&ftrace_lock);
1819 * We register the module command as a template to show others how
1820 * to register the a command as well.
1824 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1829 * cmd == 'mod' because we only registered this func
1830 * for the 'mod' ftrace_func_command.
1831 * But if you register one func with multiple commands,
1832 * you can tell which command was used by the cmd
1836 /* we must have a module name */
1840 mod = strsep(¶m, ":");
1844 if (ftrace_match_module_records(func, mod, enable))
1849 static struct ftrace_func_command ftrace_mod_cmd = {
1851 .func = ftrace_mod_callback,
1854 static int __init ftrace_mod_cmd_init(void)
1856 return register_ftrace_command(&ftrace_mod_cmd);
1858 device_initcall(ftrace_mod_cmd_init);
1861 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1863 struct ftrace_func_probe *entry;
1864 struct hlist_head *hhd;
1865 struct hlist_node *n;
1869 key = hash_long(ip, FTRACE_HASH_BITS);
1871 hhd = &ftrace_func_hash[key];
1873 if (hlist_empty(hhd))
1877 * Disable preemption for these calls to prevent a RCU grace
1878 * period. This syncs the hash iteration and freeing of items
1879 * on the hash. rcu_read_lock is too dangerous here.
1881 resched = ftrace_preempt_disable();
1882 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1883 if (entry->ip == ip)
1884 entry->ops->func(ip, parent_ip, &entry->data);
1886 ftrace_preempt_enable(resched);
1889 static struct ftrace_ops trace_probe_ops __read_mostly =
1891 .func = function_trace_probe_call,
1894 static int ftrace_probe_registered;
1896 static void __enable_ftrace_function_probe(void)
1900 if (ftrace_probe_registered)
1903 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1904 struct hlist_head *hhd = &ftrace_func_hash[i];
1908 /* Nothing registered? */
1909 if (i == FTRACE_FUNC_HASHSIZE)
1912 __register_ftrace_function(&trace_probe_ops);
1914 ftrace_probe_registered = 1;
1917 static void __disable_ftrace_function_probe(void)
1921 if (!ftrace_probe_registered)
1924 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1925 struct hlist_head *hhd = &ftrace_func_hash[i];
1930 /* no more funcs left */
1931 __unregister_ftrace_function(&trace_probe_ops);
1933 ftrace_probe_registered = 0;
1937 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1939 struct ftrace_func_probe *entry =
1940 container_of(rhp, struct ftrace_func_probe, rcu);
1942 if (entry->ops->free)
1943 entry->ops->free(&entry->data);
1949 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1952 struct ftrace_func_probe *entry;
1953 struct ftrace_page *pg;
1954 struct dyn_ftrace *rec;
1960 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
1961 len = strlen(search);
1963 /* we do not support '!' for function probes */
1967 mutex_lock(&ftrace_lock);
1968 do_for_each_ftrace_rec(pg, rec) {
1970 if (rec->flags & FTRACE_FL_FAILED)
1973 if (!ftrace_match_record(rec, search, len, type))
1976 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1978 /* If we did not process any, then return error */
1989 * The caller might want to do something special
1990 * for each function we find. We call the callback
1991 * to give the caller an opportunity to do so.
1993 if (ops->callback) {
1994 if (ops->callback(rec->ip, &entry->data) < 0) {
1995 /* caller does not like this func */
2002 entry->ip = rec->ip;
2004 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2005 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2007 } while_for_each_ftrace_rec();
2008 __enable_ftrace_function_probe();
2011 mutex_unlock(&ftrace_lock);
2017 PROBE_TEST_FUNC = 1,
2022 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2023 void *data, int flags)
2025 struct ftrace_func_probe *entry;
2026 struct hlist_node *n, *tmp;
2027 char str[KSYM_SYMBOL_LEN];
2028 int type = MATCH_FULL;
2032 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2037 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2038 len = strlen(search);
2040 /* we do not support '!' for function probes */
2045 mutex_lock(&ftrace_lock);
2046 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2047 struct hlist_head *hhd = &ftrace_func_hash[i];
2049 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2051 /* break up if statements for readability */
2052 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2055 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2058 /* do this last, since it is the most expensive */
2060 kallsyms_lookup(entry->ip, NULL, NULL,
2062 if (!ftrace_match(str, glob, len, type))
2066 hlist_del(&entry->node);
2067 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2070 __disable_ftrace_function_probe();
2071 mutex_unlock(&ftrace_lock);
2075 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2078 __unregister_ftrace_function_probe(glob, ops, data,
2079 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2083 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2085 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2088 void unregister_ftrace_function_probe_all(char *glob)
2090 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2093 static LIST_HEAD(ftrace_commands);
2094 static DEFINE_MUTEX(ftrace_cmd_mutex);
2096 int register_ftrace_command(struct ftrace_func_command *cmd)
2098 struct ftrace_func_command *p;
2101 mutex_lock(&ftrace_cmd_mutex);
2102 list_for_each_entry(p, &ftrace_commands, list) {
2103 if (strcmp(cmd->name, p->name) == 0) {
2108 list_add(&cmd->list, &ftrace_commands);
2110 mutex_unlock(&ftrace_cmd_mutex);
2115 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2117 struct ftrace_func_command *p, *n;
2120 mutex_lock(&ftrace_cmd_mutex);
2121 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2122 if (strcmp(cmd->name, p->name) == 0) {
2124 list_del_init(&p->list);
2129 mutex_unlock(&ftrace_cmd_mutex);
2134 static int ftrace_process_regex(char *buff, int len, int enable)
2136 char *func, *command, *next = buff;
2137 struct ftrace_func_command *p;
2140 func = strsep(&next, ":");
2143 if (ftrace_match_records(func, len, enable))
2150 command = strsep(&next, ":");
2152 mutex_lock(&ftrace_cmd_mutex);
2153 list_for_each_entry(p, &ftrace_commands, list) {
2154 if (strcmp(p->name, command) == 0) {
2155 ret = p->func(func, command, next, enable);
2160 mutex_unlock(&ftrace_cmd_mutex);
2166 ftrace_regex_write(struct file *file, const char __user *ubuf,
2167 size_t cnt, loff_t *ppos, int enable)
2169 struct ftrace_iterator *iter;
2170 struct trace_parser *parser;
2176 mutex_lock(&ftrace_regex_lock);
2178 if (file->f_mode & FMODE_READ) {
2179 struct seq_file *m = file->private_data;
2182 iter = file->private_data;
2184 parser = &iter->parser;
2185 read = trace_get_user(parser, ubuf, cnt, ppos);
2187 if (read >= 0 && trace_parser_loaded(parser) &&
2188 !trace_parser_cont(parser)) {
2189 ret = ftrace_process_regex(parser->buffer,
2190 parser->idx, enable);
2191 trace_parser_clear(parser);
2198 mutex_unlock(&ftrace_regex_lock);
2204 ftrace_filter_write(struct file *file, const char __user *ubuf,
2205 size_t cnt, loff_t *ppos)
2207 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2211 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2212 size_t cnt, loff_t *ppos)
2214 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2218 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2220 if (unlikely(ftrace_disabled))
2223 mutex_lock(&ftrace_regex_lock);
2225 ftrace_filter_reset(enable);
2227 ftrace_match_records(buf, len, enable);
2228 mutex_unlock(&ftrace_regex_lock);
2232 * ftrace_set_filter - set a function to filter on in ftrace
2233 * @buf - the string that holds the function filter text.
2234 * @len - the length of the string.
2235 * @reset - non zero to reset all filters before applying this filter.
2237 * Filters denote which functions should be enabled when tracing is enabled.
2238 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2240 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2242 ftrace_set_regex(buf, len, reset, 1);
2246 * ftrace_set_notrace - set a function to not trace in ftrace
2247 * @buf - the string that holds the function notrace text.
2248 * @len - the length of the string.
2249 * @reset - non zero to reset all filters before applying this filter.
2251 * Notrace Filters denote which functions should not be enabled when tracing
2252 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2255 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2257 ftrace_set_regex(buf, len, reset, 0);
2261 * command line interface to allow users to set filters on boot up.
2263 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2264 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2265 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2267 static int __init set_ftrace_notrace(char *str)
2269 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2272 __setup("ftrace_notrace=", set_ftrace_notrace);
2274 static int __init set_ftrace_filter(char *str)
2276 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2279 __setup("ftrace_filter=", set_ftrace_filter);
2281 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2282 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2283 static int __init set_graph_function(char *str)
2285 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2288 __setup("ftrace_graph_filter=", set_graph_function);
2290 static void __init set_ftrace_early_graph(char *buf)
2296 func = strsep(&buf, ",");
2297 /* we allow only one expression at a time */
2298 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2301 printk(KERN_DEBUG "ftrace: function %s not "
2302 "traceable\n", func);
2305 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2307 static void __init set_ftrace_early_filter(char *buf, int enable)
2312 func = strsep(&buf, ",");
2313 ftrace_set_regex(func, strlen(func), 0, enable);
2317 static void __init set_ftrace_early_filters(void)
2319 if (ftrace_filter_buf[0])
2320 set_ftrace_early_filter(ftrace_filter_buf, 1);
2321 if (ftrace_notrace_buf[0])
2322 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2323 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2324 if (ftrace_graph_buf[0])
2325 set_ftrace_early_graph(ftrace_graph_buf);
2326 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2330 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2332 struct seq_file *m = (struct seq_file *)file->private_data;
2333 struct ftrace_iterator *iter;
2334 struct trace_parser *parser;
2336 mutex_lock(&ftrace_regex_lock);
2337 if (file->f_mode & FMODE_READ) {
2340 seq_release(inode, file);
2342 iter = file->private_data;
2344 parser = &iter->parser;
2345 if (trace_parser_loaded(parser)) {
2346 parser->buffer[parser->idx] = 0;
2347 ftrace_match_records(parser->buffer, parser->idx, enable);
2350 mutex_lock(&ftrace_lock);
2351 if (ftrace_start_up && ftrace_enabled)
2352 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2353 mutex_unlock(&ftrace_lock);
2355 trace_parser_put(parser);
2358 mutex_unlock(&ftrace_regex_lock);
2363 ftrace_filter_release(struct inode *inode, struct file *file)
2365 return ftrace_regex_release(inode, file, 1);
2369 ftrace_notrace_release(struct inode *inode, struct file *file)
2371 return ftrace_regex_release(inode, file, 0);
2374 static const struct file_operations ftrace_avail_fops = {
2375 .open = ftrace_avail_open,
2377 .llseek = seq_lseek,
2378 .release = seq_release_private,
2381 static const struct file_operations ftrace_failures_fops = {
2382 .open = ftrace_failures_open,
2384 .llseek = seq_lseek,
2385 .release = seq_release_private,
2388 static const struct file_operations ftrace_filter_fops = {
2389 .open = ftrace_filter_open,
2391 .write = ftrace_filter_write,
2392 .llseek = ftrace_regex_lseek,
2393 .release = ftrace_filter_release,
2396 static const struct file_operations ftrace_notrace_fops = {
2397 .open = ftrace_notrace_open,
2399 .write = ftrace_notrace_write,
2400 .llseek = ftrace_regex_lseek,
2401 .release = ftrace_notrace_release,
2404 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2406 static DEFINE_MUTEX(graph_lock);
2408 int ftrace_graph_count;
2409 int ftrace_graph_filter_enabled;
2410 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2413 __g_next(struct seq_file *m, loff_t *pos)
2415 if (*pos >= ftrace_graph_count)
2417 return &ftrace_graph_funcs[*pos];
2421 g_next(struct seq_file *m, void *v, loff_t *pos)
2424 return __g_next(m, pos);
2427 static void *g_start(struct seq_file *m, loff_t *pos)
2429 mutex_lock(&graph_lock);
2431 /* Nothing, tell g_show to print all functions are enabled */
2432 if (!ftrace_graph_filter_enabled && !*pos)
2435 return __g_next(m, pos);
2438 static void g_stop(struct seq_file *m, void *p)
2440 mutex_unlock(&graph_lock);
2443 static int g_show(struct seq_file *m, void *v)
2445 unsigned long *ptr = v;
2450 if (ptr == (unsigned long *)1) {
2451 seq_printf(m, "#### all functions enabled ####\n");
2455 seq_printf(m, "%ps\n", (void *)*ptr);
2460 static const struct seq_operations ftrace_graph_seq_ops = {
2468 ftrace_graph_open(struct inode *inode, struct file *file)
2472 if (unlikely(ftrace_disabled))
2475 mutex_lock(&graph_lock);
2476 if ((file->f_mode & FMODE_WRITE) &&
2477 (file->f_flags & O_TRUNC)) {
2478 ftrace_graph_filter_enabled = 0;
2479 ftrace_graph_count = 0;
2480 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2482 mutex_unlock(&graph_lock);
2484 if (file->f_mode & FMODE_READ)
2485 ret = seq_open(file, &ftrace_graph_seq_ops);
2491 ftrace_graph_release(struct inode *inode, struct file *file)
2493 if (file->f_mode & FMODE_READ)
2494 seq_release(inode, file);
2499 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2501 struct dyn_ftrace *rec;
2502 struct ftrace_page *pg;
2510 if (ftrace_disabled)
2514 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
2515 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2518 search_len = strlen(search);
2520 mutex_lock(&ftrace_lock);
2521 do_for_each_ftrace_rec(pg, rec) {
2523 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2526 if (ftrace_match_record(rec, search, search_len, type)) {
2527 /* if it is in the array */
2529 for (i = 0; i < *idx; i++) {
2530 if (array[i] == rec->ip) {
2539 array[(*idx)++] = rec->ip;
2540 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2545 array[i] = array[--(*idx)];
2551 } while_for_each_ftrace_rec();
2553 mutex_unlock(&ftrace_lock);
2558 ftrace_graph_filter_enabled = 1;
2563 ftrace_graph_write(struct file *file, const char __user *ubuf,
2564 size_t cnt, loff_t *ppos)
2566 struct trace_parser parser;
2572 mutex_lock(&graph_lock);
2574 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2579 read = trace_get_user(&parser, ubuf, cnt, ppos);
2581 if (read >= 0 && trace_parser_loaded((&parser))) {
2582 parser.buffer[parser.idx] = 0;
2584 /* we allow only one expression at a time */
2585 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2594 trace_parser_put(&parser);
2596 mutex_unlock(&graph_lock);
2601 static const struct file_operations ftrace_graph_fops = {
2602 .open = ftrace_graph_open,
2604 .write = ftrace_graph_write,
2605 .release = ftrace_graph_release,
2607 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2609 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2612 trace_create_file("available_filter_functions", 0444,
2613 d_tracer, NULL, &ftrace_avail_fops);
2615 trace_create_file("failures", 0444,
2616 d_tracer, NULL, &ftrace_failures_fops);
2618 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2619 NULL, &ftrace_filter_fops);
2621 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2622 NULL, &ftrace_notrace_fops);
2624 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2625 trace_create_file("set_graph_function", 0444, d_tracer,
2627 &ftrace_graph_fops);
2628 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2633 static int ftrace_process_locs(struct module *mod,
2634 unsigned long *start,
2639 unsigned long flags;
2641 mutex_lock(&ftrace_lock);
2644 addr = ftrace_call_adjust(*p++);
2646 * Some architecture linkers will pad between
2647 * the different mcount_loc sections of different
2648 * object files to satisfy alignments.
2649 * Skip any NULL pointers.
2653 ftrace_record_ip(addr);
2656 /* disable interrupts to prevent kstop machine */
2657 local_irq_save(flags);
2658 ftrace_update_code(mod);
2659 local_irq_restore(flags);
2660 mutex_unlock(&ftrace_lock);
2665 #ifdef CONFIG_MODULES
2666 void ftrace_release_mod(struct module *mod)
2668 struct dyn_ftrace *rec;
2669 struct ftrace_page *pg;
2671 if (ftrace_disabled)
2674 mutex_lock(&ftrace_lock);
2675 do_for_each_ftrace_rec(pg, rec) {
2676 if (within_module_core(rec->ip, mod)) {
2678 * rec->ip is changed in ftrace_free_rec()
2679 * It should not between s and e if record was freed.
2681 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2682 ftrace_free_rec(rec);
2684 } while_for_each_ftrace_rec();
2685 mutex_unlock(&ftrace_lock);
2688 static void ftrace_init_module(struct module *mod,
2689 unsigned long *start, unsigned long *end)
2691 if (ftrace_disabled || start == end)
2693 ftrace_process_locs(mod, start, end);
2696 static int ftrace_module_notify(struct notifier_block *self,
2697 unsigned long val, void *data)
2699 struct module *mod = data;
2702 case MODULE_STATE_COMING:
2703 ftrace_init_module(mod, mod->ftrace_callsites,
2704 mod->ftrace_callsites +
2705 mod->num_ftrace_callsites);
2707 case MODULE_STATE_GOING:
2708 ftrace_release_mod(mod);
2715 static int ftrace_module_notify(struct notifier_block *self,
2716 unsigned long val, void *data)
2720 #endif /* CONFIG_MODULES */
2722 struct notifier_block ftrace_module_nb = {
2723 .notifier_call = ftrace_module_notify,
2727 extern unsigned long __start_mcount_loc[];
2728 extern unsigned long __stop_mcount_loc[];
2730 void __init ftrace_init(void)
2732 unsigned long count, addr, flags;
2735 /* Keep the ftrace pointer to the stub */
2736 addr = (unsigned long)ftrace_stub;
2738 local_irq_save(flags);
2739 ftrace_dyn_arch_init(&addr);
2740 local_irq_restore(flags);
2742 /* ftrace_dyn_arch_init places the return code in addr */
2746 count = __stop_mcount_loc - __start_mcount_loc;
2748 ret = ftrace_dyn_table_alloc(count);
2752 last_ftrace_enabled = ftrace_enabled = 1;
2754 ret = ftrace_process_locs(NULL,
2758 ret = register_module_notifier(&ftrace_module_nb);
2760 pr_warning("Failed to register trace ftrace module notifier\n");
2762 set_ftrace_early_filters();
2766 ftrace_disabled = 1;
2771 static int __init ftrace_nodyn_init(void)
2776 device_initcall(ftrace_nodyn_init);
2778 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2779 static inline void ftrace_startup_enable(int command) { }
2780 /* Keep as macros so we do not need to define the commands */
2781 # define ftrace_startup(command) do { } while (0)
2782 # define ftrace_shutdown(command) do { } while (0)
2783 # define ftrace_startup_sysctl() do { } while (0)
2784 # define ftrace_shutdown_sysctl() do { } while (0)
2785 #endif /* CONFIG_DYNAMIC_FTRACE */
2787 static void clear_ftrace_swapper(void)
2789 struct task_struct *p;
2793 for_each_online_cpu(cpu) {
2795 clear_tsk_trace_trace(p);
2800 static void set_ftrace_swapper(void)
2802 struct task_struct *p;
2806 for_each_online_cpu(cpu) {
2808 set_tsk_trace_trace(p);
2813 static void clear_ftrace_pid(struct pid *pid)
2815 struct task_struct *p;
2818 do_each_pid_task(pid, PIDTYPE_PID, p) {
2819 clear_tsk_trace_trace(p);
2820 } while_each_pid_task(pid, PIDTYPE_PID, p);
2826 static void set_ftrace_pid(struct pid *pid)
2828 struct task_struct *p;
2831 do_each_pid_task(pid, PIDTYPE_PID, p) {
2832 set_tsk_trace_trace(p);
2833 } while_each_pid_task(pid, PIDTYPE_PID, p);
2837 static void clear_ftrace_pid_task(struct pid *pid)
2839 if (pid == ftrace_swapper_pid)
2840 clear_ftrace_swapper();
2842 clear_ftrace_pid(pid);
2845 static void set_ftrace_pid_task(struct pid *pid)
2847 if (pid == ftrace_swapper_pid)
2848 set_ftrace_swapper();
2850 set_ftrace_pid(pid);
2853 static int ftrace_pid_add(int p)
2856 struct ftrace_pid *fpid;
2859 mutex_lock(&ftrace_lock);
2862 pid = ftrace_swapper_pid;
2864 pid = find_get_pid(p);
2871 list_for_each_entry(fpid, &ftrace_pids, list)
2872 if (fpid->pid == pid)
2877 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2881 list_add(&fpid->list, &ftrace_pids);
2884 set_ftrace_pid_task(pid);
2886 ftrace_update_pid_func();
2887 ftrace_startup_enable(0);
2889 mutex_unlock(&ftrace_lock);
2893 if (pid != ftrace_swapper_pid)
2897 mutex_unlock(&ftrace_lock);
2901 static void ftrace_pid_reset(void)
2903 struct ftrace_pid *fpid, *safe;
2905 mutex_lock(&ftrace_lock);
2906 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2907 struct pid *pid = fpid->pid;
2909 clear_ftrace_pid_task(pid);
2911 list_del(&fpid->list);
2915 ftrace_update_pid_func();
2916 ftrace_startup_enable(0);
2918 mutex_unlock(&ftrace_lock);
2921 static void *fpid_start(struct seq_file *m, loff_t *pos)
2923 mutex_lock(&ftrace_lock);
2925 if (list_empty(&ftrace_pids) && (!*pos))
2928 return seq_list_start(&ftrace_pids, *pos);
2931 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2936 return seq_list_next(v, &ftrace_pids, pos);
2939 static void fpid_stop(struct seq_file *m, void *p)
2941 mutex_unlock(&ftrace_lock);
2944 static int fpid_show(struct seq_file *m, void *v)
2946 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2948 if (v == (void *)1) {
2949 seq_printf(m, "no pid\n");
2953 if (fpid->pid == ftrace_swapper_pid)
2954 seq_printf(m, "swapper tasks\n");
2956 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2961 static const struct seq_operations ftrace_pid_sops = {
2962 .start = fpid_start,
2969 ftrace_pid_open(struct inode *inode, struct file *file)
2973 if ((file->f_mode & FMODE_WRITE) &&
2974 (file->f_flags & O_TRUNC))
2977 if (file->f_mode & FMODE_READ)
2978 ret = seq_open(file, &ftrace_pid_sops);
2984 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2985 size_t cnt, loff_t *ppos)
2991 if (cnt >= sizeof(buf))
2994 if (copy_from_user(&buf, ubuf, cnt))
3000 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3001 * to clean the filter quietly.
3003 tmp = strstrip(buf);
3004 if (strlen(tmp) == 0)
3007 ret = strict_strtol(tmp, 10, &val);
3011 ret = ftrace_pid_add(val);
3013 return ret ? ret : cnt;
3017 ftrace_pid_release(struct inode *inode, struct file *file)
3019 if (file->f_mode & FMODE_READ)
3020 seq_release(inode, file);
3025 static const struct file_operations ftrace_pid_fops = {
3026 .open = ftrace_pid_open,
3027 .write = ftrace_pid_write,
3029 .llseek = seq_lseek,
3030 .release = ftrace_pid_release,
3033 static __init int ftrace_init_debugfs(void)
3035 struct dentry *d_tracer;
3037 d_tracer = tracing_init_dentry();
3041 ftrace_init_dyn_debugfs(d_tracer);
3043 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3044 NULL, &ftrace_pid_fops);
3046 ftrace_profile_debugfs(d_tracer);
3050 fs_initcall(ftrace_init_debugfs);
3053 * ftrace_kill - kill ftrace
3055 * This function should be used by panic code. It stops ftrace
3056 * but in a not so nice way. If you need to simply kill ftrace
3057 * from a non-atomic section, use ftrace_kill.
3059 void ftrace_kill(void)
3061 ftrace_disabled = 1;
3063 clear_ftrace_function();
3067 * register_ftrace_function - register a function for profiling
3068 * @ops - ops structure that holds the function for profiling.
3070 * Register a function to be called by all functions in the
3073 * Note: @ops->func and all the functions it calls must be labeled
3074 * with "notrace", otherwise it will go into a
3077 int register_ftrace_function(struct ftrace_ops *ops)
3081 if (unlikely(ftrace_disabled))
3084 mutex_lock(&ftrace_lock);
3086 ret = __register_ftrace_function(ops);
3089 mutex_unlock(&ftrace_lock);
3094 * unregister_ftrace_function - unregister a function for profiling.
3095 * @ops - ops structure that holds the function to unregister
3097 * Unregister a function that was added to be called by ftrace profiling.
3099 int unregister_ftrace_function(struct ftrace_ops *ops)
3103 mutex_lock(&ftrace_lock);
3104 ret = __unregister_ftrace_function(ops);
3106 mutex_unlock(&ftrace_lock);
3112 ftrace_enable_sysctl(struct ctl_table *table, int write,
3113 void __user *buffer, size_t *lenp,
3118 if (unlikely(ftrace_disabled))
3121 mutex_lock(&ftrace_lock);
3123 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3125 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3128 last_ftrace_enabled = !!ftrace_enabled;
3130 if (ftrace_enabled) {
3132 ftrace_startup_sysctl();
3134 /* we are starting ftrace again */
3135 if (ftrace_list != &ftrace_list_end) {
3136 if (ftrace_list->next == &ftrace_list_end)
3137 ftrace_trace_function = ftrace_list->func;
3139 ftrace_trace_function = ftrace_list_func;
3143 /* stopping ftrace calls (just send to ftrace_stub) */
3144 ftrace_trace_function = ftrace_stub;
3146 ftrace_shutdown_sysctl();
3150 mutex_unlock(&ftrace_lock);
3154 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3156 static int ftrace_graph_active;
3157 static struct notifier_block ftrace_suspend_notifier;
3159 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3164 /* The callbacks that hook a function */
3165 trace_func_graph_ret_t ftrace_graph_return =
3166 (trace_func_graph_ret_t)ftrace_stub;
3167 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3169 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3170 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3174 unsigned long flags;
3175 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3176 struct task_struct *g, *t;
3178 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3179 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3180 * sizeof(struct ftrace_ret_stack),
3182 if (!ret_stack_list[i]) {
3190 read_lock_irqsave(&tasklist_lock, flags);
3191 do_each_thread(g, t) {
3197 if (t->ret_stack == NULL) {
3198 atomic_set(&t->tracing_graph_pause, 0);
3199 atomic_set(&t->trace_overrun, 0);
3200 t->curr_ret_stack = -1;
3201 /* Make sure the tasks see the -1 first: */
3203 t->ret_stack = ret_stack_list[start++];
3205 } while_each_thread(g, t);
3208 read_unlock_irqrestore(&tasklist_lock, flags);
3210 for (i = start; i < end; i++)
3211 kfree(ret_stack_list[i]);
3216 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3217 struct task_struct *next)
3219 unsigned long long timestamp;
3223 * Does the user want to count the time a function was asleep.
3224 * If so, do not update the time stamps.
3226 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3229 timestamp = trace_clock_local();
3231 prev->ftrace_timestamp = timestamp;
3233 /* only process tasks that we timestamped */
3234 if (!next->ftrace_timestamp)
3238 * Update all the counters in next to make up for the
3239 * time next was sleeping.
3241 timestamp -= next->ftrace_timestamp;
3243 for (index = next->curr_ret_stack; index >= 0; index--)
3244 next->ret_stack[index].calltime += timestamp;
3247 /* Allocate a return stack for each task */
3248 static int start_graph_tracing(void)
3250 struct ftrace_ret_stack **ret_stack_list;
3253 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3254 sizeof(struct ftrace_ret_stack *),
3257 if (!ret_stack_list)
3260 /* The cpu_boot init_task->ret_stack will never be freed */
3261 for_each_online_cpu(cpu) {
3262 if (!idle_task(cpu)->ret_stack)
3263 ftrace_graph_init_task(idle_task(cpu));
3267 ret = alloc_retstack_tasklist(ret_stack_list);
3268 } while (ret == -EAGAIN);
3271 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3273 pr_info("ftrace_graph: Couldn't activate tracepoint"
3274 " probe to kernel_sched_switch\n");
3277 kfree(ret_stack_list);
3282 * Hibernation protection.
3283 * The state of the current task is too much unstable during
3284 * suspend/restore to disk. We want to protect against that.
3287 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3291 case PM_HIBERNATION_PREPARE:
3292 pause_graph_tracing();
3295 case PM_POST_HIBERNATION:
3296 unpause_graph_tracing();
3302 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3303 trace_func_graph_ent_t entryfunc)
3307 mutex_lock(&ftrace_lock);
3309 /* we currently allow only one tracer registered at a time */
3310 if (ftrace_graph_active) {
3315 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3316 register_pm_notifier(&ftrace_suspend_notifier);
3318 ftrace_graph_active++;
3319 ret = start_graph_tracing();
3321 ftrace_graph_active--;
3325 ftrace_graph_return = retfunc;
3326 ftrace_graph_entry = entryfunc;
3328 ftrace_startup(FTRACE_START_FUNC_RET);
3331 mutex_unlock(&ftrace_lock);
3335 void unregister_ftrace_graph(void)
3337 mutex_lock(&ftrace_lock);
3339 if (unlikely(!ftrace_graph_active))
3342 ftrace_graph_active--;
3343 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3344 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3345 ftrace_graph_entry = ftrace_graph_entry_stub;
3346 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3347 unregister_pm_notifier(&ftrace_suspend_notifier);
3350 mutex_unlock(&ftrace_lock);
3353 /* Allocate a return stack for newly created task */
3354 void ftrace_graph_init_task(struct task_struct *t)
3356 /* Make sure we do not use the parent ret_stack */
3357 t->ret_stack = NULL;
3359 if (ftrace_graph_active) {
3360 struct ftrace_ret_stack *ret_stack;
3362 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3363 * sizeof(struct ftrace_ret_stack),
3367 t->curr_ret_stack = -1;
3368 atomic_set(&t->tracing_graph_pause, 0);
3369 atomic_set(&t->trace_overrun, 0);
3370 t->ftrace_timestamp = 0;
3371 /* make curr_ret_stack visable before we add the ret_stack */
3373 t->ret_stack = ret_stack;
3377 void ftrace_graph_exit_task(struct task_struct *t)
3379 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3381 t->ret_stack = NULL;
3382 /* NULL must become visible to IRQs before we free it: */
3388 void ftrace_graph_stop(void)