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;
1056 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1060 if (file->f_mode & FMODE_READ)
1061 ret = seq_lseek(file, offset, whence);
1063 file->f_pos = ret = 1;
1068 #ifdef CONFIG_DYNAMIC_FTRACE
1070 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1071 # error Dynamic ftrace depends on MCOUNT_RECORD
1074 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1076 struct ftrace_func_probe {
1077 struct hlist_node node;
1078 struct ftrace_probe_ops *ops;
1079 unsigned long flags;
1082 struct rcu_head rcu;
1085 struct ftrace_func_entry {
1086 struct hlist_node hlist;
1090 struct ftrace_hash {
1091 unsigned long size_bits;
1092 struct hlist_head *buckets;
1093 unsigned long count;
1094 struct rcu_head rcu;
1098 * We make these constant because no one should touch them,
1099 * but they are used as the default "empty hash", to avoid allocating
1100 * it all the time. These are in a read only section such that if
1101 * anyone does try to modify it, it will cause an exception.
1103 static const struct hlist_head empty_buckets[1];
1104 static const struct ftrace_hash empty_hash = {
1105 .buckets = (struct hlist_head *)empty_buckets,
1107 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1109 static struct ftrace_ops global_ops = {
1110 .func = ftrace_stub,
1111 .notrace_hash = EMPTY_HASH,
1112 .filter_hash = EMPTY_HASH,
1113 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
1116 static DEFINE_MUTEX(ftrace_regex_lock);
1118 struct ftrace_page {
1119 struct ftrace_page *next;
1120 struct dyn_ftrace *records;
1125 static struct ftrace_page *ftrace_new_pgs;
1127 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1128 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1130 /* estimate from running different kernels */
1131 #define NR_TO_INIT 10000
1133 static struct ftrace_page *ftrace_pages_start;
1134 static struct ftrace_page *ftrace_pages;
1136 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1138 return !hash || !hash->count;
1141 static struct ftrace_func_entry *
1142 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1145 struct ftrace_func_entry *entry;
1146 struct hlist_head *hhd;
1148 if (ftrace_hash_empty(hash))
1151 if (hash->size_bits > 0)
1152 key = hash_long(ip, hash->size_bits);
1156 hhd = &hash->buckets[key];
1158 hlist_for_each_entry_rcu(entry, hhd, hlist) {
1159 if (entry->ip == ip)
1165 static void __add_hash_entry(struct ftrace_hash *hash,
1166 struct ftrace_func_entry *entry)
1168 struct hlist_head *hhd;
1171 if (hash->size_bits)
1172 key = hash_long(entry->ip, hash->size_bits);
1176 hhd = &hash->buckets[key];
1177 hlist_add_head(&entry->hlist, hhd);
1181 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1183 struct ftrace_func_entry *entry;
1185 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1190 __add_hash_entry(hash, entry);
1196 free_hash_entry(struct ftrace_hash *hash,
1197 struct ftrace_func_entry *entry)
1199 hlist_del(&entry->hlist);
1205 remove_hash_entry(struct ftrace_hash *hash,
1206 struct ftrace_func_entry *entry)
1208 hlist_del(&entry->hlist);
1212 static void ftrace_hash_clear(struct ftrace_hash *hash)
1214 struct hlist_head *hhd;
1215 struct hlist_node *tn;
1216 struct ftrace_func_entry *entry;
1217 int size = 1 << hash->size_bits;
1223 for (i = 0; i < size; i++) {
1224 hhd = &hash->buckets[i];
1225 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1226 free_hash_entry(hash, entry);
1228 FTRACE_WARN_ON(hash->count);
1231 static void free_ftrace_hash(struct ftrace_hash *hash)
1233 if (!hash || hash == EMPTY_HASH)
1235 ftrace_hash_clear(hash);
1236 kfree(hash->buckets);
1240 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1242 struct ftrace_hash *hash;
1244 hash = container_of(rcu, struct ftrace_hash, rcu);
1245 free_ftrace_hash(hash);
1248 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1250 if (!hash || hash == EMPTY_HASH)
1252 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1255 void ftrace_free_filter(struct ftrace_ops *ops)
1257 free_ftrace_hash(ops->filter_hash);
1258 free_ftrace_hash(ops->notrace_hash);
1261 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1263 struct ftrace_hash *hash;
1266 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1270 size = 1 << size_bits;
1271 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1273 if (!hash->buckets) {
1278 hash->size_bits = size_bits;
1283 static struct ftrace_hash *
1284 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1286 struct ftrace_func_entry *entry;
1287 struct ftrace_hash *new_hash;
1292 new_hash = alloc_ftrace_hash(size_bits);
1297 if (ftrace_hash_empty(hash))
1300 size = 1 << hash->size_bits;
1301 for (i = 0; i < size; i++) {
1302 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1303 ret = add_hash_entry(new_hash, entry->ip);
1309 FTRACE_WARN_ON(new_hash->count != hash->count);
1314 free_ftrace_hash(new_hash);
1319 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1321 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1324 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1325 struct ftrace_hash **dst, struct ftrace_hash *src)
1327 struct ftrace_func_entry *entry;
1328 struct hlist_node *tn;
1329 struct hlist_head *hhd;
1330 struct ftrace_hash *old_hash;
1331 struct ftrace_hash *new_hash;
1333 int size = src->count;
1339 * Remove the current set, update the hash and add
1342 ftrace_hash_rec_disable(ops, enable);
1345 * If the new source is empty, just free dst and assign it
1349 free_ftrace_hash_rcu(*dst);
1350 rcu_assign_pointer(*dst, EMPTY_HASH);
1351 /* still need to update the function records */
1357 * Make the hash size about 1/2 the # found
1359 for (size /= 2; size; size >>= 1)
1362 /* Don't allocate too much */
1363 if (bits > FTRACE_HASH_MAX_BITS)
1364 bits = FTRACE_HASH_MAX_BITS;
1367 new_hash = alloc_ftrace_hash(bits);
1371 size = 1 << src->size_bits;
1372 for (i = 0; i < size; i++) {
1373 hhd = &src->buckets[i];
1374 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1376 key = hash_long(entry->ip, bits);
1379 remove_hash_entry(src, entry);
1380 __add_hash_entry(new_hash, entry);
1385 rcu_assign_pointer(*dst, new_hash);
1386 free_ftrace_hash_rcu(old_hash);
1391 * Enable regardless of ret:
1392 * On success, we enable the new hash.
1393 * On failure, we re-enable the original hash.
1395 ftrace_hash_rec_enable(ops, enable);
1401 * Test the hashes for this ops to see if we want to call
1402 * the ops->func or not.
1404 * It's a match if the ip is in the ops->filter_hash or
1405 * the filter_hash does not exist or is empty,
1407 * the ip is not in the ops->notrace_hash.
1409 * This needs to be called with preemption disabled as
1410 * the hashes are freed with call_rcu_sched().
1413 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1415 struct ftrace_hash *filter_hash;
1416 struct ftrace_hash *notrace_hash;
1419 filter_hash = rcu_dereference_raw(ops->filter_hash);
1420 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1422 if ((ftrace_hash_empty(filter_hash) ||
1423 ftrace_lookup_ip(filter_hash, ip)) &&
1424 (ftrace_hash_empty(notrace_hash) ||
1425 !ftrace_lookup_ip(notrace_hash, ip)))
1434 * This is a double for. Do not use 'break' to break out of the loop,
1435 * you must use a goto.
1437 #define do_for_each_ftrace_rec(pg, rec) \
1438 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1440 for (_____i = 0; _____i < pg->index; _____i++) { \
1441 rec = &pg->records[_____i];
1443 #define while_for_each_ftrace_rec() \
1448 static int ftrace_cmp_recs(const void *a, const void *b)
1450 const struct dyn_ftrace *key = a;
1451 const struct dyn_ftrace *rec = b;
1453 if (key->flags < rec->ip)
1455 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1460 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1462 struct ftrace_page *pg;
1463 struct dyn_ftrace *rec;
1464 struct dyn_ftrace key;
1467 key.flags = end; /* overload flags, as it is unsigned long */
1469 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1470 if (end < pg->records[0].ip ||
1471 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1473 rec = bsearch(&key, pg->records, pg->index,
1474 sizeof(struct dyn_ftrace),
1484 * ftrace_location - return true if the ip giving is a traced location
1485 * @ip: the instruction pointer to check
1487 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1488 * That is, the instruction that is either a NOP or call to
1489 * the function tracer. It checks the ftrace internal tables to
1490 * determine if the address belongs or not.
1492 unsigned long ftrace_location(unsigned long ip)
1494 return ftrace_location_range(ip, ip);
1498 * ftrace_text_reserved - return true if range contains an ftrace location
1499 * @start: start of range to search
1500 * @end: end of range to search (inclusive). @end points to the last byte to check.
1502 * Returns 1 if @start and @end contains a ftrace location.
1503 * That is, the instruction that is either a NOP or call to
1504 * the function tracer. It checks the ftrace internal tables to
1505 * determine if the address belongs or not.
1507 int ftrace_text_reserved(void *start, void *end)
1511 ret = ftrace_location_range((unsigned long)start,
1512 (unsigned long)end);
1517 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1521 struct ftrace_hash *hash;
1522 struct ftrace_hash *other_hash;
1523 struct ftrace_page *pg;
1524 struct dyn_ftrace *rec;
1528 /* Only update if the ops has been registered */
1529 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1533 * In the filter_hash case:
1534 * If the count is zero, we update all records.
1535 * Otherwise we just update the items in the hash.
1537 * In the notrace_hash case:
1538 * We enable the update in the hash.
1539 * As disabling notrace means enabling the tracing,
1540 * and enabling notrace means disabling, the inc variable
1544 hash = ops->filter_hash;
1545 other_hash = ops->notrace_hash;
1546 if (ftrace_hash_empty(hash))
1550 hash = ops->notrace_hash;
1551 other_hash = ops->filter_hash;
1553 * If the notrace hash has no items,
1554 * then there's nothing to do.
1556 if (ftrace_hash_empty(hash))
1560 do_for_each_ftrace_rec(pg, rec) {
1561 int in_other_hash = 0;
1567 * Only the filter_hash affects all records.
1568 * Update if the record is not in the notrace hash.
1570 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1573 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1574 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1579 if (filter_hash && in_hash && !in_other_hash)
1581 else if (!filter_hash && in_hash &&
1582 (in_other_hash || ftrace_hash_empty(other_hash)))
1590 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1593 * If any ops wants regs saved for this function
1594 * then all ops will get saved regs.
1596 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1597 rec->flags |= FTRACE_FL_REGS;
1599 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1604 /* Shortcut, if we handled all records, we are done. */
1605 if (!all && count == hash->count)
1607 } while_for_each_ftrace_rec();
1610 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1613 __ftrace_hash_rec_update(ops, filter_hash, 0);
1616 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1619 __ftrace_hash_rec_update(ops, filter_hash, 1);
1622 static void print_ip_ins(const char *fmt, unsigned char *p)
1626 printk(KERN_CONT "%s", fmt);
1628 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1629 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1633 * ftrace_bug - report and shutdown function tracer
1634 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1635 * @ip: The address that failed
1637 * The arch code that enables or disables the function tracing
1638 * can call ftrace_bug() when it has detected a problem in
1639 * modifying the code. @failed should be one of either:
1640 * EFAULT - if the problem happens on reading the @ip address
1641 * EINVAL - if what is read at @ip is not what was expected
1642 * EPERM - if the problem happens on writting to the @ip address
1644 void ftrace_bug(int failed, unsigned long ip)
1648 FTRACE_WARN_ON_ONCE(1);
1649 pr_info("ftrace faulted on modifying ");
1653 FTRACE_WARN_ON_ONCE(1);
1654 pr_info("ftrace failed to modify ");
1656 print_ip_ins(" actual: ", (unsigned char *)ip);
1657 printk(KERN_CONT "\n");
1660 FTRACE_WARN_ON_ONCE(1);
1661 pr_info("ftrace faulted on writing ");
1665 FTRACE_WARN_ON_ONCE(1);
1666 pr_info("ftrace faulted on unknown error ");
1671 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1673 unsigned long flag = 0UL;
1676 * If we are updating calls:
1678 * If the record has a ref count, then we need to enable it
1679 * because someone is using it.
1681 * Otherwise we make sure its disabled.
1683 * If we are disabling calls, then disable all records that
1686 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1687 flag = FTRACE_FL_ENABLED;
1690 * If enabling and the REGS flag does not match the REGS_EN, then
1691 * do not ignore this record. Set flags to fail the compare against
1695 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1696 flag |= FTRACE_FL_REGS;
1698 /* If the state of this record hasn't changed, then do nothing */
1699 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1700 return FTRACE_UPDATE_IGNORE;
1703 /* Save off if rec is being enabled (for return value) */
1704 flag ^= rec->flags & FTRACE_FL_ENABLED;
1707 rec->flags |= FTRACE_FL_ENABLED;
1708 if (flag & FTRACE_FL_REGS) {
1709 if (rec->flags & FTRACE_FL_REGS)
1710 rec->flags |= FTRACE_FL_REGS_EN;
1712 rec->flags &= ~FTRACE_FL_REGS_EN;
1717 * If this record is being updated from a nop, then
1718 * return UPDATE_MAKE_CALL.
1719 * Otherwise, if the EN flag is set, then return
1720 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1721 * from the non-save regs, to a save regs function.
1723 * return UPDATE_MODIFY_CALL to tell the caller to convert
1724 * from the save regs, to a non-save regs function.
1726 if (flag & FTRACE_FL_ENABLED)
1727 return FTRACE_UPDATE_MAKE_CALL;
1728 else if (rec->flags & FTRACE_FL_REGS_EN)
1729 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1731 return FTRACE_UPDATE_MODIFY_CALL;
1735 /* If there's no more users, clear all flags */
1736 if (!(rec->flags & ~FTRACE_FL_MASK))
1739 /* Just disable the record (keep REGS state) */
1740 rec->flags &= ~FTRACE_FL_ENABLED;
1743 return FTRACE_UPDATE_MAKE_NOP;
1747 * ftrace_update_record, set a record that now is tracing or not
1748 * @rec: the record to update
1749 * @enable: set to 1 if the record is tracing, zero to force disable
1751 * The records that represent all functions that can be traced need
1752 * to be updated when tracing has been enabled.
1754 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1756 return ftrace_check_record(rec, enable, 1);
1760 * ftrace_test_record, check if the record has been enabled or not
1761 * @rec: the record to test
1762 * @enable: set to 1 to check if enabled, 0 if it is disabled
1764 * The arch code may need to test if a record is already set to
1765 * tracing to determine how to modify the function code that it
1768 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1770 return ftrace_check_record(rec, enable, 0);
1774 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1776 unsigned long ftrace_old_addr;
1777 unsigned long ftrace_addr;
1780 ret = ftrace_update_record(rec, enable);
1782 if (rec->flags & FTRACE_FL_REGS)
1783 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1785 ftrace_addr = (unsigned long)FTRACE_ADDR;
1788 case FTRACE_UPDATE_IGNORE:
1791 case FTRACE_UPDATE_MAKE_CALL:
1792 return ftrace_make_call(rec, ftrace_addr);
1794 case FTRACE_UPDATE_MAKE_NOP:
1795 return ftrace_make_nop(NULL, rec, ftrace_addr);
1797 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1798 case FTRACE_UPDATE_MODIFY_CALL:
1799 if (rec->flags & FTRACE_FL_REGS)
1800 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1802 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1804 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1807 return -1; /* unknow ftrace bug */
1810 void __weak ftrace_replace_code(int enable)
1812 struct dyn_ftrace *rec;
1813 struct ftrace_page *pg;
1816 if (unlikely(ftrace_disabled))
1819 do_for_each_ftrace_rec(pg, rec) {
1820 failed = __ftrace_replace_code(rec, enable);
1822 ftrace_bug(failed, rec->ip);
1823 /* Stop processing */
1826 } while_for_each_ftrace_rec();
1829 struct ftrace_rec_iter {
1830 struct ftrace_page *pg;
1835 * ftrace_rec_iter_start, start up iterating over traced functions
1837 * Returns an iterator handle that is used to iterate over all
1838 * the records that represent address locations where functions
1841 * May return NULL if no records are available.
1843 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1846 * We only use a single iterator.
1847 * Protected by the ftrace_lock mutex.
1849 static struct ftrace_rec_iter ftrace_rec_iter;
1850 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1852 iter->pg = ftrace_pages_start;
1855 /* Could have empty pages */
1856 while (iter->pg && !iter->pg->index)
1857 iter->pg = iter->pg->next;
1866 * ftrace_rec_iter_next, get the next record to process.
1867 * @iter: The handle to the iterator.
1869 * Returns the next iterator after the given iterator @iter.
1871 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1875 if (iter->index >= iter->pg->index) {
1876 iter->pg = iter->pg->next;
1879 /* Could have empty pages */
1880 while (iter->pg && !iter->pg->index)
1881 iter->pg = iter->pg->next;
1891 * ftrace_rec_iter_record, get the record at the iterator location
1892 * @iter: The current iterator location
1894 * Returns the record that the current @iter is at.
1896 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1898 return &iter->pg->records[iter->index];
1902 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1909 if (unlikely(ftrace_disabled))
1912 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1914 ftrace_bug(ret, ip);
1921 * archs can override this function if they must do something
1922 * before the modifying code is performed.
1924 int __weak ftrace_arch_code_modify_prepare(void)
1930 * archs can override this function if they must do something
1931 * after the modifying code is performed.
1933 int __weak ftrace_arch_code_modify_post_process(void)
1938 void ftrace_modify_all_code(int command)
1940 if (command & FTRACE_UPDATE_CALLS)
1941 ftrace_replace_code(1);
1942 else if (command & FTRACE_DISABLE_CALLS)
1943 ftrace_replace_code(0);
1945 if (command & FTRACE_UPDATE_TRACE_FUNC)
1946 ftrace_update_ftrace_func(ftrace_trace_function);
1948 if (command & FTRACE_START_FUNC_RET)
1949 ftrace_enable_ftrace_graph_caller();
1950 else if (command & FTRACE_STOP_FUNC_RET)
1951 ftrace_disable_ftrace_graph_caller();
1954 static int __ftrace_modify_code(void *data)
1956 int *command = data;
1958 ftrace_modify_all_code(*command);
1964 * ftrace_run_stop_machine, go back to the stop machine method
1965 * @command: The command to tell ftrace what to do
1967 * If an arch needs to fall back to the stop machine method, the
1968 * it can call this function.
1970 void ftrace_run_stop_machine(int command)
1972 stop_machine(__ftrace_modify_code, &command, NULL);
1976 * arch_ftrace_update_code, modify the code to trace or not trace
1977 * @command: The command that needs to be done
1979 * Archs can override this function if it does not need to
1980 * run stop_machine() to modify code.
1982 void __weak arch_ftrace_update_code(int command)
1984 ftrace_run_stop_machine(command);
1987 static void ftrace_run_update_code(int command)
1991 ret = ftrace_arch_code_modify_prepare();
1992 FTRACE_WARN_ON(ret);
1996 * Do not call function tracer while we update the code.
1997 * We are in stop machine.
1999 function_trace_stop++;
2002 * By default we use stop_machine() to modify the code.
2003 * But archs can do what ever they want as long as it
2004 * is safe. The stop_machine() is the safest, but also
2005 * produces the most overhead.
2007 arch_ftrace_update_code(command);
2009 function_trace_stop--;
2011 ret = ftrace_arch_code_modify_post_process();
2012 FTRACE_WARN_ON(ret);
2015 static ftrace_func_t saved_ftrace_func;
2016 static int ftrace_start_up;
2017 static int global_start_up;
2019 static void ftrace_startup_enable(int command)
2021 if (saved_ftrace_func != ftrace_trace_function) {
2022 saved_ftrace_func = ftrace_trace_function;
2023 command |= FTRACE_UPDATE_TRACE_FUNC;
2026 if (!command || !ftrace_enabled)
2029 ftrace_run_update_code(command);
2032 static int ftrace_startup(struct ftrace_ops *ops, int command)
2034 bool hash_enable = true;
2036 if (unlikely(ftrace_disabled))
2040 command |= FTRACE_UPDATE_CALLS;
2042 /* ops marked global share the filter hashes */
2043 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2045 /* Don't update hash if global is already set */
2046 if (global_start_up)
2047 hash_enable = false;
2051 ops->flags |= FTRACE_OPS_FL_ENABLED;
2053 ftrace_hash_rec_enable(ops, 1);
2055 ftrace_startup_enable(command);
2060 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
2062 bool hash_disable = true;
2064 if (unlikely(ftrace_disabled))
2069 * Just warn in case of unbalance, no need to kill ftrace, it's not
2070 * critical but the ftrace_call callers may be never nopped again after
2071 * further ftrace uses.
2073 WARN_ON_ONCE(ftrace_start_up < 0);
2075 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2078 WARN_ON_ONCE(global_start_up < 0);
2079 /* Don't update hash if global still has users */
2080 if (global_start_up) {
2081 WARN_ON_ONCE(!ftrace_start_up);
2082 hash_disable = false;
2087 ftrace_hash_rec_disable(ops, 1);
2089 if (ops != &global_ops || !global_start_up)
2090 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2092 command |= FTRACE_UPDATE_CALLS;
2094 if (saved_ftrace_func != ftrace_trace_function) {
2095 saved_ftrace_func = ftrace_trace_function;
2096 command |= FTRACE_UPDATE_TRACE_FUNC;
2099 if (!command || !ftrace_enabled)
2102 ftrace_run_update_code(command);
2105 static void ftrace_startup_sysctl(void)
2107 if (unlikely(ftrace_disabled))
2110 /* Force update next time */
2111 saved_ftrace_func = NULL;
2112 /* ftrace_start_up is true if we want ftrace running */
2113 if (ftrace_start_up)
2114 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2117 static void ftrace_shutdown_sysctl(void)
2119 if (unlikely(ftrace_disabled))
2122 /* ftrace_start_up is true if ftrace is running */
2123 if (ftrace_start_up)
2124 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2127 static cycle_t ftrace_update_time;
2128 static unsigned long ftrace_update_cnt;
2129 unsigned long ftrace_update_tot_cnt;
2131 static int ops_traces_mod(struct ftrace_ops *ops)
2133 struct ftrace_hash *hash;
2135 hash = ops->filter_hash;
2136 return ftrace_hash_empty(hash);
2139 static int ftrace_update_code(struct module *mod)
2141 struct ftrace_page *pg;
2142 struct dyn_ftrace *p;
2143 cycle_t start, stop;
2144 unsigned long ref = 0;
2148 * When adding a module, we need to check if tracers are
2149 * currently enabled and if they are set to trace all functions.
2150 * If they are, we need to enable the module functions as well
2151 * as update the reference counts for those function records.
2154 struct ftrace_ops *ops;
2156 for (ops = ftrace_ops_list;
2157 ops != &ftrace_list_end; ops = ops->next) {
2158 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2159 ops_traces_mod(ops))
2164 start = ftrace_now(raw_smp_processor_id());
2165 ftrace_update_cnt = 0;
2167 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2169 for (i = 0; i < pg->index; i++) {
2170 /* If something went wrong, bail without enabling anything */
2171 if (unlikely(ftrace_disabled))
2174 p = &pg->records[i];
2178 * Do the initial record conversion from mcount jump
2179 * to the NOP instructions.
2181 if (!ftrace_code_disable(mod, p))
2184 ftrace_update_cnt++;
2187 * If the tracing is enabled, go ahead and enable the record.
2189 * The reason not to enable the record immediatelly is the
2190 * inherent check of ftrace_make_nop/ftrace_make_call for
2191 * correct previous instructions. Making first the NOP
2192 * conversion puts the module to the correct state, thus
2193 * passing the ftrace_make_call check.
2195 if (ftrace_start_up && ref) {
2196 int failed = __ftrace_replace_code(p, 1);
2198 ftrace_bug(failed, p->ip);
2203 ftrace_new_pgs = NULL;
2205 stop = ftrace_now(raw_smp_processor_id());
2206 ftrace_update_time = stop - start;
2207 ftrace_update_tot_cnt += ftrace_update_cnt;
2212 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2217 if (WARN_ON(!count))
2220 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2223 * We want to fill as much as possible. No more than a page
2226 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2230 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2233 /* if we can't allocate this size, try something smaller */
2240 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2249 static struct ftrace_page *
2250 ftrace_allocate_pages(unsigned long num_to_init)
2252 struct ftrace_page *start_pg;
2253 struct ftrace_page *pg;
2260 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2265 * Try to allocate as much as possible in one continues
2266 * location that fills in all of the space. We want to
2267 * waste as little space as possible.
2270 cnt = ftrace_allocate_records(pg, num_to_init);
2278 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2289 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2290 free_pages((unsigned long)pg->records, order);
2291 start_pg = pg->next;
2295 pr_info("ftrace: FAILED to allocate memory for functions\n");
2299 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2304 pr_info("ftrace: No functions to be traced?\n");
2308 cnt = num_to_init / ENTRIES_PER_PAGE;
2309 pr_info("ftrace: allocating %ld entries in %d pages\n",
2310 num_to_init, cnt + 1);
2315 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2317 struct ftrace_iterator {
2320 struct ftrace_page *pg;
2321 struct dyn_ftrace *func;
2322 struct ftrace_func_probe *probe;
2323 struct trace_parser parser;
2324 struct ftrace_hash *hash;
2325 struct ftrace_ops *ops;
2332 t_hash_next(struct seq_file *m, loff_t *pos)
2334 struct ftrace_iterator *iter = m->private;
2335 struct hlist_node *hnd = NULL;
2336 struct hlist_head *hhd;
2342 hnd = &iter->probe->node;
2344 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2347 hhd = &ftrace_func_hash[iter->hidx];
2349 if (hlist_empty(hhd)) {
2365 if (WARN_ON_ONCE(!hnd))
2368 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2373 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2375 struct ftrace_iterator *iter = m->private;
2379 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2382 if (iter->func_pos > *pos)
2386 for (l = 0; l <= (*pos - iter->func_pos); ) {
2387 p = t_hash_next(m, &l);
2394 /* Only set this if we have an item */
2395 iter->flags |= FTRACE_ITER_HASH;
2401 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2403 struct ftrace_func_probe *rec;
2406 if (WARN_ON_ONCE(!rec))
2409 if (rec->ops->print)
2410 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2412 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2415 seq_printf(m, ":%p", rec->data);
2422 t_next(struct seq_file *m, void *v, loff_t *pos)
2424 struct ftrace_iterator *iter = m->private;
2425 struct ftrace_ops *ops = iter->ops;
2426 struct dyn_ftrace *rec = NULL;
2428 if (unlikely(ftrace_disabled))
2431 if (iter->flags & FTRACE_ITER_HASH)
2432 return t_hash_next(m, pos);
2435 iter->pos = iter->func_pos = *pos;
2437 if (iter->flags & FTRACE_ITER_PRINTALL)
2438 return t_hash_start(m, pos);
2441 if (iter->idx >= iter->pg->index) {
2442 if (iter->pg->next) {
2443 iter->pg = iter->pg->next;
2448 rec = &iter->pg->records[iter->idx++];
2449 if (((iter->flags & FTRACE_ITER_FILTER) &&
2450 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2452 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2453 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2455 ((iter->flags & FTRACE_ITER_ENABLED) &&
2456 !(rec->flags & ~FTRACE_FL_MASK))) {
2464 return t_hash_start(m, pos);
2471 static void reset_iter_read(struct ftrace_iterator *iter)
2475 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2478 static void *t_start(struct seq_file *m, loff_t *pos)
2480 struct ftrace_iterator *iter = m->private;
2481 struct ftrace_ops *ops = iter->ops;
2485 mutex_lock(&ftrace_lock);
2487 if (unlikely(ftrace_disabled))
2491 * If an lseek was done, then reset and start from beginning.
2493 if (*pos < iter->pos)
2494 reset_iter_read(iter);
2497 * For set_ftrace_filter reading, if we have the filter
2498 * off, we can short cut and just print out that all
2499 * functions are enabled.
2501 if (iter->flags & FTRACE_ITER_FILTER &&
2502 ftrace_hash_empty(ops->filter_hash)) {
2504 return t_hash_start(m, pos);
2505 iter->flags |= FTRACE_ITER_PRINTALL;
2506 /* reset in case of seek/pread */
2507 iter->flags &= ~FTRACE_ITER_HASH;
2511 if (iter->flags & FTRACE_ITER_HASH)
2512 return t_hash_start(m, pos);
2515 * Unfortunately, we need to restart at ftrace_pages_start
2516 * every time we let go of the ftrace_mutex. This is because
2517 * those pointers can change without the lock.
2519 iter->pg = ftrace_pages_start;
2521 for (l = 0; l <= *pos; ) {
2522 p = t_next(m, p, &l);
2528 return t_hash_start(m, pos);
2533 static void t_stop(struct seq_file *m, void *p)
2535 mutex_unlock(&ftrace_lock);
2538 static int t_show(struct seq_file *m, void *v)
2540 struct ftrace_iterator *iter = m->private;
2541 struct dyn_ftrace *rec;
2543 if (iter->flags & FTRACE_ITER_HASH)
2544 return t_hash_show(m, iter);
2546 if (iter->flags & FTRACE_ITER_PRINTALL) {
2547 seq_printf(m, "#### all functions enabled ####\n");
2556 seq_printf(m, "%ps", (void *)rec->ip);
2557 if (iter->flags & FTRACE_ITER_ENABLED)
2558 seq_printf(m, " (%ld)%s",
2559 rec->flags & ~FTRACE_FL_MASK,
2560 rec->flags & FTRACE_FL_REGS ? " R" : "");
2561 seq_printf(m, "\n");
2566 static const struct seq_operations show_ftrace_seq_ops = {
2574 ftrace_avail_open(struct inode *inode, struct file *file)
2576 struct ftrace_iterator *iter;
2578 if (unlikely(ftrace_disabled))
2581 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2583 iter->pg = ftrace_pages_start;
2584 iter->ops = &global_ops;
2587 return iter ? 0 : -ENOMEM;
2591 ftrace_enabled_open(struct inode *inode, struct file *file)
2593 struct ftrace_iterator *iter;
2595 if (unlikely(ftrace_disabled))
2598 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2600 iter->pg = ftrace_pages_start;
2601 iter->flags = FTRACE_ITER_ENABLED;
2602 iter->ops = &global_ops;
2605 return iter ? 0 : -ENOMEM;
2608 static void ftrace_filter_reset(struct ftrace_hash *hash)
2610 mutex_lock(&ftrace_lock);
2611 ftrace_hash_clear(hash);
2612 mutex_unlock(&ftrace_lock);
2616 * ftrace_regex_open - initialize function tracer filter files
2617 * @ops: The ftrace_ops that hold the hash filters
2618 * @flag: The type of filter to process
2619 * @inode: The inode, usually passed in to your open routine
2620 * @file: The file, usually passed in to your open routine
2622 * ftrace_regex_open() initializes the filter files for the
2623 * @ops. Depending on @flag it may process the filter hash or
2624 * the notrace hash of @ops. With this called from the open
2625 * routine, you can use ftrace_filter_write() for the write
2626 * routine if @flag has FTRACE_ITER_FILTER set, or
2627 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2628 * ftrace_filter_lseek() should be used as the lseek routine, and
2629 * release must call ftrace_regex_release().
2632 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2633 struct inode *inode, struct file *file)
2635 struct ftrace_iterator *iter;
2636 struct ftrace_hash *hash;
2639 if (unlikely(ftrace_disabled))
2642 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2646 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2651 if (flag & FTRACE_ITER_NOTRACE)
2652 hash = ops->notrace_hash;
2654 hash = ops->filter_hash;
2659 if (file->f_mode & FMODE_WRITE) {
2660 mutex_lock(&ftrace_lock);
2661 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2662 mutex_unlock(&ftrace_lock);
2665 trace_parser_put(&iter->parser);
2671 mutex_lock(&ftrace_regex_lock);
2673 if ((file->f_mode & FMODE_WRITE) &&
2674 (file->f_flags & O_TRUNC))
2675 ftrace_filter_reset(iter->hash);
2677 if (file->f_mode & FMODE_READ) {
2678 iter->pg = ftrace_pages_start;
2680 ret = seq_open(file, &show_ftrace_seq_ops);
2682 struct seq_file *m = file->private_data;
2686 free_ftrace_hash(iter->hash);
2687 trace_parser_put(&iter->parser);
2691 file->private_data = iter;
2692 mutex_unlock(&ftrace_regex_lock);
2698 ftrace_filter_open(struct inode *inode, struct file *file)
2700 return ftrace_regex_open(&global_ops,
2701 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2706 ftrace_notrace_open(struct inode *inode, struct file *file)
2708 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
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 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3446 __setup("ftrace_notrace=", set_ftrace_notrace);
3448 static int __init set_ftrace_filter(char *str)
3450 strlcpy(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)