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 /* Current function tracing op */
84 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
85 /* What to set function_trace_op to */
86 static struct ftrace_ops *set_function_trace_op;
88 /* List for set_ftrace_pid's pids. */
89 LIST_HEAD(ftrace_pids);
91 struct list_head list;
96 * ftrace_disabled is set when an anomaly is discovered.
97 * ftrace_disabled is much stronger than ftrace_enabled.
99 static int ftrace_disabled __read_mostly;
101 static DEFINE_MUTEX(ftrace_lock);
103 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
104 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
105 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
106 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
107 static struct ftrace_ops global_ops;
108 static struct ftrace_ops control_ops;
110 #if ARCH_SUPPORTS_FTRACE_OPS
111 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
112 struct ftrace_ops *op, struct pt_regs *regs);
114 /* See comment below, where ftrace_ops_list_func is defined */
115 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
116 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
120 * Traverse the ftrace_global_list, invoking all entries. The reason that we
121 * can use rcu_dereference_raw_notrace() is that elements removed from this list
122 * are simply leaked, so there is no need to interact with a grace-period
123 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
124 * concurrent insertions into the ftrace_global_list.
126 * Silly Alpha and silly pointer-speculation compiler optimizations!
128 #define do_for_each_ftrace_op(op, list) \
129 op = rcu_dereference_raw_notrace(list); \
133 * Optimized for just a single item in the list (as that is the normal case).
135 #define while_for_each_ftrace_op(op) \
136 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
137 unlikely((op) != &ftrace_list_end))
139 static inline void ftrace_ops_init(struct ftrace_ops *ops)
141 #ifdef CONFIG_DYNAMIC_FTRACE
142 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
143 mutex_init(&ops->regex_lock);
144 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
150 * ftrace_nr_registered_ops - return number of ops registered
152 * Returns the number of ftrace_ops registered and tracing functions
154 int ftrace_nr_registered_ops(void)
156 struct ftrace_ops *ops;
159 mutex_lock(&ftrace_lock);
161 for (ops = ftrace_ops_list;
162 ops != &ftrace_list_end; ops = ops->next)
165 mutex_unlock(&ftrace_lock);
170 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
171 struct ftrace_ops *op, struct pt_regs *regs)
173 if (!test_tsk_trace_trace(current))
176 ftrace_pid_function(ip, parent_ip, op, regs);
179 static void set_ftrace_pid_function(ftrace_func_t func)
181 /* do not set ftrace_pid_function to itself! */
182 if (func != ftrace_pid_func)
183 ftrace_pid_function = func;
187 * clear_ftrace_function - reset the ftrace function
189 * This NULLs the ftrace function and in essence stops
190 * tracing. There may be lag
192 void clear_ftrace_function(void)
194 ftrace_trace_function = ftrace_stub;
195 ftrace_pid_function = ftrace_stub;
198 static void control_ops_disable_all(struct ftrace_ops *ops)
202 for_each_possible_cpu(cpu)
203 *per_cpu_ptr(ops->disabled, cpu) = 1;
206 static int control_ops_alloc(struct ftrace_ops *ops)
208 int __percpu *disabled;
210 disabled = alloc_percpu(int);
214 ops->disabled = disabled;
215 control_ops_disable_all(ops);
219 static void ftrace_sync(struct work_struct *work)
222 * This function is just a stub to implement a hard force
223 * of synchronize_sched(). This requires synchronizing
224 * tasks even in userspace and idle.
226 * Yes, function tracing is rude.
230 static void ftrace_sync_ipi(void *data)
232 /* Probably not needed, but do it anyway */
236 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
237 static void update_function_graph_func(void);
239 static inline void update_function_graph_func(void) { }
242 static void update_ftrace_function(void)
247 * If we are at the end of the list and this ops is
248 * recursion safe and not dynamic and the arch supports passing ops,
249 * then have the mcount trampoline call the function directly.
251 if (ftrace_ops_list == &ftrace_list_end ||
252 (ftrace_ops_list->next == &ftrace_list_end &&
253 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
254 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
255 !FTRACE_FORCE_LIST_FUNC)) {
256 /* Set the ftrace_ops that the arch callback uses */
257 set_function_trace_op = ftrace_ops_list;
258 func = ftrace_ops_list->func;
260 /* Just use the default ftrace_ops */
261 set_function_trace_op = &ftrace_list_end;
262 func = ftrace_ops_list_func;
265 update_function_graph_func();
267 /* If there's no change, then do nothing more here */
268 if (ftrace_trace_function == func)
272 * If we are using the list function, it doesn't care
273 * about the function_trace_ops.
275 if (func == ftrace_ops_list_func) {
276 ftrace_trace_function = func;
278 * Don't even bother setting function_trace_ops,
279 * it would be racy to do so anyway.
284 #ifndef CONFIG_DYNAMIC_FTRACE
286 * For static tracing, we need to be a bit more careful.
287 * The function change takes affect immediately. Thus,
288 * we need to coorditate the setting of the function_trace_ops
289 * with the setting of the ftrace_trace_function.
291 * Set the function to the list ops, which will call the
292 * function we want, albeit indirectly, but it handles the
293 * ftrace_ops and doesn't depend on function_trace_op.
295 ftrace_trace_function = ftrace_ops_list_func;
297 * Make sure all CPUs see this. Yes this is slow, but static
298 * tracing is slow and nasty to have enabled.
300 schedule_on_each_cpu(ftrace_sync);
301 /* Now all cpus are using the list ops. */
302 function_trace_op = set_function_trace_op;
303 /* Make sure the function_trace_op is visible on all CPUs */
305 /* Nasty way to force a rmb on all cpus */
306 smp_call_function(ftrace_sync_ipi, NULL, 1);
307 /* OK, we are all set to update the ftrace_trace_function now! */
308 #endif /* !CONFIG_DYNAMIC_FTRACE */
310 ftrace_trace_function = func;
313 int using_ftrace_ops_list_func(void)
315 return ftrace_trace_function == ftrace_ops_list_func;
318 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
322 * We are entering ops into the list but another
323 * CPU might be walking that list. We need to make sure
324 * the ops->next pointer is valid before another CPU sees
325 * the ops pointer included into the list.
327 rcu_assign_pointer(*list, ops);
330 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
332 struct ftrace_ops **p;
335 * If we are removing the last function, then simply point
336 * to the ftrace_stub.
338 if (*list == ops && ops->next == &ftrace_list_end) {
339 *list = &ftrace_list_end;
343 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
354 static void add_ftrace_list_ops(struct ftrace_ops **list,
355 struct ftrace_ops *main_ops,
356 struct ftrace_ops *ops)
358 int first = *list == &ftrace_list_end;
359 add_ftrace_ops(list, ops);
361 add_ftrace_ops(&ftrace_ops_list, main_ops);
364 static int remove_ftrace_list_ops(struct ftrace_ops **list,
365 struct ftrace_ops *main_ops,
366 struct ftrace_ops *ops)
368 int ret = remove_ftrace_ops(list, ops);
369 if (!ret && *list == &ftrace_list_end)
370 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
374 static int __register_ftrace_function(struct ftrace_ops *ops)
376 if (ops->flags & FTRACE_OPS_FL_DELETED)
379 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
382 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
384 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
385 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
386 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
388 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
389 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
392 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
393 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
396 if (!core_kernel_data((unsigned long)ops))
397 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
399 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
400 if (control_ops_alloc(ops))
402 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
404 add_ftrace_ops(&ftrace_ops_list, ops);
407 update_ftrace_function();
412 static int __unregister_ftrace_function(struct ftrace_ops *ops)
416 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
419 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
420 ret = remove_ftrace_list_ops(&ftrace_control_list,
423 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
429 update_ftrace_function();
434 static void ftrace_update_pid_func(void)
436 /* Only do something if we are tracing something */
437 if (ftrace_trace_function == ftrace_stub)
440 update_ftrace_function();
443 #ifdef CONFIG_FUNCTION_PROFILER
444 struct ftrace_profile {
445 struct hlist_node node;
447 unsigned long counter;
448 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
449 unsigned long long time;
450 unsigned long long time_squared;
454 struct ftrace_profile_page {
455 struct ftrace_profile_page *next;
457 struct ftrace_profile records[];
460 struct ftrace_profile_stat {
462 struct hlist_head *hash;
463 struct ftrace_profile_page *pages;
464 struct ftrace_profile_page *start;
465 struct tracer_stat stat;
468 #define PROFILE_RECORDS_SIZE \
469 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
471 #define PROFILES_PER_PAGE \
472 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
474 static int ftrace_profile_enabled __read_mostly;
476 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
477 static DEFINE_MUTEX(ftrace_profile_lock);
479 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
481 #define FTRACE_PROFILE_HASH_BITS 10
482 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
485 function_stat_next(void *v, int idx)
487 struct ftrace_profile *rec = v;
488 struct ftrace_profile_page *pg;
490 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
496 if ((void *)rec >= (void *)&pg->records[pg->index]) {
500 rec = &pg->records[0];
508 static void *function_stat_start(struct tracer_stat *trace)
510 struct ftrace_profile_stat *stat =
511 container_of(trace, struct ftrace_profile_stat, stat);
513 if (!stat || !stat->start)
516 return function_stat_next(&stat->start->records[0], 0);
519 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
520 /* function graph compares on total time */
521 static int function_stat_cmp(void *p1, void *p2)
523 struct ftrace_profile *a = p1;
524 struct ftrace_profile *b = p2;
526 if (a->time < b->time)
528 if (a->time > b->time)
534 /* not function graph compares against hits */
535 static int function_stat_cmp(void *p1, void *p2)
537 struct ftrace_profile *a = p1;
538 struct ftrace_profile *b = p2;
540 if (a->counter < b->counter)
542 if (a->counter > b->counter)
549 static int function_stat_headers(struct seq_file *m)
551 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
552 seq_printf(m, " Function "
555 "--- ---- --- ---\n");
557 seq_printf(m, " Function Hit\n"
563 static int function_stat_show(struct seq_file *m, void *v)
565 struct ftrace_profile *rec = v;
566 char str[KSYM_SYMBOL_LEN];
568 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
569 static struct trace_seq s;
570 unsigned long long avg;
571 unsigned long long stddev;
573 mutex_lock(&ftrace_profile_lock);
575 /* we raced with function_profile_reset() */
576 if (unlikely(rec->counter == 0)) {
581 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
582 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
584 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
587 do_div(avg, rec->counter);
589 /* Sample standard deviation (s^2) */
590 if (rec->counter <= 1)
594 * Apply Welford's method:
595 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
597 stddev = rec->counter * rec->time_squared -
598 rec->time * rec->time;
601 * Divide only 1000 for ns^2 -> us^2 conversion.
602 * trace_print_graph_duration will divide 1000 again.
604 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
608 trace_print_graph_duration(rec->time, &s);
609 trace_seq_puts(&s, " ");
610 trace_print_graph_duration(avg, &s);
611 trace_seq_puts(&s, " ");
612 trace_print_graph_duration(stddev, &s);
613 trace_print_seq(m, &s);
617 mutex_unlock(&ftrace_profile_lock);
622 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
624 struct ftrace_profile_page *pg;
626 pg = stat->pages = stat->start;
629 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
634 memset(stat->hash, 0,
635 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
638 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
640 struct ftrace_profile_page *pg;
645 /* If we already allocated, do nothing */
649 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
653 #ifdef CONFIG_DYNAMIC_FTRACE
654 functions = ftrace_update_tot_cnt;
657 * We do not know the number of functions that exist because
658 * dynamic tracing is what counts them. With past experience
659 * we have around 20K functions. That should be more than enough.
660 * It is highly unlikely we will execute every function in
666 pg = stat->start = stat->pages;
668 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
670 for (i = 1; i < pages; i++) {
671 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
682 unsigned long tmp = (unsigned long)pg;
694 static int ftrace_profile_init_cpu(int cpu)
696 struct ftrace_profile_stat *stat;
699 stat = &per_cpu(ftrace_profile_stats, cpu);
702 /* If the profile is already created, simply reset it */
703 ftrace_profile_reset(stat);
708 * We are profiling all functions, but usually only a few thousand
709 * functions are hit. We'll make a hash of 1024 items.
711 size = FTRACE_PROFILE_HASH_SIZE;
713 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
718 /* Preallocate the function profiling pages */
719 if (ftrace_profile_pages_init(stat) < 0) {
728 static int ftrace_profile_init(void)
733 for_each_possible_cpu(cpu) {
734 ret = ftrace_profile_init_cpu(cpu);
742 /* interrupts must be disabled */
743 static struct ftrace_profile *
744 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
746 struct ftrace_profile *rec;
747 struct hlist_head *hhd;
750 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
751 hhd = &stat->hash[key];
753 if (hlist_empty(hhd))
756 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
764 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
765 struct ftrace_profile *rec)
769 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
770 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
774 * The memory is already allocated, this simply finds a new record to use.
776 static struct ftrace_profile *
777 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
779 struct ftrace_profile *rec = NULL;
781 /* prevent recursion (from NMIs) */
782 if (atomic_inc_return(&stat->disabled) != 1)
786 * Try to find the function again since an NMI
787 * could have added it
789 rec = ftrace_find_profiled_func(stat, ip);
793 if (stat->pages->index == PROFILES_PER_PAGE) {
794 if (!stat->pages->next)
796 stat->pages = stat->pages->next;
799 rec = &stat->pages->records[stat->pages->index++];
801 ftrace_add_profile(stat, rec);
804 atomic_dec(&stat->disabled);
810 function_profile_call(unsigned long ip, unsigned long parent_ip,
811 struct ftrace_ops *ops, struct pt_regs *regs)
813 struct ftrace_profile_stat *stat;
814 struct ftrace_profile *rec;
817 if (!ftrace_profile_enabled)
820 local_irq_save(flags);
822 stat = this_cpu_ptr(&ftrace_profile_stats);
823 if (!stat->hash || !ftrace_profile_enabled)
826 rec = ftrace_find_profiled_func(stat, ip);
828 rec = ftrace_profile_alloc(stat, ip);
835 local_irq_restore(flags);
838 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
839 static int profile_graph_entry(struct ftrace_graph_ent *trace)
841 function_profile_call(trace->func, 0, NULL, NULL);
845 static void profile_graph_return(struct ftrace_graph_ret *trace)
847 struct ftrace_profile_stat *stat;
848 unsigned long long calltime;
849 struct ftrace_profile *rec;
852 local_irq_save(flags);
853 stat = this_cpu_ptr(&ftrace_profile_stats);
854 if (!stat->hash || !ftrace_profile_enabled)
857 /* If the calltime was zero'd ignore it */
858 if (!trace->calltime)
861 calltime = trace->rettime - trace->calltime;
863 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
866 index = trace->depth;
868 /* Append this call time to the parent time to subtract */
870 current->ret_stack[index - 1].subtime += calltime;
872 if (current->ret_stack[index].subtime < calltime)
873 calltime -= current->ret_stack[index].subtime;
878 rec = ftrace_find_profiled_func(stat, trace->func);
880 rec->time += calltime;
881 rec->time_squared += calltime * calltime;
885 local_irq_restore(flags);
888 static int register_ftrace_profiler(void)
890 return register_ftrace_graph(&profile_graph_return,
891 &profile_graph_entry);
894 static void unregister_ftrace_profiler(void)
896 unregister_ftrace_graph();
899 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
900 .func = function_profile_call,
901 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
902 INIT_REGEX_LOCK(ftrace_profile_ops)
905 static int register_ftrace_profiler(void)
907 return register_ftrace_function(&ftrace_profile_ops);
910 static void unregister_ftrace_profiler(void)
912 unregister_ftrace_function(&ftrace_profile_ops);
914 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
917 ftrace_profile_write(struct file *filp, const char __user *ubuf,
918 size_t cnt, loff_t *ppos)
923 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
929 mutex_lock(&ftrace_profile_lock);
930 if (ftrace_profile_enabled ^ val) {
932 ret = ftrace_profile_init();
938 ret = register_ftrace_profiler();
943 ftrace_profile_enabled = 1;
945 ftrace_profile_enabled = 0;
947 * unregister_ftrace_profiler calls stop_machine
948 * so this acts like an synchronize_sched.
950 unregister_ftrace_profiler();
954 mutex_unlock(&ftrace_profile_lock);
962 ftrace_profile_read(struct file *filp, char __user *ubuf,
963 size_t cnt, loff_t *ppos)
965 char buf[64]; /* big enough to hold a number */
968 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
969 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
972 static const struct file_operations ftrace_profile_fops = {
973 .open = tracing_open_generic,
974 .read = ftrace_profile_read,
975 .write = ftrace_profile_write,
976 .llseek = default_llseek,
979 /* used to initialize the real stat files */
980 static struct tracer_stat function_stats __initdata = {
982 .stat_start = function_stat_start,
983 .stat_next = function_stat_next,
984 .stat_cmp = function_stat_cmp,
985 .stat_headers = function_stat_headers,
986 .stat_show = function_stat_show
989 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
991 struct ftrace_profile_stat *stat;
992 struct dentry *entry;
997 for_each_possible_cpu(cpu) {
998 stat = &per_cpu(ftrace_profile_stats, cpu);
1000 /* allocate enough for function name + cpu number */
1001 name = kmalloc(32, GFP_KERNEL);
1004 * The files created are permanent, if something happens
1005 * we still do not free memory.
1008 "Could not allocate stat file for cpu %d\n",
1012 stat->stat = function_stats;
1013 snprintf(name, 32, "function%d", cpu);
1014 stat->stat.name = name;
1015 ret = register_stat_tracer(&stat->stat);
1018 "Could not register function stat for cpu %d\n",
1025 entry = debugfs_create_file("function_profile_enabled", 0644,
1026 d_tracer, NULL, &ftrace_profile_fops);
1028 pr_warning("Could not create debugfs "
1029 "'function_profile_enabled' entry\n");
1032 #else /* CONFIG_FUNCTION_PROFILER */
1033 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1036 #endif /* CONFIG_FUNCTION_PROFILER */
1038 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1040 #ifdef CONFIG_DYNAMIC_FTRACE
1042 static struct ftrace_ops *removed_ops;
1044 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1045 # error Dynamic ftrace depends on MCOUNT_RECORD
1048 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1050 struct ftrace_func_probe {
1051 struct hlist_node node;
1052 struct ftrace_probe_ops *ops;
1053 unsigned long flags;
1056 struct list_head free_list;
1059 struct ftrace_func_entry {
1060 struct hlist_node hlist;
1064 struct ftrace_hash {
1065 unsigned long size_bits;
1066 struct hlist_head *buckets;
1067 unsigned long count;
1068 struct rcu_head rcu;
1072 * We make these constant because no one should touch them,
1073 * but they are used as the default "empty hash", to avoid allocating
1074 * it all the time. These are in a read only section such that if
1075 * anyone does try to modify it, it will cause an exception.
1077 static const struct hlist_head empty_buckets[1];
1078 static const struct ftrace_hash empty_hash = {
1079 .buckets = (struct hlist_head *)empty_buckets,
1081 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1083 static struct ftrace_ops global_ops = {
1084 .func = ftrace_stub,
1085 .notrace_hash = EMPTY_HASH,
1086 .filter_hash = EMPTY_HASH,
1087 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1088 INIT_REGEX_LOCK(global_ops)
1091 struct ftrace_page {
1092 struct ftrace_page *next;
1093 struct dyn_ftrace *records;
1098 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1099 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1101 /* estimate from running different kernels */
1102 #define NR_TO_INIT 10000
1104 static struct ftrace_page *ftrace_pages_start;
1105 static struct ftrace_page *ftrace_pages;
1107 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1109 return !hash || !hash->count;
1112 static struct ftrace_func_entry *
1113 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1116 struct ftrace_func_entry *entry;
1117 struct hlist_head *hhd;
1119 if (ftrace_hash_empty(hash))
1122 if (hash->size_bits > 0)
1123 key = hash_long(ip, hash->size_bits);
1127 hhd = &hash->buckets[key];
1129 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1130 if (entry->ip == ip)
1136 static void __add_hash_entry(struct ftrace_hash *hash,
1137 struct ftrace_func_entry *entry)
1139 struct hlist_head *hhd;
1142 if (hash->size_bits)
1143 key = hash_long(entry->ip, hash->size_bits);
1147 hhd = &hash->buckets[key];
1148 hlist_add_head(&entry->hlist, hhd);
1152 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1154 struct ftrace_func_entry *entry;
1156 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1161 __add_hash_entry(hash, entry);
1167 free_hash_entry(struct ftrace_hash *hash,
1168 struct ftrace_func_entry *entry)
1170 hlist_del(&entry->hlist);
1176 remove_hash_entry(struct ftrace_hash *hash,
1177 struct ftrace_func_entry *entry)
1179 hlist_del(&entry->hlist);
1183 static void ftrace_hash_clear(struct ftrace_hash *hash)
1185 struct hlist_head *hhd;
1186 struct hlist_node *tn;
1187 struct ftrace_func_entry *entry;
1188 int size = 1 << hash->size_bits;
1194 for (i = 0; i < size; i++) {
1195 hhd = &hash->buckets[i];
1196 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1197 free_hash_entry(hash, entry);
1199 FTRACE_WARN_ON(hash->count);
1202 static void free_ftrace_hash(struct ftrace_hash *hash)
1204 if (!hash || hash == EMPTY_HASH)
1206 ftrace_hash_clear(hash);
1207 kfree(hash->buckets);
1211 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1213 struct ftrace_hash *hash;
1215 hash = container_of(rcu, struct ftrace_hash, rcu);
1216 free_ftrace_hash(hash);
1219 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1221 if (!hash || hash == EMPTY_HASH)
1223 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1226 void ftrace_free_filter(struct ftrace_ops *ops)
1228 ftrace_ops_init(ops);
1229 free_ftrace_hash(ops->filter_hash);
1230 free_ftrace_hash(ops->notrace_hash);
1233 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1235 struct ftrace_hash *hash;
1238 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1242 size = 1 << size_bits;
1243 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1245 if (!hash->buckets) {
1250 hash->size_bits = size_bits;
1255 static struct ftrace_hash *
1256 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1258 struct ftrace_func_entry *entry;
1259 struct ftrace_hash *new_hash;
1264 new_hash = alloc_ftrace_hash(size_bits);
1269 if (ftrace_hash_empty(hash))
1272 size = 1 << hash->size_bits;
1273 for (i = 0; i < size; i++) {
1274 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1275 ret = add_hash_entry(new_hash, entry->ip);
1281 FTRACE_WARN_ON(new_hash->count != hash->count);
1286 free_ftrace_hash(new_hash);
1291 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1293 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1296 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1297 struct ftrace_hash **dst, struct ftrace_hash *src)
1299 struct ftrace_func_entry *entry;
1300 struct hlist_node *tn;
1301 struct hlist_head *hhd;
1302 struct ftrace_hash *old_hash;
1303 struct ftrace_hash *new_hash;
1304 int size = src->count;
1309 * If the new source is empty, just free dst and assign it
1313 new_hash = EMPTY_HASH;
1318 * Make the hash size about 1/2 the # found
1320 for (size /= 2; size; size >>= 1)
1323 /* Don't allocate too much */
1324 if (bits > FTRACE_HASH_MAX_BITS)
1325 bits = FTRACE_HASH_MAX_BITS;
1327 new_hash = alloc_ftrace_hash(bits);
1331 size = 1 << src->size_bits;
1332 for (i = 0; i < size; i++) {
1333 hhd = &src->buckets[i];
1334 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1335 remove_hash_entry(src, entry);
1336 __add_hash_entry(new_hash, entry);
1342 * Remove the current set, update the hash and add
1345 ftrace_hash_rec_disable(ops, enable);
1348 rcu_assign_pointer(*dst, new_hash);
1349 free_ftrace_hash_rcu(old_hash);
1351 ftrace_hash_rec_enable(ops, enable);
1357 * Test the hashes for this ops to see if we want to call
1358 * the ops->func or not.
1360 * It's a match if the ip is in the ops->filter_hash or
1361 * the filter_hash does not exist or is empty,
1363 * the ip is not in the ops->notrace_hash.
1365 * This needs to be called with preemption disabled as
1366 * the hashes are freed with call_rcu_sched().
1369 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1371 struct ftrace_hash *filter_hash;
1372 struct ftrace_hash *notrace_hash;
1375 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1377 * There's a small race when adding ops that the ftrace handler
1378 * that wants regs, may be called without them. We can not
1379 * allow that handler to be called if regs is NULL.
1381 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1385 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1386 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1388 if ((ftrace_hash_empty(filter_hash) ||
1389 ftrace_lookup_ip(filter_hash, ip)) &&
1390 (ftrace_hash_empty(notrace_hash) ||
1391 !ftrace_lookup_ip(notrace_hash, ip)))
1400 * This is a double for. Do not use 'break' to break out of the loop,
1401 * you must use a goto.
1403 #define do_for_each_ftrace_rec(pg, rec) \
1404 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1406 for (_____i = 0; _____i < pg->index; _____i++) { \
1407 rec = &pg->records[_____i];
1409 #define while_for_each_ftrace_rec() \
1414 static int ftrace_cmp_recs(const void *a, const void *b)
1416 const struct dyn_ftrace *key = a;
1417 const struct dyn_ftrace *rec = b;
1419 if (key->flags < rec->ip)
1421 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1426 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1428 struct ftrace_page *pg;
1429 struct dyn_ftrace *rec;
1430 struct dyn_ftrace key;
1433 key.flags = end; /* overload flags, as it is unsigned long */
1435 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1436 if (end < pg->records[0].ip ||
1437 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1439 rec = bsearch(&key, pg->records, pg->index,
1440 sizeof(struct dyn_ftrace),
1450 * ftrace_location - return true if the ip giving is a traced location
1451 * @ip: the instruction pointer to check
1453 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1454 * That is, the instruction that is either a NOP or call to
1455 * the function tracer. It checks the ftrace internal tables to
1456 * determine if the address belongs or not.
1458 unsigned long ftrace_location(unsigned long ip)
1460 return ftrace_location_range(ip, ip);
1464 * ftrace_text_reserved - return true if range contains an ftrace location
1465 * @start: start of range to search
1466 * @end: end of range to search (inclusive). @end points to the last byte to check.
1468 * Returns 1 if @start and @end contains a ftrace location.
1469 * That is, the instruction that is either a NOP or call to
1470 * the function tracer. It checks the ftrace internal tables to
1471 * determine if the address belongs or not.
1473 int ftrace_text_reserved(const void *start, const void *end)
1477 ret = ftrace_location_range((unsigned long)start,
1478 (unsigned long)end);
1483 /* Test if ops registered to this rec needs regs */
1484 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1486 struct ftrace_ops *ops;
1487 bool keep_regs = false;
1489 for (ops = ftrace_ops_list;
1490 ops != &ftrace_list_end; ops = ops->next) {
1491 /* pass rec in as regs to have non-NULL val */
1492 if (ftrace_ops_test(ops, rec->ip, rec)) {
1493 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1503 static void ftrace_remove_tramp(struct ftrace_ops *ops,
1504 struct dyn_ftrace *rec)
1506 struct ftrace_func_entry *entry;
1508 entry = ftrace_lookup_ip(ops->tramp_hash, rec->ip);
1513 * The tramp_hash entry will be removed at time
1516 ops->nr_trampolines--;
1517 rec->flags &= ~FTRACE_FL_TRAMP;
1520 static void ftrace_clear_tramps(struct dyn_ftrace *rec)
1522 struct ftrace_ops *op;
1524 do_for_each_ftrace_op(op, ftrace_ops_list) {
1525 if (op->nr_trampolines)
1526 ftrace_remove_tramp(op, rec);
1527 } while_for_each_ftrace_op(op);
1530 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1534 struct ftrace_hash *hash;
1535 struct ftrace_hash *other_hash;
1536 struct ftrace_page *pg;
1537 struct dyn_ftrace *rec;
1541 /* Only update if the ops has been registered */
1542 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1546 * In the filter_hash case:
1547 * If the count is zero, we update all records.
1548 * Otherwise we just update the items in the hash.
1550 * In the notrace_hash case:
1551 * We enable the update in the hash.
1552 * As disabling notrace means enabling the tracing,
1553 * and enabling notrace means disabling, the inc variable
1557 hash = ops->filter_hash;
1558 other_hash = ops->notrace_hash;
1559 if (ftrace_hash_empty(hash))
1563 hash = ops->notrace_hash;
1564 other_hash = ops->filter_hash;
1566 * If the notrace hash has no items,
1567 * then there's nothing to do.
1569 if (ftrace_hash_empty(hash))
1573 do_for_each_ftrace_rec(pg, rec) {
1574 int in_other_hash = 0;
1580 * Only the filter_hash affects all records.
1581 * Update if the record is not in the notrace hash.
1583 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1586 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1587 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1590 * If filter_hash is set, we want to match all functions
1591 * that are in the hash but not in the other hash.
1593 * If filter_hash is not set, then we are decrementing.
1594 * That means we match anything that is in the hash
1595 * and also in the other_hash. That is, we need to turn
1596 * off functions in the other hash because they are disabled
1599 if (filter_hash && in_hash && !in_other_hash)
1601 else if (!filter_hash && in_hash &&
1602 (in_other_hash || ftrace_hash_empty(other_hash)))
1610 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1614 * If there's only a single callback registered to a
1615 * function, and the ops has a trampoline registered
1616 * for it, then we can call it directly.
1618 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1619 rec->flags |= FTRACE_FL_TRAMP;
1620 ops->nr_trampolines++;
1623 * If we are adding another function callback
1624 * to this function, and the previous had a
1625 * trampoline used, then we need to go back to
1626 * the default trampoline.
1628 rec->flags &= ~FTRACE_FL_TRAMP;
1630 /* remove trampolines from any ops for this rec */
1631 ftrace_clear_tramps(rec);
1635 * If any ops wants regs saved for this function
1636 * then all ops will get saved regs.
1638 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1639 rec->flags |= FTRACE_FL_REGS;
1641 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1645 if (ops->trampoline && !ftrace_rec_count(rec))
1646 ftrace_remove_tramp(ops, rec);
1649 * If the rec had REGS enabled and the ops that is
1650 * being removed had REGS set, then see if there is
1651 * still any ops for this record that wants regs.
1652 * If not, we can stop recording them.
1654 if (ftrace_rec_count(rec) > 0 &&
1655 rec->flags & FTRACE_FL_REGS &&
1656 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1657 if (!test_rec_ops_needs_regs(rec))
1658 rec->flags &= ~FTRACE_FL_REGS;
1662 * flags will be cleared in ftrace_check_record()
1663 * if rec count is zero.
1667 /* Shortcut, if we handled all records, we are done. */
1668 if (!all && count == hash->count)
1670 } while_for_each_ftrace_rec();
1673 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1676 __ftrace_hash_rec_update(ops, filter_hash, 0);
1679 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1682 __ftrace_hash_rec_update(ops, filter_hash, 1);
1685 static void print_ip_ins(const char *fmt, unsigned char *p)
1689 printk(KERN_CONT "%s", fmt);
1691 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1692 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1696 * ftrace_bug - report and shutdown function tracer
1697 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1698 * @ip: The address that failed
1700 * The arch code that enables or disables the function tracing
1701 * can call ftrace_bug() when it has detected a problem in
1702 * modifying the code. @failed should be one of either:
1703 * EFAULT - if the problem happens on reading the @ip address
1704 * EINVAL - if what is read at @ip is not what was expected
1705 * EPERM - if the problem happens on writting to the @ip address
1707 void ftrace_bug(int failed, unsigned long ip)
1711 FTRACE_WARN_ON_ONCE(1);
1712 pr_info("ftrace faulted on modifying ");
1716 FTRACE_WARN_ON_ONCE(1);
1717 pr_info("ftrace failed to modify ");
1719 print_ip_ins(" actual: ", (unsigned char *)ip);
1720 printk(KERN_CONT "\n");
1723 FTRACE_WARN_ON_ONCE(1);
1724 pr_info("ftrace faulted on writing ");
1728 FTRACE_WARN_ON_ONCE(1);
1729 pr_info("ftrace faulted on unknown error ");
1734 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1736 unsigned long flag = 0UL;
1739 * If we are updating calls:
1741 * If the record has a ref count, then we need to enable it
1742 * because someone is using it.
1744 * Otherwise we make sure its disabled.
1746 * If we are disabling calls, then disable all records that
1749 if (enable && ftrace_rec_count(rec))
1750 flag = FTRACE_FL_ENABLED;
1753 * If enabling and the REGS flag does not match the REGS_EN, or
1754 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1755 * this record. Set flags to fail the compare against ENABLED.
1758 if (!(rec->flags & FTRACE_FL_REGS) !=
1759 !(rec->flags & FTRACE_FL_REGS_EN))
1760 flag |= FTRACE_FL_REGS;
1762 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1763 !(rec->flags & FTRACE_FL_TRAMP_EN))
1764 flag |= FTRACE_FL_TRAMP;
1767 /* If the state of this record hasn't changed, then do nothing */
1768 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1769 return FTRACE_UPDATE_IGNORE;
1772 /* Save off if rec is being enabled (for return value) */
1773 flag ^= rec->flags & FTRACE_FL_ENABLED;
1776 rec->flags |= FTRACE_FL_ENABLED;
1777 if (flag & FTRACE_FL_REGS) {
1778 if (rec->flags & FTRACE_FL_REGS)
1779 rec->flags |= FTRACE_FL_REGS_EN;
1781 rec->flags &= ~FTRACE_FL_REGS_EN;
1783 if (flag & FTRACE_FL_TRAMP) {
1784 if (rec->flags & FTRACE_FL_TRAMP)
1785 rec->flags |= FTRACE_FL_TRAMP_EN;
1787 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1792 * If this record is being updated from a nop, then
1793 * return UPDATE_MAKE_CALL.
1795 * return UPDATE_MODIFY_CALL to tell the caller to convert
1796 * from the save regs, to a non-save regs function or
1797 * vice versa, or from a trampoline call.
1799 if (flag & FTRACE_FL_ENABLED)
1800 return FTRACE_UPDATE_MAKE_CALL;
1802 return FTRACE_UPDATE_MODIFY_CALL;
1806 /* If there's no more users, clear all flags */
1807 if (!ftrace_rec_count(rec))
1810 /* Just disable the record (keep REGS state) */
1811 rec->flags &= ~FTRACE_FL_ENABLED;
1814 return FTRACE_UPDATE_MAKE_NOP;
1818 * ftrace_update_record, set a record that now is tracing or not
1819 * @rec: the record to update
1820 * @enable: set to 1 if the record is tracing, zero to force disable
1822 * The records that represent all functions that can be traced need
1823 * to be updated when tracing has been enabled.
1825 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1827 return ftrace_check_record(rec, enable, 1);
1831 * ftrace_test_record, check if the record has been enabled or not
1832 * @rec: the record to test
1833 * @enable: set to 1 to check if enabled, 0 if it is disabled
1835 * The arch code may need to test if a record is already set to
1836 * tracing to determine how to modify the function code that it
1839 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1841 return ftrace_check_record(rec, enable, 0);
1844 static struct ftrace_ops *
1845 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1847 struct ftrace_ops *op;
1849 /* Removed ops need to be tested first */
1850 if (removed_ops && removed_ops->tramp_hash) {
1851 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1855 do_for_each_ftrace_op(op, ftrace_ops_list) {
1856 if (!op->tramp_hash)
1859 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1862 } while_for_each_ftrace_op(op);
1867 static struct ftrace_ops *
1868 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1870 struct ftrace_ops *op;
1872 do_for_each_ftrace_op(op, ftrace_ops_list) {
1873 /* pass rec in as regs to have non-NULL val */
1874 if (ftrace_ops_test(op, rec->ip, rec))
1876 } while_for_each_ftrace_op(op);
1882 * ftrace_get_addr_new - Get the call address to set to
1883 * @rec: The ftrace record descriptor
1885 * If the record has the FTRACE_FL_REGS set, that means that it
1886 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1887 * is not not set, then it wants to convert to the normal callback.
1889 * Returns the address of the trampoline to set to
1891 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1893 struct ftrace_ops *ops;
1895 /* Trampolines take precedence over regs */
1896 if (rec->flags & FTRACE_FL_TRAMP) {
1897 ops = ftrace_find_tramp_ops_new(rec);
1898 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1899 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1900 (void *)rec->ip, (void *)rec->ip);
1901 /* Ftrace is shutting down, return anything */
1902 return (unsigned long)FTRACE_ADDR;
1904 return ops->trampoline;
1907 if (rec->flags & FTRACE_FL_REGS)
1908 return (unsigned long)FTRACE_REGS_ADDR;
1910 return (unsigned long)FTRACE_ADDR;
1914 * ftrace_get_addr_curr - Get the call address that is already there
1915 * @rec: The ftrace record descriptor
1917 * The FTRACE_FL_REGS_EN is set when the record already points to
1918 * a function that saves all the regs. Basically the '_EN' version
1919 * represents the current state of the function.
1921 * Returns the address of the trampoline that is currently being called
1923 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1925 struct ftrace_ops *ops;
1927 /* Trampolines take precedence over regs */
1928 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1929 ops = ftrace_find_tramp_ops_curr(rec);
1930 if (FTRACE_WARN_ON(!ops)) {
1931 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1932 (void *)rec->ip, (void *)rec->ip);
1933 /* Ftrace is shutting down, return anything */
1934 return (unsigned long)FTRACE_ADDR;
1936 return ops->trampoline;
1939 if (rec->flags & FTRACE_FL_REGS_EN)
1940 return (unsigned long)FTRACE_REGS_ADDR;
1942 return (unsigned long)FTRACE_ADDR;
1946 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1948 unsigned long ftrace_old_addr;
1949 unsigned long ftrace_addr;
1952 ftrace_addr = ftrace_get_addr_new(rec);
1954 /* This needs to be done before we call ftrace_update_record */
1955 ftrace_old_addr = ftrace_get_addr_curr(rec);
1957 ret = ftrace_update_record(rec, enable);
1960 case FTRACE_UPDATE_IGNORE:
1963 case FTRACE_UPDATE_MAKE_CALL:
1964 return ftrace_make_call(rec, ftrace_addr);
1966 case FTRACE_UPDATE_MAKE_NOP:
1967 return ftrace_make_nop(NULL, rec, ftrace_addr);
1969 case FTRACE_UPDATE_MODIFY_CALL:
1970 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1973 return -1; /* unknow ftrace bug */
1976 void __weak ftrace_replace_code(int enable)
1978 struct dyn_ftrace *rec;
1979 struct ftrace_page *pg;
1982 if (unlikely(ftrace_disabled))
1985 do_for_each_ftrace_rec(pg, rec) {
1986 failed = __ftrace_replace_code(rec, enable);
1988 ftrace_bug(failed, rec->ip);
1989 /* Stop processing */
1992 } while_for_each_ftrace_rec();
1995 struct ftrace_rec_iter {
1996 struct ftrace_page *pg;
2001 * ftrace_rec_iter_start, start up iterating over traced functions
2003 * Returns an iterator handle that is used to iterate over all
2004 * the records that represent address locations where functions
2007 * May return NULL if no records are available.
2009 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2012 * We only use a single iterator.
2013 * Protected by the ftrace_lock mutex.
2015 static struct ftrace_rec_iter ftrace_rec_iter;
2016 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2018 iter->pg = ftrace_pages_start;
2021 /* Could have empty pages */
2022 while (iter->pg && !iter->pg->index)
2023 iter->pg = iter->pg->next;
2032 * ftrace_rec_iter_next, get the next record to process.
2033 * @iter: The handle to the iterator.
2035 * Returns the next iterator after the given iterator @iter.
2037 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2041 if (iter->index >= iter->pg->index) {
2042 iter->pg = iter->pg->next;
2045 /* Could have empty pages */
2046 while (iter->pg && !iter->pg->index)
2047 iter->pg = iter->pg->next;
2057 * ftrace_rec_iter_record, get the record at the iterator location
2058 * @iter: The current iterator location
2060 * Returns the record that the current @iter is at.
2062 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2064 return &iter->pg->records[iter->index];
2068 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2075 if (unlikely(ftrace_disabled))
2078 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2080 ftrace_bug(ret, ip);
2087 * archs can override this function if they must do something
2088 * before the modifying code is performed.
2090 int __weak ftrace_arch_code_modify_prepare(void)
2096 * archs can override this function if they must do something
2097 * after the modifying code is performed.
2099 int __weak ftrace_arch_code_modify_post_process(void)
2104 void ftrace_modify_all_code(int command)
2106 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2110 * If the ftrace_caller calls a ftrace_ops func directly,
2111 * we need to make sure that it only traces functions it
2112 * expects to trace. When doing the switch of functions,
2113 * we need to update to the ftrace_ops_list_func first
2114 * before the transition between old and new calls are set,
2115 * as the ftrace_ops_list_func will check the ops hashes
2116 * to make sure the ops are having the right functions
2120 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2121 if (FTRACE_WARN_ON(err))
2125 if (command & FTRACE_UPDATE_CALLS)
2126 ftrace_replace_code(1);
2127 else if (command & FTRACE_DISABLE_CALLS)
2128 ftrace_replace_code(0);
2130 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2131 function_trace_op = set_function_trace_op;
2133 /* If irqs are disabled, we are in stop machine */
2134 if (!irqs_disabled())
2135 smp_call_function(ftrace_sync_ipi, NULL, 1);
2136 err = ftrace_update_ftrace_func(ftrace_trace_function);
2137 if (FTRACE_WARN_ON(err))
2141 if (command & FTRACE_START_FUNC_RET)
2142 err = ftrace_enable_ftrace_graph_caller();
2143 else if (command & FTRACE_STOP_FUNC_RET)
2144 err = ftrace_disable_ftrace_graph_caller();
2145 FTRACE_WARN_ON(err);
2148 static int __ftrace_modify_code(void *data)
2150 int *command = data;
2152 ftrace_modify_all_code(*command);
2158 * ftrace_run_stop_machine, go back to the stop machine method
2159 * @command: The command to tell ftrace what to do
2161 * If an arch needs to fall back to the stop machine method, the
2162 * it can call this function.
2164 void ftrace_run_stop_machine(int command)
2166 stop_machine(__ftrace_modify_code, &command, NULL);
2170 * arch_ftrace_update_code, modify the code to trace or not trace
2171 * @command: The command that needs to be done
2173 * Archs can override this function if it does not need to
2174 * run stop_machine() to modify code.
2176 void __weak arch_ftrace_update_code(int command)
2178 ftrace_run_stop_machine(command);
2181 static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2183 struct ftrace_page *pg;
2184 struct dyn_ftrace *rec;
2188 size = ops->nr_trampolines;
2191 * Make the hash size about 1/2 the # found
2193 for (size /= 2; size; size >>= 1)
2196 ops->tramp_hash = alloc_ftrace_hash(bits);
2198 * TODO: a failed allocation is going to screw up
2199 * the accounting of what needs to be modified
2200 * and not. For now, we kill ftrace if we fail
2201 * to allocate here. But there are ways around this,
2202 * but that will take a little more work.
2204 if (!ops->tramp_hash)
2207 do_for_each_ftrace_rec(pg, rec) {
2208 if (ftrace_rec_count(rec) == 1 &&
2209 ftrace_ops_test(ops, rec->ip, rec)) {
2212 * If another ops adds to a rec, the rec will
2213 * lose its trampoline and never get it back
2214 * until all ops are off of it.
2216 if (!(rec->flags & FTRACE_FL_TRAMP))
2219 /* This record had better have a trampoline */
2220 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2223 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2227 } while_for_each_ftrace_rec();
2229 /* The number of recs in the hash must match nr_trampolines */
2230 FTRACE_WARN_ON(ops->tramp_hash->count != ops->nr_trampolines);
2235 static int ftrace_save_tramp_hashes(void)
2237 struct ftrace_ops *op;
2241 * Now that any trampoline is being used, we need to save the
2242 * hashes for the ops that have them. This allows the mapping
2243 * back from the record to the ops that has the trampoline to
2244 * know what code is being replaced. Modifying code must always
2245 * verify what it is changing.
2247 do_for_each_ftrace_op(op, ftrace_ops_list) {
2249 /* The tramp_hash is recreated each time. */
2250 free_ftrace_hash(op->tramp_hash);
2251 op->tramp_hash = NULL;
2253 if (op->nr_trampolines) {
2254 ret = ftrace_save_ops_tramp_hash(op);
2259 } while_for_each_ftrace_op(op);
2264 static void ftrace_run_update_code(int command)
2268 ret = ftrace_arch_code_modify_prepare();
2269 FTRACE_WARN_ON(ret);
2274 * By default we use stop_machine() to modify the code.
2275 * But archs can do what ever they want as long as it
2276 * is safe. The stop_machine() is the safest, but also
2277 * produces the most overhead.
2279 arch_ftrace_update_code(command);
2281 ret = ftrace_arch_code_modify_post_process();
2282 FTRACE_WARN_ON(ret);
2284 ret = ftrace_save_tramp_hashes();
2285 FTRACE_WARN_ON(ret);
2288 static ftrace_func_t saved_ftrace_func;
2289 static int ftrace_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 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2355 command |= FTRACE_UPDATE_CALLS;
2357 if (saved_ftrace_func != ftrace_trace_function) {
2358 saved_ftrace_func = ftrace_trace_function;
2359 command |= FTRACE_UPDATE_TRACE_FUNC;
2362 if (!command || !ftrace_enabled) {
2364 * If these are control ops, they still need their
2365 * per_cpu field freed. Since, function tracing is
2366 * not currently active, we can just free them
2367 * without synchronizing all CPUs.
2369 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2370 control_ops_free(ops);
2375 * If the ops uses a trampoline, then it needs to be
2376 * tested first on update.
2380 ftrace_run_update_code(command);
2385 * Dynamic ops may be freed, we must make sure that all
2386 * callers are done before leaving this function.
2387 * The same goes for freeing the per_cpu data of the control
2390 * Again, normal synchronize_sched() is not good enough.
2391 * We need to do a hard force of sched synchronization.
2392 * This is because we use preempt_disable() to do RCU, but
2393 * the function tracers can be called where RCU is not watching
2394 * (like before user_exit()). We can not rely on the RCU
2395 * infrastructure to do the synchronization, thus we must do it
2398 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2399 schedule_on_each_cpu(ftrace_sync);
2401 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2402 control_ops_free(ops);
2408 static void ftrace_startup_sysctl(void)
2410 if (unlikely(ftrace_disabled))
2413 /* Force update next time */
2414 saved_ftrace_func = NULL;
2415 /* ftrace_start_up is true if we want ftrace running */
2416 if (ftrace_start_up)
2417 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2420 static void ftrace_shutdown_sysctl(void)
2422 if (unlikely(ftrace_disabled))
2425 /* ftrace_start_up is true if ftrace is running */
2426 if (ftrace_start_up)
2427 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2430 static cycle_t ftrace_update_time;
2431 unsigned long ftrace_update_tot_cnt;
2433 static inline int ops_traces_mod(struct ftrace_ops *ops)
2436 * Filter_hash being empty will default to trace module.
2437 * But notrace hash requires a test of individual module functions.
2439 return ftrace_hash_empty(ops->filter_hash) &&
2440 ftrace_hash_empty(ops->notrace_hash);
2444 * Check if the current ops references the record.
2446 * If the ops traces all functions, then it was already accounted for.
2447 * If the ops does not trace the current record function, skip it.
2448 * If the ops ignores the function via notrace filter, skip it.
2451 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2453 /* If ops isn't enabled, ignore it */
2454 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2457 /* If ops traces all mods, we already accounted for it */
2458 if (ops_traces_mod(ops))
2461 /* The function must be in the filter */
2462 if (!ftrace_hash_empty(ops->filter_hash) &&
2463 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2466 /* If in notrace hash, we ignore it too */
2467 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2473 static int referenced_filters(struct dyn_ftrace *rec)
2475 struct ftrace_ops *ops;
2478 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2479 if (ops_references_rec(ops, rec))
2486 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2488 struct ftrace_page *pg;
2489 struct dyn_ftrace *p;
2490 cycle_t start, stop;
2491 unsigned long update_cnt = 0;
2492 unsigned long ref = 0;
2497 * When adding a module, we need to check if tracers are
2498 * currently enabled and if they are set to trace all functions.
2499 * If they are, we need to enable the module functions as well
2500 * as update the reference counts for those function records.
2503 struct ftrace_ops *ops;
2505 for (ops = ftrace_ops_list;
2506 ops != &ftrace_list_end; ops = ops->next) {
2507 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2508 if (ops_traces_mod(ops))
2516 start = ftrace_now(raw_smp_processor_id());
2518 for (pg = new_pgs; pg; pg = pg->next) {
2520 for (i = 0; i < pg->index; i++) {
2523 /* If something went wrong, bail without enabling anything */
2524 if (unlikely(ftrace_disabled))
2527 p = &pg->records[i];
2529 cnt += referenced_filters(p);
2533 * Do the initial record conversion from mcount jump
2534 * to the NOP instructions.
2536 if (!ftrace_code_disable(mod, p))
2542 * If the tracing is enabled, go ahead and enable the record.
2544 * The reason not to enable the record immediatelly is the
2545 * inherent check of ftrace_make_nop/ftrace_make_call for
2546 * correct previous instructions. Making first the NOP
2547 * conversion puts the module to the correct state, thus
2548 * passing the ftrace_make_call check.
2550 if (ftrace_start_up && cnt) {
2551 int failed = __ftrace_replace_code(p, 1);
2553 ftrace_bug(failed, p->ip);
2558 stop = ftrace_now(raw_smp_processor_id());
2559 ftrace_update_time = stop - start;
2560 ftrace_update_tot_cnt += update_cnt;
2565 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2570 if (WARN_ON(!count))
2573 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2576 * We want to fill as much as possible. No more than a page
2579 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2583 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2586 /* if we can't allocate this size, try something smaller */
2593 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2602 static struct ftrace_page *
2603 ftrace_allocate_pages(unsigned long num_to_init)
2605 struct ftrace_page *start_pg;
2606 struct ftrace_page *pg;
2613 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2618 * Try to allocate as much as possible in one continues
2619 * location that fills in all of the space. We want to
2620 * waste as little space as possible.
2623 cnt = ftrace_allocate_records(pg, num_to_init);
2631 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)) ||
2841 (iter->flags & FTRACE_ITER_NOTRACE &&
2842 ftrace_hash_empty(ops->notrace_hash))) {
2844 return t_hash_start(m, pos);
2845 iter->flags |= FTRACE_ITER_PRINTALL;
2846 /* reset in case of seek/pread */
2847 iter->flags &= ~FTRACE_ITER_HASH;
2851 if (iter->flags & FTRACE_ITER_HASH)
2852 return t_hash_start(m, pos);
2855 * Unfortunately, we need to restart at ftrace_pages_start
2856 * every time we let go of the ftrace_mutex. This is because
2857 * those pointers can change without the lock.
2859 iter->pg = ftrace_pages_start;
2861 for (l = 0; l <= *pos; ) {
2862 p = t_next(m, p, &l);
2868 return t_hash_start(m, pos);
2873 static void t_stop(struct seq_file *m, void *p)
2875 mutex_unlock(&ftrace_lock);
2878 static int t_show(struct seq_file *m, void *v)
2880 struct ftrace_iterator *iter = m->private;
2881 struct dyn_ftrace *rec;
2883 if (iter->flags & FTRACE_ITER_HASH)
2884 return t_hash_show(m, iter);
2886 if (iter->flags & FTRACE_ITER_PRINTALL) {
2887 if (iter->flags & FTRACE_ITER_NOTRACE)
2888 seq_printf(m, "#### no functions disabled ####\n");
2890 seq_printf(m, "#### all functions enabled ####\n");
2899 seq_printf(m, "%ps", (void *)rec->ip);
2900 if (iter->flags & FTRACE_ITER_ENABLED) {
2901 seq_printf(m, " (%ld)%s",
2902 ftrace_rec_count(rec),
2903 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2904 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2905 struct ftrace_ops *ops;
2907 ops = ftrace_find_tramp_ops_curr(rec);
2908 if (ops && ops->trampoline)
2909 seq_printf(m, "\ttramp: %pS",
2910 (void *)ops->trampoline);
2912 seq_printf(m, "\ttramp: ERROR!");
2916 seq_printf(m, "\n");
2921 static const struct seq_operations show_ftrace_seq_ops = {
2929 ftrace_avail_open(struct inode *inode, struct file *file)
2931 struct ftrace_iterator *iter;
2933 if (unlikely(ftrace_disabled))
2936 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2938 iter->pg = ftrace_pages_start;
2939 iter->ops = &global_ops;
2942 return iter ? 0 : -ENOMEM;
2946 ftrace_enabled_open(struct inode *inode, struct file *file)
2948 struct ftrace_iterator *iter;
2950 if (unlikely(ftrace_disabled))
2953 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2955 iter->pg = ftrace_pages_start;
2956 iter->flags = FTRACE_ITER_ENABLED;
2957 iter->ops = &global_ops;
2960 return iter ? 0 : -ENOMEM;
2964 * ftrace_regex_open - initialize function tracer filter files
2965 * @ops: The ftrace_ops that hold the hash filters
2966 * @flag: The type of filter to process
2967 * @inode: The inode, usually passed in to your open routine
2968 * @file: The file, usually passed in to your open routine
2970 * ftrace_regex_open() initializes the filter files for the
2971 * @ops. Depending on @flag it may process the filter hash or
2972 * the notrace hash of @ops. With this called from the open
2973 * routine, you can use ftrace_filter_write() for the write
2974 * routine if @flag has FTRACE_ITER_FILTER set, or
2975 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2976 * tracing_lseek() should be used as the lseek routine, and
2977 * release must call ftrace_regex_release().
2980 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2981 struct inode *inode, struct file *file)
2983 struct ftrace_iterator *iter;
2984 struct ftrace_hash *hash;
2987 ftrace_ops_init(ops);
2989 if (unlikely(ftrace_disabled))
2992 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2996 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3004 mutex_lock(&ops->regex_lock);
3006 if (flag & FTRACE_ITER_NOTRACE)
3007 hash = ops->notrace_hash;
3009 hash = ops->filter_hash;
3011 if (file->f_mode & FMODE_WRITE) {
3012 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3014 if (file->f_flags & O_TRUNC)
3015 iter->hash = alloc_ftrace_hash(size_bits);
3017 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3020 trace_parser_put(&iter->parser);
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;
3728 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3730 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3734 goto out_regex_unlock;
3737 if (buf && !ftrace_match_records(hash, buf, len)) {
3739 goto out_regex_unlock;
3742 ret = ftrace_match_addr(hash, ip, remove);
3744 goto out_regex_unlock;
3747 mutex_lock(&ftrace_lock);
3748 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3750 ftrace_ops_update_code(ops);
3752 mutex_unlock(&ftrace_lock);
3755 mutex_unlock(&ops->regex_lock);
3757 free_ftrace_hash(hash);
3762 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3763 int reset, int enable)
3765 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3769 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3770 * @ops - the ops to set the filter with
3771 * @ip - the address to add to or remove from the filter.
3772 * @remove - non zero to remove the ip from the filter
3773 * @reset - non zero to reset all filters before applying this filter.
3775 * Filters denote which functions should be enabled when tracing is enabled
3776 * If @ip is NULL, it failes to update filter.
3778 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3779 int remove, int reset)
3781 ftrace_ops_init(ops);
3782 return ftrace_set_addr(ops, ip, remove, reset, 1);
3784 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3787 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3788 int reset, int enable)
3790 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3794 * ftrace_set_filter - set a function to filter on in ftrace
3795 * @ops - the ops to set the filter with
3796 * @buf - the string that holds the function filter text.
3797 * @len - the length of the string.
3798 * @reset - non zero to reset all filters before applying this filter.
3800 * Filters denote which functions should be enabled when tracing is enabled.
3801 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3803 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3806 ftrace_ops_init(ops);
3807 return ftrace_set_regex(ops, buf, len, reset, 1);
3809 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3812 * ftrace_set_notrace - set a function to not trace in ftrace
3813 * @ops - the ops to set the notrace filter with
3814 * @buf - the string that holds the function notrace text.
3815 * @len - the length of the string.
3816 * @reset - non zero to reset all filters before applying this filter.
3818 * Notrace Filters denote which functions should not be enabled when tracing
3819 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3822 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3825 ftrace_ops_init(ops);
3826 return ftrace_set_regex(ops, buf, len, reset, 0);
3828 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3830 * ftrace_set_global_filter - set a function to filter on with global tracers
3831 * @buf - the string that holds the function filter text.
3832 * @len - the length of the string.
3833 * @reset - non zero to reset all filters before applying this filter.
3835 * Filters denote which functions should be enabled when tracing is enabled.
3836 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3838 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3840 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3842 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3845 * ftrace_set_global_notrace - set a function to not trace with global tracers
3846 * @buf - the string that holds the function notrace text.
3847 * @len - the length of the string.
3848 * @reset - non zero to reset all filters before applying this filter.
3850 * Notrace Filters denote which functions should not be enabled when tracing
3851 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3854 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3856 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3858 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3861 * command line interface to allow users to set filters on boot up.
3863 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3864 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3865 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3867 /* Used by function selftest to not test if filter is set */
3868 bool ftrace_filter_param __initdata;
3870 static int __init set_ftrace_notrace(char *str)
3872 ftrace_filter_param = true;
3873 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3876 __setup("ftrace_notrace=", set_ftrace_notrace);
3878 static int __init set_ftrace_filter(char *str)
3880 ftrace_filter_param = true;
3881 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3884 __setup("ftrace_filter=", set_ftrace_filter);
3886 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3887 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3888 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3889 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3891 static int __init set_graph_function(char *str)
3893 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3896 __setup("ftrace_graph_filter=", set_graph_function);
3898 static int __init set_graph_notrace_function(char *str)
3900 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
3903 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
3905 static void __init set_ftrace_early_graph(char *buf, int enable)
3909 unsigned long *table = ftrace_graph_funcs;
3910 int *count = &ftrace_graph_count;
3913 table = ftrace_graph_notrace_funcs;
3914 count = &ftrace_graph_notrace_count;
3918 func = strsep(&buf, ",");
3919 /* we allow only one expression at a time */
3920 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
3922 printk(KERN_DEBUG "ftrace: function %s not "
3923 "traceable\n", func);
3926 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3929 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3933 ftrace_ops_init(ops);
3936 func = strsep(&buf, ",");
3937 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3941 static void __init set_ftrace_early_filters(void)
3943 if (ftrace_filter_buf[0])
3944 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3945 if (ftrace_notrace_buf[0])
3946 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3947 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3948 if (ftrace_graph_buf[0])
3949 set_ftrace_early_graph(ftrace_graph_buf, 1);
3950 if (ftrace_graph_notrace_buf[0])
3951 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
3952 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3955 int ftrace_regex_release(struct inode *inode, struct file *file)
3957 struct seq_file *m = (struct seq_file *)file->private_data;
3958 struct ftrace_iterator *iter;
3959 struct ftrace_hash **orig_hash;
3960 struct trace_parser *parser;
3964 if (file->f_mode & FMODE_READ) {
3966 seq_release(inode, file);
3968 iter = file->private_data;
3970 parser = &iter->parser;
3971 if (trace_parser_loaded(parser)) {
3972 parser->buffer[parser->idx] = 0;
3973 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3976 trace_parser_put(parser);
3978 mutex_lock(&iter->ops->regex_lock);
3980 if (file->f_mode & FMODE_WRITE) {
3981 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3984 orig_hash = &iter->ops->filter_hash;
3986 orig_hash = &iter->ops->notrace_hash;
3988 mutex_lock(&ftrace_lock);
3989 ret = ftrace_hash_move(iter->ops, filter_hash,
3990 orig_hash, iter->hash);
3992 ftrace_ops_update_code(iter->ops);
3994 mutex_unlock(&ftrace_lock);
3997 mutex_unlock(&iter->ops->regex_lock);
3998 free_ftrace_hash(iter->hash);
4004 static const struct file_operations ftrace_avail_fops = {
4005 .open = ftrace_avail_open,
4007 .llseek = seq_lseek,
4008 .release = seq_release_private,
4011 static const struct file_operations ftrace_enabled_fops = {
4012 .open = ftrace_enabled_open,
4014 .llseek = seq_lseek,
4015 .release = seq_release_private,
4018 static const struct file_operations ftrace_filter_fops = {
4019 .open = ftrace_filter_open,
4021 .write = ftrace_filter_write,
4022 .llseek = tracing_lseek,
4023 .release = ftrace_regex_release,
4026 static const struct file_operations ftrace_notrace_fops = {
4027 .open = ftrace_notrace_open,
4029 .write = ftrace_notrace_write,
4030 .llseek = tracing_lseek,
4031 .release = ftrace_regex_release,
4034 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4036 static DEFINE_MUTEX(graph_lock);
4038 int ftrace_graph_count;
4039 int ftrace_graph_notrace_count;
4040 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4041 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4043 struct ftrace_graph_data {
4044 unsigned long *table;
4047 const struct seq_operations *seq_ops;
4051 __g_next(struct seq_file *m, loff_t *pos)
4053 struct ftrace_graph_data *fgd = m->private;
4055 if (*pos >= *fgd->count)
4057 return &fgd->table[*pos];
4061 g_next(struct seq_file *m, void *v, loff_t *pos)
4064 return __g_next(m, pos);
4067 static void *g_start(struct seq_file *m, loff_t *pos)
4069 struct ftrace_graph_data *fgd = m->private;
4071 mutex_lock(&graph_lock);
4073 /* Nothing, tell g_show to print all functions are enabled */
4074 if (!*fgd->count && !*pos)
4077 return __g_next(m, pos);
4080 static void g_stop(struct seq_file *m, void *p)
4082 mutex_unlock(&graph_lock);
4085 static int g_show(struct seq_file *m, void *v)
4087 unsigned long *ptr = v;
4092 if (ptr == (unsigned long *)1) {
4093 struct ftrace_graph_data *fgd = m->private;
4095 if (fgd->table == ftrace_graph_funcs)
4096 seq_printf(m, "#### all functions enabled ####\n");
4098 seq_printf(m, "#### no functions disabled ####\n");
4102 seq_printf(m, "%ps\n", (void *)*ptr);
4107 static const struct seq_operations ftrace_graph_seq_ops = {
4115 __ftrace_graph_open(struct inode *inode, struct file *file,
4116 struct ftrace_graph_data *fgd)
4120 mutex_lock(&graph_lock);
4121 if ((file->f_mode & FMODE_WRITE) &&
4122 (file->f_flags & O_TRUNC)) {
4124 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4126 mutex_unlock(&graph_lock);
4128 if (file->f_mode & FMODE_READ) {
4129 ret = seq_open(file, fgd->seq_ops);
4131 struct seq_file *m = file->private_data;
4135 file->private_data = fgd;
4141 ftrace_graph_open(struct inode *inode, struct file *file)
4143 struct ftrace_graph_data *fgd;
4145 if (unlikely(ftrace_disabled))
4148 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4152 fgd->table = ftrace_graph_funcs;
4153 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4154 fgd->count = &ftrace_graph_count;
4155 fgd->seq_ops = &ftrace_graph_seq_ops;
4157 return __ftrace_graph_open(inode, file, fgd);
4161 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4163 struct ftrace_graph_data *fgd;
4165 if (unlikely(ftrace_disabled))
4168 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4172 fgd->table = ftrace_graph_notrace_funcs;
4173 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4174 fgd->count = &ftrace_graph_notrace_count;
4175 fgd->seq_ops = &ftrace_graph_seq_ops;
4177 return __ftrace_graph_open(inode, file, fgd);
4181 ftrace_graph_release(struct inode *inode, struct file *file)
4183 if (file->f_mode & FMODE_READ) {
4184 struct seq_file *m = file->private_data;
4187 seq_release(inode, file);
4189 kfree(file->private_data);
4196 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4198 struct dyn_ftrace *rec;
4199 struct ftrace_page *pg;
4208 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
4209 if (!not && *idx >= size)
4212 search_len = strlen(search);
4214 mutex_lock(&ftrace_lock);
4216 if (unlikely(ftrace_disabled)) {
4217 mutex_unlock(&ftrace_lock);
4221 do_for_each_ftrace_rec(pg, rec) {
4223 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4224 /* if it is in the array */
4226 for (i = 0; i < *idx; i++) {
4227 if (array[i] == rec->ip) {
4236 array[(*idx)++] = rec->ip;
4242 array[i] = array[--(*idx)];
4248 } while_for_each_ftrace_rec();
4250 mutex_unlock(&ftrace_lock);
4259 ftrace_graph_write(struct file *file, const char __user *ubuf,
4260 size_t cnt, loff_t *ppos)
4262 struct trace_parser parser;
4263 ssize_t read, ret = 0;
4264 struct ftrace_graph_data *fgd = file->private_data;
4269 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4272 read = trace_get_user(&parser, ubuf, cnt, ppos);
4274 if (read >= 0 && trace_parser_loaded((&parser))) {
4275 parser.buffer[parser.idx] = 0;
4277 mutex_lock(&graph_lock);
4279 /* we allow only one expression at a time */
4280 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4283 mutex_unlock(&graph_lock);
4289 trace_parser_put(&parser);
4294 static const struct file_operations ftrace_graph_fops = {
4295 .open = ftrace_graph_open,
4297 .write = ftrace_graph_write,
4298 .llseek = tracing_lseek,
4299 .release = ftrace_graph_release,
4302 static const struct file_operations ftrace_graph_notrace_fops = {
4303 .open = ftrace_graph_notrace_open,
4305 .write = ftrace_graph_write,
4306 .llseek = tracing_lseek,
4307 .release = ftrace_graph_release,
4309 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4311 void ftrace_create_filter_files(struct ftrace_ops *ops,
4312 struct dentry *parent)
4315 trace_create_file("set_ftrace_filter", 0644, parent,
4316 ops, &ftrace_filter_fops);
4318 trace_create_file("set_ftrace_notrace", 0644, parent,
4319 ops, &ftrace_notrace_fops);
4323 * The name "destroy_filter_files" is really a misnomer. Although
4324 * in the future, it may actualy delete the files, but this is
4325 * really intended to make sure the ops passed in are disabled
4326 * and that when this function returns, the caller is free to
4329 * The "destroy" name is only to match the "create" name that this
4330 * should be paired with.
4332 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4334 mutex_lock(&ftrace_lock);
4335 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4336 ftrace_shutdown(ops, 0);
4337 ops->flags |= FTRACE_OPS_FL_DELETED;
4338 mutex_unlock(&ftrace_lock);
4341 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4344 trace_create_file("available_filter_functions", 0444,
4345 d_tracer, NULL, &ftrace_avail_fops);
4347 trace_create_file("enabled_functions", 0444,
4348 d_tracer, NULL, &ftrace_enabled_fops);
4350 ftrace_create_filter_files(&global_ops, d_tracer);
4352 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4353 trace_create_file("set_graph_function", 0444, d_tracer,
4355 &ftrace_graph_fops);
4356 trace_create_file("set_graph_notrace", 0444, d_tracer,
4358 &ftrace_graph_notrace_fops);
4359 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4364 static int ftrace_cmp_ips(const void *a, const void *b)
4366 const unsigned long *ipa = a;
4367 const unsigned long *ipb = b;
4376 static void ftrace_swap_ips(void *a, void *b, int size)
4378 unsigned long *ipa = a;
4379 unsigned long *ipb = b;
4387 static int ftrace_process_locs(struct module *mod,
4388 unsigned long *start,
4391 struct ftrace_page *start_pg;
4392 struct ftrace_page *pg;
4393 struct dyn_ftrace *rec;
4394 unsigned long count;
4397 unsigned long flags = 0; /* Shut up gcc */
4400 count = end - start;
4405 sort(start, count, sizeof(*start),
4406 ftrace_cmp_ips, ftrace_swap_ips);
4408 start_pg = ftrace_allocate_pages(count);
4412 mutex_lock(&ftrace_lock);
4415 * Core and each module needs their own pages, as
4416 * modules will free them when they are removed.
4417 * Force a new page to be allocated for modules.
4420 WARN_ON(ftrace_pages || ftrace_pages_start);
4421 /* First initialization */
4422 ftrace_pages = ftrace_pages_start = start_pg;
4427 if (WARN_ON(ftrace_pages->next)) {
4428 /* Hmm, we have free pages? */
4429 while (ftrace_pages->next)
4430 ftrace_pages = ftrace_pages->next;
4433 ftrace_pages->next = start_pg;
4439 addr = ftrace_call_adjust(*p++);
4441 * Some architecture linkers will pad between
4442 * the different mcount_loc sections of different
4443 * object files to satisfy alignments.
4444 * Skip any NULL pointers.
4449 if (pg->index == pg->size) {
4450 /* We should have allocated enough */
4451 if (WARN_ON(!pg->next))
4456 rec = &pg->records[pg->index++];
4460 /* We should have used all pages */
4463 /* Assign the last page to ftrace_pages */
4467 * We only need to disable interrupts on start up
4468 * because we are modifying code that an interrupt
4469 * may execute, and the modification is not atomic.
4470 * But for modules, nothing runs the code we modify
4471 * until we are finished with it, and there's no
4472 * reason to cause large interrupt latencies while we do it.
4475 local_irq_save(flags);
4476 ftrace_update_code(mod, start_pg);
4478 local_irq_restore(flags);
4481 mutex_unlock(&ftrace_lock);
4486 #ifdef CONFIG_MODULES
4488 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4490 void ftrace_release_mod(struct module *mod)
4492 struct dyn_ftrace *rec;
4493 struct ftrace_page **last_pg;
4494 struct ftrace_page *pg;
4497 mutex_lock(&ftrace_lock);
4499 if (ftrace_disabled)
4503 * Each module has its own ftrace_pages, remove
4504 * them from the list.
4506 last_pg = &ftrace_pages_start;
4507 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4508 rec = &pg->records[0];
4509 if (within_module_core(rec->ip, mod)) {
4511 * As core pages are first, the first
4512 * page should never be a module page.
4514 if (WARN_ON(pg == ftrace_pages_start))
4517 /* Check if we are deleting the last page */
4518 if (pg == ftrace_pages)
4519 ftrace_pages = next_to_ftrace_page(last_pg);
4521 *last_pg = pg->next;
4522 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4523 free_pages((unsigned long)pg->records, order);
4526 last_pg = &pg->next;
4529 mutex_unlock(&ftrace_lock);
4532 static void ftrace_init_module(struct module *mod,
4533 unsigned long *start, unsigned long *end)
4535 if (ftrace_disabled || start == end)
4537 ftrace_process_locs(mod, start, end);
4540 void ftrace_module_init(struct module *mod)
4542 ftrace_init_module(mod, mod->ftrace_callsites,
4543 mod->ftrace_callsites +
4544 mod->num_ftrace_callsites);
4547 static int ftrace_module_notify_exit(struct notifier_block *self,
4548 unsigned long val, void *data)
4550 struct module *mod = data;
4552 if (val == MODULE_STATE_GOING)
4553 ftrace_release_mod(mod);
4558 static int ftrace_module_notify_exit(struct notifier_block *self,
4559 unsigned long val, void *data)
4563 #endif /* CONFIG_MODULES */
4565 struct notifier_block ftrace_module_exit_nb = {
4566 .notifier_call = ftrace_module_notify_exit,
4567 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4570 void __init ftrace_init(void)
4572 extern unsigned long __start_mcount_loc[];
4573 extern unsigned long __stop_mcount_loc[];
4574 unsigned long count, flags;
4577 local_irq_save(flags);
4578 ret = ftrace_dyn_arch_init();
4579 local_irq_restore(flags);
4583 count = __stop_mcount_loc - __start_mcount_loc;
4585 pr_info("ftrace: No functions to be traced?\n");
4589 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4590 count, count / ENTRIES_PER_PAGE + 1);
4592 last_ftrace_enabled = ftrace_enabled = 1;
4594 ret = ftrace_process_locs(NULL,
4598 ret = register_module_notifier(&ftrace_module_exit_nb);
4600 pr_warning("Failed to register trace ftrace module exit notifier\n");
4602 set_ftrace_early_filters();
4606 ftrace_disabled = 1;
4611 static struct ftrace_ops global_ops = {
4612 .func = ftrace_stub,
4613 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4614 INIT_REGEX_LOCK(global_ops)
4617 static int __init ftrace_nodyn_init(void)
4622 core_initcall(ftrace_nodyn_init);
4624 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4625 static inline void ftrace_startup_enable(int command) { }
4626 /* Keep as macros so we do not need to define the commands */
4627 # define ftrace_startup(ops, command) \
4629 int ___ret = __register_ftrace_function(ops); \
4631 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4634 # define ftrace_shutdown(ops, command) \
4636 int ___ret = __unregister_ftrace_function(ops); \
4638 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4642 # define ftrace_startup_sysctl() do { } while (0)
4643 # define ftrace_shutdown_sysctl() do { } while (0)
4646 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4651 #endif /* CONFIG_DYNAMIC_FTRACE */
4653 __init void ftrace_init_global_array_ops(struct trace_array *tr)
4655 tr->ops = &global_ops;
4656 tr->ops->private = tr;
4659 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4661 /* If we filter on pids, update to use the pid function */
4662 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4663 if (WARN_ON(tr->ops->func != ftrace_stub))
4664 printk("ftrace ops had %pS for function\n",
4666 /* Only the top level instance does pid tracing */
4667 if (!list_empty(&ftrace_pids)) {
4668 set_ftrace_pid_function(func);
4669 func = ftrace_pid_func;
4672 tr->ops->func = func;
4673 tr->ops->private = tr;
4676 void ftrace_reset_array_ops(struct trace_array *tr)
4678 tr->ops->func = ftrace_stub;
4682 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4683 struct ftrace_ops *op, struct pt_regs *regs)
4685 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4689 * Some of the ops may be dynamically allocated,
4690 * they must be freed after a synchronize_sched().
4692 preempt_disable_notrace();
4693 trace_recursion_set(TRACE_CONTROL_BIT);
4696 * Control funcs (perf) uses RCU. Only trace if
4697 * RCU is currently active.
4699 if (!rcu_is_watching())
4702 do_for_each_ftrace_op(op, ftrace_control_list) {
4703 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4704 !ftrace_function_local_disabled(op) &&
4705 ftrace_ops_test(op, ip, regs))
4706 op->func(ip, parent_ip, op, regs);
4707 } while_for_each_ftrace_op(op);
4709 trace_recursion_clear(TRACE_CONTROL_BIT);
4710 preempt_enable_notrace();
4713 static struct ftrace_ops control_ops = {
4714 .func = ftrace_ops_control_func,
4715 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4716 INIT_REGEX_LOCK(control_ops)
4720 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4721 struct ftrace_ops *ignored, struct pt_regs *regs)
4723 struct ftrace_ops *op;
4726 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4731 * Some of the ops may be dynamically allocated,
4732 * they must be freed after a synchronize_sched().
4734 preempt_disable_notrace();
4735 do_for_each_ftrace_op(op, ftrace_ops_list) {
4736 if (ftrace_ops_test(op, ip, regs)) {
4737 if (FTRACE_WARN_ON(!op->func)) {
4738 pr_warn("op=%p %pS\n", op, op);
4741 op->func(ip, parent_ip, op, regs);
4743 } while_for_each_ftrace_op(op);
4745 preempt_enable_notrace();
4746 trace_clear_recursion(bit);
4750 * Some archs only support passing ip and parent_ip. Even though
4751 * the list function ignores the op parameter, we do not want any
4752 * C side effects, where a function is called without the caller
4753 * sending a third parameter.
4754 * Archs are to support both the regs and ftrace_ops at the same time.
4755 * If they support ftrace_ops, it is assumed they support regs.
4756 * If call backs want to use regs, they must either check for regs
4757 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4758 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4759 * An architecture can pass partial regs with ftrace_ops and still
4760 * set the ARCH_SUPPORT_FTARCE_OPS.
4762 #if ARCH_SUPPORTS_FTRACE_OPS
4763 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4764 struct ftrace_ops *op, struct pt_regs *regs)
4766 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4769 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4771 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4775 static void clear_ftrace_swapper(void)
4777 struct task_struct *p;
4781 for_each_online_cpu(cpu) {
4783 clear_tsk_trace_trace(p);
4788 static void set_ftrace_swapper(void)
4790 struct task_struct *p;
4794 for_each_online_cpu(cpu) {
4796 set_tsk_trace_trace(p);
4801 static void clear_ftrace_pid(struct pid *pid)
4803 struct task_struct *p;
4806 do_each_pid_task(pid, PIDTYPE_PID, p) {
4807 clear_tsk_trace_trace(p);
4808 } while_each_pid_task(pid, PIDTYPE_PID, p);
4814 static void set_ftrace_pid(struct pid *pid)
4816 struct task_struct *p;
4819 do_each_pid_task(pid, PIDTYPE_PID, p) {
4820 set_tsk_trace_trace(p);
4821 } while_each_pid_task(pid, PIDTYPE_PID, p);
4825 static void clear_ftrace_pid_task(struct pid *pid)
4827 if (pid == ftrace_swapper_pid)
4828 clear_ftrace_swapper();
4830 clear_ftrace_pid(pid);
4833 static void set_ftrace_pid_task(struct pid *pid)
4835 if (pid == ftrace_swapper_pid)
4836 set_ftrace_swapper();
4838 set_ftrace_pid(pid);
4841 static int ftrace_pid_add(int p)
4844 struct ftrace_pid *fpid;
4847 mutex_lock(&ftrace_lock);
4850 pid = ftrace_swapper_pid;
4852 pid = find_get_pid(p);
4859 list_for_each_entry(fpid, &ftrace_pids, list)
4860 if (fpid->pid == pid)
4865 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4869 list_add(&fpid->list, &ftrace_pids);
4872 set_ftrace_pid_task(pid);
4874 ftrace_update_pid_func();
4875 ftrace_startup_enable(0);
4877 mutex_unlock(&ftrace_lock);
4881 if (pid != ftrace_swapper_pid)
4885 mutex_unlock(&ftrace_lock);
4889 static void ftrace_pid_reset(void)
4891 struct ftrace_pid *fpid, *safe;
4893 mutex_lock(&ftrace_lock);
4894 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4895 struct pid *pid = fpid->pid;
4897 clear_ftrace_pid_task(pid);
4899 list_del(&fpid->list);
4903 ftrace_update_pid_func();
4904 ftrace_startup_enable(0);
4906 mutex_unlock(&ftrace_lock);
4909 static void *fpid_start(struct seq_file *m, loff_t *pos)
4911 mutex_lock(&ftrace_lock);
4913 if (list_empty(&ftrace_pids) && (!*pos))
4916 return seq_list_start(&ftrace_pids, *pos);
4919 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4924 return seq_list_next(v, &ftrace_pids, pos);
4927 static void fpid_stop(struct seq_file *m, void *p)
4929 mutex_unlock(&ftrace_lock);
4932 static int fpid_show(struct seq_file *m, void *v)
4934 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4936 if (v == (void *)1) {
4937 seq_printf(m, "no pid\n");
4941 if (fpid->pid == ftrace_swapper_pid)
4942 seq_printf(m, "swapper tasks\n");
4944 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4949 static const struct seq_operations ftrace_pid_sops = {
4950 .start = fpid_start,
4957 ftrace_pid_open(struct inode *inode, struct file *file)
4961 if ((file->f_mode & FMODE_WRITE) &&
4962 (file->f_flags & O_TRUNC))
4965 if (file->f_mode & FMODE_READ)
4966 ret = seq_open(file, &ftrace_pid_sops);
4972 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4973 size_t cnt, loff_t *ppos)
4979 if (cnt >= sizeof(buf))
4982 if (copy_from_user(&buf, ubuf, cnt))
4988 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4989 * to clean the filter quietly.
4991 tmp = strstrip(buf);
4992 if (strlen(tmp) == 0)
4995 ret = kstrtol(tmp, 10, &val);
4999 ret = ftrace_pid_add(val);
5001 return ret ? ret : cnt;
5005 ftrace_pid_release(struct inode *inode, struct file *file)
5007 if (file->f_mode & FMODE_READ)
5008 seq_release(inode, file);
5013 static const struct file_operations ftrace_pid_fops = {
5014 .open = ftrace_pid_open,
5015 .write = ftrace_pid_write,
5017 .llseek = tracing_lseek,
5018 .release = ftrace_pid_release,
5021 static __init int ftrace_init_debugfs(void)
5023 struct dentry *d_tracer;
5025 d_tracer = tracing_init_dentry();
5029 ftrace_init_dyn_debugfs(d_tracer);
5031 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5032 NULL, &ftrace_pid_fops);
5034 ftrace_profile_debugfs(d_tracer);
5038 fs_initcall(ftrace_init_debugfs);
5041 * ftrace_kill - kill ftrace
5043 * This function should be used by panic code. It stops ftrace
5044 * but in a not so nice way. If you need to simply kill ftrace
5045 * from a non-atomic section, use ftrace_kill.
5047 void ftrace_kill(void)
5049 ftrace_disabled = 1;
5051 clear_ftrace_function();
5055 * Test if ftrace is dead or not.
5057 int ftrace_is_dead(void)
5059 return ftrace_disabled;
5063 * register_ftrace_function - register a function for profiling
5064 * @ops - ops structure that holds the function for profiling.
5066 * Register a function to be called by all functions in the
5069 * Note: @ops->func and all the functions it calls must be labeled
5070 * with "notrace", otherwise it will go into a
5073 int register_ftrace_function(struct ftrace_ops *ops)
5077 ftrace_ops_init(ops);
5079 mutex_lock(&ftrace_lock);
5081 ret = ftrace_startup(ops, 0);
5083 mutex_unlock(&ftrace_lock);
5087 EXPORT_SYMBOL_GPL(register_ftrace_function);
5090 * unregister_ftrace_function - unregister a function for profiling.
5091 * @ops - ops structure that holds the function to unregister
5093 * Unregister a function that was added to be called by ftrace profiling.
5095 int unregister_ftrace_function(struct ftrace_ops *ops)
5099 mutex_lock(&ftrace_lock);
5100 ret = ftrace_shutdown(ops, 0);
5101 mutex_unlock(&ftrace_lock);
5105 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5108 ftrace_enable_sysctl(struct ctl_table *table, int write,
5109 void __user *buffer, size_t *lenp,
5114 mutex_lock(&ftrace_lock);
5116 if (unlikely(ftrace_disabled))
5119 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5121 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5124 last_ftrace_enabled = !!ftrace_enabled;
5126 if (ftrace_enabled) {
5128 ftrace_startup_sysctl();
5130 /* we are starting ftrace again */
5131 if (ftrace_ops_list != &ftrace_list_end)
5132 update_ftrace_function();
5135 /* stopping ftrace calls (just send to ftrace_stub) */
5136 ftrace_trace_function = ftrace_stub;
5138 ftrace_shutdown_sysctl();
5142 mutex_unlock(&ftrace_lock);
5146 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5148 static int ftrace_graph_active;
5150 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5155 /* The callbacks that hook a function */
5156 trace_func_graph_ret_t ftrace_graph_return =
5157 (trace_func_graph_ret_t)ftrace_stub;
5158 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5159 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5161 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5162 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5166 unsigned long flags;
5167 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5168 struct task_struct *g, *t;
5170 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5171 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5172 * sizeof(struct ftrace_ret_stack),
5174 if (!ret_stack_list[i]) {
5182 read_lock_irqsave(&tasklist_lock, flags);
5183 do_each_thread(g, t) {
5189 if (t->ret_stack == NULL) {
5190 atomic_set(&t->tracing_graph_pause, 0);
5191 atomic_set(&t->trace_overrun, 0);
5192 t->curr_ret_stack = -1;
5193 /* Make sure the tasks see the -1 first: */
5195 t->ret_stack = ret_stack_list[start++];
5197 } while_each_thread(g, t);
5200 read_unlock_irqrestore(&tasklist_lock, flags);
5202 for (i = start; i < end; i++)
5203 kfree(ret_stack_list[i]);
5208 ftrace_graph_probe_sched_switch(void *ignore,
5209 struct task_struct *prev, struct task_struct *next)
5211 unsigned long long timestamp;
5215 * Does the user want to count the time a function was asleep.
5216 * If so, do not update the time stamps.
5218 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5221 timestamp = trace_clock_local();
5223 prev->ftrace_timestamp = timestamp;
5225 /* only process tasks that we timestamped */
5226 if (!next->ftrace_timestamp)
5230 * Update all the counters in next to make up for the
5231 * time next was sleeping.
5233 timestamp -= next->ftrace_timestamp;
5235 for (index = next->curr_ret_stack; index >= 0; index--)
5236 next->ret_stack[index].calltime += timestamp;
5239 /* Allocate a return stack for each task */
5240 static int start_graph_tracing(void)
5242 struct ftrace_ret_stack **ret_stack_list;
5245 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5246 sizeof(struct ftrace_ret_stack *),
5249 if (!ret_stack_list)
5252 /* The cpu_boot init_task->ret_stack will never be freed */
5253 for_each_online_cpu(cpu) {
5254 if (!idle_task(cpu)->ret_stack)
5255 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5259 ret = alloc_retstack_tasklist(ret_stack_list);
5260 } while (ret == -EAGAIN);
5263 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5265 pr_info("ftrace_graph: Couldn't activate tracepoint"
5266 " probe to kernel_sched_switch\n");
5269 kfree(ret_stack_list);
5274 * Hibernation protection.
5275 * The state of the current task is too much unstable during
5276 * suspend/restore to disk. We want to protect against that.
5279 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5283 case PM_HIBERNATION_PREPARE:
5284 pause_graph_tracing();
5287 case PM_POST_HIBERNATION:
5288 unpause_graph_tracing();
5294 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5296 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5298 return __ftrace_graph_entry(trace);
5302 * The function graph tracer should only trace the functions defined
5303 * by set_ftrace_filter and set_ftrace_notrace. If another function
5304 * tracer ops is registered, the graph tracer requires testing the
5305 * function against the global ops, and not just trace any function
5306 * that any ftrace_ops registered.
5308 static void update_function_graph_func(void)
5310 if (ftrace_ops_list == &ftrace_list_end ||
5311 (ftrace_ops_list == &global_ops &&
5312 global_ops.next == &ftrace_list_end))
5313 ftrace_graph_entry = __ftrace_graph_entry;
5315 ftrace_graph_entry = ftrace_graph_entry_test;
5318 static struct notifier_block ftrace_suspend_notifier = {
5319 .notifier_call = ftrace_suspend_notifier_call,
5322 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5323 trace_func_graph_ent_t entryfunc)
5327 mutex_lock(&ftrace_lock);
5329 /* we currently allow only one tracer registered at a time */
5330 if (ftrace_graph_active) {
5335 register_pm_notifier(&ftrace_suspend_notifier);
5337 ftrace_graph_active++;
5338 ret = start_graph_tracing();
5340 ftrace_graph_active--;
5344 ftrace_graph_return = retfunc;
5347 * Update the indirect function to the entryfunc, and the
5348 * function that gets called to the entry_test first. Then
5349 * call the update fgraph entry function to determine if
5350 * the entryfunc should be called directly or not.
5352 __ftrace_graph_entry = entryfunc;
5353 ftrace_graph_entry = ftrace_graph_entry_test;
5354 update_function_graph_func();
5356 /* Function graph doesn't use the .func field of global_ops */
5357 global_ops.flags |= FTRACE_OPS_FL_STUB;
5359 #ifdef CONFIG_DYNAMIC_FTRACE
5360 /* Optimize function graph calling (if implemented by arch) */
5361 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5362 global_ops.trampoline = FTRACE_GRAPH_TRAMP_ADDR;
5365 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
5368 mutex_unlock(&ftrace_lock);
5372 void unregister_ftrace_graph(void)
5374 mutex_lock(&ftrace_lock);
5376 if (unlikely(!ftrace_graph_active))
5379 ftrace_graph_active--;
5380 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5381 ftrace_graph_entry = ftrace_graph_entry_stub;
5382 __ftrace_graph_entry = ftrace_graph_entry_stub;
5383 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5384 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
5385 #ifdef CONFIG_DYNAMIC_FTRACE
5386 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5387 global_ops.trampoline = 0;
5389 unregister_pm_notifier(&ftrace_suspend_notifier);
5390 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5393 mutex_unlock(&ftrace_lock);
5396 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5399 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5401 atomic_set(&t->tracing_graph_pause, 0);
5402 atomic_set(&t->trace_overrun, 0);
5403 t->ftrace_timestamp = 0;
5404 /* make curr_ret_stack visible before we add the ret_stack */
5406 t->ret_stack = ret_stack;
5410 * Allocate a return stack for the idle task. May be the first
5411 * time through, or it may be done by CPU hotplug online.
5413 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5415 t->curr_ret_stack = -1;
5417 * The idle task has no parent, it either has its own
5418 * stack or no stack at all.
5421 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5423 if (ftrace_graph_active) {
5424 struct ftrace_ret_stack *ret_stack;
5426 ret_stack = per_cpu(idle_ret_stack, cpu);
5428 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5429 * sizeof(struct ftrace_ret_stack),
5433 per_cpu(idle_ret_stack, cpu) = ret_stack;
5435 graph_init_task(t, ret_stack);
5439 /* Allocate a return stack for newly created task */
5440 void ftrace_graph_init_task(struct task_struct *t)
5442 /* Make sure we do not use the parent ret_stack */
5443 t->ret_stack = NULL;
5444 t->curr_ret_stack = -1;
5446 if (ftrace_graph_active) {
5447 struct ftrace_ret_stack *ret_stack;
5449 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5450 * sizeof(struct ftrace_ret_stack),
5454 graph_init_task(t, ret_stack);
5458 void ftrace_graph_exit_task(struct task_struct *t)
5460 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5462 t->ret_stack = NULL;
5463 /* NULL must become visible to IRQs before we free it: */