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 /* Flags that do not get reset */
49 #define FTRACE_NOCLEAR_FLAGS (FTRACE_FL_DISABLED | FTRACE_FL_TOUCHED)
51 #define FTRACE_INVALID_FUNCTION "__ftrace_invalid_address__"
53 #define FTRACE_WARN_ON(cond) \
61 #define FTRACE_WARN_ON_ONCE(cond) \
64 if (WARN_ON_ONCE(___r)) \
69 /* hash bits for specific function selection */
70 #define FTRACE_HASH_DEFAULT_BITS 10
71 #define FTRACE_HASH_MAX_BITS 12
73 #ifdef CONFIG_DYNAMIC_FTRACE
74 #define INIT_OPS_HASH(opsname) \
75 .func_hash = &opsname.local_hash, \
76 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
78 #define INIT_OPS_HASH(opsname)
82 FTRACE_MODIFY_ENABLE_FL = (1 << 0),
83 FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
86 struct ftrace_ops ftrace_list_end __read_mostly = {
88 .flags = FTRACE_OPS_FL_STUB,
89 INIT_OPS_HASH(ftrace_list_end)
92 /* ftrace_enabled is a method to turn ftrace on or off */
93 int ftrace_enabled __read_mostly;
94 static int __maybe_unused last_ftrace_enabled;
96 /* Current function tracing op */
97 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
98 /* What to set function_trace_op to */
99 static struct ftrace_ops *set_function_trace_op;
101 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
103 struct trace_array *tr;
105 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
110 return tr->function_pids != NULL || tr->function_no_pids != NULL;
113 static void ftrace_update_trampoline(struct ftrace_ops *ops);
116 * ftrace_disabled is set when an anomaly is discovered.
117 * ftrace_disabled is much stronger than ftrace_enabled.
119 static int ftrace_disabled __read_mostly;
121 DEFINE_MUTEX(ftrace_lock);
123 struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
124 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
125 struct ftrace_ops global_ops;
127 /* Defined by vmlinux.lds.h see the comment above arch_ftrace_ops_list_func for details */
128 void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
129 struct ftrace_ops *op, struct ftrace_regs *fregs);
131 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
133 * Stub used to invoke the list ops without requiring a separate trampoline.
135 const struct ftrace_ops ftrace_list_ops = {
136 .func = ftrace_ops_list_func,
137 .flags = FTRACE_OPS_FL_STUB,
140 static void ftrace_ops_nop_func(unsigned long ip, unsigned long parent_ip,
141 struct ftrace_ops *op,
142 struct ftrace_regs *fregs)
148 * Stub used when a call site is disabled. May be called transiently by threads
149 * which have made it into ftrace_caller but haven't yet recovered the ops at
150 * the point the call site is disabled.
152 const struct ftrace_ops ftrace_nop_ops = {
153 .func = ftrace_ops_nop_func,
154 .flags = FTRACE_OPS_FL_STUB,
158 static inline void ftrace_ops_init(struct ftrace_ops *ops)
160 #ifdef CONFIG_DYNAMIC_FTRACE
161 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
162 mutex_init(&ops->local_hash.regex_lock);
163 ops->func_hash = &ops->local_hash;
164 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
169 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
170 struct ftrace_ops *op, struct ftrace_regs *fregs)
172 struct trace_array *tr = op->private;
176 pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid);
177 if (pid == FTRACE_PID_IGNORE)
179 if (pid != FTRACE_PID_TRACE &&
184 op->saved_func(ip, parent_ip, op, fregs);
187 static void ftrace_sync_ipi(void *data)
189 /* Probably not needed, but do it anyway */
193 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
196 * If this is a dynamic or RCU ops, or we force list func,
197 * then it needs to call the list anyway.
199 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
200 FTRACE_FORCE_LIST_FUNC)
201 return ftrace_ops_list_func;
203 return ftrace_ops_get_func(ops);
206 static void update_ftrace_function(void)
211 * Prepare the ftrace_ops that the arch callback will use.
212 * If there's only one ftrace_ops registered, the ftrace_ops_list
213 * will point to the ops we want.
215 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
216 lockdep_is_held(&ftrace_lock));
218 /* If there's no ftrace_ops registered, just call the stub function */
219 if (set_function_trace_op == &ftrace_list_end) {
223 * If we are at the end of the list and this ops is
224 * recursion safe and not dynamic and the arch supports passing ops,
225 * then have the mcount trampoline call the function directly.
227 } else if (rcu_dereference_protected(ftrace_ops_list->next,
228 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
229 func = ftrace_ops_get_list_func(ftrace_ops_list);
232 /* Just use the default ftrace_ops */
233 set_function_trace_op = &ftrace_list_end;
234 func = ftrace_ops_list_func;
237 update_function_graph_func();
239 /* If there's no change, then do nothing more here */
240 if (ftrace_trace_function == func)
244 * If we are using the list function, it doesn't care
245 * about the function_trace_ops.
247 if (func == ftrace_ops_list_func) {
248 ftrace_trace_function = func;
250 * Don't even bother setting function_trace_ops,
251 * it would be racy to do so anyway.
256 #ifndef CONFIG_DYNAMIC_FTRACE
258 * For static tracing, we need to be a bit more careful.
259 * The function change takes affect immediately. Thus,
260 * we need to coordinate the setting of the function_trace_ops
261 * with the setting of the ftrace_trace_function.
263 * Set the function to the list ops, which will call the
264 * function we want, albeit indirectly, but it handles the
265 * ftrace_ops and doesn't depend on function_trace_op.
267 ftrace_trace_function = ftrace_ops_list_func;
269 * Make sure all CPUs see this. Yes this is slow, but static
270 * tracing is slow and nasty to have enabled.
272 synchronize_rcu_tasks_rude();
273 /* Now all cpus are using the list ops. */
274 function_trace_op = set_function_trace_op;
275 /* Make sure the function_trace_op is visible on all CPUs */
277 /* Nasty way to force a rmb on all cpus */
278 smp_call_function(ftrace_sync_ipi, NULL, 1);
279 /* OK, we are all set to update the ftrace_trace_function now! */
280 #endif /* !CONFIG_DYNAMIC_FTRACE */
282 ftrace_trace_function = func;
285 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
286 struct ftrace_ops *ops)
288 rcu_assign_pointer(ops->next, *list);
291 * We are entering ops into the list but another
292 * CPU might be walking that list. We need to make sure
293 * the ops->next pointer is valid before another CPU sees
294 * the ops pointer included into the list.
296 rcu_assign_pointer(*list, ops);
299 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
300 struct ftrace_ops *ops)
302 struct ftrace_ops **p;
305 * If we are removing the last function, then simply point
306 * to the ftrace_stub.
308 if (rcu_dereference_protected(*list,
309 lockdep_is_held(&ftrace_lock)) == ops &&
310 rcu_dereference_protected(ops->next,
311 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
312 *list = &ftrace_list_end;
316 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
327 static void ftrace_update_trampoline(struct ftrace_ops *ops);
329 int __register_ftrace_function(struct ftrace_ops *ops)
331 if (ops->flags & FTRACE_OPS_FL_DELETED)
334 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
337 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
339 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
340 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
341 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
343 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
344 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
347 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
348 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
350 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
353 if (!is_kernel_core_data((unsigned long)ops))
354 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
356 add_ftrace_ops(&ftrace_ops_list, ops);
358 /* Always save the function, and reset at unregistering */
359 ops->saved_func = ops->func;
361 if (ftrace_pids_enabled(ops))
362 ops->func = ftrace_pid_func;
364 ftrace_update_trampoline(ops);
367 update_ftrace_function();
372 int __unregister_ftrace_function(struct ftrace_ops *ops)
376 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
379 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
385 update_ftrace_function();
387 ops->func = ops->saved_func;
392 static void ftrace_update_pid_func(void)
394 struct ftrace_ops *op;
396 /* Only do something if we are tracing something */
397 if (ftrace_trace_function == ftrace_stub)
400 do_for_each_ftrace_op(op, ftrace_ops_list) {
401 if (op->flags & FTRACE_OPS_FL_PID) {
402 op->func = ftrace_pids_enabled(op) ?
403 ftrace_pid_func : op->saved_func;
404 ftrace_update_trampoline(op);
406 } while_for_each_ftrace_op(op);
408 update_ftrace_function();
411 #ifdef CONFIG_FUNCTION_PROFILER
412 struct ftrace_profile {
413 struct hlist_node node;
415 unsigned long counter;
416 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
417 unsigned long long time;
418 unsigned long long time_squared;
422 struct ftrace_profile_page {
423 struct ftrace_profile_page *next;
425 struct ftrace_profile records[];
428 struct ftrace_profile_stat {
430 struct hlist_head *hash;
431 struct ftrace_profile_page *pages;
432 struct ftrace_profile_page *start;
433 struct tracer_stat stat;
436 #define PROFILE_RECORDS_SIZE \
437 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
439 #define PROFILES_PER_PAGE \
440 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
442 static int ftrace_profile_enabled __read_mostly;
444 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
445 static DEFINE_MUTEX(ftrace_profile_lock);
447 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
449 #define FTRACE_PROFILE_HASH_BITS 10
450 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
453 function_stat_next(void *v, int idx)
455 struct ftrace_profile *rec = v;
456 struct ftrace_profile_page *pg;
458 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
464 if ((void *)rec >= (void *)&pg->records[pg->index]) {
468 rec = &pg->records[0];
476 static void *function_stat_start(struct tracer_stat *trace)
478 struct ftrace_profile_stat *stat =
479 container_of(trace, struct ftrace_profile_stat, stat);
481 if (!stat || !stat->start)
484 return function_stat_next(&stat->start->records[0], 0);
487 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
488 /* function graph compares on total time */
489 static int function_stat_cmp(const void *p1, const void *p2)
491 const struct ftrace_profile *a = p1;
492 const struct ftrace_profile *b = p2;
494 if (a->time < b->time)
496 if (a->time > b->time)
502 /* not function graph compares against hits */
503 static int function_stat_cmp(const void *p1, const void *p2)
505 const struct ftrace_profile *a = p1;
506 const struct ftrace_profile *b = p2;
508 if (a->counter < b->counter)
510 if (a->counter > b->counter)
517 static int function_stat_headers(struct seq_file *m)
519 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
520 seq_puts(m, " Function "
523 "--- ---- --- ---\n");
525 seq_puts(m, " Function Hit\n"
531 static int function_stat_show(struct seq_file *m, void *v)
533 struct ftrace_profile *rec = v;
534 char str[KSYM_SYMBOL_LEN];
536 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
537 static struct trace_seq s;
538 unsigned long long avg;
539 unsigned long long stddev;
541 mutex_lock(&ftrace_profile_lock);
543 /* we raced with function_profile_reset() */
544 if (unlikely(rec->counter == 0)) {
549 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
550 avg = div64_ul(rec->time, rec->counter);
551 if (tracing_thresh && (avg < tracing_thresh))
555 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
556 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
558 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
561 /* Sample standard deviation (s^2) */
562 if (rec->counter <= 1)
566 * Apply Welford's method:
567 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
569 stddev = rec->counter * rec->time_squared -
570 rec->time * rec->time;
573 * Divide only 1000 for ns^2 -> us^2 conversion.
574 * trace_print_graph_duration will divide 1000 again.
576 stddev = div64_ul(stddev,
577 rec->counter * (rec->counter - 1) * 1000);
581 trace_print_graph_duration(rec->time, &s);
582 trace_seq_puts(&s, " ");
583 trace_print_graph_duration(avg, &s);
584 trace_seq_puts(&s, " ");
585 trace_print_graph_duration(stddev, &s);
586 trace_print_seq(m, &s);
590 mutex_unlock(&ftrace_profile_lock);
595 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
597 struct ftrace_profile_page *pg;
599 pg = stat->pages = stat->start;
602 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
607 memset(stat->hash, 0,
608 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
611 static int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
613 struct ftrace_profile_page *pg;
618 /* If we already allocated, do nothing */
622 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
626 #ifdef CONFIG_DYNAMIC_FTRACE
627 functions = ftrace_update_tot_cnt;
630 * We do not know the number of functions that exist because
631 * dynamic tracing is what counts them. With past experience
632 * we have around 20K functions. That should be more than enough.
633 * It is highly unlikely we will execute every function in
639 pg = stat->start = stat->pages;
641 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
643 for (i = 1; i < pages; i++) {
644 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
655 unsigned long tmp = (unsigned long)pg;
667 static int ftrace_profile_init_cpu(int cpu)
669 struct ftrace_profile_stat *stat;
672 stat = &per_cpu(ftrace_profile_stats, cpu);
675 /* If the profile is already created, simply reset it */
676 ftrace_profile_reset(stat);
681 * We are profiling all functions, but usually only a few thousand
682 * functions are hit. We'll make a hash of 1024 items.
684 size = FTRACE_PROFILE_HASH_SIZE;
686 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
691 /* Preallocate the function profiling pages */
692 if (ftrace_profile_pages_init(stat) < 0) {
701 static int ftrace_profile_init(void)
706 for_each_possible_cpu(cpu) {
707 ret = ftrace_profile_init_cpu(cpu);
715 /* interrupts must be disabled */
716 static struct ftrace_profile *
717 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
719 struct ftrace_profile *rec;
720 struct hlist_head *hhd;
723 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
724 hhd = &stat->hash[key];
726 if (hlist_empty(hhd))
729 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
737 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
738 struct ftrace_profile *rec)
742 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
743 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
747 * The memory is already allocated, this simply finds a new record to use.
749 static struct ftrace_profile *
750 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
752 struct ftrace_profile *rec = NULL;
754 /* prevent recursion (from NMIs) */
755 if (atomic_inc_return(&stat->disabled) != 1)
759 * Try to find the function again since an NMI
760 * could have added it
762 rec = ftrace_find_profiled_func(stat, ip);
766 if (stat->pages->index == PROFILES_PER_PAGE) {
767 if (!stat->pages->next)
769 stat->pages = stat->pages->next;
772 rec = &stat->pages->records[stat->pages->index++];
774 ftrace_add_profile(stat, rec);
777 atomic_dec(&stat->disabled);
783 function_profile_call(unsigned long ip, unsigned long parent_ip,
784 struct ftrace_ops *ops, struct ftrace_regs *fregs)
786 struct ftrace_profile_stat *stat;
787 struct ftrace_profile *rec;
790 if (!ftrace_profile_enabled)
793 local_irq_save(flags);
795 stat = this_cpu_ptr(&ftrace_profile_stats);
796 if (!stat->hash || !ftrace_profile_enabled)
799 rec = ftrace_find_profiled_func(stat, ip);
801 rec = ftrace_profile_alloc(stat, ip);
808 local_irq_restore(flags);
811 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
812 static bool fgraph_graph_time = true;
814 void ftrace_graph_graph_time_control(bool enable)
816 fgraph_graph_time = enable;
819 static int profile_graph_entry(struct ftrace_graph_ent *trace)
821 struct ftrace_ret_stack *ret_stack;
823 function_profile_call(trace->func, 0, NULL, NULL);
825 /* If function graph is shutting down, ret_stack can be NULL */
826 if (!current->ret_stack)
829 ret_stack = ftrace_graph_get_ret_stack(current, 0);
831 ret_stack->subtime = 0;
836 static void profile_graph_return(struct ftrace_graph_ret *trace)
838 struct ftrace_ret_stack *ret_stack;
839 struct ftrace_profile_stat *stat;
840 unsigned long long calltime;
841 struct ftrace_profile *rec;
844 local_irq_save(flags);
845 stat = this_cpu_ptr(&ftrace_profile_stats);
846 if (!stat->hash || !ftrace_profile_enabled)
849 /* If the calltime was zero'd ignore it */
850 if (!trace->calltime)
853 calltime = trace->rettime - trace->calltime;
855 if (!fgraph_graph_time) {
857 /* Append this call time to the parent time to subtract */
858 ret_stack = ftrace_graph_get_ret_stack(current, 1);
860 ret_stack->subtime += calltime;
862 ret_stack = ftrace_graph_get_ret_stack(current, 0);
863 if (ret_stack && ret_stack->subtime < calltime)
864 calltime -= ret_stack->subtime;
869 rec = ftrace_find_profiled_func(stat, trace->func);
871 rec->time += calltime;
872 rec->time_squared += calltime * calltime;
876 local_irq_restore(flags);
879 static struct fgraph_ops fprofiler_ops = {
880 .entryfunc = &profile_graph_entry,
881 .retfunc = &profile_graph_return,
884 static int register_ftrace_profiler(void)
886 return register_ftrace_graph(&fprofiler_ops);
889 static void unregister_ftrace_profiler(void)
891 unregister_ftrace_graph(&fprofiler_ops);
894 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
895 .func = function_profile_call,
896 .flags = FTRACE_OPS_FL_INITIALIZED,
897 INIT_OPS_HASH(ftrace_profile_ops)
900 static int register_ftrace_profiler(void)
902 return register_ftrace_function(&ftrace_profile_ops);
905 static void unregister_ftrace_profiler(void)
907 unregister_ftrace_function(&ftrace_profile_ops);
909 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
912 ftrace_profile_write(struct file *filp, const char __user *ubuf,
913 size_t cnt, loff_t *ppos)
918 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
924 mutex_lock(&ftrace_profile_lock);
925 if (ftrace_profile_enabled ^ val) {
927 ret = ftrace_profile_init();
933 ret = register_ftrace_profiler();
938 ftrace_profile_enabled = 1;
940 ftrace_profile_enabled = 0;
942 * unregister_ftrace_profiler calls stop_machine
943 * so this acts like an synchronize_rcu.
945 unregister_ftrace_profiler();
949 mutex_unlock(&ftrace_profile_lock);
957 ftrace_profile_read(struct file *filp, char __user *ubuf,
958 size_t cnt, loff_t *ppos)
960 char buf[64]; /* big enough to hold a number */
963 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
964 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
967 static const struct file_operations ftrace_profile_fops = {
968 .open = tracing_open_generic,
969 .read = ftrace_profile_read,
970 .write = ftrace_profile_write,
971 .llseek = default_llseek,
974 /* used to initialize the real stat files */
975 static struct tracer_stat function_stats __initdata = {
977 .stat_start = function_stat_start,
978 .stat_next = function_stat_next,
979 .stat_cmp = function_stat_cmp,
980 .stat_headers = function_stat_headers,
981 .stat_show = function_stat_show
984 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
986 struct ftrace_profile_stat *stat;
991 for_each_possible_cpu(cpu) {
992 stat = &per_cpu(ftrace_profile_stats, cpu);
994 name = kasprintf(GFP_KERNEL, "function%d", cpu);
997 * The files created are permanent, if something happens
998 * we still do not free memory.
1001 "Could not allocate stat file for cpu %d\n",
1005 stat->stat = function_stats;
1006 stat->stat.name = name;
1007 ret = register_stat_tracer(&stat->stat);
1010 "Could not register function stat for cpu %d\n",
1017 trace_create_file("function_profile_enabled",
1018 TRACE_MODE_WRITE, d_tracer, NULL,
1019 &ftrace_profile_fops);
1022 #else /* CONFIG_FUNCTION_PROFILER */
1023 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1026 #endif /* CONFIG_FUNCTION_PROFILER */
1028 #ifdef CONFIG_DYNAMIC_FTRACE
1030 static struct ftrace_ops *removed_ops;
1033 * Set when doing a global update, like enabling all recs or disabling them.
1034 * It is not set when just updating a single ftrace_ops.
1036 static bool update_all_ops;
1038 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1039 # error Dynamic ftrace depends on MCOUNT_RECORD
1042 struct ftrace_func_probe {
1043 struct ftrace_probe_ops *probe_ops;
1044 struct ftrace_ops ops;
1045 struct trace_array *tr;
1046 struct list_head list;
1052 * We make these constant because no one should touch them,
1053 * but they are used as the default "empty hash", to avoid allocating
1054 * it all the time. These are in a read only section such that if
1055 * anyone does try to modify it, it will cause an exception.
1057 static const struct hlist_head empty_buckets[1];
1058 static const struct ftrace_hash empty_hash = {
1059 .buckets = (struct hlist_head *)empty_buckets,
1061 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1063 struct ftrace_ops global_ops = {
1064 .func = ftrace_stub,
1065 .local_hash.notrace_hash = EMPTY_HASH,
1066 .local_hash.filter_hash = EMPTY_HASH,
1067 INIT_OPS_HASH(global_ops)
1068 .flags = FTRACE_OPS_FL_INITIALIZED |
1073 * Used by the stack unwinder to know about dynamic ftrace trampolines.
1075 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1077 struct ftrace_ops *op = NULL;
1080 * Some of the ops may be dynamically allocated,
1081 * they are freed after a synchronize_rcu().
1083 preempt_disable_notrace();
1085 do_for_each_ftrace_op(op, ftrace_ops_list) {
1087 * This is to check for dynamically allocated trampolines.
1088 * Trampolines that are in kernel text will have
1089 * core_kernel_text() return true.
1091 if (op->trampoline && op->trampoline_size)
1092 if (addr >= op->trampoline &&
1093 addr < op->trampoline + op->trampoline_size) {
1094 preempt_enable_notrace();
1097 } while_for_each_ftrace_op(op);
1098 preempt_enable_notrace();
1104 * This is used by __kernel_text_address() to return true if the
1105 * address is on a dynamically allocated trampoline that would
1106 * not return true for either core_kernel_text() or
1107 * is_module_text_address().
1109 bool is_ftrace_trampoline(unsigned long addr)
1111 return ftrace_ops_trampoline(addr) != NULL;
1114 struct ftrace_page {
1115 struct ftrace_page *next;
1116 struct dyn_ftrace *records;
1121 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1122 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1124 static struct ftrace_page *ftrace_pages_start;
1125 static struct ftrace_page *ftrace_pages;
1127 static __always_inline unsigned long
1128 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1130 if (hash->size_bits > 0)
1131 return hash_long(ip, hash->size_bits);
1136 /* Only use this function if ftrace_hash_empty() has already been tested */
1137 static __always_inline struct ftrace_func_entry *
1138 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1141 struct ftrace_func_entry *entry;
1142 struct hlist_head *hhd;
1144 key = ftrace_hash_key(hash, ip);
1145 hhd = &hash->buckets[key];
1147 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1148 if (entry->ip == ip)
1155 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1156 * @hash: The hash to look at
1157 * @ip: The instruction pointer to test
1159 * Search a given @hash to see if a given instruction pointer (@ip)
1162 * Returns the entry that holds the @ip if found. NULL otherwise.
1164 struct ftrace_func_entry *
1165 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1167 if (ftrace_hash_empty(hash))
1170 return __ftrace_lookup_ip(hash, ip);
1173 static void __add_hash_entry(struct ftrace_hash *hash,
1174 struct ftrace_func_entry *entry)
1176 struct hlist_head *hhd;
1179 key = ftrace_hash_key(hash, entry->ip);
1180 hhd = &hash->buckets[key];
1181 hlist_add_head(&entry->hlist, hhd);
1185 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1187 struct ftrace_func_entry *entry;
1189 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1194 __add_hash_entry(hash, entry);
1200 free_hash_entry(struct ftrace_hash *hash,
1201 struct ftrace_func_entry *entry)
1203 hlist_del(&entry->hlist);
1209 remove_hash_entry(struct ftrace_hash *hash,
1210 struct ftrace_func_entry *entry)
1212 hlist_del_rcu(&entry->hlist);
1216 static void ftrace_hash_clear(struct ftrace_hash *hash)
1218 struct hlist_head *hhd;
1219 struct hlist_node *tn;
1220 struct ftrace_func_entry *entry;
1221 int size = 1 << hash->size_bits;
1227 for (i = 0; i < size; i++) {
1228 hhd = &hash->buckets[i];
1229 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1230 free_hash_entry(hash, entry);
1232 FTRACE_WARN_ON(hash->count);
1235 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1237 list_del(&ftrace_mod->list);
1238 kfree(ftrace_mod->module);
1239 kfree(ftrace_mod->func);
1243 static void clear_ftrace_mod_list(struct list_head *head)
1245 struct ftrace_mod_load *p, *n;
1247 /* stack tracer isn't supported yet */
1251 mutex_lock(&ftrace_lock);
1252 list_for_each_entry_safe(p, n, head, list)
1254 mutex_unlock(&ftrace_lock);
1257 static void free_ftrace_hash(struct ftrace_hash *hash)
1259 if (!hash || hash == EMPTY_HASH)
1261 ftrace_hash_clear(hash);
1262 kfree(hash->buckets);
1266 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1268 struct ftrace_hash *hash;
1270 hash = container_of(rcu, struct ftrace_hash, rcu);
1271 free_ftrace_hash(hash);
1274 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1276 if (!hash || hash == EMPTY_HASH)
1278 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
1282 * ftrace_free_filter - remove all filters for an ftrace_ops
1283 * @ops - the ops to remove the filters from
1285 void ftrace_free_filter(struct ftrace_ops *ops)
1287 ftrace_ops_init(ops);
1288 free_ftrace_hash(ops->func_hash->filter_hash);
1289 free_ftrace_hash(ops->func_hash->notrace_hash);
1291 EXPORT_SYMBOL_GPL(ftrace_free_filter);
1293 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1295 struct ftrace_hash *hash;
1298 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1302 size = 1 << size_bits;
1303 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1305 if (!hash->buckets) {
1310 hash->size_bits = size_bits;
1316 static int ftrace_add_mod(struct trace_array *tr,
1317 const char *func, const char *module,
1320 struct ftrace_mod_load *ftrace_mod;
1321 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1323 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1327 INIT_LIST_HEAD(&ftrace_mod->list);
1328 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1329 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1330 ftrace_mod->enable = enable;
1332 if (!ftrace_mod->func || !ftrace_mod->module)
1335 list_add(&ftrace_mod->list, mod_head);
1340 free_ftrace_mod(ftrace_mod);
1345 static struct ftrace_hash *
1346 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1348 struct ftrace_func_entry *entry;
1349 struct ftrace_hash *new_hash;
1354 new_hash = alloc_ftrace_hash(size_bits);
1359 new_hash->flags = hash->flags;
1362 if (ftrace_hash_empty(hash))
1365 size = 1 << hash->size_bits;
1366 for (i = 0; i < size; i++) {
1367 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1368 ret = add_hash_entry(new_hash, entry->ip);
1374 FTRACE_WARN_ON(new_hash->count != hash->count);
1379 free_ftrace_hash(new_hash);
1384 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1386 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1388 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1389 struct ftrace_hash *new_hash);
1391 static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
1393 struct ftrace_func_entry *entry;
1394 struct ftrace_hash *new_hash;
1395 struct hlist_head *hhd;
1396 struct hlist_node *tn;
1401 * Use around half the size (max bit of it), but
1402 * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits).
1404 bits = fls(size / 2);
1406 /* Don't allocate too much */
1407 if (bits > FTRACE_HASH_MAX_BITS)
1408 bits = FTRACE_HASH_MAX_BITS;
1410 new_hash = alloc_ftrace_hash(bits);
1414 new_hash->flags = src->flags;
1416 size = 1 << src->size_bits;
1417 for (i = 0; i < size; i++) {
1418 hhd = &src->buckets[i];
1419 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1420 remove_hash_entry(src, entry);
1421 __add_hash_entry(new_hash, entry);
1427 static struct ftrace_hash *
1428 __ftrace_hash_move(struct ftrace_hash *src)
1430 int size = src->count;
1433 * If the new source is empty, just return the empty_hash.
1435 if (ftrace_hash_empty(src))
1438 return dup_hash(src, size);
1442 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1443 struct ftrace_hash **dst, struct ftrace_hash *src)
1445 struct ftrace_hash *new_hash;
1448 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1449 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1452 new_hash = __ftrace_hash_move(src);
1456 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1458 /* IPMODIFY should be updated only when filter_hash updating */
1459 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1461 free_ftrace_hash(new_hash);
1467 * Remove the current set, update the hash and add
1470 ftrace_hash_rec_disable_modify(ops, enable);
1472 rcu_assign_pointer(*dst, new_hash);
1474 ftrace_hash_rec_enable_modify(ops, enable);
1479 static bool hash_contains_ip(unsigned long ip,
1480 struct ftrace_ops_hash *hash)
1483 * The function record is a match if it exists in the filter
1484 * hash and not in the notrace hash. Note, an empty hash is
1485 * considered a match for the filter hash, but an empty
1486 * notrace hash is considered not in the notrace hash.
1488 return (ftrace_hash_empty(hash->filter_hash) ||
1489 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1490 (ftrace_hash_empty(hash->notrace_hash) ||
1491 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1495 * Test the hashes for this ops to see if we want to call
1496 * the ops->func or not.
1498 * It's a match if the ip is in the ops->filter_hash or
1499 * the filter_hash does not exist or is empty,
1501 * the ip is not in the ops->notrace_hash.
1503 * This needs to be called with preemption disabled as
1504 * the hashes are freed with call_rcu().
1507 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1509 struct ftrace_ops_hash hash;
1512 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1514 * There's a small race when adding ops that the ftrace handler
1515 * that wants regs, may be called without them. We can not
1516 * allow that handler to be called if regs is NULL.
1518 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1522 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1523 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1525 if (hash_contains_ip(ip, &hash))
1534 * This is a double for. Do not use 'break' to break out of the loop,
1535 * you must use a goto.
1537 #define do_for_each_ftrace_rec(pg, rec) \
1538 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1540 for (_____i = 0; _____i < pg->index; _____i++) { \
1541 rec = &pg->records[_____i];
1543 #define while_for_each_ftrace_rec() \
1548 static int ftrace_cmp_recs(const void *a, const void *b)
1550 const struct dyn_ftrace *key = a;
1551 const struct dyn_ftrace *rec = b;
1553 if (key->flags < rec->ip)
1555 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1560 static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
1562 struct ftrace_page *pg;
1563 struct dyn_ftrace *rec = NULL;
1564 struct dyn_ftrace key;
1567 key.flags = end; /* overload flags, as it is unsigned long */
1569 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1570 if (pg->index == 0 ||
1571 end < pg->records[0].ip ||
1572 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1574 rec = bsearch(&key, pg->records, pg->index,
1575 sizeof(struct dyn_ftrace),
1584 * ftrace_location_range - return the first address of a traced location
1585 * if it touches the given ip range
1586 * @start: start of range to search.
1587 * @end: end of range to search (inclusive). @end points to the last byte
1590 * Returns rec->ip if the related ftrace location is a least partly within
1591 * the given address range. That is, the first address of the instruction
1592 * that is either a NOP or call to the function tracer. It checks the ftrace
1593 * internal tables to determine if the address belongs or not.
1595 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1597 struct dyn_ftrace *rec;
1599 rec = lookup_rec(start, end);
1607 * ftrace_location - return the ftrace location
1608 * @ip: the instruction pointer to check
1610 * If @ip matches the ftrace location, return @ip.
1611 * If @ip matches sym+0, return sym's ftrace location.
1612 * Otherwise, return 0.
1614 unsigned long ftrace_location(unsigned long ip)
1616 struct dyn_ftrace *rec;
1617 unsigned long offset;
1620 rec = lookup_rec(ip, ip);
1622 if (!kallsyms_lookup_size_offset(ip, &size, &offset))
1625 /* map sym+0 to __fentry__ */
1627 rec = lookup_rec(ip, ip + size - 1);
1638 * ftrace_text_reserved - return true if range contains an ftrace location
1639 * @start: start of range to search
1640 * @end: end of range to search (inclusive). @end points to the last byte to check.
1642 * Returns 1 if @start and @end contains a ftrace location.
1643 * That is, the instruction that is either a NOP or call to
1644 * the function tracer. It checks the ftrace internal tables to
1645 * determine if the address belongs or not.
1647 int ftrace_text_reserved(const void *start, const void *end)
1651 ret = ftrace_location_range((unsigned long)start,
1652 (unsigned long)end);
1657 /* Test if ops registered to this rec needs regs */
1658 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1660 struct ftrace_ops *ops;
1661 bool keep_regs = false;
1663 for (ops = ftrace_ops_list;
1664 ops != &ftrace_list_end; ops = ops->next) {
1665 /* pass rec in as regs to have non-NULL val */
1666 if (ftrace_ops_test(ops, rec->ip, rec)) {
1667 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1677 static struct ftrace_ops *
1678 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1679 static struct ftrace_ops *
1680 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1681 static struct ftrace_ops *
1682 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1684 static bool skip_record(struct dyn_ftrace *rec)
1687 * At boot up, weak functions are set to disable. Function tracing
1688 * can be enabled before they are, and they still need to be disabled now.
1689 * If the record is disabled, still continue if it is marked as already
1690 * enabled (this is needed to keep the accounting working).
1692 return rec->flags & FTRACE_FL_DISABLED &&
1693 !(rec->flags & FTRACE_FL_ENABLED);
1696 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1700 struct ftrace_hash *hash;
1701 struct ftrace_hash *other_hash;
1702 struct ftrace_page *pg;
1703 struct dyn_ftrace *rec;
1704 bool update = false;
1708 /* Only update if the ops has been registered */
1709 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1713 * In the filter_hash case:
1714 * If the count is zero, we update all records.
1715 * Otherwise we just update the items in the hash.
1717 * In the notrace_hash case:
1718 * We enable the update in the hash.
1719 * As disabling notrace means enabling the tracing,
1720 * and enabling notrace means disabling, the inc variable
1724 hash = ops->func_hash->filter_hash;
1725 other_hash = ops->func_hash->notrace_hash;
1726 if (ftrace_hash_empty(hash))
1730 hash = ops->func_hash->notrace_hash;
1731 other_hash = ops->func_hash->filter_hash;
1733 * If the notrace hash has no items,
1734 * then there's nothing to do.
1736 if (ftrace_hash_empty(hash))
1740 do_for_each_ftrace_rec(pg, rec) {
1741 int in_other_hash = 0;
1745 if (skip_record(rec))
1750 * Only the filter_hash affects all records.
1751 * Update if the record is not in the notrace hash.
1753 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1756 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1757 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1760 * If filter_hash is set, we want to match all functions
1761 * that are in the hash but not in the other hash.
1763 * If filter_hash is not set, then we are decrementing.
1764 * That means we match anything that is in the hash
1765 * and also in the other_hash. That is, we need to turn
1766 * off functions in the other hash because they are disabled
1769 if (filter_hash && in_hash && !in_other_hash)
1771 else if (!filter_hash && in_hash &&
1772 (in_other_hash || ftrace_hash_empty(other_hash)))
1780 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1783 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1784 rec->flags |= FTRACE_FL_DIRECT;
1787 * If there's only a single callback registered to a
1788 * function, and the ops has a trampoline registered
1789 * for it, then we can call it directly.
1791 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1792 rec->flags |= FTRACE_FL_TRAMP;
1795 * If we are adding another function callback
1796 * to this function, and the previous had a
1797 * custom trampoline in use, then we need to go
1798 * back to the default trampoline.
1800 rec->flags &= ~FTRACE_FL_TRAMP;
1803 * If any ops wants regs saved for this function
1804 * then all ops will get saved regs.
1806 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1807 rec->flags |= FTRACE_FL_REGS;
1809 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1814 * Only the internal direct_ops should have the
1815 * DIRECT flag set. Thus, if it is removing a
1816 * function, then that function should no longer
1819 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1820 rec->flags &= ~FTRACE_FL_DIRECT;
1823 * If the rec had REGS enabled and the ops that is
1824 * being removed had REGS set, then see if there is
1825 * still any ops for this record that wants regs.
1826 * If not, we can stop recording them.
1828 if (ftrace_rec_count(rec) > 0 &&
1829 rec->flags & FTRACE_FL_REGS &&
1830 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1831 if (!test_rec_ops_needs_regs(rec))
1832 rec->flags &= ~FTRACE_FL_REGS;
1836 * The TRAMP needs to be set only if rec count
1837 * is decremented to one, and the ops that is
1838 * left has a trampoline. As TRAMP can only be
1839 * enabled if there is only a single ops attached
1842 if (ftrace_rec_count(rec) == 1 &&
1843 ftrace_find_tramp_ops_any_other(rec, ops))
1844 rec->flags |= FTRACE_FL_TRAMP;
1846 rec->flags &= ~FTRACE_FL_TRAMP;
1849 * flags will be cleared in ftrace_check_record()
1850 * if rec count is zero.
1855 * If the rec has a single associated ops, and ops->func can be
1856 * called directly, allow the call site to call via the ops.
1858 if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS) &&
1859 ftrace_rec_count(rec) == 1 &&
1860 ftrace_ops_get_func(ops) == ops->func)
1861 rec->flags |= FTRACE_FL_CALL_OPS;
1863 rec->flags &= ~FTRACE_FL_CALL_OPS;
1867 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1868 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
1870 /* Shortcut, if we handled all records, we are done. */
1871 if (!all && count == hash->count)
1873 } while_for_each_ftrace_rec();
1878 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1881 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1884 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1887 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1890 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1891 int filter_hash, int inc)
1893 struct ftrace_ops *op;
1895 __ftrace_hash_rec_update(ops, filter_hash, inc);
1897 if (ops->func_hash != &global_ops.local_hash)
1901 * If the ops shares the global_ops hash, then we need to update
1902 * all ops that are enabled and use this hash.
1904 do_for_each_ftrace_op(op, ftrace_ops_list) {
1908 if (op->func_hash == &global_ops.local_hash)
1909 __ftrace_hash_rec_update(op, filter_hash, inc);
1910 } while_for_each_ftrace_op(op);
1913 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1916 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1919 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1922 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1926 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1927 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1928 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1929 * Note that old_hash and new_hash has below meanings
1930 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1931 * - If the hash is EMPTY_HASH, it hits nothing
1932 * - Anything else hits the recs which match the hash entries.
1934 * DIRECT ops does not have IPMODIFY flag, but we still need to check it
1935 * against functions with FTRACE_FL_IPMODIFY. If there is any overlap, call
1936 * ops_func(SHARE_IPMODIFY_SELF) to make sure current ops can share with
1937 * IPMODIFY. If ops_func(SHARE_IPMODIFY_SELF) returns non-zero, propagate
1938 * the return value to the caller and eventually to the owner of the DIRECT
1941 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1942 struct ftrace_hash *old_hash,
1943 struct ftrace_hash *new_hash)
1945 struct ftrace_page *pg;
1946 struct dyn_ftrace *rec, *end = NULL;
1948 bool is_ipmodify, is_direct;
1950 /* Only update if the ops has been registered */
1951 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1954 is_ipmodify = ops->flags & FTRACE_OPS_FL_IPMODIFY;
1955 is_direct = ops->flags & FTRACE_OPS_FL_DIRECT;
1957 /* neither IPMODIFY nor DIRECT, skip */
1958 if (!is_ipmodify && !is_direct)
1961 if (WARN_ON_ONCE(is_ipmodify && is_direct))
1965 * Since the IPMODIFY and DIRECT are very address sensitive
1966 * actions, we do not allow ftrace_ops to set all functions to new
1969 if (!new_hash || !old_hash)
1972 /* Update rec->flags */
1973 do_for_each_ftrace_rec(pg, rec) {
1975 if (rec->flags & FTRACE_FL_DISABLED)
1978 /* We need to update only differences of filter_hash */
1979 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1980 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1981 if (in_old == in_new)
1985 if (rec->flags & FTRACE_FL_IPMODIFY) {
1988 /* Cannot have two ipmodify on same rec */
1992 FTRACE_WARN_ON(rec->flags & FTRACE_FL_DIRECT);
1995 * Another ops with IPMODIFY is already
1996 * attached. We are now attaching a direct
1997 * ops. Run SHARE_IPMODIFY_SELF, to check
1998 * whether sharing is supported.
2002 ret = ops->ops_func(ops, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF);
2005 } else if (is_ipmodify) {
2006 rec->flags |= FTRACE_FL_IPMODIFY;
2008 } else if (is_ipmodify) {
2009 rec->flags &= ~FTRACE_FL_IPMODIFY;
2011 } while_for_each_ftrace_rec();
2018 /* Roll back what we did above */
2019 do_for_each_ftrace_rec(pg, rec) {
2021 if (rec->flags & FTRACE_FL_DISABLED)
2027 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
2028 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
2029 if (in_old == in_new)
2033 rec->flags &= ~FTRACE_FL_IPMODIFY;
2035 rec->flags |= FTRACE_FL_IPMODIFY;
2036 } while_for_each_ftrace_rec();
2042 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
2044 struct ftrace_hash *hash = ops->func_hash->filter_hash;
2046 if (ftrace_hash_empty(hash))
2049 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
2052 /* Disabling always succeeds */
2053 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
2055 struct ftrace_hash *hash = ops->func_hash->filter_hash;
2057 if (ftrace_hash_empty(hash))
2060 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
2063 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
2064 struct ftrace_hash *new_hash)
2066 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
2068 if (ftrace_hash_empty(old_hash))
2071 if (ftrace_hash_empty(new_hash))
2074 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
2077 static void print_ip_ins(const char *fmt, const unsigned char *p)
2079 char ins[MCOUNT_INSN_SIZE];
2081 if (copy_from_kernel_nofault(ins, p, MCOUNT_INSN_SIZE)) {
2082 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
2086 printk(KERN_CONT "%s", fmt);
2087 pr_cont("%*phC", MCOUNT_INSN_SIZE, ins);
2090 enum ftrace_bug_type ftrace_bug_type;
2091 const void *ftrace_expected;
2093 static void print_bug_type(void)
2095 switch (ftrace_bug_type) {
2096 case FTRACE_BUG_UNKNOWN:
2098 case FTRACE_BUG_INIT:
2099 pr_info("Initializing ftrace call sites\n");
2101 case FTRACE_BUG_NOP:
2102 pr_info("Setting ftrace call site to NOP\n");
2104 case FTRACE_BUG_CALL:
2105 pr_info("Setting ftrace call site to call ftrace function\n");
2107 case FTRACE_BUG_UPDATE:
2108 pr_info("Updating ftrace call site to call a different ftrace function\n");
2114 * ftrace_bug - report and shutdown function tracer
2115 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2116 * @rec: The record that failed
2118 * The arch code that enables or disables the function tracing
2119 * can call ftrace_bug() when it has detected a problem in
2120 * modifying the code. @failed should be one of either:
2121 * EFAULT - if the problem happens on reading the @ip address
2122 * EINVAL - if what is read at @ip is not what was expected
2123 * EPERM - if the problem happens on writing to the @ip address
2125 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2127 unsigned long ip = rec ? rec->ip : 0;
2129 pr_info("------------[ ftrace bug ]------------\n");
2133 pr_info("ftrace faulted on modifying ");
2134 print_ip_sym(KERN_INFO, ip);
2137 pr_info("ftrace failed to modify ");
2138 print_ip_sym(KERN_INFO, ip);
2139 print_ip_ins(" actual: ", (unsigned char *)ip);
2141 if (ftrace_expected) {
2142 print_ip_ins(" expected: ", ftrace_expected);
2147 pr_info("ftrace faulted on writing ");
2148 print_ip_sym(KERN_INFO, ip);
2151 pr_info("ftrace faulted on unknown error ");
2152 print_ip_sym(KERN_INFO, ip);
2156 struct ftrace_ops *ops = NULL;
2158 pr_info("ftrace record flags: %lx\n", rec->flags);
2159 pr_cont(" (%ld)%s%s", ftrace_rec_count(rec),
2160 rec->flags & FTRACE_FL_REGS ? " R" : " ",
2161 rec->flags & FTRACE_FL_CALL_OPS ? " O" : " ");
2162 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2163 ops = ftrace_find_tramp_ops_any(rec);
2166 pr_cont("\ttramp: %pS (%pS)",
2167 (void *)ops->trampoline,
2169 ops = ftrace_find_tramp_ops_next(rec, ops);
2172 pr_cont("\ttramp: ERROR!");
2175 ip = ftrace_get_addr_curr(rec);
2176 pr_cont("\n expected tramp: %lx\n", ip);
2179 FTRACE_WARN_ON_ONCE(1);
2182 static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
2184 unsigned long flag = 0UL;
2186 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2188 if (skip_record(rec))
2189 return FTRACE_UPDATE_IGNORE;
2192 * If we are updating calls:
2194 * If the record has a ref count, then we need to enable it
2195 * because someone is using it.
2197 * Otherwise we make sure its disabled.
2199 * If we are disabling calls, then disable all records that
2202 if (enable && ftrace_rec_count(rec))
2203 flag = FTRACE_FL_ENABLED;
2206 * If enabling and the REGS flag does not match the REGS_EN, or
2207 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2208 * this record. Set flags to fail the compare against ENABLED.
2209 * Same for direct calls.
2212 if (!(rec->flags & FTRACE_FL_REGS) !=
2213 !(rec->flags & FTRACE_FL_REGS_EN))
2214 flag |= FTRACE_FL_REGS;
2216 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2217 !(rec->flags & FTRACE_FL_TRAMP_EN))
2218 flag |= FTRACE_FL_TRAMP;
2221 * Direct calls are special, as count matters.
2222 * We must test the record for direct, if the
2223 * DIRECT and DIRECT_EN do not match, but only
2224 * if the count is 1. That's because, if the
2225 * count is something other than one, we do not
2226 * want the direct enabled (it will be done via the
2227 * direct helper). But if DIRECT_EN is set, and
2228 * the count is not one, we need to clear it.
2231 if (ftrace_rec_count(rec) == 1) {
2232 if (!(rec->flags & FTRACE_FL_DIRECT) !=
2233 !(rec->flags & FTRACE_FL_DIRECT_EN))
2234 flag |= FTRACE_FL_DIRECT;
2235 } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
2236 flag |= FTRACE_FL_DIRECT;
2240 * Ops calls are special, as count matters.
2241 * As with direct calls, they must only be enabled when count
2242 * is one, otherwise they'll be handled via the list ops.
2244 if (ftrace_rec_count(rec) == 1) {
2245 if (!(rec->flags & FTRACE_FL_CALL_OPS) !=
2246 !(rec->flags & FTRACE_FL_CALL_OPS_EN))
2247 flag |= FTRACE_FL_CALL_OPS;
2248 } else if (rec->flags & FTRACE_FL_CALL_OPS_EN) {
2249 flag |= FTRACE_FL_CALL_OPS;
2253 /* If the state of this record hasn't changed, then do nothing */
2254 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2255 return FTRACE_UPDATE_IGNORE;
2258 /* Save off if rec is being enabled (for return value) */
2259 flag ^= rec->flags & FTRACE_FL_ENABLED;
2262 rec->flags |= FTRACE_FL_ENABLED | FTRACE_FL_TOUCHED;
2263 if (flag & FTRACE_FL_REGS) {
2264 if (rec->flags & FTRACE_FL_REGS)
2265 rec->flags |= FTRACE_FL_REGS_EN;
2267 rec->flags &= ~FTRACE_FL_REGS_EN;
2269 if (flag & FTRACE_FL_TRAMP) {
2270 if (rec->flags & FTRACE_FL_TRAMP)
2271 rec->flags |= FTRACE_FL_TRAMP_EN;
2273 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2276 if (flag & FTRACE_FL_DIRECT) {
2278 * If there's only one user (direct_ops helper)
2279 * then we can call the direct function
2280 * directly (no ftrace trampoline).
2282 if (ftrace_rec_count(rec) == 1) {
2283 if (rec->flags & FTRACE_FL_DIRECT)
2284 rec->flags |= FTRACE_FL_DIRECT_EN;
2286 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2289 * Can only call directly if there's
2290 * only one callback to the function.
2292 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2296 if (flag & FTRACE_FL_CALL_OPS) {
2297 if (ftrace_rec_count(rec) == 1) {
2298 if (rec->flags & FTRACE_FL_CALL_OPS)
2299 rec->flags |= FTRACE_FL_CALL_OPS_EN;
2301 rec->flags &= ~FTRACE_FL_CALL_OPS_EN;
2304 * Can only call directly if there's
2305 * only one set of associated ops.
2307 rec->flags &= ~FTRACE_FL_CALL_OPS_EN;
2313 * If this record is being updated from a nop, then
2314 * return UPDATE_MAKE_CALL.
2316 * return UPDATE_MODIFY_CALL to tell the caller to convert
2317 * from the save regs, to a non-save regs function or
2318 * vice versa, or from a trampoline call.
2320 if (flag & FTRACE_FL_ENABLED) {
2321 ftrace_bug_type = FTRACE_BUG_CALL;
2322 return FTRACE_UPDATE_MAKE_CALL;
2325 ftrace_bug_type = FTRACE_BUG_UPDATE;
2326 return FTRACE_UPDATE_MODIFY_CALL;
2330 /* If there's no more users, clear all flags */
2331 if (!ftrace_rec_count(rec))
2332 rec->flags &= FTRACE_NOCLEAR_FLAGS;
2335 * Just disable the record, but keep the ops TRAMP
2336 * and REGS states. The _EN flags must be disabled though.
2338 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2339 FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN |
2340 FTRACE_FL_CALL_OPS_EN);
2343 ftrace_bug_type = FTRACE_BUG_NOP;
2344 return FTRACE_UPDATE_MAKE_NOP;
2348 * ftrace_update_record - set a record that now is tracing or not
2349 * @rec: the record to update
2350 * @enable: set to true if the record is tracing, false to force disable
2352 * The records that represent all functions that can be traced need
2353 * to be updated when tracing has been enabled.
2355 int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
2357 return ftrace_check_record(rec, enable, true);
2361 * ftrace_test_record - check if the record has been enabled or not
2362 * @rec: the record to test
2363 * @enable: set to true to check if enabled, false if it is disabled
2365 * The arch code may need to test if a record is already set to
2366 * tracing to determine how to modify the function code that it
2369 int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
2371 return ftrace_check_record(rec, enable, false);
2374 static struct ftrace_ops *
2375 ftrace_find_tramp_ops_any(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) {
2382 if (!op->trampoline)
2385 if (hash_contains_ip(ip, op->func_hash))
2387 } while_for_each_ftrace_op(op);
2392 static struct ftrace_ops *
2393 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2395 struct ftrace_ops *op;
2396 unsigned long ip = rec->ip;
2398 do_for_each_ftrace_op(op, ftrace_ops_list) {
2400 if (op == op_exclude || !op->trampoline)
2403 if (hash_contains_ip(ip, op->func_hash))
2405 } while_for_each_ftrace_op(op);
2410 static struct ftrace_ops *
2411 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2412 struct ftrace_ops *op)
2414 unsigned long ip = rec->ip;
2416 while_for_each_ftrace_op(op) {
2418 if (!op->trampoline)
2421 if (hash_contains_ip(ip, op->func_hash))
2428 static struct ftrace_ops *
2429 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2431 struct ftrace_ops *op;
2432 unsigned long ip = rec->ip;
2435 * Need to check removed ops first.
2436 * If they are being removed, and this rec has a tramp,
2437 * and this rec is in the ops list, then it would be the
2438 * one with the tramp.
2441 if (hash_contains_ip(ip, &removed_ops->old_hash))
2446 * Need to find the current trampoline for a rec.
2447 * Now, a trampoline is only attached to a rec if there
2448 * was a single 'ops' attached to it. But this can be called
2449 * when we are adding another op to the rec or removing the
2450 * current one. Thus, if the op is being added, we can
2451 * ignore it because it hasn't attached itself to the rec
2454 * If an ops is being modified (hooking to different functions)
2455 * then we don't care about the new functions that are being
2456 * added, just the old ones (that are probably being removed).
2458 * If we are adding an ops to a function that already is using
2459 * a trampoline, it needs to be removed (trampolines are only
2460 * for single ops connected), then an ops that is not being
2461 * modified also needs to be checked.
2463 do_for_each_ftrace_op(op, ftrace_ops_list) {
2465 if (!op->trampoline)
2469 * If the ops is being added, it hasn't gotten to
2470 * the point to be removed from this tree yet.
2472 if (op->flags & FTRACE_OPS_FL_ADDING)
2477 * If the ops is being modified and is in the old
2478 * hash, then it is probably being removed from this
2481 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2482 hash_contains_ip(ip, &op->old_hash))
2485 * If the ops is not being added or modified, and it's
2486 * in its normal filter hash, then this must be the one
2489 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2490 hash_contains_ip(ip, op->func_hash))
2493 } while_for_each_ftrace_op(op);
2498 static struct ftrace_ops *
2499 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2501 struct ftrace_ops *op;
2502 unsigned long ip = rec->ip;
2504 do_for_each_ftrace_op(op, ftrace_ops_list) {
2505 /* pass rec in as regs to have non-NULL val */
2506 if (hash_contains_ip(ip, op->func_hash))
2508 } while_for_each_ftrace_op(op);
2514 ftrace_find_unique_ops(struct dyn_ftrace *rec)
2516 struct ftrace_ops *op, *found = NULL;
2517 unsigned long ip = rec->ip;
2519 do_for_each_ftrace_op(op, ftrace_ops_list) {
2521 if (hash_contains_ip(ip, op->func_hash)) {
2527 } while_for_each_ftrace_op(op);
2532 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2533 /* Protected by rcu_tasks for reading, and direct_mutex for writing */
2534 static struct ftrace_hash *direct_functions = EMPTY_HASH;
2535 static DEFINE_MUTEX(direct_mutex);
2536 int ftrace_direct_func_count;
2539 * Search the direct_functions hash to see if the given instruction pointer
2540 * has a direct caller attached to it.
2542 unsigned long ftrace_find_rec_direct(unsigned long ip)
2544 struct ftrace_func_entry *entry;
2546 entry = __ftrace_lookup_ip(direct_functions, ip);
2550 return entry->direct;
2553 static struct ftrace_func_entry*
2554 ftrace_add_rec_direct(unsigned long ip, unsigned long addr,
2555 struct ftrace_hash **free_hash)
2557 struct ftrace_func_entry *entry;
2559 if (ftrace_hash_empty(direct_functions) ||
2560 direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
2561 struct ftrace_hash *new_hash;
2562 int size = ftrace_hash_empty(direct_functions) ? 0 :
2563 direct_functions->count + 1;
2568 new_hash = dup_hash(direct_functions, size);
2572 *free_hash = direct_functions;
2573 direct_functions = new_hash;
2576 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2581 entry->direct = addr;
2582 __add_hash_entry(direct_functions, entry);
2586 static void call_direct_funcs(unsigned long ip, unsigned long pip,
2587 struct ftrace_ops *ops, struct ftrace_regs *fregs)
2589 unsigned long addr = READ_ONCE(ops->direct_call);
2594 arch_ftrace_set_direct_caller(fregs, addr);
2596 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2599 * ftrace_get_addr_new - Get the call address to set to
2600 * @rec: The ftrace record descriptor
2602 * If the record has the FTRACE_FL_REGS set, that means that it
2603 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2604 * is not set, then it wants to convert to the normal callback.
2606 * Returns the address of the trampoline to set to
2608 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2610 struct ftrace_ops *ops;
2613 if ((rec->flags & FTRACE_FL_DIRECT) &&
2614 (ftrace_rec_count(rec) == 1)) {
2615 addr = ftrace_find_rec_direct(rec->ip);
2621 /* Trampolines take precedence over regs */
2622 if (rec->flags & FTRACE_FL_TRAMP) {
2623 ops = ftrace_find_tramp_ops_new(rec);
2624 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2625 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2626 (void *)rec->ip, (void *)rec->ip, rec->flags);
2627 /* Ftrace is shutting down, return anything */
2628 return (unsigned long)FTRACE_ADDR;
2630 return ops->trampoline;
2633 if (rec->flags & FTRACE_FL_REGS)
2634 return (unsigned long)FTRACE_REGS_ADDR;
2636 return (unsigned long)FTRACE_ADDR;
2640 * ftrace_get_addr_curr - Get the call address that is already there
2641 * @rec: The ftrace record descriptor
2643 * The FTRACE_FL_REGS_EN is set when the record already points to
2644 * a function that saves all the regs. Basically the '_EN' version
2645 * represents the current state of the function.
2647 * Returns the address of the trampoline that is currently being called
2649 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2651 struct ftrace_ops *ops;
2654 /* Direct calls take precedence over trampolines */
2655 if (rec->flags & FTRACE_FL_DIRECT_EN) {
2656 addr = ftrace_find_rec_direct(rec->ip);
2662 /* Trampolines take precedence over regs */
2663 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2664 ops = ftrace_find_tramp_ops_curr(rec);
2665 if (FTRACE_WARN_ON(!ops)) {
2666 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2667 (void *)rec->ip, (void *)rec->ip);
2668 /* Ftrace is shutting down, return anything */
2669 return (unsigned long)FTRACE_ADDR;
2671 return ops->trampoline;
2674 if (rec->flags & FTRACE_FL_REGS_EN)
2675 return (unsigned long)FTRACE_REGS_ADDR;
2677 return (unsigned long)FTRACE_ADDR;
2681 __ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
2683 unsigned long ftrace_old_addr;
2684 unsigned long ftrace_addr;
2687 ftrace_addr = ftrace_get_addr_new(rec);
2689 /* This needs to be done before we call ftrace_update_record */
2690 ftrace_old_addr = ftrace_get_addr_curr(rec);
2692 ret = ftrace_update_record(rec, enable);
2694 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2697 case FTRACE_UPDATE_IGNORE:
2700 case FTRACE_UPDATE_MAKE_CALL:
2701 ftrace_bug_type = FTRACE_BUG_CALL;
2702 return ftrace_make_call(rec, ftrace_addr);
2704 case FTRACE_UPDATE_MAKE_NOP:
2705 ftrace_bug_type = FTRACE_BUG_NOP;
2706 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2708 case FTRACE_UPDATE_MODIFY_CALL:
2709 ftrace_bug_type = FTRACE_BUG_UPDATE;
2710 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2713 return -1; /* unknown ftrace bug */
2716 void __weak ftrace_replace_code(int mod_flags)
2718 struct dyn_ftrace *rec;
2719 struct ftrace_page *pg;
2720 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2721 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
2724 if (unlikely(ftrace_disabled))
2727 do_for_each_ftrace_rec(pg, rec) {
2729 if (skip_record(rec))
2732 failed = __ftrace_replace_code(rec, enable);
2734 ftrace_bug(failed, rec);
2735 /* Stop processing */
2740 } while_for_each_ftrace_rec();
2743 struct ftrace_rec_iter {
2744 struct ftrace_page *pg;
2749 * ftrace_rec_iter_start - start up iterating over traced functions
2751 * Returns an iterator handle that is used to iterate over all
2752 * the records that represent address locations where functions
2755 * May return NULL if no records are available.
2757 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2760 * We only use a single iterator.
2761 * Protected by the ftrace_lock mutex.
2763 static struct ftrace_rec_iter ftrace_rec_iter;
2764 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2766 iter->pg = ftrace_pages_start;
2769 /* Could have empty pages */
2770 while (iter->pg && !iter->pg->index)
2771 iter->pg = iter->pg->next;
2780 * ftrace_rec_iter_next - get the next record to process.
2781 * @iter: The handle to the iterator.
2783 * Returns the next iterator after the given iterator @iter.
2785 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2789 if (iter->index >= iter->pg->index) {
2790 iter->pg = iter->pg->next;
2793 /* Could have empty pages */
2794 while (iter->pg && !iter->pg->index)
2795 iter->pg = iter->pg->next;
2805 * ftrace_rec_iter_record - get the record at the iterator location
2806 * @iter: The current iterator location
2808 * Returns the record that the current @iter is at.
2810 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2812 return &iter->pg->records[iter->index];
2816 ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
2820 if (unlikely(ftrace_disabled))
2823 ret = ftrace_init_nop(mod, rec);
2825 ftrace_bug_type = FTRACE_BUG_INIT;
2826 ftrace_bug(ret, rec);
2833 * archs can override this function if they must do something
2834 * before the modifying code is performed.
2836 void __weak ftrace_arch_code_modify_prepare(void)
2841 * archs can override this function if they must do something
2842 * after the modifying code is performed.
2844 void __weak ftrace_arch_code_modify_post_process(void)
2848 static int update_ftrace_func(ftrace_func_t func)
2850 static ftrace_func_t save_func;
2852 /* Avoid updating if it hasn't changed */
2853 if (func == save_func)
2858 return ftrace_update_ftrace_func(func);
2861 void ftrace_modify_all_code(int command)
2863 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2867 if (command & FTRACE_MAY_SLEEP)
2868 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2871 * If the ftrace_caller calls a ftrace_ops func directly,
2872 * we need to make sure that it only traces functions it
2873 * expects to trace. When doing the switch of functions,
2874 * we need to update to the ftrace_ops_list_func first
2875 * before the transition between old and new calls are set,
2876 * as the ftrace_ops_list_func will check the ops hashes
2877 * to make sure the ops are having the right functions
2881 err = update_ftrace_func(ftrace_ops_list_func);
2882 if (FTRACE_WARN_ON(err))
2886 if (command & FTRACE_UPDATE_CALLS)
2887 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
2888 else if (command & FTRACE_DISABLE_CALLS)
2889 ftrace_replace_code(mod_flags);
2891 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2892 function_trace_op = set_function_trace_op;
2894 /* If irqs are disabled, we are in stop machine */
2895 if (!irqs_disabled())
2896 smp_call_function(ftrace_sync_ipi, NULL, 1);
2897 err = update_ftrace_func(ftrace_trace_function);
2898 if (FTRACE_WARN_ON(err))
2902 if (command & FTRACE_START_FUNC_RET)
2903 err = ftrace_enable_ftrace_graph_caller();
2904 else if (command & FTRACE_STOP_FUNC_RET)
2905 err = ftrace_disable_ftrace_graph_caller();
2906 FTRACE_WARN_ON(err);
2909 static int __ftrace_modify_code(void *data)
2911 int *command = data;
2913 ftrace_modify_all_code(*command);
2919 * ftrace_run_stop_machine - go back to the stop machine method
2920 * @command: The command to tell ftrace what to do
2922 * If an arch needs to fall back to the stop machine method, the
2923 * it can call this function.
2925 void ftrace_run_stop_machine(int command)
2927 stop_machine(__ftrace_modify_code, &command, NULL);
2931 * arch_ftrace_update_code - modify the code to trace or not trace
2932 * @command: The command that needs to be done
2934 * Archs can override this function if it does not need to
2935 * run stop_machine() to modify code.
2937 void __weak arch_ftrace_update_code(int command)
2939 ftrace_run_stop_machine(command);
2942 static void ftrace_run_update_code(int command)
2944 ftrace_arch_code_modify_prepare();
2947 * By default we use stop_machine() to modify the code.
2948 * But archs can do what ever they want as long as it
2949 * is safe. The stop_machine() is the safest, but also
2950 * produces the most overhead.
2952 arch_ftrace_update_code(command);
2954 ftrace_arch_code_modify_post_process();
2957 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2958 struct ftrace_ops_hash *old_hash)
2960 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2961 ops->old_hash.filter_hash = old_hash->filter_hash;
2962 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2963 ftrace_run_update_code(command);
2964 ops->old_hash.filter_hash = NULL;
2965 ops->old_hash.notrace_hash = NULL;
2966 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2969 static ftrace_func_t saved_ftrace_func;
2970 static int ftrace_start_up;
2972 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2976 /* List of trace_ops that have allocated trampolines */
2977 static LIST_HEAD(ftrace_ops_trampoline_list);
2979 static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops)
2981 lockdep_assert_held(&ftrace_lock);
2982 list_add_rcu(&ops->list, &ftrace_ops_trampoline_list);
2985 static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops)
2987 lockdep_assert_held(&ftrace_lock);
2988 list_del_rcu(&ops->list);
2993 * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
2994 * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
2997 #define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
2998 #define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
3000 static void ftrace_trampoline_free(struct ftrace_ops *ops)
3002 if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) &&
3005 * Record the text poke event before the ksymbol unregister
3008 perf_event_text_poke((void *)ops->trampoline,
3009 (void *)ops->trampoline,
3010 ops->trampoline_size, NULL, 0);
3011 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
3012 ops->trampoline, ops->trampoline_size,
3013 true, FTRACE_TRAMPOLINE_SYM);
3014 /* Remove from kallsyms after the perf events */
3015 ftrace_remove_trampoline_from_kallsyms(ops);
3018 arch_ftrace_trampoline_free(ops);
3021 static void ftrace_startup_enable(int command)
3023 if (saved_ftrace_func != ftrace_trace_function) {
3024 saved_ftrace_func = ftrace_trace_function;
3025 command |= FTRACE_UPDATE_TRACE_FUNC;
3028 if (!command || !ftrace_enabled)
3031 ftrace_run_update_code(command);
3034 static void ftrace_startup_all(int command)
3036 update_all_ops = true;
3037 ftrace_startup_enable(command);
3038 update_all_ops = false;
3041 int ftrace_startup(struct ftrace_ops *ops, int command)
3045 if (unlikely(ftrace_disabled))
3048 ret = __register_ftrace_function(ops);
3055 * Note that ftrace probes uses this to start up
3056 * and modify functions it will probe. But we still
3057 * set the ADDING flag for modification, as probes
3058 * do not have trampolines. If they add them in the
3059 * future, then the probes will need to distinguish
3060 * between adding and updating probes.
3062 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
3064 ret = ftrace_hash_ipmodify_enable(ops);
3066 /* Rollback registration process */
3067 __unregister_ftrace_function(ops);
3069 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
3070 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
3071 ftrace_trampoline_free(ops);
3075 if (ftrace_hash_rec_enable(ops, 1))
3076 command |= FTRACE_UPDATE_CALLS;
3078 ftrace_startup_enable(command);
3081 * If ftrace is in an undefined state, we just remove ops from list
3082 * to prevent the NULL pointer, instead of totally rolling it back and
3083 * free trampoline, because those actions could cause further damage.
3085 if (unlikely(ftrace_disabled)) {
3086 __unregister_ftrace_function(ops);
3090 ops->flags &= ~FTRACE_OPS_FL_ADDING;
3095 int ftrace_shutdown(struct ftrace_ops *ops, int command)
3099 if (unlikely(ftrace_disabled))
3102 ret = __unregister_ftrace_function(ops);
3108 * Just warn in case of unbalance, no need to kill ftrace, it's not
3109 * critical but the ftrace_call callers may be never nopped again after
3110 * further ftrace uses.
3112 WARN_ON_ONCE(ftrace_start_up < 0);
3114 /* Disabling ipmodify never fails */
3115 ftrace_hash_ipmodify_disable(ops);
3117 if (ftrace_hash_rec_disable(ops, 1))
3118 command |= FTRACE_UPDATE_CALLS;
3120 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
3122 if (saved_ftrace_func != ftrace_trace_function) {
3123 saved_ftrace_func = ftrace_trace_function;
3124 command |= FTRACE_UPDATE_TRACE_FUNC;
3127 if (!command || !ftrace_enabled)
3131 * If the ops uses a trampoline, then it needs to be
3132 * tested first on update.
3134 ops->flags |= FTRACE_OPS_FL_REMOVING;
3137 /* The trampoline logic checks the old hashes */
3138 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
3139 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
3141 ftrace_run_update_code(command);
3144 * If there's no more ops registered with ftrace, run a
3145 * sanity check to make sure all rec flags are cleared.
3147 if (rcu_dereference_protected(ftrace_ops_list,
3148 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
3149 struct ftrace_page *pg;
3150 struct dyn_ftrace *rec;
3152 do_for_each_ftrace_rec(pg, rec) {
3153 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_NOCLEAR_FLAGS))
3154 pr_warn(" %pS flags:%lx\n",
3155 (void *)rec->ip, rec->flags);
3156 } while_for_each_ftrace_rec();
3159 ops->old_hash.filter_hash = NULL;
3160 ops->old_hash.notrace_hash = NULL;
3163 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
3167 * Dynamic ops may be freed, we must make sure that all
3168 * callers are done before leaving this function.
3170 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
3172 * We need to do a hard force of sched synchronization.
3173 * This is because we use preempt_disable() to do RCU, but
3174 * the function tracers can be called where RCU is not watching
3175 * (like before user_exit()). We can not rely on the RCU
3176 * infrastructure to do the synchronization, thus we must do it
3179 synchronize_rcu_tasks_rude();
3182 * When the kernel is preemptive, tasks can be preempted
3183 * while on a ftrace trampoline. Just scheduling a task on
3184 * a CPU is not good enough to flush them. Calling
3185 * synchronize_rcu_tasks() will wait for those tasks to
3186 * execute and either schedule voluntarily or enter user space.
3188 if (IS_ENABLED(CONFIG_PREEMPTION))
3189 synchronize_rcu_tasks();
3191 ftrace_trampoline_free(ops);
3197 static u64 ftrace_update_time;
3198 unsigned long ftrace_update_tot_cnt;
3199 unsigned long ftrace_number_of_pages;
3200 unsigned long ftrace_number_of_groups;
3202 static inline int ops_traces_mod(struct ftrace_ops *ops)
3205 * Filter_hash being empty will default to trace module.
3206 * But notrace hash requires a test of individual module functions.
3208 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
3209 ftrace_hash_empty(ops->func_hash->notrace_hash);
3212 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
3214 bool init_nop = ftrace_need_init_nop();
3215 struct ftrace_page *pg;
3216 struct dyn_ftrace *p;
3218 unsigned long update_cnt = 0;
3219 unsigned long rec_flags = 0;
3222 start = ftrace_now(raw_smp_processor_id());
3225 * When a module is loaded, this function is called to convert
3226 * the calls to mcount in its text to nops, and also to create
3227 * an entry in the ftrace data. Now, if ftrace is activated
3228 * after this call, but before the module sets its text to
3229 * read-only, the modification of enabling ftrace can fail if
3230 * the read-only is done while ftrace is converting the calls.
3231 * To prevent this, the module's records are set as disabled
3232 * and will be enabled after the call to set the module's text
3236 rec_flags |= FTRACE_FL_DISABLED;
3238 for (pg = new_pgs; pg; pg = pg->next) {
3240 for (i = 0; i < pg->index; i++) {
3242 /* If something went wrong, bail without enabling anything */
3243 if (unlikely(ftrace_disabled))
3246 p = &pg->records[i];
3247 p->flags = rec_flags;
3250 * Do the initial record conversion from mcount jump
3251 * to the NOP instructions.
3253 if (init_nop && !ftrace_nop_initialize(mod, p))
3260 stop = ftrace_now(raw_smp_processor_id());
3261 ftrace_update_time = stop - start;
3262 ftrace_update_tot_cnt += update_cnt;
3267 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3273 if (WARN_ON(!count))
3276 /* We want to fill as much as possible, with no empty pages */
3277 pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE);
3278 order = fls(pages) - 1;
3281 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3284 /* if we can't allocate this size, try something smaller */
3291 ftrace_number_of_pages += 1 << order;
3292 ftrace_number_of_groups++;
3294 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3303 static struct ftrace_page *
3304 ftrace_allocate_pages(unsigned long num_to_init)
3306 struct ftrace_page *start_pg;
3307 struct ftrace_page *pg;
3313 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3318 * Try to allocate as much as possible in one continues
3319 * location that fills in all of the space. We want to
3320 * waste as little space as possible.
3323 cnt = ftrace_allocate_records(pg, num_to_init);
3331 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3344 free_pages((unsigned long)pg->records, pg->order);
3345 ftrace_number_of_pages -= 1 << pg->order;
3347 start_pg = pg->next;
3350 ftrace_number_of_groups--;
3352 pr_info("ftrace: FAILED to allocate memory for functions\n");
3356 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3358 struct ftrace_iterator {
3362 struct ftrace_page *pg;
3363 struct dyn_ftrace *func;
3364 struct ftrace_func_probe *probe;
3365 struct ftrace_func_entry *probe_entry;
3366 struct trace_parser parser;
3367 struct ftrace_hash *hash;
3368 struct ftrace_ops *ops;
3369 struct trace_array *tr;
3370 struct list_head *mod_list;
3377 t_probe_next(struct seq_file *m, loff_t *pos)
3379 struct ftrace_iterator *iter = m->private;
3380 struct trace_array *tr = iter->ops->private;
3381 struct list_head *func_probes;
3382 struct ftrace_hash *hash;
3383 struct list_head *next;
3384 struct hlist_node *hnd = NULL;
3385 struct hlist_head *hhd;
3394 func_probes = &tr->func_probes;
3395 if (list_empty(func_probes))
3399 next = func_probes->next;
3400 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3403 if (iter->probe_entry)
3404 hnd = &iter->probe_entry->hlist;
3406 hash = iter->probe->ops.func_hash->filter_hash;
3409 * A probe being registered may temporarily have an empty hash
3410 * and it's at the end of the func_probes list.
3412 if (!hash || hash == EMPTY_HASH)
3415 size = 1 << hash->size_bits;
3418 if (iter->pidx >= size) {
3419 if (iter->probe->list.next == func_probes)
3421 next = iter->probe->list.next;
3422 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3423 hash = iter->probe->ops.func_hash->filter_hash;
3424 size = 1 << hash->size_bits;
3428 hhd = &hash->buckets[iter->pidx];
3430 if (hlist_empty(hhd)) {
3446 if (WARN_ON_ONCE(!hnd))
3449 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3454 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3456 struct ftrace_iterator *iter = m->private;
3460 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3463 if (iter->mod_pos > *pos)
3467 iter->probe_entry = NULL;
3469 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3470 p = t_probe_next(m, &l);
3477 /* Only set this if we have an item */
3478 iter->flags |= FTRACE_ITER_PROBE;
3484 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3486 struct ftrace_func_entry *probe_entry;
3487 struct ftrace_probe_ops *probe_ops;
3488 struct ftrace_func_probe *probe;
3490 probe = iter->probe;
3491 probe_entry = iter->probe_entry;
3493 if (WARN_ON_ONCE(!probe || !probe_entry))
3496 probe_ops = probe->probe_ops;
3498 if (probe_ops->print)
3499 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3501 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3502 (void *)probe_ops->func);
3508 t_mod_next(struct seq_file *m, loff_t *pos)
3510 struct ftrace_iterator *iter = m->private;
3511 struct trace_array *tr = iter->tr;
3516 iter->mod_list = iter->mod_list->next;
3518 if (iter->mod_list == &tr->mod_trace ||
3519 iter->mod_list == &tr->mod_notrace) {
3520 iter->flags &= ~FTRACE_ITER_MOD;
3524 iter->mod_pos = *pos;
3529 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3531 struct ftrace_iterator *iter = m->private;
3535 if (iter->func_pos > *pos)
3538 iter->mod_pos = iter->func_pos;
3540 /* probes are only available if tr is set */
3544 for (l = 0; l <= (*pos - iter->func_pos); ) {
3545 p = t_mod_next(m, &l);
3550 iter->flags &= ~FTRACE_ITER_MOD;
3551 return t_probe_start(m, pos);
3554 /* Only set this if we have an item */
3555 iter->flags |= FTRACE_ITER_MOD;
3561 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3563 struct ftrace_mod_load *ftrace_mod;
3564 struct trace_array *tr = iter->tr;
3566 if (WARN_ON_ONCE(!iter->mod_list) ||
3567 iter->mod_list == &tr->mod_trace ||
3568 iter->mod_list == &tr->mod_notrace)
3571 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3573 if (ftrace_mod->func)
3574 seq_printf(m, "%s", ftrace_mod->func);
3578 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3584 t_func_next(struct seq_file *m, loff_t *pos)
3586 struct ftrace_iterator *iter = m->private;
3587 struct dyn_ftrace *rec = NULL;
3592 if (iter->idx >= iter->pg->index) {
3593 if (iter->pg->next) {
3594 iter->pg = iter->pg->next;
3599 rec = &iter->pg->records[iter->idx++];
3600 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3601 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3603 ((iter->flags & FTRACE_ITER_ENABLED) &&
3604 !(rec->flags & FTRACE_FL_ENABLED)) ||
3606 ((iter->flags & FTRACE_ITER_TOUCHED) &&
3607 !(rec->flags & FTRACE_FL_TOUCHED))) {
3617 iter->pos = iter->func_pos = *pos;
3624 t_next(struct seq_file *m, void *v, loff_t *pos)
3626 struct ftrace_iterator *iter = m->private;
3627 loff_t l = *pos; /* t_probe_start() must use original pos */
3630 if (unlikely(ftrace_disabled))
3633 if (iter->flags & FTRACE_ITER_PROBE)
3634 return t_probe_next(m, pos);
3636 if (iter->flags & FTRACE_ITER_MOD)
3637 return t_mod_next(m, pos);
3639 if (iter->flags & FTRACE_ITER_PRINTALL) {
3640 /* next must increment pos, and t_probe_start does not */
3642 return t_mod_start(m, &l);
3645 ret = t_func_next(m, pos);
3648 return t_mod_start(m, &l);
3653 static void reset_iter_read(struct ftrace_iterator *iter)
3657 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3660 static void *t_start(struct seq_file *m, loff_t *pos)
3662 struct ftrace_iterator *iter = m->private;
3666 mutex_lock(&ftrace_lock);
3668 if (unlikely(ftrace_disabled))
3672 * If an lseek was done, then reset and start from beginning.
3674 if (*pos < iter->pos)
3675 reset_iter_read(iter);
3678 * For set_ftrace_filter reading, if we have the filter
3679 * off, we can short cut and just print out that all
3680 * functions are enabled.
3682 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3683 ftrace_hash_empty(iter->hash)) {
3684 iter->func_pos = 1; /* Account for the message */
3686 return t_mod_start(m, pos);
3687 iter->flags |= FTRACE_ITER_PRINTALL;
3688 /* reset in case of seek/pread */
3689 iter->flags &= ~FTRACE_ITER_PROBE;
3693 if (iter->flags & FTRACE_ITER_MOD)
3694 return t_mod_start(m, pos);
3697 * Unfortunately, we need to restart at ftrace_pages_start
3698 * every time we let go of the ftrace_mutex. This is because
3699 * those pointers can change without the lock.
3701 iter->pg = ftrace_pages_start;
3703 for (l = 0; l <= *pos; ) {
3704 p = t_func_next(m, &l);
3710 return t_mod_start(m, pos);
3715 static void t_stop(struct seq_file *m, void *p)
3717 mutex_unlock(&ftrace_lock);
3721 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3726 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3727 struct dyn_ftrace *rec)
3731 ptr = arch_ftrace_trampoline_func(ops, rec);
3733 seq_printf(m, " ->%pS", ptr);
3736 #ifdef FTRACE_MCOUNT_MAX_OFFSET
3738 * Weak functions can still have an mcount/fentry that is saved in
3739 * the __mcount_loc section. These can be detected by having a
3740 * symbol offset of greater than FTRACE_MCOUNT_MAX_OFFSET, as the
3741 * symbol found by kallsyms is not the function that the mcount/fentry
3742 * is part of. The offset is much greater in these cases.
3744 * Test the record to make sure that the ip points to a valid kallsyms
3745 * and if not, mark it disabled.
3747 static int test_for_valid_rec(struct dyn_ftrace *rec)
3749 char str[KSYM_SYMBOL_LEN];
3750 unsigned long offset;
3753 ret = kallsyms_lookup(rec->ip, NULL, &offset, NULL, str);
3755 /* Weak functions can cause invalid addresses */
3756 if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) {
3757 rec->flags |= FTRACE_FL_DISABLED;
3763 static struct workqueue_struct *ftrace_check_wq __initdata;
3764 static struct work_struct ftrace_check_work __initdata;
3767 * Scan all the mcount/fentry entries to make sure they are valid.
3769 static __init void ftrace_check_work_func(struct work_struct *work)
3771 struct ftrace_page *pg;
3772 struct dyn_ftrace *rec;
3774 mutex_lock(&ftrace_lock);
3775 do_for_each_ftrace_rec(pg, rec) {
3776 test_for_valid_rec(rec);
3777 } while_for_each_ftrace_rec();
3778 mutex_unlock(&ftrace_lock);
3781 static int __init ftrace_check_for_weak_functions(void)
3783 INIT_WORK(&ftrace_check_work, ftrace_check_work_func);
3785 ftrace_check_wq = alloc_workqueue("ftrace_check_wq", WQ_UNBOUND, 0);
3787 queue_work(ftrace_check_wq, &ftrace_check_work);
3791 static int __init ftrace_check_sync(void)
3793 /* Make sure the ftrace_check updates are finished */
3794 if (ftrace_check_wq)
3795 destroy_workqueue(ftrace_check_wq);
3799 late_initcall_sync(ftrace_check_sync);
3800 subsys_initcall(ftrace_check_for_weak_functions);
3802 static int print_rec(struct seq_file *m, unsigned long ip)
3804 unsigned long offset;
3805 char str[KSYM_SYMBOL_LEN];
3809 ret = kallsyms_lookup(ip, NULL, &offset, &modname, str);
3810 /* Weak functions can cause invalid addresses */
3811 if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) {
3812 snprintf(str, KSYM_SYMBOL_LEN, "%s_%ld",
3813 FTRACE_INVALID_FUNCTION, offset);
3819 seq_printf(m, " [%s]", modname);
3820 return ret == NULL ? -1 : 0;
3823 static inline int test_for_valid_rec(struct dyn_ftrace *rec)
3828 static inline int print_rec(struct seq_file *m, unsigned long ip)
3830 seq_printf(m, "%ps", (void *)ip);
3835 static int t_show(struct seq_file *m, void *v)
3837 struct ftrace_iterator *iter = m->private;
3838 struct dyn_ftrace *rec;
3840 if (iter->flags & FTRACE_ITER_PROBE)
3841 return t_probe_show(m, iter);
3843 if (iter->flags & FTRACE_ITER_MOD)
3844 return t_mod_show(m, iter);
3846 if (iter->flags & FTRACE_ITER_PRINTALL) {
3847 if (iter->flags & FTRACE_ITER_NOTRACE)
3848 seq_puts(m, "#### no functions disabled ####\n");
3850 seq_puts(m, "#### all functions enabled ####\n");
3859 if (print_rec(m, rec->ip)) {
3860 /* This should only happen when a rec is disabled */
3861 WARN_ON_ONCE(!(rec->flags & FTRACE_FL_DISABLED));
3866 if (iter->flags & (FTRACE_ITER_ENABLED | FTRACE_ITER_TOUCHED)) {
3867 struct ftrace_ops *ops;
3869 seq_printf(m, " (%ld)%s%s%s%s",
3870 ftrace_rec_count(rec),
3871 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3872 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
3873 rec->flags & FTRACE_FL_DIRECT ? " D" : " ",
3874 rec->flags & FTRACE_FL_CALL_OPS ? " O" : " ");
3875 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3876 ops = ftrace_find_tramp_ops_any(rec);
3879 seq_printf(m, "\ttramp: %pS (%pS)",
3880 (void *)ops->trampoline,
3882 add_trampoline_func(m, ops, rec);
3883 ops = ftrace_find_tramp_ops_next(rec, ops);
3886 seq_puts(m, "\ttramp: ERROR!");
3888 add_trampoline_func(m, NULL, rec);
3890 if (rec->flags & FTRACE_FL_CALL_OPS_EN) {
3891 ops = ftrace_find_unique_ops(rec);
3893 seq_printf(m, "\tops: %pS (%pS)",
3896 seq_puts(m, "\tops: ERROR!");
3899 if (rec->flags & FTRACE_FL_DIRECT) {
3900 unsigned long direct;
3902 direct = ftrace_find_rec_direct(rec->ip);
3904 seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3913 static const struct seq_operations show_ftrace_seq_ops = {
3921 ftrace_avail_open(struct inode *inode, struct file *file)
3923 struct ftrace_iterator *iter;
3926 ret = security_locked_down(LOCKDOWN_TRACEFS);
3930 if (unlikely(ftrace_disabled))
3933 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3937 iter->pg = ftrace_pages_start;
3938 iter->ops = &global_ops;
3944 ftrace_enabled_open(struct inode *inode, struct file *file)
3946 struct ftrace_iterator *iter;
3949 * This shows us what functions are currently being
3950 * traced and by what. Not sure if we want lockdown
3951 * to hide such critical information for an admin.
3952 * Although, perhaps it can show information we don't
3953 * want people to see, but if something is tracing
3954 * something, we probably want to know about it.
3957 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3961 iter->pg = ftrace_pages_start;
3962 iter->flags = FTRACE_ITER_ENABLED;
3963 iter->ops = &global_ops;
3969 ftrace_touched_open(struct inode *inode, struct file *file)
3971 struct ftrace_iterator *iter;
3974 * This shows us what functions have ever been enabled
3975 * (traced, direct, patched, etc). Not sure if we want lockdown
3976 * to hide such critical information for an admin.
3977 * Although, perhaps it can show information we don't
3978 * want people to see, but if something had traced
3979 * something, we probably want to know about it.
3982 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3986 iter->pg = ftrace_pages_start;
3987 iter->flags = FTRACE_ITER_TOUCHED;
3988 iter->ops = &global_ops;
3994 * ftrace_regex_open - initialize function tracer filter files
3995 * @ops: The ftrace_ops that hold the hash filters
3996 * @flag: The type of filter to process
3997 * @inode: The inode, usually passed in to your open routine
3998 * @file: The file, usually passed in to your open routine
4000 * ftrace_regex_open() initializes the filter files for the
4001 * @ops. Depending on @flag it may process the filter hash or
4002 * the notrace hash of @ops. With this called from the open
4003 * routine, you can use ftrace_filter_write() for the write
4004 * routine if @flag has FTRACE_ITER_FILTER set, or
4005 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
4006 * tracing_lseek() should be used as the lseek routine, and
4007 * release must call ftrace_regex_release().
4010 ftrace_regex_open(struct ftrace_ops *ops, int flag,
4011 struct inode *inode, struct file *file)
4013 struct ftrace_iterator *iter;
4014 struct ftrace_hash *hash;
4015 struct list_head *mod_head;
4016 struct trace_array *tr = ops->private;
4019 ftrace_ops_init(ops);
4021 if (unlikely(ftrace_disabled))
4024 if (tracing_check_open_get_tr(tr))
4027 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4031 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
4038 mutex_lock(&ops->func_hash->regex_lock);
4040 if (flag & FTRACE_ITER_NOTRACE) {
4041 hash = ops->func_hash->notrace_hash;
4042 mod_head = tr ? &tr->mod_notrace : NULL;
4044 hash = ops->func_hash->filter_hash;
4045 mod_head = tr ? &tr->mod_trace : NULL;
4048 iter->mod_list = mod_head;
4050 if (file->f_mode & FMODE_WRITE) {
4051 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
4053 if (file->f_flags & O_TRUNC) {
4054 iter->hash = alloc_ftrace_hash(size_bits);
4055 clear_ftrace_mod_list(mod_head);
4057 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
4061 trace_parser_put(&iter->parser);
4069 if (file->f_mode & FMODE_READ) {
4070 iter->pg = ftrace_pages_start;
4072 ret = seq_open(file, &show_ftrace_seq_ops);
4074 struct seq_file *m = file->private_data;
4078 free_ftrace_hash(iter->hash);
4079 trace_parser_put(&iter->parser);
4082 file->private_data = iter;
4085 mutex_unlock(&ops->func_hash->regex_lock);
4091 trace_array_put(tr);
4098 ftrace_filter_open(struct inode *inode, struct file *file)
4100 struct ftrace_ops *ops = inode->i_private;
4102 /* Checks for tracefs lockdown */
4103 return ftrace_regex_open(ops,
4104 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
4109 ftrace_notrace_open(struct inode *inode, struct file *file)
4111 struct ftrace_ops *ops = inode->i_private;
4113 /* Checks for tracefs lockdown */
4114 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
4118 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
4119 struct ftrace_glob {
4126 * If symbols in an architecture don't correspond exactly to the user-visible
4127 * name of what they represent, it is possible to define this function to
4128 * perform the necessary adjustments.
4130 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
4135 static int ftrace_match(char *str, struct ftrace_glob *g)
4140 str = arch_ftrace_match_adjust(str, g->search);
4144 if (strcmp(str, g->search) == 0)
4147 case MATCH_FRONT_ONLY:
4148 if (strncmp(str, g->search, g->len) == 0)
4151 case MATCH_MIDDLE_ONLY:
4152 if (strstr(str, g->search))
4155 case MATCH_END_ONLY:
4157 if (slen >= g->len &&
4158 memcmp(str + slen - g->len, g->search, g->len) == 0)
4162 if (glob_match(g->search, str))
4171 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
4173 struct ftrace_func_entry *entry;
4176 entry = ftrace_lookup_ip(hash, rec->ip);
4178 /* Do nothing if it doesn't exist */
4182 free_hash_entry(hash, entry);
4184 /* Do nothing if it exists */
4188 ret = add_hash_entry(hash, rec->ip);
4194 add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
4197 long index = simple_strtoul(func_g->search, NULL, 0);
4198 struct ftrace_page *pg;
4199 struct dyn_ftrace *rec;
4201 /* The index starts at 1 */
4205 do_for_each_ftrace_rec(pg, rec) {
4206 if (pg->index <= index) {
4208 /* this is a double loop, break goes to the next page */
4211 rec = &pg->records[index];
4212 enter_record(hash, rec, clear_filter);
4214 } while_for_each_ftrace_rec();
4218 #ifdef FTRACE_MCOUNT_MAX_OFFSET
4219 static int lookup_ip(unsigned long ip, char **modname, char *str)
4221 unsigned long offset;
4223 kallsyms_lookup(ip, NULL, &offset, modname, str);
4224 if (offset > FTRACE_MCOUNT_MAX_OFFSET)
4229 static int lookup_ip(unsigned long ip, char **modname, char *str)
4231 kallsyms_lookup(ip, NULL, NULL, modname, str);
4237 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
4238 struct ftrace_glob *mod_g, int exclude_mod)
4240 char str[KSYM_SYMBOL_LEN];
4243 if (lookup_ip(rec->ip, &modname, str)) {
4244 /* This should only happen when a rec is disabled */
4245 WARN_ON_ONCE(system_state == SYSTEM_RUNNING &&
4246 !(rec->flags & FTRACE_FL_DISABLED));
4251 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
4253 /* blank module name to match all modules */
4255 /* blank module globbing: modname xor exclude_mod */
4256 if (!exclude_mod != !modname)
4262 * exclude_mod is set to trace everything but the given
4263 * module. If it is set and the module matches, then
4264 * return 0. If it is not set, and the module doesn't match
4265 * also return 0. Otherwise, check the function to see if
4268 if (!mod_matches == !exclude_mod)
4271 /* blank search means to match all funcs in the mod */
4276 return ftrace_match(str, func_g);
4280 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
4282 struct ftrace_page *pg;
4283 struct dyn_ftrace *rec;
4284 struct ftrace_glob func_g = { .type = MATCH_FULL };
4285 struct ftrace_glob mod_g = { .type = MATCH_FULL };
4286 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
4287 int exclude_mod = 0;
4290 int clear_filter = 0;
4293 func_g.type = filter_parse_regex(func, len, &func_g.search,
4295 func_g.len = strlen(func_g.search);
4299 mod_g.type = filter_parse_regex(mod, strlen(mod),
4300 &mod_g.search, &exclude_mod);
4301 mod_g.len = strlen(mod_g.search);
4304 mutex_lock(&ftrace_lock);
4306 if (unlikely(ftrace_disabled))
4309 if (func_g.type == MATCH_INDEX) {
4310 found = add_rec_by_index(hash, &func_g, clear_filter);
4314 do_for_each_ftrace_rec(pg, rec) {
4316 if (rec->flags & FTRACE_FL_DISABLED)
4319 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
4320 ret = enter_record(hash, rec, clear_filter);
4328 } while_for_each_ftrace_rec();
4330 mutex_unlock(&ftrace_lock);
4336 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
4338 return match_records(hash, buff, len, NULL);
4341 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4342 struct ftrace_ops_hash *old_hash)
4344 struct ftrace_ops *op;
4346 if (!ftrace_enabled)
4349 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4350 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4355 * If this is the shared global_ops filter, then we need to
4356 * check if there is another ops that shares it, is enabled.
4357 * If so, we still need to run the modify code.
4359 if (ops->func_hash != &global_ops.local_hash)
4362 do_for_each_ftrace_op(op, ftrace_ops_list) {
4363 if (op->func_hash == &global_ops.local_hash &&
4364 op->flags & FTRACE_OPS_FL_ENABLED) {
4365 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4366 /* Only need to do this once */
4369 } while_for_each_ftrace_op(op);
4372 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
4373 struct ftrace_hash **orig_hash,
4374 struct ftrace_hash *hash,
4377 struct ftrace_ops_hash old_hash_ops;
4378 struct ftrace_hash *old_hash;
4381 old_hash = *orig_hash;
4382 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4383 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4384 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4386 ftrace_ops_update_code(ops, &old_hash_ops);
4387 free_ftrace_hash_rcu(old_hash);
4392 static bool module_exists(const char *module)
4394 /* All modules have the symbol __this_module */
4395 static const char this_mod[] = "__this_module";
4396 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
4400 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
4402 if (n > sizeof(modname) - 1)
4405 val = module_kallsyms_lookup_name(modname);
4409 static int cache_mod(struct trace_array *tr,
4410 const char *func, char *module, int enable)
4412 struct ftrace_mod_load *ftrace_mod, *n;
4413 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4416 mutex_lock(&ftrace_lock);
4418 /* We do not cache inverse filters */
4419 if (func[0] == '!') {
4423 /* Look to remove this hash */
4424 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4425 if (strcmp(ftrace_mod->module, module) != 0)
4428 /* no func matches all */
4429 if (strcmp(func, "*") == 0 ||
4430 (ftrace_mod->func &&
4431 strcmp(ftrace_mod->func, func) == 0)) {
4433 free_ftrace_mod(ftrace_mod);
4441 /* We only care about modules that have not been loaded yet */
4442 if (module_exists(module))
4445 /* Save this string off, and execute it when the module is loaded */
4446 ret = ftrace_add_mod(tr, func, module, enable);
4448 mutex_unlock(&ftrace_lock);
4454 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4455 int reset, int enable);
4457 #ifdef CONFIG_MODULES
4458 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4459 char *mod, bool enable)
4461 struct ftrace_mod_load *ftrace_mod, *n;
4462 struct ftrace_hash **orig_hash, *new_hash;
4463 LIST_HEAD(process_mods);
4466 mutex_lock(&ops->func_hash->regex_lock);
4469 orig_hash = &ops->func_hash->filter_hash;
4471 orig_hash = &ops->func_hash->notrace_hash;
4473 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4476 goto out; /* warn? */
4478 mutex_lock(&ftrace_lock);
4480 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4482 if (strcmp(ftrace_mod->module, mod) != 0)
4485 if (ftrace_mod->func)
4486 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4488 func = kstrdup("*", GFP_KERNEL);
4490 if (!func) /* warn? */
4493 list_move(&ftrace_mod->list, &process_mods);
4495 /* Use the newly allocated func, as it may be "*" */
4496 kfree(ftrace_mod->func);
4497 ftrace_mod->func = func;
4500 mutex_unlock(&ftrace_lock);
4502 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4504 func = ftrace_mod->func;
4506 /* Grabs ftrace_lock, which is why we have this extra step */
4507 match_records(new_hash, func, strlen(func), mod);
4508 free_ftrace_mod(ftrace_mod);
4511 if (enable && list_empty(head))
4512 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4514 mutex_lock(&ftrace_lock);
4516 ftrace_hash_move_and_update_ops(ops, orig_hash,
4518 mutex_unlock(&ftrace_lock);
4521 mutex_unlock(&ops->func_hash->regex_lock);
4523 free_ftrace_hash(new_hash);
4526 static void process_cached_mods(const char *mod_name)
4528 struct trace_array *tr;
4531 mod = kstrdup(mod_name, GFP_KERNEL);
4535 mutex_lock(&trace_types_lock);
4536 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4537 if (!list_empty(&tr->mod_trace))
4538 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4539 if (!list_empty(&tr->mod_notrace))
4540 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4542 mutex_unlock(&trace_types_lock);
4549 * We register the module command as a template to show others how
4550 * to register the a command as well.
4554 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4555 char *func_orig, char *cmd, char *module, int enable)
4560 /* match_records() modifies func, and we need the original */
4561 func = kstrdup(func_orig, GFP_KERNEL);
4566 * cmd == 'mod' because we only registered this func
4567 * for the 'mod' ftrace_func_command.
4568 * But if you register one func with multiple commands,
4569 * you can tell which command was used by the cmd
4572 ret = match_records(hash, func, strlen(func), module);
4576 return cache_mod(tr, func_orig, module, enable);
4582 static struct ftrace_func_command ftrace_mod_cmd = {
4584 .func = ftrace_mod_callback,
4587 static int __init ftrace_mod_cmd_init(void)
4589 return register_ftrace_command(&ftrace_mod_cmd);
4591 core_initcall(ftrace_mod_cmd_init);
4593 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4594 struct ftrace_ops *op, struct ftrace_regs *fregs)
4596 struct ftrace_probe_ops *probe_ops;
4597 struct ftrace_func_probe *probe;
4599 probe = container_of(op, struct ftrace_func_probe, ops);
4600 probe_ops = probe->probe_ops;
4603 * Disable preemption for these calls to prevent a RCU grace
4604 * period. This syncs the hash iteration and freeing of items
4605 * on the hash. rcu_read_lock is too dangerous here.
4607 preempt_disable_notrace();
4608 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4609 preempt_enable_notrace();
4612 struct ftrace_func_map {
4613 struct ftrace_func_entry entry;
4617 struct ftrace_func_mapper {
4618 struct ftrace_hash hash;
4622 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4624 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4626 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4628 struct ftrace_hash *hash;
4631 * The mapper is simply a ftrace_hash, but since the entries
4632 * in the hash are not ftrace_func_entry type, we define it
4633 * as a separate structure.
4635 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4636 return (struct ftrace_func_mapper *)hash;
4640 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4641 * @mapper: The mapper that has the ip maps
4642 * @ip: the instruction pointer to find the data for
4644 * Returns the data mapped to @ip if found otherwise NULL. The return
4645 * is actually the address of the mapper data pointer. The address is
4646 * returned for use cases where the data is no bigger than a long, and
4647 * the user can use the data pointer as its data instead of having to
4648 * allocate more memory for the reference.
4650 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4653 struct ftrace_func_entry *entry;
4654 struct ftrace_func_map *map;
4656 entry = ftrace_lookup_ip(&mapper->hash, ip);
4660 map = (struct ftrace_func_map *)entry;
4665 * ftrace_func_mapper_add_ip - Map some data to an ip
4666 * @mapper: The mapper that has the ip maps
4667 * @ip: The instruction pointer address to map @data to
4668 * @data: The data to map to @ip
4670 * Returns 0 on success otherwise an error.
4672 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4673 unsigned long ip, void *data)
4675 struct ftrace_func_entry *entry;
4676 struct ftrace_func_map *map;
4678 entry = ftrace_lookup_ip(&mapper->hash, ip);
4682 map = kmalloc(sizeof(*map), GFP_KERNEL);
4689 __add_hash_entry(&mapper->hash, &map->entry);
4695 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4696 * @mapper: The mapper that has the ip maps
4697 * @ip: The instruction pointer address to remove the data from
4699 * Returns the data if it is found, otherwise NULL.
4700 * Note, if the data pointer is used as the data itself, (see
4701 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4702 * if the data pointer was set to zero.
4704 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4707 struct ftrace_func_entry *entry;
4708 struct ftrace_func_map *map;
4711 entry = ftrace_lookup_ip(&mapper->hash, ip);
4715 map = (struct ftrace_func_map *)entry;
4718 remove_hash_entry(&mapper->hash, entry);
4725 * free_ftrace_func_mapper - free a mapping of ips and data
4726 * @mapper: The mapper that has the ip maps
4727 * @free_func: A function to be called on each data item.
4729 * This is used to free the function mapper. The @free_func is optional
4730 * and can be used if the data needs to be freed as well.
4732 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4733 ftrace_mapper_func free_func)
4735 struct ftrace_func_entry *entry;
4736 struct ftrace_func_map *map;
4737 struct hlist_head *hhd;
4743 if (free_func && mapper->hash.count) {
4744 size = 1 << mapper->hash.size_bits;
4745 for (i = 0; i < size; i++) {
4746 hhd = &mapper->hash.buckets[i];
4747 hlist_for_each_entry(entry, hhd, hlist) {
4748 map = (struct ftrace_func_map *)entry;
4753 free_ftrace_hash(&mapper->hash);
4756 static void release_probe(struct ftrace_func_probe *probe)
4758 struct ftrace_probe_ops *probe_ops;
4760 mutex_lock(&ftrace_lock);
4762 WARN_ON(probe->ref <= 0);
4764 /* Subtract the ref that was used to protect this instance */
4768 probe_ops = probe->probe_ops;
4770 * Sending zero as ip tells probe_ops to free
4771 * the probe->data itself
4773 if (probe_ops->free)
4774 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4775 list_del(&probe->list);
4778 mutex_unlock(&ftrace_lock);
4781 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4784 * Add one ref to keep it from being freed when releasing the
4785 * ftrace_lock mutex.
4791 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4792 struct ftrace_probe_ops *probe_ops,
4795 struct ftrace_func_probe *probe = NULL, *iter;
4796 struct ftrace_func_entry *entry;
4797 struct ftrace_hash **orig_hash;
4798 struct ftrace_hash *old_hash;
4799 struct ftrace_hash *hash;
4808 /* We do not support '!' for function probes */
4809 if (WARN_ON(glob[0] == '!'))
4813 mutex_lock(&ftrace_lock);
4814 /* Check if the probe_ops is already registered */
4815 list_for_each_entry(iter, &tr->func_probes, list) {
4816 if (iter->probe_ops == probe_ops) {
4822 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4824 mutex_unlock(&ftrace_lock);
4827 probe->probe_ops = probe_ops;
4828 probe->ops.func = function_trace_probe_call;
4830 ftrace_ops_init(&probe->ops);
4831 list_add(&probe->list, &tr->func_probes);
4834 acquire_probe_locked(probe);
4836 mutex_unlock(&ftrace_lock);
4839 * Note, there's a small window here that the func_hash->filter_hash
4840 * may be NULL or empty. Need to be careful when reading the loop.
4842 mutex_lock(&probe->ops.func_hash->regex_lock);
4844 orig_hash = &probe->ops.func_hash->filter_hash;
4845 old_hash = *orig_hash;
4846 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4853 ret = ftrace_match_records(hash, glob, strlen(glob));
4855 /* Nothing found? */
4862 size = 1 << hash->size_bits;
4863 for (i = 0; i < size; i++) {
4864 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4865 if (ftrace_lookup_ip(old_hash, entry->ip))
4868 * The caller might want to do something special
4869 * for each function we find. We call the callback
4870 * to give the caller an opportunity to do so.
4872 if (probe_ops->init) {
4873 ret = probe_ops->init(probe_ops, tr,
4877 if (probe_ops->free && count)
4878 probe_ops->free(probe_ops, tr,
4888 mutex_lock(&ftrace_lock);
4891 /* Nothing was added? */
4896 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4901 /* One ref for each new function traced */
4902 probe->ref += count;
4904 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4905 ret = ftrace_startup(&probe->ops, 0);
4908 mutex_unlock(&ftrace_lock);
4913 mutex_unlock(&probe->ops.func_hash->regex_lock);
4914 free_ftrace_hash(hash);
4916 release_probe(probe);
4921 if (!probe_ops->free || !count)
4924 /* Failed to do the move, need to call the free functions */
4925 for (i = 0; i < size; i++) {
4926 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4927 if (ftrace_lookup_ip(old_hash, entry->ip))
4929 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4936 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4937 struct ftrace_probe_ops *probe_ops)
4939 struct ftrace_func_probe *probe = NULL, *iter;
4940 struct ftrace_ops_hash old_hash_ops;
4941 struct ftrace_func_entry *entry;
4942 struct ftrace_glob func_g;
4943 struct ftrace_hash **orig_hash;
4944 struct ftrace_hash *old_hash;
4945 struct ftrace_hash *hash = NULL;
4946 struct hlist_node *tmp;
4947 struct hlist_head hhd;
4948 char str[KSYM_SYMBOL_LEN];
4950 int i, ret = -ENODEV;
4953 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4954 func_g.search = NULL;
4958 func_g.type = filter_parse_regex(glob, strlen(glob),
4959 &func_g.search, ¬);
4960 func_g.len = strlen(func_g.search);
4962 /* we do not support '!' for function probes */
4967 mutex_lock(&ftrace_lock);
4968 /* Check if the probe_ops is already registered */
4969 list_for_each_entry(iter, &tr->func_probes, list) {
4970 if (iter->probe_ops == probe_ops) {
4976 goto err_unlock_ftrace;
4979 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4980 goto err_unlock_ftrace;
4982 acquire_probe_locked(probe);
4984 mutex_unlock(&ftrace_lock);
4986 mutex_lock(&probe->ops.func_hash->regex_lock);
4988 orig_hash = &probe->ops.func_hash->filter_hash;
4989 old_hash = *orig_hash;
4991 if (ftrace_hash_empty(old_hash))
4994 old_hash_ops.filter_hash = old_hash;
4995 /* Probes only have filters */
4996 old_hash_ops.notrace_hash = NULL;
4999 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
5003 INIT_HLIST_HEAD(&hhd);
5005 size = 1 << hash->size_bits;
5006 for (i = 0; i < size; i++) {
5007 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
5009 if (func_g.search) {
5010 kallsyms_lookup(entry->ip, NULL, NULL,
5012 if (!ftrace_match(str, &func_g))
5016 remove_hash_entry(hash, entry);
5017 hlist_add_head(&entry->hlist, &hhd);
5021 /* Nothing found? */
5027 mutex_lock(&ftrace_lock);
5029 WARN_ON(probe->ref < count);
5031 probe->ref -= count;
5033 if (ftrace_hash_empty(hash))
5034 ftrace_shutdown(&probe->ops, 0);
5036 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
5039 /* still need to update the function call sites */
5040 if (ftrace_enabled && !ftrace_hash_empty(hash))
5041 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
5045 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
5046 hlist_del(&entry->hlist);
5047 if (probe_ops->free)
5048 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
5051 mutex_unlock(&ftrace_lock);
5054 mutex_unlock(&probe->ops.func_hash->regex_lock);
5055 free_ftrace_hash(hash);
5057 release_probe(probe);
5062 mutex_unlock(&ftrace_lock);
5066 void clear_ftrace_function_probes(struct trace_array *tr)
5068 struct ftrace_func_probe *probe, *n;
5070 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
5071 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
5074 static LIST_HEAD(ftrace_commands);
5075 static DEFINE_MUTEX(ftrace_cmd_mutex);
5078 * Currently we only register ftrace commands from __init, so mark this
5081 __init int register_ftrace_command(struct ftrace_func_command *cmd)
5083 struct ftrace_func_command *p;
5086 mutex_lock(&ftrace_cmd_mutex);
5087 list_for_each_entry(p, &ftrace_commands, list) {
5088 if (strcmp(cmd->name, p->name) == 0) {
5093 list_add(&cmd->list, &ftrace_commands);
5095 mutex_unlock(&ftrace_cmd_mutex);
5101 * Currently we only unregister ftrace commands from __init, so mark
5104 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
5106 struct ftrace_func_command *p, *n;
5109 mutex_lock(&ftrace_cmd_mutex);
5110 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
5111 if (strcmp(cmd->name, p->name) == 0) {
5113 list_del_init(&p->list);
5118 mutex_unlock(&ftrace_cmd_mutex);
5123 static int ftrace_process_regex(struct ftrace_iterator *iter,
5124 char *buff, int len, int enable)
5126 struct ftrace_hash *hash = iter->hash;
5127 struct trace_array *tr = iter->ops->private;
5128 char *func, *command, *next = buff;
5129 struct ftrace_func_command *p;
5132 func = strsep(&next, ":");
5135 ret = ftrace_match_records(hash, func, len);
5145 command = strsep(&next, ":");
5147 mutex_lock(&ftrace_cmd_mutex);
5148 list_for_each_entry(p, &ftrace_commands, list) {
5149 if (strcmp(p->name, command) == 0) {
5150 ret = p->func(tr, hash, func, command, next, enable);
5155 mutex_unlock(&ftrace_cmd_mutex);
5161 ftrace_regex_write(struct file *file, const char __user *ubuf,
5162 size_t cnt, loff_t *ppos, int enable)
5164 struct ftrace_iterator *iter;
5165 struct trace_parser *parser;
5171 if (file->f_mode & FMODE_READ) {
5172 struct seq_file *m = file->private_data;
5175 iter = file->private_data;
5177 if (unlikely(ftrace_disabled))
5180 /* iter->hash is a local copy, so we don't need regex_lock */
5182 parser = &iter->parser;
5183 read = trace_get_user(parser, ubuf, cnt, ppos);
5185 if (read >= 0 && trace_parser_loaded(parser) &&
5186 !trace_parser_cont(parser)) {
5187 ret = ftrace_process_regex(iter, parser->buffer,
5188 parser->idx, enable);
5189 trace_parser_clear(parser);
5200 ftrace_filter_write(struct file *file, const char __user *ubuf,
5201 size_t cnt, loff_t *ppos)
5203 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
5207 ftrace_notrace_write(struct file *file, const char __user *ubuf,
5208 size_t cnt, loff_t *ppos)
5210 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
5214 __ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
5216 struct ftrace_func_entry *entry;
5218 ip = ftrace_location(ip);
5223 entry = ftrace_lookup_ip(hash, ip);
5226 free_hash_entry(hash, entry);
5230 return add_hash_entry(hash, ip);
5234 ftrace_match_addr(struct ftrace_hash *hash, unsigned long *ips,
5235 unsigned int cnt, int remove)
5240 for (i = 0; i < cnt; i++) {
5241 err = __ftrace_match_addr(hash, ips[i], remove);
5244 * This expects the @hash is a temporary hash and if this
5245 * fails the caller must free the @hash.
5254 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
5255 unsigned long *ips, unsigned int cnt,
5256 int remove, int reset, int enable)
5258 struct ftrace_hash **orig_hash;
5259 struct ftrace_hash *hash;
5262 if (unlikely(ftrace_disabled))
5265 mutex_lock(&ops->func_hash->regex_lock);
5268 orig_hash = &ops->func_hash->filter_hash;
5270 orig_hash = &ops->func_hash->notrace_hash;
5273 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5275 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
5279 goto out_regex_unlock;
5282 if (buf && !ftrace_match_records(hash, buf, len)) {
5284 goto out_regex_unlock;
5287 ret = ftrace_match_addr(hash, ips, cnt, remove);
5289 goto out_regex_unlock;
5292 mutex_lock(&ftrace_lock);
5293 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
5294 mutex_unlock(&ftrace_lock);
5297 mutex_unlock(&ops->func_hash->regex_lock);
5299 free_ftrace_hash(hash);
5304 ftrace_set_addr(struct ftrace_ops *ops, unsigned long *ips, unsigned int cnt,
5305 int remove, int reset, int enable)
5307 return ftrace_set_hash(ops, NULL, 0, ips, cnt, remove, reset, enable);
5310 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
5312 struct ftrace_direct_func {
5313 struct list_head next;
5318 static LIST_HEAD(ftrace_direct_funcs);
5320 static int register_ftrace_function_nolock(struct ftrace_ops *ops);
5322 #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_ARGS)
5324 static int check_direct_multi(struct ftrace_ops *ops)
5326 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5328 if ((ops->flags & MULTI_FLAGS) != MULTI_FLAGS)
5333 static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long addr)
5335 struct ftrace_func_entry *entry, *del;
5338 size = 1 << hash->size_bits;
5339 for (i = 0; i < size; i++) {
5340 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5341 del = __ftrace_lookup_ip(direct_functions, entry->ip);
5342 if (del && del->direct == addr) {
5343 remove_hash_entry(direct_functions, del);
5351 * register_ftrace_direct - Call a custom trampoline directly
5352 * for multiple functions registered in @ops
5353 * @ops: The address of the struct ftrace_ops object
5354 * @addr: The address of the trampoline to call at @ops functions
5356 * This is used to connect a direct calls to @addr from the nop locations
5357 * of the functions registered in @ops (with by ftrace_set_filter_ip
5360 * The location that it calls (@addr) must be able to handle a direct call,
5361 * and save the parameters of the function being traced, and restore them
5362 * (or inject new ones if needed), before returning.
5366 * -EINVAL - The @ops object was already registered with this call or
5367 * when there are no functions in @ops object.
5368 * -EBUSY - Another direct function is already attached (there can be only one)
5369 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5370 * -ENOMEM - There was an allocation failure.
5372 int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
5374 struct ftrace_hash *hash, *free_hash = NULL;
5375 struct ftrace_func_entry *entry, *new;
5376 int err = -EBUSY, size, i;
5378 if (ops->func || ops->trampoline)
5380 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5382 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5385 hash = ops->func_hash->filter_hash;
5386 if (ftrace_hash_empty(hash))
5389 mutex_lock(&direct_mutex);
5391 /* Make sure requested entries are not already registered.. */
5392 size = 1 << hash->size_bits;
5393 for (i = 0; i < size; i++) {
5394 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5395 if (ftrace_find_rec_direct(entry->ip))
5400 /* ... and insert them to direct_functions hash. */
5402 for (i = 0; i < size; i++) {
5403 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5404 new = ftrace_add_rec_direct(entry->ip, addr, &free_hash);
5407 entry->direct = addr;
5411 ops->func = call_direct_funcs;
5412 ops->flags = MULTI_FLAGS;
5413 ops->trampoline = FTRACE_REGS_ADDR;
5414 ops->direct_call = addr;
5416 err = register_ftrace_function_nolock(ops);
5420 remove_direct_functions_hash(hash, addr);
5423 mutex_unlock(&direct_mutex);
5426 synchronize_rcu_tasks();
5427 free_ftrace_hash(free_hash);
5431 EXPORT_SYMBOL_GPL(register_ftrace_direct);
5434 * unregister_ftrace_direct - Remove calls to custom trampoline
5435 * previously registered by register_ftrace_direct for @ops object.
5436 * @ops: The address of the struct ftrace_ops object
5438 * This is used to remove a direct calls to @addr from the nop locations
5439 * of the functions registered in @ops (with by ftrace_set_filter_ip
5444 * -EINVAL - The @ops object was not properly registered.
5446 int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
5449 struct ftrace_hash *hash = ops->func_hash->filter_hash;
5452 if (check_direct_multi(ops))
5454 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5457 mutex_lock(&direct_mutex);
5458 err = unregister_ftrace_function(ops);
5459 remove_direct_functions_hash(hash, addr);
5460 mutex_unlock(&direct_mutex);
5462 /* cleanup for possible another register call */
5464 ops->trampoline = 0;
5467 ftrace_free_filter(ops);
5470 EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
5473 __modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
5475 struct ftrace_hash *hash;
5476 struct ftrace_func_entry *entry, *iter;
5477 static struct ftrace_ops tmp_ops = {
5478 .func = ftrace_stub,
5479 .flags = FTRACE_OPS_FL_STUB,
5484 lockdep_assert_held_once(&direct_mutex);
5486 /* Enable the tmp_ops to have the same functions as the direct ops */
5487 ftrace_ops_init(&tmp_ops);
5488 tmp_ops.func_hash = ops->func_hash;
5489 tmp_ops.direct_call = addr;
5491 err = register_ftrace_function_nolock(&tmp_ops);
5496 * Now the ftrace_ops_list_func() is called to do the direct callers.
5497 * We can safely change the direct functions attached to each entry.
5499 mutex_lock(&ftrace_lock);
5501 hash = ops->func_hash->filter_hash;
5502 size = 1 << hash->size_bits;
5503 for (i = 0; i < size; i++) {
5504 hlist_for_each_entry(iter, &hash->buckets[i], hlist) {
5505 entry = __ftrace_lookup_ip(direct_functions, iter->ip);
5508 entry->direct = addr;
5511 /* Prevent store tearing if a trampoline concurrently accesses the value */
5512 WRITE_ONCE(ops->direct_call, addr);
5514 mutex_unlock(&ftrace_lock);
5516 /* Removing the tmp_ops will add the updated direct callers to the functions */
5517 unregister_ftrace_function(&tmp_ops);
5523 * modify_ftrace_direct_nolock - Modify an existing direct 'multi' call
5524 * to call something else
5525 * @ops: The address of the struct ftrace_ops object
5526 * @addr: The address of the new trampoline to call at @ops functions
5528 * This is used to unregister currently registered direct caller and
5529 * register new one @addr on functions registered in @ops object.
5531 * Note there's window between ftrace_shutdown and ftrace_startup calls
5532 * where there will be no callbacks called.
5534 * Caller should already have direct_mutex locked, so we don't lock
5535 * direct_mutex here.
5537 * Returns: zero on success. Non zero on error, which includes:
5538 * -EINVAL - The @ops object was not properly registered.
5540 int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr)
5542 if (check_direct_multi(ops))
5544 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5547 return __modify_ftrace_direct(ops, addr);
5549 EXPORT_SYMBOL_GPL(modify_ftrace_direct_nolock);
5552 * modify_ftrace_direct - Modify an existing direct 'multi' call
5553 * to call something else
5554 * @ops: The address of the struct ftrace_ops object
5555 * @addr: The address of the new trampoline to call at @ops functions
5557 * This is used to unregister currently registered direct caller and
5558 * register new one @addr on functions registered in @ops object.
5560 * Note there's window between ftrace_shutdown and ftrace_startup calls
5561 * where there will be no callbacks called.
5563 * Returns: zero on success. Non zero on error, which includes:
5564 * -EINVAL - The @ops object was not properly registered.
5566 int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
5570 if (check_direct_multi(ops))
5572 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5575 mutex_lock(&direct_mutex);
5576 err = __modify_ftrace_direct(ops, addr);
5577 mutex_unlock(&direct_mutex);
5580 EXPORT_SYMBOL_GPL(modify_ftrace_direct);
5581 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
5584 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
5585 * @ops - the ops to set the filter with
5586 * @ip - the address to add to or remove from the filter.
5587 * @remove - non zero to remove the ip from the filter
5588 * @reset - non zero to reset all filters before applying this filter.
5590 * Filters denote which functions should be enabled when tracing is enabled
5591 * If @ip is NULL, it fails to update filter.
5593 * This can allocate memory which must be freed before @ops can be freed,
5594 * either by removing each filtered addr or by using
5595 * ftrace_free_filter(@ops).
5597 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
5598 int remove, int reset)
5600 ftrace_ops_init(ops);
5601 return ftrace_set_addr(ops, &ip, 1, remove, reset, 1);
5603 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
5606 * ftrace_set_filter_ips - set functions to filter on in ftrace by addresses
5607 * @ops - the ops to set the filter with
5608 * @ips - the array of addresses to add to or remove from the filter.
5609 * @cnt - the number of addresses in @ips
5610 * @remove - non zero to remove ips from the filter
5611 * @reset - non zero to reset all filters before applying this filter.
5613 * Filters denote which functions should be enabled when tracing is enabled
5614 * If @ips array or any ip specified within is NULL , it fails to update filter.
5616 * This can allocate memory which must be freed before @ops can be freed,
5617 * either by removing each filtered addr or by using
5618 * ftrace_free_filter(@ops).
5620 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
5621 unsigned int cnt, int remove, int reset)
5623 ftrace_ops_init(ops);
5624 return ftrace_set_addr(ops, ips, cnt, remove, reset, 1);
5626 EXPORT_SYMBOL_GPL(ftrace_set_filter_ips);
5629 * ftrace_ops_set_global_filter - setup ops to use global filters
5630 * @ops - the ops which will use the global filters
5632 * ftrace users who need global function trace filtering should call this.
5633 * It can set the global filter only if ops were not initialized before.
5635 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
5637 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
5640 ftrace_ops_init(ops);
5641 ops->func_hash = &global_ops.local_hash;
5643 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
5646 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
5647 int reset, int enable)
5649 return ftrace_set_hash(ops, buf, len, NULL, 0, 0, reset, enable);
5653 * ftrace_set_filter - set a function to filter on in ftrace
5654 * @ops - the ops to set the filter with
5655 * @buf - the string that holds the function filter text.
5656 * @len - the length of the string.
5657 * @reset - non zero to reset all filters before applying this filter.
5659 * Filters denote which functions should be enabled when tracing is enabled.
5660 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5662 * This can allocate memory which must be freed before @ops can be freed,
5663 * either by removing each filtered addr or by using
5664 * ftrace_free_filter(@ops).
5666 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
5669 ftrace_ops_init(ops);
5670 return ftrace_set_regex(ops, buf, len, reset, 1);
5672 EXPORT_SYMBOL_GPL(ftrace_set_filter);
5675 * ftrace_set_notrace - set a function to not trace in ftrace
5676 * @ops - the ops to set the notrace filter with
5677 * @buf - the string that holds the function notrace text.
5678 * @len - the length of the string.
5679 * @reset - non zero to reset all filters before applying this filter.
5681 * Notrace Filters denote which functions should not be enabled when tracing
5682 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5685 * This can allocate memory which must be freed before @ops can be freed,
5686 * either by removing each filtered addr or by using
5687 * ftrace_free_filter(@ops).
5689 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
5692 ftrace_ops_init(ops);
5693 return ftrace_set_regex(ops, buf, len, reset, 0);
5695 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
5697 * ftrace_set_global_filter - set a function to filter on with global tracers
5698 * @buf - the string that holds the function filter text.
5699 * @len - the length of the string.
5700 * @reset - non zero to reset all filters before applying this filter.
5702 * Filters denote which functions should be enabled when tracing is enabled.
5703 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5705 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
5707 ftrace_set_regex(&global_ops, buf, len, reset, 1);
5709 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
5712 * ftrace_set_global_notrace - set a function to not trace with global tracers
5713 * @buf - the string that holds the function notrace text.
5714 * @len - the length of the string.
5715 * @reset - non zero to reset all filters before applying this filter.
5717 * Notrace Filters denote which functions should not be enabled when tracing
5718 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5721 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
5723 ftrace_set_regex(&global_ops, buf, len, reset, 0);
5725 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
5728 * command line interface to allow users to set filters on boot up.
5730 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
5731 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5732 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
5734 /* Used by function selftest to not test if filter is set */
5735 bool ftrace_filter_param __initdata;
5737 static int __init set_ftrace_notrace(char *str)
5739 ftrace_filter_param = true;
5740 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
5743 __setup("ftrace_notrace=", set_ftrace_notrace);
5745 static int __init set_ftrace_filter(char *str)
5747 ftrace_filter_param = true;
5748 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
5751 __setup("ftrace_filter=", set_ftrace_filter);
5753 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5754 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
5755 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5756 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
5758 static int __init set_graph_function(char *str)
5760 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
5763 __setup("ftrace_graph_filter=", set_graph_function);
5765 static int __init set_graph_notrace_function(char *str)
5767 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
5770 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
5772 static int __init set_graph_max_depth_function(char *str)
5776 fgraph_max_depth = simple_strtoul(str, NULL, 0);
5779 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
5781 static void __init set_ftrace_early_graph(char *buf, int enable)
5785 struct ftrace_hash *hash;
5787 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5788 if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
5792 func = strsep(&buf, ",");
5793 /* we allow only one expression at a time */
5794 ret = ftrace_graph_set_hash(hash, func);
5796 printk(KERN_DEBUG "ftrace: function %s not "
5797 "traceable\n", func);
5801 ftrace_graph_hash = hash;
5803 ftrace_graph_notrace_hash = hash;
5805 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5808 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
5812 ftrace_ops_init(ops);
5815 func = strsep(&buf, ",");
5816 ftrace_set_regex(ops, func, strlen(func), 0, enable);
5820 static void __init set_ftrace_early_filters(void)
5822 if (ftrace_filter_buf[0])
5823 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5824 if (ftrace_notrace_buf[0])
5825 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5826 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5827 if (ftrace_graph_buf[0])
5828 set_ftrace_early_graph(ftrace_graph_buf, 1);
5829 if (ftrace_graph_notrace_buf[0])
5830 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5831 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5834 int ftrace_regex_release(struct inode *inode, struct file *file)
5836 struct seq_file *m = (struct seq_file *)file->private_data;
5837 struct ftrace_iterator *iter;
5838 struct ftrace_hash **orig_hash;
5839 struct trace_parser *parser;
5842 if (file->f_mode & FMODE_READ) {
5844 seq_release(inode, file);
5846 iter = file->private_data;
5848 parser = &iter->parser;
5849 if (trace_parser_loaded(parser)) {
5850 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
5852 ftrace_process_regex(iter, parser->buffer,
5853 parser->idx, enable);
5856 trace_parser_put(parser);
5858 mutex_lock(&iter->ops->func_hash->regex_lock);
5860 if (file->f_mode & FMODE_WRITE) {
5861 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5864 orig_hash = &iter->ops->func_hash->filter_hash;
5866 if (list_empty(&iter->tr->mod_trace))
5867 iter->hash->flags &= ~FTRACE_HASH_FL_MOD;
5869 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5872 orig_hash = &iter->ops->func_hash->notrace_hash;
5874 mutex_lock(&ftrace_lock);
5875 ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5876 iter->hash, filter_hash);
5877 mutex_unlock(&ftrace_lock);
5879 /* For read only, the hash is the ops hash */
5883 mutex_unlock(&iter->ops->func_hash->regex_lock);
5884 free_ftrace_hash(iter->hash);
5886 trace_array_put(iter->tr);
5892 static const struct file_operations ftrace_avail_fops = {
5893 .open = ftrace_avail_open,
5895 .llseek = seq_lseek,
5896 .release = seq_release_private,
5899 static const struct file_operations ftrace_enabled_fops = {
5900 .open = ftrace_enabled_open,
5902 .llseek = seq_lseek,
5903 .release = seq_release_private,
5906 static const struct file_operations ftrace_touched_fops = {
5907 .open = ftrace_touched_open,
5909 .llseek = seq_lseek,
5910 .release = seq_release_private,
5913 static const struct file_operations ftrace_filter_fops = {
5914 .open = ftrace_filter_open,
5916 .write = ftrace_filter_write,
5917 .llseek = tracing_lseek,
5918 .release = ftrace_regex_release,
5921 static const struct file_operations ftrace_notrace_fops = {
5922 .open = ftrace_notrace_open,
5924 .write = ftrace_notrace_write,
5925 .llseek = tracing_lseek,
5926 .release = ftrace_regex_release,
5929 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5931 static DEFINE_MUTEX(graph_lock);
5933 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
5934 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
5936 enum graph_filter_type {
5937 GRAPH_FILTER_NOTRACE = 0,
5938 GRAPH_FILTER_FUNCTION,
5941 #define FTRACE_GRAPH_EMPTY ((void *)1)
5943 struct ftrace_graph_data {
5944 struct ftrace_hash *hash;
5945 struct ftrace_func_entry *entry;
5946 int idx; /* for hash table iteration */
5947 enum graph_filter_type type;
5948 struct ftrace_hash *new_hash;
5949 const struct seq_operations *seq_ops;
5950 struct trace_parser parser;
5954 __g_next(struct seq_file *m, loff_t *pos)
5956 struct ftrace_graph_data *fgd = m->private;
5957 struct ftrace_func_entry *entry = fgd->entry;
5958 struct hlist_head *head;
5959 int i, idx = fgd->idx;
5961 if (*pos >= fgd->hash->count)
5965 hlist_for_each_entry_continue(entry, hlist) {
5973 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5974 head = &fgd->hash->buckets[i];
5975 hlist_for_each_entry(entry, head, hlist) {
5985 g_next(struct seq_file *m, void *v, loff_t *pos)
5988 return __g_next(m, pos);
5991 static void *g_start(struct seq_file *m, loff_t *pos)
5993 struct ftrace_graph_data *fgd = m->private;
5995 mutex_lock(&graph_lock);
5997 if (fgd->type == GRAPH_FILTER_FUNCTION)
5998 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5999 lockdep_is_held(&graph_lock));
6001 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6002 lockdep_is_held(&graph_lock));
6004 /* Nothing, tell g_show to print all functions are enabled */
6005 if (ftrace_hash_empty(fgd->hash) && !*pos)
6006 return FTRACE_GRAPH_EMPTY;
6010 return __g_next(m, pos);
6013 static void g_stop(struct seq_file *m, void *p)
6015 mutex_unlock(&graph_lock);
6018 static int g_show(struct seq_file *m, void *v)
6020 struct ftrace_func_entry *entry = v;
6025 if (entry == FTRACE_GRAPH_EMPTY) {
6026 struct ftrace_graph_data *fgd = m->private;
6028 if (fgd->type == GRAPH_FILTER_FUNCTION)
6029 seq_puts(m, "#### all functions enabled ####\n");
6031 seq_puts(m, "#### no functions disabled ####\n");
6035 seq_printf(m, "%ps\n", (void *)entry->ip);
6040 static const struct seq_operations ftrace_graph_seq_ops = {
6048 __ftrace_graph_open(struct inode *inode, struct file *file,
6049 struct ftrace_graph_data *fgd)
6052 struct ftrace_hash *new_hash = NULL;
6054 ret = security_locked_down(LOCKDOWN_TRACEFS);
6058 if (file->f_mode & FMODE_WRITE) {
6059 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
6061 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
6064 if (file->f_flags & O_TRUNC)
6065 new_hash = alloc_ftrace_hash(size_bits);
6067 new_hash = alloc_and_copy_ftrace_hash(size_bits,
6075 if (file->f_mode & FMODE_READ) {
6076 ret = seq_open(file, &ftrace_graph_seq_ops);
6078 struct seq_file *m = file->private_data;
6082 free_ftrace_hash(new_hash);
6086 file->private_data = fgd;
6089 if (ret < 0 && file->f_mode & FMODE_WRITE)
6090 trace_parser_put(&fgd->parser);
6092 fgd->new_hash = new_hash;
6095 * All uses of fgd->hash must be taken with the graph_lock
6096 * held. The graph_lock is going to be released, so force
6097 * fgd->hash to be reinitialized when it is taken again.
6105 ftrace_graph_open(struct inode *inode, struct file *file)
6107 struct ftrace_graph_data *fgd;
6110 if (unlikely(ftrace_disabled))
6113 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6117 mutex_lock(&graph_lock);
6119 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
6120 lockdep_is_held(&graph_lock));
6121 fgd->type = GRAPH_FILTER_FUNCTION;
6122 fgd->seq_ops = &ftrace_graph_seq_ops;
6124 ret = __ftrace_graph_open(inode, file, fgd);
6128 mutex_unlock(&graph_lock);
6133 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
6135 struct ftrace_graph_data *fgd;
6138 if (unlikely(ftrace_disabled))
6141 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6145 mutex_lock(&graph_lock);
6147 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6148 lockdep_is_held(&graph_lock));
6149 fgd->type = GRAPH_FILTER_NOTRACE;
6150 fgd->seq_ops = &ftrace_graph_seq_ops;
6152 ret = __ftrace_graph_open(inode, file, fgd);
6156 mutex_unlock(&graph_lock);
6161 ftrace_graph_release(struct inode *inode, struct file *file)
6163 struct ftrace_graph_data *fgd;
6164 struct ftrace_hash *old_hash, *new_hash;
6165 struct trace_parser *parser;
6168 if (file->f_mode & FMODE_READ) {
6169 struct seq_file *m = file->private_data;
6172 seq_release(inode, file);
6174 fgd = file->private_data;
6178 if (file->f_mode & FMODE_WRITE) {
6180 parser = &fgd->parser;
6182 if (trace_parser_loaded((parser))) {
6183 ret = ftrace_graph_set_hash(fgd->new_hash,
6187 trace_parser_put(parser);
6189 new_hash = __ftrace_hash_move(fgd->new_hash);
6195 mutex_lock(&graph_lock);
6197 if (fgd->type == GRAPH_FILTER_FUNCTION) {
6198 old_hash = rcu_dereference_protected(ftrace_graph_hash,
6199 lockdep_is_held(&graph_lock));
6200 rcu_assign_pointer(ftrace_graph_hash, new_hash);
6202 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6203 lockdep_is_held(&graph_lock));
6204 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
6207 mutex_unlock(&graph_lock);
6210 * We need to do a hard force of sched synchronization.
6211 * This is because we use preempt_disable() to do RCU, but
6212 * the function tracers can be called where RCU is not watching
6213 * (like before user_exit()). We can not rely on the RCU
6214 * infrastructure to do the synchronization, thus we must do it
6217 if (old_hash != EMPTY_HASH)
6218 synchronize_rcu_tasks_rude();
6220 free_ftrace_hash(old_hash);
6224 free_ftrace_hash(fgd->new_hash);
6231 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
6233 struct ftrace_glob func_g;
6234 struct dyn_ftrace *rec;
6235 struct ftrace_page *pg;
6236 struct ftrace_func_entry *entry;
6241 func_g.type = filter_parse_regex(buffer, strlen(buffer),
6242 &func_g.search, ¬);
6244 func_g.len = strlen(func_g.search);
6246 mutex_lock(&ftrace_lock);
6248 if (unlikely(ftrace_disabled)) {
6249 mutex_unlock(&ftrace_lock);
6253 do_for_each_ftrace_rec(pg, rec) {
6255 if (rec->flags & FTRACE_FL_DISABLED)
6258 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
6259 entry = ftrace_lookup_ip(hash, rec->ip);
6266 if (add_hash_entry(hash, rec->ip) < 0)
6270 free_hash_entry(hash, entry);
6275 } while_for_each_ftrace_rec();
6277 mutex_unlock(&ftrace_lock);
6286 ftrace_graph_write(struct file *file, const char __user *ubuf,
6287 size_t cnt, loff_t *ppos)
6289 ssize_t read, ret = 0;
6290 struct ftrace_graph_data *fgd = file->private_data;
6291 struct trace_parser *parser;
6296 /* Read mode uses seq functions */
6297 if (file->f_mode & FMODE_READ) {
6298 struct seq_file *m = file->private_data;
6302 parser = &fgd->parser;
6304 read = trace_get_user(parser, ubuf, cnt, ppos);
6306 if (read >= 0 && trace_parser_loaded(parser) &&
6307 !trace_parser_cont(parser)) {
6309 ret = ftrace_graph_set_hash(fgd->new_hash,
6311 trace_parser_clear(parser);
6320 static const struct file_operations ftrace_graph_fops = {
6321 .open = ftrace_graph_open,
6323 .write = ftrace_graph_write,
6324 .llseek = tracing_lseek,
6325 .release = ftrace_graph_release,
6328 static const struct file_operations ftrace_graph_notrace_fops = {
6329 .open = ftrace_graph_notrace_open,
6331 .write = ftrace_graph_write,
6332 .llseek = tracing_lseek,
6333 .release = ftrace_graph_release,
6335 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6337 void ftrace_create_filter_files(struct ftrace_ops *ops,
6338 struct dentry *parent)
6341 trace_create_file("set_ftrace_filter", TRACE_MODE_WRITE, parent,
6342 ops, &ftrace_filter_fops);
6344 trace_create_file("set_ftrace_notrace", TRACE_MODE_WRITE, parent,
6345 ops, &ftrace_notrace_fops);
6349 * The name "destroy_filter_files" is really a misnomer. Although
6350 * in the future, it may actually delete the files, but this is
6351 * really intended to make sure the ops passed in are disabled
6352 * and that when this function returns, the caller is free to
6355 * The "destroy" name is only to match the "create" name that this
6356 * should be paired with.
6358 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
6360 mutex_lock(&ftrace_lock);
6361 if (ops->flags & FTRACE_OPS_FL_ENABLED)
6362 ftrace_shutdown(ops, 0);
6363 ops->flags |= FTRACE_OPS_FL_DELETED;
6364 ftrace_free_filter(ops);
6365 mutex_unlock(&ftrace_lock);
6368 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
6371 trace_create_file("available_filter_functions", TRACE_MODE_READ,
6372 d_tracer, NULL, &ftrace_avail_fops);
6374 trace_create_file("enabled_functions", TRACE_MODE_READ,
6375 d_tracer, NULL, &ftrace_enabled_fops);
6377 trace_create_file("touched_functions", TRACE_MODE_READ,
6378 d_tracer, NULL, &ftrace_touched_fops);
6380 ftrace_create_filter_files(&global_ops, d_tracer);
6382 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6383 trace_create_file("set_graph_function", TRACE_MODE_WRITE, d_tracer,
6385 &ftrace_graph_fops);
6386 trace_create_file("set_graph_notrace", TRACE_MODE_WRITE, d_tracer,
6388 &ftrace_graph_notrace_fops);
6389 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6394 static int ftrace_cmp_ips(const void *a, const void *b)
6396 const unsigned long *ipa = a;
6397 const unsigned long *ipb = b;
6406 #ifdef CONFIG_FTRACE_SORT_STARTUP_TEST
6407 static void test_is_sorted(unsigned long *start, unsigned long count)
6411 for (i = 1; i < count; i++) {
6412 if (WARN(start[i - 1] > start[i],
6413 "[%d] %pS at %lx is not sorted with %pS at %lx\n", i,
6414 (void *)start[i - 1], start[i - 1],
6415 (void *)start[i], start[i]))
6419 pr_info("ftrace section at %px sorted properly\n", start);
6422 static void test_is_sorted(unsigned long *start, unsigned long count)
6427 static int ftrace_process_locs(struct module *mod,
6428 unsigned long *start,
6431 struct ftrace_page *start_pg;
6432 struct ftrace_page *pg;
6433 struct dyn_ftrace *rec;
6434 unsigned long count;
6437 unsigned long flags = 0; /* Shut up gcc */
6440 count = end - start;
6446 * Sorting mcount in vmlinux at build time depend on
6447 * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in
6448 * modules can not be sorted at build time.
6450 if (!IS_ENABLED(CONFIG_BUILDTIME_MCOUNT_SORT) || mod) {
6451 sort(start, count, sizeof(*start),
6452 ftrace_cmp_ips, NULL);
6454 test_is_sorted(start, count);
6457 start_pg = ftrace_allocate_pages(count);
6461 mutex_lock(&ftrace_lock);
6464 * Core and each module needs their own pages, as
6465 * modules will free them when they are removed.
6466 * Force a new page to be allocated for modules.
6469 WARN_ON(ftrace_pages || ftrace_pages_start);
6470 /* First initialization */
6471 ftrace_pages = ftrace_pages_start = start_pg;
6476 if (WARN_ON(ftrace_pages->next)) {
6477 /* Hmm, we have free pages? */
6478 while (ftrace_pages->next)
6479 ftrace_pages = ftrace_pages->next;
6482 ftrace_pages->next = start_pg;
6488 unsigned long end_offset;
6489 addr = ftrace_call_adjust(*p++);
6491 * Some architecture linkers will pad between
6492 * the different mcount_loc sections of different
6493 * object files to satisfy alignments.
6494 * Skip any NULL pointers.
6499 end_offset = (pg->index+1) * sizeof(pg->records[0]);
6500 if (end_offset > PAGE_SIZE << pg->order) {
6501 /* We should have allocated enough */
6502 if (WARN_ON(!pg->next))
6507 rec = &pg->records[pg->index++];
6511 /* We should have used all pages */
6514 /* Assign the last page to ftrace_pages */
6518 * We only need to disable interrupts on start up
6519 * because we are modifying code that an interrupt
6520 * may execute, and the modification is not atomic.
6521 * But for modules, nothing runs the code we modify
6522 * until we are finished with it, and there's no
6523 * reason to cause large interrupt latencies while we do it.
6526 local_irq_save(flags);
6527 ftrace_update_code(mod, start_pg);
6529 local_irq_restore(flags);
6532 mutex_unlock(&ftrace_lock);
6537 struct ftrace_mod_func {
6538 struct list_head list;
6544 struct ftrace_mod_map {
6545 struct rcu_head rcu;
6546 struct list_head list;
6548 unsigned long start_addr;
6549 unsigned long end_addr;
6550 struct list_head funcs;
6551 unsigned int num_funcs;
6554 static int ftrace_get_trampoline_kallsym(unsigned int symnum,
6555 unsigned long *value, char *type,
6556 char *name, char *module_name,
6559 struct ftrace_ops *op;
6561 list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) {
6562 if (!op->trampoline || symnum--)
6564 *value = op->trampoline;
6566 strlcpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN);
6567 strlcpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN);
6575 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) || defined(CONFIG_MODULES)
6577 * Check if the current ops references the given ip.
6579 * If the ops traces all functions, then it was already accounted for.
6580 * If the ops does not trace the current record function, skip it.
6581 * If the ops ignores the function via notrace filter, skip it.
6584 ops_references_ip(struct ftrace_ops *ops, unsigned long ip)
6586 /* If ops isn't enabled, ignore it */
6587 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
6590 /* If ops traces all then it includes this function */
6591 if (ops_traces_mod(ops))
6594 /* The function must be in the filter */
6595 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
6596 !__ftrace_lookup_ip(ops->func_hash->filter_hash, ip))
6599 /* If in notrace hash, we ignore it too */
6600 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, ip))
6607 #ifdef CONFIG_MODULES
6609 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
6611 static LIST_HEAD(ftrace_mod_maps);
6613 static int referenced_filters(struct dyn_ftrace *rec)
6615 struct ftrace_ops *ops;
6618 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
6619 if (ops_references_ip(ops, rec->ip)) {
6620 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT))
6622 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY))
6625 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
6626 rec->flags |= FTRACE_FL_REGS;
6627 if (cnt == 1 && ops->trampoline)
6628 rec->flags |= FTRACE_FL_TRAMP;
6630 rec->flags &= ~FTRACE_FL_TRAMP;
6638 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
6640 struct ftrace_func_entry *entry;
6641 struct dyn_ftrace *rec;
6644 if (ftrace_hash_empty(hash))
6647 for (i = 0; i < pg->index; i++) {
6648 rec = &pg->records[i];
6649 entry = __ftrace_lookup_ip(hash, rec->ip);
6651 * Do not allow this rec to match again.
6652 * Yeah, it may waste some memory, but will be removed
6653 * if/when the hash is modified again.
6660 /* Clear any records from hashes */
6661 static void clear_mod_from_hashes(struct ftrace_page *pg)
6663 struct trace_array *tr;
6665 mutex_lock(&trace_types_lock);
6666 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6667 if (!tr->ops || !tr->ops->func_hash)
6669 mutex_lock(&tr->ops->func_hash->regex_lock);
6670 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
6671 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
6672 mutex_unlock(&tr->ops->func_hash->regex_lock);
6674 mutex_unlock(&trace_types_lock);
6677 static void ftrace_free_mod_map(struct rcu_head *rcu)
6679 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
6680 struct ftrace_mod_func *mod_func;
6681 struct ftrace_mod_func *n;
6683 /* All the contents of mod_map are now not visible to readers */
6684 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
6685 kfree(mod_func->name);
6686 list_del(&mod_func->list);
6693 void ftrace_release_mod(struct module *mod)
6695 struct ftrace_mod_map *mod_map;
6696 struct ftrace_mod_map *n;
6697 struct dyn_ftrace *rec;
6698 struct ftrace_page **last_pg;
6699 struct ftrace_page *tmp_page = NULL;
6700 struct ftrace_page *pg;
6702 mutex_lock(&ftrace_lock);
6704 if (ftrace_disabled)
6707 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
6708 if (mod_map->mod == mod) {
6709 list_del_rcu(&mod_map->list);
6710 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
6716 * Each module has its own ftrace_pages, remove
6717 * them from the list.
6719 last_pg = &ftrace_pages_start;
6720 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
6721 rec = &pg->records[0];
6722 if (within_module_core(rec->ip, mod) ||
6723 within_module_init(rec->ip, mod)) {
6725 * As core pages are first, the first
6726 * page should never be a module page.
6728 if (WARN_ON(pg == ftrace_pages_start))
6731 /* Check if we are deleting the last page */
6732 if (pg == ftrace_pages)
6733 ftrace_pages = next_to_ftrace_page(last_pg);
6735 ftrace_update_tot_cnt -= pg->index;
6736 *last_pg = pg->next;
6738 pg->next = tmp_page;
6741 last_pg = &pg->next;
6744 mutex_unlock(&ftrace_lock);
6746 for (pg = tmp_page; pg; pg = tmp_page) {
6748 /* Needs to be called outside of ftrace_lock */
6749 clear_mod_from_hashes(pg);
6752 free_pages((unsigned long)pg->records, pg->order);
6753 ftrace_number_of_pages -= 1 << pg->order;
6755 tmp_page = pg->next;
6757 ftrace_number_of_groups--;
6761 void ftrace_module_enable(struct module *mod)
6763 struct dyn_ftrace *rec;
6764 struct ftrace_page *pg;
6766 mutex_lock(&ftrace_lock);
6768 if (ftrace_disabled)
6772 * If the tracing is enabled, go ahead and enable the record.
6774 * The reason not to enable the record immediately is the
6775 * inherent check of ftrace_make_nop/ftrace_make_call for
6776 * correct previous instructions. Making first the NOP
6777 * conversion puts the module to the correct state, thus
6778 * passing the ftrace_make_call check.
6780 * We also delay this to after the module code already set the
6781 * text to read-only, as we now need to set it back to read-write
6782 * so that we can modify the text.
6784 if (ftrace_start_up)
6785 ftrace_arch_code_modify_prepare();
6787 do_for_each_ftrace_rec(pg, rec) {
6790 * do_for_each_ftrace_rec() is a double loop.
6791 * module text shares the pg. If a record is
6792 * not part of this module, then skip this pg,
6793 * which the "break" will do.
6795 if (!within_module_core(rec->ip, mod) &&
6796 !within_module_init(rec->ip, mod))
6799 /* Weak functions should still be ignored */
6800 if (!test_for_valid_rec(rec)) {
6801 /* Clear all other flags. Should not be enabled anyway */
6802 rec->flags = FTRACE_FL_DISABLED;
6809 * When adding a module, we need to check if tracers are
6810 * currently enabled and if they are, and can trace this record,
6811 * we need to enable the module functions as well as update the
6812 * reference counts for those function records.
6814 if (ftrace_start_up)
6815 cnt += referenced_filters(rec);
6817 rec->flags &= ~FTRACE_FL_DISABLED;
6820 if (ftrace_start_up && cnt) {
6821 int failed = __ftrace_replace_code(rec, 1);
6823 ftrace_bug(failed, rec);
6828 } while_for_each_ftrace_rec();
6831 if (ftrace_start_up)
6832 ftrace_arch_code_modify_post_process();
6835 mutex_unlock(&ftrace_lock);
6837 process_cached_mods(mod->name);
6840 void ftrace_module_init(struct module *mod)
6844 if (ftrace_disabled || !mod->num_ftrace_callsites)
6847 ret = ftrace_process_locs(mod, mod->ftrace_callsites,
6848 mod->ftrace_callsites + mod->num_ftrace_callsites);
6850 pr_warn("ftrace: failed to allocate entries for module '%s' functions\n",
6854 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6855 struct dyn_ftrace *rec)
6857 struct ftrace_mod_func *mod_func;
6858 unsigned long symsize;
6859 unsigned long offset;
6860 char str[KSYM_SYMBOL_LEN];
6864 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
6868 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
6872 mod_func->name = kstrdup(str, GFP_KERNEL);
6873 if (!mod_func->name) {
6878 mod_func->ip = rec->ip - offset;
6879 mod_func->size = symsize;
6881 mod_map->num_funcs++;
6883 list_add_rcu(&mod_func->list, &mod_map->funcs);
6886 static struct ftrace_mod_map *
6887 allocate_ftrace_mod_map(struct module *mod,
6888 unsigned long start, unsigned long end)
6890 struct ftrace_mod_map *mod_map;
6892 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
6897 mod_map->start_addr = start;
6898 mod_map->end_addr = end;
6899 mod_map->num_funcs = 0;
6901 INIT_LIST_HEAD_RCU(&mod_map->funcs);
6903 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
6909 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
6910 unsigned long addr, unsigned long *size,
6911 unsigned long *off, char *sym)
6913 struct ftrace_mod_func *found_func = NULL;
6914 struct ftrace_mod_func *mod_func;
6916 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6917 if (addr >= mod_func->ip &&
6918 addr < mod_func->ip + mod_func->size) {
6919 found_func = mod_func;
6926 *size = found_func->size;
6928 *off = addr - found_func->ip;
6930 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
6932 return found_func->name;
6939 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6940 unsigned long *off, char **modname, char *sym)
6942 struct ftrace_mod_map *mod_map;
6943 const char *ret = NULL;
6945 /* mod_map is freed via call_rcu() */
6947 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6948 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6951 *modname = mod_map->mod->name;
6960 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6961 char *type, char *name,
6962 char *module_name, int *exported)
6964 struct ftrace_mod_map *mod_map;
6965 struct ftrace_mod_func *mod_func;
6969 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6971 if (symnum >= mod_map->num_funcs) {
6972 symnum -= mod_map->num_funcs;
6976 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6982 *value = mod_func->ip;
6984 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6985 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6993 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6994 module_name, exported);
7000 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
7001 struct dyn_ftrace *rec) { }
7002 static inline struct ftrace_mod_map *
7003 allocate_ftrace_mod_map(struct module *mod,
7004 unsigned long start, unsigned long end)
7008 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
7009 char *type, char *name, char *module_name,
7015 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
7016 module_name, exported);
7020 #endif /* CONFIG_MODULES */
7022 struct ftrace_init_func {
7023 struct list_head list;
7027 /* Clear any init ips from hashes */
7029 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
7031 struct ftrace_func_entry *entry;
7033 entry = ftrace_lookup_ip(hash, func->ip);
7035 * Do not allow this rec to match again.
7036 * Yeah, it may waste some memory, but will be removed
7037 * if/when the hash is modified again.
7044 clear_func_from_hashes(struct ftrace_init_func *func)
7046 struct trace_array *tr;
7048 mutex_lock(&trace_types_lock);
7049 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7050 if (!tr->ops || !tr->ops->func_hash)
7052 mutex_lock(&tr->ops->func_hash->regex_lock);
7053 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
7054 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
7055 mutex_unlock(&tr->ops->func_hash->regex_lock);
7057 mutex_unlock(&trace_types_lock);
7060 static void add_to_clear_hash_list(struct list_head *clear_list,
7061 struct dyn_ftrace *rec)
7063 struct ftrace_init_func *func;
7065 func = kmalloc(sizeof(*func), GFP_KERNEL);
7067 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
7072 list_add(&func->list, clear_list);
7075 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
7077 unsigned long start = (unsigned long)(start_ptr);
7078 unsigned long end = (unsigned long)(end_ptr);
7079 struct ftrace_page **last_pg = &ftrace_pages_start;
7080 struct ftrace_page *pg;
7081 struct dyn_ftrace *rec;
7082 struct dyn_ftrace key;
7083 struct ftrace_mod_map *mod_map = NULL;
7084 struct ftrace_init_func *func, *func_next;
7085 struct list_head clear_hash;
7087 INIT_LIST_HEAD(&clear_hash);
7090 key.flags = end; /* overload flags, as it is unsigned long */
7092 mutex_lock(&ftrace_lock);
7095 * If we are freeing module init memory, then check if
7096 * any tracer is active. If so, we need to save a mapping of
7097 * the module functions being freed with the address.
7099 if (mod && ftrace_ops_list != &ftrace_list_end)
7100 mod_map = allocate_ftrace_mod_map(mod, start, end);
7102 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
7103 if (end < pg->records[0].ip ||
7104 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
7107 rec = bsearch(&key, pg->records, pg->index,
7108 sizeof(struct dyn_ftrace),
7113 /* rec will be cleared from hashes after ftrace_lock unlock */
7114 add_to_clear_hash_list(&clear_hash, rec);
7117 save_ftrace_mod_rec(mod_map, rec);
7120 ftrace_update_tot_cnt--;
7122 *last_pg = pg->next;
7124 free_pages((unsigned long)pg->records, pg->order);
7125 ftrace_number_of_pages -= 1 << pg->order;
7127 ftrace_number_of_groups--;
7129 pg = container_of(last_pg, struct ftrace_page, next);
7134 memmove(rec, rec + 1,
7135 (pg->index - (rec - pg->records)) * sizeof(*rec));
7136 /* More than one function may be in this block */
7139 mutex_unlock(&ftrace_lock);
7141 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
7142 clear_func_from_hashes(func);
7147 void __init ftrace_free_init_mem(void)
7149 void *start = (void *)(&__init_begin);
7150 void *end = (void *)(&__init_end);
7152 ftrace_boot_snapshot();
7154 ftrace_free_mem(NULL, start, end);
7157 int __init __weak ftrace_dyn_arch_init(void)
7162 void __init ftrace_init(void)
7164 extern unsigned long __start_mcount_loc[];
7165 extern unsigned long __stop_mcount_loc[];
7166 unsigned long count, flags;
7169 local_irq_save(flags);
7170 ret = ftrace_dyn_arch_init();
7171 local_irq_restore(flags);
7175 count = __stop_mcount_loc - __start_mcount_loc;
7177 pr_info("ftrace: No functions to be traced?\n");
7181 pr_info("ftrace: allocating %ld entries in %ld pages\n",
7182 count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
7184 ret = ftrace_process_locs(NULL,
7188 pr_warn("ftrace: failed to allocate entries for functions\n");
7192 pr_info("ftrace: allocated %ld pages with %ld groups\n",
7193 ftrace_number_of_pages, ftrace_number_of_groups);
7195 last_ftrace_enabled = ftrace_enabled = 1;
7197 set_ftrace_early_filters();
7201 ftrace_disabled = 1;
7204 /* Do nothing if arch does not support this */
7205 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
7209 static void ftrace_update_trampoline(struct ftrace_ops *ops)
7211 unsigned long trampoline = ops->trampoline;
7213 arch_ftrace_update_trampoline(ops);
7214 if (ops->trampoline && ops->trampoline != trampoline &&
7215 (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) {
7216 /* Add to kallsyms before the perf events */
7217 ftrace_add_trampoline_to_kallsyms(ops);
7218 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
7219 ops->trampoline, ops->trampoline_size, false,
7220 FTRACE_TRAMPOLINE_SYM);
7222 * Record the perf text poke event after the ksymbol register
7225 perf_event_text_poke((void *)ops->trampoline, NULL, 0,
7226 (void *)ops->trampoline,
7227 ops->trampoline_size);
7231 void ftrace_init_trace_array(struct trace_array *tr)
7233 INIT_LIST_HEAD(&tr->func_probes);
7234 INIT_LIST_HEAD(&tr->mod_trace);
7235 INIT_LIST_HEAD(&tr->mod_notrace);
7239 struct ftrace_ops global_ops = {
7240 .func = ftrace_stub,
7241 .flags = FTRACE_OPS_FL_INITIALIZED |
7245 static int __init ftrace_nodyn_init(void)
7250 core_initcall(ftrace_nodyn_init);
7252 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
7253 static inline void ftrace_startup_all(int command) { }
7255 static void ftrace_update_trampoline(struct ftrace_ops *ops)
7259 #endif /* CONFIG_DYNAMIC_FTRACE */
7261 __init void ftrace_init_global_array_ops(struct trace_array *tr)
7263 tr->ops = &global_ops;
7264 tr->ops->private = tr;
7265 ftrace_init_trace_array(tr);
7268 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
7270 /* If we filter on pids, update to use the pid function */
7271 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
7272 if (WARN_ON(tr->ops->func != ftrace_stub))
7273 printk("ftrace ops had %pS for function\n",
7276 tr->ops->func = func;
7277 tr->ops->private = tr;
7280 void ftrace_reset_array_ops(struct trace_array *tr)
7282 tr->ops->func = ftrace_stub;
7285 static nokprobe_inline void
7286 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
7287 struct ftrace_ops *ignored, struct ftrace_regs *fregs)
7289 struct pt_regs *regs = ftrace_get_regs(fregs);
7290 struct ftrace_ops *op;
7294 * The ftrace_test_and_set_recursion() will disable preemption,
7295 * which is required since some of the ops may be dynamically
7296 * allocated, they must be freed after a synchronize_rcu().
7298 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
7302 do_for_each_ftrace_op(op, ftrace_ops_list) {
7303 /* Stub functions don't need to be called nor tested */
7304 if (op->flags & FTRACE_OPS_FL_STUB)
7307 * Check the following for each ops before calling their func:
7308 * if RCU flag is set, then rcu_is_watching() must be true
7309 * Otherwise test if the ip matches the ops filter
7311 * If any of the above fails then the op->func() is not executed.
7313 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
7314 ftrace_ops_test(op, ip, regs)) {
7315 if (FTRACE_WARN_ON(!op->func)) {
7316 pr_warn("op=%p %pS\n", op, op);
7319 op->func(ip, parent_ip, op, fregs);
7321 } while_for_each_ftrace_op(op);
7323 trace_clear_recursion(bit);
7327 * Some archs only support passing ip and parent_ip. Even though
7328 * the list function ignores the op parameter, we do not want any
7329 * C side effects, where a function is called without the caller
7330 * sending a third parameter.
7331 * Archs are to support both the regs and ftrace_ops at the same time.
7332 * If they support ftrace_ops, it is assumed they support regs.
7333 * If call backs want to use regs, they must either check for regs
7334 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
7335 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
7336 * An architecture can pass partial regs with ftrace_ops and still
7337 * set the ARCH_SUPPORTS_FTRACE_OPS.
7339 * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be
7340 * arch_ftrace_ops_list_func.
7342 #if ARCH_SUPPORTS_FTRACE_OPS
7343 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
7344 struct ftrace_ops *op, struct ftrace_regs *fregs)
7346 __ftrace_ops_list_func(ip, parent_ip, NULL, fregs);
7349 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
7351 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
7354 NOKPROBE_SYMBOL(arch_ftrace_ops_list_func);
7357 * If there's only one function registered but it does not support
7358 * recursion, needs RCU protection, then this function will be called
7359 * by the mcount trampoline.
7361 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
7362 struct ftrace_ops *op, struct ftrace_regs *fregs)
7366 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
7370 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
7371 op->func(ip, parent_ip, op, fregs);
7373 trace_clear_recursion(bit);
7375 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
7378 * ftrace_ops_get_func - get the function a trampoline should call
7379 * @ops: the ops to get the function for
7381 * Normally the mcount trampoline will call the ops->func, but there
7382 * are times that it should not. For example, if the ops does not
7383 * have its own recursion protection, then it should call the
7384 * ftrace_ops_assist_func() instead.
7386 * Returns the function that the trampoline should call for @ops.
7388 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
7391 * If the function does not handle recursion or needs to be RCU safe,
7392 * then we need to call the assist handler.
7394 if (ops->flags & (FTRACE_OPS_FL_RECURSION |
7396 return ftrace_ops_assist_func;
7402 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
7403 struct task_struct *prev,
7404 struct task_struct *next,
7405 unsigned int prev_state)
7407 struct trace_array *tr = data;
7408 struct trace_pid_list *pid_list;
7409 struct trace_pid_list *no_pid_list;
7411 pid_list = rcu_dereference_sched(tr->function_pids);
7412 no_pid_list = rcu_dereference_sched(tr->function_no_pids);
7414 if (trace_ignore_this_task(pid_list, no_pid_list, next))
7415 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7418 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7423 ftrace_pid_follow_sched_process_fork(void *data,
7424 struct task_struct *self,
7425 struct task_struct *task)
7427 struct trace_pid_list *pid_list;
7428 struct trace_array *tr = data;
7430 pid_list = rcu_dereference_sched(tr->function_pids);
7431 trace_filter_add_remove_task(pid_list, self, task);
7433 pid_list = rcu_dereference_sched(tr->function_no_pids);
7434 trace_filter_add_remove_task(pid_list, self, task);
7438 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
7440 struct trace_pid_list *pid_list;
7441 struct trace_array *tr = data;
7443 pid_list = rcu_dereference_sched(tr->function_pids);
7444 trace_filter_add_remove_task(pid_list, NULL, task);
7446 pid_list = rcu_dereference_sched(tr->function_no_pids);
7447 trace_filter_add_remove_task(pid_list, NULL, task);
7450 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
7453 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7455 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
7458 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7460 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
7465 static void clear_ftrace_pids(struct trace_array *tr, int type)
7467 struct trace_pid_list *pid_list;
7468 struct trace_pid_list *no_pid_list;
7471 pid_list = rcu_dereference_protected(tr->function_pids,
7472 lockdep_is_held(&ftrace_lock));
7473 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7474 lockdep_is_held(&ftrace_lock));
7476 /* Make sure there's something to do */
7477 if (!pid_type_enabled(type, pid_list, no_pid_list))
7480 /* See if the pids still need to be checked after this */
7481 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
7482 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7483 for_each_possible_cpu(cpu)
7484 per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE;
7487 if (type & TRACE_PIDS)
7488 rcu_assign_pointer(tr->function_pids, NULL);
7490 if (type & TRACE_NO_PIDS)
7491 rcu_assign_pointer(tr->function_no_pids, NULL);
7493 /* Wait till all users are no longer using pid filtering */
7496 if ((type & TRACE_PIDS) && pid_list)
7497 trace_pid_list_free(pid_list);
7499 if ((type & TRACE_NO_PIDS) && no_pid_list)
7500 trace_pid_list_free(no_pid_list);
7503 void ftrace_clear_pids(struct trace_array *tr)
7505 mutex_lock(&ftrace_lock);
7507 clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
7509 mutex_unlock(&ftrace_lock);
7512 static void ftrace_pid_reset(struct trace_array *tr, int type)
7514 mutex_lock(&ftrace_lock);
7515 clear_ftrace_pids(tr, type);
7517 ftrace_update_pid_func();
7518 ftrace_startup_all(0);
7520 mutex_unlock(&ftrace_lock);
7523 /* Greater than any max PID */
7524 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
7526 static void *fpid_start(struct seq_file *m, loff_t *pos)
7529 struct trace_pid_list *pid_list;
7530 struct trace_array *tr = m->private;
7532 mutex_lock(&ftrace_lock);
7533 rcu_read_lock_sched();
7535 pid_list = rcu_dereference_sched(tr->function_pids);
7538 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7540 return trace_pid_start(pid_list, pos);
7543 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
7545 struct trace_array *tr = m->private;
7546 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
7548 if (v == FTRACE_NO_PIDS) {
7552 return trace_pid_next(pid_list, v, pos);
7555 static void fpid_stop(struct seq_file *m, void *p)
7558 rcu_read_unlock_sched();
7559 mutex_unlock(&ftrace_lock);
7562 static int fpid_show(struct seq_file *m, void *v)
7564 if (v == FTRACE_NO_PIDS) {
7565 seq_puts(m, "no pid\n");
7569 return trace_pid_show(m, v);
7572 static const struct seq_operations ftrace_pid_sops = {
7573 .start = fpid_start,
7579 static void *fnpid_start(struct seq_file *m, loff_t *pos)
7582 struct trace_pid_list *pid_list;
7583 struct trace_array *tr = m->private;
7585 mutex_lock(&ftrace_lock);
7586 rcu_read_lock_sched();
7588 pid_list = rcu_dereference_sched(tr->function_no_pids);
7591 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7593 return trace_pid_start(pid_list, pos);
7596 static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos)
7598 struct trace_array *tr = m->private;
7599 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids);
7601 if (v == FTRACE_NO_PIDS) {
7605 return trace_pid_next(pid_list, v, pos);
7608 static const struct seq_operations ftrace_no_pid_sops = {
7609 .start = fnpid_start,
7615 static int pid_open(struct inode *inode, struct file *file, int type)
7617 const struct seq_operations *seq_ops;
7618 struct trace_array *tr = inode->i_private;
7622 ret = tracing_check_open_get_tr(tr);
7626 if ((file->f_mode & FMODE_WRITE) &&
7627 (file->f_flags & O_TRUNC))
7628 ftrace_pid_reset(tr, type);
7632 seq_ops = &ftrace_pid_sops;
7635 seq_ops = &ftrace_no_pid_sops;
7638 trace_array_put(tr);
7643 ret = seq_open(file, seq_ops);
7645 trace_array_put(tr);
7647 m = file->private_data;
7648 /* copy tr over to seq ops */
7656 ftrace_pid_open(struct inode *inode, struct file *file)
7658 return pid_open(inode, file, TRACE_PIDS);
7662 ftrace_no_pid_open(struct inode *inode, struct file *file)
7664 return pid_open(inode, file, TRACE_NO_PIDS);
7667 static void ignore_task_cpu(void *data)
7669 struct trace_array *tr = data;
7670 struct trace_pid_list *pid_list;
7671 struct trace_pid_list *no_pid_list;
7674 * This function is called by on_each_cpu() while the
7675 * event_mutex is held.
7677 pid_list = rcu_dereference_protected(tr->function_pids,
7678 mutex_is_locked(&ftrace_lock));
7679 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7680 mutex_is_locked(&ftrace_lock));
7682 if (trace_ignore_this_task(pid_list, no_pid_list, current))
7683 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7686 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7691 pid_write(struct file *filp, const char __user *ubuf,
7692 size_t cnt, loff_t *ppos, int type)
7694 struct seq_file *m = filp->private_data;
7695 struct trace_array *tr = m->private;
7696 struct trace_pid_list *filtered_pids;
7697 struct trace_pid_list *other_pids;
7698 struct trace_pid_list *pid_list;
7704 mutex_lock(&ftrace_lock);
7708 filtered_pids = rcu_dereference_protected(tr->function_pids,
7709 lockdep_is_held(&ftrace_lock));
7710 other_pids = rcu_dereference_protected(tr->function_no_pids,
7711 lockdep_is_held(&ftrace_lock));
7714 filtered_pids = rcu_dereference_protected(tr->function_no_pids,
7715 lockdep_is_held(&ftrace_lock));
7716 other_pids = rcu_dereference_protected(tr->function_pids,
7717 lockdep_is_held(&ftrace_lock));
7725 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
7731 rcu_assign_pointer(tr->function_pids, pid_list);
7734 rcu_assign_pointer(tr->function_no_pids, pid_list);
7739 if (filtered_pids) {
7741 trace_pid_list_free(filtered_pids);
7742 } else if (pid_list && !other_pids) {
7743 /* Register a probe to set whether to ignore the tracing of a task */
7744 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7748 * Ignoring of pids is done at task switch. But we have to
7749 * check for those tasks that are currently running.
7750 * Always do this in case a pid was appended or removed.
7752 on_each_cpu(ignore_task_cpu, tr, 1);
7754 ftrace_update_pid_func();
7755 ftrace_startup_all(0);
7757 mutex_unlock(&ftrace_lock);
7766 ftrace_pid_write(struct file *filp, const char __user *ubuf,
7767 size_t cnt, loff_t *ppos)
7769 return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
7773 ftrace_no_pid_write(struct file *filp, const char __user *ubuf,
7774 size_t cnt, loff_t *ppos)
7776 return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
7780 ftrace_pid_release(struct inode *inode, struct file *file)
7782 struct trace_array *tr = inode->i_private;
7784 trace_array_put(tr);
7786 return seq_release(inode, file);
7789 static const struct file_operations ftrace_pid_fops = {
7790 .open = ftrace_pid_open,
7791 .write = ftrace_pid_write,
7793 .llseek = tracing_lseek,
7794 .release = ftrace_pid_release,
7797 static const struct file_operations ftrace_no_pid_fops = {
7798 .open = ftrace_no_pid_open,
7799 .write = ftrace_no_pid_write,
7801 .llseek = tracing_lseek,
7802 .release = ftrace_pid_release,
7805 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
7807 trace_create_file("set_ftrace_pid", TRACE_MODE_WRITE, d_tracer,
7808 tr, &ftrace_pid_fops);
7809 trace_create_file("set_ftrace_notrace_pid", TRACE_MODE_WRITE,
7810 d_tracer, tr, &ftrace_no_pid_fops);
7813 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
7814 struct dentry *d_tracer)
7816 /* Only the top level directory has the dyn_tracefs and profile */
7817 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
7819 ftrace_init_dyn_tracefs(d_tracer);
7820 ftrace_profile_tracefs(d_tracer);
7824 * ftrace_kill - kill ftrace
7826 * This function should be used by panic code. It stops ftrace
7827 * but in a not so nice way. If you need to simply kill ftrace
7828 * from a non-atomic section, use ftrace_kill.
7830 void ftrace_kill(void)
7832 ftrace_disabled = 1;
7834 ftrace_trace_function = ftrace_stub;
7838 * ftrace_is_dead - Test if ftrace is dead or not.
7840 * Returns 1 if ftrace is "dead", zero otherwise.
7842 int ftrace_is_dead(void)
7844 return ftrace_disabled;
7847 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
7849 * When registering ftrace_ops with IPMODIFY, it is necessary to make sure
7850 * it doesn't conflict with any direct ftrace_ops. If there is existing
7851 * direct ftrace_ops on a kernel function being patched, call
7852 * FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER on it to enable sharing.
7854 * @ops: ftrace_ops being registered.
7858 * Negative on failure.
7860 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops)
7862 struct ftrace_func_entry *entry;
7863 struct ftrace_hash *hash;
7864 struct ftrace_ops *op;
7867 lockdep_assert_held_once(&direct_mutex);
7869 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
7872 hash = ops->func_hash->filter_hash;
7873 size = 1 << hash->size_bits;
7874 for (i = 0; i < size; i++) {
7875 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
7876 unsigned long ip = entry->ip;
7877 bool found_op = false;
7879 mutex_lock(&ftrace_lock);
7880 do_for_each_ftrace_op(op, ftrace_ops_list) {
7881 if (!(op->flags & FTRACE_OPS_FL_DIRECT))
7883 if (ops_references_ip(op, ip)) {
7887 } while_for_each_ftrace_op(op);
7888 mutex_unlock(&ftrace_lock);
7894 ret = op->ops_func(op, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER);
7905 * Similar to prepare_direct_functions_for_ipmodify, clean up after ops
7906 * with IPMODIFY is unregistered. The cleanup is optional for most DIRECT
7909 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)
7911 struct ftrace_func_entry *entry;
7912 struct ftrace_hash *hash;
7913 struct ftrace_ops *op;
7916 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
7919 mutex_lock(&direct_mutex);
7921 hash = ops->func_hash->filter_hash;
7922 size = 1 << hash->size_bits;
7923 for (i = 0; i < size; i++) {
7924 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
7925 unsigned long ip = entry->ip;
7926 bool found_op = false;
7928 mutex_lock(&ftrace_lock);
7929 do_for_each_ftrace_op(op, ftrace_ops_list) {
7930 if (!(op->flags & FTRACE_OPS_FL_DIRECT))
7932 if (ops_references_ip(op, ip)) {
7936 } while_for_each_ftrace_op(op);
7937 mutex_unlock(&ftrace_lock);
7939 /* The cleanup is optional, ignore any errors */
7940 if (found_op && op->ops_func)
7941 op->ops_func(op, FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER);
7944 mutex_unlock(&direct_mutex);
7947 #define lock_direct_mutex() mutex_lock(&direct_mutex)
7948 #define unlock_direct_mutex() mutex_unlock(&direct_mutex)
7950 #else /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
7952 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops)
7957 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)
7961 #define lock_direct_mutex() do { } while (0)
7962 #define unlock_direct_mutex() do { } while (0)
7964 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
7967 * Similar to register_ftrace_function, except we don't lock direct_mutex.
7969 static int register_ftrace_function_nolock(struct ftrace_ops *ops)
7973 ftrace_ops_init(ops);
7975 mutex_lock(&ftrace_lock);
7977 ret = ftrace_startup(ops, 0);
7979 mutex_unlock(&ftrace_lock);
7985 * register_ftrace_function - register a function for profiling
7986 * @ops: ops structure that holds the function for profiling.
7988 * Register a function to be called by all functions in the
7991 * Note: @ops->func and all the functions it calls must be labeled
7992 * with "notrace", otherwise it will go into a
7995 int register_ftrace_function(struct ftrace_ops *ops)
7999 lock_direct_mutex();
8000 ret = prepare_direct_functions_for_ipmodify(ops);
8004 ret = register_ftrace_function_nolock(ops);
8007 unlock_direct_mutex();
8010 EXPORT_SYMBOL_GPL(register_ftrace_function);
8013 * unregister_ftrace_function - unregister a function for profiling.
8014 * @ops: ops structure that holds the function to unregister
8016 * Unregister a function that was added to be called by ftrace profiling.
8018 int unregister_ftrace_function(struct ftrace_ops *ops)
8022 mutex_lock(&ftrace_lock);
8023 ret = ftrace_shutdown(ops, 0);
8024 mutex_unlock(&ftrace_lock);
8026 cleanup_direct_functions_after_ipmodify(ops);
8029 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
8031 static int symbols_cmp(const void *a, const void *b)
8033 const char **str_a = (const char **) a;
8034 const char **str_b = (const char **) b;
8036 return strcmp(*str_a, *str_b);
8039 struct kallsyms_data {
8040 unsigned long *addrs;
8046 /* This function gets called for all kernel and module symbols
8047 * and returns 1 in case we resolved all the requested symbols,
8050 static int kallsyms_callback(void *data, const char *name, unsigned long addr)
8052 struct kallsyms_data *args = data;
8056 sym = bsearch(&name, args->syms, args->cnt, sizeof(*args->syms), symbols_cmp);
8060 idx = sym - args->syms;
8061 if (args->addrs[idx])
8064 if (!ftrace_location(addr))
8067 args->addrs[idx] = addr;
8069 return args->found == args->cnt ? 1 : 0;
8073 * ftrace_lookup_symbols - Lookup addresses for array of symbols
8075 * @sorted_syms: array of symbols pointers symbols to resolve,
8076 * must be alphabetically sorted
8077 * @cnt: number of symbols/addresses in @syms/@addrs arrays
8078 * @addrs: array for storing resulting addresses
8080 * This function looks up addresses for array of symbols provided in
8081 * @syms array (must be alphabetically sorted) and stores them in
8082 * @addrs array, which needs to be big enough to store at least @cnt
8085 * This function returns 0 if all provided symbols are found,
8088 int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs)
8090 struct kallsyms_data args;
8093 memset(addrs, 0, sizeof(*addrs) * cnt);
8095 args.syms = sorted_syms;
8099 found_all = kallsyms_on_each_symbol(kallsyms_callback, &args);
8102 found_all = module_kallsyms_on_each_symbol(NULL, kallsyms_callback, &args);
8103 return found_all ? 0 : -ESRCH;
8106 #ifdef CONFIG_SYSCTL
8108 #ifdef CONFIG_DYNAMIC_FTRACE
8109 static void ftrace_startup_sysctl(void)
8113 if (unlikely(ftrace_disabled))
8116 /* Force update next time */
8117 saved_ftrace_func = NULL;
8118 /* ftrace_start_up is true if we want ftrace running */
8119 if (ftrace_start_up) {
8120 command = FTRACE_UPDATE_CALLS;
8121 if (ftrace_graph_active)
8122 command |= FTRACE_START_FUNC_RET;
8123 ftrace_startup_enable(command);
8127 static void ftrace_shutdown_sysctl(void)
8131 if (unlikely(ftrace_disabled))
8134 /* ftrace_start_up is true if ftrace is running */
8135 if (ftrace_start_up) {
8136 command = FTRACE_DISABLE_CALLS;
8137 if (ftrace_graph_active)
8138 command |= FTRACE_STOP_FUNC_RET;
8139 ftrace_run_update_code(command);
8143 # define ftrace_startup_sysctl() do { } while (0)
8144 # define ftrace_shutdown_sysctl() do { } while (0)
8145 #endif /* CONFIG_DYNAMIC_FTRACE */
8147 static bool is_permanent_ops_registered(void)
8149 struct ftrace_ops *op;
8151 do_for_each_ftrace_op(op, ftrace_ops_list) {
8152 if (op->flags & FTRACE_OPS_FL_PERMANENT)
8154 } while_for_each_ftrace_op(op);
8160 ftrace_enable_sysctl(struct ctl_table *table, int write,
8161 void *buffer, size_t *lenp, loff_t *ppos)
8165 mutex_lock(&ftrace_lock);
8167 if (unlikely(ftrace_disabled))
8170 ret = proc_dointvec(table, write, buffer, lenp, ppos);
8172 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
8175 if (ftrace_enabled) {
8177 /* we are starting ftrace again */
8178 if (rcu_dereference_protected(ftrace_ops_list,
8179 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
8180 update_ftrace_function();
8182 ftrace_startup_sysctl();
8185 if (is_permanent_ops_registered()) {
8186 ftrace_enabled = true;
8191 /* stopping ftrace calls (just send to ftrace_stub) */
8192 ftrace_trace_function = ftrace_stub;
8194 ftrace_shutdown_sysctl();
8197 last_ftrace_enabled = !!ftrace_enabled;
8199 mutex_unlock(&ftrace_lock);
8203 static struct ctl_table ftrace_sysctls[] = {
8205 .procname = "ftrace_enabled",
8206 .data = &ftrace_enabled,
8207 .maxlen = sizeof(int),
8209 .proc_handler = ftrace_enable_sysctl,
8214 static int __init ftrace_sysctl_init(void)
8216 register_sysctl_init("kernel", ftrace_sysctls);
8219 late_initcall(ftrace_sysctl_init);