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/sched/task.h>
19 #include <linux/kallsyms.h>
20 #include <linux/seq_file.h>
21 #include <linux/suspend.h>
22 #include <linux/tracefs.h>
23 #include <linux/hardirq.h>
24 #include <linux/kthread.h>
25 #include <linux/uaccess.h>
26 #include <linux/bsearch.h>
27 #include <linux/module.h>
28 #include <linux/ftrace.h>
29 #include <linux/sysctl.h>
30 #include <linux/slab.h>
31 #include <linux/ctype.h>
32 #include <linux/sort.h>
33 #include <linux/list.h>
34 #include <linux/hash.h>
35 #include <linux/rcupdate.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
613 do_div(avg, rec->counter);
614 if (tracing_thresh && (avg < tracing_thresh))
618 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
619 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
621 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
624 /* Sample standard deviation (s^2) */
625 if (rec->counter <= 1)
629 * Apply Welford's method:
630 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
632 stddev = rec->counter * rec->time_squared -
633 rec->time * rec->time;
636 * Divide only 1000 for ns^2 -> us^2 conversion.
637 * trace_print_graph_duration will divide 1000 again.
639 do_div(stddev, 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 (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
881 current->ret_stack[index].subtime = 0;
886 static void profile_graph_return(struct ftrace_graph_ret *trace)
888 struct ftrace_profile_stat *stat;
889 unsigned long long calltime;
890 struct ftrace_profile *rec;
893 local_irq_save(flags);
894 stat = this_cpu_ptr(&ftrace_profile_stats);
895 if (!stat->hash || !ftrace_profile_enabled)
898 /* If the calltime was zero'd ignore it */
899 if (!trace->calltime)
902 calltime = trace->rettime - trace->calltime;
904 if (!fgraph_graph_time) {
907 index = trace->depth;
909 /* Append this call time to the parent time to subtract */
911 current->ret_stack[index - 1].subtime += calltime;
913 if (current->ret_stack[index].subtime < calltime)
914 calltime -= current->ret_stack[index].subtime;
919 rec = ftrace_find_profiled_func(stat, trace->func);
921 rec->time += calltime;
922 rec->time_squared += calltime * calltime;
926 local_irq_restore(flags);
929 static int register_ftrace_profiler(void)
931 return register_ftrace_graph(&profile_graph_return,
932 &profile_graph_entry);
935 static void unregister_ftrace_profiler(void)
937 unregister_ftrace_graph();
940 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
941 .func = function_profile_call,
942 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
943 INIT_OPS_HASH(ftrace_profile_ops)
946 static int register_ftrace_profiler(void)
948 return register_ftrace_function(&ftrace_profile_ops);
951 static void unregister_ftrace_profiler(void)
953 unregister_ftrace_function(&ftrace_profile_ops);
955 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
958 ftrace_profile_write(struct file *filp, const char __user *ubuf,
959 size_t cnt, loff_t *ppos)
964 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
970 mutex_lock(&ftrace_profile_lock);
971 if (ftrace_profile_enabled ^ val) {
973 ret = ftrace_profile_init();
979 ret = register_ftrace_profiler();
984 ftrace_profile_enabled = 1;
986 ftrace_profile_enabled = 0;
988 * unregister_ftrace_profiler calls stop_machine
989 * so this acts like an synchronize_sched.
991 unregister_ftrace_profiler();
995 mutex_unlock(&ftrace_profile_lock);
1003 ftrace_profile_read(struct file *filp, char __user *ubuf,
1004 size_t cnt, loff_t *ppos)
1006 char buf[64]; /* big enough to hold a number */
1009 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1010 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1013 static const struct file_operations ftrace_profile_fops = {
1014 .open = tracing_open_generic,
1015 .read = ftrace_profile_read,
1016 .write = ftrace_profile_write,
1017 .llseek = default_llseek,
1020 /* used to initialize the real stat files */
1021 static struct tracer_stat function_stats __initdata = {
1022 .name = "functions",
1023 .stat_start = function_stat_start,
1024 .stat_next = function_stat_next,
1025 .stat_cmp = function_stat_cmp,
1026 .stat_headers = function_stat_headers,
1027 .stat_show = function_stat_show
1030 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1032 struct ftrace_profile_stat *stat;
1033 struct dentry *entry;
1038 for_each_possible_cpu(cpu) {
1039 stat = &per_cpu(ftrace_profile_stats, cpu);
1041 name = kasprintf(GFP_KERNEL, "function%d", cpu);
1044 * The files created are permanent, if something happens
1045 * we still do not free memory.
1048 "Could not allocate stat file for cpu %d\n",
1052 stat->stat = function_stats;
1053 stat->stat.name = name;
1054 ret = register_stat_tracer(&stat->stat);
1057 "Could not register function stat for cpu %d\n",
1064 entry = tracefs_create_file("function_profile_enabled", 0644,
1065 d_tracer, NULL, &ftrace_profile_fops);
1067 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1070 #else /* CONFIG_FUNCTION_PROFILER */
1071 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1074 #endif /* CONFIG_FUNCTION_PROFILER */
1076 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1078 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1079 static int ftrace_graph_active;
1081 # define ftrace_graph_active 0
1084 #ifdef CONFIG_DYNAMIC_FTRACE
1086 static struct ftrace_ops *removed_ops;
1089 * Set when doing a global update, like enabling all recs or disabling them.
1090 * It is not set when just updating a single ftrace_ops.
1092 static bool update_all_ops;
1094 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1095 # error Dynamic ftrace depends on MCOUNT_RECORD
1098 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1100 struct ftrace_func_probe {
1101 struct hlist_node node;
1102 struct ftrace_probe_ops *ops;
1103 unsigned long flags;
1106 struct list_head free_list;
1109 struct ftrace_func_entry {
1110 struct hlist_node hlist;
1115 * We make these constant because no one should touch them,
1116 * but they are used as the default "empty hash", to avoid allocating
1117 * it all the time. These are in a read only section such that if
1118 * anyone does try to modify it, it will cause an exception.
1120 static const struct hlist_head empty_buckets[1];
1121 static const struct ftrace_hash empty_hash = {
1122 .buckets = (struct hlist_head *)empty_buckets,
1124 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1126 static struct ftrace_ops global_ops = {
1127 .func = ftrace_stub,
1128 .local_hash.notrace_hash = EMPTY_HASH,
1129 .local_hash.filter_hash = EMPTY_HASH,
1130 INIT_OPS_HASH(global_ops)
1131 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1132 FTRACE_OPS_FL_INITIALIZED |
1137 * This is used by __kernel_text_address() to return true if the
1138 * address is on a dynamically allocated trampoline that would
1139 * not return true for either core_kernel_text() or
1140 * is_module_text_address().
1142 bool is_ftrace_trampoline(unsigned long addr)
1144 struct ftrace_ops *op;
1148 * Some of the ops may be dynamically allocated,
1149 * they are freed after a synchronize_sched().
1151 preempt_disable_notrace();
1153 do_for_each_ftrace_op(op, ftrace_ops_list) {
1155 * This is to check for dynamically allocated trampolines.
1156 * Trampolines that are in kernel text will have
1157 * core_kernel_text() return true.
1159 if (op->trampoline && op->trampoline_size)
1160 if (addr >= op->trampoline &&
1161 addr < op->trampoline + op->trampoline_size) {
1165 } while_for_each_ftrace_op(op);
1168 preempt_enable_notrace();
1173 struct ftrace_page {
1174 struct ftrace_page *next;
1175 struct dyn_ftrace *records;
1180 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1181 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1183 /* estimate from running different kernels */
1184 #define NR_TO_INIT 10000
1186 static struct ftrace_page *ftrace_pages_start;
1187 static struct ftrace_page *ftrace_pages;
1189 static __always_inline unsigned long
1190 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1192 if (hash->size_bits > 0)
1193 return hash_long(ip, hash->size_bits);
1198 /* Only use this function if ftrace_hash_empty() has already been tested */
1199 static __always_inline struct ftrace_func_entry *
1200 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1203 struct ftrace_func_entry *entry;
1204 struct hlist_head *hhd;
1206 key = ftrace_hash_key(hash, ip);
1207 hhd = &hash->buckets[key];
1209 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1210 if (entry->ip == ip)
1217 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1218 * @hash: The hash to look at
1219 * @ip: The instruction pointer to test
1221 * Search a given @hash to see if a given instruction pointer (@ip)
1224 * Returns the entry that holds the @ip if found. NULL otherwise.
1226 struct ftrace_func_entry *
1227 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1229 if (ftrace_hash_empty(hash))
1232 return __ftrace_lookup_ip(hash, ip);
1235 static void __add_hash_entry(struct ftrace_hash *hash,
1236 struct ftrace_func_entry *entry)
1238 struct hlist_head *hhd;
1241 key = ftrace_hash_key(hash, entry->ip);
1242 hhd = &hash->buckets[key];
1243 hlist_add_head(&entry->hlist, hhd);
1247 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1249 struct ftrace_func_entry *entry;
1251 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1256 __add_hash_entry(hash, entry);
1262 free_hash_entry(struct ftrace_hash *hash,
1263 struct ftrace_func_entry *entry)
1265 hlist_del(&entry->hlist);
1271 remove_hash_entry(struct ftrace_hash *hash,
1272 struct ftrace_func_entry *entry)
1274 hlist_del(&entry->hlist);
1278 static void ftrace_hash_clear(struct ftrace_hash *hash)
1280 struct hlist_head *hhd;
1281 struct hlist_node *tn;
1282 struct ftrace_func_entry *entry;
1283 int size = 1 << hash->size_bits;
1289 for (i = 0; i < size; i++) {
1290 hhd = &hash->buckets[i];
1291 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1292 free_hash_entry(hash, entry);
1294 FTRACE_WARN_ON(hash->count);
1297 static void free_ftrace_hash(struct ftrace_hash *hash)
1299 if (!hash || hash == EMPTY_HASH)
1301 ftrace_hash_clear(hash);
1302 kfree(hash->buckets);
1306 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1308 struct ftrace_hash *hash;
1310 hash = container_of(rcu, struct ftrace_hash, rcu);
1311 free_ftrace_hash(hash);
1314 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1316 if (!hash || hash == EMPTY_HASH)
1318 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1321 void ftrace_free_filter(struct ftrace_ops *ops)
1323 ftrace_ops_init(ops);
1324 free_ftrace_hash(ops->func_hash->filter_hash);
1325 free_ftrace_hash(ops->func_hash->notrace_hash);
1328 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1330 struct ftrace_hash *hash;
1333 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1337 size = 1 << size_bits;
1338 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1340 if (!hash->buckets) {
1345 hash->size_bits = size_bits;
1350 static struct ftrace_hash *
1351 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1353 struct ftrace_func_entry *entry;
1354 struct ftrace_hash *new_hash;
1359 new_hash = alloc_ftrace_hash(size_bits);
1364 if (ftrace_hash_empty(hash))
1367 size = 1 << hash->size_bits;
1368 for (i = 0; i < size; i++) {
1369 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1370 ret = add_hash_entry(new_hash, entry->ip);
1376 FTRACE_WARN_ON(new_hash->count != hash->count);
1381 free_ftrace_hash(new_hash);
1386 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1388 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1390 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1391 struct ftrace_hash *new_hash);
1393 static struct ftrace_hash *
1394 __ftrace_hash_move(struct ftrace_hash *src)
1396 struct ftrace_func_entry *entry;
1397 struct hlist_node *tn;
1398 struct hlist_head *hhd;
1399 struct ftrace_hash *new_hash;
1400 int size = src->count;
1405 * If the new source is empty, just return the empty_hash.
1411 * Make the hash size about 1/2 the # found
1413 for (size /= 2; size; size >>= 1)
1416 /* Don't allocate too much */
1417 if (bits > FTRACE_HASH_MAX_BITS)
1418 bits = FTRACE_HASH_MAX_BITS;
1420 new_hash = alloc_ftrace_hash(bits);
1424 size = 1 << src->size_bits;
1425 for (i = 0; i < size; i++) {
1426 hhd = &src->buckets[i];
1427 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1428 remove_hash_entry(src, entry);
1429 __add_hash_entry(new_hash, entry);
1437 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1438 struct ftrace_hash **dst, struct ftrace_hash *src)
1440 struct ftrace_hash *new_hash;
1443 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1444 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1447 new_hash = __ftrace_hash_move(src);
1451 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1453 /* IPMODIFY should be updated only when filter_hash updating */
1454 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1456 free_ftrace_hash(new_hash);
1462 * Remove the current set, update the hash and add
1465 ftrace_hash_rec_disable_modify(ops, enable);
1467 rcu_assign_pointer(*dst, new_hash);
1469 ftrace_hash_rec_enable_modify(ops, enable);
1474 static bool hash_contains_ip(unsigned long ip,
1475 struct ftrace_ops_hash *hash)
1478 * The function record is a match if it exists in the filter
1479 * hash and not in the notrace hash. Note, an emty hash is
1480 * considered a match for the filter hash, but an empty
1481 * notrace hash is considered not in the notrace hash.
1483 return (ftrace_hash_empty(hash->filter_hash) ||
1484 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1485 (ftrace_hash_empty(hash->notrace_hash) ||
1486 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1490 * Test the hashes for this ops to see if we want to call
1491 * the ops->func or not.
1493 * It's a match if the ip is in the ops->filter_hash or
1494 * the filter_hash does not exist or is empty,
1496 * the ip is not in the ops->notrace_hash.
1498 * This needs to be called with preemption disabled as
1499 * the hashes are freed with call_rcu_sched().
1502 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1504 struct ftrace_ops_hash hash;
1507 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1509 * There's a small race when adding ops that the ftrace handler
1510 * that wants regs, may be called without them. We can not
1511 * allow that handler to be called if regs is NULL.
1513 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1517 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1518 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
1520 if (hash_contains_ip(ip, &hash))
1529 * This is a double for. Do not use 'break' to break out of the loop,
1530 * you must use a goto.
1532 #define do_for_each_ftrace_rec(pg, rec) \
1533 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1535 for (_____i = 0; _____i < pg->index; _____i++) { \
1536 rec = &pg->records[_____i];
1538 #define while_for_each_ftrace_rec() \
1543 static int ftrace_cmp_recs(const void *a, const void *b)
1545 const struct dyn_ftrace *key = a;
1546 const struct dyn_ftrace *rec = b;
1548 if (key->flags < rec->ip)
1550 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1556 * ftrace_location_range - return the first address of a traced location
1557 * if it touches the given ip range
1558 * @start: start of range to search.
1559 * @end: end of range to search (inclusive). @end points to the last byte
1562 * Returns rec->ip if the related ftrace location is a least partly within
1563 * the given address range. That is, the first address of the instruction
1564 * that is either a NOP or call to the function tracer. It checks the ftrace
1565 * internal tables to determine if the address belongs or not.
1567 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1569 struct ftrace_page *pg;
1570 struct dyn_ftrace *rec;
1571 struct dyn_ftrace key;
1574 key.flags = end; /* overload flags, as it is unsigned long */
1576 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1577 if (end < pg->records[0].ip ||
1578 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1580 rec = bsearch(&key, pg->records, pg->index,
1581 sizeof(struct dyn_ftrace),
1591 * ftrace_location - return true if the ip giving is a traced location
1592 * @ip: the instruction pointer to check
1594 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1595 * That is, the instruction that is either a NOP or call to
1596 * the function tracer. It checks the ftrace internal tables to
1597 * determine if the address belongs or not.
1599 unsigned long ftrace_location(unsigned long ip)
1601 return ftrace_location_range(ip, ip);
1605 * ftrace_text_reserved - return true if range contains an ftrace location
1606 * @start: start of range to search
1607 * @end: end of range to search (inclusive). @end points to the last byte to check.
1609 * Returns 1 if @start and @end contains a ftrace location.
1610 * That is, the instruction that is either a NOP or call to
1611 * the function tracer. It checks the ftrace internal tables to
1612 * determine if the address belongs or not.
1614 int ftrace_text_reserved(const void *start, const void *end)
1618 ret = ftrace_location_range((unsigned long)start,
1619 (unsigned long)end);
1624 /* Test if ops registered to this rec needs regs */
1625 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1627 struct ftrace_ops *ops;
1628 bool keep_regs = false;
1630 for (ops = ftrace_ops_list;
1631 ops != &ftrace_list_end; ops = ops->next) {
1632 /* pass rec in as regs to have non-NULL val */
1633 if (ftrace_ops_test(ops, rec->ip, rec)) {
1634 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1644 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1648 struct ftrace_hash *hash;
1649 struct ftrace_hash *other_hash;
1650 struct ftrace_page *pg;
1651 struct dyn_ftrace *rec;
1652 bool update = false;
1656 /* Only update if the ops has been registered */
1657 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1661 * In the filter_hash case:
1662 * If the count is zero, we update all records.
1663 * Otherwise we just update the items in the hash.
1665 * In the notrace_hash case:
1666 * We enable the update in the hash.
1667 * As disabling notrace means enabling the tracing,
1668 * and enabling notrace means disabling, the inc variable
1672 hash = ops->func_hash->filter_hash;
1673 other_hash = ops->func_hash->notrace_hash;
1674 if (ftrace_hash_empty(hash))
1678 hash = ops->func_hash->notrace_hash;
1679 other_hash = ops->func_hash->filter_hash;
1681 * If the notrace hash has no items,
1682 * then there's nothing to do.
1684 if (ftrace_hash_empty(hash))
1688 do_for_each_ftrace_rec(pg, rec) {
1689 int in_other_hash = 0;
1693 if (rec->flags & FTRACE_FL_DISABLED)
1698 * Only the filter_hash affects all records.
1699 * Update if the record is not in the notrace hash.
1701 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1704 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1705 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1708 * If filter_hash is set, we want to match all functions
1709 * that are in the hash but not in the other hash.
1711 * If filter_hash is not set, then we are decrementing.
1712 * That means we match anything that is in the hash
1713 * and also in the other_hash. That is, we need to turn
1714 * off functions in the other hash because they are disabled
1717 if (filter_hash && in_hash && !in_other_hash)
1719 else if (!filter_hash && in_hash &&
1720 (in_other_hash || ftrace_hash_empty(other_hash)))
1728 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1732 * If there's only a single callback registered to a
1733 * function, and the ops has a trampoline registered
1734 * for it, then we can call it directly.
1736 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1737 rec->flags |= FTRACE_FL_TRAMP;
1740 * If we are adding another function callback
1741 * to this function, and the previous had a
1742 * custom trampoline in use, then we need to go
1743 * back to the default trampoline.
1745 rec->flags &= ~FTRACE_FL_TRAMP;
1748 * If any ops wants regs saved for this function
1749 * then all ops will get saved regs.
1751 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1752 rec->flags |= FTRACE_FL_REGS;
1754 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1759 * If the rec had REGS enabled and the ops that is
1760 * being removed had REGS set, then see if there is
1761 * still any ops for this record that wants regs.
1762 * If not, we can stop recording them.
1764 if (ftrace_rec_count(rec) > 0 &&
1765 rec->flags & FTRACE_FL_REGS &&
1766 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1767 if (!test_rec_ops_needs_regs(rec))
1768 rec->flags &= ~FTRACE_FL_REGS;
1772 * If the rec had TRAMP enabled, then it needs to
1773 * be cleared. As TRAMP can only be enabled iff
1774 * there is only a single ops attached to it.
1775 * In otherwords, always disable it on decrementing.
1776 * In the future, we may set it if rec count is
1777 * decremented to one, and the ops that is left
1780 rec->flags &= ~FTRACE_FL_TRAMP;
1783 * flags will be cleared in ftrace_check_record()
1784 * if rec count is zero.
1789 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1790 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1792 /* Shortcut, if we handled all records, we are done. */
1793 if (!all && count == hash->count)
1795 } while_for_each_ftrace_rec();
1800 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1803 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1806 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1809 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1812 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1813 int filter_hash, int inc)
1815 struct ftrace_ops *op;
1817 __ftrace_hash_rec_update(ops, filter_hash, inc);
1819 if (ops->func_hash != &global_ops.local_hash)
1823 * If the ops shares the global_ops hash, then we need to update
1824 * all ops that are enabled and use this hash.
1826 do_for_each_ftrace_op(op, ftrace_ops_list) {
1830 if (op->func_hash == &global_ops.local_hash)
1831 __ftrace_hash_rec_update(op, filter_hash, inc);
1832 } while_for_each_ftrace_op(op);
1835 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1838 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1841 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1844 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1848 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1849 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1850 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1851 * Note that old_hash and new_hash has below meanings
1852 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1853 * - If the hash is EMPTY_HASH, it hits nothing
1854 * - Anything else hits the recs which match the hash entries.
1856 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1857 struct ftrace_hash *old_hash,
1858 struct ftrace_hash *new_hash)
1860 struct ftrace_page *pg;
1861 struct dyn_ftrace *rec, *end = NULL;
1864 /* Only update if the ops has been registered */
1865 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1868 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1872 * Since the IPMODIFY is a very address sensitive action, we do not
1873 * allow ftrace_ops to set all functions to new hash.
1875 if (!new_hash || !old_hash)
1878 /* Update rec->flags */
1879 do_for_each_ftrace_rec(pg, rec) {
1881 if (rec->flags & FTRACE_FL_DISABLED)
1884 /* We need to update only differences of filter_hash */
1885 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1886 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1887 if (in_old == in_new)
1891 /* New entries must ensure no others are using it */
1892 if (rec->flags & FTRACE_FL_IPMODIFY)
1894 rec->flags |= FTRACE_FL_IPMODIFY;
1895 } else /* Removed entry */
1896 rec->flags &= ~FTRACE_FL_IPMODIFY;
1897 } while_for_each_ftrace_rec();
1904 /* Roll back what we did above */
1905 do_for_each_ftrace_rec(pg, rec) {
1907 if (rec->flags & FTRACE_FL_DISABLED)
1913 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1914 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1915 if (in_old == in_new)
1919 rec->flags &= ~FTRACE_FL_IPMODIFY;
1921 rec->flags |= FTRACE_FL_IPMODIFY;
1922 } while_for_each_ftrace_rec();
1928 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1930 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1932 if (ftrace_hash_empty(hash))
1935 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1938 /* Disabling always succeeds */
1939 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1941 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1943 if (ftrace_hash_empty(hash))
1946 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1949 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1950 struct ftrace_hash *new_hash)
1952 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1954 if (ftrace_hash_empty(old_hash))
1957 if (ftrace_hash_empty(new_hash))
1960 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1963 static void print_ip_ins(const char *fmt, const unsigned char *p)
1967 printk(KERN_CONT "%s", fmt);
1969 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1970 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1973 static struct ftrace_ops *
1974 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1975 static struct ftrace_ops *
1976 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
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_next(struct dyn_ftrace *rec,
2212 struct ftrace_ops *op)
2214 unsigned long ip = rec->ip;
2216 while_for_each_ftrace_op(op) {
2218 if (!op->trampoline)
2221 if (hash_contains_ip(ip, op->func_hash))
2228 static struct ftrace_ops *
2229 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2231 struct ftrace_ops *op;
2232 unsigned long ip = rec->ip;
2235 * Need to check removed ops first.
2236 * If they are being removed, and this rec has a tramp,
2237 * and this rec is in the ops list, then it would be the
2238 * one with the tramp.
2241 if (hash_contains_ip(ip, &removed_ops->old_hash))
2246 * Need to find the current trampoline for a rec.
2247 * Now, a trampoline is only attached to a rec if there
2248 * was a single 'ops' attached to it. But this can be called
2249 * when we are adding another op to the rec or removing the
2250 * current one. Thus, if the op is being added, we can
2251 * ignore it because it hasn't attached itself to the rec
2254 * If an ops is being modified (hooking to different functions)
2255 * then we don't care about the new functions that are being
2256 * added, just the old ones (that are probably being removed).
2258 * If we are adding an ops to a function that already is using
2259 * a trampoline, it needs to be removed (trampolines are only
2260 * for single ops connected), then an ops that is not being
2261 * modified also needs to be checked.
2263 do_for_each_ftrace_op(op, ftrace_ops_list) {
2265 if (!op->trampoline)
2269 * If the ops is being added, it hasn't gotten to
2270 * the point to be removed from this tree yet.
2272 if (op->flags & FTRACE_OPS_FL_ADDING)
2277 * If the ops is being modified and is in the old
2278 * hash, then it is probably being removed from this
2281 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2282 hash_contains_ip(ip, &op->old_hash))
2285 * If the ops is not being added or modified, and it's
2286 * in its normal filter hash, then this must be the one
2289 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2290 hash_contains_ip(ip, op->func_hash))
2293 } while_for_each_ftrace_op(op);
2298 static struct ftrace_ops *
2299 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2301 struct ftrace_ops *op;
2302 unsigned long ip = rec->ip;
2304 do_for_each_ftrace_op(op, ftrace_ops_list) {
2305 /* pass rec in as regs to have non-NULL val */
2306 if (hash_contains_ip(ip, op->func_hash))
2308 } while_for_each_ftrace_op(op);
2314 * ftrace_get_addr_new - Get the call address to set to
2315 * @rec: The ftrace record descriptor
2317 * If the record has the FTRACE_FL_REGS set, that means that it
2318 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2319 * is not not set, then it wants to convert to the normal callback.
2321 * Returns the address of the trampoline to set to
2323 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2325 struct ftrace_ops *ops;
2327 /* Trampolines take precedence over regs */
2328 if (rec->flags & FTRACE_FL_TRAMP) {
2329 ops = ftrace_find_tramp_ops_new(rec);
2330 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2331 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2332 (void *)rec->ip, (void *)rec->ip, rec->flags);
2333 /* Ftrace is shutting down, return anything */
2334 return (unsigned long)FTRACE_ADDR;
2336 return ops->trampoline;
2339 if (rec->flags & FTRACE_FL_REGS)
2340 return (unsigned long)FTRACE_REGS_ADDR;
2342 return (unsigned long)FTRACE_ADDR;
2346 * ftrace_get_addr_curr - Get the call address that is already there
2347 * @rec: The ftrace record descriptor
2349 * The FTRACE_FL_REGS_EN is set when the record already points to
2350 * a function that saves all the regs. Basically the '_EN' version
2351 * represents the current state of the function.
2353 * Returns the address of the trampoline that is currently being called
2355 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2357 struct ftrace_ops *ops;
2359 /* Trampolines take precedence over regs */
2360 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2361 ops = ftrace_find_tramp_ops_curr(rec);
2362 if (FTRACE_WARN_ON(!ops)) {
2363 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2364 (void *)rec->ip, (void *)rec->ip);
2365 /* Ftrace is shutting down, return anything */
2366 return (unsigned long)FTRACE_ADDR;
2368 return ops->trampoline;
2371 if (rec->flags & FTRACE_FL_REGS_EN)
2372 return (unsigned long)FTRACE_REGS_ADDR;
2374 return (unsigned long)FTRACE_ADDR;
2378 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2380 unsigned long ftrace_old_addr;
2381 unsigned long ftrace_addr;
2384 ftrace_addr = ftrace_get_addr_new(rec);
2386 /* This needs to be done before we call ftrace_update_record */
2387 ftrace_old_addr = ftrace_get_addr_curr(rec);
2389 ret = ftrace_update_record(rec, enable);
2391 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2394 case FTRACE_UPDATE_IGNORE:
2397 case FTRACE_UPDATE_MAKE_CALL:
2398 ftrace_bug_type = FTRACE_BUG_CALL;
2399 return ftrace_make_call(rec, ftrace_addr);
2401 case FTRACE_UPDATE_MAKE_NOP:
2402 ftrace_bug_type = FTRACE_BUG_NOP;
2403 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2405 case FTRACE_UPDATE_MODIFY_CALL:
2406 ftrace_bug_type = FTRACE_BUG_UPDATE;
2407 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2410 return -1; /* unknow ftrace bug */
2413 void __weak ftrace_replace_code(int enable)
2415 struct dyn_ftrace *rec;
2416 struct ftrace_page *pg;
2419 if (unlikely(ftrace_disabled))
2422 do_for_each_ftrace_rec(pg, rec) {
2424 if (rec->flags & FTRACE_FL_DISABLED)
2427 failed = __ftrace_replace_code(rec, enable);
2429 ftrace_bug(failed, rec);
2430 /* Stop processing */
2433 } while_for_each_ftrace_rec();
2436 struct ftrace_rec_iter {
2437 struct ftrace_page *pg;
2442 * ftrace_rec_iter_start, start up iterating over traced functions
2444 * Returns an iterator handle that is used to iterate over all
2445 * the records that represent address locations where functions
2448 * May return NULL if no records are available.
2450 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2453 * We only use a single iterator.
2454 * Protected by the ftrace_lock mutex.
2456 static struct ftrace_rec_iter ftrace_rec_iter;
2457 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2459 iter->pg = ftrace_pages_start;
2462 /* Could have empty pages */
2463 while (iter->pg && !iter->pg->index)
2464 iter->pg = iter->pg->next;
2473 * ftrace_rec_iter_next, get the next record to process.
2474 * @iter: The handle to the iterator.
2476 * Returns the next iterator after the given iterator @iter.
2478 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2482 if (iter->index >= iter->pg->index) {
2483 iter->pg = iter->pg->next;
2486 /* Could have empty pages */
2487 while (iter->pg && !iter->pg->index)
2488 iter->pg = iter->pg->next;
2498 * ftrace_rec_iter_record, get the record at the iterator location
2499 * @iter: The current iterator location
2501 * Returns the record that the current @iter is at.
2503 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2505 return &iter->pg->records[iter->index];
2509 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2513 if (unlikely(ftrace_disabled))
2516 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2518 ftrace_bug_type = FTRACE_BUG_INIT;
2519 ftrace_bug(ret, rec);
2526 * archs can override this function if they must do something
2527 * before the modifying code is performed.
2529 int __weak ftrace_arch_code_modify_prepare(void)
2535 * archs can override this function if they must do something
2536 * after the modifying code is performed.
2538 int __weak ftrace_arch_code_modify_post_process(void)
2543 void ftrace_modify_all_code(int command)
2545 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2549 * If the ftrace_caller calls a ftrace_ops func directly,
2550 * we need to make sure that it only traces functions it
2551 * expects to trace. When doing the switch of functions,
2552 * we need to update to the ftrace_ops_list_func first
2553 * before the transition between old and new calls are set,
2554 * as the ftrace_ops_list_func will check the ops hashes
2555 * to make sure the ops are having the right functions
2559 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2560 if (FTRACE_WARN_ON(err))
2564 if (command & FTRACE_UPDATE_CALLS)
2565 ftrace_replace_code(1);
2566 else if (command & FTRACE_DISABLE_CALLS)
2567 ftrace_replace_code(0);
2569 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2570 function_trace_op = set_function_trace_op;
2572 /* If irqs are disabled, we are in stop machine */
2573 if (!irqs_disabled())
2574 smp_call_function(ftrace_sync_ipi, NULL, 1);
2575 err = ftrace_update_ftrace_func(ftrace_trace_function);
2576 if (FTRACE_WARN_ON(err))
2580 if (command & FTRACE_START_FUNC_RET)
2581 err = ftrace_enable_ftrace_graph_caller();
2582 else if (command & FTRACE_STOP_FUNC_RET)
2583 err = ftrace_disable_ftrace_graph_caller();
2584 FTRACE_WARN_ON(err);
2587 static int __ftrace_modify_code(void *data)
2589 int *command = data;
2591 ftrace_modify_all_code(*command);
2597 * ftrace_run_stop_machine, go back to the stop machine method
2598 * @command: The command to tell ftrace what to do
2600 * If an arch needs to fall back to the stop machine method, the
2601 * it can call this function.
2603 void ftrace_run_stop_machine(int command)
2605 stop_machine(__ftrace_modify_code, &command, NULL);
2609 * arch_ftrace_update_code, modify the code to trace or not trace
2610 * @command: The command that needs to be done
2612 * Archs can override this function if it does not need to
2613 * run stop_machine() to modify code.
2615 void __weak arch_ftrace_update_code(int command)
2617 ftrace_run_stop_machine(command);
2620 static void ftrace_run_update_code(int command)
2624 ret = ftrace_arch_code_modify_prepare();
2625 FTRACE_WARN_ON(ret);
2630 * By default we use stop_machine() to modify the code.
2631 * But archs can do what ever they want as long as it
2632 * is safe. The stop_machine() is the safest, but also
2633 * produces the most overhead.
2635 arch_ftrace_update_code(command);
2637 ret = ftrace_arch_code_modify_post_process();
2638 FTRACE_WARN_ON(ret);
2641 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2642 struct ftrace_ops_hash *old_hash)
2644 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2645 ops->old_hash.filter_hash = old_hash->filter_hash;
2646 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2647 ftrace_run_update_code(command);
2648 ops->old_hash.filter_hash = NULL;
2649 ops->old_hash.notrace_hash = NULL;
2650 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2653 static ftrace_func_t saved_ftrace_func;
2654 static int ftrace_start_up;
2656 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2660 static void per_cpu_ops_free(struct ftrace_ops *ops)
2662 free_percpu(ops->disabled);
2665 static void ftrace_startup_enable(int command)
2667 if (saved_ftrace_func != ftrace_trace_function) {
2668 saved_ftrace_func = ftrace_trace_function;
2669 command |= FTRACE_UPDATE_TRACE_FUNC;
2672 if (!command || !ftrace_enabled)
2675 ftrace_run_update_code(command);
2678 static void ftrace_startup_all(int command)
2680 update_all_ops = true;
2681 ftrace_startup_enable(command);
2682 update_all_ops = false;
2685 static int ftrace_startup(struct ftrace_ops *ops, int command)
2689 if (unlikely(ftrace_disabled))
2692 ret = __register_ftrace_function(ops);
2699 * Note that ftrace probes uses this to start up
2700 * and modify functions it will probe. But we still
2701 * set the ADDING flag for modification, as probes
2702 * do not have trampolines. If they add them in the
2703 * future, then the probes will need to distinguish
2704 * between adding and updating probes.
2706 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2708 ret = ftrace_hash_ipmodify_enable(ops);
2710 /* Rollback registration process */
2711 __unregister_ftrace_function(ops);
2713 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2717 if (ftrace_hash_rec_enable(ops, 1))
2718 command |= FTRACE_UPDATE_CALLS;
2720 ftrace_startup_enable(command);
2722 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2727 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2731 if (unlikely(ftrace_disabled))
2734 ret = __unregister_ftrace_function(ops);
2740 * Just warn in case of unbalance, no need to kill ftrace, it's not
2741 * critical but the ftrace_call callers may be never nopped again after
2742 * further ftrace uses.
2744 WARN_ON_ONCE(ftrace_start_up < 0);
2746 /* Disabling ipmodify never fails */
2747 ftrace_hash_ipmodify_disable(ops);
2749 if (ftrace_hash_rec_disable(ops, 1))
2750 command |= FTRACE_UPDATE_CALLS;
2752 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2754 if (saved_ftrace_func != ftrace_trace_function) {
2755 saved_ftrace_func = ftrace_trace_function;
2756 command |= FTRACE_UPDATE_TRACE_FUNC;
2759 if (!command || !ftrace_enabled) {
2761 * If these are per_cpu ops, they still need their
2762 * per_cpu field freed. Since, function tracing is
2763 * not currently active, we can just free them
2764 * without synchronizing all CPUs.
2766 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2767 per_cpu_ops_free(ops);
2772 * If the ops uses a trampoline, then it needs to be
2773 * tested first on update.
2775 ops->flags |= FTRACE_OPS_FL_REMOVING;
2778 /* The trampoline logic checks the old hashes */
2779 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2780 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2782 ftrace_run_update_code(command);
2785 * If there's no more ops registered with ftrace, run a
2786 * sanity check to make sure all rec flags are cleared.
2788 if (ftrace_ops_list == &ftrace_list_end) {
2789 struct ftrace_page *pg;
2790 struct dyn_ftrace *rec;
2792 do_for_each_ftrace_rec(pg, rec) {
2793 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2794 pr_warn(" %pS flags:%lx\n",
2795 (void *)rec->ip, rec->flags);
2796 } while_for_each_ftrace_rec();
2799 ops->old_hash.filter_hash = NULL;
2800 ops->old_hash.notrace_hash = NULL;
2803 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2806 * Dynamic ops may be freed, we must make sure that all
2807 * callers are done before leaving this function.
2808 * The same goes for freeing the per_cpu data of the per_cpu
2811 * Again, normal synchronize_sched() is not good enough.
2812 * We need to do a hard force of sched synchronization.
2813 * This is because we use preempt_disable() to do RCU, but
2814 * the function tracers can be called where RCU is not watching
2815 * (like before user_exit()). We can not rely on the RCU
2816 * infrastructure to do the synchronization, thus we must do it
2819 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
2820 schedule_on_each_cpu(ftrace_sync);
2822 arch_ftrace_trampoline_free(ops);
2824 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2825 per_cpu_ops_free(ops);
2831 static void ftrace_startup_sysctl(void)
2835 if (unlikely(ftrace_disabled))
2838 /* Force update next time */
2839 saved_ftrace_func = NULL;
2840 /* ftrace_start_up is true if we want ftrace running */
2841 if (ftrace_start_up) {
2842 command = FTRACE_UPDATE_CALLS;
2843 if (ftrace_graph_active)
2844 command |= FTRACE_START_FUNC_RET;
2845 ftrace_startup_enable(command);
2849 static void ftrace_shutdown_sysctl(void)
2853 if (unlikely(ftrace_disabled))
2856 /* ftrace_start_up is true if ftrace is running */
2857 if (ftrace_start_up) {
2858 command = FTRACE_DISABLE_CALLS;
2859 if (ftrace_graph_active)
2860 command |= FTRACE_STOP_FUNC_RET;
2861 ftrace_run_update_code(command);
2865 static u64 ftrace_update_time;
2866 unsigned long ftrace_update_tot_cnt;
2868 static inline int ops_traces_mod(struct ftrace_ops *ops)
2871 * Filter_hash being empty will default to trace module.
2872 * But notrace hash requires a test of individual module functions.
2874 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2875 ftrace_hash_empty(ops->func_hash->notrace_hash);
2879 * Check if the current ops references the record.
2881 * If the ops traces all functions, then it was already accounted for.
2882 * If the ops does not trace the current record function, skip it.
2883 * If the ops ignores the function via notrace filter, skip it.
2886 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2888 /* If ops isn't enabled, ignore it */
2889 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2892 /* If ops traces all then it includes this function */
2893 if (ops_traces_mod(ops))
2896 /* The function must be in the filter */
2897 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2898 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2901 /* If in notrace hash, we ignore it too */
2902 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2908 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2910 struct ftrace_page *pg;
2911 struct dyn_ftrace *p;
2913 unsigned long update_cnt = 0;
2914 unsigned long rec_flags = 0;
2917 start = ftrace_now(raw_smp_processor_id());
2920 * When a module is loaded, this function is called to convert
2921 * the calls to mcount in its text to nops, and also to create
2922 * an entry in the ftrace data. Now, if ftrace is activated
2923 * after this call, but before the module sets its text to
2924 * read-only, the modification of enabling ftrace can fail if
2925 * the read-only is done while ftrace is converting the calls.
2926 * To prevent this, the module's records are set as disabled
2927 * and will be enabled after the call to set the module's text
2931 rec_flags |= FTRACE_FL_DISABLED;
2933 for (pg = new_pgs; pg; pg = pg->next) {
2935 for (i = 0; i < pg->index; i++) {
2937 /* If something went wrong, bail without enabling anything */
2938 if (unlikely(ftrace_disabled))
2941 p = &pg->records[i];
2942 p->flags = rec_flags;
2945 * Do the initial record conversion from mcount jump
2946 * to the NOP instructions.
2948 if (!ftrace_code_disable(mod, p))
2955 stop = ftrace_now(raw_smp_processor_id());
2956 ftrace_update_time = stop - start;
2957 ftrace_update_tot_cnt += update_cnt;
2962 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2967 if (WARN_ON(!count))
2970 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2973 * We want to fill as much as possible. No more than a page
2976 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2980 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2983 /* if we can't allocate this size, try something smaller */
2990 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2999 static struct ftrace_page *
3000 ftrace_allocate_pages(unsigned long num_to_init)
3002 struct ftrace_page *start_pg;
3003 struct ftrace_page *pg;
3010 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3015 * Try to allocate as much as possible in one continues
3016 * location that fills in all of the space. We want to
3017 * waste as little space as possible.
3020 cnt = ftrace_allocate_records(pg, num_to_init);
3028 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3040 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3041 free_pages((unsigned long)pg->records, order);
3042 start_pg = pg->next;
3046 pr_info("ftrace: FAILED to allocate memory for functions\n");
3050 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3052 struct ftrace_iterator {
3055 struct ftrace_page *pg;
3056 struct dyn_ftrace *func;
3057 struct ftrace_func_probe *probe;
3058 struct trace_parser parser;
3059 struct ftrace_hash *hash;
3060 struct ftrace_ops *ops;
3067 t_hash_next(struct seq_file *m, loff_t *pos)
3069 struct ftrace_iterator *iter = m->private;
3070 struct hlist_node *hnd = NULL;
3071 struct hlist_head *hhd;
3077 hnd = &iter->probe->node;
3079 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3082 hhd = &ftrace_func_hash[iter->hidx];
3084 if (hlist_empty(hhd)) {
3100 if (WARN_ON_ONCE(!hnd))
3103 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3108 static void *t_hash_start(struct seq_file *m, loff_t *pos)
3110 struct ftrace_iterator *iter = m->private;
3114 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3117 if (iter->func_pos > *pos)
3121 for (l = 0; l <= (*pos - iter->func_pos); ) {
3122 p = t_hash_next(m, &l);
3129 /* Only set this if we have an item */
3130 iter->flags |= FTRACE_ITER_HASH;
3136 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
3138 struct ftrace_func_probe *rec;
3141 if (WARN_ON_ONCE(!rec))
3144 if (rec->ops->print)
3145 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3147 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
3150 seq_printf(m, ":%p", rec->data);
3157 t_next(struct seq_file *m, void *v, loff_t *pos)
3159 struct ftrace_iterator *iter = m->private;
3160 struct ftrace_ops *ops = iter->ops;
3161 struct dyn_ftrace *rec = NULL;
3163 if (unlikely(ftrace_disabled))
3166 if (iter->flags & FTRACE_ITER_HASH)
3167 return t_hash_next(m, pos);
3170 iter->pos = iter->func_pos = *pos;
3172 if (iter->flags & FTRACE_ITER_PRINTALL)
3173 return t_hash_start(m, pos);
3176 if (iter->idx >= iter->pg->index) {
3177 if (iter->pg->next) {
3178 iter->pg = iter->pg->next;
3183 rec = &iter->pg->records[iter->idx++];
3184 if (((iter->flags & FTRACE_ITER_FILTER) &&
3185 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
3187 ((iter->flags & FTRACE_ITER_NOTRACE) &&
3188 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
3190 ((iter->flags & FTRACE_ITER_ENABLED) &&
3191 !(rec->flags & FTRACE_FL_ENABLED))) {
3199 return t_hash_start(m, pos);
3206 static void reset_iter_read(struct ftrace_iterator *iter)
3210 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
3213 static void *t_start(struct seq_file *m, loff_t *pos)
3215 struct ftrace_iterator *iter = m->private;
3216 struct ftrace_ops *ops = iter->ops;
3220 mutex_lock(&ftrace_lock);
3222 if (unlikely(ftrace_disabled))
3226 * If an lseek was done, then reset and start from beginning.
3228 if (*pos < iter->pos)
3229 reset_iter_read(iter);
3232 * For set_ftrace_filter reading, if we have the filter
3233 * off, we can short cut and just print out that all
3234 * functions are enabled.
3236 if ((iter->flags & FTRACE_ITER_FILTER &&
3237 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
3238 (iter->flags & FTRACE_ITER_NOTRACE &&
3239 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
3241 return t_hash_start(m, pos);
3242 iter->flags |= FTRACE_ITER_PRINTALL;
3243 /* reset in case of seek/pread */
3244 iter->flags &= ~FTRACE_ITER_HASH;
3248 if (iter->flags & FTRACE_ITER_HASH)
3249 return t_hash_start(m, pos);
3252 * Unfortunately, we need to restart at ftrace_pages_start
3253 * every time we let go of the ftrace_mutex. This is because
3254 * those pointers can change without the lock.
3256 iter->pg = ftrace_pages_start;
3258 for (l = 0; l <= *pos; ) {
3259 p = t_next(m, p, &l);
3265 return t_hash_start(m, pos);
3270 static void t_stop(struct seq_file *m, void *p)
3272 mutex_unlock(&ftrace_lock);
3276 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3281 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3282 struct dyn_ftrace *rec)
3286 ptr = arch_ftrace_trampoline_func(ops, rec);
3288 seq_printf(m, " ->%pS", ptr);
3291 static int t_show(struct seq_file *m, void *v)
3293 struct ftrace_iterator *iter = m->private;
3294 struct dyn_ftrace *rec;
3296 if (iter->flags & FTRACE_ITER_HASH)
3297 return t_hash_show(m, iter);
3299 if (iter->flags & FTRACE_ITER_PRINTALL) {
3300 if (iter->flags & FTRACE_ITER_NOTRACE)
3301 seq_puts(m, "#### no functions disabled ####\n");
3303 seq_puts(m, "#### all functions enabled ####\n");
3312 seq_printf(m, "%ps", (void *)rec->ip);
3313 if (iter->flags & FTRACE_ITER_ENABLED) {
3314 struct ftrace_ops *ops;
3316 seq_printf(m, " (%ld)%s%s",
3317 ftrace_rec_count(rec),
3318 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3319 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3320 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3321 ops = ftrace_find_tramp_ops_any(rec);
3324 seq_printf(m, "\ttramp: %pS (%pS)",
3325 (void *)ops->trampoline,
3327 add_trampoline_func(m, ops, rec);
3328 ops = ftrace_find_tramp_ops_next(rec, ops);
3331 seq_puts(m, "\ttramp: ERROR!");
3333 add_trampoline_func(m, NULL, rec);
3342 static const struct seq_operations show_ftrace_seq_ops = {
3350 ftrace_avail_open(struct inode *inode, struct file *file)
3352 struct ftrace_iterator *iter;
3354 if (unlikely(ftrace_disabled))
3357 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3359 iter->pg = ftrace_pages_start;
3360 iter->ops = &global_ops;
3363 return iter ? 0 : -ENOMEM;
3367 ftrace_enabled_open(struct inode *inode, struct file *file)
3369 struct ftrace_iterator *iter;
3371 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3373 iter->pg = ftrace_pages_start;
3374 iter->flags = FTRACE_ITER_ENABLED;
3375 iter->ops = &global_ops;
3378 return iter ? 0 : -ENOMEM;
3382 * ftrace_regex_open - initialize function tracer filter files
3383 * @ops: The ftrace_ops that hold the hash filters
3384 * @flag: The type of filter to process
3385 * @inode: The inode, usually passed in to your open routine
3386 * @file: The file, usually passed in to your open routine
3388 * ftrace_regex_open() initializes the filter files for the
3389 * @ops. Depending on @flag it may process the filter hash or
3390 * the notrace hash of @ops. With this called from the open
3391 * routine, you can use ftrace_filter_write() for the write
3392 * routine if @flag has FTRACE_ITER_FILTER set, or
3393 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3394 * tracing_lseek() should be used as the lseek routine, and
3395 * release must call ftrace_regex_release().
3398 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3399 struct inode *inode, struct file *file)
3401 struct ftrace_iterator *iter;
3402 struct ftrace_hash *hash;
3405 ftrace_ops_init(ops);
3407 if (unlikely(ftrace_disabled))
3410 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3414 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3422 mutex_lock(&ops->func_hash->regex_lock);
3424 if (flag & FTRACE_ITER_NOTRACE)
3425 hash = ops->func_hash->notrace_hash;
3427 hash = ops->func_hash->filter_hash;
3429 if (file->f_mode & FMODE_WRITE) {
3430 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3432 if (file->f_flags & O_TRUNC)
3433 iter->hash = alloc_ftrace_hash(size_bits);
3435 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3438 trace_parser_put(&iter->parser);
3445 if (file->f_mode & FMODE_READ) {
3446 iter->pg = ftrace_pages_start;
3448 ret = seq_open(file, &show_ftrace_seq_ops);
3450 struct seq_file *m = file->private_data;
3454 free_ftrace_hash(iter->hash);
3455 trace_parser_put(&iter->parser);
3459 file->private_data = iter;
3462 mutex_unlock(&ops->func_hash->regex_lock);
3468 ftrace_filter_open(struct inode *inode, struct file *file)
3470 struct ftrace_ops *ops = inode->i_private;
3472 return ftrace_regex_open(ops,
3473 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3478 ftrace_notrace_open(struct inode *inode, struct file *file)
3480 struct ftrace_ops *ops = inode->i_private;
3482 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3486 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3487 struct ftrace_glob {
3494 * If symbols in an architecture don't correspond exactly to the user-visible
3495 * name of what they represent, it is possible to define this function to
3496 * perform the necessary adjustments.
3498 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3503 static int ftrace_match(char *str, struct ftrace_glob *g)
3508 str = arch_ftrace_match_adjust(str, g->search);
3512 if (strcmp(str, g->search) == 0)
3515 case MATCH_FRONT_ONLY:
3516 if (strncmp(str, g->search, g->len) == 0)
3519 case MATCH_MIDDLE_ONLY:
3520 if (strstr(str, g->search))
3523 case MATCH_END_ONLY:
3525 if (slen >= g->len &&
3526 memcmp(str + slen - g->len, g->search, g->len) == 0)
3530 if (glob_match(g->search, str))
3539 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3541 struct ftrace_func_entry *entry;
3544 entry = ftrace_lookup_ip(hash, rec->ip);
3546 /* Do nothing if it doesn't exist */
3550 free_hash_entry(hash, entry);
3552 /* Do nothing if it exists */
3556 ret = add_hash_entry(hash, rec->ip);
3562 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3563 struct ftrace_glob *mod_g, int exclude_mod)
3565 char str[KSYM_SYMBOL_LEN];
3568 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3571 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3573 /* blank module name to match all modules */
3575 /* blank module globbing: modname xor exclude_mod */
3576 if ((!exclude_mod) != (!modname))
3581 /* not matching the module */
3582 if (!modname || !mod_matches) {
3589 if (mod_matches && exclude_mod)
3593 /* blank search means to match all funcs in the mod */
3598 return ftrace_match(str, func_g);
3602 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3604 struct ftrace_page *pg;
3605 struct dyn_ftrace *rec;
3606 struct ftrace_glob func_g = { .type = MATCH_FULL };
3607 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3608 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3609 int exclude_mod = 0;
3615 func_g.type = filter_parse_regex(func, len, &func_g.search,
3617 func_g.len = strlen(func_g.search);
3621 mod_g.type = filter_parse_regex(mod, strlen(mod),
3622 &mod_g.search, &exclude_mod);
3623 mod_g.len = strlen(mod_g.search);
3626 mutex_lock(&ftrace_lock);
3628 if (unlikely(ftrace_disabled))
3631 do_for_each_ftrace_rec(pg, rec) {
3633 if (rec->flags & FTRACE_FL_DISABLED)
3636 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3637 ret = enter_record(hash, rec, clear_filter);
3644 } while_for_each_ftrace_rec();
3646 mutex_unlock(&ftrace_lock);
3652 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3654 return match_records(hash, buff, len, NULL);
3659 * We register the module command as a template to show others how
3660 * to register the a command as well.
3664 ftrace_mod_callback(struct ftrace_hash *hash,
3665 char *func, char *cmd, char *module, int enable)
3670 * cmd == 'mod' because we only registered this func
3671 * for the 'mod' ftrace_func_command.
3672 * But if you register one func with multiple commands,
3673 * you can tell which command was used by the cmd
3676 ret = match_records(hash, func, strlen(func), module);
3684 static struct ftrace_func_command ftrace_mod_cmd = {
3686 .func = ftrace_mod_callback,
3689 static int __init ftrace_mod_cmd_init(void)
3691 return register_ftrace_command(&ftrace_mod_cmd);
3693 core_initcall(ftrace_mod_cmd_init);
3695 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3696 struct ftrace_ops *op, struct pt_regs *pt_regs)
3698 struct ftrace_func_probe *entry;
3699 struct hlist_head *hhd;
3702 key = hash_long(ip, FTRACE_HASH_BITS);
3704 hhd = &ftrace_func_hash[key];
3706 if (hlist_empty(hhd))
3710 * Disable preemption for these calls to prevent a RCU grace
3711 * period. This syncs the hash iteration and freeing of items
3712 * on the hash. rcu_read_lock is too dangerous here.
3714 preempt_disable_notrace();
3715 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3716 if (entry->ip == ip)
3717 entry->ops->func(ip, parent_ip, &entry->data);
3719 preempt_enable_notrace();
3722 static struct ftrace_ops trace_probe_ops __read_mostly =
3724 .func = function_trace_probe_call,
3725 .flags = FTRACE_OPS_FL_INITIALIZED,
3726 INIT_OPS_HASH(trace_probe_ops)
3729 static int ftrace_probe_registered;
3731 static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
3736 if (ftrace_probe_registered) {
3737 /* still need to update the function call sites */
3739 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3744 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3745 struct hlist_head *hhd = &ftrace_func_hash[i];
3749 /* Nothing registered? */
3750 if (i == FTRACE_FUNC_HASHSIZE)
3753 ret = ftrace_startup(&trace_probe_ops, 0);
3755 ftrace_probe_registered = 1;
3758 static void __disable_ftrace_function_probe(void)
3762 if (!ftrace_probe_registered)
3765 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3766 struct hlist_head *hhd = &ftrace_func_hash[i];
3771 /* no more funcs left */
3772 ftrace_shutdown(&trace_probe_ops, 0);
3774 ftrace_probe_registered = 0;
3778 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3780 if (entry->ops->free)
3781 entry->ops->free(entry->ops, entry->ip, &entry->data);
3786 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3789 struct ftrace_ops_hash old_hash_ops;
3790 struct ftrace_func_probe *entry;
3791 struct ftrace_glob func_g;
3792 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3793 struct ftrace_hash *old_hash = *orig_hash;
3794 struct ftrace_hash *hash;
3795 struct ftrace_page *pg;
3796 struct dyn_ftrace *rec;
3802 func_g.type = filter_parse_regex(glob, strlen(glob),
3803 &func_g.search, ¬);
3804 func_g.len = strlen(func_g.search);
3806 /* we do not support '!' for function probes */
3810 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3812 old_hash_ops.filter_hash = old_hash;
3813 /* Probes only have filters */
3814 old_hash_ops.notrace_hash = NULL;
3816 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3822 if (unlikely(ftrace_disabled)) {
3827 mutex_lock(&ftrace_lock);
3829 do_for_each_ftrace_rec(pg, rec) {
3831 if (rec->flags & FTRACE_FL_DISABLED)
3834 if (!ftrace_match_record(rec, &func_g, NULL, 0))
3837 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3839 /* If we did not process any, then return error */
3850 * The caller might want to do something special
3851 * for each function we find. We call the callback
3852 * to give the caller an opportunity to do so.
3855 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3856 /* caller does not like this func */
3862 ret = enter_record(hash, rec, 0);
3870 entry->ip = rec->ip;
3872 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3873 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3875 } while_for_each_ftrace_rec();
3877 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3879 __enable_ftrace_function_probe(&old_hash_ops);
3882 free_ftrace_hash_rcu(old_hash);
3887 mutex_unlock(&ftrace_lock);
3889 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3890 free_ftrace_hash(hash);
3896 PROBE_TEST_FUNC = 1,
3901 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3902 void *data, int flags)
3904 struct ftrace_func_entry *rec_entry;
3905 struct ftrace_func_probe *entry;
3906 struct ftrace_func_probe *p;
3907 struct ftrace_glob func_g;
3908 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3909 struct ftrace_hash *old_hash = *orig_hash;
3910 struct list_head free_list;
3911 struct ftrace_hash *hash;
3912 struct hlist_node *tmp;
3913 char str[KSYM_SYMBOL_LEN];
3916 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3917 func_g.search = NULL;
3921 func_g.type = filter_parse_regex(glob, strlen(glob),
3922 &func_g.search, ¬);
3923 func_g.len = strlen(func_g.search);
3924 func_g.search = glob;
3926 /* we do not support '!' for function probes */
3931 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3933 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3935 /* Hmm, should report this somehow */
3938 INIT_LIST_HEAD(&free_list);
3940 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3941 struct hlist_head *hhd = &ftrace_func_hash[i];
3943 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3945 /* break up if statements for readability */
3946 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3949 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3952 /* do this last, since it is the most expensive */
3953 if (func_g.search) {
3954 kallsyms_lookup(entry->ip, NULL, NULL,
3956 if (!ftrace_match(str, &func_g))
3960 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3961 /* It is possible more than one entry had this ip */
3963 free_hash_entry(hash, rec_entry);
3965 hlist_del_rcu(&entry->node);
3966 list_add(&entry->free_list, &free_list);
3969 mutex_lock(&ftrace_lock);
3970 __disable_ftrace_function_probe();
3972 * Remove after the disable is called. Otherwise, if the last
3973 * probe is removed, a null hash means *all enabled*.
3975 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3976 synchronize_sched();
3978 free_ftrace_hash_rcu(old_hash);
3980 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3981 list_del(&entry->free_list);
3982 ftrace_free_entry(entry);
3984 mutex_unlock(&ftrace_lock);
3987 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3988 free_ftrace_hash(hash);
3992 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3995 __unregister_ftrace_function_probe(glob, ops, data,
3996 PROBE_TEST_FUNC | PROBE_TEST_DATA);
4000 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
4002 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
4005 void unregister_ftrace_function_probe_all(char *glob)
4007 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
4010 static LIST_HEAD(ftrace_commands);
4011 static DEFINE_MUTEX(ftrace_cmd_mutex);
4014 * Currently we only register ftrace commands from __init, so mark this
4017 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4019 struct ftrace_func_command *p;
4022 mutex_lock(&ftrace_cmd_mutex);
4023 list_for_each_entry(p, &ftrace_commands, list) {
4024 if (strcmp(cmd->name, p->name) == 0) {
4029 list_add(&cmd->list, &ftrace_commands);
4031 mutex_unlock(&ftrace_cmd_mutex);
4037 * Currently we only unregister ftrace commands from __init, so mark
4040 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4042 struct ftrace_func_command *p, *n;
4045 mutex_lock(&ftrace_cmd_mutex);
4046 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4047 if (strcmp(cmd->name, p->name) == 0) {
4049 list_del_init(&p->list);
4054 mutex_unlock(&ftrace_cmd_mutex);
4059 static int ftrace_process_regex(struct ftrace_hash *hash,
4060 char *buff, int len, int enable)
4062 char *func, *command, *next = buff;
4063 struct ftrace_func_command *p;
4066 func = strsep(&next, ":");
4069 ret = ftrace_match_records(hash, func, len);
4079 command = strsep(&next, ":");
4081 mutex_lock(&ftrace_cmd_mutex);
4082 list_for_each_entry(p, &ftrace_commands, list) {
4083 if (strcmp(p->name, command) == 0) {
4084 ret = p->func(hash, func, command, next, enable);
4089 mutex_unlock(&ftrace_cmd_mutex);
4095 ftrace_regex_write(struct file *file, const char __user *ubuf,
4096 size_t cnt, loff_t *ppos, int enable)
4098 struct ftrace_iterator *iter;
4099 struct trace_parser *parser;
4105 if (file->f_mode & FMODE_READ) {
4106 struct seq_file *m = file->private_data;
4109 iter = file->private_data;
4111 if (unlikely(ftrace_disabled))
4114 /* iter->hash is a local copy, so we don't need regex_lock */
4116 parser = &iter->parser;
4117 read = trace_get_user(parser, ubuf, cnt, ppos);
4119 if (read >= 0 && trace_parser_loaded(parser) &&
4120 !trace_parser_cont(parser)) {
4121 ret = ftrace_process_regex(iter->hash, parser->buffer,
4122 parser->idx, enable);
4123 trace_parser_clear(parser);
4134 ftrace_filter_write(struct file *file, const char __user *ubuf,
4135 size_t cnt, loff_t *ppos)
4137 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4141 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4142 size_t cnt, loff_t *ppos)
4144 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4148 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4150 struct ftrace_func_entry *entry;
4152 if (!ftrace_location(ip))
4156 entry = ftrace_lookup_ip(hash, ip);
4159 free_hash_entry(hash, entry);
4163 return add_hash_entry(hash, ip);
4166 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4167 struct ftrace_ops_hash *old_hash)
4169 struct ftrace_ops *op;
4171 if (!ftrace_enabled)
4174 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4175 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4180 * If this is the shared global_ops filter, then we need to
4181 * check if there is another ops that shares it, is enabled.
4182 * If so, we still need to run the modify code.
4184 if (ops->func_hash != &global_ops.local_hash)
4187 do_for_each_ftrace_op(op, ftrace_ops_list) {
4188 if (op->func_hash == &global_ops.local_hash &&
4189 op->flags & FTRACE_OPS_FL_ENABLED) {
4190 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4191 /* Only need to do this once */
4194 } while_for_each_ftrace_op(op);
4198 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4199 unsigned long ip, int remove, int reset, int enable)
4201 struct ftrace_hash **orig_hash;
4202 struct ftrace_ops_hash old_hash_ops;
4203 struct ftrace_hash *old_hash;
4204 struct ftrace_hash *hash;
4207 if (unlikely(ftrace_disabled))
4210 mutex_lock(&ops->func_hash->regex_lock);
4213 orig_hash = &ops->func_hash->filter_hash;
4215 orig_hash = &ops->func_hash->notrace_hash;
4218 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4220 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4224 goto out_regex_unlock;
4227 if (buf && !ftrace_match_records(hash, buf, len)) {
4229 goto out_regex_unlock;
4232 ret = ftrace_match_addr(hash, ip, remove);
4234 goto out_regex_unlock;
4237 mutex_lock(&ftrace_lock);
4238 old_hash = *orig_hash;
4239 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4240 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4241 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4243 ftrace_ops_update_code(ops, &old_hash_ops);
4244 free_ftrace_hash_rcu(old_hash);
4246 mutex_unlock(&ftrace_lock);
4249 mutex_unlock(&ops->func_hash->regex_lock);
4251 free_ftrace_hash(hash);
4256 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4257 int reset, int enable)
4259 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4263 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4264 * @ops - the ops to set the filter with
4265 * @ip - the address to add to or remove from the filter.
4266 * @remove - non zero to remove the ip from the filter
4267 * @reset - non zero to reset all filters before applying this filter.
4269 * Filters denote which functions should be enabled when tracing is enabled
4270 * If @ip is NULL, it failes to update filter.
4272 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4273 int remove, int reset)
4275 ftrace_ops_init(ops);
4276 return ftrace_set_addr(ops, ip, remove, reset, 1);
4278 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4281 * ftrace_ops_set_global_filter - setup ops to use global filters
4282 * @ops - the ops which will use the global filters
4284 * ftrace users who need global function trace filtering should call this.
4285 * It can set the global filter only if ops were not initialized before.
4287 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4289 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4292 ftrace_ops_init(ops);
4293 ops->func_hash = &global_ops.local_hash;
4295 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4298 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4299 int reset, int enable)
4301 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4305 * ftrace_set_filter - set a function to filter on in ftrace
4306 * @ops - the ops to set the filter with
4307 * @buf - the string that holds the function filter text.
4308 * @len - the length of the string.
4309 * @reset - non zero to reset all filters before applying this filter.
4311 * Filters denote which functions should be enabled when tracing is enabled.
4312 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4314 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4317 ftrace_ops_init(ops);
4318 return ftrace_set_regex(ops, buf, len, reset, 1);
4320 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4323 * ftrace_set_notrace - set a function to not trace in ftrace
4324 * @ops - the ops to set the notrace filter with
4325 * @buf - the string that holds the function notrace text.
4326 * @len - the length of the string.
4327 * @reset - non zero to reset all filters before applying this filter.
4329 * Notrace Filters denote which functions should not be enabled when tracing
4330 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4333 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4336 ftrace_ops_init(ops);
4337 return ftrace_set_regex(ops, buf, len, reset, 0);
4339 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4341 * ftrace_set_global_filter - set a function to filter on with global tracers
4342 * @buf - the string that holds the function filter text.
4343 * @len - the length of the string.
4344 * @reset - non zero to reset all filters before applying this filter.
4346 * Filters denote which functions should be enabled when tracing is enabled.
4347 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4349 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4351 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4353 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4356 * ftrace_set_global_notrace - set a function to not trace with global tracers
4357 * @buf - the string that holds the function notrace text.
4358 * @len - the length of the string.
4359 * @reset - non zero to reset all filters before applying this filter.
4361 * Notrace Filters denote which functions should not be enabled when tracing
4362 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4365 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4367 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4369 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4372 * command line interface to allow users to set filters on boot up.
4374 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4375 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4376 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4378 /* Used by function selftest to not test if filter is set */
4379 bool ftrace_filter_param __initdata;
4381 static int __init set_ftrace_notrace(char *str)
4383 ftrace_filter_param = true;
4384 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4387 __setup("ftrace_notrace=", set_ftrace_notrace);
4389 static int __init set_ftrace_filter(char *str)
4391 ftrace_filter_param = true;
4392 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4395 __setup("ftrace_filter=", set_ftrace_filter);
4397 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4398 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4399 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4400 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
4402 static unsigned long save_global_trampoline;
4403 static unsigned long save_global_flags;
4405 static int __init set_graph_function(char *str)
4407 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4410 __setup("ftrace_graph_filter=", set_graph_function);
4412 static int __init set_graph_notrace_function(char *str)
4414 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4417 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4419 static int __init set_graph_max_depth_function(char *str)
4423 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4426 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4428 static void __init set_ftrace_early_graph(char *buf, int enable)
4432 struct ftrace_hash *hash;
4434 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4439 func = strsep(&buf, ",");
4440 /* we allow only one expression at a time */
4441 ret = ftrace_graph_set_hash(hash, func);
4443 printk(KERN_DEBUG "ftrace: function %s not "
4444 "traceable\n", func);
4448 ftrace_graph_hash = hash;
4450 ftrace_graph_notrace_hash = hash;
4452 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4455 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4459 ftrace_ops_init(ops);
4462 func = strsep(&buf, ",");
4463 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4467 static void __init set_ftrace_early_filters(void)
4469 if (ftrace_filter_buf[0])
4470 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4471 if (ftrace_notrace_buf[0])
4472 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4473 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4474 if (ftrace_graph_buf[0])
4475 set_ftrace_early_graph(ftrace_graph_buf, 1);
4476 if (ftrace_graph_notrace_buf[0])
4477 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4478 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4481 int ftrace_regex_release(struct inode *inode, struct file *file)
4483 struct seq_file *m = (struct seq_file *)file->private_data;
4484 struct ftrace_ops_hash old_hash_ops;
4485 struct ftrace_iterator *iter;
4486 struct ftrace_hash **orig_hash;
4487 struct ftrace_hash *old_hash;
4488 struct trace_parser *parser;
4492 if (file->f_mode & FMODE_READ) {
4494 seq_release(inode, file);
4496 iter = file->private_data;
4498 parser = &iter->parser;
4499 if (trace_parser_loaded(parser)) {
4500 parser->buffer[parser->idx] = 0;
4501 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
4504 trace_parser_put(parser);
4506 mutex_lock(&iter->ops->func_hash->regex_lock);
4508 if (file->f_mode & FMODE_WRITE) {
4509 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4512 orig_hash = &iter->ops->func_hash->filter_hash;
4514 orig_hash = &iter->ops->func_hash->notrace_hash;
4516 mutex_lock(&ftrace_lock);
4517 old_hash = *orig_hash;
4518 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4519 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
4520 ret = ftrace_hash_move(iter->ops, filter_hash,
4521 orig_hash, iter->hash);
4523 ftrace_ops_update_code(iter->ops, &old_hash_ops);
4524 free_ftrace_hash_rcu(old_hash);
4526 mutex_unlock(&ftrace_lock);
4529 mutex_unlock(&iter->ops->func_hash->regex_lock);
4530 free_ftrace_hash(iter->hash);
4536 static const struct file_operations ftrace_avail_fops = {
4537 .open = ftrace_avail_open,
4539 .llseek = seq_lseek,
4540 .release = seq_release_private,
4543 static const struct file_operations ftrace_enabled_fops = {
4544 .open = ftrace_enabled_open,
4546 .llseek = seq_lseek,
4547 .release = seq_release_private,
4550 static const struct file_operations ftrace_filter_fops = {
4551 .open = ftrace_filter_open,
4553 .write = ftrace_filter_write,
4554 .llseek = tracing_lseek,
4555 .release = ftrace_regex_release,
4558 static const struct file_operations ftrace_notrace_fops = {
4559 .open = ftrace_notrace_open,
4561 .write = ftrace_notrace_write,
4562 .llseek = tracing_lseek,
4563 .release = ftrace_regex_release,
4566 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4568 static DEFINE_MUTEX(graph_lock);
4570 struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
4571 struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
4573 enum graph_filter_type {
4574 GRAPH_FILTER_NOTRACE = 0,
4575 GRAPH_FILTER_FUNCTION,
4578 #define FTRACE_GRAPH_EMPTY ((void *)1)
4580 struct ftrace_graph_data {
4581 struct ftrace_hash *hash;
4582 struct ftrace_func_entry *entry;
4583 int idx; /* for hash table iteration */
4584 enum graph_filter_type type;
4585 struct ftrace_hash *new_hash;
4586 const struct seq_operations *seq_ops;
4587 struct trace_parser parser;
4591 __g_next(struct seq_file *m, loff_t *pos)
4593 struct ftrace_graph_data *fgd = m->private;
4594 struct ftrace_func_entry *entry = fgd->entry;
4595 struct hlist_head *head;
4596 int i, idx = fgd->idx;
4598 if (*pos >= fgd->hash->count)
4602 hlist_for_each_entry_continue(entry, hlist) {
4610 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
4611 head = &fgd->hash->buckets[i];
4612 hlist_for_each_entry(entry, head, hlist) {
4622 g_next(struct seq_file *m, void *v, loff_t *pos)
4625 return __g_next(m, pos);
4628 static void *g_start(struct seq_file *m, loff_t *pos)
4630 struct ftrace_graph_data *fgd = m->private;
4632 mutex_lock(&graph_lock);
4634 if (fgd->type == GRAPH_FILTER_FUNCTION)
4635 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
4636 lockdep_is_held(&graph_lock));
4638 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4639 lockdep_is_held(&graph_lock));
4641 /* Nothing, tell g_show to print all functions are enabled */
4642 if (ftrace_hash_empty(fgd->hash) && !*pos)
4643 return FTRACE_GRAPH_EMPTY;
4647 return __g_next(m, pos);
4650 static void g_stop(struct seq_file *m, void *p)
4652 mutex_unlock(&graph_lock);
4655 static int g_show(struct seq_file *m, void *v)
4657 struct ftrace_func_entry *entry = v;
4662 if (entry == FTRACE_GRAPH_EMPTY) {
4663 struct ftrace_graph_data *fgd = m->private;
4665 if (fgd->type == GRAPH_FILTER_FUNCTION)
4666 seq_puts(m, "#### all functions enabled ####\n");
4668 seq_puts(m, "#### no functions disabled ####\n");
4672 seq_printf(m, "%ps\n", (void *)entry->ip);
4677 static const struct seq_operations ftrace_graph_seq_ops = {
4685 __ftrace_graph_open(struct inode *inode, struct file *file,
4686 struct ftrace_graph_data *fgd)
4689 struct ftrace_hash *new_hash = NULL;
4691 if (file->f_mode & FMODE_WRITE) {
4692 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
4694 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
4697 if (file->f_flags & O_TRUNC)
4698 new_hash = alloc_ftrace_hash(size_bits);
4700 new_hash = alloc_and_copy_ftrace_hash(size_bits,
4708 if (file->f_mode & FMODE_READ) {
4709 ret = seq_open(file, &ftrace_graph_seq_ops);
4711 struct seq_file *m = file->private_data;
4715 free_ftrace_hash(new_hash);
4719 file->private_data = fgd;
4722 if (ret < 0 && file->f_mode & FMODE_WRITE)
4723 trace_parser_put(&fgd->parser);
4725 fgd->new_hash = new_hash;
4728 * All uses of fgd->hash must be taken with the graph_lock
4729 * held. The graph_lock is going to be released, so force
4730 * fgd->hash to be reinitialized when it is taken again.
4738 ftrace_graph_open(struct inode *inode, struct file *file)
4740 struct ftrace_graph_data *fgd;
4743 if (unlikely(ftrace_disabled))
4746 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4750 mutex_lock(&graph_lock);
4752 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
4753 lockdep_is_held(&graph_lock));
4754 fgd->type = GRAPH_FILTER_FUNCTION;
4755 fgd->seq_ops = &ftrace_graph_seq_ops;
4757 ret = __ftrace_graph_open(inode, file, fgd);
4761 mutex_unlock(&graph_lock);
4766 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4768 struct ftrace_graph_data *fgd;
4771 if (unlikely(ftrace_disabled))
4774 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4778 mutex_lock(&graph_lock);
4780 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4781 lockdep_is_held(&graph_lock));
4782 fgd->type = GRAPH_FILTER_NOTRACE;
4783 fgd->seq_ops = &ftrace_graph_seq_ops;
4785 ret = __ftrace_graph_open(inode, file, fgd);
4789 mutex_unlock(&graph_lock);
4794 ftrace_graph_release(struct inode *inode, struct file *file)
4796 struct ftrace_graph_data *fgd;
4797 struct ftrace_hash *old_hash, *new_hash;
4798 struct trace_parser *parser;
4801 if (file->f_mode & FMODE_READ) {
4802 struct seq_file *m = file->private_data;
4805 seq_release(inode, file);
4807 fgd = file->private_data;
4811 if (file->f_mode & FMODE_WRITE) {
4813 parser = &fgd->parser;
4815 if (trace_parser_loaded((parser))) {
4816 parser->buffer[parser->idx] = 0;
4817 ret = ftrace_graph_set_hash(fgd->new_hash,
4821 trace_parser_put(parser);
4823 new_hash = __ftrace_hash_move(fgd->new_hash);
4829 mutex_lock(&graph_lock);
4831 if (fgd->type == GRAPH_FILTER_FUNCTION) {
4832 old_hash = rcu_dereference_protected(ftrace_graph_hash,
4833 lockdep_is_held(&graph_lock));
4834 rcu_assign_pointer(ftrace_graph_hash, new_hash);
4836 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4837 lockdep_is_held(&graph_lock));
4838 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
4841 mutex_unlock(&graph_lock);
4843 /* Wait till all users are no longer using the old hash */
4844 synchronize_sched();
4846 free_ftrace_hash(old_hash);
4850 kfree(fgd->new_hash);
4857 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
4859 struct ftrace_glob func_g;
4860 struct dyn_ftrace *rec;
4861 struct ftrace_page *pg;
4862 struct ftrace_func_entry *entry;
4867 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4868 &func_g.search, ¬);
4870 func_g.len = strlen(func_g.search);
4872 mutex_lock(&ftrace_lock);
4874 if (unlikely(ftrace_disabled)) {
4875 mutex_unlock(&ftrace_lock);
4879 do_for_each_ftrace_rec(pg, rec) {
4881 if (rec->flags & FTRACE_FL_DISABLED)
4884 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
4885 entry = ftrace_lookup_ip(hash, rec->ip);
4892 if (add_hash_entry(hash, rec->ip) < 0)
4896 free_hash_entry(hash, entry);
4901 } while_for_each_ftrace_rec();
4903 mutex_unlock(&ftrace_lock);
4912 ftrace_graph_write(struct file *file, const char __user *ubuf,
4913 size_t cnt, loff_t *ppos)
4915 ssize_t read, ret = 0;
4916 struct ftrace_graph_data *fgd = file->private_data;
4917 struct trace_parser *parser;
4922 /* Read mode uses seq functions */
4923 if (file->f_mode & FMODE_READ) {
4924 struct seq_file *m = file->private_data;
4928 parser = &fgd->parser;
4930 read = trace_get_user(parser, ubuf, cnt, ppos);
4932 if (read >= 0 && trace_parser_loaded(parser) &&
4933 !trace_parser_cont(parser)) {
4935 ret = ftrace_graph_set_hash(fgd->new_hash,
4937 trace_parser_clear(parser);
4946 static const struct file_operations ftrace_graph_fops = {
4947 .open = ftrace_graph_open,
4949 .write = ftrace_graph_write,
4950 .llseek = tracing_lseek,
4951 .release = ftrace_graph_release,
4954 static const struct file_operations ftrace_graph_notrace_fops = {
4955 .open = ftrace_graph_notrace_open,
4957 .write = ftrace_graph_write,
4958 .llseek = tracing_lseek,
4959 .release = ftrace_graph_release,
4961 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4963 void ftrace_create_filter_files(struct ftrace_ops *ops,
4964 struct dentry *parent)
4967 trace_create_file("set_ftrace_filter", 0644, parent,
4968 ops, &ftrace_filter_fops);
4970 trace_create_file("set_ftrace_notrace", 0644, parent,
4971 ops, &ftrace_notrace_fops);
4975 * The name "destroy_filter_files" is really a misnomer. Although
4976 * in the future, it may actualy delete the files, but this is
4977 * really intended to make sure the ops passed in are disabled
4978 * and that when this function returns, the caller is free to
4981 * The "destroy" name is only to match the "create" name that this
4982 * should be paired with.
4984 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4986 mutex_lock(&ftrace_lock);
4987 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4988 ftrace_shutdown(ops, 0);
4989 ops->flags |= FTRACE_OPS_FL_DELETED;
4990 mutex_unlock(&ftrace_lock);
4993 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
4996 trace_create_file("available_filter_functions", 0444,
4997 d_tracer, NULL, &ftrace_avail_fops);
4999 trace_create_file("enabled_functions", 0444,
5000 d_tracer, NULL, &ftrace_enabled_fops);
5002 ftrace_create_filter_files(&global_ops, d_tracer);
5004 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5005 trace_create_file("set_graph_function", 0444, d_tracer,
5007 &ftrace_graph_fops);
5008 trace_create_file("set_graph_notrace", 0444, d_tracer,
5010 &ftrace_graph_notrace_fops);
5011 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5016 static int ftrace_cmp_ips(const void *a, const void *b)
5018 const unsigned long *ipa = a;
5019 const unsigned long *ipb = b;
5028 static int ftrace_process_locs(struct module *mod,
5029 unsigned long *start,
5032 struct ftrace_page *start_pg;
5033 struct ftrace_page *pg;
5034 struct dyn_ftrace *rec;
5035 unsigned long count;
5038 unsigned long flags = 0; /* Shut up gcc */
5041 count = end - start;
5046 sort(start, count, sizeof(*start),
5047 ftrace_cmp_ips, NULL);
5049 start_pg = ftrace_allocate_pages(count);
5053 mutex_lock(&ftrace_lock);
5056 * Core and each module needs their own pages, as
5057 * modules will free them when they are removed.
5058 * Force a new page to be allocated for modules.
5061 WARN_ON(ftrace_pages || ftrace_pages_start);
5062 /* First initialization */
5063 ftrace_pages = ftrace_pages_start = start_pg;
5068 if (WARN_ON(ftrace_pages->next)) {
5069 /* Hmm, we have free pages? */
5070 while (ftrace_pages->next)
5071 ftrace_pages = ftrace_pages->next;
5074 ftrace_pages->next = start_pg;
5080 addr = ftrace_call_adjust(*p++);
5082 * Some architecture linkers will pad between
5083 * the different mcount_loc sections of different
5084 * object files to satisfy alignments.
5085 * Skip any NULL pointers.
5090 if (pg->index == pg->size) {
5091 /* We should have allocated enough */
5092 if (WARN_ON(!pg->next))
5097 rec = &pg->records[pg->index++];
5101 /* We should have used all pages */
5104 /* Assign the last page to ftrace_pages */
5108 * We only need to disable interrupts on start up
5109 * because we are modifying code that an interrupt
5110 * may execute, and the modification is not atomic.
5111 * But for modules, nothing runs the code we modify
5112 * until we are finished with it, and there's no
5113 * reason to cause large interrupt latencies while we do it.
5116 local_irq_save(flags);
5117 ftrace_update_code(mod, start_pg);
5119 local_irq_restore(flags);
5122 mutex_unlock(&ftrace_lock);
5127 #ifdef CONFIG_MODULES
5129 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5131 static int referenced_filters(struct dyn_ftrace *rec)
5133 struct ftrace_ops *ops;
5136 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5137 if (ops_references_rec(ops, rec))
5144 void ftrace_release_mod(struct module *mod)
5146 struct dyn_ftrace *rec;
5147 struct ftrace_page **last_pg;
5148 struct ftrace_page *pg;
5151 mutex_lock(&ftrace_lock);
5153 if (ftrace_disabled)
5157 * Each module has its own ftrace_pages, remove
5158 * them from the list.
5160 last_pg = &ftrace_pages_start;
5161 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5162 rec = &pg->records[0];
5163 if (within_module_core(rec->ip, mod)) {
5165 * As core pages are first, the first
5166 * page should never be a module page.
5168 if (WARN_ON(pg == ftrace_pages_start))
5171 /* Check if we are deleting the last page */
5172 if (pg == ftrace_pages)
5173 ftrace_pages = next_to_ftrace_page(last_pg);
5175 *last_pg = pg->next;
5176 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5177 free_pages((unsigned long)pg->records, order);
5180 last_pg = &pg->next;
5183 mutex_unlock(&ftrace_lock);
5186 void ftrace_module_enable(struct module *mod)
5188 struct dyn_ftrace *rec;
5189 struct ftrace_page *pg;
5191 mutex_lock(&ftrace_lock);
5193 if (ftrace_disabled)
5197 * If the tracing is enabled, go ahead and enable the record.
5199 * The reason not to enable the record immediatelly is the
5200 * inherent check of ftrace_make_nop/ftrace_make_call for
5201 * correct previous instructions. Making first the NOP
5202 * conversion puts the module to the correct state, thus
5203 * passing the ftrace_make_call check.
5205 * We also delay this to after the module code already set the
5206 * text to read-only, as we now need to set it back to read-write
5207 * so that we can modify the text.
5209 if (ftrace_start_up)
5210 ftrace_arch_code_modify_prepare();
5212 do_for_each_ftrace_rec(pg, rec) {
5215 * do_for_each_ftrace_rec() is a double loop.
5216 * module text shares the pg. If a record is
5217 * not part of this module, then skip this pg,
5218 * which the "break" will do.
5220 if (!within_module_core(rec->ip, mod))
5226 * When adding a module, we need to check if tracers are
5227 * currently enabled and if they are, and can trace this record,
5228 * we need to enable the module functions as well as update the
5229 * reference counts for those function records.
5231 if (ftrace_start_up)
5232 cnt += referenced_filters(rec);
5234 /* This clears FTRACE_FL_DISABLED */
5237 if (ftrace_start_up && cnt) {
5238 int failed = __ftrace_replace_code(rec, 1);
5240 ftrace_bug(failed, rec);
5245 } while_for_each_ftrace_rec();
5248 if (ftrace_start_up)
5249 ftrace_arch_code_modify_post_process();
5252 mutex_unlock(&ftrace_lock);
5255 void ftrace_module_init(struct module *mod)
5257 if (ftrace_disabled || !mod->num_ftrace_callsites)
5260 ftrace_process_locs(mod, mod->ftrace_callsites,
5261 mod->ftrace_callsites + mod->num_ftrace_callsites);
5263 #endif /* CONFIG_MODULES */
5265 void __init ftrace_init(void)
5267 extern unsigned long __start_mcount_loc[];
5268 extern unsigned long __stop_mcount_loc[];
5269 unsigned long count, flags;
5272 local_irq_save(flags);
5273 ret = ftrace_dyn_arch_init();
5274 local_irq_restore(flags);
5278 count = __stop_mcount_loc - __start_mcount_loc;
5280 pr_info("ftrace: No functions to be traced?\n");
5284 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5285 count, count / ENTRIES_PER_PAGE + 1);
5287 last_ftrace_enabled = ftrace_enabled = 1;
5289 ret = ftrace_process_locs(NULL,
5293 set_ftrace_early_filters();
5297 ftrace_disabled = 1;
5300 /* Do nothing if arch does not support this */
5301 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5305 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5309 * Currently there's no safe way to free a trampoline when the kernel
5310 * is configured with PREEMPT. That is because a task could be preempted
5311 * when it jumped to the trampoline, it may be preempted for a long time
5312 * depending on the system load, and currently there's no way to know
5313 * when it will be off the trampoline. If the trampoline is freed
5314 * too early, when the task runs again, it will be executing on freed
5317 #ifdef CONFIG_PREEMPT
5318 /* Currently, only non dynamic ops can have a trampoline */
5319 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5323 arch_ftrace_update_trampoline(ops);
5328 static struct ftrace_ops global_ops = {
5329 .func = ftrace_stub,
5330 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5331 FTRACE_OPS_FL_INITIALIZED |
5335 static int __init ftrace_nodyn_init(void)
5340 core_initcall(ftrace_nodyn_init);
5342 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
5343 static inline void ftrace_startup_enable(int command) { }
5344 static inline void ftrace_startup_all(int command) { }
5345 /* Keep as macros so we do not need to define the commands */
5346 # define ftrace_startup(ops, command) \
5348 int ___ret = __register_ftrace_function(ops); \
5350 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5353 # define ftrace_shutdown(ops, command) \
5355 int ___ret = __unregister_ftrace_function(ops); \
5357 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5361 # define ftrace_startup_sysctl() do { } while (0)
5362 # define ftrace_shutdown_sysctl() do { } while (0)
5365 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
5370 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5374 #endif /* CONFIG_DYNAMIC_FTRACE */
5376 __init void ftrace_init_global_array_ops(struct trace_array *tr)
5378 tr->ops = &global_ops;
5379 tr->ops->private = tr;
5382 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5384 /* If we filter on pids, update to use the pid function */
5385 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5386 if (WARN_ON(tr->ops->func != ftrace_stub))
5387 printk("ftrace ops had %pS for function\n",
5390 tr->ops->func = func;
5391 tr->ops->private = tr;
5394 void ftrace_reset_array_ops(struct trace_array *tr)
5396 tr->ops->func = ftrace_stub;
5400 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5401 struct ftrace_ops *ignored, struct pt_regs *regs)
5403 struct ftrace_ops *op;
5406 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5411 * Some of the ops may be dynamically allocated,
5412 * they must be freed after a synchronize_sched().
5414 preempt_disable_notrace();
5416 do_for_each_ftrace_op(op, ftrace_ops_list) {
5418 * Check the following for each ops before calling their func:
5419 * if RCU flag is set, then rcu_is_watching() must be true
5420 * if PER_CPU is set, then ftrace_function_local_disable()
5422 * Otherwise test if the ip matches the ops filter
5424 * If any of the above fails then the op->func() is not executed.
5426 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5427 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5428 !ftrace_function_local_disabled(op)) &&
5429 ftrace_ops_test(op, ip, regs)) {
5431 if (FTRACE_WARN_ON(!op->func)) {
5432 pr_warn("op=%p %pS\n", op, op);
5435 op->func(ip, parent_ip, op, regs);
5437 } while_for_each_ftrace_op(op);
5439 preempt_enable_notrace();
5440 trace_clear_recursion(bit);
5444 * Some archs only support passing ip and parent_ip. Even though
5445 * the list function ignores the op parameter, we do not want any
5446 * C side effects, where a function is called without the caller
5447 * sending a third parameter.
5448 * Archs are to support both the regs and ftrace_ops at the same time.
5449 * If they support ftrace_ops, it is assumed they support regs.
5450 * If call backs want to use regs, they must either check for regs
5451 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5452 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
5453 * An architecture can pass partial regs with ftrace_ops and still
5454 * set the ARCH_SUPPORTS_FTRACE_OPS.
5456 #if ARCH_SUPPORTS_FTRACE_OPS
5457 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5458 struct ftrace_ops *op, struct pt_regs *regs)
5460 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
5463 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5465 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
5470 * If there's only one function registered but it does not support
5471 * recursion, needs RCU protection and/or requires per cpu handling, then
5472 * this function will be called by the mcount trampoline.
5474 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
5475 struct ftrace_ops *op, struct pt_regs *regs)
5479 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
5482 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5486 preempt_disable_notrace();
5488 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5489 !ftrace_function_local_disabled(op)) {
5490 op->func(ip, parent_ip, op, regs);
5493 preempt_enable_notrace();
5494 trace_clear_recursion(bit);
5498 * ftrace_ops_get_func - get the function a trampoline should call
5499 * @ops: the ops to get the function for
5501 * Normally the mcount trampoline will call the ops->func, but there
5502 * are times that it should not. For example, if the ops does not
5503 * have its own recursion protection, then it should call the
5504 * ftrace_ops_assist_func() instead.
5506 * Returns the function that the trampoline should call for @ops.
5508 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5511 * If the function does not handle recursion, needs to be RCU safe,
5512 * or does per cpu logic, then we need to call the assist handler.
5514 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5515 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5516 return ftrace_ops_assist_func;
5522 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5523 struct task_struct *prev, struct task_struct *next)
5525 struct trace_array *tr = data;
5526 struct trace_pid_list *pid_list;
5528 pid_list = rcu_dereference_sched(tr->function_pids);
5530 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5531 trace_ignore_this_task(pid_list, next));
5534 static void clear_ftrace_pids(struct trace_array *tr)
5536 struct trace_pid_list *pid_list;
5539 pid_list = rcu_dereference_protected(tr->function_pids,
5540 lockdep_is_held(&ftrace_lock));
5544 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5546 for_each_possible_cpu(cpu)
5547 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5549 rcu_assign_pointer(tr->function_pids, NULL);
5551 /* Wait till all users are no longer using pid filtering */
5552 synchronize_sched();
5554 trace_free_pid_list(pid_list);
5557 static void ftrace_pid_reset(struct trace_array *tr)
5559 mutex_lock(&ftrace_lock);
5560 clear_ftrace_pids(tr);
5562 ftrace_update_pid_func();
5563 ftrace_startup_all(0);
5565 mutex_unlock(&ftrace_lock);
5568 /* Greater than any max PID */
5569 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5571 static void *fpid_start(struct seq_file *m, loff_t *pos)
5574 struct trace_pid_list *pid_list;
5575 struct trace_array *tr = m->private;
5577 mutex_lock(&ftrace_lock);
5578 rcu_read_lock_sched();
5580 pid_list = rcu_dereference_sched(tr->function_pids);
5583 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5585 return trace_pid_start(pid_list, pos);
5588 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5590 struct trace_array *tr = m->private;
5591 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5593 if (v == FTRACE_NO_PIDS)
5596 return trace_pid_next(pid_list, v, pos);
5599 static void fpid_stop(struct seq_file *m, void *p)
5602 rcu_read_unlock_sched();
5603 mutex_unlock(&ftrace_lock);
5606 static int fpid_show(struct seq_file *m, void *v)
5608 if (v == FTRACE_NO_PIDS) {
5609 seq_puts(m, "no pid\n");
5613 return trace_pid_show(m, v);
5616 static const struct seq_operations ftrace_pid_sops = {
5617 .start = fpid_start,
5624 ftrace_pid_open(struct inode *inode, struct file *file)
5626 struct trace_array *tr = inode->i_private;
5630 if (trace_array_get(tr) < 0)
5633 if ((file->f_mode & FMODE_WRITE) &&
5634 (file->f_flags & O_TRUNC))
5635 ftrace_pid_reset(tr);
5637 ret = seq_open(file, &ftrace_pid_sops);
5639 trace_array_put(tr);
5641 m = file->private_data;
5642 /* copy tr over to seq ops */
5649 static void ignore_task_cpu(void *data)
5651 struct trace_array *tr = data;
5652 struct trace_pid_list *pid_list;
5655 * This function is called by on_each_cpu() while the
5656 * event_mutex is held.
5658 pid_list = rcu_dereference_protected(tr->function_pids,
5659 mutex_is_locked(&ftrace_lock));
5661 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5662 trace_ignore_this_task(pid_list, current));
5666 ftrace_pid_write(struct file *filp, const char __user *ubuf,
5667 size_t cnt, loff_t *ppos)
5669 struct seq_file *m = filp->private_data;
5670 struct trace_array *tr = m->private;
5671 struct trace_pid_list *filtered_pids = NULL;
5672 struct trace_pid_list *pid_list;
5678 mutex_lock(&ftrace_lock);
5680 filtered_pids = rcu_dereference_protected(tr->function_pids,
5681 lockdep_is_held(&ftrace_lock));
5683 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5687 rcu_assign_pointer(tr->function_pids, pid_list);
5689 if (filtered_pids) {
5690 synchronize_sched();
5691 trace_free_pid_list(filtered_pids);
5692 } else if (pid_list) {
5693 /* Register a probe to set whether to ignore the tracing of a task */
5694 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5698 * Ignoring of pids is done at task switch. But we have to
5699 * check for those tasks that are currently running.
5700 * Always do this in case a pid was appended or removed.
5702 on_each_cpu(ignore_task_cpu, tr, 1);
5704 ftrace_update_pid_func();
5705 ftrace_startup_all(0);
5707 mutex_unlock(&ftrace_lock);
5716 ftrace_pid_release(struct inode *inode, struct file *file)
5718 struct trace_array *tr = inode->i_private;
5720 trace_array_put(tr);
5722 return seq_release(inode, file);
5725 static const struct file_operations ftrace_pid_fops = {
5726 .open = ftrace_pid_open,
5727 .write = ftrace_pid_write,
5729 .llseek = tracing_lseek,
5730 .release = ftrace_pid_release,
5733 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
5735 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5736 tr, &ftrace_pid_fops);
5739 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5740 struct dentry *d_tracer)
5742 /* Only the top level directory has the dyn_tracefs and profile */
5743 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5745 ftrace_init_dyn_tracefs(d_tracer);
5746 ftrace_profile_tracefs(d_tracer);
5750 * ftrace_kill - kill ftrace
5752 * This function should be used by panic code. It stops ftrace
5753 * but in a not so nice way. If you need to simply kill ftrace
5754 * from a non-atomic section, use ftrace_kill.
5756 void ftrace_kill(void)
5758 ftrace_disabled = 1;
5760 clear_ftrace_function();
5764 * Test if ftrace is dead or not.
5766 int ftrace_is_dead(void)
5768 return ftrace_disabled;
5772 * register_ftrace_function - register a function for profiling
5773 * @ops - ops structure that holds the function for profiling.
5775 * Register a function to be called by all functions in the
5778 * Note: @ops->func and all the functions it calls must be labeled
5779 * with "notrace", otherwise it will go into a
5782 int register_ftrace_function(struct ftrace_ops *ops)
5786 ftrace_ops_init(ops);
5788 mutex_lock(&ftrace_lock);
5790 ret = ftrace_startup(ops, 0);
5792 mutex_unlock(&ftrace_lock);
5796 EXPORT_SYMBOL_GPL(register_ftrace_function);
5799 * unregister_ftrace_function - unregister a function for profiling.
5800 * @ops - ops structure that holds the function to unregister
5802 * Unregister a function that was added to be called by ftrace profiling.
5804 int unregister_ftrace_function(struct ftrace_ops *ops)
5808 mutex_lock(&ftrace_lock);
5809 ret = ftrace_shutdown(ops, 0);
5810 mutex_unlock(&ftrace_lock);
5814 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5817 ftrace_enable_sysctl(struct ctl_table *table, int write,
5818 void __user *buffer, size_t *lenp,
5823 mutex_lock(&ftrace_lock);
5825 if (unlikely(ftrace_disabled))
5828 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5830 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5833 last_ftrace_enabled = !!ftrace_enabled;
5835 if (ftrace_enabled) {
5837 /* we are starting ftrace again */
5838 if (ftrace_ops_list != &ftrace_list_end)
5839 update_ftrace_function();
5841 ftrace_startup_sysctl();
5844 /* stopping ftrace calls (just send to ftrace_stub) */
5845 ftrace_trace_function = ftrace_stub;
5847 ftrace_shutdown_sysctl();
5851 mutex_unlock(&ftrace_lock);
5855 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5857 static struct ftrace_ops graph_ops = {
5858 .func = ftrace_stub,
5859 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5860 FTRACE_OPS_FL_INITIALIZED |
5863 #ifdef FTRACE_GRAPH_TRAMP_ADDR
5864 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5865 /* trampoline_size is only needed for dynamically allocated tramps */
5867 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5870 void ftrace_graph_sleep_time_control(bool enable)
5872 fgraph_sleep_time = enable;
5875 void ftrace_graph_graph_time_control(bool enable)
5877 fgraph_graph_time = enable;
5880 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5885 /* The callbacks that hook a function */
5886 trace_func_graph_ret_t ftrace_graph_return =
5887 (trace_func_graph_ret_t)ftrace_stub;
5888 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5889 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5891 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5892 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5896 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5897 struct task_struct *g, *t;
5899 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5900 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5901 * sizeof(struct ftrace_ret_stack),
5903 if (!ret_stack_list[i]) {
5911 read_lock(&tasklist_lock);
5912 do_each_thread(g, t) {
5918 if (t->ret_stack == NULL) {
5919 atomic_set(&t->tracing_graph_pause, 0);
5920 atomic_set(&t->trace_overrun, 0);
5921 t->curr_ret_stack = -1;
5922 /* Make sure the tasks see the -1 first: */
5924 t->ret_stack = ret_stack_list[start++];
5926 } while_each_thread(g, t);
5929 read_unlock(&tasklist_lock);
5931 for (i = start; i < end; i++)
5932 kfree(ret_stack_list[i]);
5937 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
5938 struct task_struct *prev, struct task_struct *next)
5940 unsigned long long timestamp;
5944 * Does the user want to count the time a function was asleep.
5945 * If so, do not update the time stamps.
5947 if (fgraph_sleep_time)
5950 timestamp = trace_clock_local();
5952 prev->ftrace_timestamp = timestamp;
5954 /* only process tasks that we timestamped */
5955 if (!next->ftrace_timestamp)
5959 * Update all the counters in next to make up for the
5960 * time next was sleeping.
5962 timestamp -= next->ftrace_timestamp;
5964 for (index = next->curr_ret_stack; index >= 0; index--)
5965 next->ret_stack[index].calltime += timestamp;
5968 /* Allocate a return stack for each task */
5969 static int start_graph_tracing(void)
5971 struct ftrace_ret_stack **ret_stack_list;
5974 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5975 sizeof(struct ftrace_ret_stack *),
5978 if (!ret_stack_list)
5981 /* The cpu_boot init_task->ret_stack will never be freed */
5982 for_each_online_cpu(cpu) {
5983 if (!idle_task(cpu)->ret_stack)
5984 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5988 ret = alloc_retstack_tasklist(ret_stack_list);
5989 } while (ret == -EAGAIN);
5992 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5994 pr_info("ftrace_graph: Couldn't activate tracepoint"
5995 " probe to kernel_sched_switch\n");
5998 kfree(ret_stack_list);
6003 * Hibernation protection.
6004 * The state of the current task is too much unstable during
6005 * suspend/restore to disk. We want to protect against that.
6008 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6012 case PM_HIBERNATION_PREPARE:
6013 pause_graph_tracing();
6016 case PM_POST_HIBERNATION:
6017 unpause_graph_tracing();
6023 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6025 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6027 return __ftrace_graph_entry(trace);
6031 * The function graph tracer should only trace the functions defined
6032 * by set_ftrace_filter and set_ftrace_notrace. If another function
6033 * tracer ops is registered, the graph tracer requires testing the
6034 * function against the global ops, and not just trace any function
6035 * that any ftrace_ops registered.
6037 static void update_function_graph_func(void)
6039 struct ftrace_ops *op;
6040 bool do_test = false;
6043 * The graph and global ops share the same set of functions
6044 * to test. If any other ops is on the list, then
6045 * the graph tracing needs to test if its the function
6048 do_for_each_ftrace_op(op, ftrace_ops_list) {
6049 if (op != &global_ops && op != &graph_ops &&
6050 op != &ftrace_list_end) {
6052 /* in double loop, break out with goto */
6055 } while_for_each_ftrace_op(op);
6058 ftrace_graph_entry = ftrace_graph_entry_test;
6060 ftrace_graph_entry = __ftrace_graph_entry;
6063 static struct notifier_block ftrace_suspend_notifier = {
6064 .notifier_call = ftrace_suspend_notifier_call,
6067 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
6068 trace_func_graph_ent_t entryfunc)
6072 mutex_lock(&ftrace_lock);
6074 /* we currently allow only one tracer registered at a time */
6075 if (ftrace_graph_active) {
6080 register_pm_notifier(&ftrace_suspend_notifier);
6082 ftrace_graph_active++;
6083 ret = start_graph_tracing();
6085 ftrace_graph_active--;
6089 ftrace_graph_return = retfunc;
6092 * Update the indirect function to the entryfunc, and the
6093 * function that gets called to the entry_test first. Then
6094 * call the update fgraph entry function to determine if
6095 * the entryfunc should be called directly or not.
6097 __ftrace_graph_entry = entryfunc;
6098 ftrace_graph_entry = ftrace_graph_entry_test;
6099 update_function_graph_func();
6101 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
6103 mutex_unlock(&ftrace_lock);
6107 void unregister_ftrace_graph(void)
6109 mutex_lock(&ftrace_lock);
6111 if (unlikely(!ftrace_graph_active))
6114 ftrace_graph_active--;
6115 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
6116 ftrace_graph_entry = ftrace_graph_entry_stub;
6117 __ftrace_graph_entry = ftrace_graph_entry_stub;
6118 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
6119 unregister_pm_notifier(&ftrace_suspend_notifier);
6120 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6122 #ifdef CONFIG_DYNAMIC_FTRACE
6124 * Function graph does not allocate the trampoline, but
6125 * other global_ops do. We need to reset the ALLOC_TRAMP flag
6128 global_ops.trampoline = save_global_trampoline;
6129 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
6130 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
6134 mutex_unlock(&ftrace_lock);
6137 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6140 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6142 atomic_set(&t->tracing_graph_pause, 0);
6143 atomic_set(&t->trace_overrun, 0);
6144 t->ftrace_timestamp = 0;
6145 /* make curr_ret_stack visible before we add the ret_stack */
6147 t->ret_stack = ret_stack;
6151 * Allocate a return stack for the idle task. May be the first
6152 * time through, or it may be done by CPU hotplug online.
6154 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6156 t->curr_ret_stack = -1;
6158 * The idle task has no parent, it either has its own
6159 * stack or no stack at all.
6162 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6164 if (ftrace_graph_active) {
6165 struct ftrace_ret_stack *ret_stack;
6167 ret_stack = per_cpu(idle_ret_stack, cpu);
6169 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6170 * sizeof(struct ftrace_ret_stack),
6174 per_cpu(idle_ret_stack, cpu) = ret_stack;
6176 graph_init_task(t, ret_stack);
6180 /* Allocate a return stack for newly created task */
6181 void ftrace_graph_init_task(struct task_struct *t)
6183 /* Make sure we do not use the parent ret_stack */
6184 t->ret_stack = NULL;
6185 t->curr_ret_stack = -1;
6187 if (ftrace_graph_active) {
6188 struct ftrace_ret_stack *ret_stack;
6190 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6191 * sizeof(struct ftrace_ret_stack),
6195 graph_init_task(t, ret_stack);
6199 void ftrace_graph_exit_task(struct task_struct *t)
6201 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6203 t->ret_stack = NULL;
6204 /* NULL must become visible to IRQs before we free it: */