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/tracefs.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>
35 #include <linux/kprobes.h>
37 #include <trace/events/sched.h>
39 #include <asm/setup.h>
41 #include "trace_output.h"
42 #include "trace_stat.h"
44 #define FTRACE_WARN_ON(cond) \
52 #define FTRACE_WARN_ON_ONCE(cond) \
55 if (WARN_ON_ONCE(___r)) \
60 /* hash bits for specific function selection */
61 #define FTRACE_HASH_BITS 7
62 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
63 #define FTRACE_HASH_DEFAULT_BITS 10
64 #define FTRACE_HASH_MAX_BITS 12
66 #ifdef CONFIG_DYNAMIC_FTRACE
67 #define INIT_OPS_HASH(opsname) \
68 .func_hash = &opsname.local_hash, \
69 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
70 #define ASSIGN_OPS_HASH(opsname, val) \
72 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
74 #define INIT_OPS_HASH(opsname)
75 #define ASSIGN_OPS_HASH(opsname, val)
78 static struct ftrace_ops ftrace_list_end __read_mostly = {
80 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
81 INIT_OPS_HASH(ftrace_list_end)
84 /* ftrace_enabled is a method to turn ftrace on or off */
85 int ftrace_enabled __read_mostly;
86 static int last_ftrace_enabled;
88 /* Current function tracing op */
89 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
90 /* What to set function_trace_op to */
91 static struct ftrace_ops *set_function_trace_op;
93 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
95 struct trace_array *tr;
97 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
102 return tr->function_pids != NULL;
105 static void ftrace_update_trampoline(struct ftrace_ops *ops);
108 * ftrace_disabled is set when an anomaly is discovered.
109 * ftrace_disabled is much stronger than ftrace_enabled.
111 static int ftrace_disabled __read_mostly;
113 static DEFINE_MUTEX(ftrace_lock);
115 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
116 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
117 static struct ftrace_ops global_ops;
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 struct trace_array *tr = op->private;
185 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
188 op->saved_func(ip, parent_ip, op, regs);
192 * clear_ftrace_function - reset the ftrace function
194 * This NULLs the ftrace function and in essence stops
195 * tracing. There may be lag
197 void clear_ftrace_function(void)
199 ftrace_trace_function = ftrace_stub;
202 static void per_cpu_ops_disable_all(struct ftrace_ops *ops)
206 for_each_possible_cpu(cpu)
207 *per_cpu_ptr(ops->disabled, cpu) = 1;
210 static int per_cpu_ops_alloc(struct ftrace_ops *ops)
212 int __percpu *disabled;
214 if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU)))
217 disabled = alloc_percpu(int);
221 ops->disabled = disabled;
222 per_cpu_ops_disable_all(ops);
226 static void ftrace_sync(struct work_struct *work)
229 * This function is just a stub to implement a hard force
230 * of synchronize_sched(). This requires synchronizing
231 * tasks even in userspace and idle.
233 * Yes, function tracing is rude.
237 static void ftrace_sync_ipi(void *data)
239 /* Probably not needed, but do it anyway */
243 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
244 static void update_function_graph_func(void);
246 /* Both enabled by default (can be cleared by function_graph tracer flags */
247 static bool fgraph_sleep_time = true;
248 static bool fgraph_graph_time = true;
251 static inline void update_function_graph_func(void) { }
255 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
258 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
259 * then it needs to call the list anyway.
261 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU |
262 FTRACE_OPS_FL_RCU) || FTRACE_FORCE_LIST_FUNC)
263 return ftrace_ops_list_func;
265 return ftrace_ops_get_func(ops);
268 static void update_ftrace_function(void)
273 * Prepare the ftrace_ops that the arch callback will use.
274 * If there's only one ftrace_ops registered, the ftrace_ops_list
275 * will point to the ops we want.
277 set_function_trace_op = ftrace_ops_list;
279 /* If there's no ftrace_ops registered, just call the stub function */
280 if (ftrace_ops_list == &ftrace_list_end) {
284 * If we are at the end of the list and this ops is
285 * recursion safe and not dynamic and the arch supports passing ops,
286 * then have the mcount trampoline call the function directly.
288 } else if (ftrace_ops_list->next == &ftrace_list_end) {
289 func = ftrace_ops_get_list_func(ftrace_ops_list);
292 /* Just use the default ftrace_ops */
293 set_function_trace_op = &ftrace_list_end;
294 func = ftrace_ops_list_func;
297 update_function_graph_func();
299 /* If there's no change, then do nothing more here */
300 if (ftrace_trace_function == func)
304 * If we are using the list function, it doesn't care
305 * about the function_trace_ops.
307 if (func == ftrace_ops_list_func) {
308 ftrace_trace_function = func;
310 * Don't even bother setting function_trace_ops,
311 * it would be racy to do so anyway.
316 #ifndef CONFIG_DYNAMIC_FTRACE
318 * For static tracing, we need to be a bit more careful.
319 * The function change takes affect immediately. Thus,
320 * we need to coorditate the setting of the function_trace_ops
321 * with the setting of the ftrace_trace_function.
323 * Set the function to the list ops, which will call the
324 * function we want, albeit indirectly, but it handles the
325 * ftrace_ops and doesn't depend on function_trace_op.
327 ftrace_trace_function = ftrace_ops_list_func;
329 * Make sure all CPUs see this. Yes this is slow, but static
330 * tracing is slow and nasty to have enabled.
332 schedule_on_each_cpu(ftrace_sync);
333 /* Now all cpus are using the list ops. */
334 function_trace_op = set_function_trace_op;
335 /* Make sure the function_trace_op is visible on all CPUs */
337 /* Nasty way to force a rmb on all cpus */
338 smp_call_function(ftrace_sync_ipi, NULL, 1);
339 /* OK, we are all set to update the ftrace_trace_function now! */
340 #endif /* !CONFIG_DYNAMIC_FTRACE */
342 ftrace_trace_function = func;
345 int using_ftrace_ops_list_func(void)
347 return ftrace_trace_function == ftrace_ops_list_func;
350 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
354 * We are entering ops into the list but another
355 * CPU might be walking that list. We need to make sure
356 * the ops->next pointer is valid before another CPU sees
357 * the ops pointer included into the list.
359 rcu_assign_pointer(*list, ops);
362 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
364 struct ftrace_ops **p;
367 * If we are removing the last function, then simply point
368 * to the ftrace_stub.
370 if (*list == ops && ops->next == &ftrace_list_end) {
371 *list = &ftrace_list_end;
375 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
386 static void ftrace_update_trampoline(struct ftrace_ops *ops);
388 static int __register_ftrace_function(struct ftrace_ops *ops)
390 if (ops->flags & FTRACE_OPS_FL_DELETED)
393 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
396 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
398 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
399 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
400 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
402 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
403 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
406 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
407 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
410 if (!core_kernel_data((unsigned long)ops))
411 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
413 if (ops->flags & FTRACE_OPS_FL_PER_CPU) {
414 if (per_cpu_ops_alloc(ops))
418 add_ftrace_ops(&ftrace_ops_list, ops);
420 /* Always save the function, and reset at unregistering */
421 ops->saved_func = ops->func;
423 if (ftrace_pids_enabled(ops))
424 ops->func = ftrace_pid_func;
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 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
447 update_ftrace_function();
449 ops->func = ops->saved_func;
454 static void ftrace_update_pid_func(void)
456 struct ftrace_ops *op;
458 /* Only do something if we are tracing something */
459 if (ftrace_trace_function == ftrace_stub)
462 do_for_each_ftrace_op(op, ftrace_ops_list) {
463 if (op->flags & FTRACE_OPS_FL_PID) {
464 op->func = ftrace_pids_enabled(op) ?
465 ftrace_pid_func : op->saved_func;
466 ftrace_update_trampoline(op);
468 } while_for_each_ftrace_op(op);
470 update_ftrace_function();
473 #ifdef CONFIG_FUNCTION_PROFILER
474 struct ftrace_profile {
475 struct hlist_node node;
477 unsigned long counter;
478 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
479 unsigned long long time;
480 unsigned long long time_squared;
484 struct ftrace_profile_page {
485 struct ftrace_profile_page *next;
487 struct ftrace_profile records[];
490 struct ftrace_profile_stat {
492 struct hlist_head *hash;
493 struct ftrace_profile_page *pages;
494 struct ftrace_profile_page *start;
495 struct tracer_stat stat;
498 #define PROFILE_RECORDS_SIZE \
499 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
501 #define PROFILES_PER_PAGE \
502 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
504 static int ftrace_profile_enabled __read_mostly;
506 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
507 static DEFINE_MUTEX(ftrace_profile_lock);
509 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
511 #define FTRACE_PROFILE_HASH_BITS 10
512 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
515 function_stat_next(void *v, int idx)
517 struct ftrace_profile *rec = v;
518 struct ftrace_profile_page *pg;
520 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
526 if ((void *)rec >= (void *)&pg->records[pg->index]) {
530 rec = &pg->records[0];
538 static void *function_stat_start(struct tracer_stat *trace)
540 struct ftrace_profile_stat *stat =
541 container_of(trace, struct ftrace_profile_stat, stat);
543 if (!stat || !stat->start)
546 return function_stat_next(&stat->start->records[0], 0);
549 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
550 /* function graph compares on total time */
551 static int function_stat_cmp(void *p1, void *p2)
553 struct ftrace_profile *a = p1;
554 struct ftrace_profile *b = p2;
556 if (a->time < b->time)
558 if (a->time > b->time)
564 /* not function graph compares against hits */
565 static int function_stat_cmp(void *p1, void *p2)
567 struct ftrace_profile *a = p1;
568 struct ftrace_profile *b = p2;
570 if (a->counter < b->counter)
572 if (a->counter > b->counter)
579 static int function_stat_headers(struct seq_file *m)
581 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
582 seq_puts(m, " Function "
585 "--- ---- --- ---\n");
587 seq_puts(m, " Function Hit\n"
593 static int function_stat_show(struct seq_file *m, void *v)
595 struct ftrace_profile *rec = v;
596 char str[KSYM_SYMBOL_LEN];
598 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
599 static struct trace_seq s;
600 unsigned long long avg;
601 unsigned long long stddev;
603 mutex_lock(&ftrace_profile_lock);
605 /* we raced with function_profile_reset() */
606 if (unlikely(rec->counter == 0)) {
611 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
612 avg = div64_ul(rec->time, rec->counter);
613 if (tracing_thresh && (avg < tracing_thresh))
617 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
618 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
620 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
623 /* Sample standard deviation (s^2) */
624 if (rec->counter <= 1)
628 * Apply Welford's method:
629 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
631 stddev = rec->counter * rec->time_squared -
632 rec->time * rec->time;
635 * Divide only 1000 for ns^2 -> us^2 conversion.
636 * trace_print_graph_duration will divide 1000 again.
638 stddev = div64_ul(stddev,
639 rec->counter * (rec->counter - 1) * 1000);
643 trace_print_graph_duration(rec->time, &s);
644 trace_seq_puts(&s, " ");
645 trace_print_graph_duration(avg, &s);
646 trace_seq_puts(&s, " ");
647 trace_print_graph_duration(stddev, &s);
648 trace_print_seq(m, &s);
652 mutex_unlock(&ftrace_profile_lock);
657 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
659 struct ftrace_profile_page *pg;
661 pg = stat->pages = stat->start;
664 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
669 memset(stat->hash, 0,
670 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
673 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
675 struct ftrace_profile_page *pg;
680 /* If we already allocated, do nothing */
684 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
688 #ifdef CONFIG_DYNAMIC_FTRACE
689 functions = ftrace_update_tot_cnt;
692 * We do not know the number of functions that exist because
693 * dynamic tracing is what counts them. With past experience
694 * we have around 20K functions. That should be more than enough.
695 * It is highly unlikely we will execute every function in
701 pg = stat->start = stat->pages;
703 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
705 for (i = 1; i < pages; i++) {
706 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
717 unsigned long tmp = (unsigned long)pg;
729 static int ftrace_profile_init_cpu(int cpu)
731 struct ftrace_profile_stat *stat;
734 stat = &per_cpu(ftrace_profile_stats, cpu);
737 /* If the profile is already created, simply reset it */
738 ftrace_profile_reset(stat);
743 * We are profiling all functions, but usually only a few thousand
744 * functions are hit. We'll make a hash of 1024 items.
746 size = FTRACE_PROFILE_HASH_SIZE;
748 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
753 /* Preallocate the function profiling pages */
754 if (ftrace_profile_pages_init(stat) < 0) {
763 static int ftrace_profile_init(void)
768 for_each_possible_cpu(cpu) {
769 ret = ftrace_profile_init_cpu(cpu);
777 /* interrupts must be disabled */
778 static struct ftrace_profile *
779 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
781 struct ftrace_profile *rec;
782 struct hlist_head *hhd;
785 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
786 hhd = &stat->hash[key];
788 if (hlist_empty(hhd))
791 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
799 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
800 struct ftrace_profile *rec)
804 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
805 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
809 * The memory is already allocated, this simply finds a new record to use.
811 static struct ftrace_profile *
812 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
814 struct ftrace_profile *rec = NULL;
816 /* prevent recursion (from NMIs) */
817 if (atomic_inc_return(&stat->disabled) != 1)
821 * Try to find the function again since an NMI
822 * could have added it
824 rec = ftrace_find_profiled_func(stat, ip);
828 if (stat->pages->index == PROFILES_PER_PAGE) {
829 if (!stat->pages->next)
831 stat->pages = stat->pages->next;
834 rec = &stat->pages->records[stat->pages->index++];
836 ftrace_add_profile(stat, rec);
839 atomic_dec(&stat->disabled);
845 function_profile_call(unsigned long ip, unsigned long parent_ip,
846 struct ftrace_ops *ops, struct pt_regs *regs)
848 struct ftrace_profile_stat *stat;
849 struct ftrace_profile *rec;
852 if (!ftrace_profile_enabled)
855 local_irq_save(flags);
857 stat = this_cpu_ptr(&ftrace_profile_stats);
858 if (!stat->hash || !ftrace_profile_enabled)
861 rec = ftrace_find_profiled_func(stat, ip);
863 rec = ftrace_profile_alloc(stat, ip);
870 local_irq_restore(flags);
873 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
874 static int profile_graph_entry(struct ftrace_graph_ent *trace)
876 int index = trace->depth;
878 function_profile_call(trace->func, 0, NULL, NULL);
880 /* If function graph is shutting down, ret_stack can be NULL */
881 if (!current->ret_stack)
884 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
885 current->ret_stack[index].subtime = 0;
890 static void profile_graph_return(struct ftrace_graph_ret *trace)
892 struct ftrace_profile_stat *stat;
893 unsigned long long calltime;
894 struct ftrace_profile *rec;
897 local_irq_save(flags);
898 stat = this_cpu_ptr(&ftrace_profile_stats);
899 if (!stat->hash || !ftrace_profile_enabled)
902 /* If the calltime was zero'd ignore it */
903 if (!trace->calltime)
906 calltime = trace->rettime - trace->calltime;
908 if (!fgraph_graph_time) {
911 index = trace->depth;
913 /* Append this call time to the parent time to subtract */
915 current->ret_stack[index - 1].subtime += calltime;
917 if (current->ret_stack[index].subtime < calltime)
918 calltime -= current->ret_stack[index].subtime;
923 rec = ftrace_find_profiled_func(stat, trace->func);
925 rec->time += calltime;
926 rec->time_squared += calltime * calltime;
930 local_irq_restore(flags);
933 static int register_ftrace_profiler(void)
935 return register_ftrace_graph(&profile_graph_return,
936 &profile_graph_entry);
939 static void unregister_ftrace_profiler(void)
941 unregister_ftrace_graph();
944 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
945 .func = function_profile_call,
946 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
947 INIT_OPS_HASH(ftrace_profile_ops)
950 static int register_ftrace_profiler(void)
952 return register_ftrace_function(&ftrace_profile_ops);
955 static void unregister_ftrace_profiler(void)
957 unregister_ftrace_function(&ftrace_profile_ops);
959 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
962 ftrace_profile_write(struct file *filp, const char __user *ubuf,
963 size_t cnt, loff_t *ppos)
968 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
974 mutex_lock(&ftrace_profile_lock);
975 if (ftrace_profile_enabled ^ val) {
977 ret = ftrace_profile_init();
983 ret = register_ftrace_profiler();
988 ftrace_profile_enabled = 1;
990 ftrace_profile_enabled = 0;
992 * unregister_ftrace_profiler calls stop_machine
993 * so this acts like an synchronize_sched.
995 unregister_ftrace_profiler();
999 mutex_unlock(&ftrace_profile_lock);
1007 ftrace_profile_read(struct file *filp, char __user *ubuf,
1008 size_t cnt, loff_t *ppos)
1010 char buf[64]; /* big enough to hold a number */
1013 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1014 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1017 static const struct file_operations ftrace_profile_fops = {
1018 .open = tracing_open_generic,
1019 .read = ftrace_profile_read,
1020 .write = ftrace_profile_write,
1021 .llseek = default_llseek,
1024 /* used to initialize the real stat files */
1025 static struct tracer_stat function_stats __initdata = {
1026 .name = "functions",
1027 .stat_start = function_stat_start,
1028 .stat_next = function_stat_next,
1029 .stat_cmp = function_stat_cmp,
1030 .stat_headers = function_stat_headers,
1031 .stat_show = function_stat_show
1034 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1036 struct ftrace_profile_stat *stat;
1037 struct dentry *entry;
1042 for_each_possible_cpu(cpu) {
1043 stat = &per_cpu(ftrace_profile_stats, cpu);
1045 name = kasprintf(GFP_KERNEL, "function%d", cpu);
1048 * The files created are permanent, if something happens
1049 * we still do not free memory.
1052 "Could not allocate stat file for cpu %d\n",
1056 stat->stat = function_stats;
1057 stat->stat.name = name;
1058 ret = register_stat_tracer(&stat->stat);
1061 "Could not register function stat for cpu %d\n",
1068 entry = tracefs_create_file("function_profile_enabled", 0644,
1069 d_tracer, NULL, &ftrace_profile_fops);
1071 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1074 #else /* CONFIG_FUNCTION_PROFILER */
1075 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1078 #endif /* CONFIG_FUNCTION_PROFILER */
1080 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1082 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1083 static int ftrace_graph_active;
1085 # define ftrace_graph_active 0
1088 #ifdef CONFIG_DYNAMIC_FTRACE
1090 static struct ftrace_ops *removed_ops;
1093 * Set when doing a global update, like enabling all recs or disabling them.
1094 * It is not set when just updating a single ftrace_ops.
1096 static bool update_all_ops;
1098 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1099 # error Dynamic ftrace depends on MCOUNT_RECORD
1102 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1104 struct ftrace_func_probe {
1105 struct hlist_node node;
1106 struct ftrace_probe_ops *ops;
1107 unsigned long flags;
1110 struct list_head free_list;
1113 struct ftrace_func_entry {
1114 struct hlist_node hlist;
1118 struct ftrace_hash {
1119 unsigned long size_bits;
1120 struct hlist_head *buckets;
1121 unsigned long count;
1122 struct rcu_head rcu;
1126 * We make these constant because no one should touch them,
1127 * but they are used as the default "empty hash", to avoid allocating
1128 * it all the time. These are in a read only section such that if
1129 * anyone does try to modify it, it will cause an exception.
1131 static const struct hlist_head empty_buckets[1];
1132 static const struct ftrace_hash empty_hash = {
1133 .buckets = (struct hlist_head *)empty_buckets,
1135 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1137 static struct ftrace_ops global_ops = {
1138 .func = ftrace_stub,
1139 .local_hash.notrace_hash = EMPTY_HASH,
1140 .local_hash.filter_hash = EMPTY_HASH,
1141 INIT_OPS_HASH(global_ops)
1142 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1143 FTRACE_OPS_FL_INITIALIZED |
1148 * This is used by __kernel_text_address() to return true if the
1149 * address is on a dynamically allocated trampoline that would
1150 * not return true for either core_kernel_text() or
1151 * is_module_text_address().
1153 bool is_ftrace_trampoline(unsigned long addr)
1155 struct ftrace_ops *op;
1159 * Some of the ops may be dynamically allocated,
1160 * they are freed after a synchronize_sched().
1162 preempt_disable_notrace();
1164 do_for_each_ftrace_op(op, ftrace_ops_list) {
1166 * This is to check for dynamically allocated trampolines.
1167 * Trampolines that are in kernel text will have
1168 * core_kernel_text() return true.
1170 if (op->trampoline && op->trampoline_size)
1171 if (addr >= op->trampoline &&
1172 addr < op->trampoline + op->trampoline_size) {
1176 } while_for_each_ftrace_op(op);
1179 preempt_enable_notrace();
1184 struct ftrace_page {
1185 struct ftrace_page *next;
1186 struct dyn_ftrace *records;
1191 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1192 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1194 /* estimate from running different kernels */
1195 #define NR_TO_INIT 10000
1197 static struct ftrace_page *ftrace_pages_start;
1198 static struct ftrace_page *ftrace_pages;
1200 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1202 return !hash || !hash->count;
1205 static struct ftrace_func_entry *
1206 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1209 struct ftrace_func_entry *entry;
1210 struct hlist_head *hhd;
1212 if (ftrace_hash_empty(hash))
1215 if (hash->size_bits > 0)
1216 key = hash_long(ip, hash->size_bits);
1220 hhd = &hash->buckets[key];
1222 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1223 if (entry->ip == ip)
1229 static void __add_hash_entry(struct ftrace_hash *hash,
1230 struct ftrace_func_entry *entry)
1232 struct hlist_head *hhd;
1235 if (hash->size_bits)
1236 key = hash_long(entry->ip, hash->size_bits);
1240 hhd = &hash->buckets[key];
1241 hlist_add_head(&entry->hlist, hhd);
1245 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1247 struct ftrace_func_entry *entry;
1249 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1254 __add_hash_entry(hash, entry);
1260 free_hash_entry(struct ftrace_hash *hash,
1261 struct ftrace_func_entry *entry)
1263 hlist_del(&entry->hlist);
1269 remove_hash_entry(struct ftrace_hash *hash,
1270 struct ftrace_func_entry *entry)
1272 hlist_del(&entry->hlist);
1276 static void ftrace_hash_clear(struct ftrace_hash *hash)
1278 struct hlist_head *hhd;
1279 struct hlist_node *tn;
1280 struct ftrace_func_entry *entry;
1281 int size = 1 << hash->size_bits;
1287 for (i = 0; i < size; i++) {
1288 hhd = &hash->buckets[i];
1289 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1290 free_hash_entry(hash, entry);
1292 FTRACE_WARN_ON(hash->count);
1295 static void free_ftrace_hash(struct ftrace_hash *hash)
1297 if (!hash || hash == EMPTY_HASH)
1299 ftrace_hash_clear(hash);
1300 kfree(hash->buckets);
1304 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1306 struct ftrace_hash *hash;
1308 hash = container_of(rcu, struct ftrace_hash, rcu);
1309 free_ftrace_hash(hash);
1312 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1314 if (!hash || hash == EMPTY_HASH)
1316 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1319 void ftrace_free_filter(struct ftrace_ops *ops)
1321 ftrace_ops_init(ops);
1322 free_ftrace_hash(ops->func_hash->filter_hash);
1323 free_ftrace_hash(ops->func_hash->notrace_hash);
1326 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1328 struct ftrace_hash *hash;
1331 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1335 size = 1 << size_bits;
1336 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1338 if (!hash->buckets) {
1343 hash->size_bits = size_bits;
1348 static struct ftrace_hash *
1349 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1351 struct ftrace_func_entry *entry;
1352 struct ftrace_hash *new_hash;
1357 new_hash = alloc_ftrace_hash(size_bits);
1362 if (ftrace_hash_empty(hash))
1365 size = 1 << hash->size_bits;
1366 for (i = 0; i < size; i++) {
1367 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1368 ret = add_hash_entry(new_hash, entry->ip);
1374 FTRACE_WARN_ON(new_hash->count != hash->count);
1379 free_ftrace_hash(new_hash);
1384 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1386 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1388 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1389 struct ftrace_hash *new_hash);
1392 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1393 struct ftrace_hash **dst, struct ftrace_hash *src)
1395 struct ftrace_func_entry *entry;
1396 struct hlist_node *tn;
1397 struct hlist_head *hhd;
1398 struct ftrace_hash *new_hash;
1399 int size = src->count;
1404 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1405 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1409 * If the new source is empty, just free dst and assign it
1413 new_hash = EMPTY_HASH;
1418 * Make the hash size about 1/2 the # found
1420 for (size /= 2; size; size >>= 1)
1423 /* Don't allocate too much */
1424 if (bits > FTRACE_HASH_MAX_BITS)
1425 bits = FTRACE_HASH_MAX_BITS;
1427 new_hash = alloc_ftrace_hash(bits);
1431 size = 1 << src->size_bits;
1432 for (i = 0; i < size; i++) {
1433 hhd = &src->buckets[i];
1434 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1435 remove_hash_entry(src, entry);
1436 __add_hash_entry(new_hash, entry);
1441 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1443 /* IPMODIFY should be updated only when filter_hash updating */
1444 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1446 free_ftrace_hash(new_hash);
1452 * Remove the current set, update the hash and add
1455 ftrace_hash_rec_disable_modify(ops, enable);
1457 rcu_assign_pointer(*dst, new_hash);
1459 ftrace_hash_rec_enable_modify(ops, enable);
1464 static bool hash_contains_ip(unsigned long ip,
1465 struct ftrace_ops_hash *hash)
1468 * The function record is a match if it exists in the filter
1469 * hash and not in the notrace hash. Note, an emty hash is
1470 * considered a match for the filter hash, but an empty
1471 * notrace hash is considered not in the notrace hash.
1473 return (ftrace_hash_empty(hash->filter_hash) ||
1474 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1475 (ftrace_hash_empty(hash->notrace_hash) ||
1476 !ftrace_lookup_ip(hash->notrace_hash, ip));
1480 * Test the hashes for this ops to see if we want to call
1481 * the ops->func or not.
1483 * It's a match if the ip is in the ops->filter_hash or
1484 * the filter_hash does not exist or is empty,
1486 * the ip is not in the ops->notrace_hash.
1488 * This needs to be called with preemption disabled as
1489 * the hashes are freed with call_rcu_sched().
1492 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1494 struct ftrace_ops_hash hash;
1497 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1499 * There's a small race when adding ops that the ftrace handler
1500 * that wants regs, may be called without them. We can not
1501 * allow that handler to be called if regs is NULL.
1503 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1507 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1508 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
1510 if (hash_contains_ip(ip, &hash))
1519 * This is a double for. Do not use 'break' to break out of the loop,
1520 * you must use a goto.
1522 #define do_for_each_ftrace_rec(pg, rec) \
1523 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1525 for (_____i = 0; _____i < pg->index; _____i++) { \
1526 rec = &pg->records[_____i];
1528 #define while_for_each_ftrace_rec() \
1533 static int ftrace_cmp_recs(const void *a, const void *b)
1535 const struct dyn_ftrace *key = a;
1536 const struct dyn_ftrace *rec = b;
1538 if (key->flags < rec->ip)
1540 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1546 * ftrace_location_range - return the first address of a traced location
1547 * if it touches the given ip range
1548 * @start: start of range to search.
1549 * @end: end of range to search (inclusive). @end points to the last byte
1552 * Returns rec->ip if the related ftrace location is a least partly within
1553 * the given address range. That is, the first address of the instruction
1554 * that is either a NOP or call to the function tracer. It checks the ftrace
1555 * internal tables to determine if the address belongs or not.
1557 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1559 struct ftrace_page *pg;
1560 struct dyn_ftrace *rec;
1561 struct dyn_ftrace key;
1564 key.flags = end; /* overload flags, as it is unsigned long */
1566 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1567 if (end < pg->records[0].ip ||
1568 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1570 rec = bsearch(&key, pg->records, pg->index,
1571 sizeof(struct dyn_ftrace),
1581 * ftrace_location - return true if the ip giving is a traced location
1582 * @ip: the instruction pointer to check
1584 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1585 * That is, the instruction that is either a NOP or call to
1586 * the function tracer. It checks the ftrace internal tables to
1587 * determine if the address belongs or not.
1589 unsigned long ftrace_location(unsigned long ip)
1591 return ftrace_location_range(ip, ip);
1595 * ftrace_text_reserved - return true if range contains an ftrace location
1596 * @start: start of range to search
1597 * @end: end of range to search (inclusive). @end points to the last byte to check.
1599 * Returns 1 if @start and @end contains a ftrace location.
1600 * That is, the instruction that is either a NOP or call to
1601 * the function tracer. It checks the ftrace internal tables to
1602 * determine if the address belongs or not.
1604 int ftrace_text_reserved(const void *start, const void *end)
1608 ret = ftrace_location_range((unsigned long)start,
1609 (unsigned long)end);
1614 /* Test if ops registered to this rec needs regs */
1615 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1617 struct ftrace_ops *ops;
1618 bool keep_regs = false;
1620 for (ops = ftrace_ops_list;
1621 ops != &ftrace_list_end; ops = ops->next) {
1622 /* pass rec in as regs to have non-NULL val */
1623 if (ftrace_ops_test(ops, rec->ip, rec)) {
1624 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1634 static struct ftrace_ops *
1635 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1636 static struct ftrace_ops *
1637 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1638 static struct ftrace_ops *
1639 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1641 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1645 struct ftrace_hash *hash;
1646 struct ftrace_hash *other_hash;
1647 struct ftrace_page *pg;
1648 struct dyn_ftrace *rec;
1649 bool update = false;
1653 /* Only update if the ops has been registered */
1654 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1658 * In the filter_hash case:
1659 * If the count is zero, we update all records.
1660 * Otherwise we just update the items in the hash.
1662 * In the notrace_hash case:
1663 * We enable the update in the hash.
1664 * As disabling notrace means enabling the tracing,
1665 * and enabling notrace means disabling, the inc variable
1669 hash = ops->func_hash->filter_hash;
1670 other_hash = ops->func_hash->notrace_hash;
1671 if (ftrace_hash_empty(hash))
1675 hash = ops->func_hash->notrace_hash;
1676 other_hash = ops->func_hash->filter_hash;
1678 * If the notrace hash has no items,
1679 * then there's nothing to do.
1681 if (ftrace_hash_empty(hash))
1685 do_for_each_ftrace_rec(pg, rec) {
1686 int in_other_hash = 0;
1690 if (rec->flags & FTRACE_FL_DISABLED)
1695 * Only the filter_hash affects all records.
1696 * Update if the record is not in the notrace hash.
1698 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1701 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1702 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1705 * If filter_hash is set, we want to match all functions
1706 * that are in the hash but not in the other hash.
1708 * If filter_hash is not set, then we are decrementing.
1709 * That means we match anything that is in the hash
1710 * and also in the other_hash. That is, we need to turn
1711 * off functions in the other hash because they are disabled
1714 if (filter_hash && in_hash && !in_other_hash)
1716 else if (!filter_hash && in_hash &&
1717 (in_other_hash || ftrace_hash_empty(other_hash)))
1725 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1729 * If there's only a single callback registered to a
1730 * function, and the ops has a trampoline registered
1731 * for it, then we can call it directly.
1733 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1734 rec->flags |= FTRACE_FL_TRAMP;
1737 * If we are adding another function callback
1738 * to this function, and the previous had a
1739 * custom trampoline in use, then we need to go
1740 * back to the default trampoline.
1742 rec->flags &= ~FTRACE_FL_TRAMP;
1745 * If any ops wants regs saved for this function
1746 * then all ops will get saved regs.
1748 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1749 rec->flags |= FTRACE_FL_REGS;
1751 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1756 * If the rec had REGS enabled and the ops that is
1757 * being removed had REGS set, then see if there is
1758 * still any ops for this record that wants regs.
1759 * If not, we can stop recording them.
1761 if (ftrace_rec_count(rec) > 0 &&
1762 rec->flags & FTRACE_FL_REGS &&
1763 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1764 if (!test_rec_ops_needs_regs(rec))
1765 rec->flags &= ~FTRACE_FL_REGS;
1769 * The TRAMP needs to be set only if rec count
1770 * is decremented to one, and the ops that is
1771 * left has a trampoline. As TRAMP can only be
1772 * enabled if there is only a single ops attached
1775 if (ftrace_rec_count(rec) == 1 &&
1776 ftrace_find_tramp_ops_any_other(rec, ops))
1777 rec->flags |= FTRACE_FL_TRAMP;
1779 rec->flags &= ~FTRACE_FL_TRAMP;
1782 * flags will be cleared in ftrace_check_record()
1783 * if rec count is zero.
1788 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1789 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1791 /* Shortcut, if we handled all records, we are done. */
1792 if (!all && count == hash->count)
1794 } while_for_each_ftrace_rec();
1799 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1802 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1805 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1808 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1811 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1812 int filter_hash, int inc)
1814 struct ftrace_ops *op;
1816 __ftrace_hash_rec_update(ops, filter_hash, inc);
1818 if (ops->func_hash != &global_ops.local_hash)
1822 * If the ops shares the global_ops hash, then we need to update
1823 * all ops that are enabled and use this hash.
1825 do_for_each_ftrace_op(op, ftrace_ops_list) {
1829 if (op->func_hash == &global_ops.local_hash)
1830 __ftrace_hash_rec_update(op, filter_hash, inc);
1831 } while_for_each_ftrace_op(op);
1834 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1837 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1840 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1843 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1847 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1848 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1849 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1850 * Note that old_hash and new_hash has below meanings
1851 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1852 * - If the hash is EMPTY_HASH, it hits nothing
1853 * - Anything else hits the recs which match the hash entries.
1855 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1856 struct ftrace_hash *old_hash,
1857 struct ftrace_hash *new_hash)
1859 struct ftrace_page *pg;
1860 struct dyn_ftrace *rec, *end = NULL;
1863 /* Only update if the ops has been registered */
1864 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1867 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1871 * Since the IPMODIFY is a very address sensitive action, we do not
1872 * allow ftrace_ops to set all functions to new hash.
1874 if (!new_hash || !old_hash)
1877 /* Update rec->flags */
1878 do_for_each_ftrace_rec(pg, rec) {
1880 if (rec->flags & FTRACE_FL_DISABLED)
1883 /* We need to update only differences of filter_hash */
1884 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1885 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1886 if (in_old == in_new)
1890 /* New entries must ensure no others are using it */
1891 if (rec->flags & FTRACE_FL_IPMODIFY)
1893 rec->flags |= FTRACE_FL_IPMODIFY;
1894 } else /* Removed entry */
1895 rec->flags &= ~FTRACE_FL_IPMODIFY;
1896 } while_for_each_ftrace_rec();
1903 /* Roll back what we did above */
1904 do_for_each_ftrace_rec(pg, rec) {
1906 if (rec->flags & FTRACE_FL_DISABLED)
1912 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1913 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1914 if (in_old == in_new)
1918 rec->flags &= ~FTRACE_FL_IPMODIFY;
1920 rec->flags |= FTRACE_FL_IPMODIFY;
1921 } while_for_each_ftrace_rec();
1927 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1929 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1931 if (ftrace_hash_empty(hash))
1934 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1937 /* Disabling always succeeds */
1938 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1940 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1942 if (ftrace_hash_empty(hash))
1945 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1948 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1949 struct ftrace_hash *new_hash)
1951 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1953 if (ftrace_hash_empty(old_hash))
1956 if (ftrace_hash_empty(new_hash))
1959 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1962 static void print_ip_ins(const char *fmt, const unsigned char *p)
1964 char ins[MCOUNT_INSN_SIZE];
1967 if (probe_kernel_read(ins, p, MCOUNT_INSN_SIZE)) {
1968 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
1972 printk(KERN_CONT "%s", fmt);
1974 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1975 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
1978 enum ftrace_bug_type ftrace_bug_type;
1979 const void *ftrace_expected;
1981 static void print_bug_type(void)
1983 switch (ftrace_bug_type) {
1984 case FTRACE_BUG_UNKNOWN:
1986 case FTRACE_BUG_INIT:
1987 pr_info("Initializing ftrace call sites\n");
1989 case FTRACE_BUG_NOP:
1990 pr_info("Setting ftrace call site to NOP\n");
1992 case FTRACE_BUG_CALL:
1993 pr_info("Setting ftrace call site to call ftrace function\n");
1995 case FTRACE_BUG_UPDATE:
1996 pr_info("Updating ftrace call site to call a different ftrace function\n");
2002 * ftrace_bug - report and shutdown function tracer
2003 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2004 * @rec: The record that failed
2006 * The arch code that enables or disables the function tracing
2007 * can call ftrace_bug() when it has detected a problem in
2008 * modifying the code. @failed should be one of either:
2009 * EFAULT - if the problem happens on reading the @ip address
2010 * EINVAL - if what is read at @ip is not what was expected
2011 * EPERM - if the problem happens on writting to the @ip address
2013 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2015 unsigned long ip = rec ? rec->ip : 0;
2019 FTRACE_WARN_ON_ONCE(1);
2020 pr_info("ftrace faulted on modifying ");
2024 FTRACE_WARN_ON_ONCE(1);
2025 pr_info("ftrace failed to modify ");
2027 print_ip_ins(" actual: ", (unsigned char *)ip);
2029 if (ftrace_expected) {
2030 print_ip_ins(" expected: ", ftrace_expected);
2035 FTRACE_WARN_ON_ONCE(1);
2036 pr_info("ftrace faulted on writing ");
2040 FTRACE_WARN_ON_ONCE(1);
2041 pr_info("ftrace faulted on unknown error ");
2046 struct ftrace_ops *ops = NULL;
2048 pr_info("ftrace record flags: %lx\n", rec->flags);
2049 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2050 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2051 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2052 ops = ftrace_find_tramp_ops_any(rec);
2055 pr_cont("\ttramp: %pS (%pS)",
2056 (void *)ops->trampoline,
2058 ops = ftrace_find_tramp_ops_next(rec, ops);
2061 pr_cont("\ttramp: ERROR!");
2064 ip = ftrace_get_addr_curr(rec);
2065 pr_cont("\n expected tramp: %lx\n", ip);
2069 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2071 unsigned long flag = 0UL;
2073 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2075 if (rec->flags & FTRACE_FL_DISABLED)
2076 return FTRACE_UPDATE_IGNORE;
2079 * If we are updating calls:
2081 * If the record has a ref count, then we need to enable it
2082 * because someone is using it.
2084 * Otherwise we make sure its disabled.
2086 * If we are disabling calls, then disable all records that
2089 if (enable && ftrace_rec_count(rec))
2090 flag = FTRACE_FL_ENABLED;
2093 * If enabling and the REGS flag does not match the REGS_EN, or
2094 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2095 * this record. Set flags to fail the compare against ENABLED.
2098 if (!(rec->flags & FTRACE_FL_REGS) !=
2099 !(rec->flags & FTRACE_FL_REGS_EN))
2100 flag |= FTRACE_FL_REGS;
2102 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2103 !(rec->flags & FTRACE_FL_TRAMP_EN))
2104 flag |= FTRACE_FL_TRAMP;
2107 /* If the state of this record hasn't changed, then do nothing */
2108 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2109 return FTRACE_UPDATE_IGNORE;
2112 /* Save off if rec is being enabled (for return value) */
2113 flag ^= rec->flags & FTRACE_FL_ENABLED;
2116 rec->flags |= FTRACE_FL_ENABLED;
2117 if (flag & FTRACE_FL_REGS) {
2118 if (rec->flags & FTRACE_FL_REGS)
2119 rec->flags |= FTRACE_FL_REGS_EN;
2121 rec->flags &= ~FTRACE_FL_REGS_EN;
2123 if (flag & FTRACE_FL_TRAMP) {
2124 if (rec->flags & FTRACE_FL_TRAMP)
2125 rec->flags |= FTRACE_FL_TRAMP_EN;
2127 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2132 * If this record is being updated from a nop, then
2133 * return UPDATE_MAKE_CALL.
2135 * return UPDATE_MODIFY_CALL to tell the caller to convert
2136 * from the save regs, to a non-save regs function or
2137 * vice versa, or from a trampoline call.
2139 if (flag & FTRACE_FL_ENABLED) {
2140 ftrace_bug_type = FTRACE_BUG_CALL;
2141 return FTRACE_UPDATE_MAKE_CALL;
2144 ftrace_bug_type = FTRACE_BUG_UPDATE;
2145 return FTRACE_UPDATE_MODIFY_CALL;
2149 /* If there's no more users, clear all flags */
2150 if (!ftrace_rec_count(rec))
2154 * Just disable the record, but keep the ops TRAMP
2155 * and REGS states. The _EN flags must be disabled though.
2157 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2161 ftrace_bug_type = FTRACE_BUG_NOP;
2162 return FTRACE_UPDATE_MAKE_NOP;
2166 * ftrace_update_record, set a record that now is tracing or not
2167 * @rec: the record to update
2168 * @enable: set to 1 if the record is tracing, zero to force disable
2170 * The records that represent all functions that can be traced need
2171 * to be updated when tracing has been enabled.
2173 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2175 return ftrace_check_record(rec, enable, 1);
2179 * ftrace_test_record, check if the record has been enabled or not
2180 * @rec: the record to test
2181 * @enable: set to 1 to check if enabled, 0 if it is disabled
2183 * The arch code may need to test if a record is already set to
2184 * tracing to determine how to modify the function code that it
2187 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2189 return ftrace_check_record(rec, enable, 0);
2192 static struct ftrace_ops *
2193 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2195 struct ftrace_ops *op;
2196 unsigned long ip = rec->ip;
2198 do_for_each_ftrace_op(op, ftrace_ops_list) {
2200 if (!op->trampoline)
2203 if (hash_contains_ip(ip, op->func_hash))
2205 } while_for_each_ftrace_op(op);
2210 static struct ftrace_ops *
2211 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2213 struct ftrace_ops *op;
2214 unsigned long ip = rec->ip;
2216 do_for_each_ftrace_op(op, ftrace_ops_list) {
2218 if (op == op_exclude || !op->trampoline)
2221 if (hash_contains_ip(ip, op->func_hash))
2223 } while_for_each_ftrace_op(op);
2228 static struct ftrace_ops *
2229 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2230 struct ftrace_ops *op)
2232 unsigned long ip = rec->ip;
2234 while_for_each_ftrace_op(op) {
2236 if (!op->trampoline)
2239 if (hash_contains_ip(ip, op->func_hash))
2246 static struct ftrace_ops *
2247 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2249 struct ftrace_ops *op;
2250 unsigned long ip = rec->ip;
2253 * Need to check removed ops first.
2254 * If they are being removed, and this rec has a tramp,
2255 * and this rec is in the ops list, then it would be the
2256 * one with the tramp.
2259 if (hash_contains_ip(ip, &removed_ops->old_hash))
2264 * Need to find the current trampoline for a rec.
2265 * Now, a trampoline is only attached to a rec if there
2266 * was a single 'ops' attached to it. But this can be called
2267 * when we are adding another op to the rec or removing the
2268 * current one. Thus, if the op is being added, we can
2269 * ignore it because it hasn't attached itself to the rec
2272 * If an ops is being modified (hooking to different functions)
2273 * then we don't care about the new functions that are being
2274 * added, just the old ones (that are probably being removed).
2276 * If we are adding an ops to a function that already is using
2277 * a trampoline, it needs to be removed (trampolines are only
2278 * for single ops connected), then an ops that is not being
2279 * modified also needs to be checked.
2281 do_for_each_ftrace_op(op, ftrace_ops_list) {
2283 if (!op->trampoline)
2287 * If the ops is being added, it hasn't gotten to
2288 * the point to be removed from this tree yet.
2290 if (op->flags & FTRACE_OPS_FL_ADDING)
2295 * If the ops is being modified and is in the old
2296 * hash, then it is probably being removed from this
2299 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2300 hash_contains_ip(ip, &op->old_hash))
2303 * If the ops is not being added or modified, and it's
2304 * in its normal filter hash, then this must be the one
2307 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2308 hash_contains_ip(ip, op->func_hash))
2311 } while_for_each_ftrace_op(op);
2316 static struct ftrace_ops *
2317 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2319 struct ftrace_ops *op;
2320 unsigned long ip = rec->ip;
2322 do_for_each_ftrace_op(op, ftrace_ops_list) {
2323 /* pass rec in as regs to have non-NULL val */
2324 if (hash_contains_ip(ip, op->func_hash))
2326 } while_for_each_ftrace_op(op);
2332 * ftrace_get_addr_new - Get the call address to set to
2333 * @rec: The ftrace record descriptor
2335 * If the record has the FTRACE_FL_REGS set, that means that it
2336 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2337 * is not not set, then it wants to convert to the normal callback.
2339 * Returns the address of the trampoline to set to
2341 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2343 struct ftrace_ops *ops;
2345 /* Trampolines take precedence over regs */
2346 if (rec->flags & FTRACE_FL_TRAMP) {
2347 ops = ftrace_find_tramp_ops_new(rec);
2348 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2349 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2350 (void *)rec->ip, (void *)rec->ip, rec->flags);
2351 /* Ftrace is shutting down, return anything */
2352 return (unsigned long)FTRACE_ADDR;
2354 return ops->trampoline;
2357 if (rec->flags & FTRACE_FL_REGS)
2358 return (unsigned long)FTRACE_REGS_ADDR;
2360 return (unsigned long)FTRACE_ADDR;
2364 * ftrace_get_addr_curr - Get the call address that is already there
2365 * @rec: The ftrace record descriptor
2367 * The FTRACE_FL_REGS_EN is set when the record already points to
2368 * a function that saves all the regs. Basically the '_EN' version
2369 * represents the current state of the function.
2371 * Returns the address of the trampoline that is currently being called
2373 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2375 struct ftrace_ops *ops;
2377 /* Trampolines take precedence over regs */
2378 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2379 ops = ftrace_find_tramp_ops_curr(rec);
2380 if (FTRACE_WARN_ON(!ops)) {
2381 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2382 (void *)rec->ip, (void *)rec->ip);
2383 /* Ftrace is shutting down, return anything */
2384 return (unsigned long)FTRACE_ADDR;
2386 return ops->trampoline;
2389 if (rec->flags & FTRACE_FL_REGS_EN)
2390 return (unsigned long)FTRACE_REGS_ADDR;
2392 return (unsigned long)FTRACE_ADDR;
2396 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2398 unsigned long ftrace_old_addr;
2399 unsigned long ftrace_addr;
2402 ftrace_addr = ftrace_get_addr_new(rec);
2404 /* This needs to be done before we call ftrace_update_record */
2405 ftrace_old_addr = ftrace_get_addr_curr(rec);
2407 ret = ftrace_update_record(rec, enable);
2409 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2412 case FTRACE_UPDATE_IGNORE:
2415 case FTRACE_UPDATE_MAKE_CALL:
2416 ftrace_bug_type = FTRACE_BUG_CALL;
2417 return ftrace_make_call(rec, ftrace_addr);
2419 case FTRACE_UPDATE_MAKE_NOP:
2420 ftrace_bug_type = FTRACE_BUG_NOP;
2421 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2423 case FTRACE_UPDATE_MODIFY_CALL:
2424 ftrace_bug_type = FTRACE_BUG_UPDATE;
2425 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2428 return -1; /* unknow ftrace bug */
2431 void __weak ftrace_replace_code(int enable)
2433 struct dyn_ftrace *rec;
2434 struct ftrace_page *pg;
2437 if (unlikely(ftrace_disabled))
2440 do_for_each_ftrace_rec(pg, rec) {
2442 if (rec->flags & FTRACE_FL_DISABLED)
2445 failed = __ftrace_replace_code(rec, enable);
2447 ftrace_bug(failed, rec);
2448 /* Stop processing */
2451 } while_for_each_ftrace_rec();
2454 struct ftrace_rec_iter {
2455 struct ftrace_page *pg;
2460 * ftrace_rec_iter_start, start up iterating over traced functions
2462 * Returns an iterator handle that is used to iterate over all
2463 * the records that represent address locations where functions
2466 * May return NULL if no records are available.
2468 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2471 * We only use a single iterator.
2472 * Protected by the ftrace_lock mutex.
2474 static struct ftrace_rec_iter ftrace_rec_iter;
2475 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2477 iter->pg = ftrace_pages_start;
2480 /* Could have empty pages */
2481 while (iter->pg && !iter->pg->index)
2482 iter->pg = iter->pg->next;
2491 * ftrace_rec_iter_next, get the next record to process.
2492 * @iter: The handle to the iterator.
2494 * Returns the next iterator after the given iterator @iter.
2496 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2500 if (iter->index >= iter->pg->index) {
2501 iter->pg = iter->pg->next;
2504 /* Could have empty pages */
2505 while (iter->pg && !iter->pg->index)
2506 iter->pg = iter->pg->next;
2516 * ftrace_rec_iter_record, get the record at the iterator location
2517 * @iter: The current iterator location
2519 * Returns the record that the current @iter is at.
2521 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2523 return &iter->pg->records[iter->index];
2527 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2531 if (unlikely(ftrace_disabled))
2534 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2536 ftrace_bug_type = FTRACE_BUG_INIT;
2537 ftrace_bug(ret, rec);
2544 * archs can override this function if they must do something
2545 * before the modifying code is performed.
2547 int __weak ftrace_arch_code_modify_prepare(void)
2553 * archs can override this function if they must do something
2554 * after the modifying code is performed.
2556 int __weak ftrace_arch_code_modify_post_process(void)
2561 void ftrace_modify_all_code(int command)
2563 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2567 * If the ftrace_caller calls a ftrace_ops func directly,
2568 * we need to make sure that it only traces functions it
2569 * expects to trace. When doing the switch of functions,
2570 * we need to update to the ftrace_ops_list_func first
2571 * before the transition between old and new calls are set,
2572 * as the ftrace_ops_list_func will check the ops hashes
2573 * to make sure the ops are having the right functions
2577 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2578 if (FTRACE_WARN_ON(err))
2582 if (command & FTRACE_UPDATE_CALLS)
2583 ftrace_replace_code(1);
2584 else if (command & FTRACE_DISABLE_CALLS)
2585 ftrace_replace_code(0);
2587 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2588 function_trace_op = set_function_trace_op;
2590 /* If irqs are disabled, we are in stop machine */
2591 if (!irqs_disabled())
2592 smp_call_function(ftrace_sync_ipi, NULL, 1);
2593 err = ftrace_update_ftrace_func(ftrace_trace_function);
2594 if (FTRACE_WARN_ON(err))
2598 if (command & FTRACE_START_FUNC_RET)
2599 err = ftrace_enable_ftrace_graph_caller();
2600 else if (command & FTRACE_STOP_FUNC_RET)
2601 err = ftrace_disable_ftrace_graph_caller();
2602 FTRACE_WARN_ON(err);
2605 static int __ftrace_modify_code(void *data)
2607 int *command = data;
2609 ftrace_modify_all_code(*command);
2615 * ftrace_run_stop_machine, go back to the stop machine method
2616 * @command: The command to tell ftrace what to do
2618 * If an arch needs to fall back to the stop machine method, the
2619 * it can call this function.
2621 void ftrace_run_stop_machine(int command)
2623 stop_machine(__ftrace_modify_code, &command, NULL);
2627 * arch_ftrace_update_code, modify the code to trace or not trace
2628 * @command: The command that needs to be done
2630 * Archs can override this function if it does not need to
2631 * run stop_machine() to modify code.
2633 void __weak arch_ftrace_update_code(int command)
2635 ftrace_run_stop_machine(command);
2638 static void ftrace_run_update_code(int command)
2642 ret = ftrace_arch_code_modify_prepare();
2643 FTRACE_WARN_ON(ret);
2648 * By default we use stop_machine() to modify the code.
2649 * But archs can do what ever they want as long as it
2650 * is safe. The stop_machine() is the safest, but also
2651 * produces the most overhead.
2653 arch_ftrace_update_code(command);
2655 ret = ftrace_arch_code_modify_post_process();
2656 FTRACE_WARN_ON(ret);
2659 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2660 struct ftrace_ops_hash *old_hash)
2662 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2663 ops->old_hash.filter_hash = old_hash->filter_hash;
2664 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2665 ftrace_run_update_code(command);
2666 ops->old_hash.filter_hash = NULL;
2667 ops->old_hash.notrace_hash = NULL;
2668 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2671 static ftrace_func_t saved_ftrace_func;
2672 static int ftrace_start_up;
2674 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2678 static void per_cpu_ops_free(struct ftrace_ops *ops)
2680 free_percpu(ops->disabled);
2683 static void ftrace_startup_enable(int command)
2685 if (saved_ftrace_func != ftrace_trace_function) {
2686 saved_ftrace_func = ftrace_trace_function;
2687 command |= FTRACE_UPDATE_TRACE_FUNC;
2690 if (!command || !ftrace_enabled)
2693 ftrace_run_update_code(command);
2696 static void ftrace_startup_all(int command)
2698 update_all_ops = true;
2699 ftrace_startup_enable(command);
2700 update_all_ops = false;
2703 static int ftrace_startup(struct ftrace_ops *ops, int command)
2707 if (unlikely(ftrace_disabled))
2710 ret = __register_ftrace_function(ops);
2717 * Note that ftrace probes uses this to start up
2718 * and modify functions it will probe. But we still
2719 * set the ADDING flag for modification, as probes
2720 * do not have trampolines. If they add them in the
2721 * future, then the probes will need to distinguish
2722 * between adding and updating probes.
2724 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2726 ret = ftrace_hash_ipmodify_enable(ops);
2728 /* Rollback registration process */
2729 __unregister_ftrace_function(ops);
2731 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2735 if (ftrace_hash_rec_enable(ops, 1))
2736 command |= FTRACE_UPDATE_CALLS;
2738 ftrace_startup_enable(command);
2741 * If ftrace is in an undefined state, we just remove ops from list
2742 * to prevent the NULL pointer, instead of totally rolling it back and
2743 * free trampoline, because those actions could cause further damage.
2745 if (unlikely(ftrace_disabled)) {
2746 __unregister_ftrace_function(ops);
2750 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2755 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2759 if (unlikely(ftrace_disabled))
2762 ret = __unregister_ftrace_function(ops);
2768 * Just warn in case of unbalance, no need to kill ftrace, it's not
2769 * critical but the ftrace_call callers may be never nopped again after
2770 * further ftrace uses.
2772 WARN_ON_ONCE(ftrace_start_up < 0);
2774 /* Disabling ipmodify never fails */
2775 ftrace_hash_ipmodify_disable(ops);
2777 if (ftrace_hash_rec_disable(ops, 1))
2778 command |= FTRACE_UPDATE_CALLS;
2780 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2782 if (saved_ftrace_func != ftrace_trace_function) {
2783 saved_ftrace_func = ftrace_trace_function;
2784 command |= FTRACE_UPDATE_TRACE_FUNC;
2787 if (!command || !ftrace_enabled) {
2789 * If these are dynamic or per_cpu ops, they still
2790 * need their data freed. Since, function tracing is
2791 * not currently active, we can just free them
2792 * without synchronizing all CPUs.
2794 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU))
2801 * If the ops uses a trampoline, then it needs to be
2802 * tested first on update.
2804 ops->flags |= FTRACE_OPS_FL_REMOVING;
2807 /* The trampoline logic checks the old hashes */
2808 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2809 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2811 ftrace_run_update_code(command);
2814 * If there's no more ops registered with ftrace, run a
2815 * sanity check to make sure all rec flags are cleared.
2817 if (ftrace_ops_list == &ftrace_list_end) {
2818 struct ftrace_page *pg;
2819 struct dyn_ftrace *rec;
2821 do_for_each_ftrace_rec(pg, rec) {
2822 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2823 pr_warn(" %pS flags:%lx\n",
2824 (void *)rec->ip, rec->flags);
2825 } while_for_each_ftrace_rec();
2828 ops->old_hash.filter_hash = NULL;
2829 ops->old_hash.notrace_hash = NULL;
2832 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2835 * Dynamic ops may be freed, we must make sure that all
2836 * callers are done before leaving this function.
2837 * The same goes for freeing the per_cpu data of the per_cpu
2840 * Again, normal synchronize_sched() is not good enough.
2841 * We need to do a hard force of sched synchronization.
2842 * This is because we use preempt_disable() to do RCU, but
2843 * the function tracers can be called where RCU is not watching
2844 * (like before user_exit()). We can not rely on the RCU
2845 * infrastructure to do the synchronization, thus we must do it
2848 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
2849 schedule_on_each_cpu(ftrace_sync);
2852 arch_ftrace_trampoline_free(ops);
2854 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2855 per_cpu_ops_free(ops);
2861 static void ftrace_startup_sysctl(void)
2865 if (unlikely(ftrace_disabled))
2868 /* Force update next time */
2869 saved_ftrace_func = NULL;
2870 /* ftrace_start_up is true if we want ftrace running */
2871 if (ftrace_start_up) {
2872 command = FTRACE_UPDATE_CALLS;
2873 if (ftrace_graph_active)
2874 command |= FTRACE_START_FUNC_RET;
2875 ftrace_startup_enable(command);
2879 static void ftrace_shutdown_sysctl(void)
2883 if (unlikely(ftrace_disabled))
2886 /* ftrace_start_up is true if ftrace is running */
2887 if (ftrace_start_up) {
2888 command = FTRACE_DISABLE_CALLS;
2889 if (ftrace_graph_active)
2890 command |= FTRACE_STOP_FUNC_RET;
2891 ftrace_run_update_code(command);
2895 static cycle_t ftrace_update_time;
2896 unsigned long ftrace_update_tot_cnt;
2898 static inline int ops_traces_mod(struct ftrace_ops *ops)
2901 * Filter_hash being empty will default to trace module.
2902 * But notrace hash requires a test of individual module functions.
2904 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2905 ftrace_hash_empty(ops->func_hash->notrace_hash);
2909 * Check if the current ops references the record.
2911 * If the ops traces all functions, then it was already accounted for.
2912 * If the ops does not trace the current record function, skip it.
2913 * If the ops ignores the function via notrace filter, skip it.
2916 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2918 /* If ops isn't enabled, ignore it */
2919 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2922 /* If ops traces all then it includes this function */
2923 if (ops_traces_mod(ops))
2926 /* The function must be in the filter */
2927 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2928 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2931 /* If in notrace hash, we ignore it too */
2932 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2938 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2940 struct ftrace_page *pg;
2941 struct dyn_ftrace *p;
2942 cycle_t start, stop;
2943 unsigned long update_cnt = 0;
2944 unsigned long rec_flags = 0;
2947 start = ftrace_now(raw_smp_processor_id());
2950 * When a module is loaded, this function is called to convert
2951 * the calls to mcount in its text to nops, and also to create
2952 * an entry in the ftrace data. Now, if ftrace is activated
2953 * after this call, but before the module sets its text to
2954 * read-only, the modification of enabling ftrace can fail if
2955 * the read-only is done while ftrace is converting the calls.
2956 * To prevent this, the module's records are set as disabled
2957 * and will be enabled after the call to set the module's text
2961 rec_flags |= FTRACE_FL_DISABLED;
2963 for (pg = new_pgs; pg; pg = pg->next) {
2965 for (i = 0; i < pg->index; i++) {
2967 /* If something went wrong, bail without enabling anything */
2968 if (unlikely(ftrace_disabled))
2971 p = &pg->records[i];
2972 p->flags = rec_flags;
2975 * Do the initial record conversion from mcount jump
2976 * to the NOP instructions.
2978 if (!ftrace_code_disable(mod, p))
2985 stop = ftrace_now(raw_smp_processor_id());
2986 ftrace_update_time = stop - start;
2987 ftrace_update_tot_cnt += update_cnt;
2992 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2997 if (WARN_ON(!count))
3000 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3003 * We want to fill as much as possible. No more than a page
3006 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3010 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3013 /* if we can't allocate this size, try something smaller */
3020 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3029 static struct ftrace_page *
3030 ftrace_allocate_pages(unsigned long num_to_init)
3032 struct ftrace_page *start_pg;
3033 struct ftrace_page *pg;
3040 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3045 * Try to allocate as much as possible in one continues
3046 * location that fills in all of the space. We want to
3047 * waste as little space as possible.
3050 cnt = ftrace_allocate_records(pg, num_to_init);
3058 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3070 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3071 free_pages((unsigned long)pg->records, order);
3072 start_pg = pg->next;
3076 pr_info("ftrace: FAILED to allocate memory for functions\n");
3080 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3082 struct ftrace_iterator {
3085 struct ftrace_page *pg;
3086 struct dyn_ftrace *func;
3087 struct ftrace_func_probe *probe;
3088 struct trace_parser parser;
3089 struct ftrace_hash *hash;
3090 struct ftrace_ops *ops;
3097 t_hash_next(struct seq_file *m, loff_t *pos)
3099 struct ftrace_iterator *iter = m->private;
3100 struct hlist_node *hnd = NULL;
3101 struct hlist_head *hhd;
3107 hnd = &iter->probe->node;
3109 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3112 hhd = &ftrace_func_hash[iter->hidx];
3114 if (hlist_empty(hhd)) {
3130 if (WARN_ON_ONCE(!hnd))
3133 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3138 static void *t_hash_start(struct seq_file *m, loff_t *pos)
3140 struct ftrace_iterator *iter = m->private;
3144 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3147 if (iter->func_pos > *pos)
3151 for (l = 0; l <= (*pos - iter->func_pos); ) {
3152 p = t_hash_next(m, &l);
3159 /* Only set this if we have an item */
3160 iter->flags |= FTRACE_ITER_HASH;
3166 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
3168 struct ftrace_func_probe *rec;
3171 if (WARN_ON_ONCE(!rec))
3174 if (rec->ops->print)
3175 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3177 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
3180 seq_printf(m, ":%p", rec->data);
3187 t_next(struct seq_file *m, void *v, loff_t *pos)
3189 struct ftrace_iterator *iter = m->private;
3190 struct ftrace_ops *ops = iter->ops;
3191 struct dyn_ftrace *rec = NULL;
3193 if (unlikely(ftrace_disabled))
3196 if (iter->flags & FTRACE_ITER_HASH)
3197 return t_hash_next(m, pos);
3200 iter->pos = iter->func_pos = *pos;
3202 if (iter->flags & FTRACE_ITER_PRINTALL)
3203 return t_hash_start(m, pos);
3206 if (iter->idx >= iter->pg->index) {
3207 if (iter->pg->next) {
3208 iter->pg = iter->pg->next;
3213 rec = &iter->pg->records[iter->idx++];
3214 if (((iter->flags & FTRACE_ITER_FILTER) &&
3215 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
3217 ((iter->flags & FTRACE_ITER_NOTRACE) &&
3218 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
3220 ((iter->flags & FTRACE_ITER_ENABLED) &&
3221 !(rec->flags & FTRACE_FL_ENABLED))) {
3229 return t_hash_start(m, pos);
3236 static void reset_iter_read(struct ftrace_iterator *iter)
3240 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
3243 static void *t_start(struct seq_file *m, loff_t *pos)
3245 struct ftrace_iterator *iter = m->private;
3246 struct ftrace_ops *ops = iter->ops;
3250 mutex_lock(&ftrace_lock);
3252 if (unlikely(ftrace_disabled))
3256 * If an lseek was done, then reset and start from beginning.
3258 if (*pos < iter->pos)
3259 reset_iter_read(iter);
3262 * For set_ftrace_filter reading, if we have the filter
3263 * off, we can short cut and just print out that all
3264 * functions are enabled.
3266 if ((iter->flags & FTRACE_ITER_FILTER &&
3267 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
3268 (iter->flags & FTRACE_ITER_NOTRACE &&
3269 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
3271 return t_hash_start(m, pos);
3272 iter->flags |= FTRACE_ITER_PRINTALL;
3273 /* reset in case of seek/pread */
3274 iter->flags &= ~FTRACE_ITER_HASH;
3278 if (iter->flags & FTRACE_ITER_HASH)
3279 return t_hash_start(m, pos);
3282 * Unfortunately, we need to restart at ftrace_pages_start
3283 * every time we let go of the ftrace_mutex. This is because
3284 * those pointers can change without the lock.
3286 iter->pg = ftrace_pages_start;
3288 for (l = 0; l <= *pos; ) {
3289 p = t_next(m, p, &l);
3295 return t_hash_start(m, pos);
3300 static void t_stop(struct seq_file *m, void *p)
3302 mutex_unlock(&ftrace_lock);
3306 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3311 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3312 struct dyn_ftrace *rec)
3316 ptr = arch_ftrace_trampoline_func(ops, rec);
3318 seq_printf(m, " ->%pS", ptr);
3321 static int t_show(struct seq_file *m, void *v)
3323 struct ftrace_iterator *iter = m->private;
3324 struct dyn_ftrace *rec;
3326 if (iter->flags & FTRACE_ITER_HASH)
3327 return t_hash_show(m, iter);
3329 if (iter->flags & FTRACE_ITER_PRINTALL) {
3330 if (iter->flags & FTRACE_ITER_NOTRACE)
3331 seq_puts(m, "#### no functions disabled ####\n");
3333 seq_puts(m, "#### all functions enabled ####\n");
3342 seq_printf(m, "%ps", (void *)rec->ip);
3343 if (iter->flags & FTRACE_ITER_ENABLED) {
3344 struct ftrace_ops *ops;
3346 seq_printf(m, " (%ld)%s%s",
3347 ftrace_rec_count(rec),
3348 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3349 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3350 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3351 ops = ftrace_find_tramp_ops_any(rec);
3354 seq_printf(m, "\ttramp: %pS (%pS)",
3355 (void *)ops->trampoline,
3357 add_trampoline_func(m, ops, rec);
3358 ops = ftrace_find_tramp_ops_next(rec, ops);
3361 seq_puts(m, "\ttramp: ERROR!");
3363 add_trampoline_func(m, NULL, rec);
3372 static const struct seq_operations show_ftrace_seq_ops = {
3380 ftrace_avail_open(struct inode *inode, struct file *file)
3382 struct ftrace_iterator *iter;
3384 if (unlikely(ftrace_disabled))
3387 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3389 iter->pg = ftrace_pages_start;
3390 iter->ops = &global_ops;
3393 return iter ? 0 : -ENOMEM;
3397 ftrace_enabled_open(struct inode *inode, struct file *file)
3399 struct ftrace_iterator *iter;
3401 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3403 iter->pg = ftrace_pages_start;
3404 iter->flags = FTRACE_ITER_ENABLED;
3405 iter->ops = &global_ops;
3408 return iter ? 0 : -ENOMEM;
3412 * ftrace_regex_open - initialize function tracer filter files
3413 * @ops: The ftrace_ops that hold the hash filters
3414 * @flag: The type of filter to process
3415 * @inode: The inode, usually passed in to your open routine
3416 * @file: The file, usually passed in to your open routine
3418 * ftrace_regex_open() initializes the filter files for the
3419 * @ops. Depending on @flag it may process the filter hash or
3420 * the notrace hash of @ops. With this called from the open
3421 * routine, you can use ftrace_filter_write() for the write
3422 * routine if @flag has FTRACE_ITER_FILTER set, or
3423 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3424 * tracing_lseek() should be used as the lseek routine, and
3425 * release must call ftrace_regex_release().
3428 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3429 struct inode *inode, struct file *file)
3431 struct ftrace_iterator *iter;
3432 struct ftrace_hash *hash;
3435 ftrace_ops_init(ops);
3437 if (unlikely(ftrace_disabled))
3440 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3444 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3452 mutex_lock(&ops->func_hash->regex_lock);
3454 if (flag & FTRACE_ITER_NOTRACE)
3455 hash = ops->func_hash->notrace_hash;
3457 hash = ops->func_hash->filter_hash;
3459 if (file->f_mode & FMODE_WRITE) {
3460 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3462 if (file->f_flags & O_TRUNC)
3463 iter->hash = alloc_ftrace_hash(size_bits);
3465 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3468 trace_parser_put(&iter->parser);
3475 if (file->f_mode & FMODE_READ) {
3476 iter->pg = ftrace_pages_start;
3478 ret = seq_open(file, &show_ftrace_seq_ops);
3480 struct seq_file *m = file->private_data;
3484 free_ftrace_hash(iter->hash);
3485 trace_parser_put(&iter->parser);
3489 file->private_data = iter;
3492 mutex_unlock(&ops->func_hash->regex_lock);
3498 ftrace_filter_open(struct inode *inode, struct file *file)
3500 struct ftrace_ops *ops = inode->i_private;
3502 return ftrace_regex_open(ops,
3503 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3508 ftrace_notrace_open(struct inode *inode, struct file *file)
3510 struct ftrace_ops *ops = inode->i_private;
3512 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3516 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3517 struct ftrace_glob {
3524 * If symbols in an architecture don't correspond exactly to the user-visible
3525 * name of what they represent, it is possible to define this function to
3526 * perform the necessary adjustments.
3528 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3533 static int ftrace_match(char *str, struct ftrace_glob *g)
3538 str = arch_ftrace_match_adjust(str, g->search);
3542 if (strcmp(str, g->search) == 0)
3545 case MATCH_FRONT_ONLY:
3546 if (strncmp(str, g->search, g->len) == 0)
3549 case MATCH_MIDDLE_ONLY:
3550 if (strstr(str, g->search))
3553 case MATCH_END_ONLY:
3555 if (slen >= g->len &&
3556 memcmp(str + slen - g->len, g->search, g->len) == 0)
3565 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3567 struct ftrace_func_entry *entry;
3570 entry = ftrace_lookup_ip(hash, rec->ip);
3572 /* Do nothing if it doesn't exist */
3576 free_hash_entry(hash, entry);
3578 /* Do nothing if it exists */
3582 ret = add_hash_entry(hash, rec->ip);
3588 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3589 struct ftrace_glob *mod_g, int exclude_mod)
3591 char str[KSYM_SYMBOL_LEN];
3594 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3597 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3599 /* blank module name to match all modules */
3601 /* blank module globbing: modname xor exclude_mod */
3602 if ((!exclude_mod) != (!modname))
3607 /* not matching the module */
3608 if (!modname || !mod_matches) {
3615 if (mod_matches && exclude_mod)
3619 /* blank search means to match all funcs in the mod */
3624 return ftrace_match(str, func_g);
3628 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3630 struct ftrace_page *pg;
3631 struct dyn_ftrace *rec;
3632 struct ftrace_glob func_g = { .type = MATCH_FULL };
3633 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3634 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3635 int exclude_mod = 0;
3638 int clear_filter = 0;
3641 func_g.type = filter_parse_regex(func, len, &func_g.search,
3643 func_g.len = strlen(func_g.search);
3647 mod_g.type = filter_parse_regex(mod, strlen(mod),
3648 &mod_g.search, &exclude_mod);
3649 mod_g.len = strlen(mod_g.search);
3652 mutex_lock(&ftrace_lock);
3654 if (unlikely(ftrace_disabled))
3657 do_for_each_ftrace_rec(pg, rec) {
3659 if (rec->flags & FTRACE_FL_DISABLED)
3662 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3663 ret = enter_record(hash, rec, clear_filter);
3670 } while_for_each_ftrace_rec();
3672 mutex_unlock(&ftrace_lock);
3678 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3680 return match_records(hash, buff, len, NULL);
3685 * We register the module command as a template to show others how
3686 * to register the a command as well.
3690 ftrace_mod_callback(struct ftrace_hash *hash,
3691 char *func, char *cmd, char *module, int enable)
3696 * cmd == 'mod' because we only registered this func
3697 * for the 'mod' ftrace_func_command.
3698 * But if you register one func with multiple commands,
3699 * you can tell which command was used by the cmd
3702 ret = match_records(hash, func, strlen(func), module);
3710 static struct ftrace_func_command ftrace_mod_cmd = {
3712 .func = ftrace_mod_callback,
3715 static int __init ftrace_mod_cmd_init(void)
3717 return register_ftrace_command(&ftrace_mod_cmd);
3719 core_initcall(ftrace_mod_cmd_init);
3721 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3722 struct ftrace_ops *op, struct pt_regs *pt_regs)
3724 struct ftrace_func_probe *entry;
3725 struct hlist_head *hhd;
3728 key = hash_long(ip, FTRACE_HASH_BITS);
3730 hhd = &ftrace_func_hash[key];
3732 if (hlist_empty(hhd))
3736 * Disable preemption for these calls to prevent a RCU grace
3737 * period. This syncs the hash iteration and freeing of items
3738 * on the hash. rcu_read_lock is too dangerous here.
3740 preempt_disable_notrace();
3741 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3742 if (entry->ip == ip)
3743 entry->ops->func(ip, parent_ip, &entry->data);
3745 preempt_enable_notrace();
3748 static struct ftrace_ops trace_probe_ops __read_mostly =
3750 .func = function_trace_probe_call,
3751 .flags = FTRACE_OPS_FL_INITIALIZED,
3752 INIT_OPS_HASH(trace_probe_ops)
3755 static int ftrace_probe_registered;
3757 static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
3762 if (ftrace_probe_registered) {
3763 /* still need to update the function call sites */
3765 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3770 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3771 struct hlist_head *hhd = &ftrace_func_hash[i];
3775 /* Nothing registered? */
3776 if (i == FTRACE_FUNC_HASHSIZE)
3779 ret = ftrace_startup(&trace_probe_ops, 0);
3781 ftrace_probe_registered = 1;
3784 static bool __disable_ftrace_function_probe(void)
3788 if (!ftrace_probe_registered)
3791 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3792 struct hlist_head *hhd = &ftrace_func_hash[i];
3797 /* no more funcs left */
3798 ftrace_shutdown(&trace_probe_ops, 0);
3800 ftrace_probe_registered = 0;
3805 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3807 if (entry->ops->free)
3808 entry->ops->free(entry->ops, entry->ip, &entry->data);
3813 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3816 struct ftrace_ops_hash old_hash_ops;
3817 struct ftrace_func_probe *entry;
3818 struct ftrace_glob func_g;
3819 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3820 struct ftrace_hash *old_hash = *orig_hash;
3821 struct ftrace_hash *hash;
3822 struct ftrace_page *pg;
3823 struct dyn_ftrace *rec;
3829 func_g.type = filter_parse_regex(glob, strlen(glob),
3830 &func_g.search, ¬);
3831 func_g.len = strlen(func_g.search);
3833 /* we do not support '!' for function probes */
3837 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3839 old_hash_ops.filter_hash = old_hash;
3840 /* Probes only have filters */
3841 old_hash_ops.notrace_hash = NULL;
3843 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3849 if (unlikely(ftrace_disabled)) {
3854 mutex_lock(&ftrace_lock);
3856 do_for_each_ftrace_rec(pg, rec) {
3858 if (rec->flags & FTRACE_FL_DISABLED)
3861 if (!ftrace_match_record(rec, &func_g, NULL, 0))
3864 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3866 /* If we did not process any, then return error */
3877 * The caller might want to do something special
3878 * for each function we find. We call the callback
3879 * to give the caller an opportunity to do so.
3882 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3883 /* caller does not like this func */
3889 ret = enter_record(hash, rec, 0);
3897 entry->ip = rec->ip;
3899 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3900 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3902 } while_for_each_ftrace_rec();
3904 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3906 __enable_ftrace_function_probe(&old_hash_ops);
3909 free_ftrace_hash_rcu(old_hash);
3914 mutex_unlock(&ftrace_lock);
3916 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3917 free_ftrace_hash(hash);
3923 PROBE_TEST_FUNC = 1,
3928 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3929 void *data, int flags)
3931 struct ftrace_ops_hash old_hash_ops;
3932 struct ftrace_func_entry *rec_entry;
3933 struct ftrace_func_probe *entry;
3934 struct ftrace_func_probe *p;
3935 struct ftrace_glob func_g;
3936 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3937 struct ftrace_hash *old_hash = *orig_hash;
3938 struct list_head free_list;
3939 struct ftrace_hash *hash;
3940 struct hlist_node *tmp;
3941 char str[KSYM_SYMBOL_LEN];
3945 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3946 func_g.search = NULL;
3950 func_g.type = filter_parse_regex(glob, strlen(glob),
3951 &func_g.search, ¬);
3952 func_g.len = strlen(func_g.search);
3954 /* we do not support '!' for function probes */
3959 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3961 old_hash_ops.filter_hash = old_hash;
3962 /* Probes only have filters */
3963 old_hash_ops.notrace_hash = NULL;
3965 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3967 /* Hmm, should report this somehow */
3970 INIT_LIST_HEAD(&free_list);
3972 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3973 struct hlist_head *hhd = &ftrace_func_hash[i];
3975 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3977 /* break up if statements for readability */
3978 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3981 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3984 /* do this last, since it is the most expensive */
3985 if (func_g.search) {
3986 kallsyms_lookup(entry->ip, NULL, NULL,
3988 if (!ftrace_match(str, &func_g))
3992 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3993 /* It is possible more than one entry had this ip */
3995 free_hash_entry(hash, rec_entry);
3997 hlist_del_rcu(&entry->node);
3998 list_add(&entry->free_list, &free_list);
4001 mutex_lock(&ftrace_lock);
4002 disabled = __disable_ftrace_function_probe();
4004 * Remove after the disable is called. Otherwise, if the last
4005 * probe is removed, a null hash means *all enabled*.
4007 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
4009 /* still need to update the function call sites */
4010 if (ftrace_enabled && !disabled)
4011 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
4013 synchronize_sched();
4015 free_ftrace_hash_rcu(old_hash);
4017 list_for_each_entry_safe(entry, p, &free_list, free_list) {
4018 list_del(&entry->free_list);
4019 ftrace_free_entry(entry);
4021 mutex_unlock(&ftrace_lock);
4024 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
4025 free_ftrace_hash(hash);
4029 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
4032 __unregister_ftrace_function_probe(glob, ops, data,
4033 PROBE_TEST_FUNC | PROBE_TEST_DATA);
4037 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
4039 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
4042 void unregister_ftrace_function_probe_all(char *glob)
4044 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
4047 static LIST_HEAD(ftrace_commands);
4048 static DEFINE_MUTEX(ftrace_cmd_mutex);
4051 * Currently we only register ftrace commands from __init, so mark this
4054 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4056 struct ftrace_func_command *p;
4059 mutex_lock(&ftrace_cmd_mutex);
4060 list_for_each_entry(p, &ftrace_commands, list) {
4061 if (strcmp(cmd->name, p->name) == 0) {
4066 list_add(&cmd->list, &ftrace_commands);
4068 mutex_unlock(&ftrace_cmd_mutex);
4074 * Currently we only unregister ftrace commands from __init, so mark
4077 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4079 struct ftrace_func_command *p, *n;
4082 mutex_lock(&ftrace_cmd_mutex);
4083 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4084 if (strcmp(cmd->name, p->name) == 0) {
4086 list_del_init(&p->list);
4091 mutex_unlock(&ftrace_cmd_mutex);
4096 static int ftrace_process_regex(struct ftrace_hash *hash,
4097 char *buff, int len, int enable)
4099 char *func, *command, *next = buff;
4100 struct ftrace_func_command *p;
4103 func = strsep(&next, ":");
4106 ret = ftrace_match_records(hash, func, len);
4116 command = strsep(&next, ":");
4118 mutex_lock(&ftrace_cmd_mutex);
4119 list_for_each_entry(p, &ftrace_commands, list) {
4120 if (strcmp(p->name, command) == 0) {
4121 ret = p->func(hash, func, command, next, enable);
4126 mutex_unlock(&ftrace_cmd_mutex);
4132 ftrace_regex_write(struct file *file, const char __user *ubuf,
4133 size_t cnt, loff_t *ppos, int enable)
4135 struct ftrace_iterator *iter;
4136 struct trace_parser *parser;
4142 if (file->f_mode & FMODE_READ) {
4143 struct seq_file *m = file->private_data;
4146 iter = file->private_data;
4148 if (unlikely(ftrace_disabled))
4151 /* iter->hash is a local copy, so we don't need regex_lock */
4153 parser = &iter->parser;
4154 read = trace_get_user(parser, ubuf, cnt, ppos);
4156 if (read >= 0 && trace_parser_loaded(parser) &&
4157 !trace_parser_cont(parser)) {
4158 ret = ftrace_process_regex(iter->hash, parser->buffer,
4159 parser->idx, enable);
4160 trace_parser_clear(parser);
4171 ftrace_filter_write(struct file *file, const char __user *ubuf,
4172 size_t cnt, loff_t *ppos)
4174 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4178 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4179 size_t cnt, loff_t *ppos)
4181 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4185 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4187 struct ftrace_func_entry *entry;
4189 if (!ftrace_location(ip))
4193 entry = ftrace_lookup_ip(hash, ip);
4196 free_hash_entry(hash, entry);
4200 return add_hash_entry(hash, ip);
4203 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4204 struct ftrace_ops_hash *old_hash)
4206 struct ftrace_ops *op;
4208 if (!ftrace_enabled)
4211 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4212 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4217 * If this is the shared global_ops filter, then we need to
4218 * check if there is another ops that shares it, is enabled.
4219 * If so, we still need to run the modify code.
4221 if (ops->func_hash != &global_ops.local_hash)
4224 do_for_each_ftrace_op(op, ftrace_ops_list) {
4225 if (op->func_hash == &global_ops.local_hash &&
4226 op->flags & FTRACE_OPS_FL_ENABLED) {
4227 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4228 /* Only need to do this once */
4231 } while_for_each_ftrace_op(op);
4235 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4236 unsigned long ip, int remove, int reset, int enable)
4238 struct ftrace_hash **orig_hash;
4239 struct ftrace_ops_hash old_hash_ops;
4240 struct ftrace_hash *old_hash;
4241 struct ftrace_hash *hash;
4244 if (unlikely(ftrace_disabled))
4247 mutex_lock(&ops->func_hash->regex_lock);
4250 orig_hash = &ops->func_hash->filter_hash;
4252 orig_hash = &ops->func_hash->notrace_hash;
4255 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4257 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4261 goto out_regex_unlock;
4264 if (buf && !ftrace_match_records(hash, buf, len)) {
4266 goto out_regex_unlock;
4269 ret = ftrace_match_addr(hash, ip, remove);
4271 goto out_regex_unlock;
4274 mutex_lock(&ftrace_lock);
4275 old_hash = *orig_hash;
4276 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4277 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4278 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4280 ftrace_ops_update_code(ops, &old_hash_ops);
4281 free_ftrace_hash_rcu(old_hash);
4283 mutex_unlock(&ftrace_lock);
4286 mutex_unlock(&ops->func_hash->regex_lock);
4288 free_ftrace_hash(hash);
4293 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4294 int reset, int enable)
4296 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4300 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4301 * @ops - the ops to set the filter with
4302 * @ip - the address to add to or remove from the filter.
4303 * @remove - non zero to remove the ip from the filter
4304 * @reset - non zero to reset all filters before applying this filter.
4306 * Filters denote which functions should be enabled when tracing is enabled
4307 * If @ip is NULL, it failes to update filter.
4309 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4310 int remove, int reset)
4312 ftrace_ops_init(ops);
4313 return ftrace_set_addr(ops, ip, remove, reset, 1);
4315 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4318 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4319 int reset, int enable)
4321 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4325 * ftrace_set_filter - set a function to filter on in ftrace
4326 * @ops - the ops to set the filter with
4327 * @buf - the string that holds the function filter text.
4328 * @len - the length of the string.
4329 * @reset - non zero to reset all filters before applying this filter.
4331 * Filters denote which functions should be enabled when tracing is enabled.
4332 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4334 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4337 ftrace_ops_init(ops);
4338 return ftrace_set_regex(ops, buf, len, reset, 1);
4340 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4343 * ftrace_set_notrace - set a function to not trace in ftrace
4344 * @ops - the ops to set the notrace filter with
4345 * @buf - the string that holds the function notrace text.
4346 * @len - the length of the string.
4347 * @reset - non zero to reset all filters before applying this filter.
4349 * Notrace Filters denote which functions should not be enabled when tracing
4350 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4353 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4356 ftrace_ops_init(ops);
4357 return ftrace_set_regex(ops, buf, len, reset, 0);
4359 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4361 * ftrace_set_global_filter - set a function to filter on with global tracers
4362 * @buf - the string that holds the function filter text.
4363 * @len - the length of the string.
4364 * @reset - non zero to reset all filters before applying this filter.
4366 * Filters denote which functions should be enabled when tracing is enabled.
4367 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4369 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4371 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4373 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4376 * ftrace_set_global_notrace - set a function to not trace with global tracers
4377 * @buf - the string that holds the function notrace text.
4378 * @len - the length of the string.
4379 * @reset - non zero to reset all filters before applying this filter.
4381 * Notrace Filters denote which functions should not be enabled when tracing
4382 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4385 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4387 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4389 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4392 * command line interface to allow users to set filters on boot up.
4394 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4395 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4396 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4398 /* Used by function selftest to not test if filter is set */
4399 bool ftrace_filter_param __initdata;
4401 static int __init set_ftrace_notrace(char *str)
4403 ftrace_filter_param = true;
4404 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4407 __setup("ftrace_notrace=", set_ftrace_notrace);
4409 static int __init set_ftrace_filter(char *str)
4411 ftrace_filter_param = true;
4412 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4415 __setup("ftrace_filter=", set_ftrace_filter);
4417 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4418 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4419 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4420 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
4422 static int __init set_graph_function(char *str)
4424 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4427 __setup("ftrace_graph_filter=", set_graph_function);
4429 static int __init set_graph_notrace_function(char *str)
4431 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4434 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4436 static void __init set_ftrace_early_graph(char *buf, int enable)
4440 unsigned long *table = ftrace_graph_funcs;
4441 int *count = &ftrace_graph_count;
4444 table = ftrace_graph_notrace_funcs;
4445 count = &ftrace_graph_notrace_count;
4449 func = strsep(&buf, ",");
4450 /* we allow only one expression at a time */
4451 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
4453 printk(KERN_DEBUG "ftrace: function %s not "
4454 "traceable\n", func);
4457 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4460 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4464 ftrace_ops_init(ops);
4467 func = strsep(&buf, ",");
4468 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4472 static void __init set_ftrace_early_filters(void)
4474 if (ftrace_filter_buf[0])
4475 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4476 if (ftrace_notrace_buf[0])
4477 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4478 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4479 if (ftrace_graph_buf[0])
4480 set_ftrace_early_graph(ftrace_graph_buf, 1);
4481 if (ftrace_graph_notrace_buf[0])
4482 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4483 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4486 int ftrace_regex_release(struct inode *inode, struct file *file)
4488 struct seq_file *m = (struct seq_file *)file->private_data;
4489 struct ftrace_ops_hash old_hash_ops;
4490 struct ftrace_iterator *iter;
4491 struct ftrace_hash **orig_hash;
4492 struct ftrace_hash *old_hash;
4493 struct trace_parser *parser;
4497 if (file->f_mode & FMODE_READ) {
4499 seq_release(inode, file);
4501 iter = file->private_data;
4503 parser = &iter->parser;
4504 if (trace_parser_loaded(parser)) {
4505 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
4507 parser->buffer[parser->idx] = 0;
4508 ftrace_process_regex(iter->hash, parser->buffer,
4509 parser->idx, enable);
4512 trace_parser_put(parser);
4514 mutex_lock(&iter->ops->func_hash->regex_lock);
4516 if (file->f_mode & FMODE_WRITE) {
4517 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4520 orig_hash = &iter->ops->func_hash->filter_hash;
4522 orig_hash = &iter->ops->func_hash->notrace_hash;
4524 mutex_lock(&ftrace_lock);
4525 old_hash = *orig_hash;
4526 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4527 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
4528 ret = ftrace_hash_move(iter->ops, filter_hash,
4529 orig_hash, iter->hash);
4531 ftrace_ops_update_code(iter->ops, &old_hash_ops);
4532 free_ftrace_hash_rcu(old_hash);
4534 mutex_unlock(&ftrace_lock);
4537 mutex_unlock(&iter->ops->func_hash->regex_lock);
4538 free_ftrace_hash(iter->hash);
4544 static const struct file_operations ftrace_avail_fops = {
4545 .open = ftrace_avail_open,
4547 .llseek = seq_lseek,
4548 .release = seq_release_private,
4551 static const struct file_operations ftrace_enabled_fops = {
4552 .open = ftrace_enabled_open,
4554 .llseek = seq_lseek,
4555 .release = seq_release_private,
4558 static const struct file_operations ftrace_filter_fops = {
4559 .open = ftrace_filter_open,
4561 .write = ftrace_filter_write,
4562 .llseek = tracing_lseek,
4563 .release = ftrace_regex_release,
4566 static const struct file_operations ftrace_notrace_fops = {
4567 .open = ftrace_notrace_open,
4569 .write = ftrace_notrace_write,
4570 .llseek = tracing_lseek,
4571 .release = ftrace_regex_release,
4574 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4576 static DEFINE_MUTEX(graph_lock);
4578 int ftrace_graph_count;
4579 int ftrace_graph_notrace_count;
4580 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4581 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4583 struct ftrace_graph_data {
4584 unsigned long *table;
4587 const struct seq_operations *seq_ops;
4591 __g_next(struct seq_file *m, loff_t *pos)
4593 struct ftrace_graph_data *fgd = m->private;
4595 if (*pos >= *fgd->count)
4597 return &fgd->table[*pos];
4601 g_next(struct seq_file *m, void *v, loff_t *pos)
4604 return __g_next(m, pos);
4607 static void *g_start(struct seq_file *m, loff_t *pos)
4609 struct ftrace_graph_data *fgd = m->private;
4611 mutex_lock(&graph_lock);
4613 /* Nothing, tell g_show to print all functions are enabled */
4614 if (!*fgd->count && !*pos)
4617 return __g_next(m, pos);
4620 static void g_stop(struct seq_file *m, void *p)
4622 mutex_unlock(&graph_lock);
4625 static int g_show(struct seq_file *m, void *v)
4627 unsigned long *ptr = v;
4632 if (ptr == (unsigned long *)1) {
4633 struct ftrace_graph_data *fgd = m->private;
4635 if (fgd->table == ftrace_graph_funcs)
4636 seq_puts(m, "#### all functions enabled ####\n");
4638 seq_puts(m, "#### no functions disabled ####\n");
4642 seq_printf(m, "%ps\n", (void *)*ptr);
4647 static const struct seq_operations ftrace_graph_seq_ops = {
4655 __ftrace_graph_open(struct inode *inode, struct file *file,
4656 struct ftrace_graph_data *fgd)
4660 mutex_lock(&graph_lock);
4661 if ((file->f_mode & FMODE_WRITE) &&
4662 (file->f_flags & O_TRUNC)) {
4664 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4666 mutex_unlock(&graph_lock);
4668 if (file->f_mode & FMODE_READ) {
4669 ret = seq_open(file, fgd->seq_ops);
4671 struct seq_file *m = file->private_data;
4675 file->private_data = fgd;
4681 ftrace_graph_open(struct inode *inode, struct file *file)
4683 struct ftrace_graph_data *fgd;
4685 if (unlikely(ftrace_disabled))
4688 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4692 fgd->table = ftrace_graph_funcs;
4693 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4694 fgd->count = &ftrace_graph_count;
4695 fgd->seq_ops = &ftrace_graph_seq_ops;
4697 return __ftrace_graph_open(inode, file, fgd);
4701 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4703 struct ftrace_graph_data *fgd;
4705 if (unlikely(ftrace_disabled))
4708 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4712 fgd->table = ftrace_graph_notrace_funcs;
4713 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4714 fgd->count = &ftrace_graph_notrace_count;
4715 fgd->seq_ops = &ftrace_graph_seq_ops;
4717 return __ftrace_graph_open(inode, file, fgd);
4721 ftrace_graph_release(struct inode *inode, struct file *file)
4723 if (file->f_mode & FMODE_READ) {
4724 struct seq_file *m = file->private_data;
4727 seq_release(inode, file);
4729 kfree(file->private_data);
4736 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4738 struct ftrace_glob func_g;
4739 struct dyn_ftrace *rec;
4740 struct ftrace_page *pg;
4747 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4748 &func_g.search, ¬);
4749 if (!not && *idx >= size)
4752 func_g.len = strlen(func_g.search);
4754 mutex_lock(&ftrace_lock);
4756 if (unlikely(ftrace_disabled)) {
4757 mutex_unlock(&ftrace_lock);
4761 do_for_each_ftrace_rec(pg, rec) {
4763 if (rec->flags & FTRACE_FL_DISABLED)
4766 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
4767 /* if it is in the array */
4769 for (i = 0; i < *idx; i++) {
4770 if (array[i] == rec->ip) {
4779 array[(*idx)++] = rec->ip;
4785 array[i] = array[--(*idx)];
4791 } while_for_each_ftrace_rec();
4793 mutex_unlock(&ftrace_lock);
4802 ftrace_graph_write(struct file *file, const char __user *ubuf,
4803 size_t cnt, loff_t *ppos)
4805 struct trace_parser parser;
4806 ssize_t read, ret = 0;
4807 struct ftrace_graph_data *fgd = file->private_data;
4812 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4815 read = trace_get_user(&parser, ubuf, cnt, ppos);
4817 if (read >= 0 && trace_parser_loaded((&parser))) {
4818 parser.buffer[parser.idx] = 0;
4820 mutex_lock(&graph_lock);
4822 /* we allow only one expression at a time */
4823 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4826 mutex_unlock(&graph_lock);
4832 trace_parser_put(&parser);
4837 static const struct file_operations ftrace_graph_fops = {
4838 .open = ftrace_graph_open,
4840 .write = ftrace_graph_write,
4841 .llseek = tracing_lseek,
4842 .release = ftrace_graph_release,
4845 static const struct file_operations ftrace_graph_notrace_fops = {
4846 .open = ftrace_graph_notrace_open,
4848 .write = ftrace_graph_write,
4849 .llseek = tracing_lseek,
4850 .release = ftrace_graph_release,
4852 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4854 void ftrace_create_filter_files(struct ftrace_ops *ops,
4855 struct dentry *parent)
4858 trace_create_file("set_ftrace_filter", 0644, parent,
4859 ops, &ftrace_filter_fops);
4861 trace_create_file("set_ftrace_notrace", 0644, parent,
4862 ops, &ftrace_notrace_fops);
4866 * The name "destroy_filter_files" is really a misnomer. Although
4867 * in the future, it may actualy delete the files, but this is
4868 * really intended to make sure the ops passed in are disabled
4869 * and that when this function returns, the caller is free to
4872 * The "destroy" name is only to match the "create" name that this
4873 * should be paired with.
4875 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4877 mutex_lock(&ftrace_lock);
4878 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4879 ftrace_shutdown(ops, 0);
4880 ops->flags |= FTRACE_OPS_FL_DELETED;
4881 ftrace_free_filter(ops);
4882 mutex_unlock(&ftrace_lock);
4885 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
4888 trace_create_file("available_filter_functions", 0444,
4889 d_tracer, NULL, &ftrace_avail_fops);
4891 trace_create_file("enabled_functions", 0444,
4892 d_tracer, NULL, &ftrace_enabled_fops);
4894 ftrace_create_filter_files(&global_ops, d_tracer);
4896 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4897 trace_create_file("set_graph_function", 0444, d_tracer,
4899 &ftrace_graph_fops);
4900 trace_create_file("set_graph_notrace", 0444, d_tracer,
4902 &ftrace_graph_notrace_fops);
4903 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4908 static int ftrace_cmp_ips(const void *a, const void *b)
4910 const unsigned long *ipa = a;
4911 const unsigned long *ipb = b;
4920 static int ftrace_process_locs(struct module *mod,
4921 unsigned long *start,
4924 struct ftrace_page *start_pg;
4925 struct ftrace_page *pg;
4926 struct dyn_ftrace *rec;
4927 unsigned long count;
4930 unsigned long flags = 0; /* Shut up gcc */
4933 count = end - start;
4938 sort(start, count, sizeof(*start),
4939 ftrace_cmp_ips, NULL);
4941 start_pg = ftrace_allocate_pages(count);
4945 mutex_lock(&ftrace_lock);
4948 * Core and each module needs their own pages, as
4949 * modules will free them when they are removed.
4950 * Force a new page to be allocated for modules.
4953 WARN_ON(ftrace_pages || ftrace_pages_start);
4954 /* First initialization */
4955 ftrace_pages = ftrace_pages_start = start_pg;
4960 if (WARN_ON(ftrace_pages->next)) {
4961 /* Hmm, we have free pages? */
4962 while (ftrace_pages->next)
4963 ftrace_pages = ftrace_pages->next;
4966 ftrace_pages->next = start_pg;
4972 addr = ftrace_call_adjust(*p++);
4974 * Some architecture linkers will pad between
4975 * the different mcount_loc sections of different
4976 * object files to satisfy alignments.
4977 * Skip any NULL pointers.
4982 if (pg->index == pg->size) {
4983 /* We should have allocated enough */
4984 if (WARN_ON(!pg->next))
4989 rec = &pg->records[pg->index++];
4993 /* We should have used all pages */
4996 /* Assign the last page to ftrace_pages */
5000 * We only need to disable interrupts on start up
5001 * because we are modifying code that an interrupt
5002 * may execute, and the modification is not atomic.
5003 * But for modules, nothing runs the code we modify
5004 * until we are finished with it, and there's no
5005 * reason to cause large interrupt latencies while we do it.
5008 local_irq_save(flags);
5009 ftrace_update_code(mod, start_pg);
5011 local_irq_restore(flags);
5014 mutex_unlock(&ftrace_lock);
5019 #ifdef CONFIG_MODULES
5021 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5023 static int referenced_filters(struct dyn_ftrace *rec)
5025 struct ftrace_ops *ops;
5028 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5029 if (ops_references_rec(ops, rec)) {
5031 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
5032 rec->flags |= FTRACE_FL_REGS;
5039 void ftrace_release_mod(struct module *mod)
5041 struct dyn_ftrace *rec;
5042 struct ftrace_page **last_pg;
5043 struct ftrace_page *pg;
5046 mutex_lock(&ftrace_lock);
5048 if (ftrace_disabled)
5052 * Each module has its own ftrace_pages, remove
5053 * them from the list.
5055 last_pg = &ftrace_pages_start;
5056 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5057 rec = &pg->records[0];
5058 if (within_module_core(rec->ip, mod)) {
5060 * As core pages are first, the first
5061 * page should never be a module page.
5063 if (WARN_ON(pg == ftrace_pages_start))
5066 /* Check if we are deleting the last page */
5067 if (pg == ftrace_pages)
5068 ftrace_pages = next_to_ftrace_page(last_pg);
5070 *last_pg = pg->next;
5071 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5072 free_pages((unsigned long)pg->records, order);
5075 last_pg = &pg->next;
5078 mutex_unlock(&ftrace_lock);
5081 void ftrace_module_enable(struct module *mod)
5083 struct dyn_ftrace *rec;
5084 struct ftrace_page *pg;
5086 mutex_lock(&ftrace_lock);
5088 if (ftrace_disabled)
5092 * If the tracing is enabled, go ahead and enable the record.
5094 * The reason not to enable the record immediatelly is the
5095 * inherent check of ftrace_make_nop/ftrace_make_call for
5096 * correct previous instructions. Making first the NOP
5097 * conversion puts the module to the correct state, thus
5098 * passing the ftrace_make_call check.
5100 * We also delay this to after the module code already set the
5101 * text to read-only, as we now need to set it back to read-write
5102 * so that we can modify the text.
5104 if (ftrace_start_up)
5105 ftrace_arch_code_modify_prepare();
5107 do_for_each_ftrace_rec(pg, rec) {
5110 * do_for_each_ftrace_rec() is a double loop.
5111 * module text shares the pg. If a record is
5112 * not part of this module, then skip this pg,
5113 * which the "break" will do.
5115 if (!within_module_core(rec->ip, mod))
5121 * When adding a module, we need to check if tracers are
5122 * currently enabled and if they are, and can trace this record,
5123 * we need to enable the module functions as well as update the
5124 * reference counts for those function records.
5126 if (ftrace_start_up)
5127 cnt += referenced_filters(rec);
5129 rec->flags &= ~FTRACE_FL_DISABLED;
5132 if (ftrace_start_up && cnt) {
5133 int failed = __ftrace_replace_code(rec, 1);
5135 ftrace_bug(failed, rec);
5140 } while_for_each_ftrace_rec();
5143 if (ftrace_start_up)
5144 ftrace_arch_code_modify_post_process();
5147 mutex_unlock(&ftrace_lock);
5150 void ftrace_module_init(struct module *mod)
5152 if (ftrace_disabled || !mod->num_ftrace_callsites)
5155 ftrace_process_locs(mod, mod->ftrace_callsites,
5156 mod->ftrace_callsites + mod->num_ftrace_callsites);
5158 #endif /* CONFIG_MODULES */
5160 void __init ftrace_init(void)
5162 extern unsigned long __start_mcount_loc[];
5163 extern unsigned long __stop_mcount_loc[];
5164 unsigned long count, flags;
5167 local_irq_save(flags);
5168 ret = ftrace_dyn_arch_init();
5169 local_irq_restore(flags);
5173 count = __stop_mcount_loc - __start_mcount_loc;
5175 pr_info("ftrace: No functions to be traced?\n");
5179 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5180 count, count / ENTRIES_PER_PAGE + 1);
5182 last_ftrace_enabled = ftrace_enabled = 1;
5184 ret = ftrace_process_locs(NULL,
5188 set_ftrace_early_filters();
5192 ftrace_disabled = 1;
5195 /* Do nothing if arch does not support this */
5196 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5200 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5204 * Currently there's no safe way to free a trampoline when the kernel
5205 * is configured with PREEMPT. That is because a task could be preempted
5206 * when it jumped to the trampoline, it may be preempted for a long time
5207 * depending on the system load, and currently there's no way to know
5208 * when it will be off the trampoline. If the trampoline is freed
5209 * too early, when the task runs again, it will be executing on freed
5212 #ifdef CONFIG_PREEMPT
5213 /* Currently, only non dynamic ops can have a trampoline */
5214 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5218 arch_ftrace_update_trampoline(ops);
5223 static struct ftrace_ops global_ops = {
5224 .func = ftrace_stub,
5225 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5226 FTRACE_OPS_FL_INITIALIZED |
5230 static int __init ftrace_nodyn_init(void)
5235 core_initcall(ftrace_nodyn_init);
5237 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
5238 static inline void ftrace_startup_enable(int command) { }
5239 static inline void ftrace_startup_all(int command) { }
5240 /* Keep as macros so we do not need to define the commands */
5241 # define ftrace_startup(ops, command) \
5243 int ___ret = __register_ftrace_function(ops); \
5245 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5248 # define ftrace_shutdown(ops, command) \
5250 int ___ret = __unregister_ftrace_function(ops); \
5252 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5256 # define ftrace_startup_sysctl() do { } while (0)
5257 # define ftrace_shutdown_sysctl() do { } while (0)
5260 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
5265 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5269 #endif /* CONFIG_DYNAMIC_FTRACE */
5271 __init void ftrace_init_global_array_ops(struct trace_array *tr)
5273 tr->ops = &global_ops;
5274 tr->ops->private = tr;
5277 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5279 /* If we filter on pids, update to use the pid function */
5280 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5281 if (WARN_ON(tr->ops->func != ftrace_stub))
5282 printk("ftrace ops had %pS for function\n",
5285 tr->ops->func = func;
5286 tr->ops->private = tr;
5289 void ftrace_reset_array_ops(struct trace_array *tr)
5291 tr->ops->func = ftrace_stub;
5294 static nokprobe_inline void
5295 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5296 struct ftrace_ops *ignored, struct pt_regs *regs)
5298 struct ftrace_ops *op;
5301 bit = trace_test_and_set_recursion(TRACE_LIST_START);
5306 * Some of the ops may be dynamically allocated,
5307 * they must be freed after a synchronize_sched().
5309 preempt_disable_notrace();
5311 do_for_each_ftrace_op(op, ftrace_ops_list) {
5313 * Check the following for each ops before calling their func:
5314 * if RCU flag is set, then rcu_is_watching() must be true
5315 * if PER_CPU is set, then ftrace_function_local_disable()
5317 * Otherwise test if the ip matches the ops filter
5319 * If any of the above fails then the op->func() is not executed.
5321 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5322 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5323 !ftrace_function_local_disabled(op)) &&
5324 ftrace_ops_test(op, ip, regs)) {
5326 if (FTRACE_WARN_ON(!op->func)) {
5327 pr_warn("op=%p %pS\n", op, op);
5330 op->func(ip, parent_ip, op, regs);
5332 } while_for_each_ftrace_op(op);
5334 preempt_enable_notrace();
5335 trace_clear_recursion(bit);
5339 * Some archs only support passing ip and parent_ip. Even though
5340 * the list function ignores the op parameter, we do not want any
5341 * C side effects, where a function is called without the caller
5342 * sending a third parameter.
5343 * Archs are to support both the regs and ftrace_ops at the same time.
5344 * If they support ftrace_ops, it is assumed they support regs.
5345 * If call backs want to use regs, they must either check for regs
5346 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5347 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
5348 * An architecture can pass partial regs with ftrace_ops and still
5349 * set the ARCH_SUPPORTS_FTRACE_OPS.
5351 #if ARCH_SUPPORTS_FTRACE_OPS
5352 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5353 struct ftrace_ops *op, struct pt_regs *regs)
5355 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
5357 NOKPROBE_SYMBOL(ftrace_ops_list_func);
5359 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5361 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
5363 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
5367 * If there's only one function registered but it does not support
5368 * recursion, needs RCU protection and/or requires per cpu handling, then
5369 * this function will be called by the mcount trampoline.
5371 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
5372 struct ftrace_ops *op, struct pt_regs *regs)
5376 bit = trace_test_and_set_recursion(TRACE_LIST_START);
5380 preempt_disable_notrace();
5382 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5383 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5384 !ftrace_function_local_disabled(op))) {
5385 op->func(ip, parent_ip, op, regs);
5388 preempt_enable_notrace();
5389 trace_clear_recursion(bit);
5391 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
5394 * ftrace_ops_get_func - get the function a trampoline should call
5395 * @ops: the ops to get the function for
5397 * Normally the mcount trampoline will call the ops->func, but there
5398 * are times that it should not. For example, if the ops does not
5399 * have its own recursion protection, then it should call the
5400 * ftrace_ops_recurs_func() instead.
5402 * Returns the function that the trampoline should call for @ops.
5404 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5407 * If the function does not handle recursion, needs to be RCU safe,
5408 * or does per cpu logic, then we need to call the assist handler.
5410 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5411 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5412 return ftrace_ops_assist_func;
5418 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5419 struct task_struct *prev, struct task_struct *next)
5421 struct trace_array *tr = data;
5422 struct trace_pid_list *pid_list;
5424 pid_list = rcu_dereference_sched(tr->function_pids);
5426 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5427 trace_ignore_this_task(pid_list, next));
5430 static void clear_ftrace_pids(struct trace_array *tr)
5432 struct trace_pid_list *pid_list;
5435 pid_list = rcu_dereference_protected(tr->function_pids,
5436 lockdep_is_held(&ftrace_lock));
5440 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5442 for_each_possible_cpu(cpu)
5443 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5445 rcu_assign_pointer(tr->function_pids, NULL);
5447 /* Wait till all users are no longer using pid filtering */
5448 synchronize_sched();
5450 trace_free_pid_list(pid_list);
5453 void ftrace_clear_pids(struct trace_array *tr)
5455 mutex_lock(&ftrace_lock);
5457 clear_ftrace_pids(tr);
5459 mutex_unlock(&ftrace_lock);
5462 static void ftrace_pid_reset(struct trace_array *tr)
5464 mutex_lock(&ftrace_lock);
5465 clear_ftrace_pids(tr);
5467 ftrace_update_pid_func();
5468 ftrace_startup_all(0);
5470 mutex_unlock(&ftrace_lock);
5473 /* Greater than any max PID */
5474 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5476 static void *fpid_start(struct seq_file *m, loff_t *pos)
5479 struct trace_pid_list *pid_list;
5480 struct trace_array *tr = m->private;
5482 mutex_lock(&ftrace_lock);
5483 rcu_read_lock_sched();
5485 pid_list = rcu_dereference_sched(tr->function_pids);
5488 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5490 return trace_pid_start(pid_list, pos);
5493 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5495 struct trace_array *tr = m->private;
5496 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5498 if (v == FTRACE_NO_PIDS) {
5502 return trace_pid_next(pid_list, v, pos);
5505 static void fpid_stop(struct seq_file *m, void *p)
5508 rcu_read_unlock_sched();
5509 mutex_unlock(&ftrace_lock);
5512 static int fpid_show(struct seq_file *m, void *v)
5514 if (v == FTRACE_NO_PIDS) {
5515 seq_puts(m, "no pid\n");
5519 return trace_pid_show(m, v);
5522 static const struct seq_operations ftrace_pid_sops = {
5523 .start = fpid_start,
5530 ftrace_pid_open(struct inode *inode, struct file *file)
5532 struct trace_array *tr = inode->i_private;
5536 if (trace_array_get(tr) < 0)
5539 if ((file->f_mode & FMODE_WRITE) &&
5540 (file->f_flags & O_TRUNC))
5541 ftrace_pid_reset(tr);
5543 ret = seq_open(file, &ftrace_pid_sops);
5545 trace_array_put(tr);
5547 m = file->private_data;
5548 /* copy tr over to seq ops */
5555 static void ignore_task_cpu(void *data)
5557 struct trace_array *tr = data;
5558 struct trace_pid_list *pid_list;
5561 * This function is called by on_each_cpu() while the
5562 * event_mutex is held.
5564 pid_list = rcu_dereference_protected(tr->function_pids,
5565 mutex_is_locked(&ftrace_lock));
5567 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5568 trace_ignore_this_task(pid_list, current));
5572 ftrace_pid_write(struct file *filp, const char __user *ubuf,
5573 size_t cnt, loff_t *ppos)
5575 struct seq_file *m = filp->private_data;
5576 struct trace_array *tr = m->private;
5577 struct trace_pid_list *filtered_pids = NULL;
5578 struct trace_pid_list *pid_list;
5584 mutex_lock(&ftrace_lock);
5586 filtered_pids = rcu_dereference_protected(tr->function_pids,
5587 lockdep_is_held(&ftrace_lock));
5589 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5593 rcu_assign_pointer(tr->function_pids, pid_list);
5595 if (filtered_pids) {
5596 synchronize_sched();
5597 trace_free_pid_list(filtered_pids);
5598 } else if (pid_list) {
5599 /* Register a probe to set whether to ignore the tracing of a task */
5600 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5604 * Ignoring of pids is done at task switch. But we have to
5605 * check for those tasks that are currently running.
5606 * Always do this in case a pid was appended or removed.
5608 on_each_cpu(ignore_task_cpu, tr, 1);
5610 ftrace_update_pid_func();
5611 ftrace_startup_all(0);
5613 mutex_unlock(&ftrace_lock);
5622 ftrace_pid_release(struct inode *inode, struct file *file)
5624 struct trace_array *tr = inode->i_private;
5626 trace_array_put(tr);
5628 return seq_release(inode, file);
5631 static const struct file_operations ftrace_pid_fops = {
5632 .open = ftrace_pid_open,
5633 .write = ftrace_pid_write,
5635 .llseek = tracing_lseek,
5636 .release = ftrace_pid_release,
5639 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
5641 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5642 tr, &ftrace_pid_fops);
5645 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5646 struct dentry *d_tracer)
5648 /* Only the top level directory has the dyn_tracefs and profile */
5649 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5651 ftrace_init_dyn_tracefs(d_tracer);
5652 ftrace_profile_tracefs(d_tracer);
5656 * ftrace_kill - kill ftrace
5658 * This function should be used by panic code. It stops ftrace
5659 * but in a not so nice way. If you need to simply kill ftrace
5660 * from a non-atomic section, use ftrace_kill.
5662 void ftrace_kill(void)
5664 ftrace_disabled = 1;
5666 clear_ftrace_function();
5670 * Test if ftrace is dead or not.
5672 int ftrace_is_dead(void)
5674 return ftrace_disabled;
5678 * register_ftrace_function - register a function for profiling
5679 * @ops - ops structure that holds the function for profiling.
5681 * Register a function to be called by all functions in the
5684 * Note: @ops->func and all the functions it calls must be labeled
5685 * with "notrace", otherwise it will go into a
5688 int register_ftrace_function(struct ftrace_ops *ops)
5692 ftrace_ops_init(ops);
5694 mutex_lock(&ftrace_lock);
5696 ret = ftrace_startup(ops, 0);
5698 mutex_unlock(&ftrace_lock);
5702 EXPORT_SYMBOL_GPL(register_ftrace_function);
5705 * unregister_ftrace_function - unregister a function for profiling.
5706 * @ops - ops structure that holds the function to unregister
5708 * Unregister a function that was added to be called by ftrace profiling.
5710 int unregister_ftrace_function(struct ftrace_ops *ops)
5714 mutex_lock(&ftrace_lock);
5715 ret = ftrace_shutdown(ops, 0);
5716 mutex_unlock(&ftrace_lock);
5720 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5723 ftrace_enable_sysctl(struct ctl_table *table, int write,
5724 void __user *buffer, size_t *lenp,
5729 mutex_lock(&ftrace_lock);
5731 if (unlikely(ftrace_disabled))
5734 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5736 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5739 last_ftrace_enabled = !!ftrace_enabled;
5741 if (ftrace_enabled) {
5743 /* we are starting ftrace again */
5744 if (ftrace_ops_list != &ftrace_list_end)
5745 update_ftrace_function();
5747 ftrace_startup_sysctl();
5750 /* stopping ftrace calls (just send to ftrace_stub) */
5751 ftrace_trace_function = ftrace_stub;
5753 ftrace_shutdown_sysctl();
5757 mutex_unlock(&ftrace_lock);
5761 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5763 static struct ftrace_ops graph_ops = {
5764 .func = ftrace_stub,
5765 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5766 FTRACE_OPS_FL_INITIALIZED |
5769 #ifdef FTRACE_GRAPH_TRAMP_ADDR
5770 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5771 /* trampoline_size is only needed for dynamically allocated tramps */
5773 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5776 void ftrace_graph_sleep_time_control(bool enable)
5778 fgraph_sleep_time = enable;
5781 void ftrace_graph_graph_time_control(bool enable)
5783 fgraph_graph_time = enable;
5786 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5791 /* The callbacks that hook a function */
5792 trace_func_graph_ret_t ftrace_graph_return =
5793 (trace_func_graph_ret_t)ftrace_stub;
5794 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5795 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5797 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5798 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5802 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5803 struct task_struct *g, *t;
5805 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5806 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5807 * sizeof(struct ftrace_ret_stack),
5809 if (!ret_stack_list[i]) {
5817 read_lock(&tasklist_lock);
5818 do_each_thread(g, t) {
5824 if (t->ret_stack == NULL) {
5825 atomic_set(&t->trace_overrun, 0);
5826 t->curr_ret_stack = -1;
5827 /* Make sure the tasks see the -1 first: */
5829 t->ret_stack = ret_stack_list[start++];
5831 } while_each_thread(g, t);
5834 read_unlock(&tasklist_lock);
5836 for (i = start; i < end; i++)
5837 kfree(ret_stack_list[i]);
5842 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
5843 struct task_struct *prev, struct task_struct *next)
5845 unsigned long long timestamp;
5849 * Does the user want to count the time a function was asleep.
5850 * If so, do not update the time stamps.
5852 if (fgraph_sleep_time)
5855 timestamp = trace_clock_local();
5857 prev->ftrace_timestamp = timestamp;
5859 /* only process tasks that we timestamped */
5860 if (!next->ftrace_timestamp)
5864 * Update all the counters in next to make up for the
5865 * time next was sleeping.
5867 timestamp -= next->ftrace_timestamp;
5869 for (index = next->curr_ret_stack; index >= 0; index--)
5870 next->ret_stack[index].calltime += timestamp;
5873 /* Allocate a return stack for each task */
5874 static int start_graph_tracing(void)
5876 struct ftrace_ret_stack **ret_stack_list;
5879 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5880 sizeof(struct ftrace_ret_stack *),
5883 if (!ret_stack_list)
5886 /* The cpu_boot init_task->ret_stack will never be freed */
5887 for_each_online_cpu(cpu) {
5888 if (!idle_task(cpu)->ret_stack)
5889 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5893 ret = alloc_retstack_tasklist(ret_stack_list);
5894 } while (ret == -EAGAIN);
5897 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5899 pr_info("ftrace_graph: Couldn't activate tracepoint"
5900 " probe to kernel_sched_switch\n");
5903 kfree(ret_stack_list);
5908 * Hibernation protection.
5909 * The state of the current task is too much unstable during
5910 * suspend/restore to disk. We want to protect against that.
5913 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5917 case PM_HIBERNATION_PREPARE:
5918 pause_graph_tracing();
5921 case PM_POST_HIBERNATION:
5922 unpause_graph_tracing();
5928 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5930 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5932 return __ftrace_graph_entry(trace);
5936 * The function graph tracer should only trace the functions defined
5937 * by set_ftrace_filter and set_ftrace_notrace. If another function
5938 * tracer ops is registered, the graph tracer requires testing the
5939 * function against the global ops, and not just trace any function
5940 * that any ftrace_ops registered.
5942 static void update_function_graph_func(void)
5944 struct ftrace_ops *op;
5945 bool do_test = false;
5948 * The graph and global ops share the same set of functions
5949 * to test. If any other ops is on the list, then
5950 * the graph tracing needs to test if its the function
5953 do_for_each_ftrace_op(op, ftrace_ops_list) {
5954 if (op != &global_ops && op != &graph_ops &&
5955 op != &ftrace_list_end) {
5957 /* in double loop, break out with goto */
5960 } while_for_each_ftrace_op(op);
5963 ftrace_graph_entry = ftrace_graph_entry_test;
5965 ftrace_graph_entry = __ftrace_graph_entry;
5968 static struct notifier_block ftrace_suspend_notifier = {
5969 .notifier_call = ftrace_suspend_notifier_call,
5972 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5973 trace_func_graph_ent_t entryfunc)
5977 mutex_lock(&ftrace_lock);
5979 /* we currently allow only one tracer registered at a time */
5980 if (ftrace_graph_active) {
5985 register_pm_notifier(&ftrace_suspend_notifier);
5987 ftrace_graph_active++;
5988 ret = start_graph_tracing();
5990 ftrace_graph_active--;
5994 ftrace_graph_return = retfunc;
5997 * Update the indirect function to the entryfunc, and the
5998 * function that gets called to the entry_test first. Then
5999 * call the update fgraph entry function to determine if
6000 * the entryfunc should be called directly or not.
6002 __ftrace_graph_entry = entryfunc;
6003 ftrace_graph_entry = ftrace_graph_entry_test;
6004 update_function_graph_func();
6006 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
6008 mutex_unlock(&ftrace_lock);
6012 void unregister_ftrace_graph(void)
6014 mutex_lock(&ftrace_lock);
6016 if (unlikely(!ftrace_graph_active))
6019 ftrace_graph_active--;
6020 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
6021 ftrace_graph_entry = ftrace_graph_entry_stub;
6022 __ftrace_graph_entry = ftrace_graph_entry_stub;
6023 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
6024 unregister_pm_notifier(&ftrace_suspend_notifier);
6025 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6028 mutex_unlock(&ftrace_lock);
6031 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6034 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6036 atomic_set(&t->trace_overrun, 0);
6037 t->ftrace_timestamp = 0;
6038 /* make curr_ret_stack visible before we add the ret_stack */
6040 t->ret_stack = ret_stack;
6044 * Allocate a return stack for the idle task. May be the first
6045 * time through, or it may be done by CPU hotplug online.
6047 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6049 t->curr_ret_stack = -1;
6051 * The idle task has no parent, it either has its own
6052 * stack or no stack at all.
6055 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6057 if (ftrace_graph_active) {
6058 struct ftrace_ret_stack *ret_stack;
6060 ret_stack = per_cpu(idle_ret_stack, cpu);
6062 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6063 * sizeof(struct ftrace_ret_stack),
6067 per_cpu(idle_ret_stack, cpu) = ret_stack;
6069 graph_init_task(t, ret_stack);
6073 /* Allocate a return stack for newly created task */
6074 void ftrace_graph_init_task(struct task_struct *t)
6076 /* Make sure we do not use the parent ret_stack */
6077 t->ret_stack = NULL;
6078 t->curr_ret_stack = -1;
6080 if (ftrace_graph_active) {
6081 struct ftrace_ret_stack *ret_stack;
6083 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6084 * sizeof(struct ftrace_ret_stack),
6088 graph_init_task(t, ret_stack);
6092 void ftrace_graph_exit_task(struct task_struct *t)
6094 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6096 t->ret_stack = NULL;
6097 /* NULL must become visible to IRQs before we free it: */