2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
36 #include <trace/events/sched.h>
38 #include <asm/setup.h>
40 #include "trace_output.h"
41 #include "trace_stat.h"
43 #define FTRACE_WARN_ON(cond) \
51 #define FTRACE_WARN_ON_ONCE(cond) \
54 if (WARN_ON_ONCE(___r)) \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
67 static struct ftrace_ops ftrace_list_end __read_mostly = {
71 /* ftrace_enabled is a method to turn ftrace on or off */
72 int ftrace_enabled __read_mostly;
73 static int last_ftrace_enabled;
75 /* Quick disabling of function tracer. */
76 int function_trace_stop __read_mostly;
78 /* Current function tracing op */
79 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
81 /* List for set_ftrace_pid's pids. */
82 LIST_HEAD(ftrace_pids);
84 struct list_head list;
89 * ftrace_disabled is set when an anomaly is discovered.
90 * ftrace_disabled is much stronger than ftrace_enabled.
92 static int ftrace_disabled __read_mostly;
94 static DEFINE_MUTEX(ftrace_lock);
96 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
97 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
98 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
99 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
100 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
101 static struct ftrace_ops global_ops;
102 static struct ftrace_ops control_ops;
104 #if ARCH_SUPPORTS_FTRACE_OPS
105 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
106 struct ftrace_ops *op, struct pt_regs *regs);
108 /* See comment below, where ftrace_ops_list_func is defined */
109 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
110 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
114 * Traverse the ftrace_global_list, invoking all entries. The reason that we
115 * can use rcu_dereference_raw() is that elements removed from this list
116 * are simply leaked, so there is no need to interact with a grace-period
117 * mechanism. The rcu_dereference_raw() calls are needed to handle
118 * concurrent insertions into the ftrace_global_list.
120 * Silly Alpha and silly pointer-speculation compiler optimizations!
123 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
124 struct ftrace_ops *op, struct pt_regs *regs)
126 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
129 trace_recursion_set(TRACE_GLOBAL_BIT);
130 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
131 while (op != &ftrace_list_end) {
132 op->func(ip, parent_ip, op, regs);
133 op = rcu_dereference_raw(op->next); /*see above*/
135 trace_recursion_clear(TRACE_GLOBAL_BIT);
138 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
139 struct ftrace_ops *op, struct pt_regs *regs)
141 if (!test_tsk_trace_trace(current))
144 ftrace_pid_function(ip, parent_ip, op, regs);
147 static void set_ftrace_pid_function(ftrace_func_t func)
149 /* do not set ftrace_pid_function to itself! */
150 if (func != ftrace_pid_func)
151 ftrace_pid_function = func;
155 * clear_ftrace_function - reset the ftrace function
157 * This NULLs the ftrace function and in essence stops
158 * tracing. There may be lag
160 void clear_ftrace_function(void)
162 ftrace_trace_function = ftrace_stub;
163 ftrace_pid_function = ftrace_stub;
166 static void control_ops_disable_all(struct ftrace_ops *ops)
170 for_each_possible_cpu(cpu)
171 *per_cpu_ptr(ops->disabled, cpu) = 1;
174 static int control_ops_alloc(struct ftrace_ops *ops)
176 int __percpu *disabled;
178 disabled = alloc_percpu(int);
182 ops->disabled = disabled;
183 control_ops_disable_all(ops);
187 static void control_ops_free(struct ftrace_ops *ops)
189 free_percpu(ops->disabled);
192 static void update_global_ops(void)
197 * If there's only one function registered, then call that
198 * function directly. Otherwise, we need to iterate over the
199 * registered callers.
201 if (ftrace_global_list == &ftrace_list_end ||
202 ftrace_global_list->next == &ftrace_list_end)
203 func = ftrace_global_list->func;
205 func = ftrace_global_list_func;
207 /* If we filter on pids, update to use the pid function */
208 if (!list_empty(&ftrace_pids)) {
209 set_ftrace_pid_function(func);
210 func = ftrace_pid_func;
213 global_ops.func = func;
216 static void update_ftrace_function(void)
223 * If we are at the end of the list and this ops is
224 * not dynamic and the arch supports passing ops, then have the
225 * mcount trampoline call the function directly.
227 if (ftrace_ops_list == &ftrace_list_end ||
228 (ftrace_ops_list->next == &ftrace_list_end &&
229 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
230 !FTRACE_FORCE_LIST_FUNC)) {
231 /* Set the ftrace_ops that the arch callback uses */
232 if (ftrace_ops_list == &global_ops)
233 function_trace_op = ftrace_global_list;
235 function_trace_op = ftrace_ops_list;
236 func = ftrace_ops_list->func;
238 /* Just use the default ftrace_ops */
239 function_trace_op = &ftrace_list_end;
240 func = ftrace_ops_list_func;
243 ftrace_trace_function = func;
246 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
250 * We are entering ops into the list but another
251 * CPU might be walking that list. We need to make sure
252 * the ops->next pointer is valid before another CPU sees
253 * the ops pointer included into the list.
255 rcu_assign_pointer(*list, ops);
258 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
260 struct ftrace_ops **p;
263 * If we are removing the last function, then simply point
264 * to the ftrace_stub.
266 if (*list == ops && ops->next == &ftrace_list_end) {
267 *list = &ftrace_list_end;
271 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
282 static void add_ftrace_list_ops(struct ftrace_ops **list,
283 struct ftrace_ops *main_ops,
284 struct ftrace_ops *ops)
286 int first = *list == &ftrace_list_end;
287 add_ftrace_ops(list, ops);
289 add_ftrace_ops(&ftrace_ops_list, main_ops);
292 static int remove_ftrace_list_ops(struct ftrace_ops **list,
293 struct ftrace_ops *main_ops,
294 struct ftrace_ops *ops)
296 int ret = remove_ftrace_ops(list, ops);
297 if (!ret && *list == &ftrace_list_end)
298 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
302 static int __register_ftrace_function(struct ftrace_ops *ops)
304 if (unlikely(ftrace_disabled))
307 if (FTRACE_WARN_ON(ops == &global_ops))
310 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
313 /* We don't support both control and global flags set. */
314 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
317 #ifndef ARCH_SUPPORTS_FTRACE_SAVE_REGS
319 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
320 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
321 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
323 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
324 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
327 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
328 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
331 if (!core_kernel_data((unsigned long)ops))
332 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
334 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
335 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
336 ops->flags |= FTRACE_OPS_FL_ENABLED;
337 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
338 if (control_ops_alloc(ops))
340 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
342 add_ftrace_ops(&ftrace_ops_list, ops);
345 update_ftrace_function();
350 static int __unregister_ftrace_function(struct ftrace_ops *ops)
357 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
360 if (FTRACE_WARN_ON(ops == &global_ops))
363 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
364 ret = remove_ftrace_list_ops(&ftrace_global_list,
367 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
368 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
369 ret = remove_ftrace_list_ops(&ftrace_control_list,
373 * The ftrace_ops is now removed from the list,
374 * so there'll be no new users. We must ensure
375 * all current users are done before we free
379 control_ops_free(ops);
382 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
388 update_ftrace_function();
391 * Dynamic ops may be freed, we must make sure that all
392 * callers are done before leaving this function.
394 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
400 static void ftrace_update_pid_func(void)
402 /* Only do something if we are tracing something */
403 if (ftrace_trace_function == ftrace_stub)
406 update_ftrace_function();
409 #ifdef CONFIG_FUNCTION_PROFILER
410 struct ftrace_profile {
411 struct hlist_node node;
413 unsigned long counter;
414 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
415 unsigned long long time;
416 unsigned long long time_squared;
420 struct ftrace_profile_page {
421 struct ftrace_profile_page *next;
423 struct ftrace_profile records[];
426 struct ftrace_profile_stat {
428 struct hlist_head *hash;
429 struct ftrace_profile_page *pages;
430 struct ftrace_profile_page *start;
431 struct tracer_stat stat;
434 #define PROFILE_RECORDS_SIZE \
435 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
437 #define PROFILES_PER_PAGE \
438 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
440 static int ftrace_profile_bits __read_mostly;
441 static int ftrace_profile_enabled __read_mostly;
443 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
444 static DEFINE_MUTEX(ftrace_profile_lock);
446 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
448 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
451 function_stat_next(void *v, int idx)
453 struct ftrace_profile *rec = v;
454 struct ftrace_profile_page *pg;
456 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
462 if ((void *)rec >= (void *)&pg->records[pg->index]) {
466 rec = &pg->records[0];
474 static void *function_stat_start(struct tracer_stat *trace)
476 struct ftrace_profile_stat *stat =
477 container_of(trace, struct ftrace_profile_stat, stat);
479 if (!stat || !stat->start)
482 return function_stat_next(&stat->start->records[0], 0);
485 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
486 /* function graph compares on total time */
487 static int function_stat_cmp(void *p1, void *p2)
489 struct ftrace_profile *a = p1;
490 struct ftrace_profile *b = p2;
492 if (a->time < b->time)
494 if (a->time > b->time)
500 /* not function graph compares against hits */
501 static int function_stat_cmp(void *p1, void *p2)
503 struct ftrace_profile *a = p1;
504 struct ftrace_profile *b = p2;
506 if (a->counter < b->counter)
508 if (a->counter > b->counter)
515 static int function_stat_headers(struct seq_file *m)
517 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
518 seq_printf(m, " Function "
521 "--- ---- --- ---\n");
523 seq_printf(m, " Function Hit\n"
529 static int function_stat_show(struct seq_file *m, void *v)
531 struct ftrace_profile *rec = v;
532 char str[KSYM_SYMBOL_LEN];
534 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
535 static struct trace_seq s;
536 unsigned long long avg;
537 unsigned long long stddev;
539 mutex_lock(&ftrace_profile_lock);
541 /* we raced with function_profile_reset() */
542 if (unlikely(rec->counter == 0)) {
547 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
548 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
550 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
553 do_div(avg, rec->counter);
555 /* Sample standard deviation (s^2) */
556 if (rec->counter <= 1)
559 stddev = rec->time_squared - rec->counter * avg * avg;
561 * Divide only 1000 for ns^2 -> us^2 conversion.
562 * trace_print_graph_duration will divide 1000 again.
564 do_div(stddev, (rec->counter - 1) * 1000);
568 trace_print_graph_duration(rec->time, &s);
569 trace_seq_puts(&s, " ");
570 trace_print_graph_duration(avg, &s);
571 trace_seq_puts(&s, " ");
572 trace_print_graph_duration(stddev, &s);
573 trace_print_seq(m, &s);
577 mutex_unlock(&ftrace_profile_lock);
582 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
584 struct ftrace_profile_page *pg;
586 pg = stat->pages = stat->start;
589 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
594 memset(stat->hash, 0,
595 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
598 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
600 struct ftrace_profile_page *pg;
605 /* If we already allocated, do nothing */
609 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
613 #ifdef CONFIG_DYNAMIC_FTRACE
614 functions = ftrace_update_tot_cnt;
617 * We do not know the number of functions that exist because
618 * dynamic tracing is what counts them. With past experience
619 * we have around 20K functions. That should be more than enough.
620 * It is highly unlikely we will execute every function in
626 pg = stat->start = stat->pages;
628 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
630 for (i = 0; i < pages; i++) {
631 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
642 unsigned long tmp = (unsigned long)pg;
648 free_page((unsigned long)stat->pages);
655 static int ftrace_profile_init_cpu(int cpu)
657 struct ftrace_profile_stat *stat;
660 stat = &per_cpu(ftrace_profile_stats, cpu);
663 /* If the profile is already created, simply reset it */
664 ftrace_profile_reset(stat);
669 * We are profiling all functions, but usually only a few thousand
670 * functions are hit. We'll make a hash of 1024 items.
672 size = FTRACE_PROFILE_HASH_SIZE;
674 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
679 if (!ftrace_profile_bits) {
682 for (; size; size >>= 1)
683 ftrace_profile_bits++;
686 /* Preallocate the function profiling pages */
687 if (ftrace_profile_pages_init(stat) < 0) {
696 static int ftrace_profile_init(void)
701 for_each_online_cpu(cpu) {
702 ret = ftrace_profile_init_cpu(cpu);
710 /* interrupts must be disabled */
711 static struct ftrace_profile *
712 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
714 struct ftrace_profile *rec;
715 struct hlist_head *hhd;
716 struct hlist_node *n;
719 key = hash_long(ip, ftrace_profile_bits);
720 hhd = &stat->hash[key];
722 if (hlist_empty(hhd))
725 hlist_for_each_entry_rcu(rec, n, hhd, node) {
733 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
734 struct ftrace_profile *rec)
738 key = hash_long(rec->ip, ftrace_profile_bits);
739 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
743 * The memory is already allocated, this simply finds a new record to use.
745 static struct ftrace_profile *
746 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
748 struct ftrace_profile *rec = NULL;
750 /* prevent recursion (from NMIs) */
751 if (atomic_inc_return(&stat->disabled) != 1)
755 * Try to find the function again since an NMI
756 * could have added it
758 rec = ftrace_find_profiled_func(stat, ip);
762 if (stat->pages->index == PROFILES_PER_PAGE) {
763 if (!stat->pages->next)
765 stat->pages = stat->pages->next;
768 rec = &stat->pages->records[stat->pages->index++];
770 ftrace_add_profile(stat, rec);
773 atomic_dec(&stat->disabled);
779 function_profile_call(unsigned long ip, unsigned long parent_ip,
780 struct ftrace_ops *ops, struct pt_regs *regs)
782 struct ftrace_profile_stat *stat;
783 struct ftrace_profile *rec;
786 if (!ftrace_profile_enabled)
789 local_irq_save(flags);
791 stat = &__get_cpu_var(ftrace_profile_stats);
792 if (!stat->hash || !ftrace_profile_enabled)
795 rec = ftrace_find_profiled_func(stat, ip);
797 rec = ftrace_profile_alloc(stat, ip);
804 local_irq_restore(flags);
807 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
808 static int profile_graph_entry(struct ftrace_graph_ent *trace)
810 function_profile_call(trace->func, 0, NULL, NULL);
814 static void profile_graph_return(struct ftrace_graph_ret *trace)
816 struct ftrace_profile_stat *stat;
817 unsigned long long calltime;
818 struct ftrace_profile *rec;
821 local_irq_save(flags);
822 stat = &__get_cpu_var(ftrace_profile_stats);
823 if (!stat->hash || !ftrace_profile_enabled)
826 /* If the calltime was zero'd ignore it */
827 if (!trace->calltime)
830 calltime = trace->rettime - trace->calltime;
832 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
835 index = trace->depth;
837 /* Append this call time to the parent time to subtract */
839 current->ret_stack[index - 1].subtime += calltime;
841 if (current->ret_stack[index].subtime < calltime)
842 calltime -= current->ret_stack[index].subtime;
847 rec = ftrace_find_profiled_func(stat, trace->func);
849 rec->time += calltime;
850 rec->time_squared += calltime * calltime;
854 local_irq_restore(flags);
857 static int register_ftrace_profiler(void)
859 return register_ftrace_graph(&profile_graph_return,
860 &profile_graph_entry);
863 static void unregister_ftrace_profiler(void)
865 unregister_ftrace_graph();
868 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
869 .func = function_profile_call,
872 static int register_ftrace_profiler(void)
874 return register_ftrace_function(&ftrace_profile_ops);
877 static void unregister_ftrace_profiler(void)
879 unregister_ftrace_function(&ftrace_profile_ops);
881 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
884 ftrace_profile_write(struct file *filp, const char __user *ubuf,
885 size_t cnt, loff_t *ppos)
890 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
896 mutex_lock(&ftrace_profile_lock);
897 if (ftrace_profile_enabled ^ val) {
899 ret = ftrace_profile_init();
905 ret = register_ftrace_profiler();
910 ftrace_profile_enabled = 1;
912 ftrace_profile_enabled = 0;
914 * unregister_ftrace_profiler calls stop_machine
915 * so this acts like an synchronize_sched.
917 unregister_ftrace_profiler();
921 mutex_unlock(&ftrace_profile_lock);
929 ftrace_profile_read(struct file *filp, char __user *ubuf,
930 size_t cnt, loff_t *ppos)
932 char buf[64]; /* big enough to hold a number */
935 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
936 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
939 static const struct file_operations ftrace_profile_fops = {
940 .open = tracing_open_generic,
941 .read = ftrace_profile_read,
942 .write = ftrace_profile_write,
943 .llseek = default_llseek,
946 /* used to initialize the real stat files */
947 static struct tracer_stat function_stats __initdata = {
949 .stat_start = function_stat_start,
950 .stat_next = function_stat_next,
951 .stat_cmp = function_stat_cmp,
952 .stat_headers = function_stat_headers,
953 .stat_show = function_stat_show
956 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
958 struct ftrace_profile_stat *stat;
959 struct dentry *entry;
964 for_each_possible_cpu(cpu) {
965 stat = &per_cpu(ftrace_profile_stats, cpu);
967 /* allocate enough for function name + cpu number */
968 name = kmalloc(32, GFP_KERNEL);
971 * The files created are permanent, if something happens
972 * we still do not free memory.
975 "Could not allocate stat file for cpu %d\n",
979 stat->stat = function_stats;
980 snprintf(name, 32, "function%d", cpu);
981 stat->stat.name = name;
982 ret = register_stat_tracer(&stat->stat);
985 "Could not register function stat for cpu %d\n",
992 entry = debugfs_create_file("function_profile_enabled", 0644,
993 d_tracer, NULL, &ftrace_profile_fops);
995 pr_warning("Could not create debugfs "
996 "'function_profile_enabled' entry\n");
999 #else /* CONFIG_FUNCTION_PROFILER */
1000 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1003 #endif /* CONFIG_FUNCTION_PROFILER */
1005 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1007 #ifdef CONFIG_DYNAMIC_FTRACE
1009 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1010 # error Dynamic ftrace depends on MCOUNT_RECORD
1013 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1015 struct ftrace_func_probe {
1016 struct hlist_node node;
1017 struct ftrace_probe_ops *ops;
1018 unsigned long flags;
1021 struct rcu_head rcu;
1024 struct ftrace_func_entry {
1025 struct hlist_node hlist;
1029 struct ftrace_hash {
1030 unsigned long size_bits;
1031 struct hlist_head *buckets;
1032 unsigned long count;
1033 struct rcu_head rcu;
1037 * We make these constant because no one should touch them,
1038 * but they are used as the default "empty hash", to avoid allocating
1039 * it all the time. These are in a read only section such that if
1040 * anyone does try to modify it, it will cause an exception.
1042 static const struct hlist_head empty_buckets[1];
1043 static const struct ftrace_hash empty_hash = {
1044 .buckets = (struct hlist_head *)empty_buckets,
1046 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1048 static struct ftrace_ops global_ops = {
1049 .func = ftrace_stub,
1050 .notrace_hash = EMPTY_HASH,
1051 .filter_hash = EMPTY_HASH,
1054 static DEFINE_MUTEX(ftrace_regex_lock);
1056 struct ftrace_page {
1057 struct ftrace_page *next;
1058 struct dyn_ftrace *records;
1063 static struct ftrace_page *ftrace_new_pgs;
1065 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1066 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1068 /* estimate from running different kernels */
1069 #define NR_TO_INIT 10000
1071 static struct ftrace_page *ftrace_pages_start;
1072 static struct ftrace_page *ftrace_pages;
1074 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1076 return !hash || !hash->count;
1079 static struct ftrace_func_entry *
1080 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1083 struct ftrace_func_entry *entry;
1084 struct hlist_head *hhd;
1085 struct hlist_node *n;
1087 if (ftrace_hash_empty(hash))
1090 if (hash->size_bits > 0)
1091 key = hash_long(ip, hash->size_bits);
1095 hhd = &hash->buckets[key];
1097 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1098 if (entry->ip == ip)
1104 static void __add_hash_entry(struct ftrace_hash *hash,
1105 struct ftrace_func_entry *entry)
1107 struct hlist_head *hhd;
1110 if (hash->size_bits)
1111 key = hash_long(entry->ip, hash->size_bits);
1115 hhd = &hash->buckets[key];
1116 hlist_add_head(&entry->hlist, hhd);
1120 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1122 struct ftrace_func_entry *entry;
1124 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1129 __add_hash_entry(hash, entry);
1135 free_hash_entry(struct ftrace_hash *hash,
1136 struct ftrace_func_entry *entry)
1138 hlist_del(&entry->hlist);
1144 remove_hash_entry(struct ftrace_hash *hash,
1145 struct ftrace_func_entry *entry)
1147 hlist_del(&entry->hlist);
1151 static void ftrace_hash_clear(struct ftrace_hash *hash)
1153 struct hlist_head *hhd;
1154 struct hlist_node *tp, *tn;
1155 struct ftrace_func_entry *entry;
1156 int size = 1 << hash->size_bits;
1162 for (i = 0; i < size; i++) {
1163 hhd = &hash->buckets[i];
1164 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1165 free_hash_entry(hash, entry);
1167 FTRACE_WARN_ON(hash->count);
1170 static void free_ftrace_hash(struct ftrace_hash *hash)
1172 if (!hash || hash == EMPTY_HASH)
1174 ftrace_hash_clear(hash);
1175 kfree(hash->buckets);
1179 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1181 struct ftrace_hash *hash;
1183 hash = container_of(rcu, struct ftrace_hash, rcu);
1184 free_ftrace_hash(hash);
1187 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1189 if (!hash || hash == EMPTY_HASH)
1191 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1194 void ftrace_free_filter(struct ftrace_ops *ops)
1196 free_ftrace_hash(ops->filter_hash);
1197 free_ftrace_hash(ops->notrace_hash);
1200 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1202 struct ftrace_hash *hash;
1205 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1209 size = 1 << size_bits;
1210 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1212 if (!hash->buckets) {
1217 hash->size_bits = size_bits;
1222 static struct ftrace_hash *
1223 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1225 struct ftrace_func_entry *entry;
1226 struct ftrace_hash *new_hash;
1227 struct hlist_node *tp;
1232 new_hash = alloc_ftrace_hash(size_bits);
1237 if (ftrace_hash_empty(hash))
1240 size = 1 << hash->size_bits;
1241 for (i = 0; i < size; i++) {
1242 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1243 ret = add_hash_entry(new_hash, entry->ip);
1249 FTRACE_WARN_ON(new_hash->count != hash->count);
1254 free_ftrace_hash(new_hash);
1259 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1261 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1264 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1265 struct ftrace_hash **dst, struct ftrace_hash *src)
1267 struct ftrace_func_entry *entry;
1268 struct hlist_node *tp, *tn;
1269 struct hlist_head *hhd;
1270 struct ftrace_hash *old_hash;
1271 struct ftrace_hash *new_hash;
1273 int size = src->count;
1279 * Remove the current set, update the hash and add
1282 ftrace_hash_rec_disable(ops, enable);
1285 * If the new source is empty, just free dst and assign it
1289 free_ftrace_hash_rcu(*dst);
1290 rcu_assign_pointer(*dst, EMPTY_HASH);
1291 /* still need to update the function records */
1297 * Make the hash size about 1/2 the # found
1299 for (size /= 2; size; size >>= 1)
1302 /* Don't allocate too much */
1303 if (bits > FTRACE_HASH_MAX_BITS)
1304 bits = FTRACE_HASH_MAX_BITS;
1307 new_hash = alloc_ftrace_hash(bits);
1311 size = 1 << src->size_bits;
1312 for (i = 0; i < size; i++) {
1313 hhd = &src->buckets[i];
1314 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1316 key = hash_long(entry->ip, bits);
1319 remove_hash_entry(src, entry);
1320 __add_hash_entry(new_hash, entry);
1325 rcu_assign_pointer(*dst, new_hash);
1326 free_ftrace_hash_rcu(old_hash);
1331 * Enable regardless of ret:
1332 * On success, we enable the new hash.
1333 * On failure, we re-enable the original hash.
1335 ftrace_hash_rec_enable(ops, enable);
1341 * Test the hashes for this ops to see if we want to call
1342 * the ops->func or not.
1344 * It's a match if the ip is in the ops->filter_hash or
1345 * the filter_hash does not exist or is empty,
1347 * the ip is not in the ops->notrace_hash.
1349 * This needs to be called with preemption disabled as
1350 * the hashes are freed with call_rcu_sched().
1353 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1355 struct ftrace_hash *filter_hash;
1356 struct ftrace_hash *notrace_hash;
1359 filter_hash = rcu_dereference_raw(ops->filter_hash);
1360 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1362 if ((ftrace_hash_empty(filter_hash) ||
1363 ftrace_lookup_ip(filter_hash, ip)) &&
1364 (ftrace_hash_empty(notrace_hash) ||
1365 !ftrace_lookup_ip(notrace_hash, ip)))
1374 * This is a double for. Do not use 'break' to break out of the loop,
1375 * you must use a goto.
1377 #define do_for_each_ftrace_rec(pg, rec) \
1378 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1380 for (_____i = 0; _____i < pg->index; _____i++) { \
1381 rec = &pg->records[_____i];
1383 #define while_for_each_ftrace_rec() \
1388 static int ftrace_cmp_recs(const void *a, const void *b)
1390 const struct dyn_ftrace *key = a;
1391 const struct dyn_ftrace *rec = b;
1393 if (key->flags < rec->ip)
1395 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1400 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1402 struct ftrace_page *pg;
1403 struct dyn_ftrace *rec;
1404 struct dyn_ftrace key;
1407 key.flags = end; /* overload flags, as it is unsigned long */
1409 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1410 if (end < pg->records[0].ip ||
1411 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1413 rec = bsearch(&key, pg->records, pg->index,
1414 sizeof(struct dyn_ftrace),
1424 * ftrace_location - return true if the ip giving is a traced location
1425 * @ip: the instruction pointer to check
1427 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1428 * That is, the instruction that is either a NOP or call to
1429 * the function tracer. It checks the ftrace internal tables to
1430 * determine if the address belongs or not.
1432 unsigned long ftrace_location(unsigned long ip)
1434 return ftrace_location_range(ip, ip);
1438 * ftrace_text_reserved - return true if range contains an ftrace location
1439 * @start: start of range to search
1440 * @end: end of range to search (inclusive). @end points to the last byte to check.
1442 * Returns 1 if @start and @end contains a ftrace location.
1443 * That is, the instruction that is either a NOP or call to
1444 * the function tracer. It checks the ftrace internal tables to
1445 * determine if the address belongs or not.
1447 int ftrace_text_reserved(void *start, void *end)
1451 ret = ftrace_location_range((unsigned long)start,
1452 (unsigned long)end);
1457 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1461 struct ftrace_hash *hash;
1462 struct ftrace_hash *other_hash;
1463 struct ftrace_page *pg;
1464 struct dyn_ftrace *rec;
1468 /* Only update if the ops has been registered */
1469 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1473 * In the filter_hash case:
1474 * If the count is zero, we update all records.
1475 * Otherwise we just update the items in the hash.
1477 * In the notrace_hash case:
1478 * We enable the update in the hash.
1479 * As disabling notrace means enabling the tracing,
1480 * and enabling notrace means disabling, the inc variable
1484 hash = ops->filter_hash;
1485 other_hash = ops->notrace_hash;
1486 if (ftrace_hash_empty(hash))
1490 hash = ops->notrace_hash;
1491 other_hash = ops->filter_hash;
1493 * If the notrace hash has no items,
1494 * then there's nothing to do.
1496 if (ftrace_hash_empty(hash))
1500 do_for_each_ftrace_rec(pg, rec) {
1501 int in_other_hash = 0;
1507 * Only the filter_hash affects all records.
1508 * Update if the record is not in the notrace hash.
1510 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1513 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1514 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1519 if (filter_hash && in_hash && !in_other_hash)
1521 else if (!filter_hash && in_hash &&
1522 (in_other_hash || ftrace_hash_empty(other_hash)))
1530 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1533 * If any ops wants regs saved for this function
1534 * then all ops will get saved regs.
1536 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1537 rec->flags |= FTRACE_FL_REGS;
1539 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1544 /* Shortcut, if we handled all records, we are done. */
1545 if (!all && count == hash->count)
1547 } while_for_each_ftrace_rec();
1550 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1553 __ftrace_hash_rec_update(ops, filter_hash, 0);
1556 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1559 __ftrace_hash_rec_update(ops, filter_hash, 1);
1562 static void print_ip_ins(const char *fmt, unsigned char *p)
1566 printk(KERN_CONT "%s", fmt);
1568 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1569 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1573 * ftrace_bug - report and shutdown function tracer
1574 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1575 * @ip: The address that failed
1577 * The arch code that enables or disables the function tracing
1578 * can call ftrace_bug() when it has detected a problem in
1579 * modifying the code. @failed should be one of either:
1580 * EFAULT - if the problem happens on reading the @ip address
1581 * EINVAL - if what is read at @ip is not what was expected
1582 * EPERM - if the problem happens on writting to the @ip address
1584 void ftrace_bug(int failed, unsigned long ip)
1588 FTRACE_WARN_ON_ONCE(1);
1589 pr_info("ftrace faulted on modifying ");
1593 FTRACE_WARN_ON_ONCE(1);
1594 pr_info("ftrace failed to modify ");
1596 print_ip_ins(" actual: ", (unsigned char *)ip);
1597 printk(KERN_CONT "\n");
1600 FTRACE_WARN_ON_ONCE(1);
1601 pr_info("ftrace faulted on writing ");
1605 FTRACE_WARN_ON_ONCE(1);
1606 pr_info("ftrace faulted on unknown error ");
1611 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1613 unsigned long flag = 0UL;
1616 * If we are updating calls:
1618 * If the record has a ref count, then we need to enable it
1619 * because someone is using it.
1621 * Otherwise we make sure its disabled.
1623 * If we are disabling calls, then disable all records that
1626 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1627 flag = FTRACE_FL_ENABLED;
1630 * If enabling and the REGS flag does not match the REGS_EN, then
1631 * do not ignore this record. Set flags to fail the compare against
1635 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1636 flag |= FTRACE_FL_REGS;
1638 /* If the state of this record hasn't changed, then do nothing */
1639 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1640 return FTRACE_UPDATE_IGNORE;
1643 /* Save off if rec is being enabled (for return value) */
1644 flag ^= rec->flags & FTRACE_FL_ENABLED;
1647 rec->flags |= FTRACE_FL_ENABLED;
1648 if (flag & FTRACE_FL_REGS) {
1649 if (rec->flags & FTRACE_FL_REGS)
1650 rec->flags |= FTRACE_FL_REGS_EN;
1652 rec->flags &= ~FTRACE_FL_REGS_EN;
1657 * If this record is being updated from a nop, then
1658 * return UPDATE_MAKE_CALL.
1659 * Otherwise, if the EN flag is set, then return
1660 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1661 * from the non-save regs, to a save regs function.
1663 * return UPDATE_MODIFY_CALL to tell the caller to convert
1664 * from the save regs, to a non-save regs function.
1666 if (flag & FTRACE_FL_ENABLED)
1667 return FTRACE_UPDATE_MAKE_CALL;
1668 else if (rec->flags & FTRACE_FL_REGS_EN)
1669 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1671 return FTRACE_UPDATE_MODIFY_CALL;
1675 /* If there's no more users, clear all flags */
1676 if (!(rec->flags & ~FTRACE_FL_MASK))
1679 /* Just disable the record (keep REGS state) */
1680 rec->flags &= ~FTRACE_FL_ENABLED;
1683 return FTRACE_UPDATE_MAKE_NOP;
1687 * ftrace_update_record, set a record that now is tracing or not
1688 * @rec: the record to update
1689 * @enable: set to 1 if the record is tracing, zero to force disable
1691 * The records that represent all functions that can be traced need
1692 * to be updated when tracing has been enabled.
1694 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1696 return ftrace_check_record(rec, enable, 1);
1700 * ftrace_test_record, check if the record has been enabled or not
1701 * @rec: the record to test
1702 * @enable: set to 1 to check if enabled, 0 if it is disabled
1704 * The arch code may need to test if a record is already set to
1705 * tracing to determine how to modify the function code that it
1708 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1710 return ftrace_check_record(rec, enable, 0);
1714 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1716 unsigned long ftrace_old_addr;
1717 unsigned long ftrace_addr;
1720 ret = ftrace_update_record(rec, enable);
1722 if (rec->flags & FTRACE_FL_REGS)
1723 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1725 ftrace_addr = (unsigned long)FTRACE_ADDR;
1728 case FTRACE_UPDATE_IGNORE:
1731 case FTRACE_UPDATE_MAKE_CALL:
1732 return ftrace_make_call(rec, ftrace_addr);
1734 case FTRACE_UPDATE_MAKE_NOP:
1735 return ftrace_make_nop(NULL, rec, ftrace_addr);
1737 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1738 case FTRACE_UPDATE_MODIFY_CALL:
1739 if (rec->flags & FTRACE_FL_REGS)
1740 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1742 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1744 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1747 return -1; /* unknow ftrace bug */
1750 void __weak ftrace_replace_code(int enable)
1752 struct dyn_ftrace *rec;
1753 struct ftrace_page *pg;
1756 if (unlikely(ftrace_disabled))
1759 do_for_each_ftrace_rec(pg, rec) {
1760 failed = __ftrace_replace_code(rec, enable);
1762 ftrace_bug(failed, rec->ip);
1763 /* Stop processing */
1766 } while_for_each_ftrace_rec();
1769 struct ftrace_rec_iter {
1770 struct ftrace_page *pg;
1775 * ftrace_rec_iter_start, start up iterating over traced functions
1777 * Returns an iterator handle that is used to iterate over all
1778 * the records that represent address locations where functions
1781 * May return NULL if no records are available.
1783 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1786 * We only use a single iterator.
1787 * Protected by the ftrace_lock mutex.
1789 static struct ftrace_rec_iter ftrace_rec_iter;
1790 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1792 iter->pg = ftrace_pages_start;
1795 /* Could have empty pages */
1796 while (iter->pg && !iter->pg->index)
1797 iter->pg = iter->pg->next;
1806 * ftrace_rec_iter_next, get the next record to process.
1807 * @iter: The handle to the iterator.
1809 * Returns the next iterator after the given iterator @iter.
1811 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1815 if (iter->index >= iter->pg->index) {
1816 iter->pg = iter->pg->next;
1819 /* Could have empty pages */
1820 while (iter->pg && !iter->pg->index)
1821 iter->pg = iter->pg->next;
1831 * ftrace_rec_iter_record, get the record at the iterator location
1832 * @iter: The current iterator location
1834 * Returns the record that the current @iter is at.
1836 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1838 return &iter->pg->records[iter->index];
1842 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1849 if (unlikely(ftrace_disabled))
1852 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1854 ftrace_bug(ret, ip);
1861 * archs can override this function if they must do something
1862 * before the modifying code is performed.
1864 int __weak ftrace_arch_code_modify_prepare(void)
1870 * archs can override this function if they must do something
1871 * after the modifying code is performed.
1873 int __weak ftrace_arch_code_modify_post_process(void)
1878 void ftrace_modify_all_code(int command)
1880 if (command & FTRACE_UPDATE_CALLS)
1881 ftrace_replace_code(1);
1882 else if (command & FTRACE_DISABLE_CALLS)
1883 ftrace_replace_code(0);
1885 if (command & FTRACE_UPDATE_TRACE_FUNC)
1886 ftrace_update_ftrace_func(ftrace_trace_function);
1888 if (command & FTRACE_START_FUNC_RET)
1889 ftrace_enable_ftrace_graph_caller();
1890 else if (command & FTRACE_STOP_FUNC_RET)
1891 ftrace_disable_ftrace_graph_caller();
1894 static int __ftrace_modify_code(void *data)
1896 int *command = data;
1898 ftrace_modify_all_code(*command);
1904 * ftrace_run_stop_machine, go back to the stop machine method
1905 * @command: The command to tell ftrace what to do
1907 * If an arch needs to fall back to the stop machine method, the
1908 * it can call this function.
1910 void ftrace_run_stop_machine(int command)
1912 stop_machine(__ftrace_modify_code, &command, NULL);
1916 * arch_ftrace_update_code, modify the code to trace or not trace
1917 * @command: The command that needs to be done
1919 * Archs can override this function if it does not need to
1920 * run stop_machine() to modify code.
1922 void __weak arch_ftrace_update_code(int command)
1924 ftrace_run_stop_machine(command);
1927 static void ftrace_run_update_code(int command)
1931 ret = ftrace_arch_code_modify_prepare();
1932 FTRACE_WARN_ON(ret);
1936 * Do not call function tracer while we update the code.
1937 * We are in stop machine.
1939 function_trace_stop++;
1942 * By default we use stop_machine() to modify the code.
1943 * But archs can do what ever they want as long as it
1944 * is safe. The stop_machine() is the safest, but also
1945 * produces the most overhead.
1947 arch_ftrace_update_code(command);
1949 function_trace_stop--;
1951 ret = ftrace_arch_code_modify_post_process();
1952 FTRACE_WARN_ON(ret);
1955 static ftrace_func_t saved_ftrace_func;
1956 static int ftrace_start_up;
1957 static int global_start_up;
1959 static void ftrace_startup_enable(int command)
1961 if (saved_ftrace_func != ftrace_trace_function) {
1962 saved_ftrace_func = ftrace_trace_function;
1963 command |= FTRACE_UPDATE_TRACE_FUNC;
1966 if (!command || !ftrace_enabled)
1969 ftrace_run_update_code(command);
1972 static int ftrace_startup(struct ftrace_ops *ops, int command)
1974 bool hash_enable = true;
1976 if (unlikely(ftrace_disabled))
1980 command |= FTRACE_UPDATE_CALLS;
1982 /* ops marked global share the filter hashes */
1983 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1985 /* Don't update hash if global is already set */
1986 if (global_start_up)
1987 hash_enable = false;
1991 ops->flags |= FTRACE_OPS_FL_ENABLED;
1993 ftrace_hash_rec_enable(ops, 1);
1995 ftrace_startup_enable(command);
2000 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
2002 bool hash_disable = true;
2004 if (unlikely(ftrace_disabled))
2009 * Just warn in case of unbalance, no need to kill ftrace, it's not
2010 * critical but the ftrace_call callers may be never nopped again after
2011 * further ftrace uses.
2013 WARN_ON_ONCE(ftrace_start_up < 0);
2015 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2018 WARN_ON_ONCE(global_start_up < 0);
2019 /* Don't update hash if global still has users */
2020 if (global_start_up) {
2021 WARN_ON_ONCE(!ftrace_start_up);
2022 hash_disable = false;
2027 ftrace_hash_rec_disable(ops, 1);
2029 if (ops != &global_ops || !global_start_up)
2030 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2032 command |= FTRACE_UPDATE_CALLS;
2034 if (saved_ftrace_func != ftrace_trace_function) {
2035 saved_ftrace_func = ftrace_trace_function;
2036 command |= FTRACE_UPDATE_TRACE_FUNC;
2039 if (!command || !ftrace_enabled)
2042 ftrace_run_update_code(command);
2045 static void ftrace_startup_sysctl(void)
2047 if (unlikely(ftrace_disabled))
2050 /* Force update next time */
2051 saved_ftrace_func = NULL;
2052 /* ftrace_start_up is true if we want ftrace running */
2053 if (ftrace_start_up)
2054 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2057 static void ftrace_shutdown_sysctl(void)
2059 if (unlikely(ftrace_disabled))
2062 /* ftrace_start_up is true if ftrace is running */
2063 if (ftrace_start_up)
2064 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2067 static cycle_t ftrace_update_time;
2068 static unsigned long ftrace_update_cnt;
2069 unsigned long ftrace_update_tot_cnt;
2071 static int ops_traces_mod(struct ftrace_ops *ops)
2073 struct ftrace_hash *hash;
2075 hash = ops->filter_hash;
2076 return ftrace_hash_empty(hash);
2079 static int ftrace_update_code(struct module *mod)
2081 struct ftrace_page *pg;
2082 struct dyn_ftrace *p;
2083 cycle_t start, stop;
2084 unsigned long ref = 0;
2088 * When adding a module, we need to check if tracers are
2089 * currently enabled and if they are set to trace all functions.
2090 * If they are, we need to enable the module functions as well
2091 * as update the reference counts for those function records.
2094 struct ftrace_ops *ops;
2096 for (ops = ftrace_ops_list;
2097 ops != &ftrace_list_end; ops = ops->next) {
2098 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2099 ops_traces_mod(ops))
2104 start = ftrace_now(raw_smp_processor_id());
2105 ftrace_update_cnt = 0;
2107 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2109 for (i = 0; i < pg->index; i++) {
2110 /* If something went wrong, bail without enabling anything */
2111 if (unlikely(ftrace_disabled))
2114 p = &pg->records[i];
2118 * Do the initial record conversion from mcount jump
2119 * to the NOP instructions.
2121 if (!ftrace_code_disable(mod, p))
2124 ftrace_update_cnt++;
2127 * If the tracing is enabled, go ahead and enable the record.
2129 * The reason not to enable the record immediatelly is the
2130 * inherent check of ftrace_make_nop/ftrace_make_call for
2131 * correct previous instructions. Making first the NOP
2132 * conversion puts the module to the correct state, thus
2133 * passing the ftrace_make_call check.
2135 if (ftrace_start_up && ref) {
2136 int failed = __ftrace_replace_code(p, 1);
2138 ftrace_bug(failed, p->ip);
2143 ftrace_new_pgs = NULL;
2145 stop = ftrace_now(raw_smp_processor_id());
2146 ftrace_update_time = stop - start;
2147 ftrace_update_tot_cnt += ftrace_update_cnt;
2152 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2157 if (WARN_ON(!count))
2160 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2163 * We want to fill as much as possible. No more than a page
2166 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2170 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2173 /* if we can't allocate this size, try something smaller */
2180 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2189 static struct ftrace_page *
2190 ftrace_allocate_pages(unsigned long num_to_init)
2192 struct ftrace_page *start_pg;
2193 struct ftrace_page *pg;
2200 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2205 * Try to allocate as much as possible in one continues
2206 * location that fills in all of the space. We want to
2207 * waste as little space as possible.
2210 cnt = ftrace_allocate_records(pg, num_to_init);
2218 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2229 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2230 free_pages((unsigned long)pg->records, order);
2231 start_pg = pg->next;
2235 pr_info("ftrace: FAILED to allocate memory for functions\n");
2239 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2244 pr_info("ftrace: No functions to be traced?\n");
2248 cnt = num_to_init / ENTRIES_PER_PAGE;
2249 pr_info("ftrace: allocating %ld entries in %d pages\n",
2250 num_to_init, cnt + 1);
2255 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2257 struct ftrace_iterator {
2260 struct ftrace_page *pg;
2261 struct dyn_ftrace *func;
2262 struct ftrace_func_probe *probe;
2263 struct trace_parser parser;
2264 struct ftrace_hash *hash;
2265 struct ftrace_ops *ops;
2272 t_hash_next(struct seq_file *m, loff_t *pos)
2274 struct ftrace_iterator *iter = m->private;
2275 struct hlist_node *hnd = NULL;
2276 struct hlist_head *hhd;
2282 hnd = &iter->probe->node;
2284 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2287 hhd = &ftrace_func_hash[iter->hidx];
2289 if (hlist_empty(hhd)) {
2305 if (WARN_ON_ONCE(!hnd))
2308 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2313 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2315 struct ftrace_iterator *iter = m->private;
2319 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2322 if (iter->func_pos > *pos)
2326 for (l = 0; l <= (*pos - iter->func_pos); ) {
2327 p = t_hash_next(m, &l);
2334 /* Only set this if we have an item */
2335 iter->flags |= FTRACE_ITER_HASH;
2341 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2343 struct ftrace_func_probe *rec;
2346 if (WARN_ON_ONCE(!rec))
2349 if (rec->ops->print)
2350 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2352 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2355 seq_printf(m, ":%p", rec->data);
2362 t_next(struct seq_file *m, void *v, loff_t *pos)
2364 struct ftrace_iterator *iter = m->private;
2365 struct ftrace_ops *ops = iter->ops;
2366 struct dyn_ftrace *rec = NULL;
2368 if (unlikely(ftrace_disabled))
2371 if (iter->flags & FTRACE_ITER_HASH)
2372 return t_hash_next(m, pos);
2375 iter->pos = iter->func_pos = *pos;
2377 if (iter->flags & FTRACE_ITER_PRINTALL)
2378 return t_hash_start(m, pos);
2381 if (iter->idx >= iter->pg->index) {
2382 if (iter->pg->next) {
2383 iter->pg = iter->pg->next;
2388 rec = &iter->pg->records[iter->idx++];
2389 if (((iter->flags & FTRACE_ITER_FILTER) &&
2390 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2392 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2393 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2395 ((iter->flags & FTRACE_ITER_ENABLED) &&
2396 !(rec->flags & ~FTRACE_FL_MASK))) {
2404 return t_hash_start(m, pos);
2411 static void reset_iter_read(struct ftrace_iterator *iter)
2415 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
2418 static void *t_start(struct seq_file *m, loff_t *pos)
2420 struct ftrace_iterator *iter = m->private;
2421 struct ftrace_ops *ops = iter->ops;
2425 mutex_lock(&ftrace_lock);
2427 if (unlikely(ftrace_disabled))
2431 * If an lseek was done, then reset and start from beginning.
2433 if (*pos < iter->pos)
2434 reset_iter_read(iter);
2437 * For set_ftrace_filter reading, if we have the filter
2438 * off, we can short cut and just print out that all
2439 * functions are enabled.
2441 if (iter->flags & FTRACE_ITER_FILTER &&
2442 ftrace_hash_empty(ops->filter_hash)) {
2444 return t_hash_start(m, pos);
2445 iter->flags |= FTRACE_ITER_PRINTALL;
2446 /* reset in case of seek/pread */
2447 iter->flags &= ~FTRACE_ITER_HASH;
2451 if (iter->flags & FTRACE_ITER_HASH)
2452 return t_hash_start(m, pos);
2455 * Unfortunately, we need to restart at ftrace_pages_start
2456 * every time we let go of the ftrace_mutex. This is because
2457 * those pointers can change without the lock.
2459 iter->pg = ftrace_pages_start;
2461 for (l = 0; l <= *pos; ) {
2462 p = t_next(m, p, &l);
2468 return t_hash_start(m, pos);
2473 static void t_stop(struct seq_file *m, void *p)
2475 mutex_unlock(&ftrace_lock);
2478 static int t_show(struct seq_file *m, void *v)
2480 struct ftrace_iterator *iter = m->private;
2481 struct dyn_ftrace *rec;
2483 if (iter->flags & FTRACE_ITER_HASH)
2484 return t_hash_show(m, iter);
2486 if (iter->flags & FTRACE_ITER_PRINTALL) {
2487 seq_printf(m, "#### all functions enabled ####\n");
2496 seq_printf(m, "%ps", (void *)rec->ip);
2497 if (iter->flags & FTRACE_ITER_ENABLED)
2498 seq_printf(m, " (%ld)%s",
2499 rec->flags & ~FTRACE_FL_MASK,
2500 rec->flags & FTRACE_FL_REGS ? " R" : "");
2501 seq_printf(m, "\n");
2506 static const struct seq_operations show_ftrace_seq_ops = {
2514 ftrace_avail_open(struct inode *inode, struct file *file)
2516 struct ftrace_iterator *iter;
2518 if (unlikely(ftrace_disabled))
2521 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2523 iter->pg = ftrace_pages_start;
2524 iter->ops = &global_ops;
2527 return iter ? 0 : -ENOMEM;
2531 ftrace_enabled_open(struct inode *inode, struct file *file)
2533 struct ftrace_iterator *iter;
2535 if (unlikely(ftrace_disabled))
2538 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2540 iter->pg = ftrace_pages_start;
2541 iter->flags = FTRACE_ITER_ENABLED;
2542 iter->ops = &global_ops;
2545 return iter ? 0 : -ENOMEM;
2548 static void ftrace_filter_reset(struct ftrace_hash *hash)
2550 mutex_lock(&ftrace_lock);
2551 ftrace_hash_clear(hash);
2552 mutex_unlock(&ftrace_lock);
2556 * ftrace_regex_open - initialize function tracer filter files
2557 * @ops: The ftrace_ops that hold the hash filters
2558 * @flag: The type of filter to process
2559 * @inode: The inode, usually passed in to your open routine
2560 * @file: The file, usually passed in to your open routine
2562 * ftrace_regex_open() initializes the filter files for the
2563 * @ops. Depending on @flag it may process the filter hash or
2564 * the notrace hash of @ops. With this called from the open
2565 * routine, you can use ftrace_filter_write() for the write
2566 * routine if @flag has FTRACE_ITER_FILTER set, or
2567 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2568 * ftrace_regex_lseek() should be used as the lseek routine, and
2569 * release must call ftrace_regex_release().
2572 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2573 struct inode *inode, struct file *file)
2575 struct ftrace_iterator *iter;
2576 struct ftrace_hash *hash;
2579 if (unlikely(ftrace_disabled))
2582 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2586 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2591 if (flag & FTRACE_ITER_NOTRACE)
2592 hash = ops->notrace_hash;
2594 hash = ops->filter_hash;
2599 if (file->f_mode & FMODE_WRITE) {
2600 mutex_lock(&ftrace_lock);
2601 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2602 mutex_unlock(&ftrace_lock);
2605 trace_parser_put(&iter->parser);
2611 mutex_lock(&ftrace_regex_lock);
2613 if ((file->f_mode & FMODE_WRITE) &&
2614 (file->f_flags & O_TRUNC))
2615 ftrace_filter_reset(iter->hash);
2617 if (file->f_mode & FMODE_READ) {
2618 iter->pg = ftrace_pages_start;
2620 ret = seq_open(file, &show_ftrace_seq_ops);
2622 struct seq_file *m = file->private_data;
2626 free_ftrace_hash(iter->hash);
2627 trace_parser_put(&iter->parser);
2631 file->private_data = iter;
2632 mutex_unlock(&ftrace_regex_lock);
2638 ftrace_filter_open(struct inode *inode, struct file *file)
2640 return ftrace_regex_open(&global_ops,
2641 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2646 ftrace_notrace_open(struct inode *inode, struct file *file)
2648 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2653 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
2657 if (file->f_mode & FMODE_READ)
2658 ret = seq_lseek(file, offset, origin);
2660 file->f_pos = ret = 1;
2665 static int ftrace_match(char *str, char *regex, int len, int type)
2672 if (strcmp(str, regex) == 0)
2675 case MATCH_FRONT_ONLY:
2676 if (strncmp(str, regex, len) == 0)
2679 case MATCH_MIDDLE_ONLY:
2680 if (strstr(str, regex))
2683 case MATCH_END_ONLY:
2685 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2694 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2696 struct ftrace_func_entry *entry;
2699 entry = ftrace_lookup_ip(hash, rec->ip);
2701 /* Do nothing if it doesn't exist */
2705 free_hash_entry(hash, entry);
2707 /* Do nothing if it exists */
2711 ret = add_hash_entry(hash, rec->ip);
2717 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2718 char *regex, int len, int type)
2720 char str[KSYM_SYMBOL_LEN];
2723 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2726 /* module lookup requires matching the module */
2727 if (!modname || strcmp(modname, mod))
2730 /* blank search means to match all funcs in the mod */
2735 return ftrace_match(str, regex, len, type);
2739 match_records(struct ftrace_hash *hash, char *buff,
2740 int len, char *mod, int not)
2742 unsigned search_len = 0;
2743 struct ftrace_page *pg;
2744 struct dyn_ftrace *rec;
2745 int type = MATCH_FULL;
2746 char *search = buff;
2751 type = filter_parse_regex(buff, len, &search, ¬);
2752 search_len = strlen(search);
2755 mutex_lock(&ftrace_lock);
2757 if (unlikely(ftrace_disabled))
2760 do_for_each_ftrace_rec(pg, rec) {
2761 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2762 ret = enter_record(hash, rec, not);
2769 } while_for_each_ftrace_rec();
2771 mutex_unlock(&ftrace_lock);
2777 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2779 return match_records(hash, buff, len, NULL, 0);
2783 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2787 /* blank or '*' mean the same */
2788 if (strcmp(buff, "*") == 0)
2791 /* handle the case of 'dont filter this module' */
2792 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2797 return match_records(hash, buff, strlen(buff), mod, not);
2801 * We register the module command as a template to show others how
2802 * to register the a command as well.
2806 ftrace_mod_callback(struct ftrace_hash *hash,
2807 char *func, char *cmd, char *param, int enable)
2813 * cmd == 'mod' because we only registered this func
2814 * for the 'mod' ftrace_func_command.
2815 * But if you register one func with multiple commands,
2816 * you can tell which command was used by the cmd
2820 /* we must have a module name */
2824 mod = strsep(¶m, ":");
2828 ret = ftrace_match_module_records(hash, func, mod);
2837 static struct ftrace_func_command ftrace_mod_cmd = {
2839 .func = ftrace_mod_callback,
2842 static int __init ftrace_mod_cmd_init(void)
2844 return register_ftrace_command(&ftrace_mod_cmd);
2846 device_initcall(ftrace_mod_cmd_init);
2848 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2849 struct ftrace_ops *op, struct pt_regs *pt_regs)
2851 struct ftrace_func_probe *entry;
2852 struct hlist_head *hhd;
2853 struct hlist_node *n;
2856 key = hash_long(ip, FTRACE_HASH_BITS);
2858 hhd = &ftrace_func_hash[key];
2860 if (hlist_empty(hhd))
2864 * Disable preemption for these calls to prevent a RCU grace
2865 * period. This syncs the hash iteration and freeing of items
2866 * on the hash. rcu_read_lock is too dangerous here.
2868 preempt_disable_notrace();
2869 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2870 if (entry->ip == ip)
2871 entry->ops->func(ip, parent_ip, &entry->data);
2873 preempt_enable_notrace();
2876 static struct ftrace_ops trace_probe_ops __read_mostly =
2878 .func = function_trace_probe_call,
2881 static int ftrace_probe_registered;
2883 static void __enable_ftrace_function_probe(void)
2888 if (ftrace_probe_registered)
2891 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2892 struct hlist_head *hhd = &ftrace_func_hash[i];
2896 /* Nothing registered? */
2897 if (i == FTRACE_FUNC_HASHSIZE)
2900 ret = __register_ftrace_function(&trace_probe_ops);
2902 ret = ftrace_startup(&trace_probe_ops, 0);
2904 ftrace_probe_registered = 1;
2907 static void __disable_ftrace_function_probe(void)
2912 if (!ftrace_probe_registered)
2915 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2916 struct hlist_head *hhd = &ftrace_func_hash[i];
2921 /* no more funcs left */
2922 ret = __unregister_ftrace_function(&trace_probe_ops);
2924 ftrace_shutdown(&trace_probe_ops, 0);
2926 ftrace_probe_registered = 0;
2930 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2932 struct ftrace_func_probe *entry =
2933 container_of(rhp, struct ftrace_func_probe, rcu);
2935 if (entry->ops->free)
2936 entry->ops->free(&entry->data);
2942 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2945 struct ftrace_func_probe *entry;
2946 struct ftrace_page *pg;
2947 struct dyn_ftrace *rec;
2953 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2954 len = strlen(search);
2956 /* we do not support '!' for function probes */
2960 mutex_lock(&ftrace_lock);
2962 if (unlikely(ftrace_disabled))
2965 do_for_each_ftrace_rec(pg, rec) {
2967 if (!ftrace_match_record(rec, NULL, search, len, type))
2970 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2972 /* If we did not process any, then return error */
2983 * The caller might want to do something special
2984 * for each function we find. We call the callback
2985 * to give the caller an opportunity to do so.
2987 if (ops->callback) {
2988 if (ops->callback(rec->ip, &entry->data) < 0) {
2989 /* caller does not like this func */
2996 entry->ip = rec->ip;
2998 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2999 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3001 } while_for_each_ftrace_rec();
3002 __enable_ftrace_function_probe();
3005 mutex_unlock(&ftrace_lock);
3011 PROBE_TEST_FUNC = 1,
3016 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3017 void *data, int flags)
3019 struct ftrace_func_probe *entry;
3020 struct hlist_node *n, *tmp;
3021 char str[KSYM_SYMBOL_LEN];
3022 int type = MATCH_FULL;
3026 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3031 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3032 len = strlen(search);
3034 /* we do not support '!' for function probes */
3039 mutex_lock(&ftrace_lock);
3040 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3041 struct hlist_head *hhd = &ftrace_func_hash[i];
3043 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3045 /* break up if statements for readability */
3046 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3049 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3052 /* do this last, since it is the most expensive */
3054 kallsyms_lookup(entry->ip, NULL, NULL,
3056 if (!ftrace_match(str, glob, len, type))
3060 hlist_del(&entry->node);
3061 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3064 __disable_ftrace_function_probe();
3065 mutex_unlock(&ftrace_lock);
3069 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3072 __unregister_ftrace_function_probe(glob, ops, data,
3073 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3077 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3079 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3082 void unregister_ftrace_function_probe_all(char *glob)
3084 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3087 static LIST_HEAD(ftrace_commands);
3088 static DEFINE_MUTEX(ftrace_cmd_mutex);
3090 int register_ftrace_command(struct ftrace_func_command *cmd)
3092 struct ftrace_func_command *p;
3095 mutex_lock(&ftrace_cmd_mutex);
3096 list_for_each_entry(p, &ftrace_commands, list) {
3097 if (strcmp(cmd->name, p->name) == 0) {
3102 list_add(&cmd->list, &ftrace_commands);
3104 mutex_unlock(&ftrace_cmd_mutex);
3109 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3111 struct ftrace_func_command *p, *n;
3114 mutex_lock(&ftrace_cmd_mutex);
3115 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3116 if (strcmp(cmd->name, p->name) == 0) {
3118 list_del_init(&p->list);
3123 mutex_unlock(&ftrace_cmd_mutex);
3128 static int ftrace_process_regex(struct ftrace_hash *hash,
3129 char *buff, int len, int enable)
3131 char *func, *command, *next = buff;
3132 struct ftrace_func_command *p;
3135 func = strsep(&next, ":");
3138 ret = ftrace_match_records(hash, func, len);
3148 command = strsep(&next, ":");
3150 mutex_lock(&ftrace_cmd_mutex);
3151 list_for_each_entry(p, &ftrace_commands, list) {
3152 if (strcmp(p->name, command) == 0) {
3153 ret = p->func(hash, func, command, next, enable);
3158 mutex_unlock(&ftrace_cmd_mutex);
3164 ftrace_regex_write(struct file *file, const char __user *ubuf,
3165 size_t cnt, loff_t *ppos, int enable)
3167 struct ftrace_iterator *iter;
3168 struct trace_parser *parser;
3174 mutex_lock(&ftrace_regex_lock);
3177 if (unlikely(ftrace_disabled))
3180 if (file->f_mode & FMODE_READ) {
3181 struct seq_file *m = file->private_data;
3184 iter = file->private_data;
3186 parser = &iter->parser;
3187 read = trace_get_user(parser, ubuf, cnt, ppos);
3189 if (read >= 0 && trace_parser_loaded(parser) &&
3190 !trace_parser_cont(parser)) {
3191 ret = ftrace_process_regex(iter->hash, parser->buffer,
3192 parser->idx, enable);
3193 trace_parser_clear(parser);
3200 mutex_unlock(&ftrace_regex_lock);
3206 ftrace_filter_write(struct file *file, const char __user *ubuf,
3207 size_t cnt, loff_t *ppos)
3209 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3213 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3214 size_t cnt, loff_t *ppos)
3216 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3220 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3221 int reset, int enable)
3223 struct ftrace_hash **orig_hash;
3224 struct ftrace_hash *hash;
3227 /* All global ops uses the global ops filters */
3228 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3231 if (unlikely(ftrace_disabled))
3235 orig_hash = &ops->filter_hash;
3237 orig_hash = &ops->notrace_hash;
3239 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3243 mutex_lock(&ftrace_regex_lock);
3245 ftrace_filter_reset(hash);
3246 if (buf && !ftrace_match_records(hash, buf, len)) {
3248 goto out_regex_unlock;
3251 mutex_lock(&ftrace_lock);
3252 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3253 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3255 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3257 mutex_unlock(&ftrace_lock);
3260 mutex_unlock(&ftrace_regex_lock);
3262 free_ftrace_hash(hash);
3267 * ftrace_set_filter - set a function to filter on in ftrace
3268 * @ops - the ops to set the filter with
3269 * @buf - the string that holds the function filter text.
3270 * @len - the length of the string.
3271 * @reset - non zero to reset all filters before applying this filter.
3273 * Filters denote which functions should be enabled when tracing is enabled.
3274 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3276 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3279 return ftrace_set_regex(ops, buf, len, reset, 1);
3281 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3284 * ftrace_set_notrace - set a function to not trace in ftrace
3285 * @ops - the ops to set the notrace filter with
3286 * @buf - the string that holds the function notrace text.
3287 * @len - the length of the string.
3288 * @reset - non zero to reset all filters before applying this filter.
3290 * Notrace Filters denote which functions should not be enabled when tracing
3291 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3294 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3297 return ftrace_set_regex(ops, buf, len, reset, 0);
3299 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3301 * ftrace_set_filter - set a function to filter on in ftrace
3302 * @ops - the ops to set the filter with
3303 * @buf - the string that holds the function filter text.
3304 * @len - the length of the string.
3305 * @reset - non zero to reset all filters before applying this filter.
3307 * Filters denote which functions should be enabled when tracing is enabled.
3308 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3310 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3312 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3314 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3317 * ftrace_set_notrace - set a function to not trace in ftrace
3318 * @ops - the ops to set the notrace filter with
3319 * @buf - the string that holds the function notrace text.
3320 * @len - the length of the string.
3321 * @reset - non zero to reset all filters before applying this filter.
3323 * Notrace Filters denote which functions should not be enabled when tracing
3324 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3327 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3329 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3331 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3334 * command line interface to allow users to set filters on boot up.
3336 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3337 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3338 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3340 static int __init set_ftrace_notrace(char *str)
3342 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3345 __setup("ftrace_notrace=", set_ftrace_notrace);
3347 static int __init set_ftrace_filter(char *str)
3349 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3352 __setup("ftrace_filter=", set_ftrace_filter);
3354 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3355 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3356 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3358 static int __init set_graph_function(char *str)
3360 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3363 __setup("ftrace_graph_filter=", set_graph_function);
3365 static void __init set_ftrace_early_graph(char *buf)
3371 func = strsep(&buf, ",");
3372 /* we allow only one expression at a time */
3373 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3376 printk(KERN_DEBUG "ftrace: function %s not "
3377 "traceable\n", func);
3380 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3383 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3388 func = strsep(&buf, ",");
3389 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3393 static void __init set_ftrace_early_filters(void)
3395 if (ftrace_filter_buf[0])
3396 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3397 if (ftrace_notrace_buf[0])
3398 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3399 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3400 if (ftrace_graph_buf[0])
3401 set_ftrace_early_graph(ftrace_graph_buf);
3402 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3405 int ftrace_regex_release(struct inode *inode, struct file *file)
3407 struct seq_file *m = (struct seq_file *)file->private_data;
3408 struct ftrace_iterator *iter;
3409 struct ftrace_hash **orig_hash;
3410 struct trace_parser *parser;
3414 mutex_lock(&ftrace_regex_lock);
3415 if (file->f_mode & FMODE_READ) {
3418 seq_release(inode, file);
3420 iter = file->private_data;
3422 parser = &iter->parser;
3423 if (trace_parser_loaded(parser)) {
3424 parser->buffer[parser->idx] = 0;
3425 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3428 trace_parser_put(parser);
3430 if (file->f_mode & FMODE_WRITE) {
3431 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3434 orig_hash = &iter->ops->filter_hash;
3436 orig_hash = &iter->ops->notrace_hash;
3438 mutex_lock(&ftrace_lock);
3439 ret = ftrace_hash_move(iter->ops, filter_hash,
3440 orig_hash, iter->hash);
3441 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3443 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3445 mutex_unlock(&ftrace_lock);
3447 free_ftrace_hash(iter->hash);
3450 mutex_unlock(&ftrace_regex_lock);
3454 static const struct file_operations ftrace_avail_fops = {
3455 .open = ftrace_avail_open,
3457 .llseek = seq_lseek,
3458 .release = seq_release_private,
3461 static const struct file_operations ftrace_enabled_fops = {
3462 .open = ftrace_enabled_open,
3464 .llseek = seq_lseek,
3465 .release = seq_release_private,
3468 static const struct file_operations ftrace_filter_fops = {
3469 .open = ftrace_filter_open,
3471 .write = ftrace_filter_write,
3472 .llseek = ftrace_regex_lseek,
3473 .release = ftrace_regex_release,
3476 static const struct file_operations ftrace_notrace_fops = {
3477 .open = ftrace_notrace_open,
3479 .write = ftrace_notrace_write,
3480 .llseek = ftrace_regex_lseek,
3481 .release = ftrace_regex_release,
3484 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3486 static DEFINE_MUTEX(graph_lock);
3488 int ftrace_graph_count;
3489 int ftrace_graph_filter_enabled;
3490 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3493 __g_next(struct seq_file *m, loff_t *pos)
3495 if (*pos >= ftrace_graph_count)
3497 return &ftrace_graph_funcs[*pos];
3501 g_next(struct seq_file *m, void *v, loff_t *pos)
3504 return __g_next(m, pos);
3507 static void *g_start(struct seq_file *m, loff_t *pos)
3509 mutex_lock(&graph_lock);
3511 /* Nothing, tell g_show to print all functions are enabled */
3512 if (!ftrace_graph_filter_enabled && !*pos)
3515 return __g_next(m, pos);
3518 static void g_stop(struct seq_file *m, void *p)
3520 mutex_unlock(&graph_lock);
3523 static int g_show(struct seq_file *m, void *v)
3525 unsigned long *ptr = v;
3530 if (ptr == (unsigned long *)1) {
3531 seq_printf(m, "#### all functions enabled ####\n");
3535 seq_printf(m, "%ps\n", (void *)*ptr);
3540 static const struct seq_operations ftrace_graph_seq_ops = {
3548 ftrace_graph_open(struct inode *inode, struct file *file)
3552 if (unlikely(ftrace_disabled))
3555 mutex_lock(&graph_lock);
3556 if ((file->f_mode & FMODE_WRITE) &&
3557 (file->f_flags & O_TRUNC)) {
3558 ftrace_graph_filter_enabled = 0;
3559 ftrace_graph_count = 0;
3560 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3562 mutex_unlock(&graph_lock);
3564 if (file->f_mode & FMODE_READ)
3565 ret = seq_open(file, &ftrace_graph_seq_ops);
3571 ftrace_graph_release(struct inode *inode, struct file *file)
3573 if (file->f_mode & FMODE_READ)
3574 seq_release(inode, file);
3579 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3581 struct dyn_ftrace *rec;
3582 struct ftrace_page *pg;
3591 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3592 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3595 search_len = strlen(search);
3597 mutex_lock(&ftrace_lock);
3599 if (unlikely(ftrace_disabled)) {
3600 mutex_unlock(&ftrace_lock);
3604 do_for_each_ftrace_rec(pg, rec) {
3606 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3607 /* if it is in the array */
3609 for (i = 0; i < *idx; i++) {
3610 if (array[i] == rec->ip) {
3619 array[(*idx)++] = rec->ip;
3620 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3625 array[i] = array[--(*idx)];
3631 } while_for_each_ftrace_rec();
3633 mutex_unlock(&ftrace_lock);
3638 ftrace_graph_filter_enabled = 1;
3643 ftrace_graph_write(struct file *file, const char __user *ubuf,
3644 size_t cnt, loff_t *ppos)
3646 struct trace_parser parser;
3652 mutex_lock(&graph_lock);
3654 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3659 read = trace_get_user(&parser, ubuf, cnt, ppos);
3661 if (read >= 0 && trace_parser_loaded((&parser))) {
3662 parser.buffer[parser.idx] = 0;
3664 /* we allow only one expression at a time */
3665 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3674 trace_parser_put(&parser);
3676 mutex_unlock(&graph_lock);
3681 static const struct file_operations ftrace_graph_fops = {
3682 .open = ftrace_graph_open,
3684 .write = ftrace_graph_write,
3685 .release = ftrace_graph_release,
3686 .llseek = seq_lseek,
3688 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3690 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3693 trace_create_file("available_filter_functions", 0444,
3694 d_tracer, NULL, &ftrace_avail_fops);
3696 trace_create_file("enabled_functions", 0444,
3697 d_tracer, NULL, &ftrace_enabled_fops);
3699 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3700 NULL, &ftrace_filter_fops);
3702 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3703 NULL, &ftrace_notrace_fops);
3705 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3706 trace_create_file("set_graph_function", 0444, d_tracer,
3708 &ftrace_graph_fops);
3709 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3714 static int ftrace_cmp_ips(const void *a, const void *b)
3716 const unsigned long *ipa = a;
3717 const unsigned long *ipb = b;
3726 static void ftrace_swap_ips(void *a, void *b, int size)
3728 unsigned long *ipa = a;
3729 unsigned long *ipb = b;
3737 static int ftrace_process_locs(struct module *mod,
3738 unsigned long *start,
3741 struct ftrace_page *start_pg;
3742 struct ftrace_page *pg;
3743 struct dyn_ftrace *rec;
3744 unsigned long count;
3747 unsigned long flags = 0; /* Shut up gcc */
3750 count = end - start;
3755 sort(start, count, sizeof(*start),
3756 ftrace_cmp_ips, ftrace_swap_ips);
3758 start_pg = ftrace_allocate_pages(count);
3762 mutex_lock(&ftrace_lock);
3765 * Core and each module needs their own pages, as
3766 * modules will free them when they are removed.
3767 * Force a new page to be allocated for modules.
3770 WARN_ON(ftrace_pages || ftrace_pages_start);
3771 /* First initialization */
3772 ftrace_pages = ftrace_pages_start = start_pg;
3777 if (WARN_ON(ftrace_pages->next)) {
3778 /* Hmm, we have free pages? */
3779 while (ftrace_pages->next)
3780 ftrace_pages = ftrace_pages->next;
3783 ftrace_pages->next = start_pg;
3789 addr = ftrace_call_adjust(*p++);
3791 * Some architecture linkers will pad between
3792 * the different mcount_loc sections of different
3793 * object files to satisfy alignments.
3794 * Skip any NULL pointers.
3799 if (pg->index == pg->size) {
3800 /* We should have allocated enough */
3801 if (WARN_ON(!pg->next))
3806 rec = &pg->records[pg->index++];
3810 /* We should have used all pages */
3813 /* Assign the last page to ftrace_pages */
3816 /* These new locations need to be initialized */
3817 ftrace_new_pgs = start_pg;
3820 * We only need to disable interrupts on start up
3821 * because we are modifying code that an interrupt
3822 * may execute, and the modification is not atomic.
3823 * But for modules, nothing runs the code we modify
3824 * until we are finished with it, and there's no
3825 * reason to cause large interrupt latencies while we do it.
3828 local_irq_save(flags);
3829 ftrace_update_code(mod);
3831 local_irq_restore(flags);
3834 mutex_unlock(&ftrace_lock);
3839 #ifdef CONFIG_MODULES
3841 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3843 void ftrace_release_mod(struct module *mod)
3845 struct dyn_ftrace *rec;
3846 struct ftrace_page **last_pg;
3847 struct ftrace_page *pg;
3850 mutex_lock(&ftrace_lock);
3852 if (ftrace_disabled)
3856 * Each module has its own ftrace_pages, remove
3857 * them from the list.
3859 last_pg = &ftrace_pages_start;
3860 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3861 rec = &pg->records[0];
3862 if (within_module_core(rec->ip, mod)) {
3864 * As core pages are first, the first
3865 * page should never be a module page.
3867 if (WARN_ON(pg == ftrace_pages_start))
3870 /* Check if we are deleting the last page */
3871 if (pg == ftrace_pages)
3872 ftrace_pages = next_to_ftrace_page(last_pg);
3874 *last_pg = pg->next;
3875 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3876 free_pages((unsigned long)pg->records, order);
3879 last_pg = &pg->next;
3882 mutex_unlock(&ftrace_lock);
3885 static void ftrace_init_module(struct module *mod,
3886 unsigned long *start, unsigned long *end)
3888 if (ftrace_disabled || start == end)
3890 ftrace_process_locs(mod, start, end);
3893 static int ftrace_module_notify(struct notifier_block *self,
3894 unsigned long val, void *data)
3896 struct module *mod = data;
3899 case MODULE_STATE_COMING:
3900 ftrace_init_module(mod, mod->ftrace_callsites,
3901 mod->ftrace_callsites +
3902 mod->num_ftrace_callsites);
3904 case MODULE_STATE_GOING:
3905 ftrace_release_mod(mod);
3912 static int ftrace_module_notify(struct notifier_block *self,
3913 unsigned long val, void *data)
3917 #endif /* CONFIG_MODULES */
3919 struct notifier_block ftrace_module_nb = {
3920 .notifier_call = ftrace_module_notify,
3924 extern unsigned long __start_mcount_loc[];
3925 extern unsigned long __stop_mcount_loc[];
3927 void __init ftrace_init(void)
3929 unsigned long count, addr, flags;
3932 /* Keep the ftrace pointer to the stub */
3933 addr = (unsigned long)ftrace_stub;
3935 local_irq_save(flags);
3936 ftrace_dyn_arch_init(&addr);
3937 local_irq_restore(flags);
3939 /* ftrace_dyn_arch_init places the return code in addr */
3943 count = __stop_mcount_loc - __start_mcount_loc;
3945 ret = ftrace_dyn_table_alloc(count);
3949 last_ftrace_enabled = ftrace_enabled = 1;
3951 ret = ftrace_process_locs(NULL,
3955 ret = register_module_notifier(&ftrace_module_nb);
3957 pr_warning("Failed to register trace ftrace module notifier\n");
3959 set_ftrace_early_filters();
3963 ftrace_disabled = 1;
3968 static struct ftrace_ops global_ops = {
3969 .func = ftrace_stub,
3972 static int __init ftrace_nodyn_init(void)
3977 device_initcall(ftrace_nodyn_init);
3979 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3980 static inline void ftrace_startup_enable(int command) { }
3981 /* Keep as macros so we do not need to define the commands */
3982 # define ftrace_startup(ops, command) \
3984 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3987 # define ftrace_shutdown(ops, command) do { } while (0)
3988 # define ftrace_startup_sysctl() do { } while (0)
3989 # define ftrace_shutdown_sysctl() do { } while (0)
3992 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3997 #endif /* CONFIG_DYNAMIC_FTRACE */
4000 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4001 struct ftrace_ops *op, struct pt_regs *regs)
4003 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4007 * Some of the ops may be dynamically allocated,
4008 * they must be freed after a synchronize_sched().
4010 preempt_disable_notrace();
4011 trace_recursion_set(TRACE_CONTROL_BIT);
4012 op = rcu_dereference_raw(ftrace_control_list);
4013 while (op != &ftrace_list_end) {
4014 if (!ftrace_function_local_disabled(op) &&
4015 ftrace_ops_test(op, ip))
4016 op->func(ip, parent_ip, op, regs);
4018 op = rcu_dereference_raw(op->next);
4020 trace_recursion_clear(TRACE_CONTROL_BIT);
4021 preempt_enable_notrace();
4024 static struct ftrace_ops control_ops = {
4025 .func = ftrace_ops_control_func,
4029 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4030 struct ftrace_ops *ignored, struct pt_regs *regs)
4032 struct ftrace_ops *op;
4034 if (function_trace_stop)
4037 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4040 trace_recursion_set(TRACE_INTERNAL_BIT);
4042 * Some of the ops may be dynamically allocated,
4043 * they must be freed after a synchronize_sched().
4045 preempt_disable_notrace();
4046 op = rcu_dereference_raw(ftrace_ops_list);
4047 while (op != &ftrace_list_end) {
4048 if (ftrace_ops_test(op, ip))
4049 op->func(ip, parent_ip, op, regs);
4050 op = rcu_dereference_raw(op->next);
4052 preempt_enable_notrace();
4053 trace_recursion_clear(TRACE_INTERNAL_BIT);
4057 * Some archs only support passing ip and parent_ip. Even though
4058 * the list function ignores the op parameter, we do not want any
4059 * C side effects, where a function is called without the caller
4060 * sending a third parameter.
4061 * Archs are to support both the regs and ftrace_ops at the same time.
4062 * If they support ftrace_ops, it is assumed they support regs.
4063 * If call backs want to use regs, they must either check for regs
4064 * being NULL, or ARCH_SUPPORTS_FTRACE_SAVE_REGS.
4065 * Note, ARCH_SUPPORT_SAVE_REGS expects a full regs to be saved.
4066 * An architecture can pass partial regs with ftrace_ops and still
4067 * set the ARCH_SUPPORT_FTARCE_OPS.
4069 #if ARCH_SUPPORTS_FTRACE_OPS
4070 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4071 struct ftrace_ops *op, struct pt_regs *regs)
4073 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4076 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4078 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4082 static void clear_ftrace_swapper(void)
4084 struct task_struct *p;
4088 for_each_online_cpu(cpu) {
4090 clear_tsk_trace_trace(p);
4095 static void set_ftrace_swapper(void)
4097 struct task_struct *p;
4101 for_each_online_cpu(cpu) {
4103 set_tsk_trace_trace(p);
4108 static void clear_ftrace_pid(struct pid *pid)
4110 struct task_struct *p;
4113 do_each_pid_task(pid, PIDTYPE_PID, p) {
4114 clear_tsk_trace_trace(p);
4115 } while_each_pid_task(pid, PIDTYPE_PID, p);
4121 static void set_ftrace_pid(struct pid *pid)
4123 struct task_struct *p;
4126 do_each_pid_task(pid, PIDTYPE_PID, p) {
4127 set_tsk_trace_trace(p);
4128 } while_each_pid_task(pid, PIDTYPE_PID, p);
4132 static void clear_ftrace_pid_task(struct pid *pid)
4134 if (pid == ftrace_swapper_pid)
4135 clear_ftrace_swapper();
4137 clear_ftrace_pid(pid);
4140 static void set_ftrace_pid_task(struct pid *pid)
4142 if (pid == ftrace_swapper_pid)
4143 set_ftrace_swapper();
4145 set_ftrace_pid(pid);
4148 static int ftrace_pid_add(int p)
4151 struct ftrace_pid *fpid;
4154 mutex_lock(&ftrace_lock);
4157 pid = ftrace_swapper_pid;
4159 pid = find_get_pid(p);
4166 list_for_each_entry(fpid, &ftrace_pids, list)
4167 if (fpid->pid == pid)
4172 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4176 list_add(&fpid->list, &ftrace_pids);
4179 set_ftrace_pid_task(pid);
4181 ftrace_update_pid_func();
4182 ftrace_startup_enable(0);
4184 mutex_unlock(&ftrace_lock);
4188 if (pid != ftrace_swapper_pid)
4192 mutex_unlock(&ftrace_lock);
4196 static void ftrace_pid_reset(void)
4198 struct ftrace_pid *fpid, *safe;
4200 mutex_lock(&ftrace_lock);
4201 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4202 struct pid *pid = fpid->pid;
4204 clear_ftrace_pid_task(pid);
4206 list_del(&fpid->list);
4210 ftrace_update_pid_func();
4211 ftrace_startup_enable(0);
4213 mutex_unlock(&ftrace_lock);
4216 static void *fpid_start(struct seq_file *m, loff_t *pos)
4218 mutex_lock(&ftrace_lock);
4220 if (list_empty(&ftrace_pids) && (!*pos))
4223 return seq_list_start(&ftrace_pids, *pos);
4226 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4231 return seq_list_next(v, &ftrace_pids, pos);
4234 static void fpid_stop(struct seq_file *m, void *p)
4236 mutex_unlock(&ftrace_lock);
4239 static int fpid_show(struct seq_file *m, void *v)
4241 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4243 if (v == (void *)1) {
4244 seq_printf(m, "no pid\n");
4248 if (fpid->pid == ftrace_swapper_pid)
4249 seq_printf(m, "swapper tasks\n");
4251 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4256 static const struct seq_operations ftrace_pid_sops = {
4257 .start = fpid_start,
4264 ftrace_pid_open(struct inode *inode, struct file *file)
4268 if ((file->f_mode & FMODE_WRITE) &&
4269 (file->f_flags & O_TRUNC))
4272 if (file->f_mode & FMODE_READ)
4273 ret = seq_open(file, &ftrace_pid_sops);
4279 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4280 size_t cnt, loff_t *ppos)
4286 if (cnt >= sizeof(buf))
4289 if (copy_from_user(&buf, ubuf, cnt))
4295 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4296 * to clean the filter quietly.
4298 tmp = strstrip(buf);
4299 if (strlen(tmp) == 0)
4302 ret = strict_strtol(tmp, 10, &val);
4306 ret = ftrace_pid_add(val);
4308 return ret ? ret : cnt;
4312 ftrace_pid_release(struct inode *inode, struct file *file)
4314 if (file->f_mode & FMODE_READ)
4315 seq_release(inode, file);
4320 static const struct file_operations ftrace_pid_fops = {
4321 .open = ftrace_pid_open,
4322 .write = ftrace_pid_write,
4324 .llseek = seq_lseek,
4325 .release = ftrace_pid_release,
4328 static __init int ftrace_init_debugfs(void)
4330 struct dentry *d_tracer;
4332 d_tracer = tracing_init_dentry();
4336 ftrace_init_dyn_debugfs(d_tracer);
4338 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4339 NULL, &ftrace_pid_fops);
4341 ftrace_profile_debugfs(d_tracer);
4345 fs_initcall(ftrace_init_debugfs);
4348 * ftrace_kill - kill ftrace
4350 * This function should be used by panic code. It stops ftrace
4351 * but in a not so nice way. If you need to simply kill ftrace
4352 * from a non-atomic section, use ftrace_kill.
4354 void ftrace_kill(void)
4356 ftrace_disabled = 1;
4358 clear_ftrace_function();
4362 * Test if ftrace is dead or not.
4364 int ftrace_is_dead(void)
4366 return ftrace_disabled;
4370 * register_ftrace_function - register a function for profiling
4371 * @ops - ops structure that holds the function for profiling.
4373 * Register a function to be called by all functions in the
4376 * Note: @ops->func and all the functions it calls must be labeled
4377 * with "notrace", otherwise it will go into a
4380 int register_ftrace_function(struct ftrace_ops *ops)
4384 mutex_lock(&ftrace_lock);
4386 ret = __register_ftrace_function(ops);
4388 ret = ftrace_startup(ops, 0);
4390 mutex_unlock(&ftrace_lock);
4394 EXPORT_SYMBOL_GPL(register_ftrace_function);
4397 * unregister_ftrace_function - unregister a function for profiling.
4398 * @ops - ops structure that holds the function to unregister
4400 * Unregister a function that was added to be called by ftrace profiling.
4402 int unregister_ftrace_function(struct ftrace_ops *ops)
4406 mutex_lock(&ftrace_lock);
4407 ret = __unregister_ftrace_function(ops);
4409 ftrace_shutdown(ops, 0);
4410 mutex_unlock(&ftrace_lock);
4414 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4417 ftrace_enable_sysctl(struct ctl_table *table, int write,
4418 void __user *buffer, size_t *lenp,
4423 mutex_lock(&ftrace_lock);
4425 if (unlikely(ftrace_disabled))
4428 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4430 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4433 last_ftrace_enabled = !!ftrace_enabled;
4435 if (ftrace_enabled) {
4437 ftrace_startup_sysctl();
4439 /* we are starting ftrace again */
4440 if (ftrace_ops_list != &ftrace_list_end) {
4441 if (ftrace_ops_list->next == &ftrace_list_end)
4442 ftrace_trace_function = ftrace_ops_list->func;
4444 ftrace_trace_function = ftrace_ops_list_func;
4448 /* stopping ftrace calls (just send to ftrace_stub) */
4449 ftrace_trace_function = ftrace_stub;
4451 ftrace_shutdown_sysctl();
4455 mutex_unlock(&ftrace_lock);
4459 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4461 static int ftrace_graph_active;
4462 static struct notifier_block ftrace_suspend_notifier;
4464 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4469 /* The callbacks that hook a function */
4470 trace_func_graph_ret_t ftrace_graph_return =
4471 (trace_func_graph_ret_t)ftrace_stub;
4472 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4474 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4475 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4479 unsigned long flags;
4480 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4481 struct task_struct *g, *t;
4483 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4484 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4485 * sizeof(struct ftrace_ret_stack),
4487 if (!ret_stack_list[i]) {
4495 read_lock_irqsave(&tasklist_lock, flags);
4496 do_each_thread(g, t) {
4502 if (t->ret_stack == NULL) {
4503 atomic_set(&t->tracing_graph_pause, 0);
4504 atomic_set(&t->trace_overrun, 0);
4505 t->curr_ret_stack = -1;
4506 /* Make sure the tasks see the -1 first: */
4508 t->ret_stack = ret_stack_list[start++];
4510 } while_each_thread(g, t);
4513 read_unlock_irqrestore(&tasklist_lock, flags);
4515 for (i = start; i < end; i++)
4516 kfree(ret_stack_list[i]);
4521 ftrace_graph_probe_sched_switch(void *ignore,
4522 struct task_struct *prev, struct task_struct *next)
4524 unsigned long long timestamp;
4528 * Does the user want to count the time a function was asleep.
4529 * If so, do not update the time stamps.
4531 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4534 timestamp = trace_clock_local();
4536 prev->ftrace_timestamp = timestamp;
4538 /* only process tasks that we timestamped */
4539 if (!next->ftrace_timestamp)
4543 * Update all the counters in next to make up for the
4544 * time next was sleeping.
4546 timestamp -= next->ftrace_timestamp;
4548 for (index = next->curr_ret_stack; index >= 0; index--)
4549 next->ret_stack[index].calltime += timestamp;
4552 /* Allocate a return stack for each task */
4553 static int start_graph_tracing(void)
4555 struct ftrace_ret_stack **ret_stack_list;
4558 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4559 sizeof(struct ftrace_ret_stack *),
4562 if (!ret_stack_list)
4565 /* The cpu_boot init_task->ret_stack will never be freed */
4566 for_each_online_cpu(cpu) {
4567 if (!idle_task(cpu)->ret_stack)
4568 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4572 ret = alloc_retstack_tasklist(ret_stack_list);
4573 } while (ret == -EAGAIN);
4576 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4578 pr_info("ftrace_graph: Couldn't activate tracepoint"
4579 " probe to kernel_sched_switch\n");
4582 kfree(ret_stack_list);
4587 * Hibernation protection.
4588 * The state of the current task is too much unstable during
4589 * suspend/restore to disk. We want to protect against that.
4592 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4596 case PM_HIBERNATION_PREPARE:
4597 pause_graph_tracing();
4600 case PM_POST_HIBERNATION:
4601 unpause_graph_tracing();
4607 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4608 trace_func_graph_ent_t entryfunc)
4612 mutex_lock(&ftrace_lock);
4614 /* we currently allow only one tracer registered at a time */
4615 if (ftrace_graph_active) {
4620 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4621 register_pm_notifier(&ftrace_suspend_notifier);
4623 ftrace_graph_active++;
4624 ret = start_graph_tracing();
4626 ftrace_graph_active--;
4630 ftrace_graph_return = retfunc;
4631 ftrace_graph_entry = entryfunc;
4633 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4636 mutex_unlock(&ftrace_lock);
4640 void unregister_ftrace_graph(void)
4642 mutex_lock(&ftrace_lock);
4644 if (unlikely(!ftrace_graph_active))
4647 ftrace_graph_active--;
4648 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4649 ftrace_graph_entry = ftrace_graph_entry_stub;
4650 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4651 unregister_pm_notifier(&ftrace_suspend_notifier);
4652 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4655 mutex_unlock(&ftrace_lock);
4658 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4661 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4663 atomic_set(&t->tracing_graph_pause, 0);
4664 atomic_set(&t->trace_overrun, 0);
4665 t->ftrace_timestamp = 0;
4666 /* make curr_ret_stack visible before we add the ret_stack */
4668 t->ret_stack = ret_stack;
4672 * Allocate a return stack for the idle task. May be the first
4673 * time through, or it may be done by CPU hotplug online.
4675 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4677 t->curr_ret_stack = -1;
4679 * The idle task has no parent, it either has its own
4680 * stack or no stack at all.
4683 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4685 if (ftrace_graph_active) {
4686 struct ftrace_ret_stack *ret_stack;
4688 ret_stack = per_cpu(idle_ret_stack, cpu);
4690 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4691 * sizeof(struct ftrace_ret_stack),
4695 per_cpu(idle_ret_stack, cpu) = ret_stack;
4697 graph_init_task(t, ret_stack);
4701 /* Allocate a return stack for newly created task */
4702 void ftrace_graph_init_task(struct task_struct *t)
4704 /* Make sure we do not use the parent ret_stack */
4705 t->ret_stack = NULL;
4706 t->curr_ret_stack = -1;
4708 if (ftrace_graph_active) {
4709 struct ftrace_ret_stack *ret_stack;
4711 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4712 * sizeof(struct ftrace_ret_stack),
4716 graph_init_task(t, ret_stack);
4720 void ftrace_graph_exit_task(struct task_struct *t)
4722 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4724 t->ret_stack = NULL;
4725 /* NULL must become visible to IRQs before we free it: */
4731 void ftrace_graph_stop(void)