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 | FTRACE_OPS_FL_STUB,
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 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
165 do_for_each_ftrace_op(op, ftrace_global_list) {
166 op->func(ip, parent_ip, op, regs);
167 } while_for_each_ftrace_op(op);
169 trace_clear_recursion(bit);
172 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
173 struct ftrace_ops *op, struct pt_regs *regs)
175 if (!test_tsk_trace_trace(current))
178 ftrace_pid_function(ip, parent_ip, op, regs);
181 static void set_ftrace_pid_function(ftrace_func_t func)
183 /* do not set ftrace_pid_function to itself! */
184 if (func != ftrace_pid_func)
185 ftrace_pid_function = func;
189 * clear_ftrace_function - reset the ftrace function
191 * This NULLs the ftrace function and in essence stops
192 * tracing. There may be lag
194 void clear_ftrace_function(void)
196 ftrace_trace_function = ftrace_stub;
197 ftrace_pid_function = ftrace_stub;
200 static void control_ops_disable_all(struct ftrace_ops *ops)
204 for_each_possible_cpu(cpu)
205 *per_cpu_ptr(ops->disabled, cpu) = 1;
208 static int control_ops_alloc(struct ftrace_ops *ops)
210 int __percpu *disabled;
212 disabled = alloc_percpu(int);
216 ops->disabled = disabled;
217 control_ops_disable_all(ops);
221 static void control_ops_free(struct ftrace_ops *ops)
223 free_percpu(ops->disabled);
226 static void update_global_ops(void)
231 * If there's only one function registered, then call that
232 * function directly. Otherwise, we need to iterate over the
233 * registered callers.
235 if (ftrace_global_list == &ftrace_list_end ||
236 ftrace_global_list->next == &ftrace_list_end) {
237 func = ftrace_global_list->func;
239 * As we are calling the function directly.
240 * If it does not have recursion protection,
241 * the function_trace_op needs to be updated
244 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
245 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
247 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
249 func = ftrace_global_list_func;
250 /* The list has its own recursion protection. */
251 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
255 /* If we filter on pids, update to use the pid function */
256 if (!list_empty(&ftrace_pids)) {
257 set_ftrace_pid_function(func);
258 func = ftrace_pid_func;
261 global_ops.func = func;
264 static void update_ftrace_function(void)
271 * If we are at the end of the list and this ops is
272 * recursion safe and not dynamic and the arch supports passing ops,
273 * then have the mcount trampoline call the function directly.
275 if (ftrace_ops_list == &ftrace_list_end ||
276 (ftrace_ops_list->next == &ftrace_list_end &&
277 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
278 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
279 !FTRACE_FORCE_LIST_FUNC)) {
280 /* Set the ftrace_ops that the arch callback uses */
281 if (ftrace_ops_list == &global_ops)
282 function_trace_op = ftrace_global_list;
284 function_trace_op = ftrace_ops_list;
285 func = ftrace_ops_list->func;
287 /* Just use the default ftrace_ops */
288 function_trace_op = &ftrace_list_end;
289 func = ftrace_ops_list_func;
292 ftrace_trace_function = func;
295 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
299 * We are entering ops into the list but another
300 * CPU might be walking that list. We need to make sure
301 * the ops->next pointer is valid before another CPU sees
302 * the ops pointer included into the list.
304 rcu_assign_pointer(*list, ops);
307 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
309 struct ftrace_ops **p;
312 * If we are removing the last function, then simply point
313 * to the ftrace_stub.
315 if (*list == ops && ops->next == &ftrace_list_end) {
316 *list = &ftrace_list_end;
320 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
331 static void add_ftrace_list_ops(struct ftrace_ops **list,
332 struct ftrace_ops *main_ops,
333 struct ftrace_ops *ops)
335 int first = *list == &ftrace_list_end;
336 add_ftrace_ops(list, ops);
338 add_ftrace_ops(&ftrace_ops_list, main_ops);
341 static int remove_ftrace_list_ops(struct ftrace_ops **list,
342 struct ftrace_ops *main_ops,
343 struct ftrace_ops *ops)
345 int ret = remove_ftrace_ops(list, ops);
346 if (!ret && *list == &ftrace_list_end)
347 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
351 static int __register_ftrace_function(struct ftrace_ops *ops)
353 if (unlikely(ftrace_disabled))
356 if (FTRACE_WARN_ON(ops == &global_ops))
359 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
362 /* We don't support both control and global flags set. */
363 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
366 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
368 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
369 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
370 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
372 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
373 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
376 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
377 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
380 if (!core_kernel_data((unsigned long)ops))
381 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
383 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
384 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
385 ops->flags |= FTRACE_OPS_FL_ENABLED;
386 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
387 if (control_ops_alloc(ops))
389 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
391 add_ftrace_ops(&ftrace_ops_list, ops);
394 update_ftrace_function();
399 static int __unregister_ftrace_function(struct ftrace_ops *ops)
406 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
409 if (FTRACE_WARN_ON(ops == &global_ops))
412 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
413 ret = remove_ftrace_list_ops(&ftrace_global_list,
416 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
417 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
418 ret = remove_ftrace_list_ops(&ftrace_control_list,
422 * The ftrace_ops is now removed from the list,
423 * so there'll be no new users. We must ensure
424 * all current users are done before we free
428 control_ops_free(ops);
431 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
437 update_ftrace_function();
440 * Dynamic ops may be freed, we must make sure that all
441 * callers are done before leaving this function.
443 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
449 static void ftrace_update_pid_func(void)
451 /* Only do something if we are tracing something */
452 if (ftrace_trace_function == ftrace_stub)
455 update_ftrace_function();
458 #ifdef CONFIG_FUNCTION_PROFILER
459 struct ftrace_profile {
460 struct hlist_node node;
462 unsigned long counter;
463 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
464 unsigned long long time;
465 unsigned long long time_squared;
469 struct ftrace_profile_page {
470 struct ftrace_profile_page *next;
472 struct ftrace_profile records[];
475 struct ftrace_profile_stat {
477 struct hlist_head *hash;
478 struct ftrace_profile_page *pages;
479 struct ftrace_profile_page *start;
480 struct tracer_stat stat;
483 #define PROFILE_RECORDS_SIZE \
484 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
486 #define PROFILES_PER_PAGE \
487 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
489 static int ftrace_profile_bits __read_mostly;
490 static int ftrace_profile_enabled __read_mostly;
492 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
493 static DEFINE_MUTEX(ftrace_profile_lock);
495 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
497 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
500 function_stat_next(void *v, int idx)
502 struct ftrace_profile *rec = v;
503 struct ftrace_profile_page *pg;
505 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
511 if ((void *)rec >= (void *)&pg->records[pg->index]) {
515 rec = &pg->records[0];
523 static void *function_stat_start(struct tracer_stat *trace)
525 struct ftrace_profile_stat *stat =
526 container_of(trace, struct ftrace_profile_stat, stat);
528 if (!stat || !stat->start)
531 return function_stat_next(&stat->start->records[0], 0);
534 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
535 /* function graph compares on total time */
536 static int function_stat_cmp(void *p1, void *p2)
538 struct ftrace_profile *a = p1;
539 struct ftrace_profile *b = p2;
541 if (a->time < b->time)
543 if (a->time > b->time)
549 /* not function graph compares against hits */
550 static int function_stat_cmp(void *p1, void *p2)
552 struct ftrace_profile *a = p1;
553 struct ftrace_profile *b = p2;
555 if (a->counter < b->counter)
557 if (a->counter > b->counter)
564 static int function_stat_headers(struct seq_file *m)
566 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
567 seq_printf(m, " Function "
570 "--- ---- --- ---\n");
572 seq_printf(m, " Function Hit\n"
578 static int function_stat_show(struct seq_file *m, void *v)
580 struct ftrace_profile *rec = v;
581 char str[KSYM_SYMBOL_LEN];
583 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
584 static struct trace_seq s;
585 unsigned long long avg;
586 unsigned long long stddev;
588 mutex_lock(&ftrace_profile_lock);
590 /* we raced with function_profile_reset() */
591 if (unlikely(rec->counter == 0)) {
596 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
597 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
599 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
602 do_div(avg, rec->counter);
604 /* Sample standard deviation (s^2) */
605 if (rec->counter <= 1)
608 stddev = rec->time_squared - rec->counter * avg * avg;
610 * Divide only 1000 for ns^2 -> us^2 conversion.
611 * trace_print_graph_duration will divide 1000 again.
613 do_div(stddev, (rec->counter - 1) * 1000);
617 trace_print_graph_duration(rec->time, &s);
618 trace_seq_puts(&s, " ");
619 trace_print_graph_duration(avg, &s);
620 trace_seq_puts(&s, " ");
621 trace_print_graph_duration(stddev, &s);
622 trace_print_seq(m, &s);
626 mutex_unlock(&ftrace_profile_lock);
631 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
633 struct ftrace_profile_page *pg;
635 pg = stat->pages = stat->start;
638 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
643 memset(stat->hash, 0,
644 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
647 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
649 struct ftrace_profile_page *pg;
654 /* If we already allocated, do nothing */
658 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
662 #ifdef CONFIG_DYNAMIC_FTRACE
663 functions = ftrace_update_tot_cnt;
666 * We do not know the number of functions that exist because
667 * dynamic tracing is what counts them. With past experience
668 * we have around 20K functions. That should be more than enough.
669 * It is highly unlikely we will execute every function in
675 pg = stat->start = stat->pages;
677 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
679 for (i = 0; i < pages; i++) {
680 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
691 unsigned long tmp = (unsigned long)pg;
703 static int ftrace_profile_init_cpu(int cpu)
705 struct ftrace_profile_stat *stat;
708 stat = &per_cpu(ftrace_profile_stats, cpu);
711 /* If the profile is already created, simply reset it */
712 ftrace_profile_reset(stat);
717 * We are profiling all functions, but usually only a few thousand
718 * functions are hit. We'll make a hash of 1024 items.
720 size = FTRACE_PROFILE_HASH_SIZE;
722 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
727 if (!ftrace_profile_bits) {
730 for (; size; size >>= 1)
731 ftrace_profile_bits++;
734 /* Preallocate the function profiling pages */
735 if (ftrace_profile_pages_init(stat) < 0) {
744 static int ftrace_profile_init(void)
749 for_each_online_cpu(cpu) {
750 ret = ftrace_profile_init_cpu(cpu);
758 /* interrupts must be disabled */
759 static struct ftrace_profile *
760 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
762 struct ftrace_profile *rec;
763 struct hlist_head *hhd;
766 key = hash_long(ip, ftrace_profile_bits);
767 hhd = &stat->hash[key];
769 if (hlist_empty(hhd))
772 hlist_for_each_entry_rcu(rec, hhd, node) {
780 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
781 struct ftrace_profile *rec)
785 key = hash_long(rec->ip, ftrace_profile_bits);
786 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
790 * The memory is already allocated, this simply finds a new record to use.
792 static struct ftrace_profile *
793 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
795 struct ftrace_profile *rec = NULL;
797 /* prevent recursion (from NMIs) */
798 if (atomic_inc_return(&stat->disabled) != 1)
802 * Try to find the function again since an NMI
803 * could have added it
805 rec = ftrace_find_profiled_func(stat, ip);
809 if (stat->pages->index == PROFILES_PER_PAGE) {
810 if (!stat->pages->next)
812 stat->pages = stat->pages->next;
815 rec = &stat->pages->records[stat->pages->index++];
817 ftrace_add_profile(stat, rec);
820 atomic_dec(&stat->disabled);
826 function_profile_call(unsigned long ip, unsigned long parent_ip,
827 struct ftrace_ops *ops, struct pt_regs *regs)
829 struct ftrace_profile_stat *stat;
830 struct ftrace_profile *rec;
833 if (!ftrace_profile_enabled)
836 local_irq_save(flags);
838 stat = &__get_cpu_var(ftrace_profile_stats);
839 if (!stat->hash || !ftrace_profile_enabled)
842 rec = ftrace_find_profiled_func(stat, ip);
844 rec = ftrace_profile_alloc(stat, ip);
851 local_irq_restore(flags);
854 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
855 static int profile_graph_entry(struct ftrace_graph_ent *trace)
857 function_profile_call(trace->func, 0, NULL, NULL);
861 static void profile_graph_return(struct ftrace_graph_ret *trace)
863 struct ftrace_profile_stat *stat;
864 unsigned long long calltime;
865 struct ftrace_profile *rec;
868 local_irq_save(flags);
869 stat = &__get_cpu_var(ftrace_profile_stats);
870 if (!stat->hash || !ftrace_profile_enabled)
873 /* If the calltime was zero'd ignore it */
874 if (!trace->calltime)
877 calltime = trace->rettime - trace->calltime;
879 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
882 index = trace->depth;
884 /* Append this call time to the parent time to subtract */
886 current->ret_stack[index - 1].subtime += calltime;
888 if (current->ret_stack[index].subtime < calltime)
889 calltime -= current->ret_stack[index].subtime;
894 rec = ftrace_find_profiled_func(stat, trace->func);
896 rec->time += calltime;
897 rec->time_squared += calltime * calltime;
901 local_irq_restore(flags);
904 static int register_ftrace_profiler(void)
906 return register_ftrace_graph(&profile_graph_return,
907 &profile_graph_entry);
910 static void unregister_ftrace_profiler(void)
912 unregister_ftrace_graph();
915 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
916 .func = function_profile_call,
917 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
920 static int register_ftrace_profiler(void)
922 return register_ftrace_function(&ftrace_profile_ops);
925 static void unregister_ftrace_profiler(void)
927 unregister_ftrace_function(&ftrace_profile_ops);
929 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
932 ftrace_profile_write(struct file *filp, const char __user *ubuf,
933 size_t cnt, loff_t *ppos)
938 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
944 mutex_lock(&ftrace_profile_lock);
945 if (ftrace_profile_enabled ^ val) {
947 ret = ftrace_profile_init();
953 ret = register_ftrace_profiler();
958 ftrace_profile_enabled = 1;
960 ftrace_profile_enabled = 0;
962 * unregister_ftrace_profiler calls stop_machine
963 * so this acts like an synchronize_sched.
965 unregister_ftrace_profiler();
969 mutex_unlock(&ftrace_profile_lock);
977 ftrace_profile_read(struct file *filp, char __user *ubuf,
978 size_t cnt, loff_t *ppos)
980 char buf[64]; /* big enough to hold a number */
983 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
984 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
987 static const struct file_operations ftrace_profile_fops = {
988 .open = tracing_open_generic,
989 .read = ftrace_profile_read,
990 .write = ftrace_profile_write,
991 .llseek = default_llseek,
994 /* used to initialize the real stat files */
995 static struct tracer_stat function_stats __initdata = {
997 .stat_start = function_stat_start,
998 .stat_next = function_stat_next,
999 .stat_cmp = function_stat_cmp,
1000 .stat_headers = function_stat_headers,
1001 .stat_show = function_stat_show
1004 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1006 struct ftrace_profile_stat *stat;
1007 struct dentry *entry;
1012 for_each_possible_cpu(cpu) {
1013 stat = &per_cpu(ftrace_profile_stats, cpu);
1015 /* allocate enough for function name + cpu number */
1016 name = kmalloc(32, GFP_KERNEL);
1019 * The files created are permanent, if something happens
1020 * we still do not free memory.
1023 "Could not allocate stat file for cpu %d\n",
1027 stat->stat = function_stats;
1028 snprintf(name, 32, "function%d", cpu);
1029 stat->stat.name = name;
1030 ret = register_stat_tracer(&stat->stat);
1033 "Could not register function stat for cpu %d\n",
1040 entry = debugfs_create_file("function_profile_enabled", 0644,
1041 d_tracer, NULL, &ftrace_profile_fops);
1043 pr_warning("Could not create debugfs "
1044 "'function_profile_enabled' entry\n");
1047 #else /* CONFIG_FUNCTION_PROFILER */
1048 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1051 #endif /* CONFIG_FUNCTION_PROFILER */
1053 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1055 #ifdef CONFIG_DYNAMIC_FTRACE
1057 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1058 # error Dynamic ftrace depends on MCOUNT_RECORD
1061 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1063 struct ftrace_func_probe {
1064 struct hlist_node node;
1065 struct ftrace_probe_ops *ops;
1066 unsigned long flags;
1069 struct rcu_head rcu;
1072 struct ftrace_func_entry {
1073 struct hlist_node hlist;
1077 struct ftrace_hash {
1078 unsigned long size_bits;
1079 struct hlist_head *buckets;
1080 unsigned long count;
1081 struct rcu_head rcu;
1085 * We make these constant because no one should touch them,
1086 * but they are used as the default "empty hash", to avoid allocating
1087 * it all the time. These are in a read only section such that if
1088 * anyone does try to modify it, it will cause an exception.
1090 static const struct hlist_head empty_buckets[1];
1091 static const struct ftrace_hash empty_hash = {
1092 .buckets = (struct hlist_head *)empty_buckets,
1094 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1096 static struct ftrace_ops global_ops = {
1097 .func = ftrace_stub,
1098 .notrace_hash = EMPTY_HASH,
1099 .filter_hash = EMPTY_HASH,
1100 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
1103 static DEFINE_MUTEX(ftrace_regex_lock);
1105 struct ftrace_page {
1106 struct ftrace_page *next;
1107 struct dyn_ftrace *records;
1112 static struct ftrace_page *ftrace_new_pgs;
1114 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1115 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1117 /* estimate from running different kernels */
1118 #define NR_TO_INIT 10000
1120 static struct ftrace_page *ftrace_pages_start;
1121 static struct ftrace_page *ftrace_pages;
1123 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1125 return !hash || !hash->count;
1128 static struct ftrace_func_entry *
1129 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1132 struct ftrace_func_entry *entry;
1133 struct hlist_head *hhd;
1135 if (ftrace_hash_empty(hash))
1138 if (hash->size_bits > 0)
1139 key = hash_long(ip, hash->size_bits);
1143 hhd = &hash->buckets[key];
1145 hlist_for_each_entry_rcu(entry, hhd, hlist) {
1146 if (entry->ip == ip)
1152 static void __add_hash_entry(struct ftrace_hash *hash,
1153 struct ftrace_func_entry *entry)
1155 struct hlist_head *hhd;
1158 if (hash->size_bits)
1159 key = hash_long(entry->ip, hash->size_bits);
1163 hhd = &hash->buckets[key];
1164 hlist_add_head(&entry->hlist, hhd);
1168 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1170 struct ftrace_func_entry *entry;
1172 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1177 __add_hash_entry(hash, entry);
1183 free_hash_entry(struct ftrace_hash *hash,
1184 struct ftrace_func_entry *entry)
1186 hlist_del(&entry->hlist);
1192 remove_hash_entry(struct ftrace_hash *hash,
1193 struct ftrace_func_entry *entry)
1195 hlist_del(&entry->hlist);
1199 static void ftrace_hash_clear(struct ftrace_hash *hash)
1201 struct hlist_head *hhd;
1202 struct hlist_node *tn;
1203 struct ftrace_func_entry *entry;
1204 int size = 1 << hash->size_bits;
1210 for (i = 0; i < size; i++) {
1211 hhd = &hash->buckets[i];
1212 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1213 free_hash_entry(hash, entry);
1215 FTRACE_WARN_ON(hash->count);
1218 static void free_ftrace_hash(struct ftrace_hash *hash)
1220 if (!hash || hash == EMPTY_HASH)
1222 ftrace_hash_clear(hash);
1223 kfree(hash->buckets);
1227 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1229 struct ftrace_hash *hash;
1231 hash = container_of(rcu, struct ftrace_hash, rcu);
1232 free_ftrace_hash(hash);
1235 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1237 if (!hash || hash == EMPTY_HASH)
1239 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1242 void ftrace_free_filter(struct ftrace_ops *ops)
1244 free_ftrace_hash(ops->filter_hash);
1245 free_ftrace_hash(ops->notrace_hash);
1248 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1250 struct ftrace_hash *hash;
1253 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1257 size = 1 << size_bits;
1258 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1260 if (!hash->buckets) {
1265 hash->size_bits = size_bits;
1270 static struct ftrace_hash *
1271 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1273 struct ftrace_func_entry *entry;
1274 struct ftrace_hash *new_hash;
1279 new_hash = alloc_ftrace_hash(size_bits);
1284 if (ftrace_hash_empty(hash))
1287 size = 1 << hash->size_bits;
1288 for (i = 0; i < size; i++) {
1289 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1290 ret = add_hash_entry(new_hash, entry->ip);
1296 FTRACE_WARN_ON(new_hash->count != hash->count);
1301 free_ftrace_hash(new_hash);
1306 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1308 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1311 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1312 struct ftrace_hash **dst, struct ftrace_hash *src)
1314 struct ftrace_func_entry *entry;
1315 struct hlist_node *tn;
1316 struct hlist_head *hhd;
1317 struct ftrace_hash *old_hash;
1318 struct ftrace_hash *new_hash;
1320 int size = src->count;
1326 * Remove the current set, update the hash and add
1329 ftrace_hash_rec_disable(ops, enable);
1332 * If the new source is empty, just free dst and assign it
1336 free_ftrace_hash_rcu(*dst);
1337 rcu_assign_pointer(*dst, EMPTY_HASH);
1338 /* still need to update the function records */
1344 * Make the hash size about 1/2 the # found
1346 for (size /= 2; size; size >>= 1)
1349 /* Don't allocate too much */
1350 if (bits > FTRACE_HASH_MAX_BITS)
1351 bits = FTRACE_HASH_MAX_BITS;
1354 new_hash = alloc_ftrace_hash(bits);
1358 size = 1 << src->size_bits;
1359 for (i = 0; i < size; i++) {
1360 hhd = &src->buckets[i];
1361 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1363 key = hash_long(entry->ip, bits);
1366 remove_hash_entry(src, entry);
1367 __add_hash_entry(new_hash, entry);
1372 rcu_assign_pointer(*dst, new_hash);
1373 free_ftrace_hash_rcu(old_hash);
1378 * Enable regardless of ret:
1379 * On success, we enable the new hash.
1380 * On failure, we re-enable the original hash.
1382 ftrace_hash_rec_enable(ops, enable);
1388 * Test the hashes for this ops to see if we want to call
1389 * the ops->func or not.
1391 * It's a match if the ip is in the ops->filter_hash or
1392 * the filter_hash does not exist or is empty,
1394 * the ip is not in the ops->notrace_hash.
1396 * This needs to be called with preemption disabled as
1397 * the hashes are freed with call_rcu_sched().
1400 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1402 struct ftrace_hash *filter_hash;
1403 struct ftrace_hash *notrace_hash;
1406 filter_hash = rcu_dereference_raw(ops->filter_hash);
1407 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1409 if ((ftrace_hash_empty(filter_hash) ||
1410 ftrace_lookup_ip(filter_hash, ip)) &&
1411 (ftrace_hash_empty(notrace_hash) ||
1412 !ftrace_lookup_ip(notrace_hash, ip)))
1421 * This is a double for. Do not use 'break' to break out of the loop,
1422 * you must use a goto.
1424 #define do_for_each_ftrace_rec(pg, rec) \
1425 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1427 for (_____i = 0; _____i < pg->index; _____i++) { \
1428 rec = &pg->records[_____i];
1430 #define while_for_each_ftrace_rec() \
1435 static int ftrace_cmp_recs(const void *a, const void *b)
1437 const struct dyn_ftrace *key = a;
1438 const struct dyn_ftrace *rec = b;
1440 if (key->flags < rec->ip)
1442 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1447 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1449 struct ftrace_page *pg;
1450 struct dyn_ftrace *rec;
1451 struct dyn_ftrace key;
1454 key.flags = end; /* overload flags, as it is unsigned long */
1456 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1457 if (end < pg->records[0].ip ||
1458 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1460 rec = bsearch(&key, pg->records, pg->index,
1461 sizeof(struct dyn_ftrace),
1471 * ftrace_location - return true if the ip giving is a traced location
1472 * @ip: the instruction pointer to check
1474 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1475 * That is, the instruction that is either a NOP or call to
1476 * the function tracer. It checks the ftrace internal tables to
1477 * determine if the address belongs or not.
1479 unsigned long ftrace_location(unsigned long ip)
1481 return ftrace_location_range(ip, ip);
1485 * ftrace_text_reserved - return true if range contains an ftrace location
1486 * @start: start of range to search
1487 * @end: end of range to search (inclusive). @end points to the last byte to check.
1489 * Returns 1 if @start and @end contains a ftrace location.
1490 * That is, the instruction that is either a NOP or call to
1491 * the function tracer. It checks the ftrace internal tables to
1492 * determine if the address belongs or not.
1494 int ftrace_text_reserved(void *start, void *end)
1498 ret = ftrace_location_range((unsigned long)start,
1499 (unsigned long)end);
1504 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1508 struct ftrace_hash *hash;
1509 struct ftrace_hash *other_hash;
1510 struct ftrace_page *pg;
1511 struct dyn_ftrace *rec;
1515 /* Only update if the ops has been registered */
1516 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1520 * In the filter_hash case:
1521 * If the count is zero, we update all records.
1522 * Otherwise we just update the items in the hash.
1524 * In the notrace_hash case:
1525 * We enable the update in the hash.
1526 * As disabling notrace means enabling the tracing,
1527 * and enabling notrace means disabling, the inc variable
1531 hash = ops->filter_hash;
1532 other_hash = ops->notrace_hash;
1533 if (ftrace_hash_empty(hash))
1537 hash = ops->notrace_hash;
1538 other_hash = ops->filter_hash;
1540 * If the notrace hash has no items,
1541 * then there's nothing to do.
1543 if (ftrace_hash_empty(hash))
1547 do_for_each_ftrace_rec(pg, rec) {
1548 int in_other_hash = 0;
1554 * Only the filter_hash affects all records.
1555 * Update if the record is not in the notrace hash.
1557 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1560 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1561 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1566 if (filter_hash && in_hash && !in_other_hash)
1568 else if (!filter_hash && in_hash &&
1569 (in_other_hash || ftrace_hash_empty(other_hash)))
1577 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1580 * If any ops wants regs saved for this function
1581 * then all ops will get saved regs.
1583 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1584 rec->flags |= FTRACE_FL_REGS;
1586 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1591 /* Shortcut, if we handled all records, we are done. */
1592 if (!all && count == hash->count)
1594 } while_for_each_ftrace_rec();
1597 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1600 __ftrace_hash_rec_update(ops, filter_hash, 0);
1603 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1606 __ftrace_hash_rec_update(ops, filter_hash, 1);
1609 static void print_ip_ins(const char *fmt, unsigned char *p)
1613 printk(KERN_CONT "%s", fmt);
1615 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1616 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1620 * ftrace_bug - report and shutdown function tracer
1621 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1622 * @ip: The address that failed
1624 * The arch code that enables or disables the function tracing
1625 * can call ftrace_bug() when it has detected a problem in
1626 * modifying the code. @failed should be one of either:
1627 * EFAULT - if the problem happens on reading the @ip address
1628 * EINVAL - if what is read at @ip is not what was expected
1629 * EPERM - if the problem happens on writting to the @ip address
1631 void ftrace_bug(int failed, unsigned long ip)
1635 FTRACE_WARN_ON_ONCE(1);
1636 pr_info("ftrace faulted on modifying ");
1640 FTRACE_WARN_ON_ONCE(1);
1641 pr_info("ftrace failed to modify ");
1643 print_ip_ins(" actual: ", (unsigned char *)ip);
1644 printk(KERN_CONT "\n");
1647 FTRACE_WARN_ON_ONCE(1);
1648 pr_info("ftrace faulted on writing ");
1652 FTRACE_WARN_ON_ONCE(1);
1653 pr_info("ftrace faulted on unknown error ");
1658 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1660 unsigned long flag = 0UL;
1663 * If we are updating calls:
1665 * If the record has a ref count, then we need to enable it
1666 * because someone is using it.
1668 * Otherwise we make sure its disabled.
1670 * If we are disabling calls, then disable all records that
1673 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1674 flag = FTRACE_FL_ENABLED;
1677 * If enabling and the REGS flag does not match the REGS_EN, then
1678 * do not ignore this record. Set flags to fail the compare against
1682 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1683 flag |= FTRACE_FL_REGS;
1685 /* If the state of this record hasn't changed, then do nothing */
1686 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1687 return FTRACE_UPDATE_IGNORE;
1690 /* Save off if rec is being enabled (for return value) */
1691 flag ^= rec->flags & FTRACE_FL_ENABLED;
1694 rec->flags |= FTRACE_FL_ENABLED;
1695 if (flag & FTRACE_FL_REGS) {
1696 if (rec->flags & FTRACE_FL_REGS)
1697 rec->flags |= FTRACE_FL_REGS_EN;
1699 rec->flags &= ~FTRACE_FL_REGS_EN;
1704 * If this record is being updated from a nop, then
1705 * return UPDATE_MAKE_CALL.
1706 * Otherwise, if the EN flag is set, then return
1707 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1708 * from the non-save regs, to a save regs function.
1710 * return UPDATE_MODIFY_CALL to tell the caller to convert
1711 * from the save regs, to a non-save regs function.
1713 if (flag & FTRACE_FL_ENABLED)
1714 return FTRACE_UPDATE_MAKE_CALL;
1715 else if (rec->flags & FTRACE_FL_REGS_EN)
1716 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1718 return FTRACE_UPDATE_MODIFY_CALL;
1722 /* If there's no more users, clear all flags */
1723 if (!(rec->flags & ~FTRACE_FL_MASK))
1726 /* Just disable the record (keep REGS state) */
1727 rec->flags &= ~FTRACE_FL_ENABLED;
1730 return FTRACE_UPDATE_MAKE_NOP;
1734 * ftrace_update_record, set a record that now is tracing or not
1735 * @rec: the record to update
1736 * @enable: set to 1 if the record is tracing, zero to force disable
1738 * The records that represent all functions that can be traced need
1739 * to be updated when tracing has been enabled.
1741 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1743 return ftrace_check_record(rec, enable, 1);
1747 * ftrace_test_record, check if the record has been enabled or not
1748 * @rec: the record to test
1749 * @enable: set to 1 to check if enabled, 0 if it is disabled
1751 * The arch code may need to test if a record is already set to
1752 * tracing to determine how to modify the function code that it
1755 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1757 return ftrace_check_record(rec, enable, 0);
1761 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1763 unsigned long ftrace_old_addr;
1764 unsigned long ftrace_addr;
1767 ret = ftrace_update_record(rec, enable);
1769 if (rec->flags & FTRACE_FL_REGS)
1770 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1772 ftrace_addr = (unsigned long)FTRACE_ADDR;
1775 case FTRACE_UPDATE_IGNORE:
1778 case FTRACE_UPDATE_MAKE_CALL:
1779 return ftrace_make_call(rec, ftrace_addr);
1781 case FTRACE_UPDATE_MAKE_NOP:
1782 return ftrace_make_nop(NULL, rec, ftrace_addr);
1784 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1785 case FTRACE_UPDATE_MODIFY_CALL:
1786 if (rec->flags & FTRACE_FL_REGS)
1787 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1789 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1791 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1794 return -1; /* unknow ftrace bug */
1797 void __weak ftrace_replace_code(int enable)
1799 struct dyn_ftrace *rec;
1800 struct ftrace_page *pg;
1803 if (unlikely(ftrace_disabled))
1806 do_for_each_ftrace_rec(pg, rec) {
1807 failed = __ftrace_replace_code(rec, enable);
1809 ftrace_bug(failed, rec->ip);
1810 /* Stop processing */
1813 } while_for_each_ftrace_rec();
1816 struct ftrace_rec_iter {
1817 struct ftrace_page *pg;
1822 * ftrace_rec_iter_start, start up iterating over traced functions
1824 * Returns an iterator handle that is used to iterate over all
1825 * the records that represent address locations where functions
1828 * May return NULL if no records are available.
1830 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1833 * We only use a single iterator.
1834 * Protected by the ftrace_lock mutex.
1836 static struct ftrace_rec_iter ftrace_rec_iter;
1837 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1839 iter->pg = ftrace_pages_start;
1842 /* Could have empty pages */
1843 while (iter->pg && !iter->pg->index)
1844 iter->pg = iter->pg->next;
1853 * ftrace_rec_iter_next, get the next record to process.
1854 * @iter: The handle to the iterator.
1856 * Returns the next iterator after the given iterator @iter.
1858 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1862 if (iter->index >= iter->pg->index) {
1863 iter->pg = iter->pg->next;
1866 /* Could have empty pages */
1867 while (iter->pg && !iter->pg->index)
1868 iter->pg = iter->pg->next;
1878 * ftrace_rec_iter_record, get the record at the iterator location
1879 * @iter: The current iterator location
1881 * Returns the record that the current @iter is at.
1883 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1885 return &iter->pg->records[iter->index];
1889 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1896 if (unlikely(ftrace_disabled))
1899 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1901 ftrace_bug(ret, ip);
1908 * archs can override this function if they must do something
1909 * before the modifying code is performed.
1911 int __weak ftrace_arch_code_modify_prepare(void)
1917 * archs can override this function if they must do something
1918 * after the modifying code is performed.
1920 int __weak ftrace_arch_code_modify_post_process(void)
1925 void ftrace_modify_all_code(int command)
1927 if (command & FTRACE_UPDATE_CALLS)
1928 ftrace_replace_code(1);
1929 else if (command & FTRACE_DISABLE_CALLS)
1930 ftrace_replace_code(0);
1932 if (command & FTRACE_UPDATE_TRACE_FUNC)
1933 ftrace_update_ftrace_func(ftrace_trace_function);
1935 if (command & FTRACE_START_FUNC_RET)
1936 ftrace_enable_ftrace_graph_caller();
1937 else if (command & FTRACE_STOP_FUNC_RET)
1938 ftrace_disable_ftrace_graph_caller();
1941 static int __ftrace_modify_code(void *data)
1943 int *command = data;
1945 ftrace_modify_all_code(*command);
1951 * ftrace_run_stop_machine, go back to the stop machine method
1952 * @command: The command to tell ftrace what to do
1954 * If an arch needs to fall back to the stop machine method, the
1955 * it can call this function.
1957 void ftrace_run_stop_machine(int command)
1959 stop_machine(__ftrace_modify_code, &command, NULL);
1963 * arch_ftrace_update_code, modify the code to trace or not trace
1964 * @command: The command that needs to be done
1966 * Archs can override this function if it does not need to
1967 * run stop_machine() to modify code.
1969 void __weak arch_ftrace_update_code(int command)
1971 ftrace_run_stop_machine(command);
1974 static void ftrace_run_update_code(int command)
1978 ret = ftrace_arch_code_modify_prepare();
1979 FTRACE_WARN_ON(ret);
1983 * Do not call function tracer while we update the code.
1984 * We are in stop machine.
1986 function_trace_stop++;
1989 * By default we use stop_machine() to modify the code.
1990 * But archs can do what ever they want as long as it
1991 * is safe. The stop_machine() is the safest, but also
1992 * produces the most overhead.
1994 arch_ftrace_update_code(command);
1996 function_trace_stop--;
1998 ret = ftrace_arch_code_modify_post_process();
1999 FTRACE_WARN_ON(ret);
2002 static ftrace_func_t saved_ftrace_func;
2003 static int ftrace_start_up;
2004 static int global_start_up;
2006 static void ftrace_startup_enable(int command)
2008 if (saved_ftrace_func != ftrace_trace_function) {
2009 saved_ftrace_func = ftrace_trace_function;
2010 command |= FTRACE_UPDATE_TRACE_FUNC;
2013 if (!command || !ftrace_enabled)
2016 ftrace_run_update_code(command);
2019 static int ftrace_startup(struct ftrace_ops *ops, int command)
2021 bool hash_enable = true;
2023 if (unlikely(ftrace_disabled))
2027 command |= FTRACE_UPDATE_CALLS;
2029 /* ops marked global share the filter hashes */
2030 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2032 /* Don't update hash if global is already set */
2033 if (global_start_up)
2034 hash_enable = false;
2038 ops->flags |= FTRACE_OPS_FL_ENABLED;
2040 ftrace_hash_rec_enable(ops, 1);
2042 ftrace_startup_enable(command);
2047 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
2049 bool hash_disable = true;
2051 if (unlikely(ftrace_disabled))
2056 * Just warn in case of unbalance, no need to kill ftrace, it's not
2057 * critical but the ftrace_call callers may be never nopped again after
2058 * further ftrace uses.
2060 WARN_ON_ONCE(ftrace_start_up < 0);
2062 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2065 WARN_ON_ONCE(global_start_up < 0);
2066 /* Don't update hash if global still has users */
2067 if (global_start_up) {
2068 WARN_ON_ONCE(!ftrace_start_up);
2069 hash_disable = false;
2074 ftrace_hash_rec_disable(ops, 1);
2076 if (ops != &global_ops || !global_start_up)
2077 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2079 command |= FTRACE_UPDATE_CALLS;
2081 if (saved_ftrace_func != ftrace_trace_function) {
2082 saved_ftrace_func = ftrace_trace_function;
2083 command |= FTRACE_UPDATE_TRACE_FUNC;
2086 if (!command || !ftrace_enabled)
2089 ftrace_run_update_code(command);
2092 static void ftrace_startup_sysctl(void)
2094 if (unlikely(ftrace_disabled))
2097 /* Force update next time */
2098 saved_ftrace_func = NULL;
2099 /* ftrace_start_up is true if we want ftrace running */
2100 if (ftrace_start_up)
2101 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2104 static void ftrace_shutdown_sysctl(void)
2106 if (unlikely(ftrace_disabled))
2109 /* ftrace_start_up is true if ftrace is running */
2110 if (ftrace_start_up)
2111 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2114 static cycle_t ftrace_update_time;
2115 static unsigned long ftrace_update_cnt;
2116 unsigned long ftrace_update_tot_cnt;
2118 static int ops_traces_mod(struct ftrace_ops *ops)
2120 struct ftrace_hash *hash;
2122 hash = ops->filter_hash;
2123 return ftrace_hash_empty(hash);
2126 static int ftrace_update_code(struct module *mod)
2128 struct ftrace_page *pg;
2129 struct dyn_ftrace *p;
2130 cycle_t start, stop;
2131 unsigned long ref = 0;
2135 * When adding a module, we need to check if tracers are
2136 * currently enabled and if they are set to trace all functions.
2137 * If they are, we need to enable the module functions as well
2138 * as update the reference counts for those function records.
2141 struct ftrace_ops *ops;
2143 for (ops = ftrace_ops_list;
2144 ops != &ftrace_list_end; ops = ops->next) {
2145 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2146 ops_traces_mod(ops))
2151 start = ftrace_now(raw_smp_processor_id());
2152 ftrace_update_cnt = 0;
2154 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2156 for (i = 0; i < pg->index; i++) {
2157 /* If something went wrong, bail without enabling anything */
2158 if (unlikely(ftrace_disabled))
2161 p = &pg->records[i];
2165 * Do the initial record conversion from mcount jump
2166 * to the NOP instructions.
2168 if (!ftrace_code_disable(mod, p))
2171 ftrace_update_cnt++;
2174 * If the tracing is enabled, go ahead and enable the record.
2176 * The reason not to enable the record immediatelly is the
2177 * inherent check of ftrace_make_nop/ftrace_make_call for
2178 * correct previous instructions. Making first the NOP
2179 * conversion puts the module to the correct state, thus
2180 * passing the ftrace_make_call check.
2182 if (ftrace_start_up && ref) {
2183 int failed = __ftrace_replace_code(p, 1);
2185 ftrace_bug(failed, p->ip);
2190 ftrace_new_pgs = NULL;
2192 stop = ftrace_now(raw_smp_processor_id());
2193 ftrace_update_time = stop - start;
2194 ftrace_update_tot_cnt += ftrace_update_cnt;
2199 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2204 if (WARN_ON(!count))
2207 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2210 * We want to fill as much as possible. No more than a page
2213 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2217 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2220 /* if we can't allocate this size, try something smaller */
2227 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2236 static struct ftrace_page *
2237 ftrace_allocate_pages(unsigned long num_to_init)
2239 struct ftrace_page *start_pg;
2240 struct ftrace_page *pg;
2247 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2252 * Try to allocate as much as possible in one continues
2253 * location that fills in all of the space. We want to
2254 * waste as little space as possible.
2257 cnt = ftrace_allocate_records(pg, num_to_init);
2265 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2276 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2277 free_pages((unsigned long)pg->records, order);
2278 start_pg = pg->next;
2282 pr_info("ftrace: FAILED to allocate memory for functions\n");
2286 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2291 pr_info("ftrace: No functions to be traced?\n");
2295 cnt = num_to_init / ENTRIES_PER_PAGE;
2296 pr_info("ftrace: allocating %ld entries in %d pages\n",
2297 num_to_init, cnt + 1);
2302 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2304 struct ftrace_iterator {
2307 struct ftrace_page *pg;
2308 struct dyn_ftrace *func;
2309 struct ftrace_func_probe *probe;
2310 struct trace_parser parser;
2311 struct ftrace_hash *hash;
2312 struct ftrace_ops *ops;
2319 t_hash_next(struct seq_file *m, loff_t *pos)
2321 struct ftrace_iterator *iter = m->private;
2322 struct hlist_node *hnd = NULL;
2323 struct hlist_head *hhd;
2329 hnd = &iter->probe->node;
2331 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2334 hhd = &ftrace_func_hash[iter->hidx];
2336 if (hlist_empty(hhd)) {
2352 if (WARN_ON_ONCE(!hnd))
2355 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2360 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2362 struct ftrace_iterator *iter = m->private;
2366 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2369 if (iter->func_pos > *pos)
2373 for (l = 0; l <= (*pos - iter->func_pos); ) {
2374 p = t_hash_next(m, &l);
2381 /* Only set this if we have an item */
2382 iter->flags |= FTRACE_ITER_HASH;
2388 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2390 struct ftrace_func_probe *rec;
2393 if (WARN_ON_ONCE(!rec))
2396 if (rec->ops->print)
2397 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2399 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2402 seq_printf(m, ":%p", rec->data);
2409 t_next(struct seq_file *m, void *v, loff_t *pos)
2411 struct ftrace_iterator *iter = m->private;
2412 struct ftrace_ops *ops = iter->ops;
2413 struct dyn_ftrace *rec = NULL;
2415 if (unlikely(ftrace_disabled))
2418 if (iter->flags & FTRACE_ITER_HASH)
2419 return t_hash_next(m, pos);
2422 iter->pos = iter->func_pos = *pos;
2424 if (iter->flags & FTRACE_ITER_PRINTALL)
2425 return t_hash_start(m, pos);
2428 if (iter->idx >= iter->pg->index) {
2429 if (iter->pg->next) {
2430 iter->pg = iter->pg->next;
2435 rec = &iter->pg->records[iter->idx++];
2436 if (((iter->flags & FTRACE_ITER_FILTER) &&
2437 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2439 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2440 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2442 ((iter->flags & FTRACE_ITER_ENABLED) &&
2443 !(rec->flags & ~FTRACE_FL_MASK))) {
2451 return t_hash_start(m, pos);
2458 static void reset_iter_read(struct ftrace_iterator *iter)
2462 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2465 static void *t_start(struct seq_file *m, loff_t *pos)
2467 struct ftrace_iterator *iter = m->private;
2468 struct ftrace_ops *ops = iter->ops;
2472 mutex_lock(&ftrace_lock);
2474 if (unlikely(ftrace_disabled))
2478 * If an lseek was done, then reset and start from beginning.
2480 if (*pos < iter->pos)
2481 reset_iter_read(iter);
2484 * For set_ftrace_filter reading, if we have the filter
2485 * off, we can short cut and just print out that all
2486 * functions are enabled.
2488 if (iter->flags & FTRACE_ITER_FILTER &&
2489 ftrace_hash_empty(ops->filter_hash)) {
2491 return t_hash_start(m, pos);
2492 iter->flags |= FTRACE_ITER_PRINTALL;
2493 /* reset in case of seek/pread */
2494 iter->flags &= ~FTRACE_ITER_HASH;
2498 if (iter->flags & FTRACE_ITER_HASH)
2499 return t_hash_start(m, pos);
2502 * Unfortunately, we need to restart at ftrace_pages_start
2503 * every time we let go of the ftrace_mutex. This is because
2504 * those pointers can change without the lock.
2506 iter->pg = ftrace_pages_start;
2508 for (l = 0; l <= *pos; ) {
2509 p = t_next(m, p, &l);
2515 return t_hash_start(m, pos);
2520 static void t_stop(struct seq_file *m, void *p)
2522 mutex_unlock(&ftrace_lock);
2525 static int t_show(struct seq_file *m, void *v)
2527 struct ftrace_iterator *iter = m->private;
2528 struct dyn_ftrace *rec;
2530 if (iter->flags & FTRACE_ITER_HASH)
2531 return t_hash_show(m, iter);
2533 if (iter->flags & FTRACE_ITER_PRINTALL) {
2534 seq_printf(m, "#### all functions enabled ####\n");
2543 seq_printf(m, "%ps", (void *)rec->ip);
2544 if (iter->flags & FTRACE_ITER_ENABLED)
2545 seq_printf(m, " (%ld)%s",
2546 rec->flags & ~FTRACE_FL_MASK,
2547 rec->flags & FTRACE_FL_REGS ? " R" : "");
2548 seq_printf(m, "\n");
2553 static const struct seq_operations show_ftrace_seq_ops = {
2561 ftrace_avail_open(struct inode *inode, struct file *file)
2563 struct ftrace_iterator *iter;
2565 if (unlikely(ftrace_disabled))
2568 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2570 iter->pg = ftrace_pages_start;
2571 iter->ops = &global_ops;
2574 return iter ? 0 : -ENOMEM;
2578 ftrace_enabled_open(struct inode *inode, struct file *file)
2580 struct ftrace_iterator *iter;
2582 if (unlikely(ftrace_disabled))
2585 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2587 iter->pg = ftrace_pages_start;
2588 iter->flags = FTRACE_ITER_ENABLED;
2589 iter->ops = &global_ops;
2592 return iter ? 0 : -ENOMEM;
2595 static void ftrace_filter_reset(struct ftrace_hash *hash)
2597 mutex_lock(&ftrace_lock);
2598 ftrace_hash_clear(hash);
2599 mutex_unlock(&ftrace_lock);
2603 * ftrace_regex_open - initialize function tracer filter files
2604 * @ops: The ftrace_ops that hold the hash filters
2605 * @flag: The type of filter to process
2606 * @inode: The inode, usually passed in to your open routine
2607 * @file: The file, usually passed in to your open routine
2609 * ftrace_regex_open() initializes the filter files for the
2610 * @ops. Depending on @flag it may process the filter hash or
2611 * the notrace hash of @ops. With this called from the open
2612 * routine, you can use ftrace_filter_write() for the write
2613 * routine if @flag has FTRACE_ITER_FILTER set, or
2614 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2615 * ftrace_regex_lseek() should be used as the lseek routine, and
2616 * release must call ftrace_regex_release().
2619 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2620 struct inode *inode, struct file *file)
2622 struct ftrace_iterator *iter;
2623 struct ftrace_hash *hash;
2626 if (unlikely(ftrace_disabled))
2629 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2633 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2638 if (flag & FTRACE_ITER_NOTRACE)
2639 hash = ops->notrace_hash;
2641 hash = ops->filter_hash;
2646 if (file->f_mode & FMODE_WRITE) {
2647 mutex_lock(&ftrace_lock);
2648 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2649 mutex_unlock(&ftrace_lock);
2652 trace_parser_put(&iter->parser);
2658 mutex_lock(&ftrace_regex_lock);
2660 if ((file->f_mode & FMODE_WRITE) &&
2661 (file->f_flags & O_TRUNC))
2662 ftrace_filter_reset(iter->hash);
2664 if (file->f_mode & FMODE_READ) {
2665 iter->pg = ftrace_pages_start;
2667 ret = seq_open(file, &show_ftrace_seq_ops);
2669 struct seq_file *m = file->private_data;
2673 free_ftrace_hash(iter->hash);
2674 trace_parser_put(&iter->parser);
2678 file->private_data = iter;
2679 mutex_unlock(&ftrace_regex_lock);
2685 ftrace_filter_open(struct inode *inode, struct file *file)
2687 return ftrace_regex_open(&global_ops,
2688 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2693 ftrace_notrace_open(struct inode *inode, struct file *file)
2695 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2700 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
2704 if (file->f_mode & FMODE_READ)
2705 ret = seq_lseek(file, offset, whence);
2707 file->f_pos = ret = 1;
2712 static int ftrace_match(char *str, char *regex, int len, int type)
2719 if (strcmp(str, regex) == 0)
2722 case MATCH_FRONT_ONLY:
2723 if (strncmp(str, regex, len) == 0)
2726 case MATCH_MIDDLE_ONLY:
2727 if (strstr(str, regex))
2730 case MATCH_END_ONLY:
2732 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2741 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2743 struct ftrace_func_entry *entry;
2746 entry = ftrace_lookup_ip(hash, rec->ip);
2748 /* Do nothing if it doesn't exist */
2752 free_hash_entry(hash, entry);
2754 /* Do nothing if it exists */
2758 ret = add_hash_entry(hash, rec->ip);
2764 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2765 char *regex, int len, int type)
2767 char str[KSYM_SYMBOL_LEN];
2770 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2773 /* module lookup requires matching the module */
2774 if (!modname || strcmp(modname, mod))
2777 /* blank search means to match all funcs in the mod */
2782 return ftrace_match(str, regex, len, type);
2786 match_records(struct ftrace_hash *hash, char *buff,
2787 int len, char *mod, int not)
2789 unsigned search_len = 0;
2790 struct ftrace_page *pg;
2791 struct dyn_ftrace *rec;
2792 int type = MATCH_FULL;
2793 char *search = buff;
2798 type = filter_parse_regex(buff, len, &search, ¬);
2799 search_len = strlen(search);
2802 mutex_lock(&ftrace_lock);
2804 if (unlikely(ftrace_disabled))
2807 do_for_each_ftrace_rec(pg, rec) {
2808 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2809 ret = enter_record(hash, rec, not);
2816 } while_for_each_ftrace_rec();
2818 mutex_unlock(&ftrace_lock);
2824 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2826 return match_records(hash, buff, len, NULL, 0);
2830 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2834 /* blank or '*' mean the same */
2835 if (strcmp(buff, "*") == 0)
2838 /* handle the case of 'dont filter this module' */
2839 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2844 return match_records(hash, buff, strlen(buff), mod, not);
2848 * We register the module command as a template to show others how
2849 * to register the a command as well.
2853 ftrace_mod_callback(struct ftrace_hash *hash,
2854 char *func, char *cmd, char *param, int enable)
2860 * cmd == 'mod' because we only registered this func
2861 * for the 'mod' ftrace_func_command.
2862 * But if you register one func with multiple commands,
2863 * you can tell which command was used by the cmd
2867 /* we must have a module name */
2871 mod = strsep(¶m, ":");
2875 ret = ftrace_match_module_records(hash, func, mod);
2884 static struct ftrace_func_command ftrace_mod_cmd = {
2886 .func = ftrace_mod_callback,
2889 static int __init ftrace_mod_cmd_init(void)
2891 return register_ftrace_command(&ftrace_mod_cmd);
2893 core_initcall(ftrace_mod_cmd_init);
2895 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2896 struct ftrace_ops *op, struct pt_regs *pt_regs)
2898 struct ftrace_func_probe *entry;
2899 struct hlist_head *hhd;
2902 key = hash_long(ip, FTRACE_HASH_BITS);
2904 hhd = &ftrace_func_hash[key];
2906 if (hlist_empty(hhd))
2910 * Disable preemption for these calls to prevent a RCU grace
2911 * period. This syncs the hash iteration and freeing of items
2912 * on the hash. rcu_read_lock is too dangerous here.
2914 preempt_disable_notrace();
2915 hlist_for_each_entry_rcu(entry, hhd, node) {
2916 if (entry->ip == ip)
2917 entry->ops->func(ip, parent_ip, &entry->data);
2919 preempt_enable_notrace();
2922 static struct ftrace_ops trace_probe_ops __read_mostly =
2924 .func = function_trace_probe_call,
2927 static int ftrace_probe_registered;
2929 static void __enable_ftrace_function_probe(void)
2934 if (ftrace_probe_registered)
2937 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2938 struct hlist_head *hhd = &ftrace_func_hash[i];
2942 /* Nothing registered? */
2943 if (i == FTRACE_FUNC_HASHSIZE)
2946 ret = __register_ftrace_function(&trace_probe_ops);
2948 ret = ftrace_startup(&trace_probe_ops, 0);
2950 ftrace_probe_registered = 1;
2953 static void __disable_ftrace_function_probe(void)
2958 if (!ftrace_probe_registered)
2961 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2962 struct hlist_head *hhd = &ftrace_func_hash[i];
2967 /* no more funcs left */
2968 ret = __unregister_ftrace_function(&trace_probe_ops);
2970 ftrace_shutdown(&trace_probe_ops, 0);
2972 ftrace_probe_registered = 0;
2976 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2978 struct ftrace_func_probe *entry =
2979 container_of(rhp, struct ftrace_func_probe, rcu);
2981 if (entry->ops->free)
2982 entry->ops->free(&entry->data);
2988 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2991 struct ftrace_func_probe *entry;
2992 struct ftrace_page *pg;
2993 struct dyn_ftrace *rec;
2999 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3000 len = strlen(search);
3002 /* we do not support '!' for function probes */
3006 mutex_lock(&ftrace_lock);
3008 if (unlikely(ftrace_disabled))
3011 do_for_each_ftrace_rec(pg, rec) {
3013 if (!ftrace_match_record(rec, NULL, search, len, type))
3016 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3018 /* If we did not process any, then return error */
3029 * The caller might want to do something special
3030 * for each function we find. We call the callback
3031 * to give the caller an opportunity to do so.
3033 if (ops->callback) {
3034 if (ops->callback(rec->ip, &entry->data) < 0) {
3035 /* caller does not like this func */
3042 entry->ip = rec->ip;
3044 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3045 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3047 } while_for_each_ftrace_rec();
3048 __enable_ftrace_function_probe();
3051 mutex_unlock(&ftrace_lock);
3057 PROBE_TEST_FUNC = 1,
3062 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3063 void *data, int flags)
3065 struct ftrace_func_probe *entry;
3066 struct hlist_node *tmp;
3067 char str[KSYM_SYMBOL_LEN];
3068 int type = MATCH_FULL;
3072 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3077 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3078 len = strlen(search);
3080 /* we do not support '!' for function probes */
3085 mutex_lock(&ftrace_lock);
3086 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3087 struct hlist_head *hhd = &ftrace_func_hash[i];
3089 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3091 /* break up if statements for readability */
3092 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3095 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3098 /* do this last, since it is the most expensive */
3100 kallsyms_lookup(entry->ip, NULL, NULL,
3102 if (!ftrace_match(str, glob, len, type))
3106 hlist_del_rcu(&entry->node);
3107 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
3110 __disable_ftrace_function_probe();
3111 mutex_unlock(&ftrace_lock);
3115 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3118 __unregister_ftrace_function_probe(glob, ops, data,
3119 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3123 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3125 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3128 void unregister_ftrace_function_probe_all(char *glob)
3130 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3133 static LIST_HEAD(ftrace_commands);
3134 static DEFINE_MUTEX(ftrace_cmd_mutex);
3136 int register_ftrace_command(struct ftrace_func_command *cmd)
3138 struct ftrace_func_command *p;
3141 mutex_lock(&ftrace_cmd_mutex);
3142 list_for_each_entry(p, &ftrace_commands, list) {
3143 if (strcmp(cmd->name, p->name) == 0) {
3148 list_add(&cmd->list, &ftrace_commands);
3150 mutex_unlock(&ftrace_cmd_mutex);
3155 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3157 struct ftrace_func_command *p, *n;
3160 mutex_lock(&ftrace_cmd_mutex);
3161 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3162 if (strcmp(cmd->name, p->name) == 0) {
3164 list_del_init(&p->list);
3169 mutex_unlock(&ftrace_cmd_mutex);
3174 static int ftrace_process_regex(struct ftrace_hash *hash,
3175 char *buff, int len, int enable)
3177 char *func, *command, *next = buff;
3178 struct ftrace_func_command *p;
3181 func = strsep(&next, ":");
3184 ret = ftrace_match_records(hash, func, len);
3194 command = strsep(&next, ":");
3196 mutex_lock(&ftrace_cmd_mutex);
3197 list_for_each_entry(p, &ftrace_commands, list) {
3198 if (strcmp(p->name, command) == 0) {
3199 ret = p->func(hash, func, command, next, enable);
3204 mutex_unlock(&ftrace_cmd_mutex);
3210 ftrace_regex_write(struct file *file, const char __user *ubuf,
3211 size_t cnt, loff_t *ppos, int enable)
3213 struct ftrace_iterator *iter;
3214 struct trace_parser *parser;
3220 mutex_lock(&ftrace_regex_lock);
3223 if (unlikely(ftrace_disabled))
3226 if (file->f_mode & FMODE_READ) {
3227 struct seq_file *m = file->private_data;
3230 iter = file->private_data;
3232 parser = &iter->parser;
3233 read = trace_get_user(parser, ubuf, cnt, ppos);
3235 if (read >= 0 && trace_parser_loaded(parser) &&
3236 !trace_parser_cont(parser)) {
3237 ret = ftrace_process_regex(iter->hash, parser->buffer,
3238 parser->idx, enable);
3239 trace_parser_clear(parser);
3246 mutex_unlock(&ftrace_regex_lock);
3252 ftrace_filter_write(struct file *file, const char __user *ubuf,
3253 size_t cnt, loff_t *ppos)
3255 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3259 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3260 size_t cnt, loff_t *ppos)
3262 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3266 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3268 struct ftrace_func_entry *entry;
3270 if (!ftrace_location(ip))
3274 entry = ftrace_lookup_ip(hash, ip);
3277 free_hash_entry(hash, entry);
3281 return add_hash_entry(hash, ip);
3285 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3286 unsigned long ip, int remove, int reset, int enable)
3288 struct ftrace_hash **orig_hash;
3289 struct ftrace_hash *hash;
3292 /* All global ops uses the global ops filters */
3293 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3296 if (unlikely(ftrace_disabled))
3300 orig_hash = &ops->filter_hash;
3302 orig_hash = &ops->notrace_hash;
3304 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3308 mutex_lock(&ftrace_regex_lock);
3310 ftrace_filter_reset(hash);
3311 if (buf && !ftrace_match_records(hash, buf, len)) {
3313 goto out_regex_unlock;
3316 ret = ftrace_match_addr(hash, ip, remove);
3318 goto out_regex_unlock;
3321 mutex_lock(&ftrace_lock);
3322 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3323 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3325 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3327 mutex_unlock(&ftrace_lock);
3330 mutex_unlock(&ftrace_regex_lock);
3332 free_ftrace_hash(hash);
3337 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3338 int reset, int enable)
3340 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3344 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3345 * @ops - the ops to set the filter with
3346 * @ip - the address to add to or remove from the filter.
3347 * @remove - non zero to remove the ip from the filter
3348 * @reset - non zero to reset all filters before applying this filter.
3350 * Filters denote which functions should be enabled when tracing is enabled
3351 * If @ip is NULL, it failes to update filter.
3353 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3354 int remove, int reset)
3356 return ftrace_set_addr(ops, ip, remove, reset, 1);
3358 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3361 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3362 int reset, int enable)
3364 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3368 * ftrace_set_filter - set a function to filter on in ftrace
3369 * @ops - the ops to set the filter with
3370 * @buf - the string that holds the function filter text.
3371 * @len - the length of the string.
3372 * @reset - non zero to reset all filters before applying this filter.
3374 * Filters denote which functions should be enabled when tracing is enabled.
3375 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3377 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3380 return ftrace_set_regex(ops, buf, len, reset, 1);
3382 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3385 * ftrace_set_notrace - set a function to not trace in ftrace
3386 * @ops - the ops to set the notrace filter with
3387 * @buf - the string that holds the function notrace text.
3388 * @len - the length of the string.
3389 * @reset - non zero to reset all filters before applying this filter.
3391 * Notrace Filters denote which functions should not be enabled when tracing
3392 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3395 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3398 return ftrace_set_regex(ops, buf, len, reset, 0);
3400 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3402 * ftrace_set_filter - set a function to filter on in ftrace
3403 * @ops - the ops to set the filter with
3404 * @buf - the string that holds the function filter text.
3405 * @len - the length of the string.
3406 * @reset - non zero to reset all filters before applying this filter.
3408 * Filters denote which functions should be enabled when tracing is enabled.
3409 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3411 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3413 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3415 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3418 * ftrace_set_notrace - set a function to not trace in ftrace
3419 * @ops - the ops to set the notrace filter with
3420 * @buf - the string that holds the function notrace text.
3421 * @len - the length of the string.
3422 * @reset - non zero to reset all filters before applying this filter.
3424 * Notrace Filters denote which functions should not be enabled when tracing
3425 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3428 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3430 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3432 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3435 * command line interface to allow users to set filters on boot up.
3437 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3438 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3439 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3441 static int __init set_ftrace_notrace(char *str)
3443 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3446 __setup("ftrace_notrace=", set_ftrace_notrace);
3448 static int __init set_ftrace_filter(char *str)
3450 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3453 __setup("ftrace_filter=", set_ftrace_filter);
3455 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3456 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3457 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3459 static int __init set_graph_function(char *str)
3461 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3464 __setup("ftrace_graph_filter=", set_graph_function);
3466 static void __init set_ftrace_early_graph(char *buf)
3472 func = strsep(&buf, ",");
3473 /* we allow only one expression at a time */
3474 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3477 printk(KERN_DEBUG "ftrace: function %s not "
3478 "traceable\n", func);
3481 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3484 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3489 func = strsep(&buf, ",");
3490 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3494 static void __init set_ftrace_early_filters(void)
3496 if (ftrace_filter_buf[0])
3497 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3498 if (ftrace_notrace_buf[0])
3499 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3500 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3501 if (ftrace_graph_buf[0])
3502 set_ftrace_early_graph(ftrace_graph_buf);
3503 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3506 int ftrace_regex_release(struct inode *inode, struct file *file)
3508 struct seq_file *m = (struct seq_file *)file->private_data;
3509 struct ftrace_iterator *iter;
3510 struct ftrace_hash **orig_hash;
3511 struct trace_parser *parser;
3515 mutex_lock(&ftrace_regex_lock);
3516 if (file->f_mode & FMODE_READ) {
3519 seq_release(inode, file);
3521 iter = file->private_data;
3523 parser = &iter->parser;
3524 if (trace_parser_loaded(parser)) {
3525 parser->buffer[parser->idx] = 0;
3526 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3529 trace_parser_put(parser);
3531 if (file->f_mode & FMODE_WRITE) {
3532 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3535 orig_hash = &iter->ops->filter_hash;
3537 orig_hash = &iter->ops->notrace_hash;
3539 mutex_lock(&ftrace_lock);
3540 ret = ftrace_hash_move(iter->ops, filter_hash,
3541 orig_hash, iter->hash);
3542 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3544 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3546 mutex_unlock(&ftrace_lock);
3548 free_ftrace_hash(iter->hash);
3551 mutex_unlock(&ftrace_regex_lock);
3555 static const struct file_operations ftrace_avail_fops = {
3556 .open = ftrace_avail_open,
3558 .llseek = seq_lseek,
3559 .release = seq_release_private,
3562 static const struct file_operations ftrace_enabled_fops = {
3563 .open = ftrace_enabled_open,
3565 .llseek = seq_lseek,
3566 .release = seq_release_private,
3569 static const struct file_operations ftrace_filter_fops = {
3570 .open = ftrace_filter_open,
3572 .write = ftrace_filter_write,
3573 .llseek = ftrace_filter_lseek,
3574 .release = ftrace_regex_release,
3577 static const struct file_operations ftrace_notrace_fops = {
3578 .open = ftrace_notrace_open,
3580 .write = ftrace_notrace_write,
3581 .llseek = ftrace_filter_lseek,
3582 .release = ftrace_regex_release,
3585 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3587 static DEFINE_MUTEX(graph_lock);
3589 int ftrace_graph_count;
3590 int ftrace_graph_filter_enabled;
3591 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3594 __g_next(struct seq_file *m, loff_t *pos)
3596 if (*pos >= ftrace_graph_count)
3598 return &ftrace_graph_funcs[*pos];
3602 g_next(struct seq_file *m, void *v, loff_t *pos)
3605 return __g_next(m, pos);
3608 static void *g_start(struct seq_file *m, loff_t *pos)
3610 mutex_lock(&graph_lock);
3612 /* Nothing, tell g_show to print all functions are enabled */
3613 if (!ftrace_graph_filter_enabled && !*pos)
3616 return __g_next(m, pos);
3619 static void g_stop(struct seq_file *m, void *p)
3621 mutex_unlock(&graph_lock);
3624 static int g_show(struct seq_file *m, void *v)
3626 unsigned long *ptr = v;
3631 if (ptr == (unsigned long *)1) {
3632 seq_printf(m, "#### all functions enabled ####\n");
3636 seq_printf(m, "%ps\n", (void *)*ptr);
3641 static const struct seq_operations ftrace_graph_seq_ops = {
3649 ftrace_graph_open(struct inode *inode, struct file *file)
3653 if (unlikely(ftrace_disabled))
3656 mutex_lock(&graph_lock);
3657 if ((file->f_mode & FMODE_WRITE) &&
3658 (file->f_flags & O_TRUNC)) {
3659 ftrace_graph_filter_enabled = 0;
3660 ftrace_graph_count = 0;
3661 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3663 mutex_unlock(&graph_lock);
3665 if (file->f_mode & FMODE_READ)
3666 ret = seq_open(file, &ftrace_graph_seq_ops);
3672 ftrace_graph_release(struct inode *inode, struct file *file)
3674 if (file->f_mode & FMODE_READ)
3675 seq_release(inode, file);
3680 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3682 struct dyn_ftrace *rec;
3683 struct ftrace_page *pg;
3692 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3693 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3696 search_len = strlen(search);
3698 mutex_lock(&ftrace_lock);
3700 if (unlikely(ftrace_disabled)) {
3701 mutex_unlock(&ftrace_lock);
3705 do_for_each_ftrace_rec(pg, rec) {
3707 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3708 /* if it is in the array */
3710 for (i = 0; i < *idx; i++) {
3711 if (array[i] == rec->ip) {
3720 array[(*idx)++] = rec->ip;
3721 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3726 array[i] = array[--(*idx)];
3732 } while_for_each_ftrace_rec();
3734 mutex_unlock(&ftrace_lock);
3739 ftrace_graph_filter_enabled = 1;
3744 ftrace_graph_write(struct file *file, const char __user *ubuf,
3745 size_t cnt, loff_t *ppos)
3747 struct trace_parser parser;
3753 mutex_lock(&graph_lock);
3755 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3760 read = trace_get_user(&parser, ubuf, cnt, ppos);
3762 if (read >= 0 && trace_parser_loaded((&parser))) {
3763 parser.buffer[parser.idx] = 0;
3765 /* we allow only one expression at a time */
3766 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3775 trace_parser_put(&parser);
3777 mutex_unlock(&graph_lock);
3782 static const struct file_operations ftrace_graph_fops = {
3783 .open = ftrace_graph_open,
3785 .write = ftrace_graph_write,
3786 .llseek = ftrace_filter_lseek,
3787 .release = ftrace_graph_release,
3789 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3791 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3794 trace_create_file("available_filter_functions", 0444,
3795 d_tracer, NULL, &ftrace_avail_fops);
3797 trace_create_file("enabled_functions", 0444,
3798 d_tracer, NULL, &ftrace_enabled_fops);
3800 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3801 NULL, &ftrace_filter_fops);
3803 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3804 NULL, &ftrace_notrace_fops);
3806 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3807 trace_create_file("set_graph_function", 0444, d_tracer,
3809 &ftrace_graph_fops);
3810 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3815 static int ftrace_cmp_ips(const void *a, const void *b)
3817 const unsigned long *ipa = a;
3818 const unsigned long *ipb = b;
3827 static void ftrace_swap_ips(void *a, void *b, int size)
3829 unsigned long *ipa = a;
3830 unsigned long *ipb = b;
3838 static int ftrace_process_locs(struct module *mod,
3839 unsigned long *start,
3842 struct ftrace_page *start_pg;
3843 struct ftrace_page *pg;
3844 struct dyn_ftrace *rec;
3845 unsigned long count;
3848 unsigned long flags = 0; /* Shut up gcc */
3851 count = end - start;
3856 sort(start, count, sizeof(*start),
3857 ftrace_cmp_ips, ftrace_swap_ips);
3859 start_pg = ftrace_allocate_pages(count);
3863 mutex_lock(&ftrace_lock);
3866 * Core and each module needs their own pages, as
3867 * modules will free them when they are removed.
3868 * Force a new page to be allocated for modules.
3871 WARN_ON(ftrace_pages || ftrace_pages_start);
3872 /* First initialization */
3873 ftrace_pages = ftrace_pages_start = start_pg;
3878 if (WARN_ON(ftrace_pages->next)) {
3879 /* Hmm, we have free pages? */
3880 while (ftrace_pages->next)
3881 ftrace_pages = ftrace_pages->next;
3884 ftrace_pages->next = start_pg;
3890 addr = ftrace_call_adjust(*p++);
3892 * Some architecture linkers will pad between
3893 * the different mcount_loc sections of different
3894 * object files to satisfy alignments.
3895 * Skip any NULL pointers.
3900 if (pg->index == pg->size) {
3901 /* We should have allocated enough */
3902 if (WARN_ON(!pg->next))
3907 rec = &pg->records[pg->index++];
3911 /* We should have used all pages */
3914 /* Assign the last page to ftrace_pages */
3917 /* These new locations need to be initialized */
3918 ftrace_new_pgs = start_pg;
3921 * We only need to disable interrupts on start up
3922 * because we are modifying code that an interrupt
3923 * may execute, and the modification is not atomic.
3924 * But for modules, nothing runs the code we modify
3925 * until we are finished with it, and there's no
3926 * reason to cause large interrupt latencies while we do it.
3929 local_irq_save(flags);
3930 ftrace_update_code(mod);
3932 local_irq_restore(flags);
3935 mutex_unlock(&ftrace_lock);
3940 #ifdef CONFIG_MODULES
3942 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3944 void ftrace_release_mod(struct module *mod)
3946 struct dyn_ftrace *rec;
3947 struct ftrace_page **last_pg;
3948 struct ftrace_page *pg;
3951 mutex_lock(&ftrace_lock);
3953 if (ftrace_disabled)
3957 * Each module has its own ftrace_pages, remove
3958 * them from the list.
3960 last_pg = &ftrace_pages_start;
3961 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3962 rec = &pg->records[0];
3963 if (within_module_core(rec->ip, mod)) {
3965 * As core pages are first, the first
3966 * page should never be a module page.
3968 if (WARN_ON(pg == ftrace_pages_start))
3971 /* Check if we are deleting the last page */
3972 if (pg == ftrace_pages)
3973 ftrace_pages = next_to_ftrace_page(last_pg);
3975 *last_pg = pg->next;
3976 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3977 free_pages((unsigned long)pg->records, order);
3980 last_pg = &pg->next;
3983 mutex_unlock(&ftrace_lock);
3986 static void ftrace_init_module(struct module *mod,
3987 unsigned long *start, unsigned long *end)
3989 if (ftrace_disabled || start == end)
3991 ftrace_process_locs(mod, start, end);
3994 static int ftrace_module_notify_enter(struct notifier_block *self,
3995 unsigned long val, void *data)
3997 struct module *mod = data;
3999 if (val == MODULE_STATE_COMING)
4000 ftrace_init_module(mod, mod->ftrace_callsites,
4001 mod->ftrace_callsites +
4002 mod->num_ftrace_callsites);
4006 static int ftrace_module_notify_exit(struct notifier_block *self,
4007 unsigned long val, void *data)
4009 struct module *mod = data;
4011 if (val == MODULE_STATE_GOING)
4012 ftrace_release_mod(mod);
4017 static int ftrace_module_notify_enter(struct notifier_block *self,
4018 unsigned long val, void *data)
4022 static int ftrace_module_notify_exit(struct notifier_block *self,
4023 unsigned long val, void *data)
4027 #endif /* CONFIG_MODULES */
4029 struct notifier_block ftrace_module_enter_nb = {
4030 .notifier_call = ftrace_module_notify_enter,
4031 .priority = INT_MAX, /* Run before anything that can use kprobes */
4034 struct notifier_block ftrace_module_exit_nb = {
4035 .notifier_call = ftrace_module_notify_exit,
4036 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4039 extern unsigned long __start_mcount_loc[];
4040 extern unsigned long __stop_mcount_loc[];
4042 void __init ftrace_init(void)
4044 unsigned long count, addr, flags;
4047 /* Keep the ftrace pointer to the stub */
4048 addr = (unsigned long)ftrace_stub;
4050 local_irq_save(flags);
4051 ftrace_dyn_arch_init(&addr);
4052 local_irq_restore(flags);
4054 /* ftrace_dyn_arch_init places the return code in addr */
4058 count = __stop_mcount_loc - __start_mcount_loc;
4060 ret = ftrace_dyn_table_alloc(count);
4064 last_ftrace_enabled = ftrace_enabled = 1;
4066 ret = ftrace_process_locs(NULL,
4070 ret = register_module_notifier(&ftrace_module_enter_nb);
4072 pr_warning("Failed to register trace ftrace module enter notifier\n");
4074 ret = register_module_notifier(&ftrace_module_exit_nb);
4076 pr_warning("Failed to register trace ftrace module exit notifier\n");
4078 set_ftrace_early_filters();
4082 ftrace_disabled = 1;
4087 static struct ftrace_ops global_ops = {
4088 .func = ftrace_stub,
4089 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4092 static int __init ftrace_nodyn_init(void)
4097 core_initcall(ftrace_nodyn_init);
4099 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4100 static inline void ftrace_startup_enable(int command) { }
4101 /* Keep as macros so we do not need to define the commands */
4102 # define ftrace_startup(ops, command) \
4104 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4107 # define ftrace_shutdown(ops, command) do { } while (0)
4108 # define ftrace_startup_sysctl() do { } while (0)
4109 # define ftrace_shutdown_sysctl() do { } while (0)
4112 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4117 #endif /* CONFIG_DYNAMIC_FTRACE */
4120 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4121 struct ftrace_ops *op, struct pt_regs *regs)
4123 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4127 * Some of the ops may be dynamically allocated,
4128 * they must be freed after a synchronize_sched().
4130 preempt_disable_notrace();
4131 trace_recursion_set(TRACE_CONTROL_BIT);
4132 do_for_each_ftrace_op(op, ftrace_control_list) {
4133 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4134 !ftrace_function_local_disabled(op) &&
4135 ftrace_ops_test(op, ip))
4136 op->func(ip, parent_ip, op, regs);
4137 } while_for_each_ftrace_op(op);
4138 trace_recursion_clear(TRACE_CONTROL_BIT);
4139 preempt_enable_notrace();
4142 static struct ftrace_ops control_ops = {
4143 .func = ftrace_ops_control_func,
4144 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4148 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4149 struct ftrace_ops *ignored, struct pt_regs *regs)
4151 struct ftrace_ops *op;
4154 if (function_trace_stop)
4157 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4162 * Some of the ops may be dynamically allocated,
4163 * they must be freed after a synchronize_sched().
4165 preempt_disable_notrace();
4166 do_for_each_ftrace_op(op, ftrace_ops_list) {
4167 if (ftrace_ops_test(op, ip))
4168 op->func(ip, parent_ip, op, regs);
4169 } while_for_each_ftrace_op(op);
4170 preempt_enable_notrace();
4171 trace_clear_recursion(bit);
4175 * Some archs only support passing ip and parent_ip. Even though
4176 * the list function ignores the op parameter, we do not want any
4177 * C side effects, where a function is called without the caller
4178 * sending a third parameter.
4179 * Archs are to support both the regs and ftrace_ops at the same time.
4180 * If they support ftrace_ops, it is assumed they support regs.
4181 * If call backs want to use regs, they must either check for regs
4182 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4183 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4184 * An architecture can pass partial regs with ftrace_ops and still
4185 * set the ARCH_SUPPORT_FTARCE_OPS.
4187 #if ARCH_SUPPORTS_FTRACE_OPS
4188 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4189 struct ftrace_ops *op, struct pt_regs *regs)
4191 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4194 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4196 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4200 static void clear_ftrace_swapper(void)
4202 struct task_struct *p;
4206 for_each_online_cpu(cpu) {
4208 clear_tsk_trace_trace(p);
4213 static void set_ftrace_swapper(void)
4215 struct task_struct *p;
4219 for_each_online_cpu(cpu) {
4221 set_tsk_trace_trace(p);
4226 static void clear_ftrace_pid(struct pid *pid)
4228 struct task_struct *p;
4231 do_each_pid_task(pid, PIDTYPE_PID, p) {
4232 clear_tsk_trace_trace(p);
4233 } while_each_pid_task(pid, PIDTYPE_PID, p);
4239 static void set_ftrace_pid(struct pid *pid)
4241 struct task_struct *p;
4244 do_each_pid_task(pid, PIDTYPE_PID, p) {
4245 set_tsk_trace_trace(p);
4246 } while_each_pid_task(pid, PIDTYPE_PID, p);
4250 static void clear_ftrace_pid_task(struct pid *pid)
4252 if (pid == ftrace_swapper_pid)
4253 clear_ftrace_swapper();
4255 clear_ftrace_pid(pid);
4258 static void set_ftrace_pid_task(struct pid *pid)
4260 if (pid == ftrace_swapper_pid)
4261 set_ftrace_swapper();
4263 set_ftrace_pid(pid);
4266 static int ftrace_pid_add(int p)
4269 struct ftrace_pid *fpid;
4272 mutex_lock(&ftrace_lock);
4275 pid = ftrace_swapper_pid;
4277 pid = find_get_pid(p);
4284 list_for_each_entry(fpid, &ftrace_pids, list)
4285 if (fpid->pid == pid)
4290 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4294 list_add(&fpid->list, &ftrace_pids);
4297 set_ftrace_pid_task(pid);
4299 ftrace_update_pid_func();
4300 ftrace_startup_enable(0);
4302 mutex_unlock(&ftrace_lock);
4306 if (pid != ftrace_swapper_pid)
4310 mutex_unlock(&ftrace_lock);
4314 static void ftrace_pid_reset(void)
4316 struct ftrace_pid *fpid, *safe;
4318 mutex_lock(&ftrace_lock);
4319 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4320 struct pid *pid = fpid->pid;
4322 clear_ftrace_pid_task(pid);
4324 list_del(&fpid->list);
4328 ftrace_update_pid_func();
4329 ftrace_startup_enable(0);
4331 mutex_unlock(&ftrace_lock);
4334 static void *fpid_start(struct seq_file *m, loff_t *pos)
4336 mutex_lock(&ftrace_lock);
4338 if (list_empty(&ftrace_pids) && (!*pos))
4341 return seq_list_start(&ftrace_pids, *pos);
4344 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4349 return seq_list_next(v, &ftrace_pids, pos);
4352 static void fpid_stop(struct seq_file *m, void *p)
4354 mutex_unlock(&ftrace_lock);
4357 static int fpid_show(struct seq_file *m, void *v)
4359 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4361 if (v == (void *)1) {
4362 seq_printf(m, "no pid\n");
4366 if (fpid->pid == ftrace_swapper_pid)
4367 seq_printf(m, "swapper tasks\n");
4369 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4374 static const struct seq_operations ftrace_pid_sops = {
4375 .start = fpid_start,
4382 ftrace_pid_open(struct inode *inode, struct file *file)
4386 if ((file->f_mode & FMODE_WRITE) &&
4387 (file->f_flags & O_TRUNC))
4390 if (file->f_mode & FMODE_READ)
4391 ret = seq_open(file, &ftrace_pid_sops);
4397 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4398 size_t cnt, loff_t *ppos)
4404 if (cnt >= sizeof(buf))
4407 if (copy_from_user(&buf, ubuf, cnt))
4413 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4414 * to clean the filter quietly.
4416 tmp = strstrip(buf);
4417 if (strlen(tmp) == 0)
4420 ret = kstrtol(tmp, 10, &val);
4424 ret = ftrace_pid_add(val);
4426 return ret ? ret : cnt;
4430 ftrace_pid_release(struct inode *inode, struct file *file)
4432 if (file->f_mode & FMODE_READ)
4433 seq_release(inode, file);
4438 static const struct file_operations ftrace_pid_fops = {
4439 .open = ftrace_pid_open,
4440 .write = ftrace_pid_write,
4442 .llseek = ftrace_filter_lseek,
4443 .release = ftrace_pid_release,
4446 static __init int ftrace_init_debugfs(void)
4448 struct dentry *d_tracer;
4450 d_tracer = tracing_init_dentry();
4454 ftrace_init_dyn_debugfs(d_tracer);
4456 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4457 NULL, &ftrace_pid_fops);
4459 ftrace_profile_debugfs(d_tracer);
4463 fs_initcall(ftrace_init_debugfs);
4466 * ftrace_kill - kill ftrace
4468 * This function should be used by panic code. It stops ftrace
4469 * but in a not so nice way. If you need to simply kill ftrace
4470 * from a non-atomic section, use ftrace_kill.
4472 void ftrace_kill(void)
4474 ftrace_disabled = 1;
4476 clear_ftrace_function();
4480 * Test if ftrace is dead or not.
4482 int ftrace_is_dead(void)
4484 return ftrace_disabled;
4488 * register_ftrace_function - register a function for profiling
4489 * @ops - ops structure that holds the function for profiling.
4491 * Register a function to be called by all functions in the
4494 * Note: @ops->func and all the functions it calls must be labeled
4495 * with "notrace", otherwise it will go into a
4498 int register_ftrace_function(struct ftrace_ops *ops)
4502 mutex_lock(&ftrace_lock);
4504 ret = __register_ftrace_function(ops);
4506 ret = ftrace_startup(ops, 0);
4508 mutex_unlock(&ftrace_lock);
4512 EXPORT_SYMBOL_GPL(register_ftrace_function);
4515 * unregister_ftrace_function - unregister a function for profiling.
4516 * @ops - ops structure that holds the function to unregister
4518 * Unregister a function that was added to be called by ftrace profiling.
4520 int unregister_ftrace_function(struct ftrace_ops *ops)
4524 mutex_lock(&ftrace_lock);
4525 ret = __unregister_ftrace_function(ops);
4527 ftrace_shutdown(ops, 0);
4528 mutex_unlock(&ftrace_lock);
4532 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4535 ftrace_enable_sysctl(struct ctl_table *table, int write,
4536 void __user *buffer, size_t *lenp,
4541 mutex_lock(&ftrace_lock);
4543 if (unlikely(ftrace_disabled))
4546 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4548 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4551 last_ftrace_enabled = !!ftrace_enabled;
4553 if (ftrace_enabled) {
4555 ftrace_startup_sysctl();
4557 /* we are starting ftrace again */
4558 if (ftrace_ops_list != &ftrace_list_end)
4559 update_ftrace_function();
4562 /* stopping ftrace calls (just send to ftrace_stub) */
4563 ftrace_trace_function = ftrace_stub;
4565 ftrace_shutdown_sysctl();
4569 mutex_unlock(&ftrace_lock);
4573 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4575 static int ftrace_graph_active;
4576 static struct notifier_block ftrace_suspend_notifier;
4578 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4583 /* The callbacks that hook a function */
4584 trace_func_graph_ret_t ftrace_graph_return =
4585 (trace_func_graph_ret_t)ftrace_stub;
4586 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4588 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4589 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4593 unsigned long flags;
4594 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4595 struct task_struct *g, *t;
4597 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4598 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4599 * sizeof(struct ftrace_ret_stack),
4601 if (!ret_stack_list[i]) {
4609 read_lock_irqsave(&tasklist_lock, flags);
4610 do_each_thread(g, t) {
4616 if (t->ret_stack == NULL) {
4617 atomic_set(&t->tracing_graph_pause, 0);
4618 atomic_set(&t->trace_overrun, 0);
4619 t->curr_ret_stack = -1;
4620 /* Make sure the tasks see the -1 first: */
4622 t->ret_stack = ret_stack_list[start++];
4624 } while_each_thread(g, t);
4627 read_unlock_irqrestore(&tasklist_lock, flags);
4629 for (i = start; i < end; i++)
4630 kfree(ret_stack_list[i]);
4635 ftrace_graph_probe_sched_switch(void *ignore,
4636 struct task_struct *prev, struct task_struct *next)
4638 unsigned long long timestamp;
4642 * Does the user want to count the time a function was asleep.
4643 * If so, do not update the time stamps.
4645 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4648 timestamp = trace_clock_local();
4650 prev->ftrace_timestamp = timestamp;
4652 /* only process tasks that we timestamped */
4653 if (!next->ftrace_timestamp)
4657 * Update all the counters in next to make up for the
4658 * time next was sleeping.
4660 timestamp -= next->ftrace_timestamp;
4662 for (index = next->curr_ret_stack; index >= 0; index--)
4663 next->ret_stack[index].calltime += timestamp;
4666 /* Allocate a return stack for each task */
4667 static int start_graph_tracing(void)
4669 struct ftrace_ret_stack **ret_stack_list;
4672 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4673 sizeof(struct ftrace_ret_stack *),
4676 if (!ret_stack_list)
4679 /* The cpu_boot init_task->ret_stack will never be freed */
4680 for_each_online_cpu(cpu) {
4681 if (!idle_task(cpu)->ret_stack)
4682 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4686 ret = alloc_retstack_tasklist(ret_stack_list);
4687 } while (ret == -EAGAIN);
4690 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4692 pr_info("ftrace_graph: Couldn't activate tracepoint"
4693 " probe to kernel_sched_switch\n");
4696 kfree(ret_stack_list);
4701 * Hibernation protection.
4702 * The state of the current task is too much unstable during
4703 * suspend/restore to disk. We want to protect against that.
4706 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4710 case PM_HIBERNATION_PREPARE:
4711 pause_graph_tracing();
4714 case PM_POST_HIBERNATION:
4715 unpause_graph_tracing();
4721 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4722 trace_func_graph_ent_t entryfunc)
4726 mutex_lock(&ftrace_lock);
4728 /* we currently allow only one tracer registered at a time */
4729 if (ftrace_graph_active) {
4734 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4735 register_pm_notifier(&ftrace_suspend_notifier);
4737 ftrace_graph_active++;
4738 ret = start_graph_tracing();
4740 ftrace_graph_active--;
4744 ftrace_graph_return = retfunc;
4745 ftrace_graph_entry = entryfunc;
4747 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4750 mutex_unlock(&ftrace_lock);
4754 void unregister_ftrace_graph(void)
4756 mutex_lock(&ftrace_lock);
4758 if (unlikely(!ftrace_graph_active))
4761 ftrace_graph_active--;
4762 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4763 ftrace_graph_entry = ftrace_graph_entry_stub;
4764 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4765 unregister_pm_notifier(&ftrace_suspend_notifier);
4766 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4769 mutex_unlock(&ftrace_lock);
4772 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4775 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4777 atomic_set(&t->tracing_graph_pause, 0);
4778 atomic_set(&t->trace_overrun, 0);
4779 t->ftrace_timestamp = 0;
4780 /* make curr_ret_stack visible before we add the ret_stack */
4782 t->ret_stack = ret_stack;
4786 * Allocate a return stack for the idle task. May be the first
4787 * time through, or it may be done by CPU hotplug online.
4789 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4791 t->curr_ret_stack = -1;
4793 * The idle task has no parent, it either has its own
4794 * stack or no stack at all.
4797 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4799 if (ftrace_graph_active) {
4800 struct ftrace_ret_stack *ret_stack;
4802 ret_stack = per_cpu(idle_ret_stack, cpu);
4804 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4805 * sizeof(struct ftrace_ret_stack),
4809 per_cpu(idle_ret_stack, cpu) = ret_stack;
4811 graph_init_task(t, ret_stack);
4815 /* Allocate a return stack for newly created task */
4816 void ftrace_graph_init_task(struct task_struct *t)
4818 /* Make sure we do not use the parent ret_stack */
4819 t->ret_stack = NULL;
4820 t->curr_ret_stack = -1;
4822 if (ftrace_graph_active) {
4823 struct ftrace_ret_stack *ret_stack;
4825 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4826 * sizeof(struct ftrace_ret_stack),
4830 graph_init_task(t, ret_stack);
4834 void ftrace_graph_exit_task(struct task_struct *t)
4836 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4838 t->ret_stack = NULL;
4839 /* NULL must become visible to IRQs before we free it: */
4845 void ftrace_graph_stop(void)