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/module.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/slab.h>
29 #include <linux/ctype.h>
30 #include <linux/list.h>
31 #include <linux/hash.h>
32 #include <linux/rcupdate.h>
34 #include <trace/events/sched.h>
36 #include <asm/setup.h>
38 #include "trace_output.h"
39 #include "trace_stat.h"
41 #define FTRACE_WARN_ON(cond) \
49 #define FTRACE_WARN_ON_ONCE(cond) \
52 if (WARN_ON_ONCE(___r)) \
57 /* hash bits for specific function selection */
58 #define FTRACE_HASH_BITS 7
59 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
60 #define FTRACE_HASH_DEFAULT_BITS 10
61 #define FTRACE_HASH_MAX_BITS 12
63 /* ftrace_enabled is a method to turn ftrace on or off */
64 int ftrace_enabled __read_mostly;
65 static int last_ftrace_enabled;
67 /* Quick disabling of function tracer. */
68 int function_trace_stop;
70 /* List for set_ftrace_pid's pids. */
71 LIST_HEAD(ftrace_pids);
73 struct list_head list;
78 * ftrace_disabled is set when an anomaly is discovered.
79 * ftrace_disabled is much stronger than ftrace_enabled.
81 static int ftrace_disabled __read_mostly;
83 static DEFINE_MUTEX(ftrace_lock);
85 static struct ftrace_ops ftrace_list_end __read_mostly = {
89 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
90 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
91 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
92 static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
93 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
94 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
95 static struct ftrace_ops global_ops;
98 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
101 * Traverse the ftrace_global_list, invoking all entries. The reason that we
102 * can use rcu_dereference_raw() is that elements removed from this list
103 * are simply leaked, so there is no need to interact with a grace-period
104 * mechanism. The rcu_dereference_raw() calls are needed to handle
105 * concurrent insertions into the ftrace_global_list.
107 * Silly Alpha and silly pointer-speculation compiler optimizations!
109 static void ftrace_global_list_func(unsigned long ip,
110 unsigned long parent_ip)
112 struct ftrace_ops *op;
114 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
117 trace_recursion_set(TRACE_GLOBAL_BIT);
118 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
119 while (op != &ftrace_list_end) {
120 op->func(ip, parent_ip);
121 op = rcu_dereference_raw(op->next); /*see above*/
123 trace_recursion_clear(TRACE_GLOBAL_BIT);
126 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
128 if (!test_tsk_trace_trace(current))
131 ftrace_pid_function(ip, parent_ip);
134 static void set_ftrace_pid_function(ftrace_func_t func)
136 /* do not set ftrace_pid_function to itself! */
137 if (func != ftrace_pid_func)
138 ftrace_pid_function = func;
142 * clear_ftrace_function - reset the ftrace function
144 * This NULLs the ftrace function and in essence stops
145 * tracing. There may be lag
147 void clear_ftrace_function(void)
149 ftrace_trace_function = ftrace_stub;
150 __ftrace_trace_function = ftrace_stub;
151 __ftrace_trace_function_delay = ftrace_stub;
152 ftrace_pid_function = ftrace_stub;
155 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
157 * For those archs that do not test ftrace_trace_stop in their
158 * mcount call site, we need to do it from C.
160 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
162 if (function_trace_stop)
165 __ftrace_trace_function(ip, parent_ip);
169 static void update_global_ops(void)
174 * If there's only one function registered, then call that
175 * function directly. Otherwise, we need to iterate over the
176 * registered callers.
178 if (ftrace_global_list == &ftrace_list_end ||
179 ftrace_global_list->next == &ftrace_list_end)
180 func = ftrace_global_list->func;
182 func = ftrace_global_list_func;
184 /* If we filter on pids, update to use the pid function */
185 if (!list_empty(&ftrace_pids)) {
186 set_ftrace_pid_function(func);
187 func = ftrace_pid_func;
190 global_ops.func = func;
193 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
194 static void update_function_graph_func(void);
196 static inline void update_function_graph_func(void) { }
199 static void update_ftrace_function(void)
206 * If we are at the end of the list and this ops is
207 * not dynamic, then have the mcount trampoline call
208 * the function directly
210 if (ftrace_ops_list == &ftrace_list_end ||
211 (ftrace_ops_list->next == &ftrace_list_end &&
212 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
213 func = ftrace_ops_list->func;
215 func = ftrace_ops_list_func;
217 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
218 ftrace_trace_function = func;
220 #ifdef CONFIG_DYNAMIC_FTRACE
221 /* do not update till all functions have been modified */
222 __ftrace_trace_function_delay = func;
224 __ftrace_trace_function = func;
226 ftrace_trace_function = ftrace_test_stop_func;
230 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
234 * We are entering ops into the list but another
235 * CPU might be walking that list. We need to make sure
236 * the ops->next pointer is valid before another CPU sees
237 * the ops pointer included into the list.
239 rcu_assign_pointer(*list, ops);
242 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
244 struct ftrace_ops **p;
246 update_function_graph_func();
249 * If we are removing the last function, then simply point
250 * to the ftrace_stub.
252 if (*list == ops && ops->next == &ftrace_list_end) {
253 *list = &ftrace_list_end;
257 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
268 static int __register_ftrace_function(struct ftrace_ops *ops)
270 if (FTRACE_WARN_ON(ops == &global_ops))
273 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
276 if (!core_kernel_data((unsigned long)ops))
277 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
279 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
280 int first = ftrace_global_list == &ftrace_list_end;
281 add_ftrace_ops(&ftrace_global_list, ops);
282 ops->flags |= FTRACE_OPS_FL_ENABLED;
284 add_ftrace_ops(&ftrace_ops_list, &global_ops);
286 add_ftrace_ops(&ftrace_ops_list, ops);
289 update_ftrace_function();
294 static void ftrace_sync(struct work_struct *work)
297 * This function is just a stub to implement a hard force
298 * of synchronize_sched(). This requires synchronizing
299 * tasks even in userspace and idle.
301 * Yes, function tracing is rude.
305 static int __unregister_ftrace_function(struct ftrace_ops *ops)
309 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
312 if (FTRACE_WARN_ON(ops == &global_ops))
315 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
316 ret = remove_ftrace_ops(&ftrace_global_list, ops);
317 if (!ret && ftrace_global_list == &ftrace_list_end)
318 ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
320 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
322 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
328 update_ftrace_function();
333 static void ftrace_update_pid_func(void)
335 /* Only do something if we are tracing something */
336 if (ftrace_trace_function == ftrace_stub)
339 update_ftrace_function();
342 #ifdef CONFIG_FUNCTION_PROFILER
343 struct ftrace_profile {
344 struct hlist_node node;
346 unsigned long counter;
347 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
348 unsigned long long time;
349 unsigned long long time_squared;
353 struct ftrace_profile_page {
354 struct ftrace_profile_page *next;
356 struct ftrace_profile records[];
359 struct ftrace_profile_stat {
361 struct hlist_head *hash;
362 struct ftrace_profile_page *pages;
363 struct ftrace_profile_page *start;
364 struct tracer_stat stat;
367 #define PROFILE_RECORDS_SIZE \
368 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
370 #define PROFILES_PER_PAGE \
371 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
373 static int ftrace_profile_bits __read_mostly;
374 static int ftrace_profile_enabled __read_mostly;
376 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
377 static DEFINE_MUTEX(ftrace_profile_lock);
379 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
381 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
384 function_stat_next(void *v, int idx)
386 struct ftrace_profile *rec = v;
387 struct ftrace_profile_page *pg;
389 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
395 if ((void *)rec >= (void *)&pg->records[pg->index]) {
399 rec = &pg->records[0];
407 static void *function_stat_start(struct tracer_stat *trace)
409 struct ftrace_profile_stat *stat =
410 container_of(trace, struct ftrace_profile_stat, stat);
412 if (!stat || !stat->start)
415 return function_stat_next(&stat->start->records[0], 0);
418 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
419 /* function graph compares on total time */
420 static int function_stat_cmp(void *p1, void *p2)
422 struct ftrace_profile *a = p1;
423 struct ftrace_profile *b = p2;
425 if (a->time < b->time)
427 if (a->time > b->time)
433 /* not function graph compares against hits */
434 static int function_stat_cmp(void *p1, void *p2)
436 struct ftrace_profile *a = p1;
437 struct ftrace_profile *b = p2;
439 if (a->counter < b->counter)
441 if (a->counter > b->counter)
448 static int function_stat_headers(struct seq_file *m)
450 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
451 seq_printf(m, " Function "
454 "--- ---- --- ---\n");
456 seq_printf(m, " Function Hit\n"
462 static int function_stat_show(struct seq_file *m, void *v)
464 struct ftrace_profile *rec = v;
465 char str[KSYM_SYMBOL_LEN];
467 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
468 static struct trace_seq s;
469 unsigned long long avg;
470 unsigned long long stddev;
472 mutex_lock(&ftrace_profile_lock);
474 /* we raced with function_profile_reset() */
475 if (unlikely(rec->counter == 0)) {
480 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
481 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
483 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
486 do_div(avg, rec->counter);
488 /* Sample standard deviation (s^2) */
489 if (rec->counter <= 1)
492 stddev = rec->time_squared - rec->counter * avg * avg;
494 * Divide only 1000 for ns^2 -> us^2 conversion.
495 * trace_print_graph_duration will divide 1000 again.
497 do_div(stddev, (rec->counter - 1) * 1000);
501 trace_print_graph_duration(rec->time, &s);
502 trace_seq_puts(&s, " ");
503 trace_print_graph_duration(avg, &s);
504 trace_seq_puts(&s, " ");
505 trace_print_graph_duration(stddev, &s);
506 trace_print_seq(m, &s);
510 mutex_unlock(&ftrace_profile_lock);
515 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
517 struct ftrace_profile_page *pg;
519 pg = stat->pages = stat->start;
522 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
527 memset(stat->hash, 0,
528 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
531 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
533 struct ftrace_profile_page *pg;
538 /* If we already allocated, do nothing */
542 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
546 #ifdef CONFIG_DYNAMIC_FTRACE
547 functions = ftrace_update_tot_cnt;
550 * We do not know the number of functions that exist because
551 * dynamic tracing is what counts them. With past experience
552 * we have around 20K functions. That should be more than enough.
553 * It is highly unlikely we will execute every function in
559 pg = stat->start = stat->pages;
561 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
563 for (i = 1; i < pages; i++) {
564 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
575 unsigned long tmp = (unsigned long)pg;
587 static int ftrace_profile_init_cpu(int cpu)
589 struct ftrace_profile_stat *stat;
592 stat = &per_cpu(ftrace_profile_stats, cpu);
595 /* If the profile is already created, simply reset it */
596 ftrace_profile_reset(stat);
601 * We are profiling all functions, but usually only a few thousand
602 * functions are hit. We'll make a hash of 1024 items.
604 size = FTRACE_PROFILE_HASH_SIZE;
606 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
611 if (!ftrace_profile_bits) {
614 for (; size; size >>= 1)
615 ftrace_profile_bits++;
618 /* Preallocate the function profiling pages */
619 if (ftrace_profile_pages_init(stat) < 0) {
628 static int ftrace_profile_init(void)
633 for_each_possible_cpu(cpu) {
634 ret = ftrace_profile_init_cpu(cpu);
642 /* interrupts must be disabled */
643 static struct ftrace_profile *
644 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
646 struct ftrace_profile *rec;
647 struct hlist_head *hhd;
648 struct hlist_node *n;
651 key = hash_long(ip, ftrace_profile_bits);
652 hhd = &stat->hash[key];
654 if (hlist_empty(hhd))
657 hlist_for_each_entry_rcu(rec, n, hhd, node) {
665 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
666 struct ftrace_profile *rec)
670 key = hash_long(rec->ip, ftrace_profile_bits);
671 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
675 * The memory is already allocated, this simply finds a new record to use.
677 static struct ftrace_profile *
678 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
680 struct ftrace_profile *rec = NULL;
682 /* prevent recursion (from NMIs) */
683 if (atomic_inc_return(&stat->disabled) != 1)
687 * Try to find the function again since an NMI
688 * could have added it
690 rec = ftrace_find_profiled_func(stat, ip);
694 if (stat->pages->index == PROFILES_PER_PAGE) {
695 if (!stat->pages->next)
697 stat->pages = stat->pages->next;
700 rec = &stat->pages->records[stat->pages->index++];
702 ftrace_add_profile(stat, rec);
705 atomic_dec(&stat->disabled);
711 function_profile_call(unsigned long ip, unsigned long parent_ip)
713 struct ftrace_profile_stat *stat;
714 struct ftrace_profile *rec;
717 if (!ftrace_profile_enabled)
720 local_irq_save(flags);
722 stat = &__get_cpu_var(ftrace_profile_stats);
723 if (!stat->hash || !ftrace_profile_enabled)
726 rec = ftrace_find_profiled_func(stat, ip);
728 rec = ftrace_profile_alloc(stat, ip);
735 local_irq_restore(flags);
738 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
739 static int profile_graph_entry(struct ftrace_graph_ent *trace)
741 function_profile_call(trace->func, 0);
745 static void profile_graph_return(struct ftrace_graph_ret *trace)
747 struct ftrace_profile_stat *stat;
748 unsigned long long calltime;
749 struct ftrace_profile *rec;
752 local_irq_save(flags);
753 stat = &__get_cpu_var(ftrace_profile_stats);
754 if (!stat->hash || !ftrace_profile_enabled)
757 /* If the calltime was zero'd ignore it */
758 if (!trace->calltime)
761 calltime = trace->rettime - trace->calltime;
763 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
766 index = trace->depth;
768 /* Append this call time to the parent time to subtract */
770 current->ret_stack[index - 1].subtime += calltime;
772 if (current->ret_stack[index].subtime < calltime)
773 calltime -= current->ret_stack[index].subtime;
778 rec = ftrace_find_profiled_func(stat, trace->func);
780 rec->time += calltime;
781 rec->time_squared += calltime * calltime;
785 local_irq_restore(flags);
788 static int register_ftrace_profiler(void)
790 return register_ftrace_graph(&profile_graph_return,
791 &profile_graph_entry);
794 static void unregister_ftrace_profiler(void)
796 unregister_ftrace_graph();
799 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
800 .func = function_profile_call,
803 static int register_ftrace_profiler(void)
805 return register_ftrace_function(&ftrace_profile_ops);
808 static void unregister_ftrace_profiler(void)
810 unregister_ftrace_function(&ftrace_profile_ops);
812 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
815 ftrace_profile_write(struct file *filp, const char __user *ubuf,
816 size_t cnt, loff_t *ppos)
821 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
827 mutex_lock(&ftrace_profile_lock);
828 if (ftrace_profile_enabled ^ val) {
830 ret = ftrace_profile_init();
836 ret = register_ftrace_profiler();
841 ftrace_profile_enabled = 1;
843 ftrace_profile_enabled = 0;
845 * unregister_ftrace_profiler calls stop_machine
846 * so this acts like an synchronize_sched.
848 unregister_ftrace_profiler();
852 mutex_unlock(&ftrace_profile_lock);
860 ftrace_profile_read(struct file *filp, char __user *ubuf,
861 size_t cnt, loff_t *ppos)
863 char buf[64]; /* big enough to hold a number */
866 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
867 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
870 static const struct file_operations ftrace_profile_fops = {
871 .open = tracing_open_generic,
872 .read = ftrace_profile_read,
873 .write = ftrace_profile_write,
874 .llseek = default_llseek,
877 /* used to initialize the real stat files */
878 static struct tracer_stat function_stats __initdata = {
880 .stat_start = function_stat_start,
881 .stat_next = function_stat_next,
882 .stat_cmp = function_stat_cmp,
883 .stat_headers = function_stat_headers,
884 .stat_show = function_stat_show
887 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
889 struct ftrace_profile_stat *stat;
890 struct dentry *entry;
895 for_each_possible_cpu(cpu) {
896 stat = &per_cpu(ftrace_profile_stats, cpu);
898 /* allocate enough for function name + cpu number */
899 name = kmalloc(32, GFP_KERNEL);
902 * The files created are permanent, if something happens
903 * we still do not free memory.
906 "Could not allocate stat file for cpu %d\n",
910 stat->stat = function_stats;
911 snprintf(name, 32, "function%d", cpu);
912 stat->stat.name = name;
913 ret = register_stat_tracer(&stat->stat);
916 "Could not register function stat for cpu %d\n",
923 entry = debugfs_create_file("function_profile_enabled", 0644,
924 d_tracer, NULL, &ftrace_profile_fops);
926 pr_warning("Could not create debugfs "
927 "'function_profile_enabled' entry\n");
930 #else /* CONFIG_FUNCTION_PROFILER */
931 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
934 #endif /* CONFIG_FUNCTION_PROFILER */
936 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
939 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
943 if (file->f_mode & FMODE_READ)
944 ret = seq_lseek(file, offset, whence);
946 file->f_pos = ret = 1;
951 #ifdef CONFIG_DYNAMIC_FTRACE
953 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
954 # error Dynamic ftrace depends on MCOUNT_RECORD
957 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
959 struct ftrace_func_probe {
960 struct hlist_node node;
961 struct ftrace_probe_ops *ops;
969 FTRACE_UPDATE_CALLS = (1 << 0),
970 FTRACE_DISABLE_CALLS = (1 << 1),
971 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
972 FTRACE_START_FUNC_RET = (1 << 3),
973 FTRACE_STOP_FUNC_RET = (1 << 4),
975 struct ftrace_func_entry {
976 struct hlist_node hlist;
981 unsigned long size_bits;
982 struct hlist_head *buckets;
988 * We make these constant because no one should touch them,
989 * but they are used as the default "empty hash", to avoid allocating
990 * it all the time. These are in a read only section such that if
991 * anyone does try to modify it, it will cause an exception.
993 static const struct hlist_head empty_buckets[1];
994 static const struct ftrace_hash empty_hash = {
995 .buckets = (struct hlist_head *)empty_buckets,
997 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
999 static struct ftrace_ops global_ops = {
1000 .func = ftrace_stub,
1001 .notrace_hash = EMPTY_HASH,
1002 .filter_hash = EMPTY_HASH,
1005 static struct dyn_ftrace *ftrace_new_addrs;
1007 static DEFINE_MUTEX(ftrace_regex_lock);
1009 struct ftrace_page {
1010 struct ftrace_page *next;
1012 struct dyn_ftrace records[];
1015 #define ENTRIES_PER_PAGE \
1016 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
1018 /* estimate from running different kernels */
1019 #define NR_TO_INIT 10000
1021 static struct ftrace_page *ftrace_pages_start;
1022 static struct ftrace_page *ftrace_pages;
1024 static struct dyn_ftrace *ftrace_free_records;
1026 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1028 return !hash || !hash->count;
1031 static struct ftrace_func_entry *
1032 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1035 struct ftrace_func_entry *entry;
1036 struct hlist_head *hhd;
1037 struct hlist_node *n;
1039 if (ftrace_hash_empty(hash))
1042 if (hash->size_bits > 0)
1043 key = hash_long(ip, hash->size_bits);
1047 hhd = &hash->buckets[key];
1049 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1050 if (entry->ip == ip)
1056 static void __add_hash_entry(struct ftrace_hash *hash,
1057 struct ftrace_func_entry *entry)
1059 struct hlist_head *hhd;
1062 if (hash->size_bits)
1063 key = hash_long(entry->ip, hash->size_bits);
1067 hhd = &hash->buckets[key];
1068 hlist_add_head(&entry->hlist, hhd);
1072 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1074 struct ftrace_func_entry *entry;
1076 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1081 __add_hash_entry(hash, entry);
1087 free_hash_entry(struct ftrace_hash *hash,
1088 struct ftrace_func_entry *entry)
1090 hlist_del(&entry->hlist);
1096 remove_hash_entry(struct ftrace_hash *hash,
1097 struct ftrace_func_entry *entry)
1099 hlist_del(&entry->hlist);
1103 static void ftrace_hash_clear(struct ftrace_hash *hash)
1105 struct hlist_head *hhd;
1106 struct hlist_node *tp, *tn;
1107 struct ftrace_func_entry *entry;
1108 int size = 1 << hash->size_bits;
1114 for (i = 0; i < size; i++) {
1115 hhd = &hash->buckets[i];
1116 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1117 free_hash_entry(hash, entry);
1119 FTRACE_WARN_ON(hash->count);
1122 static void free_ftrace_hash(struct ftrace_hash *hash)
1124 if (!hash || hash == EMPTY_HASH)
1126 ftrace_hash_clear(hash);
1127 kfree(hash->buckets);
1131 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1133 struct ftrace_hash *hash;
1135 hash = container_of(rcu, struct ftrace_hash, rcu);
1136 free_ftrace_hash(hash);
1139 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1141 if (!hash || hash == EMPTY_HASH)
1143 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1146 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1148 struct ftrace_hash *hash;
1151 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1155 size = 1 << size_bits;
1156 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1158 if (!hash->buckets) {
1163 hash->size_bits = size_bits;
1168 static struct ftrace_hash *
1169 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1171 struct ftrace_func_entry *entry;
1172 struct ftrace_hash *new_hash;
1173 struct hlist_node *tp;
1178 new_hash = alloc_ftrace_hash(size_bits);
1183 if (ftrace_hash_empty(hash))
1186 size = 1 << hash->size_bits;
1187 for (i = 0; i < size; i++) {
1188 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1189 ret = add_hash_entry(new_hash, entry->ip);
1195 FTRACE_WARN_ON(new_hash->count != hash->count);
1200 free_ftrace_hash(new_hash);
1205 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1207 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1210 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1211 struct ftrace_hash **dst, struct ftrace_hash *src)
1213 struct ftrace_func_entry *entry;
1214 struct hlist_node *tp, *tn;
1215 struct hlist_head *hhd;
1216 struct ftrace_hash *old_hash;
1217 struct ftrace_hash *new_hash;
1219 int size = src->count;
1225 * Remove the current set, update the hash and add
1228 ftrace_hash_rec_disable(ops, enable);
1231 * If the new source is empty, just free dst and assign it
1235 free_ftrace_hash_rcu(*dst);
1236 rcu_assign_pointer(*dst, EMPTY_HASH);
1237 /* still need to update the function records */
1243 * Make the hash size about 1/2 the # found
1245 for (size /= 2; size; size >>= 1)
1248 /* Don't allocate too much */
1249 if (bits > FTRACE_HASH_MAX_BITS)
1250 bits = FTRACE_HASH_MAX_BITS;
1253 new_hash = alloc_ftrace_hash(bits);
1257 size = 1 << src->size_bits;
1258 for (i = 0; i < size; i++) {
1259 hhd = &src->buckets[i];
1260 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1262 key = hash_long(entry->ip, bits);
1265 remove_hash_entry(src, entry);
1266 __add_hash_entry(new_hash, entry);
1271 rcu_assign_pointer(*dst, new_hash);
1272 free_ftrace_hash_rcu(old_hash);
1277 * Enable regardless of ret:
1278 * On success, we enable the new hash.
1279 * On failure, we re-enable the original hash.
1281 ftrace_hash_rec_enable(ops, enable);
1287 * Test the hashes for this ops to see if we want to call
1288 * the ops->func or not.
1290 * It's a match if the ip is in the ops->filter_hash or
1291 * the filter_hash does not exist or is empty,
1293 * the ip is not in the ops->notrace_hash.
1295 * This needs to be called with preemption disabled as
1296 * the hashes are freed with call_rcu_sched().
1299 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1301 struct ftrace_hash *filter_hash;
1302 struct ftrace_hash *notrace_hash;
1305 filter_hash = rcu_dereference_raw(ops->filter_hash);
1306 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1308 if ((ftrace_hash_empty(filter_hash) ||
1309 ftrace_lookup_ip(filter_hash, ip)) &&
1310 (ftrace_hash_empty(notrace_hash) ||
1311 !ftrace_lookup_ip(notrace_hash, ip)))
1320 * This is a double for. Do not use 'break' to break out of the loop,
1321 * you must use a goto.
1323 #define do_for_each_ftrace_rec(pg, rec) \
1324 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1326 for (_____i = 0; _____i < pg->index; _____i++) { \
1327 rec = &pg->records[_____i];
1329 #define while_for_each_ftrace_rec() \
1333 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1337 struct ftrace_hash *hash;
1338 struct ftrace_hash *other_hash;
1339 struct ftrace_page *pg;
1340 struct dyn_ftrace *rec;
1344 /* Only update if the ops has been registered */
1345 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1349 * In the filter_hash case:
1350 * If the count is zero, we update all records.
1351 * Otherwise we just update the items in the hash.
1353 * In the notrace_hash case:
1354 * We enable the update in the hash.
1355 * As disabling notrace means enabling the tracing,
1356 * and enabling notrace means disabling, the inc variable
1360 hash = ops->filter_hash;
1361 other_hash = ops->notrace_hash;
1362 if (ftrace_hash_empty(hash))
1366 hash = ops->notrace_hash;
1367 other_hash = ops->filter_hash;
1369 * If the notrace hash has no items,
1370 * then there's nothing to do.
1372 if (ftrace_hash_empty(hash))
1376 do_for_each_ftrace_rec(pg, rec) {
1377 int in_other_hash = 0;
1383 * Only the filter_hash affects all records.
1384 * Update if the record is not in the notrace hash.
1386 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1389 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1390 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1395 if (filter_hash && in_hash && !in_other_hash)
1397 else if (!filter_hash && in_hash &&
1398 (in_other_hash || ftrace_hash_empty(other_hash)))
1406 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1409 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1414 /* Shortcut, if we handled all records, we are done. */
1415 if (!all && count == hash->count)
1417 } while_for_each_ftrace_rec();
1420 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1423 __ftrace_hash_rec_update(ops, filter_hash, 0);
1426 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1429 __ftrace_hash_rec_update(ops, filter_hash, 1);
1432 static void ftrace_free_rec(struct dyn_ftrace *rec)
1434 rec->freelist = ftrace_free_records;
1435 ftrace_free_records = rec;
1436 rec->flags |= FTRACE_FL_FREE;
1439 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1441 struct dyn_ftrace *rec;
1443 /* First check for freed records */
1444 if (ftrace_free_records) {
1445 rec = ftrace_free_records;
1447 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
1448 FTRACE_WARN_ON_ONCE(1);
1449 ftrace_free_records = NULL;
1453 ftrace_free_records = rec->freelist;
1454 memset(rec, 0, sizeof(*rec));
1458 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1459 if (!ftrace_pages->next) {
1460 /* allocate another page */
1461 ftrace_pages->next =
1462 (void *)get_zeroed_page(GFP_KERNEL);
1463 if (!ftrace_pages->next)
1466 ftrace_pages = ftrace_pages->next;
1469 return &ftrace_pages->records[ftrace_pages->index++];
1472 static struct dyn_ftrace *
1473 ftrace_record_ip(unsigned long ip)
1475 struct dyn_ftrace *rec;
1477 if (ftrace_disabled)
1480 rec = ftrace_alloc_dyn_node(ip);
1485 rec->newlist = ftrace_new_addrs;
1486 ftrace_new_addrs = rec;
1491 static void print_ip_ins(const char *fmt, unsigned char *p)
1495 printk(KERN_CONT "%s", fmt);
1497 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1498 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1501 static void ftrace_bug(int failed, unsigned long ip)
1505 FTRACE_WARN_ON_ONCE(1);
1506 pr_info("ftrace faulted on modifying ");
1510 FTRACE_WARN_ON_ONCE(1);
1511 pr_info("ftrace failed to modify ");
1513 print_ip_ins(" actual: ", (unsigned char *)ip);
1514 printk(KERN_CONT "\n");
1517 FTRACE_WARN_ON_ONCE(1);
1518 pr_info("ftrace faulted on writing ");
1522 FTRACE_WARN_ON_ONCE(1);
1523 pr_info("ftrace faulted on unknown error ");
1529 /* Return 1 if the address range is reserved for ftrace */
1530 int ftrace_text_reserved(void *start, void *end)
1532 struct dyn_ftrace *rec;
1533 struct ftrace_page *pg;
1535 do_for_each_ftrace_rec(pg, rec) {
1536 if (rec->ip <= (unsigned long)end &&
1537 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1539 } while_for_each_ftrace_rec();
1545 __ftrace_replace_code(struct dyn_ftrace *rec, int update)
1547 unsigned long ftrace_addr;
1548 unsigned long flag = 0UL;
1550 ftrace_addr = (unsigned long)FTRACE_ADDR;
1553 * If we are updating calls:
1555 * If the record has a ref count, then we need to enable it
1556 * because someone is using it.
1558 * Otherwise we make sure its disabled.
1560 * If we are disabling calls, then disable all records that
1563 if (update && (rec->flags & ~FTRACE_FL_MASK))
1564 flag = FTRACE_FL_ENABLED;
1566 /* If the state of this record hasn't changed, then do nothing */
1567 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1571 rec->flags |= FTRACE_FL_ENABLED;
1572 return ftrace_make_call(rec, ftrace_addr);
1575 rec->flags &= ~FTRACE_FL_ENABLED;
1576 return ftrace_make_nop(NULL, rec, ftrace_addr);
1579 static void ftrace_replace_code(int update)
1581 struct dyn_ftrace *rec;
1582 struct ftrace_page *pg;
1585 if (unlikely(ftrace_disabled))
1588 do_for_each_ftrace_rec(pg, rec) {
1589 /* Skip over free records */
1590 if (rec->flags & FTRACE_FL_FREE)
1593 failed = __ftrace_replace_code(rec, update);
1595 ftrace_bug(failed, rec->ip);
1596 /* Stop processing */
1599 } while_for_each_ftrace_rec();
1603 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1610 if (unlikely(ftrace_disabled))
1613 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1615 ftrace_bug(ret, ip);
1622 * archs can override this function if they must do something
1623 * before the modifying code is performed.
1625 int __weak ftrace_arch_code_modify_prepare(void)
1631 * archs can override this function if they must do something
1632 * after the modifying code is performed.
1634 int __weak ftrace_arch_code_modify_post_process(void)
1639 static int __ftrace_modify_code(void *data)
1641 int *command = data;
1644 * Do not call function tracer while we update the code.
1645 * We are in stop machine, no worrying about races.
1647 function_trace_stop++;
1649 if (*command & FTRACE_UPDATE_CALLS)
1650 ftrace_replace_code(1);
1651 else if (*command & FTRACE_DISABLE_CALLS)
1652 ftrace_replace_code(0);
1654 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1655 ftrace_update_ftrace_func(ftrace_trace_function);
1657 if (*command & FTRACE_START_FUNC_RET)
1658 ftrace_enable_ftrace_graph_caller();
1659 else if (*command & FTRACE_STOP_FUNC_RET)
1660 ftrace_disable_ftrace_graph_caller();
1662 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1664 * For archs that call ftrace_test_stop_func(), we must
1665 * wait till after we update all the function callers
1666 * before we update the callback. This keeps different
1667 * ops that record different functions from corrupting
1670 __ftrace_trace_function = __ftrace_trace_function_delay;
1672 function_trace_stop--;
1677 static void ftrace_run_update_code(int command)
1681 ret = ftrace_arch_code_modify_prepare();
1682 FTRACE_WARN_ON(ret);
1686 stop_machine(__ftrace_modify_code, &command, NULL);
1688 ret = ftrace_arch_code_modify_post_process();
1689 FTRACE_WARN_ON(ret);
1692 static ftrace_func_t saved_ftrace_func;
1693 static int ftrace_start_up;
1694 static int global_start_up;
1696 static void ftrace_startup_enable(int command)
1698 if (saved_ftrace_func != ftrace_trace_function) {
1699 saved_ftrace_func = ftrace_trace_function;
1700 command |= FTRACE_UPDATE_TRACE_FUNC;
1703 if (!command || !ftrace_enabled)
1706 ftrace_run_update_code(command);
1709 static int ftrace_startup(struct ftrace_ops *ops, int command)
1711 bool hash_enable = true;
1714 if (unlikely(ftrace_disabled))
1717 ret = __register_ftrace_function(ops);
1722 command |= FTRACE_UPDATE_CALLS;
1724 /* ops marked global share the filter hashes */
1725 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1727 /* Don't update hash if global is already set */
1728 if (global_start_up)
1729 hash_enable = false;
1733 ops->flags |= FTRACE_OPS_FL_ENABLED;
1735 ftrace_hash_rec_enable(ops, 1);
1737 ftrace_startup_enable(command);
1742 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
1744 bool hash_disable = true;
1747 if (unlikely(ftrace_disabled))
1750 ret = __unregister_ftrace_function(ops);
1756 * Just warn in case of unbalance, no need to kill ftrace, it's not
1757 * critical but the ftrace_call callers may be never nopped again after
1758 * further ftrace uses.
1760 WARN_ON_ONCE(ftrace_start_up < 0);
1762 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1765 WARN_ON_ONCE(global_start_up < 0);
1766 /* Don't update hash if global still has users */
1767 if (global_start_up) {
1768 WARN_ON_ONCE(!ftrace_start_up);
1769 hash_disable = false;
1774 ftrace_hash_rec_disable(ops, 1);
1776 if (ops != &global_ops || !global_start_up)
1777 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1779 command |= FTRACE_UPDATE_CALLS;
1781 if (saved_ftrace_func != ftrace_trace_function) {
1782 saved_ftrace_func = ftrace_trace_function;
1783 command |= FTRACE_UPDATE_TRACE_FUNC;
1786 if (!command || !ftrace_enabled)
1789 ftrace_run_update_code(command);
1792 * Dynamic ops may be freed, we must make sure that all
1793 * callers are done before leaving this function.
1794 * The same goes for freeing the per_cpu data of the control
1797 * Again, normal synchronize_sched() is not good enough.
1798 * We need to do a hard force of sched synchronization.
1799 * This is because we use preempt_disable() to do RCU, but
1800 * the function tracers can be called where RCU is not watching
1801 * (like before user_exit()). We can not rely on the RCU
1802 * infrastructure to do the synchronization, thus we must do it
1805 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC))
1806 schedule_on_each_cpu(ftrace_sync);
1811 static void ftrace_startup_sysctl(void)
1813 if (unlikely(ftrace_disabled))
1816 /* Force update next time */
1817 saved_ftrace_func = NULL;
1818 /* ftrace_start_up is true if we want ftrace running */
1819 if (ftrace_start_up)
1820 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
1823 static void ftrace_shutdown_sysctl(void)
1825 if (unlikely(ftrace_disabled))
1828 /* ftrace_start_up is true if ftrace is running */
1829 if (ftrace_start_up)
1830 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1833 static cycle_t ftrace_update_time;
1834 static unsigned long ftrace_update_cnt;
1835 unsigned long ftrace_update_tot_cnt;
1837 static inline int ops_traces_mod(struct ftrace_ops *ops)
1840 * Filter_hash being empty will default to trace module.
1841 * But notrace hash requires a test of individual module functions.
1843 return ftrace_hash_empty(ops->filter_hash) &&
1844 ftrace_hash_empty(ops->notrace_hash);
1848 * Check if the current ops references the record.
1850 * If the ops traces all functions, then it was already accounted for.
1851 * If the ops does not trace the current record function, skip it.
1852 * If the ops ignores the function via notrace filter, skip it.
1855 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
1857 /* If ops isn't enabled, ignore it */
1858 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1861 /* If ops traces all mods, we already accounted for it */
1862 if (ops_traces_mod(ops))
1865 /* The function must be in the filter */
1866 if (!ftrace_hash_empty(ops->filter_hash) &&
1867 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
1870 /* If in notrace hash, we ignore it too */
1871 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
1877 static int referenced_filters(struct dyn_ftrace *rec)
1879 struct ftrace_ops *ops;
1882 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
1883 if (ops_references_rec(ops, rec))
1890 static int ftrace_update_code(struct module *mod)
1892 struct dyn_ftrace *p;
1893 cycle_t start, stop;
1894 unsigned long ref = 0;
1898 * When adding a module, we need to check if tracers are
1899 * currently enabled and if they are set to trace all functions.
1900 * If they are, we need to enable the module functions as well
1901 * as update the reference counts for those function records.
1904 struct ftrace_ops *ops;
1906 for (ops = ftrace_ops_list;
1907 ops != &ftrace_list_end; ops = ops->next) {
1908 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
1909 if (ops_traces_mod(ops))
1917 start = ftrace_now(raw_smp_processor_id());
1918 ftrace_update_cnt = 0;
1920 while (ftrace_new_addrs) {
1923 /* If something went wrong, bail without enabling anything */
1924 if (unlikely(ftrace_disabled))
1927 p = ftrace_new_addrs;
1928 ftrace_new_addrs = p->newlist;
1930 cnt += referenced_filters(p);
1934 * Do the initial record conversion from mcount jump
1935 * to the NOP instructions.
1937 if (!ftrace_code_disable(mod, p)) {
1943 ftrace_update_cnt++;
1946 * If the tracing is enabled, go ahead and enable the record.
1948 * The reason not to enable the record immediatelly is the
1949 * inherent check of ftrace_make_nop/ftrace_make_call for
1950 * correct previous instructions. Making first the NOP
1951 * conversion puts the module to the correct state, thus
1952 * passing the ftrace_make_call check.
1954 if (ftrace_start_up && cnt) {
1955 int failed = __ftrace_replace_code(p, 1);
1957 ftrace_bug(failed, p->ip);
1963 stop = ftrace_now(raw_smp_processor_id());
1964 ftrace_update_time = stop - start;
1965 ftrace_update_tot_cnt += ftrace_update_cnt;
1970 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1972 struct ftrace_page *pg;
1976 /* allocate a few pages */
1977 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1978 if (!ftrace_pages_start)
1982 * Allocate a few more pages.
1984 * TODO: have some parser search vmlinux before
1985 * final linking to find all calls to ftrace.
1987 * a) know how many pages to allocate.
1989 * b) set up the table then.
1991 * The dynamic code is still necessary for
1995 pg = ftrace_pages = ftrace_pages_start;
1997 cnt = num_to_init / ENTRIES_PER_PAGE;
1998 pr_info("ftrace: allocating %ld entries in %d pages\n",
1999 num_to_init, cnt + 1);
2001 for (i = 0; i < cnt; i++) {
2002 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
2004 /* If we fail, we'll try later anyway */
2015 FTRACE_ITER_FILTER = (1 << 0),
2016 FTRACE_ITER_NOTRACE = (1 << 1),
2017 FTRACE_ITER_PRINTALL = (1 << 2),
2018 FTRACE_ITER_HASH = (1 << 3),
2019 FTRACE_ITER_ENABLED = (1 << 4),
2022 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2024 struct ftrace_iterator {
2027 struct ftrace_page *pg;
2028 struct dyn_ftrace *func;
2029 struct ftrace_func_probe *probe;
2030 struct trace_parser parser;
2031 struct ftrace_hash *hash;
2032 struct ftrace_ops *ops;
2039 t_hash_next(struct seq_file *m, loff_t *pos)
2041 struct ftrace_iterator *iter = m->private;
2042 struct hlist_node *hnd = NULL;
2043 struct hlist_head *hhd;
2049 hnd = &iter->probe->node;
2051 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2054 hhd = &ftrace_func_hash[iter->hidx];
2056 if (hlist_empty(hhd)) {
2072 if (WARN_ON_ONCE(!hnd))
2075 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2080 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2082 struct ftrace_iterator *iter = m->private;
2086 if (iter->func_pos > *pos)
2090 for (l = 0; l <= (*pos - iter->func_pos); ) {
2091 p = t_hash_next(m, &l);
2098 /* Only set this if we have an item */
2099 iter->flags |= FTRACE_ITER_HASH;
2105 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2107 struct ftrace_func_probe *rec;
2110 if (WARN_ON_ONCE(!rec))
2113 if (rec->ops->print)
2114 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2116 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2119 seq_printf(m, ":%p", rec->data);
2126 t_next(struct seq_file *m, void *v, loff_t *pos)
2128 struct ftrace_iterator *iter = m->private;
2129 struct ftrace_ops *ops = &global_ops;
2130 struct dyn_ftrace *rec = NULL;
2132 if (unlikely(ftrace_disabled))
2135 if (iter->flags & FTRACE_ITER_HASH)
2136 return t_hash_next(m, pos);
2139 iter->pos = iter->func_pos = *pos;
2141 if (iter->flags & FTRACE_ITER_PRINTALL)
2142 return t_hash_start(m, pos);
2145 if (iter->idx >= iter->pg->index) {
2146 if (iter->pg->next) {
2147 iter->pg = iter->pg->next;
2152 rec = &iter->pg->records[iter->idx++];
2153 if ((rec->flags & FTRACE_FL_FREE) ||
2155 ((iter->flags & FTRACE_ITER_FILTER) &&
2156 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2158 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2159 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2161 ((iter->flags & FTRACE_ITER_ENABLED) &&
2162 !(rec->flags & ~FTRACE_FL_MASK))) {
2170 return t_hash_start(m, pos);
2177 static void reset_iter_read(struct ftrace_iterator *iter)
2181 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2184 static void *t_start(struct seq_file *m, loff_t *pos)
2186 struct ftrace_iterator *iter = m->private;
2187 struct ftrace_ops *ops = &global_ops;
2191 mutex_lock(&ftrace_lock);
2193 if (unlikely(ftrace_disabled))
2197 * If an lseek was done, then reset and start from beginning.
2199 if (*pos < iter->pos)
2200 reset_iter_read(iter);
2203 * For set_ftrace_filter reading, if we have the filter
2204 * off, we can short cut and just print out that all
2205 * functions are enabled.
2207 if (iter->flags & FTRACE_ITER_FILTER &&
2208 ftrace_hash_empty(ops->filter_hash)) {
2210 return t_hash_start(m, pos);
2211 iter->flags |= FTRACE_ITER_PRINTALL;
2212 /* reset in case of seek/pread */
2213 iter->flags &= ~FTRACE_ITER_HASH;
2217 if (iter->flags & FTRACE_ITER_HASH)
2218 return t_hash_start(m, pos);
2221 * Unfortunately, we need to restart at ftrace_pages_start
2222 * every time we let go of the ftrace_mutex. This is because
2223 * those pointers can change without the lock.
2225 iter->pg = ftrace_pages_start;
2227 for (l = 0; l <= *pos; ) {
2228 p = t_next(m, p, &l);
2234 if (iter->flags & FTRACE_ITER_FILTER)
2235 return t_hash_start(m, pos);
2243 static void t_stop(struct seq_file *m, void *p)
2245 mutex_unlock(&ftrace_lock);
2248 static int t_show(struct seq_file *m, void *v)
2250 struct ftrace_iterator *iter = m->private;
2251 struct dyn_ftrace *rec;
2253 if (iter->flags & FTRACE_ITER_HASH)
2254 return t_hash_show(m, iter);
2256 if (iter->flags & FTRACE_ITER_PRINTALL) {
2257 seq_printf(m, "#### all functions enabled ####\n");
2266 seq_printf(m, "%ps", (void *)rec->ip);
2267 if (iter->flags & FTRACE_ITER_ENABLED)
2268 seq_printf(m, " (%ld)",
2269 rec->flags & ~FTRACE_FL_MASK);
2270 seq_printf(m, "\n");
2275 static const struct seq_operations show_ftrace_seq_ops = {
2283 ftrace_avail_open(struct inode *inode, struct file *file)
2285 struct ftrace_iterator *iter;
2288 if (unlikely(ftrace_disabled))
2291 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2295 iter->pg = ftrace_pages_start;
2297 ret = seq_open(file, &show_ftrace_seq_ops);
2299 struct seq_file *m = file->private_data;
2310 ftrace_enabled_open(struct inode *inode, struct file *file)
2312 struct ftrace_iterator *iter;
2315 if (unlikely(ftrace_disabled))
2318 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2322 iter->pg = ftrace_pages_start;
2323 iter->flags = FTRACE_ITER_ENABLED;
2325 ret = seq_open(file, &show_ftrace_seq_ops);
2327 struct seq_file *m = file->private_data;
2337 static void ftrace_filter_reset(struct ftrace_hash *hash)
2339 mutex_lock(&ftrace_lock);
2340 ftrace_hash_clear(hash);
2341 mutex_unlock(&ftrace_lock);
2345 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2346 struct inode *inode, struct file *file)
2348 struct ftrace_iterator *iter;
2349 struct ftrace_hash *hash;
2352 if (unlikely(ftrace_disabled))
2355 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2359 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2364 if (flag & FTRACE_ITER_NOTRACE)
2365 hash = ops->notrace_hash;
2367 hash = ops->filter_hash;
2372 if (file->f_mode & FMODE_WRITE) {
2373 mutex_lock(&ftrace_lock);
2374 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2375 mutex_unlock(&ftrace_lock);
2378 trace_parser_put(&iter->parser);
2384 mutex_lock(&ftrace_regex_lock);
2386 if ((file->f_mode & FMODE_WRITE) &&
2387 (file->f_flags & O_TRUNC))
2388 ftrace_filter_reset(iter->hash);
2390 if (file->f_mode & FMODE_READ) {
2391 iter->pg = ftrace_pages_start;
2393 ret = seq_open(file, &show_ftrace_seq_ops);
2395 struct seq_file *m = file->private_data;
2399 free_ftrace_hash(iter->hash);
2400 trace_parser_put(&iter->parser);
2404 file->private_data = iter;
2405 mutex_unlock(&ftrace_regex_lock);
2411 ftrace_filter_open(struct inode *inode, struct file *file)
2413 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
2418 ftrace_notrace_open(struct inode *inode, struct file *file)
2420 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2424 static int ftrace_match(char *str, char *regex, int len, int type)
2431 if (strcmp(str, regex) == 0)
2434 case MATCH_FRONT_ONLY:
2435 if (strncmp(str, regex, len) == 0)
2438 case MATCH_MIDDLE_ONLY:
2439 if (strstr(str, regex))
2442 case MATCH_END_ONLY:
2444 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2453 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2455 struct ftrace_func_entry *entry;
2458 entry = ftrace_lookup_ip(hash, rec->ip);
2460 /* Do nothing if it doesn't exist */
2464 free_hash_entry(hash, entry);
2466 /* Do nothing if it exists */
2470 ret = add_hash_entry(hash, rec->ip);
2476 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2477 char *regex, int len, int type)
2479 char str[KSYM_SYMBOL_LEN];
2482 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2485 /* module lookup requires matching the module */
2486 if (!modname || strcmp(modname, mod))
2489 /* blank search means to match all funcs in the mod */
2494 return ftrace_match(str, regex, len, type);
2498 match_records(struct ftrace_hash *hash, char *buff,
2499 int len, char *mod, int not)
2501 unsigned search_len = 0;
2502 struct ftrace_page *pg;
2503 struct dyn_ftrace *rec;
2504 int type = MATCH_FULL;
2505 char *search = buff;
2510 type = filter_parse_regex(buff, len, &search, ¬);
2511 search_len = strlen(search);
2514 mutex_lock(&ftrace_lock);
2516 if (unlikely(ftrace_disabled))
2519 do_for_each_ftrace_rec(pg, rec) {
2521 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2522 ret = enter_record(hash, rec, not);
2529 } while_for_each_ftrace_rec();
2531 mutex_unlock(&ftrace_lock);
2537 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2539 return match_records(hash, buff, len, NULL, 0);
2543 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2547 /* blank or '*' mean the same */
2548 if (strcmp(buff, "*") == 0)
2551 /* handle the case of 'dont filter this module' */
2552 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2557 return match_records(hash, buff, strlen(buff), mod, not);
2561 * We register the module command as a template to show others how
2562 * to register the a command as well.
2566 ftrace_mod_callback(struct ftrace_hash *hash,
2567 char *func, char *cmd, char *param, int enable)
2573 * cmd == 'mod' because we only registered this func
2574 * for the 'mod' ftrace_func_command.
2575 * But if you register one func with multiple commands,
2576 * you can tell which command was used by the cmd
2580 /* we must have a module name */
2584 mod = strsep(¶m, ":");
2588 ret = ftrace_match_module_records(hash, func, mod);
2597 static struct ftrace_func_command ftrace_mod_cmd = {
2599 .func = ftrace_mod_callback,
2602 static int __init ftrace_mod_cmd_init(void)
2604 return register_ftrace_command(&ftrace_mod_cmd);
2606 device_initcall(ftrace_mod_cmd_init);
2609 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2611 struct ftrace_func_probe *entry;
2612 struct hlist_head *hhd;
2613 struct hlist_node *n;
2616 key = hash_long(ip, FTRACE_HASH_BITS);
2618 hhd = &ftrace_func_hash[key];
2620 if (hlist_empty(hhd))
2624 * Disable preemption for these calls to prevent a RCU grace
2625 * period. This syncs the hash iteration and freeing of items
2626 * on the hash. rcu_read_lock is too dangerous here.
2628 preempt_disable_notrace();
2629 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2630 if (entry->ip == ip)
2631 entry->ops->func(ip, parent_ip, &entry->data);
2633 preempt_enable_notrace();
2636 static struct ftrace_ops trace_probe_ops __read_mostly =
2638 .func = function_trace_probe_call,
2641 static int ftrace_probe_registered;
2643 static void __enable_ftrace_function_probe(void)
2648 if (ftrace_probe_registered)
2651 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2652 struct hlist_head *hhd = &ftrace_func_hash[i];
2656 /* Nothing registered? */
2657 if (i == FTRACE_FUNC_HASHSIZE)
2660 ret = ftrace_startup(&trace_probe_ops, 0);
2662 ftrace_probe_registered = 1;
2665 static void __disable_ftrace_function_probe(void)
2669 if (!ftrace_probe_registered)
2672 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2673 struct hlist_head *hhd = &ftrace_func_hash[i];
2678 /* no more funcs left */
2679 ftrace_shutdown(&trace_probe_ops, 0);
2681 ftrace_probe_registered = 0;
2685 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2687 struct ftrace_func_probe *entry =
2688 container_of(rhp, struct ftrace_func_probe, rcu);
2690 if (entry->ops->free)
2691 entry->ops->free(&entry->data);
2697 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2700 struct ftrace_func_probe *entry;
2701 struct ftrace_page *pg;
2702 struct dyn_ftrace *rec;
2708 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2709 len = strlen(search);
2711 /* we do not support '!' for function probes */
2715 mutex_lock(&ftrace_lock);
2717 if (unlikely(ftrace_disabled))
2720 do_for_each_ftrace_rec(pg, rec) {
2722 if (!ftrace_match_record(rec, NULL, search, len, type))
2725 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2727 /* If we did not process any, then return error */
2738 * The caller might want to do something special
2739 * for each function we find. We call the callback
2740 * to give the caller an opportunity to do so.
2742 if (ops->callback) {
2743 if (ops->callback(rec->ip, &entry->data) < 0) {
2744 /* caller does not like this func */
2751 entry->ip = rec->ip;
2753 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2754 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2756 } while_for_each_ftrace_rec();
2757 __enable_ftrace_function_probe();
2760 mutex_unlock(&ftrace_lock);
2766 PROBE_TEST_FUNC = 1,
2771 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2772 void *data, int flags)
2774 struct ftrace_func_probe *entry;
2775 struct hlist_node *n, *tmp;
2776 char str[KSYM_SYMBOL_LEN];
2777 int type = MATCH_FULL;
2781 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2786 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2787 len = strlen(search);
2789 /* we do not support '!' for function probes */
2794 mutex_lock(&ftrace_lock);
2795 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2796 struct hlist_head *hhd = &ftrace_func_hash[i];
2798 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2800 /* break up if statements for readability */
2801 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2804 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2807 /* do this last, since it is the most expensive */
2809 kallsyms_lookup(entry->ip, NULL, NULL,
2811 if (!ftrace_match(str, glob, len, type))
2815 hlist_del_rcu(&entry->node);
2816 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
2819 __disable_ftrace_function_probe();
2820 mutex_unlock(&ftrace_lock);
2824 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2827 __unregister_ftrace_function_probe(glob, ops, data,
2828 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2832 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2834 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2837 void unregister_ftrace_function_probe_all(char *glob)
2839 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2842 static LIST_HEAD(ftrace_commands);
2843 static DEFINE_MUTEX(ftrace_cmd_mutex);
2845 int register_ftrace_command(struct ftrace_func_command *cmd)
2847 struct ftrace_func_command *p;
2850 mutex_lock(&ftrace_cmd_mutex);
2851 list_for_each_entry(p, &ftrace_commands, list) {
2852 if (strcmp(cmd->name, p->name) == 0) {
2857 list_add(&cmd->list, &ftrace_commands);
2859 mutex_unlock(&ftrace_cmd_mutex);
2864 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2866 struct ftrace_func_command *p, *n;
2869 mutex_lock(&ftrace_cmd_mutex);
2870 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2871 if (strcmp(cmd->name, p->name) == 0) {
2873 list_del_init(&p->list);
2878 mutex_unlock(&ftrace_cmd_mutex);
2883 static int ftrace_process_regex(struct ftrace_hash *hash,
2884 char *buff, int len, int enable)
2886 char *func, *command, *next = buff;
2887 struct ftrace_func_command *p;
2890 func = strsep(&next, ":");
2893 ret = ftrace_match_records(hash, func, len);
2903 command = strsep(&next, ":");
2905 mutex_lock(&ftrace_cmd_mutex);
2906 list_for_each_entry(p, &ftrace_commands, list) {
2907 if (strcmp(p->name, command) == 0) {
2908 ret = p->func(hash, func, command, next, enable);
2913 mutex_unlock(&ftrace_cmd_mutex);
2919 ftrace_regex_write(struct file *file, const char __user *ubuf,
2920 size_t cnt, loff_t *ppos, int enable)
2922 struct ftrace_iterator *iter;
2923 struct trace_parser *parser;
2929 mutex_lock(&ftrace_regex_lock);
2932 if (unlikely(ftrace_disabled))
2935 if (file->f_mode & FMODE_READ) {
2936 struct seq_file *m = file->private_data;
2939 iter = file->private_data;
2941 parser = &iter->parser;
2942 read = trace_get_user(parser, ubuf, cnt, ppos);
2944 if (read >= 0 && trace_parser_loaded(parser) &&
2945 !trace_parser_cont(parser)) {
2946 ret = ftrace_process_regex(iter->hash, parser->buffer,
2947 parser->idx, enable);
2948 trace_parser_clear(parser);
2955 mutex_unlock(&ftrace_regex_lock);
2961 ftrace_filter_write(struct file *file, const char __user *ubuf,
2962 size_t cnt, loff_t *ppos)
2964 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2968 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2969 size_t cnt, loff_t *ppos)
2971 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2975 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2976 int reset, int enable)
2978 struct ftrace_hash **orig_hash;
2979 struct ftrace_hash *hash;
2982 /* All global ops uses the global ops filters */
2983 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
2986 if (unlikely(ftrace_disabled))
2990 orig_hash = &ops->filter_hash;
2992 orig_hash = &ops->notrace_hash;
2994 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2998 mutex_lock(&ftrace_regex_lock);
3000 ftrace_filter_reset(hash);
3002 ftrace_match_records(hash, buf, len);
3004 mutex_lock(&ftrace_lock);
3005 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3006 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3008 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3010 mutex_unlock(&ftrace_lock);
3012 mutex_unlock(&ftrace_regex_lock);
3014 free_ftrace_hash(hash);
3019 * ftrace_set_filter - set a function to filter on in ftrace
3020 * @ops - the ops to set the filter with
3021 * @buf - the string that holds the function filter text.
3022 * @len - the length of the string.
3023 * @reset - non zero to reset all filters before applying this filter.
3025 * Filters denote which functions should be enabled when tracing is enabled.
3026 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3028 void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3031 ftrace_set_regex(ops, buf, len, reset, 1);
3033 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3036 * ftrace_set_notrace - set a function to not trace in ftrace
3037 * @ops - the ops to set the notrace filter with
3038 * @buf - the string that holds the function notrace text.
3039 * @len - the length of the string.
3040 * @reset - non zero to reset all filters before applying this filter.
3042 * Notrace Filters denote which functions should not be enabled when tracing
3043 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3046 void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3049 ftrace_set_regex(ops, buf, len, reset, 0);
3051 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3053 * ftrace_set_filter - set a function to filter on in ftrace
3054 * @ops - the ops to set the filter with
3055 * @buf - the string that holds the function filter text.
3056 * @len - the length of the string.
3057 * @reset - non zero to reset all filters before applying this filter.
3059 * Filters denote which functions should be enabled when tracing is enabled.
3060 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3062 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3064 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3066 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3069 * ftrace_set_notrace - set a function to not trace in ftrace
3070 * @ops - the ops to set the notrace filter with
3071 * @buf - the string that holds the function notrace text.
3072 * @len - the length of the string.
3073 * @reset - non zero to reset all filters before applying this filter.
3075 * Notrace Filters denote which functions should not be enabled when tracing
3076 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3079 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3081 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3083 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3086 * command line interface to allow users to set filters on boot up.
3088 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3089 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3090 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3092 static int __init set_ftrace_notrace(char *str)
3094 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3097 __setup("ftrace_notrace=", set_ftrace_notrace);
3099 static int __init set_ftrace_filter(char *str)
3101 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3104 __setup("ftrace_filter=", set_ftrace_filter);
3106 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3107 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3108 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3110 static int __init set_graph_function(char *str)
3112 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3115 __setup("ftrace_graph_filter=", set_graph_function);
3117 static void __init set_ftrace_early_graph(char *buf)
3123 func = strsep(&buf, ",");
3124 /* we allow only one expression at a time */
3125 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3128 printk(KERN_DEBUG "ftrace: function %s not "
3129 "traceable\n", func);
3132 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3135 set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3140 func = strsep(&buf, ",");
3141 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3145 static void __init set_ftrace_early_filters(void)
3147 if (ftrace_filter_buf[0])
3148 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
3149 if (ftrace_notrace_buf[0])
3150 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
3151 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3152 if (ftrace_graph_buf[0])
3153 set_ftrace_early_graph(ftrace_graph_buf);
3154 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3158 ftrace_regex_release(struct inode *inode, struct file *file)
3160 struct seq_file *m = (struct seq_file *)file->private_data;
3161 struct ftrace_iterator *iter;
3162 struct ftrace_hash **orig_hash;
3163 struct trace_parser *parser;
3167 mutex_lock(&ftrace_regex_lock);
3168 if (file->f_mode & FMODE_READ) {
3171 seq_release(inode, file);
3173 iter = file->private_data;
3175 parser = &iter->parser;
3176 if (trace_parser_loaded(parser)) {
3177 parser->buffer[parser->idx] = 0;
3178 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3181 trace_parser_put(parser);
3183 if (file->f_mode & FMODE_WRITE) {
3184 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3187 orig_hash = &iter->ops->filter_hash;
3189 orig_hash = &iter->ops->notrace_hash;
3191 mutex_lock(&ftrace_lock);
3192 ret = ftrace_hash_move(iter->ops, filter_hash,
3193 orig_hash, iter->hash);
3194 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3196 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3198 mutex_unlock(&ftrace_lock);
3200 free_ftrace_hash(iter->hash);
3203 mutex_unlock(&ftrace_regex_lock);
3207 static const struct file_operations ftrace_avail_fops = {
3208 .open = ftrace_avail_open,
3210 .llseek = seq_lseek,
3211 .release = seq_release_private,
3214 static const struct file_operations ftrace_enabled_fops = {
3215 .open = ftrace_enabled_open,
3217 .llseek = seq_lseek,
3218 .release = seq_release_private,
3221 static const struct file_operations ftrace_filter_fops = {
3222 .open = ftrace_filter_open,
3224 .write = ftrace_filter_write,
3225 .llseek = ftrace_filter_lseek,
3226 .release = ftrace_regex_release,
3229 static const struct file_operations ftrace_notrace_fops = {
3230 .open = ftrace_notrace_open,
3232 .write = ftrace_notrace_write,
3233 .llseek = ftrace_filter_lseek,
3234 .release = ftrace_regex_release,
3237 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3239 static DEFINE_MUTEX(graph_lock);
3241 int ftrace_graph_count;
3242 int ftrace_graph_filter_enabled;
3243 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3246 __g_next(struct seq_file *m, loff_t *pos)
3248 if (*pos >= ftrace_graph_count)
3250 return &ftrace_graph_funcs[*pos];
3254 g_next(struct seq_file *m, void *v, loff_t *pos)
3257 return __g_next(m, pos);
3260 static void *g_start(struct seq_file *m, loff_t *pos)
3262 mutex_lock(&graph_lock);
3264 /* Nothing, tell g_show to print all functions are enabled */
3265 if (!ftrace_graph_filter_enabled && !*pos)
3268 return __g_next(m, pos);
3271 static void g_stop(struct seq_file *m, void *p)
3273 mutex_unlock(&graph_lock);
3276 static int g_show(struct seq_file *m, void *v)
3278 unsigned long *ptr = v;
3283 if (ptr == (unsigned long *)1) {
3284 seq_printf(m, "#### all functions enabled ####\n");
3288 seq_printf(m, "%ps\n", (void *)*ptr);
3293 static const struct seq_operations ftrace_graph_seq_ops = {
3301 ftrace_graph_open(struct inode *inode, struct file *file)
3305 if (unlikely(ftrace_disabled))
3308 mutex_lock(&graph_lock);
3309 if ((file->f_mode & FMODE_WRITE) &&
3310 (file->f_flags & O_TRUNC)) {
3311 ftrace_graph_filter_enabled = 0;
3312 ftrace_graph_count = 0;
3313 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3315 mutex_unlock(&graph_lock);
3317 if (file->f_mode & FMODE_READ)
3318 ret = seq_open(file, &ftrace_graph_seq_ops);
3324 ftrace_graph_release(struct inode *inode, struct file *file)
3326 if (file->f_mode & FMODE_READ)
3327 seq_release(inode, file);
3332 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3334 struct dyn_ftrace *rec;
3335 struct ftrace_page *pg;
3344 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3345 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3348 search_len = strlen(search);
3350 mutex_lock(&ftrace_lock);
3352 if (unlikely(ftrace_disabled)) {
3353 mutex_unlock(&ftrace_lock);
3357 do_for_each_ftrace_rec(pg, rec) {
3359 if (rec->flags & FTRACE_FL_FREE)
3362 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3363 /* if it is in the array */
3365 for (i = 0; i < *idx; i++) {
3366 if (array[i] == rec->ip) {
3375 array[(*idx)++] = rec->ip;
3376 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3381 array[i] = array[--(*idx)];
3387 } while_for_each_ftrace_rec();
3389 mutex_unlock(&ftrace_lock);
3394 ftrace_graph_filter_enabled = !!(*idx);
3400 ftrace_graph_write(struct file *file, const char __user *ubuf,
3401 size_t cnt, loff_t *ppos)
3403 struct trace_parser parser;
3409 mutex_lock(&graph_lock);
3411 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3416 read = trace_get_user(&parser, ubuf, cnt, ppos);
3418 if (read >= 0 && trace_parser_loaded((&parser))) {
3419 parser.buffer[parser.idx] = 0;
3421 /* we allow only one expression at a time */
3422 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3431 trace_parser_put(&parser);
3433 mutex_unlock(&graph_lock);
3438 static const struct file_operations ftrace_graph_fops = {
3439 .open = ftrace_graph_open,
3441 .write = ftrace_graph_write,
3442 .llseek = ftrace_filter_lseek,
3443 .release = ftrace_graph_release,
3445 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3447 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3450 trace_create_file("available_filter_functions", 0444,
3451 d_tracer, NULL, &ftrace_avail_fops);
3453 trace_create_file("enabled_functions", 0444,
3454 d_tracer, NULL, &ftrace_enabled_fops);
3456 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3457 NULL, &ftrace_filter_fops);
3459 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3460 NULL, &ftrace_notrace_fops);
3462 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3463 trace_create_file("set_graph_function", 0444, d_tracer,
3465 &ftrace_graph_fops);
3466 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3471 static int ftrace_process_locs(struct module *mod,
3472 unsigned long *start,
3477 unsigned long flags = 0; /* Shut up gcc */
3479 mutex_lock(&ftrace_lock);
3482 addr = ftrace_call_adjust(*p++);
3484 * Some architecture linkers will pad between
3485 * the different mcount_loc sections of different
3486 * object files to satisfy alignments.
3487 * Skip any NULL pointers.
3491 ftrace_record_ip(addr);
3495 * We only need to disable interrupts on start up
3496 * because we are modifying code that an interrupt
3497 * may execute, and the modification is not atomic.
3498 * But for modules, nothing runs the code we modify
3499 * until we are finished with it, and there's no
3500 * reason to cause large interrupt latencies while we do it.
3503 local_irq_save(flags);
3504 ftrace_update_code(mod);
3506 local_irq_restore(flags);
3507 mutex_unlock(&ftrace_lock);
3512 #ifdef CONFIG_MODULES
3513 void ftrace_release_mod(struct module *mod)
3515 struct dyn_ftrace *rec;
3516 struct ftrace_page *pg;
3518 mutex_lock(&ftrace_lock);
3520 if (ftrace_disabled)
3523 do_for_each_ftrace_rec(pg, rec) {
3524 if (within_module_core(rec->ip, mod)) {
3526 * rec->ip is changed in ftrace_free_rec()
3527 * It should not between s and e if record was freed.
3529 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3530 ftrace_free_rec(rec);
3532 } while_for_each_ftrace_rec();
3534 mutex_unlock(&ftrace_lock);
3537 static void ftrace_init_module(struct module *mod,
3538 unsigned long *start, unsigned long *end)
3540 if (ftrace_disabled || start == end)
3542 ftrace_process_locs(mod, start, end);
3545 static int ftrace_module_notify_enter(struct notifier_block *self,
3546 unsigned long val, void *data)
3548 struct module *mod = data;
3550 if (val == MODULE_STATE_COMING)
3551 ftrace_init_module(mod, mod->ftrace_callsites,
3552 mod->ftrace_callsites +
3553 mod->num_ftrace_callsites);
3557 static int ftrace_module_notify_exit(struct notifier_block *self,
3558 unsigned long val, void *data)
3560 struct module *mod = data;
3562 if (val == MODULE_STATE_GOING)
3563 ftrace_release_mod(mod);
3568 static int ftrace_module_notify_enter(struct notifier_block *self,
3569 unsigned long val, void *data)
3573 static int ftrace_module_notify_exit(struct notifier_block *self,
3574 unsigned long val, void *data)
3578 #endif /* CONFIG_MODULES */
3580 struct notifier_block ftrace_module_enter_nb = {
3581 .notifier_call = ftrace_module_notify_enter,
3582 .priority = INT_MAX, /* Run before anything that can use kprobes */
3585 struct notifier_block ftrace_module_exit_nb = {
3586 .notifier_call = ftrace_module_notify_exit,
3587 .priority = INT_MIN, /* Run after anything that can remove kprobes */
3590 extern unsigned long __start_mcount_loc[];
3591 extern unsigned long __stop_mcount_loc[];
3593 void __init ftrace_init(void)
3595 unsigned long count, addr, flags;
3598 /* Keep the ftrace pointer to the stub */
3599 addr = (unsigned long)ftrace_stub;
3601 local_irq_save(flags);
3602 ftrace_dyn_arch_init(&addr);
3603 local_irq_restore(flags);
3605 /* ftrace_dyn_arch_init places the return code in addr */
3609 count = __stop_mcount_loc - __start_mcount_loc;
3611 ret = ftrace_dyn_table_alloc(count);
3615 last_ftrace_enabled = ftrace_enabled = 1;
3617 ret = ftrace_process_locs(NULL,
3621 ret = register_module_notifier(&ftrace_module_enter_nb);
3623 pr_warning("Failed to register trace ftrace module enter notifier\n");
3625 ret = register_module_notifier(&ftrace_module_exit_nb);
3627 pr_warning("Failed to register trace ftrace module exit notifier\n");
3629 set_ftrace_early_filters();
3633 ftrace_disabled = 1;
3638 static struct ftrace_ops global_ops = {
3639 .func = ftrace_stub,
3642 static int __init ftrace_nodyn_init(void)
3647 device_initcall(ftrace_nodyn_init);
3649 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3650 static inline void ftrace_startup_enable(int command) { }
3651 /* Keep as macros so we do not need to define the commands */
3652 # define ftrace_startup(ops, command) \
3654 int ___ret = __register_ftrace_function(ops); \
3656 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3659 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
3661 # define ftrace_startup_sysctl() do { } while (0)
3662 # define ftrace_shutdown_sysctl() do { } while (0)
3665 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3670 #endif /* CONFIG_DYNAMIC_FTRACE */
3673 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3675 struct ftrace_ops *op;
3677 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3680 trace_recursion_set(TRACE_INTERNAL_BIT);
3682 * Some of the ops may be dynamically allocated,
3683 * they must be freed after a synchronize_sched().
3685 preempt_disable_notrace();
3686 op = rcu_dereference_raw(ftrace_ops_list);
3687 while (op != &ftrace_list_end) {
3688 if (ftrace_ops_test(op, ip))
3689 op->func(ip, parent_ip);
3690 op = rcu_dereference_raw(op->next);
3692 preempt_enable_notrace();
3693 trace_recursion_clear(TRACE_INTERNAL_BIT);
3696 static void clear_ftrace_swapper(void)
3698 struct task_struct *p;
3702 for_each_online_cpu(cpu) {
3704 clear_tsk_trace_trace(p);
3709 static void set_ftrace_swapper(void)
3711 struct task_struct *p;
3715 for_each_online_cpu(cpu) {
3717 set_tsk_trace_trace(p);
3722 static void clear_ftrace_pid(struct pid *pid)
3724 struct task_struct *p;
3727 do_each_pid_task(pid, PIDTYPE_PID, p) {
3728 clear_tsk_trace_trace(p);
3729 } while_each_pid_task(pid, PIDTYPE_PID, p);
3735 static void set_ftrace_pid(struct pid *pid)
3737 struct task_struct *p;
3740 do_each_pid_task(pid, PIDTYPE_PID, p) {
3741 set_tsk_trace_trace(p);
3742 } while_each_pid_task(pid, PIDTYPE_PID, p);
3746 static void clear_ftrace_pid_task(struct pid *pid)
3748 if (pid == ftrace_swapper_pid)
3749 clear_ftrace_swapper();
3751 clear_ftrace_pid(pid);
3754 static void set_ftrace_pid_task(struct pid *pid)
3756 if (pid == ftrace_swapper_pid)
3757 set_ftrace_swapper();
3759 set_ftrace_pid(pid);
3762 static int ftrace_pid_add(int p)
3765 struct ftrace_pid *fpid;
3768 mutex_lock(&ftrace_lock);
3771 pid = ftrace_swapper_pid;
3773 pid = find_get_pid(p);
3780 list_for_each_entry(fpid, &ftrace_pids, list)
3781 if (fpid->pid == pid)
3786 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3790 list_add(&fpid->list, &ftrace_pids);
3793 set_ftrace_pid_task(pid);
3795 ftrace_update_pid_func();
3796 ftrace_startup_enable(0);
3798 mutex_unlock(&ftrace_lock);
3802 if (pid != ftrace_swapper_pid)
3806 mutex_unlock(&ftrace_lock);
3810 static void ftrace_pid_reset(void)
3812 struct ftrace_pid *fpid, *safe;
3814 mutex_lock(&ftrace_lock);
3815 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3816 struct pid *pid = fpid->pid;
3818 clear_ftrace_pid_task(pid);
3820 list_del(&fpid->list);
3824 ftrace_update_pid_func();
3825 ftrace_startup_enable(0);
3827 mutex_unlock(&ftrace_lock);
3830 static void *fpid_start(struct seq_file *m, loff_t *pos)
3832 mutex_lock(&ftrace_lock);
3834 if (list_empty(&ftrace_pids) && (!*pos))
3837 return seq_list_start(&ftrace_pids, *pos);
3840 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3845 return seq_list_next(v, &ftrace_pids, pos);
3848 static void fpid_stop(struct seq_file *m, void *p)
3850 mutex_unlock(&ftrace_lock);
3853 static int fpid_show(struct seq_file *m, void *v)
3855 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3857 if (v == (void *)1) {
3858 seq_printf(m, "no pid\n");
3862 if (fpid->pid == ftrace_swapper_pid)
3863 seq_printf(m, "swapper tasks\n");
3865 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3870 static const struct seq_operations ftrace_pid_sops = {
3871 .start = fpid_start,
3878 ftrace_pid_open(struct inode *inode, struct file *file)
3882 if ((file->f_mode & FMODE_WRITE) &&
3883 (file->f_flags & O_TRUNC))
3886 if (file->f_mode & FMODE_READ)
3887 ret = seq_open(file, &ftrace_pid_sops);
3893 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3894 size_t cnt, loff_t *ppos)
3900 if (cnt >= sizeof(buf))
3903 if (copy_from_user(&buf, ubuf, cnt))
3909 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3910 * to clean the filter quietly.
3912 tmp = strstrip(buf);
3913 if (strlen(tmp) == 0)
3916 ret = strict_strtol(tmp, 10, &val);
3920 ret = ftrace_pid_add(val);
3922 return ret ? ret : cnt;
3926 ftrace_pid_release(struct inode *inode, struct file *file)
3928 if (file->f_mode & FMODE_READ)
3929 seq_release(inode, file);
3934 static const struct file_operations ftrace_pid_fops = {
3935 .open = ftrace_pid_open,
3936 .write = ftrace_pid_write,
3938 .llseek = ftrace_filter_lseek,
3939 .release = ftrace_pid_release,
3942 static __init int ftrace_init_debugfs(void)
3944 struct dentry *d_tracer;
3946 d_tracer = tracing_init_dentry();
3950 ftrace_init_dyn_debugfs(d_tracer);
3952 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3953 NULL, &ftrace_pid_fops);
3955 ftrace_profile_debugfs(d_tracer);
3959 fs_initcall(ftrace_init_debugfs);
3962 * ftrace_kill - kill ftrace
3964 * This function should be used by panic code. It stops ftrace
3965 * but in a not so nice way. If you need to simply kill ftrace
3966 * from a non-atomic section, use ftrace_kill.
3968 void ftrace_kill(void)
3970 ftrace_disabled = 1;
3972 clear_ftrace_function();
3976 * Test if ftrace is dead or not.
3978 int ftrace_is_dead(void)
3980 return ftrace_disabled;
3984 * register_ftrace_function - register a function for profiling
3985 * @ops - ops structure that holds the function for profiling.
3987 * Register a function to be called by all functions in the
3990 * Note: @ops->func and all the functions it calls must be labeled
3991 * with "notrace", otherwise it will go into a
3994 int register_ftrace_function(struct ftrace_ops *ops)
3998 mutex_lock(&ftrace_lock);
4000 ret = ftrace_startup(ops, 0);
4002 mutex_unlock(&ftrace_lock);
4005 EXPORT_SYMBOL_GPL(register_ftrace_function);
4008 * unregister_ftrace_function - unregister a function for profiling.
4009 * @ops - ops structure that holds the function to unregister
4011 * Unregister a function that was added to be called by ftrace profiling.
4013 int unregister_ftrace_function(struct ftrace_ops *ops)
4017 mutex_lock(&ftrace_lock);
4018 ret = ftrace_shutdown(ops, 0);
4019 mutex_unlock(&ftrace_lock);
4023 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4026 ftrace_enable_sysctl(struct ctl_table *table, int write,
4027 void __user *buffer, size_t *lenp,
4032 mutex_lock(&ftrace_lock);
4034 if (unlikely(ftrace_disabled))
4037 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4039 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4042 last_ftrace_enabled = !!ftrace_enabled;
4044 if (ftrace_enabled) {
4046 ftrace_startup_sysctl();
4048 /* we are starting ftrace again */
4049 if (ftrace_ops_list != &ftrace_list_end)
4050 update_ftrace_function();
4053 /* stopping ftrace calls (just send to ftrace_stub) */
4054 ftrace_trace_function = ftrace_stub;
4056 ftrace_shutdown_sysctl();
4060 mutex_unlock(&ftrace_lock);
4064 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4066 static int ftrace_graph_active;
4067 static struct notifier_block ftrace_suspend_notifier;
4069 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4074 /* The callbacks that hook a function */
4075 trace_func_graph_ret_t ftrace_graph_return =
4076 (trace_func_graph_ret_t)ftrace_stub;
4077 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4078 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
4080 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4081 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4085 unsigned long flags;
4086 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4087 struct task_struct *g, *t;
4089 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4090 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4091 * sizeof(struct ftrace_ret_stack),
4093 if (!ret_stack_list[i]) {
4101 read_lock_irqsave(&tasklist_lock, flags);
4102 do_each_thread(g, t) {
4108 if (t->ret_stack == NULL) {
4109 atomic_set(&t->tracing_graph_pause, 0);
4110 atomic_set(&t->trace_overrun, 0);
4111 t->curr_ret_stack = -1;
4112 /* Make sure the tasks see the -1 first: */
4114 t->ret_stack = ret_stack_list[start++];
4116 } while_each_thread(g, t);
4119 read_unlock_irqrestore(&tasklist_lock, flags);
4121 for (i = start; i < end; i++)
4122 kfree(ret_stack_list[i]);
4127 ftrace_graph_probe_sched_switch(void *ignore,
4128 struct task_struct *prev, struct task_struct *next)
4130 unsigned long long timestamp;
4134 * Does the user want to count the time a function was asleep.
4135 * If so, do not update the time stamps.
4137 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4140 timestamp = trace_clock_local();
4142 prev->ftrace_timestamp = timestamp;
4144 /* only process tasks that we timestamped */
4145 if (!next->ftrace_timestamp)
4149 * Update all the counters in next to make up for the
4150 * time next was sleeping.
4152 timestamp -= next->ftrace_timestamp;
4154 for (index = next->curr_ret_stack; index >= 0; index--)
4155 next->ret_stack[index].calltime += timestamp;
4158 /* Allocate a return stack for each task */
4159 static int start_graph_tracing(void)
4161 struct ftrace_ret_stack **ret_stack_list;
4164 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4165 sizeof(struct ftrace_ret_stack *),
4168 if (!ret_stack_list)
4171 /* The cpu_boot init_task->ret_stack will never be freed */
4172 for_each_online_cpu(cpu) {
4173 if (!idle_task(cpu)->ret_stack)
4174 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4178 ret = alloc_retstack_tasklist(ret_stack_list);
4179 } while (ret == -EAGAIN);
4182 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4184 pr_info("ftrace_graph: Couldn't activate tracepoint"
4185 " probe to kernel_sched_switch\n");
4188 kfree(ret_stack_list);
4193 * Hibernation protection.
4194 * The state of the current task is too much unstable during
4195 * suspend/restore to disk. We want to protect against that.
4198 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4202 case PM_HIBERNATION_PREPARE:
4203 pause_graph_tracing();
4206 case PM_POST_HIBERNATION:
4207 unpause_graph_tracing();
4213 /* Just a place holder for function graph */
4214 static struct ftrace_ops fgraph_ops __read_mostly = {
4215 .func = ftrace_stub,
4216 .flags = FTRACE_OPS_FL_GLOBAL,
4219 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
4221 if (!ftrace_ops_test(&global_ops, trace->func))
4223 return __ftrace_graph_entry(trace);
4227 * The function graph tracer should only trace the functions defined
4228 * by set_ftrace_filter and set_ftrace_notrace. If another function
4229 * tracer ops is registered, the graph tracer requires testing the
4230 * function against the global ops, and not just trace any function
4231 * that any ftrace_ops registered.
4233 static void update_function_graph_func(void)
4235 if (ftrace_ops_list == &ftrace_list_end ||
4236 (ftrace_ops_list == &global_ops &&
4237 global_ops.next == &ftrace_list_end))
4238 ftrace_graph_entry = __ftrace_graph_entry;
4240 ftrace_graph_entry = ftrace_graph_entry_test;
4243 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4244 trace_func_graph_ent_t entryfunc)
4248 mutex_lock(&ftrace_lock);
4250 /* we currently allow only one tracer registered at a time */
4251 if (ftrace_graph_active) {
4256 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4257 register_pm_notifier(&ftrace_suspend_notifier);
4259 ftrace_graph_active++;
4260 ret = start_graph_tracing();
4262 ftrace_graph_active--;
4266 ftrace_graph_return = retfunc;
4269 * Update the indirect function to the entryfunc, and the
4270 * function that gets called to the entry_test first. Then
4271 * call the update fgraph entry function to determine if
4272 * the entryfunc should be called directly or not.
4274 __ftrace_graph_entry = entryfunc;
4275 ftrace_graph_entry = ftrace_graph_entry_test;
4276 update_function_graph_func();
4278 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
4281 mutex_unlock(&ftrace_lock);
4285 void unregister_ftrace_graph(void)
4287 mutex_lock(&ftrace_lock);
4289 if (unlikely(!ftrace_graph_active))
4292 ftrace_graph_active--;
4293 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4294 ftrace_graph_entry = ftrace_graph_entry_stub;
4295 __ftrace_graph_entry = ftrace_graph_entry_stub;
4296 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
4297 unregister_pm_notifier(&ftrace_suspend_notifier);
4298 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4301 mutex_unlock(&ftrace_lock);
4304 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4307 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4309 atomic_set(&t->tracing_graph_pause, 0);
4310 atomic_set(&t->trace_overrun, 0);
4311 t->ftrace_timestamp = 0;
4312 /* make curr_ret_stack visible before we add the ret_stack */
4314 t->ret_stack = ret_stack;
4318 * Allocate a return stack for the idle task. May be the first
4319 * time through, or it may be done by CPU hotplug online.
4321 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4323 t->curr_ret_stack = -1;
4325 * The idle task has no parent, it either has its own
4326 * stack or no stack at all.
4329 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4331 if (ftrace_graph_active) {
4332 struct ftrace_ret_stack *ret_stack;
4334 ret_stack = per_cpu(idle_ret_stack, cpu);
4336 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4337 * sizeof(struct ftrace_ret_stack),
4341 per_cpu(idle_ret_stack, cpu) = ret_stack;
4343 graph_init_task(t, ret_stack);
4347 /* Allocate a return stack for newly created task */
4348 void ftrace_graph_init_task(struct task_struct *t)
4350 /* Make sure we do not use the parent ret_stack */
4351 t->ret_stack = NULL;
4352 t->curr_ret_stack = -1;
4354 if (ftrace_graph_active) {
4355 struct ftrace_ret_stack *ret_stack;
4357 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4358 * sizeof(struct ftrace_ret_stack),
4362 graph_init_task(t, ret_stack);
4366 void ftrace_graph_exit_task(struct task_struct *t)
4368 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4370 t->ret_stack = NULL;
4371 /* NULL must become visible to IRQs before we free it: */
4377 void ftrace_graph_stop(void)