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 Nadia Yvette Chambers
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 static struct ftrace_ops ftrace_list_end __read_mostly = {
69 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
72 /* ftrace_enabled is a method to turn ftrace on or off */
73 int ftrace_enabled __read_mostly;
74 static int last_ftrace_enabled;
76 /* Quick disabling of function tracer. */
77 int function_trace_stop __read_mostly;
79 /* Current function tracing op */
80 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
82 /* List for set_ftrace_pid's pids. */
83 LIST_HEAD(ftrace_pids);
85 struct list_head list;
90 * ftrace_disabled is set when an anomaly is discovered.
91 * ftrace_disabled is much stronger than ftrace_enabled.
93 static int ftrace_disabled __read_mostly;
95 static DEFINE_MUTEX(ftrace_lock);
97 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
98 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
99 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
100 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
101 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
102 static struct ftrace_ops global_ops;
103 static struct ftrace_ops control_ops;
105 #if ARCH_SUPPORTS_FTRACE_OPS
106 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
107 struct ftrace_ops *op, struct pt_regs *regs);
109 /* See comment below, where ftrace_ops_list_func is defined */
110 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
111 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
115 * Traverse the ftrace_global_list, invoking all entries. The reason that we
116 * can use rcu_dereference_raw() is that elements removed from this list
117 * are simply leaked, so there is no need to interact with a grace-period
118 * mechanism. The rcu_dereference_raw() calls are needed to handle
119 * concurrent insertions into the ftrace_global_list.
121 * Silly Alpha and silly pointer-speculation compiler optimizations!
123 #define do_for_each_ftrace_op(op, list) \
124 op = rcu_dereference_raw(list); \
128 * Optimized for just a single item in the list (as that is the normal case).
130 #define while_for_each_ftrace_op(op) \
131 while (likely(op = rcu_dereference_raw((op)->next)) && \
132 unlikely((op) != &ftrace_list_end))
135 * ftrace_nr_registered_ops - return number of ops registered
137 * Returns the number of ftrace_ops registered and tracing functions
139 int ftrace_nr_registered_ops(void)
141 struct ftrace_ops *ops;
144 mutex_lock(&ftrace_lock);
146 for (ops = ftrace_ops_list;
147 ops != &ftrace_list_end; ops = ops->next)
150 mutex_unlock(&ftrace_lock);
156 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
157 struct ftrace_ops *op, struct pt_regs *regs)
161 if (in_interrupt()) {
163 bit = TRACE_GLOBAL_NMI_BIT;
166 bit = TRACE_GLOBAL_IRQ_BIT;
168 bit = TRACE_GLOBAL_SIRQ_BIT;
170 bit = TRACE_GLOBAL_BIT;
172 if (unlikely(trace_recursion_test(bit)))
175 trace_recursion_set(bit);
176 do_for_each_ftrace_op(op, ftrace_global_list) {
177 op->func(ip, parent_ip, op, regs);
178 } while_for_each_ftrace_op(op);
179 trace_recursion_clear(bit);
182 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
183 struct ftrace_ops *op, struct pt_regs *regs)
185 if (!test_tsk_trace_trace(current))
188 ftrace_pid_function(ip, parent_ip, op, regs);
191 static void set_ftrace_pid_function(ftrace_func_t func)
193 /* do not set ftrace_pid_function to itself! */
194 if (func != ftrace_pid_func)
195 ftrace_pid_function = func;
199 * clear_ftrace_function - reset the ftrace function
201 * This NULLs the ftrace function and in essence stops
202 * tracing. There may be lag
204 void clear_ftrace_function(void)
206 ftrace_trace_function = ftrace_stub;
207 ftrace_pid_function = ftrace_stub;
210 static void control_ops_disable_all(struct ftrace_ops *ops)
214 for_each_possible_cpu(cpu)
215 *per_cpu_ptr(ops->disabled, cpu) = 1;
218 static int control_ops_alloc(struct ftrace_ops *ops)
220 int __percpu *disabled;
222 disabled = alloc_percpu(int);
226 ops->disabled = disabled;
227 control_ops_disable_all(ops);
231 static void control_ops_free(struct ftrace_ops *ops)
233 free_percpu(ops->disabled);
236 static void update_global_ops(void)
241 * If there's only one function registered, then call that
242 * function directly. Otherwise, we need to iterate over the
243 * registered callers.
245 if (ftrace_global_list == &ftrace_list_end ||
246 ftrace_global_list->next == &ftrace_list_end) {
247 func = ftrace_global_list->func;
249 * As we are calling the function directly.
250 * If it does not have recursion protection,
251 * the function_trace_op needs to be updated
254 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
255 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
257 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
259 func = ftrace_global_list_func;
260 /* The list has its own recursion protection. */
261 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
265 /* If we filter on pids, update to use the pid function */
266 if (!list_empty(&ftrace_pids)) {
267 set_ftrace_pid_function(func);
268 func = ftrace_pid_func;
271 global_ops.func = func;
274 static void update_ftrace_function(void)
281 * If we are at the end of the list and this ops is
282 * recursion safe and not dynamic and the arch supports passing ops,
283 * then have the mcount trampoline call the function directly.
285 if (ftrace_ops_list == &ftrace_list_end ||
286 (ftrace_ops_list->next == &ftrace_list_end &&
287 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
288 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
289 !FTRACE_FORCE_LIST_FUNC)) {
290 /* Set the ftrace_ops that the arch callback uses */
291 if (ftrace_ops_list == &global_ops)
292 function_trace_op = ftrace_global_list;
294 function_trace_op = ftrace_ops_list;
295 func = ftrace_ops_list->func;
297 /* Just use the default ftrace_ops */
298 function_trace_op = &ftrace_list_end;
299 func = ftrace_ops_list_func;
302 ftrace_trace_function = func;
305 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
309 * We are entering ops into the list but another
310 * CPU might be walking that list. We need to make sure
311 * the ops->next pointer is valid before another CPU sees
312 * the ops pointer included into the list.
314 rcu_assign_pointer(*list, ops);
317 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
319 struct ftrace_ops **p;
322 * If we are removing the last function, then simply point
323 * to the ftrace_stub.
325 if (*list == ops && ops->next == &ftrace_list_end) {
326 *list = &ftrace_list_end;
330 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
341 static void add_ftrace_list_ops(struct ftrace_ops **list,
342 struct ftrace_ops *main_ops,
343 struct ftrace_ops *ops)
345 int first = *list == &ftrace_list_end;
346 add_ftrace_ops(list, ops);
348 add_ftrace_ops(&ftrace_ops_list, main_ops);
351 static int remove_ftrace_list_ops(struct ftrace_ops **list,
352 struct ftrace_ops *main_ops,
353 struct ftrace_ops *ops)
355 int ret = remove_ftrace_ops(list, ops);
356 if (!ret && *list == &ftrace_list_end)
357 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
361 static int __register_ftrace_function(struct ftrace_ops *ops)
363 if (unlikely(ftrace_disabled))
366 if (FTRACE_WARN_ON(ops == &global_ops))
369 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
372 /* We don't support both control and global flags set. */
373 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
376 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
378 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
379 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
380 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
382 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
383 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
386 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
387 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
390 if (!core_kernel_data((unsigned long)ops))
391 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
393 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
394 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
395 ops->flags |= FTRACE_OPS_FL_ENABLED;
396 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
397 if (control_ops_alloc(ops))
399 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
401 add_ftrace_ops(&ftrace_ops_list, ops);
404 update_ftrace_function();
409 static int __unregister_ftrace_function(struct ftrace_ops *ops)
416 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
419 if (FTRACE_WARN_ON(ops == &global_ops))
422 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
423 ret = remove_ftrace_list_ops(&ftrace_global_list,
426 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
427 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
428 ret = remove_ftrace_list_ops(&ftrace_control_list,
432 * The ftrace_ops is now removed from the list,
433 * so there'll be no new users. We must ensure
434 * all current users are done before we free
438 control_ops_free(ops);
441 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
447 update_ftrace_function();
450 * Dynamic ops may be freed, we must make sure that all
451 * callers are done before leaving this function.
453 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
459 static void ftrace_update_pid_func(void)
461 /* Only do something if we are tracing something */
462 if (ftrace_trace_function == ftrace_stub)
465 update_ftrace_function();
468 #ifdef CONFIG_FUNCTION_PROFILER
469 struct ftrace_profile {
470 struct hlist_node node;
472 unsigned long counter;
473 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
474 unsigned long long time;
475 unsigned long long time_squared;
479 struct ftrace_profile_page {
480 struct ftrace_profile_page *next;
482 struct ftrace_profile records[];
485 struct ftrace_profile_stat {
487 struct hlist_head *hash;
488 struct ftrace_profile_page *pages;
489 struct ftrace_profile_page *start;
490 struct tracer_stat stat;
493 #define PROFILE_RECORDS_SIZE \
494 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
496 #define PROFILES_PER_PAGE \
497 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
499 static int ftrace_profile_bits __read_mostly;
500 static int ftrace_profile_enabled __read_mostly;
502 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
503 static DEFINE_MUTEX(ftrace_profile_lock);
505 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
507 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
510 function_stat_next(void *v, int idx)
512 struct ftrace_profile *rec = v;
513 struct ftrace_profile_page *pg;
515 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
521 if ((void *)rec >= (void *)&pg->records[pg->index]) {
525 rec = &pg->records[0];
533 static void *function_stat_start(struct tracer_stat *trace)
535 struct ftrace_profile_stat *stat =
536 container_of(trace, struct ftrace_profile_stat, stat);
538 if (!stat || !stat->start)
541 return function_stat_next(&stat->start->records[0], 0);
544 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
545 /* function graph compares on total time */
546 static int function_stat_cmp(void *p1, void *p2)
548 struct ftrace_profile *a = p1;
549 struct ftrace_profile *b = p2;
551 if (a->time < b->time)
553 if (a->time > b->time)
559 /* not function graph compares against hits */
560 static int function_stat_cmp(void *p1, void *p2)
562 struct ftrace_profile *a = p1;
563 struct ftrace_profile *b = p2;
565 if (a->counter < b->counter)
567 if (a->counter > b->counter)
574 static int function_stat_headers(struct seq_file *m)
576 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
577 seq_printf(m, " Function "
580 "--- ---- --- ---\n");
582 seq_printf(m, " Function Hit\n"
588 static int function_stat_show(struct seq_file *m, void *v)
590 struct ftrace_profile *rec = v;
591 char str[KSYM_SYMBOL_LEN];
593 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
594 static struct trace_seq s;
595 unsigned long long avg;
596 unsigned long long stddev;
598 mutex_lock(&ftrace_profile_lock);
600 /* we raced with function_profile_reset() */
601 if (unlikely(rec->counter == 0)) {
606 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
607 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
609 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
612 do_div(avg, rec->counter);
614 /* Sample standard deviation (s^2) */
615 if (rec->counter <= 1)
618 stddev = rec->time_squared - rec->counter * avg * avg;
620 * Divide only 1000 for ns^2 -> us^2 conversion.
621 * trace_print_graph_duration will divide 1000 again.
623 do_div(stddev, (rec->counter - 1) * 1000);
627 trace_print_graph_duration(rec->time, &s);
628 trace_seq_puts(&s, " ");
629 trace_print_graph_duration(avg, &s);
630 trace_seq_puts(&s, " ");
631 trace_print_graph_duration(stddev, &s);
632 trace_print_seq(m, &s);
636 mutex_unlock(&ftrace_profile_lock);
641 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
643 struct ftrace_profile_page *pg;
645 pg = stat->pages = stat->start;
648 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
653 memset(stat->hash, 0,
654 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
657 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
659 struct ftrace_profile_page *pg;
664 /* If we already allocated, do nothing */
668 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
672 #ifdef CONFIG_DYNAMIC_FTRACE
673 functions = ftrace_update_tot_cnt;
676 * We do not know the number of functions that exist because
677 * dynamic tracing is what counts them. With past experience
678 * we have around 20K functions. That should be more than enough.
679 * It is highly unlikely we will execute every function in
685 pg = stat->start = stat->pages;
687 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
689 for (i = 0; i < pages; i++) {
690 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
701 unsigned long tmp = (unsigned long)pg;
707 free_page((unsigned long)stat->pages);
714 static int ftrace_profile_init_cpu(int cpu)
716 struct ftrace_profile_stat *stat;
719 stat = &per_cpu(ftrace_profile_stats, cpu);
722 /* If the profile is already created, simply reset it */
723 ftrace_profile_reset(stat);
728 * We are profiling all functions, but usually only a few thousand
729 * functions are hit. We'll make a hash of 1024 items.
731 size = FTRACE_PROFILE_HASH_SIZE;
733 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
738 if (!ftrace_profile_bits) {
741 for (; size; size >>= 1)
742 ftrace_profile_bits++;
745 /* Preallocate the function profiling pages */
746 if (ftrace_profile_pages_init(stat) < 0) {
755 static int ftrace_profile_init(void)
760 for_each_online_cpu(cpu) {
761 ret = ftrace_profile_init_cpu(cpu);
769 /* interrupts must be disabled */
770 static struct ftrace_profile *
771 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
773 struct ftrace_profile *rec;
774 struct hlist_head *hhd;
775 struct hlist_node *n;
778 key = hash_long(ip, ftrace_profile_bits);
779 hhd = &stat->hash[key];
781 if (hlist_empty(hhd))
784 hlist_for_each_entry_rcu(rec, n, hhd, node) {
792 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
793 struct ftrace_profile *rec)
797 key = hash_long(rec->ip, ftrace_profile_bits);
798 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
802 * The memory is already allocated, this simply finds a new record to use.
804 static struct ftrace_profile *
805 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
807 struct ftrace_profile *rec = NULL;
809 /* prevent recursion (from NMIs) */
810 if (atomic_inc_return(&stat->disabled) != 1)
814 * Try to find the function again since an NMI
815 * could have added it
817 rec = ftrace_find_profiled_func(stat, ip);
821 if (stat->pages->index == PROFILES_PER_PAGE) {
822 if (!stat->pages->next)
824 stat->pages = stat->pages->next;
827 rec = &stat->pages->records[stat->pages->index++];
829 ftrace_add_profile(stat, rec);
832 atomic_dec(&stat->disabled);
838 function_profile_call(unsigned long ip, unsigned long parent_ip,
839 struct ftrace_ops *ops, struct pt_regs *regs)
841 struct ftrace_profile_stat *stat;
842 struct ftrace_profile *rec;
845 if (!ftrace_profile_enabled)
848 local_irq_save(flags);
850 stat = &__get_cpu_var(ftrace_profile_stats);
851 if (!stat->hash || !ftrace_profile_enabled)
854 rec = ftrace_find_profiled_func(stat, ip);
856 rec = ftrace_profile_alloc(stat, ip);
863 local_irq_restore(flags);
866 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
867 static int profile_graph_entry(struct ftrace_graph_ent *trace)
869 function_profile_call(trace->func, 0, NULL, NULL);
873 static void profile_graph_return(struct ftrace_graph_ret *trace)
875 struct ftrace_profile_stat *stat;
876 unsigned long long calltime;
877 struct ftrace_profile *rec;
880 local_irq_save(flags);
881 stat = &__get_cpu_var(ftrace_profile_stats);
882 if (!stat->hash || !ftrace_profile_enabled)
885 /* If the calltime was zero'd ignore it */
886 if (!trace->calltime)
889 calltime = trace->rettime - trace->calltime;
891 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
894 index = trace->depth;
896 /* Append this call time to the parent time to subtract */
898 current->ret_stack[index - 1].subtime += calltime;
900 if (current->ret_stack[index].subtime < calltime)
901 calltime -= current->ret_stack[index].subtime;
906 rec = ftrace_find_profiled_func(stat, trace->func);
908 rec->time += calltime;
909 rec->time_squared += calltime * calltime;
913 local_irq_restore(flags);
916 static int register_ftrace_profiler(void)
918 return register_ftrace_graph(&profile_graph_return,
919 &profile_graph_entry);
922 static void unregister_ftrace_profiler(void)
924 unregister_ftrace_graph();
927 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
928 .func = function_profile_call,
929 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
932 static int register_ftrace_profiler(void)
934 return register_ftrace_function(&ftrace_profile_ops);
937 static void unregister_ftrace_profiler(void)
939 unregister_ftrace_function(&ftrace_profile_ops);
941 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
944 ftrace_profile_write(struct file *filp, const char __user *ubuf,
945 size_t cnt, loff_t *ppos)
950 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
956 mutex_lock(&ftrace_profile_lock);
957 if (ftrace_profile_enabled ^ val) {
959 ret = ftrace_profile_init();
965 ret = register_ftrace_profiler();
970 ftrace_profile_enabled = 1;
972 ftrace_profile_enabled = 0;
974 * unregister_ftrace_profiler calls stop_machine
975 * so this acts like an synchronize_sched.
977 unregister_ftrace_profiler();
981 mutex_unlock(&ftrace_profile_lock);
989 ftrace_profile_read(struct file *filp, char __user *ubuf,
990 size_t cnt, loff_t *ppos)
992 char buf[64]; /* big enough to hold a number */
995 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
996 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
999 static const struct file_operations ftrace_profile_fops = {
1000 .open = tracing_open_generic,
1001 .read = ftrace_profile_read,
1002 .write = ftrace_profile_write,
1003 .llseek = default_llseek,
1006 /* used to initialize the real stat files */
1007 static struct tracer_stat function_stats __initdata = {
1008 .name = "functions",
1009 .stat_start = function_stat_start,
1010 .stat_next = function_stat_next,
1011 .stat_cmp = function_stat_cmp,
1012 .stat_headers = function_stat_headers,
1013 .stat_show = function_stat_show
1016 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1018 struct ftrace_profile_stat *stat;
1019 struct dentry *entry;
1024 for_each_possible_cpu(cpu) {
1025 stat = &per_cpu(ftrace_profile_stats, cpu);
1027 /* allocate enough for function name + cpu number */
1028 name = kmalloc(32, GFP_KERNEL);
1031 * The files created are permanent, if something happens
1032 * we still do not free memory.
1035 "Could not allocate stat file for cpu %d\n",
1039 stat->stat = function_stats;
1040 snprintf(name, 32, "function%d", cpu);
1041 stat->stat.name = name;
1042 ret = register_stat_tracer(&stat->stat);
1045 "Could not register function stat for cpu %d\n",
1052 entry = debugfs_create_file("function_profile_enabled", 0644,
1053 d_tracer, NULL, &ftrace_profile_fops);
1055 pr_warning("Could not create debugfs "
1056 "'function_profile_enabled' entry\n");
1059 #else /* CONFIG_FUNCTION_PROFILER */
1060 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1063 #endif /* CONFIG_FUNCTION_PROFILER */
1065 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1067 #ifdef CONFIG_DYNAMIC_FTRACE
1069 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1070 # error Dynamic ftrace depends on MCOUNT_RECORD
1073 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1075 struct ftrace_func_probe {
1076 struct hlist_node node;
1077 struct ftrace_probe_ops *ops;
1078 unsigned long flags;
1081 struct rcu_head rcu;
1084 struct ftrace_func_entry {
1085 struct hlist_node hlist;
1089 struct ftrace_hash {
1090 unsigned long size_bits;
1091 struct hlist_head *buckets;
1092 unsigned long count;
1093 struct rcu_head rcu;
1097 * We make these constant because no one should touch them,
1098 * but they are used as the default "empty hash", to avoid allocating
1099 * it all the time. These are in a read only section such that if
1100 * anyone does try to modify it, it will cause an exception.
1102 static const struct hlist_head empty_buckets[1];
1103 static const struct ftrace_hash empty_hash = {
1104 .buckets = (struct hlist_head *)empty_buckets,
1106 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1108 static struct ftrace_ops global_ops = {
1109 .func = ftrace_stub,
1110 .notrace_hash = EMPTY_HASH,
1111 .filter_hash = EMPTY_HASH,
1112 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
1115 static DEFINE_MUTEX(ftrace_regex_lock);
1117 struct ftrace_page {
1118 struct ftrace_page *next;
1119 struct dyn_ftrace *records;
1124 static struct ftrace_page *ftrace_new_pgs;
1126 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1127 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1129 /* estimate from running different kernels */
1130 #define NR_TO_INIT 10000
1132 static struct ftrace_page *ftrace_pages_start;
1133 static struct ftrace_page *ftrace_pages;
1135 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1137 return !hash || !hash->count;
1140 static struct ftrace_func_entry *
1141 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1144 struct ftrace_func_entry *entry;
1145 struct hlist_head *hhd;
1146 struct hlist_node *n;
1148 if (ftrace_hash_empty(hash))
1151 if (hash->size_bits > 0)
1152 key = hash_long(ip, hash->size_bits);
1156 hhd = &hash->buckets[key];
1158 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1159 if (entry->ip == ip)
1165 static void __add_hash_entry(struct ftrace_hash *hash,
1166 struct ftrace_func_entry *entry)
1168 struct hlist_head *hhd;
1171 if (hash->size_bits)
1172 key = hash_long(entry->ip, hash->size_bits);
1176 hhd = &hash->buckets[key];
1177 hlist_add_head(&entry->hlist, hhd);
1181 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1183 struct ftrace_func_entry *entry;
1185 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1190 __add_hash_entry(hash, entry);
1196 free_hash_entry(struct ftrace_hash *hash,
1197 struct ftrace_func_entry *entry)
1199 hlist_del(&entry->hlist);
1205 remove_hash_entry(struct ftrace_hash *hash,
1206 struct ftrace_func_entry *entry)
1208 hlist_del(&entry->hlist);
1212 static void ftrace_hash_clear(struct ftrace_hash *hash)
1214 struct hlist_head *hhd;
1215 struct hlist_node *tp, *tn;
1216 struct ftrace_func_entry *entry;
1217 int size = 1 << hash->size_bits;
1223 for (i = 0; i < size; i++) {
1224 hhd = &hash->buckets[i];
1225 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1226 free_hash_entry(hash, entry);
1228 FTRACE_WARN_ON(hash->count);
1231 static void free_ftrace_hash(struct ftrace_hash *hash)
1233 if (!hash || hash == EMPTY_HASH)
1235 ftrace_hash_clear(hash);
1236 kfree(hash->buckets);
1240 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1242 struct ftrace_hash *hash;
1244 hash = container_of(rcu, struct ftrace_hash, rcu);
1245 free_ftrace_hash(hash);
1248 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1250 if (!hash || hash == EMPTY_HASH)
1252 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1255 void ftrace_free_filter(struct ftrace_ops *ops)
1257 free_ftrace_hash(ops->filter_hash);
1258 free_ftrace_hash(ops->notrace_hash);
1261 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1263 struct ftrace_hash *hash;
1266 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1270 size = 1 << size_bits;
1271 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1273 if (!hash->buckets) {
1278 hash->size_bits = size_bits;
1283 static struct ftrace_hash *
1284 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1286 struct ftrace_func_entry *entry;
1287 struct ftrace_hash *new_hash;
1288 struct hlist_node *tp;
1293 new_hash = alloc_ftrace_hash(size_bits);
1298 if (ftrace_hash_empty(hash))
1301 size = 1 << hash->size_bits;
1302 for (i = 0; i < size; i++) {
1303 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1304 ret = add_hash_entry(new_hash, entry->ip);
1310 FTRACE_WARN_ON(new_hash->count != hash->count);
1315 free_ftrace_hash(new_hash);
1320 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1322 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1325 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1326 struct ftrace_hash **dst, struct ftrace_hash *src)
1328 struct ftrace_func_entry *entry;
1329 struct hlist_node *tp, *tn;
1330 struct hlist_head *hhd;
1331 struct ftrace_hash *old_hash;
1332 struct ftrace_hash *new_hash;
1334 int size = src->count;
1340 * Remove the current set, update the hash and add
1343 ftrace_hash_rec_disable(ops, enable);
1346 * If the new source is empty, just free dst and assign it
1350 free_ftrace_hash_rcu(*dst);
1351 rcu_assign_pointer(*dst, EMPTY_HASH);
1352 /* still need to update the function records */
1358 * Make the hash size about 1/2 the # found
1360 for (size /= 2; size; size >>= 1)
1363 /* Don't allocate too much */
1364 if (bits > FTRACE_HASH_MAX_BITS)
1365 bits = FTRACE_HASH_MAX_BITS;
1368 new_hash = alloc_ftrace_hash(bits);
1372 size = 1 << src->size_bits;
1373 for (i = 0; i < size; i++) {
1374 hhd = &src->buckets[i];
1375 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1377 key = hash_long(entry->ip, bits);
1380 remove_hash_entry(src, entry);
1381 __add_hash_entry(new_hash, entry);
1386 rcu_assign_pointer(*dst, new_hash);
1387 free_ftrace_hash_rcu(old_hash);
1392 * Enable regardless of ret:
1393 * On success, we enable the new hash.
1394 * On failure, we re-enable the original hash.
1396 ftrace_hash_rec_enable(ops, enable);
1402 * Test the hashes for this ops to see if we want to call
1403 * the ops->func or not.
1405 * It's a match if the ip is in the ops->filter_hash or
1406 * the filter_hash does not exist or is empty,
1408 * the ip is not in the ops->notrace_hash.
1410 * This needs to be called with preemption disabled as
1411 * the hashes are freed with call_rcu_sched().
1414 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1416 struct ftrace_hash *filter_hash;
1417 struct ftrace_hash *notrace_hash;
1420 filter_hash = rcu_dereference_raw(ops->filter_hash);
1421 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1423 if ((ftrace_hash_empty(filter_hash) ||
1424 ftrace_lookup_ip(filter_hash, ip)) &&
1425 (ftrace_hash_empty(notrace_hash) ||
1426 !ftrace_lookup_ip(notrace_hash, ip)))
1435 * This is a double for. Do not use 'break' to break out of the loop,
1436 * you must use a goto.
1438 #define do_for_each_ftrace_rec(pg, rec) \
1439 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1441 for (_____i = 0; _____i < pg->index; _____i++) { \
1442 rec = &pg->records[_____i];
1444 #define while_for_each_ftrace_rec() \
1449 static int ftrace_cmp_recs(const void *a, const void *b)
1451 const struct dyn_ftrace *key = a;
1452 const struct dyn_ftrace *rec = b;
1454 if (key->flags < rec->ip)
1456 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1461 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1463 struct ftrace_page *pg;
1464 struct dyn_ftrace *rec;
1465 struct dyn_ftrace key;
1468 key.flags = end; /* overload flags, as it is unsigned long */
1470 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1471 if (end < pg->records[0].ip ||
1472 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1474 rec = bsearch(&key, pg->records, pg->index,
1475 sizeof(struct dyn_ftrace),
1485 * ftrace_location - return true if the ip giving is a traced location
1486 * @ip: the instruction pointer to check
1488 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1489 * That is, the instruction that is either a NOP or call to
1490 * the function tracer. It checks the ftrace internal tables to
1491 * determine if the address belongs or not.
1493 unsigned long ftrace_location(unsigned long ip)
1495 return ftrace_location_range(ip, ip);
1499 * ftrace_text_reserved - return true if range contains an ftrace location
1500 * @start: start of range to search
1501 * @end: end of range to search (inclusive). @end points to the last byte to check.
1503 * Returns 1 if @start and @end contains a ftrace location.
1504 * That is, the instruction that is either a NOP or call to
1505 * the function tracer. It checks the ftrace internal tables to
1506 * determine if the address belongs or not.
1508 int ftrace_text_reserved(void *start, void *end)
1512 ret = ftrace_location_range((unsigned long)start,
1513 (unsigned long)end);
1518 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1522 struct ftrace_hash *hash;
1523 struct ftrace_hash *other_hash;
1524 struct ftrace_page *pg;
1525 struct dyn_ftrace *rec;
1529 /* Only update if the ops has been registered */
1530 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1534 * In the filter_hash case:
1535 * If the count is zero, we update all records.
1536 * Otherwise we just update the items in the hash.
1538 * In the notrace_hash case:
1539 * We enable the update in the hash.
1540 * As disabling notrace means enabling the tracing,
1541 * and enabling notrace means disabling, the inc variable
1545 hash = ops->filter_hash;
1546 other_hash = ops->notrace_hash;
1547 if (ftrace_hash_empty(hash))
1551 hash = ops->notrace_hash;
1552 other_hash = ops->filter_hash;
1554 * If the notrace hash has no items,
1555 * then there's nothing to do.
1557 if (ftrace_hash_empty(hash))
1561 do_for_each_ftrace_rec(pg, rec) {
1562 int in_other_hash = 0;
1568 * Only the filter_hash affects all records.
1569 * Update if the record is not in the notrace hash.
1571 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1574 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1575 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1580 if (filter_hash && in_hash && !in_other_hash)
1582 else if (!filter_hash && in_hash &&
1583 (in_other_hash || ftrace_hash_empty(other_hash)))
1591 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1594 * If any ops wants regs saved for this function
1595 * then all ops will get saved regs.
1597 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1598 rec->flags |= FTRACE_FL_REGS;
1600 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1605 /* Shortcut, if we handled all records, we are done. */
1606 if (!all && count == hash->count)
1608 } while_for_each_ftrace_rec();
1611 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1614 __ftrace_hash_rec_update(ops, filter_hash, 0);
1617 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1620 __ftrace_hash_rec_update(ops, filter_hash, 1);
1623 static void print_ip_ins(const char *fmt, unsigned char *p)
1627 printk(KERN_CONT "%s", fmt);
1629 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1630 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1634 * ftrace_bug - report and shutdown function tracer
1635 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1636 * @ip: The address that failed
1638 * The arch code that enables or disables the function tracing
1639 * can call ftrace_bug() when it has detected a problem in
1640 * modifying the code. @failed should be one of either:
1641 * EFAULT - if the problem happens on reading the @ip address
1642 * EINVAL - if what is read at @ip is not what was expected
1643 * EPERM - if the problem happens on writting to the @ip address
1645 void ftrace_bug(int failed, unsigned long ip)
1649 FTRACE_WARN_ON_ONCE(1);
1650 pr_info("ftrace faulted on modifying ");
1654 FTRACE_WARN_ON_ONCE(1);
1655 pr_info("ftrace failed to modify ");
1657 print_ip_ins(" actual: ", (unsigned char *)ip);
1658 printk(KERN_CONT "\n");
1661 FTRACE_WARN_ON_ONCE(1);
1662 pr_info("ftrace faulted on writing ");
1666 FTRACE_WARN_ON_ONCE(1);
1667 pr_info("ftrace faulted on unknown error ");
1672 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1674 unsigned long flag = 0UL;
1677 * If we are updating calls:
1679 * If the record has a ref count, then we need to enable it
1680 * because someone is using it.
1682 * Otherwise we make sure its disabled.
1684 * If we are disabling calls, then disable all records that
1687 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1688 flag = FTRACE_FL_ENABLED;
1691 * If enabling and the REGS flag does not match the REGS_EN, then
1692 * do not ignore this record. Set flags to fail the compare against
1696 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1697 flag |= FTRACE_FL_REGS;
1699 /* If the state of this record hasn't changed, then do nothing */
1700 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1701 return FTRACE_UPDATE_IGNORE;
1704 /* Save off if rec is being enabled (for return value) */
1705 flag ^= rec->flags & FTRACE_FL_ENABLED;
1708 rec->flags |= FTRACE_FL_ENABLED;
1709 if (flag & FTRACE_FL_REGS) {
1710 if (rec->flags & FTRACE_FL_REGS)
1711 rec->flags |= FTRACE_FL_REGS_EN;
1713 rec->flags &= ~FTRACE_FL_REGS_EN;
1718 * If this record is being updated from a nop, then
1719 * return UPDATE_MAKE_CALL.
1720 * Otherwise, if the EN flag is set, then return
1721 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1722 * from the non-save regs, to a save regs function.
1724 * return UPDATE_MODIFY_CALL to tell the caller to convert
1725 * from the save regs, to a non-save regs function.
1727 if (flag & FTRACE_FL_ENABLED)
1728 return FTRACE_UPDATE_MAKE_CALL;
1729 else if (rec->flags & FTRACE_FL_REGS_EN)
1730 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1732 return FTRACE_UPDATE_MODIFY_CALL;
1736 /* If there's no more users, clear all flags */
1737 if (!(rec->flags & ~FTRACE_FL_MASK))
1740 /* Just disable the record (keep REGS state) */
1741 rec->flags &= ~FTRACE_FL_ENABLED;
1744 return FTRACE_UPDATE_MAKE_NOP;
1748 * ftrace_update_record, set a record that now is tracing or not
1749 * @rec: the record to update
1750 * @enable: set to 1 if the record is tracing, zero to force disable
1752 * The records that represent all functions that can be traced need
1753 * to be updated when tracing has been enabled.
1755 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1757 return ftrace_check_record(rec, enable, 1);
1761 * ftrace_test_record, check if the record has been enabled or not
1762 * @rec: the record to test
1763 * @enable: set to 1 to check if enabled, 0 if it is disabled
1765 * The arch code may need to test if a record is already set to
1766 * tracing to determine how to modify the function code that it
1769 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1771 return ftrace_check_record(rec, enable, 0);
1775 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1777 unsigned long ftrace_old_addr;
1778 unsigned long ftrace_addr;
1781 ret = ftrace_update_record(rec, enable);
1783 if (rec->flags & FTRACE_FL_REGS)
1784 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1786 ftrace_addr = (unsigned long)FTRACE_ADDR;
1789 case FTRACE_UPDATE_IGNORE:
1792 case FTRACE_UPDATE_MAKE_CALL:
1793 return ftrace_make_call(rec, ftrace_addr);
1795 case FTRACE_UPDATE_MAKE_NOP:
1796 return ftrace_make_nop(NULL, rec, ftrace_addr);
1798 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1799 case FTRACE_UPDATE_MODIFY_CALL:
1800 if (rec->flags & FTRACE_FL_REGS)
1801 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1803 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1805 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1808 return -1; /* unknow ftrace bug */
1811 void __weak ftrace_replace_code(int enable)
1813 struct dyn_ftrace *rec;
1814 struct ftrace_page *pg;
1817 if (unlikely(ftrace_disabled))
1820 do_for_each_ftrace_rec(pg, rec) {
1821 failed = __ftrace_replace_code(rec, enable);
1823 ftrace_bug(failed, rec->ip);
1824 /* Stop processing */
1827 } while_for_each_ftrace_rec();
1830 struct ftrace_rec_iter {
1831 struct ftrace_page *pg;
1836 * ftrace_rec_iter_start, start up iterating over traced functions
1838 * Returns an iterator handle that is used to iterate over all
1839 * the records that represent address locations where functions
1842 * May return NULL if no records are available.
1844 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1847 * We only use a single iterator.
1848 * Protected by the ftrace_lock mutex.
1850 static struct ftrace_rec_iter ftrace_rec_iter;
1851 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1853 iter->pg = ftrace_pages_start;
1856 /* Could have empty pages */
1857 while (iter->pg && !iter->pg->index)
1858 iter->pg = iter->pg->next;
1867 * ftrace_rec_iter_next, get the next record to process.
1868 * @iter: The handle to the iterator.
1870 * Returns the next iterator after the given iterator @iter.
1872 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1876 if (iter->index >= iter->pg->index) {
1877 iter->pg = iter->pg->next;
1880 /* Could have empty pages */
1881 while (iter->pg && !iter->pg->index)
1882 iter->pg = iter->pg->next;
1892 * ftrace_rec_iter_record, get the record at the iterator location
1893 * @iter: The current iterator location
1895 * Returns the record that the current @iter is at.
1897 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1899 return &iter->pg->records[iter->index];
1903 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1910 if (unlikely(ftrace_disabled))
1913 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1915 ftrace_bug(ret, ip);
1922 * archs can override this function if they must do something
1923 * before the modifying code is performed.
1925 int __weak ftrace_arch_code_modify_prepare(void)
1931 * archs can override this function if they must do something
1932 * after the modifying code is performed.
1934 int __weak ftrace_arch_code_modify_post_process(void)
1939 void ftrace_modify_all_code(int command)
1941 if (command & FTRACE_UPDATE_CALLS)
1942 ftrace_replace_code(1);
1943 else if (command & FTRACE_DISABLE_CALLS)
1944 ftrace_replace_code(0);
1946 if (command & FTRACE_UPDATE_TRACE_FUNC)
1947 ftrace_update_ftrace_func(ftrace_trace_function);
1949 if (command & FTRACE_START_FUNC_RET)
1950 ftrace_enable_ftrace_graph_caller();
1951 else if (command & FTRACE_STOP_FUNC_RET)
1952 ftrace_disable_ftrace_graph_caller();
1955 static int __ftrace_modify_code(void *data)
1957 int *command = data;
1959 ftrace_modify_all_code(*command);
1965 * ftrace_run_stop_machine, go back to the stop machine method
1966 * @command: The command to tell ftrace what to do
1968 * If an arch needs to fall back to the stop machine method, the
1969 * it can call this function.
1971 void ftrace_run_stop_machine(int command)
1973 stop_machine(__ftrace_modify_code, &command, NULL);
1977 * arch_ftrace_update_code, modify the code to trace or not trace
1978 * @command: The command that needs to be done
1980 * Archs can override this function if it does not need to
1981 * run stop_machine() to modify code.
1983 void __weak arch_ftrace_update_code(int command)
1985 ftrace_run_stop_machine(command);
1988 static void ftrace_run_update_code(int command)
1992 ret = ftrace_arch_code_modify_prepare();
1993 FTRACE_WARN_ON(ret);
1997 * Do not call function tracer while we update the code.
1998 * We are in stop machine.
2000 function_trace_stop++;
2003 * By default we use stop_machine() to modify the code.
2004 * But archs can do what ever they want as long as it
2005 * is safe. The stop_machine() is the safest, but also
2006 * produces the most overhead.
2008 arch_ftrace_update_code(command);
2010 function_trace_stop--;
2012 ret = ftrace_arch_code_modify_post_process();
2013 FTRACE_WARN_ON(ret);
2016 static ftrace_func_t saved_ftrace_func;
2017 static int ftrace_start_up;
2018 static int global_start_up;
2020 static void ftrace_startup_enable(int command)
2022 if (saved_ftrace_func != ftrace_trace_function) {
2023 saved_ftrace_func = ftrace_trace_function;
2024 command |= FTRACE_UPDATE_TRACE_FUNC;
2027 if (!command || !ftrace_enabled)
2030 ftrace_run_update_code(command);
2033 static int ftrace_startup(struct ftrace_ops *ops, int command)
2035 bool hash_enable = true;
2037 if (unlikely(ftrace_disabled))
2041 command |= FTRACE_UPDATE_CALLS;
2043 /* ops marked global share the filter hashes */
2044 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2046 /* Don't update hash if global is already set */
2047 if (global_start_up)
2048 hash_enable = false;
2052 ops->flags |= FTRACE_OPS_FL_ENABLED;
2054 ftrace_hash_rec_enable(ops, 1);
2056 ftrace_startup_enable(command);
2061 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
2063 bool hash_disable = true;
2065 if (unlikely(ftrace_disabled))
2070 * Just warn in case of unbalance, no need to kill ftrace, it's not
2071 * critical but the ftrace_call callers may be never nopped again after
2072 * further ftrace uses.
2074 WARN_ON_ONCE(ftrace_start_up < 0);
2076 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2079 WARN_ON_ONCE(global_start_up < 0);
2080 /* Don't update hash if global still has users */
2081 if (global_start_up) {
2082 WARN_ON_ONCE(!ftrace_start_up);
2083 hash_disable = false;
2088 ftrace_hash_rec_disable(ops, 1);
2090 if (ops != &global_ops || !global_start_up)
2091 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2093 command |= FTRACE_UPDATE_CALLS;
2095 if (saved_ftrace_func != ftrace_trace_function) {
2096 saved_ftrace_func = ftrace_trace_function;
2097 command |= FTRACE_UPDATE_TRACE_FUNC;
2100 if (!command || !ftrace_enabled)
2103 ftrace_run_update_code(command);
2106 static void ftrace_startup_sysctl(void)
2108 if (unlikely(ftrace_disabled))
2111 /* Force update next time */
2112 saved_ftrace_func = NULL;
2113 /* ftrace_start_up is true if we want ftrace running */
2114 if (ftrace_start_up)
2115 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2118 static void ftrace_shutdown_sysctl(void)
2120 if (unlikely(ftrace_disabled))
2123 /* ftrace_start_up is true if ftrace is running */
2124 if (ftrace_start_up)
2125 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2128 static cycle_t ftrace_update_time;
2129 static unsigned long ftrace_update_cnt;
2130 unsigned long ftrace_update_tot_cnt;
2132 static int ops_traces_mod(struct ftrace_ops *ops)
2134 struct ftrace_hash *hash;
2136 hash = ops->filter_hash;
2137 return ftrace_hash_empty(hash);
2140 static int ftrace_update_code(struct module *mod)
2142 struct ftrace_page *pg;
2143 struct dyn_ftrace *p;
2144 cycle_t start, stop;
2145 unsigned long ref = 0;
2149 * When adding a module, we need to check if tracers are
2150 * currently enabled and if they are set to trace all functions.
2151 * If they are, we need to enable the module functions as well
2152 * as update the reference counts for those function records.
2155 struct ftrace_ops *ops;
2157 for (ops = ftrace_ops_list;
2158 ops != &ftrace_list_end; ops = ops->next) {
2159 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2160 ops_traces_mod(ops))
2165 start = ftrace_now(raw_smp_processor_id());
2166 ftrace_update_cnt = 0;
2168 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2170 for (i = 0; i < pg->index; i++) {
2171 /* If something went wrong, bail without enabling anything */
2172 if (unlikely(ftrace_disabled))
2175 p = &pg->records[i];
2179 * Do the initial record conversion from mcount jump
2180 * to the NOP instructions.
2182 if (!ftrace_code_disable(mod, p))
2185 ftrace_update_cnt++;
2188 * If the tracing is enabled, go ahead and enable the record.
2190 * The reason not to enable the record immediatelly is the
2191 * inherent check of ftrace_make_nop/ftrace_make_call for
2192 * correct previous instructions. Making first the NOP
2193 * conversion puts the module to the correct state, thus
2194 * passing the ftrace_make_call check.
2196 if (ftrace_start_up && ref) {
2197 int failed = __ftrace_replace_code(p, 1);
2199 ftrace_bug(failed, p->ip);
2204 ftrace_new_pgs = NULL;
2206 stop = ftrace_now(raw_smp_processor_id());
2207 ftrace_update_time = stop - start;
2208 ftrace_update_tot_cnt += ftrace_update_cnt;
2213 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2218 if (WARN_ON(!count))
2221 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2224 * We want to fill as much as possible. No more than a page
2227 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2231 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2234 /* if we can't allocate this size, try something smaller */
2241 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2250 static struct ftrace_page *
2251 ftrace_allocate_pages(unsigned long num_to_init)
2253 struct ftrace_page *start_pg;
2254 struct ftrace_page *pg;
2261 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2266 * Try to allocate as much as possible in one continues
2267 * location that fills in all of the space. We want to
2268 * waste as little space as possible.
2271 cnt = ftrace_allocate_records(pg, num_to_init);
2279 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2290 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2291 free_pages((unsigned long)pg->records, order);
2292 start_pg = pg->next;
2296 pr_info("ftrace: FAILED to allocate memory for functions\n");
2300 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2305 pr_info("ftrace: No functions to be traced?\n");
2309 cnt = num_to_init / ENTRIES_PER_PAGE;
2310 pr_info("ftrace: allocating %ld entries in %d pages\n",
2311 num_to_init, cnt + 1);
2316 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2318 struct ftrace_iterator {
2321 struct ftrace_page *pg;
2322 struct dyn_ftrace *func;
2323 struct ftrace_func_probe *probe;
2324 struct trace_parser parser;
2325 struct ftrace_hash *hash;
2326 struct ftrace_ops *ops;
2333 t_hash_next(struct seq_file *m, loff_t *pos)
2335 struct ftrace_iterator *iter = m->private;
2336 struct hlist_node *hnd = NULL;
2337 struct hlist_head *hhd;
2343 hnd = &iter->probe->node;
2345 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2348 hhd = &ftrace_func_hash[iter->hidx];
2350 if (hlist_empty(hhd)) {
2366 if (WARN_ON_ONCE(!hnd))
2369 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2374 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2376 struct ftrace_iterator *iter = m->private;
2380 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2383 if (iter->func_pos > *pos)
2387 for (l = 0; l <= (*pos - iter->func_pos); ) {
2388 p = t_hash_next(m, &l);
2395 /* Only set this if we have an item */
2396 iter->flags |= FTRACE_ITER_HASH;
2402 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2404 struct ftrace_func_probe *rec;
2407 if (WARN_ON_ONCE(!rec))
2410 if (rec->ops->print)
2411 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2413 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2416 seq_printf(m, ":%p", rec->data);
2423 t_next(struct seq_file *m, void *v, loff_t *pos)
2425 struct ftrace_iterator *iter = m->private;
2426 struct ftrace_ops *ops = iter->ops;
2427 struct dyn_ftrace *rec = NULL;
2429 if (unlikely(ftrace_disabled))
2432 if (iter->flags & FTRACE_ITER_HASH)
2433 return t_hash_next(m, pos);
2436 iter->pos = iter->func_pos = *pos;
2438 if (iter->flags & FTRACE_ITER_PRINTALL)
2439 return t_hash_start(m, pos);
2442 if (iter->idx >= iter->pg->index) {
2443 if (iter->pg->next) {
2444 iter->pg = iter->pg->next;
2449 rec = &iter->pg->records[iter->idx++];
2450 if (((iter->flags & FTRACE_ITER_FILTER) &&
2451 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2453 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2454 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2456 ((iter->flags & FTRACE_ITER_ENABLED) &&
2457 !(rec->flags & ~FTRACE_FL_MASK))) {
2465 return t_hash_start(m, pos);
2472 static void reset_iter_read(struct ftrace_iterator *iter)
2476 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2479 static void *t_start(struct seq_file *m, loff_t *pos)
2481 struct ftrace_iterator *iter = m->private;
2482 struct ftrace_ops *ops = iter->ops;
2486 mutex_lock(&ftrace_lock);
2488 if (unlikely(ftrace_disabled))
2492 * If an lseek was done, then reset and start from beginning.
2494 if (*pos < iter->pos)
2495 reset_iter_read(iter);
2498 * For set_ftrace_filter reading, if we have the filter
2499 * off, we can short cut and just print out that all
2500 * functions are enabled.
2502 if (iter->flags & FTRACE_ITER_FILTER &&
2503 ftrace_hash_empty(ops->filter_hash)) {
2505 return t_hash_start(m, pos);
2506 iter->flags |= FTRACE_ITER_PRINTALL;
2507 /* reset in case of seek/pread */
2508 iter->flags &= ~FTRACE_ITER_HASH;
2512 if (iter->flags & FTRACE_ITER_HASH)
2513 return t_hash_start(m, pos);
2516 * Unfortunately, we need to restart at ftrace_pages_start
2517 * every time we let go of the ftrace_mutex. This is because
2518 * those pointers can change without the lock.
2520 iter->pg = ftrace_pages_start;
2522 for (l = 0; l <= *pos; ) {
2523 p = t_next(m, p, &l);
2529 return t_hash_start(m, pos);
2534 static void t_stop(struct seq_file *m, void *p)
2536 mutex_unlock(&ftrace_lock);
2539 static int t_show(struct seq_file *m, void *v)
2541 struct ftrace_iterator *iter = m->private;
2542 struct dyn_ftrace *rec;
2544 if (iter->flags & FTRACE_ITER_HASH)
2545 return t_hash_show(m, iter);
2547 if (iter->flags & FTRACE_ITER_PRINTALL) {
2548 seq_printf(m, "#### all functions enabled ####\n");
2557 seq_printf(m, "%ps", (void *)rec->ip);
2558 if (iter->flags & FTRACE_ITER_ENABLED)
2559 seq_printf(m, " (%ld)%s",
2560 rec->flags & ~FTRACE_FL_MASK,
2561 rec->flags & FTRACE_FL_REGS ? " R" : "");
2562 seq_printf(m, "\n");
2567 static const struct seq_operations show_ftrace_seq_ops = {
2575 ftrace_avail_open(struct inode *inode, struct file *file)
2577 struct ftrace_iterator *iter;
2579 if (unlikely(ftrace_disabled))
2582 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2584 iter->pg = ftrace_pages_start;
2585 iter->ops = &global_ops;
2588 return iter ? 0 : -ENOMEM;
2592 ftrace_enabled_open(struct inode *inode, struct file *file)
2594 struct ftrace_iterator *iter;
2596 if (unlikely(ftrace_disabled))
2599 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2601 iter->pg = ftrace_pages_start;
2602 iter->flags = FTRACE_ITER_ENABLED;
2603 iter->ops = &global_ops;
2606 return iter ? 0 : -ENOMEM;
2609 static void ftrace_filter_reset(struct ftrace_hash *hash)
2611 mutex_lock(&ftrace_lock);
2612 ftrace_hash_clear(hash);
2613 mutex_unlock(&ftrace_lock);
2617 * ftrace_regex_open - initialize function tracer filter files
2618 * @ops: The ftrace_ops that hold the hash filters
2619 * @flag: The type of filter to process
2620 * @inode: The inode, usually passed in to your open routine
2621 * @file: The file, usually passed in to your open routine
2623 * ftrace_regex_open() initializes the filter files for the
2624 * @ops. Depending on @flag it may process the filter hash or
2625 * the notrace hash of @ops. With this called from the open
2626 * routine, you can use ftrace_filter_write() for the write
2627 * routine if @flag has FTRACE_ITER_FILTER set, or
2628 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2629 * ftrace_regex_lseek() should be used as the lseek routine, and
2630 * release must call ftrace_regex_release().
2633 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2634 struct inode *inode, struct file *file)
2636 struct ftrace_iterator *iter;
2637 struct ftrace_hash *hash;
2640 if (unlikely(ftrace_disabled))
2643 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2647 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2652 if (flag & FTRACE_ITER_NOTRACE)
2653 hash = ops->notrace_hash;
2655 hash = ops->filter_hash;
2660 if (file->f_mode & FMODE_WRITE) {
2661 mutex_lock(&ftrace_lock);
2662 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2663 mutex_unlock(&ftrace_lock);
2666 trace_parser_put(&iter->parser);
2672 mutex_lock(&ftrace_regex_lock);
2674 if ((file->f_mode & FMODE_WRITE) &&
2675 (file->f_flags & O_TRUNC))
2676 ftrace_filter_reset(iter->hash);
2678 if (file->f_mode & FMODE_READ) {
2679 iter->pg = ftrace_pages_start;
2681 ret = seq_open(file, &show_ftrace_seq_ops);
2683 struct seq_file *m = file->private_data;
2687 free_ftrace_hash(iter->hash);
2688 trace_parser_put(&iter->parser);
2692 file->private_data = iter;
2693 mutex_unlock(&ftrace_regex_lock);
2699 ftrace_filter_open(struct inode *inode, struct file *file)
2701 return ftrace_regex_open(&global_ops,
2702 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2707 ftrace_notrace_open(struct inode *inode, struct file *file)
2709 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2714 ftrace_regex_lseek(struct file *file, loff_t offset, int whence)
2718 if (file->f_mode & FMODE_READ)
2719 ret = seq_lseek(file, offset, whence);
2721 file->f_pos = ret = 1;
2726 static int ftrace_match(char *str, char *regex, int len, int type)
2733 if (strcmp(str, regex) == 0)
2736 case MATCH_FRONT_ONLY:
2737 if (strncmp(str, regex, len) == 0)
2740 case MATCH_MIDDLE_ONLY:
2741 if (strstr(str, regex))
2744 case MATCH_END_ONLY:
2746 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2755 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2757 struct ftrace_func_entry *entry;
2760 entry = ftrace_lookup_ip(hash, rec->ip);
2762 /* Do nothing if it doesn't exist */
2766 free_hash_entry(hash, entry);
2768 /* Do nothing if it exists */
2772 ret = add_hash_entry(hash, rec->ip);
2778 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2779 char *regex, int len, int type)
2781 char str[KSYM_SYMBOL_LEN];
2784 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2787 /* module lookup requires matching the module */
2788 if (!modname || strcmp(modname, mod))
2791 /* blank search means to match all funcs in the mod */
2796 return ftrace_match(str, regex, len, type);
2800 match_records(struct ftrace_hash *hash, char *buff,
2801 int len, char *mod, int not)
2803 unsigned search_len = 0;
2804 struct ftrace_page *pg;
2805 struct dyn_ftrace *rec;
2806 int type = MATCH_FULL;
2807 char *search = buff;
2812 type = filter_parse_regex(buff, len, &search, ¬);
2813 search_len = strlen(search);
2816 mutex_lock(&ftrace_lock);
2818 if (unlikely(ftrace_disabled))
2821 do_for_each_ftrace_rec(pg, rec) {
2822 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2823 ret = enter_record(hash, rec, not);
2830 } while_for_each_ftrace_rec();
2832 mutex_unlock(&ftrace_lock);
2838 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2840 return match_records(hash, buff, len, NULL, 0);
2844 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2848 /* blank or '*' mean the same */
2849 if (strcmp(buff, "*") == 0)
2852 /* handle the case of 'dont filter this module' */
2853 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2858 return match_records(hash, buff, strlen(buff), mod, not);
2862 * We register the module command as a template to show others how
2863 * to register the a command as well.
2867 ftrace_mod_callback(struct ftrace_hash *hash,
2868 char *func, char *cmd, char *param, int enable)
2874 * cmd == 'mod' because we only registered this func
2875 * for the 'mod' ftrace_func_command.
2876 * But if you register one func with multiple commands,
2877 * you can tell which command was used by the cmd
2881 /* we must have a module name */
2885 mod = strsep(¶m, ":");
2889 ret = ftrace_match_module_records(hash, func, mod);
2898 static struct ftrace_func_command ftrace_mod_cmd = {
2900 .func = ftrace_mod_callback,
2903 static int __init ftrace_mod_cmd_init(void)
2905 return register_ftrace_command(&ftrace_mod_cmd);
2907 core_initcall(ftrace_mod_cmd_init);
2909 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2910 struct ftrace_ops *op, struct pt_regs *pt_regs)
2912 struct ftrace_func_probe *entry;
2913 struct hlist_head *hhd;
2914 struct hlist_node *n;
2917 key = hash_long(ip, FTRACE_HASH_BITS);
2919 hhd = &ftrace_func_hash[key];
2921 if (hlist_empty(hhd))
2925 * Disable preemption for these calls to prevent a RCU grace
2926 * period. This syncs the hash iteration and freeing of items
2927 * on the hash. rcu_read_lock is too dangerous here.
2929 preempt_disable_notrace();
2930 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2931 if (entry->ip == ip)
2932 entry->ops->func(ip, parent_ip, &entry->data);
2934 preempt_enable_notrace();
2937 static struct ftrace_ops trace_probe_ops __read_mostly =
2939 .func = function_trace_probe_call,
2942 static int ftrace_probe_registered;
2944 static void __enable_ftrace_function_probe(void)
2949 if (ftrace_probe_registered)
2952 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2953 struct hlist_head *hhd = &ftrace_func_hash[i];
2957 /* Nothing registered? */
2958 if (i == FTRACE_FUNC_HASHSIZE)
2961 ret = __register_ftrace_function(&trace_probe_ops);
2963 ret = ftrace_startup(&trace_probe_ops, 0);
2965 ftrace_probe_registered = 1;
2968 static void __disable_ftrace_function_probe(void)
2973 if (!ftrace_probe_registered)
2976 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2977 struct hlist_head *hhd = &ftrace_func_hash[i];
2982 /* no more funcs left */
2983 ret = __unregister_ftrace_function(&trace_probe_ops);
2985 ftrace_shutdown(&trace_probe_ops, 0);
2987 ftrace_probe_registered = 0;
2991 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2993 struct ftrace_func_probe *entry =
2994 container_of(rhp, struct ftrace_func_probe, rcu);
2996 if (entry->ops->free)
2997 entry->ops->free(&entry->data);
3003 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3006 struct ftrace_func_probe *entry;
3007 struct ftrace_page *pg;
3008 struct dyn_ftrace *rec;
3014 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3015 len = strlen(search);
3017 /* we do not support '!' for function probes */
3021 mutex_lock(&ftrace_lock);
3023 if (unlikely(ftrace_disabled))
3026 do_for_each_ftrace_rec(pg, rec) {
3028 if (!ftrace_match_record(rec, NULL, search, len, type))
3031 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3033 /* If we did not process any, then return error */
3044 * The caller might want to do something special
3045 * for each function we find. We call the callback
3046 * to give the caller an opportunity to do so.
3048 if (ops->callback) {
3049 if (ops->callback(rec->ip, &entry->data) < 0) {
3050 /* caller does not like this func */
3057 entry->ip = rec->ip;
3059 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3060 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3062 } while_for_each_ftrace_rec();
3063 __enable_ftrace_function_probe();
3066 mutex_unlock(&ftrace_lock);
3072 PROBE_TEST_FUNC = 1,
3077 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3078 void *data, int flags)
3080 struct ftrace_func_probe *entry;
3081 struct hlist_node *n, *tmp;
3082 char str[KSYM_SYMBOL_LEN];
3083 int type = MATCH_FULL;
3087 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3092 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3093 len = strlen(search);
3095 /* we do not support '!' for function probes */
3100 mutex_lock(&ftrace_lock);
3101 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3102 struct hlist_head *hhd = &ftrace_func_hash[i];
3104 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3106 /* break up if statements for readability */
3107 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3110 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3113 /* do this last, since it is the most expensive */
3115 kallsyms_lookup(entry->ip, NULL, NULL,
3117 if (!ftrace_match(str, glob, len, type))
3121 hlist_del(&entry->node);
3122 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3125 __disable_ftrace_function_probe();
3126 mutex_unlock(&ftrace_lock);
3130 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3133 __unregister_ftrace_function_probe(glob, ops, data,
3134 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3138 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3140 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3143 void unregister_ftrace_function_probe_all(char *glob)
3145 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3148 static LIST_HEAD(ftrace_commands);
3149 static DEFINE_MUTEX(ftrace_cmd_mutex);
3151 int register_ftrace_command(struct ftrace_func_command *cmd)
3153 struct ftrace_func_command *p;
3156 mutex_lock(&ftrace_cmd_mutex);
3157 list_for_each_entry(p, &ftrace_commands, list) {
3158 if (strcmp(cmd->name, p->name) == 0) {
3163 list_add(&cmd->list, &ftrace_commands);
3165 mutex_unlock(&ftrace_cmd_mutex);
3170 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3172 struct ftrace_func_command *p, *n;
3175 mutex_lock(&ftrace_cmd_mutex);
3176 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3177 if (strcmp(cmd->name, p->name) == 0) {
3179 list_del_init(&p->list);
3184 mutex_unlock(&ftrace_cmd_mutex);
3189 static int ftrace_process_regex(struct ftrace_hash *hash,
3190 char *buff, int len, int enable)
3192 char *func, *command, *next = buff;
3193 struct ftrace_func_command *p;
3196 func = strsep(&next, ":");
3199 ret = ftrace_match_records(hash, func, len);
3209 command = strsep(&next, ":");
3211 mutex_lock(&ftrace_cmd_mutex);
3212 list_for_each_entry(p, &ftrace_commands, list) {
3213 if (strcmp(p->name, command) == 0) {
3214 ret = p->func(hash, func, command, next, enable);
3219 mutex_unlock(&ftrace_cmd_mutex);
3225 ftrace_regex_write(struct file *file, const char __user *ubuf,
3226 size_t cnt, loff_t *ppos, int enable)
3228 struct ftrace_iterator *iter;
3229 struct trace_parser *parser;
3235 mutex_lock(&ftrace_regex_lock);
3238 if (unlikely(ftrace_disabled))
3241 if (file->f_mode & FMODE_READ) {
3242 struct seq_file *m = file->private_data;
3245 iter = file->private_data;
3247 parser = &iter->parser;
3248 read = trace_get_user(parser, ubuf, cnt, ppos);
3250 if (read >= 0 && trace_parser_loaded(parser) &&
3251 !trace_parser_cont(parser)) {
3252 ret = ftrace_process_regex(iter->hash, parser->buffer,
3253 parser->idx, enable);
3254 trace_parser_clear(parser);
3261 mutex_unlock(&ftrace_regex_lock);
3267 ftrace_filter_write(struct file *file, const char __user *ubuf,
3268 size_t cnt, loff_t *ppos)
3270 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3274 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3275 size_t cnt, loff_t *ppos)
3277 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3281 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3283 struct ftrace_func_entry *entry;
3285 if (!ftrace_location(ip))
3289 entry = ftrace_lookup_ip(hash, ip);
3292 free_hash_entry(hash, entry);
3296 return add_hash_entry(hash, ip);
3300 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3301 unsigned long ip, int remove, int reset, int enable)
3303 struct ftrace_hash **orig_hash;
3304 struct ftrace_hash *hash;
3307 /* All global ops uses the global ops filters */
3308 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3311 if (unlikely(ftrace_disabled))
3315 orig_hash = &ops->filter_hash;
3317 orig_hash = &ops->notrace_hash;
3319 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3323 mutex_lock(&ftrace_regex_lock);
3325 ftrace_filter_reset(hash);
3326 if (buf && !ftrace_match_records(hash, buf, len)) {
3328 goto out_regex_unlock;
3331 ret = ftrace_match_addr(hash, ip, remove);
3333 goto out_regex_unlock;
3336 mutex_lock(&ftrace_lock);
3337 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3338 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3340 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3342 mutex_unlock(&ftrace_lock);
3345 mutex_unlock(&ftrace_regex_lock);
3347 free_ftrace_hash(hash);
3352 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3353 int reset, int enable)
3355 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3359 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3360 * @ops - the ops to set the filter with
3361 * @ip - the address to add to or remove from the filter.
3362 * @remove - non zero to remove the ip from the filter
3363 * @reset - non zero to reset all filters before applying this filter.
3365 * Filters denote which functions should be enabled when tracing is enabled
3366 * If @ip is NULL, it failes to update filter.
3368 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3369 int remove, int reset)
3371 return ftrace_set_addr(ops, ip, remove, reset, 1);
3373 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3376 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3377 int reset, int enable)
3379 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3383 * ftrace_set_filter - set a function to filter on in ftrace
3384 * @ops - the ops to set the filter with
3385 * @buf - the string that holds the function filter text.
3386 * @len - the length of the string.
3387 * @reset - non zero to reset all filters before applying this filter.
3389 * Filters denote which functions should be enabled when tracing is enabled.
3390 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3392 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3395 return ftrace_set_regex(ops, buf, len, reset, 1);
3397 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3400 * ftrace_set_notrace - set a function to not trace in ftrace
3401 * @ops - the ops to set the notrace filter with
3402 * @buf - the string that holds the function notrace text.
3403 * @len - the length of the string.
3404 * @reset - non zero to reset all filters before applying this filter.
3406 * Notrace Filters denote which functions should not be enabled when tracing
3407 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3410 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3413 return ftrace_set_regex(ops, buf, len, reset, 0);
3415 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3417 * ftrace_set_filter - set a function to filter on in ftrace
3418 * @ops - the ops to set the filter with
3419 * @buf - the string that holds the function filter text.
3420 * @len - the length of the string.
3421 * @reset - non zero to reset all filters before applying this filter.
3423 * Filters denote which functions should be enabled when tracing is enabled.
3424 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3426 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3428 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3430 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3433 * ftrace_set_notrace - set a function to not trace in ftrace
3434 * @ops - the ops to set the notrace filter with
3435 * @buf - the string that holds the function notrace text.
3436 * @len - the length of the string.
3437 * @reset - non zero to reset all filters before applying this filter.
3439 * Notrace Filters denote which functions should not be enabled when tracing
3440 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3443 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3445 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3447 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3450 * command line interface to allow users to set filters on boot up.
3452 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3453 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3454 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3456 static int __init set_ftrace_notrace(char *str)
3458 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3461 __setup("ftrace_notrace=", set_ftrace_notrace);
3463 static int __init set_ftrace_filter(char *str)
3465 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3468 __setup("ftrace_filter=", set_ftrace_filter);
3470 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3471 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3472 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3474 static int __init set_graph_function(char *str)
3476 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3479 __setup("ftrace_graph_filter=", set_graph_function);
3481 static void __init set_ftrace_early_graph(char *buf)
3487 func = strsep(&buf, ",");
3488 /* we allow only one expression at a time */
3489 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3492 printk(KERN_DEBUG "ftrace: function %s not "
3493 "traceable\n", func);
3496 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3499 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3504 func = strsep(&buf, ",");
3505 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3509 static void __init set_ftrace_early_filters(void)
3511 if (ftrace_filter_buf[0])
3512 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3513 if (ftrace_notrace_buf[0])
3514 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3515 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3516 if (ftrace_graph_buf[0])
3517 set_ftrace_early_graph(ftrace_graph_buf);
3518 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3521 int ftrace_regex_release(struct inode *inode, struct file *file)
3523 struct seq_file *m = (struct seq_file *)file->private_data;
3524 struct ftrace_iterator *iter;
3525 struct ftrace_hash **orig_hash;
3526 struct trace_parser *parser;
3530 mutex_lock(&ftrace_regex_lock);
3531 if (file->f_mode & FMODE_READ) {
3534 seq_release(inode, file);
3536 iter = file->private_data;
3538 parser = &iter->parser;
3539 if (trace_parser_loaded(parser)) {
3540 parser->buffer[parser->idx] = 0;
3541 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3544 trace_parser_put(parser);
3546 if (file->f_mode & FMODE_WRITE) {
3547 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3550 orig_hash = &iter->ops->filter_hash;
3552 orig_hash = &iter->ops->notrace_hash;
3554 mutex_lock(&ftrace_lock);
3555 ret = ftrace_hash_move(iter->ops, filter_hash,
3556 orig_hash, iter->hash);
3557 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3559 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3561 mutex_unlock(&ftrace_lock);
3563 free_ftrace_hash(iter->hash);
3566 mutex_unlock(&ftrace_regex_lock);
3570 static const struct file_operations ftrace_avail_fops = {
3571 .open = ftrace_avail_open,
3573 .llseek = seq_lseek,
3574 .release = seq_release_private,
3577 static const struct file_operations ftrace_enabled_fops = {
3578 .open = ftrace_enabled_open,
3580 .llseek = seq_lseek,
3581 .release = seq_release_private,
3584 static const struct file_operations ftrace_filter_fops = {
3585 .open = ftrace_filter_open,
3587 .write = ftrace_filter_write,
3588 .llseek = ftrace_regex_lseek,
3589 .release = ftrace_regex_release,
3592 static const struct file_operations ftrace_notrace_fops = {
3593 .open = ftrace_notrace_open,
3595 .write = ftrace_notrace_write,
3596 .llseek = ftrace_regex_lseek,
3597 .release = ftrace_regex_release,
3600 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3602 static DEFINE_MUTEX(graph_lock);
3604 int ftrace_graph_count;
3605 int ftrace_graph_filter_enabled;
3606 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3609 __g_next(struct seq_file *m, loff_t *pos)
3611 if (*pos >= ftrace_graph_count)
3613 return &ftrace_graph_funcs[*pos];
3617 g_next(struct seq_file *m, void *v, loff_t *pos)
3620 return __g_next(m, pos);
3623 static void *g_start(struct seq_file *m, loff_t *pos)
3625 mutex_lock(&graph_lock);
3627 /* Nothing, tell g_show to print all functions are enabled */
3628 if (!ftrace_graph_filter_enabled && !*pos)
3631 return __g_next(m, pos);
3634 static void g_stop(struct seq_file *m, void *p)
3636 mutex_unlock(&graph_lock);
3639 static int g_show(struct seq_file *m, void *v)
3641 unsigned long *ptr = v;
3646 if (ptr == (unsigned long *)1) {
3647 seq_printf(m, "#### all functions enabled ####\n");
3651 seq_printf(m, "%ps\n", (void *)*ptr);
3656 static const struct seq_operations ftrace_graph_seq_ops = {
3664 ftrace_graph_open(struct inode *inode, struct file *file)
3668 if (unlikely(ftrace_disabled))
3671 mutex_lock(&graph_lock);
3672 if ((file->f_mode & FMODE_WRITE) &&
3673 (file->f_flags & O_TRUNC)) {
3674 ftrace_graph_filter_enabled = 0;
3675 ftrace_graph_count = 0;
3676 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3678 mutex_unlock(&graph_lock);
3680 if (file->f_mode & FMODE_READ)
3681 ret = seq_open(file, &ftrace_graph_seq_ops);
3687 ftrace_graph_release(struct inode *inode, struct file *file)
3689 if (file->f_mode & FMODE_READ)
3690 seq_release(inode, file);
3695 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3697 struct dyn_ftrace *rec;
3698 struct ftrace_page *pg;
3707 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3708 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3711 search_len = strlen(search);
3713 mutex_lock(&ftrace_lock);
3715 if (unlikely(ftrace_disabled)) {
3716 mutex_unlock(&ftrace_lock);
3720 do_for_each_ftrace_rec(pg, rec) {
3722 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3723 /* if it is in the array */
3725 for (i = 0; i < *idx; i++) {
3726 if (array[i] == rec->ip) {
3735 array[(*idx)++] = rec->ip;
3736 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3741 array[i] = array[--(*idx)];
3747 } while_for_each_ftrace_rec();
3749 mutex_unlock(&ftrace_lock);
3754 ftrace_graph_filter_enabled = 1;
3759 ftrace_graph_write(struct file *file, const char __user *ubuf,
3760 size_t cnt, loff_t *ppos)
3762 struct trace_parser parser;
3768 mutex_lock(&graph_lock);
3770 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3775 read = trace_get_user(&parser, ubuf, cnt, ppos);
3777 if (read >= 0 && trace_parser_loaded((&parser))) {
3778 parser.buffer[parser.idx] = 0;
3780 /* we allow only one expression at a time */
3781 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3790 trace_parser_put(&parser);
3792 mutex_unlock(&graph_lock);
3797 static const struct file_operations ftrace_graph_fops = {
3798 .open = ftrace_graph_open,
3800 .write = ftrace_graph_write,
3801 .release = ftrace_graph_release,
3802 .llseek = seq_lseek,
3804 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3806 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3809 trace_create_file("available_filter_functions", 0444,
3810 d_tracer, NULL, &ftrace_avail_fops);
3812 trace_create_file("enabled_functions", 0444,
3813 d_tracer, NULL, &ftrace_enabled_fops);
3815 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3816 NULL, &ftrace_filter_fops);
3818 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3819 NULL, &ftrace_notrace_fops);
3821 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3822 trace_create_file("set_graph_function", 0444, d_tracer,
3824 &ftrace_graph_fops);
3825 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3830 static int ftrace_cmp_ips(const void *a, const void *b)
3832 const unsigned long *ipa = a;
3833 const unsigned long *ipb = b;
3842 static void ftrace_swap_ips(void *a, void *b, int size)
3844 unsigned long *ipa = a;
3845 unsigned long *ipb = b;
3853 static int ftrace_process_locs(struct module *mod,
3854 unsigned long *start,
3857 struct ftrace_page *start_pg;
3858 struct ftrace_page *pg;
3859 struct dyn_ftrace *rec;
3860 unsigned long count;
3863 unsigned long flags = 0; /* Shut up gcc */
3866 count = end - start;
3871 sort(start, count, sizeof(*start),
3872 ftrace_cmp_ips, ftrace_swap_ips);
3874 start_pg = ftrace_allocate_pages(count);
3878 mutex_lock(&ftrace_lock);
3881 * Core and each module needs their own pages, as
3882 * modules will free them when they are removed.
3883 * Force a new page to be allocated for modules.
3886 WARN_ON(ftrace_pages || ftrace_pages_start);
3887 /* First initialization */
3888 ftrace_pages = ftrace_pages_start = start_pg;
3893 if (WARN_ON(ftrace_pages->next)) {
3894 /* Hmm, we have free pages? */
3895 while (ftrace_pages->next)
3896 ftrace_pages = ftrace_pages->next;
3899 ftrace_pages->next = start_pg;
3905 addr = ftrace_call_adjust(*p++);
3907 * Some architecture linkers will pad between
3908 * the different mcount_loc sections of different
3909 * object files to satisfy alignments.
3910 * Skip any NULL pointers.
3915 if (pg->index == pg->size) {
3916 /* We should have allocated enough */
3917 if (WARN_ON(!pg->next))
3922 rec = &pg->records[pg->index++];
3926 /* We should have used all pages */
3929 /* Assign the last page to ftrace_pages */
3932 /* These new locations need to be initialized */
3933 ftrace_new_pgs = start_pg;
3936 * We only need to disable interrupts on start up
3937 * because we are modifying code that an interrupt
3938 * may execute, and the modification is not atomic.
3939 * But for modules, nothing runs the code we modify
3940 * until we are finished with it, and there's no
3941 * reason to cause large interrupt latencies while we do it.
3944 local_irq_save(flags);
3945 ftrace_update_code(mod);
3947 local_irq_restore(flags);
3950 mutex_unlock(&ftrace_lock);
3955 #ifdef CONFIG_MODULES
3957 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3959 void ftrace_release_mod(struct module *mod)
3961 struct dyn_ftrace *rec;
3962 struct ftrace_page **last_pg;
3963 struct ftrace_page *pg;
3966 mutex_lock(&ftrace_lock);
3968 if (ftrace_disabled)
3972 * Each module has its own ftrace_pages, remove
3973 * them from the list.
3975 last_pg = &ftrace_pages_start;
3976 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3977 rec = &pg->records[0];
3978 if (within_module_core(rec->ip, mod)) {
3980 * As core pages are first, the first
3981 * page should never be a module page.
3983 if (WARN_ON(pg == ftrace_pages_start))
3986 /* Check if we are deleting the last page */
3987 if (pg == ftrace_pages)
3988 ftrace_pages = next_to_ftrace_page(last_pg);
3990 *last_pg = pg->next;
3991 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3992 free_pages((unsigned long)pg->records, order);
3995 last_pg = &pg->next;
3998 mutex_unlock(&ftrace_lock);
4001 static void ftrace_init_module(struct module *mod,
4002 unsigned long *start, unsigned long *end)
4004 if (ftrace_disabled || start == end)
4006 ftrace_process_locs(mod, start, end);
4009 static int ftrace_module_notify(struct notifier_block *self,
4010 unsigned long val, void *data)
4012 struct module *mod = data;
4015 case MODULE_STATE_COMING:
4016 ftrace_init_module(mod, mod->ftrace_callsites,
4017 mod->ftrace_callsites +
4018 mod->num_ftrace_callsites);
4020 case MODULE_STATE_GOING:
4021 ftrace_release_mod(mod);
4028 static int ftrace_module_notify(struct notifier_block *self,
4029 unsigned long val, void *data)
4033 #endif /* CONFIG_MODULES */
4035 struct notifier_block ftrace_module_nb = {
4036 .notifier_call = ftrace_module_notify,
4037 .priority = INT_MAX, /* Run before anything that can use kprobes */
4040 extern unsigned long __start_mcount_loc[];
4041 extern unsigned long __stop_mcount_loc[];
4043 void __init ftrace_init(void)
4045 unsigned long count, addr, flags;
4048 /* Keep the ftrace pointer to the stub */
4049 addr = (unsigned long)ftrace_stub;
4051 local_irq_save(flags);
4052 ftrace_dyn_arch_init(&addr);
4053 local_irq_restore(flags);
4055 /* ftrace_dyn_arch_init places the return code in addr */
4059 count = __stop_mcount_loc - __start_mcount_loc;
4061 ret = ftrace_dyn_table_alloc(count);
4065 last_ftrace_enabled = ftrace_enabled = 1;
4067 ret = ftrace_process_locs(NULL,
4071 ret = register_module_notifier(&ftrace_module_nb);
4073 pr_warning("Failed to register trace ftrace module notifier\n");
4075 set_ftrace_early_filters();
4079 ftrace_disabled = 1;
4084 static struct ftrace_ops global_ops = {
4085 .func = ftrace_stub,
4086 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4089 static int __init ftrace_nodyn_init(void)
4094 core_initcall(ftrace_nodyn_init);
4096 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4097 static inline void ftrace_startup_enable(int command) { }
4098 /* Keep as macros so we do not need to define the commands */
4099 # define ftrace_startup(ops, command) \
4101 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4104 # define ftrace_shutdown(ops, command) do { } while (0)
4105 # define ftrace_startup_sysctl() do { } while (0)
4106 # define ftrace_shutdown_sysctl() do { } while (0)
4109 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4114 #endif /* CONFIG_DYNAMIC_FTRACE */
4117 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4118 struct ftrace_ops *op, struct pt_regs *regs)
4120 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4124 * Some of the ops may be dynamically allocated,
4125 * they must be freed after a synchronize_sched().
4127 preempt_disable_notrace();
4128 trace_recursion_set(TRACE_CONTROL_BIT);
4129 do_for_each_ftrace_op(op, ftrace_control_list) {
4130 if (!ftrace_function_local_disabled(op) &&
4131 ftrace_ops_test(op, ip))
4132 op->func(ip, parent_ip, op, regs);
4133 } while_for_each_ftrace_op(op);
4134 trace_recursion_clear(TRACE_CONTROL_BIT);
4135 preempt_enable_notrace();
4138 static struct ftrace_ops control_ops = {
4139 .func = ftrace_ops_control_func,
4140 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4144 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4145 struct ftrace_ops *ignored, struct pt_regs *regs)
4147 struct ftrace_ops *op;
4150 if (function_trace_stop)
4153 if (in_interrupt()) {
4155 bit = TRACE_INTERNAL_NMI_BIT;
4158 bit = TRACE_INTERNAL_IRQ_BIT;
4160 bit = TRACE_INTERNAL_SIRQ_BIT;
4162 bit = TRACE_INTERNAL_BIT;
4164 if (unlikely(trace_recursion_test(bit)))
4167 trace_recursion_set(bit);
4170 * Some of the ops may be dynamically allocated,
4171 * they must be freed after a synchronize_sched().
4173 preempt_disable_notrace();
4174 do_for_each_ftrace_op(op, ftrace_ops_list) {
4175 if (ftrace_ops_test(op, ip))
4176 op->func(ip, parent_ip, op, regs);
4177 } while_for_each_ftrace_op(op);
4178 preempt_enable_notrace();
4179 trace_recursion_clear(bit);
4183 * Some archs only support passing ip and parent_ip. Even though
4184 * the list function ignores the op parameter, we do not want any
4185 * C side effects, where a function is called without the caller
4186 * sending a third parameter.
4187 * Archs are to support both the regs and ftrace_ops at the same time.
4188 * If they support ftrace_ops, it is assumed they support regs.
4189 * If call backs want to use regs, they must either check for regs
4190 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4191 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4192 * An architecture can pass partial regs with ftrace_ops and still
4193 * set the ARCH_SUPPORT_FTARCE_OPS.
4195 #if ARCH_SUPPORTS_FTRACE_OPS
4196 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4197 struct ftrace_ops *op, struct pt_regs *regs)
4199 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4202 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4204 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4208 static void clear_ftrace_swapper(void)
4210 struct task_struct *p;
4214 for_each_online_cpu(cpu) {
4216 clear_tsk_trace_trace(p);
4221 static void set_ftrace_swapper(void)
4223 struct task_struct *p;
4227 for_each_online_cpu(cpu) {
4229 set_tsk_trace_trace(p);
4234 static void clear_ftrace_pid(struct pid *pid)
4236 struct task_struct *p;
4239 do_each_pid_task(pid, PIDTYPE_PID, p) {
4240 clear_tsk_trace_trace(p);
4241 } while_each_pid_task(pid, PIDTYPE_PID, p);
4247 static void set_ftrace_pid(struct pid *pid)
4249 struct task_struct *p;
4252 do_each_pid_task(pid, PIDTYPE_PID, p) {
4253 set_tsk_trace_trace(p);
4254 } while_each_pid_task(pid, PIDTYPE_PID, p);
4258 static void clear_ftrace_pid_task(struct pid *pid)
4260 if (pid == ftrace_swapper_pid)
4261 clear_ftrace_swapper();
4263 clear_ftrace_pid(pid);
4266 static void set_ftrace_pid_task(struct pid *pid)
4268 if (pid == ftrace_swapper_pid)
4269 set_ftrace_swapper();
4271 set_ftrace_pid(pid);
4274 static int ftrace_pid_add(int p)
4277 struct ftrace_pid *fpid;
4280 mutex_lock(&ftrace_lock);
4283 pid = ftrace_swapper_pid;
4285 pid = find_get_pid(p);
4292 list_for_each_entry(fpid, &ftrace_pids, list)
4293 if (fpid->pid == pid)
4298 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4302 list_add(&fpid->list, &ftrace_pids);
4305 set_ftrace_pid_task(pid);
4307 ftrace_update_pid_func();
4308 ftrace_startup_enable(0);
4310 mutex_unlock(&ftrace_lock);
4314 if (pid != ftrace_swapper_pid)
4318 mutex_unlock(&ftrace_lock);
4322 static void ftrace_pid_reset(void)
4324 struct ftrace_pid *fpid, *safe;
4326 mutex_lock(&ftrace_lock);
4327 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4328 struct pid *pid = fpid->pid;
4330 clear_ftrace_pid_task(pid);
4332 list_del(&fpid->list);
4336 ftrace_update_pid_func();
4337 ftrace_startup_enable(0);
4339 mutex_unlock(&ftrace_lock);
4342 static void *fpid_start(struct seq_file *m, loff_t *pos)
4344 mutex_lock(&ftrace_lock);
4346 if (list_empty(&ftrace_pids) && (!*pos))
4349 return seq_list_start(&ftrace_pids, *pos);
4352 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4357 return seq_list_next(v, &ftrace_pids, pos);
4360 static void fpid_stop(struct seq_file *m, void *p)
4362 mutex_unlock(&ftrace_lock);
4365 static int fpid_show(struct seq_file *m, void *v)
4367 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4369 if (v == (void *)1) {
4370 seq_printf(m, "no pid\n");
4374 if (fpid->pid == ftrace_swapper_pid)
4375 seq_printf(m, "swapper tasks\n");
4377 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4382 static const struct seq_operations ftrace_pid_sops = {
4383 .start = fpid_start,
4390 ftrace_pid_open(struct inode *inode, struct file *file)
4394 if ((file->f_mode & FMODE_WRITE) &&
4395 (file->f_flags & O_TRUNC))
4398 if (file->f_mode & FMODE_READ)
4399 ret = seq_open(file, &ftrace_pid_sops);
4405 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4406 size_t cnt, loff_t *ppos)
4412 if (cnt >= sizeof(buf))
4415 if (copy_from_user(&buf, ubuf, cnt))
4421 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4422 * to clean the filter quietly.
4424 tmp = strstrip(buf);
4425 if (strlen(tmp) == 0)
4428 ret = kstrtol(tmp, 10, &val);
4432 ret = ftrace_pid_add(val);
4434 return ret ? ret : cnt;
4438 ftrace_pid_release(struct inode *inode, struct file *file)
4440 if (file->f_mode & FMODE_READ)
4441 seq_release(inode, file);
4446 static const struct file_operations ftrace_pid_fops = {
4447 .open = ftrace_pid_open,
4448 .write = ftrace_pid_write,
4450 .llseek = seq_lseek,
4451 .release = ftrace_pid_release,
4454 static __init int ftrace_init_debugfs(void)
4456 struct dentry *d_tracer;
4458 d_tracer = tracing_init_dentry();
4462 ftrace_init_dyn_debugfs(d_tracer);
4464 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4465 NULL, &ftrace_pid_fops);
4467 ftrace_profile_debugfs(d_tracer);
4471 fs_initcall(ftrace_init_debugfs);
4474 * ftrace_kill - kill ftrace
4476 * This function should be used by panic code. It stops ftrace
4477 * but in a not so nice way. If you need to simply kill ftrace
4478 * from a non-atomic section, use ftrace_kill.
4480 void ftrace_kill(void)
4482 ftrace_disabled = 1;
4484 clear_ftrace_function();
4488 * Test if ftrace is dead or not.
4490 int ftrace_is_dead(void)
4492 return ftrace_disabled;
4496 * register_ftrace_function - register a function for profiling
4497 * @ops - ops structure that holds the function for profiling.
4499 * Register a function to be called by all functions in the
4502 * Note: @ops->func and all the functions it calls must be labeled
4503 * with "notrace", otherwise it will go into a
4506 int register_ftrace_function(struct ftrace_ops *ops)
4510 mutex_lock(&ftrace_lock);
4512 ret = __register_ftrace_function(ops);
4514 ret = ftrace_startup(ops, 0);
4516 mutex_unlock(&ftrace_lock);
4520 EXPORT_SYMBOL_GPL(register_ftrace_function);
4523 * unregister_ftrace_function - unregister a function for profiling.
4524 * @ops - ops structure that holds the function to unregister
4526 * Unregister a function that was added to be called by ftrace profiling.
4528 int unregister_ftrace_function(struct ftrace_ops *ops)
4532 mutex_lock(&ftrace_lock);
4533 ret = __unregister_ftrace_function(ops);
4535 ftrace_shutdown(ops, 0);
4536 mutex_unlock(&ftrace_lock);
4540 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4543 ftrace_enable_sysctl(struct ctl_table *table, int write,
4544 void __user *buffer, size_t *lenp,
4549 mutex_lock(&ftrace_lock);
4551 if (unlikely(ftrace_disabled))
4554 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4556 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4559 last_ftrace_enabled = !!ftrace_enabled;
4561 if (ftrace_enabled) {
4563 ftrace_startup_sysctl();
4565 /* we are starting ftrace again */
4566 if (ftrace_ops_list != &ftrace_list_end) {
4567 if (ftrace_ops_list->next == &ftrace_list_end)
4568 ftrace_trace_function = ftrace_ops_list->func;
4570 ftrace_trace_function = ftrace_ops_list_func;
4574 /* stopping ftrace calls (just send to ftrace_stub) */
4575 ftrace_trace_function = ftrace_stub;
4577 ftrace_shutdown_sysctl();
4581 mutex_unlock(&ftrace_lock);
4585 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4587 static int ftrace_graph_active;
4588 static struct notifier_block ftrace_suspend_notifier;
4590 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4595 /* The callbacks that hook a function */
4596 trace_func_graph_ret_t ftrace_graph_return =
4597 (trace_func_graph_ret_t)ftrace_stub;
4598 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4600 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4601 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4605 unsigned long flags;
4606 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4607 struct task_struct *g, *t;
4609 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4610 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4611 * sizeof(struct ftrace_ret_stack),
4613 if (!ret_stack_list[i]) {
4621 read_lock_irqsave(&tasklist_lock, flags);
4622 do_each_thread(g, t) {
4628 if (t->ret_stack == NULL) {
4629 atomic_set(&t->tracing_graph_pause, 0);
4630 atomic_set(&t->trace_overrun, 0);
4631 t->curr_ret_stack = -1;
4632 /* Make sure the tasks see the -1 first: */
4634 t->ret_stack = ret_stack_list[start++];
4636 } while_each_thread(g, t);
4639 read_unlock_irqrestore(&tasklist_lock, flags);
4641 for (i = start; i < end; i++)
4642 kfree(ret_stack_list[i]);
4647 ftrace_graph_probe_sched_switch(void *ignore,
4648 struct task_struct *prev, struct task_struct *next)
4650 unsigned long long timestamp;
4654 * Does the user want to count the time a function was asleep.
4655 * If so, do not update the time stamps.
4657 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4660 timestamp = trace_clock_local();
4662 prev->ftrace_timestamp = timestamp;
4664 /* only process tasks that we timestamped */
4665 if (!next->ftrace_timestamp)
4669 * Update all the counters in next to make up for the
4670 * time next was sleeping.
4672 timestamp -= next->ftrace_timestamp;
4674 for (index = next->curr_ret_stack; index >= 0; index--)
4675 next->ret_stack[index].calltime += timestamp;
4678 /* Allocate a return stack for each task */
4679 static int start_graph_tracing(void)
4681 struct ftrace_ret_stack **ret_stack_list;
4684 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4685 sizeof(struct ftrace_ret_stack *),
4688 if (!ret_stack_list)
4691 /* The cpu_boot init_task->ret_stack will never be freed */
4692 for_each_online_cpu(cpu) {
4693 if (!idle_task(cpu)->ret_stack)
4694 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4698 ret = alloc_retstack_tasklist(ret_stack_list);
4699 } while (ret == -EAGAIN);
4702 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4704 pr_info("ftrace_graph: Couldn't activate tracepoint"
4705 " probe to kernel_sched_switch\n");
4708 kfree(ret_stack_list);
4713 * Hibernation protection.
4714 * The state of the current task is too much unstable during
4715 * suspend/restore to disk. We want to protect against that.
4718 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4722 case PM_HIBERNATION_PREPARE:
4723 pause_graph_tracing();
4726 case PM_POST_HIBERNATION:
4727 unpause_graph_tracing();
4733 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4734 trace_func_graph_ent_t entryfunc)
4738 mutex_lock(&ftrace_lock);
4740 /* we currently allow only one tracer registered at a time */
4741 if (ftrace_graph_active) {
4746 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4747 register_pm_notifier(&ftrace_suspend_notifier);
4749 ftrace_graph_active++;
4750 ret = start_graph_tracing();
4752 ftrace_graph_active--;
4756 ftrace_graph_return = retfunc;
4757 ftrace_graph_entry = entryfunc;
4759 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4762 mutex_unlock(&ftrace_lock);
4766 void unregister_ftrace_graph(void)
4768 mutex_lock(&ftrace_lock);
4770 if (unlikely(!ftrace_graph_active))
4773 ftrace_graph_active--;
4774 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4775 ftrace_graph_entry = ftrace_graph_entry_stub;
4776 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4777 unregister_pm_notifier(&ftrace_suspend_notifier);
4778 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4781 mutex_unlock(&ftrace_lock);
4784 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4787 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4789 atomic_set(&t->tracing_graph_pause, 0);
4790 atomic_set(&t->trace_overrun, 0);
4791 t->ftrace_timestamp = 0;
4792 /* make curr_ret_stack visible before we add the ret_stack */
4794 t->ret_stack = ret_stack;
4798 * Allocate a return stack for the idle task. May be the first
4799 * time through, or it may be done by CPU hotplug online.
4801 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4803 t->curr_ret_stack = -1;
4805 * The idle task has no parent, it either has its own
4806 * stack or no stack at all.
4809 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4811 if (ftrace_graph_active) {
4812 struct ftrace_ret_stack *ret_stack;
4814 ret_stack = per_cpu(idle_ret_stack, cpu);
4816 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4817 * sizeof(struct ftrace_ret_stack),
4821 per_cpu(idle_ret_stack, cpu) = ret_stack;
4823 graph_init_task(t, ret_stack);
4827 /* Allocate a return stack for newly created task */
4828 void ftrace_graph_init_task(struct task_struct *t)
4830 /* Make sure we do not use the parent ret_stack */
4831 t->ret_stack = NULL;
4832 t->curr_ret_stack = -1;
4834 if (ftrace_graph_active) {
4835 struct ftrace_ret_stack *ret_stack;
4837 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4838 * sizeof(struct ftrace_ret_stack),
4842 graph_init_task(t, ret_stack);
4846 void ftrace_graph_exit_task(struct task_struct *t)
4848 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4850 t->ret_stack = NULL;
4851 /* NULL must become visible to IRQs before we free it: */
4857 void ftrace_graph_stop(void)