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 static inline void ftrace_ops_init(struct ftrace_ops *ops)
130 #ifdef CONFIG_DYNAMIC_FTRACE
131 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
132 mutex_init(&ops->local_hash.regex_lock);
133 ops->func_hash = &ops->local_hash;
134 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
139 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
140 struct ftrace_ops *op, struct ftrace_regs *fregs)
142 struct trace_array *tr = op->private;
146 pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid);
147 if (pid == FTRACE_PID_IGNORE)
149 if (pid != FTRACE_PID_TRACE &&
154 op->saved_func(ip, parent_ip, op, fregs);
157 static void ftrace_sync_ipi(void *data)
159 /* Probably not needed, but do it anyway */
163 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
166 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
167 * then it needs to call the list anyway.
169 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
170 FTRACE_FORCE_LIST_FUNC)
171 return ftrace_ops_list_func;
173 return ftrace_ops_get_func(ops);
176 static void update_ftrace_function(void)
181 * Prepare the ftrace_ops that the arch callback will use.
182 * If there's only one ftrace_ops registered, the ftrace_ops_list
183 * will point to the ops we want.
185 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
186 lockdep_is_held(&ftrace_lock));
188 /* If there's no ftrace_ops registered, just call the stub function */
189 if (set_function_trace_op == &ftrace_list_end) {
193 * If we are at the end of the list and this ops is
194 * recursion safe and not dynamic and the arch supports passing ops,
195 * then have the mcount trampoline call the function directly.
197 } else if (rcu_dereference_protected(ftrace_ops_list->next,
198 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
199 func = ftrace_ops_get_list_func(ftrace_ops_list);
202 /* Just use the default ftrace_ops */
203 set_function_trace_op = &ftrace_list_end;
204 func = ftrace_ops_list_func;
207 update_function_graph_func();
209 /* If there's no change, then do nothing more here */
210 if (ftrace_trace_function == func)
214 * If we are using the list function, it doesn't care
215 * about the function_trace_ops.
217 if (func == ftrace_ops_list_func) {
218 ftrace_trace_function = func;
220 * Don't even bother setting function_trace_ops,
221 * it would be racy to do so anyway.
226 #ifndef CONFIG_DYNAMIC_FTRACE
228 * For static tracing, we need to be a bit more careful.
229 * The function change takes affect immediately. Thus,
230 * we need to coordinate the setting of the function_trace_ops
231 * with the setting of the ftrace_trace_function.
233 * Set the function to the list ops, which will call the
234 * function we want, albeit indirectly, but it handles the
235 * ftrace_ops and doesn't depend on function_trace_op.
237 ftrace_trace_function = ftrace_ops_list_func;
239 * Make sure all CPUs see this. Yes this is slow, but static
240 * tracing is slow and nasty to have enabled.
242 synchronize_rcu_tasks_rude();
243 /* Now all cpus are using the list ops. */
244 function_trace_op = set_function_trace_op;
245 /* Make sure the function_trace_op is visible on all CPUs */
247 /* Nasty way to force a rmb on all cpus */
248 smp_call_function(ftrace_sync_ipi, NULL, 1);
249 /* OK, we are all set to update the ftrace_trace_function now! */
250 #endif /* !CONFIG_DYNAMIC_FTRACE */
252 ftrace_trace_function = func;
255 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
256 struct ftrace_ops *ops)
258 rcu_assign_pointer(ops->next, *list);
261 * We are entering ops into the list but another
262 * CPU might be walking that list. We need to make sure
263 * the ops->next pointer is valid before another CPU sees
264 * the ops pointer included into the list.
266 rcu_assign_pointer(*list, ops);
269 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
270 struct ftrace_ops *ops)
272 struct ftrace_ops **p;
275 * If we are removing the last function, then simply point
276 * to the ftrace_stub.
278 if (rcu_dereference_protected(*list,
279 lockdep_is_held(&ftrace_lock)) == ops &&
280 rcu_dereference_protected(ops->next,
281 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
282 *list = &ftrace_list_end;
286 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
297 static void ftrace_update_trampoline(struct ftrace_ops *ops);
299 int __register_ftrace_function(struct ftrace_ops *ops)
301 if (ops->flags & FTRACE_OPS_FL_DELETED)
304 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
307 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
309 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
310 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
311 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
313 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
314 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
317 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
318 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
320 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
323 if (!is_kernel_core_data((unsigned long)ops))
324 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
326 add_ftrace_ops(&ftrace_ops_list, ops);
328 /* Always save the function, and reset at unregistering */
329 ops->saved_func = ops->func;
331 if (ftrace_pids_enabled(ops))
332 ops->func = ftrace_pid_func;
334 ftrace_update_trampoline(ops);
337 update_ftrace_function();
342 int __unregister_ftrace_function(struct ftrace_ops *ops)
346 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
349 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
355 update_ftrace_function();
357 ops->func = ops->saved_func;
362 static void ftrace_update_pid_func(void)
364 struct ftrace_ops *op;
366 /* Only do something if we are tracing something */
367 if (ftrace_trace_function == ftrace_stub)
370 do_for_each_ftrace_op(op, ftrace_ops_list) {
371 if (op->flags & FTRACE_OPS_FL_PID) {
372 op->func = ftrace_pids_enabled(op) ?
373 ftrace_pid_func : op->saved_func;
374 ftrace_update_trampoline(op);
376 } while_for_each_ftrace_op(op);
378 update_ftrace_function();
381 #ifdef CONFIG_FUNCTION_PROFILER
382 struct ftrace_profile {
383 struct hlist_node node;
385 unsigned long counter;
386 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
387 unsigned long long time;
388 unsigned long long time_squared;
392 struct ftrace_profile_page {
393 struct ftrace_profile_page *next;
395 struct ftrace_profile records[];
398 struct ftrace_profile_stat {
400 struct hlist_head *hash;
401 struct ftrace_profile_page *pages;
402 struct ftrace_profile_page *start;
403 struct tracer_stat stat;
406 #define PROFILE_RECORDS_SIZE \
407 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
409 #define PROFILES_PER_PAGE \
410 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
412 static int ftrace_profile_enabled __read_mostly;
414 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
415 static DEFINE_MUTEX(ftrace_profile_lock);
417 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
419 #define FTRACE_PROFILE_HASH_BITS 10
420 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
423 function_stat_next(void *v, int idx)
425 struct ftrace_profile *rec = v;
426 struct ftrace_profile_page *pg;
428 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
434 if ((void *)rec >= (void *)&pg->records[pg->index]) {
438 rec = &pg->records[0];
446 static void *function_stat_start(struct tracer_stat *trace)
448 struct ftrace_profile_stat *stat =
449 container_of(trace, struct ftrace_profile_stat, stat);
451 if (!stat || !stat->start)
454 return function_stat_next(&stat->start->records[0], 0);
457 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
458 /* function graph compares on total time */
459 static int function_stat_cmp(const void *p1, const void *p2)
461 const struct ftrace_profile *a = p1;
462 const struct ftrace_profile *b = p2;
464 if (a->time < b->time)
466 if (a->time > b->time)
472 /* not function graph compares against hits */
473 static int function_stat_cmp(const void *p1, const void *p2)
475 const struct ftrace_profile *a = p1;
476 const struct ftrace_profile *b = p2;
478 if (a->counter < b->counter)
480 if (a->counter > b->counter)
487 static int function_stat_headers(struct seq_file *m)
489 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
490 seq_puts(m, " Function "
493 "--- ---- --- ---\n");
495 seq_puts(m, " Function Hit\n"
501 static int function_stat_show(struct seq_file *m, void *v)
503 struct ftrace_profile *rec = v;
504 char str[KSYM_SYMBOL_LEN];
506 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
507 static struct trace_seq s;
508 unsigned long long avg;
509 unsigned long long stddev;
511 mutex_lock(&ftrace_profile_lock);
513 /* we raced with function_profile_reset() */
514 if (unlikely(rec->counter == 0)) {
519 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
520 avg = div64_ul(rec->time, rec->counter);
521 if (tracing_thresh && (avg < tracing_thresh))
525 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
526 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
528 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
531 /* Sample standard deviation (s^2) */
532 if (rec->counter <= 1)
536 * Apply Welford's method:
537 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
539 stddev = rec->counter * rec->time_squared -
540 rec->time * rec->time;
543 * Divide only 1000 for ns^2 -> us^2 conversion.
544 * trace_print_graph_duration will divide 1000 again.
546 stddev = div64_ul(stddev,
547 rec->counter * (rec->counter - 1) * 1000);
551 trace_print_graph_duration(rec->time, &s);
552 trace_seq_puts(&s, " ");
553 trace_print_graph_duration(avg, &s);
554 trace_seq_puts(&s, " ");
555 trace_print_graph_duration(stddev, &s);
556 trace_print_seq(m, &s);
560 mutex_unlock(&ftrace_profile_lock);
565 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
567 struct ftrace_profile_page *pg;
569 pg = stat->pages = stat->start;
572 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
577 memset(stat->hash, 0,
578 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
581 static int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
583 struct ftrace_profile_page *pg;
588 /* If we already allocated, do nothing */
592 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
596 #ifdef CONFIG_DYNAMIC_FTRACE
597 functions = ftrace_update_tot_cnt;
600 * We do not know the number of functions that exist because
601 * dynamic tracing is what counts them. With past experience
602 * we have around 20K functions. That should be more than enough.
603 * It is highly unlikely we will execute every function in
609 pg = stat->start = stat->pages;
611 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
613 for (i = 1; i < pages; i++) {
614 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
625 unsigned long tmp = (unsigned long)pg;
637 static int ftrace_profile_init_cpu(int cpu)
639 struct ftrace_profile_stat *stat;
642 stat = &per_cpu(ftrace_profile_stats, cpu);
645 /* If the profile is already created, simply reset it */
646 ftrace_profile_reset(stat);
651 * We are profiling all functions, but usually only a few thousand
652 * functions are hit. We'll make a hash of 1024 items.
654 size = FTRACE_PROFILE_HASH_SIZE;
656 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
661 /* Preallocate the function profiling pages */
662 if (ftrace_profile_pages_init(stat) < 0) {
671 static int ftrace_profile_init(void)
676 for_each_possible_cpu(cpu) {
677 ret = ftrace_profile_init_cpu(cpu);
685 /* interrupts must be disabled */
686 static struct ftrace_profile *
687 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
689 struct ftrace_profile *rec;
690 struct hlist_head *hhd;
693 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
694 hhd = &stat->hash[key];
696 if (hlist_empty(hhd))
699 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
707 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
708 struct ftrace_profile *rec)
712 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
713 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
717 * The memory is already allocated, this simply finds a new record to use.
719 static struct ftrace_profile *
720 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
722 struct ftrace_profile *rec = NULL;
724 /* prevent recursion (from NMIs) */
725 if (atomic_inc_return(&stat->disabled) != 1)
729 * Try to find the function again since an NMI
730 * could have added it
732 rec = ftrace_find_profiled_func(stat, ip);
736 if (stat->pages->index == PROFILES_PER_PAGE) {
737 if (!stat->pages->next)
739 stat->pages = stat->pages->next;
742 rec = &stat->pages->records[stat->pages->index++];
744 ftrace_add_profile(stat, rec);
747 atomic_dec(&stat->disabled);
753 function_profile_call(unsigned long ip, unsigned long parent_ip,
754 struct ftrace_ops *ops, struct ftrace_regs *fregs)
756 struct ftrace_profile_stat *stat;
757 struct ftrace_profile *rec;
760 if (!ftrace_profile_enabled)
763 local_irq_save(flags);
765 stat = this_cpu_ptr(&ftrace_profile_stats);
766 if (!stat->hash || !ftrace_profile_enabled)
769 rec = ftrace_find_profiled_func(stat, ip);
771 rec = ftrace_profile_alloc(stat, ip);
778 local_irq_restore(flags);
781 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
782 static bool fgraph_graph_time = true;
784 void ftrace_graph_graph_time_control(bool enable)
786 fgraph_graph_time = enable;
789 static int profile_graph_entry(struct ftrace_graph_ent *trace)
791 struct ftrace_ret_stack *ret_stack;
793 function_profile_call(trace->func, 0, NULL, NULL);
795 /* If function graph is shutting down, ret_stack can be NULL */
796 if (!current->ret_stack)
799 ret_stack = ftrace_graph_get_ret_stack(current, 0);
801 ret_stack->subtime = 0;
806 static void profile_graph_return(struct ftrace_graph_ret *trace)
808 struct ftrace_ret_stack *ret_stack;
809 struct ftrace_profile_stat *stat;
810 unsigned long long calltime;
811 struct ftrace_profile *rec;
814 local_irq_save(flags);
815 stat = this_cpu_ptr(&ftrace_profile_stats);
816 if (!stat->hash || !ftrace_profile_enabled)
819 /* If the calltime was zero'd ignore it */
820 if (!trace->calltime)
823 calltime = trace->rettime - trace->calltime;
825 if (!fgraph_graph_time) {
827 /* Append this call time to the parent time to subtract */
828 ret_stack = ftrace_graph_get_ret_stack(current, 1);
830 ret_stack->subtime += calltime;
832 ret_stack = ftrace_graph_get_ret_stack(current, 0);
833 if (ret_stack && ret_stack->subtime < calltime)
834 calltime -= ret_stack->subtime;
839 rec = ftrace_find_profiled_func(stat, trace->func);
841 rec->time += calltime;
842 rec->time_squared += calltime * calltime;
846 local_irq_restore(flags);
849 static struct fgraph_ops fprofiler_ops = {
850 .entryfunc = &profile_graph_entry,
851 .retfunc = &profile_graph_return,
854 static int register_ftrace_profiler(void)
856 return register_ftrace_graph(&fprofiler_ops);
859 static void unregister_ftrace_profiler(void)
861 unregister_ftrace_graph(&fprofiler_ops);
864 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
865 .func = function_profile_call,
866 .flags = FTRACE_OPS_FL_INITIALIZED,
867 INIT_OPS_HASH(ftrace_profile_ops)
870 static int register_ftrace_profiler(void)
872 return register_ftrace_function(&ftrace_profile_ops);
875 static void unregister_ftrace_profiler(void)
877 unregister_ftrace_function(&ftrace_profile_ops);
879 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
882 ftrace_profile_write(struct file *filp, const char __user *ubuf,
883 size_t cnt, loff_t *ppos)
888 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
894 mutex_lock(&ftrace_profile_lock);
895 if (ftrace_profile_enabled ^ val) {
897 ret = ftrace_profile_init();
903 ret = register_ftrace_profiler();
908 ftrace_profile_enabled = 1;
910 ftrace_profile_enabled = 0;
912 * unregister_ftrace_profiler calls stop_machine
913 * so this acts like an synchronize_rcu.
915 unregister_ftrace_profiler();
919 mutex_unlock(&ftrace_profile_lock);
927 ftrace_profile_read(struct file *filp, char __user *ubuf,
928 size_t cnt, loff_t *ppos)
930 char buf[64]; /* big enough to hold a number */
933 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
934 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
937 static const struct file_operations ftrace_profile_fops = {
938 .open = tracing_open_generic,
939 .read = ftrace_profile_read,
940 .write = ftrace_profile_write,
941 .llseek = default_llseek,
944 /* used to initialize the real stat files */
945 static struct tracer_stat function_stats __initdata = {
947 .stat_start = function_stat_start,
948 .stat_next = function_stat_next,
949 .stat_cmp = function_stat_cmp,
950 .stat_headers = function_stat_headers,
951 .stat_show = function_stat_show
954 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
956 struct ftrace_profile_stat *stat;
961 for_each_possible_cpu(cpu) {
962 stat = &per_cpu(ftrace_profile_stats, cpu);
964 name = kasprintf(GFP_KERNEL, "function%d", cpu);
967 * The files created are permanent, if something happens
968 * we still do not free memory.
971 "Could not allocate stat file for cpu %d\n",
975 stat->stat = function_stats;
976 stat->stat.name = name;
977 ret = register_stat_tracer(&stat->stat);
980 "Could not register function stat for cpu %d\n",
987 trace_create_file("function_profile_enabled",
988 TRACE_MODE_WRITE, d_tracer, NULL,
989 &ftrace_profile_fops);
992 #else /* CONFIG_FUNCTION_PROFILER */
993 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
996 #endif /* CONFIG_FUNCTION_PROFILER */
998 #ifdef CONFIG_DYNAMIC_FTRACE
1000 static struct ftrace_ops *removed_ops;
1003 * Set when doing a global update, like enabling all recs or disabling them.
1004 * It is not set when just updating a single ftrace_ops.
1006 static bool update_all_ops;
1008 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1009 # error Dynamic ftrace depends on MCOUNT_RECORD
1012 struct ftrace_func_probe {
1013 struct ftrace_probe_ops *probe_ops;
1014 struct ftrace_ops ops;
1015 struct trace_array *tr;
1016 struct list_head list;
1022 * We make these constant because no one should touch them,
1023 * but they are used as the default "empty hash", to avoid allocating
1024 * it all the time. These are in a read only section such that if
1025 * anyone does try to modify it, it will cause an exception.
1027 static const struct hlist_head empty_buckets[1];
1028 static const struct ftrace_hash empty_hash = {
1029 .buckets = (struct hlist_head *)empty_buckets,
1031 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1033 struct ftrace_ops global_ops = {
1034 .func = ftrace_stub,
1035 .local_hash.notrace_hash = EMPTY_HASH,
1036 .local_hash.filter_hash = EMPTY_HASH,
1037 INIT_OPS_HASH(global_ops)
1038 .flags = FTRACE_OPS_FL_INITIALIZED |
1043 * Used by the stack unwinder to know about dynamic ftrace trampolines.
1045 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1047 struct ftrace_ops *op = NULL;
1050 * Some of the ops may be dynamically allocated,
1051 * they are freed after a synchronize_rcu().
1053 preempt_disable_notrace();
1055 do_for_each_ftrace_op(op, ftrace_ops_list) {
1057 * This is to check for dynamically allocated trampolines.
1058 * Trampolines that are in kernel text will have
1059 * core_kernel_text() return true.
1061 if (op->trampoline && op->trampoline_size)
1062 if (addr >= op->trampoline &&
1063 addr < op->trampoline + op->trampoline_size) {
1064 preempt_enable_notrace();
1067 } while_for_each_ftrace_op(op);
1068 preempt_enable_notrace();
1074 * This is used by __kernel_text_address() to return true if the
1075 * address is on a dynamically allocated trampoline that would
1076 * not return true for either core_kernel_text() or
1077 * is_module_text_address().
1079 bool is_ftrace_trampoline(unsigned long addr)
1081 return ftrace_ops_trampoline(addr) != NULL;
1084 struct ftrace_page {
1085 struct ftrace_page *next;
1086 struct dyn_ftrace *records;
1091 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1092 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1094 static struct ftrace_page *ftrace_pages_start;
1095 static struct ftrace_page *ftrace_pages;
1097 static __always_inline unsigned long
1098 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1100 if (hash->size_bits > 0)
1101 return hash_long(ip, hash->size_bits);
1106 /* Only use this function if ftrace_hash_empty() has already been tested */
1107 static __always_inline struct ftrace_func_entry *
1108 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1111 struct ftrace_func_entry *entry;
1112 struct hlist_head *hhd;
1114 key = ftrace_hash_key(hash, ip);
1115 hhd = &hash->buckets[key];
1117 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1118 if (entry->ip == ip)
1125 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1126 * @hash: The hash to look at
1127 * @ip: The instruction pointer to test
1129 * Search a given @hash to see if a given instruction pointer (@ip)
1132 * Returns the entry that holds the @ip if found. NULL otherwise.
1134 struct ftrace_func_entry *
1135 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1137 if (ftrace_hash_empty(hash))
1140 return __ftrace_lookup_ip(hash, ip);
1143 static void __add_hash_entry(struct ftrace_hash *hash,
1144 struct ftrace_func_entry *entry)
1146 struct hlist_head *hhd;
1149 key = ftrace_hash_key(hash, entry->ip);
1150 hhd = &hash->buckets[key];
1151 hlist_add_head(&entry->hlist, hhd);
1155 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1157 struct ftrace_func_entry *entry;
1159 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1164 __add_hash_entry(hash, entry);
1170 free_hash_entry(struct ftrace_hash *hash,
1171 struct ftrace_func_entry *entry)
1173 hlist_del(&entry->hlist);
1179 remove_hash_entry(struct ftrace_hash *hash,
1180 struct ftrace_func_entry *entry)
1182 hlist_del_rcu(&entry->hlist);
1186 static void ftrace_hash_clear(struct ftrace_hash *hash)
1188 struct hlist_head *hhd;
1189 struct hlist_node *tn;
1190 struct ftrace_func_entry *entry;
1191 int size = 1 << hash->size_bits;
1197 for (i = 0; i < size; i++) {
1198 hhd = &hash->buckets[i];
1199 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1200 free_hash_entry(hash, entry);
1202 FTRACE_WARN_ON(hash->count);
1205 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1207 list_del(&ftrace_mod->list);
1208 kfree(ftrace_mod->module);
1209 kfree(ftrace_mod->func);
1213 static void clear_ftrace_mod_list(struct list_head *head)
1215 struct ftrace_mod_load *p, *n;
1217 /* stack tracer isn't supported yet */
1221 mutex_lock(&ftrace_lock);
1222 list_for_each_entry_safe(p, n, head, list)
1224 mutex_unlock(&ftrace_lock);
1227 static void free_ftrace_hash(struct ftrace_hash *hash)
1229 if (!hash || hash == EMPTY_HASH)
1231 ftrace_hash_clear(hash);
1232 kfree(hash->buckets);
1236 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1238 struct ftrace_hash *hash;
1240 hash = container_of(rcu, struct ftrace_hash, rcu);
1241 free_ftrace_hash(hash);
1244 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1246 if (!hash || hash == EMPTY_HASH)
1248 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
1251 void ftrace_free_filter(struct ftrace_ops *ops)
1253 ftrace_ops_init(ops);
1254 free_ftrace_hash(ops->func_hash->filter_hash);
1255 free_ftrace_hash(ops->func_hash->notrace_hash);
1258 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1260 struct ftrace_hash *hash;
1263 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1267 size = 1 << size_bits;
1268 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1270 if (!hash->buckets) {
1275 hash->size_bits = size_bits;
1281 static int ftrace_add_mod(struct trace_array *tr,
1282 const char *func, const char *module,
1285 struct ftrace_mod_load *ftrace_mod;
1286 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1288 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1292 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1293 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1294 ftrace_mod->enable = enable;
1296 if (!ftrace_mod->func || !ftrace_mod->module)
1299 list_add(&ftrace_mod->list, mod_head);
1304 free_ftrace_mod(ftrace_mod);
1309 static struct ftrace_hash *
1310 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1312 struct ftrace_func_entry *entry;
1313 struct ftrace_hash *new_hash;
1318 new_hash = alloc_ftrace_hash(size_bits);
1323 new_hash->flags = hash->flags;
1326 if (ftrace_hash_empty(hash))
1329 size = 1 << hash->size_bits;
1330 for (i = 0; i < size; i++) {
1331 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1332 ret = add_hash_entry(new_hash, entry->ip);
1338 FTRACE_WARN_ON(new_hash->count != hash->count);
1343 free_ftrace_hash(new_hash);
1348 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1350 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1352 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1353 struct ftrace_hash *new_hash);
1355 static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
1357 struct ftrace_func_entry *entry;
1358 struct ftrace_hash *new_hash;
1359 struct hlist_head *hhd;
1360 struct hlist_node *tn;
1365 * Use around half the size (max bit of it), but
1366 * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits).
1368 bits = fls(size / 2);
1370 /* Don't allocate too much */
1371 if (bits > FTRACE_HASH_MAX_BITS)
1372 bits = FTRACE_HASH_MAX_BITS;
1374 new_hash = alloc_ftrace_hash(bits);
1378 new_hash->flags = src->flags;
1380 size = 1 << src->size_bits;
1381 for (i = 0; i < size; i++) {
1382 hhd = &src->buckets[i];
1383 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1384 remove_hash_entry(src, entry);
1385 __add_hash_entry(new_hash, entry);
1391 static struct ftrace_hash *
1392 __ftrace_hash_move(struct ftrace_hash *src)
1394 int size = src->count;
1397 * If the new source is empty, just return the empty_hash.
1399 if (ftrace_hash_empty(src))
1402 return dup_hash(src, size);
1406 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1407 struct ftrace_hash **dst, struct ftrace_hash *src)
1409 struct ftrace_hash *new_hash;
1412 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1413 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1416 new_hash = __ftrace_hash_move(src);
1420 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1422 /* IPMODIFY should be updated only when filter_hash updating */
1423 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1425 free_ftrace_hash(new_hash);
1431 * Remove the current set, update the hash and add
1434 ftrace_hash_rec_disable_modify(ops, enable);
1436 rcu_assign_pointer(*dst, new_hash);
1438 ftrace_hash_rec_enable_modify(ops, enable);
1443 static bool hash_contains_ip(unsigned long ip,
1444 struct ftrace_ops_hash *hash)
1447 * The function record is a match if it exists in the filter
1448 * hash and not in the notrace hash. Note, an empty hash is
1449 * considered a match for the filter hash, but an empty
1450 * notrace hash is considered not in the notrace hash.
1452 return (ftrace_hash_empty(hash->filter_hash) ||
1453 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1454 (ftrace_hash_empty(hash->notrace_hash) ||
1455 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1459 * Test the hashes for this ops to see if we want to call
1460 * the ops->func or not.
1462 * It's a match if the ip is in the ops->filter_hash or
1463 * the filter_hash does not exist or is empty,
1465 * the ip is not in the ops->notrace_hash.
1467 * This needs to be called with preemption disabled as
1468 * the hashes are freed with call_rcu().
1471 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1473 struct ftrace_ops_hash hash;
1476 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1478 * There's a small race when adding ops that the ftrace handler
1479 * that wants regs, may be called without them. We can not
1480 * allow that handler to be called if regs is NULL.
1482 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1486 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1487 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1489 if (hash_contains_ip(ip, &hash))
1498 * This is a double for. Do not use 'break' to break out of the loop,
1499 * you must use a goto.
1501 #define do_for_each_ftrace_rec(pg, rec) \
1502 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1504 for (_____i = 0; _____i < pg->index; _____i++) { \
1505 rec = &pg->records[_____i];
1507 #define while_for_each_ftrace_rec() \
1512 static int ftrace_cmp_recs(const void *a, const void *b)
1514 const struct dyn_ftrace *key = a;
1515 const struct dyn_ftrace *rec = b;
1517 if (key->flags < rec->ip)
1519 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1524 static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
1526 struct ftrace_page *pg;
1527 struct dyn_ftrace *rec = NULL;
1528 struct dyn_ftrace key;
1531 key.flags = end; /* overload flags, as it is unsigned long */
1533 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1534 if (end < pg->records[0].ip ||
1535 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1537 rec = bsearch(&key, pg->records, pg->index,
1538 sizeof(struct dyn_ftrace),
1547 * ftrace_location_range - return the first address of a traced location
1548 * if it touches the given ip range
1549 * @start: start of range to search.
1550 * @end: end of range to search (inclusive). @end points to the last byte
1553 * Returns rec->ip if the related ftrace location is a least partly within
1554 * the given address range. That is, the first address of the instruction
1555 * that is either a NOP or call to the function tracer. It checks the ftrace
1556 * internal tables to determine if the address belongs or not.
1558 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1560 struct dyn_ftrace *rec;
1562 rec = lookup_rec(start, end);
1570 * ftrace_location - return the ftrace location
1571 * @ip: the instruction pointer to check
1573 * If @ip matches the ftrace location, return @ip.
1574 * If @ip matches sym+0, return sym's ftrace location.
1575 * Otherwise, return 0.
1577 unsigned long ftrace_location(unsigned long ip)
1579 struct dyn_ftrace *rec;
1580 unsigned long offset;
1583 rec = lookup_rec(ip, ip);
1585 if (!kallsyms_lookup_size_offset(ip, &size, &offset))
1588 /* map sym+0 to __fentry__ */
1590 rec = lookup_rec(ip, ip + size - 1);
1601 * ftrace_text_reserved - return true if range contains an ftrace location
1602 * @start: start of range to search
1603 * @end: end of range to search (inclusive). @end points to the last byte to check.
1605 * Returns 1 if @start and @end contains a ftrace location.
1606 * That is, the instruction that is either a NOP or call to
1607 * the function tracer. It checks the ftrace internal tables to
1608 * determine if the address belongs or not.
1610 int ftrace_text_reserved(const void *start, const void *end)
1614 ret = ftrace_location_range((unsigned long)start,
1615 (unsigned long)end);
1620 /* Test if ops registered to this rec needs regs */
1621 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1623 struct ftrace_ops *ops;
1624 bool keep_regs = false;
1626 for (ops = ftrace_ops_list;
1627 ops != &ftrace_list_end; ops = ops->next) {
1628 /* pass rec in as regs to have non-NULL val */
1629 if (ftrace_ops_test(ops, rec->ip, rec)) {
1630 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1640 static struct ftrace_ops *
1641 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1642 static struct ftrace_ops *
1643 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1644 static struct ftrace_ops *
1645 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1647 static bool skip_record(struct dyn_ftrace *rec)
1650 * At boot up, weak functions are set to disable. Function tracing
1651 * can be enabled before they are, and they still need to be disabled now.
1652 * If the record is disabled, still continue if it is marked as already
1653 * enabled (this is needed to keep the accounting working).
1655 return rec->flags & FTRACE_FL_DISABLED &&
1656 !(rec->flags & FTRACE_FL_ENABLED);
1659 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1663 struct ftrace_hash *hash;
1664 struct ftrace_hash *other_hash;
1665 struct ftrace_page *pg;
1666 struct dyn_ftrace *rec;
1667 bool update = false;
1671 /* Only update if the ops has been registered */
1672 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1676 * In the filter_hash case:
1677 * If the count is zero, we update all records.
1678 * Otherwise we just update the items in the hash.
1680 * In the notrace_hash case:
1681 * We enable the update in the hash.
1682 * As disabling notrace means enabling the tracing,
1683 * and enabling notrace means disabling, the inc variable
1687 hash = ops->func_hash->filter_hash;
1688 other_hash = ops->func_hash->notrace_hash;
1689 if (ftrace_hash_empty(hash))
1693 hash = ops->func_hash->notrace_hash;
1694 other_hash = ops->func_hash->filter_hash;
1696 * If the notrace hash has no items,
1697 * then there's nothing to do.
1699 if (ftrace_hash_empty(hash))
1703 do_for_each_ftrace_rec(pg, rec) {
1704 int in_other_hash = 0;
1708 if (skip_record(rec))
1713 * Only the filter_hash affects all records.
1714 * Update if the record is not in the notrace hash.
1716 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1719 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1720 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1723 * If filter_hash is set, we want to match all functions
1724 * that are in the hash but not in the other hash.
1726 * If filter_hash is not set, then we are decrementing.
1727 * That means we match anything that is in the hash
1728 * and also in the other_hash. That is, we need to turn
1729 * off functions in the other hash because they are disabled
1732 if (filter_hash && in_hash && !in_other_hash)
1734 else if (!filter_hash && in_hash &&
1735 (in_other_hash || ftrace_hash_empty(other_hash)))
1743 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1746 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1747 rec->flags |= FTRACE_FL_DIRECT;
1750 * If there's only a single callback registered to a
1751 * function, and the ops has a trampoline registered
1752 * for it, then we can call it directly.
1754 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1755 rec->flags |= FTRACE_FL_TRAMP;
1758 * If we are adding another function callback
1759 * to this function, and the previous had a
1760 * custom trampoline in use, then we need to go
1761 * back to the default trampoline.
1763 rec->flags &= ~FTRACE_FL_TRAMP;
1766 * If any ops wants regs saved for this function
1767 * then all ops will get saved regs.
1769 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1770 rec->flags |= FTRACE_FL_REGS;
1772 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1777 * Only the internal direct_ops should have the
1778 * DIRECT flag set. Thus, if it is removing a
1779 * function, then that function should no longer
1782 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1783 rec->flags &= ~FTRACE_FL_DIRECT;
1786 * If the rec had REGS enabled and the ops that is
1787 * being removed had REGS set, then see if there is
1788 * still any ops for this record that wants regs.
1789 * If not, we can stop recording them.
1791 if (ftrace_rec_count(rec) > 0 &&
1792 rec->flags & FTRACE_FL_REGS &&
1793 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1794 if (!test_rec_ops_needs_regs(rec))
1795 rec->flags &= ~FTRACE_FL_REGS;
1799 * The TRAMP needs to be set only if rec count
1800 * is decremented to one, and the ops that is
1801 * left has a trampoline. As TRAMP can only be
1802 * enabled if there is only a single ops attached
1805 if (ftrace_rec_count(rec) == 1 &&
1806 ftrace_find_tramp_ops_any_other(rec, ops))
1807 rec->flags |= FTRACE_FL_TRAMP;
1809 rec->flags &= ~FTRACE_FL_TRAMP;
1812 * flags will be cleared in ftrace_check_record()
1813 * if rec count is zero.
1818 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1819 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
1821 /* Shortcut, if we handled all records, we are done. */
1822 if (!all && count == hash->count)
1824 } while_for_each_ftrace_rec();
1829 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1832 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1835 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1838 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1841 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1842 int filter_hash, int inc)
1844 struct ftrace_ops *op;
1846 __ftrace_hash_rec_update(ops, filter_hash, inc);
1848 if (ops->func_hash != &global_ops.local_hash)
1852 * If the ops shares the global_ops hash, then we need to update
1853 * all ops that are enabled and use this hash.
1855 do_for_each_ftrace_op(op, ftrace_ops_list) {
1859 if (op->func_hash == &global_ops.local_hash)
1860 __ftrace_hash_rec_update(op, filter_hash, inc);
1861 } while_for_each_ftrace_op(op);
1864 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1867 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1870 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1873 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1877 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1878 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1879 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1880 * Note that old_hash and new_hash has below meanings
1881 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1882 * - If the hash is EMPTY_HASH, it hits nothing
1883 * - Anything else hits the recs which match the hash entries.
1885 * DIRECT ops does not have IPMODIFY flag, but we still need to check it
1886 * against functions with FTRACE_FL_IPMODIFY. If there is any overlap, call
1887 * ops_func(SHARE_IPMODIFY_SELF) to make sure current ops can share with
1888 * IPMODIFY. If ops_func(SHARE_IPMODIFY_SELF) returns non-zero, propagate
1889 * the return value to the caller and eventually to the owner of the DIRECT
1892 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1893 struct ftrace_hash *old_hash,
1894 struct ftrace_hash *new_hash)
1896 struct ftrace_page *pg;
1897 struct dyn_ftrace *rec, *end = NULL;
1899 bool is_ipmodify, is_direct;
1901 /* Only update if the ops has been registered */
1902 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1905 is_ipmodify = ops->flags & FTRACE_OPS_FL_IPMODIFY;
1906 is_direct = ops->flags & FTRACE_OPS_FL_DIRECT;
1908 /* neither IPMODIFY nor DIRECT, skip */
1909 if (!is_ipmodify && !is_direct)
1912 if (WARN_ON_ONCE(is_ipmodify && is_direct))
1916 * Since the IPMODIFY and DIRECT are very address sensitive
1917 * actions, we do not allow ftrace_ops to set all functions to new
1920 if (!new_hash || !old_hash)
1923 /* Update rec->flags */
1924 do_for_each_ftrace_rec(pg, rec) {
1926 if (rec->flags & FTRACE_FL_DISABLED)
1929 /* We need to update only differences of filter_hash */
1930 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1931 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1932 if (in_old == in_new)
1936 if (rec->flags & FTRACE_FL_IPMODIFY) {
1939 /* Cannot have two ipmodify on same rec */
1943 FTRACE_WARN_ON(rec->flags & FTRACE_FL_DIRECT);
1946 * Another ops with IPMODIFY is already
1947 * attached. We are now attaching a direct
1948 * ops. Run SHARE_IPMODIFY_SELF, to check
1949 * whether sharing is supported.
1953 ret = ops->ops_func(ops, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF);
1956 } else if (is_ipmodify) {
1957 rec->flags |= FTRACE_FL_IPMODIFY;
1959 } else if (is_ipmodify) {
1960 rec->flags &= ~FTRACE_FL_IPMODIFY;
1962 } while_for_each_ftrace_rec();
1969 /* Roll back what we did above */
1970 do_for_each_ftrace_rec(pg, rec) {
1972 if (rec->flags & FTRACE_FL_DISABLED)
1978 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1979 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1980 if (in_old == in_new)
1984 rec->flags &= ~FTRACE_FL_IPMODIFY;
1986 rec->flags |= FTRACE_FL_IPMODIFY;
1987 } while_for_each_ftrace_rec();
1993 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1995 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1997 if (ftrace_hash_empty(hash))
2000 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
2003 /* Disabling always succeeds */
2004 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
2006 struct ftrace_hash *hash = ops->func_hash->filter_hash;
2008 if (ftrace_hash_empty(hash))
2011 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
2014 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
2015 struct ftrace_hash *new_hash)
2017 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
2019 if (ftrace_hash_empty(old_hash))
2022 if (ftrace_hash_empty(new_hash))
2025 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
2028 static void print_ip_ins(const char *fmt, const unsigned char *p)
2030 char ins[MCOUNT_INSN_SIZE];
2032 if (copy_from_kernel_nofault(ins, p, MCOUNT_INSN_SIZE)) {
2033 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
2037 printk(KERN_CONT "%s", fmt);
2038 pr_cont("%*phC", MCOUNT_INSN_SIZE, ins);
2041 enum ftrace_bug_type ftrace_bug_type;
2042 const void *ftrace_expected;
2044 static void print_bug_type(void)
2046 switch (ftrace_bug_type) {
2047 case FTRACE_BUG_UNKNOWN:
2049 case FTRACE_BUG_INIT:
2050 pr_info("Initializing ftrace call sites\n");
2052 case FTRACE_BUG_NOP:
2053 pr_info("Setting ftrace call site to NOP\n");
2055 case FTRACE_BUG_CALL:
2056 pr_info("Setting ftrace call site to call ftrace function\n");
2058 case FTRACE_BUG_UPDATE:
2059 pr_info("Updating ftrace call site to call a different ftrace function\n");
2065 * ftrace_bug - report and shutdown function tracer
2066 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2067 * @rec: The record that failed
2069 * The arch code that enables or disables the function tracing
2070 * can call ftrace_bug() when it has detected a problem in
2071 * modifying the code. @failed should be one of either:
2072 * EFAULT - if the problem happens on reading the @ip address
2073 * EINVAL - if what is read at @ip is not what was expected
2074 * EPERM - if the problem happens on writing to the @ip address
2076 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2078 unsigned long ip = rec ? rec->ip : 0;
2080 pr_info("------------[ ftrace bug ]------------\n");
2084 pr_info("ftrace faulted on modifying ");
2085 print_ip_sym(KERN_INFO, ip);
2088 pr_info("ftrace failed to modify ");
2089 print_ip_sym(KERN_INFO, ip);
2090 print_ip_ins(" actual: ", (unsigned char *)ip);
2092 if (ftrace_expected) {
2093 print_ip_ins(" expected: ", ftrace_expected);
2098 pr_info("ftrace faulted on writing ");
2099 print_ip_sym(KERN_INFO, ip);
2102 pr_info("ftrace faulted on unknown error ");
2103 print_ip_sym(KERN_INFO, ip);
2107 struct ftrace_ops *ops = NULL;
2109 pr_info("ftrace record flags: %lx\n", rec->flags);
2110 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2111 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2112 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2113 ops = ftrace_find_tramp_ops_any(rec);
2116 pr_cont("\ttramp: %pS (%pS)",
2117 (void *)ops->trampoline,
2119 ops = ftrace_find_tramp_ops_next(rec, ops);
2122 pr_cont("\ttramp: ERROR!");
2125 ip = ftrace_get_addr_curr(rec);
2126 pr_cont("\n expected tramp: %lx\n", ip);
2129 FTRACE_WARN_ON_ONCE(1);
2132 static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
2134 unsigned long flag = 0UL;
2136 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2138 if (skip_record(rec))
2139 return FTRACE_UPDATE_IGNORE;
2142 * If we are updating calls:
2144 * If the record has a ref count, then we need to enable it
2145 * because someone is using it.
2147 * Otherwise we make sure its disabled.
2149 * If we are disabling calls, then disable all records that
2152 if (enable && ftrace_rec_count(rec))
2153 flag = FTRACE_FL_ENABLED;
2156 * If enabling and the REGS flag does not match the REGS_EN, or
2157 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2158 * this record. Set flags to fail the compare against ENABLED.
2159 * Same for direct calls.
2162 if (!(rec->flags & FTRACE_FL_REGS) !=
2163 !(rec->flags & FTRACE_FL_REGS_EN))
2164 flag |= FTRACE_FL_REGS;
2166 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2167 !(rec->flags & FTRACE_FL_TRAMP_EN))
2168 flag |= FTRACE_FL_TRAMP;
2171 * Direct calls are special, as count matters.
2172 * We must test the record for direct, if the
2173 * DIRECT and DIRECT_EN do not match, but only
2174 * if the count is 1. That's because, if the
2175 * count is something other than one, we do not
2176 * want the direct enabled (it will be done via the
2177 * direct helper). But if DIRECT_EN is set, and
2178 * the count is not one, we need to clear it.
2180 if (ftrace_rec_count(rec) == 1) {
2181 if (!(rec->flags & FTRACE_FL_DIRECT) !=
2182 !(rec->flags & FTRACE_FL_DIRECT_EN))
2183 flag |= FTRACE_FL_DIRECT;
2184 } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
2185 flag |= FTRACE_FL_DIRECT;
2189 /* If the state of this record hasn't changed, then do nothing */
2190 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2191 return FTRACE_UPDATE_IGNORE;
2194 /* Save off if rec is being enabled (for return value) */
2195 flag ^= rec->flags & FTRACE_FL_ENABLED;
2198 rec->flags |= FTRACE_FL_ENABLED;
2199 if (flag & FTRACE_FL_REGS) {
2200 if (rec->flags & FTRACE_FL_REGS)
2201 rec->flags |= FTRACE_FL_REGS_EN;
2203 rec->flags &= ~FTRACE_FL_REGS_EN;
2205 if (flag & FTRACE_FL_TRAMP) {
2206 if (rec->flags & FTRACE_FL_TRAMP)
2207 rec->flags |= FTRACE_FL_TRAMP_EN;
2209 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2212 if (flag & FTRACE_FL_DIRECT) {
2214 * If there's only one user (direct_ops helper)
2215 * then we can call the direct function
2216 * directly (no ftrace trampoline).
2218 if (ftrace_rec_count(rec) == 1) {
2219 if (rec->flags & FTRACE_FL_DIRECT)
2220 rec->flags |= FTRACE_FL_DIRECT_EN;
2222 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2225 * Can only call directly if there's
2226 * only one callback to the function.
2228 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2234 * If this record is being updated from a nop, then
2235 * return UPDATE_MAKE_CALL.
2237 * return UPDATE_MODIFY_CALL to tell the caller to convert
2238 * from the save regs, to a non-save regs function or
2239 * vice versa, or from a trampoline call.
2241 if (flag & FTRACE_FL_ENABLED) {
2242 ftrace_bug_type = FTRACE_BUG_CALL;
2243 return FTRACE_UPDATE_MAKE_CALL;
2246 ftrace_bug_type = FTRACE_BUG_UPDATE;
2247 return FTRACE_UPDATE_MODIFY_CALL;
2251 /* If there's no more users, clear all flags */
2252 if (!ftrace_rec_count(rec))
2253 rec->flags &= FTRACE_FL_DISABLED;
2256 * Just disable the record, but keep the ops TRAMP
2257 * and REGS states. The _EN flags must be disabled though.
2259 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2260 FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN);
2263 ftrace_bug_type = FTRACE_BUG_NOP;
2264 return FTRACE_UPDATE_MAKE_NOP;
2268 * ftrace_update_record - set a record that now is tracing or not
2269 * @rec: the record to update
2270 * @enable: set to true if the record is tracing, false to force disable
2272 * The records that represent all functions that can be traced need
2273 * to be updated when tracing has been enabled.
2275 int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
2277 return ftrace_check_record(rec, enable, true);
2281 * ftrace_test_record - check if the record has been enabled or not
2282 * @rec: the record to test
2283 * @enable: set to true to check if enabled, false if it is disabled
2285 * The arch code may need to test if a record is already set to
2286 * tracing to determine how to modify the function code that it
2289 int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
2291 return ftrace_check_record(rec, enable, false);
2294 static struct ftrace_ops *
2295 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2297 struct ftrace_ops *op;
2298 unsigned long ip = rec->ip;
2300 do_for_each_ftrace_op(op, ftrace_ops_list) {
2302 if (!op->trampoline)
2305 if (hash_contains_ip(ip, op->func_hash))
2307 } while_for_each_ftrace_op(op);
2312 static struct ftrace_ops *
2313 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2315 struct ftrace_ops *op;
2316 unsigned long ip = rec->ip;
2318 do_for_each_ftrace_op(op, ftrace_ops_list) {
2320 if (op == op_exclude || !op->trampoline)
2323 if (hash_contains_ip(ip, op->func_hash))
2325 } while_for_each_ftrace_op(op);
2330 static struct ftrace_ops *
2331 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2332 struct ftrace_ops *op)
2334 unsigned long ip = rec->ip;
2336 while_for_each_ftrace_op(op) {
2338 if (!op->trampoline)
2341 if (hash_contains_ip(ip, op->func_hash))
2348 static struct ftrace_ops *
2349 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2351 struct ftrace_ops *op;
2352 unsigned long ip = rec->ip;
2355 * Need to check removed ops first.
2356 * If they are being removed, and this rec has a tramp,
2357 * and this rec is in the ops list, then it would be the
2358 * one with the tramp.
2361 if (hash_contains_ip(ip, &removed_ops->old_hash))
2366 * Need to find the current trampoline for a rec.
2367 * Now, a trampoline is only attached to a rec if there
2368 * was a single 'ops' attached to it. But this can be called
2369 * when we are adding another op to the rec or removing the
2370 * current one. Thus, if the op is being added, we can
2371 * ignore it because it hasn't attached itself to the rec
2374 * If an ops is being modified (hooking to different functions)
2375 * then we don't care about the new functions that are being
2376 * added, just the old ones (that are probably being removed).
2378 * If we are adding an ops to a function that already is using
2379 * a trampoline, it needs to be removed (trampolines are only
2380 * for single ops connected), then an ops that is not being
2381 * modified also needs to be checked.
2383 do_for_each_ftrace_op(op, ftrace_ops_list) {
2385 if (!op->trampoline)
2389 * If the ops is being added, it hasn't gotten to
2390 * the point to be removed from this tree yet.
2392 if (op->flags & FTRACE_OPS_FL_ADDING)
2397 * If the ops is being modified and is in the old
2398 * hash, then it is probably being removed from this
2401 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2402 hash_contains_ip(ip, &op->old_hash))
2405 * If the ops is not being added or modified, and it's
2406 * in its normal filter hash, then this must be the one
2409 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2410 hash_contains_ip(ip, op->func_hash))
2413 } while_for_each_ftrace_op(op);
2418 static struct ftrace_ops *
2419 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2421 struct ftrace_ops *op;
2422 unsigned long ip = rec->ip;
2424 do_for_each_ftrace_op(op, ftrace_ops_list) {
2425 /* pass rec in as regs to have non-NULL val */
2426 if (hash_contains_ip(ip, op->func_hash))
2428 } while_for_each_ftrace_op(op);
2433 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2434 /* Protected by rcu_tasks for reading, and direct_mutex for writing */
2435 static struct ftrace_hash *direct_functions = EMPTY_HASH;
2436 static DEFINE_MUTEX(direct_mutex);
2437 int ftrace_direct_func_count;
2440 * Search the direct_functions hash to see if the given instruction pointer
2441 * has a direct caller attached to it.
2443 unsigned long ftrace_find_rec_direct(unsigned long ip)
2445 struct ftrace_func_entry *entry;
2447 entry = __ftrace_lookup_ip(direct_functions, ip);
2451 return entry->direct;
2454 static struct ftrace_func_entry*
2455 ftrace_add_rec_direct(unsigned long ip, unsigned long addr,
2456 struct ftrace_hash **free_hash)
2458 struct ftrace_func_entry *entry;
2460 if (ftrace_hash_empty(direct_functions) ||
2461 direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
2462 struct ftrace_hash *new_hash;
2463 int size = ftrace_hash_empty(direct_functions) ? 0 :
2464 direct_functions->count + 1;
2469 new_hash = dup_hash(direct_functions, size);
2473 *free_hash = direct_functions;
2474 direct_functions = new_hash;
2477 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2482 entry->direct = addr;
2483 __add_hash_entry(direct_functions, entry);
2487 static void call_direct_funcs(unsigned long ip, unsigned long pip,
2488 struct ftrace_ops *ops, struct ftrace_regs *fregs)
2490 struct pt_regs *regs = ftrace_get_regs(fregs);
2493 addr = ftrace_find_rec_direct(ip);
2497 arch_ftrace_set_direct_caller(regs, addr);
2500 struct ftrace_ops direct_ops = {
2501 .func = call_direct_funcs,
2502 .flags = FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS
2503 | FTRACE_OPS_FL_PERMANENT,
2505 * By declaring the main trampoline as this trampoline
2506 * it will never have one allocated for it. Allocated
2507 * trampolines should not call direct functions.
2508 * The direct_ops should only be called by the builtin
2509 * ftrace_regs_caller trampoline.
2511 .trampoline = FTRACE_REGS_ADDR,
2513 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2516 * ftrace_get_addr_new - Get the call address to set to
2517 * @rec: The ftrace record descriptor
2519 * If the record has the FTRACE_FL_REGS set, that means that it
2520 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2521 * is not set, then it wants to convert to the normal callback.
2523 * Returns the address of the trampoline to set to
2525 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2527 struct ftrace_ops *ops;
2530 if ((rec->flags & FTRACE_FL_DIRECT) &&
2531 (ftrace_rec_count(rec) == 1)) {
2532 addr = ftrace_find_rec_direct(rec->ip);
2538 /* Trampolines take precedence over regs */
2539 if (rec->flags & FTRACE_FL_TRAMP) {
2540 ops = ftrace_find_tramp_ops_new(rec);
2541 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2542 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2543 (void *)rec->ip, (void *)rec->ip, rec->flags);
2544 /* Ftrace is shutting down, return anything */
2545 return (unsigned long)FTRACE_ADDR;
2547 return ops->trampoline;
2550 if (rec->flags & FTRACE_FL_REGS)
2551 return (unsigned long)FTRACE_REGS_ADDR;
2553 return (unsigned long)FTRACE_ADDR;
2557 * ftrace_get_addr_curr - Get the call address that is already there
2558 * @rec: The ftrace record descriptor
2560 * The FTRACE_FL_REGS_EN is set when the record already points to
2561 * a function that saves all the regs. Basically the '_EN' version
2562 * represents the current state of the function.
2564 * Returns the address of the trampoline that is currently being called
2566 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2568 struct ftrace_ops *ops;
2571 /* Direct calls take precedence over trampolines */
2572 if (rec->flags & FTRACE_FL_DIRECT_EN) {
2573 addr = ftrace_find_rec_direct(rec->ip);
2579 /* Trampolines take precedence over regs */
2580 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2581 ops = ftrace_find_tramp_ops_curr(rec);
2582 if (FTRACE_WARN_ON(!ops)) {
2583 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2584 (void *)rec->ip, (void *)rec->ip);
2585 /* Ftrace is shutting down, return anything */
2586 return (unsigned long)FTRACE_ADDR;
2588 return ops->trampoline;
2591 if (rec->flags & FTRACE_FL_REGS_EN)
2592 return (unsigned long)FTRACE_REGS_ADDR;
2594 return (unsigned long)FTRACE_ADDR;
2598 __ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
2600 unsigned long ftrace_old_addr;
2601 unsigned long ftrace_addr;
2604 ftrace_addr = ftrace_get_addr_new(rec);
2606 /* This needs to be done before we call ftrace_update_record */
2607 ftrace_old_addr = ftrace_get_addr_curr(rec);
2609 ret = ftrace_update_record(rec, enable);
2611 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2614 case FTRACE_UPDATE_IGNORE:
2617 case FTRACE_UPDATE_MAKE_CALL:
2618 ftrace_bug_type = FTRACE_BUG_CALL;
2619 return ftrace_make_call(rec, ftrace_addr);
2621 case FTRACE_UPDATE_MAKE_NOP:
2622 ftrace_bug_type = FTRACE_BUG_NOP;
2623 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2625 case FTRACE_UPDATE_MODIFY_CALL:
2626 ftrace_bug_type = FTRACE_BUG_UPDATE;
2627 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2630 return -1; /* unknown ftrace bug */
2633 void __weak ftrace_replace_code(int mod_flags)
2635 struct dyn_ftrace *rec;
2636 struct ftrace_page *pg;
2637 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2638 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
2641 if (unlikely(ftrace_disabled))
2644 do_for_each_ftrace_rec(pg, rec) {
2646 if (skip_record(rec))
2649 failed = __ftrace_replace_code(rec, enable);
2651 ftrace_bug(failed, rec);
2652 /* Stop processing */
2657 } while_for_each_ftrace_rec();
2660 struct ftrace_rec_iter {
2661 struct ftrace_page *pg;
2666 * ftrace_rec_iter_start - start up iterating over traced functions
2668 * Returns an iterator handle that is used to iterate over all
2669 * the records that represent address locations where functions
2672 * May return NULL if no records are available.
2674 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2677 * We only use a single iterator.
2678 * Protected by the ftrace_lock mutex.
2680 static struct ftrace_rec_iter ftrace_rec_iter;
2681 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2683 iter->pg = ftrace_pages_start;
2686 /* Could have empty pages */
2687 while (iter->pg && !iter->pg->index)
2688 iter->pg = iter->pg->next;
2697 * ftrace_rec_iter_next - get the next record to process.
2698 * @iter: The handle to the iterator.
2700 * Returns the next iterator after the given iterator @iter.
2702 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2706 if (iter->index >= iter->pg->index) {
2707 iter->pg = iter->pg->next;
2710 /* Could have empty pages */
2711 while (iter->pg && !iter->pg->index)
2712 iter->pg = iter->pg->next;
2722 * ftrace_rec_iter_record - get the record at the iterator location
2723 * @iter: The current iterator location
2725 * Returns the record that the current @iter is at.
2727 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2729 return &iter->pg->records[iter->index];
2733 ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
2737 if (unlikely(ftrace_disabled))
2740 ret = ftrace_init_nop(mod, rec);
2742 ftrace_bug_type = FTRACE_BUG_INIT;
2743 ftrace_bug(ret, rec);
2750 * archs can override this function if they must do something
2751 * before the modifying code is performed.
2753 void __weak ftrace_arch_code_modify_prepare(void)
2758 * archs can override this function if they must do something
2759 * after the modifying code is performed.
2761 void __weak ftrace_arch_code_modify_post_process(void)
2765 void ftrace_modify_all_code(int command)
2767 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2771 if (command & FTRACE_MAY_SLEEP)
2772 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2775 * If the ftrace_caller calls a ftrace_ops func directly,
2776 * we need to make sure that it only traces functions it
2777 * expects to trace. When doing the switch of functions,
2778 * we need to update to the ftrace_ops_list_func first
2779 * before the transition between old and new calls are set,
2780 * as the ftrace_ops_list_func will check the ops hashes
2781 * to make sure the ops are having the right functions
2785 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2786 if (FTRACE_WARN_ON(err))
2790 if (command & FTRACE_UPDATE_CALLS)
2791 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
2792 else if (command & FTRACE_DISABLE_CALLS)
2793 ftrace_replace_code(mod_flags);
2795 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2796 function_trace_op = set_function_trace_op;
2798 /* If irqs are disabled, we are in stop machine */
2799 if (!irqs_disabled())
2800 smp_call_function(ftrace_sync_ipi, NULL, 1);
2801 err = ftrace_update_ftrace_func(ftrace_trace_function);
2802 if (FTRACE_WARN_ON(err))
2806 if (command & FTRACE_START_FUNC_RET)
2807 err = ftrace_enable_ftrace_graph_caller();
2808 else if (command & FTRACE_STOP_FUNC_RET)
2809 err = ftrace_disable_ftrace_graph_caller();
2810 FTRACE_WARN_ON(err);
2813 static int __ftrace_modify_code(void *data)
2815 int *command = data;
2817 ftrace_modify_all_code(*command);
2823 * ftrace_run_stop_machine - go back to the stop machine method
2824 * @command: The command to tell ftrace what to do
2826 * If an arch needs to fall back to the stop machine method, the
2827 * it can call this function.
2829 void ftrace_run_stop_machine(int command)
2831 stop_machine(__ftrace_modify_code, &command, NULL);
2835 * arch_ftrace_update_code - modify the code to trace or not trace
2836 * @command: The command that needs to be done
2838 * Archs can override this function if it does not need to
2839 * run stop_machine() to modify code.
2841 void __weak arch_ftrace_update_code(int command)
2843 ftrace_run_stop_machine(command);
2846 static void ftrace_run_update_code(int command)
2848 ftrace_arch_code_modify_prepare();
2851 * By default we use stop_machine() to modify the code.
2852 * But archs can do what ever they want as long as it
2853 * is safe. The stop_machine() is the safest, but also
2854 * produces the most overhead.
2856 arch_ftrace_update_code(command);
2858 ftrace_arch_code_modify_post_process();
2861 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2862 struct ftrace_ops_hash *old_hash)
2864 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2865 ops->old_hash.filter_hash = old_hash->filter_hash;
2866 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2867 ftrace_run_update_code(command);
2868 ops->old_hash.filter_hash = NULL;
2869 ops->old_hash.notrace_hash = NULL;
2870 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2873 static ftrace_func_t saved_ftrace_func;
2874 static int ftrace_start_up;
2876 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2880 /* List of trace_ops that have allocated trampolines */
2881 static LIST_HEAD(ftrace_ops_trampoline_list);
2883 static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops)
2885 lockdep_assert_held(&ftrace_lock);
2886 list_add_rcu(&ops->list, &ftrace_ops_trampoline_list);
2889 static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops)
2891 lockdep_assert_held(&ftrace_lock);
2892 list_del_rcu(&ops->list);
2897 * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
2898 * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
2901 #define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
2902 #define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
2904 static void ftrace_trampoline_free(struct ftrace_ops *ops)
2906 if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) &&
2909 * Record the text poke event before the ksymbol unregister
2912 perf_event_text_poke((void *)ops->trampoline,
2913 (void *)ops->trampoline,
2914 ops->trampoline_size, NULL, 0);
2915 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
2916 ops->trampoline, ops->trampoline_size,
2917 true, FTRACE_TRAMPOLINE_SYM);
2918 /* Remove from kallsyms after the perf events */
2919 ftrace_remove_trampoline_from_kallsyms(ops);
2922 arch_ftrace_trampoline_free(ops);
2925 static void ftrace_startup_enable(int command)
2927 if (saved_ftrace_func != ftrace_trace_function) {
2928 saved_ftrace_func = ftrace_trace_function;
2929 command |= FTRACE_UPDATE_TRACE_FUNC;
2932 if (!command || !ftrace_enabled)
2935 ftrace_run_update_code(command);
2938 static void ftrace_startup_all(int command)
2940 update_all_ops = true;
2941 ftrace_startup_enable(command);
2942 update_all_ops = false;
2945 int ftrace_startup(struct ftrace_ops *ops, int command)
2949 if (unlikely(ftrace_disabled))
2952 ret = __register_ftrace_function(ops);
2959 * Note that ftrace probes uses this to start up
2960 * and modify functions it will probe. But we still
2961 * set the ADDING flag for modification, as probes
2962 * do not have trampolines. If they add them in the
2963 * future, then the probes will need to distinguish
2964 * between adding and updating probes.
2966 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2968 ret = ftrace_hash_ipmodify_enable(ops);
2970 /* Rollback registration process */
2971 __unregister_ftrace_function(ops);
2973 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2974 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2975 ftrace_trampoline_free(ops);
2979 if (ftrace_hash_rec_enable(ops, 1))
2980 command |= FTRACE_UPDATE_CALLS;
2982 ftrace_startup_enable(command);
2985 * If ftrace is in an undefined state, we just remove ops from list
2986 * to prevent the NULL pointer, instead of totally rolling it back and
2987 * free trampoline, because those actions could cause further damage.
2989 if (unlikely(ftrace_disabled)) {
2990 __unregister_ftrace_function(ops);
2994 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2999 int ftrace_shutdown(struct ftrace_ops *ops, int command)
3003 if (unlikely(ftrace_disabled))
3006 ret = __unregister_ftrace_function(ops);
3012 * Just warn in case of unbalance, no need to kill ftrace, it's not
3013 * critical but the ftrace_call callers may be never nopped again after
3014 * further ftrace uses.
3016 WARN_ON_ONCE(ftrace_start_up < 0);
3018 /* Disabling ipmodify never fails */
3019 ftrace_hash_ipmodify_disable(ops);
3021 if (ftrace_hash_rec_disable(ops, 1))
3022 command |= FTRACE_UPDATE_CALLS;
3024 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
3026 if (saved_ftrace_func != ftrace_trace_function) {
3027 saved_ftrace_func = ftrace_trace_function;
3028 command |= FTRACE_UPDATE_TRACE_FUNC;
3031 if (!command || !ftrace_enabled) {
3033 * If these are dynamic or per_cpu ops, they still
3034 * need their data freed. Since, function tracing is
3035 * not currently active, we can just free them
3036 * without synchronizing all CPUs.
3038 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
3045 * If the ops uses a trampoline, then it needs to be
3046 * tested first on update.
3048 ops->flags |= FTRACE_OPS_FL_REMOVING;
3051 /* The trampoline logic checks the old hashes */
3052 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
3053 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
3055 ftrace_run_update_code(command);
3058 * If there's no more ops registered with ftrace, run a
3059 * sanity check to make sure all rec flags are cleared.
3061 if (rcu_dereference_protected(ftrace_ops_list,
3062 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
3063 struct ftrace_page *pg;
3064 struct dyn_ftrace *rec;
3066 do_for_each_ftrace_rec(pg, rec) {
3067 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
3068 pr_warn(" %pS flags:%lx\n",
3069 (void *)rec->ip, rec->flags);
3070 } while_for_each_ftrace_rec();
3073 ops->old_hash.filter_hash = NULL;
3074 ops->old_hash.notrace_hash = NULL;
3077 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
3080 * Dynamic ops may be freed, we must make sure that all
3081 * callers are done before leaving this function.
3082 * The same goes for freeing the per_cpu data of the per_cpu
3085 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
3087 * We need to do a hard force of sched synchronization.
3088 * This is because we use preempt_disable() to do RCU, but
3089 * the function tracers can be called where RCU is not watching
3090 * (like before user_exit()). We can not rely on the RCU
3091 * infrastructure to do the synchronization, thus we must do it
3094 synchronize_rcu_tasks_rude();
3097 * When the kernel is preemptive, tasks can be preempted
3098 * while on a ftrace trampoline. Just scheduling a task on
3099 * a CPU is not good enough to flush them. Calling
3100 * synchronize_rcu_tasks() will wait for those tasks to
3101 * execute and either schedule voluntarily or enter user space.
3103 if (IS_ENABLED(CONFIG_PREEMPTION))
3104 synchronize_rcu_tasks();
3107 ftrace_trampoline_free(ops);
3113 static u64 ftrace_update_time;
3114 unsigned long ftrace_update_tot_cnt;
3115 unsigned long ftrace_number_of_pages;
3116 unsigned long ftrace_number_of_groups;
3118 static inline int ops_traces_mod(struct ftrace_ops *ops)
3121 * Filter_hash being empty will default to trace module.
3122 * But notrace hash requires a test of individual module functions.
3124 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
3125 ftrace_hash_empty(ops->func_hash->notrace_hash);
3128 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
3130 bool init_nop = ftrace_need_init_nop();
3131 struct ftrace_page *pg;
3132 struct dyn_ftrace *p;
3134 unsigned long update_cnt = 0;
3135 unsigned long rec_flags = 0;
3138 start = ftrace_now(raw_smp_processor_id());
3141 * When a module is loaded, this function is called to convert
3142 * the calls to mcount in its text to nops, and also to create
3143 * an entry in the ftrace data. Now, if ftrace is activated
3144 * after this call, but before the module sets its text to
3145 * read-only, the modification of enabling ftrace can fail if
3146 * the read-only is done while ftrace is converting the calls.
3147 * To prevent this, the module's records are set as disabled
3148 * and will be enabled after the call to set the module's text
3152 rec_flags |= FTRACE_FL_DISABLED;
3154 for (pg = new_pgs; pg; pg = pg->next) {
3156 for (i = 0; i < pg->index; i++) {
3158 /* If something went wrong, bail without enabling anything */
3159 if (unlikely(ftrace_disabled))
3162 p = &pg->records[i];
3163 p->flags = rec_flags;
3166 * Do the initial record conversion from mcount jump
3167 * to the NOP instructions.
3169 if (init_nop && !ftrace_nop_initialize(mod, p))
3176 stop = ftrace_now(raw_smp_processor_id());
3177 ftrace_update_time = stop - start;
3178 ftrace_update_tot_cnt += update_cnt;
3183 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3189 if (WARN_ON(!count))
3192 /* We want to fill as much as possible, with no empty pages */
3193 pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE);
3194 order = fls(pages) - 1;
3197 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3200 /* if we can't allocate this size, try something smaller */
3207 ftrace_number_of_pages += 1 << order;
3208 ftrace_number_of_groups++;
3210 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3219 static struct ftrace_page *
3220 ftrace_allocate_pages(unsigned long num_to_init)
3222 struct ftrace_page *start_pg;
3223 struct ftrace_page *pg;
3229 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3234 * Try to allocate as much as possible in one continues
3235 * location that fills in all of the space. We want to
3236 * waste as little space as possible.
3239 cnt = ftrace_allocate_records(pg, num_to_init);
3247 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3260 free_pages((unsigned long)pg->records, pg->order);
3261 ftrace_number_of_pages -= 1 << pg->order;
3263 start_pg = pg->next;
3266 ftrace_number_of_groups--;
3268 pr_info("ftrace: FAILED to allocate memory for functions\n");
3272 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3274 struct ftrace_iterator {
3278 struct ftrace_page *pg;
3279 struct dyn_ftrace *func;
3280 struct ftrace_func_probe *probe;
3281 struct ftrace_func_entry *probe_entry;
3282 struct trace_parser parser;
3283 struct ftrace_hash *hash;
3284 struct ftrace_ops *ops;
3285 struct trace_array *tr;
3286 struct list_head *mod_list;
3293 t_probe_next(struct seq_file *m, loff_t *pos)
3295 struct ftrace_iterator *iter = m->private;
3296 struct trace_array *tr = iter->ops->private;
3297 struct list_head *func_probes;
3298 struct ftrace_hash *hash;
3299 struct list_head *next;
3300 struct hlist_node *hnd = NULL;
3301 struct hlist_head *hhd;
3310 func_probes = &tr->func_probes;
3311 if (list_empty(func_probes))
3315 next = func_probes->next;
3316 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3319 if (iter->probe_entry)
3320 hnd = &iter->probe_entry->hlist;
3322 hash = iter->probe->ops.func_hash->filter_hash;
3325 * A probe being registered may temporarily have an empty hash
3326 * and it's at the end of the func_probes list.
3328 if (!hash || hash == EMPTY_HASH)
3331 size = 1 << hash->size_bits;
3334 if (iter->pidx >= size) {
3335 if (iter->probe->list.next == func_probes)
3337 next = iter->probe->list.next;
3338 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3339 hash = iter->probe->ops.func_hash->filter_hash;
3340 size = 1 << hash->size_bits;
3344 hhd = &hash->buckets[iter->pidx];
3346 if (hlist_empty(hhd)) {
3362 if (WARN_ON_ONCE(!hnd))
3365 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3370 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3372 struct ftrace_iterator *iter = m->private;
3376 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3379 if (iter->mod_pos > *pos)
3383 iter->probe_entry = NULL;
3385 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3386 p = t_probe_next(m, &l);
3393 /* Only set this if we have an item */
3394 iter->flags |= FTRACE_ITER_PROBE;
3400 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3402 struct ftrace_func_entry *probe_entry;
3403 struct ftrace_probe_ops *probe_ops;
3404 struct ftrace_func_probe *probe;
3406 probe = iter->probe;
3407 probe_entry = iter->probe_entry;
3409 if (WARN_ON_ONCE(!probe || !probe_entry))
3412 probe_ops = probe->probe_ops;
3414 if (probe_ops->print)
3415 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3417 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3418 (void *)probe_ops->func);
3424 t_mod_next(struct seq_file *m, loff_t *pos)
3426 struct ftrace_iterator *iter = m->private;
3427 struct trace_array *tr = iter->tr;
3432 iter->mod_list = iter->mod_list->next;
3434 if (iter->mod_list == &tr->mod_trace ||
3435 iter->mod_list == &tr->mod_notrace) {
3436 iter->flags &= ~FTRACE_ITER_MOD;
3440 iter->mod_pos = *pos;
3445 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3447 struct ftrace_iterator *iter = m->private;
3451 if (iter->func_pos > *pos)
3454 iter->mod_pos = iter->func_pos;
3456 /* probes are only available if tr is set */
3460 for (l = 0; l <= (*pos - iter->func_pos); ) {
3461 p = t_mod_next(m, &l);
3466 iter->flags &= ~FTRACE_ITER_MOD;
3467 return t_probe_start(m, pos);
3470 /* Only set this if we have an item */
3471 iter->flags |= FTRACE_ITER_MOD;
3477 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3479 struct ftrace_mod_load *ftrace_mod;
3480 struct trace_array *tr = iter->tr;
3482 if (WARN_ON_ONCE(!iter->mod_list) ||
3483 iter->mod_list == &tr->mod_trace ||
3484 iter->mod_list == &tr->mod_notrace)
3487 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3489 if (ftrace_mod->func)
3490 seq_printf(m, "%s", ftrace_mod->func);
3494 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3500 t_func_next(struct seq_file *m, loff_t *pos)
3502 struct ftrace_iterator *iter = m->private;
3503 struct dyn_ftrace *rec = NULL;
3508 if (iter->idx >= iter->pg->index) {
3509 if (iter->pg->next) {
3510 iter->pg = iter->pg->next;
3515 rec = &iter->pg->records[iter->idx++];
3516 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3517 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3519 ((iter->flags & FTRACE_ITER_ENABLED) &&
3520 !(rec->flags & FTRACE_FL_ENABLED))) {
3530 iter->pos = iter->func_pos = *pos;
3537 t_next(struct seq_file *m, void *v, loff_t *pos)
3539 struct ftrace_iterator *iter = m->private;
3540 loff_t l = *pos; /* t_probe_start() must use original pos */
3543 if (unlikely(ftrace_disabled))
3546 if (iter->flags & FTRACE_ITER_PROBE)
3547 return t_probe_next(m, pos);
3549 if (iter->flags & FTRACE_ITER_MOD)
3550 return t_mod_next(m, pos);
3552 if (iter->flags & FTRACE_ITER_PRINTALL) {
3553 /* next must increment pos, and t_probe_start does not */
3555 return t_mod_start(m, &l);
3558 ret = t_func_next(m, pos);
3561 return t_mod_start(m, &l);
3566 static void reset_iter_read(struct ftrace_iterator *iter)
3570 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3573 static void *t_start(struct seq_file *m, loff_t *pos)
3575 struct ftrace_iterator *iter = m->private;
3579 mutex_lock(&ftrace_lock);
3581 if (unlikely(ftrace_disabled))
3585 * If an lseek was done, then reset and start from beginning.
3587 if (*pos < iter->pos)
3588 reset_iter_read(iter);
3591 * For set_ftrace_filter reading, if we have the filter
3592 * off, we can short cut and just print out that all
3593 * functions are enabled.
3595 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3596 ftrace_hash_empty(iter->hash)) {
3597 iter->func_pos = 1; /* Account for the message */
3599 return t_mod_start(m, pos);
3600 iter->flags |= FTRACE_ITER_PRINTALL;
3601 /* reset in case of seek/pread */
3602 iter->flags &= ~FTRACE_ITER_PROBE;
3606 if (iter->flags & FTRACE_ITER_MOD)
3607 return t_mod_start(m, pos);
3610 * Unfortunately, we need to restart at ftrace_pages_start
3611 * every time we let go of the ftrace_mutex. This is because
3612 * those pointers can change without the lock.
3614 iter->pg = ftrace_pages_start;
3616 for (l = 0; l <= *pos; ) {
3617 p = t_func_next(m, &l);
3623 return t_mod_start(m, pos);
3628 static void t_stop(struct seq_file *m, void *p)
3630 mutex_unlock(&ftrace_lock);
3634 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3639 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3640 struct dyn_ftrace *rec)
3644 ptr = arch_ftrace_trampoline_func(ops, rec);
3646 seq_printf(m, " ->%pS", ptr);
3649 #ifdef FTRACE_MCOUNT_MAX_OFFSET
3651 * Weak functions can still have an mcount/fentry that is saved in
3652 * the __mcount_loc section. These can be detected by having a
3653 * symbol offset of greater than FTRACE_MCOUNT_MAX_OFFSET, as the
3654 * symbol found by kallsyms is not the function that the mcount/fentry
3655 * is part of. The offset is much greater in these cases.
3657 * Test the record to make sure that the ip points to a valid kallsyms
3658 * and if not, mark it disabled.
3660 static int test_for_valid_rec(struct dyn_ftrace *rec)
3662 char str[KSYM_SYMBOL_LEN];
3663 unsigned long offset;
3666 ret = kallsyms_lookup(rec->ip, NULL, &offset, NULL, str);
3668 /* Weak functions can cause invalid addresses */
3669 if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) {
3670 rec->flags |= FTRACE_FL_DISABLED;
3676 static struct workqueue_struct *ftrace_check_wq __initdata;
3677 static struct work_struct ftrace_check_work __initdata;
3680 * Scan all the mcount/fentry entries to make sure they are valid.
3682 static __init void ftrace_check_work_func(struct work_struct *work)
3684 struct ftrace_page *pg;
3685 struct dyn_ftrace *rec;
3687 mutex_lock(&ftrace_lock);
3688 do_for_each_ftrace_rec(pg, rec) {
3689 test_for_valid_rec(rec);
3690 } while_for_each_ftrace_rec();
3691 mutex_unlock(&ftrace_lock);
3694 static int __init ftrace_check_for_weak_functions(void)
3696 INIT_WORK(&ftrace_check_work, ftrace_check_work_func);
3698 ftrace_check_wq = alloc_workqueue("ftrace_check_wq", WQ_UNBOUND, 0);
3700 queue_work(ftrace_check_wq, &ftrace_check_work);
3704 static int __init ftrace_check_sync(void)
3706 /* Make sure the ftrace_check updates are finished */
3707 if (ftrace_check_wq)
3708 destroy_workqueue(ftrace_check_wq);
3712 late_initcall_sync(ftrace_check_sync);
3713 subsys_initcall(ftrace_check_for_weak_functions);
3715 static int print_rec(struct seq_file *m, unsigned long ip)
3717 unsigned long offset;
3718 char str[KSYM_SYMBOL_LEN];
3722 ret = kallsyms_lookup(ip, NULL, &offset, &modname, str);
3723 /* Weak functions can cause invalid addresses */
3724 if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) {
3725 snprintf(str, KSYM_SYMBOL_LEN, "%s_%ld",
3726 FTRACE_INVALID_FUNCTION, offset);
3732 seq_printf(m, " [%s]", modname);
3733 return ret == NULL ? -1 : 0;
3736 static inline int test_for_valid_rec(struct dyn_ftrace *rec)
3741 static inline int print_rec(struct seq_file *m, unsigned long ip)
3743 seq_printf(m, "%ps", (void *)ip);
3748 static int t_show(struct seq_file *m, void *v)
3750 struct ftrace_iterator *iter = m->private;
3751 struct dyn_ftrace *rec;
3753 if (iter->flags & FTRACE_ITER_PROBE)
3754 return t_probe_show(m, iter);
3756 if (iter->flags & FTRACE_ITER_MOD)
3757 return t_mod_show(m, iter);
3759 if (iter->flags & FTRACE_ITER_PRINTALL) {
3760 if (iter->flags & FTRACE_ITER_NOTRACE)
3761 seq_puts(m, "#### no functions disabled ####\n");
3763 seq_puts(m, "#### all functions enabled ####\n");
3772 if (print_rec(m, rec->ip)) {
3773 /* This should only happen when a rec is disabled */
3774 WARN_ON_ONCE(!(rec->flags & FTRACE_FL_DISABLED));
3779 if (iter->flags & FTRACE_ITER_ENABLED) {
3780 struct ftrace_ops *ops;
3782 seq_printf(m, " (%ld)%s%s%s",
3783 ftrace_rec_count(rec),
3784 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3785 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
3786 rec->flags & FTRACE_FL_DIRECT ? " D" : " ");
3787 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3788 ops = ftrace_find_tramp_ops_any(rec);
3791 seq_printf(m, "\ttramp: %pS (%pS)",
3792 (void *)ops->trampoline,
3794 add_trampoline_func(m, ops, rec);
3795 ops = ftrace_find_tramp_ops_next(rec, ops);
3798 seq_puts(m, "\ttramp: ERROR!");
3800 add_trampoline_func(m, NULL, rec);
3802 if (rec->flags & FTRACE_FL_DIRECT) {
3803 unsigned long direct;
3805 direct = ftrace_find_rec_direct(rec->ip);
3807 seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3816 static const struct seq_operations show_ftrace_seq_ops = {
3824 ftrace_avail_open(struct inode *inode, struct file *file)
3826 struct ftrace_iterator *iter;
3829 ret = security_locked_down(LOCKDOWN_TRACEFS);
3833 if (unlikely(ftrace_disabled))
3836 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3840 iter->pg = ftrace_pages_start;
3841 iter->ops = &global_ops;
3847 ftrace_enabled_open(struct inode *inode, struct file *file)
3849 struct ftrace_iterator *iter;
3852 * This shows us what functions are currently being
3853 * traced and by what. Not sure if we want lockdown
3854 * to hide such critical information for an admin.
3855 * Although, perhaps it can show information we don't
3856 * want people to see, but if something is tracing
3857 * something, we probably want to know about it.
3860 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3864 iter->pg = ftrace_pages_start;
3865 iter->flags = FTRACE_ITER_ENABLED;
3866 iter->ops = &global_ops;
3872 * ftrace_regex_open - initialize function tracer filter files
3873 * @ops: The ftrace_ops that hold the hash filters
3874 * @flag: The type of filter to process
3875 * @inode: The inode, usually passed in to your open routine
3876 * @file: The file, usually passed in to your open routine
3878 * ftrace_regex_open() initializes the filter files for the
3879 * @ops. Depending on @flag it may process the filter hash or
3880 * the notrace hash of @ops. With this called from the open
3881 * routine, you can use ftrace_filter_write() for the write
3882 * routine if @flag has FTRACE_ITER_FILTER set, or
3883 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3884 * tracing_lseek() should be used as the lseek routine, and
3885 * release must call ftrace_regex_release().
3888 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3889 struct inode *inode, struct file *file)
3891 struct ftrace_iterator *iter;
3892 struct ftrace_hash *hash;
3893 struct list_head *mod_head;
3894 struct trace_array *tr = ops->private;
3897 ftrace_ops_init(ops);
3899 if (unlikely(ftrace_disabled))
3902 if (tracing_check_open_get_tr(tr))
3905 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3909 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3916 mutex_lock(&ops->func_hash->regex_lock);
3918 if (flag & FTRACE_ITER_NOTRACE) {
3919 hash = ops->func_hash->notrace_hash;
3920 mod_head = tr ? &tr->mod_notrace : NULL;
3922 hash = ops->func_hash->filter_hash;
3923 mod_head = tr ? &tr->mod_trace : NULL;
3926 iter->mod_list = mod_head;
3928 if (file->f_mode & FMODE_WRITE) {
3929 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3931 if (file->f_flags & O_TRUNC) {
3932 iter->hash = alloc_ftrace_hash(size_bits);
3933 clear_ftrace_mod_list(mod_head);
3935 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3939 trace_parser_put(&iter->parser);
3947 if (file->f_mode & FMODE_READ) {
3948 iter->pg = ftrace_pages_start;
3950 ret = seq_open(file, &show_ftrace_seq_ops);
3952 struct seq_file *m = file->private_data;
3956 free_ftrace_hash(iter->hash);
3957 trace_parser_put(&iter->parser);
3960 file->private_data = iter;
3963 mutex_unlock(&ops->func_hash->regex_lock);
3969 trace_array_put(tr);
3976 ftrace_filter_open(struct inode *inode, struct file *file)
3978 struct ftrace_ops *ops = inode->i_private;
3980 /* Checks for tracefs lockdown */
3981 return ftrace_regex_open(ops,
3982 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3987 ftrace_notrace_open(struct inode *inode, struct file *file)
3989 struct ftrace_ops *ops = inode->i_private;
3991 /* Checks for tracefs lockdown */
3992 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3996 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3997 struct ftrace_glob {
4004 * If symbols in an architecture don't correspond exactly to the user-visible
4005 * name of what they represent, it is possible to define this function to
4006 * perform the necessary adjustments.
4008 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
4013 static int ftrace_match(char *str, struct ftrace_glob *g)
4018 str = arch_ftrace_match_adjust(str, g->search);
4022 if (strcmp(str, g->search) == 0)
4025 case MATCH_FRONT_ONLY:
4026 if (strncmp(str, g->search, g->len) == 0)
4029 case MATCH_MIDDLE_ONLY:
4030 if (strstr(str, g->search))
4033 case MATCH_END_ONLY:
4035 if (slen >= g->len &&
4036 memcmp(str + slen - g->len, g->search, g->len) == 0)
4040 if (glob_match(g->search, str))
4049 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
4051 struct ftrace_func_entry *entry;
4054 entry = ftrace_lookup_ip(hash, rec->ip);
4056 /* Do nothing if it doesn't exist */
4060 free_hash_entry(hash, entry);
4062 /* Do nothing if it exists */
4066 ret = add_hash_entry(hash, rec->ip);
4072 add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
4075 long index = simple_strtoul(func_g->search, NULL, 0);
4076 struct ftrace_page *pg;
4077 struct dyn_ftrace *rec;
4079 /* The index starts at 1 */
4083 do_for_each_ftrace_rec(pg, rec) {
4084 if (pg->index <= index) {
4086 /* this is a double loop, break goes to the next page */
4089 rec = &pg->records[index];
4090 enter_record(hash, rec, clear_filter);
4092 } while_for_each_ftrace_rec();
4096 #ifdef FTRACE_MCOUNT_MAX_OFFSET
4097 static int lookup_ip(unsigned long ip, char **modname, char *str)
4099 unsigned long offset;
4101 kallsyms_lookup(ip, NULL, &offset, modname, str);
4102 if (offset > FTRACE_MCOUNT_MAX_OFFSET)
4107 static int lookup_ip(unsigned long ip, char **modname, char *str)
4109 kallsyms_lookup(ip, NULL, NULL, modname, str);
4115 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
4116 struct ftrace_glob *mod_g, int exclude_mod)
4118 char str[KSYM_SYMBOL_LEN];
4121 if (lookup_ip(rec->ip, &modname, str)) {
4122 /* This should only happen when a rec is disabled */
4123 WARN_ON_ONCE(system_state == SYSTEM_RUNNING &&
4124 !(rec->flags & FTRACE_FL_DISABLED));
4129 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
4131 /* blank module name to match all modules */
4133 /* blank module globbing: modname xor exclude_mod */
4134 if (!exclude_mod != !modname)
4140 * exclude_mod is set to trace everything but the given
4141 * module. If it is set and the module matches, then
4142 * return 0. If it is not set, and the module doesn't match
4143 * also return 0. Otherwise, check the function to see if
4146 if (!mod_matches == !exclude_mod)
4149 /* blank search means to match all funcs in the mod */
4154 return ftrace_match(str, func_g);
4158 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
4160 struct ftrace_page *pg;
4161 struct dyn_ftrace *rec;
4162 struct ftrace_glob func_g = { .type = MATCH_FULL };
4163 struct ftrace_glob mod_g = { .type = MATCH_FULL };
4164 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
4165 int exclude_mod = 0;
4168 int clear_filter = 0;
4171 func_g.type = filter_parse_regex(func, len, &func_g.search,
4173 func_g.len = strlen(func_g.search);
4177 mod_g.type = filter_parse_regex(mod, strlen(mod),
4178 &mod_g.search, &exclude_mod);
4179 mod_g.len = strlen(mod_g.search);
4182 mutex_lock(&ftrace_lock);
4184 if (unlikely(ftrace_disabled))
4187 if (func_g.type == MATCH_INDEX) {
4188 found = add_rec_by_index(hash, &func_g, clear_filter);
4192 do_for_each_ftrace_rec(pg, rec) {
4194 if (rec->flags & FTRACE_FL_DISABLED)
4197 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
4198 ret = enter_record(hash, rec, clear_filter);
4205 } while_for_each_ftrace_rec();
4207 mutex_unlock(&ftrace_lock);
4213 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
4215 return match_records(hash, buff, len, NULL);
4218 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4219 struct ftrace_ops_hash *old_hash)
4221 struct ftrace_ops *op;
4223 if (!ftrace_enabled)
4226 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4227 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4232 * If this is the shared global_ops filter, then we need to
4233 * check if there is another ops that shares it, is enabled.
4234 * If so, we still need to run the modify code.
4236 if (ops->func_hash != &global_ops.local_hash)
4239 do_for_each_ftrace_op(op, ftrace_ops_list) {
4240 if (op->func_hash == &global_ops.local_hash &&
4241 op->flags & FTRACE_OPS_FL_ENABLED) {
4242 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4243 /* Only need to do this once */
4246 } while_for_each_ftrace_op(op);
4249 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
4250 struct ftrace_hash **orig_hash,
4251 struct ftrace_hash *hash,
4254 struct ftrace_ops_hash old_hash_ops;
4255 struct ftrace_hash *old_hash;
4258 old_hash = *orig_hash;
4259 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4260 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4261 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4263 ftrace_ops_update_code(ops, &old_hash_ops);
4264 free_ftrace_hash_rcu(old_hash);
4269 static bool module_exists(const char *module)
4271 /* All modules have the symbol __this_module */
4272 static const char this_mod[] = "__this_module";
4273 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
4277 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
4279 if (n > sizeof(modname) - 1)
4282 val = module_kallsyms_lookup_name(modname);
4286 static int cache_mod(struct trace_array *tr,
4287 const char *func, char *module, int enable)
4289 struct ftrace_mod_load *ftrace_mod, *n;
4290 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4293 mutex_lock(&ftrace_lock);
4295 /* We do not cache inverse filters */
4296 if (func[0] == '!') {
4300 /* Look to remove this hash */
4301 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4302 if (strcmp(ftrace_mod->module, module) != 0)
4305 /* no func matches all */
4306 if (strcmp(func, "*") == 0 ||
4307 (ftrace_mod->func &&
4308 strcmp(ftrace_mod->func, func) == 0)) {
4310 free_ftrace_mod(ftrace_mod);
4318 /* We only care about modules that have not been loaded yet */
4319 if (module_exists(module))
4322 /* Save this string off, and execute it when the module is loaded */
4323 ret = ftrace_add_mod(tr, func, module, enable);
4325 mutex_unlock(&ftrace_lock);
4331 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4332 int reset, int enable);
4334 #ifdef CONFIG_MODULES
4335 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4336 char *mod, bool enable)
4338 struct ftrace_mod_load *ftrace_mod, *n;
4339 struct ftrace_hash **orig_hash, *new_hash;
4340 LIST_HEAD(process_mods);
4343 mutex_lock(&ops->func_hash->regex_lock);
4346 orig_hash = &ops->func_hash->filter_hash;
4348 orig_hash = &ops->func_hash->notrace_hash;
4350 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4353 goto out; /* warn? */
4355 mutex_lock(&ftrace_lock);
4357 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4359 if (strcmp(ftrace_mod->module, mod) != 0)
4362 if (ftrace_mod->func)
4363 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4365 func = kstrdup("*", GFP_KERNEL);
4367 if (!func) /* warn? */
4370 list_move(&ftrace_mod->list, &process_mods);
4372 /* Use the newly allocated func, as it may be "*" */
4373 kfree(ftrace_mod->func);
4374 ftrace_mod->func = func;
4377 mutex_unlock(&ftrace_lock);
4379 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4381 func = ftrace_mod->func;
4383 /* Grabs ftrace_lock, which is why we have this extra step */
4384 match_records(new_hash, func, strlen(func), mod);
4385 free_ftrace_mod(ftrace_mod);
4388 if (enable && list_empty(head))
4389 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4391 mutex_lock(&ftrace_lock);
4393 ftrace_hash_move_and_update_ops(ops, orig_hash,
4395 mutex_unlock(&ftrace_lock);
4398 mutex_unlock(&ops->func_hash->regex_lock);
4400 free_ftrace_hash(new_hash);
4403 static void process_cached_mods(const char *mod_name)
4405 struct trace_array *tr;
4408 mod = kstrdup(mod_name, GFP_KERNEL);
4412 mutex_lock(&trace_types_lock);
4413 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4414 if (!list_empty(&tr->mod_trace))
4415 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4416 if (!list_empty(&tr->mod_notrace))
4417 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4419 mutex_unlock(&trace_types_lock);
4426 * We register the module command as a template to show others how
4427 * to register the a command as well.
4431 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4432 char *func_orig, char *cmd, char *module, int enable)
4437 /* match_records() modifies func, and we need the original */
4438 func = kstrdup(func_orig, GFP_KERNEL);
4443 * cmd == 'mod' because we only registered this func
4444 * for the 'mod' ftrace_func_command.
4445 * But if you register one func with multiple commands,
4446 * you can tell which command was used by the cmd
4449 ret = match_records(hash, func, strlen(func), module);
4453 return cache_mod(tr, func_orig, module, enable);
4459 static struct ftrace_func_command ftrace_mod_cmd = {
4461 .func = ftrace_mod_callback,
4464 static int __init ftrace_mod_cmd_init(void)
4466 return register_ftrace_command(&ftrace_mod_cmd);
4468 core_initcall(ftrace_mod_cmd_init);
4470 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4471 struct ftrace_ops *op, struct ftrace_regs *fregs)
4473 struct ftrace_probe_ops *probe_ops;
4474 struct ftrace_func_probe *probe;
4476 probe = container_of(op, struct ftrace_func_probe, ops);
4477 probe_ops = probe->probe_ops;
4480 * Disable preemption for these calls to prevent a RCU grace
4481 * period. This syncs the hash iteration and freeing of items
4482 * on the hash. rcu_read_lock is too dangerous here.
4484 preempt_disable_notrace();
4485 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4486 preempt_enable_notrace();
4489 struct ftrace_func_map {
4490 struct ftrace_func_entry entry;
4494 struct ftrace_func_mapper {
4495 struct ftrace_hash hash;
4499 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4501 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4503 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4505 struct ftrace_hash *hash;
4508 * The mapper is simply a ftrace_hash, but since the entries
4509 * in the hash are not ftrace_func_entry type, we define it
4510 * as a separate structure.
4512 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4513 return (struct ftrace_func_mapper *)hash;
4517 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4518 * @mapper: The mapper that has the ip maps
4519 * @ip: the instruction pointer to find the data for
4521 * Returns the data mapped to @ip if found otherwise NULL. The return
4522 * is actually the address of the mapper data pointer. The address is
4523 * returned for use cases where the data is no bigger than a long, and
4524 * the user can use the data pointer as its data instead of having to
4525 * allocate more memory for the reference.
4527 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4530 struct ftrace_func_entry *entry;
4531 struct ftrace_func_map *map;
4533 entry = ftrace_lookup_ip(&mapper->hash, ip);
4537 map = (struct ftrace_func_map *)entry;
4542 * ftrace_func_mapper_add_ip - Map some data to an ip
4543 * @mapper: The mapper that has the ip maps
4544 * @ip: The instruction pointer address to map @data to
4545 * @data: The data to map to @ip
4547 * Returns 0 on success otherwise an error.
4549 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4550 unsigned long ip, void *data)
4552 struct ftrace_func_entry *entry;
4553 struct ftrace_func_map *map;
4555 entry = ftrace_lookup_ip(&mapper->hash, ip);
4559 map = kmalloc(sizeof(*map), GFP_KERNEL);
4566 __add_hash_entry(&mapper->hash, &map->entry);
4572 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4573 * @mapper: The mapper that has the ip maps
4574 * @ip: The instruction pointer address to remove the data from
4576 * Returns the data if it is found, otherwise NULL.
4577 * Note, if the data pointer is used as the data itself, (see
4578 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4579 * if the data pointer was set to zero.
4581 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4584 struct ftrace_func_entry *entry;
4585 struct ftrace_func_map *map;
4588 entry = ftrace_lookup_ip(&mapper->hash, ip);
4592 map = (struct ftrace_func_map *)entry;
4595 remove_hash_entry(&mapper->hash, entry);
4602 * free_ftrace_func_mapper - free a mapping of ips and data
4603 * @mapper: The mapper that has the ip maps
4604 * @free_func: A function to be called on each data item.
4606 * This is used to free the function mapper. The @free_func is optional
4607 * and can be used if the data needs to be freed as well.
4609 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4610 ftrace_mapper_func free_func)
4612 struct ftrace_func_entry *entry;
4613 struct ftrace_func_map *map;
4614 struct hlist_head *hhd;
4620 if (free_func && mapper->hash.count) {
4621 size = 1 << mapper->hash.size_bits;
4622 for (i = 0; i < size; i++) {
4623 hhd = &mapper->hash.buckets[i];
4624 hlist_for_each_entry(entry, hhd, hlist) {
4625 map = (struct ftrace_func_map *)entry;
4630 free_ftrace_hash(&mapper->hash);
4633 static void release_probe(struct ftrace_func_probe *probe)
4635 struct ftrace_probe_ops *probe_ops;
4637 mutex_lock(&ftrace_lock);
4639 WARN_ON(probe->ref <= 0);
4641 /* Subtract the ref that was used to protect this instance */
4645 probe_ops = probe->probe_ops;
4647 * Sending zero as ip tells probe_ops to free
4648 * the probe->data itself
4650 if (probe_ops->free)
4651 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4652 list_del(&probe->list);
4655 mutex_unlock(&ftrace_lock);
4658 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4661 * Add one ref to keep it from being freed when releasing the
4662 * ftrace_lock mutex.
4668 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4669 struct ftrace_probe_ops *probe_ops,
4672 struct ftrace_func_probe *probe = NULL, *iter;
4673 struct ftrace_func_entry *entry;
4674 struct ftrace_hash **orig_hash;
4675 struct ftrace_hash *old_hash;
4676 struct ftrace_hash *hash;
4685 /* We do not support '!' for function probes */
4686 if (WARN_ON(glob[0] == '!'))
4690 mutex_lock(&ftrace_lock);
4691 /* Check if the probe_ops is already registered */
4692 list_for_each_entry(iter, &tr->func_probes, list) {
4693 if (iter->probe_ops == probe_ops) {
4699 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4701 mutex_unlock(&ftrace_lock);
4704 probe->probe_ops = probe_ops;
4705 probe->ops.func = function_trace_probe_call;
4707 ftrace_ops_init(&probe->ops);
4708 list_add(&probe->list, &tr->func_probes);
4711 acquire_probe_locked(probe);
4713 mutex_unlock(&ftrace_lock);
4716 * Note, there's a small window here that the func_hash->filter_hash
4717 * may be NULL or empty. Need to be careful when reading the loop.
4719 mutex_lock(&probe->ops.func_hash->regex_lock);
4721 orig_hash = &probe->ops.func_hash->filter_hash;
4722 old_hash = *orig_hash;
4723 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4730 ret = ftrace_match_records(hash, glob, strlen(glob));
4732 /* Nothing found? */
4739 size = 1 << hash->size_bits;
4740 for (i = 0; i < size; i++) {
4741 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4742 if (ftrace_lookup_ip(old_hash, entry->ip))
4745 * The caller might want to do something special
4746 * for each function we find. We call the callback
4747 * to give the caller an opportunity to do so.
4749 if (probe_ops->init) {
4750 ret = probe_ops->init(probe_ops, tr,
4754 if (probe_ops->free && count)
4755 probe_ops->free(probe_ops, tr,
4765 mutex_lock(&ftrace_lock);
4768 /* Nothing was added? */
4773 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4778 /* One ref for each new function traced */
4779 probe->ref += count;
4781 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4782 ret = ftrace_startup(&probe->ops, 0);
4785 mutex_unlock(&ftrace_lock);
4790 mutex_unlock(&probe->ops.func_hash->regex_lock);
4791 free_ftrace_hash(hash);
4793 release_probe(probe);
4798 if (!probe_ops->free || !count)
4801 /* Failed to do the move, need to call the free functions */
4802 for (i = 0; i < size; i++) {
4803 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4804 if (ftrace_lookup_ip(old_hash, entry->ip))
4806 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4813 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4814 struct ftrace_probe_ops *probe_ops)
4816 struct ftrace_func_probe *probe = NULL, *iter;
4817 struct ftrace_ops_hash old_hash_ops;
4818 struct ftrace_func_entry *entry;
4819 struct ftrace_glob func_g;
4820 struct ftrace_hash **orig_hash;
4821 struct ftrace_hash *old_hash;
4822 struct ftrace_hash *hash = NULL;
4823 struct hlist_node *tmp;
4824 struct hlist_head hhd;
4825 char str[KSYM_SYMBOL_LEN];
4827 int i, ret = -ENODEV;
4830 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4831 func_g.search = NULL;
4835 func_g.type = filter_parse_regex(glob, strlen(glob),
4836 &func_g.search, ¬);
4837 func_g.len = strlen(func_g.search);
4839 /* we do not support '!' for function probes */
4844 mutex_lock(&ftrace_lock);
4845 /* Check if the probe_ops is already registered */
4846 list_for_each_entry(iter, &tr->func_probes, list) {
4847 if (iter->probe_ops == probe_ops) {
4853 goto err_unlock_ftrace;
4856 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4857 goto err_unlock_ftrace;
4859 acquire_probe_locked(probe);
4861 mutex_unlock(&ftrace_lock);
4863 mutex_lock(&probe->ops.func_hash->regex_lock);
4865 orig_hash = &probe->ops.func_hash->filter_hash;
4866 old_hash = *orig_hash;
4868 if (ftrace_hash_empty(old_hash))
4871 old_hash_ops.filter_hash = old_hash;
4872 /* Probes only have filters */
4873 old_hash_ops.notrace_hash = NULL;
4876 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4880 INIT_HLIST_HEAD(&hhd);
4882 size = 1 << hash->size_bits;
4883 for (i = 0; i < size; i++) {
4884 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4886 if (func_g.search) {
4887 kallsyms_lookup(entry->ip, NULL, NULL,
4889 if (!ftrace_match(str, &func_g))
4893 remove_hash_entry(hash, entry);
4894 hlist_add_head(&entry->hlist, &hhd);
4898 /* Nothing found? */
4904 mutex_lock(&ftrace_lock);
4906 WARN_ON(probe->ref < count);
4908 probe->ref -= count;
4910 if (ftrace_hash_empty(hash))
4911 ftrace_shutdown(&probe->ops, 0);
4913 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4916 /* still need to update the function call sites */
4917 if (ftrace_enabled && !ftrace_hash_empty(hash))
4918 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4922 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4923 hlist_del(&entry->hlist);
4924 if (probe_ops->free)
4925 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4928 mutex_unlock(&ftrace_lock);
4931 mutex_unlock(&probe->ops.func_hash->regex_lock);
4932 free_ftrace_hash(hash);
4934 release_probe(probe);
4939 mutex_unlock(&ftrace_lock);
4943 void clear_ftrace_function_probes(struct trace_array *tr)
4945 struct ftrace_func_probe *probe, *n;
4947 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4948 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4951 static LIST_HEAD(ftrace_commands);
4952 static DEFINE_MUTEX(ftrace_cmd_mutex);
4955 * Currently we only register ftrace commands from __init, so mark this
4958 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4960 struct ftrace_func_command *p;
4963 mutex_lock(&ftrace_cmd_mutex);
4964 list_for_each_entry(p, &ftrace_commands, list) {
4965 if (strcmp(cmd->name, p->name) == 0) {
4970 list_add(&cmd->list, &ftrace_commands);
4972 mutex_unlock(&ftrace_cmd_mutex);
4978 * Currently we only unregister ftrace commands from __init, so mark
4981 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4983 struct ftrace_func_command *p, *n;
4986 mutex_lock(&ftrace_cmd_mutex);
4987 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4988 if (strcmp(cmd->name, p->name) == 0) {
4990 list_del_init(&p->list);
4995 mutex_unlock(&ftrace_cmd_mutex);
5000 static int ftrace_process_regex(struct ftrace_iterator *iter,
5001 char *buff, int len, int enable)
5003 struct ftrace_hash *hash = iter->hash;
5004 struct trace_array *tr = iter->ops->private;
5005 char *func, *command, *next = buff;
5006 struct ftrace_func_command *p;
5009 func = strsep(&next, ":");
5012 ret = ftrace_match_records(hash, func, len);
5022 command = strsep(&next, ":");
5024 mutex_lock(&ftrace_cmd_mutex);
5025 list_for_each_entry(p, &ftrace_commands, list) {
5026 if (strcmp(p->name, command) == 0) {
5027 ret = p->func(tr, hash, func, command, next, enable);
5032 mutex_unlock(&ftrace_cmd_mutex);
5038 ftrace_regex_write(struct file *file, const char __user *ubuf,
5039 size_t cnt, loff_t *ppos, int enable)
5041 struct ftrace_iterator *iter;
5042 struct trace_parser *parser;
5048 if (file->f_mode & FMODE_READ) {
5049 struct seq_file *m = file->private_data;
5052 iter = file->private_data;
5054 if (unlikely(ftrace_disabled))
5057 /* iter->hash is a local copy, so we don't need regex_lock */
5059 parser = &iter->parser;
5060 read = trace_get_user(parser, ubuf, cnt, ppos);
5062 if (read >= 0 && trace_parser_loaded(parser) &&
5063 !trace_parser_cont(parser)) {
5064 ret = ftrace_process_regex(iter, parser->buffer,
5065 parser->idx, enable);
5066 trace_parser_clear(parser);
5077 ftrace_filter_write(struct file *file, const char __user *ubuf,
5078 size_t cnt, loff_t *ppos)
5080 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
5084 ftrace_notrace_write(struct file *file, const char __user *ubuf,
5085 size_t cnt, loff_t *ppos)
5087 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
5091 __ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
5093 struct ftrace_func_entry *entry;
5095 ip = ftrace_location(ip);
5100 entry = ftrace_lookup_ip(hash, ip);
5103 free_hash_entry(hash, entry);
5107 return add_hash_entry(hash, ip);
5111 ftrace_match_addr(struct ftrace_hash *hash, unsigned long *ips,
5112 unsigned int cnt, int remove)
5117 for (i = 0; i < cnt; i++) {
5118 err = __ftrace_match_addr(hash, ips[i], remove);
5121 * This expects the @hash is a temporary hash and if this
5122 * fails the caller must free the @hash.
5131 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
5132 unsigned long *ips, unsigned int cnt,
5133 int remove, int reset, int enable)
5135 struct ftrace_hash **orig_hash;
5136 struct ftrace_hash *hash;
5139 if (unlikely(ftrace_disabled))
5142 mutex_lock(&ops->func_hash->regex_lock);
5145 orig_hash = &ops->func_hash->filter_hash;
5147 orig_hash = &ops->func_hash->notrace_hash;
5150 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5152 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
5156 goto out_regex_unlock;
5159 if (buf && !ftrace_match_records(hash, buf, len)) {
5161 goto out_regex_unlock;
5164 ret = ftrace_match_addr(hash, ips, cnt, remove);
5166 goto out_regex_unlock;
5169 mutex_lock(&ftrace_lock);
5170 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
5171 mutex_unlock(&ftrace_lock);
5174 mutex_unlock(&ops->func_hash->regex_lock);
5176 free_ftrace_hash(hash);
5181 ftrace_set_addr(struct ftrace_ops *ops, unsigned long *ips, unsigned int cnt,
5182 int remove, int reset, int enable)
5184 return ftrace_set_hash(ops, NULL, 0, ips, cnt, remove, reset, enable);
5187 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
5189 struct ftrace_direct_func {
5190 struct list_head next;
5195 static LIST_HEAD(ftrace_direct_funcs);
5198 * ftrace_find_direct_func - test an address if it is a registered direct caller
5199 * @addr: The address of a registered direct caller
5201 * This searches to see if a ftrace direct caller has been registered
5202 * at a specific address, and if so, it returns a descriptor for it.
5204 * This can be used by architecture code to see if an address is
5205 * a direct caller (trampoline) attached to a fentry/mcount location.
5206 * This is useful for the function_graph tracer, as it may need to
5207 * do adjustments if it traced a location that also has a direct
5208 * trampoline attached to it.
5210 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
5212 struct ftrace_direct_func *entry;
5215 /* May be called by fgraph trampoline (protected by rcu tasks) */
5216 list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
5217 if (entry->addr == addr) {
5228 static struct ftrace_direct_func *ftrace_alloc_direct_func(unsigned long addr)
5230 struct ftrace_direct_func *direct;
5232 direct = kmalloc(sizeof(*direct), GFP_KERNEL);
5235 direct->addr = addr;
5237 list_add_rcu(&direct->next, &ftrace_direct_funcs);
5238 ftrace_direct_func_count++;
5242 static int register_ftrace_function_nolock(struct ftrace_ops *ops);
5245 * register_ftrace_direct - Call a custom trampoline directly
5246 * @ip: The address of the nop at the beginning of a function
5247 * @addr: The address of the trampoline to call at @ip
5249 * This is used to connect a direct call from the nop location (@ip)
5250 * at the start of ftrace traced functions. The location that it calls
5251 * (@addr) must be able to handle a direct call, and save the parameters
5252 * of the function being traced, and restore them (or inject new ones
5253 * if needed), before returning.
5257 * -EBUSY - Another direct function is already attached (there can be only one)
5258 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5259 * -ENOMEM - There was an allocation failure.
5261 int register_ftrace_direct(unsigned long ip, unsigned long addr)
5263 struct ftrace_direct_func *direct;
5264 struct ftrace_func_entry *entry;
5265 struct ftrace_hash *free_hash = NULL;
5266 struct dyn_ftrace *rec;
5269 mutex_lock(&direct_mutex);
5271 ip = ftrace_location(ip);
5275 /* See if there's a direct function at @ip already */
5277 if (ftrace_find_rec_direct(ip))
5281 rec = lookup_rec(ip, ip);
5286 * Check if the rec says it has a direct call but we didn't
5289 if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
5292 /* Make sure the ip points to the exact record */
5293 if (ip != rec->ip) {
5295 /* Need to check this ip for a direct. */
5296 if (ftrace_find_rec_direct(ip))
5301 direct = ftrace_find_direct_func(addr);
5303 direct = ftrace_alloc_direct_func(addr);
5308 entry = ftrace_add_rec_direct(ip, addr, &free_hash);
5312 ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
5314 if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
5315 ret = register_ftrace_function_nolock(&direct_ops);
5317 ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5321 remove_hash_entry(direct_functions, entry);
5323 if (!direct->count) {
5324 list_del_rcu(&direct->next);
5325 synchronize_rcu_tasks();
5328 free_ftrace_hash(free_hash);
5330 ftrace_direct_func_count--;
5336 mutex_unlock(&direct_mutex);
5339 synchronize_rcu_tasks();
5340 free_ftrace_hash(free_hash);
5345 EXPORT_SYMBOL_GPL(register_ftrace_direct);
5347 static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
5348 struct dyn_ftrace **recp)
5350 struct ftrace_func_entry *entry;
5351 struct dyn_ftrace *rec;
5353 rec = lookup_rec(*ip, *ip);
5357 entry = __ftrace_lookup_ip(direct_functions, rec->ip);
5359 WARN_ON(rec->flags & FTRACE_FL_DIRECT);
5363 WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
5365 /* Passed in ip just needs to be on the call site */
5374 int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
5376 struct ftrace_direct_func *direct;
5377 struct ftrace_func_entry *entry;
5378 struct ftrace_hash *hash;
5381 mutex_lock(&direct_mutex);
5383 ip = ftrace_location(ip);
5387 entry = find_direct_entry(&ip, NULL);
5391 hash = direct_ops.func_hash->filter_hash;
5392 if (hash->count == 1)
5393 unregister_ftrace_function(&direct_ops);
5395 ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5399 remove_hash_entry(direct_functions, entry);
5401 direct = ftrace_find_direct_func(addr);
5402 if (!WARN_ON(!direct)) {
5403 /* This is the good path (see the ! before WARN) */
5405 WARN_ON(direct->count < 0);
5406 if (!direct->count) {
5407 list_del_rcu(&direct->next);
5408 synchronize_rcu_tasks();
5411 ftrace_direct_func_count--;
5415 mutex_unlock(&direct_mutex);
5419 EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
5421 static struct ftrace_ops stub_ops = {
5422 .func = ftrace_stub,
5426 * ftrace_modify_direct_caller - modify ftrace nop directly
5427 * @entry: The ftrace hash entry of the direct helper for @rec
5428 * @rec: The record representing the function site to patch
5429 * @old_addr: The location that the site at @rec->ip currently calls
5430 * @new_addr: The location that the site at @rec->ip should call
5432 * An architecture may overwrite this function to optimize the
5433 * changing of the direct callback on an ftrace nop location.
5434 * This is called with the ftrace_lock mutex held, and no other
5435 * ftrace callbacks are on the associated record (@rec). Thus,
5436 * it is safe to modify the ftrace record, where it should be
5437 * currently calling @old_addr directly, to call @new_addr.
5439 * This is called with direct_mutex locked.
5441 * Safety checks should be made to make sure that the code at
5442 * @rec->ip is currently calling @old_addr. And this must
5443 * also update entry->direct to @new_addr.
5445 int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
5446 struct dyn_ftrace *rec,
5447 unsigned long old_addr,
5448 unsigned long new_addr)
5450 unsigned long ip = rec->ip;
5453 lockdep_assert_held(&direct_mutex);
5456 * The ftrace_lock was used to determine if the record
5457 * had more than one registered user to it. If it did,
5458 * we needed to prevent that from changing to do the quick
5459 * switch. But if it did not (only a direct caller was attached)
5460 * then this function is called. But this function can deal
5461 * with attached callers to the rec that we care about, and
5462 * since this function uses standard ftrace calls that take
5463 * the ftrace_lock mutex, we need to release it.
5465 mutex_unlock(&ftrace_lock);
5468 * By setting a stub function at the same address, we force
5469 * the code to call the iterator and the direct_ops helper.
5470 * This means that @ip does not call the direct call, and
5471 * we can simply modify it.
5473 ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
5477 ret = register_ftrace_function_nolock(&stub_ops);
5479 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5483 entry->direct = new_addr;
5486 * By removing the stub, we put back the direct call, calling
5489 unregister_ftrace_function(&stub_ops);
5490 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5493 mutex_lock(&ftrace_lock);
5499 * modify_ftrace_direct - Modify an existing direct call to call something else
5500 * @ip: The instruction pointer to modify
5501 * @old_addr: The address that the current @ip calls directly
5502 * @new_addr: The address that the @ip should call
5504 * This modifies a ftrace direct caller at an instruction pointer without
5505 * having to disable it first. The direct call will switch over to the
5506 * @new_addr without missing anything.
5508 * Returns: zero on success. Non zero on error, which includes:
5509 * -ENODEV : the @ip given has no direct caller attached
5510 * -EINVAL : the @old_addr does not match the current direct caller
5512 int modify_ftrace_direct(unsigned long ip,
5513 unsigned long old_addr, unsigned long new_addr)
5515 struct ftrace_direct_func *direct, *new_direct = NULL;
5516 struct ftrace_func_entry *entry;
5517 struct dyn_ftrace *rec;
5520 mutex_lock(&direct_mutex);
5522 mutex_lock(&ftrace_lock);
5524 ip = ftrace_location(ip);
5528 entry = find_direct_entry(&ip, &rec);
5533 if (entry->direct != old_addr)
5536 direct = ftrace_find_direct_func(old_addr);
5537 if (WARN_ON(!direct))
5539 if (direct->count > 1) {
5541 new_direct = ftrace_alloc_direct_func(new_addr);
5545 new_direct->count++;
5547 direct->addr = new_addr;
5551 * If there's no other ftrace callback on the rec->ip location,
5552 * then it can be changed directly by the architecture.
5553 * If there is another caller, then we just need to change the
5554 * direct caller helper to point to @new_addr.
5556 if (ftrace_rec_count(rec) == 1) {
5557 ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
5559 entry->direct = new_addr;
5563 if (unlikely(ret && new_direct)) {
5565 list_del_rcu(&new_direct->next);
5566 synchronize_rcu_tasks();
5568 ftrace_direct_func_count--;
5572 mutex_unlock(&ftrace_lock);
5573 mutex_unlock(&direct_mutex);
5576 EXPORT_SYMBOL_GPL(modify_ftrace_direct);
5578 #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS)
5580 static int check_direct_multi(struct ftrace_ops *ops)
5582 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5584 if ((ops->flags & MULTI_FLAGS) != MULTI_FLAGS)
5589 static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long addr)
5591 struct ftrace_func_entry *entry, *del;
5594 size = 1 << hash->size_bits;
5595 for (i = 0; i < size; i++) {
5596 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5597 del = __ftrace_lookup_ip(direct_functions, entry->ip);
5598 if (del && del->direct == addr) {
5599 remove_hash_entry(direct_functions, del);
5607 * register_ftrace_direct_multi - Call a custom trampoline directly
5608 * for multiple functions registered in @ops
5609 * @ops: The address of the struct ftrace_ops object
5610 * @addr: The address of the trampoline to call at @ops functions
5612 * This is used to connect a direct calls to @addr from the nop locations
5613 * of the functions registered in @ops (with by ftrace_set_filter_ip
5616 * The location that it calls (@addr) must be able to handle a direct call,
5617 * and save the parameters of the function being traced, and restore them
5618 * (or inject new ones if needed), before returning.
5622 * -EINVAL - The @ops object was already registered with this call or
5623 * when there are no functions in @ops object.
5624 * -EBUSY - Another direct function is already attached (there can be only one)
5625 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5626 * -ENOMEM - There was an allocation failure.
5628 int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5630 struct ftrace_hash *hash, *free_hash = NULL;
5631 struct ftrace_func_entry *entry, *new;
5632 int err = -EBUSY, size, i;
5634 if (ops->func || ops->trampoline)
5636 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5638 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5641 hash = ops->func_hash->filter_hash;
5642 if (ftrace_hash_empty(hash))
5645 mutex_lock(&direct_mutex);
5647 /* Make sure requested entries are not already registered.. */
5648 size = 1 << hash->size_bits;
5649 for (i = 0; i < size; i++) {
5650 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5651 if (ftrace_find_rec_direct(entry->ip))
5656 /* ... and insert them to direct_functions hash. */
5658 for (i = 0; i < size; i++) {
5659 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5660 new = ftrace_add_rec_direct(entry->ip, addr, &free_hash);
5663 entry->direct = addr;
5667 ops->func = call_direct_funcs;
5668 ops->flags = MULTI_FLAGS;
5669 ops->trampoline = FTRACE_REGS_ADDR;
5671 err = register_ftrace_function_nolock(ops);
5675 remove_direct_functions_hash(hash, addr);
5678 mutex_unlock(&direct_mutex);
5681 synchronize_rcu_tasks();
5682 free_ftrace_hash(free_hash);
5686 EXPORT_SYMBOL_GPL(register_ftrace_direct_multi);
5689 * unregister_ftrace_direct_multi - Remove calls to custom trampoline
5690 * previously registered by register_ftrace_direct_multi for @ops object.
5691 * @ops: The address of the struct ftrace_ops object
5693 * This is used to remove a direct calls to @addr from the nop locations
5694 * of the functions registered in @ops (with by ftrace_set_filter_ip
5699 * -EINVAL - The @ops object was not properly registered.
5701 int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5703 struct ftrace_hash *hash = ops->func_hash->filter_hash;
5706 if (check_direct_multi(ops))
5708 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5711 mutex_lock(&direct_mutex);
5712 err = unregister_ftrace_function(ops);
5713 remove_direct_functions_hash(hash, addr);
5714 mutex_unlock(&direct_mutex);
5716 /* cleanup for possible another register call */
5718 ops->trampoline = 0;
5721 EXPORT_SYMBOL_GPL(unregister_ftrace_direct_multi);
5724 __modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5726 struct ftrace_hash *hash;
5727 struct ftrace_func_entry *entry, *iter;
5728 static struct ftrace_ops tmp_ops = {
5729 .func = ftrace_stub,
5730 .flags = FTRACE_OPS_FL_STUB,
5735 lockdep_assert_held_once(&direct_mutex);
5737 /* Enable the tmp_ops to have the same functions as the direct ops */
5738 ftrace_ops_init(&tmp_ops);
5739 tmp_ops.func_hash = ops->func_hash;
5741 err = register_ftrace_function_nolock(&tmp_ops);
5746 * Now the ftrace_ops_list_func() is called to do the direct callers.
5747 * We can safely change the direct functions attached to each entry.
5749 mutex_lock(&ftrace_lock);
5751 hash = ops->func_hash->filter_hash;
5752 size = 1 << hash->size_bits;
5753 for (i = 0; i < size; i++) {
5754 hlist_for_each_entry(iter, &hash->buckets[i], hlist) {
5755 entry = __ftrace_lookup_ip(direct_functions, iter->ip);
5758 entry->direct = addr;
5762 mutex_unlock(&ftrace_lock);
5764 /* Removing the tmp_ops will add the updated direct callers to the functions */
5765 unregister_ftrace_function(&tmp_ops);
5771 * modify_ftrace_direct_multi_nolock - Modify an existing direct 'multi' call
5772 * to call something else
5773 * @ops: The address of the struct ftrace_ops object
5774 * @addr: The address of the new trampoline to call at @ops functions
5776 * This is used to unregister currently registered direct caller and
5777 * register new one @addr on functions registered in @ops object.
5779 * Note there's window between ftrace_shutdown and ftrace_startup calls
5780 * where there will be no callbacks called.
5782 * Caller should already have direct_mutex locked, so we don't lock
5783 * direct_mutex here.
5785 * Returns: zero on success. Non zero on error, which includes:
5786 * -EINVAL - The @ops object was not properly registered.
5788 int modify_ftrace_direct_multi_nolock(struct ftrace_ops *ops, unsigned long addr)
5790 if (check_direct_multi(ops))
5792 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5795 return __modify_ftrace_direct_multi(ops, addr);
5797 EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi_nolock);
5800 * modify_ftrace_direct_multi - Modify an existing direct 'multi' call
5801 * to call something else
5802 * @ops: The address of the struct ftrace_ops object
5803 * @addr: The address of the new trampoline to call at @ops functions
5805 * This is used to unregister currently registered direct caller and
5806 * register new one @addr on functions registered in @ops object.
5808 * Note there's window between ftrace_shutdown and ftrace_startup calls
5809 * where there will be no callbacks called.
5811 * Returns: zero on success. Non zero on error, which includes:
5812 * -EINVAL - The @ops object was not properly registered.
5814 int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5818 if (check_direct_multi(ops))
5820 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5823 mutex_lock(&direct_mutex);
5824 err = __modify_ftrace_direct_multi(ops, addr);
5825 mutex_unlock(&direct_mutex);
5828 EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi);
5829 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
5832 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
5833 * @ops - the ops to set the filter with
5834 * @ip - the address to add to or remove from the filter.
5835 * @remove - non zero to remove the ip from the filter
5836 * @reset - non zero to reset all filters before applying this filter.
5838 * Filters denote which functions should be enabled when tracing is enabled
5839 * If @ip is NULL, it fails to update filter.
5841 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
5842 int remove, int reset)
5844 ftrace_ops_init(ops);
5845 return ftrace_set_addr(ops, &ip, 1, remove, reset, 1);
5847 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
5850 * ftrace_set_filter_ips - set functions to filter on in ftrace by addresses
5851 * @ops - the ops to set the filter with
5852 * @ips - the array of addresses to add to or remove from the filter.
5853 * @cnt - the number of addresses in @ips
5854 * @remove - non zero to remove ips from the filter
5855 * @reset - non zero to reset all filters before applying this filter.
5857 * Filters denote which functions should be enabled when tracing is enabled
5858 * If @ips array or any ip specified within is NULL , it fails to update filter.
5860 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
5861 unsigned int cnt, int remove, int reset)
5863 ftrace_ops_init(ops);
5864 return ftrace_set_addr(ops, ips, cnt, remove, reset, 1);
5866 EXPORT_SYMBOL_GPL(ftrace_set_filter_ips);
5869 * ftrace_ops_set_global_filter - setup ops to use global filters
5870 * @ops - the ops which will use the global filters
5872 * ftrace users who need global function trace filtering should call this.
5873 * It can set the global filter only if ops were not initialized before.
5875 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
5877 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
5880 ftrace_ops_init(ops);
5881 ops->func_hash = &global_ops.local_hash;
5883 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
5886 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
5887 int reset, int enable)
5889 return ftrace_set_hash(ops, buf, len, NULL, 0, 0, reset, enable);
5893 * ftrace_set_filter - set a function to filter on in ftrace
5894 * @ops - the ops to set the filter with
5895 * @buf - the string that holds the function filter text.
5896 * @len - the length of the string.
5897 * @reset - non zero to reset all filters before applying this filter.
5899 * Filters denote which functions should be enabled when tracing is enabled.
5900 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5902 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
5905 ftrace_ops_init(ops);
5906 return ftrace_set_regex(ops, buf, len, reset, 1);
5908 EXPORT_SYMBOL_GPL(ftrace_set_filter);
5911 * ftrace_set_notrace - set a function to not trace in ftrace
5912 * @ops - the ops to set the notrace filter with
5913 * @buf - the string that holds the function notrace text.
5914 * @len - the length of the string.
5915 * @reset - non zero to reset all filters before applying this filter.
5917 * Notrace Filters denote which functions should not be enabled when tracing
5918 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5921 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
5924 ftrace_ops_init(ops);
5925 return ftrace_set_regex(ops, buf, len, reset, 0);
5927 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
5929 * ftrace_set_global_filter - set a function to filter on with global tracers
5930 * @buf - the string that holds the function filter text.
5931 * @len - the length of the string.
5932 * @reset - non zero to reset all filters before applying this filter.
5934 * Filters denote which functions should be enabled when tracing is enabled.
5935 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5937 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
5939 ftrace_set_regex(&global_ops, buf, len, reset, 1);
5941 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
5944 * ftrace_set_global_notrace - set a function to not trace with global tracers
5945 * @buf - the string that holds the function notrace text.
5946 * @len - the length of the string.
5947 * @reset - non zero to reset all filters before applying this filter.
5949 * Notrace Filters denote which functions should not be enabled when tracing
5950 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5953 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
5955 ftrace_set_regex(&global_ops, buf, len, reset, 0);
5957 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
5960 * command line interface to allow users to set filters on boot up.
5962 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
5963 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5964 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
5966 /* Used by function selftest to not test if filter is set */
5967 bool ftrace_filter_param __initdata;
5969 static int __init set_ftrace_notrace(char *str)
5971 ftrace_filter_param = true;
5972 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
5975 __setup("ftrace_notrace=", set_ftrace_notrace);
5977 static int __init set_ftrace_filter(char *str)
5979 ftrace_filter_param = true;
5980 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
5983 __setup("ftrace_filter=", set_ftrace_filter);
5985 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5986 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
5987 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5988 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
5990 static int __init set_graph_function(char *str)
5992 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
5995 __setup("ftrace_graph_filter=", set_graph_function);
5997 static int __init set_graph_notrace_function(char *str)
5999 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
6002 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
6004 static int __init set_graph_max_depth_function(char *str)
6008 fgraph_max_depth = simple_strtoul(str, NULL, 0);
6011 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
6013 static void __init set_ftrace_early_graph(char *buf, int enable)
6017 struct ftrace_hash *hash;
6019 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
6020 if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
6024 func = strsep(&buf, ",");
6025 /* we allow only one expression at a time */
6026 ret = ftrace_graph_set_hash(hash, func);
6028 printk(KERN_DEBUG "ftrace: function %s not "
6029 "traceable\n", func);
6033 ftrace_graph_hash = hash;
6035 ftrace_graph_notrace_hash = hash;
6037 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6040 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
6044 ftrace_ops_init(ops);
6047 func = strsep(&buf, ",");
6048 ftrace_set_regex(ops, func, strlen(func), 0, enable);
6052 static void __init set_ftrace_early_filters(void)
6054 if (ftrace_filter_buf[0])
6055 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
6056 if (ftrace_notrace_buf[0])
6057 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
6058 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6059 if (ftrace_graph_buf[0])
6060 set_ftrace_early_graph(ftrace_graph_buf, 1);
6061 if (ftrace_graph_notrace_buf[0])
6062 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
6063 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6066 int ftrace_regex_release(struct inode *inode, struct file *file)
6068 struct seq_file *m = (struct seq_file *)file->private_data;
6069 struct ftrace_iterator *iter;
6070 struct ftrace_hash **orig_hash;
6071 struct trace_parser *parser;
6074 if (file->f_mode & FMODE_READ) {
6076 seq_release(inode, file);
6078 iter = file->private_data;
6080 parser = &iter->parser;
6081 if (trace_parser_loaded(parser)) {
6082 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
6084 ftrace_process_regex(iter, parser->buffer,
6085 parser->idx, enable);
6088 trace_parser_put(parser);
6090 mutex_lock(&iter->ops->func_hash->regex_lock);
6092 if (file->f_mode & FMODE_WRITE) {
6093 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
6096 orig_hash = &iter->ops->func_hash->filter_hash;
6098 if (list_empty(&iter->tr->mod_trace))
6099 iter->hash->flags &= ~FTRACE_HASH_FL_MOD;
6101 iter->hash->flags |= FTRACE_HASH_FL_MOD;
6104 orig_hash = &iter->ops->func_hash->notrace_hash;
6106 mutex_lock(&ftrace_lock);
6107 ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
6108 iter->hash, filter_hash);
6109 mutex_unlock(&ftrace_lock);
6111 /* For read only, the hash is the ops hash */
6115 mutex_unlock(&iter->ops->func_hash->regex_lock);
6116 free_ftrace_hash(iter->hash);
6118 trace_array_put(iter->tr);
6124 static const struct file_operations ftrace_avail_fops = {
6125 .open = ftrace_avail_open,
6127 .llseek = seq_lseek,
6128 .release = seq_release_private,
6131 static const struct file_operations ftrace_enabled_fops = {
6132 .open = ftrace_enabled_open,
6134 .llseek = seq_lseek,
6135 .release = seq_release_private,
6138 static const struct file_operations ftrace_filter_fops = {
6139 .open = ftrace_filter_open,
6141 .write = ftrace_filter_write,
6142 .llseek = tracing_lseek,
6143 .release = ftrace_regex_release,
6146 static const struct file_operations ftrace_notrace_fops = {
6147 .open = ftrace_notrace_open,
6149 .write = ftrace_notrace_write,
6150 .llseek = tracing_lseek,
6151 .release = ftrace_regex_release,
6154 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6156 static DEFINE_MUTEX(graph_lock);
6158 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
6159 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
6161 enum graph_filter_type {
6162 GRAPH_FILTER_NOTRACE = 0,
6163 GRAPH_FILTER_FUNCTION,
6166 #define FTRACE_GRAPH_EMPTY ((void *)1)
6168 struct ftrace_graph_data {
6169 struct ftrace_hash *hash;
6170 struct ftrace_func_entry *entry;
6171 int idx; /* for hash table iteration */
6172 enum graph_filter_type type;
6173 struct ftrace_hash *new_hash;
6174 const struct seq_operations *seq_ops;
6175 struct trace_parser parser;
6179 __g_next(struct seq_file *m, loff_t *pos)
6181 struct ftrace_graph_data *fgd = m->private;
6182 struct ftrace_func_entry *entry = fgd->entry;
6183 struct hlist_head *head;
6184 int i, idx = fgd->idx;
6186 if (*pos >= fgd->hash->count)
6190 hlist_for_each_entry_continue(entry, hlist) {
6198 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
6199 head = &fgd->hash->buckets[i];
6200 hlist_for_each_entry(entry, head, hlist) {
6210 g_next(struct seq_file *m, void *v, loff_t *pos)
6213 return __g_next(m, pos);
6216 static void *g_start(struct seq_file *m, loff_t *pos)
6218 struct ftrace_graph_data *fgd = m->private;
6220 mutex_lock(&graph_lock);
6222 if (fgd->type == GRAPH_FILTER_FUNCTION)
6223 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
6224 lockdep_is_held(&graph_lock));
6226 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6227 lockdep_is_held(&graph_lock));
6229 /* Nothing, tell g_show to print all functions are enabled */
6230 if (ftrace_hash_empty(fgd->hash) && !*pos)
6231 return FTRACE_GRAPH_EMPTY;
6235 return __g_next(m, pos);
6238 static void g_stop(struct seq_file *m, void *p)
6240 mutex_unlock(&graph_lock);
6243 static int g_show(struct seq_file *m, void *v)
6245 struct ftrace_func_entry *entry = v;
6250 if (entry == FTRACE_GRAPH_EMPTY) {
6251 struct ftrace_graph_data *fgd = m->private;
6253 if (fgd->type == GRAPH_FILTER_FUNCTION)
6254 seq_puts(m, "#### all functions enabled ####\n");
6256 seq_puts(m, "#### no functions disabled ####\n");
6260 seq_printf(m, "%ps\n", (void *)entry->ip);
6265 static const struct seq_operations ftrace_graph_seq_ops = {
6273 __ftrace_graph_open(struct inode *inode, struct file *file,
6274 struct ftrace_graph_data *fgd)
6277 struct ftrace_hash *new_hash = NULL;
6279 ret = security_locked_down(LOCKDOWN_TRACEFS);
6283 if (file->f_mode & FMODE_WRITE) {
6284 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
6286 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
6289 if (file->f_flags & O_TRUNC)
6290 new_hash = alloc_ftrace_hash(size_bits);
6292 new_hash = alloc_and_copy_ftrace_hash(size_bits,
6300 if (file->f_mode & FMODE_READ) {
6301 ret = seq_open(file, &ftrace_graph_seq_ops);
6303 struct seq_file *m = file->private_data;
6307 free_ftrace_hash(new_hash);
6311 file->private_data = fgd;
6314 if (ret < 0 && file->f_mode & FMODE_WRITE)
6315 trace_parser_put(&fgd->parser);
6317 fgd->new_hash = new_hash;
6320 * All uses of fgd->hash must be taken with the graph_lock
6321 * held. The graph_lock is going to be released, so force
6322 * fgd->hash to be reinitialized when it is taken again.
6330 ftrace_graph_open(struct inode *inode, struct file *file)
6332 struct ftrace_graph_data *fgd;
6335 if (unlikely(ftrace_disabled))
6338 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6342 mutex_lock(&graph_lock);
6344 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
6345 lockdep_is_held(&graph_lock));
6346 fgd->type = GRAPH_FILTER_FUNCTION;
6347 fgd->seq_ops = &ftrace_graph_seq_ops;
6349 ret = __ftrace_graph_open(inode, file, fgd);
6353 mutex_unlock(&graph_lock);
6358 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
6360 struct ftrace_graph_data *fgd;
6363 if (unlikely(ftrace_disabled))
6366 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6370 mutex_lock(&graph_lock);
6372 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6373 lockdep_is_held(&graph_lock));
6374 fgd->type = GRAPH_FILTER_NOTRACE;
6375 fgd->seq_ops = &ftrace_graph_seq_ops;
6377 ret = __ftrace_graph_open(inode, file, fgd);
6381 mutex_unlock(&graph_lock);
6386 ftrace_graph_release(struct inode *inode, struct file *file)
6388 struct ftrace_graph_data *fgd;
6389 struct ftrace_hash *old_hash, *new_hash;
6390 struct trace_parser *parser;
6393 if (file->f_mode & FMODE_READ) {
6394 struct seq_file *m = file->private_data;
6397 seq_release(inode, file);
6399 fgd = file->private_data;
6403 if (file->f_mode & FMODE_WRITE) {
6405 parser = &fgd->parser;
6407 if (trace_parser_loaded((parser))) {
6408 ret = ftrace_graph_set_hash(fgd->new_hash,
6412 trace_parser_put(parser);
6414 new_hash = __ftrace_hash_move(fgd->new_hash);
6420 mutex_lock(&graph_lock);
6422 if (fgd->type == GRAPH_FILTER_FUNCTION) {
6423 old_hash = rcu_dereference_protected(ftrace_graph_hash,
6424 lockdep_is_held(&graph_lock));
6425 rcu_assign_pointer(ftrace_graph_hash, new_hash);
6427 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6428 lockdep_is_held(&graph_lock));
6429 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
6432 mutex_unlock(&graph_lock);
6435 * We need to do a hard force of sched synchronization.
6436 * This is because we use preempt_disable() to do RCU, but
6437 * the function tracers can be called where RCU is not watching
6438 * (like before user_exit()). We can not rely on the RCU
6439 * infrastructure to do the synchronization, thus we must do it
6442 if (old_hash != EMPTY_HASH)
6443 synchronize_rcu_tasks_rude();
6445 free_ftrace_hash(old_hash);
6449 free_ftrace_hash(fgd->new_hash);
6456 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
6458 struct ftrace_glob func_g;
6459 struct dyn_ftrace *rec;
6460 struct ftrace_page *pg;
6461 struct ftrace_func_entry *entry;
6466 func_g.type = filter_parse_regex(buffer, strlen(buffer),
6467 &func_g.search, ¬);
6469 func_g.len = strlen(func_g.search);
6471 mutex_lock(&ftrace_lock);
6473 if (unlikely(ftrace_disabled)) {
6474 mutex_unlock(&ftrace_lock);
6478 do_for_each_ftrace_rec(pg, rec) {
6480 if (rec->flags & FTRACE_FL_DISABLED)
6483 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
6484 entry = ftrace_lookup_ip(hash, rec->ip);
6491 if (add_hash_entry(hash, rec->ip) < 0)
6495 free_hash_entry(hash, entry);
6500 } while_for_each_ftrace_rec();
6502 mutex_unlock(&ftrace_lock);
6511 ftrace_graph_write(struct file *file, const char __user *ubuf,
6512 size_t cnt, loff_t *ppos)
6514 ssize_t read, ret = 0;
6515 struct ftrace_graph_data *fgd = file->private_data;
6516 struct trace_parser *parser;
6521 /* Read mode uses seq functions */
6522 if (file->f_mode & FMODE_READ) {
6523 struct seq_file *m = file->private_data;
6527 parser = &fgd->parser;
6529 read = trace_get_user(parser, ubuf, cnt, ppos);
6531 if (read >= 0 && trace_parser_loaded(parser) &&
6532 !trace_parser_cont(parser)) {
6534 ret = ftrace_graph_set_hash(fgd->new_hash,
6536 trace_parser_clear(parser);
6545 static const struct file_operations ftrace_graph_fops = {
6546 .open = ftrace_graph_open,
6548 .write = ftrace_graph_write,
6549 .llseek = tracing_lseek,
6550 .release = ftrace_graph_release,
6553 static const struct file_operations ftrace_graph_notrace_fops = {
6554 .open = ftrace_graph_notrace_open,
6556 .write = ftrace_graph_write,
6557 .llseek = tracing_lseek,
6558 .release = ftrace_graph_release,
6560 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6562 void ftrace_create_filter_files(struct ftrace_ops *ops,
6563 struct dentry *parent)
6566 trace_create_file("set_ftrace_filter", TRACE_MODE_WRITE, parent,
6567 ops, &ftrace_filter_fops);
6569 trace_create_file("set_ftrace_notrace", TRACE_MODE_WRITE, parent,
6570 ops, &ftrace_notrace_fops);
6574 * The name "destroy_filter_files" is really a misnomer. Although
6575 * in the future, it may actually delete the files, but this is
6576 * really intended to make sure the ops passed in are disabled
6577 * and that when this function returns, the caller is free to
6580 * The "destroy" name is only to match the "create" name that this
6581 * should be paired with.
6583 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
6585 mutex_lock(&ftrace_lock);
6586 if (ops->flags & FTRACE_OPS_FL_ENABLED)
6587 ftrace_shutdown(ops, 0);
6588 ops->flags |= FTRACE_OPS_FL_DELETED;
6589 ftrace_free_filter(ops);
6590 mutex_unlock(&ftrace_lock);
6593 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
6596 trace_create_file("available_filter_functions", TRACE_MODE_READ,
6597 d_tracer, NULL, &ftrace_avail_fops);
6599 trace_create_file("enabled_functions", TRACE_MODE_READ,
6600 d_tracer, NULL, &ftrace_enabled_fops);
6602 ftrace_create_filter_files(&global_ops, d_tracer);
6604 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6605 trace_create_file("set_graph_function", TRACE_MODE_WRITE, d_tracer,
6607 &ftrace_graph_fops);
6608 trace_create_file("set_graph_notrace", TRACE_MODE_WRITE, d_tracer,
6610 &ftrace_graph_notrace_fops);
6611 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6616 static int ftrace_cmp_ips(const void *a, const void *b)
6618 const unsigned long *ipa = a;
6619 const unsigned long *ipb = b;
6628 #ifdef CONFIG_FTRACE_SORT_STARTUP_TEST
6629 static void test_is_sorted(unsigned long *start, unsigned long count)
6633 for (i = 1; i < count; i++) {
6634 if (WARN(start[i - 1] > start[i],
6635 "[%d] %pS at %lx is not sorted with %pS at %lx\n", i,
6636 (void *)start[i - 1], start[i - 1],
6637 (void *)start[i], start[i]))
6641 pr_info("ftrace section at %px sorted properly\n", start);
6644 static void test_is_sorted(unsigned long *start, unsigned long count)
6649 static int ftrace_process_locs(struct module *mod,
6650 unsigned long *start,
6653 struct ftrace_page *start_pg;
6654 struct ftrace_page *pg;
6655 struct dyn_ftrace *rec;
6656 unsigned long count;
6659 unsigned long flags = 0; /* Shut up gcc */
6662 count = end - start;
6668 * Sorting mcount in vmlinux at build time depend on
6669 * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in
6670 * modules can not be sorted at build time.
6672 if (!IS_ENABLED(CONFIG_BUILDTIME_MCOUNT_SORT) || mod) {
6673 sort(start, count, sizeof(*start),
6674 ftrace_cmp_ips, NULL);
6676 test_is_sorted(start, count);
6679 start_pg = ftrace_allocate_pages(count);
6683 mutex_lock(&ftrace_lock);
6686 * Core and each module needs their own pages, as
6687 * modules will free them when they are removed.
6688 * Force a new page to be allocated for modules.
6691 WARN_ON(ftrace_pages || ftrace_pages_start);
6692 /* First initialization */
6693 ftrace_pages = ftrace_pages_start = start_pg;
6698 if (WARN_ON(ftrace_pages->next)) {
6699 /* Hmm, we have free pages? */
6700 while (ftrace_pages->next)
6701 ftrace_pages = ftrace_pages->next;
6704 ftrace_pages->next = start_pg;
6710 unsigned long end_offset;
6711 addr = ftrace_call_adjust(*p++);
6713 * Some architecture linkers will pad between
6714 * the different mcount_loc sections of different
6715 * object files to satisfy alignments.
6716 * Skip any NULL pointers.
6721 end_offset = (pg->index+1) * sizeof(pg->records[0]);
6722 if (end_offset > PAGE_SIZE << pg->order) {
6723 /* We should have allocated enough */
6724 if (WARN_ON(!pg->next))
6729 rec = &pg->records[pg->index++];
6733 /* We should have used all pages */
6736 /* Assign the last page to ftrace_pages */
6740 * We only need to disable interrupts on start up
6741 * because we are modifying code that an interrupt
6742 * may execute, and the modification is not atomic.
6743 * But for modules, nothing runs the code we modify
6744 * until we are finished with it, and there's no
6745 * reason to cause large interrupt latencies while we do it.
6748 local_irq_save(flags);
6749 ftrace_update_code(mod, start_pg);
6751 local_irq_restore(flags);
6754 mutex_unlock(&ftrace_lock);
6759 struct ftrace_mod_func {
6760 struct list_head list;
6766 struct ftrace_mod_map {
6767 struct rcu_head rcu;
6768 struct list_head list;
6770 unsigned long start_addr;
6771 unsigned long end_addr;
6772 struct list_head funcs;
6773 unsigned int num_funcs;
6776 static int ftrace_get_trampoline_kallsym(unsigned int symnum,
6777 unsigned long *value, char *type,
6778 char *name, char *module_name,
6781 struct ftrace_ops *op;
6783 list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) {
6784 if (!op->trampoline || symnum--)
6786 *value = op->trampoline;
6788 strlcpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN);
6789 strlcpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN);
6797 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) || defined(CONFIG_MODULES)
6799 * Check if the current ops references the given ip.
6801 * If the ops traces all functions, then it was already accounted for.
6802 * If the ops does not trace the current record function, skip it.
6803 * If the ops ignores the function via notrace filter, skip it.
6806 ops_references_ip(struct ftrace_ops *ops, unsigned long ip)
6808 /* If ops isn't enabled, ignore it */
6809 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
6812 /* If ops traces all then it includes this function */
6813 if (ops_traces_mod(ops))
6816 /* The function must be in the filter */
6817 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
6818 !__ftrace_lookup_ip(ops->func_hash->filter_hash, ip))
6821 /* If in notrace hash, we ignore it too */
6822 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, ip))
6829 #ifdef CONFIG_MODULES
6831 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
6833 static LIST_HEAD(ftrace_mod_maps);
6835 static int referenced_filters(struct dyn_ftrace *rec)
6837 struct ftrace_ops *ops;
6840 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
6841 if (ops_references_ip(ops, rec->ip)) {
6842 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT))
6844 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY))
6847 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
6848 rec->flags |= FTRACE_FL_REGS;
6849 if (cnt == 1 && ops->trampoline)
6850 rec->flags |= FTRACE_FL_TRAMP;
6852 rec->flags &= ~FTRACE_FL_TRAMP;
6860 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
6862 struct ftrace_func_entry *entry;
6863 struct dyn_ftrace *rec;
6866 if (ftrace_hash_empty(hash))
6869 for (i = 0; i < pg->index; i++) {
6870 rec = &pg->records[i];
6871 entry = __ftrace_lookup_ip(hash, rec->ip);
6873 * Do not allow this rec to match again.
6874 * Yeah, it may waste some memory, but will be removed
6875 * if/when the hash is modified again.
6882 /* Clear any records from hashes */
6883 static void clear_mod_from_hashes(struct ftrace_page *pg)
6885 struct trace_array *tr;
6887 mutex_lock(&trace_types_lock);
6888 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6889 if (!tr->ops || !tr->ops->func_hash)
6891 mutex_lock(&tr->ops->func_hash->regex_lock);
6892 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
6893 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
6894 mutex_unlock(&tr->ops->func_hash->regex_lock);
6896 mutex_unlock(&trace_types_lock);
6899 static void ftrace_free_mod_map(struct rcu_head *rcu)
6901 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
6902 struct ftrace_mod_func *mod_func;
6903 struct ftrace_mod_func *n;
6905 /* All the contents of mod_map are now not visible to readers */
6906 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
6907 kfree(mod_func->name);
6908 list_del(&mod_func->list);
6915 void ftrace_release_mod(struct module *mod)
6917 struct ftrace_mod_map *mod_map;
6918 struct ftrace_mod_map *n;
6919 struct dyn_ftrace *rec;
6920 struct ftrace_page **last_pg;
6921 struct ftrace_page *tmp_page = NULL;
6922 struct ftrace_page *pg;
6924 mutex_lock(&ftrace_lock);
6926 if (ftrace_disabled)
6929 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
6930 if (mod_map->mod == mod) {
6931 list_del_rcu(&mod_map->list);
6932 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
6938 * Each module has its own ftrace_pages, remove
6939 * them from the list.
6941 last_pg = &ftrace_pages_start;
6942 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
6943 rec = &pg->records[0];
6944 if (within_module_core(rec->ip, mod) ||
6945 within_module_init(rec->ip, mod)) {
6947 * As core pages are first, the first
6948 * page should never be a module page.
6950 if (WARN_ON(pg == ftrace_pages_start))
6953 /* Check if we are deleting the last page */
6954 if (pg == ftrace_pages)
6955 ftrace_pages = next_to_ftrace_page(last_pg);
6957 ftrace_update_tot_cnt -= pg->index;
6958 *last_pg = pg->next;
6960 pg->next = tmp_page;
6963 last_pg = &pg->next;
6966 mutex_unlock(&ftrace_lock);
6968 for (pg = tmp_page; pg; pg = tmp_page) {
6970 /* Needs to be called outside of ftrace_lock */
6971 clear_mod_from_hashes(pg);
6974 free_pages((unsigned long)pg->records, pg->order);
6975 ftrace_number_of_pages -= 1 << pg->order;
6977 tmp_page = pg->next;
6979 ftrace_number_of_groups--;
6983 void ftrace_module_enable(struct module *mod)
6985 struct dyn_ftrace *rec;
6986 struct ftrace_page *pg;
6988 mutex_lock(&ftrace_lock);
6990 if (ftrace_disabled)
6994 * If the tracing is enabled, go ahead and enable the record.
6996 * The reason not to enable the record immediately is the
6997 * inherent check of ftrace_make_nop/ftrace_make_call for
6998 * correct previous instructions. Making first the NOP
6999 * conversion puts the module to the correct state, thus
7000 * passing the ftrace_make_call check.
7002 * We also delay this to after the module code already set the
7003 * text to read-only, as we now need to set it back to read-write
7004 * so that we can modify the text.
7006 if (ftrace_start_up)
7007 ftrace_arch_code_modify_prepare();
7009 do_for_each_ftrace_rec(pg, rec) {
7012 * do_for_each_ftrace_rec() is a double loop.
7013 * module text shares the pg. If a record is
7014 * not part of this module, then skip this pg,
7015 * which the "break" will do.
7017 if (!within_module_core(rec->ip, mod) &&
7018 !within_module_init(rec->ip, mod))
7021 /* Weak functions should still be ignored */
7022 if (!test_for_valid_rec(rec)) {
7023 /* Clear all other flags. Should not be enabled anyway */
7024 rec->flags = FTRACE_FL_DISABLED;
7031 * When adding a module, we need to check if tracers are
7032 * currently enabled and if they are, and can trace this record,
7033 * we need to enable the module functions as well as update the
7034 * reference counts for those function records.
7036 if (ftrace_start_up)
7037 cnt += referenced_filters(rec);
7039 rec->flags &= ~FTRACE_FL_DISABLED;
7042 if (ftrace_start_up && cnt) {
7043 int failed = __ftrace_replace_code(rec, 1);
7045 ftrace_bug(failed, rec);
7050 } while_for_each_ftrace_rec();
7053 if (ftrace_start_up)
7054 ftrace_arch_code_modify_post_process();
7057 mutex_unlock(&ftrace_lock);
7059 process_cached_mods(mod->name);
7062 void ftrace_module_init(struct module *mod)
7066 if (ftrace_disabled || !mod->num_ftrace_callsites)
7069 ret = ftrace_process_locs(mod, mod->ftrace_callsites,
7070 mod->ftrace_callsites + mod->num_ftrace_callsites);
7072 pr_warn("ftrace: failed to allocate entries for module '%s' functions\n",
7076 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
7077 struct dyn_ftrace *rec)
7079 struct ftrace_mod_func *mod_func;
7080 unsigned long symsize;
7081 unsigned long offset;
7082 char str[KSYM_SYMBOL_LEN];
7086 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
7090 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
7094 mod_func->name = kstrdup(str, GFP_KERNEL);
7095 if (!mod_func->name) {
7100 mod_func->ip = rec->ip - offset;
7101 mod_func->size = symsize;
7103 mod_map->num_funcs++;
7105 list_add_rcu(&mod_func->list, &mod_map->funcs);
7108 static struct ftrace_mod_map *
7109 allocate_ftrace_mod_map(struct module *mod,
7110 unsigned long start, unsigned long end)
7112 struct ftrace_mod_map *mod_map;
7114 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
7119 mod_map->start_addr = start;
7120 mod_map->end_addr = end;
7121 mod_map->num_funcs = 0;
7123 INIT_LIST_HEAD_RCU(&mod_map->funcs);
7125 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
7131 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
7132 unsigned long addr, unsigned long *size,
7133 unsigned long *off, char *sym)
7135 struct ftrace_mod_func *found_func = NULL;
7136 struct ftrace_mod_func *mod_func;
7138 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
7139 if (addr >= mod_func->ip &&
7140 addr < mod_func->ip + mod_func->size) {
7141 found_func = mod_func;
7148 *size = found_func->size;
7150 *off = addr - found_func->ip;
7152 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
7154 return found_func->name;
7161 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
7162 unsigned long *off, char **modname, char *sym)
7164 struct ftrace_mod_map *mod_map;
7165 const char *ret = NULL;
7167 /* mod_map is freed via call_rcu() */
7169 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
7170 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
7173 *modname = mod_map->mod->name;
7182 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
7183 char *type, char *name,
7184 char *module_name, int *exported)
7186 struct ftrace_mod_map *mod_map;
7187 struct ftrace_mod_func *mod_func;
7191 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
7193 if (symnum >= mod_map->num_funcs) {
7194 symnum -= mod_map->num_funcs;
7198 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
7204 *value = mod_func->ip;
7206 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
7207 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
7215 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
7216 module_name, exported);
7222 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
7223 struct dyn_ftrace *rec) { }
7224 static inline struct ftrace_mod_map *
7225 allocate_ftrace_mod_map(struct module *mod,
7226 unsigned long start, unsigned long end)
7230 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
7231 char *type, char *name, char *module_name,
7237 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
7238 module_name, exported);
7242 #endif /* CONFIG_MODULES */
7244 struct ftrace_init_func {
7245 struct list_head list;
7249 /* Clear any init ips from hashes */
7251 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
7253 struct ftrace_func_entry *entry;
7255 entry = ftrace_lookup_ip(hash, func->ip);
7257 * Do not allow this rec to match again.
7258 * Yeah, it may waste some memory, but will be removed
7259 * if/when the hash is modified again.
7266 clear_func_from_hashes(struct ftrace_init_func *func)
7268 struct trace_array *tr;
7270 mutex_lock(&trace_types_lock);
7271 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
7272 if (!tr->ops || !tr->ops->func_hash)
7274 mutex_lock(&tr->ops->func_hash->regex_lock);
7275 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
7276 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
7277 mutex_unlock(&tr->ops->func_hash->regex_lock);
7279 mutex_unlock(&trace_types_lock);
7282 static void add_to_clear_hash_list(struct list_head *clear_list,
7283 struct dyn_ftrace *rec)
7285 struct ftrace_init_func *func;
7287 func = kmalloc(sizeof(*func), GFP_KERNEL);
7289 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
7294 list_add(&func->list, clear_list);
7297 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
7299 unsigned long start = (unsigned long)(start_ptr);
7300 unsigned long end = (unsigned long)(end_ptr);
7301 struct ftrace_page **last_pg = &ftrace_pages_start;
7302 struct ftrace_page *pg;
7303 struct dyn_ftrace *rec;
7304 struct dyn_ftrace key;
7305 struct ftrace_mod_map *mod_map = NULL;
7306 struct ftrace_init_func *func, *func_next;
7307 struct list_head clear_hash;
7309 INIT_LIST_HEAD(&clear_hash);
7312 key.flags = end; /* overload flags, as it is unsigned long */
7314 mutex_lock(&ftrace_lock);
7317 * If we are freeing module init memory, then check if
7318 * any tracer is active. If so, we need to save a mapping of
7319 * the module functions being freed with the address.
7321 if (mod && ftrace_ops_list != &ftrace_list_end)
7322 mod_map = allocate_ftrace_mod_map(mod, start, end);
7324 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
7325 if (end < pg->records[0].ip ||
7326 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
7329 rec = bsearch(&key, pg->records, pg->index,
7330 sizeof(struct dyn_ftrace),
7335 /* rec will be cleared from hashes after ftrace_lock unlock */
7336 add_to_clear_hash_list(&clear_hash, rec);
7339 save_ftrace_mod_rec(mod_map, rec);
7342 ftrace_update_tot_cnt--;
7344 *last_pg = pg->next;
7346 free_pages((unsigned long)pg->records, pg->order);
7347 ftrace_number_of_pages -= 1 << pg->order;
7349 ftrace_number_of_groups--;
7351 pg = container_of(last_pg, struct ftrace_page, next);
7356 memmove(rec, rec + 1,
7357 (pg->index - (rec - pg->records)) * sizeof(*rec));
7358 /* More than one function may be in this block */
7361 mutex_unlock(&ftrace_lock);
7363 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
7364 clear_func_from_hashes(func);
7369 void __init ftrace_free_init_mem(void)
7371 void *start = (void *)(&__init_begin);
7372 void *end = (void *)(&__init_end);
7374 ftrace_boot_snapshot();
7376 ftrace_free_mem(NULL, start, end);
7379 int __init __weak ftrace_dyn_arch_init(void)
7384 void __init ftrace_init(void)
7386 extern unsigned long __start_mcount_loc[];
7387 extern unsigned long __stop_mcount_loc[];
7388 unsigned long count, flags;
7391 local_irq_save(flags);
7392 ret = ftrace_dyn_arch_init();
7393 local_irq_restore(flags);
7397 count = __stop_mcount_loc - __start_mcount_loc;
7399 pr_info("ftrace: No functions to be traced?\n");
7403 pr_info("ftrace: allocating %ld entries in %ld pages\n",
7404 count, count / ENTRIES_PER_PAGE + 1);
7406 ret = ftrace_process_locs(NULL,
7410 pr_warn("ftrace: failed to allocate entries for functions\n");
7414 pr_info("ftrace: allocated %ld pages with %ld groups\n",
7415 ftrace_number_of_pages, ftrace_number_of_groups);
7417 last_ftrace_enabled = ftrace_enabled = 1;
7419 set_ftrace_early_filters();
7423 ftrace_disabled = 1;
7426 /* Do nothing if arch does not support this */
7427 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
7431 static void ftrace_update_trampoline(struct ftrace_ops *ops)
7433 unsigned long trampoline = ops->trampoline;
7435 arch_ftrace_update_trampoline(ops);
7436 if (ops->trampoline && ops->trampoline != trampoline &&
7437 (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) {
7438 /* Add to kallsyms before the perf events */
7439 ftrace_add_trampoline_to_kallsyms(ops);
7440 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
7441 ops->trampoline, ops->trampoline_size, false,
7442 FTRACE_TRAMPOLINE_SYM);
7444 * Record the perf text poke event after the ksymbol register
7447 perf_event_text_poke((void *)ops->trampoline, NULL, 0,
7448 (void *)ops->trampoline,
7449 ops->trampoline_size);
7453 void ftrace_init_trace_array(struct trace_array *tr)
7455 INIT_LIST_HEAD(&tr->func_probes);
7456 INIT_LIST_HEAD(&tr->mod_trace);
7457 INIT_LIST_HEAD(&tr->mod_notrace);
7461 struct ftrace_ops global_ops = {
7462 .func = ftrace_stub,
7463 .flags = FTRACE_OPS_FL_INITIALIZED |
7467 static int __init ftrace_nodyn_init(void)
7472 core_initcall(ftrace_nodyn_init);
7474 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
7475 static inline void ftrace_startup_all(int command) { }
7477 static void ftrace_update_trampoline(struct ftrace_ops *ops)
7481 #endif /* CONFIG_DYNAMIC_FTRACE */
7483 __init void ftrace_init_global_array_ops(struct trace_array *tr)
7485 tr->ops = &global_ops;
7486 tr->ops->private = tr;
7487 ftrace_init_trace_array(tr);
7490 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
7492 /* If we filter on pids, update to use the pid function */
7493 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
7494 if (WARN_ON(tr->ops->func != ftrace_stub))
7495 printk("ftrace ops had %pS for function\n",
7498 tr->ops->func = func;
7499 tr->ops->private = tr;
7502 void ftrace_reset_array_ops(struct trace_array *tr)
7504 tr->ops->func = ftrace_stub;
7507 static nokprobe_inline void
7508 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
7509 struct ftrace_ops *ignored, struct ftrace_regs *fregs)
7511 struct pt_regs *regs = ftrace_get_regs(fregs);
7512 struct ftrace_ops *op;
7516 * The ftrace_test_and_set_recursion() will disable preemption,
7517 * which is required since some of the ops may be dynamically
7518 * allocated, they must be freed after a synchronize_rcu().
7520 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
7524 do_for_each_ftrace_op(op, ftrace_ops_list) {
7525 /* Stub functions don't need to be called nor tested */
7526 if (op->flags & FTRACE_OPS_FL_STUB)
7529 * Check the following for each ops before calling their func:
7530 * if RCU flag is set, then rcu_is_watching() must be true
7531 * if PER_CPU is set, then ftrace_function_local_disable()
7533 * Otherwise test if the ip matches the ops filter
7535 * If any of the above fails then the op->func() is not executed.
7537 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
7538 ftrace_ops_test(op, ip, regs)) {
7539 if (FTRACE_WARN_ON(!op->func)) {
7540 pr_warn("op=%p %pS\n", op, op);
7543 op->func(ip, parent_ip, op, fregs);
7545 } while_for_each_ftrace_op(op);
7547 trace_clear_recursion(bit);
7551 * Some archs only support passing ip and parent_ip. Even though
7552 * the list function ignores the op parameter, we do not want any
7553 * C side effects, where a function is called without the caller
7554 * sending a third parameter.
7555 * Archs are to support both the regs and ftrace_ops at the same time.
7556 * If they support ftrace_ops, it is assumed they support regs.
7557 * If call backs want to use regs, they must either check for regs
7558 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
7559 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
7560 * An architecture can pass partial regs with ftrace_ops and still
7561 * set the ARCH_SUPPORTS_FTRACE_OPS.
7563 * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be
7564 * arch_ftrace_ops_list_func.
7566 #if ARCH_SUPPORTS_FTRACE_OPS
7567 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
7568 struct ftrace_ops *op, struct ftrace_regs *fregs)
7570 __ftrace_ops_list_func(ip, parent_ip, NULL, fregs);
7573 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
7575 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
7578 NOKPROBE_SYMBOL(arch_ftrace_ops_list_func);
7581 * If there's only one function registered but it does not support
7582 * recursion, needs RCU protection and/or requires per cpu handling, then
7583 * this function will be called by the mcount trampoline.
7585 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
7586 struct ftrace_ops *op, struct ftrace_regs *fregs)
7590 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
7594 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
7595 op->func(ip, parent_ip, op, fregs);
7597 trace_clear_recursion(bit);
7599 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
7602 * ftrace_ops_get_func - get the function a trampoline should call
7603 * @ops: the ops to get the function for
7605 * Normally the mcount trampoline will call the ops->func, but there
7606 * are times that it should not. For example, if the ops does not
7607 * have its own recursion protection, then it should call the
7608 * ftrace_ops_assist_func() instead.
7610 * Returns the function that the trampoline should call for @ops.
7612 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
7615 * If the function does not handle recursion or needs to be RCU safe,
7616 * then we need to call the assist handler.
7618 if (ops->flags & (FTRACE_OPS_FL_RECURSION |
7620 return ftrace_ops_assist_func;
7626 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
7627 struct task_struct *prev,
7628 struct task_struct *next,
7629 unsigned int prev_state)
7631 struct trace_array *tr = data;
7632 struct trace_pid_list *pid_list;
7633 struct trace_pid_list *no_pid_list;
7635 pid_list = rcu_dereference_sched(tr->function_pids);
7636 no_pid_list = rcu_dereference_sched(tr->function_no_pids);
7638 if (trace_ignore_this_task(pid_list, no_pid_list, next))
7639 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7642 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7647 ftrace_pid_follow_sched_process_fork(void *data,
7648 struct task_struct *self,
7649 struct task_struct *task)
7651 struct trace_pid_list *pid_list;
7652 struct trace_array *tr = data;
7654 pid_list = rcu_dereference_sched(tr->function_pids);
7655 trace_filter_add_remove_task(pid_list, self, task);
7657 pid_list = rcu_dereference_sched(tr->function_no_pids);
7658 trace_filter_add_remove_task(pid_list, self, task);
7662 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
7664 struct trace_pid_list *pid_list;
7665 struct trace_array *tr = data;
7667 pid_list = rcu_dereference_sched(tr->function_pids);
7668 trace_filter_add_remove_task(pid_list, NULL, task);
7670 pid_list = rcu_dereference_sched(tr->function_no_pids);
7671 trace_filter_add_remove_task(pid_list, NULL, task);
7674 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
7677 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7679 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
7682 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7684 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
7689 static void clear_ftrace_pids(struct trace_array *tr, int type)
7691 struct trace_pid_list *pid_list;
7692 struct trace_pid_list *no_pid_list;
7695 pid_list = rcu_dereference_protected(tr->function_pids,
7696 lockdep_is_held(&ftrace_lock));
7697 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7698 lockdep_is_held(&ftrace_lock));
7700 /* Make sure there's something to do */
7701 if (!pid_type_enabled(type, pid_list, no_pid_list))
7704 /* See if the pids still need to be checked after this */
7705 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
7706 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7707 for_each_possible_cpu(cpu)
7708 per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE;
7711 if (type & TRACE_PIDS)
7712 rcu_assign_pointer(tr->function_pids, NULL);
7714 if (type & TRACE_NO_PIDS)
7715 rcu_assign_pointer(tr->function_no_pids, NULL);
7717 /* Wait till all users are no longer using pid filtering */
7720 if ((type & TRACE_PIDS) && pid_list)
7721 trace_pid_list_free(pid_list);
7723 if ((type & TRACE_NO_PIDS) && no_pid_list)
7724 trace_pid_list_free(no_pid_list);
7727 void ftrace_clear_pids(struct trace_array *tr)
7729 mutex_lock(&ftrace_lock);
7731 clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
7733 mutex_unlock(&ftrace_lock);
7736 static void ftrace_pid_reset(struct trace_array *tr, int type)
7738 mutex_lock(&ftrace_lock);
7739 clear_ftrace_pids(tr, type);
7741 ftrace_update_pid_func();
7742 ftrace_startup_all(0);
7744 mutex_unlock(&ftrace_lock);
7747 /* Greater than any max PID */
7748 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
7750 static void *fpid_start(struct seq_file *m, loff_t *pos)
7753 struct trace_pid_list *pid_list;
7754 struct trace_array *tr = m->private;
7756 mutex_lock(&ftrace_lock);
7757 rcu_read_lock_sched();
7759 pid_list = rcu_dereference_sched(tr->function_pids);
7762 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7764 return trace_pid_start(pid_list, pos);
7767 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
7769 struct trace_array *tr = m->private;
7770 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
7772 if (v == FTRACE_NO_PIDS) {
7776 return trace_pid_next(pid_list, v, pos);
7779 static void fpid_stop(struct seq_file *m, void *p)
7782 rcu_read_unlock_sched();
7783 mutex_unlock(&ftrace_lock);
7786 static int fpid_show(struct seq_file *m, void *v)
7788 if (v == FTRACE_NO_PIDS) {
7789 seq_puts(m, "no pid\n");
7793 return trace_pid_show(m, v);
7796 static const struct seq_operations ftrace_pid_sops = {
7797 .start = fpid_start,
7803 static void *fnpid_start(struct seq_file *m, loff_t *pos)
7806 struct trace_pid_list *pid_list;
7807 struct trace_array *tr = m->private;
7809 mutex_lock(&ftrace_lock);
7810 rcu_read_lock_sched();
7812 pid_list = rcu_dereference_sched(tr->function_no_pids);
7815 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7817 return trace_pid_start(pid_list, pos);
7820 static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos)
7822 struct trace_array *tr = m->private;
7823 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids);
7825 if (v == FTRACE_NO_PIDS) {
7829 return trace_pid_next(pid_list, v, pos);
7832 static const struct seq_operations ftrace_no_pid_sops = {
7833 .start = fnpid_start,
7839 static int pid_open(struct inode *inode, struct file *file, int type)
7841 const struct seq_operations *seq_ops;
7842 struct trace_array *tr = inode->i_private;
7846 ret = tracing_check_open_get_tr(tr);
7850 if ((file->f_mode & FMODE_WRITE) &&
7851 (file->f_flags & O_TRUNC))
7852 ftrace_pid_reset(tr, type);
7856 seq_ops = &ftrace_pid_sops;
7859 seq_ops = &ftrace_no_pid_sops;
7862 trace_array_put(tr);
7867 ret = seq_open(file, seq_ops);
7869 trace_array_put(tr);
7871 m = file->private_data;
7872 /* copy tr over to seq ops */
7880 ftrace_pid_open(struct inode *inode, struct file *file)
7882 return pid_open(inode, file, TRACE_PIDS);
7886 ftrace_no_pid_open(struct inode *inode, struct file *file)
7888 return pid_open(inode, file, TRACE_NO_PIDS);
7891 static void ignore_task_cpu(void *data)
7893 struct trace_array *tr = data;
7894 struct trace_pid_list *pid_list;
7895 struct trace_pid_list *no_pid_list;
7898 * This function is called by on_each_cpu() while the
7899 * event_mutex is held.
7901 pid_list = rcu_dereference_protected(tr->function_pids,
7902 mutex_is_locked(&ftrace_lock));
7903 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7904 mutex_is_locked(&ftrace_lock));
7906 if (trace_ignore_this_task(pid_list, no_pid_list, current))
7907 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7910 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7915 pid_write(struct file *filp, const char __user *ubuf,
7916 size_t cnt, loff_t *ppos, int type)
7918 struct seq_file *m = filp->private_data;
7919 struct trace_array *tr = m->private;
7920 struct trace_pid_list *filtered_pids;
7921 struct trace_pid_list *other_pids;
7922 struct trace_pid_list *pid_list;
7928 mutex_lock(&ftrace_lock);
7932 filtered_pids = rcu_dereference_protected(tr->function_pids,
7933 lockdep_is_held(&ftrace_lock));
7934 other_pids = rcu_dereference_protected(tr->function_no_pids,
7935 lockdep_is_held(&ftrace_lock));
7938 filtered_pids = rcu_dereference_protected(tr->function_no_pids,
7939 lockdep_is_held(&ftrace_lock));
7940 other_pids = rcu_dereference_protected(tr->function_pids,
7941 lockdep_is_held(&ftrace_lock));
7949 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
7955 rcu_assign_pointer(tr->function_pids, pid_list);
7958 rcu_assign_pointer(tr->function_no_pids, pid_list);
7963 if (filtered_pids) {
7965 trace_pid_list_free(filtered_pids);
7966 } else if (pid_list && !other_pids) {
7967 /* Register a probe to set whether to ignore the tracing of a task */
7968 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7972 * Ignoring of pids is done at task switch. But we have to
7973 * check for those tasks that are currently running.
7974 * Always do this in case a pid was appended or removed.
7976 on_each_cpu(ignore_task_cpu, tr, 1);
7978 ftrace_update_pid_func();
7979 ftrace_startup_all(0);
7981 mutex_unlock(&ftrace_lock);
7990 ftrace_pid_write(struct file *filp, const char __user *ubuf,
7991 size_t cnt, loff_t *ppos)
7993 return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
7997 ftrace_no_pid_write(struct file *filp, const char __user *ubuf,
7998 size_t cnt, loff_t *ppos)
8000 return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
8004 ftrace_pid_release(struct inode *inode, struct file *file)
8006 struct trace_array *tr = inode->i_private;
8008 trace_array_put(tr);
8010 return seq_release(inode, file);
8013 static const struct file_operations ftrace_pid_fops = {
8014 .open = ftrace_pid_open,
8015 .write = ftrace_pid_write,
8017 .llseek = tracing_lseek,
8018 .release = ftrace_pid_release,
8021 static const struct file_operations ftrace_no_pid_fops = {
8022 .open = ftrace_no_pid_open,
8023 .write = ftrace_no_pid_write,
8025 .llseek = tracing_lseek,
8026 .release = ftrace_pid_release,
8029 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
8031 trace_create_file("set_ftrace_pid", TRACE_MODE_WRITE, d_tracer,
8032 tr, &ftrace_pid_fops);
8033 trace_create_file("set_ftrace_notrace_pid", TRACE_MODE_WRITE,
8034 d_tracer, tr, &ftrace_no_pid_fops);
8037 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
8038 struct dentry *d_tracer)
8040 /* Only the top level directory has the dyn_tracefs and profile */
8041 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
8043 ftrace_init_dyn_tracefs(d_tracer);
8044 ftrace_profile_tracefs(d_tracer);
8048 * ftrace_kill - kill ftrace
8050 * This function should be used by panic code. It stops ftrace
8051 * but in a not so nice way. If you need to simply kill ftrace
8052 * from a non-atomic section, use ftrace_kill.
8054 void ftrace_kill(void)
8056 ftrace_disabled = 1;
8058 ftrace_trace_function = ftrace_stub;
8062 * ftrace_is_dead - Test if ftrace is dead or not.
8064 * Returns 1 if ftrace is "dead", zero otherwise.
8066 int ftrace_is_dead(void)
8068 return ftrace_disabled;
8071 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
8073 * When registering ftrace_ops with IPMODIFY, it is necessary to make sure
8074 * it doesn't conflict with any direct ftrace_ops. If there is existing
8075 * direct ftrace_ops on a kernel function being patched, call
8076 * FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER on it to enable sharing.
8078 * @ops: ftrace_ops being registered.
8082 * Negative on failure.
8084 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops)
8086 struct ftrace_func_entry *entry;
8087 struct ftrace_hash *hash;
8088 struct ftrace_ops *op;
8091 lockdep_assert_held_once(&direct_mutex);
8093 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
8096 hash = ops->func_hash->filter_hash;
8097 size = 1 << hash->size_bits;
8098 for (i = 0; i < size; i++) {
8099 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
8100 unsigned long ip = entry->ip;
8101 bool found_op = false;
8103 mutex_lock(&ftrace_lock);
8104 do_for_each_ftrace_op(op, ftrace_ops_list) {
8105 if (!(op->flags & FTRACE_OPS_FL_DIRECT))
8107 if (ops_references_ip(op, ip)) {
8111 } while_for_each_ftrace_op(op);
8112 mutex_unlock(&ftrace_lock);
8118 ret = op->ops_func(op, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER);
8129 * Similar to prepare_direct_functions_for_ipmodify, clean up after ops
8130 * with IPMODIFY is unregistered. The cleanup is optional for most DIRECT
8133 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)
8135 struct ftrace_func_entry *entry;
8136 struct ftrace_hash *hash;
8137 struct ftrace_ops *op;
8140 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
8143 mutex_lock(&direct_mutex);
8145 hash = ops->func_hash->filter_hash;
8146 size = 1 << hash->size_bits;
8147 for (i = 0; i < size; i++) {
8148 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
8149 unsigned long ip = entry->ip;
8150 bool found_op = false;
8152 mutex_lock(&ftrace_lock);
8153 do_for_each_ftrace_op(op, ftrace_ops_list) {
8154 if (!(op->flags & FTRACE_OPS_FL_DIRECT))
8156 if (ops_references_ip(op, ip)) {
8160 } while_for_each_ftrace_op(op);
8161 mutex_unlock(&ftrace_lock);
8163 /* The cleanup is optional, ignore any errors */
8164 if (found_op && op->ops_func)
8165 op->ops_func(op, FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER);
8168 mutex_unlock(&direct_mutex);
8171 #define lock_direct_mutex() mutex_lock(&direct_mutex)
8172 #define unlock_direct_mutex() mutex_unlock(&direct_mutex)
8174 #else /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
8176 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops)
8181 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops)
8185 #define lock_direct_mutex() do { } while (0)
8186 #define unlock_direct_mutex() do { } while (0)
8188 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
8191 * Similar to register_ftrace_function, except we don't lock direct_mutex.
8193 static int register_ftrace_function_nolock(struct ftrace_ops *ops)
8197 ftrace_ops_init(ops);
8199 mutex_lock(&ftrace_lock);
8201 ret = ftrace_startup(ops, 0);
8203 mutex_unlock(&ftrace_lock);
8209 * register_ftrace_function - register a function for profiling
8210 * @ops: ops structure that holds the function for profiling.
8212 * Register a function to be called by all functions in the
8215 * Note: @ops->func and all the functions it calls must be labeled
8216 * with "notrace", otherwise it will go into a
8219 int register_ftrace_function(struct ftrace_ops *ops)
8223 lock_direct_mutex();
8224 ret = prepare_direct_functions_for_ipmodify(ops);
8228 ret = register_ftrace_function_nolock(ops);
8231 unlock_direct_mutex();
8234 EXPORT_SYMBOL_GPL(register_ftrace_function);
8237 * unregister_ftrace_function - unregister a function for profiling.
8238 * @ops: ops structure that holds the function to unregister
8240 * Unregister a function that was added to be called by ftrace profiling.
8242 int unregister_ftrace_function(struct ftrace_ops *ops)
8246 mutex_lock(&ftrace_lock);
8247 ret = ftrace_shutdown(ops, 0);
8248 mutex_unlock(&ftrace_lock);
8250 cleanup_direct_functions_after_ipmodify(ops);
8253 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
8255 static int symbols_cmp(const void *a, const void *b)
8257 const char **str_a = (const char **) a;
8258 const char **str_b = (const char **) b;
8260 return strcmp(*str_a, *str_b);
8263 struct kallsyms_data {
8264 unsigned long *addrs;
8270 static int kallsyms_callback(void *data, const char *name,
8271 struct module *mod, unsigned long addr)
8273 struct kallsyms_data *args = data;
8277 sym = bsearch(&name, args->syms, args->cnt, sizeof(*args->syms), symbols_cmp);
8281 idx = sym - args->syms;
8282 if (args->addrs[idx])
8285 if (!ftrace_location(addr))
8288 args->addrs[idx] = addr;
8290 return args->found == args->cnt ? 1 : 0;
8294 * ftrace_lookup_symbols - Lookup addresses for array of symbols
8296 * @sorted_syms: array of symbols pointers symbols to resolve,
8297 * must be alphabetically sorted
8298 * @cnt: number of symbols/addresses in @syms/@addrs arrays
8299 * @addrs: array for storing resulting addresses
8301 * This function looks up addresses for array of symbols provided in
8302 * @syms array (must be alphabetically sorted) and stores them in
8303 * @addrs array, which needs to be big enough to store at least @cnt
8306 * This function returns 0 if all provided symbols are found,
8309 int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs)
8311 struct kallsyms_data args;
8314 memset(addrs, 0, sizeof(*addrs) * cnt);
8316 args.syms = sorted_syms;
8319 err = kallsyms_on_each_symbol(kallsyms_callback, &args);
8322 return args.found == args.cnt ? 0 : -ESRCH;
8325 #ifdef CONFIG_SYSCTL
8327 #ifdef CONFIG_DYNAMIC_FTRACE
8328 static void ftrace_startup_sysctl(void)
8332 if (unlikely(ftrace_disabled))
8335 /* Force update next time */
8336 saved_ftrace_func = NULL;
8337 /* ftrace_start_up is true if we want ftrace running */
8338 if (ftrace_start_up) {
8339 command = FTRACE_UPDATE_CALLS;
8340 if (ftrace_graph_active)
8341 command |= FTRACE_START_FUNC_RET;
8342 ftrace_startup_enable(command);
8346 static void ftrace_shutdown_sysctl(void)
8350 if (unlikely(ftrace_disabled))
8353 /* ftrace_start_up is true if ftrace is running */
8354 if (ftrace_start_up) {
8355 command = FTRACE_DISABLE_CALLS;
8356 if (ftrace_graph_active)
8357 command |= FTRACE_STOP_FUNC_RET;
8358 ftrace_run_update_code(command);
8362 # define ftrace_startup_sysctl() do { } while (0)
8363 # define ftrace_shutdown_sysctl() do { } while (0)
8364 #endif /* CONFIG_DYNAMIC_FTRACE */
8366 static bool is_permanent_ops_registered(void)
8368 struct ftrace_ops *op;
8370 do_for_each_ftrace_op(op, ftrace_ops_list) {
8371 if (op->flags & FTRACE_OPS_FL_PERMANENT)
8373 } while_for_each_ftrace_op(op);
8379 ftrace_enable_sysctl(struct ctl_table *table, int write,
8380 void *buffer, size_t *lenp, loff_t *ppos)
8384 mutex_lock(&ftrace_lock);
8386 if (unlikely(ftrace_disabled))
8389 ret = proc_dointvec(table, write, buffer, lenp, ppos);
8391 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
8394 if (ftrace_enabled) {
8396 /* we are starting ftrace again */
8397 if (rcu_dereference_protected(ftrace_ops_list,
8398 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
8399 update_ftrace_function();
8401 ftrace_startup_sysctl();
8404 if (is_permanent_ops_registered()) {
8405 ftrace_enabled = true;
8410 /* stopping ftrace calls (just send to ftrace_stub) */
8411 ftrace_trace_function = ftrace_stub;
8413 ftrace_shutdown_sysctl();
8416 last_ftrace_enabled = !!ftrace_enabled;
8418 mutex_unlock(&ftrace_lock);
8422 static struct ctl_table ftrace_sysctls[] = {
8424 .procname = "ftrace_enabled",
8425 .data = &ftrace_enabled,
8426 .maxlen = sizeof(int),
8428 .proc_handler = ftrace_enable_sysctl,
8433 static int __init ftrace_sysctl_init(void)
8435 register_sysctl_init("kernel", ftrace_sysctls);
8438 late_initcall(ftrace_sysctl_init);