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_CONTROL)
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
72 #define INIT_OPS_HASH(opsname)
75 static struct ftrace_ops ftrace_list_end __read_mostly = {
77 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
78 INIT_OPS_HASH(ftrace_list_end)
81 /* ftrace_enabled is a method to turn ftrace on or off */
82 int ftrace_enabled __read_mostly;
83 static int last_ftrace_enabled;
85 /* Current function tracing op */
86 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
87 /* What to set function_trace_op to */
88 static struct ftrace_ops *set_function_trace_op;
90 /* List for set_ftrace_pid's pids. */
91 LIST_HEAD(ftrace_pids);
93 struct list_head list;
98 * ftrace_disabled is set when an anomaly is discovered.
99 * ftrace_disabled is much stronger than ftrace_enabled.
101 static int ftrace_disabled __read_mostly;
103 static DEFINE_MUTEX(ftrace_lock);
105 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
106 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
107 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
108 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
109 static struct ftrace_ops global_ops;
110 static struct ftrace_ops control_ops;
112 #if ARCH_SUPPORTS_FTRACE_OPS
113 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
114 struct ftrace_ops *op, struct pt_regs *regs);
116 /* See comment below, where ftrace_ops_list_func is defined */
117 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
118 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
122 * Traverse the ftrace_global_list, invoking all entries. The reason that we
123 * can use rcu_dereference_raw_notrace() is that elements removed from this list
124 * are simply leaked, so there is no need to interact with a grace-period
125 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
126 * concurrent insertions into the ftrace_global_list.
128 * Silly Alpha and silly pointer-speculation compiler optimizations!
130 #define do_for_each_ftrace_op(op, list) \
131 op = rcu_dereference_raw_notrace(list); \
135 * Optimized for just a single item in the list (as that is the normal case).
137 #define while_for_each_ftrace_op(op) \
138 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
139 unlikely((op) != &ftrace_list_end))
141 static inline void ftrace_ops_init(struct ftrace_ops *ops)
143 #ifdef CONFIG_DYNAMIC_FTRACE
144 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
145 mutex_init(&ops->local_hash.regex_lock);
146 ops->func_hash = &ops->local_hash;
147 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
153 * ftrace_nr_registered_ops - return number of ops registered
155 * Returns the number of ftrace_ops registered and tracing functions
157 int ftrace_nr_registered_ops(void)
159 struct ftrace_ops *ops;
162 mutex_lock(&ftrace_lock);
164 for (ops = ftrace_ops_list;
165 ops != &ftrace_list_end; ops = ops->next)
168 mutex_unlock(&ftrace_lock);
173 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
174 struct ftrace_ops *op, struct pt_regs *regs)
176 if (!test_tsk_trace_trace(current))
179 ftrace_pid_function(ip, parent_ip, op, regs);
182 static void set_ftrace_pid_function(ftrace_func_t func)
184 /* do not set ftrace_pid_function to itself! */
185 if (func != ftrace_pid_func)
186 ftrace_pid_function = func;
190 * clear_ftrace_function - reset the ftrace function
192 * This NULLs the ftrace function and in essence stops
193 * tracing. There may be lag
195 void clear_ftrace_function(void)
197 ftrace_trace_function = ftrace_stub;
198 ftrace_pid_function = ftrace_stub;
201 static void control_ops_disable_all(struct ftrace_ops *ops)
205 for_each_possible_cpu(cpu)
206 *per_cpu_ptr(ops->disabled, cpu) = 1;
209 static int control_ops_alloc(struct ftrace_ops *ops)
211 int __percpu *disabled;
213 disabled = alloc_percpu(int);
217 ops->disabled = disabled;
218 control_ops_disable_all(ops);
222 static void ftrace_sync(struct work_struct *work)
225 * This function is just a stub to implement a hard force
226 * of synchronize_sched(). This requires synchronizing
227 * tasks even in userspace and idle.
229 * Yes, function tracing is rude.
233 static void ftrace_sync_ipi(void *data)
235 /* Probably not needed, but do it anyway */
239 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
240 static void update_function_graph_func(void);
242 static inline void update_function_graph_func(void) { }
245 static void update_ftrace_function(void)
250 * If we are at the end of the list and this ops is
251 * recursion safe and not dynamic and the arch supports passing ops,
252 * then have the mcount trampoline call the function directly.
254 if (ftrace_ops_list == &ftrace_list_end ||
255 (ftrace_ops_list->next == &ftrace_list_end &&
256 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
257 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
258 !FTRACE_FORCE_LIST_FUNC)) {
259 /* Set the ftrace_ops that the arch callback uses */
260 set_function_trace_op = ftrace_ops_list;
261 func = ftrace_ops_list->func;
263 /* Just use the default ftrace_ops */
264 set_function_trace_op = &ftrace_list_end;
265 func = ftrace_ops_list_func;
268 update_function_graph_func();
270 /* If there's no change, then do nothing more here */
271 if (ftrace_trace_function == func)
275 * If we are using the list function, it doesn't care
276 * about the function_trace_ops.
278 if (func == ftrace_ops_list_func) {
279 ftrace_trace_function = func;
281 * Don't even bother setting function_trace_ops,
282 * it would be racy to do so anyway.
287 #ifndef CONFIG_DYNAMIC_FTRACE
289 * For static tracing, we need to be a bit more careful.
290 * The function change takes affect immediately. Thus,
291 * we need to coorditate the setting of the function_trace_ops
292 * with the setting of the ftrace_trace_function.
294 * Set the function to the list ops, which will call the
295 * function we want, albeit indirectly, but it handles the
296 * ftrace_ops and doesn't depend on function_trace_op.
298 ftrace_trace_function = ftrace_ops_list_func;
300 * Make sure all CPUs see this. Yes this is slow, but static
301 * tracing is slow and nasty to have enabled.
303 schedule_on_each_cpu(ftrace_sync);
304 /* Now all cpus are using the list ops. */
305 function_trace_op = set_function_trace_op;
306 /* Make sure the function_trace_op is visible on all CPUs */
308 /* Nasty way to force a rmb on all cpus */
309 smp_call_function(ftrace_sync_ipi, NULL, 1);
310 /* OK, we are all set to update the ftrace_trace_function now! */
311 #endif /* !CONFIG_DYNAMIC_FTRACE */
313 ftrace_trace_function = func;
316 int using_ftrace_ops_list_func(void)
318 return ftrace_trace_function == ftrace_ops_list_func;
321 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
325 * We are entering ops into the list but another
326 * CPU might be walking that list. We need to make sure
327 * the ops->next pointer is valid before another CPU sees
328 * the ops pointer included into the list.
330 rcu_assign_pointer(*list, ops);
333 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
335 struct ftrace_ops **p;
338 * If we are removing the last function, then simply point
339 * to the ftrace_stub.
341 if (*list == ops && ops->next == &ftrace_list_end) {
342 *list = &ftrace_list_end;
346 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
357 static void add_ftrace_list_ops(struct ftrace_ops **list,
358 struct ftrace_ops *main_ops,
359 struct ftrace_ops *ops)
361 int first = *list == &ftrace_list_end;
362 add_ftrace_ops(list, ops);
364 add_ftrace_ops(&ftrace_ops_list, main_ops);
367 static int remove_ftrace_list_ops(struct ftrace_ops **list,
368 struct ftrace_ops *main_ops,
369 struct ftrace_ops *ops)
371 int ret = remove_ftrace_ops(list, ops);
372 if (!ret && *list == &ftrace_list_end)
373 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
377 static int __register_ftrace_function(struct ftrace_ops *ops)
379 if (ops->flags & FTRACE_OPS_FL_DELETED)
382 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
385 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
387 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
388 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
389 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
391 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
392 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
395 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
396 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
399 if (!core_kernel_data((unsigned long)ops))
400 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
402 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
403 if (control_ops_alloc(ops))
405 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
407 add_ftrace_ops(&ftrace_ops_list, ops);
410 update_ftrace_function();
415 static int __unregister_ftrace_function(struct ftrace_ops *ops)
419 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
422 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
423 ret = remove_ftrace_list_ops(&ftrace_control_list,
426 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
432 update_ftrace_function();
437 static void ftrace_update_pid_func(void)
439 /* Only do something if we are tracing something */
440 if (ftrace_trace_function == ftrace_stub)
443 update_ftrace_function();
446 #ifdef CONFIG_FUNCTION_PROFILER
447 struct ftrace_profile {
448 struct hlist_node node;
450 unsigned long counter;
451 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
452 unsigned long long time;
453 unsigned long long time_squared;
457 struct ftrace_profile_page {
458 struct ftrace_profile_page *next;
460 struct ftrace_profile records[];
463 struct ftrace_profile_stat {
465 struct hlist_head *hash;
466 struct ftrace_profile_page *pages;
467 struct ftrace_profile_page *start;
468 struct tracer_stat stat;
471 #define PROFILE_RECORDS_SIZE \
472 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
474 #define PROFILES_PER_PAGE \
475 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
477 static int ftrace_profile_enabled __read_mostly;
479 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
480 static DEFINE_MUTEX(ftrace_profile_lock);
482 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
484 #define FTRACE_PROFILE_HASH_BITS 10
485 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
488 function_stat_next(void *v, int idx)
490 struct ftrace_profile *rec = v;
491 struct ftrace_profile_page *pg;
493 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
499 if ((void *)rec >= (void *)&pg->records[pg->index]) {
503 rec = &pg->records[0];
511 static void *function_stat_start(struct tracer_stat *trace)
513 struct ftrace_profile_stat *stat =
514 container_of(trace, struct ftrace_profile_stat, stat);
516 if (!stat || !stat->start)
519 return function_stat_next(&stat->start->records[0], 0);
522 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
523 /* function graph compares on total time */
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->time < b->time)
531 if (a->time > b->time)
537 /* not function graph compares against hits */
538 static int function_stat_cmp(void *p1, void *p2)
540 struct ftrace_profile *a = p1;
541 struct ftrace_profile *b = p2;
543 if (a->counter < b->counter)
545 if (a->counter > b->counter)
552 static int function_stat_headers(struct seq_file *m)
554 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
555 seq_printf(m, " Function "
558 "--- ---- --- ---\n");
560 seq_printf(m, " Function Hit\n"
566 static int function_stat_show(struct seq_file *m, void *v)
568 struct ftrace_profile *rec = v;
569 char str[KSYM_SYMBOL_LEN];
571 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
572 static struct trace_seq s;
573 unsigned long long avg;
574 unsigned long long stddev;
576 mutex_lock(&ftrace_profile_lock);
578 /* we raced with function_profile_reset() */
579 if (unlikely(rec->counter == 0)) {
584 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
585 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
587 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
590 do_div(avg, rec->counter);
592 /* Sample standard deviation (s^2) */
593 if (rec->counter <= 1)
597 * Apply Welford's method:
598 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
600 stddev = rec->counter * rec->time_squared -
601 rec->time * rec->time;
604 * Divide only 1000 for ns^2 -> us^2 conversion.
605 * trace_print_graph_duration will divide 1000 again.
607 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
611 trace_print_graph_duration(rec->time, &s);
612 trace_seq_puts(&s, " ");
613 trace_print_graph_duration(avg, &s);
614 trace_seq_puts(&s, " ");
615 trace_print_graph_duration(stddev, &s);
616 trace_print_seq(m, &s);
620 mutex_unlock(&ftrace_profile_lock);
625 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
627 struct ftrace_profile_page *pg;
629 pg = stat->pages = stat->start;
632 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
637 memset(stat->hash, 0,
638 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
641 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
643 struct ftrace_profile_page *pg;
648 /* If we already allocated, do nothing */
652 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
656 #ifdef CONFIG_DYNAMIC_FTRACE
657 functions = ftrace_update_tot_cnt;
660 * We do not know the number of functions that exist because
661 * dynamic tracing is what counts them. With past experience
662 * we have around 20K functions. That should be more than enough.
663 * It is highly unlikely we will execute every function in
669 pg = stat->start = stat->pages;
671 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
673 for (i = 1; i < pages; i++) {
674 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
685 unsigned long tmp = (unsigned long)pg;
697 static int ftrace_profile_init_cpu(int cpu)
699 struct ftrace_profile_stat *stat;
702 stat = &per_cpu(ftrace_profile_stats, cpu);
705 /* If the profile is already created, simply reset it */
706 ftrace_profile_reset(stat);
711 * We are profiling all functions, but usually only a few thousand
712 * functions are hit. We'll make a hash of 1024 items.
714 size = FTRACE_PROFILE_HASH_SIZE;
716 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
721 /* Preallocate the function profiling pages */
722 if (ftrace_profile_pages_init(stat) < 0) {
731 static int ftrace_profile_init(void)
736 for_each_possible_cpu(cpu) {
737 ret = ftrace_profile_init_cpu(cpu);
745 /* interrupts must be disabled */
746 static struct ftrace_profile *
747 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
749 struct ftrace_profile *rec;
750 struct hlist_head *hhd;
753 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
754 hhd = &stat->hash[key];
756 if (hlist_empty(hhd))
759 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
767 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
768 struct ftrace_profile *rec)
772 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
773 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
777 * The memory is already allocated, this simply finds a new record to use.
779 static struct ftrace_profile *
780 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
782 struct ftrace_profile *rec = NULL;
784 /* prevent recursion (from NMIs) */
785 if (atomic_inc_return(&stat->disabled) != 1)
789 * Try to find the function again since an NMI
790 * could have added it
792 rec = ftrace_find_profiled_func(stat, ip);
796 if (stat->pages->index == PROFILES_PER_PAGE) {
797 if (!stat->pages->next)
799 stat->pages = stat->pages->next;
802 rec = &stat->pages->records[stat->pages->index++];
804 ftrace_add_profile(stat, rec);
807 atomic_dec(&stat->disabled);
813 function_profile_call(unsigned long ip, unsigned long parent_ip,
814 struct ftrace_ops *ops, struct pt_regs *regs)
816 struct ftrace_profile_stat *stat;
817 struct ftrace_profile *rec;
820 if (!ftrace_profile_enabled)
823 local_irq_save(flags);
825 stat = this_cpu_ptr(&ftrace_profile_stats);
826 if (!stat->hash || !ftrace_profile_enabled)
829 rec = ftrace_find_profiled_func(stat, ip);
831 rec = ftrace_profile_alloc(stat, ip);
838 local_irq_restore(flags);
841 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
842 static int profile_graph_entry(struct ftrace_graph_ent *trace)
844 function_profile_call(trace->func, 0, NULL, NULL);
848 static void profile_graph_return(struct ftrace_graph_ret *trace)
850 struct ftrace_profile_stat *stat;
851 unsigned long long calltime;
852 struct ftrace_profile *rec;
855 local_irq_save(flags);
856 stat = this_cpu_ptr(&ftrace_profile_stats);
857 if (!stat->hash || !ftrace_profile_enabled)
860 /* If the calltime was zero'd ignore it */
861 if (!trace->calltime)
864 calltime = trace->rettime - trace->calltime;
866 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
869 index = trace->depth;
871 /* Append this call time to the parent time to subtract */
873 current->ret_stack[index - 1].subtime += calltime;
875 if (current->ret_stack[index].subtime < calltime)
876 calltime -= current->ret_stack[index].subtime;
881 rec = ftrace_find_profiled_func(stat, trace->func);
883 rec->time += calltime;
884 rec->time_squared += calltime * calltime;
888 local_irq_restore(flags);
891 static int register_ftrace_profiler(void)
893 return register_ftrace_graph(&profile_graph_return,
894 &profile_graph_entry);
897 static void unregister_ftrace_profiler(void)
899 unregister_ftrace_graph();
902 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
903 .func = function_profile_call,
904 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
905 INIT_OPS_HASH(ftrace_profile_ops)
908 static int register_ftrace_profiler(void)
910 return register_ftrace_function(&ftrace_profile_ops);
913 static void unregister_ftrace_profiler(void)
915 unregister_ftrace_function(&ftrace_profile_ops);
917 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
920 ftrace_profile_write(struct file *filp, const char __user *ubuf,
921 size_t cnt, loff_t *ppos)
926 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
932 mutex_lock(&ftrace_profile_lock);
933 if (ftrace_profile_enabled ^ val) {
935 ret = ftrace_profile_init();
941 ret = register_ftrace_profiler();
946 ftrace_profile_enabled = 1;
948 ftrace_profile_enabled = 0;
950 * unregister_ftrace_profiler calls stop_machine
951 * so this acts like an synchronize_sched.
953 unregister_ftrace_profiler();
957 mutex_unlock(&ftrace_profile_lock);
965 ftrace_profile_read(struct file *filp, char __user *ubuf,
966 size_t cnt, loff_t *ppos)
968 char buf[64]; /* big enough to hold a number */
971 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
972 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
975 static const struct file_operations ftrace_profile_fops = {
976 .open = tracing_open_generic,
977 .read = ftrace_profile_read,
978 .write = ftrace_profile_write,
979 .llseek = default_llseek,
982 /* used to initialize the real stat files */
983 static struct tracer_stat function_stats __initdata = {
985 .stat_start = function_stat_start,
986 .stat_next = function_stat_next,
987 .stat_cmp = function_stat_cmp,
988 .stat_headers = function_stat_headers,
989 .stat_show = function_stat_show
992 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
994 struct ftrace_profile_stat *stat;
995 struct dentry *entry;
1000 for_each_possible_cpu(cpu) {
1001 stat = &per_cpu(ftrace_profile_stats, cpu);
1003 /* allocate enough for function name + cpu number */
1004 name = kmalloc(32, GFP_KERNEL);
1007 * The files created are permanent, if something happens
1008 * we still do not free memory.
1011 "Could not allocate stat file for cpu %d\n",
1015 stat->stat = function_stats;
1016 snprintf(name, 32, "function%d", cpu);
1017 stat->stat.name = name;
1018 ret = register_stat_tracer(&stat->stat);
1021 "Could not register function stat for cpu %d\n",
1028 entry = debugfs_create_file("function_profile_enabled", 0644,
1029 d_tracer, NULL, &ftrace_profile_fops);
1031 pr_warning("Could not create debugfs "
1032 "'function_profile_enabled' entry\n");
1035 #else /* CONFIG_FUNCTION_PROFILER */
1036 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1039 #endif /* CONFIG_FUNCTION_PROFILER */
1041 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1043 #ifdef CONFIG_DYNAMIC_FTRACE
1045 static struct ftrace_ops *removed_ops;
1047 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1048 # error Dynamic ftrace depends on MCOUNT_RECORD
1051 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1053 struct ftrace_func_probe {
1054 struct hlist_node node;
1055 struct ftrace_probe_ops *ops;
1056 unsigned long flags;
1059 struct list_head free_list;
1062 struct ftrace_func_entry {
1063 struct hlist_node hlist;
1067 struct ftrace_hash {
1068 unsigned long size_bits;
1069 struct hlist_head *buckets;
1070 unsigned long count;
1071 struct rcu_head rcu;
1075 * We make these constant because no one should touch them,
1076 * but they are used as the default "empty hash", to avoid allocating
1077 * it all the time. These are in a read only section such that if
1078 * anyone does try to modify it, it will cause an exception.
1080 static const struct hlist_head empty_buckets[1];
1081 static const struct ftrace_hash empty_hash = {
1082 .buckets = (struct hlist_head *)empty_buckets,
1084 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1086 static struct ftrace_ops global_ops = {
1087 .func = ftrace_stub,
1088 .local_hash.notrace_hash = EMPTY_HASH,
1089 .local_hash.filter_hash = EMPTY_HASH,
1090 INIT_OPS_HASH(global_ops)
1091 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1092 FTRACE_OPS_FL_INITIALIZED,
1095 struct ftrace_page {
1096 struct ftrace_page *next;
1097 struct dyn_ftrace *records;
1102 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1103 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1105 /* estimate from running different kernels */
1106 #define NR_TO_INIT 10000
1108 static struct ftrace_page *ftrace_pages_start;
1109 static struct ftrace_page *ftrace_pages;
1111 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1113 return !hash || !hash->count;
1116 static struct ftrace_func_entry *
1117 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1120 struct ftrace_func_entry *entry;
1121 struct hlist_head *hhd;
1123 if (ftrace_hash_empty(hash))
1126 if (hash->size_bits > 0)
1127 key = hash_long(ip, hash->size_bits);
1131 hhd = &hash->buckets[key];
1133 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1134 if (entry->ip == ip)
1140 static void __add_hash_entry(struct ftrace_hash *hash,
1141 struct ftrace_func_entry *entry)
1143 struct hlist_head *hhd;
1146 if (hash->size_bits)
1147 key = hash_long(entry->ip, hash->size_bits);
1151 hhd = &hash->buckets[key];
1152 hlist_add_head(&entry->hlist, hhd);
1156 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1158 struct ftrace_func_entry *entry;
1160 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1165 __add_hash_entry(hash, entry);
1171 free_hash_entry(struct ftrace_hash *hash,
1172 struct ftrace_func_entry *entry)
1174 hlist_del(&entry->hlist);
1180 remove_hash_entry(struct ftrace_hash *hash,
1181 struct ftrace_func_entry *entry)
1183 hlist_del(&entry->hlist);
1187 static void ftrace_hash_clear(struct ftrace_hash *hash)
1189 struct hlist_head *hhd;
1190 struct hlist_node *tn;
1191 struct ftrace_func_entry *entry;
1192 int size = 1 << hash->size_bits;
1198 for (i = 0; i < size; i++) {
1199 hhd = &hash->buckets[i];
1200 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1201 free_hash_entry(hash, entry);
1203 FTRACE_WARN_ON(hash->count);
1206 static void free_ftrace_hash(struct ftrace_hash *hash)
1208 if (!hash || hash == EMPTY_HASH)
1210 ftrace_hash_clear(hash);
1211 kfree(hash->buckets);
1215 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1217 struct ftrace_hash *hash;
1219 hash = container_of(rcu, struct ftrace_hash, rcu);
1220 free_ftrace_hash(hash);
1223 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1225 if (!hash || hash == EMPTY_HASH)
1227 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1230 void ftrace_free_filter(struct ftrace_ops *ops)
1232 ftrace_ops_init(ops);
1233 free_ftrace_hash(ops->func_hash->filter_hash);
1234 free_ftrace_hash(ops->func_hash->notrace_hash);
1237 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1239 struct ftrace_hash *hash;
1242 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1246 size = 1 << size_bits;
1247 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1249 if (!hash->buckets) {
1254 hash->size_bits = size_bits;
1259 static struct ftrace_hash *
1260 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1262 struct ftrace_func_entry *entry;
1263 struct ftrace_hash *new_hash;
1268 new_hash = alloc_ftrace_hash(size_bits);
1273 if (ftrace_hash_empty(hash))
1276 size = 1 << hash->size_bits;
1277 for (i = 0; i < size; i++) {
1278 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1279 ret = add_hash_entry(new_hash, entry->ip);
1285 FTRACE_WARN_ON(new_hash->count != hash->count);
1290 free_ftrace_hash(new_hash);
1295 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1297 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1300 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1301 struct ftrace_hash **dst, struct ftrace_hash *src)
1303 struct ftrace_func_entry *entry;
1304 struct hlist_node *tn;
1305 struct hlist_head *hhd;
1306 struct ftrace_hash *old_hash;
1307 struct ftrace_hash *new_hash;
1308 int size = src->count;
1313 * If the new source is empty, just free dst and assign it
1317 new_hash = EMPTY_HASH;
1322 * Make the hash size about 1/2 the # found
1324 for (size /= 2; size; size >>= 1)
1327 /* Don't allocate too much */
1328 if (bits > FTRACE_HASH_MAX_BITS)
1329 bits = FTRACE_HASH_MAX_BITS;
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, tn, hhd, hlist) {
1339 remove_hash_entry(src, entry);
1340 __add_hash_entry(new_hash, entry);
1346 * Remove the current set, update the hash and add
1349 ftrace_hash_rec_disable(ops, enable);
1352 rcu_assign_pointer(*dst, new_hash);
1353 free_ftrace_hash_rcu(old_hash);
1355 ftrace_hash_rec_enable(ops, enable);
1361 * Test the hashes for this ops to see if we want to call
1362 * the ops->func or not.
1364 * It's a match if the ip is in the ops->filter_hash or
1365 * the filter_hash does not exist or is empty,
1367 * the ip is not in the ops->notrace_hash.
1369 * This needs to be called with preemption disabled as
1370 * the hashes are freed with call_rcu_sched().
1373 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1375 struct ftrace_hash *filter_hash;
1376 struct ftrace_hash *notrace_hash;
1379 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1381 * There's a small race when adding ops that the ftrace handler
1382 * that wants regs, may be called without them. We can not
1383 * allow that handler to be called if regs is NULL.
1385 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1389 filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1390 notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
1392 if ((ftrace_hash_empty(filter_hash) ||
1393 ftrace_lookup_ip(filter_hash, ip)) &&
1394 (ftrace_hash_empty(notrace_hash) ||
1395 !ftrace_lookup_ip(notrace_hash, ip)))
1404 * This is a double for. Do not use 'break' to break out of the loop,
1405 * you must use a goto.
1407 #define do_for_each_ftrace_rec(pg, rec) \
1408 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1410 for (_____i = 0; _____i < pg->index; _____i++) { \
1411 rec = &pg->records[_____i];
1413 #define while_for_each_ftrace_rec() \
1418 static int ftrace_cmp_recs(const void *a, const void *b)
1420 const struct dyn_ftrace *key = a;
1421 const struct dyn_ftrace *rec = b;
1423 if (key->flags < rec->ip)
1425 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1430 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1432 struct ftrace_page *pg;
1433 struct dyn_ftrace *rec;
1434 struct dyn_ftrace key;
1437 key.flags = end; /* overload flags, as it is unsigned long */
1439 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1440 if (end < pg->records[0].ip ||
1441 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1443 rec = bsearch(&key, pg->records, pg->index,
1444 sizeof(struct dyn_ftrace),
1454 * ftrace_location - return true if the ip giving is a traced location
1455 * @ip: the instruction pointer to check
1457 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1458 * That is, the instruction that is either a NOP or call to
1459 * the function tracer. It checks the ftrace internal tables to
1460 * determine if the address belongs or not.
1462 unsigned long ftrace_location(unsigned long ip)
1464 return ftrace_location_range(ip, ip);
1468 * ftrace_text_reserved - return true if range contains an ftrace location
1469 * @start: start of range to search
1470 * @end: end of range to search (inclusive). @end points to the last byte to check.
1472 * Returns 1 if @start and @end contains a ftrace location.
1473 * That is, the instruction that is either a NOP or call to
1474 * the function tracer. It checks the ftrace internal tables to
1475 * determine if the address belongs or not.
1477 int ftrace_text_reserved(const void *start, const void *end)
1481 ret = ftrace_location_range((unsigned long)start,
1482 (unsigned long)end);
1487 /* Test if ops registered to this rec needs regs */
1488 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1490 struct ftrace_ops *ops;
1491 bool keep_regs = false;
1493 for (ops = ftrace_ops_list;
1494 ops != &ftrace_list_end; ops = ops->next) {
1495 /* pass rec in as regs to have non-NULL val */
1496 if (ftrace_ops_test(ops, rec->ip, rec)) {
1497 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1507 static void ftrace_remove_tramp(struct ftrace_ops *ops,
1508 struct dyn_ftrace *rec)
1510 struct ftrace_func_entry *entry;
1512 entry = ftrace_lookup_ip(ops->tramp_hash, rec->ip);
1517 * The tramp_hash entry will be removed at time
1520 ops->nr_trampolines--;
1521 rec->flags &= ~FTRACE_FL_TRAMP;
1524 static void ftrace_clear_tramps(struct dyn_ftrace *rec)
1526 struct ftrace_ops *op;
1528 do_for_each_ftrace_op(op, ftrace_ops_list) {
1529 if (op->nr_trampolines)
1530 ftrace_remove_tramp(op, rec);
1531 } while_for_each_ftrace_op(op);
1534 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1538 struct ftrace_hash *hash;
1539 struct ftrace_hash *other_hash;
1540 struct ftrace_page *pg;
1541 struct dyn_ftrace *rec;
1545 /* Only update if the ops has been registered */
1546 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1550 * In the filter_hash case:
1551 * If the count is zero, we update all records.
1552 * Otherwise we just update the items in the hash.
1554 * In the notrace_hash case:
1555 * We enable the update in the hash.
1556 * As disabling notrace means enabling the tracing,
1557 * and enabling notrace means disabling, the inc variable
1561 hash = ops->func_hash->filter_hash;
1562 other_hash = ops->func_hash->notrace_hash;
1563 if (ftrace_hash_empty(hash))
1567 hash = ops->func_hash->notrace_hash;
1568 other_hash = ops->func_hash->filter_hash;
1570 * If the notrace hash has no items,
1571 * then there's nothing to do.
1573 if (ftrace_hash_empty(hash))
1577 do_for_each_ftrace_rec(pg, rec) {
1578 int in_other_hash = 0;
1584 * Only the filter_hash affects all records.
1585 * Update if the record is not in the notrace hash.
1587 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1590 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1591 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1594 * If filter_hash is set, we want to match all functions
1595 * that are in the hash but not in the other hash.
1597 * If filter_hash is not set, then we are decrementing.
1598 * That means we match anything that is in the hash
1599 * and also in the other_hash. That is, we need to turn
1600 * off functions in the other hash because they are disabled
1603 if (filter_hash && in_hash && !in_other_hash)
1605 else if (!filter_hash && in_hash &&
1606 (in_other_hash || ftrace_hash_empty(other_hash)))
1614 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1618 * If there's only a single callback registered to a
1619 * function, and the ops has a trampoline registered
1620 * for it, then we can call it directly.
1622 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1623 rec->flags |= FTRACE_FL_TRAMP;
1624 ops->nr_trampolines++;
1627 * If we are adding another function callback
1628 * to this function, and the previous had a
1629 * trampoline used, then we need to go back to
1630 * the default trampoline.
1632 rec->flags &= ~FTRACE_FL_TRAMP;
1634 /* remove trampolines from any ops for this rec */
1635 ftrace_clear_tramps(rec);
1639 * If any ops wants regs saved for this function
1640 * then all ops will get saved regs.
1642 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1643 rec->flags |= FTRACE_FL_REGS;
1645 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1649 if (ops->trampoline && !ftrace_rec_count(rec))
1650 ftrace_remove_tramp(ops, rec);
1653 * If the rec had REGS enabled and the ops that is
1654 * being removed had REGS set, then see if there is
1655 * still any ops for this record that wants regs.
1656 * If not, we can stop recording them.
1658 if (ftrace_rec_count(rec) > 0 &&
1659 rec->flags & FTRACE_FL_REGS &&
1660 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1661 if (!test_rec_ops_needs_regs(rec))
1662 rec->flags &= ~FTRACE_FL_REGS;
1666 * flags will be cleared in ftrace_check_record()
1667 * if rec count is zero.
1671 /* Shortcut, if we handled all records, we are done. */
1672 if (!all && count == hash->count)
1674 } while_for_each_ftrace_rec();
1677 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1680 __ftrace_hash_rec_update(ops, filter_hash, 0);
1683 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1686 __ftrace_hash_rec_update(ops, filter_hash, 1);
1689 static void print_ip_ins(const char *fmt, unsigned char *p)
1693 printk(KERN_CONT "%s", fmt);
1695 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1696 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1700 * ftrace_bug - report and shutdown function tracer
1701 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1702 * @ip: The address that failed
1704 * The arch code that enables or disables the function tracing
1705 * can call ftrace_bug() when it has detected a problem in
1706 * modifying the code. @failed should be one of either:
1707 * EFAULT - if the problem happens on reading the @ip address
1708 * EINVAL - if what is read at @ip is not what was expected
1709 * EPERM - if the problem happens on writting to the @ip address
1711 void ftrace_bug(int failed, unsigned long ip)
1715 FTRACE_WARN_ON_ONCE(1);
1716 pr_info("ftrace faulted on modifying ");
1720 FTRACE_WARN_ON_ONCE(1);
1721 pr_info("ftrace failed to modify ");
1723 print_ip_ins(" actual: ", (unsigned char *)ip);
1724 printk(KERN_CONT "\n");
1727 FTRACE_WARN_ON_ONCE(1);
1728 pr_info("ftrace faulted on writing ");
1732 FTRACE_WARN_ON_ONCE(1);
1733 pr_info("ftrace faulted on unknown error ");
1738 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1740 unsigned long flag = 0UL;
1743 * If we are updating calls:
1745 * If the record has a ref count, then we need to enable it
1746 * because someone is using it.
1748 * Otherwise we make sure its disabled.
1750 * If we are disabling calls, then disable all records that
1753 if (enable && ftrace_rec_count(rec))
1754 flag = FTRACE_FL_ENABLED;
1757 * If enabling and the REGS flag does not match the REGS_EN, or
1758 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1759 * this record. Set flags to fail the compare against ENABLED.
1762 if (!(rec->flags & FTRACE_FL_REGS) !=
1763 !(rec->flags & FTRACE_FL_REGS_EN))
1764 flag |= FTRACE_FL_REGS;
1766 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1767 !(rec->flags & FTRACE_FL_TRAMP_EN))
1768 flag |= FTRACE_FL_TRAMP;
1771 /* If the state of this record hasn't changed, then do nothing */
1772 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1773 return FTRACE_UPDATE_IGNORE;
1776 /* Save off if rec is being enabled (for return value) */
1777 flag ^= rec->flags & FTRACE_FL_ENABLED;
1780 rec->flags |= FTRACE_FL_ENABLED;
1781 if (flag & FTRACE_FL_REGS) {
1782 if (rec->flags & FTRACE_FL_REGS)
1783 rec->flags |= FTRACE_FL_REGS_EN;
1785 rec->flags &= ~FTRACE_FL_REGS_EN;
1787 if (flag & FTRACE_FL_TRAMP) {
1788 if (rec->flags & FTRACE_FL_TRAMP)
1789 rec->flags |= FTRACE_FL_TRAMP_EN;
1791 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1796 * If this record is being updated from a nop, then
1797 * return UPDATE_MAKE_CALL.
1799 * return UPDATE_MODIFY_CALL to tell the caller to convert
1800 * from the save regs, to a non-save regs function or
1801 * vice versa, or from a trampoline call.
1803 if (flag & FTRACE_FL_ENABLED)
1804 return FTRACE_UPDATE_MAKE_CALL;
1806 return FTRACE_UPDATE_MODIFY_CALL;
1810 /* If there's no more users, clear all flags */
1811 if (!ftrace_rec_count(rec))
1814 /* Just disable the record (keep REGS state) */
1815 rec->flags &= ~FTRACE_FL_ENABLED;
1818 return FTRACE_UPDATE_MAKE_NOP;
1822 * ftrace_update_record, set a record that now is tracing or not
1823 * @rec: the record to update
1824 * @enable: set to 1 if the record is tracing, zero to force disable
1826 * The records that represent all functions that can be traced need
1827 * to be updated when tracing has been enabled.
1829 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1831 return ftrace_check_record(rec, enable, 1);
1835 * ftrace_test_record, check if the record has been enabled or not
1836 * @rec: the record to test
1837 * @enable: set to 1 to check if enabled, 0 if it is disabled
1839 * The arch code may need to test if a record is already set to
1840 * tracing to determine how to modify the function code that it
1843 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1845 return ftrace_check_record(rec, enable, 0);
1848 static struct ftrace_ops *
1849 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1851 struct ftrace_ops *op;
1853 /* Removed ops need to be tested first */
1854 if (removed_ops && removed_ops->tramp_hash) {
1855 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1859 do_for_each_ftrace_op(op, ftrace_ops_list) {
1860 if (!op->tramp_hash)
1863 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1866 } while_for_each_ftrace_op(op);
1871 static struct ftrace_ops *
1872 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1874 struct ftrace_ops *op;
1876 do_for_each_ftrace_op(op, ftrace_ops_list) {
1877 /* pass rec in as regs to have non-NULL val */
1878 if (ftrace_ops_test(op, rec->ip, rec))
1880 } while_for_each_ftrace_op(op);
1886 * ftrace_get_addr_new - Get the call address to set to
1887 * @rec: The ftrace record descriptor
1889 * If the record has the FTRACE_FL_REGS set, that means that it
1890 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1891 * is not not set, then it wants to convert to the normal callback.
1893 * Returns the address of the trampoline to set to
1895 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1897 struct ftrace_ops *ops;
1899 /* Trampolines take precedence over regs */
1900 if (rec->flags & FTRACE_FL_TRAMP) {
1901 ops = ftrace_find_tramp_ops_new(rec);
1902 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1903 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1904 (void *)rec->ip, (void *)rec->ip);
1905 /* Ftrace is shutting down, return anything */
1906 return (unsigned long)FTRACE_ADDR;
1908 return ops->trampoline;
1911 if (rec->flags & FTRACE_FL_REGS)
1912 return (unsigned long)FTRACE_REGS_ADDR;
1914 return (unsigned long)FTRACE_ADDR;
1918 * ftrace_get_addr_curr - Get the call address that is already there
1919 * @rec: The ftrace record descriptor
1921 * The FTRACE_FL_REGS_EN is set when the record already points to
1922 * a function that saves all the regs. Basically the '_EN' version
1923 * represents the current state of the function.
1925 * Returns the address of the trampoline that is currently being called
1927 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1929 struct ftrace_ops *ops;
1931 /* Trampolines take precedence over regs */
1932 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1933 ops = ftrace_find_tramp_ops_curr(rec);
1934 if (FTRACE_WARN_ON(!ops)) {
1935 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1936 (void *)rec->ip, (void *)rec->ip);
1937 /* Ftrace is shutting down, return anything */
1938 return (unsigned long)FTRACE_ADDR;
1940 return ops->trampoline;
1943 if (rec->flags & FTRACE_FL_REGS_EN)
1944 return (unsigned long)FTRACE_REGS_ADDR;
1946 return (unsigned long)FTRACE_ADDR;
1950 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1952 unsigned long ftrace_old_addr;
1953 unsigned long ftrace_addr;
1956 ftrace_addr = ftrace_get_addr_new(rec);
1958 /* This needs to be done before we call ftrace_update_record */
1959 ftrace_old_addr = ftrace_get_addr_curr(rec);
1961 ret = ftrace_update_record(rec, enable);
1964 case FTRACE_UPDATE_IGNORE:
1967 case FTRACE_UPDATE_MAKE_CALL:
1968 return ftrace_make_call(rec, ftrace_addr);
1970 case FTRACE_UPDATE_MAKE_NOP:
1971 return ftrace_make_nop(NULL, rec, ftrace_addr);
1973 case FTRACE_UPDATE_MODIFY_CALL:
1974 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1977 return -1; /* unknow ftrace bug */
1980 void __weak ftrace_replace_code(int enable)
1982 struct dyn_ftrace *rec;
1983 struct ftrace_page *pg;
1986 if (unlikely(ftrace_disabled))
1989 do_for_each_ftrace_rec(pg, rec) {
1990 failed = __ftrace_replace_code(rec, enable);
1992 ftrace_bug(failed, rec->ip);
1993 /* Stop processing */
1996 } while_for_each_ftrace_rec();
1999 struct ftrace_rec_iter {
2000 struct ftrace_page *pg;
2005 * ftrace_rec_iter_start, start up iterating over traced functions
2007 * Returns an iterator handle that is used to iterate over all
2008 * the records that represent address locations where functions
2011 * May return NULL if no records are available.
2013 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2016 * We only use a single iterator.
2017 * Protected by the ftrace_lock mutex.
2019 static struct ftrace_rec_iter ftrace_rec_iter;
2020 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2022 iter->pg = ftrace_pages_start;
2025 /* Could have empty pages */
2026 while (iter->pg && !iter->pg->index)
2027 iter->pg = iter->pg->next;
2036 * ftrace_rec_iter_next, get the next record to process.
2037 * @iter: The handle to the iterator.
2039 * Returns the next iterator after the given iterator @iter.
2041 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2045 if (iter->index >= iter->pg->index) {
2046 iter->pg = iter->pg->next;
2049 /* Could have empty pages */
2050 while (iter->pg && !iter->pg->index)
2051 iter->pg = iter->pg->next;
2061 * ftrace_rec_iter_record, get the record at the iterator location
2062 * @iter: The current iterator location
2064 * Returns the record that the current @iter is at.
2066 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2068 return &iter->pg->records[iter->index];
2072 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2079 if (unlikely(ftrace_disabled))
2082 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2084 ftrace_bug(ret, ip);
2091 * archs can override this function if they must do something
2092 * before the modifying code is performed.
2094 int __weak ftrace_arch_code_modify_prepare(void)
2100 * archs can override this function if they must do something
2101 * after the modifying code is performed.
2103 int __weak ftrace_arch_code_modify_post_process(void)
2108 void ftrace_modify_all_code(int command)
2110 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2114 * If the ftrace_caller calls a ftrace_ops func directly,
2115 * we need to make sure that it only traces functions it
2116 * expects to trace. When doing the switch of functions,
2117 * we need to update to the ftrace_ops_list_func first
2118 * before the transition between old and new calls are set,
2119 * as the ftrace_ops_list_func will check the ops hashes
2120 * to make sure the ops are having the right functions
2124 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2125 if (FTRACE_WARN_ON(err))
2129 if (command & FTRACE_UPDATE_CALLS)
2130 ftrace_replace_code(1);
2131 else if (command & FTRACE_DISABLE_CALLS)
2132 ftrace_replace_code(0);
2134 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2135 function_trace_op = set_function_trace_op;
2137 /* If irqs are disabled, we are in stop machine */
2138 if (!irqs_disabled())
2139 smp_call_function(ftrace_sync_ipi, NULL, 1);
2140 err = ftrace_update_ftrace_func(ftrace_trace_function);
2141 if (FTRACE_WARN_ON(err))
2145 if (command & FTRACE_START_FUNC_RET)
2146 err = ftrace_enable_ftrace_graph_caller();
2147 else if (command & FTRACE_STOP_FUNC_RET)
2148 err = ftrace_disable_ftrace_graph_caller();
2149 FTRACE_WARN_ON(err);
2152 static int __ftrace_modify_code(void *data)
2154 int *command = data;
2156 ftrace_modify_all_code(*command);
2162 * ftrace_run_stop_machine, go back to the stop machine method
2163 * @command: The command to tell ftrace what to do
2165 * If an arch needs to fall back to the stop machine method, the
2166 * it can call this function.
2168 void ftrace_run_stop_machine(int command)
2170 stop_machine(__ftrace_modify_code, &command, NULL);
2174 * arch_ftrace_update_code, modify the code to trace or not trace
2175 * @command: The command that needs to be done
2177 * Archs can override this function if it does not need to
2178 * run stop_machine() to modify code.
2180 void __weak arch_ftrace_update_code(int command)
2182 ftrace_run_stop_machine(command);
2185 static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2187 struct ftrace_page *pg;
2188 struct dyn_ftrace *rec;
2192 size = ops->nr_trampolines;
2195 * Make the hash size about 1/2 the # found
2197 for (size /= 2; size; size >>= 1)
2200 ops->tramp_hash = alloc_ftrace_hash(bits);
2202 * TODO: a failed allocation is going to screw up
2203 * the accounting of what needs to be modified
2204 * and not. For now, we kill ftrace if we fail
2205 * to allocate here. But there are ways around this,
2206 * but that will take a little more work.
2208 if (!ops->tramp_hash)
2211 do_for_each_ftrace_rec(pg, rec) {
2212 if (ftrace_rec_count(rec) == 1 &&
2213 ftrace_ops_test(ops, rec->ip, rec)) {
2216 * If another ops adds to a rec, the rec will
2217 * lose its trampoline and never get it back
2218 * until all ops are off of it.
2220 if (!(rec->flags & FTRACE_FL_TRAMP))
2223 /* This record had better have a trampoline */
2224 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2227 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2231 } while_for_each_ftrace_rec();
2233 /* The number of recs in the hash must match nr_trampolines */
2234 FTRACE_WARN_ON(ops->tramp_hash->count != ops->nr_trampolines);
2239 static int ftrace_save_tramp_hashes(void)
2241 struct ftrace_ops *op;
2245 * Now that any trampoline is being used, we need to save the
2246 * hashes for the ops that have them. This allows the mapping
2247 * back from the record to the ops that has the trampoline to
2248 * know what code is being replaced. Modifying code must always
2249 * verify what it is changing.
2251 do_for_each_ftrace_op(op, ftrace_ops_list) {
2253 /* The tramp_hash is recreated each time. */
2254 free_ftrace_hash(op->tramp_hash);
2255 op->tramp_hash = NULL;
2257 if (op->nr_trampolines) {
2258 ret = ftrace_save_ops_tramp_hash(op);
2263 } while_for_each_ftrace_op(op);
2268 static void ftrace_run_update_code(int command)
2272 ret = ftrace_arch_code_modify_prepare();
2273 FTRACE_WARN_ON(ret);
2278 * By default we use stop_machine() to modify the code.
2279 * But archs can do what ever they want as long as it
2280 * is safe. The stop_machine() is the safest, but also
2281 * produces the most overhead.
2283 arch_ftrace_update_code(command);
2285 ret = ftrace_arch_code_modify_post_process();
2286 FTRACE_WARN_ON(ret);
2288 ret = ftrace_save_tramp_hashes();
2289 FTRACE_WARN_ON(ret);
2292 static ftrace_func_t saved_ftrace_func;
2293 static int ftrace_start_up;
2295 static void control_ops_free(struct ftrace_ops *ops)
2297 free_percpu(ops->disabled);
2300 static void ftrace_startup_enable(int command)
2302 if (saved_ftrace_func != ftrace_trace_function) {
2303 saved_ftrace_func = ftrace_trace_function;
2304 command |= FTRACE_UPDATE_TRACE_FUNC;
2307 if (!command || !ftrace_enabled)
2310 ftrace_run_update_code(command);
2313 static int ftrace_startup(struct ftrace_ops *ops, int command)
2317 if (unlikely(ftrace_disabled))
2320 ret = __register_ftrace_function(ops);
2325 command |= FTRACE_UPDATE_CALLS;
2327 ops->flags |= FTRACE_OPS_FL_ENABLED;
2329 ftrace_hash_rec_enable(ops, 1);
2331 ftrace_startup_enable(command);
2336 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2340 if (unlikely(ftrace_disabled))
2343 ret = __unregister_ftrace_function(ops);
2349 * Just warn in case of unbalance, no need to kill ftrace, it's not
2350 * critical but the ftrace_call callers may be never nopped again after
2351 * further ftrace uses.
2353 WARN_ON_ONCE(ftrace_start_up < 0);
2355 ftrace_hash_rec_disable(ops, 1);
2357 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2359 command |= FTRACE_UPDATE_CALLS;
2361 if (saved_ftrace_func != ftrace_trace_function) {
2362 saved_ftrace_func = ftrace_trace_function;
2363 command |= FTRACE_UPDATE_TRACE_FUNC;
2366 if (!command || !ftrace_enabled) {
2368 * If these are control ops, they still need their
2369 * per_cpu field freed. Since, function tracing is
2370 * not currently active, we can just free them
2371 * without synchronizing all CPUs.
2373 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2374 control_ops_free(ops);
2379 * If the ops uses a trampoline, then it needs to be
2380 * tested first on update.
2384 ftrace_run_update_code(command);
2389 * Dynamic ops may be freed, we must make sure that all
2390 * callers are done before leaving this function.
2391 * The same goes for freeing the per_cpu data of the control
2394 * Again, normal synchronize_sched() is not good enough.
2395 * We need to do a hard force of sched synchronization.
2396 * This is because we use preempt_disable() to do RCU, but
2397 * the function tracers can be called where RCU is not watching
2398 * (like before user_exit()). We can not rely on the RCU
2399 * infrastructure to do the synchronization, thus we must do it
2402 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2403 schedule_on_each_cpu(ftrace_sync);
2405 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2406 control_ops_free(ops);
2412 static void ftrace_startup_sysctl(void)
2414 if (unlikely(ftrace_disabled))
2417 /* Force update next time */
2418 saved_ftrace_func = NULL;
2419 /* ftrace_start_up is true if we want ftrace running */
2420 if (ftrace_start_up)
2421 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2424 static void ftrace_shutdown_sysctl(void)
2426 if (unlikely(ftrace_disabled))
2429 /* ftrace_start_up is true if ftrace is running */
2430 if (ftrace_start_up)
2431 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2434 static cycle_t ftrace_update_time;
2435 unsigned long ftrace_update_tot_cnt;
2437 static inline int ops_traces_mod(struct ftrace_ops *ops)
2440 * Filter_hash being empty will default to trace module.
2441 * But notrace hash requires a test of individual module functions.
2443 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2444 ftrace_hash_empty(ops->func_hash->notrace_hash);
2448 * Check if the current ops references the record.
2450 * If the ops traces all functions, then it was already accounted for.
2451 * If the ops does not trace the current record function, skip it.
2452 * If the ops ignores the function via notrace filter, skip it.
2455 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2457 /* If ops isn't enabled, ignore it */
2458 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2461 /* If ops traces all mods, we already accounted for it */
2462 if (ops_traces_mod(ops))
2465 /* The function must be in the filter */
2466 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2467 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2470 /* If in notrace hash, we ignore it too */
2471 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2477 static int referenced_filters(struct dyn_ftrace *rec)
2479 struct ftrace_ops *ops;
2482 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2483 if (ops_references_rec(ops, rec))
2490 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2492 struct ftrace_page *pg;
2493 struct dyn_ftrace *p;
2494 cycle_t start, stop;
2495 unsigned long update_cnt = 0;
2496 unsigned long ref = 0;
2501 * When adding a module, we need to check if tracers are
2502 * currently enabled and if they are set to trace all functions.
2503 * If they are, we need to enable the module functions as well
2504 * as update the reference counts for those function records.
2507 struct ftrace_ops *ops;
2509 for (ops = ftrace_ops_list;
2510 ops != &ftrace_list_end; ops = ops->next) {
2511 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2512 if (ops_traces_mod(ops))
2520 start = ftrace_now(raw_smp_processor_id());
2522 for (pg = new_pgs; pg; pg = pg->next) {
2524 for (i = 0; i < pg->index; i++) {
2527 /* If something went wrong, bail without enabling anything */
2528 if (unlikely(ftrace_disabled))
2531 p = &pg->records[i];
2533 cnt += referenced_filters(p);
2537 * Do the initial record conversion from mcount jump
2538 * to the NOP instructions.
2540 if (!ftrace_code_disable(mod, p))
2546 * If the tracing is enabled, go ahead and enable the record.
2548 * The reason not to enable the record immediatelly is the
2549 * inherent check of ftrace_make_nop/ftrace_make_call for
2550 * correct previous instructions. Making first the NOP
2551 * conversion puts the module to the correct state, thus
2552 * passing the ftrace_make_call check.
2554 if (ftrace_start_up && cnt) {
2555 int failed = __ftrace_replace_code(p, 1);
2557 ftrace_bug(failed, p->ip);
2562 stop = ftrace_now(raw_smp_processor_id());
2563 ftrace_update_time = stop - start;
2564 ftrace_update_tot_cnt += update_cnt;
2569 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2574 if (WARN_ON(!count))
2577 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2580 * We want to fill as much as possible. No more than a page
2583 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2587 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2590 /* if we can't allocate this size, try something smaller */
2597 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2606 static struct ftrace_page *
2607 ftrace_allocate_pages(unsigned long num_to_init)
2609 struct ftrace_page *start_pg;
2610 struct ftrace_page *pg;
2617 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2622 * Try to allocate as much as possible in one continues
2623 * location that fills in all of the space. We want to
2624 * waste as little space as possible.
2627 cnt = ftrace_allocate_records(pg, num_to_init);
2635 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2647 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2648 free_pages((unsigned long)pg->records, order);
2649 start_pg = pg->next;
2653 pr_info("ftrace: FAILED to allocate memory for functions\n");
2657 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2659 struct ftrace_iterator {
2662 struct ftrace_page *pg;
2663 struct dyn_ftrace *func;
2664 struct ftrace_func_probe *probe;
2665 struct trace_parser parser;
2666 struct ftrace_hash *hash;
2667 struct ftrace_ops *ops;
2674 t_hash_next(struct seq_file *m, loff_t *pos)
2676 struct ftrace_iterator *iter = m->private;
2677 struct hlist_node *hnd = NULL;
2678 struct hlist_head *hhd;
2684 hnd = &iter->probe->node;
2686 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2689 hhd = &ftrace_func_hash[iter->hidx];
2691 if (hlist_empty(hhd)) {
2707 if (WARN_ON_ONCE(!hnd))
2710 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2715 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2717 struct ftrace_iterator *iter = m->private;
2721 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2724 if (iter->func_pos > *pos)
2728 for (l = 0; l <= (*pos - iter->func_pos); ) {
2729 p = t_hash_next(m, &l);
2736 /* Only set this if we have an item */
2737 iter->flags |= FTRACE_ITER_HASH;
2743 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2745 struct ftrace_func_probe *rec;
2748 if (WARN_ON_ONCE(!rec))
2751 if (rec->ops->print)
2752 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2754 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2757 seq_printf(m, ":%p", rec->data);
2764 t_next(struct seq_file *m, void *v, loff_t *pos)
2766 struct ftrace_iterator *iter = m->private;
2767 struct ftrace_ops *ops = iter->ops;
2768 struct dyn_ftrace *rec = NULL;
2770 if (unlikely(ftrace_disabled))
2773 if (iter->flags & FTRACE_ITER_HASH)
2774 return t_hash_next(m, pos);
2777 iter->pos = iter->func_pos = *pos;
2779 if (iter->flags & FTRACE_ITER_PRINTALL)
2780 return t_hash_start(m, pos);
2783 if (iter->idx >= iter->pg->index) {
2784 if (iter->pg->next) {
2785 iter->pg = iter->pg->next;
2790 rec = &iter->pg->records[iter->idx++];
2791 if (((iter->flags & FTRACE_ITER_FILTER) &&
2792 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
2794 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2795 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
2797 ((iter->flags & FTRACE_ITER_ENABLED) &&
2798 !(rec->flags & FTRACE_FL_ENABLED))) {
2806 return t_hash_start(m, pos);
2813 static void reset_iter_read(struct ftrace_iterator *iter)
2817 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2820 static void *t_start(struct seq_file *m, loff_t *pos)
2822 struct ftrace_iterator *iter = m->private;
2823 struct ftrace_ops *ops = iter->ops;
2827 mutex_lock(&ftrace_lock);
2829 if (unlikely(ftrace_disabled))
2833 * If an lseek was done, then reset and start from beginning.
2835 if (*pos < iter->pos)
2836 reset_iter_read(iter);
2839 * For set_ftrace_filter reading, if we have the filter
2840 * off, we can short cut and just print out that all
2841 * functions are enabled.
2843 if ((iter->flags & FTRACE_ITER_FILTER &&
2844 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
2845 (iter->flags & FTRACE_ITER_NOTRACE &&
2846 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
2848 return t_hash_start(m, pos);
2849 iter->flags |= FTRACE_ITER_PRINTALL;
2850 /* reset in case of seek/pread */
2851 iter->flags &= ~FTRACE_ITER_HASH;
2855 if (iter->flags & FTRACE_ITER_HASH)
2856 return t_hash_start(m, pos);
2859 * Unfortunately, we need to restart at ftrace_pages_start
2860 * every time we let go of the ftrace_mutex. This is because
2861 * those pointers can change without the lock.
2863 iter->pg = ftrace_pages_start;
2865 for (l = 0; l <= *pos; ) {
2866 p = t_next(m, p, &l);
2872 return t_hash_start(m, pos);
2877 static void t_stop(struct seq_file *m, void *p)
2879 mutex_unlock(&ftrace_lock);
2882 static int t_show(struct seq_file *m, void *v)
2884 struct ftrace_iterator *iter = m->private;
2885 struct dyn_ftrace *rec;
2887 if (iter->flags & FTRACE_ITER_HASH)
2888 return t_hash_show(m, iter);
2890 if (iter->flags & FTRACE_ITER_PRINTALL) {
2891 if (iter->flags & FTRACE_ITER_NOTRACE)
2892 seq_printf(m, "#### no functions disabled ####\n");
2894 seq_printf(m, "#### all functions enabled ####\n");
2903 seq_printf(m, "%ps", (void *)rec->ip);
2904 if (iter->flags & FTRACE_ITER_ENABLED) {
2905 seq_printf(m, " (%ld)%s",
2906 ftrace_rec_count(rec),
2907 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2908 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2909 struct ftrace_ops *ops;
2911 ops = ftrace_find_tramp_ops_curr(rec);
2912 if (ops && ops->trampoline)
2913 seq_printf(m, "\ttramp: %pS",
2914 (void *)ops->trampoline);
2916 seq_printf(m, "\ttramp: ERROR!");
2920 seq_printf(m, "\n");
2925 static const struct seq_operations show_ftrace_seq_ops = {
2933 ftrace_avail_open(struct inode *inode, struct file *file)
2935 struct ftrace_iterator *iter;
2937 if (unlikely(ftrace_disabled))
2940 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2942 iter->pg = ftrace_pages_start;
2943 iter->ops = &global_ops;
2946 return iter ? 0 : -ENOMEM;
2950 ftrace_enabled_open(struct inode *inode, struct file *file)
2952 struct ftrace_iterator *iter;
2954 if (unlikely(ftrace_disabled))
2957 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2959 iter->pg = ftrace_pages_start;
2960 iter->flags = FTRACE_ITER_ENABLED;
2961 iter->ops = &global_ops;
2964 return iter ? 0 : -ENOMEM;
2968 * ftrace_regex_open - initialize function tracer filter files
2969 * @ops: The ftrace_ops that hold the hash filters
2970 * @flag: The type of filter to process
2971 * @inode: The inode, usually passed in to your open routine
2972 * @file: The file, usually passed in to your open routine
2974 * ftrace_regex_open() initializes the filter files for the
2975 * @ops. Depending on @flag it may process the filter hash or
2976 * the notrace hash of @ops. With this called from the open
2977 * routine, you can use ftrace_filter_write() for the write
2978 * routine if @flag has FTRACE_ITER_FILTER set, or
2979 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2980 * tracing_lseek() should be used as the lseek routine, and
2981 * release must call ftrace_regex_release().
2984 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2985 struct inode *inode, struct file *file)
2987 struct ftrace_iterator *iter;
2988 struct ftrace_hash *hash;
2991 ftrace_ops_init(ops);
2993 if (unlikely(ftrace_disabled))
2996 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3000 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3008 mutex_lock(&ops->func_hash->regex_lock);
3010 if (flag & FTRACE_ITER_NOTRACE)
3011 hash = ops->func_hash->notrace_hash;
3013 hash = ops->func_hash->filter_hash;
3015 if (file->f_mode & FMODE_WRITE) {
3016 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3018 if (file->f_flags & O_TRUNC)
3019 iter->hash = alloc_ftrace_hash(size_bits);
3021 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3024 trace_parser_put(&iter->parser);
3031 if (file->f_mode & FMODE_READ) {
3032 iter->pg = ftrace_pages_start;
3034 ret = seq_open(file, &show_ftrace_seq_ops);
3036 struct seq_file *m = file->private_data;
3040 free_ftrace_hash(iter->hash);
3041 trace_parser_put(&iter->parser);
3045 file->private_data = iter;
3048 mutex_unlock(&ops->func_hash->regex_lock);
3054 ftrace_filter_open(struct inode *inode, struct file *file)
3056 struct ftrace_ops *ops = inode->i_private;
3058 return ftrace_regex_open(ops,
3059 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3064 ftrace_notrace_open(struct inode *inode, struct file *file)
3066 struct ftrace_ops *ops = inode->i_private;
3068 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3072 static int ftrace_match(char *str, char *regex, int len, int type)
3079 if (strcmp(str, regex) == 0)
3082 case MATCH_FRONT_ONLY:
3083 if (strncmp(str, regex, len) == 0)
3086 case MATCH_MIDDLE_ONLY:
3087 if (strstr(str, regex))
3090 case MATCH_END_ONLY:
3092 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
3101 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
3103 struct ftrace_func_entry *entry;
3106 entry = ftrace_lookup_ip(hash, rec->ip);
3108 /* Do nothing if it doesn't exist */
3112 free_hash_entry(hash, entry);
3114 /* Do nothing if it exists */
3118 ret = add_hash_entry(hash, rec->ip);
3124 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3125 char *regex, int len, int type)
3127 char str[KSYM_SYMBOL_LEN];
3130 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3133 /* module lookup requires matching the module */
3134 if (!modname || strcmp(modname, mod))
3137 /* blank search means to match all funcs in the mod */
3142 return ftrace_match(str, regex, len, type);
3146 match_records(struct ftrace_hash *hash, char *buff,
3147 int len, char *mod, int not)
3149 unsigned search_len = 0;
3150 struct ftrace_page *pg;
3151 struct dyn_ftrace *rec;
3152 int type = MATCH_FULL;
3153 char *search = buff;
3158 type = filter_parse_regex(buff, len, &search, ¬);
3159 search_len = strlen(search);
3162 mutex_lock(&ftrace_lock);
3164 if (unlikely(ftrace_disabled))
3167 do_for_each_ftrace_rec(pg, rec) {
3168 if (ftrace_match_record(rec, mod, search, search_len, type)) {
3169 ret = enter_record(hash, rec, not);
3176 } while_for_each_ftrace_rec();
3178 mutex_unlock(&ftrace_lock);
3184 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3186 return match_records(hash, buff, len, NULL, 0);
3190 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
3194 /* blank or '*' mean the same */
3195 if (strcmp(buff, "*") == 0)
3198 /* handle the case of 'dont filter this module' */
3199 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3204 return match_records(hash, buff, strlen(buff), mod, not);
3208 * We register the module command as a template to show others how
3209 * to register the a command as well.
3213 ftrace_mod_callback(struct ftrace_hash *hash,
3214 char *func, char *cmd, char *param, int enable)
3220 * cmd == 'mod' because we only registered this func
3221 * for the 'mod' ftrace_func_command.
3222 * But if you register one func with multiple commands,
3223 * you can tell which command was used by the cmd
3227 /* we must have a module name */
3231 mod = strsep(¶m, ":");
3235 ret = ftrace_match_module_records(hash, func, mod);
3244 static struct ftrace_func_command ftrace_mod_cmd = {
3246 .func = ftrace_mod_callback,
3249 static int __init ftrace_mod_cmd_init(void)
3251 return register_ftrace_command(&ftrace_mod_cmd);
3253 core_initcall(ftrace_mod_cmd_init);
3255 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3256 struct ftrace_ops *op, struct pt_regs *pt_regs)
3258 struct ftrace_func_probe *entry;
3259 struct hlist_head *hhd;
3262 key = hash_long(ip, FTRACE_HASH_BITS);
3264 hhd = &ftrace_func_hash[key];
3266 if (hlist_empty(hhd))
3270 * Disable preemption for these calls to prevent a RCU grace
3271 * period. This syncs the hash iteration and freeing of items
3272 * on the hash. rcu_read_lock is too dangerous here.
3274 preempt_disable_notrace();
3275 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3276 if (entry->ip == ip)
3277 entry->ops->func(ip, parent_ip, &entry->data);
3279 preempt_enable_notrace();
3282 static struct ftrace_ops trace_probe_ops __read_mostly =
3284 .func = function_trace_probe_call,
3285 .flags = FTRACE_OPS_FL_INITIALIZED,
3286 INIT_OPS_HASH(trace_probe_ops)
3289 static int ftrace_probe_registered;
3291 static void __enable_ftrace_function_probe(void)
3296 if (ftrace_probe_registered) {
3297 /* still need to update the function call sites */
3299 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3303 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3304 struct hlist_head *hhd = &ftrace_func_hash[i];
3308 /* Nothing registered? */
3309 if (i == FTRACE_FUNC_HASHSIZE)
3312 ret = ftrace_startup(&trace_probe_ops, 0);
3314 ftrace_probe_registered = 1;
3317 static void __disable_ftrace_function_probe(void)
3321 if (!ftrace_probe_registered)
3324 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3325 struct hlist_head *hhd = &ftrace_func_hash[i];
3330 /* no more funcs left */
3331 ftrace_shutdown(&trace_probe_ops, 0);
3333 ftrace_probe_registered = 0;
3337 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3339 if (entry->ops->free)
3340 entry->ops->free(entry->ops, entry->ip, &entry->data);
3345 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3348 struct ftrace_func_probe *entry;
3349 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3350 struct ftrace_hash *hash;
3351 struct ftrace_page *pg;
3352 struct dyn_ftrace *rec;
3359 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3360 len = strlen(search);
3362 /* we do not support '!' for function probes */
3366 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3368 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3374 if (unlikely(ftrace_disabled)) {
3379 mutex_lock(&ftrace_lock);
3381 do_for_each_ftrace_rec(pg, rec) {
3383 if (!ftrace_match_record(rec, NULL, search, len, type))
3386 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3388 /* If we did not process any, then return error */
3399 * The caller might want to do something special
3400 * for each function we find. We call the callback
3401 * to give the caller an opportunity to do so.
3404 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3405 /* caller does not like this func */
3411 ret = enter_record(hash, rec, 0);
3419 entry->ip = rec->ip;
3421 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3422 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3424 } while_for_each_ftrace_rec();
3426 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3430 __enable_ftrace_function_probe();
3433 mutex_unlock(&ftrace_lock);
3435 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3436 free_ftrace_hash(hash);
3442 PROBE_TEST_FUNC = 1,
3447 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3448 void *data, int flags)
3450 struct ftrace_func_entry *rec_entry;
3451 struct ftrace_func_probe *entry;
3452 struct ftrace_func_probe *p;
3453 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3454 struct list_head free_list;
3455 struct ftrace_hash *hash;
3456 struct hlist_node *tmp;
3457 char str[KSYM_SYMBOL_LEN];
3458 int type = MATCH_FULL;
3462 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3467 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3468 len = strlen(search);
3470 /* we do not support '!' for function probes */
3475 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3477 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3479 /* Hmm, should report this somehow */
3482 INIT_LIST_HEAD(&free_list);
3484 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3485 struct hlist_head *hhd = &ftrace_func_hash[i];
3487 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3489 /* break up if statements for readability */
3490 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3493 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3496 /* do this last, since it is the most expensive */
3498 kallsyms_lookup(entry->ip, NULL, NULL,
3500 if (!ftrace_match(str, glob, len, type))
3504 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3505 /* It is possible more than one entry had this ip */
3507 free_hash_entry(hash, rec_entry);
3509 hlist_del_rcu(&entry->node);
3510 list_add(&entry->free_list, &free_list);
3513 mutex_lock(&ftrace_lock);
3514 __disable_ftrace_function_probe();
3516 * Remove after the disable is called. Otherwise, if the last
3517 * probe is removed, a null hash means *all enabled*.
3519 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3520 synchronize_sched();
3521 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3522 list_del(&entry->free_list);
3523 ftrace_free_entry(entry);
3525 mutex_unlock(&ftrace_lock);
3528 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3529 free_ftrace_hash(hash);
3533 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3536 __unregister_ftrace_function_probe(glob, ops, data,
3537 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3541 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3543 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3546 void unregister_ftrace_function_probe_all(char *glob)
3548 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3551 static LIST_HEAD(ftrace_commands);
3552 static DEFINE_MUTEX(ftrace_cmd_mutex);
3555 * Currently we only register ftrace commands from __init, so mark this
3558 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3560 struct ftrace_func_command *p;
3563 mutex_lock(&ftrace_cmd_mutex);
3564 list_for_each_entry(p, &ftrace_commands, list) {
3565 if (strcmp(cmd->name, p->name) == 0) {
3570 list_add(&cmd->list, &ftrace_commands);
3572 mutex_unlock(&ftrace_cmd_mutex);
3578 * Currently we only unregister ftrace commands from __init, so mark
3581 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3583 struct ftrace_func_command *p, *n;
3586 mutex_lock(&ftrace_cmd_mutex);
3587 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3588 if (strcmp(cmd->name, p->name) == 0) {
3590 list_del_init(&p->list);
3595 mutex_unlock(&ftrace_cmd_mutex);
3600 static int ftrace_process_regex(struct ftrace_hash *hash,
3601 char *buff, int len, int enable)
3603 char *func, *command, *next = buff;
3604 struct ftrace_func_command *p;
3607 func = strsep(&next, ":");
3610 ret = ftrace_match_records(hash, func, len);
3620 command = strsep(&next, ":");
3622 mutex_lock(&ftrace_cmd_mutex);
3623 list_for_each_entry(p, &ftrace_commands, list) {
3624 if (strcmp(p->name, command) == 0) {
3625 ret = p->func(hash, func, command, next, enable);
3630 mutex_unlock(&ftrace_cmd_mutex);
3636 ftrace_regex_write(struct file *file, const char __user *ubuf,
3637 size_t cnt, loff_t *ppos, int enable)
3639 struct ftrace_iterator *iter;
3640 struct trace_parser *parser;
3646 if (file->f_mode & FMODE_READ) {
3647 struct seq_file *m = file->private_data;
3650 iter = file->private_data;
3652 if (unlikely(ftrace_disabled))
3655 /* iter->hash is a local copy, so we don't need regex_lock */
3657 parser = &iter->parser;
3658 read = trace_get_user(parser, ubuf, cnt, ppos);
3660 if (read >= 0 && trace_parser_loaded(parser) &&
3661 !trace_parser_cont(parser)) {
3662 ret = ftrace_process_regex(iter->hash, parser->buffer,
3663 parser->idx, enable);
3664 trace_parser_clear(parser);
3675 ftrace_filter_write(struct file *file, const char __user *ubuf,
3676 size_t cnt, loff_t *ppos)
3678 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3682 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3683 size_t cnt, loff_t *ppos)
3685 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3689 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3691 struct ftrace_func_entry *entry;
3693 if (!ftrace_location(ip))
3697 entry = ftrace_lookup_ip(hash, ip);
3700 free_hash_entry(hash, entry);
3704 return add_hash_entry(hash, ip);
3707 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3709 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3710 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3714 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3715 unsigned long ip, int remove, int reset, int enable)
3717 struct ftrace_hash **orig_hash;
3718 struct ftrace_hash *hash;
3721 if (unlikely(ftrace_disabled))
3724 mutex_lock(&ops->func_hash->regex_lock);
3727 orig_hash = &ops->func_hash->filter_hash;
3729 orig_hash = &ops->func_hash->notrace_hash;
3732 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3734 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3738 goto out_regex_unlock;
3741 if (buf && !ftrace_match_records(hash, buf, len)) {
3743 goto out_regex_unlock;
3746 ret = ftrace_match_addr(hash, ip, remove);
3748 goto out_regex_unlock;
3751 mutex_lock(&ftrace_lock);
3752 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3754 ftrace_ops_update_code(ops);
3756 mutex_unlock(&ftrace_lock);
3759 mutex_unlock(&ops->func_hash->regex_lock);
3761 free_ftrace_hash(hash);
3766 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3767 int reset, int enable)
3769 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3773 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3774 * @ops - the ops to set the filter with
3775 * @ip - the address to add to or remove from the filter.
3776 * @remove - non zero to remove the ip from the filter
3777 * @reset - non zero to reset all filters before applying this filter.
3779 * Filters denote which functions should be enabled when tracing is enabled
3780 * If @ip is NULL, it failes to update filter.
3782 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3783 int remove, int reset)
3785 ftrace_ops_init(ops);
3786 return ftrace_set_addr(ops, ip, remove, reset, 1);
3788 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3791 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3792 int reset, int enable)
3794 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3798 * ftrace_set_filter - set a function to filter on in ftrace
3799 * @ops - the ops to set the filter with
3800 * @buf - the string that holds the function filter text.
3801 * @len - the length of the string.
3802 * @reset - non zero to reset all filters before applying this filter.
3804 * Filters denote which functions should be enabled when tracing is enabled.
3805 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3807 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3810 ftrace_ops_init(ops);
3811 return ftrace_set_regex(ops, buf, len, reset, 1);
3813 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3816 * ftrace_set_notrace - set a function to not trace in ftrace
3817 * @ops - the ops to set the notrace filter with
3818 * @buf - the string that holds the function notrace text.
3819 * @len - the length of the string.
3820 * @reset - non zero to reset all filters before applying this filter.
3822 * Notrace Filters denote which functions should not be enabled when tracing
3823 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3826 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3829 ftrace_ops_init(ops);
3830 return ftrace_set_regex(ops, buf, len, reset, 0);
3832 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3834 * ftrace_set_global_filter - set a function to filter on with global tracers
3835 * @buf - the string that holds the function filter text.
3836 * @len - the length of the string.
3837 * @reset - non zero to reset all filters before applying this filter.
3839 * Filters denote which functions should be enabled when tracing is enabled.
3840 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3842 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3844 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3846 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3849 * ftrace_set_global_notrace - set a function to not trace with global tracers
3850 * @buf - the string that holds the function notrace text.
3851 * @len - the length of the string.
3852 * @reset - non zero to reset all filters before applying this filter.
3854 * Notrace Filters denote which functions should not be enabled when tracing
3855 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3858 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3860 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3862 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3865 * command line interface to allow users to set filters on boot up.
3867 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3868 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3869 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3871 /* Used by function selftest to not test if filter is set */
3872 bool ftrace_filter_param __initdata;
3874 static int __init set_ftrace_notrace(char *str)
3876 ftrace_filter_param = true;
3877 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3880 __setup("ftrace_notrace=", set_ftrace_notrace);
3882 static int __init set_ftrace_filter(char *str)
3884 ftrace_filter_param = true;
3885 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3888 __setup("ftrace_filter=", set_ftrace_filter);
3890 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3891 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3892 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3893 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3895 static int __init set_graph_function(char *str)
3897 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3900 __setup("ftrace_graph_filter=", set_graph_function);
3902 static int __init set_graph_notrace_function(char *str)
3904 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
3907 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
3909 static void __init set_ftrace_early_graph(char *buf, int enable)
3913 unsigned long *table = ftrace_graph_funcs;
3914 int *count = &ftrace_graph_count;
3917 table = ftrace_graph_notrace_funcs;
3918 count = &ftrace_graph_notrace_count;
3922 func = strsep(&buf, ",");
3923 /* we allow only one expression at a time */
3924 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
3926 printk(KERN_DEBUG "ftrace: function %s not "
3927 "traceable\n", func);
3930 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3933 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3937 ftrace_ops_init(ops);
3940 func = strsep(&buf, ",");
3941 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3945 static void __init set_ftrace_early_filters(void)
3947 if (ftrace_filter_buf[0])
3948 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3949 if (ftrace_notrace_buf[0])
3950 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3951 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3952 if (ftrace_graph_buf[0])
3953 set_ftrace_early_graph(ftrace_graph_buf, 1);
3954 if (ftrace_graph_notrace_buf[0])
3955 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
3956 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3959 int ftrace_regex_release(struct inode *inode, struct file *file)
3961 struct seq_file *m = (struct seq_file *)file->private_data;
3962 struct ftrace_iterator *iter;
3963 struct ftrace_hash **orig_hash;
3964 struct trace_parser *parser;
3968 if (file->f_mode & FMODE_READ) {
3970 seq_release(inode, file);
3972 iter = file->private_data;
3974 parser = &iter->parser;
3975 if (trace_parser_loaded(parser)) {
3976 parser->buffer[parser->idx] = 0;
3977 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3980 trace_parser_put(parser);
3982 mutex_lock(&iter->ops->func_hash->regex_lock);
3984 if (file->f_mode & FMODE_WRITE) {
3985 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3988 orig_hash = &iter->ops->func_hash->filter_hash;
3990 orig_hash = &iter->ops->func_hash->notrace_hash;
3992 mutex_lock(&ftrace_lock);
3993 ret = ftrace_hash_move(iter->ops, filter_hash,
3994 orig_hash, iter->hash);
3996 ftrace_ops_update_code(iter->ops);
3998 mutex_unlock(&ftrace_lock);
4001 mutex_unlock(&iter->ops->func_hash->regex_lock);
4002 free_ftrace_hash(iter->hash);
4008 static const struct file_operations ftrace_avail_fops = {
4009 .open = ftrace_avail_open,
4011 .llseek = seq_lseek,
4012 .release = seq_release_private,
4015 static const struct file_operations ftrace_enabled_fops = {
4016 .open = ftrace_enabled_open,
4018 .llseek = seq_lseek,
4019 .release = seq_release_private,
4022 static const struct file_operations ftrace_filter_fops = {
4023 .open = ftrace_filter_open,
4025 .write = ftrace_filter_write,
4026 .llseek = tracing_lseek,
4027 .release = ftrace_regex_release,
4030 static const struct file_operations ftrace_notrace_fops = {
4031 .open = ftrace_notrace_open,
4033 .write = ftrace_notrace_write,
4034 .llseek = tracing_lseek,
4035 .release = ftrace_regex_release,
4038 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4040 static DEFINE_MUTEX(graph_lock);
4042 int ftrace_graph_count;
4043 int ftrace_graph_notrace_count;
4044 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4045 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4047 struct ftrace_graph_data {
4048 unsigned long *table;
4051 const struct seq_operations *seq_ops;
4055 __g_next(struct seq_file *m, loff_t *pos)
4057 struct ftrace_graph_data *fgd = m->private;
4059 if (*pos >= *fgd->count)
4061 return &fgd->table[*pos];
4065 g_next(struct seq_file *m, void *v, loff_t *pos)
4068 return __g_next(m, pos);
4071 static void *g_start(struct seq_file *m, loff_t *pos)
4073 struct ftrace_graph_data *fgd = m->private;
4075 mutex_lock(&graph_lock);
4077 /* Nothing, tell g_show to print all functions are enabled */
4078 if (!*fgd->count && !*pos)
4081 return __g_next(m, pos);
4084 static void g_stop(struct seq_file *m, void *p)
4086 mutex_unlock(&graph_lock);
4089 static int g_show(struct seq_file *m, void *v)
4091 unsigned long *ptr = v;
4096 if (ptr == (unsigned long *)1) {
4097 struct ftrace_graph_data *fgd = m->private;
4099 if (fgd->table == ftrace_graph_funcs)
4100 seq_printf(m, "#### all functions enabled ####\n");
4102 seq_printf(m, "#### no functions disabled ####\n");
4106 seq_printf(m, "%ps\n", (void *)*ptr);
4111 static const struct seq_operations ftrace_graph_seq_ops = {
4119 __ftrace_graph_open(struct inode *inode, struct file *file,
4120 struct ftrace_graph_data *fgd)
4124 mutex_lock(&graph_lock);
4125 if ((file->f_mode & FMODE_WRITE) &&
4126 (file->f_flags & O_TRUNC)) {
4128 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4130 mutex_unlock(&graph_lock);
4132 if (file->f_mode & FMODE_READ) {
4133 ret = seq_open(file, fgd->seq_ops);
4135 struct seq_file *m = file->private_data;
4139 file->private_data = fgd;
4145 ftrace_graph_open(struct inode *inode, struct file *file)
4147 struct ftrace_graph_data *fgd;
4149 if (unlikely(ftrace_disabled))
4152 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4156 fgd->table = ftrace_graph_funcs;
4157 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4158 fgd->count = &ftrace_graph_count;
4159 fgd->seq_ops = &ftrace_graph_seq_ops;
4161 return __ftrace_graph_open(inode, file, fgd);
4165 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4167 struct ftrace_graph_data *fgd;
4169 if (unlikely(ftrace_disabled))
4172 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4176 fgd->table = ftrace_graph_notrace_funcs;
4177 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4178 fgd->count = &ftrace_graph_notrace_count;
4179 fgd->seq_ops = &ftrace_graph_seq_ops;
4181 return __ftrace_graph_open(inode, file, fgd);
4185 ftrace_graph_release(struct inode *inode, struct file *file)
4187 if (file->f_mode & FMODE_READ) {
4188 struct seq_file *m = file->private_data;
4191 seq_release(inode, file);
4193 kfree(file->private_data);
4200 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4202 struct dyn_ftrace *rec;
4203 struct ftrace_page *pg;
4212 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
4213 if (!not && *idx >= size)
4216 search_len = strlen(search);
4218 mutex_lock(&ftrace_lock);
4220 if (unlikely(ftrace_disabled)) {
4221 mutex_unlock(&ftrace_lock);
4225 do_for_each_ftrace_rec(pg, rec) {
4227 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4228 /* if it is in the array */
4230 for (i = 0; i < *idx; i++) {
4231 if (array[i] == rec->ip) {
4240 array[(*idx)++] = rec->ip;
4246 array[i] = array[--(*idx)];
4252 } while_for_each_ftrace_rec();
4254 mutex_unlock(&ftrace_lock);
4263 ftrace_graph_write(struct file *file, const char __user *ubuf,
4264 size_t cnt, loff_t *ppos)
4266 struct trace_parser parser;
4267 ssize_t read, ret = 0;
4268 struct ftrace_graph_data *fgd = file->private_data;
4273 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4276 read = trace_get_user(&parser, ubuf, cnt, ppos);
4278 if (read >= 0 && trace_parser_loaded((&parser))) {
4279 parser.buffer[parser.idx] = 0;
4281 mutex_lock(&graph_lock);
4283 /* we allow only one expression at a time */
4284 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4287 mutex_unlock(&graph_lock);
4293 trace_parser_put(&parser);
4298 static const struct file_operations ftrace_graph_fops = {
4299 .open = ftrace_graph_open,
4301 .write = ftrace_graph_write,
4302 .llseek = tracing_lseek,
4303 .release = ftrace_graph_release,
4306 static const struct file_operations ftrace_graph_notrace_fops = {
4307 .open = ftrace_graph_notrace_open,
4309 .write = ftrace_graph_write,
4310 .llseek = tracing_lseek,
4311 .release = ftrace_graph_release,
4313 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4315 void ftrace_create_filter_files(struct ftrace_ops *ops,
4316 struct dentry *parent)
4319 trace_create_file("set_ftrace_filter", 0644, parent,
4320 ops, &ftrace_filter_fops);
4322 trace_create_file("set_ftrace_notrace", 0644, parent,
4323 ops, &ftrace_notrace_fops);
4327 * The name "destroy_filter_files" is really a misnomer. Although
4328 * in the future, it may actualy delete the files, but this is
4329 * really intended to make sure the ops passed in are disabled
4330 * and that when this function returns, the caller is free to
4333 * The "destroy" name is only to match the "create" name that this
4334 * should be paired with.
4336 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4338 mutex_lock(&ftrace_lock);
4339 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4340 ftrace_shutdown(ops, 0);
4341 ops->flags |= FTRACE_OPS_FL_DELETED;
4342 mutex_unlock(&ftrace_lock);
4345 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4348 trace_create_file("available_filter_functions", 0444,
4349 d_tracer, NULL, &ftrace_avail_fops);
4351 trace_create_file("enabled_functions", 0444,
4352 d_tracer, NULL, &ftrace_enabled_fops);
4354 ftrace_create_filter_files(&global_ops, d_tracer);
4356 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4357 trace_create_file("set_graph_function", 0444, d_tracer,
4359 &ftrace_graph_fops);
4360 trace_create_file("set_graph_notrace", 0444, d_tracer,
4362 &ftrace_graph_notrace_fops);
4363 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4368 static int ftrace_cmp_ips(const void *a, const void *b)
4370 const unsigned long *ipa = a;
4371 const unsigned long *ipb = b;
4380 static void ftrace_swap_ips(void *a, void *b, int size)
4382 unsigned long *ipa = a;
4383 unsigned long *ipb = b;
4391 static int ftrace_process_locs(struct module *mod,
4392 unsigned long *start,
4395 struct ftrace_page *start_pg;
4396 struct ftrace_page *pg;
4397 struct dyn_ftrace *rec;
4398 unsigned long count;
4401 unsigned long flags = 0; /* Shut up gcc */
4404 count = end - start;
4409 sort(start, count, sizeof(*start),
4410 ftrace_cmp_ips, ftrace_swap_ips);
4412 start_pg = ftrace_allocate_pages(count);
4416 mutex_lock(&ftrace_lock);
4419 * Core and each module needs their own pages, as
4420 * modules will free them when they are removed.
4421 * Force a new page to be allocated for modules.
4424 WARN_ON(ftrace_pages || ftrace_pages_start);
4425 /* First initialization */
4426 ftrace_pages = ftrace_pages_start = start_pg;
4431 if (WARN_ON(ftrace_pages->next)) {
4432 /* Hmm, we have free pages? */
4433 while (ftrace_pages->next)
4434 ftrace_pages = ftrace_pages->next;
4437 ftrace_pages->next = start_pg;
4443 addr = ftrace_call_adjust(*p++);
4445 * Some architecture linkers will pad between
4446 * the different mcount_loc sections of different
4447 * object files to satisfy alignments.
4448 * Skip any NULL pointers.
4453 if (pg->index == pg->size) {
4454 /* We should have allocated enough */
4455 if (WARN_ON(!pg->next))
4460 rec = &pg->records[pg->index++];
4464 /* We should have used all pages */
4467 /* Assign the last page to ftrace_pages */
4471 * We only need to disable interrupts on start up
4472 * because we are modifying code that an interrupt
4473 * may execute, and the modification is not atomic.
4474 * But for modules, nothing runs the code we modify
4475 * until we are finished with it, and there's no
4476 * reason to cause large interrupt latencies while we do it.
4479 local_irq_save(flags);
4480 ftrace_update_code(mod, start_pg);
4482 local_irq_restore(flags);
4485 mutex_unlock(&ftrace_lock);
4490 #ifdef CONFIG_MODULES
4492 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4494 void ftrace_release_mod(struct module *mod)
4496 struct dyn_ftrace *rec;
4497 struct ftrace_page **last_pg;
4498 struct ftrace_page *pg;
4501 mutex_lock(&ftrace_lock);
4503 if (ftrace_disabled)
4507 * Each module has its own ftrace_pages, remove
4508 * them from the list.
4510 last_pg = &ftrace_pages_start;
4511 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4512 rec = &pg->records[0];
4513 if (within_module_core(rec->ip, mod)) {
4515 * As core pages are first, the first
4516 * page should never be a module page.
4518 if (WARN_ON(pg == ftrace_pages_start))
4521 /* Check if we are deleting the last page */
4522 if (pg == ftrace_pages)
4523 ftrace_pages = next_to_ftrace_page(last_pg);
4525 *last_pg = pg->next;
4526 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4527 free_pages((unsigned long)pg->records, order);
4530 last_pg = &pg->next;
4533 mutex_unlock(&ftrace_lock);
4536 static void ftrace_init_module(struct module *mod,
4537 unsigned long *start, unsigned long *end)
4539 if (ftrace_disabled || start == end)
4541 ftrace_process_locs(mod, start, end);
4544 void ftrace_module_init(struct module *mod)
4546 ftrace_init_module(mod, mod->ftrace_callsites,
4547 mod->ftrace_callsites +
4548 mod->num_ftrace_callsites);
4551 static int ftrace_module_notify_exit(struct notifier_block *self,
4552 unsigned long val, void *data)
4554 struct module *mod = data;
4556 if (val == MODULE_STATE_GOING)
4557 ftrace_release_mod(mod);
4562 static int ftrace_module_notify_exit(struct notifier_block *self,
4563 unsigned long val, void *data)
4567 #endif /* CONFIG_MODULES */
4569 struct notifier_block ftrace_module_exit_nb = {
4570 .notifier_call = ftrace_module_notify_exit,
4571 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4574 void __init ftrace_init(void)
4576 extern unsigned long __start_mcount_loc[];
4577 extern unsigned long __stop_mcount_loc[];
4578 unsigned long count, flags;
4581 local_irq_save(flags);
4582 ret = ftrace_dyn_arch_init();
4583 local_irq_restore(flags);
4587 count = __stop_mcount_loc - __start_mcount_loc;
4589 pr_info("ftrace: No functions to be traced?\n");
4593 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4594 count, count / ENTRIES_PER_PAGE + 1);
4596 last_ftrace_enabled = ftrace_enabled = 1;
4598 ret = ftrace_process_locs(NULL,
4602 ret = register_module_notifier(&ftrace_module_exit_nb);
4604 pr_warning("Failed to register trace ftrace module exit notifier\n");
4606 set_ftrace_early_filters();
4610 ftrace_disabled = 1;
4615 static struct ftrace_ops global_ops = {
4616 .func = ftrace_stub,
4617 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4618 INIT_OPS_HASH(global_ops)
4621 static int __init ftrace_nodyn_init(void)
4626 core_initcall(ftrace_nodyn_init);
4628 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4629 static inline void ftrace_startup_enable(int command) { }
4630 /* Keep as macros so we do not need to define the commands */
4631 # define ftrace_startup(ops, command) \
4633 int ___ret = __register_ftrace_function(ops); \
4635 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4638 # define ftrace_shutdown(ops, command) \
4640 int ___ret = __unregister_ftrace_function(ops); \
4642 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4646 # define ftrace_startup_sysctl() do { } while (0)
4647 # define ftrace_shutdown_sysctl() do { } while (0)
4650 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4655 #endif /* CONFIG_DYNAMIC_FTRACE */
4657 __init void ftrace_init_global_array_ops(struct trace_array *tr)
4659 tr->ops = &global_ops;
4660 tr->ops->private = tr;
4663 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4665 /* If we filter on pids, update to use the pid function */
4666 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4667 if (WARN_ON(tr->ops->func != ftrace_stub))
4668 printk("ftrace ops had %pS for function\n",
4670 /* Only the top level instance does pid tracing */
4671 if (!list_empty(&ftrace_pids)) {
4672 set_ftrace_pid_function(func);
4673 func = ftrace_pid_func;
4676 tr->ops->func = func;
4677 tr->ops->private = tr;
4680 void ftrace_reset_array_ops(struct trace_array *tr)
4682 tr->ops->func = ftrace_stub;
4686 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4687 struct ftrace_ops *op, struct pt_regs *regs)
4689 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4693 * Some of the ops may be dynamically allocated,
4694 * they must be freed after a synchronize_sched().
4696 preempt_disable_notrace();
4697 trace_recursion_set(TRACE_CONTROL_BIT);
4700 * Control funcs (perf) uses RCU. Only trace if
4701 * RCU is currently active.
4703 if (!rcu_is_watching())
4706 do_for_each_ftrace_op(op, ftrace_control_list) {
4707 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4708 !ftrace_function_local_disabled(op) &&
4709 ftrace_ops_test(op, ip, regs))
4710 op->func(ip, parent_ip, op, regs);
4711 } while_for_each_ftrace_op(op);
4713 trace_recursion_clear(TRACE_CONTROL_BIT);
4714 preempt_enable_notrace();
4717 static struct ftrace_ops control_ops = {
4718 .func = ftrace_ops_control_func,
4719 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4720 INIT_OPS_HASH(control_ops)
4724 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4725 struct ftrace_ops *ignored, struct pt_regs *regs)
4727 struct ftrace_ops *op;
4730 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4735 * Some of the ops may be dynamically allocated,
4736 * they must be freed after a synchronize_sched().
4738 preempt_disable_notrace();
4739 do_for_each_ftrace_op(op, ftrace_ops_list) {
4740 if (ftrace_ops_test(op, ip, regs)) {
4741 if (FTRACE_WARN_ON(!op->func)) {
4742 pr_warn("op=%p %pS\n", op, op);
4745 op->func(ip, parent_ip, op, regs);
4747 } while_for_each_ftrace_op(op);
4749 preempt_enable_notrace();
4750 trace_clear_recursion(bit);
4754 * Some archs only support passing ip and parent_ip. Even though
4755 * the list function ignores the op parameter, we do not want any
4756 * C side effects, where a function is called without the caller
4757 * sending a third parameter.
4758 * Archs are to support both the regs and ftrace_ops at the same time.
4759 * If they support ftrace_ops, it is assumed they support regs.
4760 * If call backs want to use regs, they must either check for regs
4761 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4762 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4763 * An architecture can pass partial regs with ftrace_ops and still
4764 * set the ARCH_SUPPORT_FTARCE_OPS.
4766 #if ARCH_SUPPORTS_FTRACE_OPS
4767 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4768 struct ftrace_ops *op, struct pt_regs *regs)
4770 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4773 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4775 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4779 static void clear_ftrace_swapper(void)
4781 struct task_struct *p;
4785 for_each_online_cpu(cpu) {
4787 clear_tsk_trace_trace(p);
4792 static void set_ftrace_swapper(void)
4794 struct task_struct *p;
4798 for_each_online_cpu(cpu) {
4800 set_tsk_trace_trace(p);
4805 static void clear_ftrace_pid(struct pid *pid)
4807 struct task_struct *p;
4810 do_each_pid_task(pid, PIDTYPE_PID, p) {
4811 clear_tsk_trace_trace(p);
4812 } while_each_pid_task(pid, PIDTYPE_PID, p);
4818 static void set_ftrace_pid(struct pid *pid)
4820 struct task_struct *p;
4823 do_each_pid_task(pid, PIDTYPE_PID, p) {
4824 set_tsk_trace_trace(p);
4825 } while_each_pid_task(pid, PIDTYPE_PID, p);
4829 static void clear_ftrace_pid_task(struct pid *pid)
4831 if (pid == ftrace_swapper_pid)
4832 clear_ftrace_swapper();
4834 clear_ftrace_pid(pid);
4837 static void set_ftrace_pid_task(struct pid *pid)
4839 if (pid == ftrace_swapper_pid)
4840 set_ftrace_swapper();
4842 set_ftrace_pid(pid);
4845 static int ftrace_pid_add(int p)
4848 struct ftrace_pid *fpid;
4851 mutex_lock(&ftrace_lock);
4854 pid = ftrace_swapper_pid;
4856 pid = find_get_pid(p);
4863 list_for_each_entry(fpid, &ftrace_pids, list)
4864 if (fpid->pid == pid)
4869 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4873 list_add(&fpid->list, &ftrace_pids);
4876 set_ftrace_pid_task(pid);
4878 ftrace_update_pid_func();
4879 ftrace_startup_enable(0);
4881 mutex_unlock(&ftrace_lock);
4885 if (pid != ftrace_swapper_pid)
4889 mutex_unlock(&ftrace_lock);
4893 static void ftrace_pid_reset(void)
4895 struct ftrace_pid *fpid, *safe;
4897 mutex_lock(&ftrace_lock);
4898 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4899 struct pid *pid = fpid->pid;
4901 clear_ftrace_pid_task(pid);
4903 list_del(&fpid->list);
4907 ftrace_update_pid_func();
4908 ftrace_startup_enable(0);
4910 mutex_unlock(&ftrace_lock);
4913 static void *fpid_start(struct seq_file *m, loff_t *pos)
4915 mutex_lock(&ftrace_lock);
4917 if (list_empty(&ftrace_pids) && (!*pos))
4920 return seq_list_start(&ftrace_pids, *pos);
4923 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4928 return seq_list_next(v, &ftrace_pids, pos);
4931 static void fpid_stop(struct seq_file *m, void *p)
4933 mutex_unlock(&ftrace_lock);
4936 static int fpid_show(struct seq_file *m, void *v)
4938 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4940 if (v == (void *)1) {
4941 seq_printf(m, "no pid\n");
4945 if (fpid->pid == ftrace_swapper_pid)
4946 seq_printf(m, "swapper tasks\n");
4948 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4953 static const struct seq_operations ftrace_pid_sops = {
4954 .start = fpid_start,
4961 ftrace_pid_open(struct inode *inode, struct file *file)
4965 if ((file->f_mode & FMODE_WRITE) &&
4966 (file->f_flags & O_TRUNC))
4969 if (file->f_mode & FMODE_READ)
4970 ret = seq_open(file, &ftrace_pid_sops);
4976 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4977 size_t cnt, loff_t *ppos)
4983 if (cnt >= sizeof(buf))
4986 if (copy_from_user(&buf, ubuf, cnt))
4992 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4993 * to clean the filter quietly.
4995 tmp = strstrip(buf);
4996 if (strlen(tmp) == 0)
4999 ret = kstrtol(tmp, 10, &val);
5003 ret = ftrace_pid_add(val);
5005 return ret ? ret : cnt;
5009 ftrace_pid_release(struct inode *inode, struct file *file)
5011 if (file->f_mode & FMODE_READ)
5012 seq_release(inode, file);
5017 static const struct file_operations ftrace_pid_fops = {
5018 .open = ftrace_pid_open,
5019 .write = ftrace_pid_write,
5021 .llseek = tracing_lseek,
5022 .release = ftrace_pid_release,
5025 static __init int ftrace_init_debugfs(void)
5027 struct dentry *d_tracer;
5029 d_tracer = tracing_init_dentry();
5033 ftrace_init_dyn_debugfs(d_tracer);
5035 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5036 NULL, &ftrace_pid_fops);
5038 ftrace_profile_debugfs(d_tracer);
5042 fs_initcall(ftrace_init_debugfs);
5045 * ftrace_kill - kill ftrace
5047 * This function should be used by panic code. It stops ftrace
5048 * but in a not so nice way. If you need to simply kill ftrace
5049 * from a non-atomic section, use ftrace_kill.
5051 void ftrace_kill(void)
5053 ftrace_disabled = 1;
5055 clear_ftrace_function();
5059 * Test if ftrace is dead or not.
5061 int ftrace_is_dead(void)
5063 return ftrace_disabled;
5067 * register_ftrace_function - register a function for profiling
5068 * @ops - ops structure that holds the function for profiling.
5070 * Register a function to be called by all functions in the
5073 * Note: @ops->func and all the functions it calls must be labeled
5074 * with "notrace", otherwise it will go into a
5077 int register_ftrace_function(struct ftrace_ops *ops)
5081 ftrace_ops_init(ops);
5083 mutex_lock(&ftrace_lock);
5085 ret = ftrace_startup(ops, 0);
5087 mutex_unlock(&ftrace_lock);
5091 EXPORT_SYMBOL_GPL(register_ftrace_function);
5094 * unregister_ftrace_function - unregister a function for profiling.
5095 * @ops - ops structure that holds the function to unregister
5097 * Unregister a function that was added to be called by ftrace profiling.
5099 int unregister_ftrace_function(struct ftrace_ops *ops)
5103 mutex_lock(&ftrace_lock);
5104 ret = ftrace_shutdown(ops, 0);
5105 mutex_unlock(&ftrace_lock);
5109 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5112 ftrace_enable_sysctl(struct ctl_table *table, int write,
5113 void __user *buffer, size_t *lenp,
5118 mutex_lock(&ftrace_lock);
5120 if (unlikely(ftrace_disabled))
5123 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5125 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5128 last_ftrace_enabled = !!ftrace_enabled;
5130 if (ftrace_enabled) {
5132 ftrace_startup_sysctl();
5134 /* we are starting ftrace again */
5135 if (ftrace_ops_list != &ftrace_list_end)
5136 update_ftrace_function();
5139 /* stopping ftrace calls (just send to ftrace_stub) */
5140 ftrace_trace_function = ftrace_stub;
5142 ftrace_shutdown_sysctl();
5146 mutex_unlock(&ftrace_lock);
5150 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5152 static int ftrace_graph_active;
5154 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5159 /* The callbacks that hook a function */
5160 trace_func_graph_ret_t ftrace_graph_return =
5161 (trace_func_graph_ret_t)ftrace_stub;
5162 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5163 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5165 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5166 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5170 unsigned long flags;
5171 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5172 struct task_struct *g, *t;
5174 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5175 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5176 * sizeof(struct ftrace_ret_stack),
5178 if (!ret_stack_list[i]) {
5186 read_lock_irqsave(&tasklist_lock, flags);
5187 do_each_thread(g, t) {
5193 if (t->ret_stack == NULL) {
5194 atomic_set(&t->tracing_graph_pause, 0);
5195 atomic_set(&t->trace_overrun, 0);
5196 t->curr_ret_stack = -1;
5197 /* Make sure the tasks see the -1 first: */
5199 t->ret_stack = ret_stack_list[start++];
5201 } while_each_thread(g, t);
5204 read_unlock_irqrestore(&tasklist_lock, flags);
5206 for (i = start; i < end; i++)
5207 kfree(ret_stack_list[i]);
5212 ftrace_graph_probe_sched_switch(void *ignore,
5213 struct task_struct *prev, struct task_struct *next)
5215 unsigned long long timestamp;
5219 * Does the user want to count the time a function was asleep.
5220 * If so, do not update the time stamps.
5222 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5225 timestamp = trace_clock_local();
5227 prev->ftrace_timestamp = timestamp;
5229 /* only process tasks that we timestamped */
5230 if (!next->ftrace_timestamp)
5234 * Update all the counters in next to make up for the
5235 * time next was sleeping.
5237 timestamp -= next->ftrace_timestamp;
5239 for (index = next->curr_ret_stack; index >= 0; index--)
5240 next->ret_stack[index].calltime += timestamp;
5243 /* Allocate a return stack for each task */
5244 static int start_graph_tracing(void)
5246 struct ftrace_ret_stack **ret_stack_list;
5249 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5250 sizeof(struct ftrace_ret_stack *),
5253 if (!ret_stack_list)
5256 /* The cpu_boot init_task->ret_stack will never be freed */
5257 for_each_online_cpu(cpu) {
5258 if (!idle_task(cpu)->ret_stack)
5259 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5263 ret = alloc_retstack_tasklist(ret_stack_list);
5264 } while (ret == -EAGAIN);
5267 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5269 pr_info("ftrace_graph: Couldn't activate tracepoint"
5270 " probe to kernel_sched_switch\n");
5273 kfree(ret_stack_list);
5278 * Hibernation protection.
5279 * The state of the current task is too much unstable during
5280 * suspend/restore to disk. We want to protect against that.
5283 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5287 case PM_HIBERNATION_PREPARE:
5288 pause_graph_tracing();
5291 case PM_POST_HIBERNATION:
5292 unpause_graph_tracing();
5298 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5300 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5302 return __ftrace_graph_entry(trace);
5306 * The function graph tracer should only trace the functions defined
5307 * by set_ftrace_filter and set_ftrace_notrace. If another function
5308 * tracer ops is registered, the graph tracer requires testing the
5309 * function against the global ops, and not just trace any function
5310 * that any ftrace_ops registered.
5312 static void update_function_graph_func(void)
5314 if (ftrace_ops_list == &ftrace_list_end ||
5315 (ftrace_ops_list == &global_ops &&
5316 global_ops.next == &ftrace_list_end))
5317 ftrace_graph_entry = __ftrace_graph_entry;
5319 ftrace_graph_entry = ftrace_graph_entry_test;
5322 static struct notifier_block ftrace_suspend_notifier = {
5323 .notifier_call = ftrace_suspend_notifier_call,
5326 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5327 trace_func_graph_ent_t entryfunc)
5331 mutex_lock(&ftrace_lock);
5333 /* we currently allow only one tracer registered at a time */
5334 if (ftrace_graph_active) {
5339 register_pm_notifier(&ftrace_suspend_notifier);
5341 ftrace_graph_active++;
5342 ret = start_graph_tracing();
5344 ftrace_graph_active--;
5348 ftrace_graph_return = retfunc;
5351 * Update the indirect function to the entryfunc, and the
5352 * function that gets called to the entry_test first. Then
5353 * call the update fgraph entry function to determine if
5354 * the entryfunc should be called directly or not.
5356 __ftrace_graph_entry = entryfunc;
5357 ftrace_graph_entry = ftrace_graph_entry_test;
5358 update_function_graph_func();
5360 /* Function graph doesn't use the .func field of global_ops */
5361 global_ops.flags |= FTRACE_OPS_FL_STUB;
5363 #ifdef CONFIG_DYNAMIC_FTRACE
5364 /* Optimize function graph calling (if implemented by arch) */
5365 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5366 global_ops.trampoline = FTRACE_GRAPH_TRAMP_ADDR;
5369 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
5372 mutex_unlock(&ftrace_lock);
5376 void unregister_ftrace_graph(void)
5378 mutex_lock(&ftrace_lock);
5380 if (unlikely(!ftrace_graph_active))
5383 ftrace_graph_active--;
5384 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5385 ftrace_graph_entry = ftrace_graph_entry_stub;
5386 __ftrace_graph_entry = ftrace_graph_entry_stub;
5387 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5388 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
5389 #ifdef CONFIG_DYNAMIC_FTRACE
5390 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5391 global_ops.trampoline = 0;
5393 unregister_pm_notifier(&ftrace_suspend_notifier);
5394 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5397 mutex_unlock(&ftrace_lock);
5400 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5403 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5405 atomic_set(&t->tracing_graph_pause, 0);
5406 atomic_set(&t->trace_overrun, 0);
5407 t->ftrace_timestamp = 0;
5408 /* make curr_ret_stack visible before we add the ret_stack */
5410 t->ret_stack = ret_stack;
5414 * Allocate a return stack for the idle task. May be the first
5415 * time through, or it may be done by CPU hotplug online.
5417 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5419 t->curr_ret_stack = -1;
5421 * The idle task has no parent, it either has its own
5422 * stack or no stack at all.
5425 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5427 if (ftrace_graph_active) {
5428 struct ftrace_ret_stack *ret_stack;
5430 ret_stack = per_cpu(idle_ret_stack, cpu);
5432 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5433 * sizeof(struct ftrace_ret_stack),
5437 per_cpu(idle_ret_stack, cpu) = ret_stack;
5439 graph_init_task(t, ret_stack);
5443 /* Allocate a return stack for newly created task */
5444 void ftrace_graph_init_task(struct task_struct *t)
5446 /* Make sure we do not use the parent ret_stack */
5447 t->ret_stack = NULL;
5448 t->curr_ret_stack = -1;
5450 if (ftrace_graph_active) {
5451 struct ftrace_ret_stack *ret_stack;
5453 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5454 * sizeof(struct ftrace_ret_stack),
5458 graph_init_task(t, ret_stack);
5462 void ftrace_graph_exit_task(struct task_struct *t)
5464 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5466 t->ret_stack = NULL;
5467 /* NULL must become visible to IRQs before we free it: */