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 __maybe_unused 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 the ftrace location
1572 * @ip: the instruction pointer to check
1574 * If @ip matches the ftrace location, return @ip.
1575 * If @ip matches sym+0, return sym's ftrace location.
1576 * Otherwise, return 0.
1578 unsigned long ftrace_location(unsigned long ip)
1580 struct dyn_ftrace *rec;
1581 unsigned long offset;
1584 rec = lookup_rec(ip, ip);
1586 if (!kallsyms_lookup_size_offset(ip, &size, &offset))
1589 /* map sym+0 to __fentry__ */
1591 rec = lookup_rec(ip, ip + size - 1);
1602 * ftrace_text_reserved - return true if range contains an ftrace location
1603 * @start: start of range to search
1604 * @end: end of range to search (inclusive). @end points to the last byte to check.
1606 * Returns 1 if @start and @end contains a ftrace location.
1607 * That is, the instruction that is either a NOP or call to
1608 * the function tracer. It checks the ftrace internal tables to
1609 * determine if the address belongs or not.
1611 int ftrace_text_reserved(const void *start, const void *end)
1615 ret = ftrace_location_range((unsigned long)start,
1616 (unsigned long)end);
1621 /* Test if ops registered to this rec needs regs */
1622 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1624 struct ftrace_ops *ops;
1625 bool keep_regs = false;
1627 for (ops = ftrace_ops_list;
1628 ops != &ftrace_list_end; ops = ops->next) {
1629 /* pass rec in as regs to have non-NULL val */
1630 if (ftrace_ops_test(ops, rec->ip, rec)) {
1631 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1641 static struct ftrace_ops *
1642 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1643 static struct ftrace_ops *
1644 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1645 static struct ftrace_ops *
1646 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1648 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1652 struct ftrace_hash *hash;
1653 struct ftrace_hash *other_hash;
1654 struct ftrace_page *pg;
1655 struct dyn_ftrace *rec;
1656 bool update = false;
1660 /* Only update if the ops has been registered */
1661 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1665 * In the filter_hash case:
1666 * If the count is zero, we update all records.
1667 * Otherwise we just update the items in the hash.
1669 * In the notrace_hash case:
1670 * We enable the update in the hash.
1671 * As disabling notrace means enabling the tracing,
1672 * and enabling notrace means disabling, the inc variable
1676 hash = ops->func_hash->filter_hash;
1677 other_hash = ops->func_hash->notrace_hash;
1678 if (ftrace_hash_empty(hash))
1682 hash = ops->func_hash->notrace_hash;
1683 other_hash = ops->func_hash->filter_hash;
1685 * If the notrace hash has no items,
1686 * then there's nothing to do.
1688 if (ftrace_hash_empty(hash))
1692 do_for_each_ftrace_rec(pg, rec) {
1693 int in_other_hash = 0;
1697 if (rec->flags & FTRACE_FL_DISABLED)
1702 * Only the filter_hash affects all records.
1703 * Update if the record is not in the notrace hash.
1705 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1708 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1709 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1712 * If filter_hash is set, we want to match all functions
1713 * that are in the hash but not in the other hash.
1715 * If filter_hash is not set, then we are decrementing.
1716 * That means we match anything that is in the hash
1717 * and also in the other_hash. That is, we need to turn
1718 * off functions in the other hash because they are disabled
1721 if (filter_hash && in_hash && !in_other_hash)
1723 else if (!filter_hash && in_hash &&
1724 (in_other_hash || ftrace_hash_empty(other_hash)))
1732 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1735 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1736 rec->flags |= FTRACE_FL_DIRECT;
1739 * If there's only a single callback registered to a
1740 * function, and the ops has a trampoline registered
1741 * for it, then we can call it directly.
1743 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1744 rec->flags |= FTRACE_FL_TRAMP;
1747 * If we are adding another function callback
1748 * to this function, and the previous had a
1749 * custom trampoline in use, then we need to go
1750 * back to the default trampoline.
1752 rec->flags &= ~FTRACE_FL_TRAMP;
1755 * If any ops wants regs saved for this function
1756 * then all ops will get saved regs.
1758 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1759 rec->flags |= FTRACE_FL_REGS;
1761 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1766 * Only the internal direct_ops should have the
1767 * DIRECT flag set. Thus, if it is removing a
1768 * function, then that function should no longer
1771 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1772 rec->flags &= ~FTRACE_FL_DIRECT;
1775 * If the rec had REGS enabled and the ops that is
1776 * being removed had REGS set, then see if there is
1777 * still any ops for this record that wants regs.
1778 * If not, we can stop recording them.
1780 if (ftrace_rec_count(rec) > 0 &&
1781 rec->flags & FTRACE_FL_REGS &&
1782 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1783 if (!test_rec_ops_needs_regs(rec))
1784 rec->flags &= ~FTRACE_FL_REGS;
1788 * The TRAMP needs to be set only if rec count
1789 * is decremented to one, and the ops that is
1790 * left has a trampoline. As TRAMP can only be
1791 * enabled if there is only a single ops attached
1794 if (ftrace_rec_count(rec) == 1 &&
1795 ftrace_find_tramp_ops_any_other(rec, ops))
1796 rec->flags |= FTRACE_FL_TRAMP;
1798 rec->flags &= ~FTRACE_FL_TRAMP;
1801 * flags will be cleared in ftrace_check_record()
1802 * if rec count is zero.
1807 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1808 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
1810 /* Shortcut, if we handled all records, we are done. */
1811 if (!all && count == hash->count)
1813 } while_for_each_ftrace_rec();
1818 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1821 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1824 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1827 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1830 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1831 int filter_hash, int inc)
1833 struct ftrace_ops *op;
1835 __ftrace_hash_rec_update(ops, filter_hash, inc);
1837 if (ops->func_hash != &global_ops.local_hash)
1841 * If the ops shares the global_ops hash, then we need to update
1842 * all ops that are enabled and use this hash.
1844 do_for_each_ftrace_op(op, ftrace_ops_list) {
1848 if (op->func_hash == &global_ops.local_hash)
1849 __ftrace_hash_rec_update(op, filter_hash, inc);
1850 } while_for_each_ftrace_op(op);
1853 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1856 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1859 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1862 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1866 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1867 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1868 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1869 * Note that old_hash and new_hash has below meanings
1870 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1871 * - If the hash is EMPTY_HASH, it hits nothing
1872 * - Anything else hits the recs which match the hash entries.
1874 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1875 struct ftrace_hash *old_hash,
1876 struct ftrace_hash *new_hash)
1878 struct ftrace_page *pg;
1879 struct dyn_ftrace *rec, *end = NULL;
1882 /* Only update if the ops has been registered */
1883 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1886 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1890 * Since the IPMODIFY is a very address sensitive action, we do not
1891 * allow ftrace_ops to set all functions to new hash.
1893 if (!new_hash || !old_hash)
1896 /* Update rec->flags */
1897 do_for_each_ftrace_rec(pg, rec) {
1899 if (rec->flags & FTRACE_FL_DISABLED)
1902 /* We need to update only differences of filter_hash */
1903 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1904 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1905 if (in_old == in_new)
1909 /* New entries must ensure no others are using it */
1910 if (rec->flags & FTRACE_FL_IPMODIFY)
1912 rec->flags |= FTRACE_FL_IPMODIFY;
1913 } else /* Removed entry */
1914 rec->flags &= ~FTRACE_FL_IPMODIFY;
1915 } while_for_each_ftrace_rec();
1922 /* Roll back what we did above */
1923 do_for_each_ftrace_rec(pg, rec) {
1925 if (rec->flags & FTRACE_FL_DISABLED)
1931 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1932 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1933 if (in_old == in_new)
1937 rec->flags &= ~FTRACE_FL_IPMODIFY;
1939 rec->flags |= FTRACE_FL_IPMODIFY;
1940 } while_for_each_ftrace_rec();
1946 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1948 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1950 if (ftrace_hash_empty(hash))
1953 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1956 /* Disabling always succeeds */
1957 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1959 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1961 if (ftrace_hash_empty(hash))
1964 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1967 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1968 struct ftrace_hash *new_hash)
1970 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1972 if (ftrace_hash_empty(old_hash))
1975 if (ftrace_hash_empty(new_hash))
1978 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1981 static void print_ip_ins(const char *fmt, const unsigned char *p)
1983 char ins[MCOUNT_INSN_SIZE];
1986 if (copy_from_kernel_nofault(ins, p, MCOUNT_INSN_SIZE)) {
1987 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
1991 printk(KERN_CONT "%s", fmt);
1993 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1994 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
1997 enum ftrace_bug_type ftrace_bug_type;
1998 const void *ftrace_expected;
2000 static void print_bug_type(void)
2002 switch (ftrace_bug_type) {
2003 case FTRACE_BUG_UNKNOWN:
2005 case FTRACE_BUG_INIT:
2006 pr_info("Initializing ftrace call sites\n");
2008 case FTRACE_BUG_NOP:
2009 pr_info("Setting ftrace call site to NOP\n");
2011 case FTRACE_BUG_CALL:
2012 pr_info("Setting ftrace call site to call ftrace function\n");
2014 case FTRACE_BUG_UPDATE:
2015 pr_info("Updating ftrace call site to call a different ftrace function\n");
2021 * ftrace_bug - report and shutdown function tracer
2022 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2023 * @rec: The record that failed
2025 * The arch code that enables or disables the function tracing
2026 * can call ftrace_bug() when it has detected a problem in
2027 * modifying the code. @failed should be one of either:
2028 * EFAULT - if the problem happens on reading the @ip address
2029 * EINVAL - if what is read at @ip is not what was expected
2030 * EPERM - if the problem happens on writing to the @ip address
2032 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2034 unsigned long ip = rec ? rec->ip : 0;
2036 pr_info("------------[ ftrace bug ]------------\n");
2040 pr_info("ftrace faulted on modifying ");
2041 print_ip_sym(KERN_INFO, ip);
2044 pr_info("ftrace failed to modify ");
2045 print_ip_sym(KERN_INFO, ip);
2046 print_ip_ins(" actual: ", (unsigned char *)ip);
2048 if (ftrace_expected) {
2049 print_ip_ins(" expected: ", ftrace_expected);
2054 pr_info("ftrace faulted on writing ");
2055 print_ip_sym(KERN_INFO, ip);
2058 pr_info("ftrace faulted on unknown error ");
2059 print_ip_sym(KERN_INFO, ip);
2063 struct ftrace_ops *ops = NULL;
2065 pr_info("ftrace record flags: %lx\n", rec->flags);
2066 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2067 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2068 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2069 ops = ftrace_find_tramp_ops_any(rec);
2072 pr_cont("\ttramp: %pS (%pS)",
2073 (void *)ops->trampoline,
2075 ops = ftrace_find_tramp_ops_next(rec, ops);
2078 pr_cont("\ttramp: ERROR!");
2081 ip = ftrace_get_addr_curr(rec);
2082 pr_cont("\n expected tramp: %lx\n", ip);
2085 FTRACE_WARN_ON_ONCE(1);
2088 static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
2090 unsigned long flag = 0UL;
2092 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2094 if (rec->flags & FTRACE_FL_DISABLED)
2095 return FTRACE_UPDATE_IGNORE;
2098 * If we are updating calls:
2100 * If the record has a ref count, then we need to enable it
2101 * because someone is using it.
2103 * Otherwise we make sure its disabled.
2105 * If we are disabling calls, then disable all records that
2108 if (enable && ftrace_rec_count(rec))
2109 flag = FTRACE_FL_ENABLED;
2112 * If enabling and the REGS flag does not match the REGS_EN, or
2113 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2114 * this record. Set flags to fail the compare against ENABLED.
2115 * Same for direct calls.
2118 if (!(rec->flags & FTRACE_FL_REGS) !=
2119 !(rec->flags & FTRACE_FL_REGS_EN))
2120 flag |= FTRACE_FL_REGS;
2122 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2123 !(rec->flags & FTRACE_FL_TRAMP_EN))
2124 flag |= FTRACE_FL_TRAMP;
2127 * Direct calls are special, as count matters.
2128 * We must test the record for direct, if the
2129 * DIRECT and DIRECT_EN do not match, but only
2130 * if the count is 1. That's because, if the
2131 * count is something other than one, we do not
2132 * want the direct enabled (it will be done via the
2133 * direct helper). But if DIRECT_EN is set, and
2134 * the count is not one, we need to clear it.
2136 if (ftrace_rec_count(rec) == 1) {
2137 if (!(rec->flags & FTRACE_FL_DIRECT) !=
2138 !(rec->flags & FTRACE_FL_DIRECT_EN))
2139 flag |= FTRACE_FL_DIRECT;
2140 } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
2141 flag |= FTRACE_FL_DIRECT;
2145 /* If the state of this record hasn't changed, then do nothing */
2146 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2147 return FTRACE_UPDATE_IGNORE;
2150 /* Save off if rec is being enabled (for return value) */
2151 flag ^= rec->flags & FTRACE_FL_ENABLED;
2154 rec->flags |= FTRACE_FL_ENABLED;
2155 if (flag & FTRACE_FL_REGS) {
2156 if (rec->flags & FTRACE_FL_REGS)
2157 rec->flags |= FTRACE_FL_REGS_EN;
2159 rec->flags &= ~FTRACE_FL_REGS_EN;
2161 if (flag & FTRACE_FL_TRAMP) {
2162 if (rec->flags & FTRACE_FL_TRAMP)
2163 rec->flags |= FTRACE_FL_TRAMP_EN;
2165 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2168 if (flag & FTRACE_FL_DIRECT) {
2170 * If there's only one user (direct_ops helper)
2171 * then we can call the direct function
2172 * directly (no ftrace trampoline).
2174 if (ftrace_rec_count(rec) == 1) {
2175 if (rec->flags & FTRACE_FL_DIRECT)
2176 rec->flags |= FTRACE_FL_DIRECT_EN;
2178 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2181 * Can only call directly if there's
2182 * only one callback to the function.
2184 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2190 * If this record is being updated from a nop, then
2191 * return UPDATE_MAKE_CALL.
2193 * return UPDATE_MODIFY_CALL to tell the caller to convert
2194 * from the save regs, to a non-save regs function or
2195 * vice versa, or from a trampoline call.
2197 if (flag & FTRACE_FL_ENABLED) {
2198 ftrace_bug_type = FTRACE_BUG_CALL;
2199 return FTRACE_UPDATE_MAKE_CALL;
2202 ftrace_bug_type = FTRACE_BUG_UPDATE;
2203 return FTRACE_UPDATE_MODIFY_CALL;
2207 /* If there's no more users, clear all flags */
2208 if (!ftrace_rec_count(rec))
2212 * Just disable the record, but keep the ops TRAMP
2213 * and REGS states. The _EN flags must be disabled though.
2215 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2216 FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN);
2219 ftrace_bug_type = FTRACE_BUG_NOP;
2220 return FTRACE_UPDATE_MAKE_NOP;
2224 * ftrace_update_record - set a record that now is tracing or not
2225 * @rec: the record to update
2226 * @enable: set to true if the record is tracing, false to force disable
2228 * The records that represent all functions that can be traced need
2229 * to be updated when tracing has been enabled.
2231 int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
2233 return ftrace_check_record(rec, enable, true);
2237 * ftrace_test_record - check if the record has been enabled or not
2238 * @rec: the record to test
2239 * @enable: set to true to check if enabled, false if it is disabled
2241 * The arch code may need to test if a record is already set to
2242 * tracing to determine how to modify the function code that it
2245 int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
2247 return ftrace_check_record(rec, enable, false);
2250 static struct ftrace_ops *
2251 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2253 struct ftrace_ops *op;
2254 unsigned long ip = rec->ip;
2256 do_for_each_ftrace_op(op, ftrace_ops_list) {
2258 if (!op->trampoline)
2261 if (hash_contains_ip(ip, op->func_hash))
2263 } while_for_each_ftrace_op(op);
2268 static struct ftrace_ops *
2269 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2271 struct ftrace_ops *op;
2272 unsigned long ip = rec->ip;
2274 do_for_each_ftrace_op(op, ftrace_ops_list) {
2276 if (op == op_exclude || !op->trampoline)
2279 if (hash_contains_ip(ip, op->func_hash))
2281 } while_for_each_ftrace_op(op);
2286 static struct ftrace_ops *
2287 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2288 struct ftrace_ops *op)
2290 unsigned long ip = rec->ip;
2292 while_for_each_ftrace_op(op) {
2294 if (!op->trampoline)
2297 if (hash_contains_ip(ip, op->func_hash))
2304 static struct ftrace_ops *
2305 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2307 struct ftrace_ops *op;
2308 unsigned long ip = rec->ip;
2311 * Need to check removed ops first.
2312 * If they are being removed, and this rec has a tramp,
2313 * and this rec is in the ops list, then it would be the
2314 * one with the tramp.
2317 if (hash_contains_ip(ip, &removed_ops->old_hash))
2322 * Need to find the current trampoline for a rec.
2323 * Now, a trampoline is only attached to a rec if there
2324 * was a single 'ops' attached to it. But this can be called
2325 * when we are adding another op to the rec or removing the
2326 * current one. Thus, if the op is being added, we can
2327 * ignore it because it hasn't attached itself to the rec
2330 * If an ops is being modified (hooking to different functions)
2331 * then we don't care about the new functions that are being
2332 * added, just the old ones (that are probably being removed).
2334 * If we are adding an ops to a function that already is using
2335 * a trampoline, it needs to be removed (trampolines are only
2336 * for single ops connected), then an ops that is not being
2337 * modified also needs to be checked.
2339 do_for_each_ftrace_op(op, ftrace_ops_list) {
2341 if (!op->trampoline)
2345 * If the ops is being added, it hasn't gotten to
2346 * the point to be removed from this tree yet.
2348 if (op->flags & FTRACE_OPS_FL_ADDING)
2353 * If the ops is being modified and is in the old
2354 * hash, then it is probably being removed from this
2357 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2358 hash_contains_ip(ip, &op->old_hash))
2361 * If the ops is not being added or modified, and it's
2362 * in its normal filter hash, then this must be the one
2365 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2366 hash_contains_ip(ip, op->func_hash))
2369 } while_for_each_ftrace_op(op);
2374 static struct ftrace_ops *
2375 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2377 struct ftrace_ops *op;
2378 unsigned long ip = rec->ip;
2380 do_for_each_ftrace_op(op, ftrace_ops_list) {
2381 /* pass rec in as regs to have non-NULL val */
2382 if (hash_contains_ip(ip, op->func_hash))
2384 } while_for_each_ftrace_op(op);
2389 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2390 /* Protected by rcu_tasks for reading, and direct_mutex for writing */
2391 static struct ftrace_hash *direct_functions = EMPTY_HASH;
2392 static DEFINE_MUTEX(direct_mutex);
2393 int ftrace_direct_func_count;
2396 * Search the direct_functions hash to see if the given instruction pointer
2397 * has a direct caller attached to it.
2399 unsigned long ftrace_find_rec_direct(unsigned long ip)
2401 struct ftrace_func_entry *entry;
2403 entry = __ftrace_lookup_ip(direct_functions, ip);
2407 return entry->direct;
2410 static struct ftrace_func_entry*
2411 ftrace_add_rec_direct(unsigned long ip, unsigned long addr,
2412 struct ftrace_hash **free_hash)
2414 struct ftrace_func_entry *entry;
2416 if (ftrace_hash_empty(direct_functions) ||
2417 direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
2418 struct ftrace_hash *new_hash;
2419 int size = ftrace_hash_empty(direct_functions) ? 0 :
2420 direct_functions->count + 1;
2425 new_hash = dup_hash(direct_functions, size);
2429 *free_hash = direct_functions;
2430 direct_functions = new_hash;
2433 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2438 entry->direct = addr;
2439 __add_hash_entry(direct_functions, entry);
2443 static void call_direct_funcs(unsigned long ip, unsigned long pip,
2444 struct ftrace_ops *ops, struct ftrace_regs *fregs)
2446 struct pt_regs *regs = ftrace_get_regs(fregs);
2449 addr = ftrace_find_rec_direct(ip);
2453 arch_ftrace_set_direct_caller(regs, addr);
2456 struct ftrace_ops direct_ops = {
2457 .func = call_direct_funcs,
2458 .flags = FTRACE_OPS_FL_IPMODIFY
2459 | FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS
2460 | FTRACE_OPS_FL_PERMANENT,
2462 * By declaring the main trampoline as this trampoline
2463 * it will never have one allocated for it. Allocated
2464 * trampolines should not call direct functions.
2465 * The direct_ops should only be called by the builtin
2466 * ftrace_regs_caller trampoline.
2468 .trampoline = FTRACE_REGS_ADDR,
2470 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2473 * ftrace_get_addr_new - Get the call address to set to
2474 * @rec: The ftrace record descriptor
2476 * If the record has the FTRACE_FL_REGS set, that means that it
2477 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2478 * is not set, then it wants to convert to the normal callback.
2480 * Returns the address of the trampoline to set to
2482 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2484 struct ftrace_ops *ops;
2487 if ((rec->flags & FTRACE_FL_DIRECT) &&
2488 (ftrace_rec_count(rec) == 1)) {
2489 addr = ftrace_find_rec_direct(rec->ip);
2495 /* Trampolines take precedence over regs */
2496 if (rec->flags & FTRACE_FL_TRAMP) {
2497 ops = ftrace_find_tramp_ops_new(rec);
2498 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2499 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2500 (void *)rec->ip, (void *)rec->ip, rec->flags);
2501 /* Ftrace is shutting down, return anything */
2502 return (unsigned long)FTRACE_ADDR;
2504 return ops->trampoline;
2507 if (rec->flags & FTRACE_FL_REGS)
2508 return (unsigned long)FTRACE_REGS_ADDR;
2510 return (unsigned long)FTRACE_ADDR;
2514 * ftrace_get_addr_curr - Get the call address that is already there
2515 * @rec: The ftrace record descriptor
2517 * The FTRACE_FL_REGS_EN is set when the record already points to
2518 * a function that saves all the regs. Basically the '_EN' version
2519 * represents the current state of the function.
2521 * Returns the address of the trampoline that is currently being called
2523 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2525 struct ftrace_ops *ops;
2528 /* Direct calls take precedence over trampolines */
2529 if (rec->flags & FTRACE_FL_DIRECT_EN) {
2530 addr = ftrace_find_rec_direct(rec->ip);
2536 /* Trampolines take precedence over regs */
2537 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2538 ops = ftrace_find_tramp_ops_curr(rec);
2539 if (FTRACE_WARN_ON(!ops)) {
2540 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2541 (void *)rec->ip, (void *)rec->ip);
2542 /* Ftrace is shutting down, return anything */
2543 return (unsigned long)FTRACE_ADDR;
2545 return ops->trampoline;
2548 if (rec->flags & FTRACE_FL_REGS_EN)
2549 return (unsigned long)FTRACE_REGS_ADDR;
2551 return (unsigned long)FTRACE_ADDR;
2555 __ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
2557 unsigned long ftrace_old_addr;
2558 unsigned long ftrace_addr;
2561 ftrace_addr = ftrace_get_addr_new(rec);
2563 /* This needs to be done before we call ftrace_update_record */
2564 ftrace_old_addr = ftrace_get_addr_curr(rec);
2566 ret = ftrace_update_record(rec, enable);
2568 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2571 case FTRACE_UPDATE_IGNORE:
2574 case FTRACE_UPDATE_MAKE_CALL:
2575 ftrace_bug_type = FTRACE_BUG_CALL;
2576 return ftrace_make_call(rec, ftrace_addr);
2578 case FTRACE_UPDATE_MAKE_NOP:
2579 ftrace_bug_type = FTRACE_BUG_NOP;
2580 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2582 case FTRACE_UPDATE_MODIFY_CALL:
2583 ftrace_bug_type = FTRACE_BUG_UPDATE;
2584 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2587 return -1; /* unknown ftrace bug */
2590 void __weak ftrace_replace_code(int mod_flags)
2592 struct dyn_ftrace *rec;
2593 struct ftrace_page *pg;
2594 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2595 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
2598 if (unlikely(ftrace_disabled))
2601 do_for_each_ftrace_rec(pg, rec) {
2603 if (rec->flags & FTRACE_FL_DISABLED)
2606 failed = __ftrace_replace_code(rec, enable);
2608 ftrace_bug(failed, rec);
2609 /* Stop processing */
2614 } while_for_each_ftrace_rec();
2617 struct ftrace_rec_iter {
2618 struct ftrace_page *pg;
2623 * ftrace_rec_iter_start - start up iterating over traced functions
2625 * Returns an iterator handle that is used to iterate over all
2626 * the records that represent address locations where functions
2629 * May return NULL if no records are available.
2631 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2634 * We only use a single iterator.
2635 * Protected by the ftrace_lock mutex.
2637 static struct ftrace_rec_iter ftrace_rec_iter;
2638 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2640 iter->pg = ftrace_pages_start;
2643 /* Could have empty pages */
2644 while (iter->pg && !iter->pg->index)
2645 iter->pg = iter->pg->next;
2654 * ftrace_rec_iter_next - get the next record to process.
2655 * @iter: The handle to the iterator.
2657 * Returns the next iterator after the given iterator @iter.
2659 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2663 if (iter->index >= iter->pg->index) {
2664 iter->pg = iter->pg->next;
2667 /* Could have empty pages */
2668 while (iter->pg && !iter->pg->index)
2669 iter->pg = iter->pg->next;
2679 * ftrace_rec_iter_record - get the record at the iterator location
2680 * @iter: The current iterator location
2682 * Returns the record that the current @iter is at.
2684 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2686 return &iter->pg->records[iter->index];
2690 ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
2694 if (unlikely(ftrace_disabled))
2697 ret = ftrace_init_nop(mod, rec);
2699 ftrace_bug_type = FTRACE_BUG_INIT;
2700 ftrace_bug(ret, rec);
2707 * archs can override this function if they must do something
2708 * before the modifying code is performed.
2710 int __weak ftrace_arch_code_modify_prepare(void)
2716 * archs can override this function if they must do something
2717 * after the modifying code is performed.
2719 int __weak ftrace_arch_code_modify_post_process(void)
2724 void ftrace_modify_all_code(int command)
2726 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2730 if (command & FTRACE_MAY_SLEEP)
2731 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2734 * If the ftrace_caller calls a ftrace_ops func directly,
2735 * we need to make sure that it only traces functions it
2736 * expects to trace. When doing the switch of functions,
2737 * we need to update to the ftrace_ops_list_func first
2738 * before the transition between old and new calls are set,
2739 * as the ftrace_ops_list_func will check the ops hashes
2740 * to make sure the ops are having the right functions
2744 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2745 if (FTRACE_WARN_ON(err))
2749 if (command & FTRACE_UPDATE_CALLS)
2750 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
2751 else if (command & FTRACE_DISABLE_CALLS)
2752 ftrace_replace_code(mod_flags);
2754 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2755 function_trace_op = set_function_trace_op;
2757 /* If irqs are disabled, we are in stop machine */
2758 if (!irqs_disabled())
2759 smp_call_function(ftrace_sync_ipi, NULL, 1);
2760 err = ftrace_update_ftrace_func(ftrace_trace_function);
2761 if (FTRACE_WARN_ON(err))
2765 if (command & FTRACE_START_FUNC_RET)
2766 err = ftrace_enable_ftrace_graph_caller();
2767 else if (command & FTRACE_STOP_FUNC_RET)
2768 err = ftrace_disable_ftrace_graph_caller();
2769 FTRACE_WARN_ON(err);
2772 static int __ftrace_modify_code(void *data)
2774 int *command = data;
2776 ftrace_modify_all_code(*command);
2782 * ftrace_run_stop_machine - go back to the stop machine method
2783 * @command: The command to tell ftrace what to do
2785 * If an arch needs to fall back to the stop machine method, the
2786 * it can call this function.
2788 void ftrace_run_stop_machine(int command)
2790 stop_machine(__ftrace_modify_code, &command, NULL);
2794 * arch_ftrace_update_code - modify the code to trace or not trace
2795 * @command: The command that needs to be done
2797 * Archs can override this function if it does not need to
2798 * run stop_machine() to modify code.
2800 void __weak arch_ftrace_update_code(int command)
2802 ftrace_run_stop_machine(command);
2805 static void ftrace_run_update_code(int command)
2809 ret = ftrace_arch_code_modify_prepare();
2810 FTRACE_WARN_ON(ret);
2815 * By default we use stop_machine() to modify the code.
2816 * But archs can do what ever they want as long as it
2817 * is safe. The stop_machine() is the safest, but also
2818 * produces the most overhead.
2820 arch_ftrace_update_code(command);
2822 ret = ftrace_arch_code_modify_post_process();
2823 FTRACE_WARN_ON(ret);
2826 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2827 struct ftrace_ops_hash *old_hash)
2829 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2830 ops->old_hash.filter_hash = old_hash->filter_hash;
2831 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2832 ftrace_run_update_code(command);
2833 ops->old_hash.filter_hash = NULL;
2834 ops->old_hash.notrace_hash = NULL;
2835 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2838 static ftrace_func_t saved_ftrace_func;
2839 static int ftrace_start_up;
2841 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2845 /* List of trace_ops that have allocated trampolines */
2846 static LIST_HEAD(ftrace_ops_trampoline_list);
2848 static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops)
2850 lockdep_assert_held(&ftrace_lock);
2851 list_add_rcu(&ops->list, &ftrace_ops_trampoline_list);
2854 static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops)
2856 lockdep_assert_held(&ftrace_lock);
2857 list_del_rcu(&ops->list);
2862 * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
2863 * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
2866 #define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
2867 #define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
2869 static void ftrace_trampoline_free(struct ftrace_ops *ops)
2871 if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) &&
2874 * Record the text poke event before the ksymbol unregister
2877 perf_event_text_poke((void *)ops->trampoline,
2878 (void *)ops->trampoline,
2879 ops->trampoline_size, NULL, 0);
2880 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
2881 ops->trampoline, ops->trampoline_size,
2882 true, FTRACE_TRAMPOLINE_SYM);
2883 /* Remove from kallsyms after the perf events */
2884 ftrace_remove_trampoline_from_kallsyms(ops);
2887 arch_ftrace_trampoline_free(ops);
2890 static void ftrace_startup_enable(int command)
2892 if (saved_ftrace_func != ftrace_trace_function) {
2893 saved_ftrace_func = ftrace_trace_function;
2894 command |= FTRACE_UPDATE_TRACE_FUNC;
2897 if (!command || !ftrace_enabled)
2900 ftrace_run_update_code(command);
2903 static void ftrace_startup_all(int command)
2905 update_all_ops = true;
2906 ftrace_startup_enable(command);
2907 update_all_ops = false;
2910 int ftrace_startup(struct ftrace_ops *ops, int command)
2914 if (unlikely(ftrace_disabled))
2917 ret = __register_ftrace_function(ops);
2924 * Note that ftrace probes uses this to start up
2925 * and modify functions it will probe. But we still
2926 * set the ADDING flag for modification, as probes
2927 * do not have trampolines. If they add them in the
2928 * future, then the probes will need to distinguish
2929 * between adding and updating probes.
2931 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2933 ret = ftrace_hash_ipmodify_enable(ops);
2935 /* Rollback registration process */
2936 __unregister_ftrace_function(ops);
2938 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2939 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2940 ftrace_trampoline_free(ops);
2944 if (ftrace_hash_rec_enable(ops, 1))
2945 command |= FTRACE_UPDATE_CALLS;
2947 ftrace_startup_enable(command);
2949 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2954 int ftrace_shutdown(struct ftrace_ops *ops, int command)
2958 if (unlikely(ftrace_disabled))
2961 ret = __unregister_ftrace_function(ops);
2967 * Just warn in case of unbalance, no need to kill ftrace, it's not
2968 * critical but the ftrace_call callers may be never nopped again after
2969 * further ftrace uses.
2971 WARN_ON_ONCE(ftrace_start_up < 0);
2973 /* Disabling ipmodify never fails */
2974 ftrace_hash_ipmodify_disable(ops);
2976 if (ftrace_hash_rec_disable(ops, 1))
2977 command |= FTRACE_UPDATE_CALLS;
2979 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2981 if (saved_ftrace_func != ftrace_trace_function) {
2982 saved_ftrace_func = ftrace_trace_function;
2983 command |= FTRACE_UPDATE_TRACE_FUNC;
2986 if (!command || !ftrace_enabled) {
2988 * If these are dynamic or per_cpu ops, they still
2989 * need their data freed. Since, function tracing is
2990 * not currently active, we can just free them
2991 * without synchronizing all CPUs.
2993 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
3000 * If the ops uses a trampoline, then it needs to be
3001 * tested first on update.
3003 ops->flags |= FTRACE_OPS_FL_REMOVING;
3006 /* The trampoline logic checks the old hashes */
3007 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
3008 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
3010 ftrace_run_update_code(command);
3013 * If there's no more ops registered with ftrace, run a
3014 * sanity check to make sure all rec flags are cleared.
3016 if (rcu_dereference_protected(ftrace_ops_list,
3017 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
3018 struct ftrace_page *pg;
3019 struct dyn_ftrace *rec;
3021 do_for_each_ftrace_rec(pg, rec) {
3022 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
3023 pr_warn(" %pS flags:%lx\n",
3024 (void *)rec->ip, rec->flags);
3025 } while_for_each_ftrace_rec();
3028 ops->old_hash.filter_hash = NULL;
3029 ops->old_hash.notrace_hash = NULL;
3032 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
3035 * Dynamic ops may be freed, we must make sure that all
3036 * callers are done before leaving this function.
3037 * The same goes for freeing the per_cpu data of the per_cpu
3040 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
3042 * We need to do a hard force of sched synchronization.
3043 * This is because we use preempt_disable() to do RCU, but
3044 * the function tracers can be called where RCU is not watching
3045 * (like before user_exit()). We can not rely on the RCU
3046 * infrastructure to do the synchronization, thus we must do it
3049 synchronize_rcu_tasks_rude();
3052 * When the kernel is preemptive, tasks can be preempted
3053 * while on a ftrace trampoline. Just scheduling a task on
3054 * a CPU is not good enough to flush them. Calling
3055 * synchronize_rcu_tasks() will wait for those tasks to
3056 * execute and either schedule voluntarily or enter user space.
3058 if (IS_ENABLED(CONFIG_PREEMPTION))
3059 synchronize_rcu_tasks();
3062 ftrace_trampoline_free(ops);
3068 static u64 ftrace_update_time;
3069 unsigned long ftrace_update_tot_cnt;
3070 unsigned long ftrace_number_of_pages;
3071 unsigned long ftrace_number_of_groups;
3073 static inline int ops_traces_mod(struct ftrace_ops *ops)
3076 * Filter_hash being empty will default to trace module.
3077 * But notrace hash requires a test of individual module functions.
3079 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
3080 ftrace_hash_empty(ops->func_hash->notrace_hash);
3084 * Check if the current ops references the record.
3086 * If the ops traces all functions, then it was already accounted for.
3087 * If the ops does not trace the current record function, skip it.
3088 * If the ops ignores the function via notrace filter, skip it.
3091 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3093 /* If ops isn't enabled, ignore it */
3094 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
3097 /* If ops traces all then it includes this function */
3098 if (ops_traces_mod(ops))
3101 /* The function must be in the filter */
3102 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
3103 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
3106 /* If in notrace hash, we ignore it too */
3107 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
3113 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
3115 bool init_nop = ftrace_need_init_nop();
3116 struct ftrace_page *pg;
3117 struct dyn_ftrace *p;
3119 unsigned long update_cnt = 0;
3120 unsigned long rec_flags = 0;
3123 start = ftrace_now(raw_smp_processor_id());
3126 * When a module is loaded, this function is called to convert
3127 * the calls to mcount in its text to nops, and also to create
3128 * an entry in the ftrace data. Now, if ftrace is activated
3129 * after this call, but before the module sets its text to
3130 * read-only, the modification of enabling ftrace can fail if
3131 * the read-only is done while ftrace is converting the calls.
3132 * To prevent this, the module's records are set as disabled
3133 * and will be enabled after the call to set the module's text
3137 rec_flags |= FTRACE_FL_DISABLED;
3139 for (pg = new_pgs; pg; pg = pg->next) {
3141 for (i = 0; i < pg->index; i++) {
3143 /* If something went wrong, bail without enabling anything */
3144 if (unlikely(ftrace_disabled))
3147 p = &pg->records[i];
3148 p->flags = rec_flags;
3151 * Do the initial record conversion from mcount jump
3152 * to the NOP instructions.
3154 if (init_nop && !ftrace_nop_initialize(mod, p))
3161 stop = ftrace_now(raw_smp_processor_id());
3162 ftrace_update_time = stop - start;
3163 ftrace_update_tot_cnt += update_cnt;
3168 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3174 if (WARN_ON(!count))
3177 /* We want to fill as much as possible, with no empty pages */
3178 pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE);
3179 order = fls(pages) - 1;
3182 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3185 /* if we can't allocate this size, try something smaller */
3192 ftrace_number_of_pages += 1 << order;
3193 ftrace_number_of_groups++;
3195 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3204 static struct ftrace_page *
3205 ftrace_allocate_pages(unsigned long num_to_init)
3207 struct ftrace_page *start_pg;
3208 struct ftrace_page *pg;
3214 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3219 * Try to allocate as much as possible in one continues
3220 * location that fills in all of the space. We want to
3221 * waste as little space as possible.
3224 cnt = ftrace_allocate_records(pg, num_to_init);
3232 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3245 free_pages((unsigned long)pg->records, pg->order);
3246 ftrace_number_of_pages -= 1 << pg->order;
3248 start_pg = pg->next;
3251 ftrace_number_of_groups--;
3253 pr_info("ftrace: FAILED to allocate memory for functions\n");
3257 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3259 struct ftrace_iterator {
3263 struct ftrace_page *pg;
3264 struct dyn_ftrace *func;
3265 struct ftrace_func_probe *probe;
3266 struct ftrace_func_entry *probe_entry;
3267 struct trace_parser parser;
3268 struct ftrace_hash *hash;
3269 struct ftrace_ops *ops;
3270 struct trace_array *tr;
3271 struct list_head *mod_list;
3278 t_probe_next(struct seq_file *m, loff_t *pos)
3280 struct ftrace_iterator *iter = m->private;
3281 struct trace_array *tr = iter->ops->private;
3282 struct list_head *func_probes;
3283 struct ftrace_hash *hash;
3284 struct list_head *next;
3285 struct hlist_node *hnd = NULL;
3286 struct hlist_head *hhd;
3295 func_probes = &tr->func_probes;
3296 if (list_empty(func_probes))
3300 next = func_probes->next;
3301 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3304 if (iter->probe_entry)
3305 hnd = &iter->probe_entry->hlist;
3307 hash = iter->probe->ops.func_hash->filter_hash;
3310 * A probe being registered may temporarily have an empty hash
3311 * and it's at the end of the func_probes list.
3313 if (!hash || hash == EMPTY_HASH)
3316 size = 1 << hash->size_bits;
3319 if (iter->pidx >= size) {
3320 if (iter->probe->list.next == func_probes)
3322 next = iter->probe->list.next;
3323 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3324 hash = iter->probe->ops.func_hash->filter_hash;
3325 size = 1 << hash->size_bits;
3329 hhd = &hash->buckets[iter->pidx];
3331 if (hlist_empty(hhd)) {
3347 if (WARN_ON_ONCE(!hnd))
3350 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3355 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3357 struct ftrace_iterator *iter = m->private;
3361 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3364 if (iter->mod_pos > *pos)
3368 iter->probe_entry = NULL;
3370 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3371 p = t_probe_next(m, &l);
3378 /* Only set this if we have an item */
3379 iter->flags |= FTRACE_ITER_PROBE;
3385 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3387 struct ftrace_func_entry *probe_entry;
3388 struct ftrace_probe_ops *probe_ops;
3389 struct ftrace_func_probe *probe;
3391 probe = iter->probe;
3392 probe_entry = iter->probe_entry;
3394 if (WARN_ON_ONCE(!probe || !probe_entry))
3397 probe_ops = probe->probe_ops;
3399 if (probe_ops->print)
3400 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3402 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3403 (void *)probe_ops->func);
3409 t_mod_next(struct seq_file *m, loff_t *pos)
3411 struct ftrace_iterator *iter = m->private;
3412 struct trace_array *tr = iter->tr;
3417 iter->mod_list = iter->mod_list->next;
3419 if (iter->mod_list == &tr->mod_trace ||
3420 iter->mod_list == &tr->mod_notrace) {
3421 iter->flags &= ~FTRACE_ITER_MOD;
3425 iter->mod_pos = *pos;
3430 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3432 struct ftrace_iterator *iter = m->private;
3436 if (iter->func_pos > *pos)
3439 iter->mod_pos = iter->func_pos;
3441 /* probes are only available if tr is set */
3445 for (l = 0; l <= (*pos - iter->func_pos); ) {
3446 p = t_mod_next(m, &l);
3451 iter->flags &= ~FTRACE_ITER_MOD;
3452 return t_probe_start(m, pos);
3455 /* Only set this if we have an item */
3456 iter->flags |= FTRACE_ITER_MOD;
3462 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3464 struct ftrace_mod_load *ftrace_mod;
3465 struct trace_array *tr = iter->tr;
3467 if (WARN_ON_ONCE(!iter->mod_list) ||
3468 iter->mod_list == &tr->mod_trace ||
3469 iter->mod_list == &tr->mod_notrace)
3472 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3474 if (ftrace_mod->func)
3475 seq_printf(m, "%s", ftrace_mod->func);
3479 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3485 t_func_next(struct seq_file *m, loff_t *pos)
3487 struct ftrace_iterator *iter = m->private;
3488 struct dyn_ftrace *rec = NULL;
3493 if (iter->idx >= iter->pg->index) {
3494 if (iter->pg->next) {
3495 iter->pg = iter->pg->next;
3500 rec = &iter->pg->records[iter->idx++];
3501 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3502 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3504 ((iter->flags & FTRACE_ITER_ENABLED) &&
3505 !(rec->flags & FTRACE_FL_ENABLED))) {
3515 iter->pos = iter->func_pos = *pos;
3522 t_next(struct seq_file *m, void *v, loff_t *pos)
3524 struct ftrace_iterator *iter = m->private;
3525 loff_t l = *pos; /* t_probe_start() must use original pos */
3528 if (unlikely(ftrace_disabled))
3531 if (iter->flags & FTRACE_ITER_PROBE)
3532 return t_probe_next(m, pos);
3534 if (iter->flags & FTRACE_ITER_MOD)
3535 return t_mod_next(m, pos);
3537 if (iter->flags & FTRACE_ITER_PRINTALL) {
3538 /* next must increment pos, and t_probe_start does not */
3540 return t_mod_start(m, &l);
3543 ret = t_func_next(m, pos);
3546 return t_mod_start(m, &l);
3551 static void reset_iter_read(struct ftrace_iterator *iter)
3555 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3558 static void *t_start(struct seq_file *m, loff_t *pos)
3560 struct ftrace_iterator *iter = m->private;
3564 mutex_lock(&ftrace_lock);
3566 if (unlikely(ftrace_disabled))
3570 * If an lseek was done, then reset and start from beginning.
3572 if (*pos < iter->pos)
3573 reset_iter_read(iter);
3576 * For set_ftrace_filter reading, if we have the filter
3577 * off, we can short cut and just print out that all
3578 * functions are enabled.
3580 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3581 ftrace_hash_empty(iter->hash)) {
3582 iter->func_pos = 1; /* Account for the message */
3584 return t_mod_start(m, pos);
3585 iter->flags |= FTRACE_ITER_PRINTALL;
3586 /* reset in case of seek/pread */
3587 iter->flags &= ~FTRACE_ITER_PROBE;
3591 if (iter->flags & FTRACE_ITER_MOD)
3592 return t_mod_start(m, pos);
3595 * Unfortunately, we need to restart at ftrace_pages_start
3596 * every time we let go of the ftrace_mutex. This is because
3597 * those pointers can change without the lock.
3599 iter->pg = ftrace_pages_start;
3601 for (l = 0; l <= *pos; ) {
3602 p = t_func_next(m, &l);
3608 return t_mod_start(m, pos);
3613 static void t_stop(struct seq_file *m, void *p)
3615 mutex_unlock(&ftrace_lock);
3619 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3624 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3625 struct dyn_ftrace *rec)
3629 ptr = arch_ftrace_trampoline_func(ops, rec);
3631 seq_printf(m, " ->%pS", ptr);
3634 static int t_show(struct seq_file *m, void *v)
3636 struct ftrace_iterator *iter = m->private;
3637 struct dyn_ftrace *rec;
3639 if (iter->flags & FTRACE_ITER_PROBE)
3640 return t_probe_show(m, iter);
3642 if (iter->flags & FTRACE_ITER_MOD)
3643 return t_mod_show(m, iter);
3645 if (iter->flags & FTRACE_ITER_PRINTALL) {
3646 if (iter->flags & FTRACE_ITER_NOTRACE)
3647 seq_puts(m, "#### no functions disabled ####\n");
3649 seq_puts(m, "#### all functions enabled ####\n");
3658 seq_printf(m, "%ps", (void *)rec->ip);
3659 if (iter->flags & FTRACE_ITER_ENABLED) {
3660 struct ftrace_ops *ops;
3662 seq_printf(m, " (%ld)%s%s%s",
3663 ftrace_rec_count(rec),
3664 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3665 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
3666 rec->flags & FTRACE_FL_DIRECT ? " D" : " ");
3667 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3668 ops = ftrace_find_tramp_ops_any(rec);
3671 seq_printf(m, "\ttramp: %pS (%pS)",
3672 (void *)ops->trampoline,
3674 add_trampoline_func(m, ops, rec);
3675 ops = ftrace_find_tramp_ops_next(rec, ops);
3678 seq_puts(m, "\ttramp: ERROR!");
3680 add_trampoline_func(m, NULL, rec);
3682 if (rec->flags & FTRACE_FL_DIRECT) {
3683 unsigned long direct;
3685 direct = ftrace_find_rec_direct(rec->ip);
3687 seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3696 static const struct seq_operations show_ftrace_seq_ops = {
3704 ftrace_avail_open(struct inode *inode, struct file *file)
3706 struct ftrace_iterator *iter;
3709 ret = security_locked_down(LOCKDOWN_TRACEFS);
3713 if (unlikely(ftrace_disabled))
3716 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3720 iter->pg = ftrace_pages_start;
3721 iter->ops = &global_ops;
3727 ftrace_enabled_open(struct inode *inode, struct file *file)
3729 struct ftrace_iterator *iter;
3732 * This shows us what functions are currently being
3733 * traced and by what. Not sure if we want lockdown
3734 * to hide such critical information for an admin.
3735 * Although, perhaps it can show information we don't
3736 * want people to see, but if something is tracing
3737 * something, we probably want to know about it.
3740 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3744 iter->pg = ftrace_pages_start;
3745 iter->flags = FTRACE_ITER_ENABLED;
3746 iter->ops = &global_ops;
3752 * ftrace_regex_open - initialize function tracer filter files
3753 * @ops: The ftrace_ops that hold the hash filters
3754 * @flag: The type of filter to process
3755 * @inode: The inode, usually passed in to your open routine
3756 * @file: The file, usually passed in to your open routine
3758 * ftrace_regex_open() initializes the filter files for the
3759 * @ops. Depending on @flag it may process the filter hash or
3760 * the notrace hash of @ops. With this called from the open
3761 * routine, you can use ftrace_filter_write() for the write
3762 * routine if @flag has FTRACE_ITER_FILTER set, or
3763 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3764 * tracing_lseek() should be used as the lseek routine, and
3765 * release must call ftrace_regex_release().
3768 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3769 struct inode *inode, struct file *file)
3771 struct ftrace_iterator *iter;
3772 struct ftrace_hash *hash;
3773 struct list_head *mod_head;
3774 struct trace_array *tr = ops->private;
3777 ftrace_ops_init(ops);
3779 if (unlikely(ftrace_disabled))
3782 if (tracing_check_open_get_tr(tr))
3785 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3789 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3796 mutex_lock(&ops->func_hash->regex_lock);
3798 if (flag & FTRACE_ITER_NOTRACE) {
3799 hash = ops->func_hash->notrace_hash;
3800 mod_head = tr ? &tr->mod_notrace : NULL;
3802 hash = ops->func_hash->filter_hash;
3803 mod_head = tr ? &tr->mod_trace : NULL;
3806 iter->mod_list = mod_head;
3808 if (file->f_mode & FMODE_WRITE) {
3809 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3811 if (file->f_flags & O_TRUNC) {
3812 iter->hash = alloc_ftrace_hash(size_bits);
3813 clear_ftrace_mod_list(mod_head);
3815 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3819 trace_parser_put(&iter->parser);
3827 if (file->f_mode & FMODE_READ) {
3828 iter->pg = ftrace_pages_start;
3830 ret = seq_open(file, &show_ftrace_seq_ops);
3832 struct seq_file *m = file->private_data;
3836 free_ftrace_hash(iter->hash);
3837 trace_parser_put(&iter->parser);
3840 file->private_data = iter;
3843 mutex_unlock(&ops->func_hash->regex_lock);
3849 trace_array_put(tr);
3856 ftrace_filter_open(struct inode *inode, struct file *file)
3858 struct ftrace_ops *ops = inode->i_private;
3860 /* Checks for tracefs lockdown */
3861 return ftrace_regex_open(ops,
3862 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3867 ftrace_notrace_open(struct inode *inode, struct file *file)
3869 struct ftrace_ops *ops = inode->i_private;
3871 /* Checks for tracefs lockdown */
3872 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3876 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3877 struct ftrace_glob {
3884 * If symbols in an architecture don't correspond exactly to the user-visible
3885 * name of what they represent, it is possible to define this function to
3886 * perform the necessary adjustments.
3888 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3893 static int ftrace_match(char *str, struct ftrace_glob *g)
3898 str = arch_ftrace_match_adjust(str, g->search);
3902 if (strcmp(str, g->search) == 0)
3905 case MATCH_FRONT_ONLY:
3906 if (strncmp(str, g->search, g->len) == 0)
3909 case MATCH_MIDDLE_ONLY:
3910 if (strstr(str, g->search))
3913 case MATCH_END_ONLY:
3915 if (slen >= g->len &&
3916 memcmp(str + slen - g->len, g->search, g->len) == 0)
3920 if (glob_match(g->search, str))
3929 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3931 struct ftrace_func_entry *entry;
3934 entry = ftrace_lookup_ip(hash, rec->ip);
3936 /* Do nothing if it doesn't exist */
3940 free_hash_entry(hash, entry);
3942 /* Do nothing if it exists */
3946 ret = add_hash_entry(hash, rec->ip);
3952 add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3955 long index = simple_strtoul(func_g->search, NULL, 0);
3956 struct ftrace_page *pg;
3957 struct dyn_ftrace *rec;
3959 /* The index starts at 1 */
3963 do_for_each_ftrace_rec(pg, rec) {
3964 if (pg->index <= index) {
3966 /* this is a double loop, break goes to the next page */
3969 rec = &pg->records[index];
3970 enter_record(hash, rec, clear_filter);
3972 } while_for_each_ftrace_rec();
3977 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3978 struct ftrace_glob *mod_g, int exclude_mod)
3980 char str[KSYM_SYMBOL_LEN];
3983 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3986 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3988 /* blank module name to match all modules */
3990 /* blank module globbing: modname xor exclude_mod */
3991 if (!exclude_mod != !modname)
3997 * exclude_mod is set to trace everything but the given
3998 * module. If it is set and the module matches, then
3999 * return 0. If it is not set, and the module doesn't match
4000 * also return 0. Otherwise, check the function to see if
4003 if (!mod_matches == !exclude_mod)
4006 /* blank search means to match all funcs in the mod */
4011 return ftrace_match(str, func_g);
4015 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
4017 struct ftrace_page *pg;
4018 struct dyn_ftrace *rec;
4019 struct ftrace_glob func_g = { .type = MATCH_FULL };
4020 struct ftrace_glob mod_g = { .type = MATCH_FULL };
4021 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
4022 int exclude_mod = 0;
4025 int clear_filter = 0;
4028 func_g.type = filter_parse_regex(func, len, &func_g.search,
4030 func_g.len = strlen(func_g.search);
4034 mod_g.type = filter_parse_regex(mod, strlen(mod),
4035 &mod_g.search, &exclude_mod);
4036 mod_g.len = strlen(mod_g.search);
4039 mutex_lock(&ftrace_lock);
4041 if (unlikely(ftrace_disabled))
4044 if (func_g.type == MATCH_INDEX) {
4045 found = add_rec_by_index(hash, &func_g, clear_filter);
4049 do_for_each_ftrace_rec(pg, rec) {
4051 if (rec->flags & FTRACE_FL_DISABLED)
4054 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
4055 ret = enter_record(hash, rec, clear_filter);
4062 } while_for_each_ftrace_rec();
4064 mutex_unlock(&ftrace_lock);
4070 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
4072 return match_records(hash, buff, len, NULL);
4075 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4076 struct ftrace_ops_hash *old_hash)
4078 struct ftrace_ops *op;
4080 if (!ftrace_enabled)
4083 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4084 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4089 * If this is the shared global_ops filter, then we need to
4090 * check if there is another ops that shares it, is enabled.
4091 * If so, we still need to run the modify code.
4093 if (ops->func_hash != &global_ops.local_hash)
4096 do_for_each_ftrace_op(op, ftrace_ops_list) {
4097 if (op->func_hash == &global_ops.local_hash &&
4098 op->flags & FTRACE_OPS_FL_ENABLED) {
4099 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4100 /* Only need to do this once */
4103 } while_for_each_ftrace_op(op);
4106 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
4107 struct ftrace_hash **orig_hash,
4108 struct ftrace_hash *hash,
4111 struct ftrace_ops_hash old_hash_ops;
4112 struct ftrace_hash *old_hash;
4115 old_hash = *orig_hash;
4116 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4117 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4118 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4120 ftrace_ops_update_code(ops, &old_hash_ops);
4121 free_ftrace_hash_rcu(old_hash);
4126 static bool module_exists(const char *module)
4128 /* All modules have the symbol __this_module */
4129 static const char this_mod[] = "__this_module";
4130 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
4134 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
4136 if (n > sizeof(modname) - 1)
4139 val = module_kallsyms_lookup_name(modname);
4143 static int cache_mod(struct trace_array *tr,
4144 const char *func, char *module, int enable)
4146 struct ftrace_mod_load *ftrace_mod, *n;
4147 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4150 mutex_lock(&ftrace_lock);
4152 /* We do not cache inverse filters */
4153 if (func[0] == '!') {
4157 /* Look to remove this hash */
4158 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4159 if (strcmp(ftrace_mod->module, module) != 0)
4162 /* no func matches all */
4163 if (strcmp(func, "*") == 0 ||
4164 (ftrace_mod->func &&
4165 strcmp(ftrace_mod->func, func) == 0)) {
4167 free_ftrace_mod(ftrace_mod);
4175 /* We only care about modules that have not been loaded yet */
4176 if (module_exists(module))
4179 /* Save this string off, and execute it when the module is loaded */
4180 ret = ftrace_add_mod(tr, func, module, enable);
4182 mutex_unlock(&ftrace_lock);
4188 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4189 int reset, int enable);
4191 #ifdef CONFIG_MODULES
4192 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4193 char *mod, bool enable)
4195 struct ftrace_mod_load *ftrace_mod, *n;
4196 struct ftrace_hash **orig_hash, *new_hash;
4197 LIST_HEAD(process_mods);
4200 mutex_lock(&ops->func_hash->regex_lock);
4203 orig_hash = &ops->func_hash->filter_hash;
4205 orig_hash = &ops->func_hash->notrace_hash;
4207 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4210 goto out; /* warn? */
4212 mutex_lock(&ftrace_lock);
4214 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4216 if (strcmp(ftrace_mod->module, mod) != 0)
4219 if (ftrace_mod->func)
4220 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4222 func = kstrdup("*", GFP_KERNEL);
4224 if (!func) /* warn? */
4227 list_move(&ftrace_mod->list, &process_mods);
4229 /* Use the newly allocated func, as it may be "*" */
4230 kfree(ftrace_mod->func);
4231 ftrace_mod->func = func;
4234 mutex_unlock(&ftrace_lock);
4236 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4238 func = ftrace_mod->func;
4240 /* Grabs ftrace_lock, which is why we have this extra step */
4241 match_records(new_hash, func, strlen(func), mod);
4242 free_ftrace_mod(ftrace_mod);
4245 if (enable && list_empty(head))
4246 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4248 mutex_lock(&ftrace_lock);
4250 ftrace_hash_move_and_update_ops(ops, orig_hash,
4252 mutex_unlock(&ftrace_lock);
4255 mutex_unlock(&ops->func_hash->regex_lock);
4257 free_ftrace_hash(new_hash);
4260 static void process_cached_mods(const char *mod_name)
4262 struct trace_array *tr;
4265 mod = kstrdup(mod_name, GFP_KERNEL);
4269 mutex_lock(&trace_types_lock);
4270 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4271 if (!list_empty(&tr->mod_trace))
4272 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4273 if (!list_empty(&tr->mod_notrace))
4274 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4276 mutex_unlock(&trace_types_lock);
4283 * We register the module command as a template to show others how
4284 * to register the a command as well.
4288 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4289 char *func_orig, char *cmd, char *module, int enable)
4294 /* match_records() modifies func, and we need the original */
4295 func = kstrdup(func_orig, GFP_KERNEL);
4300 * cmd == 'mod' because we only registered this func
4301 * for the 'mod' ftrace_func_command.
4302 * But if you register one func with multiple commands,
4303 * you can tell which command was used by the cmd
4306 ret = match_records(hash, func, strlen(func), module);
4310 return cache_mod(tr, func_orig, module, enable);
4316 static struct ftrace_func_command ftrace_mod_cmd = {
4318 .func = ftrace_mod_callback,
4321 static int __init ftrace_mod_cmd_init(void)
4323 return register_ftrace_command(&ftrace_mod_cmd);
4325 core_initcall(ftrace_mod_cmd_init);
4327 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4328 struct ftrace_ops *op, struct ftrace_regs *fregs)
4330 struct ftrace_probe_ops *probe_ops;
4331 struct ftrace_func_probe *probe;
4333 probe = container_of(op, struct ftrace_func_probe, ops);
4334 probe_ops = probe->probe_ops;
4337 * Disable preemption for these calls to prevent a RCU grace
4338 * period. This syncs the hash iteration and freeing of items
4339 * on the hash. rcu_read_lock is too dangerous here.
4341 preempt_disable_notrace();
4342 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4343 preempt_enable_notrace();
4346 struct ftrace_func_map {
4347 struct ftrace_func_entry entry;
4351 struct ftrace_func_mapper {
4352 struct ftrace_hash hash;
4356 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4358 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4360 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4362 struct ftrace_hash *hash;
4365 * The mapper is simply a ftrace_hash, but since the entries
4366 * in the hash are not ftrace_func_entry type, we define it
4367 * as a separate structure.
4369 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4370 return (struct ftrace_func_mapper *)hash;
4374 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4375 * @mapper: The mapper that has the ip maps
4376 * @ip: the instruction pointer to find the data for
4378 * Returns the data mapped to @ip if found otherwise NULL. The return
4379 * is actually the address of the mapper data pointer. The address is
4380 * returned for use cases where the data is no bigger than a long, and
4381 * the user can use the data pointer as its data instead of having to
4382 * allocate more memory for the reference.
4384 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4387 struct ftrace_func_entry *entry;
4388 struct ftrace_func_map *map;
4390 entry = ftrace_lookup_ip(&mapper->hash, ip);
4394 map = (struct ftrace_func_map *)entry;
4399 * ftrace_func_mapper_add_ip - Map some data to an ip
4400 * @mapper: The mapper that has the ip maps
4401 * @ip: The instruction pointer address to map @data to
4402 * @data: The data to map to @ip
4404 * Returns 0 on success otherwise an error.
4406 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4407 unsigned long ip, void *data)
4409 struct ftrace_func_entry *entry;
4410 struct ftrace_func_map *map;
4412 entry = ftrace_lookup_ip(&mapper->hash, ip);
4416 map = kmalloc(sizeof(*map), GFP_KERNEL);
4423 __add_hash_entry(&mapper->hash, &map->entry);
4429 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4430 * @mapper: The mapper that has the ip maps
4431 * @ip: The instruction pointer address to remove the data from
4433 * Returns the data if it is found, otherwise NULL.
4434 * Note, if the data pointer is used as the data itself, (see
4435 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4436 * if the data pointer was set to zero.
4438 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4441 struct ftrace_func_entry *entry;
4442 struct ftrace_func_map *map;
4445 entry = ftrace_lookup_ip(&mapper->hash, ip);
4449 map = (struct ftrace_func_map *)entry;
4452 remove_hash_entry(&mapper->hash, entry);
4459 * free_ftrace_func_mapper - free a mapping of ips and data
4460 * @mapper: The mapper that has the ip maps
4461 * @free_func: A function to be called on each data item.
4463 * This is used to free the function mapper. The @free_func is optional
4464 * and can be used if the data needs to be freed as well.
4466 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4467 ftrace_mapper_func free_func)
4469 struct ftrace_func_entry *entry;
4470 struct ftrace_func_map *map;
4471 struct hlist_head *hhd;
4477 if (free_func && mapper->hash.count) {
4478 size = 1 << mapper->hash.size_bits;
4479 for (i = 0; i < size; i++) {
4480 hhd = &mapper->hash.buckets[i];
4481 hlist_for_each_entry(entry, hhd, hlist) {
4482 map = (struct ftrace_func_map *)entry;
4487 free_ftrace_hash(&mapper->hash);
4490 static void release_probe(struct ftrace_func_probe *probe)
4492 struct ftrace_probe_ops *probe_ops;
4494 mutex_lock(&ftrace_lock);
4496 WARN_ON(probe->ref <= 0);
4498 /* Subtract the ref that was used to protect this instance */
4502 probe_ops = probe->probe_ops;
4504 * Sending zero as ip tells probe_ops to free
4505 * the probe->data itself
4507 if (probe_ops->free)
4508 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4509 list_del(&probe->list);
4512 mutex_unlock(&ftrace_lock);
4515 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4518 * Add one ref to keep it from being freed when releasing the
4519 * ftrace_lock mutex.
4525 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4526 struct ftrace_probe_ops *probe_ops,
4529 struct ftrace_func_entry *entry;
4530 struct ftrace_func_probe *probe;
4531 struct ftrace_hash **orig_hash;
4532 struct ftrace_hash *old_hash;
4533 struct ftrace_hash *hash;
4542 /* We do not support '!' for function probes */
4543 if (WARN_ON(glob[0] == '!'))
4547 mutex_lock(&ftrace_lock);
4548 /* Check if the probe_ops is already registered */
4549 list_for_each_entry(probe, &tr->func_probes, list) {
4550 if (probe->probe_ops == probe_ops)
4553 if (&probe->list == &tr->func_probes) {
4554 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4556 mutex_unlock(&ftrace_lock);
4559 probe->probe_ops = probe_ops;
4560 probe->ops.func = function_trace_probe_call;
4562 ftrace_ops_init(&probe->ops);
4563 list_add(&probe->list, &tr->func_probes);
4566 acquire_probe_locked(probe);
4568 mutex_unlock(&ftrace_lock);
4571 * Note, there's a small window here that the func_hash->filter_hash
4572 * may be NULL or empty. Need to be careful when reading the loop.
4574 mutex_lock(&probe->ops.func_hash->regex_lock);
4576 orig_hash = &probe->ops.func_hash->filter_hash;
4577 old_hash = *orig_hash;
4578 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4585 ret = ftrace_match_records(hash, glob, strlen(glob));
4587 /* Nothing found? */
4594 size = 1 << hash->size_bits;
4595 for (i = 0; i < size; i++) {
4596 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4597 if (ftrace_lookup_ip(old_hash, entry->ip))
4600 * The caller might want to do something special
4601 * for each function we find. We call the callback
4602 * to give the caller an opportunity to do so.
4604 if (probe_ops->init) {
4605 ret = probe_ops->init(probe_ops, tr,
4609 if (probe_ops->free && count)
4610 probe_ops->free(probe_ops, tr,
4620 mutex_lock(&ftrace_lock);
4623 /* Nothing was added? */
4628 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4633 /* One ref for each new function traced */
4634 probe->ref += count;
4636 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4637 ret = ftrace_startup(&probe->ops, 0);
4640 mutex_unlock(&ftrace_lock);
4645 mutex_unlock(&probe->ops.func_hash->regex_lock);
4646 free_ftrace_hash(hash);
4648 release_probe(probe);
4653 if (!probe_ops->free || !count)
4656 /* Failed to do the move, need to call the free functions */
4657 for (i = 0; i < size; i++) {
4658 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4659 if (ftrace_lookup_ip(old_hash, entry->ip))
4661 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4668 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4669 struct ftrace_probe_ops *probe_ops)
4671 struct ftrace_ops_hash old_hash_ops;
4672 struct ftrace_func_entry *entry;
4673 struct ftrace_func_probe *probe;
4674 struct ftrace_glob func_g;
4675 struct ftrace_hash **orig_hash;
4676 struct ftrace_hash *old_hash;
4677 struct ftrace_hash *hash = NULL;
4678 struct hlist_node *tmp;
4679 struct hlist_head hhd;
4680 char str[KSYM_SYMBOL_LEN];
4682 int i, ret = -ENODEV;
4685 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4686 func_g.search = NULL;
4690 func_g.type = filter_parse_regex(glob, strlen(glob),
4691 &func_g.search, ¬);
4692 func_g.len = strlen(func_g.search);
4694 /* we do not support '!' for function probes */
4699 mutex_lock(&ftrace_lock);
4700 /* Check if the probe_ops is already registered */
4701 list_for_each_entry(probe, &tr->func_probes, list) {
4702 if (probe->probe_ops == probe_ops)
4705 if (&probe->list == &tr->func_probes)
4706 goto err_unlock_ftrace;
4709 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4710 goto err_unlock_ftrace;
4712 acquire_probe_locked(probe);
4714 mutex_unlock(&ftrace_lock);
4716 mutex_lock(&probe->ops.func_hash->regex_lock);
4718 orig_hash = &probe->ops.func_hash->filter_hash;
4719 old_hash = *orig_hash;
4721 if (ftrace_hash_empty(old_hash))
4724 old_hash_ops.filter_hash = old_hash;
4725 /* Probes only have filters */
4726 old_hash_ops.notrace_hash = NULL;
4729 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4733 INIT_HLIST_HEAD(&hhd);
4735 size = 1 << hash->size_bits;
4736 for (i = 0; i < size; i++) {
4737 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4739 if (func_g.search) {
4740 kallsyms_lookup(entry->ip, NULL, NULL,
4742 if (!ftrace_match(str, &func_g))
4746 remove_hash_entry(hash, entry);
4747 hlist_add_head(&entry->hlist, &hhd);
4751 /* Nothing found? */
4757 mutex_lock(&ftrace_lock);
4759 WARN_ON(probe->ref < count);
4761 probe->ref -= count;
4763 if (ftrace_hash_empty(hash))
4764 ftrace_shutdown(&probe->ops, 0);
4766 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4769 /* still need to update the function call sites */
4770 if (ftrace_enabled && !ftrace_hash_empty(hash))
4771 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4775 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4776 hlist_del(&entry->hlist);
4777 if (probe_ops->free)
4778 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4781 mutex_unlock(&ftrace_lock);
4784 mutex_unlock(&probe->ops.func_hash->regex_lock);
4785 free_ftrace_hash(hash);
4787 release_probe(probe);
4792 mutex_unlock(&ftrace_lock);
4796 void clear_ftrace_function_probes(struct trace_array *tr)
4798 struct ftrace_func_probe *probe, *n;
4800 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4801 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4804 static LIST_HEAD(ftrace_commands);
4805 static DEFINE_MUTEX(ftrace_cmd_mutex);
4808 * Currently we only register ftrace commands from __init, so mark this
4811 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4813 struct ftrace_func_command *p;
4816 mutex_lock(&ftrace_cmd_mutex);
4817 list_for_each_entry(p, &ftrace_commands, list) {
4818 if (strcmp(cmd->name, p->name) == 0) {
4823 list_add(&cmd->list, &ftrace_commands);
4825 mutex_unlock(&ftrace_cmd_mutex);
4831 * Currently we only unregister ftrace commands from __init, so mark
4834 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4836 struct ftrace_func_command *p, *n;
4839 mutex_lock(&ftrace_cmd_mutex);
4840 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4841 if (strcmp(cmd->name, p->name) == 0) {
4843 list_del_init(&p->list);
4848 mutex_unlock(&ftrace_cmd_mutex);
4853 static int ftrace_process_regex(struct ftrace_iterator *iter,
4854 char *buff, int len, int enable)
4856 struct ftrace_hash *hash = iter->hash;
4857 struct trace_array *tr = iter->ops->private;
4858 char *func, *command, *next = buff;
4859 struct ftrace_func_command *p;
4862 func = strsep(&next, ":");
4865 ret = ftrace_match_records(hash, func, len);
4875 command = strsep(&next, ":");
4877 mutex_lock(&ftrace_cmd_mutex);
4878 list_for_each_entry(p, &ftrace_commands, list) {
4879 if (strcmp(p->name, command) == 0) {
4880 ret = p->func(tr, hash, func, command, next, enable);
4885 mutex_unlock(&ftrace_cmd_mutex);
4891 ftrace_regex_write(struct file *file, const char __user *ubuf,
4892 size_t cnt, loff_t *ppos, int enable)
4894 struct ftrace_iterator *iter;
4895 struct trace_parser *parser;
4901 if (file->f_mode & FMODE_READ) {
4902 struct seq_file *m = file->private_data;
4905 iter = file->private_data;
4907 if (unlikely(ftrace_disabled))
4910 /* iter->hash is a local copy, so we don't need regex_lock */
4912 parser = &iter->parser;
4913 read = trace_get_user(parser, ubuf, cnt, ppos);
4915 if (read >= 0 && trace_parser_loaded(parser) &&
4916 !trace_parser_cont(parser)) {
4917 ret = ftrace_process_regex(iter, parser->buffer,
4918 parser->idx, enable);
4919 trace_parser_clear(parser);
4930 ftrace_filter_write(struct file *file, const char __user *ubuf,
4931 size_t cnt, loff_t *ppos)
4933 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4937 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4938 size_t cnt, loff_t *ppos)
4940 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4944 __ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4946 struct ftrace_func_entry *entry;
4948 ip = ftrace_location(ip);
4953 entry = ftrace_lookup_ip(hash, ip);
4956 free_hash_entry(hash, entry);
4960 return add_hash_entry(hash, ip);
4964 ftrace_match_addr(struct ftrace_hash *hash, unsigned long *ips,
4965 unsigned int cnt, int remove)
4970 for (i = 0; i < cnt; i++) {
4971 err = __ftrace_match_addr(hash, ips[i], remove);
4974 * This expects the @hash is a temporary hash and if this
4975 * fails the caller must free the @hash.
4984 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4985 unsigned long *ips, unsigned int cnt,
4986 int remove, int reset, int enable)
4988 struct ftrace_hash **orig_hash;
4989 struct ftrace_hash *hash;
4992 if (unlikely(ftrace_disabled))
4995 mutex_lock(&ops->func_hash->regex_lock);
4998 orig_hash = &ops->func_hash->filter_hash;
5000 orig_hash = &ops->func_hash->notrace_hash;
5003 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5005 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
5009 goto out_regex_unlock;
5012 if (buf && !ftrace_match_records(hash, buf, len)) {
5014 goto out_regex_unlock;
5017 ret = ftrace_match_addr(hash, ips, cnt, remove);
5019 goto out_regex_unlock;
5022 mutex_lock(&ftrace_lock);
5023 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
5024 mutex_unlock(&ftrace_lock);
5027 mutex_unlock(&ops->func_hash->regex_lock);
5029 free_ftrace_hash(hash);
5034 ftrace_set_addr(struct ftrace_ops *ops, unsigned long *ips, unsigned int cnt,
5035 int remove, int reset, int enable)
5037 return ftrace_set_hash(ops, NULL, 0, ips, cnt, remove, reset, enable);
5040 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
5042 struct ftrace_direct_func {
5043 struct list_head next;
5048 static LIST_HEAD(ftrace_direct_funcs);
5051 * ftrace_find_direct_func - test an address if it is a registered direct caller
5052 * @addr: The address of a registered direct caller
5054 * This searches to see if a ftrace direct caller has been registered
5055 * at a specific address, and if so, it returns a descriptor for it.
5057 * This can be used by architecture code to see if an address is
5058 * a direct caller (trampoline) attached to a fentry/mcount location.
5059 * This is useful for the function_graph tracer, as it may need to
5060 * do adjustments if it traced a location that also has a direct
5061 * trampoline attached to it.
5063 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
5065 struct ftrace_direct_func *entry;
5068 /* May be called by fgraph trampoline (protected by rcu tasks) */
5069 list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
5070 if (entry->addr == addr) {
5081 static struct ftrace_direct_func *ftrace_alloc_direct_func(unsigned long addr)
5083 struct ftrace_direct_func *direct;
5085 direct = kmalloc(sizeof(*direct), GFP_KERNEL);
5088 direct->addr = addr;
5090 list_add_rcu(&direct->next, &ftrace_direct_funcs);
5091 ftrace_direct_func_count++;
5096 * register_ftrace_direct - Call a custom trampoline directly
5097 * @ip: The address of the nop at the beginning of a function
5098 * @addr: The address of the trampoline to call at @ip
5100 * This is used to connect a direct call from the nop location (@ip)
5101 * at the start of ftrace traced functions. The location that it calls
5102 * (@addr) must be able to handle a direct call, and save the parameters
5103 * of the function being traced, and restore them (or inject new ones
5104 * if needed), before returning.
5108 * -EBUSY - Another direct function is already attached (there can be only one)
5109 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5110 * -ENOMEM - There was an allocation failure.
5112 int register_ftrace_direct(unsigned long ip, unsigned long addr)
5114 struct ftrace_direct_func *direct;
5115 struct ftrace_func_entry *entry;
5116 struct ftrace_hash *free_hash = NULL;
5117 struct dyn_ftrace *rec;
5120 mutex_lock(&direct_mutex);
5122 ip = ftrace_location(ip);
5126 /* See if there's a direct function at @ip already */
5128 if (ftrace_find_rec_direct(ip))
5132 rec = lookup_rec(ip, ip);
5137 * Check if the rec says it has a direct call but we didn't
5140 if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
5143 /* Make sure the ip points to the exact record */
5144 if (ip != rec->ip) {
5146 /* Need to check this ip for a direct. */
5147 if (ftrace_find_rec_direct(ip))
5152 direct = ftrace_find_direct_func(addr);
5154 direct = ftrace_alloc_direct_func(addr);
5159 entry = ftrace_add_rec_direct(ip, addr, &free_hash);
5163 ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
5165 remove_hash_entry(direct_functions, entry);
5167 if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
5168 ret = register_ftrace_function(&direct_ops);
5170 ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5175 if (!direct->count) {
5176 list_del_rcu(&direct->next);
5177 synchronize_rcu_tasks();
5180 free_ftrace_hash(free_hash);
5182 ftrace_direct_func_count--;
5188 mutex_unlock(&direct_mutex);
5191 synchronize_rcu_tasks();
5192 free_ftrace_hash(free_hash);
5197 EXPORT_SYMBOL_GPL(register_ftrace_direct);
5199 static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
5200 struct dyn_ftrace **recp)
5202 struct ftrace_func_entry *entry;
5203 struct dyn_ftrace *rec;
5205 rec = lookup_rec(*ip, *ip);
5209 entry = __ftrace_lookup_ip(direct_functions, rec->ip);
5211 WARN_ON(rec->flags & FTRACE_FL_DIRECT);
5215 WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
5217 /* Passed in ip just needs to be on the call site */
5226 int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
5228 struct ftrace_direct_func *direct;
5229 struct ftrace_func_entry *entry;
5230 struct ftrace_hash *hash;
5233 mutex_lock(&direct_mutex);
5235 ip = ftrace_location(ip);
5239 entry = find_direct_entry(&ip, NULL);
5243 hash = direct_ops.func_hash->filter_hash;
5244 if (hash->count == 1)
5245 unregister_ftrace_function(&direct_ops);
5247 ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5251 remove_hash_entry(direct_functions, entry);
5253 direct = ftrace_find_direct_func(addr);
5254 if (!WARN_ON(!direct)) {
5255 /* This is the good path (see the ! before WARN) */
5257 WARN_ON(direct->count < 0);
5258 if (!direct->count) {
5259 list_del_rcu(&direct->next);
5260 synchronize_rcu_tasks();
5263 ftrace_direct_func_count--;
5267 mutex_unlock(&direct_mutex);
5271 EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
5273 static struct ftrace_ops stub_ops = {
5274 .func = ftrace_stub,
5278 * ftrace_modify_direct_caller - modify ftrace nop directly
5279 * @entry: The ftrace hash entry of the direct helper for @rec
5280 * @rec: The record representing the function site to patch
5281 * @old_addr: The location that the site at @rec->ip currently calls
5282 * @new_addr: The location that the site at @rec->ip should call
5284 * An architecture may overwrite this function to optimize the
5285 * changing of the direct callback on an ftrace nop location.
5286 * This is called with the ftrace_lock mutex held, and no other
5287 * ftrace callbacks are on the associated record (@rec). Thus,
5288 * it is safe to modify the ftrace record, where it should be
5289 * currently calling @old_addr directly, to call @new_addr.
5291 * Safety checks should be made to make sure that the code at
5292 * @rec->ip is currently calling @old_addr. And this must
5293 * also update entry->direct to @new_addr.
5295 int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
5296 struct dyn_ftrace *rec,
5297 unsigned long old_addr,
5298 unsigned long new_addr)
5300 unsigned long ip = rec->ip;
5304 * The ftrace_lock was used to determine if the record
5305 * had more than one registered user to it. If it did,
5306 * we needed to prevent that from changing to do the quick
5307 * switch. But if it did not (only a direct caller was attached)
5308 * then this function is called. But this function can deal
5309 * with attached callers to the rec that we care about, and
5310 * since this function uses standard ftrace calls that take
5311 * the ftrace_lock mutex, we need to release it.
5313 mutex_unlock(&ftrace_lock);
5316 * By setting a stub function at the same address, we force
5317 * the code to call the iterator and the direct_ops helper.
5318 * This means that @ip does not call the direct call, and
5319 * we can simply modify it.
5321 ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
5325 ret = register_ftrace_function(&stub_ops);
5327 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5331 entry->direct = new_addr;
5334 * By removing the stub, we put back the direct call, calling
5337 unregister_ftrace_function(&stub_ops);
5338 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5341 mutex_lock(&ftrace_lock);
5347 * modify_ftrace_direct - Modify an existing direct call to call something else
5348 * @ip: The instruction pointer to modify
5349 * @old_addr: The address that the current @ip calls directly
5350 * @new_addr: The address that the @ip should call
5352 * This modifies a ftrace direct caller at an instruction pointer without
5353 * having to disable it first. The direct call will switch over to the
5354 * @new_addr without missing anything.
5356 * Returns: zero on success. Non zero on error, which includes:
5357 * -ENODEV : the @ip given has no direct caller attached
5358 * -EINVAL : the @old_addr does not match the current direct caller
5360 int modify_ftrace_direct(unsigned long ip,
5361 unsigned long old_addr, unsigned long new_addr)
5363 struct ftrace_direct_func *direct, *new_direct = NULL;
5364 struct ftrace_func_entry *entry;
5365 struct dyn_ftrace *rec;
5368 mutex_lock(&direct_mutex);
5370 mutex_lock(&ftrace_lock);
5372 ip = ftrace_location(ip);
5376 entry = find_direct_entry(&ip, &rec);
5381 if (entry->direct != old_addr)
5384 direct = ftrace_find_direct_func(old_addr);
5385 if (WARN_ON(!direct))
5387 if (direct->count > 1) {
5389 new_direct = ftrace_alloc_direct_func(new_addr);
5393 new_direct->count++;
5395 direct->addr = new_addr;
5399 * If there's no other ftrace callback on the rec->ip location,
5400 * then it can be changed directly by the architecture.
5401 * If there is another caller, then we just need to change the
5402 * direct caller helper to point to @new_addr.
5404 if (ftrace_rec_count(rec) == 1) {
5405 ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
5407 entry->direct = new_addr;
5411 if (unlikely(ret && new_direct)) {
5413 list_del_rcu(&new_direct->next);
5414 synchronize_rcu_tasks();
5416 ftrace_direct_func_count--;
5420 mutex_unlock(&ftrace_lock);
5421 mutex_unlock(&direct_mutex);
5424 EXPORT_SYMBOL_GPL(modify_ftrace_direct);
5426 #define MULTI_FLAGS (FTRACE_OPS_FL_IPMODIFY | FTRACE_OPS_FL_DIRECT | \
5427 FTRACE_OPS_FL_SAVE_REGS)
5429 static int check_direct_multi(struct ftrace_ops *ops)
5431 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5433 if ((ops->flags & MULTI_FLAGS) != MULTI_FLAGS)
5438 static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long addr)
5440 struct ftrace_func_entry *entry, *del;
5443 size = 1 << hash->size_bits;
5444 for (i = 0; i < size; i++) {
5445 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5446 del = __ftrace_lookup_ip(direct_functions, entry->ip);
5447 if (del && del->direct == addr) {
5448 remove_hash_entry(direct_functions, del);
5456 * register_ftrace_direct_multi - Call a custom trampoline directly
5457 * for multiple functions registered in @ops
5458 * @ops: The address of the struct ftrace_ops object
5459 * @addr: The address of the trampoline to call at @ops functions
5461 * This is used to connect a direct calls to @addr from the nop locations
5462 * of the functions registered in @ops (with by ftrace_set_filter_ip
5465 * The location that it calls (@addr) must be able to handle a direct call,
5466 * and save the parameters of the function being traced, and restore them
5467 * (or inject new ones if needed), before returning.
5471 * -EINVAL - The @ops object was already registered with this call or
5472 * when there are no functions in @ops object.
5473 * -EBUSY - Another direct function is already attached (there can be only one)
5474 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5475 * -ENOMEM - There was an allocation failure.
5477 int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5479 struct ftrace_hash *hash, *free_hash = NULL;
5480 struct ftrace_func_entry *entry, *new;
5481 int err = -EBUSY, size, i;
5483 if (ops->func || ops->trampoline)
5485 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5487 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5490 hash = ops->func_hash->filter_hash;
5491 if (ftrace_hash_empty(hash))
5494 mutex_lock(&direct_mutex);
5496 /* Make sure requested entries are not already registered.. */
5497 size = 1 << hash->size_bits;
5498 for (i = 0; i < size; i++) {
5499 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5500 if (ftrace_find_rec_direct(entry->ip))
5505 /* ... and insert them to direct_functions hash. */
5507 for (i = 0; i < size; i++) {
5508 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5509 new = ftrace_add_rec_direct(entry->ip, addr, &free_hash);
5512 entry->direct = addr;
5516 ops->func = call_direct_funcs;
5517 ops->flags = MULTI_FLAGS;
5518 ops->trampoline = FTRACE_REGS_ADDR;
5520 err = register_ftrace_function(ops);
5524 remove_direct_functions_hash(hash, addr);
5527 mutex_unlock(&direct_mutex);
5530 synchronize_rcu_tasks();
5531 free_ftrace_hash(free_hash);
5535 EXPORT_SYMBOL_GPL(register_ftrace_direct_multi);
5538 * unregister_ftrace_direct_multi - Remove calls to custom trampoline
5539 * previously registered by register_ftrace_direct_multi for @ops object.
5540 * @ops: The address of the struct ftrace_ops object
5542 * This is used to remove a direct calls to @addr from the nop locations
5543 * of the functions registered in @ops (with by ftrace_set_filter_ip
5548 * -EINVAL - The @ops object was not properly registered.
5550 int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5552 struct ftrace_hash *hash = ops->func_hash->filter_hash;
5555 if (check_direct_multi(ops))
5557 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5560 mutex_lock(&direct_mutex);
5561 err = unregister_ftrace_function(ops);
5562 remove_direct_functions_hash(hash, addr);
5563 mutex_unlock(&direct_mutex);
5565 /* cleanup for possible another register call */
5567 ops->trampoline = 0;
5570 EXPORT_SYMBOL_GPL(unregister_ftrace_direct_multi);
5573 * modify_ftrace_direct_multi - Modify an existing direct 'multi' call
5574 * to call something else
5575 * @ops: The address of the struct ftrace_ops object
5576 * @addr: The address of the new trampoline to call at @ops functions
5578 * This is used to unregister currently registered direct caller and
5579 * register new one @addr on functions registered in @ops object.
5581 * Note there's window between ftrace_shutdown and ftrace_startup calls
5582 * where there will be no callbacks called.
5584 * Returns: zero on success. Non zero on error, which includes:
5585 * -EINVAL - The @ops object was not properly registered.
5587 int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5589 struct ftrace_hash *hash;
5590 struct ftrace_func_entry *entry, *iter;
5591 static struct ftrace_ops tmp_ops = {
5592 .func = ftrace_stub,
5593 .flags = FTRACE_OPS_FL_STUB,
5598 if (check_direct_multi(ops))
5600 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5603 mutex_lock(&direct_mutex);
5605 /* Enable the tmp_ops to have the same functions as the direct ops */
5606 ftrace_ops_init(&tmp_ops);
5607 tmp_ops.func_hash = ops->func_hash;
5609 err = register_ftrace_function(&tmp_ops);
5614 * Now the ftrace_ops_list_func() is called to do the direct callers.
5615 * We can safely change the direct functions attached to each entry.
5617 mutex_lock(&ftrace_lock);
5619 hash = ops->func_hash->filter_hash;
5620 size = 1 << hash->size_bits;
5621 for (i = 0; i < size; i++) {
5622 hlist_for_each_entry(iter, &hash->buckets[i], hlist) {
5623 entry = __ftrace_lookup_ip(direct_functions, iter->ip);
5626 entry->direct = addr;
5630 mutex_unlock(&ftrace_lock);
5632 /* Removing the tmp_ops will add the updated direct callers to the functions */
5633 unregister_ftrace_function(&tmp_ops);
5636 mutex_unlock(&direct_mutex);
5639 EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi);
5640 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
5643 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
5644 * @ops - the ops to set the filter with
5645 * @ip - the address to add to or remove from the filter.
5646 * @remove - non zero to remove the ip from the filter
5647 * @reset - non zero to reset all filters before applying this filter.
5649 * Filters denote which functions should be enabled when tracing is enabled
5650 * If @ip is NULL, it fails to update filter.
5652 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
5653 int remove, int reset)
5655 ftrace_ops_init(ops);
5656 return ftrace_set_addr(ops, &ip, 1, remove, reset, 1);
5658 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
5661 * ftrace_set_filter_ips - set functions to filter on in ftrace by addresses
5662 * @ops - the ops to set the filter with
5663 * @ips - the array of addresses to add to or remove from the filter.
5664 * @cnt - the number of addresses in @ips
5665 * @remove - non zero to remove ips from the filter
5666 * @reset - non zero to reset all filters before applying this filter.
5668 * Filters denote which functions should be enabled when tracing is enabled
5669 * If @ips array or any ip specified within is NULL , it fails to update filter.
5671 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
5672 unsigned int cnt, int remove, int reset)
5674 ftrace_ops_init(ops);
5675 return ftrace_set_addr(ops, ips, cnt, remove, reset, 1);
5677 EXPORT_SYMBOL_GPL(ftrace_set_filter_ips);
5680 * ftrace_ops_set_global_filter - setup ops to use global filters
5681 * @ops - the ops which will use the global filters
5683 * ftrace users who need global function trace filtering should call this.
5684 * It can set the global filter only if ops were not initialized before.
5686 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
5688 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
5691 ftrace_ops_init(ops);
5692 ops->func_hash = &global_ops.local_hash;
5694 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
5697 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
5698 int reset, int enable)
5700 return ftrace_set_hash(ops, buf, len, NULL, 0, 0, reset, enable);
5704 * ftrace_set_filter - set a function to filter on in ftrace
5705 * @ops - the ops to set the filter with
5706 * @buf - the string that holds the function filter text.
5707 * @len - the length of the string.
5708 * @reset - non zero to reset all filters before applying this filter.
5710 * Filters denote which functions should be enabled when tracing is enabled.
5711 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5713 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
5716 ftrace_ops_init(ops);
5717 return ftrace_set_regex(ops, buf, len, reset, 1);
5719 EXPORT_SYMBOL_GPL(ftrace_set_filter);
5722 * ftrace_set_notrace - set a function to not trace in ftrace
5723 * @ops - the ops to set the notrace filter with
5724 * @buf - the string that holds the function notrace text.
5725 * @len - the length of the string.
5726 * @reset - non zero to reset all filters before applying this filter.
5728 * Notrace Filters denote which functions should not be enabled when tracing
5729 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5732 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
5735 ftrace_ops_init(ops);
5736 return ftrace_set_regex(ops, buf, len, reset, 0);
5738 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
5740 * ftrace_set_global_filter - set a function to filter on with global tracers
5741 * @buf - the string that holds the function filter text.
5742 * @len - the length of the string.
5743 * @reset - non zero to reset all filters before applying this filter.
5745 * Filters denote which functions should be enabled when tracing is enabled.
5746 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5748 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
5750 ftrace_set_regex(&global_ops, buf, len, reset, 1);
5752 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
5755 * ftrace_set_global_notrace - set a function to not trace with global tracers
5756 * @buf - the string that holds the function notrace text.
5757 * @len - the length of the string.
5758 * @reset - non zero to reset all filters before applying this filter.
5760 * Notrace Filters denote which functions should not be enabled when tracing
5761 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5764 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
5766 ftrace_set_regex(&global_ops, buf, len, reset, 0);
5768 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
5771 * command line interface to allow users to set filters on boot up.
5773 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
5774 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5775 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
5777 /* Used by function selftest to not test if filter is set */
5778 bool ftrace_filter_param __initdata;
5780 static int __init set_ftrace_notrace(char *str)
5782 ftrace_filter_param = true;
5783 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
5786 __setup("ftrace_notrace=", set_ftrace_notrace);
5788 static int __init set_ftrace_filter(char *str)
5790 ftrace_filter_param = true;
5791 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
5794 __setup("ftrace_filter=", set_ftrace_filter);
5796 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5797 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
5798 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5799 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
5801 static int __init set_graph_function(char *str)
5803 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
5806 __setup("ftrace_graph_filter=", set_graph_function);
5808 static int __init set_graph_notrace_function(char *str)
5810 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
5813 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
5815 static int __init set_graph_max_depth_function(char *str)
5819 fgraph_max_depth = simple_strtoul(str, NULL, 0);
5822 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
5824 static void __init set_ftrace_early_graph(char *buf, int enable)
5828 struct ftrace_hash *hash;
5830 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5831 if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
5835 func = strsep(&buf, ",");
5836 /* we allow only one expression at a time */
5837 ret = ftrace_graph_set_hash(hash, func);
5839 printk(KERN_DEBUG "ftrace: function %s not "
5840 "traceable\n", func);
5844 ftrace_graph_hash = hash;
5846 ftrace_graph_notrace_hash = hash;
5848 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5851 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
5855 ftrace_ops_init(ops);
5858 func = strsep(&buf, ",");
5859 ftrace_set_regex(ops, func, strlen(func), 0, enable);
5863 static void __init set_ftrace_early_filters(void)
5865 if (ftrace_filter_buf[0])
5866 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5867 if (ftrace_notrace_buf[0])
5868 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5869 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5870 if (ftrace_graph_buf[0])
5871 set_ftrace_early_graph(ftrace_graph_buf, 1);
5872 if (ftrace_graph_notrace_buf[0])
5873 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5874 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5877 int ftrace_regex_release(struct inode *inode, struct file *file)
5879 struct seq_file *m = (struct seq_file *)file->private_data;
5880 struct ftrace_iterator *iter;
5881 struct ftrace_hash **orig_hash;
5882 struct trace_parser *parser;
5885 if (file->f_mode & FMODE_READ) {
5887 seq_release(inode, file);
5889 iter = file->private_data;
5891 parser = &iter->parser;
5892 if (trace_parser_loaded(parser)) {
5893 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
5895 ftrace_process_regex(iter, parser->buffer,
5896 parser->idx, enable);
5899 trace_parser_put(parser);
5901 mutex_lock(&iter->ops->func_hash->regex_lock);
5903 if (file->f_mode & FMODE_WRITE) {
5904 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5907 orig_hash = &iter->ops->func_hash->filter_hash;
5908 if (iter->tr && !list_empty(&iter->tr->mod_trace))
5909 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5911 orig_hash = &iter->ops->func_hash->notrace_hash;
5913 mutex_lock(&ftrace_lock);
5914 ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5915 iter->hash, filter_hash);
5916 mutex_unlock(&ftrace_lock);
5918 /* For read only, the hash is the ops hash */
5922 mutex_unlock(&iter->ops->func_hash->regex_lock);
5923 free_ftrace_hash(iter->hash);
5925 trace_array_put(iter->tr);
5931 static const struct file_operations ftrace_avail_fops = {
5932 .open = ftrace_avail_open,
5934 .llseek = seq_lseek,
5935 .release = seq_release_private,
5938 static const struct file_operations ftrace_enabled_fops = {
5939 .open = ftrace_enabled_open,
5941 .llseek = seq_lseek,
5942 .release = seq_release_private,
5945 static const struct file_operations ftrace_filter_fops = {
5946 .open = ftrace_filter_open,
5948 .write = ftrace_filter_write,
5949 .llseek = tracing_lseek,
5950 .release = ftrace_regex_release,
5953 static const struct file_operations ftrace_notrace_fops = {
5954 .open = ftrace_notrace_open,
5956 .write = ftrace_notrace_write,
5957 .llseek = tracing_lseek,
5958 .release = ftrace_regex_release,
5961 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5963 static DEFINE_MUTEX(graph_lock);
5965 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
5966 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
5968 enum graph_filter_type {
5969 GRAPH_FILTER_NOTRACE = 0,
5970 GRAPH_FILTER_FUNCTION,
5973 #define FTRACE_GRAPH_EMPTY ((void *)1)
5975 struct ftrace_graph_data {
5976 struct ftrace_hash *hash;
5977 struct ftrace_func_entry *entry;
5978 int idx; /* for hash table iteration */
5979 enum graph_filter_type type;
5980 struct ftrace_hash *new_hash;
5981 const struct seq_operations *seq_ops;
5982 struct trace_parser parser;
5986 __g_next(struct seq_file *m, loff_t *pos)
5988 struct ftrace_graph_data *fgd = m->private;
5989 struct ftrace_func_entry *entry = fgd->entry;
5990 struct hlist_head *head;
5991 int i, idx = fgd->idx;
5993 if (*pos >= fgd->hash->count)
5997 hlist_for_each_entry_continue(entry, hlist) {
6005 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
6006 head = &fgd->hash->buckets[i];
6007 hlist_for_each_entry(entry, head, hlist) {
6017 g_next(struct seq_file *m, void *v, loff_t *pos)
6020 return __g_next(m, pos);
6023 static void *g_start(struct seq_file *m, loff_t *pos)
6025 struct ftrace_graph_data *fgd = m->private;
6027 mutex_lock(&graph_lock);
6029 if (fgd->type == GRAPH_FILTER_FUNCTION)
6030 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
6031 lockdep_is_held(&graph_lock));
6033 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6034 lockdep_is_held(&graph_lock));
6036 /* Nothing, tell g_show to print all functions are enabled */
6037 if (ftrace_hash_empty(fgd->hash) && !*pos)
6038 return FTRACE_GRAPH_EMPTY;
6042 return __g_next(m, pos);
6045 static void g_stop(struct seq_file *m, void *p)
6047 mutex_unlock(&graph_lock);
6050 static int g_show(struct seq_file *m, void *v)
6052 struct ftrace_func_entry *entry = v;
6057 if (entry == FTRACE_GRAPH_EMPTY) {
6058 struct ftrace_graph_data *fgd = m->private;
6060 if (fgd->type == GRAPH_FILTER_FUNCTION)
6061 seq_puts(m, "#### all functions enabled ####\n");
6063 seq_puts(m, "#### no functions disabled ####\n");
6067 seq_printf(m, "%ps\n", (void *)entry->ip);
6072 static const struct seq_operations ftrace_graph_seq_ops = {
6080 __ftrace_graph_open(struct inode *inode, struct file *file,
6081 struct ftrace_graph_data *fgd)
6084 struct ftrace_hash *new_hash = NULL;
6086 ret = security_locked_down(LOCKDOWN_TRACEFS);
6090 if (file->f_mode & FMODE_WRITE) {
6091 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
6093 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
6096 if (file->f_flags & O_TRUNC)
6097 new_hash = alloc_ftrace_hash(size_bits);
6099 new_hash = alloc_and_copy_ftrace_hash(size_bits,
6107 if (file->f_mode & FMODE_READ) {
6108 ret = seq_open(file, &ftrace_graph_seq_ops);
6110 struct seq_file *m = file->private_data;
6114 free_ftrace_hash(new_hash);
6118 file->private_data = fgd;
6121 if (ret < 0 && file->f_mode & FMODE_WRITE)
6122 trace_parser_put(&fgd->parser);
6124 fgd->new_hash = new_hash;
6127 * All uses of fgd->hash must be taken with the graph_lock
6128 * held. The graph_lock is going to be released, so force
6129 * fgd->hash to be reinitialized when it is taken again.
6137 ftrace_graph_open(struct inode *inode, struct file *file)
6139 struct ftrace_graph_data *fgd;
6142 if (unlikely(ftrace_disabled))
6145 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6149 mutex_lock(&graph_lock);
6151 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
6152 lockdep_is_held(&graph_lock));
6153 fgd->type = GRAPH_FILTER_FUNCTION;
6154 fgd->seq_ops = &ftrace_graph_seq_ops;
6156 ret = __ftrace_graph_open(inode, file, fgd);
6160 mutex_unlock(&graph_lock);
6165 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
6167 struct ftrace_graph_data *fgd;
6170 if (unlikely(ftrace_disabled))
6173 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6177 mutex_lock(&graph_lock);
6179 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6180 lockdep_is_held(&graph_lock));
6181 fgd->type = GRAPH_FILTER_NOTRACE;
6182 fgd->seq_ops = &ftrace_graph_seq_ops;
6184 ret = __ftrace_graph_open(inode, file, fgd);
6188 mutex_unlock(&graph_lock);
6193 ftrace_graph_release(struct inode *inode, struct file *file)
6195 struct ftrace_graph_data *fgd;
6196 struct ftrace_hash *old_hash, *new_hash;
6197 struct trace_parser *parser;
6200 if (file->f_mode & FMODE_READ) {
6201 struct seq_file *m = file->private_data;
6204 seq_release(inode, file);
6206 fgd = file->private_data;
6210 if (file->f_mode & FMODE_WRITE) {
6212 parser = &fgd->parser;
6214 if (trace_parser_loaded((parser))) {
6215 ret = ftrace_graph_set_hash(fgd->new_hash,
6219 trace_parser_put(parser);
6221 new_hash = __ftrace_hash_move(fgd->new_hash);
6227 mutex_lock(&graph_lock);
6229 if (fgd->type == GRAPH_FILTER_FUNCTION) {
6230 old_hash = rcu_dereference_protected(ftrace_graph_hash,
6231 lockdep_is_held(&graph_lock));
6232 rcu_assign_pointer(ftrace_graph_hash, new_hash);
6234 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6235 lockdep_is_held(&graph_lock));
6236 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
6239 mutex_unlock(&graph_lock);
6242 * We need to do a hard force of sched synchronization.
6243 * This is because we use preempt_disable() to do RCU, but
6244 * the function tracers can be called where RCU is not watching
6245 * (like before user_exit()). We can not rely on the RCU
6246 * infrastructure to do the synchronization, thus we must do it
6249 if (old_hash != EMPTY_HASH)
6250 synchronize_rcu_tasks_rude();
6252 free_ftrace_hash(old_hash);
6256 free_ftrace_hash(fgd->new_hash);
6263 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
6265 struct ftrace_glob func_g;
6266 struct dyn_ftrace *rec;
6267 struct ftrace_page *pg;
6268 struct ftrace_func_entry *entry;
6273 func_g.type = filter_parse_regex(buffer, strlen(buffer),
6274 &func_g.search, ¬);
6276 func_g.len = strlen(func_g.search);
6278 mutex_lock(&ftrace_lock);
6280 if (unlikely(ftrace_disabled)) {
6281 mutex_unlock(&ftrace_lock);
6285 do_for_each_ftrace_rec(pg, rec) {
6287 if (rec->flags & FTRACE_FL_DISABLED)
6290 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
6291 entry = ftrace_lookup_ip(hash, rec->ip);
6298 if (add_hash_entry(hash, rec->ip) < 0)
6302 free_hash_entry(hash, entry);
6307 } while_for_each_ftrace_rec();
6309 mutex_unlock(&ftrace_lock);
6318 ftrace_graph_write(struct file *file, const char __user *ubuf,
6319 size_t cnt, loff_t *ppos)
6321 ssize_t read, ret = 0;
6322 struct ftrace_graph_data *fgd = file->private_data;
6323 struct trace_parser *parser;
6328 /* Read mode uses seq functions */
6329 if (file->f_mode & FMODE_READ) {
6330 struct seq_file *m = file->private_data;
6334 parser = &fgd->parser;
6336 read = trace_get_user(parser, ubuf, cnt, ppos);
6338 if (read >= 0 && trace_parser_loaded(parser) &&
6339 !trace_parser_cont(parser)) {
6341 ret = ftrace_graph_set_hash(fgd->new_hash,
6343 trace_parser_clear(parser);
6352 static const struct file_operations ftrace_graph_fops = {
6353 .open = ftrace_graph_open,
6355 .write = ftrace_graph_write,
6356 .llseek = tracing_lseek,
6357 .release = ftrace_graph_release,
6360 static const struct file_operations ftrace_graph_notrace_fops = {
6361 .open = ftrace_graph_notrace_open,
6363 .write = ftrace_graph_write,
6364 .llseek = tracing_lseek,
6365 .release = ftrace_graph_release,
6367 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6369 void ftrace_create_filter_files(struct ftrace_ops *ops,
6370 struct dentry *parent)
6373 trace_create_file("set_ftrace_filter", TRACE_MODE_WRITE, parent,
6374 ops, &ftrace_filter_fops);
6376 trace_create_file("set_ftrace_notrace", TRACE_MODE_WRITE, parent,
6377 ops, &ftrace_notrace_fops);
6381 * The name "destroy_filter_files" is really a misnomer. Although
6382 * in the future, it may actually delete the files, but this is
6383 * really intended to make sure the ops passed in are disabled
6384 * and that when this function returns, the caller is free to
6387 * The "destroy" name is only to match the "create" name that this
6388 * should be paired with.
6390 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
6392 mutex_lock(&ftrace_lock);
6393 if (ops->flags & FTRACE_OPS_FL_ENABLED)
6394 ftrace_shutdown(ops, 0);
6395 ops->flags |= FTRACE_OPS_FL_DELETED;
6396 ftrace_free_filter(ops);
6397 mutex_unlock(&ftrace_lock);
6400 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
6403 trace_create_file("available_filter_functions", TRACE_MODE_READ,
6404 d_tracer, NULL, &ftrace_avail_fops);
6406 trace_create_file("enabled_functions", TRACE_MODE_READ,
6407 d_tracer, NULL, &ftrace_enabled_fops);
6409 ftrace_create_filter_files(&global_ops, d_tracer);
6411 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6412 trace_create_file("set_graph_function", TRACE_MODE_WRITE, d_tracer,
6414 &ftrace_graph_fops);
6415 trace_create_file("set_graph_notrace", TRACE_MODE_WRITE, d_tracer,
6417 &ftrace_graph_notrace_fops);
6418 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6423 static int ftrace_cmp_ips(const void *a, const void *b)
6425 const unsigned long *ipa = a;
6426 const unsigned long *ipb = b;
6435 #ifdef CONFIG_FTRACE_SORT_STARTUP_TEST
6436 static void test_is_sorted(unsigned long *start, unsigned long count)
6440 for (i = 1; i < count; i++) {
6441 if (WARN(start[i - 1] > start[i],
6442 "[%d] %pS at %lx is not sorted with %pS at %lx\n", i,
6443 (void *)start[i - 1], start[i - 1],
6444 (void *)start[i], start[i]))
6448 pr_info("ftrace section at %px sorted properly\n", start);
6451 static void test_is_sorted(unsigned long *start, unsigned long count)
6456 static int ftrace_process_locs(struct module *mod,
6457 unsigned long *start,
6460 struct ftrace_page *start_pg;
6461 struct ftrace_page *pg;
6462 struct dyn_ftrace *rec;
6463 unsigned long count;
6466 unsigned long flags = 0; /* Shut up gcc */
6469 count = end - start;
6475 * Sorting mcount in vmlinux at build time depend on
6476 * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in
6477 * modules can not be sorted at build time.
6479 if (!IS_ENABLED(CONFIG_BUILDTIME_MCOUNT_SORT) || mod) {
6480 sort(start, count, sizeof(*start),
6481 ftrace_cmp_ips, NULL);
6483 test_is_sorted(start, count);
6486 start_pg = ftrace_allocate_pages(count);
6490 mutex_lock(&ftrace_lock);
6493 * Core and each module needs their own pages, as
6494 * modules will free them when they are removed.
6495 * Force a new page to be allocated for modules.
6498 WARN_ON(ftrace_pages || ftrace_pages_start);
6499 /* First initialization */
6500 ftrace_pages = ftrace_pages_start = start_pg;
6505 if (WARN_ON(ftrace_pages->next)) {
6506 /* Hmm, we have free pages? */
6507 while (ftrace_pages->next)
6508 ftrace_pages = ftrace_pages->next;
6511 ftrace_pages->next = start_pg;
6517 unsigned long end_offset;
6518 addr = ftrace_call_adjust(*p++);
6520 * Some architecture linkers will pad between
6521 * the different mcount_loc sections of different
6522 * object files to satisfy alignments.
6523 * Skip any NULL pointers.
6528 end_offset = (pg->index+1) * sizeof(pg->records[0]);
6529 if (end_offset > PAGE_SIZE << pg->order) {
6530 /* We should have allocated enough */
6531 if (WARN_ON(!pg->next))
6536 rec = &pg->records[pg->index++];
6540 /* We should have used all pages */
6543 /* Assign the last page to ftrace_pages */
6547 * We only need to disable interrupts on start up
6548 * because we are modifying code that an interrupt
6549 * may execute, and the modification is not atomic.
6550 * But for modules, nothing runs the code we modify
6551 * until we are finished with it, and there's no
6552 * reason to cause large interrupt latencies while we do it.
6555 local_irq_save(flags);
6556 ftrace_update_code(mod, start_pg);
6558 local_irq_restore(flags);
6561 mutex_unlock(&ftrace_lock);
6566 struct ftrace_mod_func {
6567 struct list_head list;
6573 struct ftrace_mod_map {
6574 struct rcu_head rcu;
6575 struct list_head list;
6577 unsigned long start_addr;
6578 unsigned long end_addr;
6579 struct list_head funcs;
6580 unsigned int num_funcs;
6583 static int ftrace_get_trampoline_kallsym(unsigned int symnum,
6584 unsigned long *value, char *type,
6585 char *name, char *module_name,
6588 struct ftrace_ops *op;
6590 list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) {
6591 if (!op->trampoline || symnum--)
6593 *value = op->trampoline;
6595 strlcpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN);
6596 strlcpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN);
6604 #ifdef CONFIG_MODULES
6606 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
6608 static LIST_HEAD(ftrace_mod_maps);
6610 static int referenced_filters(struct dyn_ftrace *rec)
6612 struct ftrace_ops *ops;
6615 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
6616 if (ops_references_rec(ops, rec)) {
6617 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT))
6619 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY))
6622 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
6623 rec->flags |= FTRACE_FL_REGS;
6624 if (cnt == 1 && ops->trampoline)
6625 rec->flags |= FTRACE_FL_TRAMP;
6627 rec->flags &= ~FTRACE_FL_TRAMP;
6635 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
6637 struct ftrace_func_entry *entry;
6638 struct dyn_ftrace *rec;
6641 if (ftrace_hash_empty(hash))
6644 for (i = 0; i < pg->index; i++) {
6645 rec = &pg->records[i];
6646 entry = __ftrace_lookup_ip(hash, rec->ip);
6648 * Do not allow this rec to match again.
6649 * Yeah, it may waste some memory, but will be removed
6650 * if/when the hash is modified again.
6657 /* Clear any records from hashes */
6658 static void clear_mod_from_hashes(struct ftrace_page *pg)
6660 struct trace_array *tr;
6662 mutex_lock(&trace_types_lock);
6663 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6664 if (!tr->ops || !tr->ops->func_hash)
6666 mutex_lock(&tr->ops->func_hash->regex_lock);
6667 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
6668 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
6669 mutex_unlock(&tr->ops->func_hash->regex_lock);
6671 mutex_unlock(&trace_types_lock);
6674 static void ftrace_free_mod_map(struct rcu_head *rcu)
6676 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
6677 struct ftrace_mod_func *mod_func;
6678 struct ftrace_mod_func *n;
6680 /* All the contents of mod_map are now not visible to readers */
6681 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
6682 kfree(mod_func->name);
6683 list_del(&mod_func->list);
6690 void ftrace_release_mod(struct module *mod)
6692 struct ftrace_mod_map *mod_map;
6693 struct ftrace_mod_map *n;
6694 struct dyn_ftrace *rec;
6695 struct ftrace_page **last_pg;
6696 struct ftrace_page *tmp_page = NULL;
6697 struct ftrace_page *pg;
6699 mutex_lock(&ftrace_lock);
6701 if (ftrace_disabled)
6704 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
6705 if (mod_map->mod == mod) {
6706 list_del_rcu(&mod_map->list);
6707 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
6713 * Each module has its own ftrace_pages, remove
6714 * them from the list.
6716 last_pg = &ftrace_pages_start;
6717 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
6718 rec = &pg->records[0];
6719 if (within_module_core(rec->ip, mod) ||
6720 within_module_init(rec->ip, mod)) {
6722 * As core pages are first, the first
6723 * page should never be a module page.
6725 if (WARN_ON(pg == ftrace_pages_start))
6728 /* Check if we are deleting the last page */
6729 if (pg == ftrace_pages)
6730 ftrace_pages = next_to_ftrace_page(last_pg);
6732 ftrace_update_tot_cnt -= pg->index;
6733 *last_pg = pg->next;
6735 pg->next = tmp_page;
6738 last_pg = &pg->next;
6741 mutex_unlock(&ftrace_lock);
6743 for (pg = tmp_page; pg; pg = tmp_page) {
6745 /* Needs to be called outside of ftrace_lock */
6746 clear_mod_from_hashes(pg);
6749 free_pages((unsigned long)pg->records, pg->order);
6750 ftrace_number_of_pages -= 1 << pg->order;
6752 tmp_page = pg->next;
6754 ftrace_number_of_groups--;
6758 void ftrace_module_enable(struct module *mod)
6760 struct dyn_ftrace *rec;
6761 struct ftrace_page *pg;
6763 mutex_lock(&ftrace_lock);
6765 if (ftrace_disabled)
6769 * If the tracing is enabled, go ahead and enable the record.
6771 * The reason not to enable the record immediately is the
6772 * inherent check of ftrace_make_nop/ftrace_make_call for
6773 * correct previous instructions. Making first the NOP
6774 * conversion puts the module to the correct state, thus
6775 * passing the ftrace_make_call check.
6777 * We also delay this to after the module code already set the
6778 * text to read-only, as we now need to set it back to read-write
6779 * so that we can modify the text.
6781 if (ftrace_start_up)
6782 ftrace_arch_code_modify_prepare();
6784 do_for_each_ftrace_rec(pg, rec) {
6787 * do_for_each_ftrace_rec() is a double loop.
6788 * module text shares the pg. If a record is
6789 * not part of this module, then skip this pg,
6790 * which the "break" will do.
6792 if (!within_module_core(rec->ip, mod) &&
6793 !within_module_init(rec->ip, mod))
6799 * When adding a module, we need to check if tracers are
6800 * currently enabled and if they are, and can trace this record,
6801 * we need to enable the module functions as well as update the
6802 * reference counts for those function records.
6804 if (ftrace_start_up)
6805 cnt += referenced_filters(rec);
6807 rec->flags &= ~FTRACE_FL_DISABLED;
6810 if (ftrace_start_up && cnt) {
6811 int failed = __ftrace_replace_code(rec, 1);
6813 ftrace_bug(failed, rec);
6818 } while_for_each_ftrace_rec();
6821 if (ftrace_start_up)
6822 ftrace_arch_code_modify_post_process();
6825 mutex_unlock(&ftrace_lock);
6827 process_cached_mods(mod->name);
6830 void ftrace_module_init(struct module *mod)
6832 if (ftrace_disabled || !mod->num_ftrace_callsites)
6835 ftrace_process_locs(mod, mod->ftrace_callsites,
6836 mod->ftrace_callsites + mod->num_ftrace_callsites);
6839 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6840 struct dyn_ftrace *rec)
6842 struct ftrace_mod_func *mod_func;
6843 unsigned long symsize;
6844 unsigned long offset;
6845 char str[KSYM_SYMBOL_LEN];
6849 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
6853 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
6857 mod_func->name = kstrdup(str, GFP_KERNEL);
6858 if (!mod_func->name) {
6863 mod_func->ip = rec->ip - offset;
6864 mod_func->size = symsize;
6866 mod_map->num_funcs++;
6868 list_add_rcu(&mod_func->list, &mod_map->funcs);
6871 static struct ftrace_mod_map *
6872 allocate_ftrace_mod_map(struct module *mod,
6873 unsigned long start, unsigned long end)
6875 struct ftrace_mod_map *mod_map;
6877 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
6882 mod_map->start_addr = start;
6883 mod_map->end_addr = end;
6884 mod_map->num_funcs = 0;
6886 INIT_LIST_HEAD_RCU(&mod_map->funcs);
6888 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
6894 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
6895 unsigned long addr, unsigned long *size,
6896 unsigned long *off, char *sym)
6898 struct ftrace_mod_func *found_func = NULL;
6899 struct ftrace_mod_func *mod_func;
6901 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6902 if (addr >= mod_func->ip &&
6903 addr < mod_func->ip + mod_func->size) {
6904 found_func = mod_func;
6911 *size = found_func->size;
6913 *off = addr - found_func->ip;
6915 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
6917 return found_func->name;
6924 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6925 unsigned long *off, char **modname, char *sym)
6927 struct ftrace_mod_map *mod_map;
6928 const char *ret = NULL;
6930 /* mod_map is freed via call_rcu() */
6932 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6933 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6936 *modname = mod_map->mod->name;
6945 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6946 char *type, char *name,
6947 char *module_name, int *exported)
6949 struct ftrace_mod_map *mod_map;
6950 struct ftrace_mod_func *mod_func;
6954 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6956 if (symnum >= mod_map->num_funcs) {
6957 symnum -= mod_map->num_funcs;
6961 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6967 *value = mod_func->ip;
6969 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6970 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6978 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6979 module_name, exported);
6985 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6986 struct dyn_ftrace *rec) { }
6987 static inline struct ftrace_mod_map *
6988 allocate_ftrace_mod_map(struct module *mod,
6989 unsigned long start, unsigned long end)
6993 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6994 char *type, char *name, char *module_name,
7000 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
7001 module_name, exported);
7005 #endif /* CONFIG_MODULES */
7007 struct ftrace_init_func {
7008 struct list_head list;
7012 /* Clear any init ips from hashes */
7014 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
7016 struct ftrace_func_entry *entry;
7018 entry = ftrace_lookup_ip(hash, func->ip);
7020 * Do not allow this rec to match again.
7021 * Yeah, it may waste some memory, but will be removed
7022 * if/when the hash is modified again.
7029 clear_func_from_hashes(struct ftrace_init_func *func)
7031 struct trace_array *tr;
7033 mutex_lock(&trace_types_lock);
7034 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7035 if (!tr->ops || !tr->ops->func_hash)
7037 mutex_lock(&tr->ops->func_hash->regex_lock);
7038 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
7039 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
7040 mutex_unlock(&tr->ops->func_hash->regex_lock);
7042 mutex_unlock(&trace_types_lock);
7045 static void add_to_clear_hash_list(struct list_head *clear_list,
7046 struct dyn_ftrace *rec)
7048 struct ftrace_init_func *func;
7050 func = kmalloc(sizeof(*func), GFP_KERNEL);
7052 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
7057 list_add(&func->list, clear_list);
7060 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
7062 unsigned long start = (unsigned long)(start_ptr);
7063 unsigned long end = (unsigned long)(end_ptr);
7064 struct ftrace_page **last_pg = &ftrace_pages_start;
7065 struct ftrace_page *pg;
7066 struct dyn_ftrace *rec;
7067 struct dyn_ftrace key;
7068 struct ftrace_mod_map *mod_map = NULL;
7069 struct ftrace_init_func *func, *func_next;
7070 struct list_head clear_hash;
7072 INIT_LIST_HEAD(&clear_hash);
7075 key.flags = end; /* overload flags, as it is unsigned long */
7077 mutex_lock(&ftrace_lock);
7080 * If we are freeing module init memory, then check if
7081 * any tracer is active. If so, we need to save a mapping of
7082 * the module functions being freed with the address.
7084 if (mod && ftrace_ops_list != &ftrace_list_end)
7085 mod_map = allocate_ftrace_mod_map(mod, start, end);
7087 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
7088 if (end < pg->records[0].ip ||
7089 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
7092 rec = bsearch(&key, pg->records, pg->index,
7093 sizeof(struct dyn_ftrace),
7098 /* rec will be cleared from hashes after ftrace_lock unlock */
7099 add_to_clear_hash_list(&clear_hash, rec);
7102 save_ftrace_mod_rec(mod_map, rec);
7105 ftrace_update_tot_cnt--;
7107 *last_pg = pg->next;
7109 free_pages((unsigned long)pg->records, pg->order);
7110 ftrace_number_of_pages -= 1 << pg->order;
7112 ftrace_number_of_groups--;
7114 pg = container_of(last_pg, struct ftrace_page, next);
7119 memmove(rec, rec + 1,
7120 (pg->index - (rec - pg->records)) * sizeof(*rec));
7121 /* More than one function may be in this block */
7124 mutex_unlock(&ftrace_lock);
7126 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
7127 clear_func_from_hashes(func);
7132 void __init ftrace_free_init_mem(void)
7134 void *start = (void *)(&__init_begin);
7135 void *end = (void *)(&__init_end);
7137 ftrace_boot_snapshot();
7139 ftrace_free_mem(NULL, start, end);
7142 int __init __weak ftrace_dyn_arch_init(void)
7147 void __init ftrace_init(void)
7149 extern unsigned long __start_mcount_loc[];
7150 extern unsigned long __stop_mcount_loc[];
7151 unsigned long count, flags;
7154 local_irq_save(flags);
7155 ret = ftrace_dyn_arch_init();
7156 local_irq_restore(flags);
7160 count = __stop_mcount_loc - __start_mcount_loc;
7162 pr_info("ftrace: No functions to be traced?\n");
7166 pr_info("ftrace: allocating %ld entries in %ld pages\n",
7167 count, count / ENTRIES_PER_PAGE + 1);
7169 last_ftrace_enabled = ftrace_enabled = 1;
7171 ret = ftrace_process_locs(NULL,
7175 pr_info("ftrace: allocated %ld pages with %ld groups\n",
7176 ftrace_number_of_pages, ftrace_number_of_groups);
7178 set_ftrace_early_filters();
7182 ftrace_disabled = 1;
7185 /* Do nothing if arch does not support this */
7186 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
7190 static void ftrace_update_trampoline(struct ftrace_ops *ops)
7192 unsigned long trampoline = ops->trampoline;
7194 arch_ftrace_update_trampoline(ops);
7195 if (ops->trampoline && ops->trampoline != trampoline &&
7196 (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) {
7197 /* Add to kallsyms before the perf events */
7198 ftrace_add_trampoline_to_kallsyms(ops);
7199 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
7200 ops->trampoline, ops->trampoline_size, false,
7201 FTRACE_TRAMPOLINE_SYM);
7203 * Record the perf text poke event after the ksymbol register
7206 perf_event_text_poke((void *)ops->trampoline, NULL, 0,
7207 (void *)ops->trampoline,
7208 ops->trampoline_size);
7212 void ftrace_init_trace_array(struct trace_array *tr)
7214 INIT_LIST_HEAD(&tr->func_probes);
7215 INIT_LIST_HEAD(&tr->mod_trace);
7216 INIT_LIST_HEAD(&tr->mod_notrace);
7220 struct ftrace_ops global_ops = {
7221 .func = ftrace_stub,
7222 .flags = FTRACE_OPS_FL_INITIALIZED |
7226 static int __init ftrace_nodyn_init(void)
7231 core_initcall(ftrace_nodyn_init);
7233 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
7234 static inline void ftrace_startup_all(int command) { }
7236 static void ftrace_update_trampoline(struct ftrace_ops *ops)
7240 #endif /* CONFIG_DYNAMIC_FTRACE */
7242 __init void ftrace_init_global_array_ops(struct trace_array *tr)
7244 tr->ops = &global_ops;
7245 tr->ops->private = tr;
7246 ftrace_init_trace_array(tr);
7249 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
7251 /* If we filter on pids, update to use the pid function */
7252 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
7253 if (WARN_ON(tr->ops->func != ftrace_stub))
7254 printk("ftrace ops had %pS for function\n",
7257 tr->ops->func = func;
7258 tr->ops->private = tr;
7261 void ftrace_reset_array_ops(struct trace_array *tr)
7263 tr->ops->func = ftrace_stub;
7266 static nokprobe_inline void
7267 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
7268 struct ftrace_ops *ignored, struct ftrace_regs *fregs)
7270 struct pt_regs *regs = ftrace_get_regs(fregs);
7271 struct ftrace_ops *op;
7275 * The ftrace_test_and_set_recursion() will disable preemption,
7276 * which is required since some of the ops may be dynamically
7277 * allocated, they must be freed after a synchronize_rcu().
7279 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
7283 do_for_each_ftrace_op(op, ftrace_ops_list) {
7284 /* Stub functions don't need to be called nor tested */
7285 if (op->flags & FTRACE_OPS_FL_STUB)
7288 * Check the following for each ops before calling their func:
7289 * if RCU flag is set, then rcu_is_watching() must be true
7290 * if PER_CPU is set, then ftrace_function_local_disable()
7292 * Otherwise test if the ip matches the ops filter
7294 * If any of the above fails then the op->func() is not executed.
7296 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
7297 ftrace_ops_test(op, ip, regs)) {
7298 if (FTRACE_WARN_ON(!op->func)) {
7299 pr_warn("op=%p %pS\n", op, op);
7302 op->func(ip, parent_ip, op, fregs);
7304 } while_for_each_ftrace_op(op);
7306 trace_clear_recursion(bit);
7310 * Some archs only support passing ip and parent_ip. Even though
7311 * the list function ignores the op parameter, we do not want any
7312 * C side effects, where a function is called without the caller
7313 * sending a third parameter.
7314 * Archs are to support both the regs and ftrace_ops at the same time.
7315 * If they support ftrace_ops, it is assumed they support regs.
7316 * If call backs want to use regs, they must either check for regs
7317 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
7318 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
7319 * An architecture can pass partial regs with ftrace_ops and still
7320 * set the ARCH_SUPPORTS_FTRACE_OPS.
7322 * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be
7323 * arch_ftrace_ops_list_func.
7325 #if ARCH_SUPPORTS_FTRACE_OPS
7326 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
7327 struct ftrace_ops *op, struct ftrace_regs *fregs)
7329 __ftrace_ops_list_func(ip, parent_ip, NULL, fregs);
7332 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
7334 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
7337 NOKPROBE_SYMBOL(arch_ftrace_ops_list_func);
7340 * If there's only one function registered but it does not support
7341 * recursion, needs RCU protection and/or requires per cpu handling, then
7342 * this function will be called by the mcount trampoline.
7344 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
7345 struct ftrace_ops *op, struct ftrace_regs *fregs)
7349 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
7353 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
7354 op->func(ip, parent_ip, op, fregs);
7356 trace_clear_recursion(bit);
7358 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
7361 * ftrace_ops_get_func - get the function a trampoline should call
7362 * @ops: the ops to get the function for
7364 * Normally the mcount trampoline will call the ops->func, but there
7365 * are times that it should not. For example, if the ops does not
7366 * have its own recursion protection, then it should call the
7367 * ftrace_ops_assist_func() instead.
7369 * Returns the function that the trampoline should call for @ops.
7371 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
7374 * If the function does not handle recursion or needs to be RCU safe,
7375 * then we need to call the assist handler.
7377 if (ops->flags & (FTRACE_OPS_FL_RECURSION |
7379 return ftrace_ops_assist_func;
7385 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
7386 unsigned int prev_state,
7387 struct task_struct *prev,
7388 struct task_struct *next)
7390 struct trace_array *tr = data;
7391 struct trace_pid_list *pid_list;
7392 struct trace_pid_list *no_pid_list;
7394 pid_list = rcu_dereference_sched(tr->function_pids);
7395 no_pid_list = rcu_dereference_sched(tr->function_no_pids);
7397 if (trace_ignore_this_task(pid_list, no_pid_list, next))
7398 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7401 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7406 ftrace_pid_follow_sched_process_fork(void *data,
7407 struct task_struct *self,
7408 struct task_struct *task)
7410 struct trace_pid_list *pid_list;
7411 struct trace_array *tr = data;
7413 pid_list = rcu_dereference_sched(tr->function_pids);
7414 trace_filter_add_remove_task(pid_list, self, task);
7416 pid_list = rcu_dereference_sched(tr->function_no_pids);
7417 trace_filter_add_remove_task(pid_list, self, task);
7421 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
7423 struct trace_pid_list *pid_list;
7424 struct trace_array *tr = data;
7426 pid_list = rcu_dereference_sched(tr->function_pids);
7427 trace_filter_add_remove_task(pid_list, NULL, task);
7429 pid_list = rcu_dereference_sched(tr->function_no_pids);
7430 trace_filter_add_remove_task(pid_list, NULL, task);
7433 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
7436 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7438 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
7441 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7443 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
7448 static void clear_ftrace_pids(struct trace_array *tr, int type)
7450 struct trace_pid_list *pid_list;
7451 struct trace_pid_list *no_pid_list;
7454 pid_list = rcu_dereference_protected(tr->function_pids,
7455 lockdep_is_held(&ftrace_lock));
7456 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7457 lockdep_is_held(&ftrace_lock));
7459 /* Make sure there's something to do */
7460 if (!pid_type_enabled(type, pid_list, no_pid_list))
7463 /* See if the pids still need to be checked after this */
7464 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
7465 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7466 for_each_possible_cpu(cpu)
7467 per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE;
7470 if (type & TRACE_PIDS)
7471 rcu_assign_pointer(tr->function_pids, NULL);
7473 if (type & TRACE_NO_PIDS)
7474 rcu_assign_pointer(tr->function_no_pids, NULL);
7476 /* Wait till all users are no longer using pid filtering */
7479 if ((type & TRACE_PIDS) && pid_list)
7480 trace_pid_list_free(pid_list);
7482 if ((type & TRACE_NO_PIDS) && no_pid_list)
7483 trace_pid_list_free(no_pid_list);
7486 void ftrace_clear_pids(struct trace_array *tr)
7488 mutex_lock(&ftrace_lock);
7490 clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
7492 mutex_unlock(&ftrace_lock);
7495 static void ftrace_pid_reset(struct trace_array *tr, int type)
7497 mutex_lock(&ftrace_lock);
7498 clear_ftrace_pids(tr, type);
7500 ftrace_update_pid_func();
7501 ftrace_startup_all(0);
7503 mutex_unlock(&ftrace_lock);
7506 /* Greater than any max PID */
7507 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
7509 static void *fpid_start(struct seq_file *m, loff_t *pos)
7512 struct trace_pid_list *pid_list;
7513 struct trace_array *tr = m->private;
7515 mutex_lock(&ftrace_lock);
7516 rcu_read_lock_sched();
7518 pid_list = rcu_dereference_sched(tr->function_pids);
7521 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7523 return trace_pid_start(pid_list, pos);
7526 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
7528 struct trace_array *tr = m->private;
7529 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
7531 if (v == FTRACE_NO_PIDS) {
7535 return trace_pid_next(pid_list, v, pos);
7538 static void fpid_stop(struct seq_file *m, void *p)
7541 rcu_read_unlock_sched();
7542 mutex_unlock(&ftrace_lock);
7545 static int fpid_show(struct seq_file *m, void *v)
7547 if (v == FTRACE_NO_PIDS) {
7548 seq_puts(m, "no pid\n");
7552 return trace_pid_show(m, v);
7555 static const struct seq_operations ftrace_pid_sops = {
7556 .start = fpid_start,
7562 static void *fnpid_start(struct seq_file *m, loff_t *pos)
7565 struct trace_pid_list *pid_list;
7566 struct trace_array *tr = m->private;
7568 mutex_lock(&ftrace_lock);
7569 rcu_read_lock_sched();
7571 pid_list = rcu_dereference_sched(tr->function_no_pids);
7574 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7576 return trace_pid_start(pid_list, pos);
7579 static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos)
7581 struct trace_array *tr = m->private;
7582 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids);
7584 if (v == FTRACE_NO_PIDS) {
7588 return trace_pid_next(pid_list, v, pos);
7591 static const struct seq_operations ftrace_no_pid_sops = {
7592 .start = fnpid_start,
7598 static int pid_open(struct inode *inode, struct file *file, int type)
7600 const struct seq_operations *seq_ops;
7601 struct trace_array *tr = inode->i_private;
7605 ret = tracing_check_open_get_tr(tr);
7609 if ((file->f_mode & FMODE_WRITE) &&
7610 (file->f_flags & O_TRUNC))
7611 ftrace_pid_reset(tr, type);
7615 seq_ops = &ftrace_pid_sops;
7618 seq_ops = &ftrace_no_pid_sops;
7621 trace_array_put(tr);
7626 ret = seq_open(file, seq_ops);
7628 trace_array_put(tr);
7630 m = file->private_data;
7631 /* copy tr over to seq ops */
7639 ftrace_pid_open(struct inode *inode, struct file *file)
7641 return pid_open(inode, file, TRACE_PIDS);
7645 ftrace_no_pid_open(struct inode *inode, struct file *file)
7647 return pid_open(inode, file, TRACE_NO_PIDS);
7650 static void ignore_task_cpu(void *data)
7652 struct trace_array *tr = data;
7653 struct trace_pid_list *pid_list;
7654 struct trace_pid_list *no_pid_list;
7657 * This function is called by on_each_cpu() while the
7658 * event_mutex is held.
7660 pid_list = rcu_dereference_protected(tr->function_pids,
7661 mutex_is_locked(&ftrace_lock));
7662 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7663 mutex_is_locked(&ftrace_lock));
7665 if (trace_ignore_this_task(pid_list, no_pid_list, current))
7666 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7669 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7674 pid_write(struct file *filp, const char __user *ubuf,
7675 size_t cnt, loff_t *ppos, int type)
7677 struct seq_file *m = filp->private_data;
7678 struct trace_array *tr = m->private;
7679 struct trace_pid_list *filtered_pids;
7680 struct trace_pid_list *other_pids;
7681 struct trace_pid_list *pid_list;
7687 mutex_lock(&ftrace_lock);
7691 filtered_pids = rcu_dereference_protected(tr->function_pids,
7692 lockdep_is_held(&ftrace_lock));
7693 other_pids = rcu_dereference_protected(tr->function_no_pids,
7694 lockdep_is_held(&ftrace_lock));
7697 filtered_pids = rcu_dereference_protected(tr->function_no_pids,
7698 lockdep_is_held(&ftrace_lock));
7699 other_pids = rcu_dereference_protected(tr->function_pids,
7700 lockdep_is_held(&ftrace_lock));
7708 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
7714 rcu_assign_pointer(tr->function_pids, pid_list);
7717 rcu_assign_pointer(tr->function_no_pids, pid_list);
7722 if (filtered_pids) {
7724 trace_pid_list_free(filtered_pids);
7725 } else if (pid_list && !other_pids) {
7726 /* Register a probe to set whether to ignore the tracing of a task */
7727 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7731 * Ignoring of pids is done at task switch. But we have to
7732 * check for those tasks that are currently running.
7733 * Always do this in case a pid was appended or removed.
7735 on_each_cpu(ignore_task_cpu, tr, 1);
7737 ftrace_update_pid_func();
7738 ftrace_startup_all(0);
7740 mutex_unlock(&ftrace_lock);
7749 ftrace_pid_write(struct file *filp, const char __user *ubuf,
7750 size_t cnt, loff_t *ppos)
7752 return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
7756 ftrace_no_pid_write(struct file *filp, const char __user *ubuf,
7757 size_t cnt, loff_t *ppos)
7759 return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
7763 ftrace_pid_release(struct inode *inode, struct file *file)
7765 struct trace_array *tr = inode->i_private;
7767 trace_array_put(tr);
7769 return seq_release(inode, file);
7772 static const struct file_operations ftrace_pid_fops = {
7773 .open = ftrace_pid_open,
7774 .write = ftrace_pid_write,
7776 .llseek = tracing_lseek,
7777 .release = ftrace_pid_release,
7780 static const struct file_operations ftrace_no_pid_fops = {
7781 .open = ftrace_no_pid_open,
7782 .write = ftrace_no_pid_write,
7784 .llseek = tracing_lseek,
7785 .release = ftrace_pid_release,
7788 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
7790 trace_create_file("set_ftrace_pid", TRACE_MODE_WRITE, d_tracer,
7791 tr, &ftrace_pid_fops);
7792 trace_create_file("set_ftrace_notrace_pid", TRACE_MODE_WRITE,
7793 d_tracer, tr, &ftrace_no_pid_fops);
7796 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
7797 struct dentry *d_tracer)
7799 /* Only the top level directory has the dyn_tracefs and profile */
7800 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
7802 ftrace_init_dyn_tracefs(d_tracer);
7803 ftrace_profile_tracefs(d_tracer);
7807 * ftrace_kill - kill ftrace
7809 * This function should be used by panic code. It stops ftrace
7810 * but in a not so nice way. If you need to simply kill ftrace
7811 * from a non-atomic section, use ftrace_kill.
7813 void ftrace_kill(void)
7815 ftrace_disabled = 1;
7817 ftrace_trace_function = ftrace_stub;
7821 * ftrace_is_dead - Test if ftrace is dead or not.
7823 * Returns 1 if ftrace is "dead", zero otherwise.
7825 int ftrace_is_dead(void)
7827 return ftrace_disabled;
7831 * register_ftrace_function - register a function for profiling
7832 * @ops: ops structure that holds the function for profiling.
7834 * Register a function to be called by all functions in the
7837 * Note: @ops->func and all the functions it calls must be labeled
7838 * with "notrace", otherwise it will go into a
7841 int register_ftrace_function(struct ftrace_ops *ops)
7845 ftrace_ops_init(ops);
7847 mutex_lock(&ftrace_lock);
7849 ret = ftrace_startup(ops, 0);
7851 mutex_unlock(&ftrace_lock);
7855 EXPORT_SYMBOL_GPL(register_ftrace_function);
7858 * unregister_ftrace_function - unregister a function for profiling.
7859 * @ops: ops structure that holds the function to unregister
7861 * Unregister a function that was added to be called by ftrace profiling.
7863 int unregister_ftrace_function(struct ftrace_ops *ops)
7867 mutex_lock(&ftrace_lock);
7868 ret = ftrace_shutdown(ops, 0);
7869 mutex_unlock(&ftrace_lock);
7873 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
7875 #ifdef CONFIG_SYSCTL
7877 #ifdef CONFIG_DYNAMIC_FTRACE
7878 static void ftrace_startup_sysctl(void)
7882 if (unlikely(ftrace_disabled))
7885 /* Force update next time */
7886 saved_ftrace_func = NULL;
7887 /* ftrace_start_up is true if we want ftrace running */
7888 if (ftrace_start_up) {
7889 command = FTRACE_UPDATE_CALLS;
7890 if (ftrace_graph_active)
7891 command |= FTRACE_START_FUNC_RET;
7892 ftrace_startup_enable(command);
7896 static void ftrace_shutdown_sysctl(void)
7900 if (unlikely(ftrace_disabled))
7903 /* ftrace_start_up is true if ftrace is running */
7904 if (ftrace_start_up) {
7905 command = FTRACE_DISABLE_CALLS;
7906 if (ftrace_graph_active)
7907 command |= FTRACE_STOP_FUNC_RET;
7908 ftrace_run_update_code(command);
7912 # define ftrace_startup_sysctl() do { } while (0)
7913 # define ftrace_shutdown_sysctl() do { } while (0)
7914 #endif /* CONFIG_DYNAMIC_FTRACE */
7916 static bool is_permanent_ops_registered(void)
7918 struct ftrace_ops *op;
7920 do_for_each_ftrace_op(op, ftrace_ops_list) {
7921 if (op->flags & FTRACE_OPS_FL_PERMANENT)
7923 } while_for_each_ftrace_op(op);
7929 ftrace_enable_sysctl(struct ctl_table *table, int write,
7930 void *buffer, size_t *lenp, loff_t *ppos)
7934 mutex_lock(&ftrace_lock);
7936 if (unlikely(ftrace_disabled))
7939 ret = proc_dointvec(table, write, buffer, lenp, ppos);
7941 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
7944 if (ftrace_enabled) {
7946 /* we are starting ftrace again */
7947 if (rcu_dereference_protected(ftrace_ops_list,
7948 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
7949 update_ftrace_function();
7951 ftrace_startup_sysctl();
7954 if (is_permanent_ops_registered()) {
7955 ftrace_enabled = true;
7960 /* stopping ftrace calls (just send to ftrace_stub) */
7961 ftrace_trace_function = ftrace_stub;
7963 ftrace_shutdown_sysctl();
7966 last_ftrace_enabled = !!ftrace_enabled;
7968 mutex_unlock(&ftrace_lock);
7972 static struct ctl_table ftrace_sysctls[] = {
7974 .procname = "ftrace_enabled",
7975 .data = &ftrace_enabled,
7976 .maxlen = sizeof(int),
7978 .proc_handler = ftrace_enable_sysctl,
7983 static int __init ftrace_sysctl_init(void)
7985 register_sysctl_init("kernel", ftrace_sysctls);
7988 late_initcall(ftrace_sysctl_init);