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;
677 static int ftrace_profile_init_cpu(int cpu)
679 struct ftrace_profile_stat *stat;
682 stat = &per_cpu(ftrace_profile_stats, cpu);
685 /* If the profile is already created, simply reset it */
686 ftrace_profile_reset(stat);
691 * We are profiling all functions, but usually only a few thousand
692 * functions are hit. We'll make a hash of 1024 items.
694 size = FTRACE_PROFILE_HASH_SIZE;
696 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
701 if (!ftrace_profile_bits) {
704 for (; size; size >>= 1)
705 ftrace_profile_bits++;
708 /* Preallocate the function profiling pages */
709 if (ftrace_profile_pages_init(stat) < 0) {
718 static int ftrace_profile_init(void)
723 for_each_online_cpu(cpu) {
724 ret = ftrace_profile_init_cpu(cpu);
732 /* interrupts must be disabled */
733 static struct ftrace_profile *
734 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
736 struct ftrace_profile *rec;
737 struct hlist_head *hhd;
738 struct hlist_node *n;
741 key = hash_long(ip, ftrace_profile_bits);
742 hhd = &stat->hash[key];
744 if (hlist_empty(hhd))
747 hlist_for_each_entry_rcu(rec, n, hhd, node) {
755 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
756 struct ftrace_profile *rec)
760 key = hash_long(rec->ip, ftrace_profile_bits);
761 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
765 * The memory is already allocated, this simply finds a new record to use.
767 static struct ftrace_profile *
768 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
770 struct ftrace_profile *rec = NULL;
772 /* prevent recursion (from NMIs) */
773 if (atomic_inc_return(&stat->disabled) != 1)
777 * Try to find the function again since an NMI
778 * could have added it
780 rec = ftrace_find_profiled_func(stat, ip);
784 if (stat->pages->index == PROFILES_PER_PAGE) {
785 if (!stat->pages->next)
787 stat->pages = stat->pages->next;
790 rec = &stat->pages->records[stat->pages->index++];
792 ftrace_add_profile(stat, rec);
795 atomic_dec(&stat->disabled);
801 function_profile_call(unsigned long ip, unsigned long parent_ip,
802 struct ftrace_ops *ops, struct pt_regs *regs)
804 struct ftrace_profile_stat *stat;
805 struct ftrace_profile *rec;
808 if (!ftrace_profile_enabled)
811 local_irq_save(flags);
813 stat = &__get_cpu_var(ftrace_profile_stats);
814 if (!stat->hash || !ftrace_profile_enabled)
817 rec = ftrace_find_profiled_func(stat, ip);
819 rec = ftrace_profile_alloc(stat, ip);
826 local_irq_restore(flags);
829 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
830 static int profile_graph_entry(struct ftrace_graph_ent *trace)
832 function_profile_call(trace->func, 0, NULL, NULL);
836 static void profile_graph_return(struct ftrace_graph_ret *trace)
838 struct ftrace_profile_stat *stat;
839 unsigned long long calltime;
840 struct ftrace_profile *rec;
843 local_irq_save(flags);
844 stat = &__get_cpu_var(ftrace_profile_stats);
845 if (!stat->hash || !ftrace_profile_enabled)
848 /* If the calltime was zero'd ignore it */
849 if (!trace->calltime)
852 calltime = trace->rettime - trace->calltime;
854 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
857 index = trace->depth;
859 /* Append this call time to the parent time to subtract */
861 current->ret_stack[index - 1].subtime += calltime;
863 if (current->ret_stack[index].subtime < calltime)
864 calltime -= current->ret_stack[index].subtime;
869 rec = ftrace_find_profiled_func(stat, trace->func);
871 rec->time += calltime;
872 rec->time_squared += calltime * calltime;
876 local_irq_restore(flags);
879 static int register_ftrace_profiler(void)
881 return register_ftrace_graph(&profile_graph_return,
882 &profile_graph_entry);
885 static void unregister_ftrace_profiler(void)
887 unregister_ftrace_graph();
890 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
891 .func = function_profile_call,
892 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
895 static int register_ftrace_profiler(void)
897 return register_ftrace_function(&ftrace_profile_ops);
900 static void unregister_ftrace_profiler(void)
902 unregister_ftrace_function(&ftrace_profile_ops);
904 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
907 ftrace_profile_write(struct file *filp, const char __user *ubuf,
908 size_t cnt, loff_t *ppos)
913 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
919 mutex_lock(&ftrace_profile_lock);
920 if (ftrace_profile_enabled ^ val) {
922 ret = ftrace_profile_init();
928 ret = register_ftrace_profiler();
933 ftrace_profile_enabled = 1;
935 ftrace_profile_enabled = 0;
937 * unregister_ftrace_profiler calls stop_machine
938 * so this acts like an synchronize_sched.
940 unregister_ftrace_profiler();
944 mutex_unlock(&ftrace_profile_lock);
952 ftrace_profile_read(struct file *filp, char __user *ubuf,
953 size_t cnt, loff_t *ppos)
955 char buf[64]; /* big enough to hold a number */
958 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
959 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
962 static const struct file_operations ftrace_profile_fops = {
963 .open = tracing_open_generic,
964 .read = ftrace_profile_read,
965 .write = ftrace_profile_write,
966 .llseek = default_llseek,
969 /* used to initialize the real stat files */
970 static struct tracer_stat function_stats __initdata = {
972 .stat_start = function_stat_start,
973 .stat_next = function_stat_next,
974 .stat_cmp = function_stat_cmp,
975 .stat_headers = function_stat_headers,
976 .stat_show = function_stat_show
979 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
981 struct ftrace_profile_stat *stat;
982 struct dentry *entry;
987 for_each_possible_cpu(cpu) {
988 stat = &per_cpu(ftrace_profile_stats, cpu);
990 /* allocate enough for function name + cpu number */
991 name = kmalloc(32, GFP_KERNEL);
994 * The files created are permanent, if something happens
995 * we still do not free memory.
998 "Could not allocate stat file for cpu %d\n",
1002 stat->stat = function_stats;
1003 snprintf(name, 32, "function%d", cpu);
1004 stat->stat.name = name;
1005 ret = register_stat_tracer(&stat->stat);
1008 "Could not register function stat for cpu %d\n",
1015 entry = debugfs_create_file("function_profile_enabled", 0644,
1016 d_tracer, NULL, &ftrace_profile_fops);
1018 pr_warning("Could not create debugfs "
1019 "'function_profile_enabled' entry\n");
1022 #else /* CONFIG_FUNCTION_PROFILER */
1023 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1026 #endif /* CONFIG_FUNCTION_PROFILER */
1028 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1030 #ifdef CONFIG_DYNAMIC_FTRACE
1032 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1033 # error Dynamic ftrace depends on MCOUNT_RECORD
1036 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1038 struct ftrace_func_probe {
1039 struct hlist_node node;
1040 struct ftrace_probe_ops *ops;
1041 unsigned long flags;
1044 struct rcu_head rcu;
1047 struct ftrace_func_entry {
1048 struct hlist_node hlist;
1052 struct ftrace_hash {
1053 unsigned long size_bits;
1054 struct hlist_head *buckets;
1055 unsigned long count;
1056 struct rcu_head rcu;
1060 * We make these constant because no one should touch them,
1061 * but they are used as the default "empty hash", to avoid allocating
1062 * it all the time. These are in a read only section such that if
1063 * anyone does try to modify it, it will cause an exception.
1065 static const struct hlist_head empty_buckets[1];
1066 static const struct ftrace_hash empty_hash = {
1067 .buckets = (struct hlist_head *)empty_buckets,
1069 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1071 static struct ftrace_ops global_ops = {
1072 .func = ftrace_stub,
1073 .notrace_hash = EMPTY_HASH,
1074 .filter_hash = EMPTY_HASH,
1075 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
1078 static DEFINE_MUTEX(ftrace_regex_lock);
1080 struct ftrace_page {
1081 struct ftrace_page *next;
1082 struct dyn_ftrace *records;
1087 static struct ftrace_page *ftrace_new_pgs;
1089 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1090 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1092 /* estimate from running different kernels */
1093 #define NR_TO_INIT 10000
1095 static struct ftrace_page *ftrace_pages_start;
1096 static struct ftrace_page *ftrace_pages;
1098 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1100 return !hash || !hash->count;
1103 static struct ftrace_func_entry *
1104 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1107 struct ftrace_func_entry *entry;
1108 struct hlist_head *hhd;
1109 struct hlist_node *n;
1111 if (ftrace_hash_empty(hash))
1114 if (hash->size_bits > 0)
1115 key = hash_long(ip, hash->size_bits);
1119 hhd = &hash->buckets[key];
1121 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1122 if (entry->ip == ip)
1128 static void __add_hash_entry(struct ftrace_hash *hash,
1129 struct ftrace_func_entry *entry)
1131 struct hlist_head *hhd;
1134 if (hash->size_bits)
1135 key = hash_long(entry->ip, hash->size_bits);
1139 hhd = &hash->buckets[key];
1140 hlist_add_head(&entry->hlist, hhd);
1144 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1146 struct ftrace_func_entry *entry;
1148 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1153 __add_hash_entry(hash, entry);
1159 free_hash_entry(struct ftrace_hash *hash,
1160 struct ftrace_func_entry *entry)
1162 hlist_del(&entry->hlist);
1168 remove_hash_entry(struct ftrace_hash *hash,
1169 struct ftrace_func_entry *entry)
1171 hlist_del(&entry->hlist);
1175 static void ftrace_hash_clear(struct ftrace_hash *hash)
1177 struct hlist_head *hhd;
1178 struct hlist_node *tp, *tn;
1179 struct ftrace_func_entry *entry;
1180 int size = 1 << hash->size_bits;
1186 for (i = 0; i < size; i++) {
1187 hhd = &hash->buckets[i];
1188 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1189 free_hash_entry(hash, entry);
1191 FTRACE_WARN_ON(hash->count);
1194 static void free_ftrace_hash(struct ftrace_hash *hash)
1196 if (!hash || hash == EMPTY_HASH)
1198 ftrace_hash_clear(hash);
1199 kfree(hash->buckets);
1203 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1205 struct ftrace_hash *hash;
1207 hash = container_of(rcu, struct ftrace_hash, rcu);
1208 free_ftrace_hash(hash);
1211 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1213 if (!hash || hash == EMPTY_HASH)
1215 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1218 void ftrace_free_filter(struct ftrace_ops *ops)
1220 free_ftrace_hash(ops->filter_hash);
1221 free_ftrace_hash(ops->notrace_hash);
1224 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1226 struct ftrace_hash *hash;
1229 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1233 size = 1 << size_bits;
1234 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1236 if (!hash->buckets) {
1241 hash->size_bits = size_bits;
1246 static struct ftrace_hash *
1247 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1249 struct ftrace_func_entry *entry;
1250 struct ftrace_hash *new_hash;
1251 struct hlist_node *tp;
1256 new_hash = alloc_ftrace_hash(size_bits);
1261 if (ftrace_hash_empty(hash))
1264 size = 1 << hash->size_bits;
1265 for (i = 0; i < size; i++) {
1266 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1267 ret = add_hash_entry(new_hash, entry->ip);
1273 FTRACE_WARN_ON(new_hash->count != hash->count);
1278 free_ftrace_hash(new_hash);
1283 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1285 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1288 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1289 struct ftrace_hash **dst, struct ftrace_hash *src)
1291 struct ftrace_func_entry *entry;
1292 struct hlist_node *tp, *tn;
1293 struct hlist_head *hhd;
1294 struct ftrace_hash *old_hash;
1295 struct ftrace_hash *new_hash;
1297 int size = src->count;
1303 * Remove the current set, update the hash and add
1306 ftrace_hash_rec_disable(ops, enable);
1309 * If the new source is empty, just free dst and assign it
1313 free_ftrace_hash_rcu(*dst);
1314 rcu_assign_pointer(*dst, EMPTY_HASH);
1315 /* still need to update the function records */
1321 * Make the hash size about 1/2 the # found
1323 for (size /= 2; size; size >>= 1)
1326 /* Don't allocate too much */
1327 if (bits > FTRACE_HASH_MAX_BITS)
1328 bits = FTRACE_HASH_MAX_BITS;
1331 new_hash = alloc_ftrace_hash(bits);
1335 size = 1 << src->size_bits;
1336 for (i = 0; i < size; i++) {
1337 hhd = &src->buckets[i];
1338 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1340 key = hash_long(entry->ip, bits);
1343 remove_hash_entry(src, entry);
1344 __add_hash_entry(new_hash, entry);
1349 rcu_assign_pointer(*dst, new_hash);
1350 free_ftrace_hash_rcu(old_hash);
1355 * Enable regardless of ret:
1356 * On success, we enable the new hash.
1357 * On failure, we re-enable the original hash.
1359 ftrace_hash_rec_enable(ops, enable);
1365 * Test the hashes for this ops to see if we want to call
1366 * the ops->func or not.
1368 * It's a match if the ip is in the ops->filter_hash or
1369 * the filter_hash does not exist or is empty,
1371 * the ip is not in the ops->notrace_hash.
1373 * This needs to be called with preemption disabled as
1374 * the hashes are freed with call_rcu_sched().
1377 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1379 struct ftrace_hash *filter_hash;
1380 struct ftrace_hash *notrace_hash;
1383 filter_hash = rcu_dereference_raw(ops->filter_hash);
1384 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1386 if ((ftrace_hash_empty(filter_hash) ||
1387 ftrace_lookup_ip(filter_hash, ip)) &&
1388 (ftrace_hash_empty(notrace_hash) ||
1389 !ftrace_lookup_ip(notrace_hash, ip)))
1398 * This is a double for. Do not use 'break' to break out of the loop,
1399 * you must use a goto.
1401 #define do_for_each_ftrace_rec(pg, rec) \
1402 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1404 for (_____i = 0; _____i < pg->index; _____i++) { \
1405 rec = &pg->records[_____i];
1407 #define while_for_each_ftrace_rec() \
1412 static int ftrace_cmp_recs(const void *a, const void *b)
1414 const struct dyn_ftrace *key = a;
1415 const struct dyn_ftrace *rec = b;
1417 if (key->flags < rec->ip)
1419 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1424 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1426 struct ftrace_page *pg;
1427 struct dyn_ftrace *rec;
1428 struct dyn_ftrace key;
1431 key.flags = end; /* overload flags, as it is unsigned long */
1433 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1434 if (end < pg->records[0].ip ||
1435 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1437 rec = bsearch(&key, pg->records, pg->index,
1438 sizeof(struct dyn_ftrace),
1448 * ftrace_location - return true if the ip giving is a traced location
1449 * @ip: the instruction pointer to check
1451 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1452 * That is, the instruction that is either a NOP or call to
1453 * the function tracer. It checks the ftrace internal tables to
1454 * determine if the address belongs or not.
1456 unsigned long ftrace_location(unsigned long ip)
1458 return ftrace_location_range(ip, ip);
1462 * ftrace_text_reserved - return true if range contains an ftrace location
1463 * @start: start of range to search
1464 * @end: end of range to search (inclusive). @end points to the last byte to check.
1466 * Returns 1 if @start and @end contains a ftrace location.
1467 * That is, the instruction that is either a NOP or call to
1468 * the function tracer. It checks the ftrace internal tables to
1469 * determine if the address belongs or not.
1471 int ftrace_text_reserved(void *start, void *end)
1475 ret = ftrace_location_range((unsigned long)start,
1476 (unsigned long)end);
1481 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1485 struct ftrace_hash *hash;
1486 struct ftrace_hash *other_hash;
1487 struct ftrace_page *pg;
1488 struct dyn_ftrace *rec;
1492 /* Only update if the ops has been registered */
1493 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1497 * In the filter_hash case:
1498 * If the count is zero, we update all records.
1499 * Otherwise we just update the items in the hash.
1501 * In the notrace_hash case:
1502 * We enable the update in the hash.
1503 * As disabling notrace means enabling the tracing,
1504 * and enabling notrace means disabling, the inc variable
1508 hash = ops->filter_hash;
1509 other_hash = ops->notrace_hash;
1510 if (ftrace_hash_empty(hash))
1514 hash = ops->notrace_hash;
1515 other_hash = ops->filter_hash;
1517 * If the notrace hash has no items,
1518 * then there's nothing to do.
1520 if (ftrace_hash_empty(hash))
1524 do_for_each_ftrace_rec(pg, rec) {
1525 int in_other_hash = 0;
1531 * Only the filter_hash affects all records.
1532 * Update if the record is not in the notrace hash.
1534 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1537 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1538 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1543 if (filter_hash && in_hash && !in_other_hash)
1545 else if (!filter_hash && in_hash &&
1546 (in_other_hash || ftrace_hash_empty(other_hash)))
1554 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1557 * If any ops wants regs saved for this function
1558 * then all ops will get saved regs.
1560 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1561 rec->flags |= FTRACE_FL_REGS;
1563 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1568 /* Shortcut, if we handled all records, we are done. */
1569 if (!all && count == hash->count)
1571 } while_for_each_ftrace_rec();
1574 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1577 __ftrace_hash_rec_update(ops, filter_hash, 0);
1580 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1583 __ftrace_hash_rec_update(ops, filter_hash, 1);
1586 static void print_ip_ins(const char *fmt, unsigned char *p)
1590 printk(KERN_CONT "%s", fmt);
1592 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1593 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1597 * ftrace_bug - report and shutdown function tracer
1598 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1599 * @ip: The address that failed
1601 * The arch code that enables or disables the function tracing
1602 * can call ftrace_bug() when it has detected a problem in
1603 * modifying the code. @failed should be one of either:
1604 * EFAULT - if the problem happens on reading the @ip address
1605 * EINVAL - if what is read at @ip is not what was expected
1606 * EPERM - if the problem happens on writting to the @ip address
1608 void ftrace_bug(int failed, unsigned long ip)
1612 FTRACE_WARN_ON_ONCE(1);
1613 pr_info("ftrace faulted on modifying ");
1617 FTRACE_WARN_ON_ONCE(1);
1618 pr_info("ftrace failed to modify ");
1620 print_ip_ins(" actual: ", (unsigned char *)ip);
1621 printk(KERN_CONT "\n");
1624 FTRACE_WARN_ON_ONCE(1);
1625 pr_info("ftrace faulted on writing ");
1629 FTRACE_WARN_ON_ONCE(1);
1630 pr_info("ftrace faulted on unknown error ");
1635 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1637 unsigned long flag = 0UL;
1640 * If we are updating calls:
1642 * If the record has a ref count, then we need to enable it
1643 * because someone is using it.
1645 * Otherwise we make sure its disabled.
1647 * If we are disabling calls, then disable all records that
1650 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1651 flag = FTRACE_FL_ENABLED;
1654 * If enabling and the REGS flag does not match the REGS_EN, then
1655 * do not ignore this record. Set flags to fail the compare against
1659 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1660 flag |= FTRACE_FL_REGS;
1662 /* If the state of this record hasn't changed, then do nothing */
1663 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1664 return FTRACE_UPDATE_IGNORE;
1667 /* Save off if rec is being enabled (for return value) */
1668 flag ^= rec->flags & FTRACE_FL_ENABLED;
1671 rec->flags |= FTRACE_FL_ENABLED;
1672 if (flag & FTRACE_FL_REGS) {
1673 if (rec->flags & FTRACE_FL_REGS)
1674 rec->flags |= FTRACE_FL_REGS_EN;
1676 rec->flags &= ~FTRACE_FL_REGS_EN;
1681 * If this record is being updated from a nop, then
1682 * return UPDATE_MAKE_CALL.
1683 * Otherwise, if the EN flag is set, then return
1684 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1685 * from the non-save regs, to a save regs function.
1687 * return UPDATE_MODIFY_CALL to tell the caller to convert
1688 * from the save regs, to a non-save regs function.
1690 if (flag & FTRACE_FL_ENABLED)
1691 return FTRACE_UPDATE_MAKE_CALL;
1692 else if (rec->flags & FTRACE_FL_REGS_EN)
1693 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1695 return FTRACE_UPDATE_MODIFY_CALL;
1699 /* If there's no more users, clear all flags */
1700 if (!(rec->flags & ~FTRACE_FL_MASK))
1703 /* Just disable the record (keep REGS state) */
1704 rec->flags &= ~FTRACE_FL_ENABLED;
1707 return FTRACE_UPDATE_MAKE_NOP;
1711 * ftrace_update_record, set a record that now is tracing or not
1712 * @rec: the record to update
1713 * @enable: set to 1 if the record is tracing, zero to force disable
1715 * The records that represent all functions that can be traced need
1716 * to be updated when tracing has been enabled.
1718 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1720 return ftrace_check_record(rec, enable, 1);
1724 * ftrace_test_record, check if the record has been enabled or not
1725 * @rec: the record to test
1726 * @enable: set to 1 to check if enabled, 0 if it is disabled
1728 * The arch code may need to test if a record is already set to
1729 * tracing to determine how to modify the function code that it
1732 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1734 return ftrace_check_record(rec, enable, 0);
1738 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1740 unsigned long ftrace_old_addr;
1741 unsigned long ftrace_addr;
1744 ret = ftrace_update_record(rec, enable);
1746 if (rec->flags & FTRACE_FL_REGS)
1747 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1749 ftrace_addr = (unsigned long)FTRACE_ADDR;
1752 case FTRACE_UPDATE_IGNORE:
1755 case FTRACE_UPDATE_MAKE_CALL:
1756 return ftrace_make_call(rec, ftrace_addr);
1758 case FTRACE_UPDATE_MAKE_NOP:
1759 return ftrace_make_nop(NULL, rec, ftrace_addr);
1761 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1762 case FTRACE_UPDATE_MODIFY_CALL:
1763 if (rec->flags & FTRACE_FL_REGS)
1764 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1766 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1768 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1771 return -1; /* unknow ftrace bug */
1774 void __weak ftrace_replace_code(int enable)
1776 struct dyn_ftrace *rec;
1777 struct ftrace_page *pg;
1780 if (unlikely(ftrace_disabled))
1783 do_for_each_ftrace_rec(pg, rec) {
1784 failed = __ftrace_replace_code(rec, enable);
1786 ftrace_bug(failed, rec->ip);
1787 /* Stop processing */
1790 } while_for_each_ftrace_rec();
1793 struct ftrace_rec_iter {
1794 struct ftrace_page *pg;
1799 * ftrace_rec_iter_start, start up iterating over traced functions
1801 * Returns an iterator handle that is used to iterate over all
1802 * the records that represent address locations where functions
1805 * May return NULL if no records are available.
1807 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1810 * We only use a single iterator.
1811 * Protected by the ftrace_lock mutex.
1813 static struct ftrace_rec_iter ftrace_rec_iter;
1814 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1816 iter->pg = ftrace_pages_start;
1819 /* Could have empty pages */
1820 while (iter->pg && !iter->pg->index)
1821 iter->pg = iter->pg->next;
1830 * ftrace_rec_iter_next, get the next record to process.
1831 * @iter: The handle to the iterator.
1833 * Returns the next iterator after the given iterator @iter.
1835 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1839 if (iter->index >= iter->pg->index) {
1840 iter->pg = iter->pg->next;
1843 /* Could have empty pages */
1844 while (iter->pg && !iter->pg->index)
1845 iter->pg = iter->pg->next;
1855 * ftrace_rec_iter_record, get the record at the iterator location
1856 * @iter: The current iterator location
1858 * Returns the record that the current @iter is at.
1860 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1862 return &iter->pg->records[iter->index];
1866 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1873 if (unlikely(ftrace_disabled))
1876 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1878 ftrace_bug(ret, ip);
1885 * archs can override this function if they must do something
1886 * before the modifying code is performed.
1888 int __weak ftrace_arch_code_modify_prepare(void)
1894 * archs can override this function if they must do something
1895 * after the modifying code is performed.
1897 int __weak ftrace_arch_code_modify_post_process(void)
1902 void ftrace_modify_all_code(int command)
1904 if (command & FTRACE_UPDATE_CALLS)
1905 ftrace_replace_code(1);
1906 else if (command & FTRACE_DISABLE_CALLS)
1907 ftrace_replace_code(0);
1909 if (command & FTRACE_UPDATE_TRACE_FUNC)
1910 ftrace_update_ftrace_func(ftrace_trace_function);
1912 if (command & FTRACE_START_FUNC_RET)
1913 ftrace_enable_ftrace_graph_caller();
1914 else if (command & FTRACE_STOP_FUNC_RET)
1915 ftrace_disable_ftrace_graph_caller();
1918 static int __ftrace_modify_code(void *data)
1920 int *command = data;
1922 ftrace_modify_all_code(*command);
1928 * ftrace_run_stop_machine, go back to the stop machine method
1929 * @command: The command to tell ftrace what to do
1931 * If an arch needs to fall back to the stop machine method, the
1932 * it can call this function.
1934 void ftrace_run_stop_machine(int command)
1936 stop_machine(__ftrace_modify_code, &command, NULL);
1940 * arch_ftrace_update_code, modify the code to trace or not trace
1941 * @command: The command that needs to be done
1943 * Archs can override this function if it does not need to
1944 * run stop_machine() to modify code.
1946 void __weak arch_ftrace_update_code(int command)
1948 ftrace_run_stop_machine(command);
1951 static void ftrace_run_update_code(int command)
1955 ret = ftrace_arch_code_modify_prepare();
1956 FTRACE_WARN_ON(ret);
1960 * Do not call function tracer while we update the code.
1961 * We are in stop machine.
1963 function_trace_stop++;
1966 * By default we use stop_machine() to modify the code.
1967 * But archs can do what ever they want as long as it
1968 * is safe. The stop_machine() is the safest, but also
1969 * produces the most overhead.
1971 arch_ftrace_update_code(command);
1973 function_trace_stop--;
1975 ret = ftrace_arch_code_modify_post_process();
1976 FTRACE_WARN_ON(ret);
1979 static ftrace_func_t saved_ftrace_func;
1980 static int ftrace_start_up;
1981 static int global_start_up;
1983 static void ftrace_startup_enable(int command)
1985 if (saved_ftrace_func != ftrace_trace_function) {
1986 saved_ftrace_func = ftrace_trace_function;
1987 command |= FTRACE_UPDATE_TRACE_FUNC;
1990 if (!command || !ftrace_enabled)
1993 ftrace_run_update_code(command);
1996 static int ftrace_startup(struct ftrace_ops *ops, int command)
1998 bool hash_enable = true;
2000 if (unlikely(ftrace_disabled))
2004 command |= FTRACE_UPDATE_CALLS;
2006 /* ops marked global share the filter hashes */
2007 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2009 /* Don't update hash if global is already set */
2010 if (global_start_up)
2011 hash_enable = false;
2015 ops->flags |= FTRACE_OPS_FL_ENABLED;
2017 ftrace_hash_rec_enable(ops, 1);
2019 ftrace_startup_enable(command);
2024 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
2026 bool hash_disable = true;
2028 if (unlikely(ftrace_disabled))
2033 * Just warn in case of unbalance, no need to kill ftrace, it's not
2034 * critical but the ftrace_call callers may be never nopped again after
2035 * further ftrace uses.
2037 WARN_ON_ONCE(ftrace_start_up < 0);
2039 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2042 WARN_ON_ONCE(global_start_up < 0);
2043 /* Don't update hash if global still has users */
2044 if (global_start_up) {
2045 WARN_ON_ONCE(!ftrace_start_up);
2046 hash_disable = false;
2051 ftrace_hash_rec_disable(ops, 1);
2053 if (ops != &global_ops || !global_start_up)
2054 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2056 command |= FTRACE_UPDATE_CALLS;
2058 if (saved_ftrace_func != ftrace_trace_function) {
2059 saved_ftrace_func = ftrace_trace_function;
2060 command |= FTRACE_UPDATE_TRACE_FUNC;
2063 if (!command || !ftrace_enabled)
2066 ftrace_run_update_code(command);
2069 static void ftrace_startup_sysctl(void)
2071 if (unlikely(ftrace_disabled))
2074 /* Force update next time */
2075 saved_ftrace_func = NULL;
2076 /* ftrace_start_up is true if we want ftrace running */
2077 if (ftrace_start_up)
2078 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2081 static void ftrace_shutdown_sysctl(void)
2083 if (unlikely(ftrace_disabled))
2086 /* ftrace_start_up is true if ftrace is running */
2087 if (ftrace_start_up)
2088 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2091 static cycle_t ftrace_update_time;
2092 static unsigned long ftrace_update_cnt;
2093 unsigned long ftrace_update_tot_cnt;
2095 static int ops_traces_mod(struct ftrace_ops *ops)
2097 struct ftrace_hash *hash;
2099 hash = ops->filter_hash;
2100 return ftrace_hash_empty(hash);
2103 static int ftrace_update_code(struct module *mod)
2105 struct ftrace_page *pg;
2106 struct dyn_ftrace *p;
2107 cycle_t start, stop;
2108 unsigned long ref = 0;
2112 * When adding a module, we need to check if tracers are
2113 * currently enabled and if they are set to trace all functions.
2114 * If they are, we need to enable the module functions as well
2115 * as update the reference counts for those function records.
2118 struct ftrace_ops *ops;
2120 for (ops = ftrace_ops_list;
2121 ops != &ftrace_list_end; ops = ops->next) {
2122 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2123 ops_traces_mod(ops))
2128 start = ftrace_now(raw_smp_processor_id());
2129 ftrace_update_cnt = 0;
2131 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2133 for (i = 0; i < pg->index; i++) {
2134 /* If something went wrong, bail without enabling anything */
2135 if (unlikely(ftrace_disabled))
2138 p = &pg->records[i];
2142 * Do the initial record conversion from mcount jump
2143 * to the NOP instructions.
2145 if (!ftrace_code_disable(mod, p))
2148 ftrace_update_cnt++;
2151 * If the tracing is enabled, go ahead and enable the record.
2153 * The reason not to enable the record immediatelly is the
2154 * inherent check of ftrace_make_nop/ftrace_make_call for
2155 * correct previous instructions. Making first the NOP
2156 * conversion puts the module to the correct state, thus
2157 * passing the ftrace_make_call check.
2159 if (ftrace_start_up && ref) {
2160 int failed = __ftrace_replace_code(p, 1);
2162 ftrace_bug(failed, p->ip);
2167 ftrace_new_pgs = NULL;
2169 stop = ftrace_now(raw_smp_processor_id());
2170 ftrace_update_time = stop - start;
2171 ftrace_update_tot_cnt += ftrace_update_cnt;
2176 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2181 if (WARN_ON(!count))
2184 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2187 * We want to fill as much as possible. No more than a page
2190 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2194 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2197 /* if we can't allocate this size, try something smaller */
2204 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2213 static struct ftrace_page *
2214 ftrace_allocate_pages(unsigned long num_to_init)
2216 struct ftrace_page *start_pg;
2217 struct ftrace_page *pg;
2224 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2229 * Try to allocate as much as possible in one continues
2230 * location that fills in all of the space. We want to
2231 * waste as little space as possible.
2234 cnt = ftrace_allocate_records(pg, num_to_init);
2242 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2253 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2254 free_pages((unsigned long)pg->records, order);
2255 start_pg = pg->next;
2259 pr_info("ftrace: FAILED to allocate memory for functions\n");
2263 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2268 pr_info("ftrace: No functions to be traced?\n");
2272 cnt = num_to_init / ENTRIES_PER_PAGE;
2273 pr_info("ftrace: allocating %ld entries in %d pages\n",
2274 num_to_init, cnt + 1);
2279 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2281 struct ftrace_iterator {
2284 struct ftrace_page *pg;
2285 struct dyn_ftrace *func;
2286 struct ftrace_func_probe *probe;
2287 struct trace_parser parser;
2288 struct ftrace_hash *hash;
2289 struct ftrace_ops *ops;
2296 t_hash_next(struct seq_file *m, loff_t *pos)
2298 struct ftrace_iterator *iter = m->private;
2299 struct hlist_node *hnd = NULL;
2300 struct hlist_head *hhd;
2306 hnd = &iter->probe->node;
2308 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2311 hhd = &ftrace_func_hash[iter->hidx];
2313 if (hlist_empty(hhd)) {
2329 if (WARN_ON_ONCE(!hnd))
2332 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2337 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2339 struct ftrace_iterator *iter = m->private;
2343 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2346 if (iter->func_pos > *pos)
2350 for (l = 0; l <= (*pos - iter->func_pos); ) {
2351 p = t_hash_next(m, &l);
2358 /* Only set this if we have an item */
2359 iter->flags |= FTRACE_ITER_HASH;
2365 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2367 struct ftrace_func_probe *rec;
2370 if (WARN_ON_ONCE(!rec))
2373 if (rec->ops->print)
2374 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2376 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2379 seq_printf(m, ":%p", rec->data);
2386 t_next(struct seq_file *m, void *v, loff_t *pos)
2388 struct ftrace_iterator *iter = m->private;
2389 struct ftrace_ops *ops = iter->ops;
2390 struct dyn_ftrace *rec = NULL;
2392 if (unlikely(ftrace_disabled))
2395 if (iter->flags & FTRACE_ITER_HASH)
2396 return t_hash_next(m, pos);
2399 iter->pos = iter->func_pos = *pos;
2401 if (iter->flags & FTRACE_ITER_PRINTALL)
2402 return t_hash_start(m, pos);
2405 if (iter->idx >= iter->pg->index) {
2406 if (iter->pg->next) {
2407 iter->pg = iter->pg->next;
2412 rec = &iter->pg->records[iter->idx++];
2413 if (((iter->flags & FTRACE_ITER_FILTER) &&
2414 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2416 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2417 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2419 ((iter->flags & FTRACE_ITER_ENABLED) &&
2420 !(rec->flags & ~FTRACE_FL_MASK))) {
2428 return t_hash_start(m, pos);
2435 static void reset_iter_read(struct ftrace_iterator *iter)
2439 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2442 static void *t_start(struct seq_file *m, loff_t *pos)
2444 struct ftrace_iterator *iter = m->private;
2445 struct ftrace_ops *ops = iter->ops;
2449 mutex_lock(&ftrace_lock);
2451 if (unlikely(ftrace_disabled))
2455 * If an lseek was done, then reset and start from beginning.
2457 if (*pos < iter->pos)
2458 reset_iter_read(iter);
2461 * For set_ftrace_filter reading, if we have the filter
2462 * off, we can short cut and just print out that all
2463 * functions are enabled.
2465 if (iter->flags & FTRACE_ITER_FILTER &&
2466 ftrace_hash_empty(ops->filter_hash)) {
2468 return t_hash_start(m, pos);
2469 iter->flags |= FTRACE_ITER_PRINTALL;
2470 /* reset in case of seek/pread */
2471 iter->flags &= ~FTRACE_ITER_HASH;
2475 if (iter->flags & FTRACE_ITER_HASH)
2476 return t_hash_start(m, pos);
2479 * Unfortunately, we need to restart at ftrace_pages_start
2480 * every time we let go of the ftrace_mutex. This is because
2481 * those pointers can change without the lock.
2483 iter->pg = ftrace_pages_start;
2485 for (l = 0; l <= *pos; ) {
2486 p = t_next(m, p, &l);
2492 return t_hash_start(m, pos);
2497 static void t_stop(struct seq_file *m, void *p)
2499 mutex_unlock(&ftrace_lock);
2502 static int t_show(struct seq_file *m, void *v)
2504 struct ftrace_iterator *iter = m->private;
2505 struct dyn_ftrace *rec;
2507 if (iter->flags & FTRACE_ITER_HASH)
2508 return t_hash_show(m, iter);
2510 if (iter->flags & FTRACE_ITER_PRINTALL) {
2511 seq_printf(m, "#### all functions enabled ####\n");
2520 seq_printf(m, "%ps", (void *)rec->ip);
2521 if (iter->flags & FTRACE_ITER_ENABLED)
2522 seq_printf(m, " (%ld)%s",
2523 rec->flags & ~FTRACE_FL_MASK,
2524 rec->flags & FTRACE_FL_REGS ? " R" : "");
2525 seq_printf(m, "\n");
2530 static const struct seq_operations show_ftrace_seq_ops = {
2538 ftrace_avail_open(struct inode *inode, struct file *file)
2540 struct ftrace_iterator *iter;
2542 if (unlikely(ftrace_disabled))
2545 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2547 iter->pg = ftrace_pages_start;
2548 iter->ops = &global_ops;
2551 return iter ? 0 : -ENOMEM;
2555 ftrace_enabled_open(struct inode *inode, struct file *file)
2557 struct ftrace_iterator *iter;
2559 if (unlikely(ftrace_disabled))
2562 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2564 iter->pg = ftrace_pages_start;
2565 iter->flags = FTRACE_ITER_ENABLED;
2566 iter->ops = &global_ops;
2569 return iter ? 0 : -ENOMEM;
2572 static void ftrace_filter_reset(struct ftrace_hash *hash)
2574 mutex_lock(&ftrace_lock);
2575 ftrace_hash_clear(hash);
2576 mutex_unlock(&ftrace_lock);
2580 * ftrace_regex_open - initialize function tracer filter files
2581 * @ops: The ftrace_ops that hold the hash filters
2582 * @flag: The type of filter to process
2583 * @inode: The inode, usually passed in to your open routine
2584 * @file: The file, usually passed in to your open routine
2586 * ftrace_regex_open() initializes the filter files for the
2587 * @ops. Depending on @flag it may process the filter hash or
2588 * the notrace hash of @ops. With this called from the open
2589 * routine, you can use ftrace_filter_write() for the write
2590 * routine if @flag has FTRACE_ITER_FILTER set, or
2591 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2592 * ftrace_regex_lseek() should be used as the lseek routine, and
2593 * release must call ftrace_regex_release().
2596 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2597 struct inode *inode, struct file *file)
2599 struct ftrace_iterator *iter;
2600 struct ftrace_hash *hash;
2603 if (unlikely(ftrace_disabled))
2606 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2610 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2615 if (flag & FTRACE_ITER_NOTRACE)
2616 hash = ops->notrace_hash;
2618 hash = ops->filter_hash;
2623 if (file->f_mode & FMODE_WRITE) {
2624 mutex_lock(&ftrace_lock);
2625 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2626 mutex_unlock(&ftrace_lock);
2629 trace_parser_put(&iter->parser);
2635 mutex_lock(&ftrace_regex_lock);
2637 if ((file->f_mode & FMODE_WRITE) &&
2638 (file->f_flags & O_TRUNC))
2639 ftrace_filter_reset(iter->hash);
2641 if (file->f_mode & FMODE_READ) {
2642 iter->pg = ftrace_pages_start;
2644 ret = seq_open(file, &show_ftrace_seq_ops);
2646 struct seq_file *m = file->private_data;
2650 free_ftrace_hash(iter->hash);
2651 trace_parser_put(&iter->parser);
2655 file->private_data = iter;
2656 mutex_unlock(&ftrace_regex_lock);
2662 ftrace_filter_open(struct inode *inode, struct file *file)
2664 return ftrace_regex_open(&global_ops,
2665 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2670 ftrace_notrace_open(struct inode *inode, struct file *file)
2672 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2677 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
2681 if (file->f_mode & FMODE_READ)
2682 ret = seq_lseek(file, offset, whence);
2684 file->f_pos = ret = 1;
2689 static int ftrace_match(char *str, char *regex, int len, int type)
2696 if (strcmp(str, regex) == 0)
2699 case MATCH_FRONT_ONLY:
2700 if (strncmp(str, regex, len) == 0)
2703 case MATCH_MIDDLE_ONLY:
2704 if (strstr(str, regex))
2707 case MATCH_END_ONLY:
2709 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2718 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2720 struct ftrace_func_entry *entry;
2723 entry = ftrace_lookup_ip(hash, rec->ip);
2725 /* Do nothing if it doesn't exist */
2729 free_hash_entry(hash, entry);
2731 /* Do nothing if it exists */
2735 ret = add_hash_entry(hash, rec->ip);
2741 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2742 char *regex, int len, int type)
2744 char str[KSYM_SYMBOL_LEN];
2747 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2750 /* module lookup requires matching the module */
2751 if (!modname || strcmp(modname, mod))
2754 /* blank search means to match all funcs in the mod */
2759 return ftrace_match(str, regex, len, type);
2763 match_records(struct ftrace_hash *hash, char *buff,
2764 int len, char *mod, int not)
2766 unsigned search_len = 0;
2767 struct ftrace_page *pg;
2768 struct dyn_ftrace *rec;
2769 int type = MATCH_FULL;
2770 char *search = buff;
2775 type = filter_parse_regex(buff, len, &search, ¬);
2776 search_len = strlen(search);
2779 mutex_lock(&ftrace_lock);
2781 if (unlikely(ftrace_disabled))
2784 do_for_each_ftrace_rec(pg, rec) {
2785 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2786 ret = enter_record(hash, rec, not);
2793 } while_for_each_ftrace_rec();
2795 mutex_unlock(&ftrace_lock);
2801 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2803 return match_records(hash, buff, len, NULL, 0);
2807 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2811 /* blank or '*' mean the same */
2812 if (strcmp(buff, "*") == 0)
2815 /* handle the case of 'dont filter this module' */
2816 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2821 return match_records(hash, buff, strlen(buff), mod, not);
2825 * We register the module command as a template to show others how
2826 * to register the a command as well.
2830 ftrace_mod_callback(struct ftrace_hash *hash,
2831 char *func, char *cmd, char *param, int enable)
2837 * cmd == 'mod' because we only registered this func
2838 * for the 'mod' ftrace_func_command.
2839 * But if you register one func with multiple commands,
2840 * you can tell which command was used by the cmd
2844 /* we must have a module name */
2848 mod = strsep(¶m, ":");
2852 ret = ftrace_match_module_records(hash, func, mod);
2861 static struct ftrace_func_command ftrace_mod_cmd = {
2863 .func = ftrace_mod_callback,
2866 static int __init ftrace_mod_cmd_init(void)
2868 return register_ftrace_command(&ftrace_mod_cmd);
2870 core_initcall(ftrace_mod_cmd_init);
2872 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2873 struct ftrace_ops *op, struct pt_regs *pt_regs)
2875 struct ftrace_func_probe *entry;
2876 struct hlist_head *hhd;
2877 struct hlist_node *n;
2880 key = hash_long(ip, FTRACE_HASH_BITS);
2882 hhd = &ftrace_func_hash[key];
2884 if (hlist_empty(hhd))
2888 * Disable preemption for these calls to prevent a RCU grace
2889 * period. This syncs the hash iteration and freeing of items
2890 * on the hash. rcu_read_lock is too dangerous here.
2892 preempt_disable_notrace();
2893 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2894 if (entry->ip == ip)
2895 entry->ops->func(ip, parent_ip, &entry->data);
2897 preempt_enable_notrace();
2900 static struct ftrace_ops trace_probe_ops __read_mostly =
2902 .func = function_trace_probe_call,
2905 static int ftrace_probe_registered;
2907 static void __enable_ftrace_function_probe(void)
2912 if (ftrace_probe_registered)
2915 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2916 struct hlist_head *hhd = &ftrace_func_hash[i];
2920 /* Nothing registered? */
2921 if (i == FTRACE_FUNC_HASHSIZE)
2924 ret = __register_ftrace_function(&trace_probe_ops);
2926 ret = ftrace_startup(&trace_probe_ops, 0);
2928 ftrace_probe_registered = 1;
2931 static void __disable_ftrace_function_probe(void)
2936 if (!ftrace_probe_registered)
2939 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2940 struct hlist_head *hhd = &ftrace_func_hash[i];
2945 /* no more funcs left */
2946 ret = __unregister_ftrace_function(&trace_probe_ops);
2948 ftrace_shutdown(&trace_probe_ops, 0);
2950 ftrace_probe_registered = 0;
2954 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2956 struct ftrace_func_probe *entry =
2957 container_of(rhp, struct ftrace_func_probe, rcu);
2959 if (entry->ops->free)
2960 entry->ops->free(&entry->data);
2966 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2969 struct ftrace_func_probe *entry;
2970 struct ftrace_page *pg;
2971 struct dyn_ftrace *rec;
2977 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2978 len = strlen(search);
2980 /* we do not support '!' for function probes */
2984 mutex_lock(&ftrace_lock);
2986 if (unlikely(ftrace_disabled))
2989 do_for_each_ftrace_rec(pg, rec) {
2991 if (!ftrace_match_record(rec, NULL, search, len, type))
2994 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2996 /* If we did not process any, then return error */
3007 * The caller might want to do something special
3008 * for each function we find. We call the callback
3009 * to give the caller an opportunity to do so.
3011 if (ops->callback) {
3012 if (ops->callback(rec->ip, &entry->data) < 0) {
3013 /* caller does not like this func */
3020 entry->ip = rec->ip;
3022 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3023 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3025 } while_for_each_ftrace_rec();
3026 __enable_ftrace_function_probe();
3029 mutex_unlock(&ftrace_lock);
3035 PROBE_TEST_FUNC = 1,
3040 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3041 void *data, int flags)
3043 struct ftrace_func_probe *entry;
3044 struct hlist_node *n, *tmp;
3045 char str[KSYM_SYMBOL_LEN];
3046 int type = MATCH_FULL;
3050 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3055 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3056 len = strlen(search);
3058 /* we do not support '!' for function probes */
3063 mutex_lock(&ftrace_lock);
3064 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3065 struct hlist_head *hhd = &ftrace_func_hash[i];
3067 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3069 /* break up if statements for readability */
3070 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3073 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3076 /* do this last, since it is the most expensive */
3078 kallsyms_lookup(entry->ip, NULL, NULL,
3080 if (!ftrace_match(str, glob, len, type))
3084 hlist_del_rcu(&entry->node);
3085 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
3088 __disable_ftrace_function_probe();
3089 mutex_unlock(&ftrace_lock);
3093 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3096 __unregister_ftrace_function_probe(glob, ops, data,
3097 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3101 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3103 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3106 void unregister_ftrace_function_probe_all(char *glob)
3108 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3111 static LIST_HEAD(ftrace_commands);
3112 static DEFINE_MUTEX(ftrace_cmd_mutex);
3114 int register_ftrace_command(struct ftrace_func_command *cmd)
3116 struct ftrace_func_command *p;
3119 mutex_lock(&ftrace_cmd_mutex);
3120 list_for_each_entry(p, &ftrace_commands, list) {
3121 if (strcmp(cmd->name, p->name) == 0) {
3126 list_add(&cmd->list, &ftrace_commands);
3128 mutex_unlock(&ftrace_cmd_mutex);
3133 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3135 struct ftrace_func_command *p, *n;
3138 mutex_lock(&ftrace_cmd_mutex);
3139 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3140 if (strcmp(cmd->name, p->name) == 0) {
3142 list_del_init(&p->list);
3147 mutex_unlock(&ftrace_cmd_mutex);
3152 static int ftrace_process_regex(struct ftrace_hash *hash,
3153 char *buff, int len, int enable)
3155 char *func, *command, *next = buff;
3156 struct ftrace_func_command *p;
3159 func = strsep(&next, ":");
3162 ret = ftrace_match_records(hash, func, len);
3172 command = strsep(&next, ":");
3174 mutex_lock(&ftrace_cmd_mutex);
3175 list_for_each_entry(p, &ftrace_commands, list) {
3176 if (strcmp(p->name, command) == 0) {
3177 ret = p->func(hash, func, command, next, enable);
3182 mutex_unlock(&ftrace_cmd_mutex);
3188 ftrace_regex_write(struct file *file, const char __user *ubuf,
3189 size_t cnt, loff_t *ppos, int enable)
3191 struct ftrace_iterator *iter;
3192 struct trace_parser *parser;
3198 mutex_lock(&ftrace_regex_lock);
3201 if (unlikely(ftrace_disabled))
3204 if (file->f_mode & FMODE_READ) {
3205 struct seq_file *m = file->private_data;
3208 iter = file->private_data;
3210 parser = &iter->parser;
3211 read = trace_get_user(parser, ubuf, cnt, ppos);
3213 if (read >= 0 && trace_parser_loaded(parser) &&
3214 !trace_parser_cont(parser)) {
3215 ret = ftrace_process_regex(iter->hash, parser->buffer,
3216 parser->idx, enable);
3217 trace_parser_clear(parser);
3224 mutex_unlock(&ftrace_regex_lock);
3230 ftrace_filter_write(struct file *file, const char __user *ubuf,
3231 size_t cnt, loff_t *ppos)
3233 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3237 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3238 size_t cnt, loff_t *ppos)
3240 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3244 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3246 struct ftrace_func_entry *entry;
3248 if (!ftrace_location(ip))
3252 entry = ftrace_lookup_ip(hash, ip);
3255 free_hash_entry(hash, entry);
3259 return add_hash_entry(hash, ip);
3263 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3264 unsigned long ip, int remove, int reset, int enable)
3266 struct ftrace_hash **orig_hash;
3267 struct ftrace_hash *hash;
3270 /* All global ops uses the global ops filters */
3271 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3274 if (unlikely(ftrace_disabled))
3278 orig_hash = &ops->filter_hash;
3280 orig_hash = &ops->notrace_hash;
3282 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3286 mutex_lock(&ftrace_regex_lock);
3288 ftrace_filter_reset(hash);
3289 if (buf && !ftrace_match_records(hash, buf, len)) {
3291 goto out_regex_unlock;
3294 ret = ftrace_match_addr(hash, ip, remove);
3296 goto out_regex_unlock;
3299 mutex_lock(&ftrace_lock);
3300 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3301 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3303 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3305 mutex_unlock(&ftrace_lock);
3308 mutex_unlock(&ftrace_regex_lock);
3310 free_ftrace_hash(hash);
3315 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3316 int reset, int enable)
3318 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3322 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3323 * @ops - the ops to set the filter with
3324 * @ip - the address to add to or remove from the filter.
3325 * @remove - non zero to remove the ip from the filter
3326 * @reset - non zero to reset all filters before applying this filter.
3328 * Filters denote which functions should be enabled when tracing is enabled
3329 * If @ip is NULL, it failes to update filter.
3331 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3332 int remove, int reset)
3334 return ftrace_set_addr(ops, ip, remove, reset, 1);
3336 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3339 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3340 int reset, int enable)
3342 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3346 * ftrace_set_filter - set a function to filter on in ftrace
3347 * @ops - the ops to set the filter with
3348 * @buf - the string that holds the function filter text.
3349 * @len - the length of the string.
3350 * @reset - non zero to reset all filters before applying this filter.
3352 * Filters denote which functions should be enabled when tracing is enabled.
3353 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3355 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3358 return ftrace_set_regex(ops, buf, len, reset, 1);
3360 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3363 * ftrace_set_notrace - set a function to not trace in ftrace
3364 * @ops - the ops to set the notrace filter with
3365 * @buf - the string that holds the function notrace text.
3366 * @len - the length of the string.
3367 * @reset - non zero to reset all filters before applying this filter.
3369 * Notrace Filters denote which functions should not be enabled when tracing
3370 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3373 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3376 return ftrace_set_regex(ops, buf, len, reset, 0);
3378 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3380 * ftrace_set_filter - set a function to filter on in ftrace
3381 * @ops - the ops to set the filter with
3382 * @buf - the string that holds the function filter text.
3383 * @len - the length of the string.
3384 * @reset - non zero to reset all filters before applying this filter.
3386 * Filters denote which functions should be enabled when tracing is enabled.
3387 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3389 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3391 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3393 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3396 * ftrace_set_notrace - set a function to not trace in ftrace
3397 * @ops - the ops to set the notrace filter with
3398 * @buf - the string that holds the function notrace text.
3399 * @len - the length of the string.
3400 * @reset - non zero to reset all filters before applying this filter.
3402 * Notrace Filters denote which functions should not be enabled when tracing
3403 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3406 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3408 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3410 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3413 * command line interface to allow users to set filters on boot up.
3415 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3416 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3417 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3419 static int __init set_ftrace_notrace(char *str)
3421 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3424 __setup("ftrace_notrace=", set_ftrace_notrace);
3426 static int __init set_ftrace_filter(char *str)
3428 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3431 __setup("ftrace_filter=", set_ftrace_filter);
3433 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3434 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3435 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3437 static int __init set_graph_function(char *str)
3439 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3442 __setup("ftrace_graph_filter=", set_graph_function);
3444 static void __init set_ftrace_early_graph(char *buf)
3450 func = strsep(&buf, ",");
3451 /* we allow only one expression at a time */
3452 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3455 printk(KERN_DEBUG "ftrace: function %s not "
3456 "traceable\n", func);
3459 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3462 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3467 func = strsep(&buf, ",");
3468 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3472 static void __init set_ftrace_early_filters(void)
3474 if (ftrace_filter_buf[0])
3475 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3476 if (ftrace_notrace_buf[0])
3477 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3478 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3479 if (ftrace_graph_buf[0])
3480 set_ftrace_early_graph(ftrace_graph_buf);
3481 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3484 int ftrace_regex_release(struct inode *inode, struct file *file)
3486 struct seq_file *m = (struct seq_file *)file->private_data;
3487 struct ftrace_iterator *iter;
3488 struct ftrace_hash **orig_hash;
3489 struct trace_parser *parser;
3493 mutex_lock(&ftrace_regex_lock);
3494 if (file->f_mode & FMODE_READ) {
3497 seq_release(inode, file);
3499 iter = file->private_data;
3501 parser = &iter->parser;
3502 if (trace_parser_loaded(parser)) {
3503 parser->buffer[parser->idx] = 0;
3504 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3507 trace_parser_put(parser);
3509 if (file->f_mode & FMODE_WRITE) {
3510 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3513 orig_hash = &iter->ops->filter_hash;
3515 orig_hash = &iter->ops->notrace_hash;
3517 mutex_lock(&ftrace_lock);
3518 ret = ftrace_hash_move(iter->ops, filter_hash,
3519 orig_hash, iter->hash);
3520 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3522 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3524 mutex_unlock(&ftrace_lock);
3526 free_ftrace_hash(iter->hash);
3529 mutex_unlock(&ftrace_regex_lock);
3533 static const struct file_operations ftrace_avail_fops = {
3534 .open = ftrace_avail_open,
3536 .llseek = seq_lseek,
3537 .release = seq_release_private,
3540 static const struct file_operations ftrace_enabled_fops = {
3541 .open = ftrace_enabled_open,
3543 .llseek = seq_lseek,
3544 .release = seq_release_private,
3547 static const struct file_operations ftrace_filter_fops = {
3548 .open = ftrace_filter_open,
3550 .write = ftrace_filter_write,
3551 .llseek = ftrace_filter_lseek,
3552 .release = ftrace_regex_release,
3555 static const struct file_operations ftrace_notrace_fops = {
3556 .open = ftrace_notrace_open,
3558 .write = ftrace_notrace_write,
3559 .llseek = ftrace_filter_lseek,
3560 .release = ftrace_regex_release,
3563 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3565 static DEFINE_MUTEX(graph_lock);
3567 int ftrace_graph_count;
3568 int ftrace_graph_filter_enabled;
3569 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3572 __g_next(struct seq_file *m, loff_t *pos)
3574 if (*pos >= ftrace_graph_count)
3576 return &ftrace_graph_funcs[*pos];
3580 g_next(struct seq_file *m, void *v, loff_t *pos)
3583 return __g_next(m, pos);
3586 static void *g_start(struct seq_file *m, loff_t *pos)
3588 mutex_lock(&graph_lock);
3590 /* Nothing, tell g_show to print all functions are enabled */
3591 if (!ftrace_graph_filter_enabled && !*pos)
3594 return __g_next(m, pos);
3597 static void g_stop(struct seq_file *m, void *p)
3599 mutex_unlock(&graph_lock);
3602 static int g_show(struct seq_file *m, void *v)
3604 unsigned long *ptr = v;
3609 if (ptr == (unsigned long *)1) {
3610 seq_printf(m, "#### all functions enabled ####\n");
3614 seq_printf(m, "%ps\n", (void *)*ptr);
3619 static const struct seq_operations ftrace_graph_seq_ops = {
3627 ftrace_graph_open(struct inode *inode, struct file *file)
3631 if (unlikely(ftrace_disabled))
3634 mutex_lock(&graph_lock);
3635 if ((file->f_mode & FMODE_WRITE) &&
3636 (file->f_flags & O_TRUNC)) {
3637 ftrace_graph_filter_enabled = 0;
3638 ftrace_graph_count = 0;
3639 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3641 mutex_unlock(&graph_lock);
3643 if (file->f_mode & FMODE_READ)
3644 ret = seq_open(file, &ftrace_graph_seq_ops);
3650 ftrace_graph_release(struct inode *inode, struct file *file)
3652 if (file->f_mode & FMODE_READ)
3653 seq_release(inode, file);
3658 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3660 struct dyn_ftrace *rec;
3661 struct ftrace_page *pg;
3670 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3671 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3674 search_len = strlen(search);
3676 mutex_lock(&ftrace_lock);
3678 if (unlikely(ftrace_disabled)) {
3679 mutex_unlock(&ftrace_lock);
3683 do_for_each_ftrace_rec(pg, rec) {
3685 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3686 /* if it is in the array */
3688 for (i = 0; i < *idx; i++) {
3689 if (array[i] == rec->ip) {
3698 array[(*idx)++] = rec->ip;
3699 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3704 array[i] = array[--(*idx)];
3710 } while_for_each_ftrace_rec();
3712 mutex_unlock(&ftrace_lock);
3717 ftrace_graph_filter_enabled = 1;
3722 ftrace_graph_write(struct file *file, const char __user *ubuf,
3723 size_t cnt, loff_t *ppos)
3725 struct trace_parser parser;
3731 mutex_lock(&graph_lock);
3733 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3738 read = trace_get_user(&parser, ubuf, cnt, ppos);
3740 if (read >= 0 && trace_parser_loaded((&parser))) {
3741 parser.buffer[parser.idx] = 0;
3743 /* we allow only one expression at a time */
3744 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3753 trace_parser_put(&parser);
3755 mutex_unlock(&graph_lock);
3760 static const struct file_operations ftrace_graph_fops = {
3761 .open = ftrace_graph_open,
3763 .write = ftrace_graph_write,
3764 .llseek = ftrace_filter_lseek,
3765 .release = ftrace_graph_release,
3767 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3769 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3772 trace_create_file("available_filter_functions", 0444,
3773 d_tracer, NULL, &ftrace_avail_fops);
3775 trace_create_file("enabled_functions", 0444,
3776 d_tracer, NULL, &ftrace_enabled_fops);
3778 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3779 NULL, &ftrace_filter_fops);
3781 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3782 NULL, &ftrace_notrace_fops);
3784 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3785 trace_create_file("set_graph_function", 0444, d_tracer,
3787 &ftrace_graph_fops);
3788 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3793 static int ftrace_cmp_ips(const void *a, const void *b)
3795 const unsigned long *ipa = a;
3796 const unsigned long *ipb = b;
3805 static void ftrace_swap_ips(void *a, void *b, int size)
3807 unsigned long *ipa = a;
3808 unsigned long *ipb = b;
3816 static int ftrace_process_locs(struct module *mod,
3817 unsigned long *start,
3820 struct ftrace_page *start_pg;
3821 struct ftrace_page *pg;
3822 struct dyn_ftrace *rec;
3823 unsigned long count;
3826 unsigned long flags = 0; /* Shut up gcc */
3829 count = end - start;
3834 sort(start, count, sizeof(*start),
3835 ftrace_cmp_ips, ftrace_swap_ips);
3837 start_pg = ftrace_allocate_pages(count);
3841 mutex_lock(&ftrace_lock);
3844 * Core and each module needs their own pages, as
3845 * modules will free them when they are removed.
3846 * Force a new page to be allocated for modules.
3849 WARN_ON(ftrace_pages || ftrace_pages_start);
3850 /* First initialization */
3851 ftrace_pages = ftrace_pages_start = start_pg;
3856 if (WARN_ON(ftrace_pages->next)) {
3857 /* Hmm, we have free pages? */
3858 while (ftrace_pages->next)
3859 ftrace_pages = ftrace_pages->next;
3862 ftrace_pages->next = start_pg;
3868 addr = ftrace_call_adjust(*p++);
3870 * Some architecture linkers will pad between
3871 * the different mcount_loc sections of different
3872 * object files to satisfy alignments.
3873 * Skip any NULL pointers.
3878 if (pg->index == pg->size) {
3879 /* We should have allocated enough */
3880 if (WARN_ON(!pg->next))
3885 rec = &pg->records[pg->index++];
3889 /* We should have used all pages */
3892 /* Assign the last page to ftrace_pages */
3895 /* These new locations need to be initialized */
3896 ftrace_new_pgs = start_pg;
3899 * We only need to disable interrupts on start up
3900 * because we are modifying code that an interrupt
3901 * may execute, and the modification is not atomic.
3902 * But for modules, nothing runs the code we modify
3903 * until we are finished with it, and there's no
3904 * reason to cause large interrupt latencies while we do it.
3907 local_irq_save(flags);
3908 ftrace_update_code(mod);
3910 local_irq_restore(flags);
3913 mutex_unlock(&ftrace_lock);
3918 #ifdef CONFIG_MODULES
3920 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3922 void ftrace_release_mod(struct module *mod)
3924 struct dyn_ftrace *rec;
3925 struct ftrace_page **last_pg;
3926 struct ftrace_page *pg;
3929 mutex_lock(&ftrace_lock);
3931 if (ftrace_disabled)
3935 * Each module has its own ftrace_pages, remove
3936 * them from the list.
3938 last_pg = &ftrace_pages_start;
3939 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3940 rec = &pg->records[0];
3941 if (within_module_core(rec->ip, mod)) {
3943 * As core pages are first, the first
3944 * page should never be a module page.
3946 if (WARN_ON(pg == ftrace_pages_start))
3949 /* Check if we are deleting the last page */
3950 if (pg == ftrace_pages)
3951 ftrace_pages = next_to_ftrace_page(last_pg);
3953 *last_pg = pg->next;
3954 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3955 free_pages((unsigned long)pg->records, order);
3958 last_pg = &pg->next;
3961 mutex_unlock(&ftrace_lock);
3964 static void ftrace_init_module(struct module *mod,
3965 unsigned long *start, unsigned long *end)
3967 if (ftrace_disabled || start == end)
3969 ftrace_process_locs(mod, start, end);
3972 static int ftrace_module_notify_enter(struct notifier_block *self,
3973 unsigned long val, void *data)
3975 struct module *mod = data;
3977 if (val == MODULE_STATE_COMING)
3978 ftrace_init_module(mod, mod->ftrace_callsites,
3979 mod->ftrace_callsites +
3980 mod->num_ftrace_callsites);
3984 static int ftrace_module_notify_exit(struct notifier_block *self,
3985 unsigned long val, void *data)
3987 struct module *mod = data;
3989 if (val == MODULE_STATE_GOING)
3990 ftrace_release_mod(mod);
3995 static int ftrace_module_notify_enter(struct notifier_block *self,
3996 unsigned long val, void *data)
4000 static int ftrace_module_notify_exit(struct notifier_block *self,
4001 unsigned long val, void *data)
4005 #endif /* CONFIG_MODULES */
4007 struct notifier_block ftrace_module_enter_nb = {
4008 .notifier_call = ftrace_module_notify_enter,
4009 .priority = INT_MAX, /* Run before anything that can use kprobes */
4012 struct notifier_block ftrace_module_exit_nb = {
4013 .notifier_call = ftrace_module_notify_exit,
4014 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4017 extern unsigned long __start_mcount_loc[];
4018 extern unsigned long __stop_mcount_loc[];
4020 void __init ftrace_init(void)
4022 unsigned long count, addr, flags;
4025 /* Keep the ftrace pointer to the stub */
4026 addr = (unsigned long)ftrace_stub;
4028 local_irq_save(flags);
4029 ftrace_dyn_arch_init(&addr);
4030 local_irq_restore(flags);
4032 /* ftrace_dyn_arch_init places the return code in addr */
4036 count = __stop_mcount_loc - __start_mcount_loc;
4038 ret = ftrace_dyn_table_alloc(count);
4042 last_ftrace_enabled = ftrace_enabled = 1;
4044 ret = ftrace_process_locs(NULL,
4048 ret = register_module_notifier(&ftrace_module_enter_nb);
4050 pr_warning("Failed to register trace ftrace module enter notifier\n");
4052 ret = register_module_notifier(&ftrace_module_exit_nb);
4054 pr_warning("Failed to register trace ftrace module exit notifier\n");
4056 set_ftrace_early_filters();
4060 ftrace_disabled = 1;
4065 static struct ftrace_ops global_ops = {
4066 .func = ftrace_stub,
4067 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4070 static int __init ftrace_nodyn_init(void)
4075 core_initcall(ftrace_nodyn_init);
4077 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4078 static inline void ftrace_startup_enable(int command) { }
4079 /* Keep as macros so we do not need to define the commands */
4080 # define ftrace_startup(ops, command) \
4082 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4085 # define ftrace_shutdown(ops, command) do { } while (0)
4086 # define ftrace_startup_sysctl() do { } while (0)
4087 # define ftrace_shutdown_sysctl() do { } while (0)
4090 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4095 #endif /* CONFIG_DYNAMIC_FTRACE */
4098 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4099 struct ftrace_ops *op, struct pt_regs *regs)
4101 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4105 * Some of the ops may be dynamically allocated,
4106 * they must be freed after a synchronize_sched().
4108 preempt_disable_notrace();
4109 trace_recursion_set(TRACE_CONTROL_BIT);
4110 op = rcu_dereference_raw(ftrace_control_list);
4111 while (op != &ftrace_list_end) {
4112 if (!ftrace_function_local_disabled(op) &&
4113 ftrace_ops_test(op, ip))
4114 op->func(ip, parent_ip, op, regs);
4116 op = rcu_dereference_raw(op->next);
4118 trace_recursion_clear(TRACE_CONTROL_BIT);
4119 preempt_enable_notrace();
4122 static struct ftrace_ops control_ops = {
4123 .func = ftrace_ops_control_func,
4124 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4128 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4129 struct ftrace_ops *ignored, struct pt_regs *regs)
4131 struct ftrace_ops *op;
4133 if (function_trace_stop)
4136 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4139 trace_recursion_set(TRACE_INTERNAL_BIT);
4141 * Some of the ops may be dynamically allocated,
4142 * they must be freed after a synchronize_sched().
4144 preempt_disable_notrace();
4145 op = rcu_dereference_raw(ftrace_ops_list);
4146 while (op != &ftrace_list_end) {
4147 if (ftrace_ops_test(op, ip))
4148 op->func(ip, parent_ip, op, regs);
4149 op = rcu_dereference_raw(op->next);
4151 preempt_enable_notrace();
4152 trace_recursion_clear(TRACE_INTERNAL_BIT);
4156 * Some archs only support passing ip and parent_ip. Even though
4157 * the list function ignores the op parameter, we do not want any
4158 * C side effects, where a function is called without the caller
4159 * sending a third parameter.
4160 * Archs are to support both the regs and ftrace_ops at the same time.
4161 * If they support ftrace_ops, it is assumed they support regs.
4162 * If call backs want to use regs, they must either check for regs
4163 * being NULL, or ARCH_SUPPORTS_FTRACE_SAVE_REGS.
4164 * Note, ARCH_SUPPORT_SAVE_REGS expects a full regs to be saved.
4165 * An architecture can pass partial regs with ftrace_ops and still
4166 * set the ARCH_SUPPORT_FTARCE_OPS.
4168 #if ARCH_SUPPORTS_FTRACE_OPS
4169 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4170 struct ftrace_ops *op, struct pt_regs *regs)
4172 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4175 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4177 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4181 static void clear_ftrace_swapper(void)
4183 struct task_struct *p;
4187 for_each_online_cpu(cpu) {
4189 clear_tsk_trace_trace(p);
4194 static void set_ftrace_swapper(void)
4196 struct task_struct *p;
4200 for_each_online_cpu(cpu) {
4202 set_tsk_trace_trace(p);
4207 static void clear_ftrace_pid(struct pid *pid)
4209 struct task_struct *p;
4212 do_each_pid_task(pid, PIDTYPE_PID, p) {
4213 clear_tsk_trace_trace(p);
4214 } while_each_pid_task(pid, PIDTYPE_PID, p);
4220 static void set_ftrace_pid(struct pid *pid)
4222 struct task_struct *p;
4225 do_each_pid_task(pid, PIDTYPE_PID, p) {
4226 set_tsk_trace_trace(p);
4227 } while_each_pid_task(pid, PIDTYPE_PID, p);
4231 static void clear_ftrace_pid_task(struct pid *pid)
4233 if (pid == ftrace_swapper_pid)
4234 clear_ftrace_swapper();
4236 clear_ftrace_pid(pid);
4239 static void set_ftrace_pid_task(struct pid *pid)
4241 if (pid == ftrace_swapper_pid)
4242 set_ftrace_swapper();
4244 set_ftrace_pid(pid);
4247 static int ftrace_pid_add(int p)
4250 struct ftrace_pid *fpid;
4253 mutex_lock(&ftrace_lock);
4256 pid = ftrace_swapper_pid;
4258 pid = find_get_pid(p);
4265 list_for_each_entry(fpid, &ftrace_pids, list)
4266 if (fpid->pid == pid)
4271 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4275 list_add(&fpid->list, &ftrace_pids);
4278 set_ftrace_pid_task(pid);
4280 ftrace_update_pid_func();
4281 ftrace_startup_enable(0);
4283 mutex_unlock(&ftrace_lock);
4287 if (pid != ftrace_swapper_pid)
4291 mutex_unlock(&ftrace_lock);
4295 static void ftrace_pid_reset(void)
4297 struct ftrace_pid *fpid, *safe;
4299 mutex_lock(&ftrace_lock);
4300 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4301 struct pid *pid = fpid->pid;
4303 clear_ftrace_pid_task(pid);
4305 list_del(&fpid->list);
4309 ftrace_update_pid_func();
4310 ftrace_startup_enable(0);
4312 mutex_unlock(&ftrace_lock);
4315 static void *fpid_start(struct seq_file *m, loff_t *pos)
4317 mutex_lock(&ftrace_lock);
4319 if (list_empty(&ftrace_pids) && (!*pos))
4322 return seq_list_start(&ftrace_pids, *pos);
4325 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4330 return seq_list_next(v, &ftrace_pids, pos);
4333 static void fpid_stop(struct seq_file *m, void *p)
4335 mutex_unlock(&ftrace_lock);
4338 static int fpid_show(struct seq_file *m, void *v)
4340 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4342 if (v == (void *)1) {
4343 seq_printf(m, "no pid\n");
4347 if (fpid->pid == ftrace_swapper_pid)
4348 seq_printf(m, "swapper tasks\n");
4350 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4355 static const struct seq_operations ftrace_pid_sops = {
4356 .start = fpid_start,
4363 ftrace_pid_open(struct inode *inode, struct file *file)
4367 if ((file->f_mode & FMODE_WRITE) &&
4368 (file->f_flags & O_TRUNC))
4371 if (file->f_mode & FMODE_READ)
4372 ret = seq_open(file, &ftrace_pid_sops);
4378 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4379 size_t cnt, loff_t *ppos)
4385 if (cnt >= sizeof(buf))
4388 if (copy_from_user(&buf, ubuf, cnt))
4394 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4395 * to clean the filter quietly.
4397 tmp = strstrip(buf);
4398 if (strlen(tmp) == 0)
4401 ret = kstrtol(tmp, 10, &val);
4405 ret = ftrace_pid_add(val);
4407 return ret ? ret : cnt;
4411 ftrace_pid_release(struct inode *inode, struct file *file)
4413 if (file->f_mode & FMODE_READ)
4414 seq_release(inode, file);
4419 static const struct file_operations ftrace_pid_fops = {
4420 .open = ftrace_pid_open,
4421 .write = ftrace_pid_write,
4423 .llseek = ftrace_filter_lseek,
4424 .release = ftrace_pid_release,
4427 static __init int ftrace_init_debugfs(void)
4429 struct dentry *d_tracer;
4431 d_tracer = tracing_init_dentry();
4435 ftrace_init_dyn_debugfs(d_tracer);
4437 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4438 NULL, &ftrace_pid_fops);
4440 ftrace_profile_debugfs(d_tracer);
4444 fs_initcall(ftrace_init_debugfs);
4447 * ftrace_kill - kill ftrace
4449 * This function should be used by panic code. It stops ftrace
4450 * but in a not so nice way. If you need to simply kill ftrace
4451 * from a non-atomic section, use ftrace_kill.
4453 void ftrace_kill(void)
4455 ftrace_disabled = 1;
4457 clear_ftrace_function();
4461 * Test if ftrace is dead or not.
4463 int ftrace_is_dead(void)
4465 return ftrace_disabled;
4469 * register_ftrace_function - register a function for profiling
4470 * @ops - ops structure that holds the function for profiling.
4472 * Register a function to be called by all functions in the
4475 * Note: @ops->func and all the functions it calls must be labeled
4476 * with "notrace", otherwise it will go into a
4479 int register_ftrace_function(struct ftrace_ops *ops)
4483 mutex_lock(&ftrace_lock);
4485 ret = __register_ftrace_function(ops);
4487 ret = ftrace_startup(ops, 0);
4489 mutex_unlock(&ftrace_lock);
4493 EXPORT_SYMBOL_GPL(register_ftrace_function);
4496 * unregister_ftrace_function - unregister a function for profiling.
4497 * @ops - ops structure that holds the function to unregister
4499 * Unregister a function that was added to be called by ftrace profiling.
4501 int unregister_ftrace_function(struct ftrace_ops *ops)
4505 mutex_lock(&ftrace_lock);
4506 ret = __unregister_ftrace_function(ops);
4508 ftrace_shutdown(ops, 0);
4509 mutex_unlock(&ftrace_lock);
4513 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4516 ftrace_enable_sysctl(struct ctl_table *table, int write,
4517 void __user *buffer, size_t *lenp,
4522 mutex_lock(&ftrace_lock);
4524 if (unlikely(ftrace_disabled))
4527 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4529 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4532 last_ftrace_enabled = !!ftrace_enabled;
4534 if (ftrace_enabled) {
4536 ftrace_startup_sysctl();
4538 /* we are starting ftrace again */
4539 if (ftrace_ops_list != &ftrace_list_end)
4540 update_ftrace_function();
4543 /* stopping ftrace calls (just send to ftrace_stub) */
4544 ftrace_trace_function = ftrace_stub;
4546 ftrace_shutdown_sysctl();
4550 mutex_unlock(&ftrace_lock);
4554 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4556 static int ftrace_graph_active;
4557 static struct notifier_block ftrace_suspend_notifier;
4559 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4564 /* The callbacks that hook a function */
4565 trace_func_graph_ret_t ftrace_graph_return =
4566 (trace_func_graph_ret_t)ftrace_stub;
4567 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4569 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4570 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4574 unsigned long flags;
4575 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4576 struct task_struct *g, *t;
4578 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4579 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4580 * sizeof(struct ftrace_ret_stack),
4582 if (!ret_stack_list[i]) {
4590 read_lock_irqsave(&tasklist_lock, flags);
4591 do_each_thread(g, t) {
4597 if (t->ret_stack == NULL) {
4598 atomic_set(&t->tracing_graph_pause, 0);
4599 atomic_set(&t->trace_overrun, 0);
4600 t->curr_ret_stack = -1;
4601 /* Make sure the tasks see the -1 first: */
4603 t->ret_stack = ret_stack_list[start++];
4605 } while_each_thread(g, t);
4608 read_unlock_irqrestore(&tasklist_lock, flags);
4610 for (i = start; i < end; i++)
4611 kfree(ret_stack_list[i]);
4616 ftrace_graph_probe_sched_switch(void *ignore,
4617 struct task_struct *prev, struct task_struct *next)
4619 unsigned long long timestamp;
4623 * Does the user want to count the time a function was asleep.
4624 * If so, do not update the time stamps.
4626 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4629 timestamp = trace_clock_local();
4631 prev->ftrace_timestamp = timestamp;
4633 /* only process tasks that we timestamped */
4634 if (!next->ftrace_timestamp)
4638 * Update all the counters in next to make up for the
4639 * time next was sleeping.
4641 timestamp -= next->ftrace_timestamp;
4643 for (index = next->curr_ret_stack; index >= 0; index--)
4644 next->ret_stack[index].calltime += timestamp;
4647 /* Allocate a return stack for each task */
4648 static int start_graph_tracing(void)
4650 struct ftrace_ret_stack **ret_stack_list;
4653 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4654 sizeof(struct ftrace_ret_stack *),
4657 if (!ret_stack_list)
4660 /* The cpu_boot init_task->ret_stack will never be freed */
4661 for_each_online_cpu(cpu) {
4662 if (!idle_task(cpu)->ret_stack)
4663 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4667 ret = alloc_retstack_tasklist(ret_stack_list);
4668 } while (ret == -EAGAIN);
4671 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4673 pr_info("ftrace_graph: Couldn't activate tracepoint"
4674 " probe to kernel_sched_switch\n");
4677 kfree(ret_stack_list);
4682 * Hibernation protection.
4683 * The state of the current task is too much unstable during
4684 * suspend/restore to disk. We want to protect against that.
4687 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4691 case PM_HIBERNATION_PREPARE:
4692 pause_graph_tracing();
4695 case PM_POST_HIBERNATION:
4696 unpause_graph_tracing();
4702 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4703 trace_func_graph_ent_t entryfunc)
4707 mutex_lock(&ftrace_lock);
4709 /* we currently allow only one tracer registered at a time */
4710 if (ftrace_graph_active) {
4715 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4716 register_pm_notifier(&ftrace_suspend_notifier);
4718 ftrace_graph_active++;
4719 ret = start_graph_tracing();
4721 ftrace_graph_active--;
4725 ftrace_graph_return = retfunc;
4726 ftrace_graph_entry = entryfunc;
4728 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4731 mutex_unlock(&ftrace_lock);
4735 void unregister_ftrace_graph(void)
4737 mutex_lock(&ftrace_lock);
4739 if (unlikely(!ftrace_graph_active))
4742 ftrace_graph_active--;
4743 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4744 ftrace_graph_entry = ftrace_graph_entry_stub;
4745 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4746 unregister_pm_notifier(&ftrace_suspend_notifier);
4747 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4750 mutex_unlock(&ftrace_lock);
4753 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4756 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4758 atomic_set(&t->tracing_graph_pause, 0);
4759 atomic_set(&t->trace_overrun, 0);
4760 t->ftrace_timestamp = 0;
4761 /* make curr_ret_stack visible before we add the ret_stack */
4763 t->ret_stack = ret_stack;
4767 * Allocate a return stack for the idle task. May be the first
4768 * time through, or it may be done by CPU hotplug online.
4770 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4772 t->curr_ret_stack = -1;
4774 * The idle task has no parent, it either has its own
4775 * stack or no stack at all.
4778 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4780 if (ftrace_graph_active) {
4781 struct ftrace_ret_stack *ret_stack;
4783 ret_stack = per_cpu(idle_ret_stack, cpu);
4785 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4786 * sizeof(struct ftrace_ret_stack),
4790 per_cpu(idle_ret_stack, cpu) = ret_stack;
4792 graph_init_task(t, ret_stack);
4796 /* Allocate a return stack for newly created task */
4797 void ftrace_graph_init_task(struct task_struct *t)
4799 /* Make sure we do not use the parent ret_stack */
4800 t->ret_stack = NULL;
4801 t->curr_ret_stack = -1;
4803 if (ftrace_graph_active) {
4804 struct ftrace_ret_stack *ret_stack;
4806 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4807 * sizeof(struct ftrace_ret_stack),
4811 graph_init_task(t, ret_stack);
4815 void ftrace_graph_exit_task(struct task_struct *t)
4817 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4819 t->ret_stack = NULL;
4820 /* NULL must become visible to IRQs before we free it: */
4826 void ftrace_graph_stop(void)