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;
938 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
939 static int ftrace_graph_active;
941 # define ftrace_graph_active 0
945 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
949 if (file->f_mode & FMODE_READ)
950 ret = seq_lseek(file, offset, whence);
952 file->f_pos = ret = 1;
957 #ifdef CONFIG_DYNAMIC_FTRACE
959 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
960 # error Dynamic ftrace depends on MCOUNT_RECORD
963 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
965 struct ftrace_func_probe {
966 struct hlist_node node;
967 struct ftrace_probe_ops *ops;
975 FTRACE_UPDATE_CALLS = (1 << 0),
976 FTRACE_DISABLE_CALLS = (1 << 1),
977 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
978 FTRACE_START_FUNC_RET = (1 << 3),
979 FTRACE_STOP_FUNC_RET = (1 << 4),
981 struct ftrace_func_entry {
982 struct hlist_node hlist;
987 unsigned long size_bits;
988 struct hlist_head *buckets;
994 * We make these constant because no one should touch them,
995 * but they are used as the default "empty hash", to avoid allocating
996 * it all the time. These are in a read only section such that if
997 * anyone does try to modify it, it will cause an exception.
999 static const struct hlist_head empty_buckets[1];
1000 static const struct ftrace_hash empty_hash = {
1001 .buckets = (struct hlist_head *)empty_buckets,
1003 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1005 static struct ftrace_ops global_ops = {
1006 .func = ftrace_stub,
1007 .notrace_hash = EMPTY_HASH,
1008 .filter_hash = EMPTY_HASH,
1011 static struct dyn_ftrace *ftrace_new_addrs;
1013 static DEFINE_MUTEX(ftrace_regex_lock);
1015 struct ftrace_page {
1016 struct ftrace_page *next;
1018 struct dyn_ftrace records[];
1021 #define ENTRIES_PER_PAGE \
1022 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
1024 /* estimate from running different kernels */
1025 #define NR_TO_INIT 10000
1027 static struct ftrace_page *ftrace_pages_start;
1028 static struct ftrace_page *ftrace_pages;
1030 static struct dyn_ftrace *ftrace_free_records;
1032 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1034 return !hash || !hash->count;
1037 static struct ftrace_func_entry *
1038 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1041 struct ftrace_func_entry *entry;
1042 struct hlist_head *hhd;
1043 struct hlist_node *n;
1045 if (ftrace_hash_empty(hash))
1048 if (hash->size_bits > 0)
1049 key = hash_long(ip, hash->size_bits);
1053 hhd = &hash->buckets[key];
1055 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1056 if (entry->ip == ip)
1062 static void __add_hash_entry(struct ftrace_hash *hash,
1063 struct ftrace_func_entry *entry)
1065 struct hlist_head *hhd;
1068 if (hash->size_bits)
1069 key = hash_long(entry->ip, hash->size_bits);
1073 hhd = &hash->buckets[key];
1074 hlist_add_head(&entry->hlist, hhd);
1078 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1080 struct ftrace_func_entry *entry;
1082 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1087 __add_hash_entry(hash, entry);
1093 free_hash_entry(struct ftrace_hash *hash,
1094 struct ftrace_func_entry *entry)
1096 hlist_del(&entry->hlist);
1102 remove_hash_entry(struct ftrace_hash *hash,
1103 struct ftrace_func_entry *entry)
1105 hlist_del(&entry->hlist);
1109 static void ftrace_hash_clear(struct ftrace_hash *hash)
1111 struct hlist_head *hhd;
1112 struct hlist_node *tp, *tn;
1113 struct ftrace_func_entry *entry;
1114 int size = 1 << hash->size_bits;
1120 for (i = 0; i < size; i++) {
1121 hhd = &hash->buckets[i];
1122 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1123 free_hash_entry(hash, entry);
1125 FTRACE_WARN_ON(hash->count);
1128 static void free_ftrace_hash(struct ftrace_hash *hash)
1130 if (!hash || hash == EMPTY_HASH)
1132 ftrace_hash_clear(hash);
1133 kfree(hash->buckets);
1137 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1139 struct ftrace_hash *hash;
1141 hash = container_of(rcu, struct ftrace_hash, rcu);
1142 free_ftrace_hash(hash);
1145 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1147 if (!hash || hash == EMPTY_HASH)
1149 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1152 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1154 struct ftrace_hash *hash;
1157 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1161 size = 1 << size_bits;
1162 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1164 if (!hash->buckets) {
1169 hash->size_bits = size_bits;
1174 static struct ftrace_hash *
1175 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1177 struct ftrace_func_entry *entry;
1178 struct ftrace_hash *new_hash;
1179 struct hlist_node *tp;
1184 new_hash = alloc_ftrace_hash(size_bits);
1189 if (ftrace_hash_empty(hash))
1192 size = 1 << hash->size_bits;
1193 for (i = 0; i < size; i++) {
1194 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1195 ret = add_hash_entry(new_hash, entry->ip);
1201 FTRACE_WARN_ON(new_hash->count != hash->count);
1206 free_ftrace_hash(new_hash);
1211 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1213 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1216 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1217 struct ftrace_hash **dst, struct ftrace_hash *src)
1219 struct ftrace_func_entry *entry;
1220 struct hlist_node *tp, *tn;
1221 struct hlist_head *hhd;
1222 struct ftrace_hash *old_hash;
1223 struct ftrace_hash *new_hash;
1225 int size = src->count;
1231 * Remove the current set, update the hash and add
1234 ftrace_hash_rec_disable(ops, enable);
1237 * If the new source is empty, just free dst and assign it
1241 free_ftrace_hash_rcu(*dst);
1242 rcu_assign_pointer(*dst, EMPTY_HASH);
1243 /* still need to update the function records */
1249 * Make the hash size about 1/2 the # found
1251 for (size /= 2; size; size >>= 1)
1254 /* Don't allocate too much */
1255 if (bits > FTRACE_HASH_MAX_BITS)
1256 bits = FTRACE_HASH_MAX_BITS;
1259 new_hash = alloc_ftrace_hash(bits);
1263 size = 1 << src->size_bits;
1264 for (i = 0; i < size; i++) {
1265 hhd = &src->buckets[i];
1266 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1268 key = hash_long(entry->ip, bits);
1271 remove_hash_entry(src, entry);
1272 __add_hash_entry(new_hash, entry);
1277 rcu_assign_pointer(*dst, new_hash);
1278 free_ftrace_hash_rcu(old_hash);
1283 * Enable regardless of ret:
1284 * On success, we enable the new hash.
1285 * On failure, we re-enable the original hash.
1287 ftrace_hash_rec_enable(ops, enable);
1293 * Test the hashes for this ops to see if we want to call
1294 * the ops->func or not.
1296 * It's a match if the ip is in the ops->filter_hash or
1297 * the filter_hash does not exist or is empty,
1299 * the ip is not in the ops->notrace_hash.
1301 * This needs to be called with preemption disabled as
1302 * the hashes are freed with call_rcu_sched().
1305 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1307 struct ftrace_hash *filter_hash;
1308 struct ftrace_hash *notrace_hash;
1311 filter_hash = rcu_dereference_raw(ops->filter_hash);
1312 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1314 if ((ftrace_hash_empty(filter_hash) ||
1315 ftrace_lookup_ip(filter_hash, ip)) &&
1316 (ftrace_hash_empty(notrace_hash) ||
1317 !ftrace_lookup_ip(notrace_hash, ip)))
1326 * This is a double for. Do not use 'break' to break out of the loop,
1327 * you must use a goto.
1329 #define do_for_each_ftrace_rec(pg, rec) \
1330 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1332 for (_____i = 0; _____i < pg->index; _____i++) { \
1333 rec = &pg->records[_____i];
1335 #define while_for_each_ftrace_rec() \
1339 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1343 struct ftrace_hash *hash;
1344 struct ftrace_hash *other_hash;
1345 struct ftrace_page *pg;
1346 struct dyn_ftrace *rec;
1350 /* Only update if the ops has been registered */
1351 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1355 * In the filter_hash case:
1356 * If the count is zero, we update all records.
1357 * Otherwise we just update the items in the hash.
1359 * In the notrace_hash case:
1360 * We enable the update in the hash.
1361 * As disabling notrace means enabling the tracing,
1362 * and enabling notrace means disabling, the inc variable
1366 hash = ops->filter_hash;
1367 other_hash = ops->notrace_hash;
1368 if (ftrace_hash_empty(hash))
1372 hash = ops->notrace_hash;
1373 other_hash = ops->filter_hash;
1375 * If the notrace hash has no items,
1376 * then there's nothing to do.
1378 if (ftrace_hash_empty(hash))
1382 do_for_each_ftrace_rec(pg, rec) {
1383 int in_other_hash = 0;
1389 * Only the filter_hash affects all records.
1390 * Update if the record is not in the notrace hash.
1392 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1395 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1396 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1401 if (filter_hash && in_hash && !in_other_hash)
1403 else if (!filter_hash && in_hash &&
1404 (in_other_hash || ftrace_hash_empty(other_hash)))
1412 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1415 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1420 /* Shortcut, if we handled all records, we are done. */
1421 if (!all && count == hash->count)
1423 } while_for_each_ftrace_rec();
1426 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1429 __ftrace_hash_rec_update(ops, filter_hash, 0);
1432 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1435 __ftrace_hash_rec_update(ops, filter_hash, 1);
1438 static void ftrace_free_rec(struct dyn_ftrace *rec)
1440 rec->freelist = ftrace_free_records;
1441 ftrace_free_records = rec;
1442 rec->flags |= FTRACE_FL_FREE;
1445 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1447 struct dyn_ftrace *rec;
1449 /* First check for freed records */
1450 if (ftrace_free_records) {
1451 rec = ftrace_free_records;
1453 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
1454 FTRACE_WARN_ON_ONCE(1);
1455 ftrace_free_records = NULL;
1459 ftrace_free_records = rec->freelist;
1460 memset(rec, 0, sizeof(*rec));
1464 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1465 if (!ftrace_pages->next) {
1466 /* allocate another page */
1467 ftrace_pages->next =
1468 (void *)get_zeroed_page(GFP_KERNEL);
1469 if (!ftrace_pages->next)
1472 ftrace_pages = ftrace_pages->next;
1475 return &ftrace_pages->records[ftrace_pages->index++];
1478 static struct dyn_ftrace *
1479 ftrace_record_ip(unsigned long ip)
1481 struct dyn_ftrace *rec;
1483 if (ftrace_disabled)
1486 rec = ftrace_alloc_dyn_node(ip);
1491 rec->newlist = ftrace_new_addrs;
1492 ftrace_new_addrs = rec;
1497 static void print_ip_ins(const char *fmt, unsigned char *p)
1501 printk(KERN_CONT "%s", fmt);
1503 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1504 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1507 static void ftrace_bug(int failed, unsigned long ip)
1511 FTRACE_WARN_ON_ONCE(1);
1512 pr_info("ftrace faulted on modifying ");
1516 FTRACE_WARN_ON_ONCE(1);
1517 pr_info("ftrace failed to modify ");
1519 print_ip_ins(" actual: ", (unsigned char *)ip);
1520 printk(KERN_CONT "\n");
1523 FTRACE_WARN_ON_ONCE(1);
1524 pr_info("ftrace faulted on writing ");
1528 FTRACE_WARN_ON_ONCE(1);
1529 pr_info("ftrace faulted on unknown error ");
1535 /* Return 1 if the address range is reserved for ftrace */
1536 int ftrace_text_reserved(void *start, void *end)
1538 struct dyn_ftrace *rec;
1539 struct ftrace_page *pg;
1541 do_for_each_ftrace_rec(pg, rec) {
1542 if (rec->ip <= (unsigned long)end &&
1543 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1545 } while_for_each_ftrace_rec();
1551 __ftrace_replace_code(struct dyn_ftrace *rec, int update)
1553 unsigned long ftrace_addr;
1554 unsigned long flag = 0UL;
1556 ftrace_addr = (unsigned long)FTRACE_ADDR;
1559 * If we are updating calls:
1561 * If the record has a ref count, then we need to enable it
1562 * because someone is using it.
1564 * Otherwise we make sure its disabled.
1566 * If we are disabling calls, then disable all records that
1569 if (update && (rec->flags & ~FTRACE_FL_MASK))
1570 flag = FTRACE_FL_ENABLED;
1572 /* If the state of this record hasn't changed, then do nothing */
1573 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1577 rec->flags |= FTRACE_FL_ENABLED;
1578 return ftrace_make_call(rec, ftrace_addr);
1581 rec->flags &= ~FTRACE_FL_ENABLED;
1582 return ftrace_make_nop(NULL, rec, ftrace_addr);
1585 static void ftrace_replace_code(int update)
1587 struct dyn_ftrace *rec;
1588 struct ftrace_page *pg;
1591 if (unlikely(ftrace_disabled))
1594 do_for_each_ftrace_rec(pg, rec) {
1595 /* Skip over free records */
1596 if (rec->flags & FTRACE_FL_FREE)
1599 failed = __ftrace_replace_code(rec, update);
1601 ftrace_bug(failed, rec->ip);
1602 /* Stop processing */
1605 } while_for_each_ftrace_rec();
1609 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1616 if (unlikely(ftrace_disabled))
1619 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1621 ftrace_bug(ret, ip);
1628 * archs can override this function if they must do something
1629 * before the modifying code is performed.
1631 int __weak ftrace_arch_code_modify_prepare(void)
1637 * archs can override this function if they must do something
1638 * after the modifying code is performed.
1640 int __weak ftrace_arch_code_modify_post_process(void)
1645 static int __ftrace_modify_code(void *data)
1647 int *command = data;
1650 * Do not call function tracer while we update the code.
1651 * We are in stop machine, no worrying about races.
1653 function_trace_stop++;
1655 if (*command & FTRACE_UPDATE_CALLS)
1656 ftrace_replace_code(1);
1657 else if (*command & FTRACE_DISABLE_CALLS)
1658 ftrace_replace_code(0);
1660 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1661 ftrace_update_ftrace_func(ftrace_trace_function);
1663 if (*command & FTRACE_START_FUNC_RET)
1664 ftrace_enable_ftrace_graph_caller();
1665 else if (*command & FTRACE_STOP_FUNC_RET)
1666 ftrace_disable_ftrace_graph_caller();
1668 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1670 * For archs that call ftrace_test_stop_func(), we must
1671 * wait till after we update all the function callers
1672 * before we update the callback. This keeps different
1673 * ops that record different functions from corrupting
1676 __ftrace_trace_function = __ftrace_trace_function_delay;
1678 function_trace_stop--;
1683 static void ftrace_run_update_code(int command)
1687 ret = ftrace_arch_code_modify_prepare();
1688 FTRACE_WARN_ON(ret);
1692 stop_machine(__ftrace_modify_code, &command, NULL);
1694 ret = ftrace_arch_code_modify_post_process();
1695 FTRACE_WARN_ON(ret);
1698 static ftrace_func_t saved_ftrace_func;
1699 static int ftrace_start_up;
1700 static int global_start_up;
1702 static void ftrace_startup_enable(int command)
1704 if (saved_ftrace_func != ftrace_trace_function) {
1705 saved_ftrace_func = ftrace_trace_function;
1706 command |= FTRACE_UPDATE_TRACE_FUNC;
1709 if (!command || !ftrace_enabled)
1712 ftrace_run_update_code(command);
1715 static int ftrace_startup(struct ftrace_ops *ops, int command)
1717 bool hash_enable = true;
1720 if (unlikely(ftrace_disabled))
1723 ret = __register_ftrace_function(ops);
1728 command |= FTRACE_UPDATE_CALLS;
1730 /* ops marked global share the filter hashes */
1731 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1733 /* Don't update hash if global is already set */
1734 if (global_start_up)
1735 hash_enable = false;
1739 ops->flags |= FTRACE_OPS_FL_ENABLED;
1741 ftrace_hash_rec_enable(ops, 1);
1743 ftrace_startup_enable(command);
1748 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
1750 bool hash_disable = true;
1753 if (unlikely(ftrace_disabled))
1756 ret = __unregister_ftrace_function(ops);
1762 * Just warn in case of unbalance, no need to kill ftrace, it's not
1763 * critical but the ftrace_call callers may be never nopped again after
1764 * further ftrace uses.
1766 WARN_ON_ONCE(ftrace_start_up < 0);
1768 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1771 WARN_ON_ONCE(global_start_up < 0);
1772 /* Don't update hash if global still has users */
1773 if (global_start_up) {
1774 WARN_ON_ONCE(!ftrace_start_up);
1775 hash_disable = false;
1780 ftrace_hash_rec_disable(ops, 1);
1782 if (ops != &global_ops || !global_start_up)
1783 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1785 command |= FTRACE_UPDATE_CALLS;
1787 if (saved_ftrace_func != ftrace_trace_function) {
1788 saved_ftrace_func = ftrace_trace_function;
1789 command |= FTRACE_UPDATE_TRACE_FUNC;
1792 if (!command || !ftrace_enabled)
1795 ftrace_run_update_code(command);
1798 * Dynamic ops may be freed, we must make sure that all
1799 * callers are done before leaving this function.
1800 * The same goes for freeing the per_cpu data of the control
1803 * Again, normal synchronize_sched() is not good enough.
1804 * We need to do a hard force of sched synchronization.
1805 * This is because we use preempt_disable() to do RCU, but
1806 * the function tracers can be called where RCU is not watching
1807 * (like before user_exit()). We can not rely on the RCU
1808 * infrastructure to do the synchronization, thus we must do it
1811 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC))
1812 schedule_on_each_cpu(ftrace_sync);
1817 static void ftrace_startup_sysctl(void)
1821 if (unlikely(ftrace_disabled))
1824 /* Force update next time */
1825 saved_ftrace_func = NULL;
1826 /* ftrace_start_up is true if we want ftrace running */
1827 if (ftrace_start_up) {
1828 command = FTRACE_UPDATE_CALLS;
1829 if (ftrace_graph_active)
1830 command |= FTRACE_START_FUNC_RET;
1831 ftrace_startup_enable(command);
1835 static void ftrace_shutdown_sysctl(void)
1839 if (unlikely(ftrace_disabled))
1842 /* ftrace_start_up is true if ftrace is running */
1843 if (ftrace_start_up) {
1844 command = FTRACE_DISABLE_CALLS;
1845 if (ftrace_graph_active)
1846 command |= FTRACE_STOP_FUNC_RET;
1847 ftrace_run_update_code(command);
1851 static cycle_t ftrace_update_time;
1852 static unsigned long ftrace_update_cnt;
1853 unsigned long ftrace_update_tot_cnt;
1855 static inline int ops_traces_mod(struct ftrace_ops *ops)
1858 * Filter_hash being empty will default to trace module.
1859 * But notrace hash requires a test of individual module functions.
1861 return ftrace_hash_empty(ops->filter_hash) &&
1862 ftrace_hash_empty(ops->notrace_hash);
1866 * Check if the current ops references the record.
1868 * If the ops traces all functions, then it was already accounted for.
1869 * If the ops does not trace the current record function, skip it.
1870 * If the ops ignores the function via notrace filter, skip it.
1873 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
1875 /* If ops isn't enabled, ignore it */
1876 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1879 /* If ops traces all mods, we already accounted for it */
1880 if (ops_traces_mod(ops))
1883 /* The function must be in the filter */
1884 if (!ftrace_hash_empty(ops->filter_hash) &&
1885 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
1888 /* If in notrace hash, we ignore it too */
1889 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
1895 static int referenced_filters(struct dyn_ftrace *rec)
1897 struct ftrace_ops *ops;
1900 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
1901 if (ops_references_rec(ops, rec))
1908 static int ftrace_update_code(struct module *mod)
1910 struct dyn_ftrace *p;
1911 cycle_t start, stop;
1912 unsigned long ref = 0;
1916 * When adding a module, we need to check if tracers are
1917 * currently enabled and if they are set to trace all functions.
1918 * If they are, we need to enable the module functions as well
1919 * as update the reference counts for those function records.
1922 struct ftrace_ops *ops;
1924 for (ops = ftrace_ops_list;
1925 ops != &ftrace_list_end; ops = ops->next) {
1926 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
1927 if (ops_traces_mod(ops))
1935 start = ftrace_now(raw_smp_processor_id());
1936 ftrace_update_cnt = 0;
1938 while (ftrace_new_addrs) {
1941 /* If something went wrong, bail without enabling anything */
1942 if (unlikely(ftrace_disabled))
1945 p = ftrace_new_addrs;
1946 ftrace_new_addrs = p->newlist;
1948 cnt += referenced_filters(p);
1952 * Do the initial record conversion from mcount jump
1953 * to the NOP instructions.
1955 if (!ftrace_code_disable(mod, p)) {
1961 ftrace_update_cnt++;
1964 * If the tracing is enabled, go ahead and enable the record.
1966 * The reason not to enable the record immediatelly is the
1967 * inherent check of ftrace_make_nop/ftrace_make_call for
1968 * correct previous instructions. Making first the NOP
1969 * conversion puts the module to the correct state, thus
1970 * passing the ftrace_make_call check.
1972 if (ftrace_start_up && cnt) {
1973 int failed = __ftrace_replace_code(p, 1);
1975 ftrace_bug(failed, p->ip);
1981 stop = ftrace_now(raw_smp_processor_id());
1982 ftrace_update_time = stop - start;
1983 ftrace_update_tot_cnt += ftrace_update_cnt;
1988 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1990 struct ftrace_page *pg;
1994 /* allocate a few pages */
1995 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1996 if (!ftrace_pages_start)
2000 * Allocate a few more pages.
2002 * TODO: have some parser search vmlinux before
2003 * final linking to find all calls to ftrace.
2005 * a) know how many pages to allocate.
2007 * b) set up the table then.
2009 * The dynamic code is still necessary for
2013 pg = ftrace_pages = ftrace_pages_start;
2015 cnt = num_to_init / ENTRIES_PER_PAGE;
2016 pr_info("ftrace: allocating %ld entries in %d pages\n",
2017 num_to_init, cnt + 1);
2019 for (i = 0; i < cnt; i++) {
2020 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
2022 /* If we fail, we'll try later anyway */
2033 FTRACE_ITER_FILTER = (1 << 0),
2034 FTRACE_ITER_NOTRACE = (1 << 1),
2035 FTRACE_ITER_PRINTALL = (1 << 2),
2036 FTRACE_ITER_HASH = (1 << 3),
2037 FTRACE_ITER_ENABLED = (1 << 4),
2040 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2042 struct ftrace_iterator {
2045 struct ftrace_page *pg;
2046 struct dyn_ftrace *func;
2047 struct ftrace_func_probe *probe;
2048 struct trace_parser parser;
2049 struct ftrace_hash *hash;
2050 struct ftrace_ops *ops;
2057 t_hash_next(struct seq_file *m, loff_t *pos)
2059 struct ftrace_iterator *iter = m->private;
2060 struct hlist_node *hnd = NULL;
2061 struct hlist_head *hhd;
2067 hnd = &iter->probe->node;
2069 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2072 hhd = &ftrace_func_hash[iter->hidx];
2074 if (hlist_empty(hhd)) {
2090 if (WARN_ON_ONCE(!hnd))
2093 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2098 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2100 struct ftrace_iterator *iter = m->private;
2104 if (iter->func_pos > *pos)
2108 for (l = 0; l <= (*pos - iter->func_pos); ) {
2109 p = t_hash_next(m, &l);
2116 /* Only set this if we have an item */
2117 iter->flags |= FTRACE_ITER_HASH;
2123 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2125 struct ftrace_func_probe *rec;
2128 if (WARN_ON_ONCE(!rec))
2131 if (rec->ops->print)
2132 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2134 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2137 seq_printf(m, ":%p", rec->data);
2144 t_next(struct seq_file *m, void *v, loff_t *pos)
2146 struct ftrace_iterator *iter = m->private;
2147 struct ftrace_ops *ops = &global_ops;
2148 struct dyn_ftrace *rec = NULL;
2150 if (unlikely(ftrace_disabled))
2153 if (iter->flags & FTRACE_ITER_HASH)
2154 return t_hash_next(m, pos);
2157 iter->pos = iter->func_pos = *pos;
2159 if (iter->flags & FTRACE_ITER_PRINTALL)
2160 return t_hash_start(m, pos);
2163 if (iter->idx >= iter->pg->index) {
2164 if (iter->pg->next) {
2165 iter->pg = iter->pg->next;
2170 rec = &iter->pg->records[iter->idx++];
2171 if ((rec->flags & FTRACE_FL_FREE) ||
2173 ((iter->flags & FTRACE_ITER_FILTER) &&
2174 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2176 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2177 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2179 ((iter->flags & FTRACE_ITER_ENABLED) &&
2180 !(rec->flags & ~FTRACE_FL_MASK))) {
2188 return t_hash_start(m, pos);
2195 static void reset_iter_read(struct ftrace_iterator *iter)
2199 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2202 static void *t_start(struct seq_file *m, loff_t *pos)
2204 struct ftrace_iterator *iter = m->private;
2205 struct ftrace_ops *ops = &global_ops;
2209 mutex_lock(&ftrace_lock);
2211 if (unlikely(ftrace_disabled))
2215 * If an lseek was done, then reset and start from beginning.
2217 if (*pos < iter->pos)
2218 reset_iter_read(iter);
2221 * For set_ftrace_filter reading, if we have the filter
2222 * off, we can short cut and just print out that all
2223 * functions are enabled.
2225 if (iter->flags & FTRACE_ITER_FILTER &&
2226 ftrace_hash_empty(ops->filter_hash)) {
2228 return t_hash_start(m, pos);
2229 iter->flags |= FTRACE_ITER_PRINTALL;
2230 /* reset in case of seek/pread */
2231 iter->flags &= ~FTRACE_ITER_HASH;
2235 if (iter->flags & FTRACE_ITER_HASH)
2236 return t_hash_start(m, pos);
2239 * Unfortunately, we need to restart at ftrace_pages_start
2240 * every time we let go of the ftrace_mutex. This is because
2241 * those pointers can change without the lock.
2243 iter->pg = ftrace_pages_start;
2245 for (l = 0; l <= *pos; ) {
2246 p = t_next(m, p, &l);
2252 if (iter->flags & FTRACE_ITER_FILTER)
2253 return t_hash_start(m, pos);
2261 static void t_stop(struct seq_file *m, void *p)
2263 mutex_unlock(&ftrace_lock);
2266 static int t_show(struct seq_file *m, void *v)
2268 struct ftrace_iterator *iter = m->private;
2269 struct dyn_ftrace *rec;
2271 if (iter->flags & FTRACE_ITER_HASH)
2272 return t_hash_show(m, iter);
2274 if (iter->flags & FTRACE_ITER_PRINTALL) {
2275 seq_printf(m, "#### all functions enabled ####\n");
2284 seq_printf(m, "%ps", (void *)rec->ip);
2285 if (iter->flags & FTRACE_ITER_ENABLED)
2286 seq_printf(m, " (%ld)",
2287 rec->flags & ~FTRACE_FL_MASK);
2288 seq_printf(m, "\n");
2293 static const struct seq_operations show_ftrace_seq_ops = {
2301 ftrace_avail_open(struct inode *inode, struct file *file)
2303 struct ftrace_iterator *iter;
2306 if (unlikely(ftrace_disabled))
2309 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2313 iter->pg = ftrace_pages_start;
2315 ret = seq_open(file, &show_ftrace_seq_ops);
2317 struct seq_file *m = file->private_data;
2328 ftrace_enabled_open(struct inode *inode, struct file *file)
2330 struct ftrace_iterator *iter;
2333 if (unlikely(ftrace_disabled))
2336 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2340 iter->pg = ftrace_pages_start;
2341 iter->flags = FTRACE_ITER_ENABLED;
2343 ret = seq_open(file, &show_ftrace_seq_ops);
2345 struct seq_file *m = file->private_data;
2355 static void ftrace_filter_reset(struct ftrace_hash *hash)
2357 mutex_lock(&ftrace_lock);
2358 ftrace_hash_clear(hash);
2359 mutex_unlock(&ftrace_lock);
2363 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2364 struct inode *inode, struct file *file)
2366 struct ftrace_iterator *iter;
2367 struct ftrace_hash *hash;
2370 if (unlikely(ftrace_disabled))
2373 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2377 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2382 if (flag & FTRACE_ITER_NOTRACE)
2383 hash = ops->notrace_hash;
2385 hash = ops->filter_hash;
2390 if (file->f_mode & FMODE_WRITE) {
2391 mutex_lock(&ftrace_lock);
2392 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2393 mutex_unlock(&ftrace_lock);
2396 trace_parser_put(&iter->parser);
2402 mutex_lock(&ftrace_regex_lock);
2404 if ((file->f_mode & FMODE_WRITE) &&
2405 (file->f_flags & O_TRUNC))
2406 ftrace_filter_reset(iter->hash);
2408 if (file->f_mode & FMODE_READ) {
2409 iter->pg = ftrace_pages_start;
2411 ret = seq_open(file, &show_ftrace_seq_ops);
2413 struct seq_file *m = file->private_data;
2417 free_ftrace_hash(iter->hash);
2418 trace_parser_put(&iter->parser);
2422 file->private_data = iter;
2423 mutex_unlock(&ftrace_regex_lock);
2429 ftrace_filter_open(struct inode *inode, struct file *file)
2431 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
2436 ftrace_notrace_open(struct inode *inode, struct file *file)
2438 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2442 static int ftrace_match(char *str, char *regex, int len, int type)
2449 if (strcmp(str, regex) == 0)
2452 case MATCH_FRONT_ONLY:
2453 if (strncmp(str, regex, len) == 0)
2456 case MATCH_MIDDLE_ONLY:
2457 if (strstr(str, regex))
2460 case MATCH_END_ONLY:
2462 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2471 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2473 struct ftrace_func_entry *entry;
2476 entry = ftrace_lookup_ip(hash, rec->ip);
2478 /* Do nothing if it doesn't exist */
2482 free_hash_entry(hash, entry);
2484 /* Do nothing if it exists */
2488 ret = add_hash_entry(hash, rec->ip);
2494 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2495 char *regex, int len, int type)
2497 char str[KSYM_SYMBOL_LEN];
2500 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2503 /* module lookup requires matching the module */
2504 if (!modname || strcmp(modname, mod))
2507 /* blank search means to match all funcs in the mod */
2512 return ftrace_match(str, regex, len, type);
2516 match_records(struct ftrace_hash *hash, char *buff,
2517 int len, char *mod, int not)
2519 unsigned search_len = 0;
2520 struct ftrace_page *pg;
2521 struct dyn_ftrace *rec;
2522 int type = MATCH_FULL;
2523 char *search = buff;
2528 type = filter_parse_regex(buff, len, &search, ¬);
2529 search_len = strlen(search);
2532 mutex_lock(&ftrace_lock);
2534 if (unlikely(ftrace_disabled))
2537 do_for_each_ftrace_rec(pg, rec) {
2539 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2540 ret = enter_record(hash, rec, not);
2547 } while_for_each_ftrace_rec();
2549 mutex_unlock(&ftrace_lock);
2555 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2557 return match_records(hash, buff, len, NULL, 0);
2561 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2565 /* blank or '*' mean the same */
2566 if (strcmp(buff, "*") == 0)
2569 /* handle the case of 'dont filter this module' */
2570 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2575 return match_records(hash, buff, strlen(buff), mod, not);
2579 * We register the module command as a template to show others how
2580 * to register the a command as well.
2584 ftrace_mod_callback(struct ftrace_hash *hash,
2585 char *func, char *cmd, char *param, int enable)
2591 * cmd == 'mod' because we only registered this func
2592 * for the 'mod' ftrace_func_command.
2593 * But if you register one func with multiple commands,
2594 * you can tell which command was used by the cmd
2598 /* we must have a module name */
2602 mod = strsep(¶m, ":");
2606 ret = ftrace_match_module_records(hash, func, mod);
2615 static struct ftrace_func_command ftrace_mod_cmd = {
2617 .func = ftrace_mod_callback,
2620 static int __init ftrace_mod_cmd_init(void)
2622 return register_ftrace_command(&ftrace_mod_cmd);
2624 device_initcall(ftrace_mod_cmd_init);
2627 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2629 struct ftrace_func_probe *entry;
2630 struct hlist_head *hhd;
2631 struct hlist_node *n;
2634 key = hash_long(ip, FTRACE_HASH_BITS);
2636 hhd = &ftrace_func_hash[key];
2638 if (hlist_empty(hhd))
2642 * Disable preemption for these calls to prevent a RCU grace
2643 * period. This syncs the hash iteration and freeing of items
2644 * on the hash. rcu_read_lock is too dangerous here.
2646 preempt_disable_notrace();
2647 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2648 if (entry->ip == ip)
2649 entry->ops->func(ip, parent_ip, &entry->data);
2651 preempt_enable_notrace();
2654 static struct ftrace_ops trace_probe_ops __read_mostly =
2656 .func = function_trace_probe_call,
2659 static int ftrace_probe_registered;
2661 static void __enable_ftrace_function_probe(void)
2666 if (ftrace_probe_registered)
2669 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2670 struct hlist_head *hhd = &ftrace_func_hash[i];
2674 /* Nothing registered? */
2675 if (i == FTRACE_FUNC_HASHSIZE)
2678 ret = ftrace_startup(&trace_probe_ops, 0);
2680 ftrace_probe_registered = 1;
2683 static void __disable_ftrace_function_probe(void)
2687 if (!ftrace_probe_registered)
2690 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2691 struct hlist_head *hhd = &ftrace_func_hash[i];
2696 /* no more funcs left */
2697 ftrace_shutdown(&trace_probe_ops, 0);
2699 ftrace_probe_registered = 0;
2703 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2705 struct ftrace_func_probe *entry =
2706 container_of(rhp, struct ftrace_func_probe, rcu);
2708 if (entry->ops->free)
2709 entry->ops->free(&entry->data);
2715 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2718 struct ftrace_func_probe *entry;
2719 struct ftrace_page *pg;
2720 struct dyn_ftrace *rec;
2726 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2727 len = strlen(search);
2729 /* we do not support '!' for function probes */
2733 mutex_lock(&ftrace_lock);
2735 if (unlikely(ftrace_disabled))
2738 do_for_each_ftrace_rec(pg, rec) {
2740 if (!ftrace_match_record(rec, NULL, search, len, type))
2743 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2745 /* If we did not process any, then return error */
2756 * The caller might want to do something special
2757 * for each function we find. We call the callback
2758 * to give the caller an opportunity to do so.
2760 if (ops->callback) {
2761 if (ops->callback(rec->ip, &entry->data) < 0) {
2762 /* caller does not like this func */
2769 entry->ip = rec->ip;
2771 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2772 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2774 } while_for_each_ftrace_rec();
2775 __enable_ftrace_function_probe();
2778 mutex_unlock(&ftrace_lock);
2784 PROBE_TEST_FUNC = 1,
2789 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2790 void *data, int flags)
2792 struct ftrace_func_probe *entry;
2793 struct hlist_node *n, *tmp;
2794 char str[KSYM_SYMBOL_LEN];
2795 int type = MATCH_FULL;
2799 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2804 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2805 len = strlen(search);
2807 /* we do not support '!' for function probes */
2812 mutex_lock(&ftrace_lock);
2813 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2814 struct hlist_head *hhd = &ftrace_func_hash[i];
2816 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2818 /* break up if statements for readability */
2819 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2822 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2825 /* do this last, since it is the most expensive */
2827 kallsyms_lookup(entry->ip, NULL, NULL,
2829 if (!ftrace_match(str, glob, len, type))
2833 hlist_del_rcu(&entry->node);
2834 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
2837 __disable_ftrace_function_probe();
2838 mutex_unlock(&ftrace_lock);
2842 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2845 __unregister_ftrace_function_probe(glob, ops, data,
2846 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2850 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2852 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2855 void unregister_ftrace_function_probe_all(char *glob)
2857 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2860 static LIST_HEAD(ftrace_commands);
2861 static DEFINE_MUTEX(ftrace_cmd_mutex);
2863 int register_ftrace_command(struct ftrace_func_command *cmd)
2865 struct ftrace_func_command *p;
2868 mutex_lock(&ftrace_cmd_mutex);
2869 list_for_each_entry(p, &ftrace_commands, list) {
2870 if (strcmp(cmd->name, p->name) == 0) {
2875 list_add(&cmd->list, &ftrace_commands);
2877 mutex_unlock(&ftrace_cmd_mutex);
2882 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2884 struct ftrace_func_command *p, *n;
2887 mutex_lock(&ftrace_cmd_mutex);
2888 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2889 if (strcmp(cmd->name, p->name) == 0) {
2891 list_del_init(&p->list);
2896 mutex_unlock(&ftrace_cmd_mutex);
2901 static int ftrace_process_regex(struct ftrace_hash *hash,
2902 char *buff, int len, int enable)
2904 char *func, *command, *next = buff;
2905 struct ftrace_func_command *p;
2908 func = strsep(&next, ":");
2911 ret = ftrace_match_records(hash, func, len);
2921 command = strsep(&next, ":");
2923 mutex_lock(&ftrace_cmd_mutex);
2924 list_for_each_entry(p, &ftrace_commands, list) {
2925 if (strcmp(p->name, command) == 0) {
2926 ret = p->func(hash, func, command, next, enable);
2931 mutex_unlock(&ftrace_cmd_mutex);
2937 ftrace_regex_write(struct file *file, const char __user *ubuf,
2938 size_t cnt, loff_t *ppos, int enable)
2940 struct ftrace_iterator *iter;
2941 struct trace_parser *parser;
2947 mutex_lock(&ftrace_regex_lock);
2950 if (unlikely(ftrace_disabled))
2953 if (file->f_mode & FMODE_READ) {
2954 struct seq_file *m = file->private_data;
2957 iter = file->private_data;
2959 parser = &iter->parser;
2960 read = trace_get_user(parser, ubuf, cnt, ppos);
2962 if (read >= 0 && trace_parser_loaded(parser) &&
2963 !trace_parser_cont(parser)) {
2964 ret = ftrace_process_regex(iter->hash, parser->buffer,
2965 parser->idx, enable);
2966 trace_parser_clear(parser);
2973 mutex_unlock(&ftrace_regex_lock);
2979 ftrace_filter_write(struct file *file, const char __user *ubuf,
2980 size_t cnt, loff_t *ppos)
2982 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2986 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2987 size_t cnt, loff_t *ppos)
2989 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2993 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2994 int reset, int enable)
2996 struct ftrace_hash **orig_hash;
2997 struct ftrace_hash *hash;
3000 /* All global ops uses the global ops filters */
3001 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3004 if (unlikely(ftrace_disabled))
3008 orig_hash = &ops->filter_hash;
3010 orig_hash = &ops->notrace_hash;
3012 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3016 mutex_lock(&ftrace_regex_lock);
3018 ftrace_filter_reset(hash);
3020 ftrace_match_records(hash, buf, len);
3022 mutex_lock(&ftrace_lock);
3023 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3024 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3026 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3028 mutex_unlock(&ftrace_lock);
3030 mutex_unlock(&ftrace_regex_lock);
3032 free_ftrace_hash(hash);
3037 * ftrace_set_filter - set a function to filter on in ftrace
3038 * @ops - the ops to set the filter with
3039 * @buf - the string that holds the function filter text.
3040 * @len - the length of the string.
3041 * @reset - non zero to reset all filters before applying this filter.
3043 * Filters denote which functions should be enabled when tracing is enabled.
3044 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3046 void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3049 ftrace_set_regex(ops, buf, len, reset, 1);
3051 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3054 * ftrace_set_notrace - set a function to not trace in ftrace
3055 * @ops - the ops to set the notrace filter with
3056 * @buf - the string that holds the function notrace text.
3057 * @len - the length of the string.
3058 * @reset - non zero to reset all filters before applying this filter.
3060 * Notrace Filters denote which functions should not be enabled when tracing
3061 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3064 void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3067 ftrace_set_regex(ops, buf, len, reset, 0);
3069 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3071 * ftrace_set_filter - set a function to filter on in ftrace
3072 * @ops - the ops to set the filter with
3073 * @buf - the string that holds the function filter text.
3074 * @len - the length of the string.
3075 * @reset - non zero to reset all filters before applying this filter.
3077 * Filters denote which functions should be enabled when tracing is enabled.
3078 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3080 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3082 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3084 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3087 * ftrace_set_notrace - set a function to not trace in ftrace
3088 * @ops - the ops to set the notrace filter with
3089 * @buf - the string that holds the function notrace text.
3090 * @len - the length of the string.
3091 * @reset - non zero to reset all filters before applying this filter.
3093 * Notrace Filters denote which functions should not be enabled when tracing
3094 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3097 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3099 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3101 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3104 * command line interface to allow users to set filters on boot up.
3106 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3107 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3108 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3110 static int __init set_ftrace_notrace(char *str)
3112 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3115 __setup("ftrace_notrace=", set_ftrace_notrace);
3117 static int __init set_ftrace_filter(char *str)
3119 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3122 __setup("ftrace_filter=", set_ftrace_filter);
3124 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3125 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3126 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3128 static int __init set_graph_function(char *str)
3130 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3133 __setup("ftrace_graph_filter=", set_graph_function);
3135 static void __init set_ftrace_early_graph(char *buf)
3141 func = strsep(&buf, ",");
3142 /* we allow only one expression at a time */
3143 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3146 printk(KERN_DEBUG "ftrace: function %s not "
3147 "traceable\n", func);
3150 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3153 set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3158 func = strsep(&buf, ",");
3159 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3163 static void __init set_ftrace_early_filters(void)
3165 if (ftrace_filter_buf[0])
3166 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
3167 if (ftrace_notrace_buf[0])
3168 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
3169 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3170 if (ftrace_graph_buf[0])
3171 set_ftrace_early_graph(ftrace_graph_buf);
3172 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3176 ftrace_regex_release(struct inode *inode, struct file *file)
3178 struct seq_file *m = (struct seq_file *)file->private_data;
3179 struct ftrace_iterator *iter;
3180 struct ftrace_hash **orig_hash;
3181 struct trace_parser *parser;
3185 mutex_lock(&ftrace_regex_lock);
3186 if (file->f_mode & FMODE_READ) {
3189 seq_release(inode, file);
3191 iter = file->private_data;
3193 parser = &iter->parser;
3194 if (trace_parser_loaded(parser)) {
3195 parser->buffer[parser->idx] = 0;
3196 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3199 trace_parser_put(parser);
3201 if (file->f_mode & FMODE_WRITE) {
3202 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3205 orig_hash = &iter->ops->filter_hash;
3207 orig_hash = &iter->ops->notrace_hash;
3209 mutex_lock(&ftrace_lock);
3210 ret = ftrace_hash_move(iter->ops, filter_hash,
3211 orig_hash, iter->hash);
3212 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3214 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3216 mutex_unlock(&ftrace_lock);
3218 free_ftrace_hash(iter->hash);
3221 mutex_unlock(&ftrace_regex_lock);
3225 static const struct file_operations ftrace_avail_fops = {
3226 .open = ftrace_avail_open,
3228 .llseek = seq_lseek,
3229 .release = seq_release_private,
3232 static const struct file_operations ftrace_enabled_fops = {
3233 .open = ftrace_enabled_open,
3235 .llseek = seq_lseek,
3236 .release = seq_release_private,
3239 static const struct file_operations ftrace_filter_fops = {
3240 .open = ftrace_filter_open,
3242 .write = ftrace_filter_write,
3243 .llseek = ftrace_filter_lseek,
3244 .release = ftrace_regex_release,
3247 static const struct file_operations ftrace_notrace_fops = {
3248 .open = ftrace_notrace_open,
3250 .write = ftrace_notrace_write,
3251 .llseek = ftrace_filter_lseek,
3252 .release = ftrace_regex_release,
3255 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3257 static DEFINE_MUTEX(graph_lock);
3259 int ftrace_graph_count;
3260 int ftrace_graph_filter_enabled;
3261 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3264 __g_next(struct seq_file *m, loff_t *pos)
3266 if (*pos >= ftrace_graph_count)
3268 return &ftrace_graph_funcs[*pos];
3272 g_next(struct seq_file *m, void *v, loff_t *pos)
3275 return __g_next(m, pos);
3278 static void *g_start(struct seq_file *m, loff_t *pos)
3280 mutex_lock(&graph_lock);
3282 /* Nothing, tell g_show to print all functions are enabled */
3283 if (!ftrace_graph_filter_enabled && !*pos)
3286 return __g_next(m, pos);
3289 static void g_stop(struct seq_file *m, void *p)
3291 mutex_unlock(&graph_lock);
3294 static int g_show(struct seq_file *m, void *v)
3296 unsigned long *ptr = v;
3301 if (ptr == (unsigned long *)1) {
3302 seq_printf(m, "#### all functions enabled ####\n");
3306 seq_printf(m, "%ps\n", (void *)*ptr);
3311 static const struct seq_operations ftrace_graph_seq_ops = {
3319 ftrace_graph_open(struct inode *inode, struct file *file)
3323 if (unlikely(ftrace_disabled))
3326 mutex_lock(&graph_lock);
3327 if ((file->f_mode & FMODE_WRITE) &&
3328 (file->f_flags & O_TRUNC)) {
3329 ftrace_graph_filter_enabled = 0;
3330 ftrace_graph_count = 0;
3331 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3333 mutex_unlock(&graph_lock);
3335 if (file->f_mode & FMODE_READ)
3336 ret = seq_open(file, &ftrace_graph_seq_ops);
3342 ftrace_graph_release(struct inode *inode, struct file *file)
3344 if (file->f_mode & FMODE_READ)
3345 seq_release(inode, file);
3350 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3352 struct dyn_ftrace *rec;
3353 struct ftrace_page *pg;
3362 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3363 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3366 search_len = strlen(search);
3368 mutex_lock(&ftrace_lock);
3370 if (unlikely(ftrace_disabled)) {
3371 mutex_unlock(&ftrace_lock);
3375 do_for_each_ftrace_rec(pg, rec) {
3377 if (rec->flags & FTRACE_FL_FREE)
3380 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3381 /* if it is in the array */
3383 for (i = 0; i < *idx; i++) {
3384 if (array[i] == rec->ip) {
3393 array[(*idx)++] = rec->ip;
3394 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3399 array[i] = array[--(*idx)];
3405 } while_for_each_ftrace_rec();
3407 mutex_unlock(&ftrace_lock);
3412 ftrace_graph_filter_enabled = !!(*idx);
3418 ftrace_graph_write(struct file *file, const char __user *ubuf,
3419 size_t cnt, loff_t *ppos)
3421 struct trace_parser parser;
3427 mutex_lock(&graph_lock);
3429 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3434 read = trace_get_user(&parser, ubuf, cnt, ppos);
3436 if (read >= 0 && trace_parser_loaded((&parser))) {
3437 parser.buffer[parser.idx] = 0;
3439 /* we allow only one expression at a time */
3440 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3449 trace_parser_put(&parser);
3451 mutex_unlock(&graph_lock);
3456 static const struct file_operations ftrace_graph_fops = {
3457 .open = ftrace_graph_open,
3459 .write = ftrace_graph_write,
3460 .llseek = ftrace_filter_lseek,
3461 .release = ftrace_graph_release,
3463 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3465 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3468 trace_create_file("available_filter_functions", 0444,
3469 d_tracer, NULL, &ftrace_avail_fops);
3471 trace_create_file("enabled_functions", 0444,
3472 d_tracer, NULL, &ftrace_enabled_fops);
3474 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3475 NULL, &ftrace_filter_fops);
3477 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3478 NULL, &ftrace_notrace_fops);
3480 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3481 trace_create_file("set_graph_function", 0444, d_tracer,
3483 &ftrace_graph_fops);
3484 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3489 static int ftrace_process_locs(struct module *mod,
3490 unsigned long *start,
3495 unsigned long flags = 0; /* Shut up gcc */
3497 mutex_lock(&ftrace_lock);
3500 addr = ftrace_call_adjust(*p++);
3502 * Some architecture linkers will pad between
3503 * the different mcount_loc sections of different
3504 * object files to satisfy alignments.
3505 * Skip any NULL pointers.
3509 ftrace_record_ip(addr);
3513 * We only need to disable interrupts on start up
3514 * because we are modifying code that an interrupt
3515 * may execute, and the modification is not atomic.
3516 * But for modules, nothing runs the code we modify
3517 * until we are finished with it, and there's no
3518 * reason to cause large interrupt latencies while we do it.
3521 local_irq_save(flags);
3522 ftrace_update_code(mod);
3524 local_irq_restore(flags);
3525 mutex_unlock(&ftrace_lock);
3530 #ifdef CONFIG_MODULES
3531 void ftrace_release_mod(struct module *mod)
3533 struct dyn_ftrace *rec;
3534 struct ftrace_page *pg;
3536 mutex_lock(&ftrace_lock);
3538 if (ftrace_disabled)
3541 do_for_each_ftrace_rec(pg, rec) {
3542 if (within_module_core(rec->ip, mod)) {
3544 * rec->ip is changed in ftrace_free_rec()
3545 * It should not between s and e if record was freed.
3547 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3548 ftrace_free_rec(rec);
3550 } while_for_each_ftrace_rec();
3552 mutex_unlock(&ftrace_lock);
3555 static void ftrace_init_module(struct module *mod,
3556 unsigned long *start, unsigned long *end)
3558 if (ftrace_disabled || start == end)
3560 ftrace_process_locs(mod, start, end);
3563 void ftrace_module_init(struct module *mod)
3565 ftrace_init_module(mod, mod->ftrace_callsites,
3566 mod->ftrace_callsites +
3567 mod->num_ftrace_callsites);
3570 static int ftrace_module_notify_exit(struct notifier_block *self,
3571 unsigned long val, void *data)
3573 struct module *mod = data;
3575 if (val == MODULE_STATE_GOING)
3576 ftrace_release_mod(mod);
3581 static int ftrace_module_notify_exit(struct notifier_block *self,
3582 unsigned long val, void *data)
3586 #endif /* CONFIG_MODULES */
3588 struct notifier_block ftrace_module_exit_nb = {
3589 .notifier_call = ftrace_module_notify_exit,
3590 .priority = INT_MIN, /* Run after anything that can remove kprobes */
3593 extern unsigned long __start_mcount_loc[];
3594 extern unsigned long __stop_mcount_loc[];
3596 void __init ftrace_init(void)
3598 unsigned long count, addr, flags;
3601 /* Keep the ftrace pointer to the stub */
3602 addr = (unsigned long)ftrace_stub;
3604 local_irq_save(flags);
3605 ftrace_dyn_arch_init(&addr);
3606 local_irq_restore(flags);
3608 /* ftrace_dyn_arch_init places the return code in addr */
3612 count = __stop_mcount_loc - __start_mcount_loc;
3614 ret = ftrace_dyn_table_alloc(count);
3618 last_ftrace_enabled = ftrace_enabled = 1;
3620 ret = ftrace_process_locs(NULL,
3624 ret = register_module_notifier(&ftrace_module_exit_nb);
3626 pr_warning("Failed to register trace ftrace module exit notifier\n");
3628 set_ftrace_early_filters();
3632 ftrace_disabled = 1;
3637 static struct ftrace_ops global_ops = {
3638 .func = ftrace_stub,
3641 static int __init ftrace_nodyn_init(void)
3646 device_initcall(ftrace_nodyn_init);
3648 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3649 static inline void ftrace_startup_enable(int command) { }
3650 /* Keep as macros so we do not need to define the commands */
3651 # define ftrace_startup(ops, command) \
3653 int ___ret = __register_ftrace_function(ops); \
3655 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3658 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
3660 # define ftrace_startup_sysctl() do { } while (0)
3661 # define ftrace_shutdown_sysctl() do { } while (0)
3664 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3669 #endif /* CONFIG_DYNAMIC_FTRACE */
3672 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3674 struct ftrace_ops *op;
3676 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3679 trace_recursion_set(TRACE_INTERNAL_BIT);
3681 * Some of the ops may be dynamically allocated,
3682 * they must be freed after a synchronize_sched().
3684 preempt_disable_notrace();
3685 op = rcu_dereference_raw(ftrace_ops_list);
3686 while (op != &ftrace_list_end) {
3687 if (ftrace_ops_test(op, ip))
3688 op->func(ip, parent_ip);
3689 op = rcu_dereference_raw(op->next);
3691 preempt_enable_notrace();
3692 trace_recursion_clear(TRACE_INTERNAL_BIT);
3695 static void clear_ftrace_swapper(void)
3697 struct task_struct *p;
3701 for_each_online_cpu(cpu) {
3703 clear_tsk_trace_trace(p);
3708 static void set_ftrace_swapper(void)
3710 struct task_struct *p;
3714 for_each_online_cpu(cpu) {
3716 set_tsk_trace_trace(p);
3721 static void clear_ftrace_pid(struct pid *pid)
3723 struct task_struct *p;
3726 do_each_pid_task(pid, PIDTYPE_PID, p) {
3727 clear_tsk_trace_trace(p);
3728 } while_each_pid_task(pid, PIDTYPE_PID, p);
3734 static void set_ftrace_pid(struct pid *pid)
3736 struct task_struct *p;
3739 do_each_pid_task(pid, PIDTYPE_PID, p) {
3740 set_tsk_trace_trace(p);
3741 } while_each_pid_task(pid, PIDTYPE_PID, p);
3745 static void clear_ftrace_pid_task(struct pid *pid)
3747 if (pid == ftrace_swapper_pid)
3748 clear_ftrace_swapper();
3750 clear_ftrace_pid(pid);
3753 static void set_ftrace_pid_task(struct pid *pid)
3755 if (pid == ftrace_swapper_pid)
3756 set_ftrace_swapper();
3758 set_ftrace_pid(pid);
3761 static int ftrace_pid_add(int p)
3764 struct ftrace_pid *fpid;
3767 mutex_lock(&ftrace_lock);
3770 pid = ftrace_swapper_pid;
3772 pid = find_get_pid(p);
3779 list_for_each_entry(fpid, &ftrace_pids, list)
3780 if (fpid->pid == pid)
3785 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3789 list_add(&fpid->list, &ftrace_pids);
3792 set_ftrace_pid_task(pid);
3794 ftrace_update_pid_func();
3795 ftrace_startup_enable(0);
3797 mutex_unlock(&ftrace_lock);
3801 if (pid != ftrace_swapper_pid)
3805 mutex_unlock(&ftrace_lock);
3809 static void ftrace_pid_reset(void)
3811 struct ftrace_pid *fpid, *safe;
3813 mutex_lock(&ftrace_lock);
3814 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3815 struct pid *pid = fpid->pid;
3817 clear_ftrace_pid_task(pid);
3819 list_del(&fpid->list);
3823 ftrace_update_pid_func();
3824 ftrace_startup_enable(0);
3826 mutex_unlock(&ftrace_lock);
3829 static void *fpid_start(struct seq_file *m, loff_t *pos)
3831 mutex_lock(&ftrace_lock);
3833 if (list_empty(&ftrace_pids) && (!*pos))
3836 return seq_list_start(&ftrace_pids, *pos);
3839 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3844 return seq_list_next(v, &ftrace_pids, pos);
3847 static void fpid_stop(struct seq_file *m, void *p)
3849 mutex_unlock(&ftrace_lock);
3852 static int fpid_show(struct seq_file *m, void *v)
3854 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3856 if (v == (void *)1) {
3857 seq_printf(m, "no pid\n");
3861 if (fpid->pid == ftrace_swapper_pid)
3862 seq_printf(m, "swapper tasks\n");
3864 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3869 static const struct seq_operations ftrace_pid_sops = {
3870 .start = fpid_start,
3877 ftrace_pid_open(struct inode *inode, struct file *file)
3881 if ((file->f_mode & FMODE_WRITE) &&
3882 (file->f_flags & O_TRUNC))
3885 if (file->f_mode & FMODE_READ)
3886 ret = seq_open(file, &ftrace_pid_sops);
3892 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3893 size_t cnt, loff_t *ppos)
3899 if (cnt >= sizeof(buf))
3902 if (copy_from_user(&buf, ubuf, cnt))
3908 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3909 * to clean the filter quietly.
3911 tmp = strstrip(buf);
3912 if (strlen(tmp) == 0)
3915 ret = strict_strtol(tmp, 10, &val);
3919 ret = ftrace_pid_add(val);
3921 return ret ? ret : cnt;
3925 ftrace_pid_release(struct inode *inode, struct file *file)
3927 if (file->f_mode & FMODE_READ)
3928 seq_release(inode, file);
3933 static const struct file_operations ftrace_pid_fops = {
3934 .open = ftrace_pid_open,
3935 .write = ftrace_pid_write,
3937 .llseek = ftrace_filter_lseek,
3938 .release = ftrace_pid_release,
3941 static __init int ftrace_init_debugfs(void)
3943 struct dentry *d_tracer;
3945 d_tracer = tracing_init_dentry();
3949 ftrace_init_dyn_debugfs(d_tracer);
3951 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3952 NULL, &ftrace_pid_fops);
3954 ftrace_profile_debugfs(d_tracer);
3958 fs_initcall(ftrace_init_debugfs);
3961 * ftrace_kill - kill ftrace
3963 * This function should be used by panic code. It stops ftrace
3964 * but in a not so nice way. If you need to simply kill ftrace
3965 * from a non-atomic section, use ftrace_kill.
3967 void ftrace_kill(void)
3969 ftrace_disabled = 1;
3971 clear_ftrace_function();
3975 * Test if ftrace is dead or not.
3977 int ftrace_is_dead(void)
3979 return ftrace_disabled;
3983 * register_ftrace_function - register a function for profiling
3984 * @ops - ops structure that holds the function for profiling.
3986 * Register a function to be called by all functions in the
3989 * Note: @ops->func and all the functions it calls must be labeled
3990 * with "notrace", otherwise it will go into a
3993 int register_ftrace_function(struct ftrace_ops *ops)
3997 mutex_lock(&ftrace_lock);
3999 ret = ftrace_startup(ops, 0);
4001 mutex_unlock(&ftrace_lock);
4004 EXPORT_SYMBOL_GPL(register_ftrace_function);
4007 * unregister_ftrace_function - unregister a function for profiling.
4008 * @ops - ops structure that holds the function to unregister
4010 * Unregister a function that was added to be called by ftrace profiling.
4012 int unregister_ftrace_function(struct ftrace_ops *ops)
4016 mutex_lock(&ftrace_lock);
4017 ret = ftrace_shutdown(ops, 0);
4018 mutex_unlock(&ftrace_lock);
4022 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4025 ftrace_enable_sysctl(struct ctl_table *table, int write,
4026 void __user *buffer, size_t *lenp,
4031 mutex_lock(&ftrace_lock);
4033 if (unlikely(ftrace_disabled))
4036 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4038 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4041 last_ftrace_enabled = !!ftrace_enabled;
4043 if (ftrace_enabled) {
4045 /* we are starting ftrace again */
4046 if (ftrace_ops_list != &ftrace_list_end)
4047 update_ftrace_function();
4049 ftrace_startup_sysctl();
4052 /* stopping ftrace calls (just send to ftrace_stub) */
4053 ftrace_trace_function = ftrace_stub;
4055 ftrace_shutdown_sysctl();
4059 mutex_unlock(&ftrace_lock);
4063 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4065 static struct notifier_block ftrace_suspend_notifier;
4067 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4072 /* The callbacks that hook a function */
4073 trace_func_graph_ret_t ftrace_graph_return =
4074 (trace_func_graph_ret_t)ftrace_stub;
4075 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4076 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
4078 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4079 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4083 unsigned long flags;
4084 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4085 struct task_struct *g, *t;
4087 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4088 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4089 * sizeof(struct ftrace_ret_stack),
4091 if (!ret_stack_list[i]) {
4099 read_lock_irqsave(&tasklist_lock, flags);
4100 do_each_thread(g, t) {
4106 if (t->ret_stack == NULL) {
4107 atomic_set(&t->tracing_graph_pause, 0);
4108 atomic_set(&t->trace_overrun, 0);
4109 t->curr_ret_stack = -1;
4110 /* Make sure the tasks see the -1 first: */
4112 t->ret_stack = ret_stack_list[start++];
4114 } while_each_thread(g, t);
4117 read_unlock_irqrestore(&tasklist_lock, flags);
4119 for (i = start; i < end; i++)
4120 kfree(ret_stack_list[i]);
4125 ftrace_graph_probe_sched_switch(void *ignore,
4126 struct task_struct *prev, struct task_struct *next)
4128 unsigned long long timestamp;
4132 * Does the user want to count the time a function was asleep.
4133 * If so, do not update the time stamps.
4135 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4138 timestamp = trace_clock_local();
4140 prev->ftrace_timestamp = timestamp;
4142 /* only process tasks that we timestamped */
4143 if (!next->ftrace_timestamp)
4147 * Update all the counters in next to make up for the
4148 * time next was sleeping.
4150 timestamp -= next->ftrace_timestamp;
4152 for (index = next->curr_ret_stack; index >= 0; index--)
4153 next->ret_stack[index].calltime += timestamp;
4156 /* Allocate a return stack for each task */
4157 static int start_graph_tracing(void)
4159 struct ftrace_ret_stack **ret_stack_list;
4162 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4163 sizeof(struct ftrace_ret_stack *),
4166 if (!ret_stack_list)
4169 /* The cpu_boot init_task->ret_stack will never be freed */
4170 for_each_online_cpu(cpu) {
4171 if (!idle_task(cpu)->ret_stack)
4172 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4176 ret = alloc_retstack_tasklist(ret_stack_list);
4177 } while (ret == -EAGAIN);
4180 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4182 pr_info("ftrace_graph: Couldn't activate tracepoint"
4183 " probe to kernel_sched_switch\n");
4186 kfree(ret_stack_list);
4191 * Hibernation protection.
4192 * The state of the current task is too much unstable during
4193 * suspend/restore to disk. We want to protect against that.
4196 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4200 case PM_HIBERNATION_PREPARE:
4201 pause_graph_tracing();
4204 case PM_POST_HIBERNATION:
4205 unpause_graph_tracing();
4211 /* Just a place holder for function graph */
4212 static struct ftrace_ops fgraph_ops __read_mostly = {
4213 .func = ftrace_stub,
4214 .flags = FTRACE_OPS_FL_GLOBAL,
4217 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
4219 if (!ftrace_ops_test(&global_ops, trace->func))
4221 return __ftrace_graph_entry(trace);
4225 * The function graph tracer should only trace the functions defined
4226 * by set_ftrace_filter and set_ftrace_notrace. If another function
4227 * tracer ops is registered, the graph tracer requires testing the
4228 * function against the global ops, and not just trace any function
4229 * that any ftrace_ops registered.
4231 static void update_function_graph_func(void)
4233 if (ftrace_ops_list == &ftrace_list_end ||
4234 (ftrace_ops_list == &global_ops &&
4235 global_ops.next == &ftrace_list_end))
4236 ftrace_graph_entry = __ftrace_graph_entry;
4238 ftrace_graph_entry = ftrace_graph_entry_test;
4241 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4242 trace_func_graph_ent_t entryfunc)
4246 mutex_lock(&ftrace_lock);
4248 /* we currently allow only one tracer registered at a time */
4249 if (ftrace_graph_active) {
4254 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4255 register_pm_notifier(&ftrace_suspend_notifier);
4257 ftrace_graph_active++;
4258 ret = start_graph_tracing();
4260 ftrace_graph_active--;
4264 ftrace_graph_return = retfunc;
4267 * Update the indirect function to the entryfunc, and the
4268 * function that gets called to the entry_test first. Then
4269 * call the update fgraph entry function to determine if
4270 * the entryfunc should be called directly or not.
4272 __ftrace_graph_entry = entryfunc;
4273 ftrace_graph_entry = ftrace_graph_entry_test;
4274 update_function_graph_func();
4276 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
4279 mutex_unlock(&ftrace_lock);
4283 void unregister_ftrace_graph(void)
4285 mutex_lock(&ftrace_lock);
4287 if (unlikely(!ftrace_graph_active))
4290 ftrace_graph_active--;
4291 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4292 ftrace_graph_entry = ftrace_graph_entry_stub;
4293 __ftrace_graph_entry = ftrace_graph_entry_stub;
4294 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
4295 unregister_pm_notifier(&ftrace_suspend_notifier);
4296 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4299 mutex_unlock(&ftrace_lock);
4302 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4305 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4307 atomic_set(&t->tracing_graph_pause, 0);
4308 atomic_set(&t->trace_overrun, 0);
4309 t->ftrace_timestamp = 0;
4310 /* make curr_ret_stack visible before we add the ret_stack */
4312 t->ret_stack = ret_stack;
4316 * Allocate a return stack for the idle task. May be the first
4317 * time through, or it may be done by CPU hotplug online.
4319 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4321 t->curr_ret_stack = -1;
4323 * The idle task has no parent, it either has its own
4324 * stack or no stack at all.
4327 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4329 if (ftrace_graph_active) {
4330 struct ftrace_ret_stack *ret_stack;
4332 ret_stack = per_cpu(idle_ret_stack, cpu);
4334 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4335 * sizeof(struct ftrace_ret_stack),
4339 per_cpu(idle_ret_stack, cpu) = ret_stack;
4341 graph_init_task(t, ret_stack);
4345 /* Allocate a return stack for newly created task */
4346 void ftrace_graph_init_task(struct task_struct *t)
4348 /* Make sure we do not use the parent ret_stack */
4349 t->ret_stack = NULL;
4350 t->curr_ret_stack = -1;
4352 if (ftrace_graph_active) {
4353 struct ftrace_ret_stack *ret_stack;
4355 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4356 * sizeof(struct ftrace_ret_stack),
4360 graph_init_task(t, ret_stack);
4364 void ftrace_graph_exit_task(struct task_struct *t)
4366 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4368 t->ret_stack = NULL;
4369 /* NULL must become visible to IRQs before we free it: */
4375 void ftrace_graph_stop(void)