2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
36 #include <trace/events/sched.h>
38 #include <asm/setup.h>
40 #include "trace_output.h"
41 #include "trace_stat.h"
43 #define FTRACE_WARN_ON(cond) \
51 #define FTRACE_WARN_ON_ONCE(cond) \
54 if (WARN_ON_ONCE(___r)) \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
67 static struct ftrace_ops ftrace_list_end __read_mostly = {
69 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
72 /* ftrace_enabled is a method to turn ftrace on or off */
73 int ftrace_enabled __read_mostly;
74 static int last_ftrace_enabled;
76 /* Quick disabling of function tracer. */
77 int function_trace_stop __read_mostly;
79 /* Current function tracing op */
80 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
82 /* List for set_ftrace_pid's pids. */
83 LIST_HEAD(ftrace_pids);
85 struct list_head list;
90 * ftrace_disabled is set when an anomaly is discovered.
91 * ftrace_disabled is much stronger than ftrace_enabled.
93 static int ftrace_disabled __read_mostly;
95 static DEFINE_MUTEX(ftrace_lock);
97 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
98 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
99 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
100 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
101 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
102 static struct ftrace_ops global_ops;
103 static struct ftrace_ops control_ops;
105 #if ARCH_SUPPORTS_FTRACE_OPS
106 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
107 struct ftrace_ops *op, struct pt_regs *regs);
109 /* See comment below, where ftrace_ops_list_func is defined */
110 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
111 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
115 * ftrace_nr_registered_ops - return number of ops registered
117 * Returns the number of ftrace_ops registered and tracing functions
119 int ftrace_nr_registered_ops(void)
121 struct ftrace_ops *ops;
124 mutex_lock(&ftrace_lock);
126 for (ops = ftrace_ops_list;
127 ops != &ftrace_list_end; ops = ops->next)
130 mutex_unlock(&ftrace_lock);
136 * Traverse the ftrace_global_list, invoking all entries. The reason that we
137 * can use rcu_dereference_raw() is that elements removed from this list
138 * are simply leaked, so there is no need to interact with a grace-period
139 * mechanism. The rcu_dereference_raw() calls are needed to handle
140 * concurrent insertions into the ftrace_global_list.
142 * Silly Alpha and silly pointer-speculation compiler optimizations!
145 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
146 struct ftrace_ops *op, struct pt_regs *regs)
148 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
151 trace_recursion_set(TRACE_GLOBAL_BIT);
152 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
153 while (op != &ftrace_list_end) {
154 op->func(ip, parent_ip, op, regs);
155 op = rcu_dereference_raw(op->next); /*see above*/
157 trace_recursion_clear(TRACE_GLOBAL_BIT);
160 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
161 struct ftrace_ops *op, struct pt_regs *regs)
163 if (!test_tsk_trace_trace(current))
166 ftrace_pid_function(ip, parent_ip, op, regs);
169 static void set_ftrace_pid_function(ftrace_func_t func)
171 /* do not set ftrace_pid_function to itself! */
172 if (func != ftrace_pid_func)
173 ftrace_pid_function = func;
177 * clear_ftrace_function - reset the ftrace function
179 * This NULLs the ftrace function and in essence stops
180 * tracing. There may be lag
182 void clear_ftrace_function(void)
184 ftrace_trace_function = ftrace_stub;
185 ftrace_pid_function = ftrace_stub;
188 static void control_ops_disable_all(struct ftrace_ops *ops)
192 for_each_possible_cpu(cpu)
193 *per_cpu_ptr(ops->disabled, cpu) = 1;
196 static int control_ops_alloc(struct ftrace_ops *ops)
198 int __percpu *disabled;
200 disabled = alloc_percpu(int);
204 ops->disabled = disabled;
205 control_ops_disable_all(ops);
209 static void control_ops_free(struct ftrace_ops *ops)
211 free_percpu(ops->disabled);
214 static void update_global_ops(void)
219 * If there's only one function registered, then call that
220 * function directly. Otherwise, we need to iterate over the
221 * registered callers.
223 if (ftrace_global_list == &ftrace_list_end ||
224 ftrace_global_list->next == &ftrace_list_end)
225 func = ftrace_global_list->func;
227 func = ftrace_global_list_func;
229 /* If we filter on pids, update to use the pid function */
230 if (!list_empty(&ftrace_pids)) {
231 set_ftrace_pid_function(func);
232 func = ftrace_pid_func;
235 global_ops.func = func;
238 static void update_ftrace_function(void)
245 * If we are at the end of the list and this ops is
246 * recursion safe and not dynamic and the arch supports passing ops,
247 * then have the mcount trampoline call the function directly.
249 if (ftrace_ops_list == &ftrace_list_end ||
250 (ftrace_ops_list->next == &ftrace_list_end &&
251 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
252 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
253 !FTRACE_FORCE_LIST_FUNC)) {
254 /* Set the ftrace_ops that the arch callback uses */
255 if (ftrace_ops_list == &global_ops)
256 function_trace_op = ftrace_global_list;
258 function_trace_op = ftrace_ops_list;
259 func = ftrace_ops_list->func;
261 /* Just use the default ftrace_ops */
262 function_trace_op = &ftrace_list_end;
263 func = ftrace_ops_list_func;
266 ftrace_trace_function = func;
269 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
273 * We are entering ops into the list but another
274 * CPU might be walking that list. We need to make sure
275 * the ops->next pointer is valid before another CPU sees
276 * the ops pointer included into the list.
278 rcu_assign_pointer(*list, ops);
281 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
283 struct ftrace_ops **p;
286 * If we are removing the last function, then simply point
287 * to the ftrace_stub.
289 if (*list == ops && ops->next == &ftrace_list_end) {
290 *list = &ftrace_list_end;
294 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
305 static void add_ftrace_list_ops(struct ftrace_ops **list,
306 struct ftrace_ops *main_ops,
307 struct ftrace_ops *ops)
309 int first = *list == &ftrace_list_end;
310 add_ftrace_ops(list, ops);
312 add_ftrace_ops(&ftrace_ops_list, main_ops);
315 static int remove_ftrace_list_ops(struct ftrace_ops **list,
316 struct ftrace_ops *main_ops,
317 struct ftrace_ops *ops)
319 int ret = remove_ftrace_ops(list, ops);
320 if (!ret && *list == &ftrace_list_end)
321 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
325 static int __register_ftrace_function(struct ftrace_ops *ops)
327 if (unlikely(ftrace_disabled))
330 if (FTRACE_WARN_ON(ops == &global_ops))
333 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
336 /* We don't support both control and global flags set. */
337 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
340 #ifndef ARCH_SUPPORTS_FTRACE_SAVE_REGS
342 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
343 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
344 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
346 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
347 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
350 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
351 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
354 if (!core_kernel_data((unsigned long)ops))
355 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
357 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
358 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
359 ops->flags |= FTRACE_OPS_FL_ENABLED;
360 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
361 if (control_ops_alloc(ops))
363 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
365 add_ftrace_ops(&ftrace_ops_list, ops);
368 update_ftrace_function();
373 static int __unregister_ftrace_function(struct ftrace_ops *ops)
380 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
383 if (FTRACE_WARN_ON(ops == &global_ops))
386 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
387 ret = remove_ftrace_list_ops(&ftrace_global_list,
390 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
391 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
392 ret = remove_ftrace_list_ops(&ftrace_control_list,
396 * The ftrace_ops is now removed from the list,
397 * so there'll be no new users. We must ensure
398 * all current users are done before we free
402 control_ops_free(ops);
405 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
411 update_ftrace_function();
414 * Dynamic ops may be freed, we must make sure that all
415 * callers are done before leaving this function.
417 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
423 static void ftrace_update_pid_func(void)
425 /* Only do something if we are tracing something */
426 if (ftrace_trace_function == ftrace_stub)
429 update_ftrace_function();
432 #ifdef CONFIG_FUNCTION_PROFILER
433 struct ftrace_profile {
434 struct hlist_node node;
436 unsigned long counter;
437 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
438 unsigned long long time;
439 unsigned long long time_squared;
443 struct ftrace_profile_page {
444 struct ftrace_profile_page *next;
446 struct ftrace_profile records[];
449 struct ftrace_profile_stat {
451 struct hlist_head *hash;
452 struct ftrace_profile_page *pages;
453 struct ftrace_profile_page *start;
454 struct tracer_stat stat;
457 #define PROFILE_RECORDS_SIZE \
458 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
460 #define PROFILES_PER_PAGE \
461 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
463 static int ftrace_profile_bits __read_mostly;
464 static int ftrace_profile_enabled __read_mostly;
466 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
467 static DEFINE_MUTEX(ftrace_profile_lock);
469 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
471 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
474 function_stat_next(void *v, int idx)
476 struct ftrace_profile *rec = v;
477 struct ftrace_profile_page *pg;
479 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
485 if ((void *)rec >= (void *)&pg->records[pg->index]) {
489 rec = &pg->records[0];
497 static void *function_stat_start(struct tracer_stat *trace)
499 struct ftrace_profile_stat *stat =
500 container_of(trace, struct ftrace_profile_stat, stat);
502 if (!stat || !stat->start)
505 return function_stat_next(&stat->start->records[0], 0);
508 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
509 /* function graph compares on total time */
510 static int function_stat_cmp(void *p1, void *p2)
512 struct ftrace_profile *a = p1;
513 struct ftrace_profile *b = p2;
515 if (a->time < b->time)
517 if (a->time > b->time)
523 /* not function graph compares against hits */
524 static int function_stat_cmp(void *p1, void *p2)
526 struct ftrace_profile *a = p1;
527 struct ftrace_profile *b = p2;
529 if (a->counter < b->counter)
531 if (a->counter > b->counter)
538 static int function_stat_headers(struct seq_file *m)
540 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
541 seq_printf(m, " Function "
544 "--- ---- --- ---\n");
546 seq_printf(m, " Function Hit\n"
552 static int function_stat_show(struct seq_file *m, void *v)
554 struct ftrace_profile *rec = v;
555 char str[KSYM_SYMBOL_LEN];
557 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
558 static struct trace_seq s;
559 unsigned long long avg;
560 unsigned long long stddev;
562 mutex_lock(&ftrace_profile_lock);
564 /* we raced with function_profile_reset() */
565 if (unlikely(rec->counter == 0)) {
570 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
571 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
573 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
576 do_div(avg, rec->counter);
578 /* Sample standard deviation (s^2) */
579 if (rec->counter <= 1)
582 stddev = rec->time_squared - rec->counter * avg * avg;
584 * Divide only 1000 for ns^2 -> us^2 conversion.
585 * trace_print_graph_duration will divide 1000 again.
587 do_div(stddev, (rec->counter - 1) * 1000);
591 trace_print_graph_duration(rec->time, &s);
592 trace_seq_puts(&s, " ");
593 trace_print_graph_duration(avg, &s);
594 trace_seq_puts(&s, " ");
595 trace_print_graph_duration(stddev, &s);
596 trace_print_seq(m, &s);
600 mutex_unlock(&ftrace_profile_lock);
605 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
607 struct ftrace_profile_page *pg;
609 pg = stat->pages = stat->start;
612 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
617 memset(stat->hash, 0,
618 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
621 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
623 struct ftrace_profile_page *pg;
628 /* If we already allocated, do nothing */
632 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
636 #ifdef CONFIG_DYNAMIC_FTRACE
637 functions = ftrace_update_tot_cnt;
640 * We do not know the number of functions that exist because
641 * dynamic tracing is what counts them. With past experience
642 * we have around 20K functions. That should be more than enough.
643 * It is highly unlikely we will execute every function in
649 pg = stat->start = stat->pages;
651 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
653 for (i = 0; i < pages; i++) {
654 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
665 unsigned long tmp = (unsigned long)pg;
671 free_page((unsigned long)stat->pages);
678 static int ftrace_profile_init_cpu(int cpu)
680 struct ftrace_profile_stat *stat;
683 stat = &per_cpu(ftrace_profile_stats, cpu);
686 /* If the profile is already created, simply reset it */
687 ftrace_profile_reset(stat);
692 * We are profiling all functions, but usually only a few thousand
693 * functions are hit. We'll make a hash of 1024 items.
695 size = FTRACE_PROFILE_HASH_SIZE;
697 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
702 if (!ftrace_profile_bits) {
705 for (; size; size >>= 1)
706 ftrace_profile_bits++;
709 /* Preallocate the function profiling pages */
710 if (ftrace_profile_pages_init(stat) < 0) {
719 static int ftrace_profile_init(void)
724 for_each_online_cpu(cpu) {
725 ret = ftrace_profile_init_cpu(cpu);
733 /* interrupts must be disabled */
734 static struct ftrace_profile *
735 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
737 struct ftrace_profile *rec;
738 struct hlist_head *hhd;
739 struct hlist_node *n;
742 key = hash_long(ip, ftrace_profile_bits);
743 hhd = &stat->hash[key];
745 if (hlist_empty(hhd))
748 hlist_for_each_entry_rcu(rec, n, hhd, node) {
756 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
757 struct ftrace_profile *rec)
761 key = hash_long(rec->ip, ftrace_profile_bits);
762 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
766 * The memory is already allocated, this simply finds a new record to use.
768 static struct ftrace_profile *
769 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
771 struct ftrace_profile *rec = NULL;
773 /* prevent recursion (from NMIs) */
774 if (atomic_inc_return(&stat->disabled) != 1)
778 * Try to find the function again since an NMI
779 * could have added it
781 rec = ftrace_find_profiled_func(stat, ip);
785 if (stat->pages->index == PROFILES_PER_PAGE) {
786 if (!stat->pages->next)
788 stat->pages = stat->pages->next;
791 rec = &stat->pages->records[stat->pages->index++];
793 ftrace_add_profile(stat, rec);
796 atomic_dec(&stat->disabled);
802 function_profile_call(unsigned long ip, unsigned long parent_ip,
803 struct ftrace_ops *ops, struct pt_regs *regs)
805 struct ftrace_profile_stat *stat;
806 struct ftrace_profile *rec;
809 if (!ftrace_profile_enabled)
812 local_irq_save(flags);
814 stat = &__get_cpu_var(ftrace_profile_stats);
815 if (!stat->hash || !ftrace_profile_enabled)
818 rec = ftrace_find_profiled_func(stat, ip);
820 rec = ftrace_profile_alloc(stat, ip);
827 local_irq_restore(flags);
830 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
831 static int profile_graph_entry(struct ftrace_graph_ent *trace)
833 function_profile_call(trace->func, 0, NULL, NULL);
837 static void profile_graph_return(struct ftrace_graph_ret *trace)
839 struct ftrace_profile_stat *stat;
840 unsigned long long calltime;
841 struct ftrace_profile *rec;
844 local_irq_save(flags);
845 stat = &__get_cpu_var(ftrace_profile_stats);
846 if (!stat->hash || !ftrace_profile_enabled)
849 /* If the calltime was zero'd ignore it */
850 if (!trace->calltime)
853 calltime = trace->rettime - trace->calltime;
855 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
858 index = trace->depth;
860 /* Append this call time to the parent time to subtract */
862 current->ret_stack[index - 1].subtime += calltime;
864 if (current->ret_stack[index].subtime < calltime)
865 calltime -= current->ret_stack[index].subtime;
870 rec = ftrace_find_profiled_func(stat, trace->func);
872 rec->time += calltime;
873 rec->time_squared += calltime * calltime;
877 local_irq_restore(flags);
880 static int register_ftrace_profiler(void)
882 return register_ftrace_graph(&profile_graph_return,
883 &profile_graph_entry);
886 static void unregister_ftrace_profiler(void)
888 unregister_ftrace_graph();
891 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
892 .func = function_profile_call,
893 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
896 static int register_ftrace_profiler(void)
898 return register_ftrace_function(&ftrace_profile_ops);
901 static void unregister_ftrace_profiler(void)
903 unregister_ftrace_function(&ftrace_profile_ops);
905 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
908 ftrace_profile_write(struct file *filp, const char __user *ubuf,
909 size_t cnt, loff_t *ppos)
914 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
920 mutex_lock(&ftrace_profile_lock);
921 if (ftrace_profile_enabled ^ val) {
923 ret = ftrace_profile_init();
929 ret = register_ftrace_profiler();
934 ftrace_profile_enabled = 1;
936 ftrace_profile_enabled = 0;
938 * unregister_ftrace_profiler calls stop_machine
939 * so this acts like an synchronize_sched.
941 unregister_ftrace_profiler();
945 mutex_unlock(&ftrace_profile_lock);
953 ftrace_profile_read(struct file *filp, char __user *ubuf,
954 size_t cnt, loff_t *ppos)
956 char buf[64]; /* big enough to hold a number */
959 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
960 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
963 static const struct file_operations ftrace_profile_fops = {
964 .open = tracing_open_generic,
965 .read = ftrace_profile_read,
966 .write = ftrace_profile_write,
967 .llseek = default_llseek,
970 /* used to initialize the real stat files */
971 static struct tracer_stat function_stats __initdata = {
973 .stat_start = function_stat_start,
974 .stat_next = function_stat_next,
975 .stat_cmp = function_stat_cmp,
976 .stat_headers = function_stat_headers,
977 .stat_show = function_stat_show
980 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
982 struct ftrace_profile_stat *stat;
983 struct dentry *entry;
988 for_each_possible_cpu(cpu) {
989 stat = &per_cpu(ftrace_profile_stats, cpu);
991 /* allocate enough for function name + cpu number */
992 name = kmalloc(32, GFP_KERNEL);
995 * The files created are permanent, if something happens
996 * we still do not free memory.
999 "Could not allocate stat file for cpu %d\n",
1003 stat->stat = function_stats;
1004 snprintf(name, 32, "function%d", cpu);
1005 stat->stat.name = name;
1006 ret = register_stat_tracer(&stat->stat);
1009 "Could not register function stat for cpu %d\n",
1016 entry = debugfs_create_file("function_profile_enabled", 0644,
1017 d_tracer, NULL, &ftrace_profile_fops);
1019 pr_warning("Could not create debugfs "
1020 "'function_profile_enabled' entry\n");
1023 #else /* CONFIG_FUNCTION_PROFILER */
1024 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1027 #endif /* CONFIG_FUNCTION_PROFILER */
1029 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1031 #ifdef CONFIG_DYNAMIC_FTRACE
1033 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1034 # error Dynamic ftrace depends on MCOUNT_RECORD
1037 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1039 struct ftrace_func_probe {
1040 struct hlist_node node;
1041 struct ftrace_probe_ops *ops;
1042 unsigned long flags;
1045 struct rcu_head rcu;
1048 struct ftrace_func_entry {
1049 struct hlist_node hlist;
1053 struct ftrace_hash {
1054 unsigned long size_bits;
1055 struct hlist_head *buckets;
1056 unsigned long count;
1057 struct rcu_head rcu;
1061 * We make these constant because no one should touch them,
1062 * but they are used as the default "empty hash", to avoid allocating
1063 * it all the time. These are in a read only section such that if
1064 * anyone does try to modify it, it will cause an exception.
1066 static const struct hlist_head empty_buckets[1];
1067 static const struct ftrace_hash empty_hash = {
1068 .buckets = (struct hlist_head *)empty_buckets,
1070 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1072 static struct ftrace_ops global_ops = {
1073 .func = ftrace_stub,
1074 .notrace_hash = EMPTY_HASH,
1075 .filter_hash = EMPTY_HASH,
1076 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
1079 static DEFINE_MUTEX(ftrace_regex_lock);
1081 struct ftrace_page {
1082 struct ftrace_page *next;
1083 struct dyn_ftrace *records;
1088 static struct ftrace_page *ftrace_new_pgs;
1090 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1091 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1093 /* estimate from running different kernels */
1094 #define NR_TO_INIT 10000
1096 static struct ftrace_page *ftrace_pages_start;
1097 static struct ftrace_page *ftrace_pages;
1099 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1101 return !hash || !hash->count;
1104 static struct ftrace_func_entry *
1105 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1108 struct ftrace_func_entry *entry;
1109 struct hlist_head *hhd;
1110 struct hlist_node *n;
1112 if (ftrace_hash_empty(hash))
1115 if (hash->size_bits > 0)
1116 key = hash_long(ip, hash->size_bits);
1120 hhd = &hash->buckets[key];
1122 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1123 if (entry->ip == ip)
1129 static void __add_hash_entry(struct ftrace_hash *hash,
1130 struct ftrace_func_entry *entry)
1132 struct hlist_head *hhd;
1135 if (hash->size_bits)
1136 key = hash_long(entry->ip, hash->size_bits);
1140 hhd = &hash->buckets[key];
1141 hlist_add_head(&entry->hlist, hhd);
1145 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1147 struct ftrace_func_entry *entry;
1149 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1154 __add_hash_entry(hash, entry);
1160 free_hash_entry(struct ftrace_hash *hash,
1161 struct ftrace_func_entry *entry)
1163 hlist_del(&entry->hlist);
1169 remove_hash_entry(struct ftrace_hash *hash,
1170 struct ftrace_func_entry *entry)
1172 hlist_del(&entry->hlist);
1176 static void ftrace_hash_clear(struct ftrace_hash *hash)
1178 struct hlist_head *hhd;
1179 struct hlist_node *tp, *tn;
1180 struct ftrace_func_entry *entry;
1181 int size = 1 << hash->size_bits;
1187 for (i = 0; i < size; i++) {
1188 hhd = &hash->buckets[i];
1189 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1190 free_hash_entry(hash, entry);
1192 FTRACE_WARN_ON(hash->count);
1195 static void free_ftrace_hash(struct ftrace_hash *hash)
1197 if (!hash || hash == EMPTY_HASH)
1199 ftrace_hash_clear(hash);
1200 kfree(hash->buckets);
1204 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1206 struct ftrace_hash *hash;
1208 hash = container_of(rcu, struct ftrace_hash, rcu);
1209 free_ftrace_hash(hash);
1212 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1214 if (!hash || hash == EMPTY_HASH)
1216 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1219 void ftrace_free_filter(struct ftrace_ops *ops)
1221 free_ftrace_hash(ops->filter_hash);
1222 free_ftrace_hash(ops->notrace_hash);
1225 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1227 struct ftrace_hash *hash;
1230 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1234 size = 1 << size_bits;
1235 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1237 if (!hash->buckets) {
1242 hash->size_bits = size_bits;
1247 static struct ftrace_hash *
1248 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1250 struct ftrace_func_entry *entry;
1251 struct ftrace_hash *new_hash;
1252 struct hlist_node *tp;
1257 new_hash = alloc_ftrace_hash(size_bits);
1262 if (ftrace_hash_empty(hash))
1265 size = 1 << hash->size_bits;
1266 for (i = 0; i < size; i++) {
1267 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1268 ret = add_hash_entry(new_hash, entry->ip);
1274 FTRACE_WARN_ON(new_hash->count != hash->count);
1279 free_ftrace_hash(new_hash);
1284 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1286 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1289 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1290 struct ftrace_hash **dst, struct ftrace_hash *src)
1292 struct ftrace_func_entry *entry;
1293 struct hlist_node *tp, *tn;
1294 struct hlist_head *hhd;
1295 struct ftrace_hash *old_hash;
1296 struct ftrace_hash *new_hash;
1298 int size = src->count;
1304 * Remove the current set, update the hash and add
1307 ftrace_hash_rec_disable(ops, enable);
1310 * If the new source is empty, just free dst and assign it
1314 free_ftrace_hash_rcu(*dst);
1315 rcu_assign_pointer(*dst, EMPTY_HASH);
1316 /* still need to update the function records */
1322 * Make the hash size about 1/2 the # found
1324 for (size /= 2; size; size >>= 1)
1327 /* Don't allocate too much */
1328 if (bits > FTRACE_HASH_MAX_BITS)
1329 bits = FTRACE_HASH_MAX_BITS;
1332 new_hash = alloc_ftrace_hash(bits);
1336 size = 1 << src->size_bits;
1337 for (i = 0; i < size; i++) {
1338 hhd = &src->buckets[i];
1339 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1341 key = hash_long(entry->ip, bits);
1344 remove_hash_entry(src, entry);
1345 __add_hash_entry(new_hash, entry);
1350 rcu_assign_pointer(*dst, new_hash);
1351 free_ftrace_hash_rcu(old_hash);
1356 * Enable regardless of ret:
1357 * On success, we enable the new hash.
1358 * On failure, we re-enable the original hash.
1360 ftrace_hash_rec_enable(ops, enable);
1366 * Test the hashes for this ops to see if we want to call
1367 * the ops->func or not.
1369 * It's a match if the ip is in the ops->filter_hash or
1370 * the filter_hash does not exist or is empty,
1372 * the ip is not in the ops->notrace_hash.
1374 * This needs to be called with preemption disabled as
1375 * the hashes are freed with call_rcu_sched().
1378 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1380 struct ftrace_hash *filter_hash;
1381 struct ftrace_hash *notrace_hash;
1384 filter_hash = rcu_dereference_raw(ops->filter_hash);
1385 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1387 if ((ftrace_hash_empty(filter_hash) ||
1388 ftrace_lookup_ip(filter_hash, ip)) &&
1389 (ftrace_hash_empty(notrace_hash) ||
1390 !ftrace_lookup_ip(notrace_hash, ip)))
1399 * This is a double for. Do not use 'break' to break out of the loop,
1400 * you must use a goto.
1402 #define do_for_each_ftrace_rec(pg, rec) \
1403 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1405 for (_____i = 0; _____i < pg->index; _____i++) { \
1406 rec = &pg->records[_____i];
1408 #define while_for_each_ftrace_rec() \
1413 static int ftrace_cmp_recs(const void *a, const void *b)
1415 const struct dyn_ftrace *key = a;
1416 const struct dyn_ftrace *rec = b;
1418 if (key->flags < rec->ip)
1420 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1425 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1427 struct ftrace_page *pg;
1428 struct dyn_ftrace *rec;
1429 struct dyn_ftrace key;
1432 key.flags = end; /* overload flags, as it is unsigned long */
1434 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1435 if (end < pg->records[0].ip ||
1436 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1438 rec = bsearch(&key, pg->records, pg->index,
1439 sizeof(struct dyn_ftrace),
1449 * ftrace_location - return true if the ip giving is a traced location
1450 * @ip: the instruction pointer to check
1452 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1453 * That is, the instruction that is either a NOP or call to
1454 * the function tracer. It checks the ftrace internal tables to
1455 * determine if the address belongs or not.
1457 unsigned long ftrace_location(unsigned long ip)
1459 return ftrace_location_range(ip, ip);
1463 * ftrace_text_reserved - return true if range contains an ftrace location
1464 * @start: start of range to search
1465 * @end: end of range to search (inclusive). @end points to the last byte to check.
1467 * Returns 1 if @start and @end contains a ftrace location.
1468 * That is, the instruction that is either a NOP or call to
1469 * the function tracer. It checks the ftrace internal tables to
1470 * determine if the address belongs or not.
1472 int ftrace_text_reserved(void *start, void *end)
1476 ret = ftrace_location_range((unsigned long)start,
1477 (unsigned long)end);
1482 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1486 struct ftrace_hash *hash;
1487 struct ftrace_hash *other_hash;
1488 struct ftrace_page *pg;
1489 struct dyn_ftrace *rec;
1493 /* Only update if the ops has been registered */
1494 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1498 * In the filter_hash case:
1499 * If the count is zero, we update all records.
1500 * Otherwise we just update the items in the hash.
1502 * In the notrace_hash case:
1503 * We enable the update in the hash.
1504 * As disabling notrace means enabling the tracing,
1505 * and enabling notrace means disabling, the inc variable
1509 hash = ops->filter_hash;
1510 other_hash = ops->notrace_hash;
1511 if (ftrace_hash_empty(hash))
1515 hash = ops->notrace_hash;
1516 other_hash = ops->filter_hash;
1518 * If the notrace hash has no items,
1519 * then there's nothing to do.
1521 if (ftrace_hash_empty(hash))
1525 do_for_each_ftrace_rec(pg, rec) {
1526 int in_other_hash = 0;
1532 * Only the filter_hash affects all records.
1533 * Update if the record is not in the notrace hash.
1535 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1538 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1539 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1544 if (filter_hash && in_hash && !in_other_hash)
1546 else if (!filter_hash && in_hash &&
1547 (in_other_hash || ftrace_hash_empty(other_hash)))
1555 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1558 * If any ops wants regs saved for this function
1559 * then all ops will get saved regs.
1561 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1562 rec->flags |= FTRACE_FL_REGS;
1564 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1569 /* Shortcut, if we handled all records, we are done. */
1570 if (!all && count == hash->count)
1572 } while_for_each_ftrace_rec();
1575 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1578 __ftrace_hash_rec_update(ops, filter_hash, 0);
1581 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1584 __ftrace_hash_rec_update(ops, filter_hash, 1);
1587 static void print_ip_ins(const char *fmt, unsigned char *p)
1591 printk(KERN_CONT "%s", fmt);
1593 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1594 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1598 * ftrace_bug - report and shutdown function tracer
1599 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1600 * @ip: The address that failed
1602 * The arch code that enables or disables the function tracing
1603 * can call ftrace_bug() when it has detected a problem in
1604 * modifying the code. @failed should be one of either:
1605 * EFAULT - if the problem happens on reading the @ip address
1606 * EINVAL - if what is read at @ip is not what was expected
1607 * EPERM - if the problem happens on writting to the @ip address
1609 void ftrace_bug(int failed, unsigned long ip)
1613 FTRACE_WARN_ON_ONCE(1);
1614 pr_info("ftrace faulted on modifying ");
1618 FTRACE_WARN_ON_ONCE(1);
1619 pr_info("ftrace failed to modify ");
1621 print_ip_ins(" actual: ", (unsigned char *)ip);
1622 printk(KERN_CONT "\n");
1625 FTRACE_WARN_ON_ONCE(1);
1626 pr_info("ftrace faulted on writing ");
1630 FTRACE_WARN_ON_ONCE(1);
1631 pr_info("ftrace faulted on unknown error ");
1636 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1638 unsigned long flag = 0UL;
1641 * If we are updating calls:
1643 * If the record has a ref count, then we need to enable it
1644 * because someone is using it.
1646 * Otherwise we make sure its disabled.
1648 * If we are disabling calls, then disable all records that
1651 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1652 flag = FTRACE_FL_ENABLED;
1655 * If enabling and the REGS flag does not match the REGS_EN, then
1656 * do not ignore this record. Set flags to fail the compare against
1660 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1661 flag |= FTRACE_FL_REGS;
1663 /* If the state of this record hasn't changed, then do nothing */
1664 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1665 return FTRACE_UPDATE_IGNORE;
1668 /* Save off if rec is being enabled (for return value) */
1669 flag ^= rec->flags & FTRACE_FL_ENABLED;
1672 rec->flags |= FTRACE_FL_ENABLED;
1673 if (flag & FTRACE_FL_REGS) {
1674 if (rec->flags & FTRACE_FL_REGS)
1675 rec->flags |= FTRACE_FL_REGS_EN;
1677 rec->flags &= ~FTRACE_FL_REGS_EN;
1682 * If this record is being updated from a nop, then
1683 * return UPDATE_MAKE_CALL.
1684 * Otherwise, if the EN flag is set, then return
1685 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1686 * from the non-save regs, to a save regs function.
1688 * return UPDATE_MODIFY_CALL to tell the caller to convert
1689 * from the save regs, to a non-save regs function.
1691 if (flag & FTRACE_FL_ENABLED)
1692 return FTRACE_UPDATE_MAKE_CALL;
1693 else if (rec->flags & FTRACE_FL_REGS_EN)
1694 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1696 return FTRACE_UPDATE_MODIFY_CALL;
1700 /* If there's no more users, clear all flags */
1701 if (!(rec->flags & ~FTRACE_FL_MASK))
1704 /* Just disable the record (keep REGS state) */
1705 rec->flags &= ~FTRACE_FL_ENABLED;
1708 return FTRACE_UPDATE_MAKE_NOP;
1712 * ftrace_update_record, set a record that now is tracing or not
1713 * @rec: the record to update
1714 * @enable: set to 1 if the record is tracing, zero to force disable
1716 * The records that represent all functions that can be traced need
1717 * to be updated when tracing has been enabled.
1719 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1721 return ftrace_check_record(rec, enable, 1);
1725 * ftrace_test_record, check if the record has been enabled or not
1726 * @rec: the record to test
1727 * @enable: set to 1 to check if enabled, 0 if it is disabled
1729 * The arch code may need to test if a record is already set to
1730 * tracing to determine how to modify the function code that it
1733 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1735 return ftrace_check_record(rec, enable, 0);
1739 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1741 unsigned long ftrace_old_addr;
1742 unsigned long ftrace_addr;
1745 ret = ftrace_update_record(rec, enable);
1747 if (rec->flags & FTRACE_FL_REGS)
1748 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1750 ftrace_addr = (unsigned long)FTRACE_ADDR;
1753 case FTRACE_UPDATE_IGNORE:
1756 case FTRACE_UPDATE_MAKE_CALL:
1757 return ftrace_make_call(rec, ftrace_addr);
1759 case FTRACE_UPDATE_MAKE_NOP:
1760 return ftrace_make_nop(NULL, rec, ftrace_addr);
1762 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1763 case FTRACE_UPDATE_MODIFY_CALL:
1764 if (rec->flags & FTRACE_FL_REGS)
1765 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1767 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1769 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1772 return -1; /* unknow ftrace bug */
1775 void __weak ftrace_replace_code(int enable)
1777 struct dyn_ftrace *rec;
1778 struct ftrace_page *pg;
1781 if (unlikely(ftrace_disabled))
1784 do_for_each_ftrace_rec(pg, rec) {
1785 failed = __ftrace_replace_code(rec, enable);
1787 ftrace_bug(failed, rec->ip);
1788 /* Stop processing */
1791 } while_for_each_ftrace_rec();
1794 struct ftrace_rec_iter {
1795 struct ftrace_page *pg;
1800 * ftrace_rec_iter_start, start up iterating over traced functions
1802 * Returns an iterator handle that is used to iterate over all
1803 * the records that represent address locations where functions
1806 * May return NULL if no records are available.
1808 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1811 * We only use a single iterator.
1812 * Protected by the ftrace_lock mutex.
1814 static struct ftrace_rec_iter ftrace_rec_iter;
1815 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1817 iter->pg = ftrace_pages_start;
1820 /* Could have empty pages */
1821 while (iter->pg && !iter->pg->index)
1822 iter->pg = iter->pg->next;
1831 * ftrace_rec_iter_next, get the next record to process.
1832 * @iter: The handle to the iterator.
1834 * Returns the next iterator after the given iterator @iter.
1836 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1840 if (iter->index >= iter->pg->index) {
1841 iter->pg = iter->pg->next;
1844 /* Could have empty pages */
1845 while (iter->pg && !iter->pg->index)
1846 iter->pg = iter->pg->next;
1856 * ftrace_rec_iter_record, get the record at the iterator location
1857 * @iter: The current iterator location
1859 * Returns the record that the current @iter is at.
1861 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1863 return &iter->pg->records[iter->index];
1867 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1874 if (unlikely(ftrace_disabled))
1877 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1879 ftrace_bug(ret, ip);
1886 * archs can override this function if they must do something
1887 * before the modifying code is performed.
1889 int __weak ftrace_arch_code_modify_prepare(void)
1895 * archs can override this function if they must do something
1896 * after the modifying code is performed.
1898 int __weak ftrace_arch_code_modify_post_process(void)
1903 void ftrace_modify_all_code(int command)
1905 if (command & FTRACE_UPDATE_CALLS)
1906 ftrace_replace_code(1);
1907 else if (command & FTRACE_DISABLE_CALLS)
1908 ftrace_replace_code(0);
1910 if (command & FTRACE_UPDATE_TRACE_FUNC)
1911 ftrace_update_ftrace_func(ftrace_trace_function);
1913 if (command & FTRACE_START_FUNC_RET)
1914 ftrace_enable_ftrace_graph_caller();
1915 else if (command & FTRACE_STOP_FUNC_RET)
1916 ftrace_disable_ftrace_graph_caller();
1919 static int __ftrace_modify_code(void *data)
1921 int *command = data;
1923 ftrace_modify_all_code(*command);
1929 * ftrace_run_stop_machine, go back to the stop machine method
1930 * @command: The command to tell ftrace what to do
1932 * If an arch needs to fall back to the stop machine method, the
1933 * it can call this function.
1935 void ftrace_run_stop_machine(int command)
1937 stop_machine(__ftrace_modify_code, &command, NULL);
1941 * arch_ftrace_update_code, modify the code to trace or not trace
1942 * @command: The command that needs to be done
1944 * Archs can override this function if it does not need to
1945 * run stop_machine() to modify code.
1947 void __weak arch_ftrace_update_code(int command)
1949 ftrace_run_stop_machine(command);
1952 static void ftrace_run_update_code(int command)
1956 ret = ftrace_arch_code_modify_prepare();
1957 FTRACE_WARN_ON(ret);
1961 * Do not call function tracer while we update the code.
1962 * We are in stop machine.
1964 function_trace_stop++;
1967 * By default we use stop_machine() to modify the code.
1968 * But archs can do what ever they want as long as it
1969 * is safe. The stop_machine() is the safest, but also
1970 * produces the most overhead.
1972 arch_ftrace_update_code(command);
1974 function_trace_stop--;
1976 ret = ftrace_arch_code_modify_post_process();
1977 FTRACE_WARN_ON(ret);
1980 static ftrace_func_t saved_ftrace_func;
1981 static int ftrace_start_up;
1982 static int global_start_up;
1984 static void ftrace_startup_enable(int command)
1986 if (saved_ftrace_func != ftrace_trace_function) {
1987 saved_ftrace_func = ftrace_trace_function;
1988 command |= FTRACE_UPDATE_TRACE_FUNC;
1991 if (!command || !ftrace_enabled)
1994 ftrace_run_update_code(command);
1997 static int ftrace_startup(struct ftrace_ops *ops, int command)
1999 bool hash_enable = true;
2001 if (unlikely(ftrace_disabled))
2005 command |= FTRACE_UPDATE_CALLS;
2007 /* ops marked global share the filter hashes */
2008 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2010 /* Don't update hash if global is already set */
2011 if (global_start_up)
2012 hash_enable = false;
2016 ops->flags |= FTRACE_OPS_FL_ENABLED;
2018 ftrace_hash_rec_enable(ops, 1);
2020 ftrace_startup_enable(command);
2025 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
2027 bool hash_disable = true;
2029 if (unlikely(ftrace_disabled))
2034 * Just warn in case of unbalance, no need to kill ftrace, it's not
2035 * critical but the ftrace_call callers may be never nopped again after
2036 * further ftrace uses.
2038 WARN_ON_ONCE(ftrace_start_up < 0);
2040 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2043 WARN_ON_ONCE(global_start_up < 0);
2044 /* Don't update hash if global still has users */
2045 if (global_start_up) {
2046 WARN_ON_ONCE(!ftrace_start_up);
2047 hash_disable = false;
2052 ftrace_hash_rec_disable(ops, 1);
2054 if (ops != &global_ops || !global_start_up)
2055 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2057 command |= FTRACE_UPDATE_CALLS;
2059 if (saved_ftrace_func != ftrace_trace_function) {
2060 saved_ftrace_func = ftrace_trace_function;
2061 command |= FTRACE_UPDATE_TRACE_FUNC;
2064 if (!command || !ftrace_enabled)
2067 ftrace_run_update_code(command);
2070 static void ftrace_startup_sysctl(void)
2072 if (unlikely(ftrace_disabled))
2075 /* Force update next time */
2076 saved_ftrace_func = NULL;
2077 /* ftrace_start_up is true if we want ftrace running */
2078 if (ftrace_start_up)
2079 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2082 static void ftrace_shutdown_sysctl(void)
2084 if (unlikely(ftrace_disabled))
2087 /* ftrace_start_up is true if ftrace is running */
2088 if (ftrace_start_up)
2089 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2092 static cycle_t ftrace_update_time;
2093 static unsigned long ftrace_update_cnt;
2094 unsigned long ftrace_update_tot_cnt;
2096 static int ops_traces_mod(struct ftrace_ops *ops)
2098 struct ftrace_hash *hash;
2100 hash = ops->filter_hash;
2101 return ftrace_hash_empty(hash);
2104 static int ftrace_update_code(struct module *mod)
2106 struct ftrace_page *pg;
2107 struct dyn_ftrace *p;
2108 cycle_t start, stop;
2109 unsigned long ref = 0;
2113 * When adding a module, we need to check if tracers are
2114 * currently enabled and if they are set to trace all functions.
2115 * If they are, we need to enable the module functions as well
2116 * as update the reference counts for those function records.
2119 struct ftrace_ops *ops;
2121 for (ops = ftrace_ops_list;
2122 ops != &ftrace_list_end; ops = ops->next) {
2123 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2124 ops_traces_mod(ops))
2129 start = ftrace_now(raw_smp_processor_id());
2130 ftrace_update_cnt = 0;
2132 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2134 for (i = 0; i < pg->index; i++) {
2135 /* If something went wrong, bail without enabling anything */
2136 if (unlikely(ftrace_disabled))
2139 p = &pg->records[i];
2143 * Do the initial record conversion from mcount jump
2144 * to the NOP instructions.
2146 if (!ftrace_code_disable(mod, p))
2149 ftrace_update_cnt++;
2152 * If the tracing is enabled, go ahead and enable the record.
2154 * The reason not to enable the record immediatelly is the
2155 * inherent check of ftrace_make_nop/ftrace_make_call for
2156 * correct previous instructions. Making first the NOP
2157 * conversion puts the module to the correct state, thus
2158 * passing the ftrace_make_call check.
2160 if (ftrace_start_up && ref) {
2161 int failed = __ftrace_replace_code(p, 1);
2163 ftrace_bug(failed, p->ip);
2168 ftrace_new_pgs = NULL;
2170 stop = ftrace_now(raw_smp_processor_id());
2171 ftrace_update_time = stop - start;
2172 ftrace_update_tot_cnt += ftrace_update_cnt;
2177 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2182 if (WARN_ON(!count))
2185 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2188 * We want to fill as much as possible. No more than a page
2191 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2195 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2198 /* if we can't allocate this size, try something smaller */
2205 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2214 static struct ftrace_page *
2215 ftrace_allocate_pages(unsigned long num_to_init)
2217 struct ftrace_page *start_pg;
2218 struct ftrace_page *pg;
2225 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2230 * Try to allocate as much as possible in one continues
2231 * location that fills in all of the space. We want to
2232 * waste as little space as possible.
2235 cnt = ftrace_allocate_records(pg, num_to_init);
2243 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2254 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2255 free_pages((unsigned long)pg->records, order);
2256 start_pg = pg->next;
2260 pr_info("ftrace: FAILED to allocate memory for functions\n");
2264 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2269 pr_info("ftrace: No functions to be traced?\n");
2273 cnt = num_to_init / ENTRIES_PER_PAGE;
2274 pr_info("ftrace: allocating %ld entries in %d pages\n",
2275 num_to_init, cnt + 1);
2280 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2282 struct ftrace_iterator {
2285 struct ftrace_page *pg;
2286 struct dyn_ftrace *func;
2287 struct ftrace_func_probe *probe;
2288 struct trace_parser parser;
2289 struct ftrace_hash *hash;
2290 struct ftrace_ops *ops;
2297 t_hash_next(struct seq_file *m, loff_t *pos)
2299 struct ftrace_iterator *iter = m->private;
2300 struct hlist_node *hnd = NULL;
2301 struct hlist_head *hhd;
2307 hnd = &iter->probe->node;
2309 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2312 hhd = &ftrace_func_hash[iter->hidx];
2314 if (hlist_empty(hhd)) {
2330 if (WARN_ON_ONCE(!hnd))
2333 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2338 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2340 struct ftrace_iterator *iter = m->private;
2344 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2347 if (iter->func_pos > *pos)
2351 for (l = 0; l <= (*pos - iter->func_pos); ) {
2352 p = t_hash_next(m, &l);
2359 /* Only set this if we have an item */
2360 iter->flags |= FTRACE_ITER_HASH;
2366 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2368 struct ftrace_func_probe *rec;
2371 if (WARN_ON_ONCE(!rec))
2374 if (rec->ops->print)
2375 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2377 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2380 seq_printf(m, ":%p", rec->data);
2387 t_next(struct seq_file *m, void *v, loff_t *pos)
2389 struct ftrace_iterator *iter = m->private;
2390 struct ftrace_ops *ops = iter->ops;
2391 struct dyn_ftrace *rec = NULL;
2393 if (unlikely(ftrace_disabled))
2396 if (iter->flags & FTRACE_ITER_HASH)
2397 return t_hash_next(m, pos);
2400 iter->pos = iter->func_pos = *pos;
2402 if (iter->flags & FTRACE_ITER_PRINTALL)
2403 return t_hash_start(m, pos);
2406 if (iter->idx >= iter->pg->index) {
2407 if (iter->pg->next) {
2408 iter->pg = iter->pg->next;
2413 rec = &iter->pg->records[iter->idx++];
2414 if (((iter->flags & FTRACE_ITER_FILTER) &&
2415 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2417 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2418 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2420 ((iter->flags & FTRACE_ITER_ENABLED) &&
2421 !(rec->flags & ~FTRACE_FL_MASK))) {
2429 return t_hash_start(m, pos);
2436 static void reset_iter_read(struct ftrace_iterator *iter)
2440 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2443 static void *t_start(struct seq_file *m, loff_t *pos)
2445 struct ftrace_iterator *iter = m->private;
2446 struct ftrace_ops *ops = iter->ops;
2450 mutex_lock(&ftrace_lock);
2452 if (unlikely(ftrace_disabled))
2456 * If an lseek was done, then reset and start from beginning.
2458 if (*pos < iter->pos)
2459 reset_iter_read(iter);
2462 * For set_ftrace_filter reading, if we have the filter
2463 * off, we can short cut and just print out that all
2464 * functions are enabled.
2466 if (iter->flags & FTRACE_ITER_FILTER &&
2467 ftrace_hash_empty(ops->filter_hash)) {
2469 return t_hash_start(m, pos);
2470 iter->flags |= FTRACE_ITER_PRINTALL;
2471 /* reset in case of seek/pread */
2472 iter->flags &= ~FTRACE_ITER_HASH;
2476 if (iter->flags & FTRACE_ITER_HASH)
2477 return t_hash_start(m, pos);
2480 * Unfortunately, we need to restart at ftrace_pages_start
2481 * every time we let go of the ftrace_mutex. This is because
2482 * those pointers can change without the lock.
2484 iter->pg = ftrace_pages_start;
2486 for (l = 0; l <= *pos; ) {
2487 p = t_next(m, p, &l);
2493 return t_hash_start(m, pos);
2498 static void t_stop(struct seq_file *m, void *p)
2500 mutex_unlock(&ftrace_lock);
2503 static int t_show(struct seq_file *m, void *v)
2505 struct ftrace_iterator *iter = m->private;
2506 struct dyn_ftrace *rec;
2508 if (iter->flags & FTRACE_ITER_HASH)
2509 return t_hash_show(m, iter);
2511 if (iter->flags & FTRACE_ITER_PRINTALL) {
2512 seq_printf(m, "#### all functions enabled ####\n");
2521 seq_printf(m, "%ps", (void *)rec->ip);
2522 if (iter->flags & FTRACE_ITER_ENABLED)
2523 seq_printf(m, " (%ld)%s",
2524 rec->flags & ~FTRACE_FL_MASK,
2525 rec->flags & FTRACE_FL_REGS ? " R" : "");
2526 seq_printf(m, "\n");
2531 static const struct seq_operations show_ftrace_seq_ops = {
2539 ftrace_avail_open(struct inode *inode, struct file *file)
2541 struct ftrace_iterator *iter;
2543 if (unlikely(ftrace_disabled))
2546 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2548 iter->pg = ftrace_pages_start;
2549 iter->ops = &global_ops;
2552 return iter ? 0 : -ENOMEM;
2556 ftrace_enabled_open(struct inode *inode, struct file *file)
2558 struct ftrace_iterator *iter;
2560 if (unlikely(ftrace_disabled))
2563 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2565 iter->pg = ftrace_pages_start;
2566 iter->flags = FTRACE_ITER_ENABLED;
2567 iter->ops = &global_ops;
2570 return iter ? 0 : -ENOMEM;
2573 static void ftrace_filter_reset(struct ftrace_hash *hash)
2575 mutex_lock(&ftrace_lock);
2576 ftrace_hash_clear(hash);
2577 mutex_unlock(&ftrace_lock);
2581 * ftrace_regex_open - initialize function tracer filter files
2582 * @ops: The ftrace_ops that hold the hash filters
2583 * @flag: The type of filter to process
2584 * @inode: The inode, usually passed in to your open routine
2585 * @file: The file, usually passed in to your open routine
2587 * ftrace_regex_open() initializes the filter files for the
2588 * @ops. Depending on @flag it may process the filter hash or
2589 * the notrace hash of @ops. With this called from the open
2590 * routine, you can use ftrace_filter_write() for the write
2591 * routine if @flag has FTRACE_ITER_FILTER set, or
2592 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2593 * ftrace_regex_lseek() should be used as the lseek routine, and
2594 * release must call ftrace_regex_release().
2597 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2598 struct inode *inode, struct file *file)
2600 struct ftrace_iterator *iter;
2601 struct ftrace_hash *hash;
2604 if (unlikely(ftrace_disabled))
2607 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2611 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2616 if (flag & FTRACE_ITER_NOTRACE)
2617 hash = ops->notrace_hash;
2619 hash = ops->filter_hash;
2624 if (file->f_mode & FMODE_WRITE) {
2625 mutex_lock(&ftrace_lock);
2626 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2627 mutex_unlock(&ftrace_lock);
2630 trace_parser_put(&iter->parser);
2636 mutex_lock(&ftrace_regex_lock);
2638 if ((file->f_mode & FMODE_WRITE) &&
2639 (file->f_flags & O_TRUNC))
2640 ftrace_filter_reset(iter->hash);
2642 if (file->f_mode & FMODE_READ) {
2643 iter->pg = ftrace_pages_start;
2645 ret = seq_open(file, &show_ftrace_seq_ops);
2647 struct seq_file *m = file->private_data;
2651 free_ftrace_hash(iter->hash);
2652 trace_parser_put(&iter->parser);
2656 file->private_data = iter;
2657 mutex_unlock(&ftrace_regex_lock);
2663 ftrace_filter_open(struct inode *inode, struct file *file)
2665 return ftrace_regex_open(&global_ops,
2666 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2671 ftrace_notrace_open(struct inode *inode, struct file *file)
2673 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2678 ftrace_regex_lseek(struct file *file, loff_t offset, int whence)
2682 if (file->f_mode & FMODE_READ)
2683 ret = seq_lseek(file, offset, whence);
2685 file->f_pos = ret = 1;
2690 static int ftrace_match(char *str, char *regex, int len, int type)
2697 if (strcmp(str, regex) == 0)
2700 case MATCH_FRONT_ONLY:
2701 if (strncmp(str, regex, len) == 0)
2704 case MATCH_MIDDLE_ONLY:
2705 if (strstr(str, regex))
2708 case MATCH_END_ONLY:
2710 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2719 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2721 struct ftrace_func_entry *entry;
2724 entry = ftrace_lookup_ip(hash, rec->ip);
2726 /* Do nothing if it doesn't exist */
2730 free_hash_entry(hash, entry);
2732 /* Do nothing if it exists */
2736 ret = add_hash_entry(hash, rec->ip);
2742 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2743 char *regex, int len, int type)
2745 char str[KSYM_SYMBOL_LEN];
2748 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2751 /* module lookup requires matching the module */
2752 if (!modname || strcmp(modname, mod))
2755 /* blank search means to match all funcs in the mod */
2760 return ftrace_match(str, regex, len, type);
2764 match_records(struct ftrace_hash *hash, char *buff,
2765 int len, char *mod, int not)
2767 unsigned search_len = 0;
2768 struct ftrace_page *pg;
2769 struct dyn_ftrace *rec;
2770 int type = MATCH_FULL;
2771 char *search = buff;
2776 type = filter_parse_regex(buff, len, &search, ¬);
2777 search_len = strlen(search);
2780 mutex_lock(&ftrace_lock);
2782 if (unlikely(ftrace_disabled))
2785 do_for_each_ftrace_rec(pg, rec) {
2786 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2787 ret = enter_record(hash, rec, not);
2794 } while_for_each_ftrace_rec();
2796 mutex_unlock(&ftrace_lock);
2802 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2804 return match_records(hash, buff, len, NULL, 0);
2808 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2812 /* blank or '*' mean the same */
2813 if (strcmp(buff, "*") == 0)
2816 /* handle the case of 'dont filter this module' */
2817 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2822 return match_records(hash, buff, strlen(buff), mod, not);
2826 * We register the module command as a template to show others how
2827 * to register the a command as well.
2831 ftrace_mod_callback(struct ftrace_hash *hash,
2832 char *func, char *cmd, char *param, int enable)
2838 * cmd == 'mod' because we only registered this func
2839 * for the 'mod' ftrace_func_command.
2840 * But if you register one func with multiple commands,
2841 * you can tell which command was used by the cmd
2845 /* we must have a module name */
2849 mod = strsep(¶m, ":");
2853 ret = ftrace_match_module_records(hash, func, mod);
2862 static struct ftrace_func_command ftrace_mod_cmd = {
2864 .func = ftrace_mod_callback,
2867 static int __init ftrace_mod_cmd_init(void)
2869 return register_ftrace_command(&ftrace_mod_cmd);
2871 core_initcall(ftrace_mod_cmd_init);
2873 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2874 struct ftrace_ops *op, struct pt_regs *pt_regs)
2876 struct ftrace_func_probe *entry;
2877 struct hlist_head *hhd;
2878 struct hlist_node *n;
2881 key = hash_long(ip, FTRACE_HASH_BITS);
2883 hhd = &ftrace_func_hash[key];
2885 if (hlist_empty(hhd))
2889 * Disable preemption for these calls to prevent a RCU grace
2890 * period. This syncs the hash iteration and freeing of items
2891 * on the hash. rcu_read_lock is too dangerous here.
2893 preempt_disable_notrace();
2894 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2895 if (entry->ip == ip)
2896 entry->ops->func(ip, parent_ip, &entry->data);
2898 preempt_enable_notrace();
2901 static struct ftrace_ops trace_probe_ops __read_mostly =
2903 .func = function_trace_probe_call,
2906 static int ftrace_probe_registered;
2908 static void __enable_ftrace_function_probe(void)
2913 if (ftrace_probe_registered)
2916 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2917 struct hlist_head *hhd = &ftrace_func_hash[i];
2921 /* Nothing registered? */
2922 if (i == FTRACE_FUNC_HASHSIZE)
2925 ret = __register_ftrace_function(&trace_probe_ops);
2927 ret = ftrace_startup(&trace_probe_ops, 0);
2929 ftrace_probe_registered = 1;
2932 static void __disable_ftrace_function_probe(void)
2937 if (!ftrace_probe_registered)
2940 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2941 struct hlist_head *hhd = &ftrace_func_hash[i];
2946 /* no more funcs left */
2947 ret = __unregister_ftrace_function(&trace_probe_ops);
2949 ftrace_shutdown(&trace_probe_ops, 0);
2951 ftrace_probe_registered = 0;
2955 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2957 struct ftrace_func_probe *entry =
2958 container_of(rhp, struct ftrace_func_probe, rcu);
2960 if (entry->ops->free)
2961 entry->ops->free(&entry->data);
2967 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2970 struct ftrace_func_probe *entry;
2971 struct ftrace_page *pg;
2972 struct dyn_ftrace *rec;
2978 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2979 len = strlen(search);
2981 /* we do not support '!' for function probes */
2985 mutex_lock(&ftrace_lock);
2987 if (unlikely(ftrace_disabled))
2990 do_for_each_ftrace_rec(pg, rec) {
2992 if (!ftrace_match_record(rec, NULL, search, len, type))
2995 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2997 /* If we did not process any, then return error */
3008 * The caller might want to do something special
3009 * for each function we find. We call the callback
3010 * to give the caller an opportunity to do so.
3012 if (ops->callback) {
3013 if (ops->callback(rec->ip, &entry->data) < 0) {
3014 /* caller does not like this func */
3021 entry->ip = rec->ip;
3023 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3024 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3026 } while_for_each_ftrace_rec();
3027 __enable_ftrace_function_probe();
3030 mutex_unlock(&ftrace_lock);
3036 PROBE_TEST_FUNC = 1,
3041 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3042 void *data, int flags)
3044 struct ftrace_func_probe *entry;
3045 struct hlist_node *n, *tmp;
3046 char str[KSYM_SYMBOL_LEN];
3047 int type = MATCH_FULL;
3051 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3056 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3057 len = strlen(search);
3059 /* we do not support '!' for function probes */
3064 mutex_lock(&ftrace_lock);
3065 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3066 struct hlist_head *hhd = &ftrace_func_hash[i];
3068 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3070 /* break up if statements for readability */
3071 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3074 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3077 /* do this last, since it is the most expensive */
3079 kallsyms_lookup(entry->ip, NULL, NULL,
3081 if (!ftrace_match(str, glob, len, type))
3085 hlist_del(&entry->node);
3086 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3089 __disable_ftrace_function_probe();
3090 mutex_unlock(&ftrace_lock);
3094 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3097 __unregister_ftrace_function_probe(glob, ops, data,
3098 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3102 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3104 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3107 void unregister_ftrace_function_probe_all(char *glob)
3109 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3112 static LIST_HEAD(ftrace_commands);
3113 static DEFINE_MUTEX(ftrace_cmd_mutex);
3115 int register_ftrace_command(struct ftrace_func_command *cmd)
3117 struct ftrace_func_command *p;
3120 mutex_lock(&ftrace_cmd_mutex);
3121 list_for_each_entry(p, &ftrace_commands, list) {
3122 if (strcmp(cmd->name, p->name) == 0) {
3127 list_add(&cmd->list, &ftrace_commands);
3129 mutex_unlock(&ftrace_cmd_mutex);
3134 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3136 struct ftrace_func_command *p, *n;
3139 mutex_lock(&ftrace_cmd_mutex);
3140 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3141 if (strcmp(cmd->name, p->name) == 0) {
3143 list_del_init(&p->list);
3148 mutex_unlock(&ftrace_cmd_mutex);
3153 static int ftrace_process_regex(struct ftrace_hash *hash,
3154 char *buff, int len, int enable)
3156 char *func, *command, *next = buff;
3157 struct ftrace_func_command *p;
3160 func = strsep(&next, ":");
3163 ret = ftrace_match_records(hash, func, len);
3173 command = strsep(&next, ":");
3175 mutex_lock(&ftrace_cmd_mutex);
3176 list_for_each_entry(p, &ftrace_commands, list) {
3177 if (strcmp(p->name, command) == 0) {
3178 ret = p->func(hash, func, command, next, enable);
3183 mutex_unlock(&ftrace_cmd_mutex);
3189 ftrace_regex_write(struct file *file, const char __user *ubuf,
3190 size_t cnt, loff_t *ppos, int enable)
3192 struct ftrace_iterator *iter;
3193 struct trace_parser *parser;
3199 mutex_lock(&ftrace_regex_lock);
3202 if (unlikely(ftrace_disabled))
3205 if (file->f_mode & FMODE_READ) {
3206 struct seq_file *m = file->private_data;
3209 iter = file->private_data;
3211 parser = &iter->parser;
3212 read = trace_get_user(parser, ubuf, cnt, ppos);
3214 if (read >= 0 && trace_parser_loaded(parser) &&
3215 !trace_parser_cont(parser)) {
3216 ret = ftrace_process_regex(iter->hash, parser->buffer,
3217 parser->idx, enable);
3218 trace_parser_clear(parser);
3225 mutex_unlock(&ftrace_regex_lock);
3231 ftrace_filter_write(struct file *file, const char __user *ubuf,
3232 size_t cnt, loff_t *ppos)
3234 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3238 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3239 size_t cnt, loff_t *ppos)
3241 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3245 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3247 struct ftrace_func_entry *entry;
3249 if (!ftrace_location(ip))
3253 entry = ftrace_lookup_ip(hash, ip);
3256 free_hash_entry(hash, entry);
3260 return add_hash_entry(hash, ip);
3264 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3265 unsigned long ip, int remove, int reset, int enable)
3267 struct ftrace_hash **orig_hash;
3268 struct ftrace_hash *hash;
3271 /* All global ops uses the global ops filters */
3272 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3275 if (unlikely(ftrace_disabled))
3279 orig_hash = &ops->filter_hash;
3281 orig_hash = &ops->notrace_hash;
3283 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3287 mutex_lock(&ftrace_regex_lock);
3289 ftrace_filter_reset(hash);
3290 if (buf && !ftrace_match_records(hash, buf, len)) {
3292 goto out_regex_unlock;
3295 ret = ftrace_match_addr(hash, ip, remove);
3297 goto out_regex_unlock;
3300 mutex_lock(&ftrace_lock);
3301 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3302 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3304 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3306 mutex_unlock(&ftrace_lock);
3309 mutex_unlock(&ftrace_regex_lock);
3311 free_ftrace_hash(hash);
3316 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3317 int reset, int enable)
3319 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3323 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3324 * @ops - the ops to set the filter with
3325 * @ip - the address to add to or remove from the filter.
3326 * @remove - non zero to remove the ip from the filter
3327 * @reset - non zero to reset all filters before applying this filter.
3329 * Filters denote which functions should be enabled when tracing is enabled
3330 * If @ip is NULL, it failes to update filter.
3332 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3333 int remove, int reset)
3335 return ftrace_set_addr(ops, ip, remove, reset, 1);
3337 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3340 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3341 int reset, int enable)
3343 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3347 * ftrace_set_filter - set a function to filter on in ftrace
3348 * @ops - the ops to set the filter with
3349 * @buf - the string that holds the function filter text.
3350 * @len - the length of the string.
3351 * @reset - non zero to reset all filters before applying this filter.
3353 * Filters denote which functions should be enabled when tracing is enabled.
3354 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3356 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3359 return ftrace_set_regex(ops, buf, len, reset, 1);
3361 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3364 * ftrace_set_notrace - set a function to not trace in ftrace
3365 * @ops - the ops to set the notrace filter with
3366 * @buf - the string that holds the function notrace text.
3367 * @len - the length of the string.
3368 * @reset - non zero to reset all filters before applying this filter.
3370 * Notrace Filters denote which functions should not be enabled when tracing
3371 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3374 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3377 return ftrace_set_regex(ops, buf, len, reset, 0);
3379 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3381 * ftrace_set_filter - set a function to filter on in ftrace
3382 * @ops - the ops to set the filter with
3383 * @buf - the string that holds the function filter text.
3384 * @len - the length of the string.
3385 * @reset - non zero to reset all filters before applying this filter.
3387 * Filters denote which functions should be enabled when tracing is enabled.
3388 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3390 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3392 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3394 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3397 * ftrace_set_notrace - set a function to not trace in ftrace
3398 * @ops - the ops to set the notrace filter with
3399 * @buf - the string that holds the function notrace text.
3400 * @len - the length of the string.
3401 * @reset - non zero to reset all filters before applying this filter.
3403 * Notrace Filters denote which functions should not be enabled when tracing
3404 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3407 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3409 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3411 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3414 * command line interface to allow users to set filters on boot up.
3416 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3417 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3418 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3420 static int __init set_ftrace_notrace(char *str)
3422 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3425 __setup("ftrace_notrace=", set_ftrace_notrace);
3427 static int __init set_ftrace_filter(char *str)
3429 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3432 __setup("ftrace_filter=", set_ftrace_filter);
3434 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3435 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3436 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3438 static int __init set_graph_function(char *str)
3440 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3443 __setup("ftrace_graph_filter=", set_graph_function);
3445 static void __init set_ftrace_early_graph(char *buf)
3451 func = strsep(&buf, ",");
3452 /* we allow only one expression at a time */
3453 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3456 printk(KERN_DEBUG "ftrace: function %s not "
3457 "traceable\n", func);
3460 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3463 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3468 func = strsep(&buf, ",");
3469 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3473 static void __init set_ftrace_early_filters(void)
3475 if (ftrace_filter_buf[0])
3476 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3477 if (ftrace_notrace_buf[0])
3478 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3479 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3480 if (ftrace_graph_buf[0])
3481 set_ftrace_early_graph(ftrace_graph_buf);
3482 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3485 int ftrace_regex_release(struct inode *inode, struct file *file)
3487 struct seq_file *m = (struct seq_file *)file->private_data;
3488 struct ftrace_iterator *iter;
3489 struct ftrace_hash **orig_hash;
3490 struct trace_parser *parser;
3494 mutex_lock(&ftrace_regex_lock);
3495 if (file->f_mode & FMODE_READ) {
3498 seq_release(inode, file);
3500 iter = file->private_data;
3502 parser = &iter->parser;
3503 if (trace_parser_loaded(parser)) {
3504 parser->buffer[parser->idx] = 0;
3505 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3508 trace_parser_put(parser);
3510 if (file->f_mode & FMODE_WRITE) {
3511 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3514 orig_hash = &iter->ops->filter_hash;
3516 orig_hash = &iter->ops->notrace_hash;
3518 mutex_lock(&ftrace_lock);
3519 ret = ftrace_hash_move(iter->ops, filter_hash,
3520 orig_hash, iter->hash);
3521 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3523 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3525 mutex_unlock(&ftrace_lock);
3527 free_ftrace_hash(iter->hash);
3530 mutex_unlock(&ftrace_regex_lock);
3534 static const struct file_operations ftrace_avail_fops = {
3535 .open = ftrace_avail_open,
3537 .llseek = seq_lseek,
3538 .release = seq_release_private,
3541 static const struct file_operations ftrace_enabled_fops = {
3542 .open = ftrace_enabled_open,
3544 .llseek = seq_lseek,
3545 .release = seq_release_private,
3548 static const struct file_operations ftrace_filter_fops = {
3549 .open = ftrace_filter_open,
3551 .write = ftrace_filter_write,
3552 .llseek = ftrace_regex_lseek,
3553 .release = ftrace_regex_release,
3556 static const struct file_operations ftrace_notrace_fops = {
3557 .open = ftrace_notrace_open,
3559 .write = ftrace_notrace_write,
3560 .llseek = ftrace_regex_lseek,
3561 .release = ftrace_regex_release,
3564 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3566 static DEFINE_MUTEX(graph_lock);
3568 int ftrace_graph_count;
3569 int ftrace_graph_filter_enabled;
3570 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3573 __g_next(struct seq_file *m, loff_t *pos)
3575 if (*pos >= ftrace_graph_count)
3577 return &ftrace_graph_funcs[*pos];
3581 g_next(struct seq_file *m, void *v, loff_t *pos)
3584 return __g_next(m, pos);
3587 static void *g_start(struct seq_file *m, loff_t *pos)
3589 mutex_lock(&graph_lock);
3591 /* Nothing, tell g_show to print all functions are enabled */
3592 if (!ftrace_graph_filter_enabled && !*pos)
3595 return __g_next(m, pos);
3598 static void g_stop(struct seq_file *m, void *p)
3600 mutex_unlock(&graph_lock);
3603 static int g_show(struct seq_file *m, void *v)
3605 unsigned long *ptr = v;
3610 if (ptr == (unsigned long *)1) {
3611 seq_printf(m, "#### all functions enabled ####\n");
3615 seq_printf(m, "%ps\n", (void *)*ptr);
3620 static const struct seq_operations ftrace_graph_seq_ops = {
3628 ftrace_graph_open(struct inode *inode, struct file *file)
3632 if (unlikely(ftrace_disabled))
3635 mutex_lock(&graph_lock);
3636 if ((file->f_mode & FMODE_WRITE) &&
3637 (file->f_flags & O_TRUNC)) {
3638 ftrace_graph_filter_enabled = 0;
3639 ftrace_graph_count = 0;
3640 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3642 mutex_unlock(&graph_lock);
3644 if (file->f_mode & FMODE_READ)
3645 ret = seq_open(file, &ftrace_graph_seq_ops);
3651 ftrace_graph_release(struct inode *inode, struct file *file)
3653 if (file->f_mode & FMODE_READ)
3654 seq_release(inode, file);
3659 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3661 struct dyn_ftrace *rec;
3662 struct ftrace_page *pg;
3671 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3672 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3675 search_len = strlen(search);
3677 mutex_lock(&ftrace_lock);
3679 if (unlikely(ftrace_disabled)) {
3680 mutex_unlock(&ftrace_lock);
3684 do_for_each_ftrace_rec(pg, rec) {
3686 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3687 /* if it is in the array */
3689 for (i = 0; i < *idx; i++) {
3690 if (array[i] == rec->ip) {
3699 array[(*idx)++] = rec->ip;
3700 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3705 array[i] = array[--(*idx)];
3711 } while_for_each_ftrace_rec();
3713 mutex_unlock(&ftrace_lock);
3718 ftrace_graph_filter_enabled = 1;
3723 ftrace_graph_write(struct file *file, const char __user *ubuf,
3724 size_t cnt, loff_t *ppos)
3726 struct trace_parser parser;
3732 mutex_lock(&graph_lock);
3734 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3739 read = trace_get_user(&parser, ubuf, cnt, ppos);
3741 if (read >= 0 && trace_parser_loaded((&parser))) {
3742 parser.buffer[parser.idx] = 0;
3744 /* we allow only one expression at a time */
3745 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3754 trace_parser_put(&parser);
3756 mutex_unlock(&graph_lock);
3761 static const struct file_operations ftrace_graph_fops = {
3762 .open = ftrace_graph_open,
3764 .write = ftrace_graph_write,
3765 .release = ftrace_graph_release,
3766 .llseek = seq_lseek,
3768 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3770 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3773 trace_create_file("available_filter_functions", 0444,
3774 d_tracer, NULL, &ftrace_avail_fops);
3776 trace_create_file("enabled_functions", 0444,
3777 d_tracer, NULL, &ftrace_enabled_fops);
3779 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3780 NULL, &ftrace_filter_fops);
3782 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3783 NULL, &ftrace_notrace_fops);
3785 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3786 trace_create_file("set_graph_function", 0444, d_tracer,
3788 &ftrace_graph_fops);
3789 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3794 static int ftrace_cmp_ips(const void *a, const void *b)
3796 const unsigned long *ipa = a;
3797 const unsigned long *ipb = b;
3806 static void ftrace_swap_ips(void *a, void *b, int size)
3808 unsigned long *ipa = a;
3809 unsigned long *ipb = b;
3817 static int ftrace_process_locs(struct module *mod,
3818 unsigned long *start,
3821 struct ftrace_page *start_pg;
3822 struct ftrace_page *pg;
3823 struct dyn_ftrace *rec;
3824 unsigned long count;
3827 unsigned long flags = 0; /* Shut up gcc */
3830 count = end - start;
3835 sort(start, count, sizeof(*start),
3836 ftrace_cmp_ips, ftrace_swap_ips);
3838 start_pg = ftrace_allocate_pages(count);
3842 mutex_lock(&ftrace_lock);
3845 * Core and each module needs their own pages, as
3846 * modules will free them when they are removed.
3847 * Force a new page to be allocated for modules.
3850 WARN_ON(ftrace_pages || ftrace_pages_start);
3851 /* First initialization */
3852 ftrace_pages = ftrace_pages_start = start_pg;
3857 if (WARN_ON(ftrace_pages->next)) {
3858 /* Hmm, we have free pages? */
3859 while (ftrace_pages->next)
3860 ftrace_pages = ftrace_pages->next;
3863 ftrace_pages->next = start_pg;
3869 addr = ftrace_call_adjust(*p++);
3871 * Some architecture linkers will pad between
3872 * the different mcount_loc sections of different
3873 * object files to satisfy alignments.
3874 * Skip any NULL pointers.
3879 if (pg->index == pg->size) {
3880 /* We should have allocated enough */
3881 if (WARN_ON(!pg->next))
3886 rec = &pg->records[pg->index++];
3890 /* We should have used all pages */
3893 /* Assign the last page to ftrace_pages */
3896 /* These new locations need to be initialized */
3897 ftrace_new_pgs = start_pg;
3900 * We only need to disable interrupts on start up
3901 * because we are modifying code that an interrupt
3902 * may execute, and the modification is not atomic.
3903 * But for modules, nothing runs the code we modify
3904 * until we are finished with it, and there's no
3905 * reason to cause large interrupt latencies while we do it.
3908 local_irq_save(flags);
3909 ftrace_update_code(mod);
3911 local_irq_restore(flags);
3914 mutex_unlock(&ftrace_lock);
3919 #ifdef CONFIG_MODULES
3921 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3923 void ftrace_release_mod(struct module *mod)
3925 struct dyn_ftrace *rec;
3926 struct ftrace_page **last_pg;
3927 struct ftrace_page *pg;
3930 mutex_lock(&ftrace_lock);
3932 if (ftrace_disabled)
3936 * Each module has its own ftrace_pages, remove
3937 * them from the list.
3939 last_pg = &ftrace_pages_start;
3940 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3941 rec = &pg->records[0];
3942 if (within_module_core(rec->ip, mod)) {
3944 * As core pages are first, the first
3945 * page should never be a module page.
3947 if (WARN_ON(pg == ftrace_pages_start))
3950 /* Check if we are deleting the last page */
3951 if (pg == ftrace_pages)
3952 ftrace_pages = next_to_ftrace_page(last_pg);
3954 *last_pg = pg->next;
3955 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3956 free_pages((unsigned long)pg->records, order);
3959 last_pg = &pg->next;
3962 mutex_unlock(&ftrace_lock);
3965 static void ftrace_init_module(struct module *mod,
3966 unsigned long *start, unsigned long *end)
3968 if (ftrace_disabled || start == end)
3970 ftrace_process_locs(mod, start, end);
3973 static int ftrace_module_notify(struct notifier_block *self,
3974 unsigned long val, void *data)
3976 struct module *mod = data;
3979 case MODULE_STATE_COMING:
3980 ftrace_init_module(mod, mod->ftrace_callsites,
3981 mod->ftrace_callsites +
3982 mod->num_ftrace_callsites);
3984 case MODULE_STATE_GOING:
3985 ftrace_release_mod(mod);
3992 static int ftrace_module_notify(struct notifier_block *self,
3993 unsigned long val, void *data)
3997 #endif /* CONFIG_MODULES */
3999 struct notifier_block ftrace_module_nb = {
4000 .notifier_call = ftrace_module_notify,
4001 .priority = INT_MAX, /* Run before anything that can use kprobes */
4004 extern unsigned long __start_mcount_loc[];
4005 extern unsigned long __stop_mcount_loc[];
4007 void __init ftrace_init(void)
4009 unsigned long count, addr, flags;
4012 /* Keep the ftrace pointer to the stub */
4013 addr = (unsigned long)ftrace_stub;
4015 local_irq_save(flags);
4016 ftrace_dyn_arch_init(&addr);
4017 local_irq_restore(flags);
4019 /* ftrace_dyn_arch_init places the return code in addr */
4023 count = __stop_mcount_loc - __start_mcount_loc;
4025 ret = ftrace_dyn_table_alloc(count);
4029 last_ftrace_enabled = ftrace_enabled = 1;
4031 ret = ftrace_process_locs(NULL,
4035 ret = register_module_notifier(&ftrace_module_nb);
4037 pr_warning("Failed to register trace ftrace module notifier\n");
4039 set_ftrace_early_filters();
4043 ftrace_disabled = 1;
4048 static struct ftrace_ops global_ops = {
4049 .func = ftrace_stub,
4050 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4053 static int __init ftrace_nodyn_init(void)
4058 core_initcall(ftrace_nodyn_init);
4060 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4061 static inline void ftrace_startup_enable(int command) { }
4062 /* Keep as macros so we do not need to define the commands */
4063 # define ftrace_startup(ops, command) \
4065 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4068 # define ftrace_shutdown(ops, command) do { } while (0)
4069 # define ftrace_startup_sysctl() do { } while (0)
4070 # define ftrace_shutdown_sysctl() do { } while (0)
4073 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4078 #endif /* CONFIG_DYNAMIC_FTRACE */
4081 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4082 struct ftrace_ops *op, struct pt_regs *regs)
4084 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4088 * Some of the ops may be dynamically allocated,
4089 * they must be freed after a synchronize_sched().
4091 preempt_disable_notrace();
4092 trace_recursion_set(TRACE_CONTROL_BIT);
4093 op = rcu_dereference_raw(ftrace_control_list);
4094 while (op != &ftrace_list_end) {
4095 if (!ftrace_function_local_disabled(op) &&
4096 ftrace_ops_test(op, ip))
4097 op->func(ip, parent_ip, op, regs);
4099 op = rcu_dereference_raw(op->next);
4101 trace_recursion_clear(TRACE_CONTROL_BIT);
4102 preempt_enable_notrace();
4105 static struct ftrace_ops control_ops = {
4106 .func = ftrace_ops_control_func,
4107 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4111 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4112 struct ftrace_ops *ignored, struct pt_regs *regs)
4114 struct ftrace_ops *op;
4116 if (function_trace_stop)
4119 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4122 trace_recursion_set(TRACE_INTERNAL_BIT);
4124 * Some of the ops may be dynamically allocated,
4125 * they must be freed after a synchronize_sched().
4127 preempt_disable_notrace();
4128 op = rcu_dereference_raw(ftrace_ops_list);
4129 while (op != &ftrace_list_end) {
4130 if (ftrace_ops_test(op, ip))
4131 op->func(ip, parent_ip, op, regs);
4132 op = rcu_dereference_raw(op->next);
4134 preempt_enable_notrace();
4135 trace_recursion_clear(TRACE_INTERNAL_BIT);
4139 * Some archs only support passing ip and parent_ip. Even though
4140 * the list function ignores the op parameter, we do not want any
4141 * C side effects, where a function is called without the caller
4142 * sending a third parameter.
4143 * Archs are to support both the regs and ftrace_ops at the same time.
4144 * If they support ftrace_ops, it is assumed they support regs.
4145 * If call backs want to use regs, they must either check for regs
4146 * being NULL, or ARCH_SUPPORTS_FTRACE_SAVE_REGS.
4147 * Note, ARCH_SUPPORT_SAVE_REGS expects a full regs to be saved.
4148 * An architecture can pass partial regs with ftrace_ops and still
4149 * set the ARCH_SUPPORT_FTARCE_OPS.
4151 #if ARCH_SUPPORTS_FTRACE_OPS
4152 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4153 struct ftrace_ops *op, struct pt_regs *regs)
4155 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4158 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4160 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4164 static void clear_ftrace_swapper(void)
4166 struct task_struct *p;
4170 for_each_online_cpu(cpu) {
4172 clear_tsk_trace_trace(p);
4177 static void set_ftrace_swapper(void)
4179 struct task_struct *p;
4183 for_each_online_cpu(cpu) {
4185 set_tsk_trace_trace(p);
4190 static void clear_ftrace_pid(struct pid *pid)
4192 struct task_struct *p;
4195 do_each_pid_task(pid, PIDTYPE_PID, p) {
4196 clear_tsk_trace_trace(p);
4197 } while_each_pid_task(pid, PIDTYPE_PID, p);
4203 static void set_ftrace_pid(struct pid *pid)
4205 struct task_struct *p;
4208 do_each_pid_task(pid, PIDTYPE_PID, p) {
4209 set_tsk_trace_trace(p);
4210 } while_each_pid_task(pid, PIDTYPE_PID, p);
4214 static void clear_ftrace_pid_task(struct pid *pid)
4216 if (pid == ftrace_swapper_pid)
4217 clear_ftrace_swapper();
4219 clear_ftrace_pid(pid);
4222 static void set_ftrace_pid_task(struct pid *pid)
4224 if (pid == ftrace_swapper_pid)
4225 set_ftrace_swapper();
4227 set_ftrace_pid(pid);
4230 static int ftrace_pid_add(int p)
4233 struct ftrace_pid *fpid;
4236 mutex_lock(&ftrace_lock);
4239 pid = ftrace_swapper_pid;
4241 pid = find_get_pid(p);
4248 list_for_each_entry(fpid, &ftrace_pids, list)
4249 if (fpid->pid == pid)
4254 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4258 list_add(&fpid->list, &ftrace_pids);
4261 set_ftrace_pid_task(pid);
4263 ftrace_update_pid_func();
4264 ftrace_startup_enable(0);
4266 mutex_unlock(&ftrace_lock);
4270 if (pid != ftrace_swapper_pid)
4274 mutex_unlock(&ftrace_lock);
4278 static void ftrace_pid_reset(void)
4280 struct ftrace_pid *fpid, *safe;
4282 mutex_lock(&ftrace_lock);
4283 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4284 struct pid *pid = fpid->pid;
4286 clear_ftrace_pid_task(pid);
4288 list_del(&fpid->list);
4292 ftrace_update_pid_func();
4293 ftrace_startup_enable(0);
4295 mutex_unlock(&ftrace_lock);
4298 static void *fpid_start(struct seq_file *m, loff_t *pos)
4300 mutex_lock(&ftrace_lock);
4302 if (list_empty(&ftrace_pids) && (!*pos))
4305 return seq_list_start(&ftrace_pids, *pos);
4308 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4313 return seq_list_next(v, &ftrace_pids, pos);
4316 static void fpid_stop(struct seq_file *m, void *p)
4318 mutex_unlock(&ftrace_lock);
4321 static int fpid_show(struct seq_file *m, void *v)
4323 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4325 if (v == (void *)1) {
4326 seq_printf(m, "no pid\n");
4330 if (fpid->pid == ftrace_swapper_pid)
4331 seq_printf(m, "swapper tasks\n");
4333 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4338 static const struct seq_operations ftrace_pid_sops = {
4339 .start = fpid_start,
4346 ftrace_pid_open(struct inode *inode, struct file *file)
4350 if ((file->f_mode & FMODE_WRITE) &&
4351 (file->f_flags & O_TRUNC))
4354 if (file->f_mode & FMODE_READ)
4355 ret = seq_open(file, &ftrace_pid_sops);
4361 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4362 size_t cnt, loff_t *ppos)
4368 if (cnt >= sizeof(buf))
4371 if (copy_from_user(&buf, ubuf, cnt))
4377 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4378 * to clean the filter quietly.
4380 tmp = strstrip(buf);
4381 if (strlen(tmp) == 0)
4384 ret = kstrtol(tmp, 10, &val);
4388 ret = ftrace_pid_add(val);
4390 return ret ? ret : cnt;
4394 ftrace_pid_release(struct inode *inode, struct file *file)
4396 if (file->f_mode & FMODE_READ)
4397 seq_release(inode, file);
4402 static const struct file_operations ftrace_pid_fops = {
4403 .open = ftrace_pid_open,
4404 .write = ftrace_pid_write,
4406 .llseek = seq_lseek,
4407 .release = ftrace_pid_release,
4410 static __init int ftrace_init_debugfs(void)
4412 struct dentry *d_tracer;
4414 d_tracer = tracing_init_dentry();
4418 ftrace_init_dyn_debugfs(d_tracer);
4420 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4421 NULL, &ftrace_pid_fops);
4423 ftrace_profile_debugfs(d_tracer);
4427 fs_initcall(ftrace_init_debugfs);
4430 * ftrace_kill - kill ftrace
4432 * This function should be used by panic code. It stops ftrace
4433 * but in a not so nice way. If you need to simply kill ftrace
4434 * from a non-atomic section, use ftrace_kill.
4436 void ftrace_kill(void)
4438 ftrace_disabled = 1;
4440 clear_ftrace_function();
4444 * Test if ftrace is dead or not.
4446 int ftrace_is_dead(void)
4448 return ftrace_disabled;
4452 * register_ftrace_function - register a function for profiling
4453 * @ops - ops structure that holds the function for profiling.
4455 * Register a function to be called by all functions in the
4458 * Note: @ops->func and all the functions it calls must be labeled
4459 * with "notrace", otherwise it will go into a
4462 int register_ftrace_function(struct ftrace_ops *ops)
4466 mutex_lock(&ftrace_lock);
4468 ret = __register_ftrace_function(ops);
4470 ret = ftrace_startup(ops, 0);
4472 mutex_unlock(&ftrace_lock);
4476 EXPORT_SYMBOL_GPL(register_ftrace_function);
4479 * unregister_ftrace_function - unregister a function for profiling.
4480 * @ops - ops structure that holds the function to unregister
4482 * Unregister a function that was added to be called by ftrace profiling.
4484 int unregister_ftrace_function(struct ftrace_ops *ops)
4488 mutex_lock(&ftrace_lock);
4489 ret = __unregister_ftrace_function(ops);
4491 ftrace_shutdown(ops, 0);
4492 mutex_unlock(&ftrace_lock);
4496 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4499 ftrace_enable_sysctl(struct ctl_table *table, int write,
4500 void __user *buffer, size_t *lenp,
4505 mutex_lock(&ftrace_lock);
4507 if (unlikely(ftrace_disabled))
4510 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4512 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4515 last_ftrace_enabled = !!ftrace_enabled;
4517 if (ftrace_enabled) {
4519 ftrace_startup_sysctl();
4521 /* we are starting ftrace again */
4522 if (ftrace_ops_list != &ftrace_list_end) {
4523 if (ftrace_ops_list->next == &ftrace_list_end)
4524 ftrace_trace_function = ftrace_ops_list->func;
4526 ftrace_trace_function = ftrace_ops_list_func;
4530 /* stopping ftrace calls (just send to ftrace_stub) */
4531 ftrace_trace_function = ftrace_stub;
4533 ftrace_shutdown_sysctl();
4537 mutex_unlock(&ftrace_lock);
4541 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4543 static int ftrace_graph_active;
4544 static struct notifier_block ftrace_suspend_notifier;
4546 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4551 /* The callbacks that hook a function */
4552 trace_func_graph_ret_t ftrace_graph_return =
4553 (trace_func_graph_ret_t)ftrace_stub;
4554 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4556 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4557 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4561 unsigned long flags;
4562 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4563 struct task_struct *g, *t;
4565 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4566 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4567 * sizeof(struct ftrace_ret_stack),
4569 if (!ret_stack_list[i]) {
4577 read_lock_irqsave(&tasklist_lock, flags);
4578 do_each_thread(g, t) {
4584 if (t->ret_stack == NULL) {
4585 atomic_set(&t->tracing_graph_pause, 0);
4586 atomic_set(&t->trace_overrun, 0);
4587 t->curr_ret_stack = -1;
4588 /* Make sure the tasks see the -1 first: */
4590 t->ret_stack = ret_stack_list[start++];
4592 } while_each_thread(g, t);
4595 read_unlock_irqrestore(&tasklist_lock, flags);
4597 for (i = start; i < end; i++)
4598 kfree(ret_stack_list[i]);
4603 ftrace_graph_probe_sched_switch(void *ignore,
4604 struct task_struct *prev, struct task_struct *next)
4606 unsigned long long timestamp;
4610 * Does the user want to count the time a function was asleep.
4611 * If so, do not update the time stamps.
4613 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4616 timestamp = trace_clock_local();
4618 prev->ftrace_timestamp = timestamp;
4620 /* only process tasks that we timestamped */
4621 if (!next->ftrace_timestamp)
4625 * Update all the counters in next to make up for the
4626 * time next was sleeping.
4628 timestamp -= next->ftrace_timestamp;
4630 for (index = next->curr_ret_stack; index >= 0; index--)
4631 next->ret_stack[index].calltime += timestamp;
4634 /* Allocate a return stack for each task */
4635 static int start_graph_tracing(void)
4637 struct ftrace_ret_stack **ret_stack_list;
4640 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4641 sizeof(struct ftrace_ret_stack *),
4644 if (!ret_stack_list)
4647 /* The cpu_boot init_task->ret_stack will never be freed */
4648 for_each_online_cpu(cpu) {
4649 if (!idle_task(cpu)->ret_stack)
4650 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4654 ret = alloc_retstack_tasklist(ret_stack_list);
4655 } while (ret == -EAGAIN);
4658 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4660 pr_info("ftrace_graph: Couldn't activate tracepoint"
4661 " probe to kernel_sched_switch\n");
4664 kfree(ret_stack_list);
4669 * Hibernation protection.
4670 * The state of the current task is too much unstable during
4671 * suspend/restore to disk. We want to protect against that.
4674 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4678 case PM_HIBERNATION_PREPARE:
4679 pause_graph_tracing();
4682 case PM_POST_HIBERNATION:
4683 unpause_graph_tracing();
4689 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4690 trace_func_graph_ent_t entryfunc)
4694 mutex_lock(&ftrace_lock);
4696 /* we currently allow only one tracer registered at a time */
4697 if (ftrace_graph_active) {
4702 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4703 register_pm_notifier(&ftrace_suspend_notifier);
4705 ftrace_graph_active++;
4706 ret = start_graph_tracing();
4708 ftrace_graph_active--;
4712 ftrace_graph_return = retfunc;
4713 ftrace_graph_entry = entryfunc;
4715 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4718 mutex_unlock(&ftrace_lock);
4722 void unregister_ftrace_graph(void)
4724 mutex_lock(&ftrace_lock);
4726 if (unlikely(!ftrace_graph_active))
4729 ftrace_graph_active--;
4730 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4731 ftrace_graph_entry = ftrace_graph_entry_stub;
4732 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4733 unregister_pm_notifier(&ftrace_suspend_notifier);
4734 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4737 mutex_unlock(&ftrace_lock);
4740 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4743 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4745 atomic_set(&t->tracing_graph_pause, 0);
4746 atomic_set(&t->trace_overrun, 0);
4747 t->ftrace_timestamp = 0;
4748 /* make curr_ret_stack visible before we add the ret_stack */
4750 t->ret_stack = ret_stack;
4754 * Allocate a return stack for the idle task. May be the first
4755 * time through, or it may be done by CPU hotplug online.
4757 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4759 t->curr_ret_stack = -1;
4761 * The idle task has no parent, it either has its own
4762 * stack or no stack at all.
4765 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4767 if (ftrace_graph_active) {
4768 struct ftrace_ret_stack *ret_stack;
4770 ret_stack = per_cpu(idle_ret_stack, cpu);
4772 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4773 * sizeof(struct ftrace_ret_stack),
4777 per_cpu(idle_ret_stack, cpu) = ret_stack;
4779 graph_init_task(t, ret_stack);
4783 /* Allocate a return stack for newly created task */
4784 void ftrace_graph_init_task(struct task_struct *t)
4786 /* Make sure we do not use the parent ret_stack */
4787 t->ret_stack = NULL;
4788 t->curr_ret_stack = -1;
4790 if (ftrace_graph_active) {
4791 struct ftrace_ret_stack *ret_stack;
4793 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4794 * sizeof(struct ftrace_ret_stack),
4798 graph_init_task(t, ret_stack);
4802 void ftrace_graph_exit_task(struct task_struct *t)
4804 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4806 t->ret_stack = NULL;
4807 /* NULL must become visible to IRQs before we free it: */
4813 void ftrace_graph_stop(void)