1 // SPDX-License-Identifier: GPL-2.0
3 * Infrastructure for profiling code inserted by 'gcc -pg'.
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
8 * Originally ported from the -rt patch by:
9 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
11 * Based on code in the latency_tracer, that is:
13 * Copyright (C) 2004-2006 Ingo Molnar
14 * Copyright (C) 2004 Nadia Yvette Chambers
17 #include <linux/stop_machine.h>
18 #include <linux/clocksource.h>
19 #include <linux/sched/task.h>
20 #include <linux/kallsyms.h>
21 #include <linux/security.h>
22 #include <linux/seq_file.h>
23 #include <linux/tracefs.h>
24 #include <linux/hardirq.h>
25 #include <linux/kthread.h>
26 #include <linux/uaccess.h>
27 #include <linux/bsearch.h>
28 #include <linux/module.h>
29 #include <linux/ftrace.h>
30 #include <linux/sysctl.h>
31 #include <linux/slab.h>
32 #include <linux/ctype.h>
33 #include <linux/sort.h>
34 #include <linux/list.h>
35 #include <linux/hash.h>
36 #include <linux/rcupdate.h>
37 #include <linux/kprobes.h>
39 #include <trace/events/sched.h>
41 #include <asm/sections.h>
42 #include <asm/setup.h>
44 #include "ftrace_internal.h"
45 #include "trace_output.h"
46 #include "trace_stat.h"
48 #define FTRACE_INVALID_FUNCTION "__ftrace_invalid_address__"
50 #define FTRACE_WARN_ON(cond) \
58 #define FTRACE_WARN_ON_ONCE(cond) \
61 if (WARN_ON_ONCE(___r)) \
66 /* hash bits for specific function selection */
67 #define FTRACE_HASH_DEFAULT_BITS 10
68 #define FTRACE_HASH_MAX_BITS 12
70 #ifdef CONFIG_DYNAMIC_FTRACE
71 #define INIT_OPS_HASH(opsname) \
72 .func_hash = &opsname.local_hash, \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
75 #define INIT_OPS_HASH(opsname)
79 FTRACE_MODIFY_ENABLE_FL = (1 << 0),
80 FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
83 struct ftrace_ops ftrace_list_end __read_mostly = {
85 .flags = FTRACE_OPS_FL_STUB,
86 INIT_OPS_HASH(ftrace_list_end)
89 /* ftrace_enabled is a method to turn ftrace on or off */
90 int ftrace_enabled __read_mostly;
91 static int __maybe_unused last_ftrace_enabled;
93 /* Current function tracing op */
94 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
95 /* What to set function_trace_op to */
96 static struct ftrace_ops *set_function_trace_op;
98 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
100 struct trace_array *tr;
102 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
107 return tr->function_pids != NULL || tr->function_no_pids != NULL;
110 static void ftrace_update_trampoline(struct ftrace_ops *ops);
113 * ftrace_disabled is set when an anomaly is discovered.
114 * ftrace_disabled is much stronger than ftrace_enabled.
116 static int ftrace_disabled __read_mostly;
118 DEFINE_MUTEX(ftrace_lock);
120 struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
121 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
122 struct ftrace_ops global_ops;
124 /* Defined by vmlinux.lds.h see the comment above arch_ftrace_ops_list_func for details */
125 void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
126 struct ftrace_ops *op, struct ftrace_regs *fregs);
128 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
130 * Stub used to invoke the list ops without requiring a separate trampoline.
132 const struct ftrace_ops ftrace_list_ops = {
133 .func = ftrace_ops_list_func,
134 .flags = FTRACE_OPS_FL_STUB,
137 static void ftrace_ops_nop_func(unsigned long ip, unsigned long parent_ip,
138 struct ftrace_ops *op,
139 struct ftrace_regs *fregs)
145 * Stub used when a call site is disabled. May be called transiently by threads
146 * which have made it into ftrace_caller but haven't yet recovered the ops at
147 * the point the call site is disabled.
149 const struct ftrace_ops ftrace_nop_ops = {
150 .func = ftrace_ops_nop_func,
151 .flags = FTRACE_OPS_FL_STUB,
155 static inline void ftrace_ops_init(struct ftrace_ops *ops)
157 #ifdef CONFIG_DYNAMIC_FTRACE
158 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
159 mutex_init(&ops->local_hash.regex_lock);
160 ops->func_hash = &ops->local_hash;
161 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
166 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
167 struct ftrace_ops *op, struct ftrace_regs *fregs)
169 struct trace_array *tr = op->private;
173 pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid);
174 if (pid == FTRACE_PID_IGNORE)
176 if (pid != FTRACE_PID_TRACE &&
181 op->saved_func(ip, parent_ip, op, fregs);
184 static void ftrace_sync_ipi(void *data)
186 /* Probably not needed, but do it anyway */
190 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
193 * If this is a dynamic or RCU ops, or we force list func,
194 * then it needs to call the list anyway.
196 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
197 FTRACE_FORCE_LIST_FUNC)
198 return ftrace_ops_list_func;
200 return ftrace_ops_get_func(ops);
203 static void update_ftrace_function(void)
208 * Prepare the ftrace_ops that the arch callback will use.
209 * If there's only one ftrace_ops registered, the ftrace_ops_list
210 * will point to the ops we want.
212 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
213 lockdep_is_held(&ftrace_lock));
215 /* If there's no ftrace_ops registered, just call the stub function */
216 if (set_function_trace_op == &ftrace_list_end) {
220 * If we are at the end of the list and this ops is
221 * recursion safe and not dynamic and the arch supports passing ops,
222 * then have the mcount trampoline call the function directly.
224 } else if (rcu_dereference_protected(ftrace_ops_list->next,
225 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
226 func = ftrace_ops_get_list_func(ftrace_ops_list);
229 /* Just use the default ftrace_ops */
230 set_function_trace_op = &ftrace_list_end;
231 func = ftrace_ops_list_func;
234 update_function_graph_func();
236 /* If there's no change, then do nothing more here */
237 if (ftrace_trace_function == func)
241 * If we are using the list function, it doesn't care
242 * about the function_trace_ops.
244 if (func == ftrace_ops_list_func) {
245 ftrace_trace_function = func;
247 * Don't even bother setting function_trace_ops,
248 * it would be racy to do so anyway.
253 #ifndef CONFIG_DYNAMIC_FTRACE
255 * For static tracing, we need to be a bit more careful.
256 * The function change takes affect immediately. Thus,
257 * we need to coordinate the setting of the function_trace_ops
258 * with the setting of the ftrace_trace_function.
260 * Set the function to the list ops, which will call the
261 * function we want, albeit indirectly, but it handles the
262 * ftrace_ops and doesn't depend on function_trace_op.
264 ftrace_trace_function = ftrace_ops_list_func;
266 * Make sure all CPUs see this. Yes this is slow, but static
267 * tracing is slow and nasty to have enabled.
269 synchronize_rcu_tasks_rude();
270 /* Now all cpus are using the list ops. */
271 function_trace_op = set_function_trace_op;
272 /* Make sure the function_trace_op is visible on all CPUs */
274 /* Nasty way to force a rmb on all cpus */
275 smp_call_function(ftrace_sync_ipi, NULL, 1);
276 /* OK, we are all set to update the ftrace_trace_function now! */
277 #endif /* !CONFIG_DYNAMIC_FTRACE */
279 ftrace_trace_function = func;
282 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
283 struct ftrace_ops *ops)
285 rcu_assign_pointer(ops->next, *list);
288 * We are entering ops into the list but another
289 * CPU might be walking that list. We need to make sure
290 * the ops->next pointer is valid before another CPU sees
291 * the ops pointer included into the list.
293 rcu_assign_pointer(*list, ops);
296 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
297 struct ftrace_ops *ops)
299 struct ftrace_ops **p;
302 * If we are removing the last function, then simply point
303 * to the ftrace_stub.
305 if (rcu_dereference_protected(*list,
306 lockdep_is_held(&ftrace_lock)) == ops &&
307 rcu_dereference_protected(ops->next,
308 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
309 *list = &ftrace_list_end;
313 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
324 static void ftrace_update_trampoline(struct ftrace_ops *ops);
326 int __register_ftrace_function(struct ftrace_ops *ops)
328 if (ops->flags & FTRACE_OPS_FL_DELETED)
331 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
334 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
336 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
337 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
338 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
340 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
341 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
344 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
345 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
347 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
350 if (!is_kernel_core_data((unsigned long)ops))
351 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
353 add_ftrace_ops(&ftrace_ops_list, ops);
355 /* Always save the function, and reset at unregistering */
356 ops->saved_func = ops->func;
358 if (ftrace_pids_enabled(ops))
359 ops->func = ftrace_pid_func;
361 ftrace_update_trampoline(ops);
364 update_ftrace_function();
369 int __unregister_ftrace_function(struct ftrace_ops *ops)
373 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
376 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
382 update_ftrace_function();
384 ops->func = ops->saved_func;
389 static void ftrace_update_pid_func(void)
391 struct ftrace_ops *op;
393 /* Only do something if we are tracing something */
394 if (ftrace_trace_function == ftrace_stub)
397 do_for_each_ftrace_op(op, ftrace_ops_list) {
398 if (op->flags & FTRACE_OPS_FL_PID) {
399 op->func = ftrace_pids_enabled(op) ?
400 ftrace_pid_func : op->saved_func;
401 ftrace_update_trampoline(op);
403 } while_for_each_ftrace_op(op);
405 update_ftrace_function();
408 #ifdef CONFIG_FUNCTION_PROFILER
409 struct ftrace_profile {
410 struct hlist_node node;
412 unsigned long counter;
413 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
414 unsigned long long time;
415 unsigned long long time_squared;
419 struct ftrace_profile_page {
420 struct ftrace_profile_page *next;
422 struct ftrace_profile records[];
425 struct ftrace_profile_stat {
427 struct hlist_head *hash;
428 struct ftrace_profile_page *pages;
429 struct ftrace_profile_page *start;
430 struct tracer_stat stat;
433 #define PROFILE_RECORDS_SIZE \
434 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
436 #define PROFILES_PER_PAGE \
437 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
439 static int ftrace_profile_enabled __read_mostly;
441 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
442 static DEFINE_MUTEX(ftrace_profile_lock);
444 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
446 #define FTRACE_PROFILE_HASH_BITS 10
447 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
450 function_stat_next(void *v, int idx)
452 struct ftrace_profile *rec = v;
453 struct ftrace_profile_page *pg;
455 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
461 if ((void *)rec >= (void *)&pg->records[pg->index]) {
465 rec = &pg->records[0];
473 static void *function_stat_start(struct tracer_stat *trace)
475 struct ftrace_profile_stat *stat =
476 container_of(trace, struct ftrace_profile_stat, stat);
478 if (!stat || !stat->start)
481 return function_stat_next(&stat->start->records[0], 0);
484 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
485 /* function graph compares on total time */
486 static int function_stat_cmp(const void *p1, const void *p2)
488 const struct ftrace_profile *a = p1;
489 const struct ftrace_profile *b = p2;
491 if (a->time < b->time)
493 if (a->time > b->time)
499 /* not function graph compares against hits */
500 static int function_stat_cmp(const void *p1, const void *p2)
502 const struct ftrace_profile *a = p1;
503 const struct ftrace_profile *b = p2;
505 if (a->counter < b->counter)
507 if (a->counter > b->counter)
514 static int function_stat_headers(struct seq_file *m)
516 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
517 seq_puts(m, " Function "
520 "--- ---- --- ---\n");
522 seq_puts(m, " Function Hit\n"
528 static int function_stat_show(struct seq_file *m, void *v)
530 struct ftrace_profile *rec = v;
531 char str[KSYM_SYMBOL_LEN];
533 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
534 static struct trace_seq s;
535 unsigned long long avg;
536 unsigned long long stddev;
538 mutex_lock(&ftrace_profile_lock);
540 /* we raced with function_profile_reset() */
541 if (unlikely(rec->counter == 0)) {
546 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
547 avg = div64_ul(rec->time, rec->counter);
548 if (tracing_thresh && (avg < tracing_thresh))
552 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
553 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
555 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
558 /* Sample standard deviation (s^2) */
559 if (rec->counter <= 1)
563 * Apply Welford's method:
564 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
566 stddev = rec->counter * rec->time_squared -
567 rec->time * rec->time;
570 * Divide only 1000 for ns^2 -> us^2 conversion.
571 * trace_print_graph_duration will divide 1000 again.
573 stddev = div64_ul(stddev,
574 rec->counter * (rec->counter - 1) * 1000);
578 trace_print_graph_duration(rec->time, &s);
579 trace_seq_puts(&s, " ");
580 trace_print_graph_duration(avg, &s);
581 trace_seq_puts(&s, " ");
582 trace_print_graph_duration(stddev, &s);
583 trace_print_seq(m, &s);
587 mutex_unlock(&ftrace_profile_lock);
592 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
594 struct ftrace_profile_page *pg;
596 pg = stat->pages = stat->start;
599 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
604 memset(stat->hash, 0,
605 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
608 static int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
610 struct ftrace_profile_page *pg;
615 /* If we already allocated, do nothing */
619 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
623 #ifdef CONFIG_DYNAMIC_FTRACE
624 functions = ftrace_update_tot_cnt;
627 * We do not know the number of functions that exist because
628 * dynamic tracing is what counts them. With past experience
629 * we have around 20K functions. That should be more than enough.
630 * It is highly unlikely we will execute every function in
636 pg = stat->start = stat->pages;
638 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
640 for (i = 1; i < pages; i++) {
641 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
652 unsigned long tmp = (unsigned long)pg;
664 static int ftrace_profile_init_cpu(int cpu)
666 struct ftrace_profile_stat *stat;
669 stat = &per_cpu(ftrace_profile_stats, cpu);
672 /* If the profile is already created, simply reset it */
673 ftrace_profile_reset(stat);
678 * We are profiling all functions, but usually only a few thousand
679 * functions are hit. We'll make a hash of 1024 items.
681 size = FTRACE_PROFILE_HASH_SIZE;
683 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
688 /* Preallocate the function profiling pages */
689 if (ftrace_profile_pages_init(stat) < 0) {
698 static int ftrace_profile_init(void)
703 for_each_possible_cpu(cpu) {
704 ret = ftrace_profile_init_cpu(cpu);
712 /* interrupts must be disabled */
713 static struct ftrace_profile *
714 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
716 struct ftrace_profile *rec;
717 struct hlist_head *hhd;
720 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
721 hhd = &stat->hash[key];
723 if (hlist_empty(hhd))
726 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
734 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
735 struct ftrace_profile *rec)
739 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
740 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
744 * The memory is already allocated, this simply finds a new record to use.
746 static struct ftrace_profile *
747 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
749 struct ftrace_profile *rec = NULL;
751 /* prevent recursion (from NMIs) */
752 if (atomic_inc_return(&stat->disabled) != 1)
756 * Try to find the function again since an NMI
757 * could have added it
759 rec = ftrace_find_profiled_func(stat, ip);
763 if (stat->pages->index == PROFILES_PER_PAGE) {
764 if (!stat->pages->next)
766 stat->pages = stat->pages->next;
769 rec = &stat->pages->records[stat->pages->index++];
771 ftrace_add_profile(stat, rec);
774 atomic_dec(&stat->disabled);
780 function_profile_call(unsigned long ip, unsigned long parent_ip,
781 struct ftrace_ops *ops, struct ftrace_regs *fregs)
783 struct ftrace_profile_stat *stat;
784 struct ftrace_profile *rec;
787 if (!ftrace_profile_enabled)
790 local_irq_save(flags);
792 stat = this_cpu_ptr(&ftrace_profile_stats);
793 if (!stat->hash || !ftrace_profile_enabled)
796 rec = ftrace_find_profiled_func(stat, ip);
798 rec = ftrace_profile_alloc(stat, ip);
805 local_irq_restore(flags);
808 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
809 static bool fgraph_graph_time = true;
811 void ftrace_graph_graph_time_control(bool enable)
813 fgraph_graph_time = enable;
816 static int profile_graph_entry(struct ftrace_graph_ent *trace)
818 struct ftrace_ret_stack *ret_stack;
820 function_profile_call(trace->func, 0, NULL, NULL);
822 /* If function graph is shutting down, ret_stack can be NULL */
823 if (!current->ret_stack)
826 ret_stack = ftrace_graph_get_ret_stack(current, 0);
828 ret_stack->subtime = 0;
833 static void profile_graph_return(struct ftrace_graph_ret *trace)
835 struct ftrace_ret_stack *ret_stack;
836 struct ftrace_profile_stat *stat;
837 unsigned long long calltime;
838 struct ftrace_profile *rec;
841 local_irq_save(flags);
842 stat = this_cpu_ptr(&ftrace_profile_stats);
843 if (!stat->hash || !ftrace_profile_enabled)
846 /* If the calltime was zero'd ignore it */
847 if (!trace->calltime)
850 calltime = trace->rettime - trace->calltime;
852 if (!fgraph_graph_time) {
854 /* Append this call time to the parent time to subtract */
855 ret_stack = ftrace_graph_get_ret_stack(current, 1);
857 ret_stack->subtime += calltime;
859 ret_stack = ftrace_graph_get_ret_stack(current, 0);
860 if (ret_stack && ret_stack->subtime < calltime)
861 calltime -= ret_stack->subtime;
866 rec = ftrace_find_profiled_func(stat, trace->func);
868 rec->time += calltime;
869 rec->time_squared += calltime * calltime;
873 local_irq_restore(flags);
876 static struct fgraph_ops fprofiler_ops = {
877 .entryfunc = &profile_graph_entry,
878 .retfunc = &profile_graph_return,
881 static int register_ftrace_profiler(void)
883 return register_ftrace_graph(&fprofiler_ops);
886 static void unregister_ftrace_profiler(void)
888 unregister_ftrace_graph(&fprofiler_ops);
891 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
892 .func = function_profile_call,
893 .flags = FTRACE_OPS_FL_INITIALIZED,
894 INIT_OPS_HASH(ftrace_profile_ops)
897 static int register_ftrace_profiler(void)
899 return register_ftrace_function(&ftrace_profile_ops);
902 static void unregister_ftrace_profiler(void)
904 unregister_ftrace_function(&ftrace_profile_ops);
906 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
909 ftrace_profile_write(struct file *filp, const char __user *ubuf,
910 size_t cnt, loff_t *ppos)
915 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
921 mutex_lock(&ftrace_profile_lock);
922 if (ftrace_profile_enabled ^ val) {
924 ret = ftrace_profile_init();
930 ret = register_ftrace_profiler();
935 ftrace_profile_enabled = 1;
937 ftrace_profile_enabled = 0;
939 * unregister_ftrace_profiler calls stop_machine
940 * so this acts like an synchronize_rcu.
942 unregister_ftrace_profiler();
946 mutex_unlock(&ftrace_profile_lock);
954 ftrace_profile_read(struct file *filp, char __user *ubuf,
955 size_t cnt, loff_t *ppos)
957 char buf[64]; /* big enough to hold a number */
960 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
961 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
964 static const struct file_operations ftrace_profile_fops = {
965 .open = tracing_open_generic,
966 .read = ftrace_profile_read,
967 .write = ftrace_profile_write,
968 .llseek = default_llseek,
971 /* used to initialize the real stat files */
972 static struct tracer_stat function_stats __initdata = {
974 .stat_start = function_stat_start,
975 .stat_next = function_stat_next,
976 .stat_cmp = function_stat_cmp,
977 .stat_headers = function_stat_headers,
978 .stat_show = function_stat_show
981 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
983 struct ftrace_profile_stat *stat;
988 for_each_possible_cpu(cpu) {
989 stat = &per_cpu(ftrace_profile_stats, cpu);
991 name = kasprintf(GFP_KERNEL, "function%d", cpu);
994 * The files created are permanent, if something happens
995 * we still do not free memory.
998 "Could not allocate stat file for cpu %d\n",
1002 stat->stat = function_stats;
1003 stat->stat.name = name;
1004 ret = register_stat_tracer(&stat->stat);
1007 "Could not register function stat for cpu %d\n",
1014 trace_create_file("function_profile_enabled",
1015 TRACE_MODE_WRITE, d_tracer, NULL,
1016 &ftrace_profile_fops);
1019 #else /* CONFIG_FUNCTION_PROFILER */
1020 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1023 #endif /* CONFIG_FUNCTION_PROFILER */
1025 #ifdef CONFIG_DYNAMIC_FTRACE
1027 static struct ftrace_ops *removed_ops;
1030 * Set when doing a global update, like enabling all recs or disabling them.
1031 * It is not set when just updating a single ftrace_ops.
1033 static bool update_all_ops;
1035 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1036 # error Dynamic ftrace depends on MCOUNT_RECORD
1039 struct ftrace_func_probe {
1040 struct ftrace_probe_ops *probe_ops;
1041 struct ftrace_ops ops;
1042 struct trace_array *tr;
1043 struct list_head list;
1049 * We make these constant because no one should touch them,
1050 * but they are used as the default "empty hash", to avoid allocating
1051 * it all the time. These are in a read only section such that if
1052 * anyone does try to modify it, it will cause an exception.
1054 static const struct hlist_head empty_buckets[1];
1055 static const struct ftrace_hash empty_hash = {
1056 .buckets = (struct hlist_head *)empty_buckets,
1058 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1060 struct ftrace_ops global_ops = {
1061 .func = ftrace_stub,
1062 .local_hash.notrace_hash = EMPTY_HASH,
1063 .local_hash.filter_hash = EMPTY_HASH,
1064 INIT_OPS_HASH(global_ops)
1065 .flags = FTRACE_OPS_FL_INITIALIZED |
1070 * Used by the stack unwinder to know about dynamic ftrace trampolines.
1072 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1074 struct ftrace_ops *op = NULL;
1077 * Some of the ops may be dynamically allocated,
1078 * they are freed after a synchronize_rcu().
1080 preempt_disable_notrace();
1082 do_for_each_ftrace_op(op, ftrace_ops_list) {
1084 * This is to check for dynamically allocated trampolines.
1085 * Trampolines that are in kernel text will have
1086 * core_kernel_text() return true.
1088 if (op->trampoline && op->trampoline_size)
1089 if (addr >= op->trampoline &&
1090 addr < op->trampoline + op->trampoline_size) {
1091 preempt_enable_notrace();
1094 } while_for_each_ftrace_op(op);
1095 preempt_enable_notrace();
1101 * This is used by __kernel_text_address() to return true if the
1102 * address is on a dynamically allocated trampoline that would
1103 * not return true for either core_kernel_text() or
1104 * is_module_text_address().
1106 bool is_ftrace_trampoline(unsigned long addr)
1108 return ftrace_ops_trampoline(addr) != NULL;
1111 struct ftrace_page {
1112 struct ftrace_page *next;
1113 struct dyn_ftrace *records;
1118 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1119 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1121 static struct ftrace_page *ftrace_pages_start;
1122 static struct ftrace_page *ftrace_pages;
1124 static __always_inline unsigned long
1125 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1127 if (hash->size_bits > 0)
1128 return hash_long(ip, hash->size_bits);
1133 /* Only use this function if ftrace_hash_empty() has already been tested */
1134 static __always_inline struct ftrace_func_entry *
1135 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1138 struct ftrace_func_entry *entry;
1139 struct hlist_head *hhd;
1141 key = ftrace_hash_key(hash, ip);
1142 hhd = &hash->buckets[key];
1144 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1145 if (entry->ip == ip)
1152 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1153 * @hash: The hash to look at
1154 * @ip: The instruction pointer to test
1156 * Search a given @hash to see if a given instruction pointer (@ip)
1159 * Returns the entry that holds the @ip if found. NULL otherwise.
1161 struct ftrace_func_entry *
1162 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1164 if (ftrace_hash_empty(hash))
1167 return __ftrace_lookup_ip(hash, ip);
1170 static void __add_hash_entry(struct ftrace_hash *hash,
1171 struct ftrace_func_entry *entry)
1173 struct hlist_head *hhd;
1176 key = ftrace_hash_key(hash, entry->ip);
1177 hhd = &hash->buckets[key];
1178 hlist_add_head(&entry->hlist, hhd);
1182 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1184 struct ftrace_func_entry *entry;
1186 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1191 __add_hash_entry(hash, entry);
1197 free_hash_entry(struct ftrace_hash *hash,
1198 struct ftrace_func_entry *entry)
1200 hlist_del(&entry->hlist);
1206 remove_hash_entry(struct ftrace_hash *hash,
1207 struct ftrace_func_entry *entry)
1209 hlist_del_rcu(&entry->hlist);
1213 static void ftrace_hash_clear(struct ftrace_hash *hash)
1215 struct hlist_head *hhd;
1216 struct hlist_node *tn;
1217 struct ftrace_func_entry *entry;
1218 int size = 1 << hash->size_bits;
1224 for (i = 0; i < size; i++) {
1225 hhd = &hash->buckets[i];
1226 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1227 free_hash_entry(hash, entry);
1229 FTRACE_WARN_ON(hash->count);
1232 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1234 list_del(&ftrace_mod->list);
1235 kfree(ftrace_mod->module);
1236 kfree(ftrace_mod->func);
1240 static void clear_ftrace_mod_list(struct list_head *head)
1242 struct ftrace_mod_load *p, *n;
1244 /* stack tracer isn't supported yet */
1248 mutex_lock(&ftrace_lock);
1249 list_for_each_entry_safe(p, n, head, list)
1251 mutex_unlock(&ftrace_lock);
1254 static void free_ftrace_hash(struct ftrace_hash *hash)
1256 if (!hash || hash == EMPTY_HASH)
1258 ftrace_hash_clear(hash);
1259 kfree(hash->buckets);
1263 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1265 struct ftrace_hash *hash;
1267 hash = container_of(rcu, struct ftrace_hash, rcu);
1268 free_ftrace_hash(hash);
1271 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1273 if (!hash || hash == EMPTY_HASH)
1275 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
1279 * ftrace_free_filter - remove all filters for an ftrace_ops
1280 * @ops - the ops to remove the filters from
1282 void ftrace_free_filter(struct ftrace_ops *ops)
1284 ftrace_ops_init(ops);
1285 free_ftrace_hash(ops->func_hash->filter_hash);
1286 free_ftrace_hash(ops->func_hash->notrace_hash);
1288 EXPORT_SYMBOL_GPL(ftrace_free_filter);
1290 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1292 struct ftrace_hash *hash;
1295 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1299 size = 1 << size_bits;
1300 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1302 if (!hash->buckets) {
1307 hash->size_bits = size_bits;
1313 static int ftrace_add_mod(struct trace_array *tr,
1314 const char *func, const char *module,
1317 struct ftrace_mod_load *ftrace_mod;
1318 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1320 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1324 INIT_LIST_HEAD(&ftrace_mod->list);
1325 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1326 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1327 ftrace_mod->enable = enable;
1329 if (!ftrace_mod->func || !ftrace_mod->module)
1332 list_add(&ftrace_mod->list, mod_head);
1337 free_ftrace_mod(ftrace_mod);
1342 static struct ftrace_hash *
1343 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1345 struct ftrace_func_entry *entry;
1346 struct ftrace_hash *new_hash;
1351 new_hash = alloc_ftrace_hash(size_bits);
1356 new_hash->flags = hash->flags;
1359 if (ftrace_hash_empty(hash))
1362 size = 1 << hash->size_bits;
1363 for (i = 0; i < size; i++) {
1364 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1365 ret = add_hash_entry(new_hash, entry->ip);
1371 FTRACE_WARN_ON(new_hash->count != hash->count);
1376 free_ftrace_hash(new_hash);
1381 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1383 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1385 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1386 struct ftrace_hash *new_hash);
1388 static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
1390 struct ftrace_func_entry *entry;
1391 struct ftrace_hash *new_hash;
1392 struct hlist_head *hhd;
1393 struct hlist_node *tn;
1398 * Use around half the size (max bit of it), but
1399 * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits).
1401 bits = fls(size / 2);
1403 /* Don't allocate too much */
1404 if (bits > FTRACE_HASH_MAX_BITS)
1405 bits = FTRACE_HASH_MAX_BITS;
1407 new_hash = alloc_ftrace_hash(bits);
1411 new_hash->flags = src->flags;
1413 size = 1 << src->size_bits;
1414 for (i = 0; i < size; i++) {
1415 hhd = &src->buckets[i];
1416 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1417 remove_hash_entry(src, entry);
1418 __add_hash_entry(new_hash, entry);
1424 static struct ftrace_hash *
1425 __ftrace_hash_move(struct ftrace_hash *src)
1427 int size = src->count;
1430 * If the new source is empty, just return the empty_hash.
1432 if (ftrace_hash_empty(src))
1435 return dup_hash(src, size);
1439 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1440 struct ftrace_hash **dst, struct ftrace_hash *src)
1442 struct ftrace_hash *new_hash;
1445 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1446 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1449 new_hash = __ftrace_hash_move(src);
1453 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1455 /* IPMODIFY should be updated only when filter_hash updating */
1456 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1458 free_ftrace_hash(new_hash);
1464 * Remove the current set, update the hash and add
1467 ftrace_hash_rec_disable_modify(ops, enable);
1469 rcu_assign_pointer(*dst, new_hash);
1471 ftrace_hash_rec_enable_modify(ops, enable);
1476 static bool hash_contains_ip(unsigned long ip,
1477 struct ftrace_ops_hash *hash)
1480 * The function record is a match if it exists in the filter
1481 * hash and not in the notrace hash. Note, an empty hash is
1482 * considered a match for the filter hash, but an empty
1483 * notrace hash is considered not in the notrace hash.
1485 return (ftrace_hash_empty(hash->filter_hash) ||
1486 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1487 (ftrace_hash_empty(hash->notrace_hash) ||
1488 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1492 * Test the hashes for this ops to see if we want to call
1493 * the ops->func or not.
1495 * It's a match if the ip is in the ops->filter_hash or
1496 * the filter_hash does not exist or is empty,
1498 * the ip is not in the ops->notrace_hash.
1500 * This needs to be called with preemption disabled as
1501 * the hashes are freed with call_rcu().
1504 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1506 struct ftrace_ops_hash hash;
1509 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1511 * There's a small race when adding ops that the ftrace handler
1512 * that wants regs, may be called without them. We can not
1513 * allow that handler to be called if regs is NULL.
1515 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1519 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1520 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1522 if (hash_contains_ip(ip, &hash))
1531 * This is a double for. Do not use 'break' to break out of the loop,
1532 * you must use a goto.
1534 #define do_for_each_ftrace_rec(pg, rec) \
1535 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1537 for (_____i = 0; _____i < pg->index; _____i++) { \
1538 rec = &pg->records[_____i];
1540 #define while_for_each_ftrace_rec() \
1545 static int ftrace_cmp_recs(const void *a, const void *b)
1547 const struct dyn_ftrace *key = a;
1548 const struct dyn_ftrace *rec = b;
1550 if (key->flags < rec->ip)
1552 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1557 static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
1559 struct ftrace_page *pg;
1560 struct dyn_ftrace *rec = NULL;
1561 struct dyn_ftrace key;
1564 key.flags = end; /* overload flags, as it is unsigned long */
1566 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1567 if (pg->index == 0 ||
1568 end < pg->records[0].ip ||
1569 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1571 rec = bsearch(&key, pg->records, pg->index,
1572 sizeof(struct dyn_ftrace),
1581 * ftrace_location_range - return the first address of a traced location
1582 * if it touches the given ip range
1583 * @start: start of range to search.
1584 * @end: end of range to search (inclusive). @end points to the last byte
1587 * Returns rec->ip if the related ftrace location is a least partly within
1588 * the given address range. That is, the first address of the instruction
1589 * that is either a NOP or call to the function tracer. It checks the ftrace
1590 * internal tables to determine if the address belongs or not.
1592 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1594 struct dyn_ftrace *rec;
1596 rec = lookup_rec(start, end);
1604 * ftrace_location - return the ftrace location
1605 * @ip: the instruction pointer to check
1607 * If @ip matches the ftrace location, return @ip.
1608 * If @ip matches sym+0, return sym's ftrace location.
1609 * Otherwise, return 0.
1611 unsigned long ftrace_location(unsigned long ip)
1613 struct dyn_ftrace *rec;
1614 unsigned long offset;
1617 rec = lookup_rec(ip, ip);
1619 if (!kallsyms_lookup_size_offset(ip, &size, &offset))
1622 /* map sym+0 to __fentry__ */
1624 rec = lookup_rec(ip, ip + size - 1);
1635 * ftrace_text_reserved - return true if range contains an ftrace location
1636 * @start: start of range to search
1637 * @end: end of range to search (inclusive). @end points to the last byte to check.
1639 * Returns 1 if @start and @end contains a ftrace location.
1640 * That is, the instruction that is either a NOP or call to
1641 * the function tracer. It checks the ftrace internal tables to
1642 * determine if the address belongs or not.
1644 int ftrace_text_reserved(const void *start, const void *end)
1648 ret = ftrace_location_range((unsigned long)start,
1649 (unsigned long)end);
1654 /* Test if ops registered to this rec needs regs */
1655 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1657 struct ftrace_ops *ops;
1658 bool keep_regs = false;
1660 for (ops = ftrace_ops_list;
1661 ops != &ftrace_list_end; ops = ops->next) {
1662 /* pass rec in as regs to have non-NULL val */
1663 if (ftrace_ops_test(ops, rec->ip, rec)) {
1664 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1674 static struct ftrace_ops *
1675 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1676 static struct ftrace_ops *
1677 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1678 static struct ftrace_ops *
1679 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1681 static bool skip_record(struct dyn_ftrace *rec)
1684 * At boot up, weak functions are set to disable. Function tracing
1685 * can be enabled before they are, and they still need to be disabled now.
1686 * If the record is disabled, still continue if it is marked as already
1687 * enabled (this is needed to keep the accounting working).
1689 return rec->flags & FTRACE_FL_DISABLED &&
1690 !(rec->flags & FTRACE_FL_ENABLED);
1693 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1697 struct ftrace_hash *hash;
1698 struct ftrace_hash *other_hash;
1699 struct ftrace_page *pg;
1700 struct dyn_ftrace *rec;
1701 bool update = false;
1705 /* Only update if the ops has been registered */
1706 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1710 * In the filter_hash case:
1711 * If the count is zero, we update all records.
1712 * Otherwise we just update the items in the hash.
1714 * In the notrace_hash case:
1715 * We enable the update in the hash.
1716 * As disabling notrace means enabling the tracing,
1717 * and enabling notrace means disabling, the inc variable
1721 hash = ops->func_hash->filter_hash;
1722 other_hash = ops->func_hash->notrace_hash;
1723 if (ftrace_hash_empty(hash))
1727 hash = ops->func_hash->notrace_hash;
1728 other_hash = ops->func_hash->filter_hash;
1730 * If the notrace hash has no items,
1731 * then there's nothing to do.
1733 if (ftrace_hash_empty(hash))
1737 do_for_each_ftrace_rec(pg, rec) {
1738 int in_other_hash = 0;
1742 if (skip_record(rec))
1747 * Only the filter_hash affects all records.
1748 * Update if the record is not in the notrace hash.
1750 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1753 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1754 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1757 * If filter_hash is set, we want to match all functions
1758 * that are in the hash but not in the other hash.
1760 * If filter_hash is not set, then we are decrementing.
1761 * That means we match anything that is in the hash
1762 * and also in the other_hash. That is, we need to turn
1763 * off functions in the other hash because they are disabled
1766 if (filter_hash && in_hash && !in_other_hash)
1768 else if (!filter_hash && in_hash &&
1769 (in_other_hash || ftrace_hash_empty(other_hash)))
1777 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1780 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1781 rec->flags |= FTRACE_FL_DIRECT;
1784 * If there's only a single callback registered to a
1785 * function, and the ops has a trampoline registered
1786 * for it, then we can call it directly.
1788 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1789 rec->flags |= FTRACE_FL_TRAMP;
1792 * If we are adding another function callback
1793 * to this function, and the previous had a
1794 * custom trampoline in use, then we need to go
1795 * back to the default trampoline.
1797 rec->flags &= ~FTRACE_FL_TRAMP;
1800 * If any ops wants regs saved for this function
1801 * then all ops will get saved regs.
1803 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1804 rec->flags |= FTRACE_FL_REGS;
1806 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1811 * Only the internal direct_ops should have the
1812 * DIRECT flag set. Thus, if it is removing a
1813 * function, then that function should no longer
1816 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1817 rec->flags &= ~FTRACE_FL_DIRECT;
1820 * If the rec had REGS enabled and the ops that is
1821 * being removed had REGS set, then see if there is
1822 * still any ops for this record that wants regs.
1823 * If not, we can stop recording them.
1825 if (ftrace_rec_count(rec) > 0 &&
1826 rec->flags & FTRACE_FL_REGS &&
1827 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1828 if (!test_rec_ops_needs_regs(rec))
1829 rec->flags &= ~FTRACE_FL_REGS;
1833 * The TRAMP needs to be set only if rec count
1834 * is decremented to one, and the ops that is
1835 * left has a trampoline. As TRAMP can only be
1836 * enabled if there is only a single ops attached
1839 if (ftrace_rec_count(rec) == 1 &&
1840 ftrace_find_tramp_ops_any_other(rec, ops))
1841 rec->flags |= FTRACE_FL_TRAMP;
1843 rec->flags &= ~FTRACE_FL_TRAMP;
1846 * flags will be cleared in ftrace_check_record()
1847 * if rec count is zero.
1852 * If the rec has a single associated ops, and ops->func can be
1853 * called directly, allow the call site to call via the ops.
1855 if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS) &&
1856 ftrace_rec_count(rec) == 1 &&
1857 ftrace_ops_get_func(ops) == ops->func)
1858 rec->flags |= FTRACE_FL_CALL_OPS;
1860 rec->flags &= ~FTRACE_FL_CALL_OPS;
1864 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1865 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
1867 /* Shortcut, if we handled all records, we are done. */
1868 if (!all && count == hash->count)
1870 } while_for_each_ftrace_rec();
1875 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1878 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1881 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1884 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1887 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1888 int filter_hash, int inc)
1890 struct ftrace_ops *op;
1892 __ftrace_hash_rec_update(ops, filter_hash, inc);
1894 if (ops->func_hash != &global_ops.local_hash)
1898 * If the ops shares the global_ops hash, then we need to update
1899 * all ops that are enabled and use this hash.
1901 do_for_each_ftrace_op(op, ftrace_ops_list) {
1905 if (op->func_hash == &global_ops.local_hash)
1906 __ftrace_hash_rec_update(op, filter_hash, inc);
1907 } while_for_each_ftrace_op(op);
1910 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1913 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1916 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1919 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1923 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1924 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1925 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1926 * Note that old_hash and new_hash has below meanings
1927 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1928 * - If the hash is EMPTY_HASH, it hits nothing
1929 * - Anything else hits the recs which match the hash entries.
1931 * DIRECT ops does not have IPMODIFY flag, but we still need to check it
1932 * against functions with FTRACE_FL_IPMODIFY. If there is any overlap, call
1933 * ops_func(SHARE_IPMODIFY_SELF) to make sure current ops can share with
1934 * IPMODIFY. If ops_func(SHARE_IPMODIFY_SELF) returns non-zero, propagate
1935 * the return value to the caller and eventually to the owner of the DIRECT
1938 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1939 struct ftrace_hash *old_hash,
1940 struct ftrace_hash *new_hash)
1942 struct ftrace_page *pg;
1943 struct dyn_ftrace *rec, *end = NULL;
1945 bool is_ipmodify, is_direct;
1947 /* Only update if the ops has been registered */
1948 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1951 is_ipmodify = ops->flags & FTRACE_OPS_FL_IPMODIFY;
1952 is_direct = ops->flags & FTRACE_OPS_FL_DIRECT;
1954 /* neither IPMODIFY nor DIRECT, skip */
1955 if (!is_ipmodify && !is_direct)
1958 if (WARN_ON_ONCE(is_ipmodify && is_direct))
1962 * Since the IPMODIFY and DIRECT are very address sensitive
1963 * actions, we do not allow ftrace_ops to set all functions to new
1966 if (!new_hash || !old_hash)
1969 /* Update rec->flags */
1970 do_for_each_ftrace_rec(pg, rec) {
1972 if (rec->flags & FTRACE_FL_DISABLED)
1975 /* We need to update only differences of filter_hash */
1976 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1977 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1978 if (in_old == in_new)
1982 if (rec->flags & FTRACE_FL_IPMODIFY) {
1985 /* Cannot have two ipmodify on same rec */
1989 FTRACE_WARN_ON(rec->flags & FTRACE_FL_DIRECT);
1992 * Another ops with IPMODIFY is already
1993 * attached. We are now attaching a direct
1994 * ops. Run SHARE_IPMODIFY_SELF, to check
1995 * whether sharing is supported.
1999 ret = ops->ops_func(ops, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF);
2002 } else if (is_ipmodify) {
2003 rec->flags |= FTRACE_FL_IPMODIFY;
2005 } else if (is_ipmodify) {
2006 rec->flags &= ~FTRACE_FL_IPMODIFY;
2008 } while_for_each_ftrace_rec();
2015 /* Roll back what we did above */
2016 do_for_each_ftrace_rec(pg, rec) {
2018 if (rec->flags & FTRACE_FL_DISABLED)
2024 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
2025 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
2026 if (in_old == in_new)
2030 rec->flags &= ~FTRACE_FL_IPMODIFY;
2032 rec->flags |= FTRACE_FL_IPMODIFY;
2033 } while_for_each_ftrace_rec();
2039 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
2041 struct ftrace_hash *hash = ops->func_hash->filter_hash;
2043 if (ftrace_hash_empty(hash))
2046 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
2049 /* Disabling always succeeds */
2050 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
2052 struct ftrace_hash *hash = ops->func_hash->filter_hash;
2054 if (ftrace_hash_empty(hash))
2057 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
2060 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
2061 struct ftrace_hash *new_hash)
2063 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
2065 if (ftrace_hash_empty(old_hash))
2068 if (ftrace_hash_empty(new_hash))
2071 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
2074 static void print_ip_ins(const char *fmt, const unsigned char *p)
2076 char ins[MCOUNT_INSN_SIZE];
2078 if (copy_from_kernel_nofault(ins, p, MCOUNT_INSN_SIZE)) {
2079 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
2083 printk(KERN_CONT "%s", fmt);
2084 pr_cont("%*phC", MCOUNT_INSN_SIZE, ins);
2087 enum ftrace_bug_type ftrace_bug_type;
2088 const void *ftrace_expected;
2090 static void print_bug_type(void)
2092 switch (ftrace_bug_type) {
2093 case FTRACE_BUG_UNKNOWN:
2095 case FTRACE_BUG_INIT:
2096 pr_info("Initializing ftrace call sites\n");
2098 case FTRACE_BUG_NOP:
2099 pr_info("Setting ftrace call site to NOP\n");
2101 case FTRACE_BUG_CALL:
2102 pr_info("Setting ftrace call site to call ftrace function\n");
2104 case FTRACE_BUG_UPDATE:
2105 pr_info("Updating ftrace call site to call a different ftrace function\n");
2111 * ftrace_bug - report and shutdown function tracer
2112 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2113 * @rec: The record that failed
2115 * The arch code that enables or disables the function tracing
2116 * can call ftrace_bug() when it has detected a problem in
2117 * modifying the code. @failed should be one of either:
2118 * EFAULT - if the problem happens on reading the @ip address
2119 * EINVAL - if what is read at @ip is not what was expected
2120 * EPERM - if the problem happens on writing to the @ip address
2122 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2124 unsigned long ip = rec ? rec->ip : 0;
2126 pr_info("------------[ ftrace bug ]------------\n");
2130 pr_info("ftrace faulted on modifying ");
2131 print_ip_sym(KERN_INFO, ip);
2134 pr_info("ftrace failed to modify ");
2135 print_ip_sym(KERN_INFO, ip);
2136 print_ip_ins(" actual: ", (unsigned char *)ip);
2138 if (ftrace_expected) {
2139 print_ip_ins(" expected: ", ftrace_expected);
2144 pr_info("ftrace faulted on writing ");
2145 print_ip_sym(KERN_INFO, ip);
2148 pr_info("ftrace faulted on unknown error ");
2149 print_ip_sym(KERN_INFO, ip);
2153 struct ftrace_ops *ops = NULL;
2155 pr_info("ftrace record flags: %lx\n", rec->flags);
2156 pr_cont(" (%ld)%s%s", ftrace_rec_count(rec),
2157 rec->flags & FTRACE_FL_REGS ? " R" : " ",
2158 rec->flags & FTRACE_FL_CALL_OPS ? " O" : " ");
2159 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2160 ops = ftrace_find_tramp_ops_any(rec);
2163 pr_cont("\ttramp: %pS (%pS)",
2164 (void *)ops->trampoline,
2166 ops = ftrace_find_tramp_ops_next(rec, ops);
2169 pr_cont("\ttramp: ERROR!");
2172 ip = ftrace_get_addr_curr(rec);
2173 pr_cont("\n expected tramp: %lx\n", ip);
2176 FTRACE_WARN_ON_ONCE(1);
2179 static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
2181 unsigned long flag = 0UL;
2183 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2185 if (skip_record(rec))
2186 return FTRACE_UPDATE_IGNORE;
2189 * If we are updating calls:
2191 * If the record has a ref count, then we need to enable it
2192 * because someone is using it.
2194 * Otherwise we make sure its disabled.
2196 * If we are disabling calls, then disable all records that
2199 if (enable && ftrace_rec_count(rec))
2200 flag = FTRACE_FL_ENABLED;
2203 * If enabling and the REGS flag does not match the REGS_EN, or
2204 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2205 * this record. Set flags to fail the compare against ENABLED.
2206 * Same for direct calls.
2209 if (!(rec->flags & FTRACE_FL_REGS) !=
2210 !(rec->flags & FTRACE_FL_REGS_EN))
2211 flag |= FTRACE_FL_REGS;
2213 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2214 !(rec->flags & FTRACE_FL_TRAMP_EN))
2215 flag |= FTRACE_FL_TRAMP;
2218 * Direct calls are special, as count matters.
2219 * We must test the record for direct, if the
2220 * DIRECT and DIRECT_EN do not match, but only
2221 * if the count is 1. That's because, if the
2222 * count is something other than one, we do not
2223 * want the direct enabled (it will be done via the
2224 * direct helper). But if DIRECT_EN is set, and
2225 * the count is not one, we need to clear it.
2228 if (ftrace_rec_count(rec) == 1) {
2229 if (!(rec->flags & FTRACE_FL_DIRECT) !=
2230 !(rec->flags & FTRACE_FL_DIRECT_EN))
2231 flag |= FTRACE_FL_DIRECT;
2232 } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
2233 flag |= FTRACE_FL_DIRECT;
2237 * Ops calls are special, as count matters.
2238 * As with direct calls, they must only be enabled when count
2239 * is one, otherwise they'll be handled via the list ops.
2241 if (ftrace_rec_count(rec) == 1) {
2242 if (!(rec->flags & FTRACE_FL_CALL_OPS) !=
2243 !(rec->flags & FTRACE_FL_CALL_OPS_EN))
2244 flag |= FTRACE_FL_CALL_OPS;
2245 } else if (rec->flags & FTRACE_FL_CALL_OPS_EN) {
2246 flag |= FTRACE_FL_CALL_OPS;
2250 /* If the state of this record hasn't changed, then do nothing */
2251 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2252 return FTRACE_UPDATE_IGNORE;
2255 /* Save off if rec is being enabled (for return value) */
2256 flag ^= rec->flags & FTRACE_FL_ENABLED;
2259 rec->flags |= FTRACE_FL_ENABLED;
2260 if (flag & FTRACE_FL_REGS) {
2261 if (rec->flags & FTRACE_FL_REGS)
2262 rec->flags |= FTRACE_FL_REGS_EN;
2264 rec->flags &= ~FTRACE_FL_REGS_EN;
2266 if (flag & FTRACE_FL_TRAMP) {
2267 if (rec->flags & FTRACE_FL_TRAMP)
2268 rec->flags |= FTRACE_FL_TRAMP_EN;
2270 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2273 if (flag & FTRACE_FL_DIRECT) {
2275 * If there's only one user (direct_ops helper)
2276 * then we can call the direct function
2277 * directly (no ftrace trampoline).
2279 if (ftrace_rec_count(rec) == 1) {
2280 if (rec->flags & FTRACE_FL_DIRECT)
2281 rec->flags |= FTRACE_FL_DIRECT_EN;
2283 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2286 * Can only call directly if there's
2287 * only one callback to the function.
2289 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2293 if (flag & FTRACE_FL_CALL_OPS) {
2294 if (ftrace_rec_count(rec) == 1) {
2295 if (rec->flags & FTRACE_FL_CALL_OPS)
2296 rec->flags |= FTRACE_FL_CALL_OPS_EN;
2298 rec->flags &= ~FTRACE_FL_CALL_OPS_EN;
2301 * Can only call directly if there's
2302 * only one set of associated ops.
2304 rec->flags &= ~FTRACE_FL_CALL_OPS_EN;
2310 * If this record is being updated from a nop, then
2311 * return UPDATE_MAKE_CALL.
2313 * return UPDATE_MODIFY_CALL to tell the caller to convert
2314 * from the save regs, to a non-save regs function or
2315 * vice versa, or from a trampoline call.
2317 if (flag & FTRACE_FL_ENABLED) {
2318 ftrace_bug_type = FTRACE_BUG_CALL;
2319 return FTRACE_UPDATE_MAKE_CALL;
2322 ftrace_bug_type = FTRACE_BUG_UPDATE;
2323 return FTRACE_UPDATE_MODIFY_CALL;
2327 /* If there's no more users, clear all flags */
2328 if (!ftrace_rec_count(rec))
2329 rec->flags &= FTRACE_FL_DISABLED;
2332 * Just disable the record, but keep the ops TRAMP
2333 * and REGS states. The _EN flags must be disabled though.
2335 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2336 FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN |
2337 FTRACE_FL_CALL_OPS_EN);
2340 ftrace_bug_type = FTRACE_BUG_NOP;
2341 return FTRACE_UPDATE_MAKE_NOP;
2345 * ftrace_update_record - set a record that now is tracing or not
2346 * @rec: the record to update
2347 * @enable: set to true if the record is tracing, false to force disable
2349 * The records that represent all functions that can be traced need
2350 * to be updated when tracing has been enabled.
2352 int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
2354 return ftrace_check_record(rec, enable, true);
2358 * ftrace_test_record - check if the record has been enabled or not
2359 * @rec: the record to test
2360 * @enable: set to true to check if enabled, false if it is disabled
2362 * The arch code may need to test if a record is already set to
2363 * tracing to determine how to modify the function code that it
2366 int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
2368 return ftrace_check_record(rec, enable, false);
2371 static struct ftrace_ops *
2372 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2374 struct ftrace_ops *op;
2375 unsigned long ip = rec->ip;
2377 do_for_each_ftrace_op(op, ftrace_ops_list) {
2379 if (!op->trampoline)
2382 if (hash_contains_ip(ip, op->func_hash))
2384 } while_for_each_ftrace_op(op);
2389 static struct ftrace_ops *
2390 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2392 struct ftrace_ops *op;
2393 unsigned long ip = rec->ip;
2395 do_for_each_ftrace_op(op, ftrace_ops_list) {
2397 if (op == op_exclude || !op->trampoline)
2400 if (hash_contains_ip(ip, op->func_hash))
2402 } while_for_each_ftrace_op(op);
2407 static struct ftrace_ops *
2408 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2409 struct ftrace_ops *op)
2411 unsigned long ip = rec->ip;
2413 while_for_each_ftrace_op(op) {
2415 if (!op->trampoline)
2418 if (hash_contains_ip(ip, op->func_hash))
2425 static struct ftrace_ops *
2426 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2428 struct ftrace_ops *op;
2429 unsigned long ip = rec->ip;
2432 * Need to check removed ops first.
2433 * If they are being removed, and this rec has a tramp,
2434 * and this rec is in the ops list, then it would be the
2435 * one with the tramp.
2438 if (hash_contains_ip(ip, &removed_ops->old_hash))
2443 * Need to find the current trampoline for a rec.
2444 * Now, a trampoline is only attached to a rec if there
2445 * was a single 'ops' attached to it. But this can be called
2446 * when we are adding another op to the rec or removing the
2447 * current one. Thus, if the op is being added, we can
2448 * ignore it because it hasn't attached itself to the rec
2451 * If an ops is being modified (hooking to different functions)
2452 * then we don't care about the new functions that are being
2453 * added, just the old ones (that are probably being removed).
2455 * If we are adding an ops to a function that already is using
2456 * a trampoline, it needs to be removed (trampolines are only
2457 * for single ops connected), then an ops that is not being
2458 * modified also needs to be checked.
2460 do_for_each_ftrace_op(op, ftrace_ops_list) {
2462 if (!op->trampoline)
2466 * If the ops is being added, it hasn't gotten to
2467 * the point to be removed from this tree yet.
2469 if (op->flags & FTRACE_OPS_FL_ADDING)
2474 * If the ops is being modified and is in the old
2475 * hash, then it is probably being removed from this
2478 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2479 hash_contains_ip(ip, &op->old_hash))
2482 * If the ops is not being added or modified, and it's
2483 * in its normal filter hash, then this must be the one
2486 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2487 hash_contains_ip(ip, op->func_hash))
2490 } while_for_each_ftrace_op(op);
2495 static struct ftrace_ops *
2496 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2498 struct ftrace_ops *op;
2499 unsigned long ip = rec->ip;
2501 do_for_each_ftrace_op(op, ftrace_ops_list) {
2502 /* pass rec in as regs to have non-NULL val */
2503 if (hash_contains_ip(ip, op->func_hash))
2505 } while_for_each_ftrace_op(op);
2511 ftrace_find_unique_ops(struct dyn_ftrace *rec)
2513 struct ftrace_ops *op, *found = NULL;
2514 unsigned long ip = rec->ip;
2516 do_for_each_ftrace_op(op, ftrace_ops_list) {
2518 if (hash_contains_ip(ip, op->func_hash)) {
2524 } while_for_each_ftrace_op(op);
2529 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2530 /* Protected by rcu_tasks for reading, and direct_mutex for writing */
2531 static struct ftrace_hash *direct_functions = EMPTY_HASH;
2532 static DEFINE_MUTEX(direct_mutex);
2533 int ftrace_direct_func_count;
2536 * Search the direct_functions hash to see if the given instruction pointer
2537 * has a direct caller attached to it.
2539 unsigned long ftrace_find_rec_direct(unsigned long ip)
2541 struct ftrace_func_entry *entry;
2543 entry = __ftrace_lookup_ip(direct_functions, ip);
2547 return entry->direct;
2550 static struct ftrace_func_entry*
2551 ftrace_add_rec_direct(unsigned long ip, unsigned long addr,
2552 struct ftrace_hash **free_hash)
2554 struct ftrace_func_entry *entry;
2556 if (ftrace_hash_empty(direct_functions) ||
2557 direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
2558 struct ftrace_hash *new_hash;
2559 int size = ftrace_hash_empty(direct_functions) ? 0 :
2560 direct_functions->count + 1;
2565 new_hash = dup_hash(direct_functions, size);
2569 *free_hash = direct_functions;
2570 direct_functions = new_hash;
2573 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2578 entry->direct = addr;
2579 __add_hash_entry(direct_functions, entry);
2583 static void call_direct_funcs(unsigned long ip, unsigned long pip,
2584 struct ftrace_ops *ops, struct ftrace_regs *fregs)
2588 addr = ftrace_find_rec_direct(ip);
2592 arch_ftrace_set_direct_caller(fregs, addr);
2595 static struct ftrace_ops direct_ops = {
2596 .func = call_direct_funcs,
2597 .flags = FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS
2598 | FTRACE_OPS_FL_PERMANENT,
2600 * By declaring the main trampoline as this trampoline
2601 * it will never have one allocated for it. Allocated
2602 * trampolines should not call direct functions.
2603 * The direct_ops should only be called by the builtin
2604 * ftrace_regs_caller trampoline.
2606 .trampoline = FTRACE_REGS_ADDR,
2608 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2611 * ftrace_get_addr_new - Get the call address to set to
2612 * @rec: The ftrace record descriptor
2614 * If the record has the FTRACE_FL_REGS set, that means that it
2615 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2616 * is not set, then it wants to convert to the normal callback.
2618 * Returns the address of the trampoline to set to
2620 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2622 struct ftrace_ops *ops;
2625 if ((rec->flags & FTRACE_FL_DIRECT) &&
2626 (ftrace_rec_count(rec) == 1)) {
2627 addr = ftrace_find_rec_direct(rec->ip);
2633 /* Trampolines take precedence over regs */
2634 if (rec->flags & FTRACE_FL_TRAMP) {
2635 ops = ftrace_find_tramp_ops_new(rec);
2636 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2637 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2638 (void *)rec->ip, (void *)rec->ip, rec->flags);
2639 /* Ftrace is shutting down, return anything */
2640 return (unsigned long)FTRACE_ADDR;
2642 return ops->trampoline;
2645 if (rec->flags & FTRACE_FL_REGS)
2646 return (unsigned long)FTRACE_REGS_ADDR;
2648 return (unsigned long)FTRACE_ADDR;
2652 * ftrace_get_addr_curr - Get the call address that is already there
2653 * @rec: The ftrace record descriptor
2655 * The FTRACE_FL_REGS_EN is set when the record already points to
2656 * a function that saves all the regs. Basically the '_EN' version
2657 * represents the current state of the function.
2659 * Returns the address of the trampoline that is currently being called
2661 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2663 struct ftrace_ops *ops;
2666 /* Direct calls take precedence over trampolines */
2667 if (rec->flags & FTRACE_FL_DIRECT_EN) {
2668 addr = ftrace_find_rec_direct(rec->ip);
2674 /* Trampolines take precedence over regs */
2675 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2676 ops = ftrace_find_tramp_ops_curr(rec);
2677 if (FTRACE_WARN_ON(!ops)) {
2678 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2679 (void *)rec->ip, (void *)rec->ip);
2680 /* Ftrace is shutting down, return anything */
2681 return (unsigned long)FTRACE_ADDR;
2683 return ops->trampoline;
2686 if (rec->flags & FTRACE_FL_REGS_EN)
2687 return (unsigned long)FTRACE_REGS_ADDR;
2689 return (unsigned long)FTRACE_ADDR;
2693 __ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
2695 unsigned long ftrace_old_addr;
2696 unsigned long ftrace_addr;
2699 ftrace_addr = ftrace_get_addr_new(rec);
2701 /* This needs to be done before we call ftrace_update_record */
2702 ftrace_old_addr = ftrace_get_addr_curr(rec);
2704 ret = ftrace_update_record(rec, enable);
2706 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2709 case FTRACE_UPDATE_IGNORE:
2712 case FTRACE_UPDATE_MAKE_CALL:
2713 ftrace_bug_type = FTRACE_BUG_CALL;
2714 return ftrace_make_call(rec, ftrace_addr);
2716 case FTRACE_UPDATE_MAKE_NOP:
2717 ftrace_bug_type = FTRACE_BUG_NOP;
2718 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2720 case FTRACE_UPDATE_MODIFY_CALL:
2721 ftrace_bug_type = FTRACE_BUG_UPDATE;
2722 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2725 return -1; /* unknown ftrace bug */
2728 void __weak ftrace_replace_code(int mod_flags)
2730 struct dyn_ftrace *rec;
2731 struct ftrace_page *pg;
2732 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2733 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
2736 if (unlikely(ftrace_disabled))
2739 do_for_each_ftrace_rec(pg, rec) {
2741 if (skip_record(rec))
2744 failed = __ftrace_replace_code(rec, enable);
2746 ftrace_bug(failed, rec);
2747 /* Stop processing */
2752 } while_for_each_ftrace_rec();
2755 struct ftrace_rec_iter {
2756 struct ftrace_page *pg;
2761 * ftrace_rec_iter_start - start up iterating over traced functions
2763 * Returns an iterator handle that is used to iterate over all
2764 * the records that represent address locations where functions
2767 * May return NULL if no records are available.
2769 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2772 * We only use a single iterator.
2773 * Protected by the ftrace_lock mutex.
2775 static struct ftrace_rec_iter ftrace_rec_iter;
2776 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2778 iter->pg = ftrace_pages_start;
2781 /* Could have empty pages */
2782 while (iter->pg && !iter->pg->index)
2783 iter->pg = iter->pg->next;
2792 * ftrace_rec_iter_next - get the next record to process.
2793 * @iter: The handle to the iterator.
2795 * Returns the next iterator after the given iterator @iter.
2797 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2801 if (iter->index >= iter->pg->index) {
2802 iter->pg = iter->pg->next;
2805 /* Could have empty pages */
2806 while (iter->pg && !iter->pg->index)
2807 iter->pg = iter->pg->next;
2817 * ftrace_rec_iter_record - get the record at the iterator location
2818 * @iter: The current iterator location
2820 * Returns the record that the current @iter is at.
2822 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2824 return &iter->pg->records[iter->index];
2828 ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
2832 if (unlikely(ftrace_disabled))
2835 ret = ftrace_init_nop(mod, rec);
2837 ftrace_bug_type = FTRACE_BUG_INIT;
2838 ftrace_bug(ret, rec);
2845 * archs can override this function if they must do something
2846 * before the modifying code is performed.
2848 void __weak ftrace_arch_code_modify_prepare(void)
2853 * archs can override this function if they must do something
2854 * after the modifying code is performed.
2856 void __weak ftrace_arch_code_modify_post_process(void)
2860 static int update_ftrace_func(ftrace_func_t func)
2862 static ftrace_func_t save_func;
2864 /* Avoid updating if it hasn't changed */
2865 if (func == save_func)
2870 return ftrace_update_ftrace_func(func);
2873 void ftrace_modify_all_code(int command)
2875 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2879 if (command & FTRACE_MAY_SLEEP)
2880 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2883 * If the ftrace_caller calls a ftrace_ops func directly,
2884 * we need to make sure that it only traces functions it
2885 * expects to trace. When doing the switch of functions,
2886 * we need to update to the ftrace_ops_list_func first
2887 * before the transition between old and new calls are set,
2888 * as the ftrace_ops_list_func will check the ops hashes
2889 * to make sure the ops are having the right functions
2893 err = update_ftrace_func(ftrace_ops_list_func);
2894 if (FTRACE_WARN_ON(err))
2898 if (command & FTRACE_UPDATE_CALLS)
2899 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
2900 else if (command & FTRACE_DISABLE_CALLS)
2901 ftrace_replace_code(mod_flags);
2903 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2904 function_trace_op = set_function_trace_op;
2906 /* If irqs are disabled, we are in stop machine */
2907 if (!irqs_disabled())
2908 smp_call_function(ftrace_sync_ipi, NULL, 1);
2909 err = update_ftrace_func(ftrace_trace_function);
2910 if (FTRACE_WARN_ON(err))
2914 if (command & FTRACE_START_FUNC_RET)
2915 err = ftrace_enable_ftrace_graph_caller();
2916 else if (command & FTRACE_STOP_FUNC_RET)
2917 err = ftrace_disable_ftrace_graph_caller();
2918 FTRACE_WARN_ON(err);
2921 static int __ftrace_modify_code(void *data)
2923 int *command = data;
2925 ftrace_modify_all_code(*command);
2931 * ftrace_run_stop_machine - go back to the stop machine method
2932 * @command: The command to tell ftrace what to do
2934 * If an arch needs to fall back to the stop machine method, the
2935 * it can call this function.
2937 void ftrace_run_stop_machine(int command)
2939 stop_machine(__ftrace_modify_code, &command, NULL);
2943 * arch_ftrace_update_code - modify the code to trace or not trace
2944 * @command: The command that needs to be done
2946 * Archs can override this function if it does not need to
2947 * run stop_machine() to modify code.
2949 void __weak arch_ftrace_update_code(int command)
2951 ftrace_run_stop_machine(command);
2954 static void ftrace_run_update_code(int command)
2956 ftrace_arch_code_modify_prepare();
2959 * By default we use stop_machine() to modify the code.
2960 * But archs can do what ever they want as long as it
2961 * is safe. The stop_machine() is the safest, but also
2962 * produces the most overhead.
2964 arch_ftrace_update_code(command);
2966 ftrace_arch_code_modify_post_process();
2969 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2970 struct ftrace_ops_hash *old_hash)
2972 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2973 ops->old_hash.filter_hash = old_hash->filter_hash;
2974 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2975 ftrace_run_update_code(command);
2976 ops->old_hash.filter_hash = NULL;
2977 ops->old_hash.notrace_hash = NULL;
2978 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2981 static ftrace_func_t saved_ftrace_func;
2982 static int ftrace_start_up;
2984 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2988 /* List of trace_ops that have allocated trampolines */
2989 static LIST_HEAD(ftrace_ops_trampoline_list);
2991 static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops)
2993 lockdep_assert_held(&ftrace_lock);
2994 list_add_rcu(&ops->list, &ftrace_ops_trampoline_list);
2997 static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops)
2999 lockdep_assert_held(&ftrace_lock);
3000 list_del_rcu(&ops->list);
3005 * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
3006 * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
3009 #define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
3010 #define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
3012 static void ftrace_trampoline_free(struct ftrace_ops *ops)
3014 if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) &&
3017 * Record the text poke event before the ksymbol unregister
3020 perf_event_text_poke((void *)ops->trampoline,
3021 (void *)ops->trampoline,
3022 ops->trampoline_size, NULL, 0);
3023 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
3024 ops->trampoline, ops->trampoline_size,
3025 true, FTRACE_TRAMPOLINE_SYM);
3026 /* Remove from kallsyms after the perf events */
3027 ftrace_remove_trampoline_from_kallsyms(ops);
3030 arch_ftrace_trampoline_free(ops);
3033 static void ftrace_startup_enable(int command)
3035 if (saved_ftrace_func != ftrace_trace_function) {
3036 saved_ftrace_func = ftrace_trace_function;
3037 command |= FTRACE_UPDATE_TRACE_FUNC;
3040 if (!command || !ftrace_enabled)
3043 ftrace_run_update_code(command);
3046 static void ftrace_startup_all(int command)
3048 update_all_ops = true;
3049 ftrace_startup_enable(command);
3050 update_all_ops = false;
3053 int ftrace_startup(struct ftrace_ops *ops, int command)
3057 if (unlikely(ftrace_disabled))
3060 ret = __register_ftrace_function(ops);
3067 * Note that ftrace probes uses this to start up
3068 * and modify functions it will probe. But we still
3069 * set the ADDING flag for modification, as probes
3070 * do not have trampolines. If they add them in the
3071 * future, then the probes will need to distinguish
3072 * between adding and updating probes.
3074 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
3076 ret = ftrace_hash_ipmodify_enable(ops);
3078 /* Rollback registration process */
3079 __unregister_ftrace_function(ops);
3081 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
3082 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
3083 ftrace_trampoline_free(ops);
3087 if (ftrace_hash_rec_enable(ops, 1))
3088 command |= FTRACE_UPDATE_CALLS;
3090 ftrace_startup_enable(command);
3093 * If ftrace is in an undefined state, we just remove ops from list
3094 * to prevent the NULL pointer, instead of totally rolling it back and
3095 * free trampoline, because those actions could cause further damage.
3097 if (unlikely(ftrace_disabled)) {
3098 __unregister_ftrace_function(ops);
3102 ops->flags &= ~FTRACE_OPS_FL_ADDING;
3107 int ftrace_shutdown(struct ftrace_ops *ops, int command)
3111 if (unlikely(ftrace_disabled))
3114 ret = __unregister_ftrace_function(ops);
3120 * Just warn in case of unbalance, no need to kill ftrace, it's not
3121 * critical but the ftrace_call callers may be never nopped again after
3122 * further ftrace uses.
3124 WARN_ON_ONCE(ftrace_start_up < 0);
3126 /* Disabling ipmodify never fails */
3127 ftrace_hash_ipmodify_disable(ops);
3129 if (ftrace_hash_rec_disable(ops, 1))
3130 command |= FTRACE_UPDATE_CALLS;
3132 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
3134 if (saved_ftrace_func != ftrace_trace_function) {
3135 saved_ftrace_func = ftrace_trace_function;
3136 command |= FTRACE_UPDATE_TRACE_FUNC;
3139 if (!command || !ftrace_enabled)
3143 * If the ops uses a trampoline, then it needs to be
3144 * tested first on update.
3146 ops->flags |= FTRACE_OPS_FL_REMOVING;
3149 /* The trampoline logic checks the old hashes */
3150 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
3151 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
3153 ftrace_run_update_code(command);
3156 * If there's no more ops registered with ftrace, run a
3157 * sanity check to make sure all rec flags are cleared.
3159 if (rcu_dereference_protected(ftrace_ops_list,
3160 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
3161 struct ftrace_page *pg;
3162 struct dyn_ftrace *rec;
3164 do_for_each_ftrace_rec(pg, rec) {
3165 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
3166 pr_warn(" %pS flags:%lx\n",
3167 (void *)rec->ip, rec->flags);
3168 } while_for_each_ftrace_rec();
3171 ops->old_hash.filter_hash = NULL;
3172 ops->old_hash.notrace_hash = NULL;
3175 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
3179 * Dynamic ops may be freed, we must make sure that all
3180 * callers are done before leaving this function.
3182 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
3184 * We need to do a hard force of sched synchronization.
3185 * This is because we use preempt_disable() to do RCU, but
3186 * the function tracers can be called where RCU is not watching
3187 * (like before user_exit()). We can not rely on the RCU
3188 * infrastructure to do the synchronization, thus we must do it
3191 synchronize_rcu_tasks_rude();
3194 * When the kernel is preemptive, tasks can be preempted
3195 * while on a ftrace trampoline. Just scheduling a task on
3196 * a CPU is not good enough to flush them. Calling
3197 * synchronize_rcu_tasks() will wait for those tasks to
3198 * execute and either schedule voluntarily or enter user space.
3200 if (IS_ENABLED(CONFIG_PREEMPTION))
3201 synchronize_rcu_tasks();
3203 ftrace_trampoline_free(ops);
3209 static u64 ftrace_update_time;
3210 unsigned long ftrace_update_tot_cnt;
3211 unsigned long ftrace_number_of_pages;
3212 unsigned long ftrace_number_of_groups;
3214 static inline int ops_traces_mod(struct ftrace_ops *ops)
3217 * Filter_hash being empty will default to trace module.
3218 * But notrace hash requires a test of individual module functions.
3220 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
3221 ftrace_hash_empty(ops->func_hash->notrace_hash);
3224 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
3226 bool init_nop = ftrace_need_init_nop();
3227 struct ftrace_page *pg;
3228 struct dyn_ftrace *p;
3230 unsigned long update_cnt = 0;
3231 unsigned long rec_flags = 0;
3234 start = ftrace_now(raw_smp_processor_id());
3237 * When a module is loaded, this function is called to convert
3238 * the calls to mcount in its text to nops, and also to create
3239 * an entry in the ftrace data. Now, if ftrace is activated
3240 * after this call, but before the module sets its text to
3241 * read-only, the modification of enabling ftrace can fail if
3242 * the read-only is done while ftrace is converting the calls.
3243 * To prevent this, the module's records are set as disabled
3244 * and will be enabled after the call to set the module's text
3248 rec_flags |= FTRACE_FL_DISABLED;
3250 for (pg = new_pgs; pg; pg = pg->next) {
3252 for (i = 0; i < pg->index; i++) {
3254 /* If something went wrong, bail without enabling anything */
3255 if (unlikely(ftrace_disabled))
3258 p = &pg->records[i];
3259 p->flags = rec_flags;
3262 * Do the initial record conversion from mcount jump
3263 * to the NOP instructions.
3265 if (init_nop && !ftrace_nop_initialize(mod, p))
3272 stop = ftrace_now(raw_smp_processor_id());
3273 ftrace_update_time = stop - start;
3274 ftrace_update_tot_cnt += update_cnt;
3279 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3285 if (WARN_ON(!count))
3288 /* We want to fill as much as possible, with no empty pages */
3289 pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE);
3290 order = fls(pages) - 1;
3293 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3296 /* if we can't allocate this size, try something smaller */
3303 ftrace_number_of_pages += 1 << order;
3304 ftrace_number_of_groups++;
3306 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3315 static struct ftrace_page *
3316 ftrace_allocate_pages(unsigned long num_to_init)
3318 struct ftrace_page *start_pg;
3319 struct ftrace_page *pg;
3325 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3330 * Try to allocate as much as possible in one continues
3331 * location that fills in all of the space. We want to
3332 * waste as little space as possible.
3335 cnt = ftrace_allocate_records(pg, num_to_init);
3343 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3356 free_pages((unsigned long)pg->records, pg->order);
3357 ftrace_number_of_pages -= 1 << pg->order;
3359 start_pg = pg->next;
3362 ftrace_number_of_groups--;
3364 pr_info("ftrace: FAILED to allocate memory for functions\n");
3368 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3370 struct ftrace_iterator {
3374 struct ftrace_page *pg;
3375 struct dyn_ftrace *func;
3376 struct ftrace_func_probe *probe;
3377 struct ftrace_func_entry *probe_entry;
3378 struct trace_parser parser;
3379 struct ftrace_hash *hash;
3380 struct ftrace_ops *ops;
3381 struct trace_array *tr;
3382 struct list_head *mod_list;
3389 t_probe_next(struct seq_file *m, loff_t *pos)
3391 struct ftrace_iterator *iter = m->private;
3392 struct trace_array *tr = iter->ops->private;
3393 struct list_head *func_probes;
3394 struct ftrace_hash *hash;
3395 struct list_head *next;
3396 struct hlist_node *hnd = NULL;
3397 struct hlist_head *hhd;
3406 func_probes = &tr->func_probes;
3407 if (list_empty(func_probes))
3411 next = func_probes->next;
3412 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3415 if (iter->probe_entry)
3416 hnd = &iter->probe_entry->hlist;
3418 hash = iter->probe->ops.func_hash->filter_hash;
3421 * A probe being registered may temporarily have an empty hash
3422 * and it's at the end of the func_probes list.
3424 if (!hash || hash == EMPTY_HASH)
3427 size = 1 << hash->size_bits;
3430 if (iter->pidx >= size) {
3431 if (iter->probe->list.next == func_probes)
3433 next = iter->probe->list.next;
3434 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3435 hash = iter->probe->ops.func_hash->filter_hash;
3436 size = 1 << hash->size_bits;
3440 hhd = &hash->buckets[iter->pidx];
3442 if (hlist_empty(hhd)) {
3458 if (WARN_ON_ONCE(!hnd))
3461 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3466 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3468 struct ftrace_iterator *iter = m->private;
3472 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3475 if (iter->mod_pos > *pos)
3479 iter->probe_entry = NULL;
3481 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3482 p = t_probe_next(m, &l);
3489 /* Only set this if we have an item */
3490 iter->flags |= FTRACE_ITER_PROBE;
3496 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3498 struct ftrace_func_entry *probe_entry;
3499 struct ftrace_probe_ops *probe_ops;
3500 struct ftrace_func_probe *probe;
3502 probe = iter->probe;
3503 probe_entry = iter->probe_entry;
3505 if (WARN_ON_ONCE(!probe || !probe_entry))
3508 probe_ops = probe->probe_ops;
3510 if (probe_ops->print)
3511 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3513 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3514 (void *)probe_ops->func);
3520 t_mod_next(struct seq_file *m, loff_t *pos)
3522 struct ftrace_iterator *iter = m->private;
3523 struct trace_array *tr = iter->tr;
3528 iter->mod_list = iter->mod_list->next;
3530 if (iter->mod_list == &tr->mod_trace ||
3531 iter->mod_list == &tr->mod_notrace) {
3532 iter->flags &= ~FTRACE_ITER_MOD;
3536 iter->mod_pos = *pos;
3541 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3543 struct ftrace_iterator *iter = m->private;
3547 if (iter->func_pos > *pos)
3550 iter->mod_pos = iter->func_pos;
3552 /* probes are only available if tr is set */
3556 for (l = 0; l <= (*pos - iter->func_pos); ) {
3557 p = t_mod_next(m, &l);
3562 iter->flags &= ~FTRACE_ITER_MOD;
3563 return t_probe_start(m, pos);
3566 /* Only set this if we have an item */
3567 iter->flags |= FTRACE_ITER_MOD;
3573 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3575 struct ftrace_mod_load *ftrace_mod;
3576 struct trace_array *tr = iter->tr;
3578 if (WARN_ON_ONCE(!iter->mod_list) ||
3579 iter->mod_list == &tr->mod_trace ||
3580 iter->mod_list == &tr->mod_notrace)
3583 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3585 if (ftrace_mod->func)
3586 seq_printf(m, "%s", ftrace_mod->func);
3590 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3596 t_func_next(struct seq_file *m, loff_t *pos)
3598 struct ftrace_iterator *iter = m->private;
3599 struct dyn_ftrace *rec = NULL;
3604 if (iter->idx >= iter->pg->index) {
3605 if (iter->pg->next) {
3606 iter->pg = iter->pg->next;
3611 rec = &iter->pg->records[iter->idx++];
3612 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3613 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3615 ((iter->flags & FTRACE_ITER_ENABLED) &&
3616 !(rec->flags & FTRACE_FL_ENABLED))) {
3626 iter->pos = iter->func_pos = *pos;
3633 t_next(struct seq_file *m, void *v, loff_t *pos)
3635 struct ftrace_iterator *iter = m->private;
3636 loff_t l = *pos; /* t_probe_start() must use original pos */
3639 if (unlikely(ftrace_disabled))
3642 if (iter->flags & FTRACE_ITER_PROBE)
3643 return t_probe_next(m, pos);
3645 if (iter->flags & FTRACE_ITER_MOD)
3646 return t_mod_next(m, pos);
3648 if (iter->flags & FTRACE_ITER_PRINTALL) {
3649 /* next must increment pos, and t_probe_start does not */
3651 return t_mod_start(m, &l);
3654 ret = t_func_next(m, pos);
3657 return t_mod_start(m, &l);
3662 static void reset_iter_read(struct ftrace_iterator *iter)
3666 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3669 static void *t_start(struct seq_file *m, loff_t *pos)
3671 struct ftrace_iterator *iter = m->private;
3675 mutex_lock(&ftrace_lock);
3677 if (unlikely(ftrace_disabled))
3681 * If an lseek was done, then reset and start from beginning.
3683 if (*pos < iter->pos)
3684 reset_iter_read(iter);
3687 * For set_ftrace_filter reading, if we have the filter
3688 * off, we can short cut and just print out that all
3689 * functions are enabled.
3691 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3692 ftrace_hash_empty(iter->hash)) {
3693 iter->func_pos = 1; /* Account for the message */
3695 return t_mod_start(m, pos);
3696 iter->flags |= FTRACE_ITER_PRINTALL;
3697 /* reset in case of seek/pread */
3698 iter->flags &= ~FTRACE_ITER_PROBE;
3702 if (iter->flags & FTRACE_ITER_MOD)
3703 return t_mod_start(m, pos);
3706 * Unfortunately, we need to restart at ftrace_pages_start
3707 * every time we let go of the ftrace_mutex. This is because
3708 * those pointers can change without the lock.
3710 iter->pg = ftrace_pages_start;
3712 for (l = 0; l <= *pos; ) {
3713 p = t_func_next(m, &l);
3719 return t_mod_start(m, pos);
3724 static void t_stop(struct seq_file *m, void *p)
3726 mutex_unlock(&ftrace_lock);
3730 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3735 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3736 struct dyn_ftrace *rec)
3740 ptr = arch_ftrace_trampoline_func(ops, rec);
3742 seq_printf(m, " ->%pS", ptr);
3745 #ifdef FTRACE_MCOUNT_MAX_OFFSET
3747 * Weak functions can still have an mcount/fentry that is saved in
3748 * the __mcount_loc section. These can be detected by having a
3749 * symbol offset of greater than FTRACE_MCOUNT_MAX_OFFSET, as the
3750 * symbol found by kallsyms is not the function that the mcount/fentry
3751 * is part of. The offset is much greater in these cases.
3753 * Test the record to make sure that the ip points to a valid kallsyms
3754 * and if not, mark it disabled.
3756 static int test_for_valid_rec(struct dyn_ftrace *rec)
3758 char str[KSYM_SYMBOL_LEN];
3759 unsigned long offset;
3762 ret = kallsyms_lookup(rec->ip, NULL, &offset, NULL, str);
3764 /* Weak functions can cause invalid addresses */
3765 if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) {
3766 rec->flags |= FTRACE_FL_DISABLED;
3772 static struct workqueue_struct *ftrace_check_wq __initdata;
3773 static struct work_struct ftrace_check_work __initdata;
3776 * Scan all the mcount/fentry entries to make sure they are valid.
3778 static __init void ftrace_check_work_func(struct work_struct *work)
3780 struct ftrace_page *pg;
3781 struct dyn_ftrace *rec;
3783 mutex_lock(&ftrace_lock);
3784 do_for_each_ftrace_rec(pg, rec) {
3785 test_for_valid_rec(rec);
3786 } while_for_each_ftrace_rec();
3787 mutex_unlock(&ftrace_lock);
3790 static int __init ftrace_check_for_weak_functions(void)
3792 INIT_WORK(&ftrace_check_work, ftrace_check_work_func);
3794 ftrace_check_wq = alloc_workqueue("ftrace_check_wq", WQ_UNBOUND, 0);
3796 queue_work(ftrace_check_wq, &ftrace_check_work);
3800 static int __init ftrace_check_sync(void)
3802 /* Make sure the ftrace_check updates are finished */
3803 if (ftrace_check_wq)
3804 destroy_workqueue(ftrace_check_wq);
3808 late_initcall_sync(ftrace_check_sync);
3809 subsys_initcall(ftrace_check_for_weak_functions);
3811 static int print_rec(struct seq_file *m, unsigned long ip)
3813 unsigned long offset;
3814 char str[KSYM_SYMBOL_LEN];
3818 ret = kallsyms_lookup(ip, NULL, &offset, &modname, str);
3819 /* Weak functions can cause invalid addresses */
3820 if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) {
3821 snprintf(str, KSYM_SYMBOL_LEN, "%s_%ld",
3822 FTRACE_INVALID_FUNCTION, offset);
3828 seq_printf(m, " [%s]", modname);
3829 return ret == NULL ? -1 : 0;
3832 static inline int test_for_valid_rec(struct dyn_ftrace *rec)
3837 static inline int print_rec(struct seq_file *m, unsigned long ip)
3839 seq_printf(m, "%ps", (void *)ip);
3844 static int t_show(struct seq_file *m, void *v)
3846 struct ftrace_iterator *iter = m->private;
3847 struct dyn_ftrace *rec;
3849 if (iter->flags & FTRACE_ITER_PROBE)
3850 return t_probe_show(m, iter);
3852 if (iter->flags & FTRACE_ITER_MOD)
3853 return t_mod_show(m, iter);
3855 if (iter->flags & FTRACE_ITER_PRINTALL) {
3856 if (iter->flags & FTRACE_ITER_NOTRACE)
3857 seq_puts(m, "#### no functions disabled ####\n");
3859 seq_puts(m, "#### all functions enabled ####\n");
3868 if (print_rec(m, rec->ip)) {
3869 /* This should only happen when a rec is disabled */
3870 WARN_ON_ONCE(!(rec->flags & FTRACE_FL_DISABLED));
3875 if (iter->flags & FTRACE_ITER_ENABLED) {
3876 struct ftrace_ops *ops;
3878 seq_printf(m, " (%ld)%s%s%s%s",
3879 ftrace_rec_count(rec),
3880 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3881 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
3882 rec->flags & FTRACE_FL_DIRECT ? " D" : " ",
3883 rec->flags & FTRACE_FL_CALL_OPS ? " O" : " ");
3884 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3885 ops = ftrace_find_tramp_ops_any(rec);
3888 seq_printf(m, "\ttramp: %pS (%pS)",
3889 (void *)ops->trampoline,
3891 add_trampoline_func(m, ops, rec);
3892 ops = ftrace_find_tramp_ops_next(rec, ops);
3895 seq_puts(m, "\ttramp: ERROR!");
3897 add_trampoline_func(m, NULL, rec);
3899 if (rec->flags & FTRACE_FL_CALL_OPS_EN) {
3900 ops = ftrace_find_unique_ops(rec);
3902 seq_printf(m, "\tops: %pS (%pS)",
3905 seq_puts(m, "\tops: ERROR!");
3908 if (rec->flags & FTRACE_FL_DIRECT) {
3909 unsigned long direct;
3911 direct = ftrace_find_rec_direct(rec->ip);
3913 seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3922 static const struct seq_operations show_ftrace_seq_ops = {
3930 ftrace_avail_open(struct inode *inode, struct file *file)
3932 struct ftrace_iterator *iter;
3935 ret = security_locked_down(LOCKDOWN_TRACEFS);
3939 if (unlikely(ftrace_disabled))
3942 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3946 iter->pg = ftrace_pages_start;
3947 iter->ops = &global_ops;
3953 ftrace_enabled_open(struct inode *inode, struct file *file)
3955 struct ftrace_iterator *iter;
3958 * This shows us what functions are currently being
3959 * traced and by what. Not sure if we want lockdown
3960 * to hide such critical information for an admin.
3961 * Although, perhaps it can show information we don't
3962 * want people to see, but if something is tracing
3963 * something, we probably want to know about it.
3966 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3970 iter->pg = ftrace_pages_start;
3971 iter->flags = FTRACE_ITER_ENABLED;
3972 iter->ops = &global_ops;
3978 * ftrace_regex_open - initialize function tracer filter files
3979 * @ops: The ftrace_ops that hold the hash filters
3980 * @flag: The type of filter to process
3981 * @inode: The inode, usually passed in to your open routine
3982 * @file: The file, usually passed in to your open routine
3984 * ftrace_regex_open() initializes the filter files for the
3985 * @ops. Depending on @flag it may process the filter hash or
3986 * the notrace hash of @ops. With this called from the open
3987 * routine, you can use ftrace_filter_write() for the write
3988 * routine if @flag has FTRACE_ITER_FILTER set, or
3989 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3990 * tracing_lseek() should be used as the lseek routine, and
3991 * release must call ftrace_regex_release().
3994 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3995 struct inode *inode, struct file *file)
3997 struct ftrace_iterator *iter;
3998 struct ftrace_hash *hash;
3999 struct list_head *mod_head;
4000 struct trace_array *tr = ops->private;
4003 ftrace_ops_init(ops);
4005 if (unlikely(ftrace_disabled))
4008 if (tracing_check_open_get_tr(tr))
4011 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4015 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
4022 mutex_lock(&ops->func_hash->regex_lock);
4024 if (flag & FTRACE_ITER_NOTRACE) {
4025 hash = ops->func_hash->notrace_hash;
4026 mod_head = tr ? &tr->mod_notrace : NULL;
4028 hash = ops->func_hash->filter_hash;
4029 mod_head = tr ? &tr->mod_trace : NULL;
4032 iter->mod_list = mod_head;
4034 if (file->f_mode & FMODE_WRITE) {
4035 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
4037 if (file->f_flags & O_TRUNC) {
4038 iter->hash = alloc_ftrace_hash(size_bits);
4039 clear_ftrace_mod_list(mod_head);
4041 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
4045 trace_parser_put(&iter->parser);
4053 if (file->f_mode & FMODE_READ) {
4054 iter->pg = ftrace_pages_start;
4056 ret = seq_open(file, &show_ftrace_seq_ops);
4058 struct seq_file *m = file->private_data;
4062 free_ftrace_hash(iter->hash);
4063 trace_parser_put(&iter->parser);
4066 file->private_data = iter;
4069 mutex_unlock(&ops->func_hash->regex_lock);
4075 trace_array_put(tr);
4082 ftrace_filter_open(struct inode *inode, struct file *file)
4084 struct ftrace_ops *ops = inode->i_private;
4086 /* Checks for tracefs lockdown */
4087 return ftrace_regex_open(ops,
4088 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
4093 ftrace_notrace_open(struct inode *inode, struct file *file)
4095 struct ftrace_ops *ops = inode->i_private;
4097 /* Checks for tracefs lockdown */
4098 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
4102 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
4103 struct ftrace_glob {
4110 * If symbols in an architecture don't correspond exactly to the user-visible
4111 * name of what they represent, it is possible to define this function to
4112 * perform the necessary adjustments.
4114 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
4119 static int ftrace_match(char *str, struct ftrace_glob *g)
4124 str = arch_ftrace_match_adjust(str, g->search);
4128 if (strcmp(str, g->search) == 0)
4131 case MATCH_FRONT_ONLY:
4132 if (strncmp(str, g->search, g->len) == 0)
4135 case MATCH_MIDDLE_ONLY:
4136 if (strstr(str, g->search))
4139 case MATCH_END_ONLY:
4141 if (slen >= g->len &&
4142 memcmp(str + slen - g->len, g->search, g->len) == 0)
4146 if (glob_match(g->search, str))
4155 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
4157 struct ftrace_func_entry *entry;
4160 entry = ftrace_lookup_ip(hash, rec->ip);
4162 /* Do nothing if it doesn't exist */
4166 free_hash_entry(hash, entry);
4168 /* Do nothing if it exists */
4172 ret = add_hash_entry(hash, rec->ip);
4178 add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
4181 long index = simple_strtoul(func_g->search, NULL, 0);
4182 struct ftrace_page *pg;
4183 struct dyn_ftrace *rec;
4185 /* The index starts at 1 */
4189 do_for_each_ftrace_rec(pg, rec) {
4190 if (pg->index <= index) {
4192 /* this is a double loop, break goes to the next page */
4195 rec = &pg->records[index];
4196 enter_record(hash, rec, clear_filter);
4198 } while_for_each_ftrace_rec();
4202 #ifdef FTRACE_MCOUNT_MAX_OFFSET
4203 static int lookup_ip(unsigned long ip, char **modname, char *str)
4205 unsigned long offset;
4207 kallsyms_lookup(ip, NULL, &offset, modname, str);
4208 if (offset > FTRACE_MCOUNT_MAX_OFFSET)
4213 static int lookup_ip(unsigned long ip, char **modname, char *str)
4215 kallsyms_lookup(ip, NULL, NULL, modname, str);
4221 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
4222 struct ftrace_glob *mod_g, int exclude_mod)
4224 char str[KSYM_SYMBOL_LEN];
4227 if (lookup_ip(rec->ip, &modname, str)) {
4228 /* This should only happen when a rec is disabled */
4229 WARN_ON_ONCE(system_state == SYSTEM_RUNNING &&
4230 !(rec->flags & FTRACE_FL_DISABLED));
4235 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
4237 /* blank module name to match all modules */
4239 /* blank module globbing: modname xor exclude_mod */
4240 if (!exclude_mod != !modname)
4246 * exclude_mod is set to trace everything but the given
4247 * module. If it is set and the module matches, then
4248 * return 0. If it is not set, and the module doesn't match
4249 * also return 0. Otherwise, check the function to see if
4252 if (!mod_matches == !exclude_mod)
4255 /* blank search means to match all funcs in the mod */
4260 return ftrace_match(str, func_g);
4264 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
4266 struct ftrace_page *pg;
4267 struct dyn_ftrace *rec;
4268 struct ftrace_glob func_g = { .type = MATCH_FULL };
4269 struct ftrace_glob mod_g = { .type = MATCH_FULL };
4270 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
4271 int exclude_mod = 0;
4274 int clear_filter = 0;
4277 func_g.type = filter_parse_regex(func, len, &func_g.search,
4279 func_g.len = strlen(func_g.search);
4283 mod_g.type = filter_parse_regex(mod, strlen(mod),
4284 &mod_g.search, &exclude_mod);
4285 mod_g.len = strlen(mod_g.search);
4288 mutex_lock(&ftrace_lock);
4290 if (unlikely(ftrace_disabled))
4293 if (func_g.type == MATCH_INDEX) {
4294 found = add_rec_by_index(hash, &func_g, clear_filter);
4298 do_for_each_ftrace_rec(pg, rec) {
4300 if (rec->flags & FTRACE_FL_DISABLED)
4303 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
4304 ret = enter_record(hash, rec, clear_filter);
4312 } while_for_each_ftrace_rec();
4314 mutex_unlock(&ftrace_lock);
4320 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
4322 return match_records(hash, buff, len, NULL);
4325 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4326 struct ftrace_ops_hash *old_hash)
4328 struct ftrace_ops *op;
4330 if (!ftrace_enabled)
4333 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4334 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4339 * If this is the shared global_ops filter, then we need to
4340 * check if there is another ops that shares it, is enabled.
4341 * If so, we still need to run the modify code.
4343 if (ops->func_hash != &global_ops.local_hash)
4346 do_for_each_ftrace_op(op, ftrace_ops_list) {
4347 if (op->func_hash == &global_ops.local_hash &&
4348 op->flags & FTRACE_OPS_FL_ENABLED) {
4349 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4350 /* Only need to do this once */
4353 } while_for_each_ftrace_op(op);
4356 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
4357 struct ftrace_hash **orig_hash,
4358 struct ftrace_hash *hash,
4361 struct ftrace_ops_hash old_hash_ops;
4362 struct ftrace_hash *old_hash;
4365 old_hash = *orig_hash;
4366 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4367 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4368 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4370 ftrace_ops_update_code(ops, &old_hash_ops);
4371 free_ftrace_hash_rcu(old_hash);
4376 static bool module_exists(const char *module)
4378 /* All modules have the symbol __this_module */
4379 static const char this_mod[] = "__this_module";
4380 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
4384 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
4386 if (n > sizeof(modname) - 1)
4389 val = module_kallsyms_lookup_name(modname);
4393 static int cache_mod(struct trace_array *tr,
4394 const char *func, char *module, int enable)
4396 struct ftrace_mod_load *ftrace_mod, *n;
4397 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4400 mutex_lock(&ftrace_lock);
4402 /* We do not cache inverse filters */
4403 if (func[0] == '!') {
4407 /* Look to remove this hash */
4408 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4409 if (strcmp(ftrace_mod->module, module) != 0)
4412 /* no func matches all */
4413 if (strcmp(func, "*") == 0 ||
4414 (ftrace_mod->func &&
4415 strcmp(ftrace_mod->func, func) == 0)) {
4417 free_ftrace_mod(ftrace_mod);
4425 /* We only care about modules that have not been loaded yet */
4426 if (module_exists(module))
4429 /* Save this string off, and execute it when the module is loaded */
4430 ret = ftrace_add_mod(tr, func, module, enable);
4432 mutex_unlock(&ftrace_lock);
4438 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4439 int reset, int enable);
4441 #ifdef CONFIG_MODULES
4442 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4443 char *mod, bool enable)
4445 struct ftrace_mod_load *ftrace_mod, *n;
4446 struct ftrace_hash **orig_hash, *new_hash;
4447 LIST_HEAD(process_mods);
4450 mutex_lock(&ops->func_hash->regex_lock);
4453 orig_hash = &ops->func_hash->filter_hash;
4455 orig_hash = &ops->func_hash->notrace_hash;
4457 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4460 goto out; /* warn? */
4462 mutex_lock(&ftrace_lock);
4464 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4466 if (strcmp(ftrace_mod->module, mod) != 0)
4469 if (ftrace_mod->func)
4470 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4472 func = kstrdup("*", GFP_KERNEL);
4474 if (!func) /* warn? */
4477 list_move(&ftrace_mod->list, &process_mods);
4479 /* Use the newly allocated func, as it may be "*" */
4480 kfree(ftrace_mod->func);
4481 ftrace_mod->func = func;
4484 mutex_unlock(&ftrace_lock);
4486 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4488 func = ftrace_mod->func;
4490 /* Grabs ftrace_lock, which is why we have this extra step */
4491 match_records(new_hash, func, strlen(func), mod);
4492 free_ftrace_mod(ftrace_mod);
4495 if (enable && list_empty(head))
4496 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4498 mutex_lock(&ftrace_lock);
4500 ftrace_hash_move_and_update_ops(ops, orig_hash,
4502 mutex_unlock(&ftrace_lock);
4505 mutex_unlock(&ops->func_hash->regex_lock);
4507 free_ftrace_hash(new_hash);
4510 static void process_cached_mods(const char *mod_name)
4512 struct trace_array *tr;
4515 mod = kstrdup(mod_name, GFP_KERNEL);
4519 mutex_lock(&trace_types_lock);
4520 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4521 if (!list_empty(&tr->mod_trace))
4522 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4523 if (!list_empty(&tr->mod_notrace))
4524 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4526 mutex_unlock(&trace_types_lock);
4533 * We register the module command as a template to show others how
4534 * to register the a command as well.
4538 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4539 char *func_orig, char *cmd, char *module, int enable)
4544 /* match_records() modifies func, and we need the original */
4545 func = kstrdup(func_orig, GFP_KERNEL);
4550 * cmd == 'mod' because we only registered this func
4551 * for the 'mod' ftrace_func_command.
4552 * But if you register one func with multiple commands,
4553 * you can tell which command was used by the cmd
4556 ret = match_records(hash, func, strlen(func), module);
4560 return cache_mod(tr, func_orig, module, enable);
4566 static struct ftrace_func_command ftrace_mod_cmd = {
4568 .func = ftrace_mod_callback,
4571 static int __init ftrace_mod_cmd_init(void)
4573 return register_ftrace_command(&ftrace_mod_cmd);
4575 core_initcall(ftrace_mod_cmd_init);
4577 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4578 struct ftrace_ops *op, struct ftrace_regs *fregs)
4580 struct ftrace_probe_ops *probe_ops;
4581 struct ftrace_func_probe *probe;
4583 probe = container_of(op, struct ftrace_func_probe, ops);
4584 probe_ops = probe->probe_ops;
4587 * Disable preemption for these calls to prevent a RCU grace
4588 * period. This syncs the hash iteration and freeing of items
4589 * on the hash. rcu_read_lock is too dangerous here.
4591 preempt_disable_notrace();
4592 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4593 preempt_enable_notrace();
4596 struct ftrace_func_map {
4597 struct ftrace_func_entry entry;
4601 struct ftrace_func_mapper {
4602 struct ftrace_hash hash;
4606 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4608 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4610 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4612 struct ftrace_hash *hash;
4615 * The mapper is simply a ftrace_hash, but since the entries
4616 * in the hash are not ftrace_func_entry type, we define it
4617 * as a separate structure.
4619 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4620 return (struct ftrace_func_mapper *)hash;
4624 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4625 * @mapper: The mapper that has the ip maps
4626 * @ip: the instruction pointer to find the data for
4628 * Returns the data mapped to @ip if found otherwise NULL. The return
4629 * is actually the address of the mapper data pointer. The address is
4630 * returned for use cases where the data is no bigger than a long, and
4631 * the user can use the data pointer as its data instead of having to
4632 * allocate more memory for the reference.
4634 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4637 struct ftrace_func_entry *entry;
4638 struct ftrace_func_map *map;
4640 entry = ftrace_lookup_ip(&mapper->hash, ip);
4644 map = (struct ftrace_func_map *)entry;
4649 * ftrace_func_mapper_add_ip - Map some data to an ip
4650 * @mapper: The mapper that has the ip maps
4651 * @ip: The instruction pointer address to map @data to
4652 * @data: The data to map to @ip
4654 * Returns 0 on success otherwise an error.
4656 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4657 unsigned long ip, void *data)
4659 struct ftrace_func_entry *entry;
4660 struct ftrace_func_map *map;
4662 entry = ftrace_lookup_ip(&mapper->hash, ip);
4666 map = kmalloc(sizeof(*map), GFP_KERNEL);
4673 __add_hash_entry(&mapper->hash, &map->entry);
4679 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4680 * @mapper: The mapper that has the ip maps
4681 * @ip: The instruction pointer address to remove the data from
4683 * Returns the data if it is found, otherwise NULL.
4684 * Note, if the data pointer is used as the data itself, (see
4685 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4686 * if the data pointer was set to zero.
4688 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4691 struct ftrace_func_entry *entry;
4692 struct ftrace_func_map *map;
4695 entry = ftrace_lookup_ip(&mapper->hash, ip);
4699 map = (struct ftrace_func_map *)entry;
4702 remove_hash_entry(&mapper->hash, entry);
4709 * free_ftrace_func_mapper - free a mapping of ips and data
4710 * @mapper: The mapper that has the ip maps
4711 * @free_func: A function to be called on each data item.
4713 * This is used to free the function mapper. The @free_func is optional
4714 * and can be used if the data needs to be freed as well.
4716 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4717 ftrace_mapper_func free_func)
4719 struct ftrace_func_entry *entry;
4720 struct ftrace_func_map *map;
4721 struct hlist_head *hhd;
4727 if (free_func && mapper->hash.count) {
4728 size = 1 << mapper->hash.size_bits;
4729 for (i = 0; i < size; i++) {
4730 hhd = &mapper->hash.buckets[i];
4731 hlist_for_each_entry(entry, hhd, hlist) {
4732 map = (struct ftrace_func_map *)entry;
4737 free_ftrace_hash(&mapper->hash);
4740 static void release_probe(struct ftrace_func_probe *probe)
4742 struct ftrace_probe_ops *probe_ops;
4744 mutex_lock(&ftrace_lock);
4746 WARN_ON(probe->ref <= 0);
4748 /* Subtract the ref that was used to protect this instance */
4752 probe_ops = probe->probe_ops;
4754 * Sending zero as ip tells probe_ops to free
4755 * the probe->data itself
4757 if (probe_ops->free)
4758 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4759 list_del(&probe->list);
4762 mutex_unlock(&ftrace_lock);
4765 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4768 * Add one ref to keep it from being freed when releasing the
4769 * ftrace_lock mutex.
4775 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4776 struct ftrace_probe_ops *probe_ops,
4779 struct ftrace_func_probe *probe = NULL, *iter;
4780 struct ftrace_func_entry *entry;
4781 struct ftrace_hash **orig_hash;
4782 struct ftrace_hash *old_hash;
4783 struct ftrace_hash *hash;
4792 /* We do not support '!' for function probes */
4793 if (WARN_ON(glob[0] == '!'))
4797 mutex_lock(&ftrace_lock);
4798 /* Check if the probe_ops is already registered */
4799 list_for_each_entry(iter, &tr->func_probes, list) {
4800 if (iter->probe_ops == probe_ops) {
4806 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4808 mutex_unlock(&ftrace_lock);
4811 probe->probe_ops = probe_ops;
4812 probe->ops.func = function_trace_probe_call;
4814 ftrace_ops_init(&probe->ops);
4815 list_add(&probe->list, &tr->func_probes);
4818 acquire_probe_locked(probe);
4820 mutex_unlock(&ftrace_lock);
4823 * Note, there's a small window here that the func_hash->filter_hash
4824 * may be NULL or empty. Need to be careful when reading the loop.
4826 mutex_lock(&probe->ops.func_hash->regex_lock);
4828 orig_hash = &probe->ops.func_hash->filter_hash;
4829 old_hash = *orig_hash;
4830 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4837 ret = ftrace_match_records(hash, glob, strlen(glob));
4839 /* Nothing found? */
4846 size = 1 << hash->size_bits;
4847 for (i = 0; i < size; i++) {
4848 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4849 if (ftrace_lookup_ip(old_hash, entry->ip))
4852 * The caller might want to do something special
4853 * for each function we find. We call the callback
4854 * to give the caller an opportunity to do so.
4856 if (probe_ops->init) {
4857 ret = probe_ops->init(probe_ops, tr,
4861 if (probe_ops->free && count)
4862 probe_ops->free(probe_ops, tr,
4872 mutex_lock(&ftrace_lock);
4875 /* Nothing was added? */
4880 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4885 /* One ref for each new function traced */
4886 probe->ref += count;
4888 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4889 ret = ftrace_startup(&probe->ops, 0);
4892 mutex_unlock(&ftrace_lock);
4897 mutex_unlock(&probe->ops.func_hash->regex_lock);
4898 free_ftrace_hash(hash);
4900 release_probe(probe);
4905 if (!probe_ops->free || !count)
4908 /* Failed to do the move, need to call the free functions */
4909 for (i = 0; i < size; i++) {
4910 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4911 if (ftrace_lookup_ip(old_hash, entry->ip))
4913 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4920 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4921 struct ftrace_probe_ops *probe_ops)
4923 struct ftrace_func_probe *probe = NULL, *iter;
4924 struct ftrace_ops_hash old_hash_ops;
4925 struct ftrace_func_entry *entry;
4926 struct ftrace_glob func_g;
4927 struct ftrace_hash **orig_hash;
4928 struct ftrace_hash *old_hash;
4929 struct ftrace_hash *hash = NULL;
4930 struct hlist_node *tmp;
4931 struct hlist_head hhd;
4932 char str[KSYM_SYMBOL_LEN];
4934 int i, ret = -ENODEV;
4937 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4938 func_g.search = NULL;
4942 func_g.type = filter_parse_regex(glob, strlen(glob),
4943 &func_g.search, ¬);
4944 func_g.len = strlen(func_g.search);
4946 /* we do not support '!' for function probes */
4951 mutex_lock(&ftrace_lock);
4952 /* Check if the probe_ops is already registered */
4953 list_for_each_entry(iter, &tr->func_probes, list) {
4954 if (iter->probe_ops == probe_ops) {
4960 goto err_unlock_ftrace;
4963 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4964 goto err_unlock_ftrace;
4966 acquire_probe_locked(probe);
4968 mutex_unlock(&ftrace_lock);
4970 mutex_lock(&probe->ops.func_hash->regex_lock);
4972 orig_hash = &probe->ops.func_hash->filter_hash;
4973 old_hash = *orig_hash;
4975 if (ftrace_hash_empty(old_hash))
4978 old_hash_ops.filter_hash = old_hash;
4979 /* Probes only have filters */
4980 old_hash_ops.notrace_hash = NULL;
4983 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4987 INIT_HLIST_HEAD(&hhd);
4989 size = 1 << hash->size_bits;
4990 for (i = 0; i < size; i++) {
4991 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4993 if (func_g.search) {
4994 kallsyms_lookup(entry->ip, NULL, NULL,
4996 if (!ftrace_match(str, &func_g))
5000 remove_hash_entry(hash, entry);
5001 hlist_add_head(&entry->hlist, &hhd);
5005 /* Nothing found? */
5011 mutex_lock(&ftrace_lock);
5013 WARN_ON(probe->ref < count);
5015 probe->ref -= count;
5017 if (ftrace_hash_empty(hash))
5018 ftrace_shutdown(&probe->ops, 0);
5020 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
5023 /* still need to update the function call sites */
5024 if (ftrace_enabled && !ftrace_hash_empty(hash))
5025 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
5029 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
5030 hlist_del(&entry->hlist);
5031 if (probe_ops->free)
5032 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
5035 mutex_unlock(&ftrace_lock);
5038 mutex_unlock(&probe->ops.func_hash->regex_lock);
5039 free_ftrace_hash(hash);
5041 release_probe(probe);
5046 mutex_unlock(&ftrace_lock);
5050 void clear_ftrace_function_probes(struct trace_array *tr)
5052 struct ftrace_func_probe *probe, *n;
5054 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
5055 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
5058 static LIST_HEAD(ftrace_commands);
5059 static DEFINE_MUTEX(ftrace_cmd_mutex);
5062 * Currently we only register ftrace commands from __init, so mark this
5065 __init int register_ftrace_command(struct ftrace_func_command *cmd)
5067 struct ftrace_func_command *p;
5070 mutex_lock(&ftrace_cmd_mutex);
5071 list_for_each_entry(p, &ftrace_commands, list) {
5072 if (strcmp(cmd->name, p->name) == 0) {
5077 list_add(&cmd->list, &ftrace_commands);
5079 mutex_unlock(&ftrace_cmd_mutex);
5085 * Currently we only unregister ftrace commands from __init, so mark
5088 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
5090 struct ftrace_func_command *p, *n;
5093 mutex_lock(&ftrace_cmd_mutex);
5094 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
5095 if (strcmp(cmd->name, p->name) == 0) {
5097 list_del_init(&p->list);
5102 mutex_unlock(&ftrace_cmd_mutex);
5107 static int ftrace_process_regex(struct ftrace_iterator *iter,
5108 char *buff, int len, int enable)
5110 struct ftrace_hash *hash = iter->hash;
5111 struct trace_array *tr = iter->ops->private;
5112 char *func, *command, *next = buff;
5113 struct ftrace_func_command *p;
5116 func = strsep(&next, ":");
5119 ret = ftrace_match_records(hash, func, len);
5129 command = strsep(&next, ":");
5131 mutex_lock(&ftrace_cmd_mutex);
5132 list_for_each_entry(p, &ftrace_commands, list) {
5133 if (strcmp(p->name, command) == 0) {
5134 ret = p->func(tr, hash, func, command, next, enable);
5139 mutex_unlock(&ftrace_cmd_mutex);
5145 ftrace_regex_write(struct file *file, const char __user *ubuf,
5146 size_t cnt, loff_t *ppos, int enable)
5148 struct ftrace_iterator *iter;
5149 struct trace_parser *parser;
5155 if (file->f_mode & FMODE_READ) {
5156 struct seq_file *m = file->private_data;
5159 iter = file->private_data;
5161 if (unlikely(ftrace_disabled))
5164 /* iter->hash is a local copy, so we don't need regex_lock */
5166 parser = &iter->parser;
5167 read = trace_get_user(parser, ubuf, cnt, ppos);
5169 if (read >= 0 && trace_parser_loaded(parser) &&
5170 !trace_parser_cont(parser)) {
5171 ret = ftrace_process_regex(iter, parser->buffer,
5172 parser->idx, enable);
5173 trace_parser_clear(parser);
5184 ftrace_filter_write(struct file *file, const char __user *ubuf,
5185 size_t cnt, loff_t *ppos)
5187 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
5191 ftrace_notrace_write(struct file *file, const char __user *ubuf,
5192 size_t cnt, loff_t *ppos)
5194 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
5198 __ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
5200 struct ftrace_func_entry *entry;
5202 ip = ftrace_location(ip);
5207 entry = ftrace_lookup_ip(hash, ip);
5210 free_hash_entry(hash, entry);
5214 return add_hash_entry(hash, ip);
5218 ftrace_match_addr(struct ftrace_hash *hash, unsigned long *ips,
5219 unsigned int cnt, int remove)
5224 for (i = 0; i < cnt; i++) {
5225 err = __ftrace_match_addr(hash, ips[i], remove);
5228 * This expects the @hash is a temporary hash and if this
5229 * fails the caller must free the @hash.
5238 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
5239 unsigned long *ips, unsigned int cnt,
5240 int remove, int reset, int enable)
5242 struct ftrace_hash **orig_hash;
5243 struct ftrace_hash *hash;
5246 if (unlikely(ftrace_disabled))
5249 mutex_lock(&ops->func_hash->regex_lock);
5252 orig_hash = &ops->func_hash->filter_hash;
5254 orig_hash = &ops->func_hash->notrace_hash;
5257 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5259 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
5263 goto out_regex_unlock;
5266 if (buf && !ftrace_match_records(hash, buf, len)) {
5268 goto out_regex_unlock;
5271 ret = ftrace_match_addr(hash, ips, cnt, remove);
5273 goto out_regex_unlock;
5276 mutex_lock(&ftrace_lock);
5277 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
5278 mutex_unlock(&ftrace_lock);
5281 mutex_unlock(&ops->func_hash->regex_lock);
5283 free_ftrace_hash(hash);
5288 ftrace_set_addr(struct ftrace_ops *ops, unsigned long *ips, unsigned int cnt,
5289 int remove, int reset, int enable)
5291 return ftrace_set_hash(ops, NULL, 0, ips, cnt, remove, reset, enable);
5294 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
5296 struct ftrace_direct_func {
5297 struct list_head next;
5302 static LIST_HEAD(ftrace_direct_funcs);
5305 * ftrace_find_direct_func - test an address if it is a registered direct caller
5306 * @addr: The address of a registered direct caller
5308 * This searches to see if a ftrace direct caller has been registered
5309 * at a specific address, and if so, it returns a descriptor for it.
5311 * This can be used by architecture code to see if an address is
5312 * a direct caller (trampoline) attached to a fentry/mcount location.
5313 * This is useful for the function_graph tracer, as it may need to
5314 * do adjustments if it traced a location that also has a direct
5315 * trampoline attached to it.
5317 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
5319 struct ftrace_direct_func *entry;
5322 /* May be called by fgraph trampoline (protected by rcu tasks) */
5323 list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
5324 if (entry->addr == addr) {
5335 static struct ftrace_direct_func *ftrace_alloc_direct_func(unsigned long addr)
5337 struct ftrace_direct_func *direct;
5339 direct = kmalloc(sizeof(*direct), GFP_KERNEL);
5342 direct->addr = addr;
5344 list_add_rcu(&direct->next, &ftrace_direct_funcs);
5345 ftrace_direct_func_count++;
5349 static int register_ftrace_function_nolock(struct ftrace_ops *ops);
5352 * register_ftrace_direct - Call a custom trampoline directly
5353 * @ip: The address of the nop at the beginning of a function
5354 * @addr: The address of the trampoline to call at @ip
5356 * This is used to connect a direct call from the nop location (@ip)
5357 * at the start of ftrace traced functions. The location that it calls
5358 * (@addr) must be able to handle a direct call, and save the parameters
5359 * of the function being traced, and restore them (or inject new ones
5360 * if needed), before returning.
5364 * -EBUSY - Another direct function is already attached (there can be only one)
5365 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5366 * -ENOMEM - There was an allocation failure.
5368 int register_ftrace_direct(unsigned long ip, unsigned long addr)
5370 struct ftrace_direct_func *direct;
5371 struct ftrace_func_entry *entry;
5372 struct ftrace_hash *free_hash = NULL;
5373 struct dyn_ftrace *rec;
5376 mutex_lock(&direct_mutex);
5378 ip = ftrace_location(ip);
5382 /* See if there's a direct function at @ip already */
5384 if (ftrace_find_rec_direct(ip))
5388 rec = lookup_rec(ip, ip);
5393 * Check if the rec says it has a direct call but we didn't
5396 if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
5399 /* Make sure the ip points to the exact record */
5400 if (ip != rec->ip) {
5402 /* Need to check this ip for a direct. */
5403 if (ftrace_find_rec_direct(ip))
5408 direct = ftrace_find_direct_func(addr);
5410 direct = ftrace_alloc_direct_func(addr);
5415 entry = ftrace_add_rec_direct(ip, addr, &free_hash);
5419 ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
5421 if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
5422 ret = register_ftrace_function_nolock(&direct_ops);
5424 ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5428 remove_hash_entry(direct_functions, entry);
5430 if (!direct->count) {
5431 list_del_rcu(&direct->next);
5432 synchronize_rcu_tasks();
5435 free_ftrace_hash(free_hash);
5437 ftrace_direct_func_count--;
5443 mutex_unlock(&direct_mutex);
5446 synchronize_rcu_tasks();
5447 free_ftrace_hash(free_hash);
5452 EXPORT_SYMBOL_GPL(register_ftrace_direct);
5454 static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
5455 struct dyn_ftrace **recp)
5457 struct ftrace_func_entry *entry;
5458 struct dyn_ftrace *rec;
5460 rec = lookup_rec(*ip, *ip);
5464 entry = __ftrace_lookup_ip(direct_functions, rec->ip);
5466 WARN_ON(rec->flags & FTRACE_FL_DIRECT);
5470 WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
5472 /* Passed in ip just needs to be on the call site */
5481 int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
5483 struct ftrace_direct_func *direct;
5484 struct ftrace_func_entry *entry;
5485 struct ftrace_hash *hash;
5488 mutex_lock(&direct_mutex);
5490 ip = ftrace_location(ip);
5494 entry = find_direct_entry(&ip, NULL);
5498 hash = direct_ops.func_hash->filter_hash;
5499 if (hash->count == 1)
5500 unregister_ftrace_function(&direct_ops);
5502 ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5506 remove_hash_entry(direct_functions, entry);
5508 direct = ftrace_find_direct_func(addr);
5509 if (!WARN_ON(!direct)) {
5510 /* This is the good path (see the ! before WARN) */
5512 WARN_ON(direct->count < 0);
5513 if (!direct->count) {
5514 list_del_rcu(&direct->next);
5515 synchronize_rcu_tasks();
5518 ftrace_direct_func_count--;
5522 mutex_unlock(&direct_mutex);
5526 EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
5528 static struct ftrace_ops stub_ops = {
5529 .func = ftrace_stub,
5533 * ftrace_modify_direct_caller - modify ftrace nop directly
5534 * @entry: The ftrace hash entry of the direct helper for @rec
5535 * @rec: The record representing the function site to patch
5536 * @old_addr: The location that the site at @rec->ip currently calls
5537 * @new_addr: The location that the site at @rec->ip should call
5539 * An architecture may overwrite this function to optimize the
5540 * changing of the direct callback on an ftrace nop location.
5541 * This is called with the ftrace_lock mutex held, and no other
5542 * ftrace callbacks are on the associated record (@rec). Thus,
5543 * it is safe to modify the ftrace record, where it should be
5544 * currently calling @old_addr directly, to call @new_addr.
5546 * This is called with direct_mutex locked.
5548 * Safety checks should be made to make sure that the code at
5549 * @rec->ip is currently calling @old_addr. And this must
5550 * also update entry->direct to @new_addr.
5552 int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
5553 struct dyn_ftrace *rec,
5554 unsigned long old_addr,
5555 unsigned long new_addr)
5557 unsigned long ip = rec->ip;
5560 lockdep_assert_held(&direct_mutex);
5563 * The ftrace_lock was used to determine if the record
5564 * had more than one registered user to it. If it did,
5565 * we needed to prevent that from changing to do the quick
5566 * switch. But if it did not (only a direct caller was attached)
5567 * then this function is called. But this function can deal
5568 * with attached callers to the rec that we care about, and
5569 * since this function uses standard ftrace calls that take
5570 * the ftrace_lock mutex, we need to release it.
5572 mutex_unlock(&ftrace_lock);
5575 * By setting a stub function at the same address, we force
5576 * the code to call the iterator and the direct_ops helper.
5577 * This means that @ip does not call the direct call, and
5578 * we can simply modify it.
5580 ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
5584 ret = register_ftrace_function_nolock(&stub_ops);
5586 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5590 entry->direct = new_addr;
5593 * By removing the stub, we put back the direct call, calling
5596 unregister_ftrace_function(&stub_ops);
5597 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5600 mutex_lock(&ftrace_lock);
5606 * modify_ftrace_direct - Modify an existing direct call to call something else
5607 * @ip: The instruction pointer to modify
5608 * @old_addr: The address that the current @ip calls directly
5609 * @new_addr: The address that the @ip should call
5611 * This modifies a ftrace direct caller at an instruction pointer without
5612 * having to disable it first. The direct call will switch over to the
5613 * @new_addr without missing anything.
5615 * Returns: zero on success. Non zero on error, which includes:
5616 * -ENODEV : the @ip given has no direct caller attached
5617 * -EINVAL : the @old_addr does not match the current direct caller
5619 int modify_ftrace_direct(unsigned long ip,
5620 unsigned long old_addr, unsigned long new_addr)
5622 struct ftrace_direct_func *direct, *new_direct = NULL;
5623 struct ftrace_func_entry *entry;
5624 struct dyn_ftrace *rec;
5627 mutex_lock(&direct_mutex);
5629 mutex_lock(&ftrace_lock);
5631 ip = ftrace_location(ip);
5635 entry = find_direct_entry(&ip, &rec);
5640 if (entry->direct != old_addr)
5643 direct = ftrace_find_direct_func(old_addr);
5644 if (WARN_ON(!direct))
5646 if (direct->count > 1) {
5648 new_direct = ftrace_alloc_direct_func(new_addr);
5652 new_direct->count++;
5654 direct->addr = new_addr;
5658 * If there's no other ftrace callback on the rec->ip location,
5659 * then it can be changed directly by the architecture.
5660 * If there is another caller, then we just need to change the
5661 * direct caller helper to point to @new_addr.
5663 if (ftrace_rec_count(rec) == 1) {
5664 ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
5666 entry->direct = new_addr;
5670 if (unlikely(ret && new_direct)) {
5672 list_del_rcu(&new_direct->next);
5673 synchronize_rcu_tasks();
5675 ftrace_direct_func_count--;
5679 mutex_unlock(&ftrace_lock);
5680 mutex_unlock(&direct_mutex);
5683 EXPORT_SYMBOL_GPL(modify_ftrace_direct);
5685 #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS)
5687 static int check_direct_multi(struct ftrace_ops *ops)
5689 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5691 if ((ops->flags & MULTI_FLAGS) != MULTI_FLAGS)
5696 static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long addr)
5698 struct ftrace_func_entry *entry, *del;
5701 size = 1 << hash->size_bits;
5702 for (i = 0; i < size; i++) {
5703 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5704 del = __ftrace_lookup_ip(direct_functions, entry->ip);
5705 if (del && del->direct == addr) {
5706 remove_hash_entry(direct_functions, del);
5714 * register_ftrace_direct_multi - Call a custom trampoline directly
5715 * for multiple functions registered in @ops
5716 * @ops: The address of the struct ftrace_ops object
5717 * @addr: The address of the trampoline to call at @ops functions
5719 * This is used to connect a direct calls to @addr from the nop locations
5720 * of the functions registered in @ops (with by ftrace_set_filter_ip
5723 * The location that it calls (@addr) must be able to handle a direct call,
5724 * and save the parameters of the function being traced, and restore them
5725 * (or inject new ones if needed), before returning.
5729 * -EINVAL - The @ops object was already registered with this call or
5730 * when there are no functions in @ops object.
5731 * -EBUSY - Another direct function is already attached (there can be only one)
5732 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5733 * -ENOMEM - There was an allocation failure.
5735 int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5737 struct ftrace_hash *hash, *free_hash = NULL;
5738 struct ftrace_func_entry *entry, *new;
5739 int err = -EBUSY, size, i;
5741 if (ops->func || ops->trampoline)
5743 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5745 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5748 hash = ops->func_hash->filter_hash;
5749 if (ftrace_hash_empty(hash))
5752 mutex_lock(&direct_mutex);
5754 /* Make sure requested entries are not already registered.. */
5755 size = 1 << hash->size_bits;
5756 for (i = 0; i < size; i++) {
5757 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5758 if (ftrace_find_rec_direct(entry->ip))
5763 /* ... and insert them to direct_functions hash. */
5765 for (i = 0; i < size; i++) {
5766 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5767 new = ftrace_add_rec_direct(entry->ip, addr, &free_hash);
5770 entry->direct = addr;
5774 ops->func = call_direct_funcs;
5775 ops->flags = MULTI_FLAGS;
5776 ops->trampoline = FTRACE_REGS_ADDR;
5778 err = register_ftrace_function_nolock(ops);
5782 remove_direct_functions_hash(hash, addr);
5785 mutex_unlock(&direct_mutex);
5788 synchronize_rcu_tasks();
5789 free_ftrace_hash(free_hash);
5793 EXPORT_SYMBOL_GPL(register_ftrace_direct_multi);
5796 * unregister_ftrace_direct_multi - Remove calls to custom trampoline
5797 * previously registered by register_ftrace_direct_multi for @ops object.
5798 * @ops: The address of the struct ftrace_ops object
5800 * This is used to remove a direct calls to @addr from the nop locations
5801 * of the functions registered in @ops (with by ftrace_set_filter_ip
5806 * -EINVAL - The @ops object was not properly registered.
5808 int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5810 struct ftrace_hash *hash = ops->func_hash->filter_hash;
5813 if (check_direct_multi(ops))
5815 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5818 mutex_lock(&direct_mutex);
5819 err = unregister_ftrace_function(ops);
5820 remove_direct_functions_hash(hash, addr);
5821 mutex_unlock(&direct_mutex);
5823 /* cleanup for possible another register call */
5825 ops->trampoline = 0;
5828 EXPORT_SYMBOL_GPL(unregister_ftrace_direct_multi);
5831 __modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5833 struct ftrace_hash *hash;
5834 struct ftrace_func_entry *entry, *iter;
5835 static struct ftrace_ops tmp_ops = {
5836 .func = ftrace_stub,
5837 .flags = FTRACE_OPS_FL_STUB,
5842 lockdep_assert_held_once(&direct_mutex);
5844 /* Enable the tmp_ops to have the same functions as the direct ops */
5845 ftrace_ops_init(&tmp_ops);
5846 tmp_ops.func_hash = ops->func_hash;
5848 err = register_ftrace_function_nolock(&tmp_ops);
5853 * Now the ftrace_ops_list_func() is called to do the direct callers.
5854 * We can safely change the direct functions attached to each entry.
5856 mutex_lock(&ftrace_lock);
5858 hash = ops->func_hash->filter_hash;
5859 size = 1 << hash->size_bits;
5860 for (i = 0; i < size; i++) {
5861 hlist_for_each_entry(iter, &hash->buckets[i], hlist) {
5862 entry = __ftrace_lookup_ip(direct_functions, iter->ip);
5865 entry->direct = addr;
5869 mutex_unlock(&ftrace_lock);
5871 /* Removing the tmp_ops will add the updated direct callers to the functions */
5872 unregister_ftrace_function(&tmp_ops);
5878 * modify_ftrace_direct_multi_nolock - Modify an existing direct 'multi' call
5879 * to call something else
5880 * @ops: The address of the struct ftrace_ops object
5881 * @addr: The address of the new trampoline to call at @ops functions
5883 * This is used to unregister currently registered direct caller and
5884 * register new one @addr on functions registered in @ops object.
5886 * Note there's window between ftrace_shutdown and ftrace_startup calls
5887 * where there will be no callbacks called.
5889 * Caller should already have direct_mutex locked, so we don't lock
5890 * direct_mutex here.
5892 * Returns: zero on success. Non zero on error, which includes:
5893 * -EINVAL - The @ops object was not properly registered.
5895 int modify_ftrace_direct_multi_nolock(struct ftrace_ops *ops, unsigned long addr)
5897 if (check_direct_multi(ops))
5899 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5902 return __modify_ftrace_direct_multi(ops, addr);
5904 EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi_nolock);
5907 * modify_ftrace_direct_multi - Modify an existing direct 'multi' call
5908 * to call something else
5909 * @ops: The address of the struct ftrace_ops object
5910 * @addr: The address of the new trampoline to call at @ops functions
5912 * This is used to unregister currently registered direct caller and
5913 * register new one @addr on functions registered in @ops object.
5915 * Note there's window between ftrace_shutdown and ftrace_startup calls
5916 * where there will be no callbacks called.
5918 * Returns: zero on success. Non zero on error, which includes:
5919 * -EINVAL - The @ops object was not properly registered.
5921 int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5925 if (check_direct_multi(ops))
5927 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5930 mutex_lock(&direct_mutex);
5931 err = __modify_ftrace_direct_multi(ops, addr);
5932 mutex_unlock(&direct_mutex);
5935 EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi);
5936 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
5939 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
5940 * @ops - the ops to set the filter with
5941 * @ip - the address to add to or remove from the filter.
5942 * @remove - non zero to remove the ip from the filter
5943 * @reset - non zero to reset all filters before applying this filter.
5945 * Filters denote which functions should be enabled when tracing is enabled
5946 * If @ip is NULL, it fails to update filter.
5948 * This can allocate memory which must be freed before @ops can be freed,
5949 * either by removing each filtered addr or by using
5950 * ftrace_free_filter(@ops).
5952 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
5953 int remove, int reset)
5955 ftrace_ops_init(ops);
5956 return ftrace_set_addr(ops, &ip, 1, remove, reset, 1);
5958 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
5961 * ftrace_set_filter_ips - set functions to filter on in ftrace by addresses
5962 * @ops - the ops to set the filter with
5963 * @ips - the array of addresses to add to or remove from the filter.
5964 * @cnt - the number of addresses in @ips
5965 * @remove - non zero to remove ips from the filter
5966 * @reset - non zero to reset all filters before applying this filter.
5968 * Filters denote which functions should be enabled when tracing is enabled
5969 * If @ips array or any ip specified within is NULL , it fails to update filter.
5971 * This can allocate memory which must be freed before @ops can be freed,
5972 * either by removing each filtered addr or by using
5973 * ftrace_free_filter(@ops).
5975 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
5976 unsigned int cnt, int remove, int reset)
5978 ftrace_ops_init(ops);
5979 return ftrace_set_addr(ops, ips, cnt, remove, reset, 1);
5981 EXPORT_SYMBOL_GPL(ftrace_set_filter_ips);
5984 * ftrace_ops_set_global_filter - setup ops to use global filters
5985 * @ops - the ops which will use the global filters
5987 * ftrace users who need global function trace filtering should call this.
5988 * It can set the global filter only if ops were not initialized before.
5990 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
5992 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
5995 ftrace_ops_init(ops);
5996 ops->func_hash = &global_ops.local_hash;
5998 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
6001 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
6002 int reset, int enable)
6004 return ftrace_set_hash(ops, buf, len, NULL, 0, 0, reset, enable);
6008 * ftrace_set_filter - set a function to filter on in ftrace
6009 * @ops - the ops to set the filter with
6010 * @buf - the string that holds the function filter text.
6011 * @len - the length of the string.
6012 * @reset - non zero to reset all filters before applying this filter.
6014 * Filters denote which functions should be enabled when tracing is enabled.
6015 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
6017 * This can allocate memory which must be freed before @ops can be freed,
6018 * either by removing each filtered addr or by using
6019 * ftrace_free_filter(@ops).
6021 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
6024 ftrace_ops_init(ops);
6025 return ftrace_set_regex(ops, buf, len, reset, 1);
6027 EXPORT_SYMBOL_GPL(ftrace_set_filter);
6030 * ftrace_set_notrace - set a function to not trace in ftrace
6031 * @ops - the ops to set the notrace filter with
6032 * @buf - the string that holds the function notrace text.
6033 * @len - the length of the string.
6034 * @reset - non zero to reset all filters before applying this filter.
6036 * Notrace Filters denote which functions should not be enabled when tracing
6037 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
6040 * This can allocate memory which must be freed before @ops can be freed,
6041 * either by removing each filtered addr or by using
6042 * ftrace_free_filter(@ops).
6044 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
6047 ftrace_ops_init(ops);
6048 return ftrace_set_regex(ops, buf, len, reset, 0);
6050 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
6052 * ftrace_set_global_filter - set a function to filter on with global tracers
6053 * @buf - the string that holds the function filter text.
6054 * @len - the length of the string.
6055 * @reset - non zero to reset all filters before applying this filter.
6057 * Filters denote which functions should be enabled when tracing is enabled.
6058 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
6060 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
6062 ftrace_set_regex(&global_ops, buf, len, reset, 1);
6064 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
6067 * ftrace_set_global_notrace - set a function to not trace with global tracers
6068 * @buf - the string that holds the function notrace text.
6069 * @len - the length of the string.
6070 * @reset - non zero to reset all filters before applying this filter.
6072 * Notrace Filters denote which functions should not be enabled when tracing
6073 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
6076 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
6078 ftrace_set_regex(&global_ops, buf, len, reset, 0);
6080 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
6083 * command line interface to allow users to set filters on boot up.
6085 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
6086 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
6087 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
6089 /* Used by function selftest to not test if filter is set */
6090 bool ftrace_filter_param __initdata;
6092 static int __init set_ftrace_notrace(char *str)
6094 ftrace_filter_param = true;
6095 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
6098 __setup("ftrace_notrace=", set_ftrace_notrace);
6100 static int __init set_ftrace_filter(char *str)
6102 ftrace_filter_param = true;
6103 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
6106 __setup("ftrace_filter=", set_ftrace_filter);
6108 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6109 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
6110 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
6111 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
6113 static int __init set_graph_function(char *str)
6115 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
6118 __setup("ftrace_graph_filter=", set_graph_function);
6120 static int __init set_graph_notrace_function(char *str)
6122 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
6125 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
6127 static int __init set_graph_max_depth_function(char *str)
6131 fgraph_max_depth = simple_strtoul(str, NULL, 0);
6134 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
6136 static void __init set_ftrace_early_graph(char *buf, int enable)
6140 struct ftrace_hash *hash;
6142 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
6143 if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
6147 func = strsep(&buf, ",");
6148 /* we allow only one expression at a time */
6149 ret = ftrace_graph_set_hash(hash, func);
6151 printk(KERN_DEBUG "ftrace: function %s not "
6152 "traceable\n", func);
6156 ftrace_graph_hash = hash;
6158 ftrace_graph_notrace_hash = hash;
6160 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6163 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
6167 ftrace_ops_init(ops);
6170 func = strsep(&buf, ",");
6171 ftrace_set_regex(ops, func, strlen(func), 0, enable);
6175 static void __init set_ftrace_early_filters(void)
6177 if (ftrace_filter_buf[0])
6178 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
6179 if (ftrace_notrace_buf[0])
6180 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
6181 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6182 if (ftrace_graph_buf[0])
6183 set_ftrace_early_graph(ftrace_graph_buf, 1);
6184 if (ftrace_graph_notrace_buf[0])
6185 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
6186 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6189 int ftrace_regex_release(struct inode *inode, struct file *file)
6191 struct seq_file *m = (struct seq_file *)file->private_data;
6192 struct ftrace_iterator *iter;
6193 struct ftrace_hash **orig_hash;
6194 struct trace_parser *parser;
6197 if (file->f_mode & FMODE_READ) {
6199 seq_release(inode, file);
6201 iter = file->private_data;
6203 parser = &iter->parser;
6204 if (trace_parser_loaded(parser)) {
6205 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
6207 ftrace_process_regex(iter, parser->buffer,
6208 parser->idx, enable);
6211 trace_parser_put(parser);
6213 mutex_lock(&iter->ops->func_hash->regex_lock);
6215 if (file->f_mode & FMODE_WRITE) {
6216 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
6219 orig_hash = &iter->ops->func_hash->filter_hash;
6221 if (list_empty(&iter->tr->mod_trace))
6222 iter->hash->flags &= ~FTRACE_HASH_FL_MOD;
6224 iter->hash->flags |= FTRACE_HASH_FL_MOD;
6227 orig_hash = &iter->ops->func_hash->notrace_hash;
6229 mutex_lock(&ftrace_lock);
6230 ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
6231 iter->hash, filter_hash);
6232 mutex_unlock(&ftrace_lock);
6234 /* For read only, the hash is the ops hash */
6238 mutex_unlock(&iter->ops->func_hash->regex_lock);
6239 free_ftrace_hash(iter->hash);
6241 trace_array_put(iter->tr);
6247 static const struct file_operations ftrace_avail_fops = {
6248 .open = ftrace_avail_open,
6250 .llseek = seq_lseek,
6251 .release = seq_release_private,
6254 static const struct file_operations ftrace_enabled_fops = {
6255 .open = ftrace_enabled_open,
6257 .llseek = seq_lseek,
6258 .release = seq_release_private,
6261 static const struct file_operations ftrace_filter_fops = {
6262 .open = ftrace_filter_open,
6264 .write = ftrace_filter_write,
6265 .llseek = tracing_lseek,
6266 .release = ftrace_regex_release,
6269 static const struct file_operations ftrace_notrace_fops = {
6270 .open = ftrace_notrace_open,
6272 .write = ftrace_notrace_write,
6273 .llseek = tracing_lseek,
6274 .release = ftrace_regex_release,
6277 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6279 static DEFINE_MUTEX(graph_lock);
6281 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
6282 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
6284 enum graph_filter_type {
6285 GRAPH_FILTER_NOTRACE = 0,
6286 GRAPH_FILTER_FUNCTION,
6289 #define FTRACE_GRAPH_EMPTY ((void *)1)
6291 struct ftrace_graph_data {
6292 struct ftrace_hash *hash;
6293 struct ftrace_func_entry *entry;
6294 int idx; /* for hash table iteration */
6295 enum graph_filter_type type;
6296 struct ftrace_hash *new_hash;
6297 const struct seq_operations *seq_ops;
6298 struct trace_parser parser;
6302 __g_next(struct seq_file *m, loff_t *pos)
6304 struct ftrace_graph_data *fgd = m->private;
6305 struct ftrace_func_entry *entry = fgd->entry;
6306 struct hlist_head *head;
6307 int i, idx = fgd->idx;
6309 if (*pos >= fgd->hash->count)
6313 hlist_for_each_entry_continue(entry, hlist) {
6321 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
6322 head = &fgd->hash->buckets[i];
6323 hlist_for_each_entry(entry, head, hlist) {
6333 g_next(struct seq_file *m, void *v, loff_t *pos)
6336 return __g_next(m, pos);
6339 static void *g_start(struct seq_file *m, loff_t *pos)
6341 struct ftrace_graph_data *fgd = m->private;
6343 mutex_lock(&graph_lock);
6345 if (fgd->type == GRAPH_FILTER_FUNCTION)
6346 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
6347 lockdep_is_held(&graph_lock));
6349 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6350 lockdep_is_held(&graph_lock));
6352 /* Nothing, tell g_show to print all functions are enabled */
6353 if (ftrace_hash_empty(fgd->hash) && !*pos)
6354 return FTRACE_GRAPH_EMPTY;
6358 return __g_next(m, pos);
6361 static void g_stop(struct seq_file *m, void *p)
6363 mutex_unlock(&graph_lock);
6366 static int g_show(struct seq_file *m, void *v)
6368 struct ftrace_func_entry *entry = v;
6373 if (entry == FTRACE_GRAPH_EMPTY) {
6374 struct ftrace_graph_data *fgd = m->private;
6376 if (fgd->type == GRAPH_FILTER_FUNCTION)
6377 seq_puts(m, "#### all functions enabled ####\n");
6379 seq_puts(m, "#### no functions disabled ####\n");
6383 seq_printf(m, "%ps\n", (void *)entry->ip);
6388 static const struct seq_operations ftrace_graph_seq_ops = {
6396 __ftrace_graph_open(struct inode *inode, struct file *file,
6397 struct ftrace_graph_data *fgd)
6400 struct ftrace_hash *new_hash = NULL;
6402 ret = security_locked_down(LOCKDOWN_TRACEFS);
6406 if (file->f_mode & FMODE_WRITE) {
6407 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
6409 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
6412 if (file->f_flags & O_TRUNC)
6413 new_hash = alloc_ftrace_hash(size_bits);
6415 new_hash = alloc_and_copy_ftrace_hash(size_bits,
6423 if (file->f_mode & FMODE_READ) {
6424 ret = seq_open(file, &ftrace_graph_seq_ops);
6426 struct seq_file *m = file->private_data;
6430 free_ftrace_hash(new_hash);
6434 file->private_data = fgd;
6437 if (ret < 0 && file->f_mode & FMODE_WRITE)
6438 trace_parser_put(&fgd->parser);
6440 fgd->new_hash = new_hash;
6443 * All uses of fgd->hash must be taken with the graph_lock
6444 * held. The graph_lock is going to be released, so force
6445 * fgd->hash to be reinitialized when it is taken again.
6453 ftrace_graph_open(struct inode *inode, struct file *file)
6455 struct ftrace_graph_data *fgd;
6458 if (unlikely(ftrace_disabled))
6461 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6465 mutex_lock(&graph_lock);
6467 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
6468 lockdep_is_held(&graph_lock));
6469 fgd->type = GRAPH_FILTER_FUNCTION;
6470 fgd->seq_ops = &ftrace_graph_seq_ops;
6472 ret = __ftrace_graph_open(inode, file, fgd);
6476 mutex_unlock(&graph_lock);
6481 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
6483 struct ftrace_graph_data *fgd;
6486 if (unlikely(ftrace_disabled))
6489 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6493 mutex_lock(&graph_lock);
6495 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6496 lockdep_is_held(&graph_lock));
6497 fgd->type = GRAPH_FILTER_NOTRACE;
6498 fgd->seq_ops = &ftrace_graph_seq_ops;
6500 ret = __ftrace_graph_open(inode, file, fgd);
6504 mutex_unlock(&graph_lock);
6509 ftrace_graph_release(struct inode *inode, struct file *file)
6511 struct ftrace_graph_data *fgd;
6512 struct ftrace_hash *old_hash, *new_hash;
6513 struct trace_parser *parser;
6516 if (file->f_mode & FMODE_READ) {
6517 struct seq_file *m = file->private_data;
6520 seq_release(inode, file);
6522 fgd = file->private_data;
6526 if (file->f_mode & FMODE_WRITE) {
6528 parser = &fgd->parser;
6530 if (trace_parser_loaded((parser))) {
6531 ret = ftrace_graph_set_hash(fgd->new_hash,
6535 trace_parser_put(parser);
6537 new_hash = __ftrace_hash_move(fgd->new_hash);
6543 mutex_lock(&graph_lock);
6545 if (fgd->type == GRAPH_FILTER_FUNCTION) {
6546 old_hash = rcu_dereference_protected(ftrace_graph_hash,
6547 lockdep_is_held(&graph_lock));
6548 rcu_assign_pointer(ftrace_graph_hash, new_hash);
6550 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6551 lockdep_is_held(&graph_lock));
6552 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
6555 mutex_unlock(&graph_lock);
6558 * We need to do a hard force of sched synchronization.
6559 * This is because we use preempt_disable() to do RCU, but
6560 * the function tracers can be called where RCU is not watching
6561 * (like before user_exit()). We can not rely on the RCU
6562 * infrastructure to do the synchronization, thus we must do it
6565 if (old_hash != EMPTY_HASH)
6566 synchronize_rcu_tasks_rude();
6568 free_ftrace_hash(old_hash);
6572 free_ftrace_hash(fgd->new_hash);
6579 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
6581 struct ftrace_glob func_g;
6582 struct dyn_ftrace *rec;
6583 struct ftrace_page *pg;
6584 struct ftrace_func_entry *entry;
6589 func_g.type = filter_parse_regex(buffer, strlen(buffer),
6590 &func_g.search, ¬);
6592 func_g.len = strlen(func_g.search);
6594 mutex_lock(&ftrace_lock);
6596 if (unlikely(ftrace_disabled)) {
6597 mutex_unlock(&ftrace_lock);
6601 do_for_each_ftrace_rec(pg, rec) {
6603 if (rec->flags & FTRACE_FL_DISABLED)
6606 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
6607 entry = ftrace_lookup_ip(hash, rec->ip);
6614 if (add_hash_entry(hash, rec->ip) < 0)
6618 free_hash_entry(hash, entry);
6623 } while_for_each_ftrace_rec();
6625 mutex_unlock(&ftrace_lock);
6634 ftrace_graph_write(struct file *file, const char __user *ubuf,
6635 size_t cnt, loff_t *ppos)
6637 ssize_t read, ret = 0;
6638 struct ftrace_graph_data *fgd = file->private_data;
6639 struct trace_parser *parser;
6644 /* Read mode uses seq functions */
6645 if (file->f_mode & FMODE_READ) {
6646 struct seq_file *m = file->private_data;
6650 parser = &fgd->parser;
6652 read = trace_get_user(parser, ubuf, cnt, ppos);
6654 if (read >= 0 && trace_parser_loaded(parser) &&
6655 !trace_parser_cont(parser)) {
6657 ret = ftrace_graph_set_hash(fgd->new_hash,
6659 trace_parser_clear(parser);
6668 static const struct file_operations ftrace_graph_fops = {
6669 .open = ftrace_graph_open,
6671 .write = ftrace_graph_write,
6672 .llseek = tracing_lseek,
6673 .release = ftrace_graph_release,
6676 static const struct file_operations ftrace_graph_notrace_fops = {
6677 .open = ftrace_graph_notrace_open,
6679 .write = ftrace_graph_write,
6680 .llseek = tracing_lseek,
6681 .release = ftrace_graph_release,
6683 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6685 void ftrace_create_filter_files(struct ftrace_ops *ops,
6686 struct dentry *parent)
6689 trace_create_file("set_ftrace_filter", TRACE_MODE_WRITE, parent,
6690 ops, &ftrace_filter_fops);
6692 trace_create_file("set_ftrace_notrace", TRACE_MODE_WRITE, parent,
6693 ops, &ftrace_notrace_fops);
6697 * The name "destroy_filter_files" is really a misnomer. Although
6698 * in the future, it may actually delete the files, but this is
6699 * really intended to make sure the ops passed in are disabled
6700 * and that when this function returns, the caller is free to
6703 * The "destroy" name is only to match the "create" name that this
6704 * should be paired with.
6706 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
6708 mutex_lock(&ftrace_lock);
6709 if (ops->flags & FTRACE_OPS_FL_ENABLED)
6710 ftrace_shutdown(ops, 0);
6711 ops->flags |= FTRACE_OPS_FL_DELETED;
6712 ftrace_free_filter(ops);
6713 mutex_unlock(&ftrace_lock);
6716 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
6719 trace_create_file("available_filter_functions", TRACE_MODE_READ,
6720 d_tracer, NULL, &ftrace_avail_fops);
6722 trace_create_file("enabled_functions", TRACE_MODE_READ,
6723 d_tracer, NULL, &ftrace_enabled_fops);
6725 ftrace_create_filter_files(&global_ops, d_tracer);
6727 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6728 trace_create_file("set_graph_function", TRACE_MODE_WRITE, d_tracer,
6730 &ftrace_graph_fops);
6731 trace_create_file("set_graph_notrace", TRACE_MODE_WRITE, d_tracer,
6733 &ftrace_graph_notrace_fops);
6734 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6739 static int ftrace_cmp_ips(const void *a, const void *b)
6741 const unsigned long *ipa = a;
6742 const unsigned long *ipb = b;
6751 #ifdef CONFIG_FTRACE_SORT_STARTUP_TEST
6752 static void test_is_sorted(unsigned long *start, unsigned long count)
6756 for (i = 1; i < count; i++) {
6757 if (WARN(start[i - 1] > start[i],
6758 "[%d] %pS at %lx is not sorted with %pS at %lx\n", i,
6759 (void *)start[i - 1], start[i - 1],
6760 (void *)start[i], start[i]))
6764 pr_info("ftrace section at %px sorted properly\n", start);
6767 static void test_is_sorted(unsigned long *start, unsigned long count)
6772 static int ftrace_process_locs(struct module *mod,
6773 unsigned long *start,
6776 struct ftrace_page *start_pg;
6777 struct ftrace_page *pg;
6778 struct dyn_ftrace *rec;
6779 unsigned long count;
6782 unsigned long flags = 0; /* Shut up gcc */
6785 count = end - start;
6791 * Sorting mcount in vmlinux at build time depend on
6792 * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in
6793 * modules can not be sorted at build time.
6795 if (!IS_ENABLED(CONFIG_BUILDTIME_MCOUNT_SORT) || mod) {
6796 sort(start, count, sizeof(*start),
6797 ftrace_cmp_ips, NULL);
6799 test_is_sorted(start, count);
6802 start_pg = ftrace_allocate_pages(count);
6806 mutex_lock(&ftrace_lock);
6809 * Core and each module needs their own pages, as
6810 * modules will free them when they are removed.
6811 * Force a new page to be allocated for modules.
6814 WARN_ON(ftrace_pages || ftrace_pages_start);
6815 /* First initialization */
6816 ftrace_pages = ftrace_pages_start = start_pg;
6821 if (WARN_ON(ftrace_pages->next)) {
6822 /* Hmm, we have free pages? */
6823 while (ftrace_pages->next)
6824 ftrace_pages = ftrace_pages->next;
6827 ftrace_pages->next = start_pg;
6833 unsigned long end_offset;
6834 addr = ftrace_call_adjust(*p++);
6836 * Some architecture linkers will pad between
6837 * the different mcount_loc sections of different
6838 * object files to satisfy alignments.
6839 * Skip any NULL pointers.
6844 end_offset = (pg->index+1) * sizeof(pg->records[0]);
6845 if (end_offset > PAGE_SIZE << pg->order) {
6846 /* We should have allocated enough */
6847 if (WARN_ON(!pg->next))
6852 rec = &pg->records[pg->index++];
6856 /* We should have used all pages */
6859 /* Assign the last page to ftrace_pages */
6863 * We only need to disable interrupts on start up
6864 * because we are modifying code that an interrupt
6865 * may execute, and the modification is not atomic.
6866 * But for modules, nothing runs the code we modify
6867 * until we are finished with it, and there's no
6868 * reason to cause large interrupt latencies while we do it.
6871 local_irq_save(flags);
6872 ftrace_update_code(mod, start_pg);
6874 local_irq_restore(flags);
6877 mutex_unlock(&ftrace_lock);
6882 struct ftrace_mod_func {
6883 struct list_head list;
6889 struct ftrace_mod_map {
6890 struct rcu_head rcu;
6891 struct list_head list;
6893 unsigned long start_addr;
6894 unsigned long end_addr;
6895 struct list_head funcs;
6896 unsigned int num_funcs;
6899 static int ftrace_get_trampoline_kallsym(unsigned int symnum,
6900 unsigned long *value, char *type,
6901 char *name, char *module_name,
6904 struct ftrace_ops *op;
6906 list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) {
6907 if (!op->trampoline || symnum--)
6909 *value = op->trampoline;
6911 strlcpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN);
6912 strlcpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN);
6920 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) || defined(CONFIG_MODULES)
6922 * Check if the current ops references the given ip.
6924 * If the ops traces all functions, then it was already accounted for.
6925 * If the ops does not trace the current record function, skip it.
6926 * If the ops ignores the function via notrace filter, skip it.
6929 ops_references_ip(struct ftrace_ops *ops, unsigned long ip)
6931 /* If ops isn't enabled, ignore it */
6932 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
6935 /* If ops traces all then it includes this function */
6936 if (ops_traces_mod(ops))
6939 /* The function must be in the filter */
6940 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
6941 !__ftrace_lookup_ip(ops->func_hash->filter_hash, ip))
6944 /* If in notrace hash, we ignore it too */
6945 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, ip))
6952 #ifdef CONFIG_MODULES
6954 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
6956 static LIST_HEAD(ftrace_mod_maps);
6958 static int referenced_filters(struct dyn_ftrace *rec)
6960 struct ftrace_ops *ops;
6963 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
6964 if (ops_references_ip(ops, rec->ip)) {
6965 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT))
6967 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY))
6970 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
6971 rec->flags |= FTRACE_FL_REGS;
6972 if (cnt == 1 && ops->trampoline)
6973 rec->flags |= FTRACE_FL_TRAMP;
6975 rec->flags &= ~FTRACE_FL_TRAMP;
6983 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
6985 struct ftrace_func_entry *entry;
6986 struct dyn_ftrace *rec;
6989 if (ftrace_hash_empty(hash))
6992 for (i = 0; i < pg->index; i++) {
6993 rec = &pg->records[i];
6994 entry = __ftrace_lookup_ip(hash, rec->ip);
6996 * Do not allow this rec to match again.
6997 * Yeah, it may waste some memory, but will be removed
6998 * if/when the hash is modified again.
7005 /* Clear any records from hashes */
7006 static void clear_mod_from_hashes(struct ftrace_page *pg)
7008 struct trace_array *tr;
7010 mutex_lock(&trace_types_lock);
7011 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7012 if (!tr->ops || !tr->ops->func_hash)
7014 mutex_lock(&tr->ops->func_hash->regex_lock);
7015 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
7016 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
7017 mutex_unlock(&tr->ops->func_hash->regex_lock);
7019 mutex_unlock(&trace_types_lock);
7022 static void ftrace_free_mod_map(struct rcu_head *rcu)
7024 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
7025 struct ftrace_mod_func *mod_func;
7026 struct ftrace_mod_func *n;
7028 /* All the contents of mod_map are now not visible to readers */
7029 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
7030 kfree(mod_func->name);
7031 list_del(&mod_func->list);
7038 void ftrace_release_mod(struct module *mod)
7040 struct ftrace_mod_map *mod_map;
7041 struct ftrace_mod_map *n;
7042 struct dyn_ftrace *rec;
7043 struct ftrace_page **last_pg;
7044 struct ftrace_page *tmp_page = NULL;
7045 struct ftrace_page *pg;
7047 mutex_lock(&ftrace_lock);
7049 if (ftrace_disabled)
7052 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
7053 if (mod_map->mod == mod) {
7054 list_del_rcu(&mod_map->list);
7055 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
7061 * Each module has its own ftrace_pages, remove
7062 * them from the list.
7064 last_pg = &ftrace_pages_start;
7065 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
7066 rec = &pg->records[0];
7067 if (within_module_core(rec->ip, mod) ||
7068 within_module_init(rec->ip, mod)) {
7070 * As core pages are first, the first
7071 * page should never be a module page.
7073 if (WARN_ON(pg == ftrace_pages_start))
7076 /* Check if we are deleting the last page */
7077 if (pg == ftrace_pages)
7078 ftrace_pages = next_to_ftrace_page(last_pg);
7080 ftrace_update_tot_cnt -= pg->index;
7081 *last_pg = pg->next;
7083 pg->next = tmp_page;
7086 last_pg = &pg->next;
7089 mutex_unlock(&ftrace_lock);
7091 for (pg = tmp_page; pg; pg = tmp_page) {
7093 /* Needs to be called outside of ftrace_lock */
7094 clear_mod_from_hashes(pg);
7097 free_pages((unsigned long)pg->records, pg->order);
7098 ftrace_number_of_pages -= 1 << pg->order;
7100 tmp_page = pg->next;
7102 ftrace_number_of_groups--;
7106 void ftrace_module_enable(struct module *mod)
7108 struct dyn_ftrace *rec;
7109 struct ftrace_page *pg;
7111 mutex_lock(&ftrace_lock);
7113 if (ftrace_disabled)
7117 * If the tracing is enabled, go ahead and enable the record.
7119 * The reason not to enable the record immediately is the
7120 * inherent check of ftrace_make_nop/ftrace_make_call for
7121 * correct previous instructions. Making first the NOP
7122 * conversion puts the module to the correct state, thus
7123 * passing the ftrace_make_call check.
7125 * We also delay this to after the module code already set the
7126 * text to read-only, as we now need to set it back to read-write
7127 * so that we can modify the text.
7129 if (ftrace_start_up)
7130 ftrace_arch_code_modify_prepare();
7132 do_for_each_ftrace_rec(pg, rec) {
7135 * do_for_each_ftrace_rec() is a double loop.
7136 * module text shares the pg. If a record is
7137 * not part of this module, then skip this pg,
7138 * which the "break" will do.
7140 if (!within_module_core(rec->ip, mod) &&
7141 !within_module_init(rec->ip, mod))
7144 /* Weak functions should still be ignored */
7145 if (!test_for_valid_rec(rec)) {
7146 /* Clear all other flags. Should not be enabled anyway */
7147 rec->flags = FTRACE_FL_DISABLED;
7154 * When adding a module, we need to check if tracers are
7155 * currently enabled and if they are, and can trace this record,
7156 * we need to enable the module functions as well as update the
7157 * reference counts for those function records.
7159 if (ftrace_start_up)
7160 cnt += referenced_filters(rec);
7162 rec->flags &= ~FTRACE_FL_DISABLED;
7165 if (ftrace_start_up && cnt) {
7166 int failed = __ftrace_replace_code(rec, 1);
7168 ftrace_bug(failed, rec);
7173 } while_for_each_ftrace_rec();
7176 if (ftrace_start_up)
7177 ftrace_arch_code_modify_post_process();
7180 mutex_unlock(&ftrace_lock);
7182 process_cached_mods(mod->name);
7185 void ftrace_module_init(struct module *mod)
7189 if (ftrace_disabled || !mod->num_ftrace_callsites)
7192 ret = ftrace_process_locs(mod, mod->ftrace_callsites,
7193 mod->ftrace_callsites + mod->num_ftrace_callsites);
7195 pr_warn("ftrace: failed to allocate entries for module '%s' functions\n",
7199 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
7200 struct dyn_ftrace *rec)
7202 struct ftrace_mod_func *mod_func;
7203 unsigned long symsize;
7204 unsigned long offset;
7205 char str[KSYM_SYMBOL_LEN];
7209 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
7213 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
7217 mod_func->name = kstrdup(str, GFP_KERNEL);
7218 if (!mod_func->name) {
7223 mod_func->ip = rec->ip - offset;
7224 mod_func->size = symsize;
7226 mod_map->num_funcs++;
7228 list_add_rcu(&mod_func->list, &mod_map->funcs);
7231 static struct ftrace_mod_map *
7232 allocate_ftrace_mod_map(struct module *mod,
7233 unsigned long start, unsigned long end)
7235 struct ftrace_mod_map *mod_map;
7237 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
7242 mod_map->start_addr = start;
7243 mod_map->end_addr = end;
7244 mod_map->num_funcs = 0;
7246 INIT_LIST_HEAD_RCU(&mod_map->funcs);
7248 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
7254 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
7255 unsigned long addr, unsigned long *size,
7256 unsigned long *off, char *sym)
7258 struct ftrace_mod_func *found_func = NULL;
7259 struct ftrace_mod_func *mod_func;
7261 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
7262 if (addr >= mod_func->ip &&
7263 addr < mod_func->ip + mod_func->size) {
7264 found_func = mod_func;
7271 *size = found_func->size;
7273 *off = addr - found_func->ip;
7275 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
7277 return found_func->name;
7284 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
7285 unsigned long *off, char **modname, char *sym)
7287 struct ftrace_mod_map *mod_map;
7288 const char *ret = NULL;
7290 /* mod_map is freed via call_rcu() */
7292 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
7293 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
7296 *modname = mod_map->mod->name;
7305 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
7306 char *type, char *name,
7307 char *module_name, int *exported)
7309 struct ftrace_mod_map *mod_map;
7310 struct ftrace_mod_func *mod_func;
7314 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
7316 if (symnum >= mod_map->num_funcs) {
7317 symnum -= mod_map->num_funcs;
7321 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
7327 *value = mod_func->ip;
7329 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
7330 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
7338 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
7339 module_name, exported);
7345 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
7346 struct dyn_ftrace *rec) { }
7347 static inline struct ftrace_mod_map *
7348 allocate_ftrace_mod_map(struct module *mod,
7349 unsigned long start, unsigned long end)
7353 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
7354 char *type, char *name, char *module_name,
7360 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
7361 module_name, exported);
7365 #endif /* CONFIG_MODULES */
7367 struct ftrace_init_func {
7368 struct list_head list;
7372 /* Clear any init ips from hashes */
7374 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
7376 struct ftrace_func_entry *entry;
7378 entry = ftrace_lookup_ip(hash, func->ip);
7380 * Do not allow this rec to match again.
7381 * Yeah, it may waste some memory, but will be removed
7382 * if/when the hash is modified again.
7389 clear_func_from_hashes(struct ftrace_init_func *func)
7391 struct trace_array *tr;
7393 mutex_lock(&trace_types_lock);
7394 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7395 if (!tr->ops || !tr->ops->func_hash)
7397 mutex_lock(&tr->ops->func_hash->regex_lock);
7398 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
7399 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
7400 mutex_unlock(&tr->ops->func_hash->regex_lock);
7402 mutex_unlock(&trace_types_lock);
7405 static void add_to_clear_hash_list(struct list_head *clear_list,
7406 struct dyn_ftrace *rec)
7408 struct ftrace_init_func *func;
7410 func = kmalloc(sizeof(*func), GFP_KERNEL);
7412 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
7417 list_add(&func->list, clear_list);
7420 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
7422 unsigned long start = (unsigned long)(start_ptr);
7423 unsigned long end = (unsigned long)(end_ptr);
7424 struct ftrace_page **last_pg = &ftrace_pages_start;
7425 struct ftrace_page *pg;
7426 struct dyn_ftrace *rec;
7427 struct dyn_ftrace key;
7428 struct ftrace_mod_map *mod_map = NULL;
7429 struct ftrace_init_func *func, *func_next;
7430 struct list_head clear_hash;
7432 INIT_LIST_HEAD(&clear_hash);
7435 key.flags = end; /* overload flags, as it is unsigned long */
7437 mutex_lock(&ftrace_lock);
7440 * If we are freeing module init memory, then check if
7441 * any tracer is active. If so, we need to save a mapping of
7442 * the module functions being freed with the address.
7444 if (mod && ftrace_ops_list != &ftrace_list_end)
7445 mod_map = allocate_ftrace_mod_map(mod, start, end);
7447 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
7448 if (end < pg->records[0].ip ||
7449 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
7452 rec = bsearch(&key, pg->records, pg->index,
7453 sizeof(struct dyn_ftrace),
7458 /* rec will be cleared from hashes after ftrace_lock unlock */
7459 add_to_clear_hash_list(&clear_hash, rec);
7462 save_ftrace_mod_rec(mod_map, rec);
7465 ftrace_update_tot_cnt--;
7467 *last_pg = pg->next;
7469 free_pages((unsigned long)pg->records, pg->order);
7470 ftrace_number_of_pages -= 1 << pg->order;
7472 ftrace_number_of_groups--;
7474 pg = container_of(last_pg, struct ftrace_page, next);
7479 memmove(rec, rec + 1,
7480 (pg->index - (rec - pg->records)) * sizeof(*rec));
7481 /* More than one function may be in this block */
7484 mutex_unlock(&ftrace_lock);
7486 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
7487 clear_func_from_hashes(func);
7492 void __init ftrace_free_init_mem(void)
7494 void *start = (void *)(&__init_begin);
7495 void *end = (void *)(&__init_end);
7497 ftrace_boot_snapshot();
7499 ftrace_free_mem(NULL, start, end);
7502 int __init __weak ftrace_dyn_arch_init(void)
7507 void __init ftrace_init(void)
7509 extern unsigned long __start_mcount_loc[];
7510 extern unsigned long __stop_mcount_loc[];
7511 unsigned long count, flags;
7514 local_irq_save(flags);
7515 ret = ftrace_dyn_arch_init();
7516 local_irq_restore(flags);
7520 count = __stop_mcount_loc - __start_mcount_loc;
7522 pr_info("ftrace: No functions to be traced?\n");
7526 pr_info("ftrace: allocating %ld entries in %ld pages\n",
7527 count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
7529 ret = ftrace_process_locs(NULL,
7533 pr_warn("ftrace: failed to allocate entries for functions\n");
7537 pr_info("ftrace: allocated %ld pages with %ld groups\n",
7538 ftrace_number_of_pages, ftrace_number_of_groups);
7540 last_ftrace_enabled = ftrace_enabled = 1;
7542 set_ftrace_early_filters();
7546 ftrace_disabled = 1;
7549 /* Do nothing if arch does not support this */
7550 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
7554 static void ftrace_update_trampoline(struct ftrace_ops *ops)
7556 unsigned long trampoline = ops->trampoline;
7558 arch_ftrace_update_trampoline(ops);
7559 if (ops->trampoline && ops->trampoline != trampoline &&
7560 (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) {
7561 /* Add to kallsyms before the perf events */
7562 ftrace_add_trampoline_to_kallsyms(ops);
7563 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
7564 ops->trampoline, ops->trampoline_size, false,
7565 FTRACE_TRAMPOLINE_SYM);
7567 * Record the perf text poke event after the ksymbol register
7570 perf_event_text_poke((void *)ops->trampoline, NULL, 0,
7571 (void *)ops->trampoline,
7572 ops->trampoline_size);
7576 void ftrace_init_trace_array(struct trace_array *tr)
7578 INIT_LIST_HEAD(&tr->func_probes);
7579 INIT_LIST_HEAD(&tr->mod_trace);
7580 INIT_LIST_HEAD(&tr->mod_notrace);
7584 struct ftrace_ops global_ops = {
7585 .func = ftrace_stub,
7586 .flags = FTRACE_OPS_FL_INITIALIZED |
7590 static int __init ftrace_nodyn_init(void)
7595 core_initcall(ftrace_nodyn_init);
7597 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
7598 static inline void ftrace_startup_all(int command) { }
7600 static void ftrace_update_trampoline(struct ftrace_ops *ops)
7604 #endif /* CONFIG_DYNAMIC_FTRACE */
7606 __init void ftrace_init_global_array_ops(struct trace_array *tr)
7608 tr->ops = &global_ops;
7609 tr->ops->private = tr;
7610 ftrace_init_trace_array(tr);
7613 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
7615 /* If we filter on pids, update to use the pid function */
7616 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
7617 if (WARN_ON(tr->ops->func != ftrace_stub))
7618 printk("ftrace ops had %pS for function\n",
7621 tr->ops->func = func;
7622 tr->ops->private = tr;
7625 void ftrace_reset_array_ops(struct trace_array *tr)
7627 tr->ops->func = ftrace_stub;
7630 static nokprobe_inline void
7631 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
7632 struct ftrace_ops *ignored, struct ftrace_regs *fregs)
7634 struct pt_regs *regs = ftrace_get_regs(fregs);
7635 struct ftrace_ops *op;
7639 * The ftrace_test_and_set_recursion() will disable preemption,
7640 * which is required since some of the ops may be dynamically
7641 * allocated, they must be freed after a synchronize_rcu().
7643 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
7647 do_for_each_ftrace_op(op, ftrace_ops_list) {
7648 /* Stub functions don't need to be called nor tested */
7649 if (op->flags & FTRACE_OPS_FL_STUB)
7652 * Check the following for each ops before calling their func:
7653 * if RCU flag is set, then rcu_is_watching() must be true
7654 * Otherwise test if the ip matches the ops filter
7656 * If any of the above fails then the op->func() is not executed.
7658 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
7659 ftrace_ops_test(op, ip, regs)) {
7660 if (FTRACE_WARN_ON(!op->func)) {
7661 pr_warn("op=%p %pS\n", op, op);
7664 op->func(ip, parent_ip, op, fregs);
7666 } while_for_each_ftrace_op(op);
7668 trace_clear_recursion(bit);
7672 * Some archs only support passing ip and parent_ip. Even though
7673 * the list function ignores the op parameter, we do not want any
7674 * C side effects, where a function is called without the caller
7675 * sending a third parameter.
7676 * Archs are to support both the regs and ftrace_ops at the same time.
7677 * If they support ftrace_ops, it is assumed they support regs.
7678 * If call backs want to use regs, they must either check for regs
7679 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
7680 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
7681 * An architecture can pass partial regs with ftrace_ops and still
7682 * set the ARCH_SUPPORTS_FTRACE_OPS.
7684 * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be
7685 * arch_ftrace_ops_list_func.
7687 #if ARCH_SUPPORTS_FTRACE_OPS
7688 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
7689 struct ftrace_ops *op, struct ftrace_regs *fregs)
7691 __ftrace_ops_list_func(ip, parent_ip, NULL, fregs);
7694 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
7696 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
7699 NOKPROBE_SYMBOL(arch_ftrace_ops_list_func);
7702 * If there's only one function registered but it does not support
7703 * recursion, needs RCU protection, then this function will be called
7704 * by the mcount trampoline.
7706 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
7707 struct ftrace_ops *op, struct ftrace_regs *fregs)
7711 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
7715 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
7716 op->func(ip, parent_ip, op, fregs);
7718 trace_clear_recursion(bit);
7720 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
7723 * ftrace_ops_get_func - get the function a trampoline should call
7724 * @ops: the ops to get the function for
7726 * Normally the mcount trampoline will call the ops->func, but there
7727 * are times that it should not. For example, if the ops does not
7728 * have its own recursion protection, then it should call the
7729 * ftrace_ops_assist_func() instead.
7731 * Returns the function that the trampoline should call for @ops.
7733 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
7736 * If the function does not handle recursion or needs to be RCU safe,
7737 * then we need to call the assist handler.
7739 if (ops->flags & (FTRACE_OPS_FL_RECURSION |
7741 return ftrace_ops_assist_func;
7747 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
7748 struct task_struct *prev,
7749 struct task_struct *next,
7750 unsigned int prev_state)
7752 struct trace_array *tr = data;
7753 struct trace_pid_list *pid_list;
7754 struct trace_pid_list *no_pid_list;
7756 pid_list = rcu_dereference_sched(tr->function_pids);
7757 no_pid_list = rcu_dereference_sched(tr->function_no_pids);
7759 if (trace_ignore_this_task(pid_list, no_pid_list, next))
7760 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7763 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7768 ftrace_pid_follow_sched_process_fork(void *data,
7769 struct task_struct *self,
7770 struct task_struct *task)
7772 struct trace_pid_list *pid_list;
7773 struct trace_array *tr = data;
7775 pid_list = rcu_dereference_sched(tr->function_pids);
7776 trace_filter_add_remove_task(pid_list, self, task);
7778 pid_list = rcu_dereference_sched(tr->function_no_pids);
7779 trace_filter_add_remove_task(pid_list, self, task);
7783 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
7785 struct trace_pid_list *pid_list;
7786 struct trace_array *tr = data;
7788 pid_list = rcu_dereference_sched(tr->function_pids);
7789 trace_filter_add_remove_task(pid_list, NULL, task);
7791 pid_list = rcu_dereference_sched(tr->function_no_pids);
7792 trace_filter_add_remove_task(pid_list, NULL, task);
7795 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
7798 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7800 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
7803 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7805 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
7810 static void clear_ftrace_pids(struct trace_array *tr, int type)
7812 struct trace_pid_list *pid_list;
7813 struct trace_pid_list *no_pid_list;
7816 pid_list = rcu_dereference_protected(tr->function_pids,
7817 lockdep_is_held(&ftrace_lock));
7818 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7819 lockdep_is_held(&ftrace_lock));
7821 /* Make sure there's something to do */
7822 if (!pid_type_enabled(type, pid_list, no_pid_list))
7825 /* See if the pids still need to be checked after this */
7826 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
7827 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7828 for_each_possible_cpu(cpu)
7829 per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE;
7832 if (type & TRACE_PIDS)
7833 rcu_assign_pointer(tr->function_pids, NULL);
7835 if (type & TRACE_NO_PIDS)
7836 rcu_assign_pointer(tr->function_no_pids, NULL);
7838 /* Wait till all users are no longer using pid filtering */
7841 if ((type & TRACE_PIDS) && pid_list)
7842 trace_pid_list_free(pid_list);
7844 if ((type & TRACE_NO_PIDS) && no_pid_list)
7845 trace_pid_list_free(no_pid_list);
7848 void ftrace_clear_pids(struct trace_array *tr)
7850 mutex_lock(&ftrace_lock);
7852 clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
7854 mutex_unlock(&ftrace_lock);
7857 static void ftrace_pid_reset(struct trace_array *tr, int type)
7859 mutex_lock(&ftrace_lock);
7860 clear_ftrace_pids(tr, type);
7862 ftrace_update_pid_func();
7863 ftrace_startup_all(0);
7865 mutex_unlock(&ftrace_lock);
7868 /* Greater than any max PID */
7869 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
7871 static void *fpid_start(struct seq_file *m, loff_t *pos)
7874 struct trace_pid_list *pid_list;
7875 struct trace_array *tr = m->private;
7877 mutex_lock(&ftrace_lock);
7878 rcu_read_lock_sched();
7880 pid_list = rcu_dereference_sched(tr->function_pids);
7883 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7885 return trace_pid_start(pid_list, pos);
7888 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
7890 struct trace_array *tr = m->private;
7891 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
7893 if (v == FTRACE_NO_PIDS) {
7897 return trace_pid_next(pid_list, v, pos);
7900 static void fpid_stop(struct seq_file *m, void *p)
7903 rcu_read_unlock_sched();
7904 mutex_unlock(&ftrace_lock);
7907 static int fpid_show(struct seq_file *m, void *v)
7909 if (v == FTRACE_NO_PIDS) {
7910 seq_puts(m, "no pid\n");
7914 return trace_pid_show(m, v);
7917 static const struct seq_operations ftrace_pid_sops = {
7918 .start = fpid_start,
7924 static void *fnpid_start(struct seq_file *m, loff_t *pos)
7927 struct trace_pid_list *pid_list;
7928 struct trace_array *tr = m->private;
7930 mutex_lock(&ftrace_lock);
7931 rcu_read_lock_sched();
7933 pid_list = rcu_dereference_sched(tr->function_no_pids);
7936 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7938 return trace_pid_start(pid_list, pos);
7941 static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos)
7943 struct trace_array *tr = m->private;
7944 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids);
7946 if (v == FTRACE_NO_PIDS) {
7950 return trace_pid_next(pid_list, v, pos);
7953 static const struct seq_operations ftrace_no_pid_sops = {
7954 .start = fnpid_start,
7960 static int pid_open(struct inode *inode, struct file *file, int type)
7962 const struct seq_operations *seq_ops;
7963 struct trace_array *tr = inode->i_private;
7967 ret = tracing_check_open_get_tr(tr);
7971 if ((file->f_mode & FMODE_WRITE) &&
7972 (file->f_flags & O_TRUNC))
7973 ftrace_pid_reset(tr, type);
7977 seq_ops = &ftrace_pid_sops;
7980 seq_ops = &ftrace_no_pid_sops;
7983 trace_array_put(tr);
7988 ret = seq_open(file, seq_ops);
7990 trace_array_put(tr);
7992 m = file->private_data;
7993 /* copy tr over to seq ops */
8001 ftrace_pid_open(struct inode *inode, struct file *file)
8003 return pid_open(inode, file, TRACE_PIDS);
8007 ftrace_no_pid_open(struct inode *inode, struct file *file)
8009 return pid_open(inode, file, TRACE_NO_PIDS);
8012 static void ignore_task_cpu(void *data)
8014 struct trace_array *tr = data;
8015 struct trace_pid_list *pid_list;
8016 struct trace_pid_list *no_pid_list;
8019 * This function is called by on_each_cpu() while the
8020 * event_mutex is held.
8022 pid_list = rcu_dereference_protected(tr->function_pids,
8023 mutex_is_locked(&ftrace_lock));
8024 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
8025 mutex_is_locked(&ftrace_lock));
8027 if (trace_ignore_this_task(pid_list, no_pid_list, current))
8028 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
8031 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
8036 pid_write(struct file *filp, const char __user *ubuf,
8037 size_t cnt, loff_t *ppos, int type)
8039 struct seq_file *m = filp->private_data;
8040 struct trace_array *tr = m->private;
8041 struct trace_pid_list *filtered_pids;
8042 struct trace_pid_list *other_pids;
8043 struct trace_pid_list *pid_list;
8049 mutex_lock(&ftrace_lock);
8053 filtered_pids = rcu_dereference_protected(tr->function_pids,
8054 lockdep_is_held(&ftrace_lock));
8055 other_pids = rcu_dereference_protected(tr->function_no_pids,
8056 lockdep_is_held(&ftrace_lock));
8059 filtered_pids = rcu_dereference_protected(tr->function_no_pids,
8060 lockdep_is_held(&ftrace_lock));
8061 other_pids = rcu_dereference_protected(tr->function_pids,
8062 lockdep_is_held(&ftrace_lock));
8070 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
8076 rcu_assign_pointer(tr->function_pids, pid_list);
8079 rcu_assign_pointer(tr->function_no_pids, pid_list);
8084 if (filtered_pids) {
8086 trace_pid_list_free(filtered_pids);
8087 } else if (pid_list && !other_pids) {
8088 /* Register a probe to set whether to ignore the tracing of a task */
8089 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
8093 * Ignoring of pids is done at task switch. But we have to
8094 * check for those tasks that are currently running.
8095 * Always do this in case a pid was appended or removed.
8097 on_each_cpu(ignore_task_cpu, tr, 1);
8099 ftrace_update_pid_func();
8100 ftrace_startup_all(0);
8102 mutex_unlock(&ftrace_lock);
8111 ftrace_pid_write(struct file *filp, const char __user *ubuf,
8112 size_t cnt, loff_t *ppos)
8114 return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
8118 ftrace_no_pid_write(struct file *filp, const char __user *ubuf,
8119 size_t cnt, loff_t *ppos)
8121 return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
8125 ftrace_pid_release(struct inode *inode, struct file *file)
8127 struct trace_array *tr = inode->i_private;
8129 trace_array_put(tr);
8131 return seq_release(inode, file);
8134 static const struct file_operations ftrace_pid_fops = {
8135 .open = ftrace_pid_open,
8136 .write = ftrace_pid_write,
8138 .llseek = tracing_lseek,
8139 .release = ftrace_pid_release,
8142 static const struct file_operations ftrace_no_pid_fops = {
8143 .open = ftrace_no_pid_open,
8144 .write = ftrace_no_pid_write,
8146 .llseek = tracing_lseek,
8147 .release = ftrace_pid_release,
8150 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
8152 trace_create_file("set_ftrace_pid", TRACE_MODE_WRITE, d_tracer,
8153 tr, &ftrace_pid_fops);
8154 trace_create_file("set_ftrace_notrace_pid", TRACE_MODE_WRITE,
8155 d_tracer, tr, &ftrace_no_pid_fops);
8158 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
8159 struct dentry *d_tracer)
8161 /* Only the top level directory has the dyn_tracefs and profile */
8162 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
8164 ftrace_init_dyn_tracefs(d_tracer);
8165 ftrace_profile_tracefs(d_tracer);
8169 * ftrace_kill - kill ftrace
8171 * This function should be used by panic code. It stops ftrace
8172 * but in a not so nice way. If you need to simply kill ftrace
8173 * from a non-atomic section, use ftrace_kill.
8175 void ftrace_kill(void)
8177 ftrace_disabled = 1;
8179 ftrace_trace_function = ftrace_stub;
8183 * ftrace_is_dead - Test if ftrace is dead or not.
8185 * Returns 1 if ftrace is "dead", zero otherwise.
8187 int ftrace_is_dead(void)
8189 return ftrace_disabled;
8192 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
8194 * When registering ftrace_ops with IPMODIFY, it is necessary to make sure
8195 * it doesn't conflict with any direct ftrace_ops. If there is existing
8196 * direct ftrace_ops on a kernel function being patched, call
8197 * FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER on it to enable sharing.
8199 * @ops: ftrace_ops being registered.
8203 * Negative on failure.
8205 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops)
8207 struct ftrace_func_entry *entry;
8208 struct ftrace_hash *hash;
8209 struct ftrace_ops *op;
8212 lockdep_assert_held_once(&direct_mutex);
8214 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
8217 hash = ops->func_hash->filter_hash;
8218 size = 1 << hash->size_bits;
8219 for (i = 0; i < size; i++) {
8220 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
8221 unsigned long ip = entry->ip;
8222 bool found_op = false;
8224 mutex_lock(&ftrace_lock);
8225 do_for_each_ftrace_op(op, ftrace_ops_list) {
8226 if (!(op->flags & FTRACE_OPS_FL_DIRECT))
8228 if (ops_references_ip(op, ip)) {
8232 } while_for_each_ftrace_op(op);
8233 mutex_unlock(&ftrace_lock);
8239 ret = op->ops_func(op, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER);
8250 * Similar to prepare_direct_functions_for_ipmodify, clean up after ops
8251 * with IPMODIFY is unregistered. The cleanup is optional for most DIRECT
8254 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)
8256 struct ftrace_func_entry *entry;
8257 struct ftrace_hash *hash;
8258 struct ftrace_ops *op;
8261 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
8264 mutex_lock(&direct_mutex);
8266 hash = ops->func_hash->filter_hash;
8267 size = 1 << hash->size_bits;
8268 for (i = 0; i < size; i++) {
8269 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
8270 unsigned long ip = entry->ip;
8271 bool found_op = false;
8273 mutex_lock(&ftrace_lock);
8274 do_for_each_ftrace_op(op, ftrace_ops_list) {
8275 if (!(op->flags & FTRACE_OPS_FL_DIRECT))
8277 if (ops_references_ip(op, ip)) {
8281 } while_for_each_ftrace_op(op);
8282 mutex_unlock(&ftrace_lock);
8284 /* The cleanup is optional, ignore any errors */
8285 if (found_op && op->ops_func)
8286 op->ops_func(op, FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER);
8289 mutex_unlock(&direct_mutex);
8292 #define lock_direct_mutex() mutex_lock(&direct_mutex)
8293 #define unlock_direct_mutex() mutex_unlock(&direct_mutex)
8295 #else /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
8297 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops)
8302 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)
8306 #define lock_direct_mutex() do { } while (0)
8307 #define unlock_direct_mutex() do { } while (0)
8309 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
8312 * Similar to register_ftrace_function, except we don't lock direct_mutex.
8314 static int register_ftrace_function_nolock(struct ftrace_ops *ops)
8318 ftrace_ops_init(ops);
8320 mutex_lock(&ftrace_lock);
8322 ret = ftrace_startup(ops, 0);
8324 mutex_unlock(&ftrace_lock);
8330 * register_ftrace_function - register a function for profiling
8331 * @ops: ops structure that holds the function for profiling.
8333 * Register a function to be called by all functions in the
8336 * Note: @ops->func and all the functions it calls must be labeled
8337 * with "notrace", otherwise it will go into a
8340 int register_ftrace_function(struct ftrace_ops *ops)
8344 lock_direct_mutex();
8345 ret = prepare_direct_functions_for_ipmodify(ops);
8349 ret = register_ftrace_function_nolock(ops);
8352 unlock_direct_mutex();
8355 EXPORT_SYMBOL_GPL(register_ftrace_function);
8358 * unregister_ftrace_function - unregister a function for profiling.
8359 * @ops: ops structure that holds the function to unregister
8361 * Unregister a function that was added to be called by ftrace profiling.
8363 int unregister_ftrace_function(struct ftrace_ops *ops)
8367 mutex_lock(&ftrace_lock);
8368 ret = ftrace_shutdown(ops, 0);
8369 mutex_unlock(&ftrace_lock);
8371 cleanup_direct_functions_after_ipmodify(ops);
8374 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
8376 static int symbols_cmp(const void *a, const void *b)
8378 const char **str_a = (const char **) a;
8379 const char **str_b = (const char **) b;
8381 return strcmp(*str_a, *str_b);
8384 struct kallsyms_data {
8385 unsigned long *addrs;
8391 /* This function gets called for all kernel and module symbols
8392 * and returns 1 in case we resolved all the requested symbols,
8395 static int kallsyms_callback(void *data, const char *name,
8396 struct module *mod, unsigned long addr)
8398 struct kallsyms_data *args = data;
8402 sym = bsearch(&name, args->syms, args->cnt, sizeof(*args->syms), symbols_cmp);
8406 idx = sym - args->syms;
8407 if (args->addrs[idx])
8410 if (!ftrace_location(addr))
8413 args->addrs[idx] = addr;
8415 return args->found == args->cnt ? 1 : 0;
8419 * ftrace_lookup_symbols - Lookup addresses for array of symbols
8421 * @sorted_syms: array of symbols pointers symbols to resolve,
8422 * must be alphabetically sorted
8423 * @cnt: number of symbols/addresses in @syms/@addrs arrays
8424 * @addrs: array for storing resulting addresses
8426 * This function looks up addresses for array of symbols provided in
8427 * @syms array (must be alphabetically sorted) and stores them in
8428 * @addrs array, which needs to be big enough to store at least @cnt
8431 * This function returns 0 if all provided symbols are found,
8434 int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs)
8436 struct kallsyms_data args;
8439 memset(addrs, 0, sizeof(*addrs) * cnt);
8441 args.syms = sorted_syms;
8445 found_all = kallsyms_on_each_symbol(kallsyms_callback, &args);
8448 found_all = module_kallsyms_on_each_symbol(NULL, kallsyms_callback, &args);
8449 return found_all ? 0 : -ESRCH;
8452 #ifdef CONFIG_SYSCTL
8454 #ifdef CONFIG_DYNAMIC_FTRACE
8455 static void ftrace_startup_sysctl(void)
8459 if (unlikely(ftrace_disabled))
8462 /* Force update next time */
8463 saved_ftrace_func = NULL;
8464 /* ftrace_start_up is true if we want ftrace running */
8465 if (ftrace_start_up) {
8466 command = FTRACE_UPDATE_CALLS;
8467 if (ftrace_graph_active)
8468 command |= FTRACE_START_FUNC_RET;
8469 ftrace_startup_enable(command);
8473 static void ftrace_shutdown_sysctl(void)
8477 if (unlikely(ftrace_disabled))
8480 /* ftrace_start_up is true if ftrace is running */
8481 if (ftrace_start_up) {
8482 command = FTRACE_DISABLE_CALLS;
8483 if (ftrace_graph_active)
8484 command |= FTRACE_STOP_FUNC_RET;
8485 ftrace_run_update_code(command);
8489 # define ftrace_startup_sysctl() do { } while (0)
8490 # define ftrace_shutdown_sysctl() do { } while (0)
8491 #endif /* CONFIG_DYNAMIC_FTRACE */
8493 static bool is_permanent_ops_registered(void)
8495 struct ftrace_ops *op;
8497 do_for_each_ftrace_op(op, ftrace_ops_list) {
8498 if (op->flags & FTRACE_OPS_FL_PERMANENT)
8500 } while_for_each_ftrace_op(op);
8506 ftrace_enable_sysctl(struct ctl_table *table, int write,
8507 void *buffer, size_t *lenp, loff_t *ppos)
8511 mutex_lock(&ftrace_lock);
8513 if (unlikely(ftrace_disabled))
8516 ret = proc_dointvec(table, write, buffer, lenp, ppos);
8518 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
8521 if (ftrace_enabled) {
8523 /* we are starting ftrace again */
8524 if (rcu_dereference_protected(ftrace_ops_list,
8525 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
8526 update_ftrace_function();
8528 ftrace_startup_sysctl();
8531 if (is_permanent_ops_registered()) {
8532 ftrace_enabled = true;
8537 /* stopping ftrace calls (just send to ftrace_stub) */
8538 ftrace_trace_function = ftrace_stub;
8540 ftrace_shutdown_sysctl();
8543 last_ftrace_enabled = !!ftrace_enabled;
8545 mutex_unlock(&ftrace_lock);
8549 static struct ctl_table ftrace_sysctls[] = {
8551 .procname = "ftrace_enabled",
8552 .data = &ftrace_enabled,
8553 .maxlen = sizeof(int),
8555 .proc_handler = ftrace_enable_sysctl,
8560 static int __init ftrace_sysctl_init(void)
8562 register_sysctl_init("kernel", ftrace_sysctls);
8565 late_initcall(ftrace_sysctl_init);