2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/kprobes.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
32 #include <trace/events/sched.h>
34 #include <asm/ftrace.h>
35 #include <asm/setup.h>
37 #include "trace_output.h"
38 #include "trace_stat.h"
40 #define FTRACE_WARN_ON(cond) \
46 #define FTRACE_WARN_ON_ONCE(cond) \
48 if (WARN_ON_ONCE(cond)) \
52 /* hash bits for specific function selection */
53 #define FTRACE_HASH_BITS 7
54 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56 /* ftrace_enabled is a method to turn ftrace on or off */
57 int ftrace_enabled __read_mostly;
58 static int last_ftrace_enabled;
60 /* Quick disabling of function tracer. */
61 int function_trace_stop;
63 /* List for set_ftrace_pid's pids. */
64 LIST_HEAD(ftrace_pids);
66 struct list_head list;
71 * ftrace_disabled is set when an anomaly is discovered.
72 * ftrace_disabled is much stronger than ftrace_enabled.
74 static int ftrace_disabled __read_mostly;
76 static DEFINE_MUTEX(ftrace_lock);
78 static struct ftrace_ops ftrace_list_end __read_mostly =
83 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
84 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
85 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
86 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
88 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
89 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
92 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
94 struct ftrace_ops *op = ftrace_list;
96 /* in case someone actually ports this to alpha! */
97 read_barrier_depends();
99 while (op != &ftrace_list_end) {
101 read_barrier_depends();
102 op->func(ip, parent_ip);
107 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
109 if (!test_tsk_trace_trace(current))
112 ftrace_pid_function(ip, parent_ip);
115 static void set_ftrace_pid_function(ftrace_func_t func)
117 /* do not set ftrace_pid_function to itself! */
118 if (func != ftrace_pid_func)
119 ftrace_pid_function = func;
123 * clear_ftrace_function - reset the ftrace function
125 * This NULLs the ftrace function and in essence stops
126 * tracing. There may be lag
128 void clear_ftrace_function(void)
130 ftrace_trace_function = ftrace_stub;
131 __ftrace_trace_function = ftrace_stub;
132 ftrace_pid_function = ftrace_stub;
135 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
137 * For those archs that do not test ftrace_trace_stop in their
138 * mcount call site, we need to do it from C.
140 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
142 if (function_trace_stop)
145 __ftrace_trace_function(ip, parent_ip);
149 static int __register_ftrace_function(struct ftrace_ops *ops)
151 ops->next = ftrace_list;
153 * We are entering ops into the ftrace_list but another
154 * CPU might be walking that list. We need to make sure
155 * the ops->next pointer is valid before another CPU sees
156 * the ops pointer included into the ftrace_list.
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;
270 struct ftrace_profile_page {
271 struct ftrace_profile_page *next;
273 struct ftrace_profile records[];
276 struct ftrace_profile_stat {
278 struct hlist_head *hash;
279 struct ftrace_profile_page *pages;
280 struct ftrace_profile_page *start;
281 struct tracer_stat stat;
284 #define PROFILE_RECORDS_SIZE \
285 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
287 #define PROFILES_PER_PAGE \
288 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
290 static int ftrace_profile_bits __read_mostly;
291 static int ftrace_profile_enabled __read_mostly;
293 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
294 static DEFINE_MUTEX(ftrace_profile_lock);
296 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
298 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
301 function_stat_next(void *v, int idx)
303 struct ftrace_profile *rec = v;
304 struct ftrace_profile_page *pg;
306 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
312 if ((void *)rec >= (void *)&pg->records[pg->index]) {
316 rec = &pg->records[0];
324 static void *function_stat_start(struct tracer_stat *trace)
326 struct ftrace_profile_stat *stat =
327 container_of(trace, struct ftrace_profile_stat, stat);
329 if (!stat || !stat->start)
332 return function_stat_next(&stat->start->records[0], 0);
335 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
336 /* function graph compares on total time */
337 static int function_stat_cmp(void *p1, void *p2)
339 struct ftrace_profile *a = p1;
340 struct ftrace_profile *b = p2;
342 if (a->time < b->time)
344 if (a->time > b->time)
350 /* not function graph compares against hits */
351 static int function_stat_cmp(void *p1, void *p2)
353 struct ftrace_profile *a = p1;
354 struct ftrace_profile *b = p2;
356 if (a->counter < b->counter)
358 if (a->counter > b->counter)
365 static int function_stat_headers(struct seq_file *m)
367 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
368 seq_printf(m, " Function "
373 seq_printf(m, " Function Hit\n"
379 static int function_stat_show(struct seq_file *m, void *v)
381 struct ftrace_profile *rec = v;
382 char str[KSYM_SYMBOL_LEN];
383 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
384 static DEFINE_MUTEX(mutex);
385 static struct trace_seq s;
386 unsigned long long avg;
389 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
390 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
392 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
395 do_div(avg, rec->counter);
399 trace_print_graph_duration(rec->time, &s);
400 trace_seq_puts(&s, " ");
401 trace_print_graph_duration(avg, &s);
402 trace_print_seq(m, &s);
403 mutex_unlock(&mutex);
410 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
412 struct ftrace_profile_page *pg;
414 pg = stat->pages = stat->start;
417 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
422 memset(stat->hash, 0,
423 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
426 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
428 struct ftrace_profile_page *pg;
433 /* If we already allocated, do nothing */
437 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
441 #ifdef CONFIG_DYNAMIC_FTRACE
442 functions = ftrace_update_tot_cnt;
445 * We do not know the number of functions that exist because
446 * dynamic tracing is what counts them. With past experience
447 * we have around 20K functions. That should be more than enough.
448 * It is highly unlikely we will execute every function in
454 pg = stat->start = stat->pages;
456 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
458 for (i = 0; i < pages; i++) {
459 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
470 unsigned long tmp = (unsigned long)pg;
476 free_page((unsigned long)stat->pages);
483 static int ftrace_profile_init_cpu(int cpu)
485 struct ftrace_profile_stat *stat;
488 stat = &per_cpu(ftrace_profile_stats, cpu);
491 /* If the profile is already created, simply reset it */
492 ftrace_profile_reset(stat);
497 * We are profiling all functions, but usually only a few thousand
498 * functions are hit. We'll make a hash of 1024 items.
500 size = FTRACE_PROFILE_HASH_SIZE;
502 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
507 if (!ftrace_profile_bits) {
510 for (; size; size >>= 1)
511 ftrace_profile_bits++;
514 /* Preallocate the function profiling pages */
515 if (ftrace_profile_pages_init(stat) < 0) {
524 static int ftrace_profile_init(void)
529 for_each_online_cpu(cpu) {
530 ret = ftrace_profile_init_cpu(cpu);
538 /* interrupts must be disabled */
539 static struct ftrace_profile *
540 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
542 struct ftrace_profile *rec;
543 struct hlist_head *hhd;
544 struct hlist_node *n;
547 key = hash_long(ip, ftrace_profile_bits);
548 hhd = &stat->hash[key];
550 if (hlist_empty(hhd))
553 hlist_for_each_entry_rcu(rec, n, hhd, node) {
561 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
562 struct ftrace_profile *rec)
566 key = hash_long(rec->ip, ftrace_profile_bits);
567 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
571 * The memory is already allocated, this simply finds a new record to use.
573 static struct ftrace_profile *
574 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
576 struct ftrace_profile *rec = NULL;
578 /* prevent recursion (from NMIs) */
579 if (atomic_inc_return(&stat->disabled) != 1)
583 * Try to find the function again since an NMI
584 * could have added it
586 rec = ftrace_find_profiled_func(stat, ip);
590 if (stat->pages->index == PROFILES_PER_PAGE) {
591 if (!stat->pages->next)
593 stat->pages = stat->pages->next;
596 rec = &stat->pages->records[stat->pages->index++];
598 ftrace_add_profile(stat, rec);
601 atomic_dec(&stat->disabled);
607 function_profile_call(unsigned long ip, unsigned long parent_ip)
609 struct ftrace_profile_stat *stat;
610 struct ftrace_profile *rec;
613 if (!ftrace_profile_enabled)
616 local_irq_save(flags);
618 stat = &__get_cpu_var(ftrace_profile_stats);
619 if (!stat->hash || !ftrace_profile_enabled)
622 rec = ftrace_find_profiled_func(stat, ip);
624 rec = ftrace_profile_alloc(stat, ip);
631 local_irq_restore(flags);
634 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
635 static int profile_graph_entry(struct ftrace_graph_ent *trace)
637 function_profile_call(trace->func, 0);
641 static void profile_graph_return(struct ftrace_graph_ret *trace)
643 struct ftrace_profile_stat *stat;
644 unsigned long long calltime;
645 struct ftrace_profile *rec;
648 local_irq_save(flags);
649 stat = &__get_cpu_var(ftrace_profile_stats);
650 if (!stat->hash || !ftrace_profile_enabled)
653 calltime = trace->rettime - trace->calltime;
655 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
658 index = trace->depth;
660 /* Append this call time to the parent time to subtract */
662 current->ret_stack[index - 1].subtime += calltime;
664 if (current->ret_stack[index].subtime < calltime)
665 calltime -= current->ret_stack[index].subtime;
670 rec = ftrace_find_profiled_func(stat, trace->func);
672 rec->time += calltime;
675 local_irq_restore(flags);
678 static int register_ftrace_profiler(void)
680 return register_ftrace_graph(&profile_graph_return,
681 &profile_graph_entry);
684 static void unregister_ftrace_profiler(void)
686 unregister_ftrace_graph();
689 static struct ftrace_ops ftrace_profile_ops __read_mostly =
691 .func = function_profile_call,
694 static int register_ftrace_profiler(void)
696 return register_ftrace_function(&ftrace_profile_ops);
699 static void unregister_ftrace_profiler(void)
701 unregister_ftrace_function(&ftrace_profile_ops);
703 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
706 ftrace_profile_write(struct file *filp, const char __user *ubuf,
707 size_t cnt, loff_t *ppos)
710 char buf[64]; /* big enough to hold a number */
713 if (cnt >= sizeof(buf))
716 if (copy_from_user(&buf, ubuf, cnt))
721 ret = strict_strtoul(buf, 10, &val);
727 mutex_lock(&ftrace_profile_lock);
728 if (ftrace_profile_enabled ^ val) {
730 ret = ftrace_profile_init();
736 ret = register_ftrace_profiler();
741 ftrace_profile_enabled = 1;
743 ftrace_profile_enabled = 0;
745 * unregister_ftrace_profiler calls stop_machine
746 * so this acts like an synchronize_sched.
748 unregister_ftrace_profiler();
752 mutex_unlock(&ftrace_profile_lock);
760 ftrace_profile_read(struct file *filp, char __user *ubuf,
761 size_t cnt, loff_t *ppos)
763 char buf[64]; /* big enough to hold a number */
766 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
767 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
770 static const struct file_operations ftrace_profile_fops = {
771 .open = tracing_open_generic,
772 .read = ftrace_profile_read,
773 .write = ftrace_profile_write,
776 /* used to initialize the real stat files */
777 static struct tracer_stat function_stats __initdata = {
779 .stat_start = function_stat_start,
780 .stat_next = function_stat_next,
781 .stat_cmp = function_stat_cmp,
782 .stat_headers = function_stat_headers,
783 .stat_show = function_stat_show
786 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
788 struct ftrace_profile_stat *stat;
789 struct dentry *entry;
794 for_each_possible_cpu(cpu) {
795 stat = &per_cpu(ftrace_profile_stats, cpu);
797 /* allocate enough for function name + cpu number */
798 name = kmalloc(32, GFP_KERNEL);
801 * The files created are permanent, if something happens
802 * we still do not free memory.
805 "Could not allocate stat file for cpu %d\n",
809 stat->stat = function_stats;
810 snprintf(name, 32, "function%d", cpu);
811 stat->stat.name = name;
812 ret = register_stat_tracer(&stat->stat);
815 "Could not register function stat for cpu %d\n",
822 entry = debugfs_create_file("function_profile_enabled", 0644,
823 d_tracer, NULL, &ftrace_profile_fops);
825 pr_warning("Could not create debugfs "
826 "'function_profile_enabled' entry\n");
829 #else /* CONFIG_FUNCTION_PROFILER */
830 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
833 #endif /* CONFIG_FUNCTION_PROFILER */
835 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
837 #ifdef CONFIG_DYNAMIC_FTRACE
839 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
840 # error Dynamic ftrace depends on MCOUNT_RECORD
843 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
845 struct ftrace_func_probe {
846 struct hlist_node node;
847 struct ftrace_probe_ops *ops;
855 FTRACE_ENABLE_CALLS = (1 << 0),
856 FTRACE_DISABLE_CALLS = (1 << 1),
857 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
858 FTRACE_ENABLE_MCOUNT = (1 << 3),
859 FTRACE_DISABLE_MCOUNT = (1 << 4),
860 FTRACE_START_FUNC_RET = (1 << 5),
861 FTRACE_STOP_FUNC_RET = (1 << 6),
864 static int ftrace_filtered;
866 static struct dyn_ftrace *ftrace_new_addrs;
868 static DEFINE_MUTEX(ftrace_regex_lock);
871 struct ftrace_page *next;
873 struct dyn_ftrace records[];
876 #define ENTRIES_PER_PAGE \
877 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
879 /* estimate from running different kernels */
880 #define NR_TO_INIT 10000
882 static struct ftrace_page *ftrace_pages_start;
883 static struct ftrace_page *ftrace_pages;
885 static struct dyn_ftrace *ftrace_free_records;
888 * This is a double for. Do not use 'break' to break out of the loop,
889 * you must use a goto.
891 #define do_for_each_ftrace_rec(pg, rec) \
892 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
894 for (_____i = 0; _____i < pg->index; _____i++) { \
895 rec = &pg->records[_____i];
897 #define while_for_each_ftrace_rec() \
901 #ifdef CONFIG_KPROBES
903 static int frozen_record_count;
905 static inline void freeze_record(struct dyn_ftrace *rec)
907 if (!(rec->flags & FTRACE_FL_FROZEN)) {
908 rec->flags |= FTRACE_FL_FROZEN;
909 frozen_record_count++;
913 static inline void unfreeze_record(struct dyn_ftrace *rec)
915 if (rec->flags & FTRACE_FL_FROZEN) {
916 rec->flags &= ~FTRACE_FL_FROZEN;
917 frozen_record_count--;
921 static inline int record_frozen(struct dyn_ftrace *rec)
923 return rec->flags & FTRACE_FL_FROZEN;
926 # define freeze_record(rec) ({ 0; })
927 # define unfreeze_record(rec) ({ 0; })
928 # define record_frozen(rec) ({ 0; })
929 #endif /* CONFIG_KPROBES */
931 static void ftrace_free_rec(struct dyn_ftrace *rec)
933 rec->freelist = ftrace_free_records;
934 ftrace_free_records = rec;
935 rec->flags |= FTRACE_FL_FREE;
938 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
940 struct dyn_ftrace *rec;
942 /* First check for freed records */
943 if (ftrace_free_records) {
944 rec = ftrace_free_records;
946 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
947 FTRACE_WARN_ON_ONCE(1);
948 ftrace_free_records = NULL;
952 ftrace_free_records = rec->freelist;
953 memset(rec, 0, sizeof(*rec));
957 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
958 if (!ftrace_pages->next) {
959 /* allocate another page */
961 (void *)get_zeroed_page(GFP_KERNEL);
962 if (!ftrace_pages->next)
965 ftrace_pages = ftrace_pages->next;
968 return &ftrace_pages->records[ftrace_pages->index++];
971 static struct dyn_ftrace *
972 ftrace_record_ip(unsigned long ip)
974 struct dyn_ftrace *rec;
979 rec = ftrace_alloc_dyn_node(ip);
984 rec->newlist = ftrace_new_addrs;
985 ftrace_new_addrs = rec;
990 static void print_ip_ins(const char *fmt, unsigned char *p)
994 printk(KERN_CONT "%s", fmt);
996 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
997 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1000 static void ftrace_bug(int failed, unsigned long ip)
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on modifying ");
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace failed to modify ");
1012 print_ip_ins(" actual: ", (unsigned char *)ip);
1013 printk(KERN_CONT "\n");
1016 FTRACE_WARN_ON_ONCE(1);
1017 pr_info("ftrace faulted on writing ");
1021 FTRACE_WARN_ON_ONCE(1);
1022 pr_info("ftrace faulted on unknown error ");
1029 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1031 unsigned long ftrace_addr;
1032 unsigned long flag = 0UL;
1034 ftrace_addr = (unsigned long)FTRACE_ADDR;
1037 * If this record is not to be traced or we want to disable it,
1040 * If we want to enable it and filtering is off, then enable it.
1042 * If we want to enable it and filtering is on, enable it only if
1045 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1046 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1047 flag = FTRACE_FL_ENABLED;
1050 /* If the state of this record hasn't changed, then do nothing */
1051 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1055 rec->flags |= FTRACE_FL_ENABLED;
1056 return ftrace_make_call(rec, ftrace_addr);
1059 rec->flags &= ~FTRACE_FL_ENABLED;
1060 return ftrace_make_nop(NULL, rec, ftrace_addr);
1063 static void ftrace_replace_code(int enable)
1065 struct dyn_ftrace *rec;
1066 struct ftrace_page *pg;
1069 do_for_each_ftrace_rec(pg, rec) {
1071 * Skip over free records, records that have
1072 * failed and not converted.
1074 if (rec->flags & FTRACE_FL_FREE ||
1075 rec->flags & FTRACE_FL_FAILED ||
1076 !(rec->flags & FTRACE_FL_CONVERTED))
1079 /* ignore updates to this record's mcount site */
1080 if (get_kprobe((void *)rec->ip)) {
1084 unfreeze_record(rec);
1087 failed = __ftrace_replace_code(rec, enable);
1089 rec->flags |= FTRACE_FL_FAILED;
1090 ftrace_bug(failed, rec->ip);
1091 /* Stop processing */
1094 } while_for_each_ftrace_rec();
1098 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1105 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1107 ftrace_bug(ret, ip);
1108 rec->flags |= FTRACE_FL_FAILED;
1115 * archs can override this function if they must do something
1116 * before the modifying code is performed.
1118 int __weak ftrace_arch_code_modify_prepare(void)
1124 * archs can override this function if they must do something
1125 * after the modifying code is performed.
1127 int __weak ftrace_arch_code_modify_post_process(void)
1132 static int __ftrace_modify_code(void *data)
1134 int *command = data;
1136 if (*command & FTRACE_ENABLE_CALLS)
1137 ftrace_replace_code(1);
1138 else if (*command & FTRACE_DISABLE_CALLS)
1139 ftrace_replace_code(0);
1141 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1142 ftrace_update_ftrace_func(ftrace_trace_function);
1144 if (*command & FTRACE_START_FUNC_RET)
1145 ftrace_enable_ftrace_graph_caller();
1146 else if (*command & FTRACE_STOP_FUNC_RET)
1147 ftrace_disable_ftrace_graph_caller();
1152 static void ftrace_run_update_code(int command)
1156 ret = ftrace_arch_code_modify_prepare();
1157 FTRACE_WARN_ON(ret);
1161 stop_machine(__ftrace_modify_code, &command, NULL);
1163 ret = ftrace_arch_code_modify_post_process();
1164 FTRACE_WARN_ON(ret);
1167 static ftrace_func_t saved_ftrace_func;
1168 static int ftrace_start_up;
1170 static void ftrace_startup_enable(int command)
1172 if (saved_ftrace_func != ftrace_trace_function) {
1173 saved_ftrace_func = ftrace_trace_function;
1174 command |= FTRACE_UPDATE_TRACE_FUNC;
1177 if (!command || !ftrace_enabled)
1180 ftrace_run_update_code(command);
1183 static void ftrace_startup(int command)
1185 if (unlikely(ftrace_disabled))
1189 command |= FTRACE_ENABLE_CALLS;
1191 ftrace_startup_enable(command);
1194 static void ftrace_shutdown(int command)
1196 if (unlikely(ftrace_disabled))
1201 * Just warn in case of unbalance, no need to kill ftrace, it's not
1202 * critical but the ftrace_call callers may be never nopped again after
1203 * further ftrace uses.
1205 WARN_ON_ONCE(ftrace_start_up < 0);
1207 if (!ftrace_start_up)
1208 command |= FTRACE_DISABLE_CALLS;
1210 if (saved_ftrace_func != ftrace_trace_function) {
1211 saved_ftrace_func = ftrace_trace_function;
1212 command |= FTRACE_UPDATE_TRACE_FUNC;
1215 if (!command || !ftrace_enabled)
1218 ftrace_run_update_code(command);
1221 static void ftrace_startup_sysctl(void)
1223 int command = FTRACE_ENABLE_MCOUNT;
1225 if (unlikely(ftrace_disabled))
1228 /* Force update next time */
1229 saved_ftrace_func = NULL;
1230 /* ftrace_start_up is true if we want ftrace running */
1231 if (ftrace_start_up)
1232 command |= FTRACE_ENABLE_CALLS;
1234 ftrace_run_update_code(command);
1237 static void ftrace_shutdown_sysctl(void)
1239 int command = FTRACE_DISABLE_MCOUNT;
1241 if (unlikely(ftrace_disabled))
1244 /* ftrace_start_up is true if ftrace is running */
1245 if (ftrace_start_up)
1246 command |= FTRACE_DISABLE_CALLS;
1248 ftrace_run_update_code(command);
1251 static cycle_t ftrace_update_time;
1252 static unsigned long ftrace_update_cnt;
1253 unsigned long ftrace_update_tot_cnt;
1255 static int ftrace_update_code(struct module *mod)
1257 struct dyn_ftrace *p;
1258 cycle_t start, stop;
1260 start = ftrace_now(raw_smp_processor_id());
1261 ftrace_update_cnt = 0;
1263 while (ftrace_new_addrs) {
1265 /* If something went wrong, bail without enabling anything */
1266 if (unlikely(ftrace_disabled))
1269 p = ftrace_new_addrs;
1270 ftrace_new_addrs = p->newlist;
1273 /* convert record (i.e, patch mcount-call with NOP) */
1274 if (ftrace_code_disable(mod, p)) {
1275 p->flags |= FTRACE_FL_CONVERTED;
1276 ftrace_update_cnt++;
1281 stop = ftrace_now(raw_smp_processor_id());
1282 ftrace_update_time = stop - start;
1283 ftrace_update_tot_cnt += ftrace_update_cnt;
1288 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1290 struct ftrace_page *pg;
1294 /* allocate a few pages */
1295 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1296 if (!ftrace_pages_start)
1300 * Allocate a few more pages.
1302 * TODO: have some parser search vmlinux before
1303 * final linking to find all calls to ftrace.
1305 * a) know how many pages to allocate.
1307 * b) set up the table then.
1309 * The dynamic code is still necessary for
1313 pg = ftrace_pages = ftrace_pages_start;
1315 cnt = num_to_init / ENTRIES_PER_PAGE;
1316 pr_info("ftrace: allocating %ld entries in %d pages\n",
1317 num_to_init, cnt + 1);
1319 for (i = 0; i < cnt; i++) {
1320 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1322 /* If we fail, we'll try later anyway */
1333 FTRACE_ITER_FILTER = (1 << 0),
1334 FTRACE_ITER_NOTRACE = (1 << 1),
1335 FTRACE_ITER_FAILURES = (1 << 2),
1336 FTRACE_ITER_PRINTALL = (1 << 3),
1337 FTRACE_ITER_HASH = (1 << 4),
1340 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1342 struct ftrace_iterator {
1343 struct ftrace_page *pg;
1347 struct trace_parser parser;
1351 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1353 struct ftrace_iterator *iter = m->private;
1354 struct hlist_node *hnd = v;
1355 struct hlist_head *hhd;
1357 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1362 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1365 hhd = &ftrace_func_hash[iter->hidx];
1367 if (hlist_empty(hhd)) {
1386 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1388 struct ftrace_iterator *iter = m->private;
1392 if (!(iter->flags & FTRACE_ITER_HASH))
1395 iter->flags |= FTRACE_ITER_HASH;
1398 for (l = 0; l <= *pos; ) {
1399 p = t_hash_next(m, p, &l);
1406 static int t_hash_show(struct seq_file *m, void *v)
1408 struct ftrace_func_probe *rec;
1409 struct hlist_node *hnd = v;
1411 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1413 if (rec->ops->print)
1414 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1416 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1419 seq_printf(m, ":%p", rec->data);
1426 t_next(struct seq_file *m, void *v, loff_t *pos)
1428 struct ftrace_iterator *iter = m->private;
1429 struct dyn_ftrace *rec = NULL;
1431 if (iter->flags & FTRACE_ITER_HASH)
1432 return t_hash_next(m, v, pos);
1436 if (iter->flags & FTRACE_ITER_PRINTALL)
1440 if (iter->idx >= iter->pg->index) {
1441 if (iter->pg->next) {
1442 iter->pg = iter->pg->next;
1447 rec = &iter->pg->records[iter->idx++];
1448 if ((rec->flags & FTRACE_FL_FREE) ||
1450 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1451 (rec->flags & FTRACE_FL_FAILED)) ||
1453 ((iter->flags & FTRACE_ITER_FAILURES) &&
1454 !(rec->flags & FTRACE_FL_FAILED)) ||
1456 ((iter->flags & FTRACE_ITER_FILTER) &&
1457 !(rec->flags & FTRACE_FL_FILTER)) ||
1459 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1460 !(rec->flags & FTRACE_FL_NOTRACE))) {
1469 static void *t_start(struct seq_file *m, loff_t *pos)
1471 struct ftrace_iterator *iter = m->private;
1475 mutex_lock(&ftrace_lock);
1477 * For set_ftrace_filter reading, if we have the filter
1478 * off, we can short cut and just print out that all
1479 * functions are enabled.
1481 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1483 return t_hash_start(m, pos);
1484 iter->flags |= FTRACE_ITER_PRINTALL;
1488 if (iter->flags & FTRACE_ITER_HASH)
1489 return t_hash_start(m, pos);
1491 iter->pg = ftrace_pages_start;
1493 for (l = 0; l <= *pos; ) {
1494 p = t_next(m, p, &l);
1499 if (!p && iter->flags & FTRACE_ITER_FILTER)
1500 return t_hash_start(m, pos);
1505 static void t_stop(struct seq_file *m, void *p)
1507 mutex_unlock(&ftrace_lock);
1510 static int t_show(struct seq_file *m, void *v)
1512 struct ftrace_iterator *iter = m->private;
1513 struct dyn_ftrace *rec = v;
1515 if (iter->flags & FTRACE_ITER_HASH)
1516 return t_hash_show(m, v);
1518 if (iter->flags & FTRACE_ITER_PRINTALL) {
1519 seq_printf(m, "#### all functions enabled ####\n");
1526 seq_printf(m, "%ps\n", (void *)rec->ip);
1531 static const struct seq_operations show_ftrace_seq_ops = {
1539 ftrace_avail_open(struct inode *inode, struct file *file)
1541 struct ftrace_iterator *iter;
1544 if (unlikely(ftrace_disabled))
1547 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1551 iter->pg = ftrace_pages_start;
1553 ret = seq_open(file, &show_ftrace_seq_ops);
1555 struct seq_file *m = file->private_data;
1566 ftrace_failures_open(struct inode *inode, struct file *file)
1570 struct ftrace_iterator *iter;
1572 ret = ftrace_avail_open(inode, file);
1574 m = (struct seq_file *)file->private_data;
1575 iter = (struct ftrace_iterator *)m->private;
1576 iter->flags = FTRACE_ITER_FAILURES;
1583 static void ftrace_filter_reset(int enable)
1585 struct ftrace_page *pg;
1586 struct dyn_ftrace *rec;
1587 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1589 mutex_lock(&ftrace_lock);
1591 ftrace_filtered = 0;
1592 do_for_each_ftrace_rec(pg, rec) {
1593 if (rec->flags & FTRACE_FL_FAILED)
1595 rec->flags &= ~type;
1596 } while_for_each_ftrace_rec();
1597 mutex_unlock(&ftrace_lock);
1601 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1603 struct ftrace_iterator *iter;
1606 if (unlikely(ftrace_disabled))
1609 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1613 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1618 mutex_lock(&ftrace_regex_lock);
1619 if ((file->f_mode & FMODE_WRITE) &&
1620 (file->f_flags & O_TRUNC))
1621 ftrace_filter_reset(enable);
1623 if (file->f_mode & FMODE_READ) {
1624 iter->pg = ftrace_pages_start;
1625 iter->flags = enable ? FTRACE_ITER_FILTER :
1626 FTRACE_ITER_NOTRACE;
1628 ret = seq_open(file, &show_ftrace_seq_ops);
1630 struct seq_file *m = file->private_data;
1633 trace_parser_put(&iter->parser);
1637 file->private_data = iter;
1638 mutex_unlock(&ftrace_regex_lock);
1644 ftrace_filter_open(struct inode *inode, struct file *file)
1646 return ftrace_regex_open(inode, file, 1);
1650 ftrace_notrace_open(struct inode *inode, struct file *file)
1652 return ftrace_regex_open(inode, file, 0);
1656 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1660 if (file->f_mode & FMODE_READ)
1661 ret = seq_lseek(file, offset, origin);
1663 file->f_pos = ret = 1;
1668 static int ftrace_match(char *str, char *regex, int len, int type)
1675 if (strcmp(str, regex) == 0)
1678 case MATCH_FRONT_ONLY:
1679 if (strncmp(str, regex, len) == 0)
1682 case MATCH_MIDDLE_ONLY:
1683 if (strstr(str, regex))
1686 case MATCH_END_ONLY:
1687 ptr = strstr(str, regex);
1688 if (ptr && (ptr[len] == 0))
1697 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1699 char str[KSYM_SYMBOL_LEN];
1701 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1702 return ftrace_match(str, regex, len, type);
1705 static void ftrace_match_records(char *buff, int len, int enable)
1707 unsigned int search_len;
1708 struct ftrace_page *pg;
1709 struct dyn_ftrace *rec;
1715 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1716 type = filter_parse_regex(buff, len, &search, ¬);
1718 search_len = strlen(search);
1720 mutex_lock(&ftrace_lock);
1721 do_for_each_ftrace_rec(pg, rec) {
1723 if (rec->flags & FTRACE_FL_FAILED)
1726 if (ftrace_match_record(rec, search, search_len, type)) {
1728 rec->flags &= ~flag;
1733 * Only enable filtering if we have a function that
1736 if (enable && (rec->flags & FTRACE_FL_FILTER))
1737 ftrace_filtered = 1;
1738 } while_for_each_ftrace_rec();
1739 mutex_unlock(&ftrace_lock);
1743 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1744 char *regex, int len, int type)
1746 char str[KSYM_SYMBOL_LEN];
1749 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1751 if (!modname || strcmp(modname, mod))
1754 /* blank search means to match all funcs in the mod */
1756 return ftrace_match(str, regex, len, type);
1761 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1763 unsigned search_len = 0;
1764 struct ftrace_page *pg;
1765 struct dyn_ftrace *rec;
1766 int type = MATCH_FULL;
1767 char *search = buff;
1771 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1773 /* blank or '*' mean the same */
1774 if (strcmp(buff, "*") == 0)
1777 /* handle the case of 'dont filter this module' */
1778 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1784 type = filter_parse_regex(buff, strlen(buff), &search, ¬);
1785 search_len = strlen(search);
1788 mutex_lock(&ftrace_lock);
1789 do_for_each_ftrace_rec(pg, rec) {
1791 if (rec->flags & FTRACE_FL_FAILED)
1794 if (ftrace_match_module_record(rec, mod,
1795 search, search_len, type)) {
1797 rec->flags &= ~flag;
1801 if (enable && (rec->flags & FTRACE_FL_FILTER))
1802 ftrace_filtered = 1;
1804 } while_for_each_ftrace_rec();
1805 mutex_unlock(&ftrace_lock);
1809 * We register the module command as a template to show others how
1810 * to register the a command as well.
1814 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1819 * cmd == 'mod' because we only registered this func
1820 * for the 'mod' ftrace_func_command.
1821 * But if you register one func with multiple commands,
1822 * you can tell which command was used by the cmd
1826 /* we must have a module name */
1830 mod = strsep(¶m, ":");
1834 ftrace_match_module_records(func, mod, enable);
1838 static struct ftrace_func_command ftrace_mod_cmd = {
1840 .func = ftrace_mod_callback,
1843 static int __init ftrace_mod_cmd_init(void)
1845 return register_ftrace_command(&ftrace_mod_cmd);
1847 device_initcall(ftrace_mod_cmd_init);
1850 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1852 struct ftrace_func_probe *entry;
1853 struct hlist_head *hhd;
1854 struct hlist_node *n;
1858 key = hash_long(ip, FTRACE_HASH_BITS);
1860 hhd = &ftrace_func_hash[key];
1862 if (hlist_empty(hhd))
1866 * Disable preemption for these calls to prevent a RCU grace
1867 * period. This syncs the hash iteration and freeing of items
1868 * on the hash. rcu_read_lock is too dangerous here.
1870 resched = ftrace_preempt_disable();
1871 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1872 if (entry->ip == ip)
1873 entry->ops->func(ip, parent_ip, &entry->data);
1875 ftrace_preempt_enable(resched);
1878 static struct ftrace_ops trace_probe_ops __read_mostly =
1880 .func = function_trace_probe_call,
1883 static int ftrace_probe_registered;
1885 static void __enable_ftrace_function_probe(void)
1889 if (ftrace_probe_registered)
1892 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1893 struct hlist_head *hhd = &ftrace_func_hash[i];
1897 /* Nothing registered? */
1898 if (i == FTRACE_FUNC_HASHSIZE)
1901 __register_ftrace_function(&trace_probe_ops);
1903 ftrace_probe_registered = 1;
1906 static void __disable_ftrace_function_probe(void)
1910 if (!ftrace_probe_registered)
1913 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1914 struct hlist_head *hhd = &ftrace_func_hash[i];
1919 /* no more funcs left */
1920 __unregister_ftrace_function(&trace_probe_ops);
1922 ftrace_probe_registered = 0;
1926 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1928 struct ftrace_func_probe *entry =
1929 container_of(rhp, struct ftrace_func_probe, rcu);
1931 if (entry->ops->free)
1932 entry->ops->free(&entry->data);
1938 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1941 struct ftrace_func_probe *entry;
1942 struct ftrace_page *pg;
1943 struct dyn_ftrace *rec;
1949 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
1950 len = strlen(search);
1952 /* we do not support '!' for function probes */
1956 mutex_lock(&ftrace_lock);
1957 do_for_each_ftrace_rec(pg, rec) {
1959 if (rec->flags & FTRACE_FL_FAILED)
1962 if (!ftrace_match_record(rec, search, len, type))
1965 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1967 /* If we did not process any, then return error */
1978 * The caller might want to do something special
1979 * for each function we find. We call the callback
1980 * to give the caller an opportunity to do so.
1982 if (ops->callback) {
1983 if (ops->callback(rec->ip, &entry->data) < 0) {
1984 /* caller does not like this func */
1991 entry->ip = rec->ip;
1993 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1994 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1996 } while_for_each_ftrace_rec();
1997 __enable_ftrace_function_probe();
2000 mutex_unlock(&ftrace_lock);
2006 PROBE_TEST_FUNC = 1,
2011 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2012 void *data, int flags)
2014 struct ftrace_func_probe *entry;
2015 struct hlist_node *n, *tmp;
2016 char str[KSYM_SYMBOL_LEN];
2017 int type = MATCH_FULL;
2021 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2026 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2027 len = strlen(search);
2029 /* we do not support '!' for function probes */
2034 mutex_lock(&ftrace_lock);
2035 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2036 struct hlist_head *hhd = &ftrace_func_hash[i];
2038 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2040 /* break up if statements for readability */
2041 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2044 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2047 /* do this last, since it is the most expensive */
2049 kallsyms_lookup(entry->ip, NULL, NULL,
2051 if (!ftrace_match(str, glob, len, type))
2055 hlist_del(&entry->node);
2056 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2059 __disable_ftrace_function_probe();
2060 mutex_unlock(&ftrace_lock);
2064 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2067 __unregister_ftrace_function_probe(glob, ops, data,
2068 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2072 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2074 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2077 void unregister_ftrace_function_probe_all(char *glob)
2079 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2082 static LIST_HEAD(ftrace_commands);
2083 static DEFINE_MUTEX(ftrace_cmd_mutex);
2085 int register_ftrace_command(struct ftrace_func_command *cmd)
2087 struct ftrace_func_command *p;
2090 mutex_lock(&ftrace_cmd_mutex);
2091 list_for_each_entry(p, &ftrace_commands, list) {
2092 if (strcmp(cmd->name, p->name) == 0) {
2097 list_add(&cmd->list, &ftrace_commands);
2099 mutex_unlock(&ftrace_cmd_mutex);
2104 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2106 struct ftrace_func_command *p, *n;
2109 mutex_lock(&ftrace_cmd_mutex);
2110 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2111 if (strcmp(cmd->name, p->name) == 0) {
2113 list_del_init(&p->list);
2118 mutex_unlock(&ftrace_cmd_mutex);
2123 static int ftrace_process_regex(char *buff, int len, int enable)
2125 char *func, *command, *next = buff;
2126 struct ftrace_func_command *p;
2129 func = strsep(&next, ":");
2132 ftrace_match_records(func, len, enable);
2138 command = strsep(&next, ":");
2140 mutex_lock(&ftrace_cmd_mutex);
2141 list_for_each_entry(p, &ftrace_commands, list) {
2142 if (strcmp(p->name, command) == 0) {
2143 ret = p->func(func, command, next, enable);
2148 mutex_unlock(&ftrace_cmd_mutex);
2154 ftrace_regex_write(struct file *file, const char __user *ubuf,
2155 size_t cnt, loff_t *ppos, int enable)
2157 struct ftrace_iterator *iter;
2158 struct trace_parser *parser;
2164 mutex_lock(&ftrace_regex_lock);
2166 if (file->f_mode & FMODE_READ) {
2167 struct seq_file *m = file->private_data;
2170 iter = file->private_data;
2172 parser = &iter->parser;
2173 read = trace_get_user(parser, ubuf, cnt, ppos);
2175 if (read >= 0 && trace_parser_loaded(parser) &&
2176 !trace_parser_cont(parser)) {
2177 ret = ftrace_process_regex(parser->buffer,
2178 parser->idx, enable);
2182 trace_parser_clear(parser);
2187 mutex_unlock(&ftrace_regex_lock);
2193 ftrace_filter_write(struct file *file, const char __user *ubuf,
2194 size_t cnt, loff_t *ppos)
2196 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2200 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2201 size_t cnt, loff_t *ppos)
2203 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2207 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2209 if (unlikely(ftrace_disabled))
2212 mutex_lock(&ftrace_regex_lock);
2214 ftrace_filter_reset(enable);
2216 ftrace_match_records(buf, len, enable);
2217 mutex_unlock(&ftrace_regex_lock);
2221 * ftrace_set_filter - set a function to filter on in ftrace
2222 * @buf - the string that holds the function filter text.
2223 * @len - the length of the string.
2224 * @reset - non zero to reset all filters before applying this filter.
2226 * Filters denote which functions should be enabled when tracing is enabled.
2227 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2229 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2231 ftrace_set_regex(buf, len, reset, 1);
2235 * ftrace_set_notrace - set a function to not trace in ftrace
2236 * @buf - the string that holds the function notrace text.
2237 * @len - the length of the string.
2238 * @reset - non zero to reset all filters before applying this filter.
2240 * Notrace Filters denote which functions should not be enabled when tracing
2241 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2244 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2246 ftrace_set_regex(buf, len, reset, 0);
2250 * command line interface to allow users to set filters on boot up.
2252 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2253 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2254 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2255 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2257 static int __init set_ftrace_notrace(char *str)
2259 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2262 __setup("ftrace_notrace=", set_ftrace_notrace);
2264 static int __init set_ftrace_filter(char *str)
2266 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2269 __setup("ftrace_filter=", set_ftrace_filter);
2271 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2272 static int __init set_graph_function(char *str)
2274 strncpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2277 __setup("ftrace_graph_filter=", set_graph_function);
2279 static void __init set_ftrace_early_graph(char *buf)
2285 func = strsep(&buf, ",");
2286 /* we allow only one expression at a time */
2287 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2290 printk(KERN_DEBUG "ftrace: function %s not "
2291 "traceable\n", func);
2294 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2296 static void __init set_ftrace_early_filter(char *buf, int enable)
2301 func = strsep(&buf, ",");
2302 ftrace_set_regex(func, strlen(func), 0, enable);
2306 static void __init set_ftrace_early_filters(void)
2308 if (ftrace_filter_buf[0])
2309 set_ftrace_early_filter(ftrace_filter_buf, 1);
2310 if (ftrace_notrace_buf[0])
2311 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2312 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2313 if (ftrace_graph_buf[0])
2314 set_ftrace_early_graph(ftrace_graph_buf);
2315 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2319 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2321 struct seq_file *m = (struct seq_file *)file->private_data;
2322 struct ftrace_iterator *iter;
2323 struct trace_parser *parser;
2325 mutex_lock(&ftrace_regex_lock);
2326 if (file->f_mode & FMODE_READ) {
2329 seq_release(inode, file);
2331 iter = file->private_data;
2333 parser = &iter->parser;
2334 if (trace_parser_loaded(parser)) {
2335 parser->buffer[parser->idx] = 0;
2336 ftrace_match_records(parser->buffer, parser->idx, enable);
2339 mutex_lock(&ftrace_lock);
2340 if (ftrace_start_up && ftrace_enabled)
2341 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2342 mutex_unlock(&ftrace_lock);
2344 trace_parser_put(parser);
2347 mutex_unlock(&ftrace_regex_lock);
2352 ftrace_filter_release(struct inode *inode, struct file *file)
2354 return ftrace_regex_release(inode, file, 1);
2358 ftrace_notrace_release(struct inode *inode, struct file *file)
2360 return ftrace_regex_release(inode, file, 0);
2363 static const struct file_operations ftrace_avail_fops = {
2364 .open = ftrace_avail_open,
2366 .llseek = seq_lseek,
2367 .release = seq_release_private,
2370 static const struct file_operations ftrace_failures_fops = {
2371 .open = ftrace_failures_open,
2373 .llseek = seq_lseek,
2374 .release = seq_release_private,
2377 static const struct file_operations ftrace_filter_fops = {
2378 .open = ftrace_filter_open,
2380 .write = ftrace_filter_write,
2381 .llseek = ftrace_regex_lseek,
2382 .release = ftrace_filter_release,
2385 static const struct file_operations ftrace_notrace_fops = {
2386 .open = ftrace_notrace_open,
2388 .write = ftrace_notrace_write,
2389 .llseek = ftrace_regex_lseek,
2390 .release = ftrace_notrace_release,
2393 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2395 static DEFINE_MUTEX(graph_lock);
2397 int ftrace_graph_count;
2398 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2401 __g_next(struct seq_file *m, loff_t *pos)
2403 if (*pos >= ftrace_graph_count)
2405 return &ftrace_graph_funcs[*pos];
2409 g_next(struct seq_file *m, void *v, loff_t *pos)
2412 return __g_next(m, pos);
2415 static void *g_start(struct seq_file *m, loff_t *pos)
2417 mutex_lock(&graph_lock);
2419 /* Nothing, tell g_show to print all functions are enabled */
2420 if (!ftrace_graph_count && !*pos)
2423 return __g_next(m, pos);
2426 static void g_stop(struct seq_file *m, void *p)
2428 mutex_unlock(&graph_lock);
2431 static int g_show(struct seq_file *m, void *v)
2433 unsigned long *ptr = v;
2438 if (ptr == (unsigned long *)1) {
2439 seq_printf(m, "#### all functions enabled ####\n");
2443 seq_printf(m, "%ps\n", (void *)*ptr);
2448 static const struct seq_operations ftrace_graph_seq_ops = {
2456 ftrace_graph_open(struct inode *inode, struct file *file)
2460 if (unlikely(ftrace_disabled))
2463 mutex_lock(&graph_lock);
2464 if ((file->f_mode & FMODE_WRITE) &&
2465 (file->f_flags & O_TRUNC)) {
2466 ftrace_graph_count = 0;
2467 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2469 mutex_unlock(&graph_lock);
2471 if (file->f_mode & FMODE_READ)
2472 ret = seq_open(file, &ftrace_graph_seq_ops);
2478 ftrace_graph_release(struct inode *inode, struct file *file)
2480 if (file->f_mode & FMODE_READ)
2481 seq_release(inode, file);
2486 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2488 struct dyn_ftrace *rec;
2489 struct ftrace_page *pg;
2497 if (ftrace_disabled)
2501 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
2505 search_len = strlen(search);
2507 mutex_lock(&ftrace_lock);
2508 do_for_each_ftrace_rec(pg, rec) {
2510 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2513 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2516 if (ftrace_match_record(rec, search, search_len, type)) {
2517 /* ensure it is not already in the array */
2519 for (i = 0; i < *idx; i++)
2520 if (array[i] == rec->ip) {
2525 array[(*idx)++] = rec->ip;
2529 } while_for_each_ftrace_rec();
2531 mutex_unlock(&ftrace_lock);
2533 return found ? 0 : -EINVAL;
2537 ftrace_graph_write(struct file *file, const char __user *ubuf,
2538 size_t cnt, loff_t *ppos)
2540 struct trace_parser parser;
2543 if (!cnt || cnt < 0)
2546 mutex_lock(&graph_lock);
2548 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2553 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2558 read = trace_get_user(&parser, ubuf, cnt, ppos);
2560 if (read >= 0 && trace_parser_loaded((&parser))) {
2561 parser.buffer[parser.idx] = 0;
2563 /* we allow only one expression at a time */
2564 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2573 trace_parser_put(&parser);
2575 mutex_unlock(&graph_lock);
2580 static const struct file_operations ftrace_graph_fops = {
2581 .open = ftrace_graph_open,
2583 .write = ftrace_graph_write,
2584 .release = ftrace_graph_release,
2586 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2588 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2591 trace_create_file("available_filter_functions", 0444,
2592 d_tracer, NULL, &ftrace_avail_fops);
2594 trace_create_file("failures", 0444,
2595 d_tracer, NULL, &ftrace_failures_fops);
2597 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2598 NULL, &ftrace_filter_fops);
2600 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2601 NULL, &ftrace_notrace_fops);
2603 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2604 trace_create_file("set_graph_function", 0444, d_tracer,
2606 &ftrace_graph_fops);
2607 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2612 static int ftrace_convert_nops(struct module *mod,
2613 unsigned long *start,
2618 unsigned long flags;
2620 mutex_lock(&ftrace_lock);
2623 addr = ftrace_call_adjust(*p++);
2625 * Some architecture linkers will pad between
2626 * the different mcount_loc sections of different
2627 * object files to satisfy alignments.
2628 * Skip any NULL pointers.
2632 ftrace_record_ip(addr);
2635 /* disable interrupts to prevent kstop machine */
2636 local_irq_save(flags);
2637 ftrace_update_code(mod);
2638 local_irq_restore(flags);
2639 mutex_unlock(&ftrace_lock);
2644 #ifdef CONFIG_MODULES
2645 void ftrace_release_mod(struct module *mod)
2647 struct dyn_ftrace *rec;
2648 struct ftrace_page *pg;
2650 if (ftrace_disabled)
2653 mutex_lock(&ftrace_lock);
2654 do_for_each_ftrace_rec(pg, rec) {
2655 if (within_module_core(rec->ip, mod)) {
2657 * rec->ip is changed in ftrace_free_rec()
2658 * It should not between s and e if record was freed.
2660 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2661 ftrace_free_rec(rec);
2663 } while_for_each_ftrace_rec();
2664 mutex_unlock(&ftrace_lock);
2667 static void ftrace_init_module(struct module *mod,
2668 unsigned long *start, unsigned long *end)
2670 if (ftrace_disabled || start == end)
2672 ftrace_convert_nops(mod, start, end);
2675 static int ftrace_module_notify(struct notifier_block *self,
2676 unsigned long val, void *data)
2678 struct module *mod = data;
2681 case MODULE_STATE_COMING:
2682 ftrace_init_module(mod, mod->ftrace_callsites,
2683 mod->ftrace_callsites +
2684 mod->num_ftrace_callsites);
2686 case MODULE_STATE_GOING:
2687 ftrace_release_mod(mod);
2694 static int ftrace_module_notify(struct notifier_block *self,
2695 unsigned long val, void *data)
2699 #endif /* CONFIG_MODULES */
2701 struct notifier_block ftrace_module_nb = {
2702 .notifier_call = ftrace_module_notify,
2706 extern unsigned long __start_mcount_loc[];
2707 extern unsigned long __stop_mcount_loc[];
2709 void __init ftrace_init(void)
2711 unsigned long count, addr, flags;
2714 /* Keep the ftrace pointer to the stub */
2715 addr = (unsigned long)ftrace_stub;
2717 local_irq_save(flags);
2718 ftrace_dyn_arch_init(&addr);
2719 local_irq_restore(flags);
2721 /* ftrace_dyn_arch_init places the return code in addr */
2725 count = __stop_mcount_loc - __start_mcount_loc;
2727 ret = ftrace_dyn_table_alloc(count);
2731 last_ftrace_enabled = ftrace_enabled = 1;
2733 ret = ftrace_convert_nops(NULL,
2737 ret = register_module_notifier(&ftrace_module_nb);
2739 pr_warning("Failed to register trace ftrace module notifier\n");
2741 set_ftrace_early_filters();
2745 ftrace_disabled = 1;
2750 static int __init ftrace_nodyn_init(void)
2755 device_initcall(ftrace_nodyn_init);
2757 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2758 static inline void ftrace_startup_enable(int command) { }
2759 /* Keep as macros so we do not need to define the commands */
2760 # define ftrace_startup(command) do { } while (0)
2761 # define ftrace_shutdown(command) do { } while (0)
2762 # define ftrace_startup_sysctl() do { } while (0)
2763 # define ftrace_shutdown_sysctl() do { } while (0)
2764 #endif /* CONFIG_DYNAMIC_FTRACE */
2766 static void clear_ftrace_swapper(void)
2768 struct task_struct *p;
2772 for_each_online_cpu(cpu) {
2774 clear_tsk_trace_trace(p);
2779 static void set_ftrace_swapper(void)
2781 struct task_struct *p;
2785 for_each_online_cpu(cpu) {
2787 set_tsk_trace_trace(p);
2792 static void clear_ftrace_pid(struct pid *pid)
2794 struct task_struct *p;
2797 do_each_pid_task(pid, PIDTYPE_PID, p) {
2798 clear_tsk_trace_trace(p);
2799 } while_each_pid_task(pid, PIDTYPE_PID, p);
2805 static void set_ftrace_pid(struct pid *pid)
2807 struct task_struct *p;
2810 do_each_pid_task(pid, PIDTYPE_PID, p) {
2811 set_tsk_trace_trace(p);
2812 } while_each_pid_task(pid, PIDTYPE_PID, p);
2816 static void clear_ftrace_pid_task(struct pid *pid)
2818 if (pid == ftrace_swapper_pid)
2819 clear_ftrace_swapper();
2821 clear_ftrace_pid(pid);
2824 static void set_ftrace_pid_task(struct pid *pid)
2826 if (pid == ftrace_swapper_pid)
2827 set_ftrace_swapper();
2829 set_ftrace_pid(pid);
2832 static int ftrace_pid_add(int p)
2835 struct ftrace_pid *fpid;
2838 mutex_lock(&ftrace_lock);
2841 pid = ftrace_swapper_pid;
2843 pid = find_get_pid(p);
2850 list_for_each_entry(fpid, &ftrace_pids, list)
2851 if (fpid->pid == pid)
2856 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2860 list_add(&fpid->list, &ftrace_pids);
2863 set_ftrace_pid_task(pid);
2865 ftrace_update_pid_func();
2866 ftrace_startup_enable(0);
2868 mutex_unlock(&ftrace_lock);
2872 if (pid != ftrace_swapper_pid)
2876 mutex_unlock(&ftrace_lock);
2880 static void ftrace_pid_reset(void)
2882 struct ftrace_pid *fpid, *safe;
2884 mutex_lock(&ftrace_lock);
2885 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2886 struct pid *pid = fpid->pid;
2888 clear_ftrace_pid_task(pid);
2890 list_del(&fpid->list);
2894 ftrace_update_pid_func();
2895 ftrace_startup_enable(0);
2897 mutex_unlock(&ftrace_lock);
2900 static void *fpid_start(struct seq_file *m, loff_t *pos)
2902 mutex_lock(&ftrace_lock);
2904 if (list_empty(&ftrace_pids) && (!*pos))
2907 return seq_list_start(&ftrace_pids, *pos);
2910 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2915 return seq_list_next(v, &ftrace_pids, pos);
2918 static void fpid_stop(struct seq_file *m, void *p)
2920 mutex_unlock(&ftrace_lock);
2923 static int fpid_show(struct seq_file *m, void *v)
2925 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2927 if (v == (void *)1) {
2928 seq_printf(m, "no pid\n");
2932 if (fpid->pid == ftrace_swapper_pid)
2933 seq_printf(m, "swapper tasks\n");
2935 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2940 static const struct seq_operations ftrace_pid_sops = {
2941 .start = fpid_start,
2948 ftrace_pid_open(struct inode *inode, struct file *file)
2952 if ((file->f_mode & FMODE_WRITE) &&
2953 (file->f_flags & O_TRUNC))
2956 if (file->f_mode & FMODE_READ)
2957 ret = seq_open(file, &ftrace_pid_sops);
2963 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2964 size_t cnt, loff_t *ppos)
2970 if (cnt >= sizeof(buf))
2973 if (copy_from_user(&buf, ubuf, cnt))
2979 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
2980 * to clean the filter quietly.
2983 if (strlen(buf) == 0)
2986 ret = strict_strtol(buf, 10, &val);
2990 ret = ftrace_pid_add(val);
2992 return ret ? ret : cnt;
2996 ftrace_pid_release(struct inode *inode, struct file *file)
2998 if (file->f_mode & FMODE_READ)
2999 seq_release(inode, file);
3004 static const struct file_operations ftrace_pid_fops = {
3005 .open = ftrace_pid_open,
3006 .write = ftrace_pid_write,
3008 .llseek = seq_lseek,
3009 .release = ftrace_pid_release,
3012 static __init int ftrace_init_debugfs(void)
3014 struct dentry *d_tracer;
3016 d_tracer = tracing_init_dentry();
3020 ftrace_init_dyn_debugfs(d_tracer);
3022 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3023 NULL, &ftrace_pid_fops);
3025 ftrace_profile_debugfs(d_tracer);
3029 fs_initcall(ftrace_init_debugfs);
3032 * ftrace_kill - kill ftrace
3034 * This function should be used by panic code. It stops ftrace
3035 * but in a not so nice way. If you need to simply kill ftrace
3036 * from a non-atomic section, use ftrace_kill.
3038 void ftrace_kill(void)
3040 ftrace_disabled = 1;
3042 clear_ftrace_function();
3046 * register_ftrace_function - register a function for profiling
3047 * @ops - ops structure that holds the function for profiling.
3049 * Register a function to be called by all functions in the
3052 * Note: @ops->func and all the functions it calls must be labeled
3053 * with "notrace", otherwise it will go into a
3056 int register_ftrace_function(struct ftrace_ops *ops)
3060 if (unlikely(ftrace_disabled))
3063 mutex_lock(&ftrace_lock);
3065 ret = __register_ftrace_function(ops);
3068 mutex_unlock(&ftrace_lock);
3073 * unregister_ftrace_function - unregister a function for profiling.
3074 * @ops - ops structure that holds the function to unregister
3076 * Unregister a function that was added to be called by ftrace profiling.
3078 int unregister_ftrace_function(struct ftrace_ops *ops)
3082 mutex_lock(&ftrace_lock);
3083 ret = __unregister_ftrace_function(ops);
3085 mutex_unlock(&ftrace_lock);
3091 ftrace_enable_sysctl(struct ctl_table *table, int write,
3092 void __user *buffer, size_t *lenp,
3097 if (unlikely(ftrace_disabled))
3100 mutex_lock(&ftrace_lock);
3102 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3104 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3107 last_ftrace_enabled = !!ftrace_enabled;
3109 if (ftrace_enabled) {
3111 ftrace_startup_sysctl();
3113 /* we are starting ftrace again */
3114 if (ftrace_list != &ftrace_list_end) {
3115 if (ftrace_list->next == &ftrace_list_end)
3116 ftrace_trace_function = ftrace_list->func;
3118 ftrace_trace_function = ftrace_list_func;
3122 /* stopping ftrace calls (just send to ftrace_stub) */
3123 ftrace_trace_function = ftrace_stub;
3125 ftrace_shutdown_sysctl();
3129 mutex_unlock(&ftrace_lock);
3133 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3135 static int ftrace_graph_active;
3136 static struct notifier_block ftrace_suspend_notifier;
3138 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3143 /* The callbacks that hook a function */
3144 trace_func_graph_ret_t ftrace_graph_return =
3145 (trace_func_graph_ret_t)ftrace_stub;
3146 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3148 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3149 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3153 unsigned long flags;
3154 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3155 struct task_struct *g, *t;
3157 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3158 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3159 * sizeof(struct ftrace_ret_stack),
3161 if (!ret_stack_list[i]) {
3169 read_lock_irqsave(&tasklist_lock, flags);
3170 do_each_thread(g, t) {
3176 if (t->ret_stack == NULL) {
3177 atomic_set(&t->tracing_graph_pause, 0);
3178 atomic_set(&t->trace_overrun, 0);
3179 t->curr_ret_stack = -1;
3180 /* Make sure the tasks see the -1 first: */
3182 t->ret_stack = ret_stack_list[start++];
3184 } while_each_thread(g, t);
3187 read_unlock_irqrestore(&tasklist_lock, flags);
3189 for (i = start; i < end; i++)
3190 kfree(ret_stack_list[i]);
3195 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3196 struct task_struct *next)
3198 unsigned long long timestamp;
3202 * Does the user want to count the time a function was asleep.
3203 * If so, do not update the time stamps.
3205 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3208 timestamp = trace_clock_local();
3210 prev->ftrace_timestamp = timestamp;
3212 /* only process tasks that we timestamped */
3213 if (!next->ftrace_timestamp)
3217 * Update all the counters in next to make up for the
3218 * time next was sleeping.
3220 timestamp -= next->ftrace_timestamp;
3222 for (index = next->curr_ret_stack; index >= 0; index--)
3223 next->ret_stack[index].calltime += timestamp;
3226 /* Allocate a return stack for each task */
3227 static int start_graph_tracing(void)
3229 struct ftrace_ret_stack **ret_stack_list;
3232 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3233 sizeof(struct ftrace_ret_stack *),
3236 if (!ret_stack_list)
3239 /* The cpu_boot init_task->ret_stack will never be freed */
3240 for_each_online_cpu(cpu) {
3241 if (!idle_task(cpu)->ret_stack)
3242 ftrace_graph_init_task(idle_task(cpu));
3246 ret = alloc_retstack_tasklist(ret_stack_list);
3247 } while (ret == -EAGAIN);
3250 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3252 pr_info("ftrace_graph: Couldn't activate tracepoint"
3253 " probe to kernel_sched_switch\n");
3256 kfree(ret_stack_list);
3261 * Hibernation protection.
3262 * The state of the current task is too much unstable during
3263 * suspend/restore to disk. We want to protect against that.
3266 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3270 case PM_HIBERNATION_PREPARE:
3271 pause_graph_tracing();
3274 case PM_POST_HIBERNATION:
3275 unpause_graph_tracing();
3281 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3282 trace_func_graph_ent_t entryfunc)
3286 mutex_lock(&ftrace_lock);
3288 /* we currently allow only one tracer registered at a time */
3289 if (ftrace_graph_active) {
3294 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3295 register_pm_notifier(&ftrace_suspend_notifier);
3297 ftrace_graph_active++;
3298 ret = start_graph_tracing();
3300 ftrace_graph_active--;
3304 ftrace_graph_return = retfunc;
3305 ftrace_graph_entry = entryfunc;
3307 ftrace_startup(FTRACE_START_FUNC_RET);
3310 mutex_unlock(&ftrace_lock);
3314 void unregister_ftrace_graph(void)
3316 mutex_lock(&ftrace_lock);
3318 if (unlikely(!ftrace_graph_active))
3321 ftrace_graph_active--;
3322 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3323 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3324 ftrace_graph_entry = ftrace_graph_entry_stub;
3325 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3326 unregister_pm_notifier(&ftrace_suspend_notifier);
3329 mutex_unlock(&ftrace_lock);
3332 /* Allocate a return stack for newly created task */
3333 void ftrace_graph_init_task(struct task_struct *t)
3335 /* Make sure we do not use the parent ret_stack */
3336 t->ret_stack = NULL;
3338 if (ftrace_graph_active) {
3339 struct ftrace_ret_stack *ret_stack;
3341 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3342 * sizeof(struct ftrace_ret_stack),
3346 t->curr_ret_stack = -1;
3347 atomic_set(&t->tracing_graph_pause, 0);
3348 atomic_set(&t->trace_overrun, 0);
3349 t->ftrace_timestamp = 0;
3350 /* make curr_ret_stack visable before we add the ret_stack */
3352 t->ret_stack = ret_stack;
3356 void ftrace_graph_exit_task(struct task_struct *t)
3358 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3360 t->ret_stack = NULL;
3361 /* NULL must become visible to IRQs before we free it: */
3367 void ftrace_graph_stop(void)