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/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
36 #include <trace/events/sched.h>
38 #include <asm/setup.h>
40 #include "trace_output.h"
41 #include "trace_stat.h"
43 #define FTRACE_WARN_ON(cond) \
51 #define FTRACE_WARN_ON_ONCE(cond) \
54 if (WARN_ON_ONCE(___r)) \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
67 /* ftrace_enabled is a method to turn ftrace on or off */
68 int ftrace_enabled __read_mostly;
69 static int last_ftrace_enabled;
71 /* Quick disabling of function tracer. */
72 int function_trace_stop;
74 /* List for set_ftrace_pid's pids. */
75 LIST_HEAD(ftrace_pids);
77 struct list_head list;
82 * ftrace_disabled is set when an anomaly is discovered.
83 * ftrace_disabled is much stronger than ftrace_enabled.
85 static int ftrace_disabled __read_mostly;
87 static DEFINE_MUTEX(ftrace_lock);
89 static struct ftrace_ops ftrace_list_end __read_mostly = {
93 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
94 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
95 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
96 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
97 static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
98 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
99 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
100 static struct ftrace_ops global_ops;
101 static struct ftrace_ops control_ops;
104 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
107 * Traverse the ftrace_global_list, invoking all entries. The reason that we
108 * can use rcu_dereference_raw() is that elements removed from this list
109 * are simply leaked, so there is no need to interact with a grace-period
110 * mechanism. The rcu_dereference_raw() calls are needed to handle
111 * concurrent insertions into the ftrace_global_list.
113 * Silly Alpha and silly pointer-speculation compiler optimizations!
115 static void ftrace_global_list_func(unsigned long ip,
116 unsigned long parent_ip)
118 struct ftrace_ops *op;
120 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
123 trace_recursion_set(TRACE_GLOBAL_BIT);
124 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
125 while (op != &ftrace_list_end) {
126 op->func(ip, parent_ip);
127 op = rcu_dereference_raw(op->next); /*see above*/
129 trace_recursion_clear(TRACE_GLOBAL_BIT);
132 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
134 if (!test_tsk_trace_trace(current))
137 ftrace_pid_function(ip, parent_ip);
140 static void set_ftrace_pid_function(ftrace_func_t func)
142 /* do not set ftrace_pid_function to itself! */
143 if (func != ftrace_pid_func)
144 ftrace_pid_function = func;
148 * clear_ftrace_function - reset the ftrace function
150 * This NULLs the ftrace function and in essence stops
151 * tracing. There may be lag
153 void clear_ftrace_function(void)
155 ftrace_trace_function = ftrace_stub;
156 __ftrace_trace_function = ftrace_stub;
157 __ftrace_trace_function_delay = ftrace_stub;
158 ftrace_pid_function = ftrace_stub;
161 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
163 * For those archs that do not test ftrace_trace_stop in their
164 * mcount call site, we need to do it from C.
166 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
168 if (function_trace_stop)
171 __ftrace_trace_function(ip, parent_ip);
175 static void control_ops_disable_all(struct ftrace_ops *ops)
179 for_each_possible_cpu(cpu)
180 *per_cpu_ptr(ops->disabled, cpu) = 1;
183 static int control_ops_alloc(struct ftrace_ops *ops)
185 int __percpu *disabled;
187 disabled = alloc_percpu(int);
191 ops->disabled = disabled;
192 control_ops_disable_all(ops);
196 static void control_ops_free(struct ftrace_ops *ops)
198 free_percpu(ops->disabled);
201 static void update_global_ops(void)
206 * If there's only one function registered, then call that
207 * function directly. Otherwise, we need to iterate over the
208 * registered callers.
210 if (ftrace_global_list == &ftrace_list_end ||
211 ftrace_global_list->next == &ftrace_list_end)
212 func = ftrace_global_list->func;
214 func = ftrace_global_list_func;
216 /* If we filter on pids, update to use the pid function */
217 if (!list_empty(&ftrace_pids)) {
218 set_ftrace_pid_function(func);
219 func = ftrace_pid_func;
222 global_ops.func = func;
225 static void update_ftrace_function(void)
232 * If we are at the end of the list and this ops is
233 * not dynamic, then have the mcount trampoline call
234 * the function directly
236 if (ftrace_ops_list == &ftrace_list_end ||
237 (ftrace_ops_list->next == &ftrace_list_end &&
238 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
239 func = ftrace_ops_list->func;
241 func = ftrace_ops_list_func;
243 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
244 ftrace_trace_function = func;
246 #ifdef CONFIG_DYNAMIC_FTRACE
247 /* do not update till all functions have been modified */
248 __ftrace_trace_function_delay = func;
250 __ftrace_trace_function = func;
252 ftrace_trace_function = ftrace_test_stop_func;
256 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
260 * We are entering ops into the list but another
261 * CPU might be walking that list. We need to make sure
262 * the ops->next pointer is valid before another CPU sees
263 * the ops pointer included into the list.
265 rcu_assign_pointer(*list, ops);
268 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
270 struct ftrace_ops **p;
273 * If we are removing the last function, then simply point
274 * to the ftrace_stub.
276 if (*list == ops && ops->next == &ftrace_list_end) {
277 *list = &ftrace_list_end;
281 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
292 static void add_ftrace_list_ops(struct ftrace_ops **list,
293 struct ftrace_ops *main_ops,
294 struct ftrace_ops *ops)
296 int first = *list == &ftrace_list_end;
297 add_ftrace_ops(list, ops);
299 add_ftrace_ops(&ftrace_ops_list, main_ops);
302 static int remove_ftrace_list_ops(struct ftrace_ops **list,
303 struct ftrace_ops *main_ops,
304 struct ftrace_ops *ops)
306 int ret = remove_ftrace_ops(list, ops);
307 if (!ret && *list == &ftrace_list_end)
308 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
312 static int __register_ftrace_function(struct ftrace_ops *ops)
317 if (FTRACE_WARN_ON(ops == &global_ops))
320 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
323 /* We don't support both control and global flags set. */
324 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
327 if (!core_kernel_data((unsigned long)ops))
328 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
330 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
331 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
332 ops->flags |= FTRACE_OPS_FL_ENABLED;
333 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
334 if (control_ops_alloc(ops))
336 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
338 add_ftrace_ops(&ftrace_ops_list, ops);
341 update_ftrace_function();
346 static int __unregister_ftrace_function(struct ftrace_ops *ops)
353 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
356 if (FTRACE_WARN_ON(ops == &global_ops))
359 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
360 ret = remove_ftrace_list_ops(&ftrace_global_list,
363 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
364 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
365 ret = remove_ftrace_list_ops(&ftrace_control_list,
369 * The ftrace_ops is now removed from the list,
370 * so there'll be no new users. We must ensure
371 * all current users are done before we free
375 control_ops_free(ops);
378 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
384 update_ftrace_function();
387 * Dynamic ops may be freed, we must make sure that all
388 * callers are done before leaving this function.
390 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
396 static void ftrace_update_pid_func(void)
398 /* Only do something if we are tracing something */
399 if (ftrace_trace_function == ftrace_stub)
402 update_ftrace_function();
405 #ifdef CONFIG_FUNCTION_PROFILER
406 struct ftrace_profile {
407 struct hlist_node node;
409 unsigned long counter;
410 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
411 unsigned long long time;
412 unsigned long long time_squared;
416 struct ftrace_profile_page {
417 struct ftrace_profile_page *next;
419 struct ftrace_profile records[];
422 struct ftrace_profile_stat {
424 struct hlist_head *hash;
425 struct ftrace_profile_page *pages;
426 struct ftrace_profile_page *start;
427 struct tracer_stat stat;
430 #define PROFILE_RECORDS_SIZE \
431 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
433 #define PROFILES_PER_PAGE \
434 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
436 static int ftrace_profile_bits __read_mostly;
437 static int ftrace_profile_enabled __read_mostly;
439 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
440 static DEFINE_MUTEX(ftrace_profile_lock);
442 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
444 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
447 function_stat_next(void *v, int idx)
449 struct ftrace_profile *rec = v;
450 struct ftrace_profile_page *pg;
452 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
458 if ((void *)rec >= (void *)&pg->records[pg->index]) {
462 rec = &pg->records[0];
470 static void *function_stat_start(struct tracer_stat *trace)
472 struct ftrace_profile_stat *stat =
473 container_of(trace, struct ftrace_profile_stat, stat);
475 if (!stat || !stat->start)
478 return function_stat_next(&stat->start->records[0], 0);
481 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
482 /* function graph compares on total time */
483 static int function_stat_cmp(void *p1, void *p2)
485 struct ftrace_profile *a = p1;
486 struct ftrace_profile *b = p2;
488 if (a->time < b->time)
490 if (a->time > b->time)
496 /* not function graph compares against hits */
497 static int function_stat_cmp(void *p1, void *p2)
499 struct ftrace_profile *a = p1;
500 struct ftrace_profile *b = p2;
502 if (a->counter < b->counter)
504 if (a->counter > b->counter)
511 static int function_stat_headers(struct seq_file *m)
513 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
514 seq_printf(m, " Function "
517 "--- ---- --- ---\n");
519 seq_printf(m, " Function Hit\n"
525 static int function_stat_show(struct seq_file *m, void *v)
527 struct ftrace_profile *rec = v;
528 char str[KSYM_SYMBOL_LEN];
530 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
531 static struct trace_seq s;
532 unsigned long long avg;
533 unsigned long long stddev;
535 mutex_lock(&ftrace_profile_lock);
537 /* we raced with function_profile_reset() */
538 if (unlikely(rec->counter == 0)) {
543 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
544 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
546 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
549 do_div(avg, rec->counter);
551 /* Sample standard deviation (s^2) */
552 if (rec->counter <= 1)
555 stddev = rec->time_squared - rec->counter * avg * avg;
557 * Divide only 1000 for ns^2 -> us^2 conversion.
558 * trace_print_graph_duration will divide 1000 again.
560 do_div(stddev, (rec->counter - 1) * 1000);
564 trace_print_graph_duration(rec->time, &s);
565 trace_seq_puts(&s, " ");
566 trace_print_graph_duration(avg, &s);
567 trace_seq_puts(&s, " ");
568 trace_print_graph_duration(stddev, &s);
569 trace_print_seq(m, &s);
573 mutex_unlock(&ftrace_profile_lock);
578 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
580 struct ftrace_profile_page *pg;
582 pg = stat->pages = stat->start;
585 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
590 memset(stat->hash, 0,
591 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
594 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
596 struct ftrace_profile_page *pg;
601 /* If we already allocated, do nothing */
605 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
609 #ifdef CONFIG_DYNAMIC_FTRACE
610 functions = ftrace_update_tot_cnt;
613 * We do not know the number of functions that exist because
614 * dynamic tracing is what counts them. With past experience
615 * we have around 20K functions. That should be more than enough.
616 * It is highly unlikely we will execute every function in
622 pg = stat->start = stat->pages;
624 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
626 for (i = 0; i < pages; i++) {
627 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
638 unsigned long tmp = (unsigned long)pg;
644 free_page((unsigned long)stat->pages);
651 static int ftrace_profile_init_cpu(int cpu)
653 struct ftrace_profile_stat *stat;
656 stat = &per_cpu(ftrace_profile_stats, cpu);
659 /* If the profile is already created, simply reset it */
660 ftrace_profile_reset(stat);
665 * We are profiling all functions, but usually only a few thousand
666 * functions are hit. We'll make a hash of 1024 items.
668 size = FTRACE_PROFILE_HASH_SIZE;
670 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
675 if (!ftrace_profile_bits) {
678 for (; size; size >>= 1)
679 ftrace_profile_bits++;
682 /* Preallocate the function profiling pages */
683 if (ftrace_profile_pages_init(stat) < 0) {
692 static int ftrace_profile_init(void)
697 for_each_online_cpu(cpu) {
698 ret = ftrace_profile_init_cpu(cpu);
706 /* interrupts must be disabled */
707 static struct ftrace_profile *
708 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
710 struct ftrace_profile *rec;
711 struct hlist_head *hhd;
712 struct hlist_node *n;
715 key = hash_long(ip, ftrace_profile_bits);
716 hhd = &stat->hash[key];
718 if (hlist_empty(hhd))
721 hlist_for_each_entry_rcu(rec, n, hhd, node) {
729 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
730 struct ftrace_profile *rec)
734 key = hash_long(rec->ip, ftrace_profile_bits);
735 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
739 * The memory is already allocated, this simply finds a new record to use.
741 static struct ftrace_profile *
742 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
744 struct ftrace_profile *rec = NULL;
746 /* prevent recursion (from NMIs) */
747 if (atomic_inc_return(&stat->disabled) != 1)
751 * Try to find the function again since an NMI
752 * could have added it
754 rec = ftrace_find_profiled_func(stat, ip);
758 if (stat->pages->index == PROFILES_PER_PAGE) {
759 if (!stat->pages->next)
761 stat->pages = stat->pages->next;
764 rec = &stat->pages->records[stat->pages->index++];
766 ftrace_add_profile(stat, rec);
769 atomic_dec(&stat->disabled);
775 function_profile_call(unsigned long ip, unsigned long parent_ip)
777 struct ftrace_profile_stat *stat;
778 struct ftrace_profile *rec;
781 if (!ftrace_profile_enabled)
784 local_irq_save(flags);
786 stat = &__get_cpu_var(ftrace_profile_stats);
787 if (!stat->hash || !ftrace_profile_enabled)
790 rec = ftrace_find_profiled_func(stat, ip);
792 rec = ftrace_profile_alloc(stat, ip);
799 local_irq_restore(flags);
802 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
803 static int profile_graph_entry(struct ftrace_graph_ent *trace)
805 function_profile_call(trace->func, 0);
809 static void profile_graph_return(struct ftrace_graph_ret *trace)
811 struct ftrace_profile_stat *stat;
812 unsigned long long calltime;
813 struct ftrace_profile *rec;
816 local_irq_save(flags);
817 stat = &__get_cpu_var(ftrace_profile_stats);
818 if (!stat->hash || !ftrace_profile_enabled)
821 /* If the calltime was zero'd ignore it */
822 if (!trace->calltime)
825 calltime = trace->rettime - trace->calltime;
827 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
830 index = trace->depth;
832 /* Append this call time to the parent time to subtract */
834 current->ret_stack[index - 1].subtime += calltime;
836 if (current->ret_stack[index].subtime < calltime)
837 calltime -= current->ret_stack[index].subtime;
842 rec = ftrace_find_profiled_func(stat, trace->func);
844 rec->time += calltime;
845 rec->time_squared += calltime * calltime;
849 local_irq_restore(flags);
852 static int register_ftrace_profiler(void)
854 return register_ftrace_graph(&profile_graph_return,
855 &profile_graph_entry);
858 static void unregister_ftrace_profiler(void)
860 unregister_ftrace_graph();
863 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
864 .func = function_profile_call,
867 static int register_ftrace_profiler(void)
869 return register_ftrace_function(&ftrace_profile_ops);
872 static void unregister_ftrace_profiler(void)
874 unregister_ftrace_function(&ftrace_profile_ops);
876 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
879 ftrace_profile_write(struct file *filp, const char __user *ubuf,
880 size_t cnt, loff_t *ppos)
885 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
891 mutex_lock(&ftrace_profile_lock);
892 if (ftrace_profile_enabled ^ val) {
894 ret = ftrace_profile_init();
900 ret = register_ftrace_profiler();
905 ftrace_profile_enabled = 1;
907 ftrace_profile_enabled = 0;
909 * unregister_ftrace_profiler calls stop_machine
910 * so this acts like an synchronize_sched.
912 unregister_ftrace_profiler();
916 mutex_unlock(&ftrace_profile_lock);
924 ftrace_profile_read(struct file *filp, char __user *ubuf,
925 size_t cnt, loff_t *ppos)
927 char buf[64]; /* big enough to hold a number */
930 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
931 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
934 static const struct file_operations ftrace_profile_fops = {
935 .open = tracing_open_generic,
936 .read = ftrace_profile_read,
937 .write = ftrace_profile_write,
938 .llseek = default_llseek,
941 /* used to initialize the real stat files */
942 static struct tracer_stat function_stats __initdata = {
944 .stat_start = function_stat_start,
945 .stat_next = function_stat_next,
946 .stat_cmp = function_stat_cmp,
947 .stat_headers = function_stat_headers,
948 .stat_show = function_stat_show
951 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
953 struct ftrace_profile_stat *stat;
954 struct dentry *entry;
959 for_each_possible_cpu(cpu) {
960 stat = &per_cpu(ftrace_profile_stats, cpu);
962 /* allocate enough for function name + cpu number */
963 name = kmalloc(32, GFP_KERNEL);
966 * The files created are permanent, if something happens
967 * we still do not free memory.
970 "Could not allocate stat file for cpu %d\n",
974 stat->stat = function_stats;
975 snprintf(name, 32, "function%d", cpu);
976 stat->stat.name = name;
977 ret = register_stat_tracer(&stat->stat);
980 "Could not register function stat for cpu %d\n",
987 entry = debugfs_create_file("function_profile_enabled", 0644,
988 d_tracer, NULL, &ftrace_profile_fops);
990 pr_warning("Could not create debugfs "
991 "'function_profile_enabled' entry\n");
994 #else /* CONFIG_FUNCTION_PROFILER */
995 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
998 #endif /* CONFIG_FUNCTION_PROFILER */
1000 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1002 #ifdef CONFIG_DYNAMIC_FTRACE
1004 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1005 # error Dynamic ftrace depends on MCOUNT_RECORD
1008 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1010 struct ftrace_func_probe {
1011 struct hlist_node node;
1012 struct ftrace_probe_ops *ops;
1013 unsigned long flags;
1016 struct rcu_head rcu;
1019 struct ftrace_func_entry {
1020 struct hlist_node hlist;
1024 struct ftrace_hash {
1025 unsigned long size_bits;
1026 struct hlist_head *buckets;
1027 unsigned long count;
1028 struct rcu_head rcu;
1032 * We make these constant because no one should touch them,
1033 * but they are used as the default "empty hash", to avoid allocating
1034 * it all the time. These are in a read only section such that if
1035 * anyone does try to modify it, it will cause an exception.
1037 static const struct hlist_head empty_buckets[1];
1038 static const struct ftrace_hash empty_hash = {
1039 .buckets = (struct hlist_head *)empty_buckets,
1041 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1043 static struct ftrace_ops global_ops = {
1044 .func = ftrace_stub,
1045 .notrace_hash = EMPTY_HASH,
1046 .filter_hash = EMPTY_HASH,
1049 static DEFINE_MUTEX(ftrace_regex_lock);
1051 struct ftrace_page {
1052 struct ftrace_page *next;
1053 struct dyn_ftrace *records;
1058 static struct ftrace_page *ftrace_new_pgs;
1060 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1061 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1063 /* estimate from running different kernels */
1064 #define NR_TO_INIT 10000
1066 static struct ftrace_page *ftrace_pages_start;
1067 static struct ftrace_page *ftrace_pages;
1069 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1071 return !hash || !hash->count;
1074 static struct ftrace_func_entry *
1075 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1078 struct ftrace_func_entry *entry;
1079 struct hlist_head *hhd;
1080 struct hlist_node *n;
1082 if (ftrace_hash_empty(hash))
1085 if (hash->size_bits > 0)
1086 key = hash_long(ip, hash->size_bits);
1090 hhd = &hash->buckets[key];
1092 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1093 if (entry->ip == ip)
1099 static void __add_hash_entry(struct ftrace_hash *hash,
1100 struct ftrace_func_entry *entry)
1102 struct hlist_head *hhd;
1105 if (hash->size_bits)
1106 key = hash_long(entry->ip, hash->size_bits);
1110 hhd = &hash->buckets[key];
1111 hlist_add_head(&entry->hlist, hhd);
1115 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1117 struct ftrace_func_entry *entry;
1119 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1124 __add_hash_entry(hash, entry);
1130 free_hash_entry(struct ftrace_hash *hash,
1131 struct ftrace_func_entry *entry)
1133 hlist_del(&entry->hlist);
1139 remove_hash_entry(struct ftrace_hash *hash,
1140 struct ftrace_func_entry *entry)
1142 hlist_del(&entry->hlist);
1146 static void ftrace_hash_clear(struct ftrace_hash *hash)
1148 struct hlist_head *hhd;
1149 struct hlist_node *tp, *tn;
1150 struct ftrace_func_entry *entry;
1151 int size = 1 << hash->size_bits;
1157 for (i = 0; i < size; i++) {
1158 hhd = &hash->buckets[i];
1159 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1160 free_hash_entry(hash, entry);
1162 FTRACE_WARN_ON(hash->count);
1165 static void free_ftrace_hash(struct ftrace_hash *hash)
1167 if (!hash || hash == EMPTY_HASH)
1169 ftrace_hash_clear(hash);
1170 kfree(hash->buckets);
1174 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1176 struct ftrace_hash *hash;
1178 hash = container_of(rcu, struct ftrace_hash, rcu);
1179 free_ftrace_hash(hash);
1182 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1184 if (!hash || hash == EMPTY_HASH)
1186 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1189 void ftrace_free_filter(struct ftrace_ops *ops)
1191 free_ftrace_hash(ops->filter_hash);
1192 free_ftrace_hash(ops->notrace_hash);
1195 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1197 struct ftrace_hash *hash;
1200 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1204 size = 1 << size_bits;
1205 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1207 if (!hash->buckets) {
1212 hash->size_bits = size_bits;
1217 static struct ftrace_hash *
1218 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1220 struct ftrace_func_entry *entry;
1221 struct ftrace_hash *new_hash;
1222 struct hlist_node *tp;
1227 new_hash = alloc_ftrace_hash(size_bits);
1232 if (ftrace_hash_empty(hash))
1235 size = 1 << hash->size_bits;
1236 for (i = 0; i < size; i++) {
1237 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1238 ret = add_hash_entry(new_hash, entry->ip);
1244 FTRACE_WARN_ON(new_hash->count != hash->count);
1249 free_ftrace_hash(new_hash);
1254 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1256 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1259 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1260 struct ftrace_hash **dst, struct ftrace_hash *src)
1262 struct ftrace_func_entry *entry;
1263 struct hlist_node *tp, *tn;
1264 struct hlist_head *hhd;
1265 struct ftrace_hash *old_hash;
1266 struct ftrace_hash *new_hash;
1268 int size = src->count;
1274 * Remove the current set, update the hash and add
1277 ftrace_hash_rec_disable(ops, enable);
1280 * If the new source is empty, just free dst and assign it
1284 free_ftrace_hash_rcu(*dst);
1285 rcu_assign_pointer(*dst, EMPTY_HASH);
1286 /* still need to update the function records */
1292 * Make the hash size about 1/2 the # found
1294 for (size /= 2; size; size >>= 1)
1297 /* Don't allocate too much */
1298 if (bits > FTRACE_HASH_MAX_BITS)
1299 bits = FTRACE_HASH_MAX_BITS;
1302 new_hash = alloc_ftrace_hash(bits);
1306 size = 1 << src->size_bits;
1307 for (i = 0; i < size; i++) {
1308 hhd = &src->buckets[i];
1309 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1311 key = hash_long(entry->ip, bits);
1314 remove_hash_entry(src, entry);
1315 __add_hash_entry(new_hash, entry);
1320 rcu_assign_pointer(*dst, new_hash);
1321 free_ftrace_hash_rcu(old_hash);
1326 * Enable regardless of ret:
1327 * On success, we enable the new hash.
1328 * On failure, we re-enable the original hash.
1330 ftrace_hash_rec_enable(ops, enable);
1336 * Test the hashes for this ops to see if we want to call
1337 * the ops->func or not.
1339 * It's a match if the ip is in the ops->filter_hash or
1340 * the filter_hash does not exist or is empty,
1342 * the ip is not in the ops->notrace_hash.
1344 * This needs to be called with preemption disabled as
1345 * the hashes are freed with call_rcu_sched().
1348 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1350 struct ftrace_hash *filter_hash;
1351 struct ftrace_hash *notrace_hash;
1354 filter_hash = rcu_dereference_raw(ops->filter_hash);
1355 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1357 if ((ftrace_hash_empty(filter_hash) ||
1358 ftrace_lookup_ip(filter_hash, ip)) &&
1359 (ftrace_hash_empty(notrace_hash) ||
1360 !ftrace_lookup_ip(notrace_hash, ip)))
1369 * This is a double for. Do not use 'break' to break out of the loop,
1370 * you must use a goto.
1372 #define do_for_each_ftrace_rec(pg, rec) \
1373 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1375 for (_____i = 0; _____i < pg->index; _____i++) { \
1376 rec = &pg->records[_____i];
1378 #define while_for_each_ftrace_rec() \
1383 static int ftrace_cmp_recs(const void *a, const void *b)
1385 const struct dyn_ftrace *reca = a;
1386 const struct dyn_ftrace *recb = b;
1388 if (reca->ip > recb->ip)
1390 if (reca->ip < recb->ip)
1396 * ftrace_location - return true if the ip giving is a traced location
1397 * @ip: the instruction pointer to check
1399 * Returns 1 if @ip given is a pointer to a ftrace location.
1400 * That is, the instruction that is either a NOP or call to
1401 * the function tracer. It checks the ftrace internal tables to
1402 * determine if the address belongs or not.
1404 int ftrace_location(unsigned long ip)
1406 struct ftrace_page *pg;
1407 struct dyn_ftrace *rec;
1408 struct dyn_ftrace key;
1412 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1413 rec = bsearch(&key, pg->records, pg->index,
1414 sizeof(struct dyn_ftrace),
1423 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1427 struct ftrace_hash *hash;
1428 struct ftrace_hash *other_hash;
1429 struct ftrace_page *pg;
1430 struct dyn_ftrace *rec;
1434 /* Only update if the ops has been registered */
1435 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1439 * In the filter_hash case:
1440 * If the count is zero, we update all records.
1441 * Otherwise we just update the items in the hash.
1443 * In the notrace_hash case:
1444 * We enable the update in the hash.
1445 * As disabling notrace means enabling the tracing,
1446 * and enabling notrace means disabling, the inc variable
1450 hash = ops->filter_hash;
1451 other_hash = ops->notrace_hash;
1452 if (ftrace_hash_empty(hash))
1456 hash = ops->notrace_hash;
1457 other_hash = ops->filter_hash;
1459 * If the notrace hash has no items,
1460 * then there's nothing to do.
1462 if (ftrace_hash_empty(hash))
1466 do_for_each_ftrace_rec(pg, rec) {
1467 int in_other_hash = 0;
1473 * Only the filter_hash affects all records.
1474 * Update if the record is not in the notrace hash.
1476 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1479 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1480 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1485 if (filter_hash && in_hash && !in_other_hash)
1487 else if (!filter_hash && in_hash &&
1488 (in_other_hash || ftrace_hash_empty(other_hash)))
1496 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1499 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1504 /* Shortcut, if we handled all records, we are done. */
1505 if (!all && count == hash->count)
1507 } while_for_each_ftrace_rec();
1510 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1513 __ftrace_hash_rec_update(ops, filter_hash, 0);
1516 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1519 __ftrace_hash_rec_update(ops, filter_hash, 1);
1522 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1524 if (ftrace_pages->index == ftrace_pages->size) {
1525 /* We should have allocated enough */
1526 if (WARN_ON(!ftrace_pages->next))
1528 ftrace_pages = ftrace_pages->next;
1531 return &ftrace_pages->records[ftrace_pages->index++];
1534 static struct dyn_ftrace *
1535 ftrace_record_ip(unsigned long ip)
1537 struct dyn_ftrace *rec;
1539 if (ftrace_disabled)
1542 rec = ftrace_alloc_dyn_node(ip);
1551 static void print_ip_ins(const char *fmt, unsigned char *p)
1555 printk(KERN_CONT "%s", fmt);
1557 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1558 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1562 * ftrace_bug - report and shutdown function tracer
1563 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1564 * @ip: The address that failed
1566 * The arch code that enables or disables the function tracing
1567 * can call ftrace_bug() when it has detected a problem in
1568 * modifying the code. @failed should be one of either:
1569 * EFAULT - if the problem happens on reading the @ip address
1570 * EINVAL - if what is read at @ip is not what was expected
1571 * EPERM - if the problem happens on writting to the @ip address
1573 void ftrace_bug(int failed, unsigned long ip)
1577 FTRACE_WARN_ON_ONCE(1);
1578 pr_info("ftrace faulted on modifying ");
1582 FTRACE_WARN_ON_ONCE(1);
1583 pr_info("ftrace failed to modify ");
1585 print_ip_ins(" actual: ", (unsigned char *)ip);
1586 printk(KERN_CONT "\n");
1589 FTRACE_WARN_ON_ONCE(1);
1590 pr_info("ftrace faulted on writing ");
1594 FTRACE_WARN_ON_ONCE(1);
1595 pr_info("ftrace faulted on unknown error ");
1601 /* Return 1 if the address range is reserved for ftrace */
1602 int ftrace_text_reserved(void *start, void *end)
1604 struct dyn_ftrace *rec;
1605 struct ftrace_page *pg;
1607 do_for_each_ftrace_rec(pg, rec) {
1608 if (rec->ip <= (unsigned long)end &&
1609 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1611 } while_for_each_ftrace_rec();
1615 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1617 unsigned long flag = 0UL;
1620 * If we are updating calls:
1622 * If the record has a ref count, then we need to enable it
1623 * because someone is using it.
1625 * Otherwise we make sure its disabled.
1627 * If we are disabling calls, then disable all records that
1630 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1631 flag = FTRACE_FL_ENABLED;
1633 /* If the state of this record hasn't changed, then do nothing */
1634 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1635 return FTRACE_UPDATE_IGNORE;
1639 rec->flags |= FTRACE_FL_ENABLED;
1640 return FTRACE_UPDATE_MAKE_CALL;
1644 rec->flags &= ~FTRACE_FL_ENABLED;
1646 return FTRACE_UPDATE_MAKE_NOP;
1650 * ftrace_update_record, set a record that now is tracing or not
1651 * @rec: the record to update
1652 * @enable: set to 1 if the record is tracing, zero to force disable
1654 * The records that represent all functions that can be traced need
1655 * to be updated when tracing has been enabled.
1657 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1659 return ftrace_check_record(rec, enable, 1);
1663 * ftrace_test_record, check if the record has been enabled or not
1664 * @rec: the record to test
1665 * @enable: set to 1 to check if enabled, 0 if it is disabled
1667 * The arch code may need to test if a record is already set to
1668 * tracing to determine how to modify the function code that it
1671 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1673 return ftrace_check_record(rec, enable, 0);
1677 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1679 unsigned long ftrace_addr;
1682 ftrace_addr = (unsigned long)FTRACE_ADDR;
1684 ret = ftrace_update_record(rec, enable);
1687 case FTRACE_UPDATE_IGNORE:
1690 case FTRACE_UPDATE_MAKE_CALL:
1691 return ftrace_make_call(rec, ftrace_addr);
1693 case FTRACE_UPDATE_MAKE_NOP:
1694 return ftrace_make_nop(NULL, rec, ftrace_addr);
1697 return -1; /* unknow ftrace bug */
1700 static void ftrace_replace_code(int update)
1702 struct dyn_ftrace *rec;
1703 struct ftrace_page *pg;
1706 if (unlikely(ftrace_disabled))
1709 do_for_each_ftrace_rec(pg, rec) {
1710 failed = __ftrace_replace_code(rec, update);
1712 ftrace_bug(failed, rec->ip);
1713 /* Stop processing */
1716 } while_for_each_ftrace_rec();
1719 struct ftrace_rec_iter {
1720 struct ftrace_page *pg;
1725 * ftrace_rec_iter_start, start up iterating over traced functions
1727 * Returns an iterator handle that is used to iterate over all
1728 * the records that represent address locations where functions
1731 * May return NULL if no records are available.
1733 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1736 * We only use a single iterator.
1737 * Protected by the ftrace_lock mutex.
1739 static struct ftrace_rec_iter ftrace_rec_iter;
1740 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1742 iter->pg = ftrace_pages_start;
1745 /* Could have empty pages */
1746 while (iter->pg && !iter->pg->index)
1747 iter->pg = iter->pg->next;
1756 * ftrace_rec_iter_next, get the next record to process.
1757 * @iter: The handle to the iterator.
1759 * Returns the next iterator after the given iterator @iter.
1761 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1765 if (iter->index >= iter->pg->index) {
1766 iter->pg = iter->pg->next;
1769 /* Could have empty pages */
1770 while (iter->pg && !iter->pg->index)
1771 iter->pg = iter->pg->next;
1781 * ftrace_rec_iter_record, get the record at the iterator location
1782 * @iter: The current iterator location
1784 * Returns the record that the current @iter is at.
1786 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1788 return &iter->pg->records[iter->index];
1792 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1799 if (unlikely(ftrace_disabled))
1802 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1804 ftrace_bug(ret, ip);
1811 * archs can override this function if they must do something
1812 * before the modifying code is performed.
1814 int __weak ftrace_arch_code_modify_prepare(void)
1820 * archs can override this function if they must do something
1821 * after the modifying code is performed.
1823 int __weak ftrace_arch_code_modify_post_process(void)
1828 static int __ftrace_modify_code(void *data)
1830 int *command = data;
1832 if (*command & FTRACE_UPDATE_CALLS)
1833 ftrace_replace_code(1);
1834 else if (*command & FTRACE_DISABLE_CALLS)
1835 ftrace_replace_code(0);
1837 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1838 ftrace_update_ftrace_func(ftrace_trace_function);
1840 if (*command & FTRACE_START_FUNC_RET)
1841 ftrace_enable_ftrace_graph_caller();
1842 else if (*command & FTRACE_STOP_FUNC_RET)
1843 ftrace_disable_ftrace_graph_caller();
1849 * ftrace_run_stop_machine, go back to the stop machine method
1850 * @command: The command to tell ftrace what to do
1852 * If an arch needs to fall back to the stop machine method, the
1853 * it can call this function.
1855 void ftrace_run_stop_machine(int command)
1857 stop_machine(__ftrace_modify_code, &command, NULL);
1861 * arch_ftrace_update_code, modify the code to trace or not trace
1862 * @command: The command that needs to be done
1864 * Archs can override this function if it does not need to
1865 * run stop_machine() to modify code.
1867 void __weak arch_ftrace_update_code(int command)
1869 ftrace_run_stop_machine(command);
1872 static void ftrace_run_update_code(int command)
1876 ret = ftrace_arch_code_modify_prepare();
1877 FTRACE_WARN_ON(ret);
1881 * Do not call function tracer while we update the code.
1882 * We are in stop machine.
1884 function_trace_stop++;
1887 * By default we use stop_machine() to modify the code.
1888 * But archs can do what ever they want as long as it
1889 * is safe. The stop_machine() is the safest, but also
1890 * produces the most overhead.
1892 arch_ftrace_update_code(command);
1894 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1896 * For archs that call ftrace_test_stop_func(), we must
1897 * wait till after we update all the function callers
1898 * before we update the callback. This keeps different
1899 * ops that record different functions from corrupting
1902 __ftrace_trace_function = __ftrace_trace_function_delay;
1904 function_trace_stop--;
1906 ret = ftrace_arch_code_modify_post_process();
1907 FTRACE_WARN_ON(ret);
1910 static ftrace_func_t saved_ftrace_func;
1911 static int ftrace_start_up;
1912 static int global_start_up;
1914 static void ftrace_startup_enable(int command)
1916 if (saved_ftrace_func != ftrace_trace_function) {
1917 saved_ftrace_func = ftrace_trace_function;
1918 command |= FTRACE_UPDATE_TRACE_FUNC;
1921 if (!command || !ftrace_enabled)
1924 ftrace_run_update_code(command);
1927 static int ftrace_startup(struct ftrace_ops *ops, int command)
1929 bool hash_enable = true;
1931 if (unlikely(ftrace_disabled))
1935 command |= FTRACE_UPDATE_CALLS;
1937 /* ops marked global share the filter hashes */
1938 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1940 /* Don't update hash if global is already set */
1941 if (global_start_up)
1942 hash_enable = false;
1946 ops->flags |= FTRACE_OPS_FL_ENABLED;
1948 ftrace_hash_rec_enable(ops, 1);
1950 ftrace_startup_enable(command);
1955 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
1957 bool hash_disable = true;
1959 if (unlikely(ftrace_disabled))
1964 * Just warn in case of unbalance, no need to kill ftrace, it's not
1965 * critical but the ftrace_call callers may be never nopped again after
1966 * further ftrace uses.
1968 WARN_ON_ONCE(ftrace_start_up < 0);
1970 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1973 WARN_ON_ONCE(global_start_up < 0);
1974 /* Don't update hash if global still has users */
1975 if (global_start_up) {
1976 WARN_ON_ONCE(!ftrace_start_up);
1977 hash_disable = false;
1982 ftrace_hash_rec_disable(ops, 1);
1984 if (ops != &global_ops || !global_start_up)
1985 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1987 command |= FTRACE_UPDATE_CALLS;
1989 if (saved_ftrace_func != ftrace_trace_function) {
1990 saved_ftrace_func = ftrace_trace_function;
1991 command |= FTRACE_UPDATE_TRACE_FUNC;
1994 if (!command || !ftrace_enabled)
1997 ftrace_run_update_code(command);
2000 static void ftrace_startup_sysctl(void)
2002 if (unlikely(ftrace_disabled))
2005 /* Force update next time */
2006 saved_ftrace_func = NULL;
2007 /* ftrace_start_up is true if we want ftrace running */
2008 if (ftrace_start_up)
2009 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2012 static void ftrace_shutdown_sysctl(void)
2014 if (unlikely(ftrace_disabled))
2017 /* ftrace_start_up is true if ftrace is running */
2018 if (ftrace_start_up)
2019 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2022 static cycle_t ftrace_update_time;
2023 static unsigned long ftrace_update_cnt;
2024 unsigned long ftrace_update_tot_cnt;
2026 static int ops_traces_mod(struct ftrace_ops *ops)
2028 struct ftrace_hash *hash;
2030 hash = ops->filter_hash;
2031 return ftrace_hash_empty(hash);
2034 static int ftrace_update_code(struct module *mod)
2036 struct ftrace_page *pg;
2037 struct dyn_ftrace *p;
2038 cycle_t start, stop;
2039 unsigned long ref = 0;
2043 * When adding a module, we need to check if tracers are
2044 * currently enabled and if they are set to trace all functions.
2045 * If they are, we need to enable the module functions as well
2046 * as update the reference counts for those function records.
2049 struct ftrace_ops *ops;
2051 for (ops = ftrace_ops_list;
2052 ops != &ftrace_list_end; ops = ops->next) {
2053 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2054 ops_traces_mod(ops))
2059 start = ftrace_now(raw_smp_processor_id());
2060 ftrace_update_cnt = 0;
2062 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2064 for (i = 0; i < pg->index; i++) {
2065 /* If something went wrong, bail without enabling anything */
2066 if (unlikely(ftrace_disabled))
2069 p = &pg->records[i];
2073 * Do the initial record conversion from mcount jump
2074 * to the NOP instructions.
2076 if (!ftrace_code_disable(mod, p))
2079 ftrace_update_cnt++;
2082 * If the tracing is enabled, go ahead and enable the record.
2084 * The reason not to enable the record immediatelly is the
2085 * inherent check of ftrace_make_nop/ftrace_make_call for
2086 * correct previous instructions. Making first the NOP
2087 * conversion puts the module to the correct state, thus
2088 * passing the ftrace_make_call check.
2090 if (ftrace_start_up && ref) {
2091 int failed = __ftrace_replace_code(p, 1);
2093 ftrace_bug(failed, p->ip);
2098 ftrace_new_pgs = NULL;
2100 stop = ftrace_now(raw_smp_processor_id());
2101 ftrace_update_time = stop - start;
2102 ftrace_update_tot_cnt += ftrace_update_cnt;
2107 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2112 if (WARN_ON(!count))
2115 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2118 * We want to fill as much as possible. No more than a page
2121 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2125 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2128 /* if we can't allocate this size, try something smaller */
2135 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2144 static struct ftrace_page *
2145 ftrace_allocate_pages(unsigned long num_to_init)
2147 struct ftrace_page *start_pg;
2148 struct ftrace_page *pg;
2155 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2160 * Try to allocate as much as possible in one continues
2161 * location that fills in all of the space. We want to
2162 * waste as little space as possible.
2165 cnt = ftrace_allocate_records(pg, num_to_init);
2173 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2184 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2185 free_pages((unsigned long)pg->records, order);
2186 start_pg = pg->next;
2190 pr_info("ftrace: FAILED to allocate memory for functions\n");
2194 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2199 pr_info("ftrace: No functions to be traced?\n");
2203 cnt = num_to_init / ENTRIES_PER_PAGE;
2204 pr_info("ftrace: allocating %ld entries in %d pages\n",
2205 num_to_init, cnt + 1);
2210 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2212 struct ftrace_iterator {
2215 struct ftrace_page *pg;
2216 struct dyn_ftrace *func;
2217 struct ftrace_func_probe *probe;
2218 struct trace_parser parser;
2219 struct ftrace_hash *hash;
2220 struct ftrace_ops *ops;
2227 t_hash_next(struct seq_file *m, loff_t *pos)
2229 struct ftrace_iterator *iter = m->private;
2230 struct hlist_node *hnd = NULL;
2231 struct hlist_head *hhd;
2237 hnd = &iter->probe->node;
2239 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2242 hhd = &ftrace_func_hash[iter->hidx];
2244 if (hlist_empty(hhd)) {
2260 if (WARN_ON_ONCE(!hnd))
2263 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2268 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2270 struct ftrace_iterator *iter = m->private;
2274 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2277 if (iter->func_pos > *pos)
2281 for (l = 0; l <= (*pos - iter->func_pos); ) {
2282 p = t_hash_next(m, &l);
2289 /* Only set this if we have an item */
2290 iter->flags |= FTRACE_ITER_HASH;
2296 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2298 struct ftrace_func_probe *rec;
2301 if (WARN_ON_ONCE(!rec))
2304 if (rec->ops->print)
2305 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2307 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2310 seq_printf(m, ":%p", rec->data);
2317 t_next(struct seq_file *m, void *v, loff_t *pos)
2319 struct ftrace_iterator *iter = m->private;
2320 struct ftrace_ops *ops = iter->ops;
2321 struct dyn_ftrace *rec = NULL;
2323 if (unlikely(ftrace_disabled))
2326 if (iter->flags & FTRACE_ITER_HASH)
2327 return t_hash_next(m, pos);
2330 iter->pos = iter->func_pos = *pos;
2332 if (iter->flags & FTRACE_ITER_PRINTALL)
2333 return t_hash_start(m, pos);
2336 if (iter->idx >= iter->pg->index) {
2337 if (iter->pg->next) {
2338 iter->pg = iter->pg->next;
2343 rec = &iter->pg->records[iter->idx++];
2344 if (((iter->flags & FTRACE_ITER_FILTER) &&
2345 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2347 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2348 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2350 ((iter->flags & FTRACE_ITER_ENABLED) &&
2351 !(rec->flags & ~FTRACE_FL_MASK))) {
2359 return t_hash_start(m, pos);
2366 static void reset_iter_read(struct ftrace_iterator *iter)
2370 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
2373 static void *t_start(struct seq_file *m, loff_t *pos)
2375 struct ftrace_iterator *iter = m->private;
2376 struct ftrace_ops *ops = iter->ops;
2380 mutex_lock(&ftrace_lock);
2382 if (unlikely(ftrace_disabled))
2386 * If an lseek was done, then reset and start from beginning.
2388 if (*pos < iter->pos)
2389 reset_iter_read(iter);
2392 * For set_ftrace_filter reading, if we have the filter
2393 * off, we can short cut and just print out that all
2394 * functions are enabled.
2396 if (iter->flags & FTRACE_ITER_FILTER &&
2397 ftrace_hash_empty(ops->filter_hash)) {
2399 return t_hash_start(m, pos);
2400 iter->flags |= FTRACE_ITER_PRINTALL;
2401 /* reset in case of seek/pread */
2402 iter->flags &= ~FTRACE_ITER_HASH;
2406 if (iter->flags & FTRACE_ITER_HASH)
2407 return t_hash_start(m, pos);
2410 * Unfortunately, we need to restart at ftrace_pages_start
2411 * every time we let go of the ftrace_mutex. This is because
2412 * those pointers can change without the lock.
2414 iter->pg = ftrace_pages_start;
2416 for (l = 0; l <= *pos; ) {
2417 p = t_next(m, p, &l);
2423 return t_hash_start(m, pos);
2428 static void t_stop(struct seq_file *m, void *p)
2430 mutex_unlock(&ftrace_lock);
2433 static int t_show(struct seq_file *m, void *v)
2435 struct ftrace_iterator *iter = m->private;
2436 struct dyn_ftrace *rec;
2438 if (iter->flags & FTRACE_ITER_HASH)
2439 return t_hash_show(m, iter);
2441 if (iter->flags & FTRACE_ITER_PRINTALL) {
2442 seq_printf(m, "#### all functions enabled ####\n");
2451 seq_printf(m, "%ps", (void *)rec->ip);
2452 if (iter->flags & FTRACE_ITER_ENABLED)
2453 seq_printf(m, " (%ld)",
2454 rec->flags & ~FTRACE_FL_MASK);
2455 seq_printf(m, "\n");
2460 static const struct seq_operations show_ftrace_seq_ops = {
2468 ftrace_avail_open(struct inode *inode, struct file *file)
2470 struct ftrace_iterator *iter;
2473 if (unlikely(ftrace_disabled))
2476 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2480 iter->pg = ftrace_pages_start;
2481 iter->ops = &global_ops;
2483 ret = seq_open(file, &show_ftrace_seq_ops);
2485 struct seq_file *m = file->private_data;
2496 ftrace_enabled_open(struct inode *inode, struct file *file)
2498 struct ftrace_iterator *iter;
2501 if (unlikely(ftrace_disabled))
2504 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2508 iter->pg = ftrace_pages_start;
2509 iter->flags = FTRACE_ITER_ENABLED;
2510 iter->ops = &global_ops;
2512 ret = seq_open(file, &show_ftrace_seq_ops);
2514 struct seq_file *m = file->private_data;
2524 static void ftrace_filter_reset(struct ftrace_hash *hash)
2526 mutex_lock(&ftrace_lock);
2527 ftrace_hash_clear(hash);
2528 mutex_unlock(&ftrace_lock);
2532 * ftrace_regex_open - initialize function tracer filter files
2533 * @ops: The ftrace_ops that hold the hash filters
2534 * @flag: The type of filter to process
2535 * @inode: The inode, usually passed in to your open routine
2536 * @file: The file, usually passed in to your open routine
2538 * ftrace_regex_open() initializes the filter files for the
2539 * @ops. Depending on @flag it may process the filter hash or
2540 * the notrace hash of @ops. With this called from the open
2541 * routine, you can use ftrace_filter_write() for the write
2542 * routine if @flag has FTRACE_ITER_FILTER set, or
2543 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2544 * ftrace_regex_lseek() should be used as the lseek routine, and
2545 * release must call ftrace_regex_release().
2548 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2549 struct inode *inode, struct file *file)
2551 struct ftrace_iterator *iter;
2552 struct ftrace_hash *hash;
2555 if (unlikely(ftrace_disabled))
2558 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2562 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2567 if (flag & FTRACE_ITER_NOTRACE)
2568 hash = ops->notrace_hash;
2570 hash = ops->filter_hash;
2575 if (file->f_mode & FMODE_WRITE) {
2576 mutex_lock(&ftrace_lock);
2577 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2578 mutex_unlock(&ftrace_lock);
2581 trace_parser_put(&iter->parser);
2587 mutex_lock(&ftrace_regex_lock);
2589 if ((file->f_mode & FMODE_WRITE) &&
2590 (file->f_flags & O_TRUNC))
2591 ftrace_filter_reset(iter->hash);
2593 if (file->f_mode & FMODE_READ) {
2594 iter->pg = ftrace_pages_start;
2596 ret = seq_open(file, &show_ftrace_seq_ops);
2598 struct seq_file *m = file->private_data;
2602 free_ftrace_hash(iter->hash);
2603 trace_parser_put(&iter->parser);
2607 file->private_data = iter;
2608 mutex_unlock(&ftrace_regex_lock);
2614 ftrace_filter_open(struct inode *inode, struct file *file)
2616 return ftrace_regex_open(&global_ops,
2617 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2622 ftrace_notrace_open(struct inode *inode, struct file *file)
2624 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2629 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
2633 if (file->f_mode & FMODE_READ)
2634 ret = seq_lseek(file, offset, origin);
2636 file->f_pos = ret = 1;
2641 static int ftrace_match(char *str, char *regex, int len, int type)
2648 if (strcmp(str, regex) == 0)
2651 case MATCH_FRONT_ONLY:
2652 if (strncmp(str, regex, len) == 0)
2655 case MATCH_MIDDLE_ONLY:
2656 if (strstr(str, regex))
2659 case MATCH_END_ONLY:
2661 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2670 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2672 struct ftrace_func_entry *entry;
2675 entry = ftrace_lookup_ip(hash, rec->ip);
2677 /* Do nothing if it doesn't exist */
2681 free_hash_entry(hash, entry);
2683 /* Do nothing if it exists */
2687 ret = add_hash_entry(hash, rec->ip);
2693 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2694 char *regex, int len, int type)
2696 char str[KSYM_SYMBOL_LEN];
2699 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2702 /* module lookup requires matching the module */
2703 if (!modname || strcmp(modname, mod))
2706 /* blank search means to match all funcs in the mod */
2711 return ftrace_match(str, regex, len, type);
2715 match_records(struct ftrace_hash *hash, char *buff,
2716 int len, char *mod, int not)
2718 unsigned search_len = 0;
2719 struct ftrace_page *pg;
2720 struct dyn_ftrace *rec;
2721 int type = MATCH_FULL;
2722 char *search = buff;
2727 type = filter_parse_regex(buff, len, &search, ¬);
2728 search_len = strlen(search);
2731 mutex_lock(&ftrace_lock);
2733 if (unlikely(ftrace_disabled))
2736 do_for_each_ftrace_rec(pg, rec) {
2737 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2738 ret = enter_record(hash, rec, not);
2745 } while_for_each_ftrace_rec();
2747 mutex_unlock(&ftrace_lock);
2753 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2755 return match_records(hash, buff, len, NULL, 0);
2759 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2763 /* blank or '*' mean the same */
2764 if (strcmp(buff, "*") == 0)
2767 /* handle the case of 'dont filter this module' */
2768 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2773 return match_records(hash, buff, strlen(buff), mod, not);
2777 * We register the module command as a template to show others how
2778 * to register the a command as well.
2782 ftrace_mod_callback(struct ftrace_hash *hash,
2783 char *func, char *cmd, char *param, int enable)
2789 * cmd == 'mod' because we only registered this func
2790 * for the 'mod' ftrace_func_command.
2791 * But if you register one func with multiple commands,
2792 * you can tell which command was used by the cmd
2796 /* we must have a module name */
2800 mod = strsep(¶m, ":");
2804 ret = ftrace_match_module_records(hash, func, mod);
2813 static struct ftrace_func_command ftrace_mod_cmd = {
2815 .func = ftrace_mod_callback,
2818 static int __init ftrace_mod_cmd_init(void)
2820 return register_ftrace_command(&ftrace_mod_cmd);
2822 device_initcall(ftrace_mod_cmd_init);
2825 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2827 struct ftrace_func_probe *entry;
2828 struct hlist_head *hhd;
2829 struct hlist_node *n;
2832 key = hash_long(ip, FTRACE_HASH_BITS);
2834 hhd = &ftrace_func_hash[key];
2836 if (hlist_empty(hhd))
2840 * Disable preemption for these calls to prevent a RCU grace
2841 * period. This syncs the hash iteration and freeing of items
2842 * on the hash. rcu_read_lock is too dangerous here.
2844 preempt_disable_notrace();
2845 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2846 if (entry->ip == ip)
2847 entry->ops->func(ip, parent_ip, &entry->data);
2849 preempt_enable_notrace();
2852 static struct ftrace_ops trace_probe_ops __read_mostly =
2854 .func = function_trace_probe_call,
2857 static int ftrace_probe_registered;
2859 static void __enable_ftrace_function_probe(void)
2864 if (ftrace_probe_registered)
2867 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2868 struct hlist_head *hhd = &ftrace_func_hash[i];
2872 /* Nothing registered? */
2873 if (i == FTRACE_FUNC_HASHSIZE)
2876 ret = __register_ftrace_function(&trace_probe_ops);
2878 ret = ftrace_startup(&trace_probe_ops, 0);
2880 ftrace_probe_registered = 1;
2883 static void __disable_ftrace_function_probe(void)
2888 if (!ftrace_probe_registered)
2891 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2892 struct hlist_head *hhd = &ftrace_func_hash[i];
2897 /* no more funcs left */
2898 ret = __unregister_ftrace_function(&trace_probe_ops);
2900 ftrace_shutdown(&trace_probe_ops, 0);
2902 ftrace_probe_registered = 0;
2906 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2908 struct ftrace_func_probe *entry =
2909 container_of(rhp, struct ftrace_func_probe, rcu);
2911 if (entry->ops->free)
2912 entry->ops->free(&entry->data);
2918 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2921 struct ftrace_func_probe *entry;
2922 struct ftrace_page *pg;
2923 struct dyn_ftrace *rec;
2929 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2930 len = strlen(search);
2932 /* we do not support '!' for function probes */
2936 mutex_lock(&ftrace_lock);
2938 if (unlikely(ftrace_disabled))
2941 do_for_each_ftrace_rec(pg, rec) {
2943 if (!ftrace_match_record(rec, NULL, search, len, type))
2946 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2948 /* If we did not process any, then return error */
2959 * The caller might want to do something special
2960 * for each function we find. We call the callback
2961 * to give the caller an opportunity to do so.
2963 if (ops->callback) {
2964 if (ops->callback(rec->ip, &entry->data) < 0) {
2965 /* caller does not like this func */
2972 entry->ip = rec->ip;
2974 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2975 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2977 } while_for_each_ftrace_rec();
2978 __enable_ftrace_function_probe();
2981 mutex_unlock(&ftrace_lock);
2987 PROBE_TEST_FUNC = 1,
2992 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2993 void *data, int flags)
2995 struct ftrace_func_probe *entry;
2996 struct hlist_node *n, *tmp;
2997 char str[KSYM_SYMBOL_LEN];
2998 int type = MATCH_FULL;
3002 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3007 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3008 len = strlen(search);
3010 /* we do not support '!' for function probes */
3015 mutex_lock(&ftrace_lock);
3016 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3017 struct hlist_head *hhd = &ftrace_func_hash[i];
3019 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3021 /* break up if statements for readability */
3022 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3025 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3028 /* do this last, since it is the most expensive */
3030 kallsyms_lookup(entry->ip, NULL, NULL,
3032 if (!ftrace_match(str, glob, len, type))
3036 hlist_del(&entry->node);
3037 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3040 __disable_ftrace_function_probe();
3041 mutex_unlock(&ftrace_lock);
3045 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3048 __unregister_ftrace_function_probe(glob, ops, data,
3049 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3053 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3055 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3058 void unregister_ftrace_function_probe_all(char *glob)
3060 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3063 static LIST_HEAD(ftrace_commands);
3064 static DEFINE_MUTEX(ftrace_cmd_mutex);
3066 int register_ftrace_command(struct ftrace_func_command *cmd)
3068 struct ftrace_func_command *p;
3071 mutex_lock(&ftrace_cmd_mutex);
3072 list_for_each_entry(p, &ftrace_commands, list) {
3073 if (strcmp(cmd->name, p->name) == 0) {
3078 list_add(&cmd->list, &ftrace_commands);
3080 mutex_unlock(&ftrace_cmd_mutex);
3085 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3087 struct ftrace_func_command *p, *n;
3090 mutex_lock(&ftrace_cmd_mutex);
3091 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3092 if (strcmp(cmd->name, p->name) == 0) {
3094 list_del_init(&p->list);
3099 mutex_unlock(&ftrace_cmd_mutex);
3104 static int ftrace_process_regex(struct ftrace_hash *hash,
3105 char *buff, int len, int enable)
3107 char *func, *command, *next = buff;
3108 struct ftrace_func_command *p;
3111 func = strsep(&next, ":");
3114 ret = ftrace_match_records(hash, func, len);
3124 command = strsep(&next, ":");
3126 mutex_lock(&ftrace_cmd_mutex);
3127 list_for_each_entry(p, &ftrace_commands, list) {
3128 if (strcmp(p->name, command) == 0) {
3129 ret = p->func(hash, func, command, next, enable);
3134 mutex_unlock(&ftrace_cmd_mutex);
3140 ftrace_regex_write(struct file *file, const char __user *ubuf,
3141 size_t cnt, loff_t *ppos, int enable)
3143 struct ftrace_iterator *iter;
3144 struct trace_parser *parser;
3150 mutex_lock(&ftrace_regex_lock);
3153 if (unlikely(ftrace_disabled))
3156 if (file->f_mode & FMODE_READ) {
3157 struct seq_file *m = file->private_data;
3160 iter = file->private_data;
3162 parser = &iter->parser;
3163 read = trace_get_user(parser, ubuf, cnt, ppos);
3165 if (read >= 0 && trace_parser_loaded(parser) &&
3166 !trace_parser_cont(parser)) {
3167 ret = ftrace_process_regex(iter->hash, parser->buffer,
3168 parser->idx, enable);
3169 trace_parser_clear(parser);
3176 mutex_unlock(&ftrace_regex_lock);
3182 ftrace_filter_write(struct file *file, const char __user *ubuf,
3183 size_t cnt, loff_t *ppos)
3185 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3189 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3190 size_t cnt, loff_t *ppos)
3192 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3196 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3197 int reset, int enable)
3199 struct ftrace_hash **orig_hash;
3200 struct ftrace_hash *hash;
3203 /* All global ops uses the global ops filters */
3204 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3207 if (unlikely(ftrace_disabled))
3211 orig_hash = &ops->filter_hash;
3213 orig_hash = &ops->notrace_hash;
3215 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3219 mutex_lock(&ftrace_regex_lock);
3221 ftrace_filter_reset(hash);
3222 if (buf && !ftrace_match_records(hash, buf, len)) {
3224 goto out_regex_unlock;
3227 mutex_lock(&ftrace_lock);
3228 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3229 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3231 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3233 mutex_unlock(&ftrace_lock);
3236 mutex_unlock(&ftrace_regex_lock);
3238 free_ftrace_hash(hash);
3243 * ftrace_set_filter - set a function to filter on in ftrace
3244 * @ops - the ops to set the filter with
3245 * @buf - the string that holds the function filter text.
3246 * @len - the length of the string.
3247 * @reset - non zero to reset all filters before applying this filter.
3249 * Filters denote which functions should be enabled when tracing is enabled.
3250 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3252 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3255 return ftrace_set_regex(ops, buf, len, reset, 1);
3257 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3260 * ftrace_set_notrace - set a function to not trace in ftrace
3261 * @ops - the ops to set the notrace filter with
3262 * @buf - the string that holds the function notrace text.
3263 * @len - the length of the string.
3264 * @reset - non zero to reset all filters before applying this filter.
3266 * Notrace Filters denote which functions should not be enabled when tracing
3267 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3270 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3273 return ftrace_set_regex(ops, buf, len, reset, 0);
3275 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3277 * ftrace_set_filter - set a function to filter on in ftrace
3278 * @ops - the ops to set the filter with
3279 * @buf - the string that holds the function filter text.
3280 * @len - the length of the string.
3281 * @reset - non zero to reset all filters before applying this filter.
3283 * Filters denote which functions should be enabled when tracing is enabled.
3284 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3286 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3288 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3290 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3293 * ftrace_set_notrace - set a function to not trace in ftrace
3294 * @ops - the ops to set the notrace filter with
3295 * @buf - the string that holds the function notrace text.
3296 * @len - the length of the string.
3297 * @reset - non zero to reset all filters before applying this filter.
3299 * Notrace Filters denote which functions should not be enabled when tracing
3300 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3303 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3305 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3307 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3310 * command line interface to allow users to set filters on boot up.
3312 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3313 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3314 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3316 static int __init set_ftrace_notrace(char *str)
3318 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3321 __setup("ftrace_notrace=", set_ftrace_notrace);
3323 static int __init set_ftrace_filter(char *str)
3325 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3328 __setup("ftrace_filter=", set_ftrace_filter);
3330 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3331 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3332 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3334 static int __init set_graph_function(char *str)
3336 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3339 __setup("ftrace_graph_filter=", set_graph_function);
3341 static void __init set_ftrace_early_graph(char *buf)
3347 func = strsep(&buf, ",");
3348 /* we allow only one expression at a time */
3349 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3352 printk(KERN_DEBUG "ftrace: function %s not "
3353 "traceable\n", func);
3356 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3359 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3364 func = strsep(&buf, ",");
3365 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3369 static void __init set_ftrace_early_filters(void)
3371 if (ftrace_filter_buf[0])
3372 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3373 if (ftrace_notrace_buf[0])
3374 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3375 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3376 if (ftrace_graph_buf[0])
3377 set_ftrace_early_graph(ftrace_graph_buf);
3378 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3381 int ftrace_regex_release(struct inode *inode, struct file *file)
3383 struct seq_file *m = (struct seq_file *)file->private_data;
3384 struct ftrace_iterator *iter;
3385 struct ftrace_hash **orig_hash;
3386 struct trace_parser *parser;
3390 mutex_lock(&ftrace_regex_lock);
3391 if (file->f_mode & FMODE_READ) {
3394 seq_release(inode, file);
3396 iter = file->private_data;
3398 parser = &iter->parser;
3399 if (trace_parser_loaded(parser)) {
3400 parser->buffer[parser->idx] = 0;
3401 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3404 trace_parser_put(parser);
3406 if (file->f_mode & FMODE_WRITE) {
3407 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3410 orig_hash = &iter->ops->filter_hash;
3412 orig_hash = &iter->ops->notrace_hash;
3414 mutex_lock(&ftrace_lock);
3415 ret = ftrace_hash_move(iter->ops, filter_hash,
3416 orig_hash, iter->hash);
3417 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3419 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3421 mutex_unlock(&ftrace_lock);
3423 free_ftrace_hash(iter->hash);
3426 mutex_unlock(&ftrace_regex_lock);
3430 static const struct file_operations ftrace_avail_fops = {
3431 .open = ftrace_avail_open,
3433 .llseek = seq_lseek,
3434 .release = seq_release_private,
3437 static const struct file_operations ftrace_enabled_fops = {
3438 .open = ftrace_enabled_open,
3440 .llseek = seq_lseek,
3441 .release = seq_release_private,
3444 static const struct file_operations ftrace_filter_fops = {
3445 .open = ftrace_filter_open,
3447 .write = ftrace_filter_write,
3448 .llseek = ftrace_regex_lseek,
3449 .release = ftrace_regex_release,
3452 static const struct file_operations ftrace_notrace_fops = {
3453 .open = ftrace_notrace_open,
3455 .write = ftrace_notrace_write,
3456 .llseek = ftrace_regex_lseek,
3457 .release = ftrace_regex_release,
3460 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3462 static DEFINE_MUTEX(graph_lock);
3464 int ftrace_graph_count;
3465 int ftrace_graph_filter_enabled;
3466 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3469 __g_next(struct seq_file *m, loff_t *pos)
3471 if (*pos >= ftrace_graph_count)
3473 return &ftrace_graph_funcs[*pos];
3477 g_next(struct seq_file *m, void *v, loff_t *pos)
3480 return __g_next(m, pos);
3483 static void *g_start(struct seq_file *m, loff_t *pos)
3485 mutex_lock(&graph_lock);
3487 /* Nothing, tell g_show to print all functions are enabled */
3488 if (!ftrace_graph_filter_enabled && !*pos)
3491 return __g_next(m, pos);
3494 static void g_stop(struct seq_file *m, void *p)
3496 mutex_unlock(&graph_lock);
3499 static int g_show(struct seq_file *m, void *v)
3501 unsigned long *ptr = v;
3506 if (ptr == (unsigned long *)1) {
3507 seq_printf(m, "#### all functions enabled ####\n");
3511 seq_printf(m, "%ps\n", (void *)*ptr);
3516 static const struct seq_operations ftrace_graph_seq_ops = {
3524 ftrace_graph_open(struct inode *inode, struct file *file)
3528 if (unlikely(ftrace_disabled))
3531 mutex_lock(&graph_lock);
3532 if ((file->f_mode & FMODE_WRITE) &&
3533 (file->f_flags & O_TRUNC)) {
3534 ftrace_graph_filter_enabled = 0;
3535 ftrace_graph_count = 0;
3536 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3538 mutex_unlock(&graph_lock);
3540 if (file->f_mode & FMODE_READ)
3541 ret = seq_open(file, &ftrace_graph_seq_ops);
3547 ftrace_graph_release(struct inode *inode, struct file *file)
3549 if (file->f_mode & FMODE_READ)
3550 seq_release(inode, file);
3555 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3557 struct dyn_ftrace *rec;
3558 struct ftrace_page *pg;
3567 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3568 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3571 search_len = strlen(search);
3573 mutex_lock(&ftrace_lock);
3575 if (unlikely(ftrace_disabled)) {
3576 mutex_unlock(&ftrace_lock);
3580 do_for_each_ftrace_rec(pg, rec) {
3582 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3583 /* if it is in the array */
3585 for (i = 0; i < *idx; i++) {
3586 if (array[i] == rec->ip) {
3595 array[(*idx)++] = rec->ip;
3596 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3601 array[i] = array[--(*idx)];
3607 } while_for_each_ftrace_rec();
3609 mutex_unlock(&ftrace_lock);
3614 ftrace_graph_filter_enabled = 1;
3619 ftrace_graph_write(struct file *file, const char __user *ubuf,
3620 size_t cnt, loff_t *ppos)
3622 struct trace_parser parser;
3628 mutex_lock(&graph_lock);
3630 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3635 read = trace_get_user(&parser, ubuf, cnt, ppos);
3637 if (read >= 0 && trace_parser_loaded((&parser))) {
3638 parser.buffer[parser.idx] = 0;
3640 /* we allow only one expression at a time */
3641 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3650 trace_parser_put(&parser);
3652 mutex_unlock(&graph_lock);
3657 static const struct file_operations ftrace_graph_fops = {
3658 .open = ftrace_graph_open,
3660 .write = ftrace_graph_write,
3661 .release = ftrace_graph_release,
3662 .llseek = seq_lseek,
3664 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3666 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3669 trace_create_file("available_filter_functions", 0444,
3670 d_tracer, NULL, &ftrace_avail_fops);
3672 trace_create_file("enabled_functions", 0444,
3673 d_tracer, NULL, &ftrace_enabled_fops);
3675 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3676 NULL, &ftrace_filter_fops);
3678 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3679 NULL, &ftrace_notrace_fops);
3681 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3682 trace_create_file("set_graph_function", 0444, d_tracer,
3684 &ftrace_graph_fops);
3685 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3690 static void ftrace_swap_recs(void *a, void *b, int size)
3692 struct dyn_ftrace *reca = a;
3693 struct dyn_ftrace *recb = b;
3694 struct dyn_ftrace t;
3701 static int ftrace_process_locs(struct module *mod,
3702 unsigned long *start,
3705 struct ftrace_page *pg;
3706 unsigned long count;
3709 unsigned long flags = 0; /* Shut up gcc */
3712 count = end - start;
3717 pg = ftrace_allocate_pages(count);
3721 mutex_lock(&ftrace_lock);
3724 * Core and each module needs their own pages, as
3725 * modules will free them when they are removed.
3726 * Force a new page to be allocated for modules.
3729 WARN_ON(ftrace_pages || ftrace_pages_start);
3730 /* First initialization */
3731 ftrace_pages = ftrace_pages_start = pg;
3736 if (WARN_ON(ftrace_pages->next)) {
3737 /* Hmm, we have free pages? */
3738 while (ftrace_pages->next)
3739 ftrace_pages = ftrace_pages->next;
3742 ftrace_pages->next = pg;
3748 addr = ftrace_call_adjust(*p++);
3750 * Some architecture linkers will pad between
3751 * the different mcount_loc sections of different
3752 * object files to satisfy alignments.
3753 * Skip any NULL pointers.
3757 if (!ftrace_record_ip(addr))
3761 /* These new locations need to be initialized */
3762 ftrace_new_pgs = pg;
3764 /* Make each individual set of pages sorted by ips */
3765 for (; pg; pg = pg->next)
3766 sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
3767 ftrace_cmp_recs, ftrace_swap_recs);
3770 * We only need to disable interrupts on start up
3771 * because we are modifying code that an interrupt
3772 * may execute, and the modification is not atomic.
3773 * But for modules, nothing runs the code we modify
3774 * until we are finished with it, and there's no
3775 * reason to cause large interrupt latencies while we do it.
3778 local_irq_save(flags);
3779 ftrace_update_code(mod);
3781 local_irq_restore(flags);
3784 mutex_unlock(&ftrace_lock);
3789 #ifdef CONFIG_MODULES
3791 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3793 void ftrace_release_mod(struct module *mod)
3795 struct dyn_ftrace *rec;
3796 struct ftrace_page **last_pg;
3797 struct ftrace_page *pg;
3800 mutex_lock(&ftrace_lock);
3802 if (ftrace_disabled)
3806 * Each module has its own ftrace_pages, remove
3807 * them from the list.
3809 last_pg = &ftrace_pages_start;
3810 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3811 rec = &pg->records[0];
3812 if (within_module_core(rec->ip, mod)) {
3814 * As core pages are first, the first
3815 * page should never be a module page.
3817 if (WARN_ON(pg == ftrace_pages_start))
3820 /* Check if we are deleting the last page */
3821 if (pg == ftrace_pages)
3822 ftrace_pages = next_to_ftrace_page(last_pg);
3824 *last_pg = pg->next;
3825 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3826 free_pages((unsigned long)pg->records, order);
3829 last_pg = &pg->next;
3832 mutex_unlock(&ftrace_lock);
3835 static void ftrace_init_module(struct module *mod,
3836 unsigned long *start, unsigned long *end)
3838 if (ftrace_disabled || start == end)
3840 ftrace_process_locs(mod, start, end);
3843 static int ftrace_module_notify(struct notifier_block *self,
3844 unsigned long val, void *data)
3846 struct module *mod = data;
3849 case MODULE_STATE_COMING:
3850 ftrace_init_module(mod, mod->ftrace_callsites,
3851 mod->ftrace_callsites +
3852 mod->num_ftrace_callsites);
3854 case MODULE_STATE_GOING:
3855 ftrace_release_mod(mod);
3862 static int ftrace_module_notify(struct notifier_block *self,
3863 unsigned long val, void *data)
3867 #endif /* CONFIG_MODULES */
3869 struct notifier_block ftrace_module_nb = {
3870 .notifier_call = ftrace_module_notify,
3874 extern unsigned long __start_mcount_loc[];
3875 extern unsigned long __stop_mcount_loc[];
3877 void __init ftrace_init(void)
3879 unsigned long count, addr, flags;
3882 /* Keep the ftrace pointer to the stub */
3883 addr = (unsigned long)ftrace_stub;
3885 local_irq_save(flags);
3886 ftrace_dyn_arch_init(&addr);
3887 local_irq_restore(flags);
3889 /* ftrace_dyn_arch_init places the return code in addr */
3893 count = __stop_mcount_loc - __start_mcount_loc;
3895 ret = ftrace_dyn_table_alloc(count);
3899 last_ftrace_enabled = ftrace_enabled = 1;
3901 ret = ftrace_process_locs(NULL,
3905 ret = register_module_notifier(&ftrace_module_nb);
3907 pr_warning("Failed to register trace ftrace module notifier\n");
3909 set_ftrace_early_filters();
3913 ftrace_disabled = 1;
3918 static struct ftrace_ops global_ops = {
3919 .func = ftrace_stub,
3922 static int __init ftrace_nodyn_init(void)
3927 device_initcall(ftrace_nodyn_init);
3929 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3930 static inline void ftrace_startup_enable(int command) { }
3931 /* Keep as macros so we do not need to define the commands */
3932 # define ftrace_startup(ops, command) \
3934 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3937 # define ftrace_shutdown(ops, command) do { } while (0)
3938 # define ftrace_startup_sysctl() do { } while (0)
3939 # define ftrace_shutdown_sysctl() do { } while (0)
3942 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3947 #endif /* CONFIG_DYNAMIC_FTRACE */
3950 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
3952 struct ftrace_ops *op;
3954 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
3958 * Some of the ops may be dynamically allocated,
3959 * they must be freed after a synchronize_sched().
3961 preempt_disable_notrace();
3962 trace_recursion_set(TRACE_CONTROL_BIT);
3963 op = rcu_dereference_raw(ftrace_control_list);
3964 while (op != &ftrace_list_end) {
3965 if (!ftrace_function_local_disabled(op) &&
3966 ftrace_ops_test(op, ip))
3967 op->func(ip, parent_ip);
3969 op = rcu_dereference_raw(op->next);
3971 trace_recursion_clear(TRACE_CONTROL_BIT);
3972 preempt_enable_notrace();
3975 static struct ftrace_ops control_ops = {
3976 .func = ftrace_ops_control_func,
3980 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3982 struct ftrace_ops *op;
3984 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3987 trace_recursion_set(TRACE_INTERNAL_BIT);
3989 * Some of the ops may be dynamically allocated,
3990 * they must be freed after a synchronize_sched().
3992 preempt_disable_notrace();
3993 op = rcu_dereference_raw(ftrace_ops_list);
3994 while (op != &ftrace_list_end) {
3995 if (ftrace_ops_test(op, ip))
3996 op->func(ip, parent_ip);
3997 op = rcu_dereference_raw(op->next);
3999 preempt_enable_notrace();
4000 trace_recursion_clear(TRACE_INTERNAL_BIT);
4003 static void clear_ftrace_swapper(void)
4005 struct task_struct *p;
4009 for_each_online_cpu(cpu) {
4011 clear_tsk_trace_trace(p);
4016 static void set_ftrace_swapper(void)
4018 struct task_struct *p;
4022 for_each_online_cpu(cpu) {
4024 set_tsk_trace_trace(p);
4029 static void clear_ftrace_pid(struct pid *pid)
4031 struct task_struct *p;
4034 do_each_pid_task(pid, PIDTYPE_PID, p) {
4035 clear_tsk_trace_trace(p);
4036 } while_each_pid_task(pid, PIDTYPE_PID, p);
4042 static void set_ftrace_pid(struct pid *pid)
4044 struct task_struct *p;
4047 do_each_pid_task(pid, PIDTYPE_PID, p) {
4048 set_tsk_trace_trace(p);
4049 } while_each_pid_task(pid, PIDTYPE_PID, p);
4053 static void clear_ftrace_pid_task(struct pid *pid)
4055 if (pid == ftrace_swapper_pid)
4056 clear_ftrace_swapper();
4058 clear_ftrace_pid(pid);
4061 static void set_ftrace_pid_task(struct pid *pid)
4063 if (pid == ftrace_swapper_pid)
4064 set_ftrace_swapper();
4066 set_ftrace_pid(pid);
4069 static int ftrace_pid_add(int p)
4072 struct ftrace_pid *fpid;
4075 mutex_lock(&ftrace_lock);
4078 pid = ftrace_swapper_pid;
4080 pid = find_get_pid(p);
4087 list_for_each_entry(fpid, &ftrace_pids, list)
4088 if (fpid->pid == pid)
4093 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4097 list_add(&fpid->list, &ftrace_pids);
4100 set_ftrace_pid_task(pid);
4102 ftrace_update_pid_func();
4103 ftrace_startup_enable(0);
4105 mutex_unlock(&ftrace_lock);
4109 if (pid != ftrace_swapper_pid)
4113 mutex_unlock(&ftrace_lock);
4117 static void ftrace_pid_reset(void)
4119 struct ftrace_pid *fpid, *safe;
4121 mutex_lock(&ftrace_lock);
4122 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4123 struct pid *pid = fpid->pid;
4125 clear_ftrace_pid_task(pid);
4127 list_del(&fpid->list);
4131 ftrace_update_pid_func();
4132 ftrace_startup_enable(0);
4134 mutex_unlock(&ftrace_lock);
4137 static void *fpid_start(struct seq_file *m, loff_t *pos)
4139 mutex_lock(&ftrace_lock);
4141 if (list_empty(&ftrace_pids) && (!*pos))
4144 return seq_list_start(&ftrace_pids, *pos);
4147 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4152 return seq_list_next(v, &ftrace_pids, pos);
4155 static void fpid_stop(struct seq_file *m, void *p)
4157 mutex_unlock(&ftrace_lock);
4160 static int fpid_show(struct seq_file *m, void *v)
4162 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4164 if (v == (void *)1) {
4165 seq_printf(m, "no pid\n");
4169 if (fpid->pid == ftrace_swapper_pid)
4170 seq_printf(m, "swapper tasks\n");
4172 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4177 static const struct seq_operations ftrace_pid_sops = {
4178 .start = fpid_start,
4185 ftrace_pid_open(struct inode *inode, struct file *file)
4189 if ((file->f_mode & FMODE_WRITE) &&
4190 (file->f_flags & O_TRUNC))
4193 if (file->f_mode & FMODE_READ)
4194 ret = seq_open(file, &ftrace_pid_sops);
4200 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4201 size_t cnt, loff_t *ppos)
4207 if (cnt >= sizeof(buf))
4210 if (copy_from_user(&buf, ubuf, cnt))
4216 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4217 * to clean the filter quietly.
4219 tmp = strstrip(buf);
4220 if (strlen(tmp) == 0)
4223 ret = strict_strtol(tmp, 10, &val);
4227 ret = ftrace_pid_add(val);
4229 return ret ? ret : cnt;
4233 ftrace_pid_release(struct inode *inode, struct file *file)
4235 if (file->f_mode & FMODE_READ)
4236 seq_release(inode, file);
4241 static const struct file_operations ftrace_pid_fops = {
4242 .open = ftrace_pid_open,
4243 .write = ftrace_pid_write,
4245 .llseek = seq_lseek,
4246 .release = ftrace_pid_release,
4249 static __init int ftrace_init_debugfs(void)
4251 struct dentry *d_tracer;
4253 d_tracer = tracing_init_dentry();
4257 ftrace_init_dyn_debugfs(d_tracer);
4259 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4260 NULL, &ftrace_pid_fops);
4262 ftrace_profile_debugfs(d_tracer);
4266 fs_initcall(ftrace_init_debugfs);
4269 * ftrace_kill - kill ftrace
4271 * This function should be used by panic code. It stops ftrace
4272 * but in a not so nice way. If you need to simply kill ftrace
4273 * from a non-atomic section, use ftrace_kill.
4275 void ftrace_kill(void)
4277 ftrace_disabled = 1;
4279 clear_ftrace_function();
4283 * Test if ftrace is dead or not.
4285 int ftrace_is_dead(void)
4287 return ftrace_disabled;
4291 * register_ftrace_function - register a function for profiling
4292 * @ops - ops structure that holds the function for profiling.
4294 * Register a function to be called by all functions in the
4297 * Note: @ops->func and all the functions it calls must be labeled
4298 * with "notrace", otherwise it will go into a
4301 int register_ftrace_function(struct ftrace_ops *ops)
4305 mutex_lock(&ftrace_lock);
4307 if (unlikely(ftrace_disabled))
4310 ret = __register_ftrace_function(ops);
4312 ret = ftrace_startup(ops, 0);
4316 mutex_unlock(&ftrace_lock);
4319 EXPORT_SYMBOL_GPL(register_ftrace_function);
4322 * unregister_ftrace_function - unregister a function for profiling.
4323 * @ops - ops structure that holds the function to unregister
4325 * Unregister a function that was added to be called by ftrace profiling.
4327 int unregister_ftrace_function(struct ftrace_ops *ops)
4331 mutex_lock(&ftrace_lock);
4332 ret = __unregister_ftrace_function(ops);
4334 ftrace_shutdown(ops, 0);
4335 mutex_unlock(&ftrace_lock);
4339 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4342 ftrace_enable_sysctl(struct ctl_table *table, int write,
4343 void __user *buffer, size_t *lenp,
4348 mutex_lock(&ftrace_lock);
4350 if (unlikely(ftrace_disabled))
4353 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4355 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4358 last_ftrace_enabled = !!ftrace_enabled;
4360 if (ftrace_enabled) {
4362 ftrace_startup_sysctl();
4364 /* we are starting ftrace again */
4365 if (ftrace_ops_list != &ftrace_list_end) {
4366 if (ftrace_ops_list->next == &ftrace_list_end)
4367 ftrace_trace_function = ftrace_ops_list->func;
4369 ftrace_trace_function = ftrace_ops_list_func;
4373 /* stopping ftrace calls (just send to ftrace_stub) */
4374 ftrace_trace_function = ftrace_stub;
4376 ftrace_shutdown_sysctl();
4380 mutex_unlock(&ftrace_lock);
4384 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4386 static int ftrace_graph_active;
4387 static struct notifier_block ftrace_suspend_notifier;
4389 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4394 /* The callbacks that hook a function */
4395 trace_func_graph_ret_t ftrace_graph_return =
4396 (trace_func_graph_ret_t)ftrace_stub;
4397 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4399 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4400 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4404 unsigned long flags;
4405 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4406 struct task_struct *g, *t;
4408 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4409 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4410 * sizeof(struct ftrace_ret_stack),
4412 if (!ret_stack_list[i]) {
4420 read_lock_irqsave(&tasklist_lock, flags);
4421 do_each_thread(g, t) {
4427 if (t->ret_stack == NULL) {
4428 atomic_set(&t->tracing_graph_pause, 0);
4429 atomic_set(&t->trace_overrun, 0);
4430 t->curr_ret_stack = -1;
4431 /* Make sure the tasks see the -1 first: */
4433 t->ret_stack = ret_stack_list[start++];
4435 } while_each_thread(g, t);
4438 read_unlock_irqrestore(&tasklist_lock, flags);
4440 for (i = start; i < end; i++)
4441 kfree(ret_stack_list[i]);
4446 ftrace_graph_probe_sched_switch(void *ignore,
4447 struct task_struct *prev, struct task_struct *next)
4449 unsigned long long timestamp;
4453 * Does the user want to count the time a function was asleep.
4454 * If so, do not update the time stamps.
4456 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4459 timestamp = trace_clock_local();
4461 prev->ftrace_timestamp = timestamp;
4463 /* only process tasks that we timestamped */
4464 if (!next->ftrace_timestamp)
4468 * Update all the counters in next to make up for the
4469 * time next was sleeping.
4471 timestamp -= next->ftrace_timestamp;
4473 for (index = next->curr_ret_stack; index >= 0; index--)
4474 next->ret_stack[index].calltime += timestamp;
4477 /* Allocate a return stack for each task */
4478 static int start_graph_tracing(void)
4480 struct ftrace_ret_stack **ret_stack_list;
4483 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4484 sizeof(struct ftrace_ret_stack *),
4487 if (!ret_stack_list)
4490 /* The cpu_boot init_task->ret_stack will never be freed */
4491 for_each_online_cpu(cpu) {
4492 if (!idle_task(cpu)->ret_stack)
4493 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4497 ret = alloc_retstack_tasklist(ret_stack_list);
4498 } while (ret == -EAGAIN);
4501 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4503 pr_info("ftrace_graph: Couldn't activate tracepoint"
4504 " probe to kernel_sched_switch\n");
4507 kfree(ret_stack_list);
4512 * Hibernation protection.
4513 * The state of the current task is too much unstable during
4514 * suspend/restore to disk. We want to protect against that.
4517 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4521 case PM_HIBERNATION_PREPARE:
4522 pause_graph_tracing();
4525 case PM_POST_HIBERNATION:
4526 unpause_graph_tracing();
4532 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4533 trace_func_graph_ent_t entryfunc)
4537 mutex_lock(&ftrace_lock);
4539 /* we currently allow only one tracer registered at a time */
4540 if (ftrace_graph_active) {
4545 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4546 register_pm_notifier(&ftrace_suspend_notifier);
4548 ftrace_graph_active++;
4549 ret = start_graph_tracing();
4551 ftrace_graph_active--;
4555 ftrace_graph_return = retfunc;
4556 ftrace_graph_entry = entryfunc;
4558 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4561 mutex_unlock(&ftrace_lock);
4565 void unregister_ftrace_graph(void)
4567 mutex_lock(&ftrace_lock);
4569 if (unlikely(!ftrace_graph_active))
4572 ftrace_graph_active--;
4573 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4574 ftrace_graph_entry = ftrace_graph_entry_stub;
4575 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4576 unregister_pm_notifier(&ftrace_suspend_notifier);
4577 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4580 mutex_unlock(&ftrace_lock);
4583 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4586 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4588 atomic_set(&t->tracing_graph_pause, 0);
4589 atomic_set(&t->trace_overrun, 0);
4590 t->ftrace_timestamp = 0;
4591 /* make curr_ret_stack visible before we add the ret_stack */
4593 t->ret_stack = ret_stack;
4597 * Allocate a return stack for the idle task. May be the first
4598 * time through, or it may be done by CPU hotplug online.
4600 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4602 t->curr_ret_stack = -1;
4604 * The idle task has no parent, it either has its own
4605 * stack or no stack at all.
4608 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4610 if (ftrace_graph_active) {
4611 struct ftrace_ret_stack *ret_stack;
4613 ret_stack = per_cpu(idle_ret_stack, cpu);
4615 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4616 * sizeof(struct ftrace_ret_stack),
4620 per_cpu(idle_ret_stack, cpu) = ret_stack;
4622 graph_init_task(t, ret_stack);
4626 /* Allocate a return stack for newly created task */
4627 void ftrace_graph_init_task(struct task_struct *t)
4629 /* Make sure we do not use the parent ret_stack */
4630 t->ret_stack = NULL;
4631 t->curr_ret_stack = -1;
4633 if (ftrace_graph_active) {
4634 struct ftrace_ret_stack *ret_stack;
4636 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4637 * sizeof(struct ftrace_ret_stack),
4641 graph_init_task(t, ret_stack);
4645 void ftrace_graph_exit_task(struct task_struct *t)
4647 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4649 t->ret_stack = NULL;
4650 /* NULL must become visible to IRQs before we free it: */
4656 void ftrace_graph_stop(void)