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_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
71 #define INIT_REGEX_LOCK(opsname)
74 static struct ftrace_ops ftrace_list_end __read_mostly = {
76 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
79 /* ftrace_enabled is a method to turn ftrace on or off */
80 int ftrace_enabled __read_mostly;
81 static int last_ftrace_enabled;
83 /* Quick disabling of function tracer. */
84 int function_trace_stop __read_mostly;
86 /* Current function tracing op */
87 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
88 /* What to set function_trace_op to */
89 static struct ftrace_ops *set_function_trace_op;
91 /* List for set_ftrace_pid's pids. */
92 LIST_HEAD(ftrace_pids);
94 struct list_head list;
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
102 static int ftrace_disabled __read_mostly;
104 static DEFINE_MUTEX(ftrace_lock);
106 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
107 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
108 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
109 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
110 static struct ftrace_ops global_ops;
111 static struct ftrace_ops control_ops;
113 #if ARCH_SUPPORTS_FTRACE_OPS
114 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
115 struct ftrace_ops *op, struct pt_regs *regs);
117 /* See comment below, where ftrace_ops_list_func is defined */
118 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
119 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
123 * Traverse the ftrace_global_list, invoking all entries. The reason that we
124 * can use rcu_dereference_raw_notrace() is that elements removed from this list
125 * are simply leaked, so there is no need to interact with a grace-period
126 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
127 * concurrent insertions into the ftrace_global_list.
129 * Silly Alpha and silly pointer-speculation compiler optimizations!
131 #define do_for_each_ftrace_op(op, list) \
132 op = rcu_dereference_raw_notrace(list); \
136 * Optimized for just a single item in the list (as that is the normal case).
138 #define while_for_each_ftrace_op(op) \
139 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
140 unlikely((op) != &ftrace_list_end))
142 static inline void ftrace_ops_init(struct ftrace_ops *ops)
144 #ifdef CONFIG_DYNAMIC_FTRACE
145 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
146 mutex_init(&ops->regex_lock);
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 /* If there's no change, then do nothing more here */
269 if (ftrace_trace_function == func)
272 update_function_graph_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_REGEX_LOCK(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 .notrace_hash = EMPTY_HASH,
1089 .filter_hash = EMPTY_HASH,
1090 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1091 INIT_REGEX_LOCK(global_ops)
1094 struct ftrace_page {
1095 struct ftrace_page *next;
1096 struct dyn_ftrace *records;
1101 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1102 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1104 /* estimate from running different kernels */
1105 #define NR_TO_INIT 10000
1107 static struct ftrace_page *ftrace_pages_start;
1108 static struct ftrace_page *ftrace_pages;
1110 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1112 return !hash || !hash->count;
1115 static struct ftrace_func_entry *
1116 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1119 struct ftrace_func_entry *entry;
1120 struct hlist_head *hhd;
1122 if (ftrace_hash_empty(hash))
1125 if (hash->size_bits > 0)
1126 key = hash_long(ip, hash->size_bits);
1130 hhd = &hash->buckets[key];
1132 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1133 if (entry->ip == ip)
1139 static void __add_hash_entry(struct ftrace_hash *hash,
1140 struct ftrace_func_entry *entry)
1142 struct hlist_head *hhd;
1145 if (hash->size_bits)
1146 key = hash_long(entry->ip, hash->size_bits);
1150 hhd = &hash->buckets[key];
1151 hlist_add_head(&entry->hlist, hhd);
1155 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1157 struct ftrace_func_entry *entry;
1159 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1164 __add_hash_entry(hash, entry);
1170 free_hash_entry(struct ftrace_hash *hash,
1171 struct ftrace_func_entry *entry)
1173 hlist_del(&entry->hlist);
1179 remove_hash_entry(struct ftrace_hash *hash,
1180 struct ftrace_func_entry *entry)
1182 hlist_del(&entry->hlist);
1186 static void ftrace_hash_clear(struct ftrace_hash *hash)
1188 struct hlist_head *hhd;
1189 struct hlist_node *tn;
1190 struct ftrace_func_entry *entry;
1191 int size = 1 << hash->size_bits;
1197 for (i = 0; i < size; i++) {
1198 hhd = &hash->buckets[i];
1199 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1200 free_hash_entry(hash, entry);
1202 FTRACE_WARN_ON(hash->count);
1205 static void free_ftrace_hash(struct ftrace_hash *hash)
1207 if (!hash || hash == EMPTY_HASH)
1209 ftrace_hash_clear(hash);
1210 kfree(hash->buckets);
1214 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1216 struct ftrace_hash *hash;
1218 hash = container_of(rcu, struct ftrace_hash, rcu);
1219 free_ftrace_hash(hash);
1222 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1224 if (!hash || hash == EMPTY_HASH)
1226 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1229 void ftrace_free_filter(struct ftrace_ops *ops)
1231 ftrace_ops_init(ops);
1232 free_ftrace_hash(ops->filter_hash);
1233 free_ftrace_hash(ops->notrace_hash);
1236 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1238 struct ftrace_hash *hash;
1241 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1245 size = 1 << size_bits;
1246 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1248 if (!hash->buckets) {
1253 hash->size_bits = size_bits;
1258 static struct ftrace_hash *
1259 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1261 struct ftrace_func_entry *entry;
1262 struct ftrace_hash *new_hash;
1267 new_hash = alloc_ftrace_hash(size_bits);
1272 if (ftrace_hash_empty(hash))
1275 size = 1 << hash->size_bits;
1276 for (i = 0; i < size; i++) {
1277 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1278 ret = add_hash_entry(new_hash, entry->ip);
1284 FTRACE_WARN_ON(new_hash->count != hash->count);
1289 free_ftrace_hash(new_hash);
1294 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1296 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1299 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1300 struct ftrace_hash **dst, struct ftrace_hash *src)
1302 struct ftrace_func_entry *entry;
1303 struct hlist_node *tn;
1304 struct hlist_head *hhd;
1305 struct ftrace_hash *old_hash;
1306 struct ftrace_hash *new_hash;
1307 int size = src->count;
1312 * If the new source is empty, just free dst and assign it
1316 new_hash = EMPTY_HASH;
1321 * Make the hash size about 1/2 the # found
1323 for (size /= 2; size; size >>= 1)
1326 /* Don't allocate too much */
1327 if (bits > FTRACE_HASH_MAX_BITS)
1328 bits = FTRACE_HASH_MAX_BITS;
1330 new_hash = alloc_ftrace_hash(bits);
1334 size = 1 << src->size_bits;
1335 for (i = 0; i < size; i++) {
1336 hhd = &src->buckets[i];
1337 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1338 remove_hash_entry(src, entry);
1339 __add_hash_entry(new_hash, entry);
1345 * Remove the current set, update the hash and add
1348 ftrace_hash_rec_disable(ops, enable);
1351 rcu_assign_pointer(*dst, new_hash);
1352 free_ftrace_hash_rcu(old_hash);
1354 ftrace_hash_rec_enable(ops, enable);
1360 * Test the hashes for this ops to see if we want to call
1361 * the ops->func or not.
1363 * It's a match if the ip is in the ops->filter_hash or
1364 * the filter_hash does not exist or is empty,
1366 * the ip is not in the ops->notrace_hash.
1368 * This needs to be called with preemption disabled as
1369 * the hashes are freed with call_rcu_sched().
1372 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1374 struct ftrace_hash *filter_hash;
1375 struct ftrace_hash *notrace_hash;
1378 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1380 * There's a small race when adding ops that the ftrace handler
1381 * that wants regs, may be called without them. We can not
1382 * allow that handler to be called if regs is NULL.
1384 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1388 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1389 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1391 if ((ftrace_hash_empty(filter_hash) ||
1392 ftrace_lookup_ip(filter_hash, ip)) &&
1393 (ftrace_hash_empty(notrace_hash) ||
1394 !ftrace_lookup_ip(notrace_hash, ip)))
1403 * This is a double for. Do not use 'break' to break out of the loop,
1404 * you must use a goto.
1406 #define do_for_each_ftrace_rec(pg, rec) \
1407 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1409 for (_____i = 0; _____i < pg->index; _____i++) { \
1410 rec = &pg->records[_____i];
1412 #define while_for_each_ftrace_rec() \
1417 static int ftrace_cmp_recs(const void *a, const void *b)
1419 const struct dyn_ftrace *key = a;
1420 const struct dyn_ftrace *rec = b;
1422 if (key->flags < rec->ip)
1424 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1429 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1431 struct ftrace_page *pg;
1432 struct dyn_ftrace *rec;
1433 struct dyn_ftrace key;
1436 key.flags = end; /* overload flags, as it is unsigned long */
1438 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1439 if (end < pg->records[0].ip ||
1440 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1442 rec = bsearch(&key, pg->records, pg->index,
1443 sizeof(struct dyn_ftrace),
1453 * ftrace_location - return true if the ip giving is a traced location
1454 * @ip: the instruction pointer to check
1456 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1457 * That is, the instruction that is either a NOP or call to
1458 * the function tracer. It checks the ftrace internal tables to
1459 * determine if the address belongs or not.
1461 unsigned long ftrace_location(unsigned long ip)
1463 return ftrace_location_range(ip, ip);
1467 * ftrace_text_reserved - return true if range contains an ftrace location
1468 * @start: start of range to search
1469 * @end: end of range to search (inclusive). @end points to the last byte to check.
1471 * Returns 1 if @start and @end contains a ftrace location.
1472 * That is, the instruction that is either a NOP or call to
1473 * the function tracer. It checks the ftrace internal tables to
1474 * determine if the address belongs or not.
1476 int ftrace_text_reserved(const void *start, const void *end)
1480 ret = ftrace_location_range((unsigned long)start,
1481 (unsigned long)end);
1486 /* Test if ops registered to this rec needs regs */
1487 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1489 struct ftrace_ops *ops;
1490 bool keep_regs = false;
1492 for (ops = ftrace_ops_list;
1493 ops != &ftrace_list_end; ops = ops->next) {
1494 /* pass rec in as regs to have non-NULL val */
1495 if (ftrace_ops_test(ops, rec->ip, rec)) {
1496 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1506 static void ftrace_remove_tramp(struct ftrace_ops *ops,
1507 struct dyn_ftrace *rec)
1509 struct ftrace_func_entry *entry;
1511 entry = ftrace_lookup_ip(ops->tramp_hash, rec->ip);
1516 * The tramp_hash entry will be removed at time
1520 rec->flags &= ~FTRACE_FL_TRAMP;
1523 static void ftrace_clear_tramps(struct dyn_ftrace *rec)
1525 struct ftrace_ops *op;
1527 do_for_each_ftrace_op(op, ftrace_ops_list) {
1528 if (op->trampolines)
1529 ftrace_remove_tramp(op, rec);
1530 } while_for_each_ftrace_op(op);
1533 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1537 struct ftrace_hash *hash;
1538 struct ftrace_hash *other_hash;
1539 struct ftrace_page *pg;
1540 struct dyn_ftrace *rec;
1544 /* Only update if the ops has been registered */
1545 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1549 * In the filter_hash case:
1550 * If the count is zero, we update all records.
1551 * Otherwise we just update the items in the hash.
1553 * In the notrace_hash case:
1554 * We enable the update in the hash.
1555 * As disabling notrace means enabling the tracing,
1556 * and enabling notrace means disabling, the inc variable
1560 hash = ops->filter_hash;
1561 other_hash = ops->notrace_hash;
1562 if (ftrace_hash_empty(hash))
1566 hash = ops->notrace_hash;
1567 other_hash = ops->filter_hash;
1569 * If the notrace hash has no items,
1570 * then there's nothing to do.
1572 if (ftrace_hash_empty(hash))
1576 do_for_each_ftrace_rec(pg, rec) {
1577 int in_other_hash = 0;
1583 * Only the filter_hash affects all records.
1584 * Update if the record is not in the notrace hash.
1586 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1589 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1590 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1593 * If filter_hash is set, we want to match all functions
1594 * that are in the hash but not in the other hash.
1596 * If filter_hash is not set, then we are decrementing.
1597 * That means we match anything that is in the hash
1598 * and also in the other_hash. That is, we need to turn
1599 * off functions in the other hash because they are disabled
1602 if (filter_hash && in_hash && !in_other_hash)
1604 else if (!filter_hash && in_hash &&
1605 (in_other_hash || ftrace_hash_empty(other_hash)))
1613 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1617 * If there's only a single callback registered to a
1618 * function, and the ops has a trampoline registered
1619 * for it, then we can call it directly.
1621 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1622 rec->flags |= FTRACE_FL_TRAMP;
1626 * If we are adding another function callback
1627 * to this function, and the previous had a
1628 * trampoline used, then we need to go back to
1629 * the default trampoline.
1631 rec->flags &= ~FTRACE_FL_TRAMP;
1633 /* remove trampolines from any ops for this rec */
1634 ftrace_clear_tramps(rec);
1638 * If any ops wants regs saved for this function
1639 * then all ops will get saved regs.
1641 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1642 rec->flags |= FTRACE_FL_REGS;
1644 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1648 if (ops->trampoline && !ftrace_rec_count(rec))
1649 ftrace_remove_tramp(ops, rec);
1652 * If the rec had REGS enabled and the ops that is
1653 * being removed had REGS set, then see if there is
1654 * still any ops for this record that wants regs.
1655 * If not, we can stop recording them.
1657 if (ftrace_rec_count(rec) > 0 &&
1658 rec->flags & FTRACE_FL_REGS &&
1659 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1660 if (!test_rec_ops_needs_regs(rec))
1661 rec->flags &= ~FTRACE_FL_REGS;
1665 * flags will be cleared in ftrace_check_record()
1666 * if rec count is zero.
1670 /* Shortcut, if we handled all records, we are done. */
1671 if (!all && count == hash->count)
1673 } while_for_each_ftrace_rec();
1676 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1679 __ftrace_hash_rec_update(ops, filter_hash, 0);
1682 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1685 __ftrace_hash_rec_update(ops, filter_hash, 1);
1688 static void print_ip_ins(const char *fmt, unsigned char *p)
1692 printk(KERN_CONT "%s", fmt);
1694 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1695 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1699 * ftrace_bug - report and shutdown function tracer
1700 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1701 * @ip: The address that failed
1703 * The arch code that enables or disables the function tracing
1704 * can call ftrace_bug() when it has detected a problem in
1705 * modifying the code. @failed should be one of either:
1706 * EFAULT - if the problem happens on reading the @ip address
1707 * EINVAL - if what is read at @ip is not what was expected
1708 * EPERM - if the problem happens on writting to the @ip address
1710 void ftrace_bug(int failed, unsigned long ip)
1714 FTRACE_WARN_ON_ONCE(1);
1715 pr_info("ftrace faulted on modifying ");
1719 FTRACE_WARN_ON_ONCE(1);
1720 pr_info("ftrace failed to modify ");
1722 print_ip_ins(" actual: ", (unsigned char *)ip);
1723 printk(KERN_CONT "\n");
1726 FTRACE_WARN_ON_ONCE(1);
1727 pr_info("ftrace faulted on writing ");
1731 FTRACE_WARN_ON_ONCE(1);
1732 pr_info("ftrace faulted on unknown error ");
1737 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1739 unsigned long flag = 0UL;
1742 * If we are updating calls:
1744 * If the record has a ref count, then we need to enable it
1745 * because someone is using it.
1747 * Otherwise we make sure its disabled.
1749 * If we are disabling calls, then disable all records that
1752 if (enable && ftrace_rec_count(rec))
1753 flag = FTRACE_FL_ENABLED;
1756 * If enabling and the REGS flag does not match the REGS_EN, or
1757 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1758 * this record. Set flags to fail the compare against ENABLED.
1761 if (!(rec->flags & FTRACE_FL_REGS) !=
1762 !(rec->flags & FTRACE_FL_REGS_EN))
1763 flag |= FTRACE_FL_REGS;
1765 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1766 !(rec->flags & FTRACE_FL_TRAMP_EN))
1767 flag |= FTRACE_FL_TRAMP;
1770 /* If the state of this record hasn't changed, then do nothing */
1771 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1772 return FTRACE_UPDATE_IGNORE;
1775 /* Save off if rec is being enabled (for return value) */
1776 flag ^= rec->flags & FTRACE_FL_ENABLED;
1779 rec->flags |= FTRACE_FL_ENABLED;
1780 if (flag & FTRACE_FL_REGS) {
1781 if (rec->flags & FTRACE_FL_REGS)
1782 rec->flags |= FTRACE_FL_REGS_EN;
1784 rec->flags &= ~FTRACE_FL_REGS_EN;
1786 if (flag & FTRACE_FL_TRAMP) {
1787 if (rec->flags & FTRACE_FL_TRAMP)
1788 rec->flags |= FTRACE_FL_TRAMP_EN;
1790 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1795 * If this record is being updated from a nop, then
1796 * return UPDATE_MAKE_CALL.
1798 * return UPDATE_MODIFY_CALL to tell the caller to convert
1799 * from the save regs, to a non-save regs function or
1800 * vice versa, or from a trampoline call.
1802 if (flag & FTRACE_FL_ENABLED)
1803 return FTRACE_UPDATE_MAKE_CALL;
1805 return FTRACE_UPDATE_MODIFY_CALL;
1809 /* If there's no more users, clear all flags */
1810 if (!ftrace_rec_count(rec))
1813 /* Just disable the record (keep REGS state) */
1814 rec->flags &= ~FTRACE_FL_ENABLED;
1817 return FTRACE_UPDATE_MAKE_NOP;
1821 * ftrace_update_record, set a record that now is tracing or not
1822 * @rec: the record to update
1823 * @enable: set to 1 if the record is tracing, zero to force disable
1825 * The records that represent all functions that can be traced need
1826 * to be updated when tracing has been enabled.
1828 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1830 return ftrace_check_record(rec, enable, 1);
1834 * ftrace_test_record, check if the record has been enabled or not
1835 * @rec: the record to test
1836 * @enable: set to 1 to check if enabled, 0 if it is disabled
1838 * The arch code may need to test if a record is already set to
1839 * tracing to determine how to modify the function code that it
1842 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1844 return ftrace_check_record(rec, enable, 0);
1847 static struct ftrace_ops *
1848 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1850 struct ftrace_ops *op;
1852 /* Removed ops need to be tested first */
1853 if (removed_ops && removed_ops->tramp_hash) {
1854 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1858 do_for_each_ftrace_op(op, ftrace_ops_list) {
1859 if (!op->tramp_hash)
1862 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1865 } while_for_each_ftrace_op(op);
1870 static struct ftrace_ops *
1871 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1873 struct ftrace_ops *op;
1875 do_for_each_ftrace_op(op, ftrace_ops_list) {
1876 /* pass rec in as regs to have non-NULL val */
1877 if (ftrace_ops_test(op, rec->ip, rec))
1879 } while_for_each_ftrace_op(op);
1885 * ftrace_get_addr_new - Get the call address to set to
1886 * @rec: The ftrace record descriptor
1888 * If the record has the FTRACE_FL_REGS set, that means that it
1889 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1890 * is not not set, then it wants to convert to the normal callback.
1892 * Returns the address of the trampoline to set to
1894 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1896 struct ftrace_ops *ops;
1898 /* Trampolines take precedence over regs */
1899 if (rec->flags & FTRACE_FL_TRAMP) {
1900 ops = ftrace_find_tramp_ops_new(rec);
1901 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1902 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1903 (void *)rec->ip, (void *)rec->ip);
1904 /* Ftrace is shutting down, return anything */
1905 return (unsigned long)FTRACE_ADDR;
1907 return ops->trampoline;
1910 if (rec->flags & FTRACE_FL_REGS)
1911 return (unsigned long)FTRACE_REGS_ADDR;
1913 return (unsigned long)FTRACE_ADDR;
1917 * ftrace_get_addr_curr - Get the call address that is already there
1918 * @rec: The ftrace record descriptor
1920 * The FTRACE_FL_REGS_EN is set when the record already points to
1921 * a function that saves all the regs. Basically the '_EN' version
1922 * represents the current state of the function.
1924 * Returns the address of the trampoline that is currently being called
1926 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1928 struct ftrace_ops *ops;
1930 /* Trampolines take precedence over regs */
1931 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1932 ops = ftrace_find_tramp_ops_curr(rec);
1933 if (FTRACE_WARN_ON(!ops)) {
1934 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1935 (void *)rec->ip, (void *)rec->ip);
1936 /* Ftrace is shutting down, return anything */
1937 return (unsigned long)FTRACE_ADDR;
1939 return ops->trampoline;
1942 if (rec->flags & FTRACE_FL_REGS_EN)
1943 return (unsigned long)FTRACE_REGS_ADDR;
1945 return (unsigned long)FTRACE_ADDR;
1949 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1951 unsigned long ftrace_old_addr;
1952 unsigned long ftrace_addr;
1955 ftrace_addr = ftrace_get_addr_new(rec);
1957 /* This needs to be done before we call ftrace_update_record */
1958 ftrace_old_addr = ftrace_get_addr_curr(rec);
1960 ret = ftrace_update_record(rec, enable);
1963 case FTRACE_UPDATE_IGNORE:
1966 case FTRACE_UPDATE_MAKE_CALL:
1967 return ftrace_make_call(rec, ftrace_addr);
1969 case FTRACE_UPDATE_MAKE_NOP:
1970 return ftrace_make_nop(NULL, rec, ftrace_addr);
1972 case FTRACE_UPDATE_MODIFY_CALL:
1973 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1976 return -1; /* unknow ftrace bug */
1979 void __weak ftrace_replace_code(int enable)
1981 struct dyn_ftrace *rec;
1982 struct ftrace_page *pg;
1985 if (unlikely(ftrace_disabled))
1988 do_for_each_ftrace_rec(pg, rec) {
1989 failed = __ftrace_replace_code(rec, enable);
1991 ftrace_bug(failed, rec->ip);
1992 /* Stop processing */
1995 } while_for_each_ftrace_rec();
1998 struct ftrace_rec_iter {
1999 struct ftrace_page *pg;
2004 * ftrace_rec_iter_start, start up iterating over traced functions
2006 * Returns an iterator handle that is used to iterate over all
2007 * the records that represent address locations where functions
2010 * May return NULL if no records are available.
2012 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2015 * We only use a single iterator.
2016 * Protected by the ftrace_lock mutex.
2018 static struct ftrace_rec_iter ftrace_rec_iter;
2019 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2021 iter->pg = ftrace_pages_start;
2024 /* Could have empty pages */
2025 while (iter->pg && !iter->pg->index)
2026 iter->pg = iter->pg->next;
2035 * ftrace_rec_iter_next, get the next record to process.
2036 * @iter: The handle to the iterator.
2038 * Returns the next iterator after the given iterator @iter.
2040 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2044 if (iter->index >= iter->pg->index) {
2045 iter->pg = iter->pg->next;
2048 /* Could have empty pages */
2049 while (iter->pg && !iter->pg->index)
2050 iter->pg = iter->pg->next;
2060 * ftrace_rec_iter_record, get the record at the iterator location
2061 * @iter: The current iterator location
2063 * Returns the record that the current @iter is at.
2065 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2067 return &iter->pg->records[iter->index];
2071 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2078 if (unlikely(ftrace_disabled))
2081 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2083 ftrace_bug(ret, ip);
2090 * archs can override this function if they must do something
2091 * before the modifying code is performed.
2093 int __weak ftrace_arch_code_modify_prepare(void)
2099 * archs can override this function if they must do something
2100 * after the modifying code is performed.
2102 int __weak ftrace_arch_code_modify_post_process(void)
2107 void ftrace_modify_all_code(int command)
2109 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2113 * If the ftrace_caller calls a ftrace_ops func directly,
2114 * we need to make sure that it only traces functions it
2115 * expects to trace. When doing the switch of functions,
2116 * we need to update to the ftrace_ops_list_func first
2117 * before the transition between old and new calls are set,
2118 * as the ftrace_ops_list_func will check the ops hashes
2119 * to make sure the ops are having the right functions
2123 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2124 if (FTRACE_WARN_ON(err))
2128 if (command & FTRACE_UPDATE_CALLS)
2129 ftrace_replace_code(1);
2130 else if (command & FTRACE_DISABLE_CALLS)
2131 ftrace_replace_code(0);
2133 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2134 function_trace_op = set_function_trace_op;
2136 /* If irqs are disabled, we are in stop machine */
2137 if (!irqs_disabled())
2138 smp_call_function(ftrace_sync_ipi, NULL, 1);
2139 err = ftrace_update_ftrace_func(ftrace_trace_function);
2140 if (FTRACE_WARN_ON(err))
2144 if (command & FTRACE_START_FUNC_RET)
2145 err = ftrace_enable_ftrace_graph_caller();
2146 else if (command & FTRACE_STOP_FUNC_RET)
2147 err = ftrace_disable_ftrace_graph_caller();
2148 FTRACE_WARN_ON(err);
2151 static int __ftrace_modify_code(void *data)
2153 int *command = data;
2155 ftrace_modify_all_code(*command);
2161 * ftrace_run_stop_machine, go back to the stop machine method
2162 * @command: The command to tell ftrace what to do
2164 * If an arch needs to fall back to the stop machine method, the
2165 * it can call this function.
2167 void ftrace_run_stop_machine(int command)
2169 stop_machine(__ftrace_modify_code, &command, NULL);
2173 * arch_ftrace_update_code, modify the code to trace or not trace
2174 * @command: The command that needs to be done
2176 * Archs can override this function if it does not need to
2177 * run stop_machine() to modify code.
2179 void __weak arch_ftrace_update_code(int command)
2181 ftrace_run_stop_machine(command);
2184 static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2186 struct ftrace_page *pg;
2187 struct dyn_ftrace *rec;
2191 size = ops->trampolines;
2194 * Make the hash size about 1/2 the # found
2196 for (size /= 2; size; size >>= 1)
2199 ops->tramp_hash = alloc_ftrace_hash(bits);
2201 * TODO: a failed allocation is going to screw up
2202 * the accounting of what needs to be modified
2203 * and not. For now, we kill ftrace if we fail
2204 * to allocate here. But there are ways around this,
2205 * but that will take a little more work.
2207 if (!ops->tramp_hash)
2210 do_for_each_ftrace_rec(pg, rec) {
2211 if (ftrace_rec_count(rec) == 1 &&
2212 ftrace_ops_test(ops, rec->ip, rec)) {
2214 /* This record had better have a trampoline */
2215 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2218 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2222 } while_for_each_ftrace_rec();
2227 static int ftrace_save_tramp_hashes(void)
2229 struct ftrace_ops *op;
2233 * Now that any trampoline is being used, we need to save the
2234 * hashes for the ops that have them. This allows the mapping
2235 * back from the record to the ops that has the trampoline to
2236 * know what code is being replaced. Modifying code must always
2237 * verify what it is changing.
2239 do_for_each_ftrace_op(op, ftrace_ops_list) {
2241 /* The tramp_hash is recreated each time. */
2242 free_ftrace_hash(op->tramp_hash);
2243 op->tramp_hash = NULL;
2245 if (op->trampolines) {
2246 ret = ftrace_save_ops_tramp_hash(op);
2251 } while_for_each_ftrace_op(op);
2256 static void ftrace_run_update_code(int command)
2260 ret = ftrace_arch_code_modify_prepare();
2261 FTRACE_WARN_ON(ret);
2265 * Do not call function tracer while we update the code.
2266 * We are in stop machine.
2268 function_trace_stop++;
2271 * By default we use stop_machine() to modify the code.
2272 * But archs can do what ever they want as long as it
2273 * is safe. The stop_machine() is the safest, but also
2274 * produces the most overhead.
2276 arch_ftrace_update_code(command);
2278 function_trace_stop--;
2280 ret = ftrace_arch_code_modify_post_process();
2281 FTRACE_WARN_ON(ret);
2283 ret = ftrace_save_tramp_hashes();
2284 FTRACE_WARN_ON(ret);
2287 static ftrace_func_t saved_ftrace_func;
2288 static int ftrace_start_up;
2289 static int global_start_up;
2291 static void control_ops_free(struct ftrace_ops *ops)
2293 free_percpu(ops->disabled);
2296 static void ftrace_startup_enable(int command)
2298 if (saved_ftrace_func != ftrace_trace_function) {
2299 saved_ftrace_func = ftrace_trace_function;
2300 command |= FTRACE_UPDATE_TRACE_FUNC;
2303 if (!command || !ftrace_enabled)
2306 ftrace_run_update_code(command);
2309 static int ftrace_startup(struct ftrace_ops *ops, int command)
2313 if (unlikely(ftrace_disabled))
2316 ret = __register_ftrace_function(ops);
2321 command |= FTRACE_UPDATE_CALLS;
2323 ops->flags |= FTRACE_OPS_FL_ENABLED;
2325 ftrace_hash_rec_enable(ops, 1);
2327 ftrace_startup_enable(command);
2332 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2336 if (unlikely(ftrace_disabled))
2339 ret = __unregister_ftrace_function(ops);
2345 * Just warn in case of unbalance, no need to kill ftrace, it's not
2346 * critical but the ftrace_call callers may be never nopped again after
2347 * further ftrace uses.
2349 WARN_ON_ONCE(ftrace_start_up < 0);
2351 ftrace_hash_rec_disable(ops, 1);
2353 if (!global_start_up)
2354 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2356 command |= FTRACE_UPDATE_CALLS;
2358 if (saved_ftrace_func != ftrace_trace_function) {
2359 saved_ftrace_func = ftrace_trace_function;
2360 command |= FTRACE_UPDATE_TRACE_FUNC;
2363 if (!command || !ftrace_enabled) {
2365 * If these are control ops, they still need their
2366 * per_cpu field freed. Since, function tracing is
2367 * not currently active, we can just free them
2368 * without synchronizing all CPUs.
2370 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2371 control_ops_free(ops);
2376 * If the ops uses a trampoline, then it needs to be
2377 * tested first on update.
2381 ftrace_run_update_code(command);
2386 * Dynamic ops may be freed, we must make sure that all
2387 * callers are done before leaving this function.
2388 * The same goes for freeing the per_cpu data of the control
2391 * Again, normal synchronize_sched() is not good enough.
2392 * We need to do a hard force of sched synchronization.
2393 * This is because we use preempt_disable() to do RCU, but
2394 * the function tracers can be called where RCU is not watching
2395 * (like before user_exit()). We can not rely on the RCU
2396 * infrastructure to do the synchronization, thus we must do it
2399 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2400 schedule_on_each_cpu(ftrace_sync);
2402 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2403 control_ops_free(ops);
2409 static void ftrace_startup_sysctl(void)
2411 if (unlikely(ftrace_disabled))
2414 /* Force update next time */
2415 saved_ftrace_func = NULL;
2416 /* ftrace_start_up is true if we want ftrace running */
2417 if (ftrace_start_up)
2418 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2421 static void ftrace_shutdown_sysctl(void)
2423 if (unlikely(ftrace_disabled))
2426 /* ftrace_start_up is true if ftrace is running */
2427 if (ftrace_start_up)
2428 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2431 static cycle_t ftrace_update_time;
2432 unsigned long ftrace_update_tot_cnt;
2434 static inline int ops_traces_mod(struct ftrace_ops *ops)
2437 * Filter_hash being empty will default to trace module.
2438 * But notrace hash requires a test of individual module functions.
2440 return ftrace_hash_empty(ops->filter_hash) &&
2441 ftrace_hash_empty(ops->notrace_hash);
2445 * Check if the current ops references the record.
2447 * If the ops traces all functions, then it was already accounted for.
2448 * If the ops does not trace the current record function, skip it.
2449 * If the ops ignores the function via notrace filter, skip it.
2452 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2454 /* If ops isn't enabled, ignore it */
2455 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2458 /* If ops traces all mods, we already accounted for it */
2459 if (ops_traces_mod(ops))
2462 /* The function must be in the filter */
2463 if (!ftrace_hash_empty(ops->filter_hash) &&
2464 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2467 /* If in notrace hash, we ignore it too */
2468 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2474 static int referenced_filters(struct dyn_ftrace *rec)
2476 struct ftrace_ops *ops;
2479 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2480 if (ops_references_rec(ops, rec))
2487 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2489 struct ftrace_page *pg;
2490 struct dyn_ftrace *p;
2491 cycle_t start, stop;
2492 unsigned long update_cnt = 0;
2493 unsigned long ref = 0;
2498 * When adding a module, we need to check if tracers are
2499 * currently enabled and if they are set to trace all functions.
2500 * If they are, we need to enable the module functions as well
2501 * as update the reference counts for those function records.
2504 struct ftrace_ops *ops;
2506 for (ops = ftrace_ops_list;
2507 ops != &ftrace_list_end; ops = ops->next) {
2508 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2509 if (ops_traces_mod(ops))
2517 start = ftrace_now(raw_smp_processor_id());
2519 for (pg = new_pgs; pg; pg = pg->next) {
2521 for (i = 0; i < pg->index; i++) {
2524 /* If something went wrong, bail without enabling anything */
2525 if (unlikely(ftrace_disabled))
2528 p = &pg->records[i];
2530 cnt += referenced_filters(p);
2534 * Do the initial record conversion from mcount jump
2535 * to the NOP instructions.
2537 if (!ftrace_code_disable(mod, p))
2543 * If the tracing is enabled, go ahead and enable the record.
2545 * The reason not to enable the record immediatelly is the
2546 * inherent check of ftrace_make_nop/ftrace_make_call for
2547 * correct previous instructions. Making first the NOP
2548 * conversion puts the module to the correct state, thus
2549 * passing the ftrace_make_call check.
2551 if (ftrace_start_up && cnt) {
2552 int failed = __ftrace_replace_code(p, 1);
2554 ftrace_bug(failed, p->ip);
2559 stop = ftrace_now(raw_smp_processor_id());
2560 ftrace_update_time = stop - start;
2561 ftrace_update_tot_cnt += update_cnt;
2566 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2571 if (WARN_ON(!count))
2574 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2577 * We want to fill as much as possible. No more than a page
2580 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2584 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2587 /* if we can't allocate this size, try something smaller */
2594 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2603 static struct ftrace_page *
2604 ftrace_allocate_pages(unsigned long num_to_init)
2606 struct ftrace_page *start_pg;
2607 struct ftrace_page *pg;
2614 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2619 * Try to allocate as much as possible in one continues
2620 * location that fills in all of the space. We want to
2621 * waste as little space as possible.
2624 cnt = ftrace_allocate_records(pg, num_to_init);
2632 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2643 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2644 free_pages((unsigned long)pg->records, order);
2645 start_pg = pg->next;
2649 pr_info("ftrace: FAILED to allocate memory for functions\n");
2653 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2655 struct ftrace_iterator {
2658 struct ftrace_page *pg;
2659 struct dyn_ftrace *func;
2660 struct ftrace_func_probe *probe;
2661 struct trace_parser parser;
2662 struct ftrace_hash *hash;
2663 struct ftrace_ops *ops;
2670 t_hash_next(struct seq_file *m, loff_t *pos)
2672 struct ftrace_iterator *iter = m->private;
2673 struct hlist_node *hnd = NULL;
2674 struct hlist_head *hhd;
2680 hnd = &iter->probe->node;
2682 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2685 hhd = &ftrace_func_hash[iter->hidx];
2687 if (hlist_empty(hhd)) {
2703 if (WARN_ON_ONCE(!hnd))
2706 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2711 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2713 struct ftrace_iterator *iter = m->private;
2717 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2720 if (iter->func_pos > *pos)
2724 for (l = 0; l <= (*pos - iter->func_pos); ) {
2725 p = t_hash_next(m, &l);
2732 /* Only set this if we have an item */
2733 iter->flags |= FTRACE_ITER_HASH;
2739 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2741 struct ftrace_func_probe *rec;
2744 if (WARN_ON_ONCE(!rec))
2747 if (rec->ops->print)
2748 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2750 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2753 seq_printf(m, ":%p", rec->data);
2760 t_next(struct seq_file *m, void *v, loff_t *pos)
2762 struct ftrace_iterator *iter = m->private;
2763 struct ftrace_ops *ops = iter->ops;
2764 struct dyn_ftrace *rec = NULL;
2766 if (unlikely(ftrace_disabled))
2769 if (iter->flags & FTRACE_ITER_HASH)
2770 return t_hash_next(m, pos);
2773 iter->pos = iter->func_pos = *pos;
2775 if (iter->flags & FTRACE_ITER_PRINTALL)
2776 return t_hash_start(m, pos);
2779 if (iter->idx >= iter->pg->index) {
2780 if (iter->pg->next) {
2781 iter->pg = iter->pg->next;
2786 rec = &iter->pg->records[iter->idx++];
2787 if (((iter->flags & FTRACE_ITER_FILTER) &&
2788 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2790 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2791 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2793 ((iter->flags & FTRACE_ITER_ENABLED) &&
2794 !(rec->flags & FTRACE_FL_ENABLED))) {
2802 return t_hash_start(m, pos);
2809 static void reset_iter_read(struct ftrace_iterator *iter)
2813 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2816 static void *t_start(struct seq_file *m, loff_t *pos)
2818 struct ftrace_iterator *iter = m->private;
2819 struct ftrace_ops *ops = iter->ops;
2823 mutex_lock(&ftrace_lock);
2825 if (unlikely(ftrace_disabled))
2829 * If an lseek was done, then reset and start from beginning.
2831 if (*pos < iter->pos)
2832 reset_iter_read(iter);
2835 * For set_ftrace_filter reading, if we have the filter
2836 * off, we can short cut and just print out that all
2837 * functions are enabled.
2839 if (iter->flags & FTRACE_ITER_FILTER &&
2840 ftrace_hash_empty(ops->filter_hash)) {
2842 return t_hash_start(m, pos);
2843 iter->flags |= FTRACE_ITER_PRINTALL;
2844 /* reset in case of seek/pread */
2845 iter->flags &= ~FTRACE_ITER_HASH;
2849 if (iter->flags & FTRACE_ITER_HASH)
2850 return t_hash_start(m, pos);
2853 * Unfortunately, we need to restart at ftrace_pages_start
2854 * every time we let go of the ftrace_mutex. This is because
2855 * those pointers can change without the lock.
2857 iter->pg = ftrace_pages_start;
2859 for (l = 0; l <= *pos; ) {
2860 p = t_next(m, p, &l);
2866 return t_hash_start(m, pos);
2871 static void t_stop(struct seq_file *m, void *p)
2873 mutex_unlock(&ftrace_lock);
2876 static int t_show(struct seq_file *m, void *v)
2878 struct ftrace_iterator *iter = m->private;
2879 struct dyn_ftrace *rec;
2881 if (iter->flags & FTRACE_ITER_HASH)
2882 return t_hash_show(m, iter);
2884 if (iter->flags & FTRACE_ITER_PRINTALL) {
2885 seq_printf(m, "#### all functions enabled ####\n");
2894 seq_printf(m, "%ps", (void *)rec->ip);
2895 if (iter->flags & FTRACE_ITER_ENABLED) {
2896 seq_printf(m, " (%ld)%s",
2897 ftrace_rec_count(rec),
2898 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2899 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2900 struct ftrace_ops *ops;
2902 ops = ftrace_find_tramp_ops_curr(rec);
2903 if (ops && ops->trampoline)
2904 seq_printf(m, "\ttramp: %pS",
2905 (void *)ops->trampoline);
2907 seq_printf(m, "\ttramp: ERROR!");
2911 seq_printf(m, "\n");
2916 static const struct seq_operations show_ftrace_seq_ops = {
2924 ftrace_avail_open(struct inode *inode, struct file *file)
2926 struct ftrace_iterator *iter;
2928 if (unlikely(ftrace_disabled))
2931 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2933 iter->pg = ftrace_pages_start;
2934 iter->ops = &global_ops;
2937 return iter ? 0 : -ENOMEM;
2941 ftrace_enabled_open(struct inode *inode, struct file *file)
2943 struct ftrace_iterator *iter;
2945 if (unlikely(ftrace_disabled))
2948 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2950 iter->pg = ftrace_pages_start;
2951 iter->flags = FTRACE_ITER_ENABLED;
2952 iter->ops = &global_ops;
2955 return iter ? 0 : -ENOMEM;
2958 static void ftrace_filter_reset(struct ftrace_hash *hash)
2960 mutex_lock(&ftrace_lock);
2961 ftrace_hash_clear(hash);
2962 mutex_unlock(&ftrace_lock);
2966 * ftrace_regex_open - initialize function tracer filter files
2967 * @ops: The ftrace_ops that hold the hash filters
2968 * @flag: The type of filter to process
2969 * @inode: The inode, usually passed in to your open routine
2970 * @file: The file, usually passed in to your open routine
2972 * ftrace_regex_open() initializes the filter files for the
2973 * @ops. Depending on @flag it may process the filter hash or
2974 * the notrace hash of @ops. With this called from the open
2975 * routine, you can use ftrace_filter_write() for the write
2976 * routine if @flag has FTRACE_ITER_FILTER set, or
2977 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2978 * tracing_lseek() should be used as the lseek routine, and
2979 * release must call ftrace_regex_release().
2982 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2983 struct inode *inode, struct file *file)
2985 struct ftrace_iterator *iter;
2986 struct ftrace_hash *hash;
2989 ftrace_ops_init(ops);
2991 if (unlikely(ftrace_disabled))
2994 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2998 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3006 mutex_lock(&ops->regex_lock);
3008 if (flag & FTRACE_ITER_NOTRACE)
3009 hash = ops->notrace_hash;
3011 hash = ops->filter_hash;
3013 if (file->f_mode & FMODE_WRITE) {
3014 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
3016 trace_parser_put(&iter->parser);
3023 if ((file->f_mode & FMODE_WRITE) &&
3024 (file->f_flags & O_TRUNC))
3025 ftrace_filter_reset(iter->hash);
3027 if (file->f_mode & FMODE_READ) {
3028 iter->pg = ftrace_pages_start;
3030 ret = seq_open(file, &show_ftrace_seq_ops);
3032 struct seq_file *m = file->private_data;
3036 free_ftrace_hash(iter->hash);
3037 trace_parser_put(&iter->parser);
3041 file->private_data = iter;
3044 mutex_unlock(&ops->regex_lock);
3050 ftrace_filter_open(struct inode *inode, struct file *file)
3052 struct ftrace_ops *ops = inode->i_private;
3054 return ftrace_regex_open(ops,
3055 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3060 ftrace_notrace_open(struct inode *inode, struct file *file)
3062 struct ftrace_ops *ops = inode->i_private;
3064 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3068 static int ftrace_match(char *str, char *regex, int len, int type)
3075 if (strcmp(str, regex) == 0)
3078 case MATCH_FRONT_ONLY:
3079 if (strncmp(str, regex, len) == 0)
3082 case MATCH_MIDDLE_ONLY:
3083 if (strstr(str, regex))
3086 case MATCH_END_ONLY:
3088 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
3097 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
3099 struct ftrace_func_entry *entry;
3102 entry = ftrace_lookup_ip(hash, rec->ip);
3104 /* Do nothing if it doesn't exist */
3108 free_hash_entry(hash, entry);
3110 /* Do nothing if it exists */
3114 ret = add_hash_entry(hash, rec->ip);
3120 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3121 char *regex, int len, int type)
3123 char str[KSYM_SYMBOL_LEN];
3126 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3129 /* module lookup requires matching the module */
3130 if (!modname || strcmp(modname, mod))
3133 /* blank search means to match all funcs in the mod */
3138 return ftrace_match(str, regex, len, type);
3142 match_records(struct ftrace_hash *hash, char *buff,
3143 int len, char *mod, int not)
3145 unsigned search_len = 0;
3146 struct ftrace_page *pg;
3147 struct dyn_ftrace *rec;
3148 int type = MATCH_FULL;
3149 char *search = buff;
3154 type = filter_parse_regex(buff, len, &search, ¬);
3155 search_len = strlen(search);
3158 mutex_lock(&ftrace_lock);
3160 if (unlikely(ftrace_disabled))
3163 do_for_each_ftrace_rec(pg, rec) {
3164 if (ftrace_match_record(rec, mod, search, search_len, type)) {
3165 ret = enter_record(hash, rec, not);
3172 } while_for_each_ftrace_rec();
3174 mutex_unlock(&ftrace_lock);
3180 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3182 return match_records(hash, buff, len, NULL, 0);
3186 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
3190 /* blank or '*' mean the same */
3191 if (strcmp(buff, "*") == 0)
3194 /* handle the case of 'dont filter this module' */
3195 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3200 return match_records(hash, buff, strlen(buff), mod, not);
3204 * We register the module command as a template to show others how
3205 * to register the a command as well.
3209 ftrace_mod_callback(struct ftrace_hash *hash,
3210 char *func, char *cmd, char *param, int enable)
3216 * cmd == 'mod' because we only registered this func
3217 * for the 'mod' ftrace_func_command.
3218 * But if you register one func with multiple commands,
3219 * you can tell which command was used by the cmd
3223 /* we must have a module name */
3227 mod = strsep(¶m, ":");
3231 ret = ftrace_match_module_records(hash, func, mod);
3240 static struct ftrace_func_command ftrace_mod_cmd = {
3242 .func = ftrace_mod_callback,
3245 static int __init ftrace_mod_cmd_init(void)
3247 return register_ftrace_command(&ftrace_mod_cmd);
3249 core_initcall(ftrace_mod_cmd_init);
3251 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3252 struct ftrace_ops *op, struct pt_regs *pt_regs)
3254 struct ftrace_func_probe *entry;
3255 struct hlist_head *hhd;
3258 key = hash_long(ip, FTRACE_HASH_BITS);
3260 hhd = &ftrace_func_hash[key];
3262 if (hlist_empty(hhd))
3266 * Disable preemption for these calls to prevent a RCU grace
3267 * period. This syncs the hash iteration and freeing of items
3268 * on the hash. rcu_read_lock is too dangerous here.
3270 preempt_disable_notrace();
3271 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3272 if (entry->ip == ip)
3273 entry->ops->func(ip, parent_ip, &entry->data);
3275 preempt_enable_notrace();
3278 static struct ftrace_ops trace_probe_ops __read_mostly =
3280 .func = function_trace_probe_call,
3281 .flags = FTRACE_OPS_FL_INITIALIZED,
3282 INIT_REGEX_LOCK(trace_probe_ops)
3285 static int ftrace_probe_registered;
3287 static void __enable_ftrace_function_probe(void)
3292 if (ftrace_probe_registered) {
3293 /* still need to update the function call sites */
3295 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3299 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3300 struct hlist_head *hhd = &ftrace_func_hash[i];
3304 /* Nothing registered? */
3305 if (i == FTRACE_FUNC_HASHSIZE)
3308 ret = ftrace_startup(&trace_probe_ops, 0);
3310 ftrace_probe_registered = 1;
3313 static void __disable_ftrace_function_probe(void)
3317 if (!ftrace_probe_registered)
3320 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3321 struct hlist_head *hhd = &ftrace_func_hash[i];
3326 /* no more funcs left */
3327 ftrace_shutdown(&trace_probe_ops, 0);
3329 ftrace_probe_registered = 0;
3333 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3335 if (entry->ops->free)
3336 entry->ops->free(entry->ops, entry->ip, &entry->data);
3341 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3344 struct ftrace_func_probe *entry;
3345 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3346 struct ftrace_hash *hash;
3347 struct ftrace_page *pg;
3348 struct dyn_ftrace *rec;
3355 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3356 len = strlen(search);
3358 /* we do not support '!' for function probes */
3362 mutex_lock(&trace_probe_ops.regex_lock);
3364 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3370 if (unlikely(ftrace_disabled)) {
3375 mutex_lock(&ftrace_lock);
3377 do_for_each_ftrace_rec(pg, rec) {
3379 if (!ftrace_match_record(rec, NULL, search, len, type))
3382 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3384 /* If we did not process any, then return error */
3395 * The caller might want to do something special
3396 * for each function we find. We call the callback
3397 * to give the caller an opportunity to do so.
3400 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3401 /* caller does not like this func */
3407 ret = enter_record(hash, rec, 0);
3415 entry->ip = rec->ip;
3417 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3418 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3420 } while_for_each_ftrace_rec();
3422 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3426 __enable_ftrace_function_probe();
3429 mutex_unlock(&ftrace_lock);
3431 mutex_unlock(&trace_probe_ops.regex_lock);
3432 free_ftrace_hash(hash);
3438 PROBE_TEST_FUNC = 1,
3443 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3444 void *data, int flags)
3446 struct ftrace_func_entry *rec_entry;
3447 struct ftrace_func_probe *entry;
3448 struct ftrace_func_probe *p;
3449 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3450 struct list_head free_list;
3451 struct ftrace_hash *hash;
3452 struct hlist_node *tmp;
3453 char str[KSYM_SYMBOL_LEN];
3454 int type = MATCH_FULL;
3458 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3463 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3464 len = strlen(search);
3466 /* we do not support '!' for function probes */
3471 mutex_lock(&trace_probe_ops.regex_lock);
3473 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3475 /* Hmm, should report this somehow */
3478 INIT_LIST_HEAD(&free_list);
3480 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3481 struct hlist_head *hhd = &ftrace_func_hash[i];
3483 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3485 /* break up if statements for readability */
3486 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3489 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3492 /* do this last, since it is the most expensive */
3494 kallsyms_lookup(entry->ip, NULL, NULL,
3496 if (!ftrace_match(str, glob, len, type))
3500 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3501 /* It is possible more than one entry had this ip */
3503 free_hash_entry(hash, rec_entry);
3505 hlist_del_rcu(&entry->node);
3506 list_add(&entry->free_list, &free_list);
3509 mutex_lock(&ftrace_lock);
3510 __disable_ftrace_function_probe();
3512 * Remove after the disable is called. Otherwise, if the last
3513 * probe is removed, a null hash means *all enabled*.
3515 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3516 synchronize_sched();
3517 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3518 list_del(&entry->free_list);
3519 ftrace_free_entry(entry);
3521 mutex_unlock(&ftrace_lock);
3524 mutex_unlock(&trace_probe_ops.regex_lock);
3525 free_ftrace_hash(hash);
3529 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3532 __unregister_ftrace_function_probe(glob, ops, data,
3533 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3537 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3539 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3542 void unregister_ftrace_function_probe_all(char *glob)
3544 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3547 static LIST_HEAD(ftrace_commands);
3548 static DEFINE_MUTEX(ftrace_cmd_mutex);
3551 * Currently we only register ftrace commands from __init, so mark this
3554 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3556 struct ftrace_func_command *p;
3559 mutex_lock(&ftrace_cmd_mutex);
3560 list_for_each_entry(p, &ftrace_commands, list) {
3561 if (strcmp(cmd->name, p->name) == 0) {
3566 list_add(&cmd->list, &ftrace_commands);
3568 mutex_unlock(&ftrace_cmd_mutex);
3574 * Currently we only unregister ftrace commands from __init, so mark
3577 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3579 struct ftrace_func_command *p, *n;
3582 mutex_lock(&ftrace_cmd_mutex);
3583 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3584 if (strcmp(cmd->name, p->name) == 0) {
3586 list_del_init(&p->list);
3591 mutex_unlock(&ftrace_cmd_mutex);
3596 static int ftrace_process_regex(struct ftrace_hash *hash,
3597 char *buff, int len, int enable)
3599 char *func, *command, *next = buff;
3600 struct ftrace_func_command *p;
3603 func = strsep(&next, ":");
3606 ret = ftrace_match_records(hash, func, len);
3616 command = strsep(&next, ":");
3618 mutex_lock(&ftrace_cmd_mutex);
3619 list_for_each_entry(p, &ftrace_commands, list) {
3620 if (strcmp(p->name, command) == 0) {
3621 ret = p->func(hash, func, command, next, enable);
3626 mutex_unlock(&ftrace_cmd_mutex);
3632 ftrace_regex_write(struct file *file, const char __user *ubuf,
3633 size_t cnt, loff_t *ppos, int enable)
3635 struct ftrace_iterator *iter;
3636 struct trace_parser *parser;
3642 if (file->f_mode & FMODE_READ) {
3643 struct seq_file *m = file->private_data;
3646 iter = file->private_data;
3648 if (unlikely(ftrace_disabled))
3651 /* iter->hash is a local copy, so we don't need regex_lock */
3653 parser = &iter->parser;
3654 read = trace_get_user(parser, ubuf, cnt, ppos);
3656 if (read >= 0 && trace_parser_loaded(parser) &&
3657 !trace_parser_cont(parser)) {
3658 ret = ftrace_process_regex(iter->hash, parser->buffer,
3659 parser->idx, enable);
3660 trace_parser_clear(parser);
3671 ftrace_filter_write(struct file *file, const char __user *ubuf,
3672 size_t cnt, loff_t *ppos)
3674 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3678 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3679 size_t cnt, loff_t *ppos)
3681 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3685 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3687 struct ftrace_func_entry *entry;
3689 if (!ftrace_location(ip))
3693 entry = ftrace_lookup_ip(hash, ip);
3696 free_hash_entry(hash, entry);
3700 return add_hash_entry(hash, ip);
3703 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3705 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3706 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3710 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3711 unsigned long ip, int remove, int reset, int enable)
3713 struct ftrace_hash **orig_hash;
3714 struct ftrace_hash *hash;
3717 if (unlikely(ftrace_disabled))
3720 mutex_lock(&ops->regex_lock);
3723 orig_hash = &ops->filter_hash;
3725 orig_hash = &ops->notrace_hash;
3727 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3730 goto out_regex_unlock;
3734 ftrace_filter_reset(hash);
3735 if (buf && !ftrace_match_records(hash, buf, len)) {
3737 goto out_regex_unlock;
3740 ret = ftrace_match_addr(hash, ip, remove);
3742 goto out_regex_unlock;
3745 mutex_lock(&ftrace_lock);
3746 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3748 ftrace_ops_update_code(ops);
3750 mutex_unlock(&ftrace_lock);
3753 mutex_unlock(&ops->regex_lock);
3755 free_ftrace_hash(hash);
3760 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3761 int reset, int enable)
3763 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3767 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3768 * @ops - the ops to set the filter with
3769 * @ip - the address to add to or remove from the filter.
3770 * @remove - non zero to remove the ip from the filter
3771 * @reset - non zero to reset all filters before applying this filter.
3773 * Filters denote which functions should be enabled when tracing is enabled
3774 * If @ip is NULL, it failes to update filter.
3776 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3777 int remove, int reset)
3779 ftrace_ops_init(ops);
3780 return ftrace_set_addr(ops, ip, remove, reset, 1);
3782 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3785 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3786 int reset, int enable)
3788 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3792 * ftrace_set_filter - set a function to filter on in ftrace
3793 * @ops - the ops to set the filter with
3794 * @buf - the string that holds the function filter text.
3795 * @len - the length of the string.
3796 * @reset - non zero to reset all filters before applying this filter.
3798 * Filters denote which functions should be enabled when tracing is enabled.
3799 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3801 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3804 ftrace_ops_init(ops);
3805 return ftrace_set_regex(ops, buf, len, reset, 1);
3807 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3810 * ftrace_set_notrace - set a function to not trace in ftrace
3811 * @ops - the ops to set the notrace filter with
3812 * @buf - the string that holds the function notrace text.
3813 * @len - the length of the string.
3814 * @reset - non zero to reset all filters before applying this filter.
3816 * Notrace Filters denote which functions should not be enabled when tracing
3817 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3820 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3823 ftrace_ops_init(ops);
3824 return ftrace_set_regex(ops, buf, len, reset, 0);
3826 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3828 * ftrace_set_global_filter - set a function to filter on with global tracers
3829 * @buf - the string that holds the function filter text.
3830 * @len - the length of the string.
3831 * @reset - non zero to reset all filters before applying this filter.
3833 * Filters denote which functions should be enabled when tracing is enabled.
3834 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3836 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3838 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3840 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3843 * ftrace_set_global_notrace - set a function to not trace with global tracers
3844 * @buf - the string that holds the function notrace text.
3845 * @len - the length of the string.
3846 * @reset - non zero to reset all filters before applying this filter.
3848 * Notrace Filters denote which functions should not be enabled when tracing
3849 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3852 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3854 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3856 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3859 * command line interface to allow users to set filters on boot up.
3861 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3862 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3863 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3865 /* Used by function selftest to not test if filter is set */
3866 bool ftrace_filter_param __initdata;
3868 static int __init set_ftrace_notrace(char *str)
3870 ftrace_filter_param = true;
3871 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3874 __setup("ftrace_notrace=", set_ftrace_notrace);
3876 static int __init set_ftrace_filter(char *str)
3878 ftrace_filter_param = true;
3879 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3882 __setup("ftrace_filter=", set_ftrace_filter);
3884 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3885 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3886 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3888 static int __init set_graph_function(char *str)
3890 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3893 __setup("ftrace_graph_filter=", set_graph_function);
3895 static void __init set_ftrace_early_graph(char *buf)
3901 func = strsep(&buf, ",");
3902 /* we allow only one expression at a time */
3903 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3904 FTRACE_GRAPH_MAX_FUNCS, func);
3906 printk(KERN_DEBUG "ftrace: function %s not "
3907 "traceable\n", func);
3910 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3913 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3917 ftrace_ops_init(ops);
3920 func = strsep(&buf, ",");
3921 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3925 static void __init set_ftrace_early_filters(void)
3927 if (ftrace_filter_buf[0])
3928 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3929 if (ftrace_notrace_buf[0])
3930 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3931 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3932 if (ftrace_graph_buf[0])
3933 set_ftrace_early_graph(ftrace_graph_buf);
3934 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3937 int ftrace_regex_release(struct inode *inode, struct file *file)
3939 struct seq_file *m = (struct seq_file *)file->private_data;
3940 struct ftrace_iterator *iter;
3941 struct ftrace_hash **orig_hash;
3942 struct trace_parser *parser;
3946 if (file->f_mode & FMODE_READ) {
3948 seq_release(inode, file);
3950 iter = file->private_data;
3952 parser = &iter->parser;
3953 if (trace_parser_loaded(parser)) {
3954 parser->buffer[parser->idx] = 0;
3955 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3958 trace_parser_put(parser);
3960 mutex_lock(&iter->ops->regex_lock);
3962 if (file->f_mode & FMODE_WRITE) {
3963 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3966 orig_hash = &iter->ops->filter_hash;
3968 orig_hash = &iter->ops->notrace_hash;
3970 mutex_lock(&ftrace_lock);
3971 ret = ftrace_hash_move(iter->ops, filter_hash,
3972 orig_hash, iter->hash);
3974 ftrace_ops_update_code(iter->ops);
3976 mutex_unlock(&ftrace_lock);
3979 mutex_unlock(&iter->ops->regex_lock);
3980 free_ftrace_hash(iter->hash);
3986 static const struct file_operations ftrace_avail_fops = {
3987 .open = ftrace_avail_open,
3989 .llseek = seq_lseek,
3990 .release = seq_release_private,
3993 static const struct file_operations ftrace_enabled_fops = {
3994 .open = ftrace_enabled_open,
3996 .llseek = seq_lseek,
3997 .release = seq_release_private,
4000 static const struct file_operations ftrace_filter_fops = {
4001 .open = ftrace_filter_open,
4003 .write = ftrace_filter_write,
4004 .llseek = tracing_lseek,
4005 .release = ftrace_regex_release,
4008 static const struct file_operations ftrace_notrace_fops = {
4009 .open = ftrace_notrace_open,
4011 .write = ftrace_notrace_write,
4012 .llseek = tracing_lseek,
4013 .release = ftrace_regex_release,
4016 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4018 static DEFINE_MUTEX(graph_lock);
4020 int ftrace_graph_count;
4021 int ftrace_graph_notrace_count;
4022 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4023 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4025 struct ftrace_graph_data {
4026 unsigned long *table;
4029 const struct seq_operations *seq_ops;
4033 __g_next(struct seq_file *m, loff_t *pos)
4035 struct ftrace_graph_data *fgd = m->private;
4037 if (*pos >= *fgd->count)
4039 return &fgd->table[*pos];
4043 g_next(struct seq_file *m, void *v, loff_t *pos)
4046 return __g_next(m, pos);
4049 static void *g_start(struct seq_file *m, loff_t *pos)
4051 struct ftrace_graph_data *fgd = m->private;
4053 mutex_lock(&graph_lock);
4055 /* Nothing, tell g_show to print all functions are enabled */
4056 if (!*fgd->count && !*pos)
4059 return __g_next(m, pos);
4062 static void g_stop(struct seq_file *m, void *p)
4064 mutex_unlock(&graph_lock);
4067 static int g_show(struct seq_file *m, void *v)
4069 unsigned long *ptr = v;
4074 if (ptr == (unsigned long *)1) {
4075 seq_printf(m, "#### all functions enabled ####\n");
4079 seq_printf(m, "%ps\n", (void *)*ptr);
4084 static const struct seq_operations ftrace_graph_seq_ops = {
4092 __ftrace_graph_open(struct inode *inode, struct file *file,
4093 struct ftrace_graph_data *fgd)
4097 mutex_lock(&graph_lock);
4098 if ((file->f_mode & FMODE_WRITE) &&
4099 (file->f_flags & O_TRUNC)) {
4101 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4103 mutex_unlock(&graph_lock);
4105 if (file->f_mode & FMODE_READ) {
4106 ret = seq_open(file, fgd->seq_ops);
4108 struct seq_file *m = file->private_data;
4112 file->private_data = fgd;
4118 ftrace_graph_open(struct inode *inode, struct file *file)
4120 struct ftrace_graph_data *fgd;
4122 if (unlikely(ftrace_disabled))
4125 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4129 fgd->table = ftrace_graph_funcs;
4130 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4131 fgd->count = &ftrace_graph_count;
4132 fgd->seq_ops = &ftrace_graph_seq_ops;
4134 return __ftrace_graph_open(inode, file, fgd);
4138 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4140 struct ftrace_graph_data *fgd;
4142 if (unlikely(ftrace_disabled))
4145 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4149 fgd->table = ftrace_graph_notrace_funcs;
4150 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4151 fgd->count = &ftrace_graph_notrace_count;
4152 fgd->seq_ops = &ftrace_graph_seq_ops;
4154 return __ftrace_graph_open(inode, file, fgd);
4158 ftrace_graph_release(struct inode *inode, struct file *file)
4160 if (file->f_mode & FMODE_READ) {
4161 struct seq_file *m = file->private_data;
4164 seq_release(inode, file);
4166 kfree(file->private_data);
4173 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4175 struct dyn_ftrace *rec;
4176 struct ftrace_page *pg;
4185 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
4186 if (!not && *idx >= size)
4189 search_len = strlen(search);
4191 mutex_lock(&ftrace_lock);
4193 if (unlikely(ftrace_disabled)) {
4194 mutex_unlock(&ftrace_lock);
4198 do_for_each_ftrace_rec(pg, rec) {
4200 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4201 /* if it is in the array */
4203 for (i = 0; i < *idx; i++) {
4204 if (array[i] == rec->ip) {
4213 array[(*idx)++] = rec->ip;
4219 array[i] = array[--(*idx)];
4225 } while_for_each_ftrace_rec();
4227 mutex_unlock(&ftrace_lock);
4236 ftrace_graph_write(struct file *file, const char __user *ubuf,
4237 size_t cnt, loff_t *ppos)
4239 struct trace_parser parser;
4240 ssize_t read, ret = 0;
4241 struct ftrace_graph_data *fgd = file->private_data;
4246 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4249 read = trace_get_user(&parser, ubuf, cnt, ppos);
4251 if (read >= 0 && trace_parser_loaded((&parser))) {
4252 parser.buffer[parser.idx] = 0;
4254 mutex_lock(&graph_lock);
4256 /* we allow only one expression at a time */
4257 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4260 mutex_unlock(&graph_lock);
4266 trace_parser_put(&parser);
4271 static const struct file_operations ftrace_graph_fops = {
4272 .open = ftrace_graph_open,
4274 .write = ftrace_graph_write,
4275 .llseek = tracing_lseek,
4276 .release = ftrace_graph_release,
4279 static const struct file_operations ftrace_graph_notrace_fops = {
4280 .open = ftrace_graph_notrace_open,
4282 .write = ftrace_graph_write,
4283 .llseek = tracing_lseek,
4284 .release = ftrace_graph_release,
4286 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4288 void ftrace_create_filter_files(struct ftrace_ops *ops,
4289 struct dentry *parent)
4292 trace_create_file("set_ftrace_filter", 0644, parent,
4293 ops, &ftrace_filter_fops);
4295 trace_create_file("set_ftrace_notrace", 0644, parent,
4296 ops, &ftrace_notrace_fops);
4300 * The name "destroy_filter_files" is really a misnomer. Although
4301 * in the future, it may actualy delete the files, but this is
4302 * really intended to make sure the ops passed in are disabled
4303 * and that when this function returns, the caller is free to
4306 * The "destroy" name is only to match the "create" name that this
4307 * should be paired with.
4309 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4311 mutex_lock(&ftrace_lock);
4312 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4313 ftrace_shutdown(ops, 0);
4314 ops->flags |= FTRACE_OPS_FL_DELETED;
4315 mutex_unlock(&ftrace_lock);
4318 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4321 trace_create_file("available_filter_functions", 0444,
4322 d_tracer, NULL, &ftrace_avail_fops);
4324 trace_create_file("enabled_functions", 0444,
4325 d_tracer, NULL, &ftrace_enabled_fops);
4327 ftrace_create_filter_files(&global_ops, d_tracer);
4329 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4330 trace_create_file("set_graph_function", 0444, d_tracer,
4332 &ftrace_graph_fops);
4333 trace_create_file("set_graph_notrace", 0444, d_tracer,
4335 &ftrace_graph_notrace_fops);
4336 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4341 static int ftrace_cmp_ips(const void *a, const void *b)
4343 const unsigned long *ipa = a;
4344 const unsigned long *ipb = b;
4353 static void ftrace_swap_ips(void *a, void *b, int size)
4355 unsigned long *ipa = a;
4356 unsigned long *ipb = b;
4364 static int ftrace_process_locs(struct module *mod,
4365 unsigned long *start,
4368 struct ftrace_page *start_pg;
4369 struct ftrace_page *pg;
4370 struct dyn_ftrace *rec;
4371 unsigned long count;
4374 unsigned long flags = 0; /* Shut up gcc */
4377 count = end - start;
4382 sort(start, count, sizeof(*start),
4383 ftrace_cmp_ips, ftrace_swap_ips);
4385 start_pg = ftrace_allocate_pages(count);
4389 mutex_lock(&ftrace_lock);
4392 * Core and each module needs their own pages, as
4393 * modules will free them when they are removed.
4394 * Force a new page to be allocated for modules.
4397 WARN_ON(ftrace_pages || ftrace_pages_start);
4398 /* First initialization */
4399 ftrace_pages = ftrace_pages_start = start_pg;
4404 if (WARN_ON(ftrace_pages->next)) {
4405 /* Hmm, we have free pages? */
4406 while (ftrace_pages->next)
4407 ftrace_pages = ftrace_pages->next;
4410 ftrace_pages->next = start_pg;
4416 addr = ftrace_call_adjust(*p++);
4418 * Some architecture linkers will pad between
4419 * the different mcount_loc sections of different
4420 * object files to satisfy alignments.
4421 * Skip any NULL pointers.
4426 if (pg->index == pg->size) {
4427 /* We should have allocated enough */
4428 if (WARN_ON(!pg->next))
4433 rec = &pg->records[pg->index++];
4437 /* We should have used all pages */
4440 /* Assign the last page to ftrace_pages */
4444 * We only need to disable interrupts on start up
4445 * because we are modifying code that an interrupt
4446 * may execute, and the modification is not atomic.
4447 * But for modules, nothing runs the code we modify
4448 * until we are finished with it, and there's no
4449 * reason to cause large interrupt latencies while we do it.
4452 local_irq_save(flags);
4453 ftrace_update_code(mod, start_pg);
4455 local_irq_restore(flags);
4458 mutex_unlock(&ftrace_lock);
4463 #ifdef CONFIG_MODULES
4465 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4467 void ftrace_release_mod(struct module *mod)
4469 struct dyn_ftrace *rec;
4470 struct ftrace_page **last_pg;
4471 struct ftrace_page *pg;
4474 mutex_lock(&ftrace_lock);
4476 if (ftrace_disabled)
4480 * Each module has its own ftrace_pages, remove
4481 * them from the list.
4483 last_pg = &ftrace_pages_start;
4484 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4485 rec = &pg->records[0];
4486 if (within_module_core(rec->ip, mod)) {
4488 * As core pages are first, the first
4489 * page should never be a module page.
4491 if (WARN_ON(pg == ftrace_pages_start))
4494 /* Check if we are deleting the last page */
4495 if (pg == ftrace_pages)
4496 ftrace_pages = next_to_ftrace_page(last_pg);
4498 *last_pg = pg->next;
4499 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4500 free_pages((unsigned long)pg->records, order);
4503 last_pg = &pg->next;
4506 mutex_unlock(&ftrace_lock);
4509 static void ftrace_init_module(struct module *mod,
4510 unsigned long *start, unsigned long *end)
4512 if (ftrace_disabled || start == end)
4514 ftrace_process_locs(mod, start, end);
4517 void ftrace_module_init(struct module *mod)
4519 ftrace_init_module(mod, mod->ftrace_callsites,
4520 mod->ftrace_callsites +
4521 mod->num_ftrace_callsites);
4524 static int ftrace_module_notify_exit(struct notifier_block *self,
4525 unsigned long val, void *data)
4527 struct module *mod = data;
4529 if (val == MODULE_STATE_GOING)
4530 ftrace_release_mod(mod);
4535 static int ftrace_module_notify_exit(struct notifier_block *self,
4536 unsigned long val, void *data)
4540 #endif /* CONFIG_MODULES */
4542 struct notifier_block ftrace_module_exit_nb = {
4543 .notifier_call = ftrace_module_notify_exit,
4544 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4547 void __init ftrace_init(void)
4549 extern unsigned long __start_mcount_loc[];
4550 extern unsigned long __stop_mcount_loc[];
4551 unsigned long count, flags;
4554 local_irq_save(flags);
4555 ret = ftrace_dyn_arch_init();
4556 local_irq_restore(flags);
4560 count = __stop_mcount_loc - __start_mcount_loc;
4562 pr_info("ftrace: No functions to be traced?\n");
4566 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4567 count, count / ENTRIES_PER_PAGE + 1);
4569 last_ftrace_enabled = ftrace_enabled = 1;
4571 ret = ftrace_process_locs(NULL,
4575 ret = register_module_notifier(&ftrace_module_exit_nb);
4577 pr_warning("Failed to register trace ftrace module exit notifier\n");
4579 set_ftrace_early_filters();
4583 ftrace_disabled = 1;
4588 static struct ftrace_ops global_ops = {
4589 .func = ftrace_stub,
4590 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4591 INIT_REGEX_LOCK(global_ops)
4594 static int __init ftrace_nodyn_init(void)
4599 core_initcall(ftrace_nodyn_init);
4601 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4602 static inline void ftrace_startup_enable(int command) { }
4603 /* Keep as macros so we do not need to define the commands */
4604 # define ftrace_startup(ops, command) \
4606 int ___ret = __register_ftrace_function(ops); \
4608 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4611 # define ftrace_shutdown(ops, command) \
4613 int ___ret = __unregister_ftrace_function(ops); \
4615 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4619 # define ftrace_startup_sysctl() do { } while (0)
4620 # define ftrace_shutdown_sysctl() do { } while (0)
4623 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4628 #endif /* CONFIG_DYNAMIC_FTRACE */
4630 __init void ftrace_init_global_array_ops(struct trace_array *tr)
4632 tr->ops = &global_ops;
4633 tr->ops->private = tr;
4636 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4638 /* If we filter on pids, update to use the pid function */
4639 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4640 if (WARN_ON(tr->ops->func != ftrace_stub))
4641 printk("ftrace ops had %pS for function\n",
4643 /* Only the top level instance does pid tracing */
4644 if (!list_empty(&ftrace_pids)) {
4645 set_ftrace_pid_function(func);
4646 func = ftrace_pid_func;
4649 tr->ops->func = func;
4650 tr->ops->private = tr;
4653 void ftrace_reset_array_ops(struct trace_array *tr)
4655 tr->ops->func = ftrace_stub;
4659 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4660 struct ftrace_ops *op, struct pt_regs *regs)
4662 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4666 * Some of the ops may be dynamically allocated,
4667 * they must be freed after a synchronize_sched().
4669 preempt_disable_notrace();
4670 trace_recursion_set(TRACE_CONTROL_BIT);
4673 * Control funcs (perf) uses RCU. Only trace if
4674 * RCU is currently active.
4676 if (!rcu_is_watching())
4679 do_for_each_ftrace_op(op, ftrace_control_list) {
4680 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4681 !ftrace_function_local_disabled(op) &&
4682 ftrace_ops_test(op, ip, regs))
4683 op->func(ip, parent_ip, op, regs);
4684 } while_for_each_ftrace_op(op);
4686 trace_recursion_clear(TRACE_CONTROL_BIT);
4687 preempt_enable_notrace();
4690 static struct ftrace_ops control_ops = {
4691 .func = ftrace_ops_control_func,
4692 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4693 INIT_REGEX_LOCK(control_ops)
4697 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4698 struct ftrace_ops *ignored, struct pt_regs *regs)
4700 struct ftrace_ops *op;
4703 if (function_trace_stop)
4706 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4711 * Some of the ops may be dynamically allocated,
4712 * they must be freed after a synchronize_sched().
4714 preempt_disable_notrace();
4715 do_for_each_ftrace_op(op, ftrace_ops_list) {
4716 if (ftrace_ops_test(op, ip, regs)) {
4717 if (WARN_ON(!op->func)) {
4718 function_trace_stop = 1;
4719 printk("op=%p %pS\n", op, op);
4722 op->func(ip, parent_ip, op, regs);
4724 } while_for_each_ftrace_op(op);
4726 preempt_enable_notrace();
4727 trace_clear_recursion(bit);
4731 * Some archs only support passing ip and parent_ip. Even though
4732 * the list function ignores the op parameter, we do not want any
4733 * C side effects, where a function is called without the caller
4734 * sending a third parameter.
4735 * Archs are to support both the regs and ftrace_ops at the same time.
4736 * If they support ftrace_ops, it is assumed they support regs.
4737 * If call backs want to use regs, they must either check for regs
4738 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4739 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4740 * An architecture can pass partial regs with ftrace_ops and still
4741 * set the ARCH_SUPPORT_FTARCE_OPS.
4743 #if ARCH_SUPPORTS_FTRACE_OPS
4744 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4745 struct ftrace_ops *op, struct pt_regs *regs)
4747 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4750 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4752 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4756 static void clear_ftrace_swapper(void)
4758 struct task_struct *p;
4762 for_each_online_cpu(cpu) {
4764 clear_tsk_trace_trace(p);
4769 static void set_ftrace_swapper(void)
4771 struct task_struct *p;
4775 for_each_online_cpu(cpu) {
4777 set_tsk_trace_trace(p);
4782 static void clear_ftrace_pid(struct pid *pid)
4784 struct task_struct *p;
4787 do_each_pid_task(pid, PIDTYPE_PID, p) {
4788 clear_tsk_trace_trace(p);
4789 } while_each_pid_task(pid, PIDTYPE_PID, p);
4795 static void set_ftrace_pid(struct pid *pid)
4797 struct task_struct *p;
4800 do_each_pid_task(pid, PIDTYPE_PID, p) {
4801 set_tsk_trace_trace(p);
4802 } while_each_pid_task(pid, PIDTYPE_PID, p);
4806 static void clear_ftrace_pid_task(struct pid *pid)
4808 if (pid == ftrace_swapper_pid)
4809 clear_ftrace_swapper();
4811 clear_ftrace_pid(pid);
4814 static void set_ftrace_pid_task(struct pid *pid)
4816 if (pid == ftrace_swapper_pid)
4817 set_ftrace_swapper();
4819 set_ftrace_pid(pid);
4822 static int ftrace_pid_add(int p)
4825 struct ftrace_pid *fpid;
4828 mutex_lock(&ftrace_lock);
4831 pid = ftrace_swapper_pid;
4833 pid = find_get_pid(p);
4840 list_for_each_entry(fpid, &ftrace_pids, list)
4841 if (fpid->pid == pid)
4846 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4850 list_add(&fpid->list, &ftrace_pids);
4853 set_ftrace_pid_task(pid);
4855 ftrace_update_pid_func();
4856 ftrace_startup_enable(0);
4858 mutex_unlock(&ftrace_lock);
4862 if (pid != ftrace_swapper_pid)
4866 mutex_unlock(&ftrace_lock);
4870 static void ftrace_pid_reset(void)
4872 struct ftrace_pid *fpid, *safe;
4874 mutex_lock(&ftrace_lock);
4875 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4876 struct pid *pid = fpid->pid;
4878 clear_ftrace_pid_task(pid);
4880 list_del(&fpid->list);
4884 ftrace_update_pid_func();
4885 ftrace_startup_enable(0);
4887 mutex_unlock(&ftrace_lock);
4890 static void *fpid_start(struct seq_file *m, loff_t *pos)
4892 mutex_lock(&ftrace_lock);
4894 if (list_empty(&ftrace_pids) && (!*pos))
4897 return seq_list_start(&ftrace_pids, *pos);
4900 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4905 return seq_list_next(v, &ftrace_pids, pos);
4908 static void fpid_stop(struct seq_file *m, void *p)
4910 mutex_unlock(&ftrace_lock);
4913 static int fpid_show(struct seq_file *m, void *v)
4915 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4917 if (v == (void *)1) {
4918 seq_printf(m, "no pid\n");
4922 if (fpid->pid == ftrace_swapper_pid)
4923 seq_printf(m, "swapper tasks\n");
4925 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4930 static const struct seq_operations ftrace_pid_sops = {
4931 .start = fpid_start,
4938 ftrace_pid_open(struct inode *inode, struct file *file)
4942 if ((file->f_mode & FMODE_WRITE) &&
4943 (file->f_flags & O_TRUNC))
4946 if (file->f_mode & FMODE_READ)
4947 ret = seq_open(file, &ftrace_pid_sops);
4953 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4954 size_t cnt, loff_t *ppos)
4960 if (cnt >= sizeof(buf))
4963 if (copy_from_user(&buf, ubuf, cnt))
4969 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4970 * to clean the filter quietly.
4972 tmp = strstrip(buf);
4973 if (strlen(tmp) == 0)
4976 ret = kstrtol(tmp, 10, &val);
4980 ret = ftrace_pid_add(val);
4982 return ret ? ret : cnt;
4986 ftrace_pid_release(struct inode *inode, struct file *file)
4988 if (file->f_mode & FMODE_READ)
4989 seq_release(inode, file);
4994 static const struct file_operations ftrace_pid_fops = {
4995 .open = ftrace_pid_open,
4996 .write = ftrace_pid_write,
4998 .llseek = tracing_lseek,
4999 .release = ftrace_pid_release,
5002 static __init int ftrace_init_debugfs(void)
5004 struct dentry *d_tracer;
5006 d_tracer = tracing_init_dentry();
5010 ftrace_init_dyn_debugfs(d_tracer);
5012 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5013 NULL, &ftrace_pid_fops);
5015 ftrace_profile_debugfs(d_tracer);
5019 fs_initcall(ftrace_init_debugfs);
5022 * ftrace_kill - kill ftrace
5024 * This function should be used by panic code. It stops ftrace
5025 * but in a not so nice way. If you need to simply kill ftrace
5026 * from a non-atomic section, use ftrace_kill.
5028 void ftrace_kill(void)
5030 ftrace_disabled = 1;
5032 clear_ftrace_function();
5036 * Test if ftrace is dead or not.
5038 int ftrace_is_dead(void)
5040 return ftrace_disabled;
5044 * register_ftrace_function - register a function for profiling
5045 * @ops - ops structure that holds the function for profiling.
5047 * Register a function to be called by all functions in the
5050 * Note: @ops->func and all the functions it calls must be labeled
5051 * with "notrace", otherwise it will go into a
5054 int register_ftrace_function(struct ftrace_ops *ops)
5058 ftrace_ops_init(ops);
5060 mutex_lock(&ftrace_lock);
5062 ret = ftrace_startup(ops, 0);
5064 mutex_unlock(&ftrace_lock);
5068 EXPORT_SYMBOL_GPL(register_ftrace_function);
5071 * unregister_ftrace_function - unregister a function for profiling.
5072 * @ops - ops structure that holds the function to unregister
5074 * Unregister a function that was added to be called by ftrace profiling.
5076 int unregister_ftrace_function(struct ftrace_ops *ops)
5080 mutex_lock(&ftrace_lock);
5081 ret = ftrace_shutdown(ops, 0);
5082 mutex_unlock(&ftrace_lock);
5086 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5089 ftrace_enable_sysctl(struct ctl_table *table, int write,
5090 void __user *buffer, size_t *lenp,
5095 mutex_lock(&ftrace_lock);
5097 if (unlikely(ftrace_disabled))
5100 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5102 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5105 last_ftrace_enabled = !!ftrace_enabled;
5107 if (ftrace_enabled) {
5109 ftrace_startup_sysctl();
5111 /* we are starting ftrace again */
5112 if (ftrace_ops_list != &ftrace_list_end)
5113 update_ftrace_function();
5116 /* stopping ftrace calls (just send to ftrace_stub) */
5117 ftrace_trace_function = ftrace_stub;
5119 ftrace_shutdown_sysctl();
5123 mutex_unlock(&ftrace_lock);
5127 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5129 static int ftrace_graph_active;
5131 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5136 /* The callbacks that hook a function */
5137 trace_func_graph_ret_t ftrace_graph_return =
5138 (trace_func_graph_ret_t)ftrace_stub;
5139 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5140 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5142 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5143 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5147 unsigned long flags;
5148 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5149 struct task_struct *g, *t;
5151 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5152 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5153 * sizeof(struct ftrace_ret_stack),
5155 if (!ret_stack_list[i]) {
5163 read_lock_irqsave(&tasklist_lock, flags);
5164 do_each_thread(g, t) {
5170 if (t->ret_stack == NULL) {
5171 atomic_set(&t->tracing_graph_pause, 0);
5172 atomic_set(&t->trace_overrun, 0);
5173 t->curr_ret_stack = -1;
5174 /* Make sure the tasks see the -1 first: */
5176 t->ret_stack = ret_stack_list[start++];
5178 } while_each_thread(g, t);
5181 read_unlock_irqrestore(&tasklist_lock, flags);
5183 for (i = start; i < end; i++)
5184 kfree(ret_stack_list[i]);
5189 ftrace_graph_probe_sched_switch(void *ignore,
5190 struct task_struct *prev, struct task_struct *next)
5192 unsigned long long timestamp;
5196 * Does the user want to count the time a function was asleep.
5197 * If so, do not update the time stamps.
5199 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5202 timestamp = trace_clock_local();
5204 prev->ftrace_timestamp = timestamp;
5206 /* only process tasks that we timestamped */
5207 if (!next->ftrace_timestamp)
5211 * Update all the counters in next to make up for the
5212 * time next was sleeping.
5214 timestamp -= next->ftrace_timestamp;
5216 for (index = next->curr_ret_stack; index >= 0; index--)
5217 next->ret_stack[index].calltime += timestamp;
5220 /* Allocate a return stack for each task */
5221 static int start_graph_tracing(void)
5223 struct ftrace_ret_stack **ret_stack_list;
5226 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5227 sizeof(struct ftrace_ret_stack *),
5230 if (!ret_stack_list)
5233 /* The cpu_boot init_task->ret_stack will never be freed */
5234 for_each_online_cpu(cpu) {
5235 if (!idle_task(cpu)->ret_stack)
5236 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5240 ret = alloc_retstack_tasklist(ret_stack_list);
5241 } while (ret == -EAGAIN);
5244 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5246 pr_info("ftrace_graph: Couldn't activate tracepoint"
5247 " probe to kernel_sched_switch\n");
5250 kfree(ret_stack_list);
5255 * Hibernation protection.
5256 * The state of the current task is too much unstable during
5257 * suspend/restore to disk. We want to protect against that.
5260 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5264 case PM_HIBERNATION_PREPARE:
5265 pause_graph_tracing();
5268 case PM_POST_HIBERNATION:
5269 unpause_graph_tracing();
5275 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5277 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5279 return __ftrace_graph_entry(trace);
5283 * The function graph tracer should only trace the functions defined
5284 * by set_ftrace_filter and set_ftrace_notrace. If another function
5285 * tracer ops is registered, the graph tracer requires testing the
5286 * function against the global ops, and not just trace any function
5287 * that any ftrace_ops registered.
5289 static void update_function_graph_func(void)
5291 if (ftrace_ops_list == &ftrace_list_end ||
5292 (ftrace_ops_list == &global_ops &&
5293 global_ops.next == &ftrace_list_end))
5294 ftrace_graph_entry = __ftrace_graph_entry;
5296 ftrace_graph_entry = ftrace_graph_entry_test;
5299 static struct notifier_block ftrace_suspend_notifier = {
5300 .notifier_call = ftrace_suspend_notifier_call,
5303 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5304 trace_func_graph_ent_t entryfunc)
5308 mutex_lock(&ftrace_lock);
5310 /* we currently allow only one tracer registered at a time */
5311 if (ftrace_graph_active) {
5316 register_pm_notifier(&ftrace_suspend_notifier);
5318 ftrace_graph_active++;
5319 ret = start_graph_tracing();
5321 ftrace_graph_active--;
5325 ftrace_graph_return = retfunc;
5328 * Update the indirect function to the entryfunc, and the
5329 * function that gets called to the entry_test first. Then
5330 * call the update fgraph entry function to determine if
5331 * the entryfunc should be called directly or not.
5333 __ftrace_graph_entry = entryfunc;
5334 ftrace_graph_entry = ftrace_graph_entry_test;
5335 update_function_graph_func();
5337 /* Function graph doesn't use the .func field of global_ops */
5338 global_ops.flags |= FTRACE_OPS_FL_STUB;
5340 #ifdef CONFIG_DYNAMIC_FTRACE
5341 /* Optimize function graph calling (if implemented by arch) */
5342 global_ops.trampoline = FTRACE_GRAPH_ADDR;
5345 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
5348 mutex_unlock(&ftrace_lock);
5352 void unregister_ftrace_graph(void)
5354 mutex_lock(&ftrace_lock);
5356 if (unlikely(!ftrace_graph_active))
5359 ftrace_graph_active--;
5360 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5361 ftrace_graph_entry = ftrace_graph_entry_stub;
5362 __ftrace_graph_entry = ftrace_graph_entry_stub;
5363 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5364 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
5365 #ifdef CONFIG_DYNAMIC_FTRACE
5366 global_ops.trampoline = 0;
5368 unregister_pm_notifier(&ftrace_suspend_notifier);
5369 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5372 mutex_unlock(&ftrace_lock);
5375 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5378 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5380 atomic_set(&t->tracing_graph_pause, 0);
5381 atomic_set(&t->trace_overrun, 0);
5382 t->ftrace_timestamp = 0;
5383 /* make curr_ret_stack visible before we add the ret_stack */
5385 t->ret_stack = ret_stack;
5389 * Allocate a return stack for the idle task. May be the first
5390 * time through, or it may be done by CPU hotplug online.
5392 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5394 t->curr_ret_stack = -1;
5396 * The idle task has no parent, it either has its own
5397 * stack or no stack at all.
5400 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5402 if (ftrace_graph_active) {
5403 struct ftrace_ret_stack *ret_stack;
5405 ret_stack = per_cpu(idle_ret_stack, cpu);
5407 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5408 * sizeof(struct ftrace_ret_stack),
5412 per_cpu(idle_ret_stack, cpu) = ret_stack;
5414 graph_init_task(t, ret_stack);
5418 /* Allocate a return stack for newly created task */
5419 void ftrace_graph_init_task(struct task_struct *t)
5421 /* Make sure we do not use the parent ret_stack */
5422 t->ret_stack = NULL;
5423 t->curr_ret_stack = -1;
5425 if (ftrace_graph_active) {
5426 struct ftrace_ret_stack *ret_stack;
5428 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5429 * sizeof(struct ftrace_ret_stack),
5433 graph_init_task(t, ret_stack);
5437 void ftrace_graph_exit_task(struct task_struct *t)
5439 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5441 t->ret_stack = NULL;
5442 /* NULL must become visible to IRQs before we free it: */
5448 void ftrace_graph_stop(void)