1 // SPDX-License-Identifier: GPL-2.0
3 * Infrastructure for profiling code inserted by 'gcc -pg'.
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
8 * Originally ported from the -rt patch by:
9 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
11 * Based on code in the latency_tracer, that is:
13 * Copyright (C) 2004-2006 Ingo Molnar
14 * Copyright (C) 2004 Nadia Yvette Chambers
17 #include <linux/stop_machine.h>
18 #include <linux/clocksource.h>
19 #include <linux/sched/task.h>
20 #include <linux/kallsyms.h>
21 #include <linux/security.h>
22 #include <linux/seq_file.h>
23 #include <linux/tracefs.h>
24 #include <linux/hardirq.h>
25 #include <linux/kthread.h>
26 #include <linux/uaccess.h>
27 #include <linux/bsearch.h>
28 #include <linux/module.h>
29 #include <linux/ftrace.h>
30 #include <linux/sysctl.h>
31 #include <linux/slab.h>
32 #include <linux/ctype.h>
33 #include <linux/sort.h>
34 #include <linux/list.h>
35 #include <linux/hash.h>
36 #include <linux/rcupdate.h>
37 #include <linux/kprobes.h>
39 #include <trace/events/sched.h>
41 #include <asm/sections.h>
42 #include <asm/setup.h>
44 #include "ftrace_internal.h"
45 #include "trace_output.h"
46 #include "trace_stat.h"
48 #define FTRACE_WARN_ON(cond) \
56 #define FTRACE_WARN_ON_ONCE(cond) \
59 if (WARN_ON_ONCE(___r)) \
64 /* hash bits for specific function selection */
65 #define FTRACE_HASH_DEFAULT_BITS 10
66 #define FTRACE_HASH_MAX_BITS 12
68 #ifdef CONFIG_DYNAMIC_FTRACE
69 #define INIT_OPS_HASH(opsname) \
70 .func_hash = &opsname.local_hash, \
71 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
73 #define INIT_OPS_HASH(opsname)
77 FTRACE_MODIFY_ENABLE_FL = (1 << 0),
78 FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
81 struct ftrace_ops ftrace_list_end __read_mostly = {
83 .flags = FTRACE_OPS_FL_STUB,
84 INIT_OPS_HASH(ftrace_list_end)
87 /* ftrace_enabled is a method to turn ftrace on or off */
88 int ftrace_enabled __read_mostly;
89 static int last_ftrace_enabled;
91 /* Current function tracing op */
92 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
93 /* What to set function_trace_op to */
94 static struct ftrace_ops *set_function_trace_op;
96 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
98 struct trace_array *tr;
100 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
105 return tr->function_pids != NULL || tr->function_no_pids != NULL;
108 static void ftrace_update_trampoline(struct ftrace_ops *ops);
111 * ftrace_disabled is set when an anomaly is discovered.
112 * ftrace_disabled is much stronger than ftrace_enabled.
114 static int ftrace_disabled __read_mostly;
116 DEFINE_MUTEX(ftrace_lock);
118 struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
119 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
120 struct ftrace_ops global_ops;
122 /* Defined by vmlinux.lds.h see the commment above arch_ftrace_ops_list_func for details */
123 void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
124 struct ftrace_ops *op, struct ftrace_regs *fregs);
126 static inline void ftrace_ops_init(struct ftrace_ops *ops)
128 #ifdef CONFIG_DYNAMIC_FTRACE
129 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
130 mutex_init(&ops->local_hash.regex_lock);
131 ops->func_hash = &ops->local_hash;
132 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
137 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
138 struct ftrace_ops *op, struct ftrace_regs *fregs)
140 struct trace_array *tr = op->private;
144 pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid);
145 if (pid == FTRACE_PID_IGNORE)
147 if (pid != FTRACE_PID_TRACE &&
152 op->saved_func(ip, parent_ip, op, fregs);
155 static void ftrace_sync_ipi(void *data)
157 /* Probably not needed, but do it anyway */
161 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
164 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
165 * then it needs to call the list anyway.
167 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
168 FTRACE_FORCE_LIST_FUNC)
169 return ftrace_ops_list_func;
171 return ftrace_ops_get_func(ops);
174 static void update_ftrace_function(void)
179 * Prepare the ftrace_ops that the arch callback will use.
180 * If there's only one ftrace_ops registered, the ftrace_ops_list
181 * will point to the ops we want.
183 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
184 lockdep_is_held(&ftrace_lock));
186 /* If there's no ftrace_ops registered, just call the stub function */
187 if (set_function_trace_op == &ftrace_list_end) {
191 * If we are at the end of the list and this ops is
192 * recursion safe and not dynamic and the arch supports passing ops,
193 * then have the mcount trampoline call the function directly.
195 } else if (rcu_dereference_protected(ftrace_ops_list->next,
196 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
197 func = ftrace_ops_get_list_func(ftrace_ops_list);
200 /* Just use the default ftrace_ops */
201 set_function_trace_op = &ftrace_list_end;
202 func = ftrace_ops_list_func;
205 update_function_graph_func();
207 /* If there's no change, then do nothing more here */
208 if (ftrace_trace_function == func)
212 * If we are using the list function, it doesn't care
213 * about the function_trace_ops.
215 if (func == ftrace_ops_list_func) {
216 ftrace_trace_function = func;
218 * Don't even bother setting function_trace_ops,
219 * it would be racy to do so anyway.
224 #ifndef CONFIG_DYNAMIC_FTRACE
226 * For static tracing, we need to be a bit more careful.
227 * The function change takes affect immediately. Thus,
228 * we need to coordinate the setting of the function_trace_ops
229 * with the setting of the ftrace_trace_function.
231 * Set the function to the list ops, which will call the
232 * function we want, albeit indirectly, but it handles the
233 * ftrace_ops and doesn't depend on function_trace_op.
235 ftrace_trace_function = ftrace_ops_list_func;
237 * Make sure all CPUs see this. Yes this is slow, but static
238 * tracing is slow and nasty to have enabled.
240 synchronize_rcu_tasks_rude();
241 /* Now all cpus are using the list ops. */
242 function_trace_op = set_function_trace_op;
243 /* Make sure the function_trace_op is visible on all CPUs */
245 /* Nasty way to force a rmb on all cpus */
246 smp_call_function(ftrace_sync_ipi, NULL, 1);
247 /* OK, we are all set to update the ftrace_trace_function now! */
248 #endif /* !CONFIG_DYNAMIC_FTRACE */
250 ftrace_trace_function = func;
253 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
254 struct ftrace_ops *ops)
256 rcu_assign_pointer(ops->next, *list);
259 * We are entering ops into the list but another
260 * CPU might be walking that list. We need to make sure
261 * the ops->next pointer is valid before another CPU sees
262 * the ops pointer included into the list.
264 rcu_assign_pointer(*list, ops);
267 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
268 struct ftrace_ops *ops)
270 struct ftrace_ops **p;
273 * If we are removing the last function, then simply point
274 * to the ftrace_stub.
276 if (rcu_dereference_protected(*list,
277 lockdep_is_held(&ftrace_lock)) == ops &&
278 rcu_dereference_protected(ops->next,
279 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
280 *list = &ftrace_list_end;
284 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
295 static void ftrace_update_trampoline(struct ftrace_ops *ops);
297 int __register_ftrace_function(struct ftrace_ops *ops)
299 if (ops->flags & FTRACE_OPS_FL_DELETED)
302 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
305 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
307 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
308 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
309 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
311 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
312 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
315 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
316 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
318 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
321 if (!is_kernel_core_data((unsigned long)ops))
322 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
324 add_ftrace_ops(&ftrace_ops_list, ops);
326 /* Always save the function, and reset at unregistering */
327 ops->saved_func = ops->func;
329 if (ftrace_pids_enabled(ops))
330 ops->func = ftrace_pid_func;
332 ftrace_update_trampoline(ops);
335 update_ftrace_function();
340 int __unregister_ftrace_function(struct ftrace_ops *ops)
344 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
347 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
353 update_ftrace_function();
355 ops->func = ops->saved_func;
360 static void ftrace_update_pid_func(void)
362 struct ftrace_ops *op;
364 /* Only do something if we are tracing something */
365 if (ftrace_trace_function == ftrace_stub)
368 do_for_each_ftrace_op(op, ftrace_ops_list) {
369 if (op->flags & FTRACE_OPS_FL_PID) {
370 op->func = ftrace_pids_enabled(op) ?
371 ftrace_pid_func : op->saved_func;
372 ftrace_update_trampoline(op);
374 } while_for_each_ftrace_op(op);
376 update_ftrace_function();
379 #ifdef CONFIG_FUNCTION_PROFILER
380 struct ftrace_profile {
381 struct hlist_node node;
383 unsigned long counter;
384 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
385 unsigned long long time;
386 unsigned long long time_squared;
390 struct ftrace_profile_page {
391 struct ftrace_profile_page *next;
393 struct ftrace_profile records[];
396 struct ftrace_profile_stat {
398 struct hlist_head *hash;
399 struct ftrace_profile_page *pages;
400 struct ftrace_profile_page *start;
401 struct tracer_stat stat;
404 #define PROFILE_RECORDS_SIZE \
405 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
407 #define PROFILES_PER_PAGE \
408 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
410 static int ftrace_profile_enabled __read_mostly;
412 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
413 static DEFINE_MUTEX(ftrace_profile_lock);
415 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
417 #define FTRACE_PROFILE_HASH_BITS 10
418 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
421 function_stat_next(void *v, int idx)
423 struct ftrace_profile *rec = v;
424 struct ftrace_profile_page *pg;
426 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
432 if ((void *)rec >= (void *)&pg->records[pg->index]) {
436 rec = &pg->records[0];
444 static void *function_stat_start(struct tracer_stat *trace)
446 struct ftrace_profile_stat *stat =
447 container_of(trace, struct ftrace_profile_stat, stat);
449 if (!stat || !stat->start)
452 return function_stat_next(&stat->start->records[0], 0);
455 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
456 /* function graph compares on total time */
457 static int function_stat_cmp(const void *p1, const void *p2)
459 const struct ftrace_profile *a = p1;
460 const struct ftrace_profile *b = p2;
462 if (a->time < b->time)
464 if (a->time > b->time)
470 /* not function graph compares against hits */
471 static int function_stat_cmp(const void *p1, const void *p2)
473 const struct ftrace_profile *a = p1;
474 const struct ftrace_profile *b = p2;
476 if (a->counter < b->counter)
478 if (a->counter > b->counter)
485 static int function_stat_headers(struct seq_file *m)
487 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
488 seq_puts(m, " Function "
491 "--- ---- --- ---\n");
493 seq_puts(m, " Function Hit\n"
499 static int function_stat_show(struct seq_file *m, void *v)
501 struct ftrace_profile *rec = v;
502 char str[KSYM_SYMBOL_LEN];
504 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
505 static struct trace_seq s;
506 unsigned long long avg;
507 unsigned long long stddev;
509 mutex_lock(&ftrace_profile_lock);
511 /* we raced with function_profile_reset() */
512 if (unlikely(rec->counter == 0)) {
517 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
518 avg = div64_ul(rec->time, rec->counter);
519 if (tracing_thresh && (avg < tracing_thresh))
523 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
524 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
526 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
529 /* Sample standard deviation (s^2) */
530 if (rec->counter <= 1)
534 * Apply Welford's method:
535 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
537 stddev = rec->counter * rec->time_squared -
538 rec->time * rec->time;
541 * Divide only 1000 for ns^2 -> us^2 conversion.
542 * trace_print_graph_duration will divide 1000 again.
544 stddev = div64_ul(stddev,
545 rec->counter * (rec->counter - 1) * 1000);
549 trace_print_graph_duration(rec->time, &s);
550 trace_seq_puts(&s, " ");
551 trace_print_graph_duration(avg, &s);
552 trace_seq_puts(&s, " ");
553 trace_print_graph_duration(stddev, &s);
554 trace_print_seq(m, &s);
558 mutex_unlock(&ftrace_profile_lock);
563 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
565 struct ftrace_profile_page *pg;
567 pg = stat->pages = stat->start;
570 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
575 memset(stat->hash, 0,
576 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
579 static int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
581 struct ftrace_profile_page *pg;
586 /* If we already allocated, do nothing */
590 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
594 #ifdef CONFIG_DYNAMIC_FTRACE
595 functions = ftrace_update_tot_cnt;
598 * We do not know the number of functions that exist because
599 * dynamic tracing is what counts them. With past experience
600 * we have around 20K functions. That should be more than enough.
601 * It is highly unlikely we will execute every function in
607 pg = stat->start = stat->pages;
609 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
611 for (i = 1; i < pages; i++) {
612 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
623 unsigned long tmp = (unsigned long)pg;
635 static int ftrace_profile_init_cpu(int cpu)
637 struct ftrace_profile_stat *stat;
640 stat = &per_cpu(ftrace_profile_stats, cpu);
643 /* If the profile is already created, simply reset it */
644 ftrace_profile_reset(stat);
649 * We are profiling all functions, but usually only a few thousand
650 * functions are hit. We'll make a hash of 1024 items.
652 size = FTRACE_PROFILE_HASH_SIZE;
654 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
659 /* Preallocate the function profiling pages */
660 if (ftrace_profile_pages_init(stat) < 0) {
669 static int ftrace_profile_init(void)
674 for_each_possible_cpu(cpu) {
675 ret = ftrace_profile_init_cpu(cpu);
683 /* interrupts must be disabled */
684 static struct ftrace_profile *
685 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
687 struct ftrace_profile *rec;
688 struct hlist_head *hhd;
691 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
692 hhd = &stat->hash[key];
694 if (hlist_empty(hhd))
697 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
705 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
706 struct ftrace_profile *rec)
710 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
711 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
715 * The memory is already allocated, this simply finds a new record to use.
717 static struct ftrace_profile *
718 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
720 struct ftrace_profile *rec = NULL;
722 /* prevent recursion (from NMIs) */
723 if (atomic_inc_return(&stat->disabled) != 1)
727 * Try to find the function again since an NMI
728 * could have added it
730 rec = ftrace_find_profiled_func(stat, ip);
734 if (stat->pages->index == PROFILES_PER_PAGE) {
735 if (!stat->pages->next)
737 stat->pages = stat->pages->next;
740 rec = &stat->pages->records[stat->pages->index++];
742 ftrace_add_profile(stat, rec);
745 atomic_dec(&stat->disabled);
751 function_profile_call(unsigned long ip, unsigned long parent_ip,
752 struct ftrace_ops *ops, struct ftrace_regs *fregs)
754 struct ftrace_profile_stat *stat;
755 struct ftrace_profile *rec;
758 if (!ftrace_profile_enabled)
761 local_irq_save(flags);
763 stat = this_cpu_ptr(&ftrace_profile_stats);
764 if (!stat->hash || !ftrace_profile_enabled)
767 rec = ftrace_find_profiled_func(stat, ip);
769 rec = ftrace_profile_alloc(stat, ip);
776 local_irq_restore(flags);
779 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
780 static bool fgraph_graph_time = true;
782 void ftrace_graph_graph_time_control(bool enable)
784 fgraph_graph_time = enable;
787 static int profile_graph_entry(struct ftrace_graph_ent *trace)
789 struct ftrace_ret_stack *ret_stack;
791 function_profile_call(trace->func, 0, NULL, NULL);
793 /* If function graph is shutting down, ret_stack can be NULL */
794 if (!current->ret_stack)
797 ret_stack = ftrace_graph_get_ret_stack(current, 0);
799 ret_stack->subtime = 0;
804 static void profile_graph_return(struct ftrace_graph_ret *trace)
806 struct ftrace_ret_stack *ret_stack;
807 struct ftrace_profile_stat *stat;
808 unsigned long long calltime;
809 struct ftrace_profile *rec;
812 local_irq_save(flags);
813 stat = this_cpu_ptr(&ftrace_profile_stats);
814 if (!stat->hash || !ftrace_profile_enabled)
817 /* If the calltime was zero'd ignore it */
818 if (!trace->calltime)
821 calltime = trace->rettime - trace->calltime;
823 if (!fgraph_graph_time) {
825 /* Append this call time to the parent time to subtract */
826 ret_stack = ftrace_graph_get_ret_stack(current, 1);
828 ret_stack->subtime += calltime;
830 ret_stack = ftrace_graph_get_ret_stack(current, 0);
831 if (ret_stack && ret_stack->subtime < calltime)
832 calltime -= ret_stack->subtime;
837 rec = ftrace_find_profiled_func(stat, trace->func);
839 rec->time += calltime;
840 rec->time_squared += calltime * calltime;
844 local_irq_restore(flags);
847 static struct fgraph_ops fprofiler_ops = {
848 .entryfunc = &profile_graph_entry,
849 .retfunc = &profile_graph_return,
852 static int register_ftrace_profiler(void)
854 return register_ftrace_graph(&fprofiler_ops);
857 static void unregister_ftrace_profiler(void)
859 unregister_ftrace_graph(&fprofiler_ops);
862 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
863 .func = function_profile_call,
864 .flags = FTRACE_OPS_FL_INITIALIZED,
865 INIT_OPS_HASH(ftrace_profile_ops)
868 static int register_ftrace_profiler(void)
870 return register_ftrace_function(&ftrace_profile_ops);
873 static void unregister_ftrace_profiler(void)
875 unregister_ftrace_function(&ftrace_profile_ops);
877 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
880 ftrace_profile_write(struct file *filp, const char __user *ubuf,
881 size_t cnt, loff_t *ppos)
886 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
892 mutex_lock(&ftrace_profile_lock);
893 if (ftrace_profile_enabled ^ val) {
895 ret = ftrace_profile_init();
901 ret = register_ftrace_profiler();
906 ftrace_profile_enabled = 1;
908 ftrace_profile_enabled = 0;
910 * unregister_ftrace_profiler calls stop_machine
911 * so this acts like an synchronize_rcu.
913 unregister_ftrace_profiler();
917 mutex_unlock(&ftrace_profile_lock);
925 ftrace_profile_read(struct file *filp, char __user *ubuf,
926 size_t cnt, loff_t *ppos)
928 char buf[64]; /* big enough to hold a number */
931 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
932 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
935 static const struct file_operations ftrace_profile_fops = {
936 .open = tracing_open_generic,
937 .read = ftrace_profile_read,
938 .write = ftrace_profile_write,
939 .llseek = default_llseek,
942 /* used to initialize the real stat files */
943 static struct tracer_stat function_stats __initdata = {
945 .stat_start = function_stat_start,
946 .stat_next = function_stat_next,
947 .stat_cmp = function_stat_cmp,
948 .stat_headers = function_stat_headers,
949 .stat_show = function_stat_show
952 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
954 struct ftrace_profile_stat *stat;
955 struct dentry *entry;
960 for_each_possible_cpu(cpu) {
961 stat = &per_cpu(ftrace_profile_stats, cpu);
963 name = kasprintf(GFP_KERNEL, "function%d", cpu);
966 * The files created are permanent, if something happens
967 * we still do not free memory.
970 "Could not allocate stat file for cpu %d\n",
974 stat->stat = function_stats;
975 stat->stat.name = name;
976 ret = register_stat_tracer(&stat->stat);
979 "Could not register function stat for cpu %d\n",
986 entry = tracefs_create_file("function_profile_enabled",
987 TRACE_MODE_WRITE, d_tracer, NULL,
988 &ftrace_profile_fops);
990 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
993 #else /* CONFIG_FUNCTION_PROFILER */
994 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
997 #endif /* CONFIG_FUNCTION_PROFILER */
999 #ifdef CONFIG_DYNAMIC_FTRACE
1001 static struct ftrace_ops *removed_ops;
1004 * Set when doing a global update, like enabling all recs or disabling them.
1005 * It is not set when just updating a single ftrace_ops.
1007 static bool update_all_ops;
1009 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1010 # error Dynamic ftrace depends on MCOUNT_RECORD
1013 struct ftrace_func_probe {
1014 struct ftrace_probe_ops *probe_ops;
1015 struct ftrace_ops ops;
1016 struct trace_array *tr;
1017 struct list_head list;
1023 * We make these constant because no one should touch them,
1024 * but they are used as the default "empty hash", to avoid allocating
1025 * it all the time. These are in a read only section such that if
1026 * anyone does try to modify it, it will cause an exception.
1028 static const struct hlist_head empty_buckets[1];
1029 static const struct ftrace_hash empty_hash = {
1030 .buckets = (struct hlist_head *)empty_buckets,
1032 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1034 struct ftrace_ops global_ops = {
1035 .func = ftrace_stub,
1036 .local_hash.notrace_hash = EMPTY_HASH,
1037 .local_hash.filter_hash = EMPTY_HASH,
1038 INIT_OPS_HASH(global_ops)
1039 .flags = FTRACE_OPS_FL_INITIALIZED |
1044 * Used by the stack unwinder to know about dynamic ftrace trampolines.
1046 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1048 struct ftrace_ops *op = NULL;
1051 * Some of the ops may be dynamically allocated,
1052 * they are freed after a synchronize_rcu().
1054 preempt_disable_notrace();
1056 do_for_each_ftrace_op(op, ftrace_ops_list) {
1058 * This is to check for dynamically allocated trampolines.
1059 * Trampolines that are in kernel text will have
1060 * core_kernel_text() return true.
1062 if (op->trampoline && op->trampoline_size)
1063 if (addr >= op->trampoline &&
1064 addr < op->trampoline + op->trampoline_size) {
1065 preempt_enable_notrace();
1068 } while_for_each_ftrace_op(op);
1069 preempt_enable_notrace();
1075 * This is used by __kernel_text_address() to return true if the
1076 * address is on a dynamically allocated trampoline that would
1077 * not return true for either core_kernel_text() or
1078 * is_module_text_address().
1080 bool is_ftrace_trampoline(unsigned long addr)
1082 return ftrace_ops_trampoline(addr) != NULL;
1085 struct ftrace_page {
1086 struct ftrace_page *next;
1087 struct dyn_ftrace *records;
1092 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1093 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1095 static struct ftrace_page *ftrace_pages_start;
1096 static struct ftrace_page *ftrace_pages;
1098 static __always_inline unsigned long
1099 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1101 if (hash->size_bits > 0)
1102 return hash_long(ip, hash->size_bits);
1107 /* Only use this function if ftrace_hash_empty() has already been tested */
1108 static __always_inline struct ftrace_func_entry *
1109 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1112 struct ftrace_func_entry *entry;
1113 struct hlist_head *hhd;
1115 key = ftrace_hash_key(hash, ip);
1116 hhd = &hash->buckets[key];
1118 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1119 if (entry->ip == ip)
1126 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1127 * @hash: The hash to look at
1128 * @ip: The instruction pointer to test
1130 * Search a given @hash to see if a given instruction pointer (@ip)
1133 * Returns the entry that holds the @ip if found. NULL otherwise.
1135 struct ftrace_func_entry *
1136 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1138 if (ftrace_hash_empty(hash))
1141 return __ftrace_lookup_ip(hash, ip);
1144 static void __add_hash_entry(struct ftrace_hash *hash,
1145 struct ftrace_func_entry *entry)
1147 struct hlist_head *hhd;
1150 key = ftrace_hash_key(hash, entry->ip);
1151 hhd = &hash->buckets[key];
1152 hlist_add_head(&entry->hlist, hhd);
1156 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1158 struct ftrace_func_entry *entry;
1160 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1165 __add_hash_entry(hash, entry);
1171 free_hash_entry(struct ftrace_hash *hash,
1172 struct ftrace_func_entry *entry)
1174 hlist_del(&entry->hlist);
1180 remove_hash_entry(struct ftrace_hash *hash,
1181 struct ftrace_func_entry *entry)
1183 hlist_del_rcu(&entry->hlist);
1187 static void ftrace_hash_clear(struct ftrace_hash *hash)
1189 struct hlist_head *hhd;
1190 struct hlist_node *tn;
1191 struct ftrace_func_entry *entry;
1192 int size = 1 << hash->size_bits;
1198 for (i = 0; i < size; i++) {
1199 hhd = &hash->buckets[i];
1200 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1201 free_hash_entry(hash, entry);
1203 FTRACE_WARN_ON(hash->count);
1206 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1208 list_del(&ftrace_mod->list);
1209 kfree(ftrace_mod->module);
1210 kfree(ftrace_mod->func);
1214 static void clear_ftrace_mod_list(struct list_head *head)
1216 struct ftrace_mod_load *p, *n;
1218 /* stack tracer isn't supported yet */
1222 mutex_lock(&ftrace_lock);
1223 list_for_each_entry_safe(p, n, head, list)
1225 mutex_unlock(&ftrace_lock);
1228 static void free_ftrace_hash(struct ftrace_hash *hash)
1230 if (!hash || hash == EMPTY_HASH)
1232 ftrace_hash_clear(hash);
1233 kfree(hash->buckets);
1237 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1239 struct ftrace_hash *hash;
1241 hash = container_of(rcu, struct ftrace_hash, rcu);
1242 free_ftrace_hash(hash);
1245 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1247 if (!hash || hash == EMPTY_HASH)
1249 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
1252 void ftrace_free_filter(struct ftrace_ops *ops)
1254 ftrace_ops_init(ops);
1255 free_ftrace_hash(ops->func_hash->filter_hash);
1256 free_ftrace_hash(ops->func_hash->notrace_hash);
1259 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1261 struct ftrace_hash *hash;
1264 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1268 size = 1 << size_bits;
1269 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1271 if (!hash->buckets) {
1276 hash->size_bits = size_bits;
1282 static int ftrace_add_mod(struct trace_array *tr,
1283 const char *func, const char *module,
1286 struct ftrace_mod_load *ftrace_mod;
1287 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1289 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1293 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1294 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1295 ftrace_mod->enable = enable;
1297 if (!ftrace_mod->func || !ftrace_mod->module)
1300 list_add(&ftrace_mod->list, mod_head);
1305 free_ftrace_mod(ftrace_mod);
1310 static struct ftrace_hash *
1311 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1313 struct ftrace_func_entry *entry;
1314 struct ftrace_hash *new_hash;
1319 new_hash = alloc_ftrace_hash(size_bits);
1324 new_hash->flags = hash->flags;
1327 if (ftrace_hash_empty(hash))
1330 size = 1 << hash->size_bits;
1331 for (i = 0; i < size; i++) {
1332 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1333 ret = add_hash_entry(new_hash, entry->ip);
1339 FTRACE_WARN_ON(new_hash->count != hash->count);
1344 free_ftrace_hash(new_hash);
1349 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1351 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1353 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1354 struct ftrace_hash *new_hash);
1356 static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
1358 struct ftrace_func_entry *entry;
1359 struct ftrace_hash *new_hash;
1360 struct hlist_head *hhd;
1361 struct hlist_node *tn;
1366 * Use around half the size (max bit of it), but
1367 * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits).
1369 bits = fls(size / 2);
1371 /* Don't allocate too much */
1372 if (bits > FTRACE_HASH_MAX_BITS)
1373 bits = FTRACE_HASH_MAX_BITS;
1375 new_hash = alloc_ftrace_hash(bits);
1379 new_hash->flags = src->flags;
1381 size = 1 << src->size_bits;
1382 for (i = 0; i < size; i++) {
1383 hhd = &src->buckets[i];
1384 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1385 remove_hash_entry(src, entry);
1386 __add_hash_entry(new_hash, entry);
1392 static struct ftrace_hash *
1393 __ftrace_hash_move(struct ftrace_hash *src)
1395 int size = src->count;
1398 * If the new source is empty, just return the empty_hash.
1400 if (ftrace_hash_empty(src))
1403 return dup_hash(src, size);
1407 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1408 struct ftrace_hash **dst, struct ftrace_hash *src)
1410 struct ftrace_hash *new_hash;
1413 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1414 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1417 new_hash = __ftrace_hash_move(src);
1421 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1423 /* IPMODIFY should be updated only when filter_hash updating */
1424 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1426 free_ftrace_hash(new_hash);
1432 * Remove the current set, update the hash and add
1435 ftrace_hash_rec_disable_modify(ops, enable);
1437 rcu_assign_pointer(*dst, new_hash);
1439 ftrace_hash_rec_enable_modify(ops, enable);
1444 static bool hash_contains_ip(unsigned long ip,
1445 struct ftrace_ops_hash *hash)
1448 * The function record is a match if it exists in the filter
1449 * hash and not in the notrace hash. Note, an empty hash is
1450 * considered a match for the filter hash, but an empty
1451 * notrace hash is considered not in the notrace hash.
1453 return (ftrace_hash_empty(hash->filter_hash) ||
1454 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1455 (ftrace_hash_empty(hash->notrace_hash) ||
1456 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1460 * Test the hashes for this ops to see if we want to call
1461 * the ops->func or not.
1463 * It's a match if the ip is in the ops->filter_hash or
1464 * the filter_hash does not exist or is empty,
1466 * the ip is not in the ops->notrace_hash.
1468 * This needs to be called with preemption disabled as
1469 * the hashes are freed with call_rcu().
1472 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1474 struct ftrace_ops_hash hash;
1477 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1479 * There's a small race when adding ops that the ftrace handler
1480 * that wants regs, may be called without them. We can not
1481 * allow that handler to be called if regs is NULL.
1483 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1487 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1488 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1490 if (hash_contains_ip(ip, &hash))
1499 * This is a double for. Do not use 'break' to break out of the loop,
1500 * you must use a goto.
1502 #define do_for_each_ftrace_rec(pg, rec) \
1503 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1505 for (_____i = 0; _____i < pg->index; _____i++) { \
1506 rec = &pg->records[_____i];
1508 #define while_for_each_ftrace_rec() \
1513 static int ftrace_cmp_recs(const void *a, const void *b)
1515 const struct dyn_ftrace *key = a;
1516 const struct dyn_ftrace *rec = b;
1518 if (key->flags < rec->ip)
1520 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1525 static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
1527 struct ftrace_page *pg;
1528 struct dyn_ftrace *rec = NULL;
1529 struct dyn_ftrace key;
1532 key.flags = end; /* overload flags, as it is unsigned long */
1534 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1535 if (end < pg->records[0].ip ||
1536 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1538 rec = bsearch(&key, pg->records, pg->index,
1539 sizeof(struct dyn_ftrace),
1548 * ftrace_location_range - return the first address of a traced location
1549 * if it touches the given ip range
1550 * @start: start of range to search.
1551 * @end: end of range to search (inclusive). @end points to the last byte
1554 * Returns rec->ip if the related ftrace location is a least partly within
1555 * the given address range. That is, the first address of the instruction
1556 * that is either a NOP or call to the function tracer. It checks the ftrace
1557 * internal tables to determine if the address belongs or not.
1559 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1561 struct dyn_ftrace *rec;
1563 rec = lookup_rec(start, end);
1571 * ftrace_location - return true if the ip giving is a traced location
1572 * @ip: the instruction pointer to check
1574 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1575 * That is, the instruction that is either a NOP or call to
1576 * the function tracer. It checks the ftrace internal tables to
1577 * determine if the address belongs or not.
1579 unsigned long ftrace_location(unsigned long ip)
1581 return ftrace_location_range(ip, ip);
1585 * ftrace_text_reserved - return true if range contains an ftrace location
1586 * @start: start of range to search
1587 * @end: end of range to search (inclusive). @end points to the last byte to check.
1589 * Returns 1 if @start and @end contains a ftrace location.
1590 * That is, the instruction that is either a NOP or call to
1591 * the function tracer. It checks the ftrace internal tables to
1592 * determine if the address belongs or not.
1594 int ftrace_text_reserved(const void *start, const void *end)
1598 ret = ftrace_location_range((unsigned long)start,
1599 (unsigned long)end);
1604 /* Test if ops registered to this rec needs regs */
1605 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1607 struct ftrace_ops *ops;
1608 bool keep_regs = false;
1610 for (ops = ftrace_ops_list;
1611 ops != &ftrace_list_end; ops = ops->next) {
1612 /* pass rec in as regs to have non-NULL val */
1613 if (ftrace_ops_test(ops, rec->ip, rec)) {
1614 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1624 static struct ftrace_ops *
1625 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1626 static struct ftrace_ops *
1627 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1628 static struct ftrace_ops *
1629 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1631 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1635 struct ftrace_hash *hash;
1636 struct ftrace_hash *other_hash;
1637 struct ftrace_page *pg;
1638 struct dyn_ftrace *rec;
1639 bool update = false;
1643 /* Only update if the ops has been registered */
1644 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1648 * In the filter_hash case:
1649 * If the count is zero, we update all records.
1650 * Otherwise we just update the items in the hash.
1652 * In the notrace_hash case:
1653 * We enable the update in the hash.
1654 * As disabling notrace means enabling the tracing,
1655 * and enabling notrace means disabling, the inc variable
1659 hash = ops->func_hash->filter_hash;
1660 other_hash = ops->func_hash->notrace_hash;
1661 if (ftrace_hash_empty(hash))
1665 hash = ops->func_hash->notrace_hash;
1666 other_hash = ops->func_hash->filter_hash;
1668 * If the notrace hash has no items,
1669 * then there's nothing to do.
1671 if (ftrace_hash_empty(hash))
1675 do_for_each_ftrace_rec(pg, rec) {
1676 int in_other_hash = 0;
1680 if (rec->flags & FTRACE_FL_DISABLED)
1685 * Only the filter_hash affects all records.
1686 * Update if the record is not in the notrace hash.
1688 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1691 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1692 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1695 * If filter_hash is set, we want to match all functions
1696 * that are in the hash but not in the other hash.
1698 * If filter_hash is not set, then we are decrementing.
1699 * That means we match anything that is in the hash
1700 * and also in the other_hash. That is, we need to turn
1701 * off functions in the other hash because they are disabled
1704 if (filter_hash && in_hash && !in_other_hash)
1706 else if (!filter_hash && in_hash &&
1707 (in_other_hash || ftrace_hash_empty(other_hash)))
1715 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1718 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1719 rec->flags |= FTRACE_FL_DIRECT;
1722 * If there's only a single callback registered to a
1723 * function, and the ops has a trampoline registered
1724 * for it, then we can call it directly.
1726 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1727 rec->flags |= FTRACE_FL_TRAMP;
1730 * If we are adding another function callback
1731 * to this function, and the previous had a
1732 * custom trampoline in use, then we need to go
1733 * back to the default trampoline.
1735 rec->flags &= ~FTRACE_FL_TRAMP;
1738 * If any ops wants regs saved for this function
1739 * then all ops will get saved regs.
1741 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1742 rec->flags |= FTRACE_FL_REGS;
1744 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1749 * Only the internal direct_ops should have the
1750 * DIRECT flag set. Thus, if it is removing a
1751 * function, then that function should no longer
1754 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1755 rec->flags &= ~FTRACE_FL_DIRECT;
1758 * If the rec had REGS enabled and the ops that is
1759 * being removed had REGS set, then see if there is
1760 * still any ops for this record that wants regs.
1761 * If not, we can stop recording them.
1763 if (ftrace_rec_count(rec) > 0 &&
1764 rec->flags & FTRACE_FL_REGS &&
1765 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1766 if (!test_rec_ops_needs_regs(rec))
1767 rec->flags &= ~FTRACE_FL_REGS;
1771 * The TRAMP needs to be set only if rec count
1772 * is decremented to one, and the ops that is
1773 * left has a trampoline. As TRAMP can only be
1774 * enabled if there is only a single ops attached
1777 if (ftrace_rec_count(rec) == 1 &&
1778 ftrace_find_tramp_ops_any_other(rec, ops))
1779 rec->flags |= FTRACE_FL_TRAMP;
1781 rec->flags &= ~FTRACE_FL_TRAMP;
1784 * flags will be cleared in ftrace_check_record()
1785 * if rec count is zero.
1790 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1791 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
1793 /* Shortcut, if we handled all records, we are done. */
1794 if (!all && count == hash->count)
1796 } while_for_each_ftrace_rec();
1801 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1804 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1807 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1810 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1813 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1814 int filter_hash, int inc)
1816 struct ftrace_ops *op;
1818 __ftrace_hash_rec_update(ops, filter_hash, inc);
1820 if (ops->func_hash != &global_ops.local_hash)
1824 * If the ops shares the global_ops hash, then we need to update
1825 * all ops that are enabled and use this hash.
1827 do_for_each_ftrace_op(op, ftrace_ops_list) {
1831 if (op->func_hash == &global_ops.local_hash)
1832 __ftrace_hash_rec_update(op, filter_hash, inc);
1833 } while_for_each_ftrace_op(op);
1836 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1839 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1842 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1845 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1849 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1850 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1851 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1852 * Note that old_hash and new_hash has below meanings
1853 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1854 * - If the hash is EMPTY_HASH, it hits nothing
1855 * - Anything else hits the recs which match the hash entries.
1857 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1858 struct ftrace_hash *old_hash,
1859 struct ftrace_hash *new_hash)
1861 struct ftrace_page *pg;
1862 struct dyn_ftrace *rec, *end = NULL;
1865 /* Only update if the ops has been registered */
1866 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1869 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1873 * Since the IPMODIFY is a very address sensitive action, we do not
1874 * allow ftrace_ops to set all functions to new hash.
1876 if (!new_hash || !old_hash)
1879 /* Update rec->flags */
1880 do_for_each_ftrace_rec(pg, rec) {
1882 if (rec->flags & FTRACE_FL_DISABLED)
1885 /* We need to update only differences of filter_hash */
1886 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1887 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1888 if (in_old == in_new)
1892 /* New entries must ensure no others are using it */
1893 if (rec->flags & FTRACE_FL_IPMODIFY)
1895 rec->flags |= FTRACE_FL_IPMODIFY;
1896 } else /* Removed entry */
1897 rec->flags &= ~FTRACE_FL_IPMODIFY;
1898 } while_for_each_ftrace_rec();
1905 /* Roll back what we did above */
1906 do_for_each_ftrace_rec(pg, rec) {
1908 if (rec->flags & FTRACE_FL_DISABLED)
1914 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1915 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1916 if (in_old == in_new)
1920 rec->flags &= ~FTRACE_FL_IPMODIFY;
1922 rec->flags |= FTRACE_FL_IPMODIFY;
1923 } while_for_each_ftrace_rec();
1929 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1931 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1933 if (ftrace_hash_empty(hash))
1936 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1939 /* Disabling always succeeds */
1940 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1942 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1944 if (ftrace_hash_empty(hash))
1947 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1950 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1951 struct ftrace_hash *new_hash)
1953 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1955 if (ftrace_hash_empty(old_hash))
1958 if (ftrace_hash_empty(new_hash))
1961 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1964 static void print_ip_ins(const char *fmt, const unsigned char *p)
1966 char ins[MCOUNT_INSN_SIZE];
1969 if (copy_from_kernel_nofault(ins, p, MCOUNT_INSN_SIZE)) {
1970 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
1974 printk(KERN_CONT "%s", fmt);
1976 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1977 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
1980 enum ftrace_bug_type ftrace_bug_type;
1981 const void *ftrace_expected;
1983 static void print_bug_type(void)
1985 switch (ftrace_bug_type) {
1986 case FTRACE_BUG_UNKNOWN:
1988 case FTRACE_BUG_INIT:
1989 pr_info("Initializing ftrace call sites\n");
1991 case FTRACE_BUG_NOP:
1992 pr_info("Setting ftrace call site to NOP\n");
1994 case FTRACE_BUG_CALL:
1995 pr_info("Setting ftrace call site to call ftrace function\n");
1997 case FTRACE_BUG_UPDATE:
1998 pr_info("Updating ftrace call site to call a different ftrace function\n");
2004 * ftrace_bug - report and shutdown function tracer
2005 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2006 * @rec: The record that failed
2008 * The arch code that enables or disables the function tracing
2009 * can call ftrace_bug() when it has detected a problem in
2010 * modifying the code. @failed should be one of either:
2011 * EFAULT - if the problem happens on reading the @ip address
2012 * EINVAL - if what is read at @ip is not what was expected
2013 * EPERM - if the problem happens on writing to the @ip address
2015 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2017 unsigned long ip = rec ? rec->ip : 0;
2019 pr_info("------------[ ftrace bug ]------------\n");
2023 pr_info("ftrace faulted on modifying ");
2024 print_ip_sym(KERN_INFO, ip);
2027 pr_info("ftrace failed to modify ");
2028 print_ip_sym(KERN_INFO, ip);
2029 print_ip_ins(" actual: ", (unsigned char *)ip);
2031 if (ftrace_expected) {
2032 print_ip_ins(" expected: ", ftrace_expected);
2037 pr_info("ftrace faulted on writing ");
2038 print_ip_sym(KERN_INFO, ip);
2041 pr_info("ftrace faulted on unknown error ");
2042 print_ip_sym(KERN_INFO, ip);
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);
2068 FTRACE_WARN_ON_ONCE(1);
2071 static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
2073 unsigned long flag = 0UL;
2075 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2077 if (rec->flags & FTRACE_FL_DISABLED)
2078 return FTRACE_UPDATE_IGNORE;
2081 * If we are updating calls:
2083 * If the record has a ref count, then we need to enable it
2084 * because someone is using it.
2086 * Otherwise we make sure its disabled.
2088 * If we are disabling calls, then disable all records that
2091 if (enable && ftrace_rec_count(rec))
2092 flag = FTRACE_FL_ENABLED;
2095 * If enabling and the REGS flag does not match the REGS_EN, or
2096 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2097 * this record. Set flags to fail the compare against ENABLED.
2098 * Same for direct calls.
2101 if (!(rec->flags & FTRACE_FL_REGS) !=
2102 !(rec->flags & FTRACE_FL_REGS_EN))
2103 flag |= FTRACE_FL_REGS;
2105 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2106 !(rec->flags & FTRACE_FL_TRAMP_EN))
2107 flag |= FTRACE_FL_TRAMP;
2110 * Direct calls are special, as count matters.
2111 * We must test the record for direct, if the
2112 * DIRECT and DIRECT_EN do not match, but only
2113 * if the count is 1. That's because, if the
2114 * count is something other than one, we do not
2115 * want the direct enabled (it will be done via the
2116 * direct helper). But if DIRECT_EN is set, and
2117 * the count is not one, we need to clear it.
2119 if (ftrace_rec_count(rec) == 1) {
2120 if (!(rec->flags & FTRACE_FL_DIRECT) !=
2121 !(rec->flags & FTRACE_FL_DIRECT_EN))
2122 flag |= FTRACE_FL_DIRECT;
2123 } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
2124 flag |= FTRACE_FL_DIRECT;
2128 /* If the state of this record hasn't changed, then do nothing */
2129 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2130 return FTRACE_UPDATE_IGNORE;
2133 /* Save off if rec is being enabled (for return value) */
2134 flag ^= rec->flags & FTRACE_FL_ENABLED;
2137 rec->flags |= FTRACE_FL_ENABLED;
2138 if (flag & FTRACE_FL_REGS) {
2139 if (rec->flags & FTRACE_FL_REGS)
2140 rec->flags |= FTRACE_FL_REGS_EN;
2142 rec->flags &= ~FTRACE_FL_REGS_EN;
2144 if (flag & FTRACE_FL_TRAMP) {
2145 if (rec->flags & FTRACE_FL_TRAMP)
2146 rec->flags |= FTRACE_FL_TRAMP_EN;
2148 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2151 if (flag & FTRACE_FL_DIRECT) {
2153 * If there's only one user (direct_ops helper)
2154 * then we can call the direct function
2155 * directly (no ftrace trampoline).
2157 if (ftrace_rec_count(rec) == 1) {
2158 if (rec->flags & FTRACE_FL_DIRECT)
2159 rec->flags |= FTRACE_FL_DIRECT_EN;
2161 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2164 * Can only call directly if there's
2165 * only one callback to the function.
2167 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2173 * If this record is being updated from a nop, then
2174 * return UPDATE_MAKE_CALL.
2176 * return UPDATE_MODIFY_CALL to tell the caller to convert
2177 * from the save regs, to a non-save regs function or
2178 * vice versa, or from a trampoline call.
2180 if (flag & FTRACE_FL_ENABLED) {
2181 ftrace_bug_type = FTRACE_BUG_CALL;
2182 return FTRACE_UPDATE_MAKE_CALL;
2185 ftrace_bug_type = FTRACE_BUG_UPDATE;
2186 return FTRACE_UPDATE_MODIFY_CALL;
2190 /* If there's no more users, clear all flags */
2191 if (!ftrace_rec_count(rec))
2195 * Just disable the record, but keep the ops TRAMP
2196 * and REGS states. The _EN flags must be disabled though.
2198 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2199 FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN);
2202 ftrace_bug_type = FTRACE_BUG_NOP;
2203 return FTRACE_UPDATE_MAKE_NOP;
2207 * ftrace_update_record - set a record that now is tracing or not
2208 * @rec: the record to update
2209 * @enable: set to true if the record is tracing, false to force disable
2211 * The records that represent all functions that can be traced need
2212 * to be updated when tracing has been enabled.
2214 int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
2216 return ftrace_check_record(rec, enable, true);
2220 * ftrace_test_record - check if the record has been enabled or not
2221 * @rec: the record to test
2222 * @enable: set to true to check if enabled, false if it is disabled
2224 * The arch code may need to test if a record is already set to
2225 * tracing to determine how to modify the function code that it
2228 int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
2230 return ftrace_check_record(rec, enable, false);
2233 static struct ftrace_ops *
2234 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2236 struct ftrace_ops *op;
2237 unsigned long ip = rec->ip;
2239 do_for_each_ftrace_op(op, ftrace_ops_list) {
2241 if (!op->trampoline)
2244 if (hash_contains_ip(ip, op->func_hash))
2246 } while_for_each_ftrace_op(op);
2251 static struct ftrace_ops *
2252 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2254 struct ftrace_ops *op;
2255 unsigned long ip = rec->ip;
2257 do_for_each_ftrace_op(op, ftrace_ops_list) {
2259 if (op == op_exclude || !op->trampoline)
2262 if (hash_contains_ip(ip, op->func_hash))
2264 } while_for_each_ftrace_op(op);
2269 static struct ftrace_ops *
2270 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2271 struct ftrace_ops *op)
2273 unsigned long ip = rec->ip;
2275 while_for_each_ftrace_op(op) {
2277 if (!op->trampoline)
2280 if (hash_contains_ip(ip, op->func_hash))
2287 static struct ftrace_ops *
2288 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2290 struct ftrace_ops *op;
2291 unsigned long ip = rec->ip;
2294 * Need to check removed ops first.
2295 * If they are being removed, and this rec has a tramp,
2296 * and this rec is in the ops list, then it would be the
2297 * one with the tramp.
2300 if (hash_contains_ip(ip, &removed_ops->old_hash))
2305 * Need to find the current trampoline for a rec.
2306 * Now, a trampoline is only attached to a rec if there
2307 * was a single 'ops' attached to it. But this can be called
2308 * when we are adding another op to the rec or removing the
2309 * current one. Thus, if the op is being added, we can
2310 * ignore it because it hasn't attached itself to the rec
2313 * If an ops is being modified (hooking to different functions)
2314 * then we don't care about the new functions that are being
2315 * added, just the old ones (that are probably being removed).
2317 * If we are adding an ops to a function that already is using
2318 * a trampoline, it needs to be removed (trampolines are only
2319 * for single ops connected), then an ops that is not being
2320 * modified also needs to be checked.
2322 do_for_each_ftrace_op(op, ftrace_ops_list) {
2324 if (!op->trampoline)
2328 * If the ops is being added, it hasn't gotten to
2329 * the point to be removed from this tree yet.
2331 if (op->flags & FTRACE_OPS_FL_ADDING)
2336 * If the ops is being modified and is in the old
2337 * hash, then it is probably being removed from this
2340 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2341 hash_contains_ip(ip, &op->old_hash))
2344 * If the ops is not being added or modified, and it's
2345 * in its normal filter hash, then this must be the one
2348 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2349 hash_contains_ip(ip, op->func_hash))
2352 } while_for_each_ftrace_op(op);
2357 static struct ftrace_ops *
2358 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2360 struct ftrace_ops *op;
2361 unsigned long ip = rec->ip;
2363 do_for_each_ftrace_op(op, ftrace_ops_list) {
2364 /* pass rec in as regs to have non-NULL val */
2365 if (hash_contains_ip(ip, op->func_hash))
2367 } while_for_each_ftrace_op(op);
2372 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2373 /* Protected by rcu_tasks for reading, and direct_mutex for writing */
2374 static struct ftrace_hash *direct_functions = EMPTY_HASH;
2375 static DEFINE_MUTEX(direct_mutex);
2376 int ftrace_direct_func_count;
2379 * Search the direct_functions hash to see if the given instruction pointer
2380 * has a direct caller attached to it.
2382 unsigned long ftrace_find_rec_direct(unsigned long ip)
2384 struct ftrace_func_entry *entry;
2386 entry = __ftrace_lookup_ip(direct_functions, ip);
2390 return entry->direct;
2393 static struct ftrace_func_entry*
2394 ftrace_add_rec_direct(unsigned long ip, unsigned long addr,
2395 struct ftrace_hash **free_hash)
2397 struct ftrace_func_entry *entry;
2399 if (ftrace_hash_empty(direct_functions) ||
2400 direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
2401 struct ftrace_hash *new_hash;
2402 int size = ftrace_hash_empty(direct_functions) ? 0 :
2403 direct_functions->count + 1;
2408 new_hash = dup_hash(direct_functions, size);
2412 *free_hash = direct_functions;
2413 direct_functions = new_hash;
2416 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2421 entry->direct = addr;
2422 __add_hash_entry(direct_functions, entry);
2426 static void call_direct_funcs(unsigned long ip, unsigned long pip,
2427 struct ftrace_ops *ops, struct ftrace_regs *fregs)
2429 struct pt_regs *regs = ftrace_get_regs(fregs);
2432 addr = ftrace_find_rec_direct(ip);
2436 arch_ftrace_set_direct_caller(regs, addr);
2439 struct ftrace_ops direct_ops = {
2440 .func = call_direct_funcs,
2441 .flags = FTRACE_OPS_FL_IPMODIFY
2442 | FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS
2443 | FTRACE_OPS_FL_PERMANENT,
2445 * By declaring the main trampoline as this trampoline
2446 * it will never have one allocated for it. Allocated
2447 * trampolines should not call direct functions.
2448 * The direct_ops should only be called by the builtin
2449 * ftrace_regs_caller trampoline.
2451 .trampoline = FTRACE_REGS_ADDR,
2453 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2456 * ftrace_get_addr_new - Get the call address to set to
2457 * @rec: The ftrace record descriptor
2459 * If the record has the FTRACE_FL_REGS set, that means that it
2460 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2461 * is not set, then it wants to convert to the normal callback.
2463 * Returns the address of the trampoline to set to
2465 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2467 struct ftrace_ops *ops;
2470 if ((rec->flags & FTRACE_FL_DIRECT) &&
2471 (ftrace_rec_count(rec) == 1)) {
2472 addr = ftrace_find_rec_direct(rec->ip);
2478 /* Trampolines take precedence over regs */
2479 if (rec->flags & FTRACE_FL_TRAMP) {
2480 ops = ftrace_find_tramp_ops_new(rec);
2481 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2482 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2483 (void *)rec->ip, (void *)rec->ip, rec->flags);
2484 /* Ftrace is shutting down, return anything */
2485 return (unsigned long)FTRACE_ADDR;
2487 return ops->trampoline;
2490 if (rec->flags & FTRACE_FL_REGS)
2491 return (unsigned long)FTRACE_REGS_ADDR;
2493 return (unsigned long)FTRACE_ADDR;
2497 * ftrace_get_addr_curr - Get the call address that is already there
2498 * @rec: The ftrace record descriptor
2500 * The FTRACE_FL_REGS_EN is set when the record already points to
2501 * a function that saves all the regs. Basically the '_EN' version
2502 * represents the current state of the function.
2504 * Returns the address of the trampoline that is currently being called
2506 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2508 struct ftrace_ops *ops;
2511 /* Direct calls take precedence over trampolines */
2512 if (rec->flags & FTRACE_FL_DIRECT_EN) {
2513 addr = ftrace_find_rec_direct(rec->ip);
2519 /* Trampolines take precedence over regs */
2520 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2521 ops = ftrace_find_tramp_ops_curr(rec);
2522 if (FTRACE_WARN_ON(!ops)) {
2523 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2524 (void *)rec->ip, (void *)rec->ip);
2525 /* Ftrace is shutting down, return anything */
2526 return (unsigned long)FTRACE_ADDR;
2528 return ops->trampoline;
2531 if (rec->flags & FTRACE_FL_REGS_EN)
2532 return (unsigned long)FTRACE_REGS_ADDR;
2534 return (unsigned long)FTRACE_ADDR;
2538 __ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
2540 unsigned long ftrace_old_addr;
2541 unsigned long ftrace_addr;
2544 ftrace_addr = ftrace_get_addr_new(rec);
2546 /* This needs to be done before we call ftrace_update_record */
2547 ftrace_old_addr = ftrace_get_addr_curr(rec);
2549 ret = ftrace_update_record(rec, enable);
2551 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2554 case FTRACE_UPDATE_IGNORE:
2557 case FTRACE_UPDATE_MAKE_CALL:
2558 ftrace_bug_type = FTRACE_BUG_CALL;
2559 return ftrace_make_call(rec, ftrace_addr);
2561 case FTRACE_UPDATE_MAKE_NOP:
2562 ftrace_bug_type = FTRACE_BUG_NOP;
2563 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2565 case FTRACE_UPDATE_MODIFY_CALL:
2566 ftrace_bug_type = FTRACE_BUG_UPDATE;
2567 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2570 return -1; /* unknown ftrace bug */
2573 void __weak ftrace_replace_code(int mod_flags)
2575 struct dyn_ftrace *rec;
2576 struct ftrace_page *pg;
2577 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2578 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
2581 if (unlikely(ftrace_disabled))
2584 do_for_each_ftrace_rec(pg, rec) {
2586 if (rec->flags & FTRACE_FL_DISABLED)
2589 failed = __ftrace_replace_code(rec, enable);
2591 ftrace_bug(failed, rec);
2592 /* Stop processing */
2597 } while_for_each_ftrace_rec();
2600 struct ftrace_rec_iter {
2601 struct ftrace_page *pg;
2606 * ftrace_rec_iter_start - start up iterating over traced functions
2608 * Returns an iterator handle that is used to iterate over all
2609 * the records that represent address locations where functions
2612 * May return NULL if no records are available.
2614 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2617 * We only use a single iterator.
2618 * Protected by the ftrace_lock mutex.
2620 static struct ftrace_rec_iter ftrace_rec_iter;
2621 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2623 iter->pg = ftrace_pages_start;
2626 /* Could have empty pages */
2627 while (iter->pg && !iter->pg->index)
2628 iter->pg = iter->pg->next;
2637 * ftrace_rec_iter_next - get the next record to process.
2638 * @iter: The handle to the iterator.
2640 * Returns the next iterator after the given iterator @iter.
2642 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2646 if (iter->index >= iter->pg->index) {
2647 iter->pg = iter->pg->next;
2650 /* Could have empty pages */
2651 while (iter->pg && !iter->pg->index)
2652 iter->pg = iter->pg->next;
2662 * ftrace_rec_iter_record - get the record at the iterator location
2663 * @iter: The current iterator location
2665 * Returns the record that the current @iter is at.
2667 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2669 return &iter->pg->records[iter->index];
2673 ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
2677 if (unlikely(ftrace_disabled))
2680 ret = ftrace_init_nop(mod, rec);
2682 ftrace_bug_type = FTRACE_BUG_INIT;
2683 ftrace_bug(ret, rec);
2690 * archs can override this function if they must do something
2691 * before the modifying code is performed.
2693 int __weak ftrace_arch_code_modify_prepare(void)
2699 * archs can override this function if they must do something
2700 * after the modifying code is performed.
2702 int __weak ftrace_arch_code_modify_post_process(void)
2707 void ftrace_modify_all_code(int command)
2709 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2713 if (command & FTRACE_MAY_SLEEP)
2714 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2717 * If the ftrace_caller calls a ftrace_ops func directly,
2718 * we need to make sure that it only traces functions it
2719 * expects to trace. When doing the switch of functions,
2720 * we need to update to the ftrace_ops_list_func first
2721 * before the transition between old and new calls are set,
2722 * as the ftrace_ops_list_func will check the ops hashes
2723 * to make sure the ops are having the right functions
2727 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2728 if (FTRACE_WARN_ON(err))
2732 if (command & FTRACE_UPDATE_CALLS)
2733 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
2734 else if (command & FTRACE_DISABLE_CALLS)
2735 ftrace_replace_code(mod_flags);
2737 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2738 function_trace_op = set_function_trace_op;
2740 /* If irqs are disabled, we are in stop machine */
2741 if (!irqs_disabled())
2742 smp_call_function(ftrace_sync_ipi, NULL, 1);
2743 err = ftrace_update_ftrace_func(ftrace_trace_function);
2744 if (FTRACE_WARN_ON(err))
2748 if (command & FTRACE_START_FUNC_RET)
2749 err = ftrace_enable_ftrace_graph_caller();
2750 else if (command & FTRACE_STOP_FUNC_RET)
2751 err = ftrace_disable_ftrace_graph_caller();
2752 FTRACE_WARN_ON(err);
2755 static int __ftrace_modify_code(void *data)
2757 int *command = data;
2759 ftrace_modify_all_code(*command);
2765 * ftrace_run_stop_machine - go back to the stop machine method
2766 * @command: The command to tell ftrace what to do
2768 * If an arch needs to fall back to the stop machine method, the
2769 * it can call this function.
2771 void ftrace_run_stop_machine(int command)
2773 stop_machine(__ftrace_modify_code, &command, NULL);
2777 * arch_ftrace_update_code - modify the code to trace or not trace
2778 * @command: The command that needs to be done
2780 * Archs can override this function if it does not need to
2781 * run stop_machine() to modify code.
2783 void __weak arch_ftrace_update_code(int command)
2785 ftrace_run_stop_machine(command);
2788 static void ftrace_run_update_code(int command)
2792 ret = ftrace_arch_code_modify_prepare();
2793 FTRACE_WARN_ON(ret);
2798 * By default we use stop_machine() to modify the code.
2799 * But archs can do what ever they want as long as it
2800 * is safe. The stop_machine() is the safest, but also
2801 * produces the most overhead.
2803 arch_ftrace_update_code(command);
2805 ret = ftrace_arch_code_modify_post_process();
2806 FTRACE_WARN_ON(ret);
2809 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2810 struct ftrace_ops_hash *old_hash)
2812 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2813 ops->old_hash.filter_hash = old_hash->filter_hash;
2814 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2815 ftrace_run_update_code(command);
2816 ops->old_hash.filter_hash = NULL;
2817 ops->old_hash.notrace_hash = NULL;
2818 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2821 static ftrace_func_t saved_ftrace_func;
2822 static int ftrace_start_up;
2824 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2828 /* List of trace_ops that have allocated trampolines */
2829 static LIST_HEAD(ftrace_ops_trampoline_list);
2831 static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops)
2833 lockdep_assert_held(&ftrace_lock);
2834 list_add_rcu(&ops->list, &ftrace_ops_trampoline_list);
2837 static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops)
2839 lockdep_assert_held(&ftrace_lock);
2840 list_del_rcu(&ops->list);
2845 * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
2846 * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
2849 #define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
2850 #define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
2852 static void ftrace_trampoline_free(struct ftrace_ops *ops)
2854 if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) &&
2857 * Record the text poke event before the ksymbol unregister
2860 perf_event_text_poke((void *)ops->trampoline,
2861 (void *)ops->trampoline,
2862 ops->trampoline_size, NULL, 0);
2863 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
2864 ops->trampoline, ops->trampoline_size,
2865 true, FTRACE_TRAMPOLINE_SYM);
2866 /* Remove from kallsyms after the perf events */
2867 ftrace_remove_trampoline_from_kallsyms(ops);
2870 arch_ftrace_trampoline_free(ops);
2873 static void ftrace_startup_enable(int command)
2875 if (saved_ftrace_func != ftrace_trace_function) {
2876 saved_ftrace_func = ftrace_trace_function;
2877 command |= FTRACE_UPDATE_TRACE_FUNC;
2880 if (!command || !ftrace_enabled)
2883 ftrace_run_update_code(command);
2886 static void ftrace_startup_all(int command)
2888 update_all_ops = true;
2889 ftrace_startup_enable(command);
2890 update_all_ops = false;
2893 int ftrace_startup(struct ftrace_ops *ops, int command)
2897 if (unlikely(ftrace_disabled))
2900 ret = __register_ftrace_function(ops);
2907 * Note that ftrace probes uses this to start up
2908 * and modify functions it will probe. But we still
2909 * set the ADDING flag for modification, as probes
2910 * do not have trampolines. If they add them in the
2911 * future, then the probes will need to distinguish
2912 * between adding and updating probes.
2914 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2916 ret = ftrace_hash_ipmodify_enable(ops);
2918 /* Rollback registration process */
2919 __unregister_ftrace_function(ops);
2921 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2922 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2923 ftrace_trampoline_free(ops);
2927 if (ftrace_hash_rec_enable(ops, 1))
2928 command |= FTRACE_UPDATE_CALLS;
2930 ftrace_startup_enable(command);
2932 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2937 int ftrace_shutdown(struct ftrace_ops *ops, int command)
2941 if (unlikely(ftrace_disabled))
2944 ret = __unregister_ftrace_function(ops);
2950 * Just warn in case of unbalance, no need to kill ftrace, it's not
2951 * critical but the ftrace_call callers may be never nopped again after
2952 * further ftrace uses.
2954 WARN_ON_ONCE(ftrace_start_up < 0);
2956 /* Disabling ipmodify never fails */
2957 ftrace_hash_ipmodify_disable(ops);
2959 if (ftrace_hash_rec_disable(ops, 1))
2960 command |= FTRACE_UPDATE_CALLS;
2962 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2964 if (saved_ftrace_func != ftrace_trace_function) {
2965 saved_ftrace_func = ftrace_trace_function;
2966 command |= FTRACE_UPDATE_TRACE_FUNC;
2969 if (!command || !ftrace_enabled) {
2971 * If these are dynamic or per_cpu ops, they still
2972 * need their data freed. Since, function tracing is
2973 * not currently active, we can just free them
2974 * without synchronizing all CPUs.
2976 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2983 * If the ops uses a trampoline, then it needs to be
2984 * tested first on update.
2986 ops->flags |= FTRACE_OPS_FL_REMOVING;
2989 /* The trampoline logic checks the old hashes */
2990 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2991 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2993 ftrace_run_update_code(command);
2996 * If there's no more ops registered with ftrace, run a
2997 * sanity check to make sure all rec flags are cleared.
2999 if (rcu_dereference_protected(ftrace_ops_list,
3000 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
3001 struct ftrace_page *pg;
3002 struct dyn_ftrace *rec;
3004 do_for_each_ftrace_rec(pg, rec) {
3005 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
3006 pr_warn(" %pS flags:%lx\n",
3007 (void *)rec->ip, rec->flags);
3008 } while_for_each_ftrace_rec();
3011 ops->old_hash.filter_hash = NULL;
3012 ops->old_hash.notrace_hash = NULL;
3015 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
3018 * Dynamic ops may be freed, we must make sure that all
3019 * callers are done before leaving this function.
3020 * The same goes for freeing the per_cpu data of the per_cpu
3023 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
3025 * We need to do a hard force of sched synchronization.
3026 * This is because we use preempt_disable() to do RCU, but
3027 * the function tracers can be called where RCU is not watching
3028 * (like before user_exit()). We can not rely on the RCU
3029 * infrastructure to do the synchronization, thus we must do it
3032 synchronize_rcu_tasks_rude();
3035 * When the kernel is preemptive, tasks can be preempted
3036 * while on a ftrace trampoline. Just scheduling a task on
3037 * a CPU is not good enough to flush them. Calling
3038 * synchronize_rcu_tasks() will wait for those tasks to
3039 * execute and either schedule voluntarily or enter user space.
3041 if (IS_ENABLED(CONFIG_PREEMPTION))
3042 synchronize_rcu_tasks();
3045 ftrace_trampoline_free(ops);
3051 static void ftrace_startup_sysctl(void)
3055 if (unlikely(ftrace_disabled))
3058 /* Force update next time */
3059 saved_ftrace_func = NULL;
3060 /* ftrace_start_up is true if we want ftrace running */
3061 if (ftrace_start_up) {
3062 command = FTRACE_UPDATE_CALLS;
3063 if (ftrace_graph_active)
3064 command |= FTRACE_START_FUNC_RET;
3065 ftrace_startup_enable(command);
3069 static void ftrace_shutdown_sysctl(void)
3073 if (unlikely(ftrace_disabled))
3076 /* ftrace_start_up is true if ftrace is running */
3077 if (ftrace_start_up) {
3078 command = FTRACE_DISABLE_CALLS;
3079 if (ftrace_graph_active)
3080 command |= FTRACE_STOP_FUNC_RET;
3081 ftrace_run_update_code(command);
3085 static u64 ftrace_update_time;
3086 unsigned long ftrace_update_tot_cnt;
3087 unsigned long ftrace_number_of_pages;
3088 unsigned long ftrace_number_of_groups;
3090 static inline int ops_traces_mod(struct ftrace_ops *ops)
3093 * Filter_hash being empty will default to trace module.
3094 * But notrace hash requires a test of individual module functions.
3096 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
3097 ftrace_hash_empty(ops->func_hash->notrace_hash);
3101 * Check if the current ops references the record.
3103 * If the ops traces all functions, then it was already accounted for.
3104 * If the ops does not trace the current record function, skip it.
3105 * If the ops ignores the function via notrace filter, skip it.
3108 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3110 /* If ops isn't enabled, ignore it */
3111 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
3114 /* If ops traces all then it includes this function */
3115 if (ops_traces_mod(ops))
3118 /* The function must be in the filter */
3119 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
3120 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
3123 /* If in notrace hash, we ignore it too */
3124 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
3130 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
3132 bool init_nop = ftrace_need_init_nop();
3133 struct ftrace_page *pg;
3134 struct dyn_ftrace *p;
3136 unsigned long update_cnt = 0;
3137 unsigned long rec_flags = 0;
3140 start = ftrace_now(raw_smp_processor_id());
3143 * When a module is loaded, this function is called to convert
3144 * the calls to mcount in its text to nops, and also to create
3145 * an entry in the ftrace data. Now, if ftrace is activated
3146 * after this call, but before the module sets its text to
3147 * read-only, the modification of enabling ftrace can fail if
3148 * the read-only is done while ftrace is converting the calls.
3149 * To prevent this, the module's records are set as disabled
3150 * and will be enabled after the call to set the module's text
3154 rec_flags |= FTRACE_FL_DISABLED;
3156 for (pg = new_pgs; pg; pg = pg->next) {
3158 for (i = 0; i < pg->index; i++) {
3160 /* If something went wrong, bail without enabling anything */
3161 if (unlikely(ftrace_disabled))
3164 p = &pg->records[i];
3165 p->flags = rec_flags;
3168 * Do the initial record conversion from mcount jump
3169 * to the NOP instructions.
3171 if (init_nop && !ftrace_nop_initialize(mod, p))
3178 stop = ftrace_now(raw_smp_processor_id());
3179 ftrace_update_time = stop - start;
3180 ftrace_update_tot_cnt += update_cnt;
3185 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3191 if (WARN_ON(!count))
3194 /* We want to fill as much as possible, with no empty pages */
3195 pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE);
3196 order = fls(pages) - 1;
3199 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3202 /* if we can't allocate this size, try something smaller */
3209 ftrace_number_of_pages += 1 << order;
3210 ftrace_number_of_groups++;
3212 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3221 static struct ftrace_page *
3222 ftrace_allocate_pages(unsigned long num_to_init)
3224 struct ftrace_page *start_pg;
3225 struct ftrace_page *pg;
3231 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3236 * Try to allocate as much as possible in one continues
3237 * location that fills in all of the space. We want to
3238 * waste as little space as possible.
3241 cnt = ftrace_allocate_records(pg, num_to_init);
3249 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3262 free_pages((unsigned long)pg->records, pg->order);
3263 ftrace_number_of_pages -= 1 << pg->order;
3265 start_pg = pg->next;
3268 ftrace_number_of_groups--;
3270 pr_info("ftrace: FAILED to allocate memory for functions\n");
3274 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3276 struct ftrace_iterator {
3280 struct ftrace_page *pg;
3281 struct dyn_ftrace *func;
3282 struct ftrace_func_probe *probe;
3283 struct ftrace_func_entry *probe_entry;
3284 struct trace_parser parser;
3285 struct ftrace_hash *hash;
3286 struct ftrace_ops *ops;
3287 struct trace_array *tr;
3288 struct list_head *mod_list;
3295 t_probe_next(struct seq_file *m, loff_t *pos)
3297 struct ftrace_iterator *iter = m->private;
3298 struct trace_array *tr = iter->ops->private;
3299 struct list_head *func_probes;
3300 struct ftrace_hash *hash;
3301 struct list_head *next;
3302 struct hlist_node *hnd = NULL;
3303 struct hlist_head *hhd;
3312 func_probes = &tr->func_probes;
3313 if (list_empty(func_probes))
3317 next = func_probes->next;
3318 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3321 if (iter->probe_entry)
3322 hnd = &iter->probe_entry->hlist;
3324 hash = iter->probe->ops.func_hash->filter_hash;
3327 * A probe being registered may temporarily have an empty hash
3328 * and it's at the end of the func_probes list.
3330 if (!hash || hash == EMPTY_HASH)
3333 size = 1 << hash->size_bits;
3336 if (iter->pidx >= size) {
3337 if (iter->probe->list.next == func_probes)
3339 next = iter->probe->list.next;
3340 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3341 hash = iter->probe->ops.func_hash->filter_hash;
3342 size = 1 << hash->size_bits;
3346 hhd = &hash->buckets[iter->pidx];
3348 if (hlist_empty(hhd)) {
3364 if (WARN_ON_ONCE(!hnd))
3367 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3372 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3374 struct ftrace_iterator *iter = m->private;
3378 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3381 if (iter->mod_pos > *pos)
3385 iter->probe_entry = NULL;
3387 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3388 p = t_probe_next(m, &l);
3395 /* Only set this if we have an item */
3396 iter->flags |= FTRACE_ITER_PROBE;
3402 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3404 struct ftrace_func_entry *probe_entry;
3405 struct ftrace_probe_ops *probe_ops;
3406 struct ftrace_func_probe *probe;
3408 probe = iter->probe;
3409 probe_entry = iter->probe_entry;
3411 if (WARN_ON_ONCE(!probe || !probe_entry))
3414 probe_ops = probe->probe_ops;
3416 if (probe_ops->print)
3417 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3419 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3420 (void *)probe_ops->func);
3426 t_mod_next(struct seq_file *m, loff_t *pos)
3428 struct ftrace_iterator *iter = m->private;
3429 struct trace_array *tr = iter->tr;
3434 iter->mod_list = iter->mod_list->next;
3436 if (iter->mod_list == &tr->mod_trace ||
3437 iter->mod_list == &tr->mod_notrace) {
3438 iter->flags &= ~FTRACE_ITER_MOD;
3442 iter->mod_pos = *pos;
3447 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3449 struct ftrace_iterator *iter = m->private;
3453 if (iter->func_pos > *pos)
3456 iter->mod_pos = iter->func_pos;
3458 /* probes are only available if tr is set */
3462 for (l = 0; l <= (*pos - iter->func_pos); ) {
3463 p = t_mod_next(m, &l);
3468 iter->flags &= ~FTRACE_ITER_MOD;
3469 return t_probe_start(m, pos);
3472 /* Only set this if we have an item */
3473 iter->flags |= FTRACE_ITER_MOD;
3479 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3481 struct ftrace_mod_load *ftrace_mod;
3482 struct trace_array *tr = iter->tr;
3484 if (WARN_ON_ONCE(!iter->mod_list) ||
3485 iter->mod_list == &tr->mod_trace ||
3486 iter->mod_list == &tr->mod_notrace)
3489 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3491 if (ftrace_mod->func)
3492 seq_printf(m, "%s", ftrace_mod->func);
3496 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3502 t_func_next(struct seq_file *m, loff_t *pos)
3504 struct ftrace_iterator *iter = m->private;
3505 struct dyn_ftrace *rec = NULL;
3510 if (iter->idx >= iter->pg->index) {
3511 if (iter->pg->next) {
3512 iter->pg = iter->pg->next;
3517 rec = &iter->pg->records[iter->idx++];
3518 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3519 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3521 ((iter->flags & FTRACE_ITER_ENABLED) &&
3522 !(rec->flags & FTRACE_FL_ENABLED))) {
3532 iter->pos = iter->func_pos = *pos;
3539 t_next(struct seq_file *m, void *v, loff_t *pos)
3541 struct ftrace_iterator *iter = m->private;
3542 loff_t l = *pos; /* t_probe_start() must use original pos */
3545 if (unlikely(ftrace_disabled))
3548 if (iter->flags & FTRACE_ITER_PROBE)
3549 return t_probe_next(m, pos);
3551 if (iter->flags & FTRACE_ITER_MOD)
3552 return t_mod_next(m, pos);
3554 if (iter->flags & FTRACE_ITER_PRINTALL) {
3555 /* next must increment pos, and t_probe_start does not */
3557 return t_mod_start(m, &l);
3560 ret = t_func_next(m, pos);
3563 return t_mod_start(m, &l);
3568 static void reset_iter_read(struct ftrace_iterator *iter)
3572 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3575 static void *t_start(struct seq_file *m, loff_t *pos)
3577 struct ftrace_iterator *iter = m->private;
3581 mutex_lock(&ftrace_lock);
3583 if (unlikely(ftrace_disabled))
3587 * If an lseek was done, then reset and start from beginning.
3589 if (*pos < iter->pos)
3590 reset_iter_read(iter);
3593 * For set_ftrace_filter reading, if we have the filter
3594 * off, we can short cut and just print out that all
3595 * functions are enabled.
3597 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3598 ftrace_hash_empty(iter->hash)) {
3599 iter->func_pos = 1; /* Account for the message */
3601 return t_mod_start(m, pos);
3602 iter->flags |= FTRACE_ITER_PRINTALL;
3603 /* reset in case of seek/pread */
3604 iter->flags &= ~FTRACE_ITER_PROBE;
3608 if (iter->flags & FTRACE_ITER_MOD)
3609 return t_mod_start(m, pos);
3612 * Unfortunately, we need to restart at ftrace_pages_start
3613 * every time we let go of the ftrace_mutex. This is because
3614 * those pointers can change without the lock.
3616 iter->pg = ftrace_pages_start;
3618 for (l = 0; l <= *pos; ) {
3619 p = t_func_next(m, &l);
3625 return t_mod_start(m, pos);
3630 static void t_stop(struct seq_file *m, void *p)
3632 mutex_unlock(&ftrace_lock);
3636 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3641 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3642 struct dyn_ftrace *rec)
3646 ptr = arch_ftrace_trampoline_func(ops, rec);
3648 seq_printf(m, " ->%pS", ptr);
3651 static int t_show(struct seq_file *m, void *v)
3653 struct ftrace_iterator *iter = m->private;
3654 struct dyn_ftrace *rec;
3656 if (iter->flags & FTRACE_ITER_PROBE)
3657 return t_probe_show(m, iter);
3659 if (iter->flags & FTRACE_ITER_MOD)
3660 return t_mod_show(m, iter);
3662 if (iter->flags & FTRACE_ITER_PRINTALL) {
3663 if (iter->flags & FTRACE_ITER_NOTRACE)
3664 seq_puts(m, "#### no functions disabled ####\n");
3666 seq_puts(m, "#### all functions enabled ####\n");
3675 seq_printf(m, "%ps", (void *)rec->ip);
3676 if (iter->flags & FTRACE_ITER_ENABLED) {
3677 struct ftrace_ops *ops;
3679 seq_printf(m, " (%ld)%s%s%s",
3680 ftrace_rec_count(rec),
3681 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3682 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
3683 rec->flags & FTRACE_FL_DIRECT ? " D" : " ");
3684 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3685 ops = ftrace_find_tramp_ops_any(rec);
3688 seq_printf(m, "\ttramp: %pS (%pS)",
3689 (void *)ops->trampoline,
3691 add_trampoline_func(m, ops, rec);
3692 ops = ftrace_find_tramp_ops_next(rec, ops);
3695 seq_puts(m, "\ttramp: ERROR!");
3697 add_trampoline_func(m, NULL, rec);
3699 if (rec->flags & FTRACE_FL_DIRECT) {
3700 unsigned long direct;
3702 direct = ftrace_find_rec_direct(rec->ip);
3704 seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3713 static const struct seq_operations show_ftrace_seq_ops = {
3721 ftrace_avail_open(struct inode *inode, struct file *file)
3723 struct ftrace_iterator *iter;
3726 ret = security_locked_down(LOCKDOWN_TRACEFS);
3730 if (unlikely(ftrace_disabled))
3733 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3737 iter->pg = ftrace_pages_start;
3738 iter->ops = &global_ops;
3744 ftrace_enabled_open(struct inode *inode, struct file *file)
3746 struct ftrace_iterator *iter;
3749 * This shows us what functions are currently being
3750 * traced and by what. Not sure if we want lockdown
3751 * to hide such critical information for an admin.
3752 * Although, perhaps it can show information we don't
3753 * want people to see, but if something is tracing
3754 * something, we probably want to know about it.
3757 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3761 iter->pg = ftrace_pages_start;
3762 iter->flags = FTRACE_ITER_ENABLED;
3763 iter->ops = &global_ops;
3769 * ftrace_regex_open - initialize function tracer filter files
3770 * @ops: The ftrace_ops that hold the hash filters
3771 * @flag: The type of filter to process
3772 * @inode: The inode, usually passed in to your open routine
3773 * @file: The file, usually passed in to your open routine
3775 * ftrace_regex_open() initializes the filter files for the
3776 * @ops. Depending on @flag it may process the filter hash or
3777 * the notrace hash of @ops. With this called from the open
3778 * routine, you can use ftrace_filter_write() for the write
3779 * routine if @flag has FTRACE_ITER_FILTER set, or
3780 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3781 * tracing_lseek() should be used as the lseek routine, and
3782 * release must call ftrace_regex_release().
3785 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3786 struct inode *inode, struct file *file)
3788 struct ftrace_iterator *iter;
3789 struct ftrace_hash *hash;
3790 struct list_head *mod_head;
3791 struct trace_array *tr = ops->private;
3794 ftrace_ops_init(ops);
3796 if (unlikely(ftrace_disabled))
3799 if (tracing_check_open_get_tr(tr))
3802 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3806 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3813 mutex_lock(&ops->func_hash->regex_lock);
3815 if (flag & FTRACE_ITER_NOTRACE) {
3816 hash = ops->func_hash->notrace_hash;
3817 mod_head = tr ? &tr->mod_notrace : NULL;
3819 hash = ops->func_hash->filter_hash;
3820 mod_head = tr ? &tr->mod_trace : NULL;
3823 iter->mod_list = mod_head;
3825 if (file->f_mode & FMODE_WRITE) {
3826 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3828 if (file->f_flags & O_TRUNC) {
3829 iter->hash = alloc_ftrace_hash(size_bits);
3830 clear_ftrace_mod_list(mod_head);
3832 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3836 trace_parser_put(&iter->parser);
3844 if (file->f_mode & FMODE_READ) {
3845 iter->pg = ftrace_pages_start;
3847 ret = seq_open(file, &show_ftrace_seq_ops);
3849 struct seq_file *m = file->private_data;
3853 free_ftrace_hash(iter->hash);
3854 trace_parser_put(&iter->parser);
3857 file->private_data = iter;
3860 mutex_unlock(&ops->func_hash->regex_lock);
3866 trace_array_put(tr);
3873 ftrace_filter_open(struct inode *inode, struct file *file)
3875 struct ftrace_ops *ops = inode->i_private;
3877 /* Checks for tracefs lockdown */
3878 return ftrace_regex_open(ops,
3879 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3884 ftrace_notrace_open(struct inode *inode, struct file *file)
3886 struct ftrace_ops *ops = inode->i_private;
3888 /* Checks for tracefs lockdown */
3889 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3893 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3894 struct ftrace_glob {
3901 * If symbols in an architecture don't correspond exactly to the user-visible
3902 * name of what they represent, it is possible to define this function to
3903 * perform the necessary adjustments.
3905 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3910 static int ftrace_match(char *str, struct ftrace_glob *g)
3915 str = arch_ftrace_match_adjust(str, g->search);
3919 if (strcmp(str, g->search) == 0)
3922 case MATCH_FRONT_ONLY:
3923 if (strncmp(str, g->search, g->len) == 0)
3926 case MATCH_MIDDLE_ONLY:
3927 if (strstr(str, g->search))
3930 case MATCH_END_ONLY:
3932 if (slen >= g->len &&
3933 memcmp(str + slen - g->len, g->search, g->len) == 0)
3937 if (glob_match(g->search, str))
3946 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3948 struct ftrace_func_entry *entry;
3951 entry = ftrace_lookup_ip(hash, rec->ip);
3953 /* Do nothing if it doesn't exist */
3957 free_hash_entry(hash, entry);
3959 /* Do nothing if it exists */
3963 ret = add_hash_entry(hash, rec->ip);
3969 add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3972 long index = simple_strtoul(func_g->search, NULL, 0);
3973 struct ftrace_page *pg;
3974 struct dyn_ftrace *rec;
3976 /* The index starts at 1 */
3980 do_for_each_ftrace_rec(pg, rec) {
3981 if (pg->index <= index) {
3983 /* this is a double loop, break goes to the next page */
3986 rec = &pg->records[index];
3987 enter_record(hash, rec, clear_filter);
3989 } while_for_each_ftrace_rec();
3994 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3995 struct ftrace_glob *mod_g, int exclude_mod)
3997 char str[KSYM_SYMBOL_LEN];
4000 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
4003 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
4005 /* blank module name to match all modules */
4007 /* blank module globbing: modname xor exclude_mod */
4008 if (!exclude_mod != !modname)
4014 * exclude_mod is set to trace everything but the given
4015 * module. If it is set and the module matches, then
4016 * return 0. If it is not set, and the module doesn't match
4017 * also return 0. Otherwise, check the function to see if
4020 if (!mod_matches == !exclude_mod)
4023 /* blank search means to match all funcs in the mod */
4028 return ftrace_match(str, func_g);
4032 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
4034 struct ftrace_page *pg;
4035 struct dyn_ftrace *rec;
4036 struct ftrace_glob func_g = { .type = MATCH_FULL };
4037 struct ftrace_glob mod_g = { .type = MATCH_FULL };
4038 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
4039 int exclude_mod = 0;
4042 int clear_filter = 0;
4045 func_g.type = filter_parse_regex(func, len, &func_g.search,
4047 func_g.len = strlen(func_g.search);
4051 mod_g.type = filter_parse_regex(mod, strlen(mod),
4052 &mod_g.search, &exclude_mod);
4053 mod_g.len = strlen(mod_g.search);
4056 mutex_lock(&ftrace_lock);
4058 if (unlikely(ftrace_disabled))
4061 if (func_g.type == MATCH_INDEX) {
4062 found = add_rec_by_index(hash, &func_g, clear_filter);
4066 do_for_each_ftrace_rec(pg, rec) {
4068 if (rec->flags & FTRACE_FL_DISABLED)
4071 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
4072 ret = enter_record(hash, rec, clear_filter);
4079 } while_for_each_ftrace_rec();
4081 mutex_unlock(&ftrace_lock);
4087 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
4089 return match_records(hash, buff, len, NULL);
4092 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4093 struct ftrace_ops_hash *old_hash)
4095 struct ftrace_ops *op;
4097 if (!ftrace_enabled)
4100 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4101 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4106 * If this is the shared global_ops filter, then we need to
4107 * check if there is another ops that shares it, is enabled.
4108 * If so, we still need to run the modify code.
4110 if (ops->func_hash != &global_ops.local_hash)
4113 do_for_each_ftrace_op(op, ftrace_ops_list) {
4114 if (op->func_hash == &global_ops.local_hash &&
4115 op->flags & FTRACE_OPS_FL_ENABLED) {
4116 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4117 /* Only need to do this once */
4120 } while_for_each_ftrace_op(op);
4123 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
4124 struct ftrace_hash **orig_hash,
4125 struct ftrace_hash *hash,
4128 struct ftrace_ops_hash old_hash_ops;
4129 struct ftrace_hash *old_hash;
4132 old_hash = *orig_hash;
4133 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4134 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4135 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4137 ftrace_ops_update_code(ops, &old_hash_ops);
4138 free_ftrace_hash_rcu(old_hash);
4143 static bool module_exists(const char *module)
4145 /* All modules have the symbol __this_module */
4146 static const char this_mod[] = "__this_module";
4147 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
4151 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
4153 if (n > sizeof(modname) - 1)
4156 val = module_kallsyms_lookup_name(modname);
4160 static int cache_mod(struct trace_array *tr,
4161 const char *func, char *module, int enable)
4163 struct ftrace_mod_load *ftrace_mod, *n;
4164 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4167 mutex_lock(&ftrace_lock);
4169 /* We do not cache inverse filters */
4170 if (func[0] == '!') {
4174 /* Look to remove this hash */
4175 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4176 if (strcmp(ftrace_mod->module, module) != 0)
4179 /* no func matches all */
4180 if (strcmp(func, "*") == 0 ||
4181 (ftrace_mod->func &&
4182 strcmp(ftrace_mod->func, func) == 0)) {
4184 free_ftrace_mod(ftrace_mod);
4192 /* We only care about modules that have not been loaded yet */
4193 if (module_exists(module))
4196 /* Save this string off, and execute it when the module is loaded */
4197 ret = ftrace_add_mod(tr, func, module, enable);
4199 mutex_unlock(&ftrace_lock);
4205 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4206 int reset, int enable);
4208 #ifdef CONFIG_MODULES
4209 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4210 char *mod, bool enable)
4212 struct ftrace_mod_load *ftrace_mod, *n;
4213 struct ftrace_hash **orig_hash, *new_hash;
4214 LIST_HEAD(process_mods);
4217 mutex_lock(&ops->func_hash->regex_lock);
4220 orig_hash = &ops->func_hash->filter_hash;
4222 orig_hash = &ops->func_hash->notrace_hash;
4224 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4227 goto out; /* warn? */
4229 mutex_lock(&ftrace_lock);
4231 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4233 if (strcmp(ftrace_mod->module, mod) != 0)
4236 if (ftrace_mod->func)
4237 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4239 func = kstrdup("*", GFP_KERNEL);
4241 if (!func) /* warn? */
4244 list_move(&ftrace_mod->list, &process_mods);
4246 /* Use the newly allocated func, as it may be "*" */
4247 kfree(ftrace_mod->func);
4248 ftrace_mod->func = func;
4251 mutex_unlock(&ftrace_lock);
4253 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4255 func = ftrace_mod->func;
4257 /* Grabs ftrace_lock, which is why we have this extra step */
4258 match_records(new_hash, func, strlen(func), mod);
4259 free_ftrace_mod(ftrace_mod);
4262 if (enable && list_empty(head))
4263 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4265 mutex_lock(&ftrace_lock);
4267 ftrace_hash_move_and_update_ops(ops, orig_hash,
4269 mutex_unlock(&ftrace_lock);
4272 mutex_unlock(&ops->func_hash->regex_lock);
4274 free_ftrace_hash(new_hash);
4277 static void process_cached_mods(const char *mod_name)
4279 struct trace_array *tr;
4282 mod = kstrdup(mod_name, GFP_KERNEL);
4286 mutex_lock(&trace_types_lock);
4287 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4288 if (!list_empty(&tr->mod_trace))
4289 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4290 if (!list_empty(&tr->mod_notrace))
4291 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4293 mutex_unlock(&trace_types_lock);
4300 * We register the module command as a template to show others how
4301 * to register the a command as well.
4305 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4306 char *func_orig, char *cmd, char *module, int enable)
4311 /* match_records() modifies func, and we need the original */
4312 func = kstrdup(func_orig, GFP_KERNEL);
4317 * cmd == 'mod' because we only registered this func
4318 * for the 'mod' ftrace_func_command.
4319 * But if you register one func with multiple commands,
4320 * you can tell which command was used by the cmd
4323 ret = match_records(hash, func, strlen(func), module);
4327 return cache_mod(tr, func_orig, module, enable);
4333 static struct ftrace_func_command ftrace_mod_cmd = {
4335 .func = ftrace_mod_callback,
4338 static int __init ftrace_mod_cmd_init(void)
4340 return register_ftrace_command(&ftrace_mod_cmd);
4342 core_initcall(ftrace_mod_cmd_init);
4344 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4345 struct ftrace_ops *op, struct ftrace_regs *fregs)
4347 struct ftrace_probe_ops *probe_ops;
4348 struct ftrace_func_probe *probe;
4350 probe = container_of(op, struct ftrace_func_probe, ops);
4351 probe_ops = probe->probe_ops;
4354 * Disable preemption for these calls to prevent a RCU grace
4355 * period. This syncs the hash iteration and freeing of items
4356 * on the hash. rcu_read_lock is too dangerous here.
4358 preempt_disable_notrace();
4359 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4360 preempt_enable_notrace();
4363 struct ftrace_func_map {
4364 struct ftrace_func_entry entry;
4368 struct ftrace_func_mapper {
4369 struct ftrace_hash hash;
4373 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4375 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4377 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4379 struct ftrace_hash *hash;
4382 * The mapper is simply a ftrace_hash, but since the entries
4383 * in the hash are not ftrace_func_entry type, we define it
4384 * as a separate structure.
4386 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4387 return (struct ftrace_func_mapper *)hash;
4391 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4392 * @mapper: The mapper that has the ip maps
4393 * @ip: the instruction pointer to find the data for
4395 * Returns the data mapped to @ip if found otherwise NULL. The return
4396 * is actually the address of the mapper data pointer. The address is
4397 * returned for use cases where the data is no bigger than a long, and
4398 * the user can use the data pointer as its data instead of having to
4399 * allocate more memory for the reference.
4401 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4404 struct ftrace_func_entry *entry;
4405 struct ftrace_func_map *map;
4407 entry = ftrace_lookup_ip(&mapper->hash, ip);
4411 map = (struct ftrace_func_map *)entry;
4416 * ftrace_func_mapper_add_ip - Map some data to an ip
4417 * @mapper: The mapper that has the ip maps
4418 * @ip: The instruction pointer address to map @data to
4419 * @data: The data to map to @ip
4421 * Returns 0 on success otherwise an error.
4423 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4424 unsigned long ip, void *data)
4426 struct ftrace_func_entry *entry;
4427 struct ftrace_func_map *map;
4429 entry = ftrace_lookup_ip(&mapper->hash, ip);
4433 map = kmalloc(sizeof(*map), GFP_KERNEL);
4440 __add_hash_entry(&mapper->hash, &map->entry);
4446 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4447 * @mapper: The mapper that has the ip maps
4448 * @ip: The instruction pointer address to remove the data from
4450 * Returns the data if it is found, otherwise NULL.
4451 * Note, if the data pointer is used as the data itself, (see
4452 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4453 * if the data pointer was set to zero.
4455 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4458 struct ftrace_func_entry *entry;
4459 struct ftrace_func_map *map;
4462 entry = ftrace_lookup_ip(&mapper->hash, ip);
4466 map = (struct ftrace_func_map *)entry;
4469 remove_hash_entry(&mapper->hash, entry);
4476 * free_ftrace_func_mapper - free a mapping of ips and data
4477 * @mapper: The mapper that has the ip maps
4478 * @free_func: A function to be called on each data item.
4480 * This is used to free the function mapper. The @free_func is optional
4481 * and can be used if the data needs to be freed as well.
4483 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4484 ftrace_mapper_func free_func)
4486 struct ftrace_func_entry *entry;
4487 struct ftrace_func_map *map;
4488 struct hlist_head *hhd;
4494 if (free_func && mapper->hash.count) {
4495 size = 1 << mapper->hash.size_bits;
4496 for (i = 0; i < size; i++) {
4497 hhd = &mapper->hash.buckets[i];
4498 hlist_for_each_entry(entry, hhd, hlist) {
4499 map = (struct ftrace_func_map *)entry;
4504 free_ftrace_hash(&mapper->hash);
4507 static void release_probe(struct ftrace_func_probe *probe)
4509 struct ftrace_probe_ops *probe_ops;
4511 mutex_lock(&ftrace_lock);
4513 WARN_ON(probe->ref <= 0);
4515 /* Subtract the ref that was used to protect this instance */
4519 probe_ops = probe->probe_ops;
4521 * Sending zero as ip tells probe_ops to free
4522 * the probe->data itself
4524 if (probe_ops->free)
4525 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4526 list_del(&probe->list);
4529 mutex_unlock(&ftrace_lock);
4532 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4535 * Add one ref to keep it from being freed when releasing the
4536 * ftrace_lock mutex.
4542 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4543 struct ftrace_probe_ops *probe_ops,
4546 struct ftrace_func_entry *entry;
4547 struct ftrace_func_probe *probe;
4548 struct ftrace_hash **orig_hash;
4549 struct ftrace_hash *old_hash;
4550 struct ftrace_hash *hash;
4559 /* We do not support '!' for function probes */
4560 if (WARN_ON(glob[0] == '!'))
4564 mutex_lock(&ftrace_lock);
4565 /* Check if the probe_ops is already registered */
4566 list_for_each_entry(probe, &tr->func_probes, list) {
4567 if (probe->probe_ops == probe_ops)
4570 if (&probe->list == &tr->func_probes) {
4571 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4573 mutex_unlock(&ftrace_lock);
4576 probe->probe_ops = probe_ops;
4577 probe->ops.func = function_trace_probe_call;
4579 ftrace_ops_init(&probe->ops);
4580 list_add(&probe->list, &tr->func_probes);
4583 acquire_probe_locked(probe);
4585 mutex_unlock(&ftrace_lock);
4588 * Note, there's a small window here that the func_hash->filter_hash
4589 * may be NULL or empty. Need to be careful when reading the loop.
4591 mutex_lock(&probe->ops.func_hash->regex_lock);
4593 orig_hash = &probe->ops.func_hash->filter_hash;
4594 old_hash = *orig_hash;
4595 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4602 ret = ftrace_match_records(hash, glob, strlen(glob));
4604 /* Nothing found? */
4611 size = 1 << hash->size_bits;
4612 for (i = 0; i < size; i++) {
4613 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4614 if (ftrace_lookup_ip(old_hash, entry->ip))
4617 * The caller might want to do something special
4618 * for each function we find. We call the callback
4619 * to give the caller an opportunity to do so.
4621 if (probe_ops->init) {
4622 ret = probe_ops->init(probe_ops, tr,
4626 if (probe_ops->free && count)
4627 probe_ops->free(probe_ops, tr,
4637 mutex_lock(&ftrace_lock);
4640 /* Nothing was added? */
4645 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4650 /* One ref for each new function traced */
4651 probe->ref += count;
4653 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4654 ret = ftrace_startup(&probe->ops, 0);
4657 mutex_unlock(&ftrace_lock);
4662 mutex_unlock(&probe->ops.func_hash->regex_lock);
4663 free_ftrace_hash(hash);
4665 release_probe(probe);
4670 if (!probe_ops->free || !count)
4673 /* Failed to do the move, need to call the free functions */
4674 for (i = 0; i < size; i++) {
4675 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4676 if (ftrace_lookup_ip(old_hash, entry->ip))
4678 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4685 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4686 struct ftrace_probe_ops *probe_ops)
4688 struct ftrace_ops_hash old_hash_ops;
4689 struct ftrace_func_entry *entry;
4690 struct ftrace_func_probe *probe;
4691 struct ftrace_glob func_g;
4692 struct ftrace_hash **orig_hash;
4693 struct ftrace_hash *old_hash;
4694 struct ftrace_hash *hash = NULL;
4695 struct hlist_node *tmp;
4696 struct hlist_head hhd;
4697 char str[KSYM_SYMBOL_LEN];
4699 int i, ret = -ENODEV;
4702 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4703 func_g.search = NULL;
4707 func_g.type = filter_parse_regex(glob, strlen(glob),
4708 &func_g.search, ¬);
4709 func_g.len = strlen(func_g.search);
4711 /* we do not support '!' for function probes */
4716 mutex_lock(&ftrace_lock);
4717 /* Check if the probe_ops is already registered */
4718 list_for_each_entry(probe, &tr->func_probes, list) {
4719 if (probe->probe_ops == probe_ops)
4722 if (&probe->list == &tr->func_probes)
4723 goto err_unlock_ftrace;
4726 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4727 goto err_unlock_ftrace;
4729 acquire_probe_locked(probe);
4731 mutex_unlock(&ftrace_lock);
4733 mutex_lock(&probe->ops.func_hash->regex_lock);
4735 orig_hash = &probe->ops.func_hash->filter_hash;
4736 old_hash = *orig_hash;
4738 if (ftrace_hash_empty(old_hash))
4741 old_hash_ops.filter_hash = old_hash;
4742 /* Probes only have filters */
4743 old_hash_ops.notrace_hash = NULL;
4746 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4750 INIT_HLIST_HEAD(&hhd);
4752 size = 1 << hash->size_bits;
4753 for (i = 0; i < size; i++) {
4754 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4756 if (func_g.search) {
4757 kallsyms_lookup(entry->ip, NULL, NULL,
4759 if (!ftrace_match(str, &func_g))
4763 remove_hash_entry(hash, entry);
4764 hlist_add_head(&entry->hlist, &hhd);
4768 /* Nothing found? */
4774 mutex_lock(&ftrace_lock);
4776 WARN_ON(probe->ref < count);
4778 probe->ref -= count;
4780 if (ftrace_hash_empty(hash))
4781 ftrace_shutdown(&probe->ops, 0);
4783 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4786 /* still need to update the function call sites */
4787 if (ftrace_enabled && !ftrace_hash_empty(hash))
4788 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4792 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4793 hlist_del(&entry->hlist);
4794 if (probe_ops->free)
4795 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4798 mutex_unlock(&ftrace_lock);
4801 mutex_unlock(&probe->ops.func_hash->regex_lock);
4802 free_ftrace_hash(hash);
4804 release_probe(probe);
4809 mutex_unlock(&ftrace_lock);
4813 void clear_ftrace_function_probes(struct trace_array *tr)
4815 struct ftrace_func_probe *probe, *n;
4817 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4818 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4821 static LIST_HEAD(ftrace_commands);
4822 static DEFINE_MUTEX(ftrace_cmd_mutex);
4825 * Currently we only register ftrace commands from __init, so mark this
4828 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4830 struct ftrace_func_command *p;
4833 mutex_lock(&ftrace_cmd_mutex);
4834 list_for_each_entry(p, &ftrace_commands, list) {
4835 if (strcmp(cmd->name, p->name) == 0) {
4840 list_add(&cmd->list, &ftrace_commands);
4842 mutex_unlock(&ftrace_cmd_mutex);
4848 * Currently we only unregister ftrace commands from __init, so mark
4851 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4853 struct ftrace_func_command *p, *n;
4856 mutex_lock(&ftrace_cmd_mutex);
4857 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4858 if (strcmp(cmd->name, p->name) == 0) {
4860 list_del_init(&p->list);
4865 mutex_unlock(&ftrace_cmd_mutex);
4870 static int ftrace_process_regex(struct ftrace_iterator *iter,
4871 char *buff, int len, int enable)
4873 struct ftrace_hash *hash = iter->hash;
4874 struct trace_array *tr = iter->ops->private;
4875 char *func, *command, *next = buff;
4876 struct ftrace_func_command *p;
4879 func = strsep(&next, ":");
4882 ret = ftrace_match_records(hash, func, len);
4892 command = strsep(&next, ":");
4894 mutex_lock(&ftrace_cmd_mutex);
4895 list_for_each_entry(p, &ftrace_commands, list) {
4896 if (strcmp(p->name, command) == 0) {
4897 ret = p->func(tr, hash, func, command, next, enable);
4902 mutex_unlock(&ftrace_cmd_mutex);
4908 ftrace_regex_write(struct file *file, const char __user *ubuf,
4909 size_t cnt, loff_t *ppos, int enable)
4911 struct ftrace_iterator *iter;
4912 struct trace_parser *parser;
4918 if (file->f_mode & FMODE_READ) {
4919 struct seq_file *m = file->private_data;
4922 iter = file->private_data;
4924 if (unlikely(ftrace_disabled))
4927 /* iter->hash is a local copy, so we don't need regex_lock */
4929 parser = &iter->parser;
4930 read = trace_get_user(parser, ubuf, cnt, ppos);
4932 if (read >= 0 && trace_parser_loaded(parser) &&
4933 !trace_parser_cont(parser)) {
4934 ret = ftrace_process_regex(iter, parser->buffer,
4935 parser->idx, enable);
4936 trace_parser_clear(parser);
4947 ftrace_filter_write(struct file *file, const char __user *ubuf,
4948 size_t cnt, loff_t *ppos)
4950 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4954 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4955 size_t cnt, loff_t *ppos)
4957 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4961 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4963 struct ftrace_func_entry *entry;
4965 if (!ftrace_location(ip))
4969 entry = ftrace_lookup_ip(hash, ip);
4972 free_hash_entry(hash, entry);
4976 return add_hash_entry(hash, ip);
4980 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4981 unsigned long ip, int remove, int reset, int enable)
4983 struct ftrace_hash **orig_hash;
4984 struct ftrace_hash *hash;
4987 if (unlikely(ftrace_disabled))
4990 mutex_lock(&ops->func_hash->regex_lock);
4993 orig_hash = &ops->func_hash->filter_hash;
4995 orig_hash = &ops->func_hash->notrace_hash;
4998 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5000 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
5004 goto out_regex_unlock;
5007 if (buf && !ftrace_match_records(hash, buf, len)) {
5009 goto out_regex_unlock;
5012 ret = ftrace_match_addr(hash, ip, remove);
5014 goto out_regex_unlock;
5017 mutex_lock(&ftrace_lock);
5018 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
5019 mutex_unlock(&ftrace_lock);
5022 mutex_unlock(&ops->func_hash->regex_lock);
5024 free_ftrace_hash(hash);
5029 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
5030 int reset, int enable)
5032 return ftrace_set_hash(ops, NULL, 0, ip, remove, reset, enable);
5035 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
5037 struct ftrace_direct_func {
5038 struct list_head next;
5043 static LIST_HEAD(ftrace_direct_funcs);
5046 * ftrace_find_direct_func - test an address if it is a registered direct caller
5047 * @addr: The address of a registered direct caller
5049 * This searches to see if a ftrace direct caller has been registered
5050 * at a specific address, and if so, it returns a descriptor for it.
5052 * This can be used by architecture code to see if an address is
5053 * a direct caller (trampoline) attached to a fentry/mcount location.
5054 * This is useful for the function_graph tracer, as it may need to
5055 * do adjustments if it traced a location that also has a direct
5056 * trampoline attached to it.
5058 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
5060 struct ftrace_direct_func *entry;
5063 /* May be called by fgraph trampoline (protected by rcu tasks) */
5064 list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
5065 if (entry->addr == addr) {
5076 static struct ftrace_direct_func *ftrace_alloc_direct_func(unsigned long addr)
5078 struct ftrace_direct_func *direct;
5080 direct = kmalloc(sizeof(*direct), GFP_KERNEL);
5083 direct->addr = addr;
5085 list_add_rcu(&direct->next, &ftrace_direct_funcs);
5086 ftrace_direct_func_count++;
5091 * register_ftrace_direct - Call a custom trampoline directly
5092 * @ip: The address of the nop at the beginning of a function
5093 * @addr: The address of the trampoline to call at @ip
5095 * This is used to connect a direct call from the nop location (@ip)
5096 * at the start of ftrace traced functions. The location that it calls
5097 * (@addr) must be able to handle a direct call, and save the parameters
5098 * of the function being traced, and restore them (or inject new ones
5099 * if needed), before returning.
5103 * -EBUSY - Another direct function is already attached (there can be only one)
5104 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5105 * -ENOMEM - There was an allocation failure.
5107 int register_ftrace_direct(unsigned long ip, unsigned long addr)
5109 struct ftrace_direct_func *direct;
5110 struct ftrace_func_entry *entry;
5111 struct ftrace_hash *free_hash = NULL;
5112 struct dyn_ftrace *rec;
5115 mutex_lock(&direct_mutex);
5117 /* See if there's a direct function at @ip already */
5118 if (ftrace_find_rec_direct(ip))
5122 rec = lookup_rec(ip, ip);
5127 * Check if the rec says it has a direct call but we didn't
5130 if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
5133 /* Make sure the ip points to the exact record */
5134 if (ip != rec->ip) {
5136 /* Need to check this ip for a direct. */
5137 if (ftrace_find_rec_direct(ip))
5142 direct = ftrace_find_direct_func(addr);
5144 direct = ftrace_alloc_direct_func(addr);
5149 entry = ftrace_add_rec_direct(ip, addr, &free_hash);
5153 ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
5155 remove_hash_entry(direct_functions, entry);
5157 if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
5158 ret = register_ftrace_function(&direct_ops);
5160 ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5165 if (!direct->count) {
5166 list_del_rcu(&direct->next);
5167 synchronize_rcu_tasks();
5170 free_ftrace_hash(free_hash);
5172 ftrace_direct_func_count--;
5178 mutex_unlock(&direct_mutex);
5181 synchronize_rcu_tasks();
5182 free_ftrace_hash(free_hash);
5187 EXPORT_SYMBOL_GPL(register_ftrace_direct);
5189 static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
5190 struct dyn_ftrace **recp)
5192 struct ftrace_func_entry *entry;
5193 struct dyn_ftrace *rec;
5195 rec = lookup_rec(*ip, *ip);
5199 entry = __ftrace_lookup_ip(direct_functions, rec->ip);
5201 WARN_ON(rec->flags & FTRACE_FL_DIRECT);
5205 WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
5207 /* Passed in ip just needs to be on the call site */
5216 int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
5218 struct ftrace_direct_func *direct;
5219 struct ftrace_func_entry *entry;
5220 struct ftrace_hash *hash;
5223 mutex_lock(&direct_mutex);
5225 entry = find_direct_entry(&ip, NULL);
5229 hash = direct_ops.func_hash->filter_hash;
5230 if (hash->count == 1)
5231 unregister_ftrace_function(&direct_ops);
5233 ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5237 remove_hash_entry(direct_functions, entry);
5239 direct = ftrace_find_direct_func(addr);
5240 if (!WARN_ON(!direct)) {
5241 /* This is the good path (see the ! before WARN) */
5243 WARN_ON(direct->count < 0);
5244 if (!direct->count) {
5245 list_del_rcu(&direct->next);
5246 synchronize_rcu_tasks();
5249 ftrace_direct_func_count--;
5253 mutex_unlock(&direct_mutex);
5257 EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
5259 static struct ftrace_ops stub_ops = {
5260 .func = ftrace_stub,
5264 * ftrace_modify_direct_caller - modify ftrace nop directly
5265 * @entry: The ftrace hash entry of the direct helper for @rec
5266 * @rec: The record representing the function site to patch
5267 * @old_addr: The location that the site at @rec->ip currently calls
5268 * @new_addr: The location that the site at @rec->ip should call
5270 * An architecture may overwrite this function to optimize the
5271 * changing of the direct callback on an ftrace nop location.
5272 * This is called with the ftrace_lock mutex held, and no other
5273 * ftrace callbacks are on the associated record (@rec). Thus,
5274 * it is safe to modify the ftrace record, where it should be
5275 * currently calling @old_addr directly, to call @new_addr.
5277 * Safety checks should be made to make sure that the code at
5278 * @rec->ip is currently calling @old_addr. And this must
5279 * also update entry->direct to @new_addr.
5281 int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
5282 struct dyn_ftrace *rec,
5283 unsigned long old_addr,
5284 unsigned long new_addr)
5286 unsigned long ip = rec->ip;
5290 * The ftrace_lock was used to determine if the record
5291 * had more than one registered user to it. If it did,
5292 * we needed to prevent that from changing to do the quick
5293 * switch. But if it did not (only a direct caller was attached)
5294 * then this function is called. But this function can deal
5295 * with attached callers to the rec that we care about, and
5296 * since this function uses standard ftrace calls that take
5297 * the ftrace_lock mutex, we need to release it.
5299 mutex_unlock(&ftrace_lock);
5302 * By setting a stub function at the same address, we force
5303 * the code to call the iterator and the direct_ops helper.
5304 * This means that @ip does not call the direct call, and
5305 * we can simply modify it.
5307 ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
5311 ret = register_ftrace_function(&stub_ops);
5313 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5317 entry->direct = new_addr;
5320 * By removing the stub, we put back the direct call, calling
5323 unregister_ftrace_function(&stub_ops);
5324 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5327 mutex_lock(&ftrace_lock);
5333 * modify_ftrace_direct - Modify an existing direct call to call something else
5334 * @ip: The instruction pointer to modify
5335 * @old_addr: The address that the current @ip calls directly
5336 * @new_addr: The address that the @ip should call
5338 * This modifies a ftrace direct caller at an instruction pointer without
5339 * having to disable it first. The direct call will switch over to the
5340 * @new_addr without missing anything.
5342 * Returns: zero on success. Non zero on error, which includes:
5343 * -ENODEV : the @ip given has no direct caller attached
5344 * -EINVAL : the @old_addr does not match the current direct caller
5346 int modify_ftrace_direct(unsigned long ip,
5347 unsigned long old_addr, unsigned long new_addr)
5349 struct ftrace_direct_func *direct, *new_direct = NULL;
5350 struct ftrace_func_entry *entry;
5351 struct dyn_ftrace *rec;
5354 mutex_lock(&direct_mutex);
5356 mutex_lock(&ftrace_lock);
5357 entry = find_direct_entry(&ip, &rec);
5362 if (entry->direct != old_addr)
5365 direct = ftrace_find_direct_func(old_addr);
5366 if (WARN_ON(!direct))
5368 if (direct->count > 1) {
5370 new_direct = ftrace_alloc_direct_func(new_addr);
5374 new_direct->count++;
5376 direct->addr = new_addr;
5380 * If there's no other ftrace callback on the rec->ip location,
5381 * then it can be changed directly by the architecture.
5382 * If there is another caller, then we just need to change the
5383 * direct caller helper to point to @new_addr.
5385 if (ftrace_rec_count(rec) == 1) {
5386 ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
5388 entry->direct = new_addr;
5392 if (unlikely(ret && new_direct)) {
5394 list_del_rcu(&new_direct->next);
5395 synchronize_rcu_tasks();
5397 ftrace_direct_func_count--;
5401 mutex_unlock(&ftrace_lock);
5402 mutex_unlock(&direct_mutex);
5405 EXPORT_SYMBOL_GPL(modify_ftrace_direct);
5407 #define MULTI_FLAGS (FTRACE_OPS_FL_IPMODIFY | FTRACE_OPS_FL_DIRECT | \
5408 FTRACE_OPS_FL_SAVE_REGS)
5410 static int check_direct_multi(struct ftrace_ops *ops)
5412 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5414 if ((ops->flags & MULTI_FLAGS) != MULTI_FLAGS)
5419 static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long addr)
5421 struct ftrace_func_entry *entry, *del;
5424 size = 1 << hash->size_bits;
5425 for (i = 0; i < size; i++) {
5426 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5427 del = __ftrace_lookup_ip(direct_functions, entry->ip);
5428 if (del && del->direct == addr) {
5429 remove_hash_entry(direct_functions, del);
5437 * register_ftrace_direct_multi - Call a custom trampoline directly
5438 * for multiple functions registered in @ops
5439 * @ops: The address of the struct ftrace_ops object
5440 * @addr: The address of the trampoline to call at @ops functions
5442 * This is used to connect a direct calls to @addr from the nop locations
5443 * of the functions registered in @ops (with by ftrace_set_filter_ip
5446 * The location that it calls (@addr) must be able to handle a direct call,
5447 * and save the parameters of the function being traced, and restore them
5448 * (or inject new ones if needed), before returning.
5452 * -EINVAL - The @ops object was already registered with this call or
5453 * when there are no functions in @ops object.
5454 * -EBUSY - Another direct function is already attached (there can be only one)
5455 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5456 * -ENOMEM - There was an allocation failure.
5458 int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5460 struct ftrace_hash *hash, *free_hash = NULL;
5461 struct ftrace_func_entry *entry, *new;
5462 int err = -EBUSY, size, i;
5464 if (ops->func || ops->trampoline)
5466 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5468 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5471 hash = ops->func_hash->filter_hash;
5472 if (ftrace_hash_empty(hash))
5475 mutex_lock(&direct_mutex);
5477 /* Make sure requested entries are not already registered.. */
5478 size = 1 << hash->size_bits;
5479 for (i = 0; i < size; i++) {
5480 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5481 if (ftrace_find_rec_direct(entry->ip))
5486 /* ... and insert them to direct_functions hash. */
5488 for (i = 0; i < size; i++) {
5489 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5490 new = ftrace_add_rec_direct(entry->ip, addr, &free_hash);
5493 entry->direct = addr;
5497 ops->func = call_direct_funcs;
5498 ops->flags = MULTI_FLAGS;
5499 ops->trampoline = FTRACE_REGS_ADDR;
5501 err = register_ftrace_function(ops);
5505 remove_direct_functions_hash(hash, addr);
5508 mutex_unlock(&direct_mutex);
5511 synchronize_rcu_tasks();
5512 free_ftrace_hash(free_hash);
5516 EXPORT_SYMBOL_GPL(register_ftrace_direct_multi);
5519 * unregister_ftrace_direct_multi - Remove calls to custom trampoline
5520 * previously registered by register_ftrace_direct_multi for @ops object.
5521 * @ops: The address of the struct ftrace_ops object
5523 * This is used to remove a direct calls to @addr from the nop locations
5524 * of the functions registered in @ops (with by ftrace_set_filter_ip
5529 * -EINVAL - The @ops object was not properly registered.
5531 int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5533 struct ftrace_hash *hash = ops->func_hash->filter_hash;
5536 if (check_direct_multi(ops))
5538 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5541 mutex_lock(&direct_mutex);
5542 err = unregister_ftrace_function(ops);
5543 remove_direct_functions_hash(hash, addr);
5544 mutex_unlock(&direct_mutex);
5546 /* cleanup for possible another register call */
5548 ops->trampoline = 0;
5551 EXPORT_SYMBOL_GPL(unregister_ftrace_direct_multi);
5554 * modify_ftrace_direct_multi - Modify an existing direct 'multi' call
5555 * to call something else
5556 * @ops: The address of the struct ftrace_ops object
5557 * @addr: The address of the new trampoline to call at @ops functions
5559 * This is used to unregister currently registered direct caller and
5560 * register new one @addr on functions registered in @ops object.
5562 * Note there's window between ftrace_shutdown and ftrace_startup calls
5563 * where there will be no callbacks called.
5565 * Returns: zero on success. Non zero on error, which includes:
5566 * -EINVAL - The @ops object was not properly registered.
5568 int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5570 struct ftrace_hash *hash;
5571 struct ftrace_func_entry *entry, *iter;
5572 static struct ftrace_ops tmp_ops = {
5573 .func = ftrace_stub,
5574 .flags = FTRACE_OPS_FL_STUB,
5579 if (check_direct_multi(ops))
5581 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5584 mutex_lock(&direct_mutex);
5586 /* Enable the tmp_ops to have the same functions as the direct ops */
5587 ftrace_ops_init(&tmp_ops);
5588 tmp_ops.func_hash = ops->func_hash;
5590 err = register_ftrace_function(&tmp_ops);
5595 * Now the ftrace_ops_list_func() is called to do the direct callers.
5596 * We can safely change the direct functions attached to each entry.
5598 mutex_lock(&ftrace_lock);
5600 hash = ops->func_hash->filter_hash;
5601 size = 1 << hash->size_bits;
5602 for (i = 0; i < size; i++) {
5603 hlist_for_each_entry(iter, &hash->buckets[i], hlist) {
5604 entry = __ftrace_lookup_ip(direct_functions, iter->ip);
5607 entry->direct = addr;
5611 mutex_unlock(&ftrace_lock);
5613 /* Removing the tmp_ops will add the updated direct callers to the functions */
5614 unregister_ftrace_function(&tmp_ops);
5617 mutex_unlock(&direct_mutex);
5620 EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi);
5621 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
5624 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
5625 * @ops - the ops to set the filter with
5626 * @ip - the address to add to or remove from the filter.
5627 * @remove - non zero to remove the ip from the filter
5628 * @reset - non zero to reset all filters before applying this filter.
5630 * Filters denote which functions should be enabled when tracing is enabled
5631 * If @ip is NULL, it fails to update filter.
5633 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
5634 int remove, int reset)
5636 ftrace_ops_init(ops);
5637 return ftrace_set_addr(ops, ip, remove, reset, 1);
5639 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
5642 * ftrace_ops_set_global_filter - setup ops to use global filters
5643 * @ops - the ops which will use the global filters
5645 * ftrace users who need global function trace filtering should call this.
5646 * It can set the global filter only if ops were not initialized before.
5648 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
5650 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
5653 ftrace_ops_init(ops);
5654 ops->func_hash = &global_ops.local_hash;
5656 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
5659 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
5660 int reset, int enable)
5662 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
5666 * ftrace_set_filter - set a function to filter on in ftrace
5667 * @ops - the ops to set the filter with
5668 * @buf - the string that holds the function filter text.
5669 * @len - the length of the string.
5670 * @reset - non zero to reset all filters before applying this filter.
5672 * Filters denote which functions should be enabled when tracing is enabled.
5673 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5675 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
5678 ftrace_ops_init(ops);
5679 return ftrace_set_regex(ops, buf, len, reset, 1);
5681 EXPORT_SYMBOL_GPL(ftrace_set_filter);
5684 * ftrace_set_notrace - set a function to not trace in ftrace
5685 * @ops - the ops to set the notrace filter with
5686 * @buf - the string that holds the function notrace text.
5687 * @len - the length of the string.
5688 * @reset - non zero to reset all filters before applying this filter.
5690 * Notrace Filters denote which functions should not be enabled when tracing
5691 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5694 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
5697 ftrace_ops_init(ops);
5698 return ftrace_set_regex(ops, buf, len, reset, 0);
5700 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
5702 * ftrace_set_global_filter - set a function to filter on with global tracers
5703 * @buf - the string that holds the function filter text.
5704 * @len - the length of the string.
5705 * @reset - non zero to reset all filters before applying this filter.
5707 * Filters denote which functions should be enabled when tracing is enabled.
5708 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5710 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
5712 ftrace_set_regex(&global_ops, buf, len, reset, 1);
5714 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
5717 * ftrace_set_global_notrace - set a function to not trace with global tracers
5718 * @buf - the string that holds the function notrace text.
5719 * @len - the length of the string.
5720 * @reset - non zero to reset all filters before applying this filter.
5722 * Notrace Filters denote which functions should not be enabled when tracing
5723 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5726 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
5728 ftrace_set_regex(&global_ops, buf, len, reset, 0);
5730 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
5733 * command line interface to allow users to set filters on boot up.
5735 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
5736 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5737 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
5739 /* Used by function selftest to not test if filter is set */
5740 bool ftrace_filter_param __initdata;
5742 static int __init set_ftrace_notrace(char *str)
5744 ftrace_filter_param = true;
5745 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
5748 __setup("ftrace_notrace=", set_ftrace_notrace);
5750 static int __init set_ftrace_filter(char *str)
5752 ftrace_filter_param = true;
5753 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
5756 __setup("ftrace_filter=", set_ftrace_filter);
5758 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5759 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
5760 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5761 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
5763 static int __init set_graph_function(char *str)
5765 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
5768 __setup("ftrace_graph_filter=", set_graph_function);
5770 static int __init set_graph_notrace_function(char *str)
5772 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
5775 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
5777 static int __init set_graph_max_depth_function(char *str)
5781 fgraph_max_depth = simple_strtoul(str, NULL, 0);
5784 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
5786 static void __init set_ftrace_early_graph(char *buf, int enable)
5790 struct ftrace_hash *hash;
5792 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5793 if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
5797 func = strsep(&buf, ",");
5798 /* we allow only one expression at a time */
5799 ret = ftrace_graph_set_hash(hash, func);
5801 printk(KERN_DEBUG "ftrace: function %s not "
5802 "traceable\n", func);
5806 ftrace_graph_hash = hash;
5808 ftrace_graph_notrace_hash = hash;
5810 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5813 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
5817 ftrace_ops_init(ops);
5820 func = strsep(&buf, ",");
5821 ftrace_set_regex(ops, func, strlen(func), 0, enable);
5825 static void __init set_ftrace_early_filters(void)
5827 if (ftrace_filter_buf[0])
5828 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5829 if (ftrace_notrace_buf[0])
5830 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5831 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5832 if (ftrace_graph_buf[0])
5833 set_ftrace_early_graph(ftrace_graph_buf, 1);
5834 if (ftrace_graph_notrace_buf[0])
5835 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5836 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5839 int ftrace_regex_release(struct inode *inode, struct file *file)
5841 struct seq_file *m = (struct seq_file *)file->private_data;
5842 struct ftrace_iterator *iter;
5843 struct ftrace_hash **orig_hash;
5844 struct trace_parser *parser;
5847 if (file->f_mode & FMODE_READ) {
5849 seq_release(inode, file);
5851 iter = file->private_data;
5853 parser = &iter->parser;
5854 if (trace_parser_loaded(parser)) {
5855 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
5857 ftrace_process_regex(iter, parser->buffer,
5858 parser->idx, enable);
5861 trace_parser_put(parser);
5863 mutex_lock(&iter->ops->func_hash->regex_lock);
5865 if (file->f_mode & FMODE_WRITE) {
5866 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5869 orig_hash = &iter->ops->func_hash->filter_hash;
5870 if (iter->tr && !list_empty(&iter->tr->mod_trace))
5871 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5873 orig_hash = &iter->ops->func_hash->notrace_hash;
5875 mutex_lock(&ftrace_lock);
5876 ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5877 iter->hash, filter_hash);
5878 mutex_unlock(&ftrace_lock);
5880 /* For read only, the hash is the ops hash */
5884 mutex_unlock(&iter->ops->func_hash->regex_lock);
5885 free_ftrace_hash(iter->hash);
5887 trace_array_put(iter->tr);
5893 static const struct file_operations ftrace_avail_fops = {
5894 .open = ftrace_avail_open,
5896 .llseek = seq_lseek,
5897 .release = seq_release_private,
5900 static const struct file_operations ftrace_enabled_fops = {
5901 .open = ftrace_enabled_open,
5903 .llseek = seq_lseek,
5904 .release = seq_release_private,
5907 static const struct file_operations ftrace_filter_fops = {
5908 .open = ftrace_filter_open,
5910 .write = ftrace_filter_write,
5911 .llseek = tracing_lseek,
5912 .release = ftrace_regex_release,
5915 static const struct file_operations ftrace_notrace_fops = {
5916 .open = ftrace_notrace_open,
5918 .write = ftrace_notrace_write,
5919 .llseek = tracing_lseek,
5920 .release = ftrace_regex_release,
5923 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5925 static DEFINE_MUTEX(graph_lock);
5927 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
5928 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
5930 enum graph_filter_type {
5931 GRAPH_FILTER_NOTRACE = 0,
5932 GRAPH_FILTER_FUNCTION,
5935 #define FTRACE_GRAPH_EMPTY ((void *)1)
5937 struct ftrace_graph_data {
5938 struct ftrace_hash *hash;
5939 struct ftrace_func_entry *entry;
5940 int idx; /* for hash table iteration */
5941 enum graph_filter_type type;
5942 struct ftrace_hash *new_hash;
5943 const struct seq_operations *seq_ops;
5944 struct trace_parser parser;
5948 __g_next(struct seq_file *m, loff_t *pos)
5950 struct ftrace_graph_data *fgd = m->private;
5951 struct ftrace_func_entry *entry = fgd->entry;
5952 struct hlist_head *head;
5953 int i, idx = fgd->idx;
5955 if (*pos >= fgd->hash->count)
5959 hlist_for_each_entry_continue(entry, hlist) {
5967 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5968 head = &fgd->hash->buckets[i];
5969 hlist_for_each_entry(entry, head, hlist) {
5979 g_next(struct seq_file *m, void *v, loff_t *pos)
5982 return __g_next(m, pos);
5985 static void *g_start(struct seq_file *m, loff_t *pos)
5987 struct ftrace_graph_data *fgd = m->private;
5989 mutex_lock(&graph_lock);
5991 if (fgd->type == GRAPH_FILTER_FUNCTION)
5992 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5993 lockdep_is_held(&graph_lock));
5995 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5996 lockdep_is_held(&graph_lock));
5998 /* Nothing, tell g_show to print all functions are enabled */
5999 if (ftrace_hash_empty(fgd->hash) && !*pos)
6000 return FTRACE_GRAPH_EMPTY;
6004 return __g_next(m, pos);
6007 static void g_stop(struct seq_file *m, void *p)
6009 mutex_unlock(&graph_lock);
6012 static int g_show(struct seq_file *m, void *v)
6014 struct ftrace_func_entry *entry = v;
6019 if (entry == FTRACE_GRAPH_EMPTY) {
6020 struct ftrace_graph_data *fgd = m->private;
6022 if (fgd->type == GRAPH_FILTER_FUNCTION)
6023 seq_puts(m, "#### all functions enabled ####\n");
6025 seq_puts(m, "#### no functions disabled ####\n");
6029 seq_printf(m, "%ps\n", (void *)entry->ip);
6034 static const struct seq_operations ftrace_graph_seq_ops = {
6042 __ftrace_graph_open(struct inode *inode, struct file *file,
6043 struct ftrace_graph_data *fgd)
6046 struct ftrace_hash *new_hash = NULL;
6048 ret = security_locked_down(LOCKDOWN_TRACEFS);
6052 if (file->f_mode & FMODE_WRITE) {
6053 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
6055 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
6058 if (file->f_flags & O_TRUNC)
6059 new_hash = alloc_ftrace_hash(size_bits);
6061 new_hash = alloc_and_copy_ftrace_hash(size_bits,
6069 if (file->f_mode & FMODE_READ) {
6070 ret = seq_open(file, &ftrace_graph_seq_ops);
6072 struct seq_file *m = file->private_data;
6076 free_ftrace_hash(new_hash);
6080 file->private_data = fgd;
6083 if (ret < 0 && file->f_mode & FMODE_WRITE)
6084 trace_parser_put(&fgd->parser);
6086 fgd->new_hash = new_hash;
6089 * All uses of fgd->hash must be taken with the graph_lock
6090 * held. The graph_lock is going to be released, so force
6091 * fgd->hash to be reinitialized when it is taken again.
6099 ftrace_graph_open(struct inode *inode, struct file *file)
6101 struct ftrace_graph_data *fgd;
6104 if (unlikely(ftrace_disabled))
6107 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6111 mutex_lock(&graph_lock);
6113 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
6114 lockdep_is_held(&graph_lock));
6115 fgd->type = GRAPH_FILTER_FUNCTION;
6116 fgd->seq_ops = &ftrace_graph_seq_ops;
6118 ret = __ftrace_graph_open(inode, file, fgd);
6122 mutex_unlock(&graph_lock);
6127 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
6129 struct ftrace_graph_data *fgd;
6132 if (unlikely(ftrace_disabled))
6135 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6139 mutex_lock(&graph_lock);
6141 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6142 lockdep_is_held(&graph_lock));
6143 fgd->type = GRAPH_FILTER_NOTRACE;
6144 fgd->seq_ops = &ftrace_graph_seq_ops;
6146 ret = __ftrace_graph_open(inode, file, fgd);
6150 mutex_unlock(&graph_lock);
6155 ftrace_graph_release(struct inode *inode, struct file *file)
6157 struct ftrace_graph_data *fgd;
6158 struct ftrace_hash *old_hash, *new_hash;
6159 struct trace_parser *parser;
6162 if (file->f_mode & FMODE_READ) {
6163 struct seq_file *m = file->private_data;
6166 seq_release(inode, file);
6168 fgd = file->private_data;
6172 if (file->f_mode & FMODE_WRITE) {
6174 parser = &fgd->parser;
6176 if (trace_parser_loaded((parser))) {
6177 ret = ftrace_graph_set_hash(fgd->new_hash,
6181 trace_parser_put(parser);
6183 new_hash = __ftrace_hash_move(fgd->new_hash);
6189 mutex_lock(&graph_lock);
6191 if (fgd->type == GRAPH_FILTER_FUNCTION) {
6192 old_hash = rcu_dereference_protected(ftrace_graph_hash,
6193 lockdep_is_held(&graph_lock));
6194 rcu_assign_pointer(ftrace_graph_hash, new_hash);
6196 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6197 lockdep_is_held(&graph_lock));
6198 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
6201 mutex_unlock(&graph_lock);
6204 * We need to do a hard force of sched synchronization.
6205 * This is because we use preempt_disable() to do RCU, but
6206 * the function tracers can be called where RCU is not watching
6207 * (like before user_exit()). We can not rely on the RCU
6208 * infrastructure to do the synchronization, thus we must do it
6211 if (old_hash != EMPTY_HASH)
6212 synchronize_rcu_tasks_rude();
6214 free_ftrace_hash(old_hash);
6218 free_ftrace_hash(fgd->new_hash);
6225 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
6227 struct ftrace_glob func_g;
6228 struct dyn_ftrace *rec;
6229 struct ftrace_page *pg;
6230 struct ftrace_func_entry *entry;
6235 func_g.type = filter_parse_regex(buffer, strlen(buffer),
6236 &func_g.search, ¬);
6238 func_g.len = strlen(func_g.search);
6240 mutex_lock(&ftrace_lock);
6242 if (unlikely(ftrace_disabled)) {
6243 mutex_unlock(&ftrace_lock);
6247 do_for_each_ftrace_rec(pg, rec) {
6249 if (rec->flags & FTRACE_FL_DISABLED)
6252 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
6253 entry = ftrace_lookup_ip(hash, rec->ip);
6260 if (add_hash_entry(hash, rec->ip) < 0)
6264 free_hash_entry(hash, entry);
6269 } while_for_each_ftrace_rec();
6271 mutex_unlock(&ftrace_lock);
6280 ftrace_graph_write(struct file *file, const char __user *ubuf,
6281 size_t cnt, loff_t *ppos)
6283 ssize_t read, ret = 0;
6284 struct ftrace_graph_data *fgd = file->private_data;
6285 struct trace_parser *parser;
6290 /* Read mode uses seq functions */
6291 if (file->f_mode & FMODE_READ) {
6292 struct seq_file *m = file->private_data;
6296 parser = &fgd->parser;
6298 read = trace_get_user(parser, ubuf, cnt, ppos);
6300 if (read >= 0 && trace_parser_loaded(parser) &&
6301 !trace_parser_cont(parser)) {
6303 ret = ftrace_graph_set_hash(fgd->new_hash,
6305 trace_parser_clear(parser);
6314 static const struct file_operations ftrace_graph_fops = {
6315 .open = ftrace_graph_open,
6317 .write = ftrace_graph_write,
6318 .llseek = tracing_lseek,
6319 .release = ftrace_graph_release,
6322 static const struct file_operations ftrace_graph_notrace_fops = {
6323 .open = ftrace_graph_notrace_open,
6325 .write = ftrace_graph_write,
6326 .llseek = tracing_lseek,
6327 .release = ftrace_graph_release,
6329 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6331 void ftrace_create_filter_files(struct ftrace_ops *ops,
6332 struct dentry *parent)
6335 trace_create_file("set_ftrace_filter", TRACE_MODE_WRITE, parent,
6336 ops, &ftrace_filter_fops);
6338 trace_create_file("set_ftrace_notrace", TRACE_MODE_WRITE, parent,
6339 ops, &ftrace_notrace_fops);
6343 * The name "destroy_filter_files" is really a misnomer. Although
6344 * in the future, it may actually delete the files, but this is
6345 * really intended to make sure the ops passed in are disabled
6346 * and that when this function returns, the caller is free to
6349 * The "destroy" name is only to match the "create" name that this
6350 * should be paired with.
6352 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
6354 mutex_lock(&ftrace_lock);
6355 if (ops->flags & FTRACE_OPS_FL_ENABLED)
6356 ftrace_shutdown(ops, 0);
6357 ops->flags |= FTRACE_OPS_FL_DELETED;
6358 ftrace_free_filter(ops);
6359 mutex_unlock(&ftrace_lock);
6362 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
6365 trace_create_file("available_filter_functions", TRACE_MODE_READ,
6366 d_tracer, NULL, &ftrace_avail_fops);
6368 trace_create_file("enabled_functions", TRACE_MODE_READ,
6369 d_tracer, NULL, &ftrace_enabled_fops);
6371 ftrace_create_filter_files(&global_ops, d_tracer);
6373 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6374 trace_create_file("set_graph_function", TRACE_MODE_WRITE, d_tracer,
6376 &ftrace_graph_fops);
6377 trace_create_file("set_graph_notrace", TRACE_MODE_WRITE, d_tracer,
6379 &ftrace_graph_notrace_fops);
6380 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6385 static int ftrace_cmp_ips(const void *a, const void *b)
6387 const unsigned long *ipa = a;
6388 const unsigned long *ipb = b;
6397 #ifdef CONFIG_FTRACE_SORT_STARTUP_TEST
6398 static void test_is_sorted(unsigned long *start, unsigned long count)
6402 for (i = 1; i < count; i++) {
6403 if (WARN(start[i - 1] > start[i],
6404 "[%d] %pS at %lx is not sorted with %pS at %lx\n", i,
6405 (void *)start[i - 1], start[i - 1],
6406 (void *)start[i], start[i]))
6410 pr_info("ftrace section at %px sorted properly\n", start);
6413 static void test_is_sorted(unsigned long *start, unsigned long count)
6418 static int ftrace_process_locs(struct module *mod,
6419 unsigned long *start,
6422 struct ftrace_page *start_pg;
6423 struct ftrace_page *pg;
6424 struct dyn_ftrace *rec;
6425 unsigned long count;
6428 unsigned long flags = 0; /* Shut up gcc */
6431 count = end - start;
6437 * Sorting mcount in vmlinux at build time depend on
6438 * CONFIG_BUILDTIME_TABLE_SORT, while mcount loc in
6439 * modules can not be sorted at build time.
6441 if (!IS_ENABLED(CONFIG_BUILDTIME_TABLE_SORT) || mod) {
6442 sort(start, count, sizeof(*start),
6443 ftrace_cmp_ips, NULL);
6445 test_is_sorted(start, count);
6448 start_pg = ftrace_allocate_pages(count);
6452 mutex_lock(&ftrace_lock);
6455 * Core and each module needs their own pages, as
6456 * modules will free them when they are removed.
6457 * Force a new page to be allocated for modules.
6460 WARN_ON(ftrace_pages || ftrace_pages_start);
6461 /* First initialization */
6462 ftrace_pages = ftrace_pages_start = start_pg;
6467 if (WARN_ON(ftrace_pages->next)) {
6468 /* Hmm, we have free pages? */
6469 while (ftrace_pages->next)
6470 ftrace_pages = ftrace_pages->next;
6473 ftrace_pages->next = start_pg;
6479 unsigned long end_offset;
6480 addr = ftrace_call_adjust(*p++);
6482 * Some architecture linkers will pad between
6483 * the different mcount_loc sections of different
6484 * object files to satisfy alignments.
6485 * Skip any NULL pointers.
6490 end_offset = (pg->index+1) * sizeof(pg->records[0]);
6491 if (end_offset > PAGE_SIZE << pg->order) {
6492 /* We should have allocated enough */
6493 if (WARN_ON(!pg->next))
6498 rec = &pg->records[pg->index++];
6502 /* We should have used all pages */
6505 /* Assign the last page to ftrace_pages */
6509 * We only need to disable interrupts on start up
6510 * because we are modifying code that an interrupt
6511 * may execute, and the modification is not atomic.
6512 * But for modules, nothing runs the code we modify
6513 * until we are finished with it, and there's no
6514 * reason to cause large interrupt latencies while we do it.
6517 local_irq_save(flags);
6518 ftrace_update_code(mod, start_pg);
6520 local_irq_restore(flags);
6523 mutex_unlock(&ftrace_lock);
6528 struct ftrace_mod_func {
6529 struct list_head list;
6535 struct ftrace_mod_map {
6536 struct rcu_head rcu;
6537 struct list_head list;
6539 unsigned long start_addr;
6540 unsigned long end_addr;
6541 struct list_head funcs;
6542 unsigned int num_funcs;
6545 static int ftrace_get_trampoline_kallsym(unsigned int symnum,
6546 unsigned long *value, char *type,
6547 char *name, char *module_name,
6550 struct ftrace_ops *op;
6552 list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) {
6553 if (!op->trampoline || symnum--)
6555 *value = op->trampoline;
6557 strlcpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN);
6558 strlcpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN);
6566 #ifdef CONFIG_MODULES
6568 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
6570 static LIST_HEAD(ftrace_mod_maps);
6572 static int referenced_filters(struct dyn_ftrace *rec)
6574 struct ftrace_ops *ops;
6577 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
6578 if (ops_references_rec(ops, rec)) {
6579 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT))
6581 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY))
6584 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
6585 rec->flags |= FTRACE_FL_REGS;
6586 if (cnt == 1 && ops->trampoline)
6587 rec->flags |= FTRACE_FL_TRAMP;
6589 rec->flags &= ~FTRACE_FL_TRAMP;
6597 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
6599 struct ftrace_func_entry *entry;
6600 struct dyn_ftrace *rec;
6603 if (ftrace_hash_empty(hash))
6606 for (i = 0; i < pg->index; i++) {
6607 rec = &pg->records[i];
6608 entry = __ftrace_lookup_ip(hash, rec->ip);
6610 * Do not allow this rec to match again.
6611 * Yeah, it may waste some memory, but will be removed
6612 * if/when the hash is modified again.
6619 /* Clear any records from hashes */
6620 static void clear_mod_from_hashes(struct ftrace_page *pg)
6622 struct trace_array *tr;
6624 mutex_lock(&trace_types_lock);
6625 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6626 if (!tr->ops || !tr->ops->func_hash)
6628 mutex_lock(&tr->ops->func_hash->regex_lock);
6629 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
6630 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
6631 mutex_unlock(&tr->ops->func_hash->regex_lock);
6633 mutex_unlock(&trace_types_lock);
6636 static void ftrace_free_mod_map(struct rcu_head *rcu)
6638 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
6639 struct ftrace_mod_func *mod_func;
6640 struct ftrace_mod_func *n;
6642 /* All the contents of mod_map are now not visible to readers */
6643 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
6644 kfree(mod_func->name);
6645 list_del(&mod_func->list);
6652 void ftrace_release_mod(struct module *mod)
6654 struct ftrace_mod_map *mod_map;
6655 struct ftrace_mod_map *n;
6656 struct dyn_ftrace *rec;
6657 struct ftrace_page **last_pg;
6658 struct ftrace_page *tmp_page = NULL;
6659 struct ftrace_page *pg;
6661 mutex_lock(&ftrace_lock);
6663 if (ftrace_disabled)
6666 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
6667 if (mod_map->mod == mod) {
6668 list_del_rcu(&mod_map->list);
6669 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
6675 * Each module has its own ftrace_pages, remove
6676 * them from the list.
6678 last_pg = &ftrace_pages_start;
6679 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
6680 rec = &pg->records[0];
6681 if (within_module_core(rec->ip, mod) ||
6682 within_module_init(rec->ip, mod)) {
6684 * As core pages are first, the first
6685 * page should never be a module page.
6687 if (WARN_ON(pg == ftrace_pages_start))
6690 /* Check if we are deleting the last page */
6691 if (pg == ftrace_pages)
6692 ftrace_pages = next_to_ftrace_page(last_pg);
6694 ftrace_update_tot_cnt -= pg->index;
6695 *last_pg = pg->next;
6697 pg->next = tmp_page;
6700 last_pg = &pg->next;
6703 mutex_unlock(&ftrace_lock);
6705 for (pg = tmp_page; pg; pg = tmp_page) {
6707 /* Needs to be called outside of ftrace_lock */
6708 clear_mod_from_hashes(pg);
6711 free_pages((unsigned long)pg->records, pg->order);
6712 ftrace_number_of_pages -= 1 << pg->order;
6714 tmp_page = pg->next;
6716 ftrace_number_of_groups--;
6720 void ftrace_module_enable(struct module *mod)
6722 struct dyn_ftrace *rec;
6723 struct ftrace_page *pg;
6725 mutex_lock(&ftrace_lock);
6727 if (ftrace_disabled)
6731 * If the tracing is enabled, go ahead and enable the record.
6733 * The reason not to enable the record immediately is the
6734 * inherent check of ftrace_make_nop/ftrace_make_call for
6735 * correct previous instructions. Making first the NOP
6736 * conversion puts the module to the correct state, thus
6737 * passing the ftrace_make_call check.
6739 * We also delay this to after the module code already set the
6740 * text to read-only, as we now need to set it back to read-write
6741 * so that we can modify the text.
6743 if (ftrace_start_up)
6744 ftrace_arch_code_modify_prepare();
6746 do_for_each_ftrace_rec(pg, rec) {
6749 * do_for_each_ftrace_rec() is a double loop.
6750 * module text shares the pg. If a record is
6751 * not part of this module, then skip this pg,
6752 * which the "break" will do.
6754 if (!within_module_core(rec->ip, mod) &&
6755 !within_module_init(rec->ip, mod))
6761 * When adding a module, we need to check if tracers are
6762 * currently enabled and if they are, and can trace this record,
6763 * we need to enable the module functions as well as update the
6764 * reference counts for those function records.
6766 if (ftrace_start_up)
6767 cnt += referenced_filters(rec);
6769 rec->flags &= ~FTRACE_FL_DISABLED;
6772 if (ftrace_start_up && cnt) {
6773 int failed = __ftrace_replace_code(rec, 1);
6775 ftrace_bug(failed, rec);
6780 } while_for_each_ftrace_rec();
6783 if (ftrace_start_up)
6784 ftrace_arch_code_modify_post_process();
6787 mutex_unlock(&ftrace_lock);
6789 process_cached_mods(mod->name);
6792 void ftrace_module_init(struct module *mod)
6794 if (ftrace_disabled || !mod->num_ftrace_callsites)
6797 ftrace_process_locs(mod, mod->ftrace_callsites,
6798 mod->ftrace_callsites + mod->num_ftrace_callsites);
6801 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6802 struct dyn_ftrace *rec)
6804 struct ftrace_mod_func *mod_func;
6805 unsigned long symsize;
6806 unsigned long offset;
6807 char str[KSYM_SYMBOL_LEN];
6811 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
6815 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
6819 mod_func->name = kstrdup(str, GFP_KERNEL);
6820 if (!mod_func->name) {
6825 mod_func->ip = rec->ip - offset;
6826 mod_func->size = symsize;
6828 mod_map->num_funcs++;
6830 list_add_rcu(&mod_func->list, &mod_map->funcs);
6833 static struct ftrace_mod_map *
6834 allocate_ftrace_mod_map(struct module *mod,
6835 unsigned long start, unsigned long end)
6837 struct ftrace_mod_map *mod_map;
6839 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
6844 mod_map->start_addr = start;
6845 mod_map->end_addr = end;
6846 mod_map->num_funcs = 0;
6848 INIT_LIST_HEAD_RCU(&mod_map->funcs);
6850 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
6856 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
6857 unsigned long addr, unsigned long *size,
6858 unsigned long *off, char *sym)
6860 struct ftrace_mod_func *found_func = NULL;
6861 struct ftrace_mod_func *mod_func;
6863 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6864 if (addr >= mod_func->ip &&
6865 addr < mod_func->ip + mod_func->size) {
6866 found_func = mod_func;
6873 *size = found_func->size;
6875 *off = addr - found_func->ip;
6877 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
6879 return found_func->name;
6886 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6887 unsigned long *off, char **modname, char *sym)
6889 struct ftrace_mod_map *mod_map;
6890 const char *ret = NULL;
6892 /* mod_map is freed via call_rcu() */
6894 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6895 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6898 *modname = mod_map->mod->name;
6907 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6908 char *type, char *name,
6909 char *module_name, int *exported)
6911 struct ftrace_mod_map *mod_map;
6912 struct ftrace_mod_func *mod_func;
6916 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6918 if (symnum >= mod_map->num_funcs) {
6919 symnum -= mod_map->num_funcs;
6923 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6929 *value = mod_func->ip;
6931 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6932 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6940 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6941 module_name, exported);
6947 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6948 struct dyn_ftrace *rec) { }
6949 static inline struct ftrace_mod_map *
6950 allocate_ftrace_mod_map(struct module *mod,
6951 unsigned long start, unsigned long end)
6955 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6956 char *type, char *name, char *module_name,
6962 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6963 module_name, exported);
6967 #endif /* CONFIG_MODULES */
6969 struct ftrace_init_func {
6970 struct list_head list;
6974 /* Clear any init ips from hashes */
6976 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6978 struct ftrace_func_entry *entry;
6980 entry = ftrace_lookup_ip(hash, func->ip);
6982 * Do not allow this rec to match again.
6983 * Yeah, it may waste some memory, but will be removed
6984 * if/when the hash is modified again.
6991 clear_func_from_hashes(struct ftrace_init_func *func)
6993 struct trace_array *tr;
6995 mutex_lock(&trace_types_lock);
6996 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6997 if (!tr->ops || !tr->ops->func_hash)
6999 mutex_lock(&tr->ops->func_hash->regex_lock);
7000 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
7001 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
7002 mutex_unlock(&tr->ops->func_hash->regex_lock);
7004 mutex_unlock(&trace_types_lock);
7007 static void add_to_clear_hash_list(struct list_head *clear_list,
7008 struct dyn_ftrace *rec)
7010 struct ftrace_init_func *func;
7012 func = kmalloc(sizeof(*func), GFP_KERNEL);
7014 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
7019 list_add(&func->list, clear_list);
7022 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
7024 unsigned long start = (unsigned long)(start_ptr);
7025 unsigned long end = (unsigned long)(end_ptr);
7026 struct ftrace_page **last_pg = &ftrace_pages_start;
7027 struct ftrace_page *pg;
7028 struct dyn_ftrace *rec;
7029 struct dyn_ftrace key;
7030 struct ftrace_mod_map *mod_map = NULL;
7031 struct ftrace_init_func *func, *func_next;
7032 struct list_head clear_hash;
7034 INIT_LIST_HEAD(&clear_hash);
7037 key.flags = end; /* overload flags, as it is unsigned long */
7039 mutex_lock(&ftrace_lock);
7042 * If we are freeing module init memory, then check if
7043 * any tracer is active. If so, we need to save a mapping of
7044 * the module functions being freed with the address.
7046 if (mod && ftrace_ops_list != &ftrace_list_end)
7047 mod_map = allocate_ftrace_mod_map(mod, start, end);
7049 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
7050 if (end < pg->records[0].ip ||
7051 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
7054 rec = bsearch(&key, pg->records, pg->index,
7055 sizeof(struct dyn_ftrace),
7060 /* rec will be cleared from hashes after ftrace_lock unlock */
7061 add_to_clear_hash_list(&clear_hash, rec);
7064 save_ftrace_mod_rec(mod_map, rec);
7067 ftrace_update_tot_cnt--;
7069 *last_pg = pg->next;
7071 free_pages((unsigned long)pg->records, pg->order);
7072 ftrace_number_of_pages -= 1 << pg->order;
7074 ftrace_number_of_groups--;
7076 pg = container_of(last_pg, struct ftrace_page, next);
7081 memmove(rec, rec + 1,
7082 (pg->index - (rec - pg->records)) * sizeof(*rec));
7083 /* More than one function may be in this block */
7086 mutex_unlock(&ftrace_lock);
7088 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
7089 clear_func_from_hashes(func);
7094 void __init ftrace_free_init_mem(void)
7096 void *start = (void *)(&__init_begin);
7097 void *end = (void *)(&__init_end);
7099 ftrace_free_mem(NULL, start, end);
7102 int __init __weak ftrace_dyn_arch_init(void)
7107 void __init ftrace_init(void)
7109 extern unsigned long __start_mcount_loc[];
7110 extern unsigned long __stop_mcount_loc[];
7111 unsigned long count, flags;
7114 local_irq_save(flags);
7115 ret = ftrace_dyn_arch_init();
7116 local_irq_restore(flags);
7120 count = __stop_mcount_loc - __start_mcount_loc;
7122 pr_info("ftrace: No functions to be traced?\n");
7126 pr_info("ftrace: allocating %ld entries in %ld pages\n",
7127 count, count / ENTRIES_PER_PAGE + 1);
7129 last_ftrace_enabled = ftrace_enabled = 1;
7131 ret = ftrace_process_locs(NULL,
7135 pr_info("ftrace: allocated %ld pages with %ld groups\n",
7136 ftrace_number_of_pages, ftrace_number_of_groups);
7138 set_ftrace_early_filters();
7142 ftrace_disabled = 1;
7145 /* Do nothing if arch does not support this */
7146 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
7150 static void ftrace_update_trampoline(struct ftrace_ops *ops)
7152 unsigned long trampoline = ops->trampoline;
7154 arch_ftrace_update_trampoline(ops);
7155 if (ops->trampoline && ops->trampoline != trampoline &&
7156 (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) {
7157 /* Add to kallsyms before the perf events */
7158 ftrace_add_trampoline_to_kallsyms(ops);
7159 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
7160 ops->trampoline, ops->trampoline_size, false,
7161 FTRACE_TRAMPOLINE_SYM);
7163 * Record the perf text poke event after the ksymbol register
7166 perf_event_text_poke((void *)ops->trampoline, NULL, 0,
7167 (void *)ops->trampoline,
7168 ops->trampoline_size);
7172 void ftrace_init_trace_array(struct trace_array *tr)
7174 INIT_LIST_HEAD(&tr->func_probes);
7175 INIT_LIST_HEAD(&tr->mod_trace);
7176 INIT_LIST_HEAD(&tr->mod_notrace);
7180 struct ftrace_ops global_ops = {
7181 .func = ftrace_stub,
7182 .flags = FTRACE_OPS_FL_INITIALIZED |
7186 static int __init ftrace_nodyn_init(void)
7191 core_initcall(ftrace_nodyn_init);
7193 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
7194 static inline void ftrace_startup_enable(int command) { }
7195 static inline void ftrace_startup_all(int command) { }
7197 # define ftrace_startup_sysctl() do { } while (0)
7198 # define ftrace_shutdown_sysctl() do { } while (0)
7200 static void ftrace_update_trampoline(struct ftrace_ops *ops)
7204 #endif /* CONFIG_DYNAMIC_FTRACE */
7206 __init void ftrace_init_global_array_ops(struct trace_array *tr)
7208 tr->ops = &global_ops;
7209 tr->ops->private = tr;
7210 ftrace_init_trace_array(tr);
7213 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
7215 /* If we filter on pids, update to use the pid function */
7216 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
7217 if (WARN_ON(tr->ops->func != ftrace_stub))
7218 printk("ftrace ops had %pS for function\n",
7221 tr->ops->func = func;
7222 tr->ops->private = tr;
7225 void ftrace_reset_array_ops(struct trace_array *tr)
7227 tr->ops->func = ftrace_stub;
7230 static nokprobe_inline void
7231 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
7232 struct ftrace_ops *ignored, struct ftrace_regs *fregs)
7234 struct pt_regs *regs = ftrace_get_regs(fregs);
7235 struct ftrace_ops *op;
7239 * The ftrace_test_and_set_recursion() will disable preemption,
7240 * which is required since some of the ops may be dynamically
7241 * allocated, they must be freed after a synchronize_rcu().
7243 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
7247 do_for_each_ftrace_op(op, ftrace_ops_list) {
7248 /* Stub functions don't need to be called nor tested */
7249 if (op->flags & FTRACE_OPS_FL_STUB)
7252 * Check the following for each ops before calling their func:
7253 * if RCU flag is set, then rcu_is_watching() must be true
7254 * if PER_CPU is set, then ftrace_function_local_disable()
7256 * Otherwise test if the ip matches the ops filter
7258 * If any of the above fails then the op->func() is not executed.
7260 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
7261 ftrace_ops_test(op, ip, regs)) {
7262 if (FTRACE_WARN_ON(!op->func)) {
7263 pr_warn("op=%p %pS\n", op, op);
7266 op->func(ip, parent_ip, op, fregs);
7268 } while_for_each_ftrace_op(op);
7270 trace_clear_recursion(bit);
7274 * Some archs only support passing ip and parent_ip. Even though
7275 * the list function ignores the op parameter, we do not want any
7276 * C side effects, where a function is called without the caller
7277 * sending a third parameter.
7278 * Archs are to support both the regs and ftrace_ops at the same time.
7279 * If they support ftrace_ops, it is assumed they support regs.
7280 * If call backs want to use regs, they must either check for regs
7281 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
7282 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
7283 * An architecture can pass partial regs with ftrace_ops and still
7284 * set the ARCH_SUPPORTS_FTRACE_OPS.
7286 * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be
7287 * arch_ftrace_ops_list_func.
7289 #if ARCH_SUPPORTS_FTRACE_OPS
7290 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
7291 struct ftrace_ops *op, struct ftrace_regs *fregs)
7293 __ftrace_ops_list_func(ip, parent_ip, NULL, fregs);
7296 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
7298 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
7301 NOKPROBE_SYMBOL(arch_ftrace_ops_list_func);
7304 * If there's only one function registered but it does not support
7305 * recursion, needs RCU protection and/or requires per cpu handling, then
7306 * this function will be called by the mcount trampoline.
7308 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
7309 struct ftrace_ops *op, struct ftrace_regs *fregs)
7313 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
7317 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
7318 op->func(ip, parent_ip, op, fregs);
7320 trace_clear_recursion(bit);
7322 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
7325 * ftrace_ops_get_func - get the function a trampoline should call
7326 * @ops: the ops to get the function for
7328 * Normally the mcount trampoline will call the ops->func, but there
7329 * are times that it should not. For example, if the ops does not
7330 * have its own recursion protection, then it should call the
7331 * ftrace_ops_assist_func() instead.
7333 * Returns the function that the trampoline should call for @ops.
7335 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
7338 * If the function does not handle recursion or needs to be RCU safe,
7339 * then we need to call the assist handler.
7341 if (ops->flags & (FTRACE_OPS_FL_RECURSION |
7343 return ftrace_ops_assist_func;
7349 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
7350 struct task_struct *prev, struct task_struct *next)
7352 struct trace_array *tr = data;
7353 struct trace_pid_list *pid_list;
7354 struct trace_pid_list *no_pid_list;
7356 pid_list = rcu_dereference_sched(tr->function_pids);
7357 no_pid_list = rcu_dereference_sched(tr->function_no_pids);
7359 if (trace_ignore_this_task(pid_list, no_pid_list, next))
7360 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7363 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7368 ftrace_pid_follow_sched_process_fork(void *data,
7369 struct task_struct *self,
7370 struct task_struct *task)
7372 struct trace_pid_list *pid_list;
7373 struct trace_array *tr = data;
7375 pid_list = rcu_dereference_sched(tr->function_pids);
7376 trace_filter_add_remove_task(pid_list, self, task);
7378 pid_list = rcu_dereference_sched(tr->function_no_pids);
7379 trace_filter_add_remove_task(pid_list, self, task);
7383 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
7385 struct trace_pid_list *pid_list;
7386 struct trace_array *tr = data;
7388 pid_list = rcu_dereference_sched(tr->function_pids);
7389 trace_filter_add_remove_task(pid_list, NULL, task);
7391 pid_list = rcu_dereference_sched(tr->function_no_pids);
7392 trace_filter_add_remove_task(pid_list, NULL, task);
7395 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
7398 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7400 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
7403 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7405 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
7410 static void clear_ftrace_pids(struct trace_array *tr, int type)
7412 struct trace_pid_list *pid_list;
7413 struct trace_pid_list *no_pid_list;
7416 pid_list = rcu_dereference_protected(tr->function_pids,
7417 lockdep_is_held(&ftrace_lock));
7418 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7419 lockdep_is_held(&ftrace_lock));
7421 /* Make sure there's something to do */
7422 if (!pid_type_enabled(type, pid_list, no_pid_list))
7425 /* See if the pids still need to be checked after this */
7426 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
7427 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7428 for_each_possible_cpu(cpu)
7429 per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE;
7432 if (type & TRACE_PIDS)
7433 rcu_assign_pointer(tr->function_pids, NULL);
7435 if (type & TRACE_NO_PIDS)
7436 rcu_assign_pointer(tr->function_no_pids, NULL);
7438 /* Wait till all users are no longer using pid filtering */
7441 if ((type & TRACE_PIDS) && pid_list)
7442 trace_pid_list_free(pid_list);
7444 if ((type & TRACE_NO_PIDS) && no_pid_list)
7445 trace_pid_list_free(no_pid_list);
7448 void ftrace_clear_pids(struct trace_array *tr)
7450 mutex_lock(&ftrace_lock);
7452 clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
7454 mutex_unlock(&ftrace_lock);
7457 static void ftrace_pid_reset(struct trace_array *tr, int type)
7459 mutex_lock(&ftrace_lock);
7460 clear_ftrace_pids(tr, type);
7462 ftrace_update_pid_func();
7463 ftrace_startup_all(0);
7465 mutex_unlock(&ftrace_lock);
7468 /* Greater than any max PID */
7469 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
7471 static void *fpid_start(struct seq_file *m, loff_t *pos)
7474 struct trace_pid_list *pid_list;
7475 struct trace_array *tr = m->private;
7477 mutex_lock(&ftrace_lock);
7478 rcu_read_lock_sched();
7480 pid_list = rcu_dereference_sched(tr->function_pids);
7483 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7485 return trace_pid_start(pid_list, pos);
7488 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
7490 struct trace_array *tr = m->private;
7491 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
7493 if (v == FTRACE_NO_PIDS) {
7497 return trace_pid_next(pid_list, v, pos);
7500 static void fpid_stop(struct seq_file *m, void *p)
7503 rcu_read_unlock_sched();
7504 mutex_unlock(&ftrace_lock);
7507 static int fpid_show(struct seq_file *m, void *v)
7509 if (v == FTRACE_NO_PIDS) {
7510 seq_puts(m, "no pid\n");
7514 return trace_pid_show(m, v);
7517 static const struct seq_operations ftrace_pid_sops = {
7518 .start = fpid_start,
7524 static void *fnpid_start(struct seq_file *m, loff_t *pos)
7527 struct trace_pid_list *pid_list;
7528 struct trace_array *tr = m->private;
7530 mutex_lock(&ftrace_lock);
7531 rcu_read_lock_sched();
7533 pid_list = rcu_dereference_sched(tr->function_no_pids);
7536 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7538 return trace_pid_start(pid_list, pos);
7541 static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos)
7543 struct trace_array *tr = m->private;
7544 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids);
7546 if (v == FTRACE_NO_PIDS) {
7550 return trace_pid_next(pid_list, v, pos);
7553 static const struct seq_operations ftrace_no_pid_sops = {
7554 .start = fnpid_start,
7560 static int pid_open(struct inode *inode, struct file *file, int type)
7562 const struct seq_operations *seq_ops;
7563 struct trace_array *tr = inode->i_private;
7567 ret = tracing_check_open_get_tr(tr);
7571 if ((file->f_mode & FMODE_WRITE) &&
7572 (file->f_flags & O_TRUNC))
7573 ftrace_pid_reset(tr, type);
7577 seq_ops = &ftrace_pid_sops;
7580 seq_ops = &ftrace_no_pid_sops;
7583 trace_array_put(tr);
7588 ret = seq_open(file, seq_ops);
7590 trace_array_put(tr);
7592 m = file->private_data;
7593 /* copy tr over to seq ops */
7601 ftrace_pid_open(struct inode *inode, struct file *file)
7603 return pid_open(inode, file, TRACE_PIDS);
7607 ftrace_no_pid_open(struct inode *inode, struct file *file)
7609 return pid_open(inode, file, TRACE_NO_PIDS);
7612 static void ignore_task_cpu(void *data)
7614 struct trace_array *tr = data;
7615 struct trace_pid_list *pid_list;
7616 struct trace_pid_list *no_pid_list;
7619 * This function is called by on_each_cpu() while the
7620 * event_mutex is held.
7622 pid_list = rcu_dereference_protected(tr->function_pids,
7623 mutex_is_locked(&ftrace_lock));
7624 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7625 mutex_is_locked(&ftrace_lock));
7627 if (trace_ignore_this_task(pid_list, no_pid_list, current))
7628 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7631 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7636 pid_write(struct file *filp, const char __user *ubuf,
7637 size_t cnt, loff_t *ppos, int type)
7639 struct seq_file *m = filp->private_data;
7640 struct trace_array *tr = m->private;
7641 struct trace_pid_list *filtered_pids;
7642 struct trace_pid_list *other_pids;
7643 struct trace_pid_list *pid_list;
7649 mutex_lock(&ftrace_lock);
7653 filtered_pids = rcu_dereference_protected(tr->function_pids,
7654 lockdep_is_held(&ftrace_lock));
7655 other_pids = rcu_dereference_protected(tr->function_no_pids,
7656 lockdep_is_held(&ftrace_lock));
7659 filtered_pids = rcu_dereference_protected(tr->function_no_pids,
7660 lockdep_is_held(&ftrace_lock));
7661 other_pids = rcu_dereference_protected(tr->function_pids,
7662 lockdep_is_held(&ftrace_lock));
7670 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
7676 rcu_assign_pointer(tr->function_pids, pid_list);
7679 rcu_assign_pointer(tr->function_no_pids, pid_list);
7684 if (filtered_pids) {
7686 trace_pid_list_free(filtered_pids);
7687 } else if (pid_list && !other_pids) {
7688 /* Register a probe to set whether to ignore the tracing of a task */
7689 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7693 * Ignoring of pids is done at task switch. But we have to
7694 * check for those tasks that are currently running.
7695 * Always do this in case a pid was appended or removed.
7697 on_each_cpu(ignore_task_cpu, tr, 1);
7699 ftrace_update_pid_func();
7700 ftrace_startup_all(0);
7702 mutex_unlock(&ftrace_lock);
7711 ftrace_pid_write(struct file *filp, const char __user *ubuf,
7712 size_t cnt, loff_t *ppos)
7714 return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
7718 ftrace_no_pid_write(struct file *filp, const char __user *ubuf,
7719 size_t cnt, loff_t *ppos)
7721 return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
7725 ftrace_pid_release(struct inode *inode, struct file *file)
7727 struct trace_array *tr = inode->i_private;
7729 trace_array_put(tr);
7731 return seq_release(inode, file);
7734 static const struct file_operations ftrace_pid_fops = {
7735 .open = ftrace_pid_open,
7736 .write = ftrace_pid_write,
7738 .llseek = tracing_lseek,
7739 .release = ftrace_pid_release,
7742 static const struct file_operations ftrace_no_pid_fops = {
7743 .open = ftrace_no_pid_open,
7744 .write = ftrace_no_pid_write,
7746 .llseek = tracing_lseek,
7747 .release = ftrace_pid_release,
7750 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
7752 trace_create_file("set_ftrace_pid", TRACE_MODE_WRITE, d_tracer,
7753 tr, &ftrace_pid_fops);
7754 trace_create_file("set_ftrace_notrace_pid", TRACE_MODE_WRITE,
7755 d_tracer, tr, &ftrace_no_pid_fops);
7758 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
7759 struct dentry *d_tracer)
7761 /* Only the top level directory has the dyn_tracefs and profile */
7762 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
7764 ftrace_init_dyn_tracefs(d_tracer);
7765 ftrace_profile_tracefs(d_tracer);
7769 * ftrace_kill - kill ftrace
7771 * This function should be used by panic code. It stops ftrace
7772 * but in a not so nice way. If you need to simply kill ftrace
7773 * from a non-atomic section, use ftrace_kill.
7775 void ftrace_kill(void)
7777 ftrace_disabled = 1;
7779 ftrace_trace_function = ftrace_stub;
7783 * ftrace_is_dead - Test if ftrace is dead or not.
7785 * Returns 1 if ftrace is "dead", zero otherwise.
7787 int ftrace_is_dead(void)
7789 return ftrace_disabled;
7793 * register_ftrace_function - register a function for profiling
7794 * @ops - ops structure that holds the function for profiling.
7796 * Register a function to be called by all functions in the
7799 * Note: @ops->func and all the functions it calls must be labeled
7800 * with "notrace", otherwise it will go into a
7803 int register_ftrace_function(struct ftrace_ops *ops)
7807 ftrace_ops_init(ops);
7809 mutex_lock(&ftrace_lock);
7811 ret = ftrace_startup(ops, 0);
7813 mutex_unlock(&ftrace_lock);
7817 EXPORT_SYMBOL_GPL(register_ftrace_function);
7820 * unregister_ftrace_function - unregister a function for profiling.
7821 * @ops - ops structure that holds the function to unregister
7823 * Unregister a function that was added to be called by ftrace profiling.
7825 int unregister_ftrace_function(struct ftrace_ops *ops)
7829 mutex_lock(&ftrace_lock);
7830 ret = ftrace_shutdown(ops, 0);
7831 mutex_unlock(&ftrace_lock);
7835 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
7837 static bool is_permanent_ops_registered(void)
7839 struct ftrace_ops *op;
7841 do_for_each_ftrace_op(op, ftrace_ops_list) {
7842 if (op->flags & FTRACE_OPS_FL_PERMANENT)
7844 } while_for_each_ftrace_op(op);
7850 ftrace_enable_sysctl(struct ctl_table *table, int write,
7851 void *buffer, size_t *lenp, loff_t *ppos)
7855 mutex_lock(&ftrace_lock);
7857 if (unlikely(ftrace_disabled))
7860 ret = proc_dointvec(table, write, buffer, lenp, ppos);
7862 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
7865 if (ftrace_enabled) {
7867 /* we are starting ftrace again */
7868 if (rcu_dereference_protected(ftrace_ops_list,
7869 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
7870 update_ftrace_function();
7872 ftrace_startup_sysctl();
7875 if (is_permanent_ops_registered()) {
7876 ftrace_enabled = true;
7881 /* stopping ftrace calls (just send to ftrace_stub) */
7882 ftrace_trace_function = ftrace_stub;
7884 ftrace_shutdown_sysctl();
7887 last_ftrace_enabled = !!ftrace_enabled;
7889 mutex_unlock(&ftrace_lock);