2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
36 #include <trace/events/sched.h>
38 #include <asm/setup.h>
40 #include "trace_output.h"
41 #include "trace_stat.h"
43 #define FTRACE_WARN_ON(cond) \
51 #define FTRACE_WARN_ON_ONCE(cond) \
54 if (WARN_ON_ONCE(___r)) \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
71 #define ASSIGN_OPS_HASH(opsname, val) \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
75 #define INIT_OPS_HASH(opsname)
76 #define ASSIGN_OPS_HASH(opsname, val)
79 static struct ftrace_ops ftrace_list_end __read_mostly = {
81 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
82 INIT_OPS_HASH(ftrace_list_end)
85 /* ftrace_enabled is a method to turn ftrace on or off */
86 int ftrace_enabled __read_mostly;
87 static int last_ftrace_enabled;
89 /* Current function tracing op */
90 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
91 /* What to set function_trace_op to */
92 static struct ftrace_ops *set_function_trace_op;
94 /* List for set_ftrace_pid's pids. */
95 LIST_HEAD(ftrace_pids);
97 struct list_head list;
102 * ftrace_disabled is set when an anomaly is discovered.
103 * ftrace_disabled is much stronger than ftrace_enabled.
105 static int ftrace_disabled __read_mostly;
107 static DEFINE_MUTEX(ftrace_lock);
109 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
110 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
111 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
112 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
113 static struct ftrace_ops global_ops;
114 static struct ftrace_ops control_ops;
116 static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
117 struct ftrace_ops *op, struct pt_regs *regs);
119 #if ARCH_SUPPORTS_FTRACE_OPS
120 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
121 struct ftrace_ops *op, struct pt_regs *regs);
123 /* See comment below, where ftrace_ops_list_func is defined */
124 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
125 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
129 * Traverse the ftrace_global_list, invoking all entries. The reason that we
130 * can use rcu_dereference_raw_notrace() is that elements removed from this list
131 * are simply leaked, so there is no need to interact with a grace-period
132 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
133 * concurrent insertions into the ftrace_global_list.
135 * Silly Alpha and silly pointer-speculation compiler optimizations!
137 #define do_for_each_ftrace_op(op, list) \
138 op = rcu_dereference_raw_notrace(list); \
142 * Optimized for just a single item in the list (as that is the normal case).
144 #define while_for_each_ftrace_op(op) \
145 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
146 unlikely((op) != &ftrace_list_end))
148 static inline void ftrace_ops_init(struct ftrace_ops *ops)
150 #ifdef CONFIG_DYNAMIC_FTRACE
151 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
152 mutex_init(&ops->local_hash.regex_lock);
153 ops->func_hash = &ops->local_hash;
154 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
160 * ftrace_nr_registered_ops - return number of ops registered
162 * Returns the number of ftrace_ops registered and tracing functions
164 int ftrace_nr_registered_ops(void)
166 struct ftrace_ops *ops;
169 mutex_lock(&ftrace_lock);
171 for (ops = ftrace_ops_list;
172 ops != &ftrace_list_end; ops = ops->next)
175 mutex_unlock(&ftrace_lock);
180 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
181 struct ftrace_ops *op, struct pt_regs *regs)
183 if (!test_tsk_trace_trace(current))
186 ftrace_pid_function(ip, parent_ip, op, regs);
189 static void set_ftrace_pid_function(ftrace_func_t func)
191 /* do not set ftrace_pid_function to itself! */
192 if (func != ftrace_pid_func)
193 ftrace_pid_function = func;
197 * clear_ftrace_function - reset the ftrace function
199 * This NULLs the ftrace function and in essence stops
200 * tracing. There may be lag
202 void clear_ftrace_function(void)
204 ftrace_trace_function = ftrace_stub;
205 ftrace_pid_function = ftrace_stub;
208 static void control_ops_disable_all(struct ftrace_ops *ops)
212 for_each_possible_cpu(cpu)
213 *per_cpu_ptr(ops->disabled, cpu) = 1;
216 static int control_ops_alloc(struct ftrace_ops *ops)
218 int __percpu *disabled;
220 disabled = alloc_percpu(int);
224 ops->disabled = disabled;
225 control_ops_disable_all(ops);
229 static void ftrace_sync(struct work_struct *work)
232 * This function is just a stub to implement a hard force
233 * of synchronize_sched(). This requires synchronizing
234 * tasks even in userspace and idle.
236 * Yes, function tracing is rude.
240 static void ftrace_sync_ipi(void *data)
242 /* Probably not needed, but do it anyway */
246 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
247 static void update_function_graph_func(void);
249 static inline void update_function_graph_func(void) { }
252 static void update_ftrace_function(void)
257 * Prepare the ftrace_ops that the arch callback will use.
258 * If there's only one ftrace_ops registered, the ftrace_ops_list
259 * will point to the ops we want.
261 set_function_trace_op = ftrace_ops_list;
263 /* If there's no ftrace_ops registered, just call the stub function */
264 if (ftrace_ops_list == &ftrace_list_end) {
268 * If we are at the end of the list and this ops is
269 * recursion safe and not dynamic and the arch supports passing ops,
270 * then have the mcount trampoline call the function directly.
272 } else if (ftrace_ops_list->next == &ftrace_list_end) {
273 func = ftrace_ops_get_func(ftrace_ops_list);
276 /* Just use the default ftrace_ops */
277 set_function_trace_op = &ftrace_list_end;
278 func = ftrace_ops_list_func;
281 update_function_graph_func();
283 /* If there's no change, then do nothing more here */
284 if (ftrace_trace_function == func)
288 * If we are using the list function, it doesn't care
289 * about the function_trace_ops.
291 if (func == ftrace_ops_list_func) {
292 ftrace_trace_function = func;
294 * Don't even bother setting function_trace_ops,
295 * it would be racy to do so anyway.
300 #ifndef CONFIG_DYNAMIC_FTRACE
302 * For static tracing, we need to be a bit more careful.
303 * The function change takes affect immediately. Thus,
304 * we need to coorditate the setting of the function_trace_ops
305 * with the setting of the ftrace_trace_function.
307 * Set the function to the list ops, which will call the
308 * function we want, albeit indirectly, but it handles the
309 * ftrace_ops and doesn't depend on function_trace_op.
311 ftrace_trace_function = ftrace_ops_list_func;
313 * Make sure all CPUs see this. Yes this is slow, but static
314 * tracing is slow and nasty to have enabled.
316 schedule_on_each_cpu(ftrace_sync);
317 /* Now all cpus are using the list ops. */
318 function_trace_op = set_function_trace_op;
319 /* Make sure the function_trace_op is visible on all CPUs */
321 /* Nasty way to force a rmb on all cpus */
322 smp_call_function(ftrace_sync_ipi, NULL, 1);
323 /* OK, we are all set to update the ftrace_trace_function now! */
324 #endif /* !CONFIG_DYNAMIC_FTRACE */
326 ftrace_trace_function = func;
329 int using_ftrace_ops_list_func(void)
331 return ftrace_trace_function == ftrace_ops_list_func;
334 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
338 * We are entering ops into the list but another
339 * CPU might be walking that list. We need to make sure
340 * the ops->next pointer is valid before another CPU sees
341 * the ops pointer included into the list.
343 rcu_assign_pointer(*list, ops);
346 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
348 struct ftrace_ops **p;
351 * If we are removing the last function, then simply point
352 * to the ftrace_stub.
354 if (*list == ops && ops->next == &ftrace_list_end) {
355 *list = &ftrace_list_end;
359 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
370 static void add_ftrace_list_ops(struct ftrace_ops **list,
371 struct ftrace_ops *main_ops,
372 struct ftrace_ops *ops)
374 int first = *list == &ftrace_list_end;
375 add_ftrace_ops(list, ops);
377 add_ftrace_ops(&ftrace_ops_list, main_ops);
380 static int remove_ftrace_list_ops(struct ftrace_ops **list,
381 struct ftrace_ops *main_ops,
382 struct ftrace_ops *ops)
384 int ret = remove_ftrace_ops(list, ops);
385 if (!ret && *list == &ftrace_list_end)
386 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
390 static void ftrace_update_trampoline(struct ftrace_ops *ops);
392 static int __register_ftrace_function(struct ftrace_ops *ops)
394 if (ops->flags & FTRACE_OPS_FL_DELETED)
397 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
400 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
402 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
403 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
404 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
406 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
407 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
410 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
411 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
414 if (!core_kernel_data((unsigned long)ops))
415 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
417 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
418 if (control_ops_alloc(ops))
420 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
421 /* The control_ops needs the trampoline update */
424 add_ftrace_ops(&ftrace_ops_list, ops);
426 ftrace_update_trampoline(ops);
429 update_ftrace_function();
434 static int __unregister_ftrace_function(struct ftrace_ops *ops)
438 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
441 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
442 ret = remove_ftrace_list_ops(&ftrace_control_list,
445 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
451 update_ftrace_function();
456 static void ftrace_update_pid_func(void)
458 /* Only do something if we are tracing something */
459 if (ftrace_trace_function == ftrace_stub)
462 update_ftrace_function();
465 #ifdef CONFIG_FUNCTION_PROFILER
466 struct ftrace_profile {
467 struct hlist_node node;
469 unsigned long counter;
470 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
471 unsigned long long time;
472 unsigned long long time_squared;
476 struct ftrace_profile_page {
477 struct ftrace_profile_page *next;
479 struct ftrace_profile records[];
482 struct ftrace_profile_stat {
484 struct hlist_head *hash;
485 struct ftrace_profile_page *pages;
486 struct ftrace_profile_page *start;
487 struct tracer_stat stat;
490 #define PROFILE_RECORDS_SIZE \
491 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
493 #define PROFILES_PER_PAGE \
494 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
496 static int ftrace_profile_enabled __read_mostly;
498 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
499 static DEFINE_MUTEX(ftrace_profile_lock);
501 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
503 #define FTRACE_PROFILE_HASH_BITS 10
504 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
507 function_stat_next(void *v, int idx)
509 struct ftrace_profile *rec = v;
510 struct ftrace_profile_page *pg;
512 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
518 if ((void *)rec >= (void *)&pg->records[pg->index]) {
522 rec = &pg->records[0];
530 static void *function_stat_start(struct tracer_stat *trace)
532 struct ftrace_profile_stat *stat =
533 container_of(trace, struct ftrace_profile_stat, stat);
535 if (!stat || !stat->start)
538 return function_stat_next(&stat->start->records[0], 0);
541 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
542 /* function graph compares on total time */
543 static int function_stat_cmp(void *p1, void *p2)
545 struct ftrace_profile *a = p1;
546 struct ftrace_profile *b = p2;
548 if (a->time < b->time)
550 if (a->time > b->time)
556 /* not function graph compares against hits */
557 static int function_stat_cmp(void *p1, void *p2)
559 struct ftrace_profile *a = p1;
560 struct ftrace_profile *b = p2;
562 if (a->counter < b->counter)
564 if (a->counter > b->counter)
571 static int function_stat_headers(struct seq_file *m)
573 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
574 seq_puts(m, " Function "
577 "--- ---- --- ---\n");
579 seq_puts(m, " Function Hit\n"
585 static int function_stat_show(struct seq_file *m, void *v)
587 struct ftrace_profile *rec = v;
588 char str[KSYM_SYMBOL_LEN];
590 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
591 static struct trace_seq s;
592 unsigned long long avg;
593 unsigned long long stddev;
595 mutex_lock(&ftrace_profile_lock);
597 /* we raced with function_profile_reset() */
598 if (unlikely(rec->counter == 0)) {
603 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
604 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
606 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
609 do_div(avg, rec->counter);
611 /* Sample standard deviation (s^2) */
612 if (rec->counter <= 1)
616 * Apply Welford's method:
617 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
619 stddev = rec->counter * rec->time_squared -
620 rec->time * rec->time;
623 * Divide only 1000 for ns^2 -> us^2 conversion.
624 * trace_print_graph_duration will divide 1000 again.
626 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
630 trace_print_graph_duration(rec->time, &s);
631 trace_seq_puts(&s, " ");
632 trace_print_graph_duration(avg, &s);
633 trace_seq_puts(&s, " ");
634 trace_print_graph_duration(stddev, &s);
635 trace_print_seq(m, &s);
639 mutex_unlock(&ftrace_profile_lock);
644 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
646 struct ftrace_profile_page *pg;
648 pg = stat->pages = stat->start;
651 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
656 memset(stat->hash, 0,
657 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
660 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
662 struct ftrace_profile_page *pg;
667 /* If we already allocated, do nothing */
671 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
675 #ifdef CONFIG_DYNAMIC_FTRACE
676 functions = ftrace_update_tot_cnt;
679 * We do not know the number of functions that exist because
680 * dynamic tracing is what counts them. With past experience
681 * we have around 20K functions. That should be more than enough.
682 * It is highly unlikely we will execute every function in
688 pg = stat->start = stat->pages;
690 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
692 for (i = 1; i < pages; i++) {
693 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
704 unsigned long tmp = (unsigned long)pg;
716 static int ftrace_profile_init_cpu(int cpu)
718 struct ftrace_profile_stat *stat;
721 stat = &per_cpu(ftrace_profile_stats, cpu);
724 /* If the profile is already created, simply reset it */
725 ftrace_profile_reset(stat);
730 * We are profiling all functions, but usually only a few thousand
731 * functions are hit. We'll make a hash of 1024 items.
733 size = FTRACE_PROFILE_HASH_SIZE;
735 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
740 /* Preallocate the function profiling pages */
741 if (ftrace_profile_pages_init(stat) < 0) {
750 static int ftrace_profile_init(void)
755 for_each_possible_cpu(cpu) {
756 ret = ftrace_profile_init_cpu(cpu);
764 /* interrupts must be disabled */
765 static struct ftrace_profile *
766 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
768 struct ftrace_profile *rec;
769 struct hlist_head *hhd;
772 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
773 hhd = &stat->hash[key];
775 if (hlist_empty(hhd))
778 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
786 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
787 struct ftrace_profile *rec)
791 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
792 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
796 * The memory is already allocated, this simply finds a new record to use.
798 static struct ftrace_profile *
799 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
801 struct ftrace_profile *rec = NULL;
803 /* prevent recursion (from NMIs) */
804 if (atomic_inc_return(&stat->disabled) != 1)
808 * Try to find the function again since an NMI
809 * could have added it
811 rec = ftrace_find_profiled_func(stat, ip);
815 if (stat->pages->index == PROFILES_PER_PAGE) {
816 if (!stat->pages->next)
818 stat->pages = stat->pages->next;
821 rec = &stat->pages->records[stat->pages->index++];
823 ftrace_add_profile(stat, rec);
826 atomic_dec(&stat->disabled);
832 function_profile_call(unsigned long ip, unsigned long parent_ip,
833 struct ftrace_ops *ops, struct pt_regs *regs)
835 struct ftrace_profile_stat *stat;
836 struct ftrace_profile *rec;
839 if (!ftrace_profile_enabled)
842 local_irq_save(flags);
844 stat = this_cpu_ptr(&ftrace_profile_stats);
845 if (!stat->hash || !ftrace_profile_enabled)
848 rec = ftrace_find_profiled_func(stat, ip);
850 rec = ftrace_profile_alloc(stat, ip);
857 local_irq_restore(flags);
860 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
861 static int profile_graph_entry(struct ftrace_graph_ent *trace)
863 function_profile_call(trace->func, 0, NULL, NULL);
867 static void profile_graph_return(struct ftrace_graph_ret *trace)
869 struct ftrace_profile_stat *stat;
870 unsigned long long calltime;
871 struct ftrace_profile *rec;
874 local_irq_save(flags);
875 stat = this_cpu_ptr(&ftrace_profile_stats);
876 if (!stat->hash || !ftrace_profile_enabled)
879 /* If the calltime was zero'd ignore it */
880 if (!trace->calltime)
883 calltime = trace->rettime - trace->calltime;
885 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
888 index = trace->depth;
890 /* Append this call time to the parent time to subtract */
892 current->ret_stack[index - 1].subtime += calltime;
894 if (current->ret_stack[index].subtime < calltime)
895 calltime -= current->ret_stack[index].subtime;
900 rec = ftrace_find_profiled_func(stat, trace->func);
902 rec->time += calltime;
903 rec->time_squared += calltime * calltime;
907 local_irq_restore(flags);
910 static int register_ftrace_profiler(void)
912 return register_ftrace_graph(&profile_graph_return,
913 &profile_graph_entry);
916 static void unregister_ftrace_profiler(void)
918 unregister_ftrace_graph();
921 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
922 .func = function_profile_call,
923 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
924 INIT_OPS_HASH(ftrace_profile_ops)
927 static int register_ftrace_profiler(void)
929 return register_ftrace_function(&ftrace_profile_ops);
932 static void unregister_ftrace_profiler(void)
934 unregister_ftrace_function(&ftrace_profile_ops);
936 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
939 ftrace_profile_write(struct file *filp, const char __user *ubuf,
940 size_t cnt, loff_t *ppos)
945 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
951 mutex_lock(&ftrace_profile_lock);
952 if (ftrace_profile_enabled ^ val) {
954 ret = ftrace_profile_init();
960 ret = register_ftrace_profiler();
965 ftrace_profile_enabled = 1;
967 ftrace_profile_enabled = 0;
969 * unregister_ftrace_profiler calls stop_machine
970 * so this acts like an synchronize_sched.
972 unregister_ftrace_profiler();
976 mutex_unlock(&ftrace_profile_lock);
984 ftrace_profile_read(struct file *filp, char __user *ubuf,
985 size_t cnt, loff_t *ppos)
987 char buf[64]; /* big enough to hold a number */
990 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
991 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
994 static const struct file_operations ftrace_profile_fops = {
995 .open = tracing_open_generic,
996 .read = ftrace_profile_read,
997 .write = ftrace_profile_write,
998 .llseek = default_llseek,
1001 /* used to initialize the real stat files */
1002 static struct tracer_stat function_stats __initdata = {
1003 .name = "functions",
1004 .stat_start = function_stat_start,
1005 .stat_next = function_stat_next,
1006 .stat_cmp = function_stat_cmp,
1007 .stat_headers = function_stat_headers,
1008 .stat_show = function_stat_show
1011 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1013 struct ftrace_profile_stat *stat;
1014 struct dentry *entry;
1019 for_each_possible_cpu(cpu) {
1020 stat = &per_cpu(ftrace_profile_stats, cpu);
1022 /* allocate enough for function name + cpu number */
1023 name = kmalloc(32, GFP_KERNEL);
1026 * The files created are permanent, if something happens
1027 * we still do not free memory.
1030 "Could not allocate stat file for cpu %d\n",
1034 stat->stat = function_stats;
1035 snprintf(name, 32, "function%d", cpu);
1036 stat->stat.name = name;
1037 ret = register_stat_tracer(&stat->stat);
1040 "Could not register function stat for cpu %d\n",
1047 entry = debugfs_create_file("function_profile_enabled", 0644,
1048 d_tracer, NULL, &ftrace_profile_fops);
1050 pr_warning("Could not create debugfs "
1051 "'function_profile_enabled' entry\n");
1054 #else /* CONFIG_FUNCTION_PROFILER */
1055 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1058 #endif /* CONFIG_FUNCTION_PROFILER */
1060 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1062 #ifdef CONFIG_DYNAMIC_FTRACE
1064 static struct ftrace_ops *removed_ops;
1067 * Set when doing a global update, like enabling all recs or disabling them.
1068 * It is not set when just updating a single ftrace_ops.
1070 static bool update_all_ops;
1072 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1073 # error Dynamic ftrace depends on MCOUNT_RECORD
1076 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1078 struct ftrace_func_probe {
1079 struct hlist_node node;
1080 struct ftrace_probe_ops *ops;
1081 unsigned long flags;
1084 struct list_head free_list;
1087 struct ftrace_func_entry {
1088 struct hlist_node hlist;
1092 struct ftrace_hash {
1093 unsigned long size_bits;
1094 struct hlist_head *buckets;
1095 unsigned long count;
1096 struct rcu_head rcu;
1100 * We make these constant because no one should touch them,
1101 * but they are used as the default "empty hash", to avoid allocating
1102 * it all the time. These are in a read only section such that if
1103 * anyone does try to modify it, it will cause an exception.
1105 static const struct hlist_head empty_buckets[1];
1106 static const struct ftrace_hash empty_hash = {
1107 .buckets = (struct hlist_head *)empty_buckets,
1109 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1111 static struct ftrace_ops global_ops = {
1112 .func = ftrace_stub,
1113 .local_hash.notrace_hash = EMPTY_HASH,
1114 .local_hash.filter_hash = EMPTY_HASH,
1115 INIT_OPS_HASH(global_ops)
1116 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1117 FTRACE_OPS_FL_INITIALIZED,
1120 struct ftrace_page {
1121 struct ftrace_page *next;
1122 struct dyn_ftrace *records;
1127 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1128 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1130 /* estimate from running different kernels */
1131 #define NR_TO_INIT 10000
1133 static struct ftrace_page *ftrace_pages_start;
1134 static struct ftrace_page *ftrace_pages;
1136 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1138 return !hash || !hash->count;
1141 static struct ftrace_func_entry *
1142 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1145 struct ftrace_func_entry *entry;
1146 struct hlist_head *hhd;
1148 if (ftrace_hash_empty(hash))
1151 if (hash->size_bits > 0)
1152 key = hash_long(ip, hash->size_bits);
1156 hhd = &hash->buckets[key];
1158 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1159 if (entry->ip == ip)
1165 static void __add_hash_entry(struct ftrace_hash *hash,
1166 struct ftrace_func_entry *entry)
1168 struct hlist_head *hhd;
1171 if (hash->size_bits)
1172 key = hash_long(entry->ip, hash->size_bits);
1176 hhd = &hash->buckets[key];
1177 hlist_add_head(&entry->hlist, hhd);
1181 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1183 struct ftrace_func_entry *entry;
1185 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1190 __add_hash_entry(hash, entry);
1196 free_hash_entry(struct ftrace_hash *hash,
1197 struct ftrace_func_entry *entry)
1199 hlist_del(&entry->hlist);
1205 remove_hash_entry(struct ftrace_hash *hash,
1206 struct ftrace_func_entry *entry)
1208 hlist_del(&entry->hlist);
1212 static void ftrace_hash_clear(struct ftrace_hash *hash)
1214 struct hlist_head *hhd;
1215 struct hlist_node *tn;
1216 struct ftrace_func_entry *entry;
1217 int size = 1 << hash->size_bits;
1223 for (i = 0; i < size; i++) {
1224 hhd = &hash->buckets[i];
1225 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1226 free_hash_entry(hash, entry);
1228 FTRACE_WARN_ON(hash->count);
1231 static void free_ftrace_hash(struct ftrace_hash *hash)
1233 if (!hash || hash == EMPTY_HASH)
1235 ftrace_hash_clear(hash);
1236 kfree(hash->buckets);
1240 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1242 struct ftrace_hash *hash;
1244 hash = container_of(rcu, struct ftrace_hash, rcu);
1245 free_ftrace_hash(hash);
1248 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1250 if (!hash || hash == EMPTY_HASH)
1252 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1255 void ftrace_free_filter(struct ftrace_ops *ops)
1257 ftrace_ops_init(ops);
1258 free_ftrace_hash(ops->func_hash->filter_hash);
1259 free_ftrace_hash(ops->func_hash->notrace_hash);
1262 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1264 struct ftrace_hash *hash;
1267 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1271 size = 1 << size_bits;
1272 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1274 if (!hash->buckets) {
1279 hash->size_bits = size_bits;
1284 static struct ftrace_hash *
1285 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1287 struct ftrace_func_entry *entry;
1288 struct ftrace_hash *new_hash;
1293 new_hash = alloc_ftrace_hash(size_bits);
1298 if (ftrace_hash_empty(hash))
1301 size = 1 << hash->size_bits;
1302 for (i = 0; i < size; i++) {
1303 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1304 ret = add_hash_entry(new_hash, entry->ip);
1310 FTRACE_WARN_ON(new_hash->count != hash->count);
1315 free_ftrace_hash(new_hash);
1320 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1322 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1325 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1326 struct ftrace_hash **dst, struct ftrace_hash *src)
1328 struct ftrace_func_entry *entry;
1329 struct hlist_node *tn;
1330 struct hlist_head *hhd;
1331 struct ftrace_hash *new_hash;
1332 int size = src->count;
1337 * If the new source is empty, just free dst and assign it
1341 new_hash = EMPTY_HASH;
1346 * Make the hash size about 1/2 the # found
1348 for (size /= 2; size; size >>= 1)
1351 /* Don't allocate too much */
1352 if (bits > FTRACE_HASH_MAX_BITS)
1353 bits = FTRACE_HASH_MAX_BITS;
1355 new_hash = alloc_ftrace_hash(bits);
1359 size = 1 << src->size_bits;
1360 for (i = 0; i < size; i++) {
1361 hhd = &src->buckets[i];
1362 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1363 remove_hash_entry(src, entry);
1364 __add_hash_entry(new_hash, entry);
1370 * Remove the current set, update the hash and add
1373 ftrace_hash_rec_disable_modify(ops, enable);
1375 rcu_assign_pointer(*dst, new_hash);
1377 ftrace_hash_rec_enable_modify(ops, enable);
1382 static bool hash_contains_ip(unsigned long ip,
1383 struct ftrace_ops_hash *hash)
1386 * The function record is a match if it exists in the filter
1387 * hash and not in the notrace hash. Note, an emty hash is
1388 * considered a match for the filter hash, but an empty
1389 * notrace hash is considered not in the notrace hash.
1391 return (ftrace_hash_empty(hash->filter_hash) ||
1392 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1393 (ftrace_hash_empty(hash->notrace_hash) ||
1394 !ftrace_lookup_ip(hash->notrace_hash, ip));
1398 * Test the hashes for this ops to see if we want to call
1399 * the ops->func or not.
1401 * It's a match if the ip is in the ops->filter_hash or
1402 * the filter_hash does not exist or is empty,
1404 * the ip is not in the ops->notrace_hash.
1406 * This needs to be called with preemption disabled as
1407 * the hashes are freed with call_rcu_sched().
1410 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1412 struct ftrace_ops_hash hash;
1415 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1417 * There's a small race when adding ops that the ftrace handler
1418 * that wants regs, may be called without them. We can not
1419 * allow that handler to be called if regs is NULL.
1421 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1425 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1426 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
1428 if (hash_contains_ip(ip, &hash))
1437 * This is a double for. Do not use 'break' to break out of the loop,
1438 * you must use a goto.
1440 #define do_for_each_ftrace_rec(pg, rec) \
1441 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1443 for (_____i = 0; _____i < pg->index; _____i++) { \
1444 rec = &pg->records[_____i];
1446 #define while_for_each_ftrace_rec() \
1451 static int ftrace_cmp_recs(const void *a, const void *b)
1453 const struct dyn_ftrace *key = a;
1454 const struct dyn_ftrace *rec = b;
1456 if (key->flags < rec->ip)
1458 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1463 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1465 struct ftrace_page *pg;
1466 struct dyn_ftrace *rec;
1467 struct dyn_ftrace key;
1470 key.flags = end; /* overload flags, as it is unsigned long */
1472 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1473 if (end < pg->records[0].ip ||
1474 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1476 rec = bsearch(&key, pg->records, pg->index,
1477 sizeof(struct dyn_ftrace),
1487 * ftrace_location - return true if the ip giving is a traced location
1488 * @ip: the instruction pointer to check
1490 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1491 * That is, the instruction that is either a NOP or call to
1492 * the function tracer. It checks the ftrace internal tables to
1493 * determine if the address belongs or not.
1495 unsigned long ftrace_location(unsigned long ip)
1497 return ftrace_location_range(ip, ip);
1501 * ftrace_text_reserved - return true if range contains an ftrace location
1502 * @start: start of range to search
1503 * @end: end of range to search (inclusive). @end points to the last byte to check.
1505 * Returns 1 if @start and @end contains a ftrace location.
1506 * That is, the instruction that is either a NOP or call to
1507 * the function tracer. It checks the ftrace internal tables to
1508 * determine if the address belongs or not.
1510 int ftrace_text_reserved(const void *start, const void *end)
1514 ret = ftrace_location_range((unsigned long)start,
1515 (unsigned long)end);
1520 /* Test if ops registered to this rec needs regs */
1521 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1523 struct ftrace_ops *ops;
1524 bool keep_regs = false;
1526 for (ops = ftrace_ops_list;
1527 ops != &ftrace_list_end; ops = ops->next) {
1528 /* pass rec in as regs to have non-NULL val */
1529 if (ftrace_ops_test(ops, rec->ip, rec)) {
1530 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1540 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1544 struct ftrace_hash *hash;
1545 struct ftrace_hash *other_hash;
1546 struct ftrace_page *pg;
1547 struct dyn_ftrace *rec;
1551 /* Only update if the ops has been registered */
1552 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1556 * In the filter_hash case:
1557 * If the count is zero, we update all records.
1558 * Otherwise we just update the items in the hash.
1560 * In the notrace_hash case:
1561 * We enable the update in the hash.
1562 * As disabling notrace means enabling the tracing,
1563 * and enabling notrace means disabling, the inc variable
1567 hash = ops->func_hash->filter_hash;
1568 other_hash = ops->func_hash->notrace_hash;
1569 if (ftrace_hash_empty(hash))
1573 hash = ops->func_hash->notrace_hash;
1574 other_hash = ops->func_hash->filter_hash;
1576 * If the notrace hash has no items,
1577 * then there's nothing to do.
1579 if (ftrace_hash_empty(hash))
1583 do_for_each_ftrace_rec(pg, rec) {
1584 int in_other_hash = 0;
1590 * Only the filter_hash affects all records.
1591 * Update if the record is not in the notrace hash.
1593 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1596 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1597 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1600 * If filter_hash is set, we want to match all functions
1601 * that are in the hash but not in the other hash.
1603 * If filter_hash is not set, then we are decrementing.
1604 * That means we match anything that is in the hash
1605 * and also in the other_hash. That is, we need to turn
1606 * off functions in the other hash because they are disabled
1609 if (filter_hash && in_hash && !in_other_hash)
1611 else if (!filter_hash && in_hash &&
1612 (in_other_hash || ftrace_hash_empty(other_hash)))
1620 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1624 * If there's only a single callback registered to a
1625 * function, and the ops has a trampoline registered
1626 * for it, then we can call it directly.
1628 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1629 rec->flags |= FTRACE_FL_TRAMP;
1632 * If we are adding another function callback
1633 * to this function, and the previous had a
1634 * custom trampoline in use, then we need to go
1635 * back to the default trampoline.
1637 rec->flags &= ~FTRACE_FL_TRAMP;
1640 * If any ops wants regs saved for this function
1641 * then all ops will get saved regs.
1643 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1644 rec->flags |= FTRACE_FL_REGS;
1646 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1651 * If the rec had REGS enabled and the ops that is
1652 * being removed had REGS set, then see if there is
1653 * still any ops for this record that wants regs.
1654 * If not, we can stop recording them.
1656 if (ftrace_rec_count(rec) > 0 &&
1657 rec->flags & FTRACE_FL_REGS &&
1658 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1659 if (!test_rec_ops_needs_regs(rec))
1660 rec->flags &= ~FTRACE_FL_REGS;
1664 * If the rec had TRAMP enabled, then it needs to
1665 * be cleared. As TRAMP can only be enabled iff
1666 * there is only a single ops attached to it.
1667 * In otherwords, always disable it on decrementing.
1668 * In the future, we may set it if rec count is
1669 * decremented to one, and the ops that is left
1672 rec->flags &= ~FTRACE_FL_TRAMP;
1675 * flags will be cleared in ftrace_check_record()
1676 * if rec count is zero.
1680 /* Shortcut, if we handled all records, we are done. */
1681 if (!all && count == hash->count)
1683 } while_for_each_ftrace_rec();
1686 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1689 __ftrace_hash_rec_update(ops, filter_hash, 0);
1692 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1695 __ftrace_hash_rec_update(ops, filter_hash, 1);
1698 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1699 int filter_hash, int inc)
1701 struct ftrace_ops *op;
1703 __ftrace_hash_rec_update(ops, filter_hash, inc);
1705 if (ops->func_hash != &global_ops.local_hash)
1709 * If the ops shares the global_ops hash, then we need to update
1710 * all ops that are enabled and use this hash.
1712 do_for_each_ftrace_op(op, ftrace_ops_list) {
1716 if (op->func_hash == &global_ops.local_hash)
1717 __ftrace_hash_rec_update(op, filter_hash, inc);
1718 } while_for_each_ftrace_op(op);
1721 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1724 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1727 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1730 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1733 static void print_ip_ins(const char *fmt, unsigned char *p)
1737 printk(KERN_CONT "%s", fmt);
1739 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1740 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1743 static struct ftrace_ops *
1744 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1747 * ftrace_bug - report and shutdown function tracer
1748 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1749 * @rec: The record that failed
1751 * The arch code that enables or disables the function tracing
1752 * can call ftrace_bug() when it has detected a problem in
1753 * modifying the code. @failed should be one of either:
1754 * EFAULT - if the problem happens on reading the @ip address
1755 * EINVAL - if what is read at @ip is not what was expected
1756 * EPERM - if the problem happens on writting to the @ip address
1758 void ftrace_bug(int failed, struct dyn_ftrace *rec)
1760 unsigned long ip = rec ? rec->ip : 0;
1764 FTRACE_WARN_ON_ONCE(1);
1765 pr_info("ftrace faulted on modifying ");
1769 FTRACE_WARN_ON_ONCE(1);
1770 pr_info("ftrace failed to modify ");
1772 print_ip_ins(" actual: ", (unsigned char *)ip);
1776 FTRACE_WARN_ON_ONCE(1);
1777 pr_info("ftrace faulted on writing ");
1781 FTRACE_WARN_ON_ONCE(1);
1782 pr_info("ftrace faulted on unknown error ");
1786 struct ftrace_ops *ops = NULL;
1788 pr_info("ftrace record flags: %lx\n", rec->flags);
1789 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
1790 rec->flags & FTRACE_FL_REGS ? " R" : " ");
1791 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1792 ops = ftrace_find_tramp_ops_any(rec);
1794 pr_cont("\ttramp: %pS",
1795 (void *)ops->trampoline);
1797 pr_cont("\ttramp: ERROR!");
1800 ip = ftrace_get_addr_curr(rec);
1801 pr_cont(" expected tramp: %lx\n", ip);
1805 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1807 unsigned long flag = 0UL;
1810 * If we are updating calls:
1812 * If the record has a ref count, then we need to enable it
1813 * because someone is using it.
1815 * Otherwise we make sure its disabled.
1817 * If we are disabling calls, then disable all records that
1820 if (enable && ftrace_rec_count(rec))
1821 flag = FTRACE_FL_ENABLED;
1824 * If enabling and the REGS flag does not match the REGS_EN, or
1825 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1826 * this record. Set flags to fail the compare against ENABLED.
1829 if (!(rec->flags & FTRACE_FL_REGS) !=
1830 !(rec->flags & FTRACE_FL_REGS_EN))
1831 flag |= FTRACE_FL_REGS;
1833 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1834 !(rec->flags & FTRACE_FL_TRAMP_EN))
1835 flag |= FTRACE_FL_TRAMP;
1838 /* If the state of this record hasn't changed, then do nothing */
1839 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1840 return FTRACE_UPDATE_IGNORE;
1843 /* Save off if rec is being enabled (for return value) */
1844 flag ^= rec->flags & FTRACE_FL_ENABLED;
1847 rec->flags |= FTRACE_FL_ENABLED;
1848 if (flag & FTRACE_FL_REGS) {
1849 if (rec->flags & FTRACE_FL_REGS)
1850 rec->flags |= FTRACE_FL_REGS_EN;
1852 rec->flags &= ~FTRACE_FL_REGS_EN;
1854 if (flag & FTRACE_FL_TRAMP) {
1855 if (rec->flags & FTRACE_FL_TRAMP)
1856 rec->flags |= FTRACE_FL_TRAMP_EN;
1858 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1863 * If this record is being updated from a nop, then
1864 * return UPDATE_MAKE_CALL.
1866 * return UPDATE_MODIFY_CALL to tell the caller to convert
1867 * from the save regs, to a non-save regs function or
1868 * vice versa, or from a trampoline call.
1870 if (flag & FTRACE_FL_ENABLED)
1871 return FTRACE_UPDATE_MAKE_CALL;
1873 return FTRACE_UPDATE_MODIFY_CALL;
1877 /* If there's no more users, clear all flags */
1878 if (!ftrace_rec_count(rec))
1881 /* Just disable the record (keep REGS state) */
1882 rec->flags &= ~FTRACE_FL_ENABLED;
1885 return FTRACE_UPDATE_MAKE_NOP;
1889 * ftrace_update_record, set a record that now is tracing or not
1890 * @rec: the record to update
1891 * @enable: set to 1 if the record is tracing, zero to force disable
1893 * The records that represent all functions that can be traced need
1894 * to be updated when tracing has been enabled.
1896 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1898 return ftrace_check_record(rec, enable, 1);
1902 * ftrace_test_record, check if the record has been enabled or not
1903 * @rec: the record to test
1904 * @enable: set to 1 to check if enabled, 0 if it is disabled
1906 * The arch code may need to test if a record is already set to
1907 * tracing to determine how to modify the function code that it
1910 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1912 return ftrace_check_record(rec, enable, 0);
1915 static struct ftrace_ops *
1916 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
1918 struct ftrace_ops *op;
1919 unsigned long ip = rec->ip;
1921 do_for_each_ftrace_op(op, ftrace_ops_list) {
1923 if (!op->trampoline)
1926 if (hash_contains_ip(ip, op->func_hash))
1928 } while_for_each_ftrace_op(op);
1933 static struct ftrace_ops *
1934 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1936 struct ftrace_ops *op;
1937 unsigned long ip = rec->ip;
1940 * Need to check removed ops first.
1941 * If they are being removed, and this rec has a tramp,
1942 * and this rec is in the ops list, then it would be the
1943 * one with the tramp.
1946 if (hash_contains_ip(ip, &removed_ops->old_hash))
1951 * Need to find the current trampoline for a rec.
1952 * Now, a trampoline is only attached to a rec if there
1953 * was a single 'ops' attached to it. But this can be called
1954 * when we are adding another op to the rec or removing the
1955 * current one. Thus, if the op is being added, we can
1956 * ignore it because it hasn't attached itself to the rec
1959 * If an ops is being modified (hooking to different functions)
1960 * then we don't care about the new functions that are being
1961 * added, just the old ones (that are probably being removed).
1963 * If we are adding an ops to a function that already is using
1964 * a trampoline, it needs to be removed (trampolines are only
1965 * for single ops connected), then an ops that is not being
1966 * modified also needs to be checked.
1968 do_for_each_ftrace_op(op, ftrace_ops_list) {
1970 if (!op->trampoline)
1974 * If the ops is being added, it hasn't gotten to
1975 * the point to be removed from this tree yet.
1977 if (op->flags & FTRACE_OPS_FL_ADDING)
1982 * If the ops is being modified and is in the old
1983 * hash, then it is probably being removed from this
1986 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
1987 hash_contains_ip(ip, &op->old_hash))
1990 * If the ops is not being added or modified, and it's
1991 * in its normal filter hash, then this must be the one
1994 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
1995 hash_contains_ip(ip, op->func_hash))
1998 } while_for_each_ftrace_op(op);
2003 static struct ftrace_ops *
2004 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2006 struct ftrace_ops *op;
2007 unsigned long ip = rec->ip;
2009 do_for_each_ftrace_op(op, ftrace_ops_list) {
2010 /* pass rec in as regs to have non-NULL val */
2011 if (hash_contains_ip(ip, op->func_hash))
2013 } while_for_each_ftrace_op(op);
2019 * ftrace_get_addr_new - Get the call address to set to
2020 * @rec: The ftrace record descriptor
2022 * If the record has the FTRACE_FL_REGS set, that means that it
2023 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2024 * is not not set, then it wants to convert to the normal callback.
2026 * Returns the address of the trampoline to set to
2028 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2030 struct ftrace_ops *ops;
2032 /* Trampolines take precedence over regs */
2033 if (rec->flags & FTRACE_FL_TRAMP) {
2034 ops = ftrace_find_tramp_ops_new(rec);
2035 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2036 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2037 (void *)rec->ip, (void *)rec->ip, rec->flags);
2038 /* Ftrace is shutting down, return anything */
2039 return (unsigned long)FTRACE_ADDR;
2041 return ops->trampoline;
2044 if (rec->flags & FTRACE_FL_REGS)
2045 return (unsigned long)FTRACE_REGS_ADDR;
2047 return (unsigned long)FTRACE_ADDR;
2051 * ftrace_get_addr_curr - Get the call address that is already there
2052 * @rec: The ftrace record descriptor
2054 * The FTRACE_FL_REGS_EN is set when the record already points to
2055 * a function that saves all the regs. Basically the '_EN' version
2056 * represents the current state of the function.
2058 * Returns the address of the trampoline that is currently being called
2060 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2062 struct ftrace_ops *ops;
2064 /* Trampolines take precedence over regs */
2065 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2066 ops = ftrace_find_tramp_ops_curr(rec);
2067 if (FTRACE_WARN_ON(!ops)) {
2068 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2069 (void *)rec->ip, (void *)rec->ip);
2070 /* Ftrace is shutting down, return anything */
2071 return (unsigned long)FTRACE_ADDR;
2073 return ops->trampoline;
2076 if (rec->flags & FTRACE_FL_REGS_EN)
2077 return (unsigned long)FTRACE_REGS_ADDR;
2079 return (unsigned long)FTRACE_ADDR;
2083 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2085 unsigned long ftrace_old_addr;
2086 unsigned long ftrace_addr;
2089 ftrace_addr = ftrace_get_addr_new(rec);
2091 /* This needs to be done before we call ftrace_update_record */
2092 ftrace_old_addr = ftrace_get_addr_curr(rec);
2094 ret = ftrace_update_record(rec, enable);
2097 case FTRACE_UPDATE_IGNORE:
2100 case FTRACE_UPDATE_MAKE_CALL:
2101 return ftrace_make_call(rec, ftrace_addr);
2103 case FTRACE_UPDATE_MAKE_NOP:
2104 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2106 case FTRACE_UPDATE_MODIFY_CALL:
2107 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2110 return -1; /* unknow ftrace bug */
2113 void __weak ftrace_replace_code(int enable)
2115 struct dyn_ftrace *rec;
2116 struct ftrace_page *pg;
2119 if (unlikely(ftrace_disabled))
2122 do_for_each_ftrace_rec(pg, rec) {
2123 failed = __ftrace_replace_code(rec, enable);
2125 ftrace_bug(failed, rec);
2126 /* Stop processing */
2129 } while_for_each_ftrace_rec();
2132 struct ftrace_rec_iter {
2133 struct ftrace_page *pg;
2138 * ftrace_rec_iter_start, start up iterating over traced functions
2140 * Returns an iterator handle that is used to iterate over all
2141 * the records that represent address locations where functions
2144 * May return NULL if no records are available.
2146 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2149 * We only use a single iterator.
2150 * Protected by the ftrace_lock mutex.
2152 static struct ftrace_rec_iter ftrace_rec_iter;
2153 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2155 iter->pg = ftrace_pages_start;
2158 /* Could have empty pages */
2159 while (iter->pg && !iter->pg->index)
2160 iter->pg = iter->pg->next;
2169 * ftrace_rec_iter_next, get the next record to process.
2170 * @iter: The handle to the iterator.
2172 * Returns the next iterator after the given iterator @iter.
2174 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2178 if (iter->index >= iter->pg->index) {
2179 iter->pg = iter->pg->next;
2182 /* Could have empty pages */
2183 while (iter->pg && !iter->pg->index)
2184 iter->pg = iter->pg->next;
2194 * ftrace_rec_iter_record, get the record at the iterator location
2195 * @iter: The current iterator location
2197 * Returns the record that the current @iter is at.
2199 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2201 return &iter->pg->records[iter->index];
2205 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2209 if (unlikely(ftrace_disabled))
2212 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2214 ftrace_bug(ret, rec);
2221 * archs can override this function if they must do something
2222 * before the modifying code is performed.
2224 int __weak ftrace_arch_code_modify_prepare(void)
2230 * archs can override this function if they must do something
2231 * after the modifying code is performed.
2233 int __weak ftrace_arch_code_modify_post_process(void)
2238 void ftrace_modify_all_code(int command)
2240 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2244 * If the ftrace_caller calls a ftrace_ops func directly,
2245 * we need to make sure that it only traces functions it
2246 * expects to trace. When doing the switch of functions,
2247 * we need to update to the ftrace_ops_list_func first
2248 * before the transition between old and new calls are set,
2249 * as the ftrace_ops_list_func will check the ops hashes
2250 * to make sure the ops are having the right functions
2254 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2255 if (FTRACE_WARN_ON(err))
2259 if (command & FTRACE_UPDATE_CALLS)
2260 ftrace_replace_code(1);
2261 else if (command & FTRACE_DISABLE_CALLS)
2262 ftrace_replace_code(0);
2264 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2265 function_trace_op = set_function_trace_op;
2267 /* If irqs are disabled, we are in stop machine */
2268 if (!irqs_disabled())
2269 smp_call_function(ftrace_sync_ipi, NULL, 1);
2270 err = ftrace_update_ftrace_func(ftrace_trace_function);
2271 if (FTRACE_WARN_ON(err))
2275 if (command & FTRACE_START_FUNC_RET)
2276 err = ftrace_enable_ftrace_graph_caller();
2277 else if (command & FTRACE_STOP_FUNC_RET)
2278 err = ftrace_disable_ftrace_graph_caller();
2279 FTRACE_WARN_ON(err);
2282 static int __ftrace_modify_code(void *data)
2284 int *command = data;
2286 ftrace_modify_all_code(*command);
2292 * ftrace_run_stop_machine, go back to the stop machine method
2293 * @command: The command to tell ftrace what to do
2295 * If an arch needs to fall back to the stop machine method, the
2296 * it can call this function.
2298 void ftrace_run_stop_machine(int command)
2300 stop_machine(__ftrace_modify_code, &command, NULL);
2304 * arch_ftrace_update_code, modify the code to trace or not trace
2305 * @command: The command that needs to be done
2307 * Archs can override this function if it does not need to
2308 * run stop_machine() to modify code.
2310 void __weak arch_ftrace_update_code(int command)
2312 ftrace_run_stop_machine(command);
2315 static void ftrace_run_update_code(int command)
2319 ret = ftrace_arch_code_modify_prepare();
2320 FTRACE_WARN_ON(ret);
2325 * By default we use stop_machine() to modify the code.
2326 * But archs can do what ever they want as long as it
2327 * is safe. The stop_machine() is the safest, but also
2328 * produces the most overhead.
2330 arch_ftrace_update_code(command);
2332 ret = ftrace_arch_code_modify_post_process();
2333 FTRACE_WARN_ON(ret);
2336 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2337 struct ftrace_hash *old_hash)
2339 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2340 ops->old_hash.filter_hash = old_hash;
2341 ftrace_run_update_code(command);
2342 ops->old_hash.filter_hash = NULL;
2343 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2346 static ftrace_func_t saved_ftrace_func;
2347 static int ftrace_start_up;
2349 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2353 static void control_ops_free(struct ftrace_ops *ops)
2355 free_percpu(ops->disabled);
2358 static void ftrace_startup_enable(int command)
2360 if (saved_ftrace_func != ftrace_trace_function) {
2361 saved_ftrace_func = ftrace_trace_function;
2362 command |= FTRACE_UPDATE_TRACE_FUNC;
2365 if (!command || !ftrace_enabled)
2368 ftrace_run_update_code(command);
2371 static void ftrace_startup_all(int command)
2373 update_all_ops = true;
2374 ftrace_startup_enable(command);
2375 update_all_ops = false;
2378 static int ftrace_startup(struct ftrace_ops *ops, int command)
2382 if (unlikely(ftrace_disabled))
2385 ret = __register_ftrace_function(ops);
2390 command |= FTRACE_UPDATE_CALLS;
2393 * Note that ftrace probes uses this to start up
2394 * and modify functions it will probe. But we still
2395 * set the ADDING flag for modification, as probes
2396 * do not have trampolines. If they add them in the
2397 * future, then the probes will need to distinguish
2398 * between adding and updating probes.
2400 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2402 ftrace_hash_rec_enable(ops, 1);
2404 ftrace_startup_enable(command);
2406 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2411 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2415 if (unlikely(ftrace_disabled))
2418 ret = __unregister_ftrace_function(ops);
2424 * Just warn in case of unbalance, no need to kill ftrace, it's not
2425 * critical but the ftrace_call callers may be never nopped again after
2426 * further ftrace uses.
2428 WARN_ON_ONCE(ftrace_start_up < 0);
2430 ftrace_hash_rec_disable(ops, 1);
2432 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2434 command |= FTRACE_UPDATE_CALLS;
2436 if (saved_ftrace_func != ftrace_trace_function) {
2437 saved_ftrace_func = ftrace_trace_function;
2438 command |= FTRACE_UPDATE_TRACE_FUNC;
2441 if (!command || !ftrace_enabled) {
2443 * If these are control ops, they still need their
2444 * per_cpu field freed. Since, function tracing is
2445 * not currently active, we can just free them
2446 * without synchronizing all CPUs.
2448 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2449 control_ops_free(ops);
2454 * If the ops uses a trampoline, then it needs to be
2455 * tested first on update.
2457 ops->flags |= FTRACE_OPS_FL_REMOVING;
2460 /* The trampoline logic checks the old hashes */
2461 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2462 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2464 ftrace_run_update_code(command);
2467 * If there's no more ops registered with ftrace, run a
2468 * sanity check to make sure all rec flags are cleared.
2470 if (ftrace_ops_list == &ftrace_list_end) {
2471 struct ftrace_page *pg;
2472 struct dyn_ftrace *rec;
2474 do_for_each_ftrace_rec(pg, rec) {
2475 if (FTRACE_WARN_ON_ONCE(rec->flags))
2476 pr_warn(" %pS flags:%lx\n",
2477 (void *)rec->ip, rec->flags);
2478 } while_for_each_ftrace_rec();
2481 ops->old_hash.filter_hash = NULL;
2482 ops->old_hash.notrace_hash = NULL;
2485 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2488 * Dynamic ops may be freed, we must make sure that all
2489 * callers are done before leaving this function.
2490 * The same goes for freeing the per_cpu data of the control
2493 * Again, normal synchronize_sched() is not good enough.
2494 * We need to do a hard force of sched synchronization.
2495 * This is because we use preempt_disable() to do RCU, but
2496 * the function tracers can be called where RCU is not watching
2497 * (like before user_exit()). We can not rely on the RCU
2498 * infrastructure to do the synchronization, thus we must do it
2501 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2502 schedule_on_each_cpu(ftrace_sync);
2504 arch_ftrace_trampoline_free(ops);
2506 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2507 control_ops_free(ops);
2513 static void ftrace_startup_sysctl(void)
2515 if (unlikely(ftrace_disabled))
2518 /* Force update next time */
2519 saved_ftrace_func = NULL;
2520 /* ftrace_start_up is true if we want ftrace running */
2521 if (ftrace_start_up)
2522 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2525 static void ftrace_shutdown_sysctl(void)
2527 if (unlikely(ftrace_disabled))
2530 /* ftrace_start_up is true if ftrace is running */
2531 if (ftrace_start_up)
2532 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2535 static cycle_t ftrace_update_time;
2536 unsigned long ftrace_update_tot_cnt;
2538 static inline int ops_traces_mod(struct ftrace_ops *ops)
2541 * Filter_hash being empty will default to trace module.
2542 * But notrace hash requires a test of individual module functions.
2544 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2545 ftrace_hash_empty(ops->func_hash->notrace_hash);
2549 * Check if the current ops references the record.
2551 * If the ops traces all functions, then it was already accounted for.
2552 * If the ops does not trace the current record function, skip it.
2553 * If the ops ignores the function via notrace filter, skip it.
2556 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2558 /* If ops isn't enabled, ignore it */
2559 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2562 /* If ops traces all mods, we already accounted for it */
2563 if (ops_traces_mod(ops))
2566 /* The function must be in the filter */
2567 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2568 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2571 /* If in notrace hash, we ignore it too */
2572 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2578 static int referenced_filters(struct dyn_ftrace *rec)
2580 struct ftrace_ops *ops;
2583 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2584 if (ops_references_rec(ops, rec))
2591 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2593 struct ftrace_page *pg;
2594 struct dyn_ftrace *p;
2595 cycle_t start, stop;
2596 unsigned long update_cnt = 0;
2597 unsigned long ref = 0;
2602 * When adding a module, we need to check if tracers are
2603 * currently enabled and if they are set to trace all functions.
2604 * If they are, we need to enable the module functions as well
2605 * as update the reference counts for those function records.
2608 struct ftrace_ops *ops;
2610 for (ops = ftrace_ops_list;
2611 ops != &ftrace_list_end; ops = ops->next) {
2612 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2613 if (ops_traces_mod(ops))
2621 start = ftrace_now(raw_smp_processor_id());
2623 for (pg = new_pgs; pg; pg = pg->next) {
2625 for (i = 0; i < pg->index; i++) {
2628 /* If something went wrong, bail without enabling anything */
2629 if (unlikely(ftrace_disabled))
2632 p = &pg->records[i];
2634 cnt += referenced_filters(p);
2638 * Do the initial record conversion from mcount jump
2639 * to the NOP instructions.
2641 if (!ftrace_code_disable(mod, p))
2647 * If the tracing is enabled, go ahead and enable the record.
2649 * The reason not to enable the record immediatelly is the
2650 * inherent check of ftrace_make_nop/ftrace_make_call for
2651 * correct previous instructions. Making first the NOP
2652 * conversion puts the module to the correct state, thus
2653 * passing the ftrace_make_call check.
2655 if (ftrace_start_up && cnt) {
2656 int failed = __ftrace_replace_code(p, 1);
2658 ftrace_bug(failed, p);
2663 stop = ftrace_now(raw_smp_processor_id());
2664 ftrace_update_time = stop - start;
2665 ftrace_update_tot_cnt += update_cnt;
2670 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2675 if (WARN_ON(!count))
2678 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2681 * We want to fill as much as possible. No more than a page
2684 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2688 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2691 /* if we can't allocate this size, try something smaller */
2698 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2707 static struct ftrace_page *
2708 ftrace_allocate_pages(unsigned long num_to_init)
2710 struct ftrace_page *start_pg;
2711 struct ftrace_page *pg;
2718 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2723 * Try to allocate as much as possible in one continues
2724 * location that fills in all of the space. We want to
2725 * waste as little space as possible.
2728 cnt = ftrace_allocate_records(pg, num_to_init);
2736 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2748 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2749 free_pages((unsigned long)pg->records, order);
2750 start_pg = pg->next;
2754 pr_info("ftrace: FAILED to allocate memory for functions\n");
2758 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2760 struct ftrace_iterator {
2763 struct ftrace_page *pg;
2764 struct dyn_ftrace *func;
2765 struct ftrace_func_probe *probe;
2766 struct trace_parser parser;
2767 struct ftrace_hash *hash;
2768 struct ftrace_ops *ops;
2775 t_hash_next(struct seq_file *m, loff_t *pos)
2777 struct ftrace_iterator *iter = m->private;
2778 struct hlist_node *hnd = NULL;
2779 struct hlist_head *hhd;
2785 hnd = &iter->probe->node;
2787 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2790 hhd = &ftrace_func_hash[iter->hidx];
2792 if (hlist_empty(hhd)) {
2808 if (WARN_ON_ONCE(!hnd))
2811 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2816 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2818 struct ftrace_iterator *iter = m->private;
2822 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2825 if (iter->func_pos > *pos)
2829 for (l = 0; l <= (*pos - iter->func_pos); ) {
2830 p = t_hash_next(m, &l);
2837 /* Only set this if we have an item */
2838 iter->flags |= FTRACE_ITER_HASH;
2844 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2846 struct ftrace_func_probe *rec;
2849 if (WARN_ON_ONCE(!rec))
2852 if (rec->ops->print)
2853 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2855 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2858 seq_printf(m, ":%p", rec->data);
2865 t_next(struct seq_file *m, void *v, loff_t *pos)
2867 struct ftrace_iterator *iter = m->private;
2868 struct ftrace_ops *ops = iter->ops;
2869 struct dyn_ftrace *rec = NULL;
2871 if (unlikely(ftrace_disabled))
2874 if (iter->flags & FTRACE_ITER_HASH)
2875 return t_hash_next(m, pos);
2878 iter->pos = iter->func_pos = *pos;
2880 if (iter->flags & FTRACE_ITER_PRINTALL)
2881 return t_hash_start(m, pos);
2884 if (iter->idx >= iter->pg->index) {
2885 if (iter->pg->next) {
2886 iter->pg = iter->pg->next;
2891 rec = &iter->pg->records[iter->idx++];
2892 if (((iter->flags & FTRACE_ITER_FILTER) &&
2893 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
2895 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2896 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
2898 ((iter->flags & FTRACE_ITER_ENABLED) &&
2899 !(rec->flags & FTRACE_FL_ENABLED))) {
2907 return t_hash_start(m, pos);
2914 static void reset_iter_read(struct ftrace_iterator *iter)
2918 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2921 static void *t_start(struct seq_file *m, loff_t *pos)
2923 struct ftrace_iterator *iter = m->private;
2924 struct ftrace_ops *ops = iter->ops;
2928 mutex_lock(&ftrace_lock);
2930 if (unlikely(ftrace_disabled))
2934 * If an lseek was done, then reset and start from beginning.
2936 if (*pos < iter->pos)
2937 reset_iter_read(iter);
2940 * For set_ftrace_filter reading, if we have the filter
2941 * off, we can short cut and just print out that all
2942 * functions are enabled.
2944 if ((iter->flags & FTRACE_ITER_FILTER &&
2945 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
2946 (iter->flags & FTRACE_ITER_NOTRACE &&
2947 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
2949 return t_hash_start(m, pos);
2950 iter->flags |= FTRACE_ITER_PRINTALL;
2951 /* reset in case of seek/pread */
2952 iter->flags &= ~FTRACE_ITER_HASH;
2956 if (iter->flags & FTRACE_ITER_HASH)
2957 return t_hash_start(m, pos);
2960 * Unfortunately, we need to restart at ftrace_pages_start
2961 * every time we let go of the ftrace_mutex. This is because
2962 * those pointers can change without the lock.
2964 iter->pg = ftrace_pages_start;
2966 for (l = 0; l <= *pos; ) {
2967 p = t_next(m, p, &l);
2973 return t_hash_start(m, pos);
2978 static void t_stop(struct seq_file *m, void *p)
2980 mutex_unlock(&ftrace_lock);
2984 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2989 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
2990 struct dyn_ftrace *rec)
2994 ptr = arch_ftrace_trampoline_func(ops, rec);
2996 seq_printf(m, " ->%pS", ptr);
2999 static int t_show(struct seq_file *m, void *v)
3001 struct ftrace_iterator *iter = m->private;
3002 struct dyn_ftrace *rec;
3004 if (iter->flags & FTRACE_ITER_HASH)
3005 return t_hash_show(m, iter);
3007 if (iter->flags & FTRACE_ITER_PRINTALL) {
3008 if (iter->flags & FTRACE_ITER_NOTRACE)
3009 seq_puts(m, "#### no functions disabled ####\n");
3011 seq_puts(m, "#### all functions enabled ####\n");
3020 seq_printf(m, "%ps", (void *)rec->ip);
3021 if (iter->flags & FTRACE_ITER_ENABLED) {
3022 struct ftrace_ops *ops = NULL;
3024 seq_printf(m, " (%ld)%s",
3025 ftrace_rec_count(rec),
3026 rec->flags & FTRACE_FL_REGS ? " R" : " ");
3027 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3028 ops = ftrace_find_tramp_ops_any(rec);
3030 seq_printf(m, "\ttramp: %pS",
3031 (void *)ops->trampoline);
3033 seq_puts(m, "\ttramp: ERROR!");
3036 add_trampoline_func(m, ops, rec);
3044 static const struct seq_operations show_ftrace_seq_ops = {
3052 ftrace_avail_open(struct inode *inode, struct file *file)
3054 struct ftrace_iterator *iter;
3056 if (unlikely(ftrace_disabled))
3059 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3061 iter->pg = ftrace_pages_start;
3062 iter->ops = &global_ops;
3065 return iter ? 0 : -ENOMEM;
3069 ftrace_enabled_open(struct inode *inode, struct file *file)
3071 struct ftrace_iterator *iter;
3073 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3075 iter->pg = ftrace_pages_start;
3076 iter->flags = FTRACE_ITER_ENABLED;
3077 iter->ops = &global_ops;
3080 return iter ? 0 : -ENOMEM;
3084 * ftrace_regex_open - initialize function tracer filter files
3085 * @ops: The ftrace_ops that hold the hash filters
3086 * @flag: The type of filter to process
3087 * @inode: The inode, usually passed in to your open routine
3088 * @file: The file, usually passed in to your open routine
3090 * ftrace_regex_open() initializes the filter files for the
3091 * @ops. Depending on @flag it may process the filter hash or
3092 * the notrace hash of @ops. With this called from the open
3093 * routine, you can use ftrace_filter_write() for the write
3094 * routine if @flag has FTRACE_ITER_FILTER set, or
3095 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3096 * tracing_lseek() should be used as the lseek routine, and
3097 * release must call ftrace_regex_release().
3100 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3101 struct inode *inode, struct file *file)
3103 struct ftrace_iterator *iter;
3104 struct ftrace_hash *hash;
3107 ftrace_ops_init(ops);
3109 if (unlikely(ftrace_disabled))
3112 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3116 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3124 mutex_lock(&ops->func_hash->regex_lock);
3126 if (flag & FTRACE_ITER_NOTRACE)
3127 hash = ops->func_hash->notrace_hash;
3129 hash = ops->func_hash->filter_hash;
3131 if (file->f_mode & FMODE_WRITE) {
3132 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3134 if (file->f_flags & O_TRUNC)
3135 iter->hash = alloc_ftrace_hash(size_bits);
3137 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3140 trace_parser_put(&iter->parser);
3147 if (file->f_mode & FMODE_READ) {
3148 iter->pg = ftrace_pages_start;
3150 ret = seq_open(file, &show_ftrace_seq_ops);
3152 struct seq_file *m = file->private_data;
3156 free_ftrace_hash(iter->hash);
3157 trace_parser_put(&iter->parser);
3161 file->private_data = iter;
3164 mutex_unlock(&ops->func_hash->regex_lock);
3170 ftrace_filter_open(struct inode *inode, struct file *file)
3172 struct ftrace_ops *ops = inode->i_private;
3174 return ftrace_regex_open(ops,
3175 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3180 ftrace_notrace_open(struct inode *inode, struct file *file)
3182 struct ftrace_ops *ops = inode->i_private;
3184 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3188 static int ftrace_match(char *str, char *regex, int len, int type)
3195 if (strcmp(str, regex) == 0)
3198 case MATCH_FRONT_ONLY:
3199 if (strncmp(str, regex, len) == 0)
3202 case MATCH_MIDDLE_ONLY:
3203 if (strstr(str, regex))
3206 case MATCH_END_ONLY:
3208 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
3217 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
3219 struct ftrace_func_entry *entry;
3222 entry = ftrace_lookup_ip(hash, rec->ip);
3224 /* Do nothing if it doesn't exist */
3228 free_hash_entry(hash, entry);
3230 /* Do nothing if it exists */
3234 ret = add_hash_entry(hash, rec->ip);
3240 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3241 char *regex, int len, int type)
3243 char str[KSYM_SYMBOL_LEN];
3246 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3249 /* module lookup requires matching the module */
3250 if (!modname || strcmp(modname, mod))
3253 /* blank search means to match all funcs in the mod */
3258 return ftrace_match(str, regex, len, type);
3262 match_records(struct ftrace_hash *hash, char *buff,
3263 int len, char *mod, int not)
3265 unsigned search_len = 0;
3266 struct ftrace_page *pg;
3267 struct dyn_ftrace *rec;
3268 int type = MATCH_FULL;
3269 char *search = buff;
3274 type = filter_parse_regex(buff, len, &search, ¬);
3275 search_len = strlen(search);
3278 mutex_lock(&ftrace_lock);
3280 if (unlikely(ftrace_disabled))
3283 do_for_each_ftrace_rec(pg, rec) {
3284 if (ftrace_match_record(rec, mod, search, search_len, type)) {
3285 ret = enter_record(hash, rec, not);
3292 } while_for_each_ftrace_rec();
3294 mutex_unlock(&ftrace_lock);
3300 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3302 return match_records(hash, buff, len, NULL, 0);
3306 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
3310 /* blank or '*' mean the same */
3311 if (strcmp(buff, "*") == 0)
3314 /* handle the case of 'dont filter this module' */
3315 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3320 return match_records(hash, buff, strlen(buff), mod, not);
3324 * We register the module command as a template to show others how
3325 * to register the a command as well.
3329 ftrace_mod_callback(struct ftrace_hash *hash,
3330 char *func, char *cmd, char *param, int enable)
3336 * cmd == 'mod' because we only registered this func
3337 * for the 'mod' ftrace_func_command.
3338 * But if you register one func with multiple commands,
3339 * you can tell which command was used by the cmd
3343 /* we must have a module name */
3347 mod = strsep(¶m, ":");
3351 ret = ftrace_match_module_records(hash, func, mod);
3360 static struct ftrace_func_command ftrace_mod_cmd = {
3362 .func = ftrace_mod_callback,
3365 static int __init ftrace_mod_cmd_init(void)
3367 return register_ftrace_command(&ftrace_mod_cmd);
3369 core_initcall(ftrace_mod_cmd_init);
3371 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3372 struct ftrace_ops *op, struct pt_regs *pt_regs)
3374 struct ftrace_func_probe *entry;
3375 struct hlist_head *hhd;
3378 key = hash_long(ip, FTRACE_HASH_BITS);
3380 hhd = &ftrace_func_hash[key];
3382 if (hlist_empty(hhd))
3386 * Disable preemption for these calls to prevent a RCU grace
3387 * period. This syncs the hash iteration and freeing of items
3388 * on the hash. rcu_read_lock is too dangerous here.
3390 preempt_disable_notrace();
3391 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3392 if (entry->ip == ip)
3393 entry->ops->func(ip, parent_ip, &entry->data);
3395 preempt_enable_notrace();
3398 static struct ftrace_ops trace_probe_ops __read_mostly =
3400 .func = function_trace_probe_call,
3401 .flags = FTRACE_OPS_FL_INITIALIZED,
3402 INIT_OPS_HASH(trace_probe_ops)
3405 static int ftrace_probe_registered;
3407 static void __enable_ftrace_function_probe(struct ftrace_hash *old_hash)
3412 if (ftrace_probe_registered) {
3413 /* still need to update the function call sites */
3415 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3420 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3421 struct hlist_head *hhd = &ftrace_func_hash[i];
3425 /* Nothing registered? */
3426 if (i == FTRACE_FUNC_HASHSIZE)
3429 ret = ftrace_startup(&trace_probe_ops, 0);
3431 ftrace_probe_registered = 1;
3434 static void __disable_ftrace_function_probe(void)
3438 if (!ftrace_probe_registered)
3441 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3442 struct hlist_head *hhd = &ftrace_func_hash[i];
3447 /* no more funcs left */
3448 ftrace_shutdown(&trace_probe_ops, 0);
3450 ftrace_probe_registered = 0;
3454 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3456 if (entry->ops->free)
3457 entry->ops->free(entry->ops, entry->ip, &entry->data);
3462 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3465 struct ftrace_func_probe *entry;
3466 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3467 struct ftrace_hash *old_hash = *orig_hash;
3468 struct ftrace_hash *hash;
3469 struct ftrace_page *pg;
3470 struct dyn_ftrace *rec;
3477 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3478 len = strlen(search);
3480 /* we do not support '!' for function probes */
3484 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3486 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3492 if (unlikely(ftrace_disabled)) {
3497 mutex_lock(&ftrace_lock);
3499 do_for_each_ftrace_rec(pg, rec) {
3501 if (!ftrace_match_record(rec, NULL, search, len, type))
3504 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3506 /* If we did not process any, then return error */
3517 * The caller might want to do something special
3518 * for each function we find. We call the callback
3519 * to give the caller an opportunity to do so.
3522 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3523 /* caller does not like this func */
3529 ret = enter_record(hash, rec, 0);
3537 entry->ip = rec->ip;
3539 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3540 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3542 } while_for_each_ftrace_rec();
3544 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3546 __enable_ftrace_function_probe(old_hash);
3549 free_ftrace_hash_rcu(old_hash);
3554 mutex_unlock(&ftrace_lock);
3556 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3557 free_ftrace_hash(hash);
3563 PROBE_TEST_FUNC = 1,
3568 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3569 void *data, int flags)
3571 struct ftrace_func_entry *rec_entry;
3572 struct ftrace_func_probe *entry;
3573 struct ftrace_func_probe *p;
3574 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3575 struct ftrace_hash *old_hash = *orig_hash;
3576 struct list_head free_list;
3577 struct ftrace_hash *hash;
3578 struct hlist_node *tmp;
3579 char str[KSYM_SYMBOL_LEN];
3580 int type = MATCH_FULL;
3585 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3590 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3591 len = strlen(search);
3593 /* we do not support '!' for function probes */
3598 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3600 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3602 /* Hmm, should report this somehow */
3605 INIT_LIST_HEAD(&free_list);
3607 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3608 struct hlist_head *hhd = &ftrace_func_hash[i];
3610 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3612 /* break up if statements for readability */
3613 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3616 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3619 /* do this last, since it is the most expensive */
3621 kallsyms_lookup(entry->ip, NULL, NULL,
3623 if (!ftrace_match(str, glob, len, type))
3627 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3628 /* It is possible more than one entry had this ip */
3630 free_hash_entry(hash, rec_entry);
3632 hlist_del_rcu(&entry->node);
3633 list_add(&entry->free_list, &free_list);
3636 mutex_lock(&ftrace_lock);
3637 __disable_ftrace_function_probe();
3639 * Remove after the disable is called. Otherwise, if the last
3640 * probe is removed, a null hash means *all enabled*.
3642 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3643 synchronize_sched();
3645 free_ftrace_hash_rcu(old_hash);
3647 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3648 list_del(&entry->free_list);
3649 ftrace_free_entry(entry);
3651 mutex_unlock(&ftrace_lock);
3654 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3655 free_ftrace_hash(hash);
3659 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3662 __unregister_ftrace_function_probe(glob, ops, data,
3663 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3667 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3669 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3672 void unregister_ftrace_function_probe_all(char *glob)
3674 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3677 static LIST_HEAD(ftrace_commands);
3678 static DEFINE_MUTEX(ftrace_cmd_mutex);
3681 * Currently we only register ftrace commands from __init, so mark this
3684 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3686 struct ftrace_func_command *p;
3689 mutex_lock(&ftrace_cmd_mutex);
3690 list_for_each_entry(p, &ftrace_commands, list) {
3691 if (strcmp(cmd->name, p->name) == 0) {
3696 list_add(&cmd->list, &ftrace_commands);
3698 mutex_unlock(&ftrace_cmd_mutex);
3704 * Currently we only unregister ftrace commands from __init, so mark
3707 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3709 struct ftrace_func_command *p, *n;
3712 mutex_lock(&ftrace_cmd_mutex);
3713 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3714 if (strcmp(cmd->name, p->name) == 0) {
3716 list_del_init(&p->list);
3721 mutex_unlock(&ftrace_cmd_mutex);
3726 static int ftrace_process_regex(struct ftrace_hash *hash,
3727 char *buff, int len, int enable)
3729 char *func, *command, *next = buff;
3730 struct ftrace_func_command *p;
3733 func = strsep(&next, ":");
3736 ret = ftrace_match_records(hash, func, len);
3746 command = strsep(&next, ":");
3748 mutex_lock(&ftrace_cmd_mutex);
3749 list_for_each_entry(p, &ftrace_commands, list) {
3750 if (strcmp(p->name, command) == 0) {
3751 ret = p->func(hash, func, command, next, enable);
3756 mutex_unlock(&ftrace_cmd_mutex);
3762 ftrace_regex_write(struct file *file, const char __user *ubuf,
3763 size_t cnt, loff_t *ppos, int enable)
3765 struct ftrace_iterator *iter;
3766 struct trace_parser *parser;
3772 if (file->f_mode & FMODE_READ) {
3773 struct seq_file *m = file->private_data;
3776 iter = file->private_data;
3778 if (unlikely(ftrace_disabled))
3781 /* iter->hash is a local copy, so we don't need regex_lock */
3783 parser = &iter->parser;
3784 read = trace_get_user(parser, ubuf, cnt, ppos);
3786 if (read >= 0 && trace_parser_loaded(parser) &&
3787 !trace_parser_cont(parser)) {
3788 ret = ftrace_process_regex(iter->hash, parser->buffer,
3789 parser->idx, enable);
3790 trace_parser_clear(parser);
3801 ftrace_filter_write(struct file *file, const char __user *ubuf,
3802 size_t cnt, loff_t *ppos)
3804 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3808 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3809 size_t cnt, loff_t *ppos)
3811 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3815 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3817 struct ftrace_func_entry *entry;
3819 if (!ftrace_location(ip))
3823 entry = ftrace_lookup_ip(hash, ip);
3826 free_hash_entry(hash, entry);
3830 return add_hash_entry(hash, ip);
3833 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3834 struct ftrace_hash *old_hash)
3836 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3837 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3841 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3842 unsigned long ip, int remove, int reset, int enable)
3844 struct ftrace_hash **orig_hash;
3845 struct ftrace_hash *old_hash;
3846 struct ftrace_hash *hash;
3849 if (unlikely(ftrace_disabled))
3852 mutex_lock(&ops->func_hash->regex_lock);
3855 orig_hash = &ops->func_hash->filter_hash;
3857 orig_hash = &ops->func_hash->notrace_hash;
3860 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3862 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3866 goto out_regex_unlock;
3869 if (buf && !ftrace_match_records(hash, buf, len)) {
3871 goto out_regex_unlock;
3874 ret = ftrace_match_addr(hash, ip, remove);
3876 goto out_regex_unlock;
3879 mutex_lock(&ftrace_lock);
3880 old_hash = *orig_hash;
3881 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3883 ftrace_ops_update_code(ops, old_hash);
3884 free_ftrace_hash_rcu(old_hash);
3886 mutex_unlock(&ftrace_lock);
3889 mutex_unlock(&ops->func_hash->regex_lock);
3891 free_ftrace_hash(hash);
3896 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3897 int reset, int enable)
3899 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3903 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3904 * @ops - the ops to set the filter with
3905 * @ip - the address to add to or remove from the filter.
3906 * @remove - non zero to remove the ip from the filter
3907 * @reset - non zero to reset all filters before applying this filter.
3909 * Filters denote which functions should be enabled when tracing is enabled
3910 * If @ip is NULL, it failes to update filter.
3912 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3913 int remove, int reset)
3915 ftrace_ops_init(ops);
3916 return ftrace_set_addr(ops, ip, remove, reset, 1);
3918 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3921 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3922 int reset, int enable)
3924 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3928 * ftrace_set_filter - set a function to filter on in ftrace
3929 * @ops - the ops to set the filter with
3930 * @buf - the string that holds the function filter text.
3931 * @len - the length of the string.
3932 * @reset - non zero to reset all filters before applying this filter.
3934 * Filters denote which functions should be enabled when tracing is enabled.
3935 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3937 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3940 ftrace_ops_init(ops);
3941 return ftrace_set_regex(ops, buf, len, reset, 1);
3943 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3946 * ftrace_set_notrace - set a function to not trace in ftrace
3947 * @ops - the ops to set the notrace filter with
3948 * @buf - the string that holds the function notrace text.
3949 * @len - the length of the string.
3950 * @reset - non zero to reset all filters before applying this filter.
3952 * Notrace Filters denote which functions should not be enabled when tracing
3953 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3956 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3959 ftrace_ops_init(ops);
3960 return ftrace_set_regex(ops, buf, len, reset, 0);
3962 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3964 * ftrace_set_global_filter - set a function to filter on with global tracers
3965 * @buf - the string that holds the function filter text.
3966 * @len - the length of the string.
3967 * @reset - non zero to reset all filters before applying this filter.
3969 * Filters denote which functions should be enabled when tracing is enabled.
3970 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3972 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3974 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3976 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3979 * ftrace_set_global_notrace - set a function to not trace with global tracers
3980 * @buf - the string that holds the function notrace text.
3981 * @len - the length of the string.
3982 * @reset - non zero to reset all filters before applying this filter.
3984 * Notrace Filters denote which functions should not be enabled when tracing
3985 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3988 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3990 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3992 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3995 * command line interface to allow users to set filters on boot up.
3997 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3998 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3999 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4001 /* Used by function selftest to not test if filter is set */
4002 bool ftrace_filter_param __initdata;
4004 static int __init set_ftrace_notrace(char *str)
4006 ftrace_filter_param = true;
4007 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4010 __setup("ftrace_notrace=", set_ftrace_notrace);
4012 static int __init set_ftrace_filter(char *str)
4014 ftrace_filter_param = true;
4015 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4018 __setup("ftrace_filter=", set_ftrace_filter);
4020 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4021 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4022 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4023 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
4025 static unsigned long save_global_trampoline;
4026 static unsigned long save_global_flags;
4028 static int __init set_graph_function(char *str)
4030 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4033 __setup("ftrace_graph_filter=", set_graph_function);
4035 static int __init set_graph_notrace_function(char *str)
4037 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4040 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4042 static void __init set_ftrace_early_graph(char *buf, int enable)
4046 unsigned long *table = ftrace_graph_funcs;
4047 int *count = &ftrace_graph_count;
4050 table = ftrace_graph_notrace_funcs;
4051 count = &ftrace_graph_notrace_count;
4055 func = strsep(&buf, ",");
4056 /* we allow only one expression at a time */
4057 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
4059 printk(KERN_DEBUG "ftrace: function %s not "
4060 "traceable\n", func);
4063 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4066 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4070 ftrace_ops_init(ops);
4073 func = strsep(&buf, ",");
4074 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4078 static void __init set_ftrace_early_filters(void)
4080 if (ftrace_filter_buf[0])
4081 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4082 if (ftrace_notrace_buf[0])
4083 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4084 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4085 if (ftrace_graph_buf[0])
4086 set_ftrace_early_graph(ftrace_graph_buf, 1);
4087 if (ftrace_graph_notrace_buf[0])
4088 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4089 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4092 int ftrace_regex_release(struct inode *inode, struct file *file)
4094 struct seq_file *m = (struct seq_file *)file->private_data;
4095 struct ftrace_iterator *iter;
4096 struct ftrace_hash **orig_hash;
4097 struct ftrace_hash *old_hash;
4098 struct trace_parser *parser;
4102 if (file->f_mode & FMODE_READ) {
4104 seq_release(inode, file);
4106 iter = file->private_data;
4108 parser = &iter->parser;
4109 if (trace_parser_loaded(parser)) {
4110 parser->buffer[parser->idx] = 0;
4111 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
4114 trace_parser_put(parser);
4116 mutex_lock(&iter->ops->func_hash->regex_lock);
4118 if (file->f_mode & FMODE_WRITE) {
4119 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4122 orig_hash = &iter->ops->func_hash->filter_hash;
4124 orig_hash = &iter->ops->func_hash->notrace_hash;
4126 mutex_lock(&ftrace_lock);
4127 old_hash = *orig_hash;
4128 ret = ftrace_hash_move(iter->ops, filter_hash,
4129 orig_hash, iter->hash);
4131 ftrace_ops_update_code(iter->ops, old_hash);
4132 free_ftrace_hash_rcu(old_hash);
4134 mutex_unlock(&ftrace_lock);
4137 mutex_unlock(&iter->ops->func_hash->regex_lock);
4138 free_ftrace_hash(iter->hash);
4144 static const struct file_operations ftrace_avail_fops = {
4145 .open = ftrace_avail_open,
4147 .llseek = seq_lseek,
4148 .release = seq_release_private,
4151 static const struct file_operations ftrace_enabled_fops = {
4152 .open = ftrace_enabled_open,
4154 .llseek = seq_lseek,
4155 .release = seq_release_private,
4158 static const struct file_operations ftrace_filter_fops = {
4159 .open = ftrace_filter_open,
4161 .write = ftrace_filter_write,
4162 .llseek = tracing_lseek,
4163 .release = ftrace_regex_release,
4166 static const struct file_operations ftrace_notrace_fops = {
4167 .open = ftrace_notrace_open,
4169 .write = ftrace_notrace_write,
4170 .llseek = tracing_lseek,
4171 .release = ftrace_regex_release,
4174 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4176 static DEFINE_MUTEX(graph_lock);
4178 int ftrace_graph_count;
4179 int ftrace_graph_notrace_count;
4180 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4181 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4183 struct ftrace_graph_data {
4184 unsigned long *table;
4187 const struct seq_operations *seq_ops;
4191 __g_next(struct seq_file *m, loff_t *pos)
4193 struct ftrace_graph_data *fgd = m->private;
4195 if (*pos >= *fgd->count)
4197 return &fgd->table[*pos];
4201 g_next(struct seq_file *m, void *v, loff_t *pos)
4204 return __g_next(m, pos);
4207 static void *g_start(struct seq_file *m, loff_t *pos)
4209 struct ftrace_graph_data *fgd = m->private;
4211 mutex_lock(&graph_lock);
4213 /* Nothing, tell g_show to print all functions are enabled */
4214 if (!*fgd->count && !*pos)
4217 return __g_next(m, pos);
4220 static void g_stop(struct seq_file *m, void *p)
4222 mutex_unlock(&graph_lock);
4225 static int g_show(struct seq_file *m, void *v)
4227 unsigned long *ptr = v;
4232 if (ptr == (unsigned long *)1) {
4233 struct ftrace_graph_data *fgd = m->private;
4235 if (fgd->table == ftrace_graph_funcs)
4236 seq_puts(m, "#### all functions enabled ####\n");
4238 seq_puts(m, "#### no functions disabled ####\n");
4242 seq_printf(m, "%ps\n", (void *)*ptr);
4247 static const struct seq_operations ftrace_graph_seq_ops = {
4255 __ftrace_graph_open(struct inode *inode, struct file *file,
4256 struct ftrace_graph_data *fgd)
4260 mutex_lock(&graph_lock);
4261 if ((file->f_mode & FMODE_WRITE) &&
4262 (file->f_flags & O_TRUNC)) {
4264 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4266 mutex_unlock(&graph_lock);
4268 if (file->f_mode & FMODE_READ) {
4269 ret = seq_open(file, fgd->seq_ops);
4271 struct seq_file *m = file->private_data;
4275 file->private_data = fgd;
4281 ftrace_graph_open(struct inode *inode, struct file *file)
4283 struct ftrace_graph_data *fgd;
4285 if (unlikely(ftrace_disabled))
4288 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4292 fgd->table = ftrace_graph_funcs;
4293 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4294 fgd->count = &ftrace_graph_count;
4295 fgd->seq_ops = &ftrace_graph_seq_ops;
4297 return __ftrace_graph_open(inode, file, fgd);
4301 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4303 struct ftrace_graph_data *fgd;
4305 if (unlikely(ftrace_disabled))
4308 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4312 fgd->table = ftrace_graph_notrace_funcs;
4313 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4314 fgd->count = &ftrace_graph_notrace_count;
4315 fgd->seq_ops = &ftrace_graph_seq_ops;
4317 return __ftrace_graph_open(inode, file, fgd);
4321 ftrace_graph_release(struct inode *inode, struct file *file)
4323 if (file->f_mode & FMODE_READ) {
4324 struct seq_file *m = file->private_data;
4327 seq_release(inode, file);
4329 kfree(file->private_data);
4336 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4338 struct dyn_ftrace *rec;
4339 struct ftrace_page *pg;
4348 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
4349 if (!not && *idx >= size)
4352 search_len = strlen(search);
4354 mutex_lock(&ftrace_lock);
4356 if (unlikely(ftrace_disabled)) {
4357 mutex_unlock(&ftrace_lock);
4361 do_for_each_ftrace_rec(pg, rec) {
4363 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4364 /* if it is in the array */
4366 for (i = 0; i < *idx; i++) {
4367 if (array[i] == rec->ip) {
4376 array[(*idx)++] = rec->ip;
4382 array[i] = array[--(*idx)];
4388 } while_for_each_ftrace_rec();
4390 mutex_unlock(&ftrace_lock);
4399 ftrace_graph_write(struct file *file, const char __user *ubuf,
4400 size_t cnt, loff_t *ppos)
4402 struct trace_parser parser;
4403 ssize_t read, ret = 0;
4404 struct ftrace_graph_data *fgd = file->private_data;
4409 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4412 read = trace_get_user(&parser, ubuf, cnt, ppos);
4414 if (read >= 0 && trace_parser_loaded((&parser))) {
4415 parser.buffer[parser.idx] = 0;
4417 mutex_lock(&graph_lock);
4419 /* we allow only one expression at a time */
4420 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4423 mutex_unlock(&graph_lock);
4429 trace_parser_put(&parser);
4434 static const struct file_operations ftrace_graph_fops = {
4435 .open = ftrace_graph_open,
4437 .write = ftrace_graph_write,
4438 .llseek = tracing_lseek,
4439 .release = ftrace_graph_release,
4442 static const struct file_operations ftrace_graph_notrace_fops = {
4443 .open = ftrace_graph_notrace_open,
4445 .write = ftrace_graph_write,
4446 .llseek = tracing_lseek,
4447 .release = ftrace_graph_release,
4449 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4451 void ftrace_create_filter_files(struct ftrace_ops *ops,
4452 struct dentry *parent)
4455 trace_create_file("set_ftrace_filter", 0644, parent,
4456 ops, &ftrace_filter_fops);
4458 trace_create_file("set_ftrace_notrace", 0644, parent,
4459 ops, &ftrace_notrace_fops);
4463 * The name "destroy_filter_files" is really a misnomer. Although
4464 * in the future, it may actualy delete the files, but this is
4465 * really intended to make sure the ops passed in are disabled
4466 * and that when this function returns, the caller is free to
4469 * The "destroy" name is only to match the "create" name that this
4470 * should be paired with.
4472 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4474 mutex_lock(&ftrace_lock);
4475 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4476 ftrace_shutdown(ops, 0);
4477 ops->flags |= FTRACE_OPS_FL_DELETED;
4478 mutex_unlock(&ftrace_lock);
4481 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4484 trace_create_file("available_filter_functions", 0444,
4485 d_tracer, NULL, &ftrace_avail_fops);
4487 trace_create_file("enabled_functions", 0444,
4488 d_tracer, NULL, &ftrace_enabled_fops);
4490 ftrace_create_filter_files(&global_ops, d_tracer);
4492 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4493 trace_create_file("set_graph_function", 0444, d_tracer,
4495 &ftrace_graph_fops);
4496 trace_create_file("set_graph_notrace", 0444, d_tracer,
4498 &ftrace_graph_notrace_fops);
4499 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4504 static int ftrace_cmp_ips(const void *a, const void *b)
4506 const unsigned long *ipa = a;
4507 const unsigned long *ipb = b;
4516 static void ftrace_swap_ips(void *a, void *b, int size)
4518 unsigned long *ipa = a;
4519 unsigned long *ipb = b;
4527 static int ftrace_process_locs(struct module *mod,
4528 unsigned long *start,
4531 struct ftrace_page *start_pg;
4532 struct ftrace_page *pg;
4533 struct dyn_ftrace *rec;
4534 unsigned long count;
4537 unsigned long flags = 0; /* Shut up gcc */
4540 count = end - start;
4545 sort(start, count, sizeof(*start),
4546 ftrace_cmp_ips, ftrace_swap_ips);
4548 start_pg = ftrace_allocate_pages(count);
4552 mutex_lock(&ftrace_lock);
4555 * Core and each module needs their own pages, as
4556 * modules will free them when they are removed.
4557 * Force a new page to be allocated for modules.
4560 WARN_ON(ftrace_pages || ftrace_pages_start);
4561 /* First initialization */
4562 ftrace_pages = ftrace_pages_start = start_pg;
4567 if (WARN_ON(ftrace_pages->next)) {
4568 /* Hmm, we have free pages? */
4569 while (ftrace_pages->next)
4570 ftrace_pages = ftrace_pages->next;
4573 ftrace_pages->next = start_pg;
4579 addr = ftrace_call_adjust(*p++);
4581 * Some architecture linkers will pad between
4582 * the different mcount_loc sections of different
4583 * object files to satisfy alignments.
4584 * Skip any NULL pointers.
4589 if (pg->index == pg->size) {
4590 /* We should have allocated enough */
4591 if (WARN_ON(!pg->next))
4596 rec = &pg->records[pg->index++];
4600 /* We should have used all pages */
4603 /* Assign the last page to ftrace_pages */
4607 * We only need to disable interrupts on start up
4608 * because we are modifying code that an interrupt
4609 * may execute, and the modification is not atomic.
4610 * But for modules, nothing runs the code we modify
4611 * until we are finished with it, and there's no
4612 * reason to cause large interrupt latencies while we do it.
4615 local_irq_save(flags);
4616 ftrace_update_code(mod, start_pg);
4618 local_irq_restore(flags);
4621 mutex_unlock(&ftrace_lock);
4626 #ifdef CONFIG_MODULES
4628 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4630 void ftrace_release_mod(struct module *mod)
4632 struct dyn_ftrace *rec;
4633 struct ftrace_page **last_pg;
4634 struct ftrace_page *pg;
4637 mutex_lock(&ftrace_lock);
4639 if (ftrace_disabled)
4643 * Each module has its own ftrace_pages, remove
4644 * them from the list.
4646 last_pg = &ftrace_pages_start;
4647 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4648 rec = &pg->records[0];
4649 if (within_module_core(rec->ip, mod)) {
4651 * As core pages are first, the first
4652 * page should never be a module page.
4654 if (WARN_ON(pg == ftrace_pages_start))
4657 /* Check if we are deleting the last page */
4658 if (pg == ftrace_pages)
4659 ftrace_pages = next_to_ftrace_page(last_pg);
4661 *last_pg = pg->next;
4662 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4663 free_pages((unsigned long)pg->records, order);
4666 last_pg = &pg->next;
4669 mutex_unlock(&ftrace_lock);
4672 static void ftrace_init_module(struct module *mod,
4673 unsigned long *start, unsigned long *end)
4675 if (ftrace_disabled || start == end)
4677 ftrace_process_locs(mod, start, end);
4680 void ftrace_module_init(struct module *mod)
4682 ftrace_init_module(mod, mod->ftrace_callsites,
4683 mod->ftrace_callsites +
4684 mod->num_ftrace_callsites);
4687 static int ftrace_module_notify_exit(struct notifier_block *self,
4688 unsigned long val, void *data)
4690 struct module *mod = data;
4692 if (val == MODULE_STATE_GOING)
4693 ftrace_release_mod(mod);
4698 static int ftrace_module_notify_exit(struct notifier_block *self,
4699 unsigned long val, void *data)
4703 #endif /* CONFIG_MODULES */
4705 struct notifier_block ftrace_module_exit_nb = {
4706 .notifier_call = ftrace_module_notify_exit,
4707 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4710 void __init ftrace_init(void)
4712 extern unsigned long __start_mcount_loc[];
4713 extern unsigned long __stop_mcount_loc[];
4714 unsigned long count, flags;
4717 local_irq_save(flags);
4718 ret = ftrace_dyn_arch_init();
4719 local_irq_restore(flags);
4723 count = __stop_mcount_loc - __start_mcount_loc;
4725 pr_info("ftrace: No functions to be traced?\n");
4729 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4730 count, count / ENTRIES_PER_PAGE + 1);
4732 last_ftrace_enabled = ftrace_enabled = 1;
4734 ret = ftrace_process_locs(NULL,
4738 ret = register_module_notifier(&ftrace_module_exit_nb);
4740 pr_warning("Failed to register trace ftrace module exit notifier\n");
4742 set_ftrace_early_filters();
4746 ftrace_disabled = 1;
4749 /* Do nothing if arch does not support this */
4750 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
4754 static void ftrace_update_trampoline(struct ftrace_ops *ops)
4758 * Currently there's no safe way to free a trampoline when the kernel
4759 * is configured with PREEMPT. That is because a task could be preempted
4760 * when it jumped to the trampoline, it may be preempted for a long time
4761 * depending on the system load, and currently there's no way to know
4762 * when it will be off the trampoline. If the trampoline is freed
4763 * too early, when the task runs again, it will be executing on freed
4766 #ifdef CONFIG_PREEMPT
4767 /* Currently, only non dynamic ops can have a trampoline */
4768 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
4772 arch_ftrace_update_trampoline(ops);
4777 static struct ftrace_ops global_ops = {
4778 .func = ftrace_stub,
4779 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4782 static int __init ftrace_nodyn_init(void)
4787 core_initcall(ftrace_nodyn_init);
4789 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4790 static inline void ftrace_startup_enable(int command) { }
4791 static inline void ftrace_startup_all(int command) { }
4792 /* Keep as macros so we do not need to define the commands */
4793 # define ftrace_startup(ops, command) \
4795 int ___ret = __register_ftrace_function(ops); \
4797 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4800 # define ftrace_shutdown(ops, command) \
4802 int ___ret = __unregister_ftrace_function(ops); \
4804 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4808 # define ftrace_startup_sysctl() do { } while (0)
4809 # define ftrace_shutdown_sysctl() do { } while (0)
4812 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4817 static void ftrace_update_trampoline(struct ftrace_ops *ops)
4821 #endif /* CONFIG_DYNAMIC_FTRACE */
4823 __init void ftrace_init_global_array_ops(struct trace_array *tr)
4825 tr->ops = &global_ops;
4826 tr->ops->private = tr;
4829 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4831 /* If we filter on pids, update to use the pid function */
4832 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4833 if (WARN_ON(tr->ops->func != ftrace_stub))
4834 printk("ftrace ops had %pS for function\n",
4836 /* Only the top level instance does pid tracing */
4837 if (!list_empty(&ftrace_pids)) {
4838 set_ftrace_pid_function(func);
4839 func = ftrace_pid_func;
4842 tr->ops->func = func;
4843 tr->ops->private = tr;
4846 void ftrace_reset_array_ops(struct trace_array *tr)
4848 tr->ops->func = ftrace_stub;
4852 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4853 struct ftrace_ops *op, struct pt_regs *regs)
4855 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4859 * Some of the ops may be dynamically allocated,
4860 * they must be freed after a synchronize_sched().
4862 preempt_disable_notrace();
4863 trace_recursion_set(TRACE_CONTROL_BIT);
4866 * Control funcs (perf) uses RCU. Only trace if
4867 * RCU is currently active.
4869 if (!rcu_is_watching())
4872 do_for_each_ftrace_op(op, ftrace_control_list) {
4873 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4874 !ftrace_function_local_disabled(op) &&
4875 ftrace_ops_test(op, ip, regs))
4876 op->func(ip, parent_ip, op, regs);
4877 } while_for_each_ftrace_op(op);
4879 trace_recursion_clear(TRACE_CONTROL_BIT);
4880 preempt_enable_notrace();
4883 static struct ftrace_ops control_ops = {
4884 .func = ftrace_ops_control_func,
4885 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4886 INIT_OPS_HASH(control_ops)
4890 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4891 struct ftrace_ops *ignored, struct pt_regs *regs)
4893 struct ftrace_ops *op;
4896 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4901 * Some of the ops may be dynamically allocated,
4902 * they must be freed after a synchronize_sched().
4904 preempt_disable_notrace();
4905 do_for_each_ftrace_op(op, ftrace_ops_list) {
4906 if (ftrace_ops_test(op, ip, regs)) {
4907 if (FTRACE_WARN_ON(!op->func)) {
4908 pr_warn("op=%p %pS\n", op, op);
4911 op->func(ip, parent_ip, op, regs);
4913 } while_for_each_ftrace_op(op);
4915 preempt_enable_notrace();
4916 trace_clear_recursion(bit);
4920 * Some archs only support passing ip and parent_ip. Even though
4921 * the list function ignores the op parameter, we do not want any
4922 * C side effects, where a function is called without the caller
4923 * sending a third parameter.
4924 * Archs are to support both the regs and ftrace_ops at the same time.
4925 * If they support ftrace_ops, it is assumed they support regs.
4926 * If call backs want to use regs, they must either check for regs
4927 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4928 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4929 * An architecture can pass partial regs with ftrace_ops and still
4930 * set the ARCH_SUPPORT_FTARCE_OPS.
4932 #if ARCH_SUPPORTS_FTRACE_OPS
4933 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4934 struct ftrace_ops *op, struct pt_regs *regs)
4936 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4939 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4941 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4946 * If there's only one function registered but it does not support
4947 * recursion, this function will be called by the mcount trampoline.
4948 * This function will handle recursion protection.
4950 static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
4951 struct ftrace_ops *op, struct pt_regs *regs)
4955 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4959 op->func(ip, parent_ip, op, regs);
4961 trace_clear_recursion(bit);
4965 * ftrace_ops_get_func - get the function a trampoline should call
4966 * @ops: the ops to get the function for
4968 * Normally the mcount trampoline will call the ops->func, but there
4969 * are times that it should not. For example, if the ops does not
4970 * have its own recursion protection, then it should call the
4971 * ftrace_ops_recurs_func() instead.
4973 * Returns the function that the trampoline should call for @ops.
4975 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
4978 * If this is a dynamic ops or we force list func,
4979 * then it needs to call the list anyway.
4981 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
4982 return ftrace_ops_list_func;
4985 * If the func handles its own recursion, call it directly.
4986 * Otherwise call the recursion protected function that
4987 * will call the ftrace ops function.
4989 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
4990 return ftrace_ops_recurs_func;
4995 static void clear_ftrace_swapper(void)
4997 struct task_struct *p;
5001 for_each_online_cpu(cpu) {
5003 clear_tsk_trace_trace(p);
5008 static void set_ftrace_swapper(void)
5010 struct task_struct *p;
5014 for_each_online_cpu(cpu) {
5016 set_tsk_trace_trace(p);
5021 static void clear_ftrace_pid(struct pid *pid)
5023 struct task_struct *p;
5026 do_each_pid_task(pid, PIDTYPE_PID, p) {
5027 clear_tsk_trace_trace(p);
5028 } while_each_pid_task(pid, PIDTYPE_PID, p);
5034 static void set_ftrace_pid(struct pid *pid)
5036 struct task_struct *p;
5039 do_each_pid_task(pid, PIDTYPE_PID, p) {
5040 set_tsk_trace_trace(p);
5041 } while_each_pid_task(pid, PIDTYPE_PID, p);
5045 static void clear_ftrace_pid_task(struct pid *pid)
5047 if (pid == ftrace_swapper_pid)
5048 clear_ftrace_swapper();
5050 clear_ftrace_pid(pid);
5053 static void set_ftrace_pid_task(struct pid *pid)
5055 if (pid == ftrace_swapper_pid)
5056 set_ftrace_swapper();
5058 set_ftrace_pid(pid);
5061 static int ftrace_pid_add(int p)
5064 struct ftrace_pid *fpid;
5067 mutex_lock(&ftrace_lock);
5070 pid = ftrace_swapper_pid;
5072 pid = find_get_pid(p);
5079 list_for_each_entry(fpid, &ftrace_pids, list)
5080 if (fpid->pid == pid)
5085 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5089 list_add(&fpid->list, &ftrace_pids);
5092 set_ftrace_pid_task(pid);
5094 ftrace_update_pid_func();
5096 ftrace_startup_all(0);
5098 mutex_unlock(&ftrace_lock);
5102 if (pid != ftrace_swapper_pid)
5106 mutex_unlock(&ftrace_lock);
5110 static void ftrace_pid_reset(void)
5112 struct ftrace_pid *fpid, *safe;
5114 mutex_lock(&ftrace_lock);
5115 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5116 struct pid *pid = fpid->pid;
5118 clear_ftrace_pid_task(pid);
5120 list_del(&fpid->list);
5124 ftrace_update_pid_func();
5125 ftrace_startup_all(0);
5127 mutex_unlock(&ftrace_lock);
5130 static void *fpid_start(struct seq_file *m, loff_t *pos)
5132 mutex_lock(&ftrace_lock);
5134 if (list_empty(&ftrace_pids) && (!*pos))
5137 return seq_list_start(&ftrace_pids, *pos);
5140 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5145 return seq_list_next(v, &ftrace_pids, pos);
5148 static void fpid_stop(struct seq_file *m, void *p)
5150 mutex_unlock(&ftrace_lock);
5153 static int fpid_show(struct seq_file *m, void *v)
5155 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5157 if (v == (void *)1) {
5158 seq_puts(m, "no pid\n");
5162 if (fpid->pid == ftrace_swapper_pid)
5163 seq_puts(m, "swapper tasks\n");
5165 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5170 static const struct seq_operations ftrace_pid_sops = {
5171 .start = fpid_start,
5178 ftrace_pid_open(struct inode *inode, struct file *file)
5182 if ((file->f_mode & FMODE_WRITE) &&
5183 (file->f_flags & O_TRUNC))
5186 if (file->f_mode & FMODE_READ)
5187 ret = seq_open(file, &ftrace_pid_sops);
5193 ftrace_pid_write(struct file *filp, const char __user *ubuf,
5194 size_t cnt, loff_t *ppos)
5200 if (cnt >= sizeof(buf))
5203 if (copy_from_user(&buf, ubuf, cnt))
5209 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5210 * to clean the filter quietly.
5212 tmp = strstrip(buf);
5213 if (strlen(tmp) == 0)
5216 ret = kstrtol(tmp, 10, &val);
5220 ret = ftrace_pid_add(val);
5222 return ret ? ret : cnt;
5226 ftrace_pid_release(struct inode *inode, struct file *file)
5228 if (file->f_mode & FMODE_READ)
5229 seq_release(inode, file);
5234 static const struct file_operations ftrace_pid_fops = {
5235 .open = ftrace_pid_open,
5236 .write = ftrace_pid_write,
5238 .llseek = tracing_lseek,
5239 .release = ftrace_pid_release,
5242 static __init int ftrace_init_debugfs(void)
5244 struct dentry *d_tracer;
5246 d_tracer = tracing_init_dentry();
5250 ftrace_init_dyn_debugfs(d_tracer);
5252 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5253 NULL, &ftrace_pid_fops);
5255 ftrace_profile_debugfs(d_tracer);
5259 fs_initcall(ftrace_init_debugfs);
5262 * ftrace_kill - kill ftrace
5264 * This function should be used by panic code. It stops ftrace
5265 * but in a not so nice way. If you need to simply kill ftrace
5266 * from a non-atomic section, use ftrace_kill.
5268 void ftrace_kill(void)
5270 ftrace_disabled = 1;
5272 clear_ftrace_function();
5276 * Test if ftrace is dead or not.
5278 int ftrace_is_dead(void)
5280 return ftrace_disabled;
5284 * register_ftrace_function - register a function for profiling
5285 * @ops - ops structure that holds the function for profiling.
5287 * Register a function to be called by all functions in the
5290 * Note: @ops->func and all the functions it calls must be labeled
5291 * with "notrace", otherwise it will go into a
5294 int register_ftrace_function(struct ftrace_ops *ops)
5298 ftrace_ops_init(ops);
5300 mutex_lock(&ftrace_lock);
5302 ret = ftrace_startup(ops, 0);
5304 mutex_unlock(&ftrace_lock);
5308 EXPORT_SYMBOL_GPL(register_ftrace_function);
5311 * unregister_ftrace_function - unregister a function for profiling.
5312 * @ops - ops structure that holds the function to unregister
5314 * Unregister a function that was added to be called by ftrace profiling.
5316 int unregister_ftrace_function(struct ftrace_ops *ops)
5320 mutex_lock(&ftrace_lock);
5321 ret = ftrace_shutdown(ops, 0);
5322 mutex_unlock(&ftrace_lock);
5326 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5329 ftrace_enable_sysctl(struct ctl_table *table, int write,
5330 void __user *buffer, size_t *lenp,
5335 mutex_lock(&ftrace_lock);
5337 if (unlikely(ftrace_disabled))
5340 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5342 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5345 last_ftrace_enabled = !!ftrace_enabled;
5347 if (ftrace_enabled) {
5349 ftrace_startup_sysctl();
5351 /* we are starting ftrace again */
5352 if (ftrace_ops_list != &ftrace_list_end)
5353 update_ftrace_function();
5356 /* stopping ftrace calls (just send to ftrace_stub) */
5357 ftrace_trace_function = ftrace_stub;
5359 ftrace_shutdown_sysctl();
5363 mutex_unlock(&ftrace_lock);
5367 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5369 static struct ftrace_ops graph_ops = {
5370 .func = ftrace_stub,
5371 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5372 FTRACE_OPS_FL_INITIALIZED |
5374 #ifdef FTRACE_GRAPH_TRAMP_ADDR
5375 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5377 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5380 static int ftrace_graph_active;
5382 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5387 /* The callbacks that hook a function */
5388 trace_func_graph_ret_t ftrace_graph_return =
5389 (trace_func_graph_ret_t)ftrace_stub;
5390 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5391 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5393 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5394 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5398 unsigned long flags;
5399 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5400 struct task_struct *g, *t;
5402 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5403 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5404 * sizeof(struct ftrace_ret_stack),
5406 if (!ret_stack_list[i]) {
5414 read_lock_irqsave(&tasklist_lock, flags);
5415 do_each_thread(g, t) {
5421 if (t->ret_stack == NULL) {
5422 atomic_set(&t->tracing_graph_pause, 0);
5423 atomic_set(&t->trace_overrun, 0);
5424 t->curr_ret_stack = -1;
5425 /* Make sure the tasks see the -1 first: */
5427 t->ret_stack = ret_stack_list[start++];
5429 } while_each_thread(g, t);
5432 read_unlock_irqrestore(&tasklist_lock, flags);
5434 for (i = start; i < end; i++)
5435 kfree(ret_stack_list[i]);
5440 ftrace_graph_probe_sched_switch(void *ignore,
5441 struct task_struct *prev, struct task_struct *next)
5443 unsigned long long timestamp;
5447 * Does the user want to count the time a function was asleep.
5448 * If so, do not update the time stamps.
5450 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5453 timestamp = trace_clock_local();
5455 prev->ftrace_timestamp = timestamp;
5457 /* only process tasks that we timestamped */
5458 if (!next->ftrace_timestamp)
5462 * Update all the counters in next to make up for the
5463 * time next was sleeping.
5465 timestamp -= next->ftrace_timestamp;
5467 for (index = next->curr_ret_stack; index >= 0; index--)
5468 next->ret_stack[index].calltime += timestamp;
5471 /* Allocate a return stack for each task */
5472 static int start_graph_tracing(void)
5474 struct ftrace_ret_stack **ret_stack_list;
5477 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5478 sizeof(struct ftrace_ret_stack *),
5481 if (!ret_stack_list)
5484 /* The cpu_boot init_task->ret_stack will never be freed */
5485 for_each_online_cpu(cpu) {
5486 if (!idle_task(cpu)->ret_stack)
5487 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5491 ret = alloc_retstack_tasklist(ret_stack_list);
5492 } while (ret == -EAGAIN);
5495 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5497 pr_info("ftrace_graph: Couldn't activate tracepoint"
5498 " probe to kernel_sched_switch\n");
5501 kfree(ret_stack_list);
5506 * Hibernation protection.
5507 * The state of the current task is too much unstable during
5508 * suspend/restore to disk. We want to protect against that.
5511 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5515 case PM_HIBERNATION_PREPARE:
5516 pause_graph_tracing();
5519 case PM_POST_HIBERNATION:
5520 unpause_graph_tracing();
5526 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5528 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5530 return __ftrace_graph_entry(trace);
5534 * The function graph tracer should only trace the functions defined
5535 * by set_ftrace_filter and set_ftrace_notrace. If another function
5536 * tracer ops is registered, the graph tracer requires testing the
5537 * function against the global ops, and not just trace any function
5538 * that any ftrace_ops registered.
5540 static void update_function_graph_func(void)
5542 struct ftrace_ops *op;
5543 bool do_test = false;
5546 * The graph and global ops share the same set of functions
5547 * to test. If any other ops is on the list, then
5548 * the graph tracing needs to test if its the function
5551 do_for_each_ftrace_op(op, ftrace_ops_list) {
5552 if (op != &global_ops && op != &graph_ops &&
5553 op != &ftrace_list_end) {
5555 /* in double loop, break out with goto */
5558 } while_for_each_ftrace_op(op);
5561 ftrace_graph_entry = ftrace_graph_entry_test;
5563 ftrace_graph_entry = __ftrace_graph_entry;
5566 static struct notifier_block ftrace_suspend_notifier = {
5567 .notifier_call = ftrace_suspend_notifier_call,
5570 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5571 trace_func_graph_ent_t entryfunc)
5575 mutex_lock(&ftrace_lock);
5577 /* we currently allow only one tracer registered at a time */
5578 if (ftrace_graph_active) {
5583 register_pm_notifier(&ftrace_suspend_notifier);
5585 ftrace_graph_active++;
5586 ret = start_graph_tracing();
5588 ftrace_graph_active--;
5592 ftrace_graph_return = retfunc;
5595 * Update the indirect function to the entryfunc, and the
5596 * function that gets called to the entry_test first. Then
5597 * call the update fgraph entry function to determine if
5598 * the entryfunc should be called directly or not.
5600 __ftrace_graph_entry = entryfunc;
5601 ftrace_graph_entry = ftrace_graph_entry_test;
5602 update_function_graph_func();
5604 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
5606 mutex_unlock(&ftrace_lock);
5610 void unregister_ftrace_graph(void)
5612 mutex_lock(&ftrace_lock);
5614 if (unlikely(!ftrace_graph_active))
5617 ftrace_graph_active--;
5618 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5619 ftrace_graph_entry = ftrace_graph_entry_stub;
5620 __ftrace_graph_entry = ftrace_graph_entry_stub;
5621 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
5622 unregister_pm_notifier(&ftrace_suspend_notifier);
5623 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5625 #ifdef CONFIG_DYNAMIC_FTRACE
5627 * Function graph does not allocate the trampoline, but
5628 * other global_ops do. We need to reset the ALLOC_TRAMP flag
5631 global_ops.trampoline = save_global_trampoline;
5632 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
5633 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
5637 mutex_unlock(&ftrace_lock);
5640 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5643 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5645 atomic_set(&t->tracing_graph_pause, 0);
5646 atomic_set(&t->trace_overrun, 0);
5647 t->ftrace_timestamp = 0;
5648 /* make curr_ret_stack visible before we add the ret_stack */
5650 t->ret_stack = ret_stack;
5654 * Allocate a return stack for the idle task. May be the first
5655 * time through, or it may be done by CPU hotplug online.
5657 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5659 t->curr_ret_stack = -1;
5661 * The idle task has no parent, it either has its own
5662 * stack or no stack at all.
5665 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5667 if (ftrace_graph_active) {
5668 struct ftrace_ret_stack *ret_stack;
5670 ret_stack = per_cpu(idle_ret_stack, cpu);
5672 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5673 * sizeof(struct ftrace_ret_stack),
5677 per_cpu(idle_ret_stack, cpu) = ret_stack;
5679 graph_init_task(t, ret_stack);
5683 /* Allocate a return stack for newly created task */
5684 void ftrace_graph_init_task(struct task_struct *t)
5686 /* Make sure we do not use the parent ret_stack */
5687 t->ret_stack = NULL;
5688 t->curr_ret_stack = -1;
5690 if (ftrace_graph_active) {
5691 struct ftrace_ret_stack *ret_stack;
5693 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5694 * sizeof(struct ftrace_ret_stack),
5698 graph_init_task(t, ret_stack);
5702 void ftrace_graph_exit_task(struct task_struct *t)
5704 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5706 t->ret_stack = NULL;
5707 /* NULL must become visible to IRQs before we free it: */