1 // SPDX-License-Identifier: GPL-2.0
3 * Infrastructure for profiling code inserted by 'gcc -pg'.
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
8 * Originally ported from the -rt patch by:
9 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
11 * Based on code in the latency_tracer, that is:
13 * Copyright (C) 2004-2006 Ingo Molnar
14 * Copyright (C) 2004 Nadia Yvette Chambers
17 #include <linux/stop_machine.h>
18 #include <linux/clocksource.h>
19 #include <linux/sched/task.h>
20 #include <linux/kallsyms.h>
21 #include <linux/security.h>
22 #include <linux/seq_file.h>
23 #include <linux/tracefs.h>
24 #include <linux/hardirq.h>
25 #include <linux/kthread.h>
26 #include <linux/uaccess.h>
27 #include <linux/bsearch.h>
28 #include <linux/module.h>
29 #include <linux/ftrace.h>
30 #include <linux/sysctl.h>
31 #include <linux/slab.h>
32 #include <linux/ctype.h>
33 #include <linux/sort.h>
34 #include <linux/list.h>
35 #include <linux/hash.h>
36 #include <linux/rcupdate.h>
37 #include <linux/kprobes.h>
39 #include <trace/events/sched.h>
41 #include <asm/sections.h>
42 #include <asm/setup.h>
44 #include "ftrace_internal.h"
45 #include "trace_output.h"
46 #include "trace_stat.h"
48 #define FTRACE_WARN_ON(cond) \
56 #define FTRACE_WARN_ON_ONCE(cond) \
59 if (WARN_ON_ONCE(___r)) \
64 /* hash bits for specific function selection */
65 #define FTRACE_HASH_BITS 7
66 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
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_RECURSION_SAFE | 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 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;
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 #if ARCH_SUPPORTS_FTRACE_OPS
125 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
126 struct ftrace_ops *op, struct pt_regs *regs);
128 /* See comment below, where ftrace_ops_list_func is defined */
129 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
130 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
133 static inline void ftrace_ops_init(struct ftrace_ops *ops)
135 #ifdef CONFIG_DYNAMIC_FTRACE
136 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
137 mutex_init(&ops->local_hash.regex_lock);
138 ops->func_hash = &ops->local_hash;
139 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
144 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
145 struct ftrace_ops *op, struct pt_regs *regs)
147 struct trace_array *tr = op->private;
149 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
152 op->saved_func(ip, parent_ip, op, regs);
155 static void ftrace_sync(struct work_struct *work)
158 * This function is just a stub to implement a hard force
159 * of synchronize_rcu(). This requires synchronizing
160 * tasks even in userspace and idle.
162 * Yes, function tracing is rude.
166 static void ftrace_sync_ipi(void *data)
168 /* Probably not needed, but do it anyway */
172 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
175 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
176 * then it needs to call the list anyway.
178 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
179 FTRACE_FORCE_LIST_FUNC)
180 return ftrace_ops_list_func;
182 return ftrace_ops_get_func(ops);
185 static void update_ftrace_function(void)
190 * Prepare the ftrace_ops that the arch callback will use.
191 * If there's only one ftrace_ops registered, the ftrace_ops_list
192 * will point to the ops we want.
194 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
195 lockdep_is_held(&ftrace_lock));
197 /* If there's no ftrace_ops registered, just call the stub function */
198 if (set_function_trace_op == &ftrace_list_end) {
202 * If we are at the end of the list and this ops is
203 * recursion safe and not dynamic and the arch supports passing ops,
204 * then have the mcount trampoline call the function directly.
206 } else if (rcu_dereference_protected(ftrace_ops_list->next,
207 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
208 func = ftrace_ops_get_list_func(ftrace_ops_list);
211 /* Just use the default ftrace_ops */
212 set_function_trace_op = &ftrace_list_end;
213 func = ftrace_ops_list_func;
216 update_function_graph_func();
218 /* If there's no change, then do nothing more here */
219 if (ftrace_trace_function == func)
223 * If we are using the list function, it doesn't care
224 * about the function_trace_ops.
226 if (func == ftrace_ops_list_func) {
227 ftrace_trace_function = func;
229 * Don't even bother setting function_trace_ops,
230 * it would be racy to do so anyway.
235 #ifndef CONFIG_DYNAMIC_FTRACE
237 * For static tracing, we need to be a bit more careful.
238 * The function change takes affect immediately. Thus,
239 * we need to coorditate the setting of the function_trace_ops
240 * with the setting of the ftrace_trace_function.
242 * Set the function to the list ops, which will call the
243 * function we want, albeit indirectly, but it handles the
244 * ftrace_ops and doesn't depend on function_trace_op.
246 ftrace_trace_function = ftrace_ops_list_func;
248 * Make sure all CPUs see this. Yes this is slow, but static
249 * tracing is slow and nasty to have enabled.
251 schedule_on_each_cpu(ftrace_sync);
252 /* Now all cpus are using the list ops. */
253 function_trace_op = set_function_trace_op;
254 /* Make sure the function_trace_op is visible on all CPUs */
256 /* Nasty way to force a rmb on all cpus */
257 smp_call_function(ftrace_sync_ipi, NULL, 1);
258 /* OK, we are all set to update the ftrace_trace_function now! */
259 #endif /* !CONFIG_DYNAMIC_FTRACE */
261 ftrace_trace_function = func;
264 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
265 struct ftrace_ops *ops)
267 rcu_assign_pointer(ops->next, *list);
270 * We are entering ops into the list but another
271 * CPU might be walking that list. We need to make sure
272 * the ops->next pointer is valid before another CPU sees
273 * the ops pointer included into the list.
275 rcu_assign_pointer(*list, ops);
278 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
279 struct ftrace_ops *ops)
281 struct ftrace_ops **p;
284 * If we are removing the last function, then simply point
285 * to the ftrace_stub.
287 if (rcu_dereference_protected(*list,
288 lockdep_is_held(&ftrace_lock)) == ops &&
289 rcu_dereference_protected(ops->next,
290 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
291 *list = &ftrace_list_end;
295 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
306 static void ftrace_update_trampoline(struct ftrace_ops *ops);
308 int __register_ftrace_function(struct ftrace_ops *ops)
310 if (ops->flags & FTRACE_OPS_FL_DELETED)
313 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
316 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
318 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
319 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
320 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
322 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
323 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
326 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
327 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
329 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
332 if (!core_kernel_data((unsigned long)ops))
333 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
335 add_ftrace_ops(&ftrace_ops_list, ops);
337 /* Always save the function, and reset at unregistering */
338 ops->saved_func = ops->func;
340 if (ftrace_pids_enabled(ops))
341 ops->func = ftrace_pid_func;
343 ftrace_update_trampoline(ops);
346 update_ftrace_function();
351 int __unregister_ftrace_function(struct ftrace_ops *ops)
355 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
358 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
364 update_ftrace_function();
366 ops->func = ops->saved_func;
371 static void ftrace_update_pid_func(void)
373 struct ftrace_ops *op;
375 /* Only do something if we are tracing something */
376 if (ftrace_trace_function == ftrace_stub)
379 do_for_each_ftrace_op(op, ftrace_ops_list) {
380 if (op->flags & FTRACE_OPS_FL_PID) {
381 op->func = ftrace_pids_enabled(op) ?
382 ftrace_pid_func : op->saved_func;
383 ftrace_update_trampoline(op);
385 } while_for_each_ftrace_op(op);
387 update_ftrace_function();
390 #ifdef CONFIG_FUNCTION_PROFILER
391 struct ftrace_profile {
392 struct hlist_node node;
394 unsigned long counter;
395 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
396 unsigned long long time;
397 unsigned long long time_squared;
401 struct ftrace_profile_page {
402 struct ftrace_profile_page *next;
404 struct ftrace_profile records[];
407 struct ftrace_profile_stat {
409 struct hlist_head *hash;
410 struct ftrace_profile_page *pages;
411 struct ftrace_profile_page *start;
412 struct tracer_stat stat;
415 #define PROFILE_RECORDS_SIZE \
416 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
418 #define PROFILES_PER_PAGE \
419 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
421 static int ftrace_profile_enabled __read_mostly;
423 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
424 static DEFINE_MUTEX(ftrace_profile_lock);
426 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
428 #define FTRACE_PROFILE_HASH_BITS 10
429 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
432 function_stat_next(void *v, int idx)
434 struct ftrace_profile *rec = v;
435 struct ftrace_profile_page *pg;
437 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
443 if ((void *)rec >= (void *)&pg->records[pg->index]) {
447 rec = &pg->records[0];
455 static void *function_stat_start(struct tracer_stat *trace)
457 struct ftrace_profile_stat *stat =
458 container_of(trace, struct ftrace_profile_stat, stat);
460 if (!stat || !stat->start)
463 return function_stat_next(&stat->start->records[0], 0);
466 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
467 /* function graph compares on total time */
468 static int function_stat_cmp(const void *p1, const void *p2)
470 const struct ftrace_profile *a = p1;
471 const struct ftrace_profile *b = p2;
473 if (a->time < b->time)
475 if (a->time > b->time)
481 /* not function graph compares against hits */
482 static int function_stat_cmp(const void *p1, const void *p2)
484 const struct ftrace_profile *a = p1;
485 const struct ftrace_profile *b = p2;
487 if (a->counter < b->counter)
489 if (a->counter > b->counter)
496 static int function_stat_headers(struct seq_file *m)
498 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
499 seq_puts(m, " Function "
502 "--- ---- --- ---\n");
504 seq_puts(m, " Function Hit\n"
510 static int function_stat_show(struct seq_file *m, void *v)
512 struct ftrace_profile *rec = v;
513 char str[KSYM_SYMBOL_LEN];
515 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
516 static struct trace_seq s;
517 unsigned long long avg;
518 unsigned long long stddev;
520 mutex_lock(&ftrace_profile_lock);
522 /* we raced with function_profile_reset() */
523 if (unlikely(rec->counter == 0)) {
528 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
529 avg = div64_ul(rec->time, rec->counter);
530 if (tracing_thresh && (avg < tracing_thresh))
534 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
535 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
537 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
540 /* Sample standard deviation (s^2) */
541 if (rec->counter <= 1)
545 * Apply Welford's method:
546 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
548 stddev = rec->counter * rec->time_squared -
549 rec->time * rec->time;
552 * Divide only 1000 for ns^2 -> us^2 conversion.
553 * trace_print_graph_duration will divide 1000 again.
555 stddev = div64_ul(stddev,
556 rec->counter * (rec->counter - 1) * 1000);
560 trace_print_graph_duration(rec->time, &s);
561 trace_seq_puts(&s, " ");
562 trace_print_graph_duration(avg, &s);
563 trace_seq_puts(&s, " ");
564 trace_print_graph_duration(stddev, &s);
565 trace_print_seq(m, &s);
569 mutex_unlock(&ftrace_profile_lock);
574 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
576 struct ftrace_profile_page *pg;
578 pg = stat->pages = stat->start;
581 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
586 memset(stat->hash, 0,
587 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
590 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
592 struct ftrace_profile_page *pg;
597 /* If we already allocated, do nothing */
601 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
605 #ifdef CONFIG_DYNAMIC_FTRACE
606 functions = ftrace_update_tot_cnt;
609 * We do not know the number of functions that exist because
610 * dynamic tracing is what counts them. With past experience
611 * we have around 20K functions. That should be more than enough.
612 * It is highly unlikely we will execute every function in
618 pg = stat->start = stat->pages;
620 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
622 for (i = 1; i < pages; i++) {
623 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
634 unsigned long tmp = (unsigned long)pg;
646 static int ftrace_profile_init_cpu(int cpu)
648 struct ftrace_profile_stat *stat;
651 stat = &per_cpu(ftrace_profile_stats, cpu);
654 /* If the profile is already created, simply reset it */
655 ftrace_profile_reset(stat);
660 * We are profiling all functions, but usually only a few thousand
661 * functions are hit. We'll make a hash of 1024 items.
663 size = FTRACE_PROFILE_HASH_SIZE;
665 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
670 /* Preallocate the function profiling pages */
671 if (ftrace_profile_pages_init(stat) < 0) {
680 static int ftrace_profile_init(void)
685 for_each_possible_cpu(cpu) {
686 ret = ftrace_profile_init_cpu(cpu);
694 /* interrupts must be disabled */
695 static struct ftrace_profile *
696 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
698 struct ftrace_profile *rec;
699 struct hlist_head *hhd;
702 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
703 hhd = &stat->hash[key];
705 if (hlist_empty(hhd))
708 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
716 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
717 struct ftrace_profile *rec)
721 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
722 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
726 * The memory is already allocated, this simply finds a new record to use.
728 static struct ftrace_profile *
729 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
731 struct ftrace_profile *rec = NULL;
733 /* prevent recursion (from NMIs) */
734 if (atomic_inc_return(&stat->disabled) != 1)
738 * Try to find the function again since an NMI
739 * could have added it
741 rec = ftrace_find_profiled_func(stat, ip);
745 if (stat->pages->index == PROFILES_PER_PAGE) {
746 if (!stat->pages->next)
748 stat->pages = stat->pages->next;
751 rec = &stat->pages->records[stat->pages->index++];
753 ftrace_add_profile(stat, rec);
756 atomic_dec(&stat->disabled);
762 function_profile_call(unsigned long ip, unsigned long parent_ip,
763 struct ftrace_ops *ops, struct pt_regs *regs)
765 struct ftrace_profile_stat *stat;
766 struct ftrace_profile *rec;
769 if (!ftrace_profile_enabled)
772 local_irq_save(flags);
774 stat = this_cpu_ptr(&ftrace_profile_stats);
775 if (!stat->hash || !ftrace_profile_enabled)
778 rec = ftrace_find_profiled_func(stat, ip);
780 rec = ftrace_profile_alloc(stat, ip);
787 local_irq_restore(flags);
790 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
791 static bool fgraph_graph_time = true;
793 void ftrace_graph_graph_time_control(bool enable)
795 fgraph_graph_time = enable;
798 static int profile_graph_entry(struct ftrace_graph_ent *trace)
800 struct ftrace_ret_stack *ret_stack;
802 function_profile_call(trace->func, 0, NULL, NULL);
804 /* If function graph is shutting down, ret_stack can be NULL */
805 if (!current->ret_stack)
808 ret_stack = ftrace_graph_get_ret_stack(current, 0);
810 ret_stack->subtime = 0;
815 static void profile_graph_return(struct ftrace_graph_ret *trace)
817 struct ftrace_ret_stack *ret_stack;
818 struct ftrace_profile_stat *stat;
819 unsigned long long calltime;
820 struct ftrace_profile *rec;
823 local_irq_save(flags);
824 stat = this_cpu_ptr(&ftrace_profile_stats);
825 if (!stat->hash || !ftrace_profile_enabled)
828 /* If the calltime was zero'd ignore it */
829 if (!trace->calltime)
832 calltime = trace->rettime - trace->calltime;
834 if (!fgraph_graph_time) {
836 /* Append this call time to the parent time to subtract */
837 ret_stack = ftrace_graph_get_ret_stack(current, 1);
839 ret_stack->subtime += calltime;
841 ret_stack = ftrace_graph_get_ret_stack(current, 0);
842 if (ret_stack && ret_stack->subtime < calltime)
843 calltime -= ret_stack->subtime;
848 rec = ftrace_find_profiled_func(stat, trace->func);
850 rec->time += calltime;
851 rec->time_squared += calltime * calltime;
855 local_irq_restore(flags);
858 static struct fgraph_ops fprofiler_ops = {
859 .entryfunc = &profile_graph_entry,
860 .retfunc = &profile_graph_return,
863 static int register_ftrace_profiler(void)
865 return register_ftrace_graph(&fprofiler_ops);
868 static void unregister_ftrace_profiler(void)
870 unregister_ftrace_graph(&fprofiler_ops);
873 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
874 .func = function_profile_call,
875 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
876 INIT_OPS_HASH(ftrace_profile_ops)
879 static int register_ftrace_profiler(void)
881 return register_ftrace_function(&ftrace_profile_ops);
884 static void unregister_ftrace_profiler(void)
886 unregister_ftrace_function(&ftrace_profile_ops);
888 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
891 ftrace_profile_write(struct file *filp, const char __user *ubuf,
892 size_t cnt, loff_t *ppos)
897 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
903 mutex_lock(&ftrace_profile_lock);
904 if (ftrace_profile_enabled ^ val) {
906 ret = ftrace_profile_init();
912 ret = register_ftrace_profiler();
917 ftrace_profile_enabled = 1;
919 ftrace_profile_enabled = 0;
921 * unregister_ftrace_profiler calls stop_machine
922 * so this acts like an synchronize_rcu.
924 unregister_ftrace_profiler();
928 mutex_unlock(&ftrace_profile_lock);
936 ftrace_profile_read(struct file *filp, char __user *ubuf,
937 size_t cnt, loff_t *ppos)
939 char buf[64]; /* big enough to hold a number */
942 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
943 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
946 static const struct file_operations ftrace_profile_fops = {
947 .open = tracing_open_generic,
948 .read = ftrace_profile_read,
949 .write = ftrace_profile_write,
950 .llseek = default_llseek,
953 /* used to initialize the real stat files */
954 static struct tracer_stat function_stats __initdata = {
956 .stat_start = function_stat_start,
957 .stat_next = function_stat_next,
958 .stat_cmp = function_stat_cmp,
959 .stat_headers = function_stat_headers,
960 .stat_show = function_stat_show
963 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
965 struct ftrace_profile_stat *stat;
966 struct dentry *entry;
971 for_each_possible_cpu(cpu) {
972 stat = &per_cpu(ftrace_profile_stats, cpu);
974 name = kasprintf(GFP_KERNEL, "function%d", cpu);
977 * The files created are permanent, if something happens
978 * we still do not free memory.
981 "Could not allocate stat file for cpu %d\n",
985 stat->stat = function_stats;
986 stat->stat.name = name;
987 ret = register_stat_tracer(&stat->stat);
990 "Could not register function stat for cpu %d\n",
997 entry = tracefs_create_file("function_profile_enabled", 0644,
998 d_tracer, NULL, &ftrace_profile_fops);
1000 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1003 #else /* CONFIG_FUNCTION_PROFILER */
1004 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1007 #endif /* CONFIG_FUNCTION_PROFILER */
1009 #ifdef CONFIG_DYNAMIC_FTRACE
1011 static struct ftrace_ops *removed_ops;
1014 * Set when doing a global update, like enabling all recs or disabling them.
1015 * It is not set when just updating a single ftrace_ops.
1017 static bool update_all_ops;
1019 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1020 # error Dynamic ftrace depends on MCOUNT_RECORD
1023 struct ftrace_func_probe {
1024 struct ftrace_probe_ops *probe_ops;
1025 struct ftrace_ops ops;
1026 struct trace_array *tr;
1027 struct list_head list;
1033 * We make these constant because no one should touch them,
1034 * but they are used as the default "empty hash", to avoid allocating
1035 * it all the time. These are in a read only section such that if
1036 * anyone does try to modify it, it will cause an exception.
1038 static const struct hlist_head empty_buckets[1];
1039 static const struct ftrace_hash empty_hash = {
1040 .buckets = (struct hlist_head *)empty_buckets,
1042 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1044 struct ftrace_ops global_ops = {
1045 .func = ftrace_stub,
1046 .local_hash.notrace_hash = EMPTY_HASH,
1047 .local_hash.filter_hash = EMPTY_HASH,
1048 INIT_OPS_HASH(global_ops)
1049 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1050 FTRACE_OPS_FL_INITIALIZED |
1055 * Used by the stack undwinder to know about dynamic ftrace trampolines.
1057 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1059 struct ftrace_ops *op = NULL;
1062 * Some of the ops may be dynamically allocated,
1063 * they are freed after a synchronize_rcu().
1065 preempt_disable_notrace();
1067 do_for_each_ftrace_op(op, ftrace_ops_list) {
1069 * This is to check for dynamically allocated trampolines.
1070 * Trampolines that are in kernel text will have
1071 * core_kernel_text() return true.
1073 if (op->trampoline && op->trampoline_size)
1074 if (addr >= op->trampoline &&
1075 addr < op->trampoline + op->trampoline_size) {
1076 preempt_enable_notrace();
1079 } while_for_each_ftrace_op(op);
1080 preempt_enable_notrace();
1086 * This is used by __kernel_text_address() to return true if the
1087 * address is on a dynamically allocated trampoline that would
1088 * not return true for either core_kernel_text() or
1089 * is_module_text_address().
1091 bool is_ftrace_trampoline(unsigned long addr)
1093 return ftrace_ops_trampoline(addr) != NULL;
1096 struct ftrace_page {
1097 struct ftrace_page *next;
1098 struct dyn_ftrace *records;
1103 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1104 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1106 /* estimate from running different kernels */
1107 #define NR_TO_INIT 10000
1109 static struct ftrace_page *ftrace_pages_start;
1110 static struct ftrace_page *ftrace_pages;
1112 static __always_inline unsigned long
1113 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1115 if (hash->size_bits > 0)
1116 return hash_long(ip, hash->size_bits);
1121 /* Only use this function if ftrace_hash_empty() has already been tested */
1122 static __always_inline struct ftrace_func_entry *
1123 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1126 struct ftrace_func_entry *entry;
1127 struct hlist_head *hhd;
1129 key = ftrace_hash_key(hash, ip);
1130 hhd = &hash->buckets[key];
1132 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1133 if (entry->ip == ip)
1140 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1141 * @hash: The hash to look at
1142 * @ip: The instruction pointer to test
1144 * Search a given @hash to see if a given instruction pointer (@ip)
1147 * Returns the entry that holds the @ip if found. NULL otherwise.
1149 struct ftrace_func_entry *
1150 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1152 if (ftrace_hash_empty(hash))
1155 return __ftrace_lookup_ip(hash, ip);
1158 static void __add_hash_entry(struct ftrace_hash *hash,
1159 struct ftrace_func_entry *entry)
1161 struct hlist_head *hhd;
1164 key = ftrace_hash_key(hash, entry->ip);
1165 hhd = &hash->buckets[key];
1166 hlist_add_head(&entry->hlist, hhd);
1170 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1172 struct ftrace_func_entry *entry;
1174 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1179 __add_hash_entry(hash, entry);
1185 free_hash_entry(struct ftrace_hash *hash,
1186 struct ftrace_func_entry *entry)
1188 hlist_del(&entry->hlist);
1194 remove_hash_entry(struct ftrace_hash *hash,
1195 struct ftrace_func_entry *entry)
1197 hlist_del_rcu(&entry->hlist);
1201 static void ftrace_hash_clear(struct ftrace_hash *hash)
1203 struct hlist_head *hhd;
1204 struct hlist_node *tn;
1205 struct ftrace_func_entry *entry;
1206 int size = 1 << hash->size_bits;
1212 for (i = 0; i < size; i++) {
1213 hhd = &hash->buckets[i];
1214 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1215 free_hash_entry(hash, entry);
1217 FTRACE_WARN_ON(hash->count);
1220 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1222 list_del(&ftrace_mod->list);
1223 kfree(ftrace_mod->module);
1224 kfree(ftrace_mod->func);
1228 static void clear_ftrace_mod_list(struct list_head *head)
1230 struct ftrace_mod_load *p, *n;
1232 /* stack tracer isn't supported yet */
1236 mutex_lock(&ftrace_lock);
1237 list_for_each_entry_safe(p, n, head, list)
1239 mutex_unlock(&ftrace_lock);
1242 static void free_ftrace_hash(struct ftrace_hash *hash)
1244 if (!hash || hash == EMPTY_HASH)
1246 ftrace_hash_clear(hash);
1247 kfree(hash->buckets);
1251 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1253 struct ftrace_hash *hash;
1255 hash = container_of(rcu, struct ftrace_hash, rcu);
1256 free_ftrace_hash(hash);
1259 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1261 if (!hash || hash == EMPTY_HASH)
1263 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
1266 void ftrace_free_filter(struct ftrace_ops *ops)
1268 ftrace_ops_init(ops);
1269 free_ftrace_hash(ops->func_hash->filter_hash);
1270 free_ftrace_hash(ops->func_hash->notrace_hash);
1273 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1275 struct ftrace_hash *hash;
1278 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1282 size = 1 << size_bits;
1283 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1285 if (!hash->buckets) {
1290 hash->size_bits = size_bits;
1296 static int ftrace_add_mod(struct trace_array *tr,
1297 const char *func, const char *module,
1300 struct ftrace_mod_load *ftrace_mod;
1301 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1303 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1307 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1308 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1309 ftrace_mod->enable = enable;
1311 if (!ftrace_mod->func || !ftrace_mod->module)
1314 list_add(&ftrace_mod->list, mod_head);
1319 free_ftrace_mod(ftrace_mod);
1324 static struct ftrace_hash *
1325 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1327 struct ftrace_func_entry *entry;
1328 struct ftrace_hash *new_hash;
1333 new_hash = alloc_ftrace_hash(size_bits);
1338 new_hash->flags = hash->flags;
1341 if (ftrace_hash_empty(hash))
1344 size = 1 << hash->size_bits;
1345 for (i = 0; i < size; i++) {
1346 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1347 ret = add_hash_entry(new_hash, entry->ip);
1353 FTRACE_WARN_ON(new_hash->count != hash->count);
1358 free_ftrace_hash(new_hash);
1363 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1365 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1367 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1368 struct ftrace_hash *new_hash);
1370 static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
1372 struct ftrace_func_entry *entry;
1373 struct ftrace_hash *new_hash;
1374 struct hlist_head *hhd;
1375 struct hlist_node *tn;
1380 * Make the hash size about 1/2 the # found
1382 for (size /= 2; size; size >>= 1)
1385 /* Don't allocate too much */
1386 if (bits > FTRACE_HASH_MAX_BITS)
1387 bits = FTRACE_HASH_MAX_BITS;
1389 new_hash = alloc_ftrace_hash(bits);
1393 new_hash->flags = src->flags;
1395 size = 1 << src->size_bits;
1396 for (i = 0; i < size; i++) {
1397 hhd = &src->buckets[i];
1398 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1399 remove_hash_entry(src, entry);
1400 __add_hash_entry(new_hash, entry);
1406 static struct ftrace_hash *
1407 __ftrace_hash_move(struct ftrace_hash *src)
1409 int size = src->count;
1412 * If the new source is empty, just return the empty_hash.
1414 if (ftrace_hash_empty(src))
1417 return dup_hash(src, size);
1421 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1422 struct ftrace_hash **dst, struct ftrace_hash *src)
1424 struct ftrace_hash *new_hash;
1427 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1428 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1431 new_hash = __ftrace_hash_move(src);
1435 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1437 /* IPMODIFY should be updated only when filter_hash updating */
1438 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1440 free_ftrace_hash(new_hash);
1446 * Remove the current set, update the hash and add
1449 ftrace_hash_rec_disable_modify(ops, enable);
1451 rcu_assign_pointer(*dst, new_hash);
1453 ftrace_hash_rec_enable_modify(ops, enable);
1458 static bool hash_contains_ip(unsigned long ip,
1459 struct ftrace_ops_hash *hash)
1462 * The function record is a match if it exists in the filter
1463 * hash and not in the notrace hash. Note, an emty hash is
1464 * considered a match for the filter hash, but an empty
1465 * notrace hash is considered not in the notrace hash.
1467 return (ftrace_hash_empty(hash->filter_hash) ||
1468 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1469 (ftrace_hash_empty(hash->notrace_hash) ||
1470 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1474 * Test the hashes for this ops to see if we want to call
1475 * the ops->func or not.
1477 * It's a match if the ip is in the ops->filter_hash or
1478 * the filter_hash does not exist or is empty,
1480 * the ip is not in the ops->notrace_hash.
1482 * This needs to be called with preemption disabled as
1483 * the hashes are freed with call_rcu().
1486 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1488 struct ftrace_ops_hash hash;
1491 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1493 * There's a small race when adding ops that the ftrace handler
1494 * that wants regs, may be called without them. We can not
1495 * allow that handler to be called if regs is NULL.
1497 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1501 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1502 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1504 if (hash_contains_ip(ip, &hash))
1513 * This is a double for. Do not use 'break' to break out of the loop,
1514 * you must use a goto.
1516 #define do_for_each_ftrace_rec(pg, rec) \
1517 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1519 for (_____i = 0; _____i < pg->index; _____i++) { \
1520 rec = &pg->records[_____i];
1522 #define while_for_each_ftrace_rec() \
1527 static int ftrace_cmp_recs(const void *a, const void *b)
1529 const struct dyn_ftrace *key = a;
1530 const struct dyn_ftrace *rec = b;
1532 if (key->flags < rec->ip)
1534 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1539 static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
1541 struct ftrace_page *pg;
1542 struct dyn_ftrace *rec = NULL;
1543 struct dyn_ftrace key;
1546 key.flags = end; /* overload flags, as it is unsigned long */
1548 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1549 if (end < pg->records[0].ip ||
1550 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1552 rec = bsearch(&key, pg->records, pg->index,
1553 sizeof(struct dyn_ftrace),
1560 * ftrace_location_range - return the first address of a traced location
1561 * if it touches the given ip range
1562 * @start: start of range to search.
1563 * @end: end of range to search (inclusive). @end points to the last byte
1566 * Returns rec->ip if the related ftrace location is a least partly within
1567 * the given address range. That is, the first address of the instruction
1568 * that is either a NOP or call to the function tracer. It checks the ftrace
1569 * internal tables to determine if the address belongs or not.
1571 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1573 struct dyn_ftrace *rec;
1575 rec = lookup_rec(start, end);
1583 * ftrace_location - return true if the ip giving is a traced location
1584 * @ip: the instruction pointer to check
1586 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1587 * That is, the instruction that is either a NOP or call to
1588 * the function tracer. It checks the ftrace internal tables to
1589 * determine if the address belongs or not.
1591 unsigned long ftrace_location(unsigned long ip)
1593 return ftrace_location_range(ip, ip);
1597 * ftrace_text_reserved - return true if range contains an ftrace location
1598 * @start: start of range to search
1599 * @end: end of range to search (inclusive). @end points to the last byte to check.
1601 * Returns 1 if @start and @end contains a ftrace location.
1602 * That is, the instruction that is either a NOP or call to
1603 * the function tracer. It checks the ftrace internal tables to
1604 * determine if the address belongs or not.
1606 int ftrace_text_reserved(const void *start, const void *end)
1610 ret = ftrace_location_range((unsigned long)start,
1611 (unsigned long)end);
1616 /* Test if ops registered to this rec needs regs */
1617 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1619 struct ftrace_ops *ops;
1620 bool keep_regs = false;
1622 for (ops = ftrace_ops_list;
1623 ops != &ftrace_list_end; ops = ops->next) {
1624 /* pass rec in as regs to have non-NULL val */
1625 if (ftrace_ops_test(ops, rec->ip, rec)) {
1626 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1636 static struct ftrace_ops *
1637 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1638 static struct ftrace_ops *
1639 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1641 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1645 struct ftrace_hash *hash;
1646 struct ftrace_hash *other_hash;
1647 struct ftrace_page *pg;
1648 struct dyn_ftrace *rec;
1649 bool update = false;
1653 /* Only update if the ops has been registered */
1654 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1658 * In the filter_hash case:
1659 * If the count is zero, we update all records.
1660 * Otherwise we just update the items in the hash.
1662 * In the notrace_hash case:
1663 * We enable the update in the hash.
1664 * As disabling notrace means enabling the tracing,
1665 * and enabling notrace means disabling, the inc variable
1669 hash = ops->func_hash->filter_hash;
1670 other_hash = ops->func_hash->notrace_hash;
1671 if (ftrace_hash_empty(hash))
1675 hash = ops->func_hash->notrace_hash;
1676 other_hash = ops->func_hash->filter_hash;
1678 * If the notrace hash has no items,
1679 * then there's nothing to do.
1681 if (ftrace_hash_empty(hash))
1685 do_for_each_ftrace_rec(pg, rec) {
1686 int in_other_hash = 0;
1690 if (rec->flags & FTRACE_FL_DISABLED)
1695 * Only the filter_hash affects all records.
1696 * Update if the record is not in the notrace hash.
1698 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1701 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1702 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1705 * If filter_hash is set, we want to match all functions
1706 * that are in the hash but not in the other hash.
1708 * If filter_hash is not set, then we are decrementing.
1709 * That means we match anything that is in the hash
1710 * and also in the other_hash. That is, we need to turn
1711 * off functions in the other hash because they are disabled
1714 if (filter_hash && in_hash && !in_other_hash)
1716 else if (!filter_hash && in_hash &&
1717 (in_other_hash || ftrace_hash_empty(other_hash)))
1725 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1728 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1729 rec->flags |= FTRACE_FL_DIRECT;
1732 * If there's only a single callback registered to a
1733 * function, and the ops has a trampoline registered
1734 * for it, then we can call it directly.
1736 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1737 rec->flags |= FTRACE_FL_TRAMP;
1740 * If we are adding another function callback
1741 * to this function, and the previous had a
1742 * custom trampoline in use, then we need to go
1743 * back to the default trampoline.
1745 rec->flags &= ~FTRACE_FL_TRAMP;
1748 * If any ops wants regs saved for this function
1749 * then all ops will get saved regs.
1751 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1752 rec->flags |= FTRACE_FL_REGS;
1754 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1759 * Only the internal direct_ops should have the
1760 * DIRECT flag set. Thus, if it is removing a
1761 * function, then that function should no longer
1764 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1765 rec->flags &= ~FTRACE_FL_DIRECT;
1768 * If the rec had REGS enabled and the ops that is
1769 * being removed had REGS set, then see if there is
1770 * still any ops for this record that wants regs.
1771 * If not, we can stop recording them.
1773 if (ftrace_rec_count(rec) > 0 &&
1774 rec->flags & FTRACE_FL_REGS &&
1775 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1776 if (!test_rec_ops_needs_regs(rec))
1777 rec->flags &= ~FTRACE_FL_REGS;
1781 * The TRAMP needs to be set only if rec count
1782 * is decremented to one, and the ops that is
1783 * left has a trampoline. As TRAMP can only be
1784 * enabled if there is only a single ops attached
1787 if (ftrace_rec_count(rec) == 1 &&
1788 ftrace_find_tramp_ops_any(rec))
1789 rec->flags |= FTRACE_FL_TRAMP;
1791 rec->flags &= ~FTRACE_FL_TRAMP;
1794 * flags will be cleared in ftrace_check_record()
1795 * if rec count is zero.
1800 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1801 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
1803 /* Shortcut, if we handled all records, we are done. */
1804 if (!all && count == hash->count)
1806 } while_for_each_ftrace_rec();
1811 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1814 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1817 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1820 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1823 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1824 int filter_hash, int inc)
1826 struct ftrace_ops *op;
1828 __ftrace_hash_rec_update(ops, filter_hash, inc);
1830 if (ops->func_hash != &global_ops.local_hash)
1834 * If the ops shares the global_ops hash, then we need to update
1835 * all ops that are enabled and use this hash.
1837 do_for_each_ftrace_op(op, ftrace_ops_list) {
1841 if (op->func_hash == &global_ops.local_hash)
1842 __ftrace_hash_rec_update(op, filter_hash, inc);
1843 } while_for_each_ftrace_op(op);
1846 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1849 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1852 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1855 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1859 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1860 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1861 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1862 * Note that old_hash and new_hash has below meanings
1863 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1864 * - If the hash is EMPTY_HASH, it hits nothing
1865 * - Anything else hits the recs which match the hash entries.
1867 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1868 struct ftrace_hash *old_hash,
1869 struct ftrace_hash *new_hash)
1871 struct ftrace_page *pg;
1872 struct dyn_ftrace *rec, *end = NULL;
1875 /* Only update if the ops has been registered */
1876 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1879 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1883 * Since the IPMODIFY is a very address sensitive action, we do not
1884 * allow ftrace_ops to set all functions to new hash.
1886 if (!new_hash || !old_hash)
1889 /* Update rec->flags */
1890 do_for_each_ftrace_rec(pg, rec) {
1892 if (rec->flags & FTRACE_FL_DISABLED)
1895 /* We need to update only differences of filter_hash */
1896 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1897 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1898 if (in_old == in_new)
1902 /* New entries must ensure no others are using it */
1903 if (rec->flags & FTRACE_FL_IPMODIFY)
1905 rec->flags |= FTRACE_FL_IPMODIFY;
1906 } else /* Removed entry */
1907 rec->flags &= ~FTRACE_FL_IPMODIFY;
1908 } while_for_each_ftrace_rec();
1915 /* Roll back what we did above */
1916 do_for_each_ftrace_rec(pg, rec) {
1918 if (rec->flags & FTRACE_FL_DISABLED)
1924 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1925 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1926 if (in_old == in_new)
1930 rec->flags &= ~FTRACE_FL_IPMODIFY;
1932 rec->flags |= FTRACE_FL_IPMODIFY;
1933 } while_for_each_ftrace_rec();
1939 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1941 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1943 if (ftrace_hash_empty(hash))
1946 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1949 /* Disabling always succeeds */
1950 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1952 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1954 if (ftrace_hash_empty(hash))
1957 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1960 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1961 struct ftrace_hash *new_hash)
1963 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1965 if (ftrace_hash_empty(old_hash))
1968 if (ftrace_hash_empty(new_hash))
1971 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1974 static void print_ip_ins(const char *fmt, const unsigned char *p)
1978 printk(KERN_CONT "%s", fmt);
1980 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1981 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1984 enum ftrace_bug_type ftrace_bug_type;
1985 const void *ftrace_expected;
1987 static void print_bug_type(void)
1989 switch (ftrace_bug_type) {
1990 case FTRACE_BUG_UNKNOWN:
1992 case FTRACE_BUG_INIT:
1993 pr_info("Initializing ftrace call sites\n");
1995 case FTRACE_BUG_NOP:
1996 pr_info("Setting ftrace call site to NOP\n");
1998 case FTRACE_BUG_CALL:
1999 pr_info("Setting ftrace call site to call ftrace function\n");
2001 case FTRACE_BUG_UPDATE:
2002 pr_info("Updating ftrace call site to call a different ftrace function\n");
2008 * ftrace_bug - report and shutdown function tracer
2009 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2010 * @rec: The record that failed
2012 * The arch code that enables or disables the function tracing
2013 * can call ftrace_bug() when it has detected a problem in
2014 * modifying the code. @failed should be one of either:
2015 * EFAULT - if the problem happens on reading the @ip address
2016 * EINVAL - if what is read at @ip is not what was expected
2017 * EPERM - if the problem happens on writing to the @ip address
2019 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2021 unsigned long ip = rec ? rec->ip : 0;
2025 FTRACE_WARN_ON_ONCE(1);
2026 pr_info("ftrace faulted on modifying ");
2030 FTRACE_WARN_ON_ONCE(1);
2031 pr_info("ftrace failed to modify ");
2033 print_ip_ins(" actual: ", (unsigned char *)ip);
2035 if (ftrace_expected) {
2036 print_ip_ins(" expected: ", ftrace_expected);
2041 FTRACE_WARN_ON_ONCE(1);
2042 pr_info("ftrace faulted on writing ");
2046 FTRACE_WARN_ON_ONCE(1);
2047 pr_info("ftrace faulted on unknown error ");
2052 struct ftrace_ops *ops = NULL;
2054 pr_info("ftrace record flags: %lx\n", rec->flags);
2055 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2056 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2057 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2058 ops = ftrace_find_tramp_ops_any(rec);
2061 pr_cont("\ttramp: %pS (%pS)",
2062 (void *)ops->trampoline,
2064 ops = ftrace_find_tramp_ops_next(rec, ops);
2067 pr_cont("\ttramp: ERROR!");
2070 ip = ftrace_get_addr_curr(rec);
2071 pr_cont("\n expected tramp: %lx\n", ip);
2075 static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
2077 unsigned long flag = 0UL;
2079 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2081 if (rec->flags & FTRACE_FL_DISABLED)
2082 return FTRACE_UPDATE_IGNORE;
2085 * If we are updating calls:
2087 * If the record has a ref count, then we need to enable it
2088 * because someone is using it.
2090 * Otherwise we make sure its disabled.
2092 * If we are disabling calls, then disable all records that
2095 if (enable && ftrace_rec_count(rec))
2096 flag = FTRACE_FL_ENABLED;
2099 * If enabling and the REGS flag does not match the REGS_EN, or
2100 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2101 * this record. Set flags to fail the compare against ENABLED.
2102 * Same for direct calls.
2105 if (!(rec->flags & FTRACE_FL_REGS) !=
2106 !(rec->flags & FTRACE_FL_REGS_EN))
2107 flag |= FTRACE_FL_REGS;
2109 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2110 !(rec->flags & FTRACE_FL_TRAMP_EN))
2111 flag |= FTRACE_FL_TRAMP;
2114 * Direct calls are special, as count matters.
2115 * We must test the record for direct, if the
2116 * DIRECT and DIRECT_EN do not match, but only
2117 * if the count is 1. That's because, if the
2118 * count is something other than one, we do not
2119 * want the direct enabled (it will be done via the
2120 * direct helper). But if DIRECT_EN is set, and
2121 * the count is not one, we need to clear it.
2123 if (ftrace_rec_count(rec) == 1) {
2124 if (!(rec->flags & FTRACE_FL_DIRECT) !=
2125 !(rec->flags & FTRACE_FL_DIRECT_EN))
2126 flag |= FTRACE_FL_DIRECT;
2127 } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
2128 flag |= FTRACE_FL_DIRECT;
2132 /* If the state of this record hasn't changed, then do nothing */
2133 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2134 return FTRACE_UPDATE_IGNORE;
2137 /* Save off if rec is being enabled (for return value) */
2138 flag ^= rec->flags & FTRACE_FL_ENABLED;
2141 rec->flags |= FTRACE_FL_ENABLED;
2142 if (flag & FTRACE_FL_REGS) {
2143 if (rec->flags & FTRACE_FL_REGS)
2144 rec->flags |= FTRACE_FL_REGS_EN;
2146 rec->flags &= ~FTRACE_FL_REGS_EN;
2148 if (flag & FTRACE_FL_TRAMP) {
2149 if (rec->flags & FTRACE_FL_TRAMP)
2150 rec->flags |= FTRACE_FL_TRAMP_EN;
2152 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2154 if (flag & FTRACE_FL_DIRECT) {
2156 * If there's only one user (direct_ops helper)
2157 * then we can call the direct function
2158 * directly (no ftrace trampoline).
2160 if (ftrace_rec_count(rec) == 1) {
2161 if (rec->flags & FTRACE_FL_DIRECT)
2162 rec->flags |= FTRACE_FL_DIRECT_EN;
2164 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2167 * Can only call directly if there's
2168 * only one callback to the function.
2170 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2176 * If this record is being updated from a nop, then
2177 * return UPDATE_MAKE_CALL.
2179 * return UPDATE_MODIFY_CALL to tell the caller to convert
2180 * from the save regs, to a non-save regs function or
2181 * vice versa, or from a trampoline call.
2183 if (flag & FTRACE_FL_ENABLED) {
2184 ftrace_bug_type = FTRACE_BUG_CALL;
2185 return FTRACE_UPDATE_MAKE_CALL;
2188 ftrace_bug_type = FTRACE_BUG_UPDATE;
2189 return FTRACE_UPDATE_MODIFY_CALL;
2193 /* If there's no more users, clear all flags */
2194 if (!ftrace_rec_count(rec))
2198 * Just disable the record, but keep the ops TRAMP
2199 * and REGS states. The _EN flags must be disabled though.
2201 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2202 FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN);
2205 ftrace_bug_type = FTRACE_BUG_NOP;
2206 return FTRACE_UPDATE_MAKE_NOP;
2210 * ftrace_update_record, set a record that now is tracing or not
2211 * @rec: the record to update
2212 * @enable: set to true if the record is tracing, false to force disable
2214 * The records that represent all functions that can be traced need
2215 * to be updated when tracing has been enabled.
2217 int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
2219 return ftrace_check_record(rec, enable, true);
2223 * ftrace_test_record, check if the record has been enabled or not
2224 * @rec: the record to test
2225 * @enable: set to true to check if enabled, false if it is disabled
2227 * The arch code may need to test if a record is already set to
2228 * tracing to determine how to modify the function code that it
2231 int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
2233 return ftrace_check_record(rec, enable, false);
2236 static struct ftrace_ops *
2237 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2239 struct ftrace_ops *op;
2240 unsigned long ip = rec->ip;
2242 do_for_each_ftrace_op(op, ftrace_ops_list) {
2244 if (!op->trampoline)
2247 if (hash_contains_ip(ip, op->func_hash))
2249 } while_for_each_ftrace_op(op);
2254 static struct ftrace_ops *
2255 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2256 struct ftrace_ops *op)
2258 unsigned long ip = rec->ip;
2260 while_for_each_ftrace_op(op) {
2262 if (!op->trampoline)
2265 if (hash_contains_ip(ip, op->func_hash))
2272 static struct ftrace_ops *
2273 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2275 struct ftrace_ops *op;
2276 unsigned long ip = rec->ip;
2279 * Need to check removed ops first.
2280 * If they are being removed, and this rec has a tramp,
2281 * and this rec is in the ops list, then it would be the
2282 * one with the tramp.
2285 if (hash_contains_ip(ip, &removed_ops->old_hash))
2290 * Need to find the current trampoline for a rec.
2291 * Now, a trampoline is only attached to a rec if there
2292 * was a single 'ops' attached to it. But this can be called
2293 * when we are adding another op to the rec or removing the
2294 * current one. Thus, if the op is being added, we can
2295 * ignore it because it hasn't attached itself to the rec
2298 * If an ops is being modified (hooking to different functions)
2299 * then we don't care about the new functions that are being
2300 * added, just the old ones (that are probably being removed).
2302 * If we are adding an ops to a function that already is using
2303 * a trampoline, it needs to be removed (trampolines are only
2304 * for single ops connected), then an ops that is not being
2305 * modified also needs to be checked.
2307 do_for_each_ftrace_op(op, ftrace_ops_list) {
2309 if (!op->trampoline)
2313 * If the ops is being added, it hasn't gotten to
2314 * the point to be removed from this tree yet.
2316 if (op->flags & FTRACE_OPS_FL_ADDING)
2321 * If the ops is being modified and is in the old
2322 * hash, then it is probably being removed from this
2325 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2326 hash_contains_ip(ip, &op->old_hash))
2329 * If the ops is not being added or modified, and it's
2330 * in its normal filter hash, then this must be the one
2333 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2334 hash_contains_ip(ip, op->func_hash))
2337 } while_for_each_ftrace_op(op);
2342 static struct ftrace_ops *
2343 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2345 struct ftrace_ops *op;
2346 unsigned long ip = rec->ip;
2348 do_for_each_ftrace_op(op, ftrace_ops_list) {
2349 /* pass rec in as regs to have non-NULL val */
2350 if (hash_contains_ip(ip, op->func_hash))
2352 } while_for_each_ftrace_op(op);
2357 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2358 /* Protected by rcu_tasks for reading, and direct_mutex for writing */
2359 static struct ftrace_hash *direct_functions = EMPTY_HASH;
2360 static DEFINE_MUTEX(direct_mutex);
2361 int ftrace_direct_func_count;
2364 * Search the direct_functions hash to see if the given instruction pointer
2365 * has a direct caller attached to it.
2367 unsigned long ftrace_find_rec_direct(unsigned long ip)
2369 struct ftrace_func_entry *entry;
2371 entry = __ftrace_lookup_ip(direct_functions, ip);
2375 return entry->direct;
2378 static void call_direct_funcs(unsigned long ip, unsigned long pip,
2379 struct ftrace_ops *ops, struct pt_regs *regs)
2383 addr = ftrace_find_rec_direct(ip);
2387 arch_ftrace_set_direct_caller(regs, addr);
2390 struct ftrace_ops direct_ops = {
2391 .func = call_direct_funcs,
2392 .flags = FTRACE_OPS_FL_IPMODIFY | FTRACE_OPS_FL_RECURSION_SAFE
2393 | FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS
2394 | FTRACE_OPS_FL_PERMANENT,
2396 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2399 * ftrace_get_addr_new - Get the call address to set to
2400 * @rec: The ftrace record descriptor
2402 * If the record has the FTRACE_FL_REGS set, that means that it
2403 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2404 * is not not set, then it wants to convert to the normal callback.
2406 * Returns the address of the trampoline to set to
2408 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2410 struct ftrace_ops *ops;
2413 if ((rec->flags & FTRACE_FL_DIRECT) &&
2414 (ftrace_rec_count(rec) == 1)) {
2415 addr = ftrace_find_rec_direct(rec->ip);
2421 /* Trampolines take precedence over regs */
2422 if (rec->flags & FTRACE_FL_TRAMP) {
2423 ops = ftrace_find_tramp_ops_new(rec);
2424 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2425 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2426 (void *)rec->ip, (void *)rec->ip, rec->flags);
2427 /* Ftrace is shutting down, return anything */
2428 return (unsigned long)FTRACE_ADDR;
2430 return ops->trampoline;
2433 if (rec->flags & FTRACE_FL_REGS)
2434 return (unsigned long)FTRACE_REGS_ADDR;
2436 return (unsigned long)FTRACE_ADDR;
2440 * ftrace_get_addr_curr - Get the call address that is already there
2441 * @rec: The ftrace record descriptor
2443 * The FTRACE_FL_REGS_EN is set when the record already points to
2444 * a function that saves all the regs. Basically the '_EN' version
2445 * represents the current state of the function.
2447 * Returns the address of the trampoline that is currently being called
2449 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2451 struct ftrace_ops *ops;
2454 /* Direct calls take precedence over trampolines */
2455 if (rec->flags & FTRACE_FL_DIRECT_EN) {
2456 addr = ftrace_find_rec_direct(rec->ip);
2462 /* Trampolines take precedence over regs */
2463 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2464 ops = ftrace_find_tramp_ops_curr(rec);
2465 if (FTRACE_WARN_ON(!ops)) {
2466 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2467 (void *)rec->ip, (void *)rec->ip);
2468 /* Ftrace is shutting down, return anything */
2469 return (unsigned long)FTRACE_ADDR;
2471 return ops->trampoline;
2474 if (rec->flags & FTRACE_FL_REGS_EN)
2475 return (unsigned long)FTRACE_REGS_ADDR;
2477 return (unsigned long)FTRACE_ADDR;
2481 __ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
2483 unsigned long ftrace_old_addr;
2484 unsigned long ftrace_addr;
2487 ftrace_addr = ftrace_get_addr_new(rec);
2489 /* This needs to be done before we call ftrace_update_record */
2490 ftrace_old_addr = ftrace_get_addr_curr(rec);
2492 ret = ftrace_update_record(rec, enable);
2494 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2497 case FTRACE_UPDATE_IGNORE:
2500 case FTRACE_UPDATE_MAKE_CALL:
2501 ftrace_bug_type = FTRACE_BUG_CALL;
2502 return ftrace_make_call(rec, ftrace_addr);
2504 case FTRACE_UPDATE_MAKE_NOP:
2505 ftrace_bug_type = FTRACE_BUG_NOP;
2506 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2508 case FTRACE_UPDATE_MODIFY_CALL:
2509 ftrace_bug_type = FTRACE_BUG_UPDATE;
2510 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2513 return -1; /* unknown ftrace bug */
2516 void __weak ftrace_replace_code(int mod_flags)
2518 struct dyn_ftrace *rec;
2519 struct ftrace_page *pg;
2520 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2521 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
2524 if (unlikely(ftrace_disabled))
2527 do_for_each_ftrace_rec(pg, rec) {
2529 if (rec->flags & FTRACE_FL_DISABLED)
2532 failed = __ftrace_replace_code(rec, enable);
2534 ftrace_bug(failed, rec);
2535 /* Stop processing */
2540 } while_for_each_ftrace_rec();
2543 struct ftrace_rec_iter {
2544 struct ftrace_page *pg;
2549 * ftrace_rec_iter_start, start up iterating over traced functions
2551 * Returns an iterator handle that is used to iterate over all
2552 * the records that represent address locations where functions
2555 * May return NULL if no records are available.
2557 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2560 * We only use a single iterator.
2561 * Protected by the ftrace_lock mutex.
2563 static struct ftrace_rec_iter ftrace_rec_iter;
2564 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2566 iter->pg = ftrace_pages_start;
2569 /* Could have empty pages */
2570 while (iter->pg && !iter->pg->index)
2571 iter->pg = iter->pg->next;
2580 * ftrace_rec_iter_next, get the next record to process.
2581 * @iter: The handle to the iterator.
2583 * Returns the next iterator after the given iterator @iter.
2585 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2589 if (iter->index >= iter->pg->index) {
2590 iter->pg = iter->pg->next;
2593 /* Could have empty pages */
2594 while (iter->pg && !iter->pg->index)
2595 iter->pg = iter->pg->next;
2605 * ftrace_rec_iter_record, get the record at the iterator location
2606 * @iter: The current iterator location
2608 * Returns the record that the current @iter is at.
2610 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2612 return &iter->pg->records[iter->index];
2616 ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
2620 if (unlikely(ftrace_disabled))
2623 ret = ftrace_init_nop(mod, rec);
2625 ftrace_bug_type = FTRACE_BUG_INIT;
2626 ftrace_bug(ret, rec);
2633 * archs can override this function if they must do something
2634 * before the modifying code is performed.
2636 int __weak ftrace_arch_code_modify_prepare(void)
2642 * archs can override this function if they must do something
2643 * after the modifying code is performed.
2645 int __weak ftrace_arch_code_modify_post_process(void)
2650 void ftrace_modify_all_code(int command)
2652 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2656 if (command & FTRACE_MAY_SLEEP)
2657 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2660 * If the ftrace_caller calls a ftrace_ops func directly,
2661 * we need to make sure that it only traces functions it
2662 * expects to trace. When doing the switch of functions,
2663 * we need to update to the ftrace_ops_list_func first
2664 * before the transition between old and new calls are set,
2665 * as the ftrace_ops_list_func will check the ops hashes
2666 * to make sure the ops are having the right functions
2670 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2671 if (FTRACE_WARN_ON(err))
2675 if (command & FTRACE_UPDATE_CALLS)
2676 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
2677 else if (command & FTRACE_DISABLE_CALLS)
2678 ftrace_replace_code(mod_flags);
2680 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2681 function_trace_op = set_function_trace_op;
2683 /* If irqs are disabled, we are in stop machine */
2684 if (!irqs_disabled())
2685 smp_call_function(ftrace_sync_ipi, NULL, 1);
2686 err = ftrace_update_ftrace_func(ftrace_trace_function);
2687 if (FTRACE_WARN_ON(err))
2691 if (command & FTRACE_START_FUNC_RET)
2692 err = ftrace_enable_ftrace_graph_caller();
2693 else if (command & FTRACE_STOP_FUNC_RET)
2694 err = ftrace_disable_ftrace_graph_caller();
2695 FTRACE_WARN_ON(err);
2698 static int __ftrace_modify_code(void *data)
2700 int *command = data;
2702 ftrace_modify_all_code(*command);
2708 * ftrace_run_stop_machine, go back to the stop machine method
2709 * @command: The command to tell ftrace what to do
2711 * If an arch needs to fall back to the stop machine method, the
2712 * it can call this function.
2714 void ftrace_run_stop_machine(int command)
2716 stop_machine(__ftrace_modify_code, &command, NULL);
2720 * arch_ftrace_update_code, modify the code to trace or not trace
2721 * @command: The command that needs to be done
2723 * Archs can override this function if it does not need to
2724 * run stop_machine() to modify code.
2726 void __weak arch_ftrace_update_code(int command)
2728 ftrace_run_stop_machine(command);
2731 static void ftrace_run_update_code(int command)
2735 ret = ftrace_arch_code_modify_prepare();
2736 FTRACE_WARN_ON(ret);
2741 * By default we use stop_machine() to modify the code.
2742 * But archs can do what ever they want as long as it
2743 * is safe. The stop_machine() is the safest, but also
2744 * produces the most overhead.
2746 arch_ftrace_update_code(command);
2748 ret = ftrace_arch_code_modify_post_process();
2749 FTRACE_WARN_ON(ret);
2752 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2753 struct ftrace_ops_hash *old_hash)
2755 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2756 ops->old_hash.filter_hash = old_hash->filter_hash;
2757 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2758 ftrace_run_update_code(command);
2759 ops->old_hash.filter_hash = NULL;
2760 ops->old_hash.notrace_hash = NULL;
2761 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2764 static ftrace_func_t saved_ftrace_func;
2765 static int ftrace_start_up;
2767 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2771 static void ftrace_startup_enable(int command)
2773 if (saved_ftrace_func != ftrace_trace_function) {
2774 saved_ftrace_func = ftrace_trace_function;
2775 command |= FTRACE_UPDATE_TRACE_FUNC;
2778 if (!command || !ftrace_enabled)
2781 ftrace_run_update_code(command);
2784 static void ftrace_startup_all(int command)
2786 update_all_ops = true;
2787 ftrace_startup_enable(command);
2788 update_all_ops = false;
2791 int ftrace_startup(struct ftrace_ops *ops, int command)
2795 if (unlikely(ftrace_disabled))
2798 ret = __register_ftrace_function(ops);
2805 * Note that ftrace probes uses this to start up
2806 * and modify functions it will probe. But we still
2807 * set the ADDING flag for modification, as probes
2808 * do not have trampolines. If they add them in the
2809 * future, then the probes will need to distinguish
2810 * between adding and updating probes.
2812 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2814 ret = ftrace_hash_ipmodify_enable(ops);
2816 /* Rollback registration process */
2817 __unregister_ftrace_function(ops);
2819 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2823 if (ftrace_hash_rec_enable(ops, 1))
2824 command |= FTRACE_UPDATE_CALLS;
2826 ftrace_startup_enable(command);
2828 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2833 int ftrace_shutdown(struct ftrace_ops *ops, int command)
2837 if (unlikely(ftrace_disabled))
2840 ret = __unregister_ftrace_function(ops);
2846 * Just warn in case of unbalance, no need to kill ftrace, it's not
2847 * critical but the ftrace_call callers may be never nopped again after
2848 * further ftrace uses.
2850 WARN_ON_ONCE(ftrace_start_up < 0);
2852 /* Disabling ipmodify never fails */
2853 ftrace_hash_ipmodify_disable(ops);
2855 if (ftrace_hash_rec_disable(ops, 1))
2856 command |= FTRACE_UPDATE_CALLS;
2858 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2860 if (saved_ftrace_func != ftrace_trace_function) {
2861 saved_ftrace_func = ftrace_trace_function;
2862 command |= FTRACE_UPDATE_TRACE_FUNC;
2865 if (!command || !ftrace_enabled) {
2867 * If these are dynamic or per_cpu ops, they still
2868 * need their data freed. Since, function tracing is
2869 * not currently active, we can just free them
2870 * without synchronizing all CPUs.
2872 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2879 * If the ops uses a trampoline, then it needs to be
2880 * tested first on update.
2882 ops->flags |= FTRACE_OPS_FL_REMOVING;
2885 /* The trampoline logic checks the old hashes */
2886 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2887 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2889 ftrace_run_update_code(command);
2892 * If there's no more ops registered with ftrace, run a
2893 * sanity check to make sure all rec flags are cleared.
2895 if (rcu_dereference_protected(ftrace_ops_list,
2896 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2897 struct ftrace_page *pg;
2898 struct dyn_ftrace *rec;
2900 do_for_each_ftrace_rec(pg, rec) {
2901 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2902 pr_warn(" %pS flags:%lx\n",
2903 (void *)rec->ip, rec->flags);
2904 } while_for_each_ftrace_rec();
2907 ops->old_hash.filter_hash = NULL;
2908 ops->old_hash.notrace_hash = NULL;
2911 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2914 * Dynamic ops may be freed, we must make sure that all
2915 * callers are done before leaving this function.
2916 * The same goes for freeing the per_cpu data of the per_cpu
2919 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
2921 * We need to do a hard force of sched synchronization.
2922 * This is because we use preempt_disable() to do RCU, but
2923 * the function tracers can be called where RCU is not watching
2924 * (like before user_exit()). We can not rely on the RCU
2925 * infrastructure to do the synchronization, thus we must do it
2928 schedule_on_each_cpu(ftrace_sync);
2931 * When the kernel is preeptive, tasks can be preempted
2932 * while on a ftrace trampoline. Just scheduling a task on
2933 * a CPU is not good enough to flush them. Calling
2934 * synchornize_rcu_tasks() will wait for those tasks to
2935 * execute and either schedule voluntarily or enter user space.
2937 if (IS_ENABLED(CONFIG_PREEMPTION))
2938 synchronize_rcu_tasks();
2941 arch_ftrace_trampoline_free(ops);
2947 static void ftrace_startup_sysctl(void)
2951 if (unlikely(ftrace_disabled))
2954 /* Force update next time */
2955 saved_ftrace_func = NULL;
2956 /* ftrace_start_up is true if we want ftrace running */
2957 if (ftrace_start_up) {
2958 command = FTRACE_UPDATE_CALLS;
2959 if (ftrace_graph_active)
2960 command |= FTRACE_START_FUNC_RET;
2961 ftrace_startup_enable(command);
2965 static void ftrace_shutdown_sysctl(void)
2969 if (unlikely(ftrace_disabled))
2972 /* ftrace_start_up is true if ftrace is running */
2973 if (ftrace_start_up) {
2974 command = FTRACE_DISABLE_CALLS;
2975 if (ftrace_graph_active)
2976 command |= FTRACE_STOP_FUNC_RET;
2977 ftrace_run_update_code(command);
2981 static u64 ftrace_update_time;
2982 unsigned long ftrace_update_tot_cnt;
2983 unsigned long ftrace_number_of_pages;
2984 unsigned long ftrace_number_of_groups;
2986 static inline int ops_traces_mod(struct ftrace_ops *ops)
2989 * Filter_hash being empty will default to trace module.
2990 * But notrace hash requires a test of individual module functions.
2992 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2993 ftrace_hash_empty(ops->func_hash->notrace_hash);
2997 * Check if the current ops references the record.
2999 * If the ops traces all functions, then it was already accounted for.
3000 * If the ops does not trace the current record function, skip it.
3001 * If the ops ignores the function via notrace filter, skip it.
3004 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3006 /* If ops isn't enabled, ignore it */
3007 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
3010 /* If ops traces all then it includes this function */
3011 if (ops_traces_mod(ops))
3014 /* The function must be in the filter */
3015 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
3016 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
3019 /* If in notrace hash, we ignore it too */
3020 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
3026 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
3028 struct ftrace_page *pg;
3029 struct dyn_ftrace *p;
3031 unsigned long update_cnt = 0;
3032 unsigned long rec_flags = 0;
3035 start = ftrace_now(raw_smp_processor_id());
3038 * When a module is loaded, this function is called to convert
3039 * the calls to mcount in its text to nops, and also to create
3040 * an entry in the ftrace data. Now, if ftrace is activated
3041 * after this call, but before the module sets its text to
3042 * read-only, the modification of enabling ftrace can fail if
3043 * the read-only is done while ftrace is converting the calls.
3044 * To prevent this, the module's records are set as disabled
3045 * and will be enabled after the call to set the module's text
3049 rec_flags |= FTRACE_FL_DISABLED;
3051 for (pg = new_pgs; pg; pg = pg->next) {
3053 for (i = 0; i < pg->index; i++) {
3055 /* If something went wrong, bail without enabling anything */
3056 if (unlikely(ftrace_disabled))
3059 p = &pg->records[i];
3060 p->flags = rec_flags;
3063 * Do the initial record conversion from mcount jump
3064 * to the NOP instructions.
3066 if (!__is_defined(CC_USING_NOP_MCOUNT) &&
3067 !ftrace_nop_initialize(mod, p))
3074 stop = ftrace_now(raw_smp_processor_id());
3075 ftrace_update_time = stop - start;
3076 ftrace_update_tot_cnt += update_cnt;
3081 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3086 if (WARN_ON(!count))
3089 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3092 * We want to fill as much as possible. No more than a page
3095 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3099 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3102 /* if we can't allocate this size, try something smaller */
3109 ftrace_number_of_pages += 1 << order;
3110 ftrace_number_of_groups++;
3112 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3121 static struct ftrace_page *
3122 ftrace_allocate_pages(unsigned long num_to_init)
3124 struct ftrace_page *start_pg;
3125 struct ftrace_page *pg;
3132 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3137 * Try to allocate as much as possible in one continues
3138 * location that fills in all of the space. We want to
3139 * waste as little space as possible.
3142 cnt = ftrace_allocate_records(pg, num_to_init);
3150 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3162 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3163 free_pages((unsigned long)pg->records, order);
3164 start_pg = pg->next;
3167 ftrace_number_of_pages -= 1 << order;
3168 ftrace_number_of_groups--;
3170 pr_info("ftrace: FAILED to allocate memory for functions\n");
3174 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3176 struct ftrace_iterator {
3180 struct ftrace_page *pg;
3181 struct dyn_ftrace *func;
3182 struct ftrace_func_probe *probe;
3183 struct ftrace_func_entry *probe_entry;
3184 struct trace_parser parser;
3185 struct ftrace_hash *hash;
3186 struct ftrace_ops *ops;
3187 struct trace_array *tr;
3188 struct list_head *mod_list;
3195 t_probe_next(struct seq_file *m, loff_t *pos)
3197 struct ftrace_iterator *iter = m->private;
3198 struct trace_array *tr = iter->ops->private;
3199 struct list_head *func_probes;
3200 struct ftrace_hash *hash;
3201 struct list_head *next;
3202 struct hlist_node *hnd = NULL;
3203 struct hlist_head *hhd;
3212 func_probes = &tr->func_probes;
3213 if (list_empty(func_probes))
3217 next = func_probes->next;
3218 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3221 if (iter->probe_entry)
3222 hnd = &iter->probe_entry->hlist;
3224 hash = iter->probe->ops.func_hash->filter_hash;
3227 * A probe being registered may temporarily have an empty hash
3228 * and it's at the end of the func_probes list.
3230 if (!hash || hash == EMPTY_HASH)
3233 size = 1 << hash->size_bits;
3236 if (iter->pidx >= size) {
3237 if (iter->probe->list.next == func_probes)
3239 next = iter->probe->list.next;
3240 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3241 hash = iter->probe->ops.func_hash->filter_hash;
3242 size = 1 << hash->size_bits;
3246 hhd = &hash->buckets[iter->pidx];
3248 if (hlist_empty(hhd)) {
3264 if (WARN_ON_ONCE(!hnd))
3267 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3272 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3274 struct ftrace_iterator *iter = m->private;
3278 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3281 if (iter->mod_pos > *pos)
3285 iter->probe_entry = NULL;
3287 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3288 p = t_probe_next(m, &l);
3295 /* Only set this if we have an item */
3296 iter->flags |= FTRACE_ITER_PROBE;
3302 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3304 struct ftrace_func_entry *probe_entry;
3305 struct ftrace_probe_ops *probe_ops;
3306 struct ftrace_func_probe *probe;
3308 probe = iter->probe;
3309 probe_entry = iter->probe_entry;
3311 if (WARN_ON_ONCE(!probe || !probe_entry))
3314 probe_ops = probe->probe_ops;
3316 if (probe_ops->print)
3317 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3319 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3320 (void *)probe_ops->func);
3326 t_mod_next(struct seq_file *m, loff_t *pos)
3328 struct ftrace_iterator *iter = m->private;
3329 struct trace_array *tr = iter->tr;
3334 iter->mod_list = iter->mod_list->next;
3336 if (iter->mod_list == &tr->mod_trace ||
3337 iter->mod_list == &tr->mod_notrace) {
3338 iter->flags &= ~FTRACE_ITER_MOD;
3342 iter->mod_pos = *pos;
3347 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3349 struct ftrace_iterator *iter = m->private;
3353 if (iter->func_pos > *pos)
3356 iter->mod_pos = iter->func_pos;
3358 /* probes are only available if tr is set */
3362 for (l = 0; l <= (*pos - iter->func_pos); ) {
3363 p = t_mod_next(m, &l);
3368 iter->flags &= ~FTRACE_ITER_MOD;
3369 return t_probe_start(m, pos);
3372 /* Only set this if we have an item */
3373 iter->flags |= FTRACE_ITER_MOD;
3379 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3381 struct ftrace_mod_load *ftrace_mod;
3382 struct trace_array *tr = iter->tr;
3384 if (WARN_ON_ONCE(!iter->mod_list) ||
3385 iter->mod_list == &tr->mod_trace ||
3386 iter->mod_list == &tr->mod_notrace)
3389 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3391 if (ftrace_mod->func)
3392 seq_printf(m, "%s", ftrace_mod->func);
3396 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3402 t_func_next(struct seq_file *m, loff_t *pos)
3404 struct ftrace_iterator *iter = m->private;
3405 struct dyn_ftrace *rec = NULL;
3410 if (iter->idx >= iter->pg->index) {
3411 if (iter->pg->next) {
3412 iter->pg = iter->pg->next;
3417 rec = &iter->pg->records[iter->idx++];
3418 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3419 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3421 ((iter->flags & FTRACE_ITER_ENABLED) &&
3422 !(rec->flags & FTRACE_FL_ENABLED))) {
3432 iter->pos = iter->func_pos = *pos;
3439 t_next(struct seq_file *m, void *v, loff_t *pos)
3441 struct ftrace_iterator *iter = m->private;
3442 loff_t l = *pos; /* t_probe_start() must use original pos */
3445 if (unlikely(ftrace_disabled))
3448 if (iter->flags & FTRACE_ITER_PROBE)
3449 return t_probe_next(m, pos);
3451 if (iter->flags & FTRACE_ITER_MOD)
3452 return t_mod_next(m, pos);
3454 if (iter->flags & FTRACE_ITER_PRINTALL) {
3455 /* next must increment pos, and t_probe_start does not */
3457 return t_mod_start(m, &l);
3460 ret = t_func_next(m, pos);
3463 return t_mod_start(m, &l);
3468 static void reset_iter_read(struct ftrace_iterator *iter)
3472 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3475 static void *t_start(struct seq_file *m, loff_t *pos)
3477 struct ftrace_iterator *iter = m->private;
3481 mutex_lock(&ftrace_lock);
3483 if (unlikely(ftrace_disabled))
3487 * If an lseek was done, then reset and start from beginning.
3489 if (*pos < iter->pos)
3490 reset_iter_read(iter);
3493 * For set_ftrace_filter reading, if we have the filter
3494 * off, we can short cut and just print out that all
3495 * functions are enabled.
3497 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3498 ftrace_hash_empty(iter->hash)) {
3499 iter->func_pos = 1; /* Account for the message */
3501 return t_mod_start(m, pos);
3502 iter->flags |= FTRACE_ITER_PRINTALL;
3503 /* reset in case of seek/pread */
3504 iter->flags &= ~FTRACE_ITER_PROBE;
3508 if (iter->flags & FTRACE_ITER_MOD)
3509 return t_mod_start(m, pos);
3512 * Unfortunately, we need to restart at ftrace_pages_start
3513 * every time we let go of the ftrace_mutex. This is because
3514 * those pointers can change without the lock.
3516 iter->pg = ftrace_pages_start;
3518 for (l = 0; l <= *pos; ) {
3519 p = t_func_next(m, &l);
3525 return t_mod_start(m, pos);
3530 static void t_stop(struct seq_file *m, void *p)
3532 mutex_unlock(&ftrace_lock);
3536 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3541 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3542 struct dyn_ftrace *rec)
3546 ptr = arch_ftrace_trampoline_func(ops, rec);
3548 seq_printf(m, " ->%pS", ptr);
3551 static int t_show(struct seq_file *m, void *v)
3553 struct ftrace_iterator *iter = m->private;
3554 struct dyn_ftrace *rec;
3556 if (iter->flags & FTRACE_ITER_PROBE)
3557 return t_probe_show(m, iter);
3559 if (iter->flags & FTRACE_ITER_MOD)
3560 return t_mod_show(m, iter);
3562 if (iter->flags & FTRACE_ITER_PRINTALL) {
3563 if (iter->flags & FTRACE_ITER_NOTRACE)
3564 seq_puts(m, "#### no functions disabled ####\n");
3566 seq_puts(m, "#### all functions enabled ####\n");
3575 seq_printf(m, "%ps", (void *)rec->ip);
3576 if (iter->flags & FTRACE_ITER_ENABLED) {
3577 struct ftrace_ops *ops;
3579 seq_printf(m, " (%ld)%s%s%s",
3580 ftrace_rec_count(rec),
3581 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3582 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
3583 rec->flags & FTRACE_FL_DIRECT ? " D" : " ");
3584 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3585 ops = ftrace_find_tramp_ops_any(rec);
3588 seq_printf(m, "\ttramp: %pS (%pS)",
3589 (void *)ops->trampoline,
3591 add_trampoline_func(m, ops, rec);
3592 ops = ftrace_find_tramp_ops_next(rec, ops);
3595 seq_puts(m, "\ttramp: ERROR!");
3597 add_trampoline_func(m, NULL, rec);
3599 if (rec->flags & FTRACE_FL_DIRECT) {
3600 unsigned long direct;
3602 direct = ftrace_find_rec_direct(rec->ip);
3604 seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3613 static const struct seq_operations show_ftrace_seq_ops = {
3621 ftrace_avail_open(struct inode *inode, struct file *file)
3623 struct ftrace_iterator *iter;
3626 ret = security_locked_down(LOCKDOWN_TRACEFS);
3630 if (unlikely(ftrace_disabled))
3633 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3637 iter->pg = ftrace_pages_start;
3638 iter->ops = &global_ops;
3644 ftrace_enabled_open(struct inode *inode, struct file *file)
3646 struct ftrace_iterator *iter;
3649 * This shows us what functions are currently being
3650 * traced and by what. Not sure if we want lockdown
3651 * to hide such critical information for an admin.
3652 * Although, perhaps it can show information we don't
3653 * want people to see, but if something is tracing
3654 * something, we probably want to know about it.
3657 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3661 iter->pg = ftrace_pages_start;
3662 iter->flags = FTRACE_ITER_ENABLED;
3663 iter->ops = &global_ops;
3669 * ftrace_regex_open - initialize function tracer filter files
3670 * @ops: The ftrace_ops that hold the hash filters
3671 * @flag: The type of filter to process
3672 * @inode: The inode, usually passed in to your open routine
3673 * @file: The file, usually passed in to your open routine
3675 * ftrace_regex_open() initializes the filter files for the
3676 * @ops. Depending on @flag it may process the filter hash or
3677 * the notrace hash of @ops. With this called from the open
3678 * routine, you can use ftrace_filter_write() for the write
3679 * routine if @flag has FTRACE_ITER_FILTER set, or
3680 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3681 * tracing_lseek() should be used as the lseek routine, and
3682 * release must call ftrace_regex_release().
3685 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3686 struct inode *inode, struct file *file)
3688 struct ftrace_iterator *iter;
3689 struct ftrace_hash *hash;
3690 struct list_head *mod_head;
3691 struct trace_array *tr = ops->private;
3694 ftrace_ops_init(ops);
3696 if (unlikely(ftrace_disabled))
3699 if (tracing_check_open_get_tr(tr))
3702 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3706 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3713 mutex_lock(&ops->func_hash->regex_lock);
3715 if (flag & FTRACE_ITER_NOTRACE) {
3716 hash = ops->func_hash->notrace_hash;
3717 mod_head = tr ? &tr->mod_notrace : NULL;
3719 hash = ops->func_hash->filter_hash;
3720 mod_head = tr ? &tr->mod_trace : NULL;
3723 iter->mod_list = mod_head;
3725 if (file->f_mode & FMODE_WRITE) {
3726 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3728 if (file->f_flags & O_TRUNC) {
3729 iter->hash = alloc_ftrace_hash(size_bits);
3730 clear_ftrace_mod_list(mod_head);
3732 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3736 trace_parser_put(&iter->parser);
3744 if (file->f_mode & FMODE_READ) {
3745 iter->pg = ftrace_pages_start;
3747 ret = seq_open(file, &show_ftrace_seq_ops);
3749 struct seq_file *m = file->private_data;
3753 free_ftrace_hash(iter->hash);
3754 trace_parser_put(&iter->parser);
3757 file->private_data = iter;
3760 mutex_unlock(&ops->func_hash->regex_lock);
3766 trace_array_put(tr);
3773 ftrace_filter_open(struct inode *inode, struct file *file)
3775 struct ftrace_ops *ops = inode->i_private;
3777 /* Checks for tracefs lockdown */
3778 return ftrace_regex_open(ops,
3779 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3784 ftrace_notrace_open(struct inode *inode, struct file *file)
3786 struct ftrace_ops *ops = inode->i_private;
3788 /* Checks for tracefs lockdown */
3789 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3793 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3794 struct ftrace_glob {
3801 * If symbols in an architecture don't correspond exactly to the user-visible
3802 * name of what they represent, it is possible to define this function to
3803 * perform the necessary adjustments.
3805 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3810 static int ftrace_match(char *str, struct ftrace_glob *g)
3815 str = arch_ftrace_match_adjust(str, g->search);
3819 if (strcmp(str, g->search) == 0)
3822 case MATCH_FRONT_ONLY:
3823 if (strncmp(str, g->search, g->len) == 0)
3826 case MATCH_MIDDLE_ONLY:
3827 if (strstr(str, g->search))
3830 case MATCH_END_ONLY:
3832 if (slen >= g->len &&
3833 memcmp(str + slen - g->len, g->search, g->len) == 0)
3837 if (glob_match(g->search, str))
3846 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3848 struct ftrace_func_entry *entry;
3851 entry = ftrace_lookup_ip(hash, rec->ip);
3853 /* Do nothing if it doesn't exist */
3857 free_hash_entry(hash, entry);
3859 /* Do nothing if it exists */
3863 ret = add_hash_entry(hash, rec->ip);
3869 add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3872 long index = simple_strtoul(func_g->search, NULL, 0);
3873 struct ftrace_page *pg;
3874 struct dyn_ftrace *rec;
3876 /* The index starts at 1 */
3880 do_for_each_ftrace_rec(pg, rec) {
3881 if (pg->index <= index) {
3883 /* this is a double loop, break goes to the next page */
3886 rec = &pg->records[index];
3887 enter_record(hash, rec, clear_filter);
3889 } while_for_each_ftrace_rec();
3894 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3895 struct ftrace_glob *mod_g, int exclude_mod)
3897 char str[KSYM_SYMBOL_LEN];
3900 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3903 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3905 /* blank module name to match all modules */
3907 /* blank module globbing: modname xor exclude_mod */
3908 if (!exclude_mod != !modname)
3914 * exclude_mod is set to trace everything but the given
3915 * module. If it is set and the module matches, then
3916 * return 0. If it is not set, and the module doesn't match
3917 * also return 0. Otherwise, check the function to see if
3920 if (!mod_matches == !exclude_mod)
3923 /* blank search means to match all funcs in the mod */
3928 return ftrace_match(str, func_g);
3932 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3934 struct ftrace_page *pg;
3935 struct dyn_ftrace *rec;
3936 struct ftrace_glob func_g = { .type = MATCH_FULL };
3937 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3938 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3939 int exclude_mod = 0;
3942 int clear_filter = 0;
3945 func_g.type = filter_parse_regex(func, len, &func_g.search,
3947 func_g.len = strlen(func_g.search);
3951 mod_g.type = filter_parse_regex(mod, strlen(mod),
3952 &mod_g.search, &exclude_mod);
3953 mod_g.len = strlen(mod_g.search);
3956 mutex_lock(&ftrace_lock);
3958 if (unlikely(ftrace_disabled))
3961 if (func_g.type == MATCH_INDEX) {
3962 found = add_rec_by_index(hash, &func_g, clear_filter);
3966 do_for_each_ftrace_rec(pg, rec) {
3968 if (rec->flags & FTRACE_FL_DISABLED)
3971 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3972 ret = enter_record(hash, rec, clear_filter);
3979 } while_for_each_ftrace_rec();
3981 mutex_unlock(&ftrace_lock);
3987 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3989 return match_records(hash, buff, len, NULL);
3992 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3993 struct ftrace_ops_hash *old_hash)
3995 struct ftrace_ops *op;
3997 if (!ftrace_enabled)
4000 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4001 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4006 * If this is the shared global_ops filter, then we need to
4007 * check if there is another ops that shares it, is enabled.
4008 * If so, we still need to run the modify code.
4010 if (ops->func_hash != &global_ops.local_hash)
4013 do_for_each_ftrace_op(op, ftrace_ops_list) {
4014 if (op->func_hash == &global_ops.local_hash &&
4015 op->flags & FTRACE_OPS_FL_ENABLED) {
4016 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4017 /* Only need to do this once */
4020 } while_for_each_ftrace_op(op);
4023 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
4024 struct ftrace_hash **orig_hash,
4025 struct ftrace_hash *hash,
4028 struct ftrace_ops_hash old_hash_ops;
4029 struct ftrace_hash *old_hash;
4032 old_hash = *orig_hash;
4033 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4034 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4035 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4037 ftrace_ops_update_code(ops, &old_hash_ops);
4038 free_ftrace_hash_rcu(old_hash);
4043 static bool module_exists(const char *module)
4045 /* All modules have the symbol __this_module */
4046 static const char this_mod[] = "__this_module";
4047 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
4051 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
4053 if (n > sizeof(modname) - 1)
4056 val = module_kallsyms_lookup_name(modname);
4060 static int cache_mod(struct trace_array *tr,
4061 const char *func, char *module, int enable)
4063 struct ftrace_mod_load *ftrace_mod, *n;
4064 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4067 mutex_lock(&ftrace_lock);
4069 /* We do not cache inverse filters */
4070 if (func[0] == '!') {
4074 /* Look to remove this hash */
4075 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4076 if (strcmp(ftrace_mod->module, module) != 0)
4079 /* no func matches all */
4080 if (strcmp(func, "*") == 0 ||
4081 (ftrace_mod->func &&
4082 strcmp(ftrace_mod->func, func) == 0)) {
4084 free_ftrace_mod(ftrace_mod);
4092 /* We only care about modules that have not been loaded yet */
4093 if (module_exists(module))
4096 /* Save this string off, and execute it when the module is loaded */
4097 ret = ftrace_add_mod(tr, func, module, enable);
4099 mutex_unlock(&ftrace_lock);
4105 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4106 int reset, int enable);
4108 #ifdef CONFIG_MODULES
4109 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4110 char *mod, bool enable)
4112 struct ftrace_mod_load *ftrace_mod, *n;
4113 struct ftrace_hash **orig_hash, *new_hash;
4114 LIST_HEAD(process_mods);
4118 mutex_lock(&ops->func_hash->regex_lock);
4121 orig_hash = &ops->func_hash->filter_hash;
4123 orig_hash = &ops->func_hash->notrace_hash;
4125 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4128 goto out; /* warn? */
4130 mutex_lock(&ftrace_lock);
4132 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4134 if (strcmp(ftrace_mod->module, mod) != 0)
4137 if (ftrace_mod->func)
4138 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4140 func = kstrdup("*", GFP_KERNEL);
4142 if (!func) /* warn? */
4145 list_del(&ftrace_mod->list);
4146 list_add(&ftrace_mod->list, &process_mods);
4148 /* Use the newly allocated func, as it may be "*" */
4149 kfree(ftrace_mod->func);
4150 ftrace_mod->func = func;
4153 mutex_unlock(&ftrace_lock);
4155 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4157 func = ftrace_mod->func;
4159 /* Grabs ftrace_lock, which is why we have this extra step */
4160 match_records(new_hash, func, strlen(func), mod);
4161 free_ftrace_mod(ftrace_mod);
4164 if (enable && list_empty(head))
4165 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4167 mutex_lock(&ftrace_lock);
4169 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4171 mutex_unlock(&ftrace_lock);
4174 mutex_unlock(&ops->func_hash->regex_lock);
4176 free_ftrace_hash(new_hash);
4179 static void process_cached_mods(const char *mod_name)
4181 struct trace_array *tr;
4184 mod = kstrdup(mod_name, GFP_KERNEL);
4188 mutex_lock(&trace_types_lock);
4189 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4190 if (!list_empty(&tr->mod_trace))
4191 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4192 if (!list_empty(&tr->mod_notrace))
4193 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4195 mutex_unlock(&trace_types_lock);
4202 * We register the module command as a template to show others how
4203 * to register the a command as well.
4207 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4208 char *func_orig, char *cmd, char *module, int enable)
4213 /* match_records() modifies func, and we need the original */
4214 func = kstrdup(func_orig, GFP_KERNEL);
4219 * cmd == 'mod' because we only registered this func
4220 * for the 'mod' ftrace_func_command.
4221 * But if you register one func with multiple commands,
4222 * you can tell which command was used by the cmd
4225 ret = match_records(hash, func, strlen(func), module);
4229 return cache_mod(tr, func_orig, module, enable);
4235 static struct ftrace_func_command ftrace_mod_cmd = {
4237 .func = ftrace_mod_callback,
4240 static int __init ftrace_mod_cmd_init(void)
4242 return register_ftrace_command(&ftrace_mod_cmd);
4244 core_initcall(ftrace_mod_cmd_init);
4246 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4247 struct ftrace_ops *op, struct pt_regs *pt_regs)
4249 struct ftrace_probe_ops *probe_ops;
4250 struct ftrace_func_probe *probe;
4252 probe = container_of(op, struct ftrace_func_probe, ops);
4253 probe_ops = probe->probe_ops;
4256 * Disable preemption for these calls to prevent a RCU grace
4257 * period. This syncs the hash iteration and freeing of items
4258 * on the hash. rcu_read_lock is too dangerous here.
4260 preempt_disable_notrace();
4261 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4262 preempt_enable_notrace();
4265 struct ftrace_func_map {
4266 struct ftrace_func_entry entry;
4270 struct ftrace_func_mapper {
4271 struct ftrace_hash hash;
4275 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4277 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4279 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4281 struct ftrace_hash *hash;
4284 * The mapper is simply a ftrace_hash, but since the entries
4285 * in the hash are not ftrace_func_entry type, we define it
4286 * as a separate structure.
4288 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4289 return (struct ftrace_func_mapper *)hash;
4293 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4294 * @mapper: The mapper that has the ip maps
4295 * @ip: the instruction pointer to find the data for
4297 * Returns the data mapped to @ip if found otherwise NULL. The return
4298 * is actually the address of the mapper data pointer. The address is
4299 * returned for use cases where the data is no bigger than a long, and
4300 * the user can use the data pointer as its data instead of having to
4301 * allocate more memory for the reference.
4303 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4306 struct ftrace_func_entry *entry;
4307 struct ftrace_func_map *map;
4309 entry = ftrace_lookup_ip(&mapper->hash, ip);
4313 map = (struct ftrace_func_map *)entry;
4318 * ftrace_func_mapper_add_ip - Map some data to an ip
4319 * @mapper: The mapper that has the ip maps
4320 * @ip: The instruction pointer address to map @data to
4321 * @data: The data to map to @ip
4323 * Returns 0 on succes otherwise an error.
4325 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4326 unsigned long ip, void *data)
4328 struct ftrace_func_entry *entry;
4329 struct ftrace_func_map *map;
4331 entry = ftrace_lookup_ip(&mapper->hash, ip);
4335 map = kmalloc(sizeof(*map), GFP_KERNEL);
4342 __add_hash_entry(&mapper->hash, &map->entry);
4348 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4349 * @mapper: The mapper that has the ip maps
4350 * @ip: The instruction pointer address to remove the data from
4352 * Returns the data if it is found, otherwise NULL.
4353 * Note, if the data pointer is used as the data itself, (see
4354 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4355 * if the data pointer was set to zero.
4357 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4360 struct ftrace_func_entry *entry;
4361 struct ftrace_func_map *map;
4364 entry = ftrace_lookup_ip(&mapper->hash, ip);
4368 map = (struct ftrace_func_map *)entry;
4371 remove_hash_entry(&mapper->hash, entry);
4378 * free_ftrace_func_mapper - free a mapping of ips and data
4379 * @mapper: The mapper that has the ip maps
4380 * @free_func: A function to be called on each data item.
4382 * This is used to free the function mapper. The @free_func is optional
4383 * and can be used if the data needs to be freed as well.
4385 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4386 ftrace_mapper_func free_func)
4388 struct ftrace_func_entry *entry;
4389 struct ftrace_func_map *map;
4390 struct hlist_head *hhd;
4396 if (free_func && mapper->hash.count) {
4397 size = 1 << mapper->hash.size_bits;
4398 for (i = 0; i < size; i++) {
4399 hhd = &mapper->hash.buckets[i];
4400 hlist_for_each_entry(entry, hhd, hlist) {
4401 map = (struct ftrace_func_map *)entry;
4406 free_ftrace_hash(&mapper->hash);
4409 static void release_probe(struct ftrace_func_probe *probe)
4411 struct ftrace_probe_ops *probe_ops;
4413 mutex_lock(&ftrace_lock);
4415 WARN_ON(probe->ref <= 0);
4417 /* Subtract the ref that was used to protect this instance */
4421 probe_ops = probe->probe_ops;
4423 * Sending zero as ip tells probe_ops to free
4424 * the probe->data itself
4426 if (probe_ops->free)
4427 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4428 list_del(&probe->list);
4431 mutex_unlock(&ftrace_lock);
4434 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4437 * Add one ref to keep it from being freed when releasing the
4438 * ftrace_lock mutex.
4444 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4445 struct ftrace_probe_ops *probe_ops,
4448 struct ftrace_func_entry *entry;
4449 struct ftrace_func_probe *probe;
4450 struct ftrace_hash **orig_hash;
4451 struct ftrace_hash *old_hash;
4452 struct ftrace_hash *hash;
4461 /* We do not support '!' for function probes */
4462 if (WARN_ON(glob[0] == '!'))
4466 mutex_lock(&ftrace_lock);
4467 /* Check if the probe_ops is already registered */
4468 list_for_each_entry(probe, &tr->func_probes, list) {
4469 if (probe->probe_ops == probe_ops)
4472 if (&probe->list == &tr->func_probes) {
4473 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4475 mutex_unlock(&ftrace_lock);
4478 probe->probe_ops = probe_ops;
4479 probe->ops.func = function_trace_probe_call;
4481 ftrace_ops_init(&probe->ops);
4482 list_add(&probe->list, &tr->func_probes);
4485 acquire_probe_locked(probe);
4487 mutex_unlock(&ftrace_lock);
4490 * Note, there's a small window here that the func_hash->filter_hash
4491 * may be NULL or empty. Need to be carefule when reading the loop.
4493 mutex_lock(&probe->ops.func_hash->regex_lock);
4495 orig_hash = &probe->ops.func_hash->filter_hash;
4496 old_hash = *orig_hash;
4497 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4504 ret = ftrace_match_records(hash, glob, strlen(glob));
4506 /* Nothing found? */
4513 size = 1 << hash->size_bits;
4514 for (i = 0; i < size; i++) {
4515 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4516 if (ftrace_lookup_ip(old_hash, entry->ip))
4519 * The caller might want to do something special
4520 * for each function we find. We call the callback
4521 * to give the caller an opportunity to do so.
4523 if (probe_ops->init) {
4524 ret = probe_ops->init(probe_ops, tr,
4528 if (probe_ops->free && count)
4529 probe_ops->free(probe_ops, tr,
4539 mutex_lock(&ftrace_lock);
4542 /* Nothing was added? */
4547 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4552 /* One ref for each new function traced */
4553 probe->ref += count;
4555 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4556 ret = ftrace_startup(&probe->ops, 0);
4559 mutex_unlock(&ftrace_lock);
4564 mutex_unlock(&probe->ops.func_hash->regex_lock);
4565 free_ftrace_hash(hash);
4567 release_probe(probe);
4572 if (!probe_ops->free || !count)
4575 /* Failed to do the move, need to call the free functions */
4576 for (i = 0; i < size; i++) {
4577 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4578 if (ftrace_lookup_ip(old_hash, entry->ip))
4580 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4587 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4588 struct ftrace_probe_ops *probe_ops)
4590 struct ftrace_ops_hash old_hash_ops;
4591 struct ftrace_func_entry *entry;
4592 struct ftrace_func_probe *probe;
4593 struct ftrace_glob func_g;
4594 struct ftrace_hash **orig_hash;
4595 struct ftrace_hash *old_hash;
4596 struct ftrace_hash *hash = NULL;
4597 struct hlist_node *tmp;
4598 struct hlist_head hhd;
4599 char str[KSYM_SYMBOL_LEN];
4601 int i, ret = -ENODEV;
4604 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4605 func_g.search = NULL;
4609 func_g.type = filter_parse_regex(glob, strlen(glob),
4610 &func_g.search, ¬);
4611 func_g.len = strlen(func_g.search);
4613 /* we do not support '!' for function probes */
4618 mutex_lock(&ftrace_lock);
4619 /* Check if the probe_ops is already registered */
4620 list_for_each_entry(probe, &tr->func_probes, list) {
4621 if (probe->probe_ops == probe_ops)
4624 if (&probe->list == &tr->func_probes)
4625 goto err_unlock_ftrace;
4628 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4629 goto err_unlock_ftrace;
4631 acquire_probe_locked(probe);
4633 mutex_unlock(&ftrace_lock);
4635 mutex_lock(&probe->ops.func_hash->regex_lock);
4637 orig_hash = &probe->ops.func_hash->filter_hash;
4638 old_hash = *orig_hash;
4640 if (ftrace_hash_empty(old_hash))
4643 old_hash_ops.filter_hash = old_hash;
4644 /* Probes only have filters */
4645 old_hash_ops.notrace_hash = NULL;
4648 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4652 INIT_HLIST_HEAD(&hhd);
4654 size = 1 << hash->size_bits;
4655 for (i = 0; i < size; i++) {
4656 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4658 if (func_g.search) {
4659 kallsyms_lookup(entry->ip, NULL, NULL,
4661 if (!ftrace_match(str, &func_g))
4665 remove_hash_entry(hash, entry);
4666 hlist_add_head(&entry->hlist, &hhd);
4670 /* Nothing found? */
4676 mutex_lock(&ftrace_lock);
4678 WARN_ON(probe->ref < count);
4680 probe->ref -= count;
4682 if (ftrace_hash_empty(hash))
4683 ftrace_shutdown(&probe->ops, 0);
4685 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4688 /* still need to update the function call sites */
4689 if (ftrace_enabled && !ftrace_hash_empty(hash))
4690 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4694 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4695 hlist_del(&entry->hlist);
4696 if (probe_ops->free)
4697 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4700 mutex_unlock(&ftrace_lock);
4703 mutex_unlock(&probe->ops.func_hash->regex_lock);
4704 free_ftrace_hash(hash);
4706 release_probe(probe);
4711 mutex_unlock(&ftrace_lock);
4715 void clear_ftrace_function_probes(struct trace_array *tr)
4717 struct ftrace_func_probe *probe, *n;
4719 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4720 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4723 static LIST_HEAD(ftrace_commands);
4724 static DEFINE_MUTEX(ftrace_cmd_mutex);
4727 * Currently we only register ftrace commands from __init, so mark this
4730 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4732 struct ftrace_func_command *p;
4735 mutex_lock(&ftrace_cmd_mutex);
4736 list_for_each_entry(p, &ftrace_commands, list) {
4737 if (strcmp(cmd->name, p->name) == 0) {
4742 list_add(&cmd->list, &ftrace_commands);
4744 mutex_unlock(&ftrace_cmd_mutex);
4750 * Currently we only unregister ftrace commands from __init, so mark
4753 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4755 struct ftrace_func_command *p, *n;
4758 mutex_lock(&ftrace_cmd_mutex);
4759 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4760 if (strcmp(cmd->name, p->name) == 0) {
4762 list_del_init(&p->list);
4767 mutex_unlock(&ftrace_cmd_mutex);
4772 static int ftrace_process_regex(struct ftrace_iterator *iter,
4773 char *buff, int len, int enable)
4775 struct ftrace_hash *hash = iter->hash;
4776 struct trace_array *tr = iter->ops->private;
4777 char *func, *command, *next = buff;
4778 struct ftrace_func_command *p;
4781 func = strsep(&next, ":");
4784 ret = ftrace_match_records(hash, func, len);
4794 command = strsep(&next, ":");
4796 mutex_lock(&ftrace_cmd_mutex);
4797 list_for_each_entry(p, &ftrace_commands, list) {
4798 if (strcmp(p->name, command) == 0) {
4799 ret = p->func(tr, hash, func, command, next, enable);
4804 mutex_unlock(&ftrace_cmd_mutex);
4810 ftrace_regex_write(struct file *file, const char __user *ubuf,
4811 size_t cnt, loff_t *ppos, int enable)
4813 struct ftrace_iterator *iter;
4814 struct trace_parser *parser;
4820 if (file->f_mode & FMODE_READ) {
4821 struct seq_file *m = file->private_data;
4824 iter = file->private_data;
4826 if (unlikely(ftrace_disabled))
4829 /* iter->hash is a local copy, so we don't need regex_lock */
4831 parser = &iter->parser;
4832 read = trace_get_user(parser, ubuf, cnt, ppos);
4834 if (read >= 0 && trace_parser_loaded(parser) &&
4835 !trace_parser_cont(parser)) {
4836 ret = ftrace_process_regex(iter, parser->buffer,
4837 parser->idx, enable);
4838 trace_parser_clear(parser);
4849 ftrace_filter_write(struct file *file, const char __user *ubuf,
4850 size_t cnt, loff_t *ppos)
4852 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4856 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4857 size_t cnt, loff_t *ppos)
4859 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4863 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4865 struct ftrace_func_entry *entry;
4867 if (!ftrace_location(ip))
4871 entry = ftrace_lookup_ip(hash, ip);
4874 free_hash_entry(hash, entry);
4878 return add_hash_entry(hash, ip);
4882 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4883 unsigned long ip, int remove, int reset, int enable)
4885 struct ftrace_hash **orig_hash;
4886 struct ftrace_hash *hash;
4889 if (unlikely(ftrace_disabled))
4892 mutex_lock(&ops->func_hash->regex_lock);
4895 orig_hash = &ops->func_hash->filter_hash;
4897 orig_hash = &ops->func_hash->notrace_hash;
4900 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4902 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4906 goto out_regex_unlock;
4909 if (buf && !ftrace_match_records(hash, buf, len)) {
4911 goto out_regex_unlock;
4914 ret = ftrace_match_addr(hash, ip, remove);
4916 goto out_regex_unlock;
4919 mutex_lock(&ftrace_lock);
4920 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4921 mutex_unlock(&ftrace_lock);
4924 mutex_unlock(&ops->func_hash->regex_lock);
4926 free_ftrace_hash(hash);
4931 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4932 int reset, int enable)
4934 return ftrace_set_hash(ops, NULL, 0, ip, remove, reset, enable);
4937 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
4939 struct ftrace_direct_func {
4940 struct list_head next;
4945 static LIST_HEAD(ftrace_direct_funcs);
4948 * ftrace_find_direct_func - test an address if it is a registered direct caller
4949 * @addr: The address of a registered direct caller
4951 * This searches to see if a ftrace direct caller has been registered
4952 * at a specific address, and if so, it returns a descriptor for it.
4954 * This can be used by architecture code to see if an address is
4955 * a direct caller (trampoline) attached to a fentry/mcount location.
4956 * This is useful for the function_graph tracer, as it may need to
4957 * do adjustments if it traced a location that also has a direct
4958 * trampoline attached to it.
4960 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
4962 struct ftrace_direct_func *entry;
4965 /* May be called by fgraph trampoline (protected by rcu tasks) */
4966 list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
4967 if (entry->addr == addr) {
4979 * register_ftrace_direct - Call a custom trampoline directly
4980 * @ip: The address of the nop at the beginning of a function
4981 * @addr: The address of the trampoline to call at @ip
4983 * This is used to connect a direct call from the nop location (@ip)
4984 * at the start of ftrace traced functions. The location that it calls
4985 * (@addr) must be able to handle a direct call, and save the parameters
4986 * of the function being traced, and restore them (or inject new ones
4987 * if needed), before returning.
4991 * -EBUSY - Another direct function is already attached (there can be only one)
4992 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
4993 * -ENOMEM - There was an allocation failure.
4995 int register_ftrace_direct(unsigned long ip, unsigned long addr)
4997 struct ftrace_direct_func *direct;
4998 struct ftrace_func_entry *entry;
4999 struct ftrace_hash *free_hash = NULL;
5000 struct dyn_ftrace *rec;
5003 mutex_lock(&direct_mutex);
5005 /* See if there's a direct function at @ip already */
5006 if (ftrace_find_rec_direct(ip))
5010 rec = lookup_rec(ip, ip);
5015 * Check if the rec says it has a direct call but we didn't
5018 if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
5021 /* Make sure the ip points to the exact record */
5022 if (ip != rec->ip) {
5024 /* Need to check this ip for a direct. */
5025 if (ftrace_find_rec_direct(ip))
5030 if (ftrace_hash_empty(direct_functions) ||
5031 direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
5032 struct ftrace_hash *new_hash;
5033 int size = ftrace_hash_empty(direct_functions) ? 0 :
5034 direct_functions->count + 1;
5039 new_hash = dup_hash(direct_functions, size);
5043 free_hash = direct_functions;
5044 direct_functions = new_hash;
5047 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
5051 direct = ftrace_find_direct_func(addr);
5053 direct = kmalloc(sizeof(*direct), GFP_KERNEL);
5058 direct->addr = addr;
5060 list_add_rcu(&direct->next, &ftrace_direct_funcs);
5061 ftrace_direct_func_count++;
5065 entry->direct = addr;
5066 __add_hash_entry(direct_functions, entry);
5068 ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
5070 remove_hash_entry(direct_functions, entry);
5072 if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
5073 ret = register_ftrace_function(&direct_ops);
5075 ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5080 if (!direct->count) {
5081 list_del_rcu(&direct->next);
5082 synchronize_rcu_tasks();
5085 free_ftrace_hash(free_hash);
5087 ftrace_direct_func_count--;
5093 mutex_unlock(&direct_mutex);
5096 synchronize_rcu_tasks();
5097 free_ftrace_hash(free_hash);
5102 EXPORT_SYMBOL_GPL(register_ftrace_direct);
5104 static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
5105 struct dyn_ftrace **recp)
5107 struct ftrace_func_entry *entry;
5108 struct dyn_ftrace *rec;
5110 rec = lookup_rec(*ip, *ip);
5114 entry = __ftrace_lookup_ip(direct_functions, rec->ip);
5116 WARN_ON(rec->flags & FTRACE_FL_DIRECT);
5120 WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
5122 /* Passed in ip just needs to be on the call site */
5131 int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
5133 struct ftrace_direct_func *direct;
5134 struct ftrace_func_entry *entry;
5137 mutex_lock(&direct_mutex);
5139 entry = find_direct_entry(&ip, NULL);
5143 if (direct_functions->count == 1)
5144 unregister_ftrace_function(&direct_ops);
5146 ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5150 remove_hash_entry(direct_functions, entry);
5152 direct = ftrace_find_direct_func(addr);
5153 if (!WARN_ON(!direct)) {
5154 /* This is the good path (see the ! before WARN) */
5156 WARN_ON(direct->count < 0);
5157 if (!direct->count) {
5158 list_del_rcu(&direct->next);
5159 synchronize_rcu_tasks();
5161 ftrace_direct_func_count--;
5165 mutex_unlock(&direct_mutex);
5169 EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
5171 static struct ftrace_ops stub_ops = {
5172 .func = ftrace_stub,
5176 * ftrace_modify_direct_caller - modify ftrace nop directly
5177 * @entry: The ftrace hash entry of the direct helper for @rec
5178 * @rec: The record representing the function site to patch
5179 * @old_addr: The location that the site at @rec->ip currently calls
5180 * @new_addr: The location that the site at @rec->ip should call
5182 * An architecture may overwrite this function to optimize the
5183 * changing of the direct callback on an ftrace nop location.
5184 * This is called with the ftrace_lock mutex held, and no other
5185 * ftrace callbacks are on the associated record (@rec). Thus,
5186 * it is safe to modify the ftrace record, where it should be
5187 * currently calling @old_addr directly, to call @new_addr.
5189 * Safety checks should be made to make sure that the code at
5190 * @rec->ip is currently calling @old_addr. And this must
5191 * also update entry->direct to @new_addr.
5193 int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
5194 struct dyn_ftrace *rec,
5195 unsigned long old_addr,
5196 unsigned long new_addr)
5198 unsigned long ip = rec->ip;
5202 * The ftrace_lock was used to determine if the record
5203 * had more than one registered user to it. If it did,
5204 * we needed to prevent that from changing to do the quick
5205 * switch. But if it did not (only a direct caller was attached)
5206 * then this function is called. But this function can deal
5207 * with attached callers to the rec that we care about, and
5208 * since this function uses standard ftrace calls that take
5209 * the ftrace_lock mutex, we need to release it.
5211 mutex_unlock(&ftrace_lock);
5214 * By setting a stub function at the same address, we force
5215 * the code to call the iterator and the direct_ops helper.
5216 * This means that @ip does not call the direct call, and
5217 * we can simply modify it.
5219 ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
5223 ret = register_ftrace_function(&stub_ops);
5225 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5229 entry->direct = new_addr;
5232 * By removing the stub, we put back the direct call, calling
5235 unregister_ftrace_function(&stub_ops);
5236 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5239 mutex_lock(&ftrace_lock);
5245 * modify_ftrace_direct - Modify an existing direct call to call something else
5246 * @ip: The instruction pointer to modify
5247 * @old_addr: The address that the current @ip calls directly
5248 * @new_addr: The address that the @ip should call
5250 * This modifies a ftrace direct caller at an instruction pointer without
5251 * having to disable it first. The direct call will switch over to the
5252 * @new_addr without missing anything.
5254 * Returns: zero on success. Non zero on error, which includes:
5255 * -ENODEV : the @ip given has no direct caller attached
5256 * -EINVAL : the @old_addr does not match the current direct caller
5258 int modify_ftrace_direct(unsigned long ip,
5259 unsigned long old_addr, unsigned long new_addr)
5261 struct ftrace_func_entry *entry;
5262 struct dyn_ftrace *rec;
5265 mutex_lock(&direct_mutex);
5267 mutex_lock(&ftrace_lock);
5268 entry = find_direct_entry(&ip, &rec);
5273 if (entry->direct != old_addr)
5277 * If there's no other ftrace callback on the rec->ip location,
5278 * then it can be changed directly by the architecture.
5279 * If there is another caller, then we just need to change the
5280 * direct caller helper to point to @new_addr.
5282 if (ftrace_rec_count(rec) == 1) {
5283 ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
5285 entry->direct = new_addr;
5290 mutex_unlock(&ftrace_lock);
5291 mutex_unlock(&direct_mutex);
5294 EXPORT_SYMBOL_GPL(modify_ftrace_direct);
5295 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
5298 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
5299 * @ops - the ops to set the filter with
5300 * @ip - the address to add to or remove from the filter.
5301 * @remove - non zero to remove the ip from the filter
5302 * @reset - non zero to reset all filters before applying this filter.
5304 * Filters denote which functions should be enabled when tracing is enabled
5305 * If @ip is NULL, it failes to update filter.
5307 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
5308 int remove, int reset)
5310 ftrace_ops_init(ops);
5311 return ftrace_set_addr(ops, ip, remove, reset, 1);
5313 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
5316 * ftrace_ops_set_global_filter - setup ops to use global filters
5317 * @ops - the ops which will use the global filters
5319 * ftrace users who need global function trace filtering should call this.
5320 * It can set the global filter only if ops were not initialized before.
5322 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
5324 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
5327 ftrace_ops_init(ops);
5328 ops->func_hash = &global_ops.local_hash;
5330 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
5333 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
5334 int reset, int enable)
5336 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
5340 * ftrace_set_filter - set a function to filter on in ftrace
5341 * @ops - the ops to set the filter with
5342 * @buf - the string that holds the function filter text.
5343 * @len - the length of the string.
5344 * @reset - non zero to reset all filters before applying this filter.
5346 * Filters denote which functions should be enabled when tracing is enabled.
5347 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5349 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
5352 ftrace_ops_init(ops);
5353 return ftrace_set_regex(ops, buf, len, reset, 1);
5355 EXPORT_SYMBOL_GPL(ftrace_set_filter);
5358 * ftrace_set_notrace - set a function to not trace in ftrace
5359 * @ops - the ops to set the notrace filter with
5360 * @buf - the string that holds the function notrace text.
5361 * @len - the length of the string.
5362 * @reset - non zero to reset all filters before applying this filter.
5364 * Notrace Filters denote which functions should not be enabled when tracing
5365 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5368 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
5371 ftrace_ops_init(ops);
5372 return ftrace_set_regex(ops, buf, len, reset, 0);
5374 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
5376 * ftrace_set_global_filter - set a function to filter on with global tracers
5377 * @buf - the string that holds the function filter text.
5378 * @len - the length of the string.
5379 * @reset - non zero to reset all filters before applying this filter.
5381 * Filters denote which functions should be enabled when tracing is enabled.
5382 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5384 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
5386 ftrace_set_regex(&global_ops, buf, len, reset, 1);
5388 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
5391 * ftrace_set_global_notrace - set a function to not trace with global tracers
5392 * @buf - the string that holds the function notrace text.
5393 * @len - the length of the string.
5394 * @reset - non zero to reset all filters before applying this filter.
5396 * Notrace Filters denote which functions should not be enabled when tracing
5397 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5400 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
5402 ftrace_set_regex(&global_ops, buf, len, reset, 0);
5404 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
5407 * command line interface to allow users to set filters on boot up.
5409 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
5410 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5411 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
5413 /* Used by function selftest to not test if filter is set */
5414 bool ftrace_filter_param __initdata;
5416 static int __init set_ftrace_notrace(char *str)
5418 ftrace_filter_param = true;
5419 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
5422 __setup("ftrace_notrace=", set_ftrace_notrace);
5424 static int __init set_ftrace_filter(char *str)
5426 ftrace_filter_param = true;
5427 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
5430 __setup("ftrace_filter=", set_ftrace_filter);
5432 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5433 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
5434 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5435 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
5437 static int __init set_graph_function(char *str)
5439 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
5442 __setup("ftrace_graph_filter=", set_graph_function);
5444 static int __init set_graph_notrace_function(char *str)
5446 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
5449 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
5451 static int __init set_graph_max_depth_function(char *str)
5455 fgraph_max_depth = simple_strtoul(str, NULL, 0);
5458 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
5460 static void __init set_ftrace_early_graph(char *buf, int enable)
5464 struct ftrace_hash *hash;
5466 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5471 func = strsep(&buf, ",");
5472 /* we allow only one expression at a time */
5473 ret = ftrace_graph_set_hash(hash, func);
5475 printk(KERN_DEBUG "ftrace: function %s not "
5476 "traceable\n", func);
5480 ftrace_graph_hash = hash;
5482 ftrace_graph_notrace_hash = hash;
5484 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5487 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
5491 ftrace_ops_init(ops);
5494 func = strsep(&buf, ",");
5495 ftrace_set_regex(ops, func, strlen(func), 0, enable);
5499 static void __init set_ftrace_early_filters(void)
5501 if (ftrace_filter_buf[0])
5502 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5503 if (ftrace_notrace_buf[0])
5504 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5505 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5506 if (ftrace_graph_buf[0])
5507 set_ftrace_early_graph(ftrace_graph_buf, 1);
5508 if (ftrace_graph_notrace_buf[0])
5509 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5510 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5513 int ftrace_regex_release(struct inode *inode, struct file *file)
5515 struct seq_file *m = (struct seq_file *)file->private_data;
5516 struct ftrace_iterator *iter;
5517 struct ftrace_hash **orig_hash;
5518 struct trace_parser *parser;
5522 if (file->f_mode & FMODE_READ) {
5524 seq_release(inode, file);
5526 iter = file->private_data;
5528 parser = &iter->parser;
5529 if (trace_parser_loaded(parser)) {
5530 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
5533 trace_parser_put(parser);
5535 mutex_lock(&iter->ops->func_hash->regex_lock);
5537 if (file->f_mode & FMODE_WRITE) {
5538 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5541 orig_hash = &iter->ops->func_hash->filter_hash;
5542 if (iter->tr && !list_empty(&iter->tr->mod_trace))
5543 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5545 orig_hash = &iter->ops->func_hash->notrace_hash;
5547 mutex_lock(&ftrace_lock);
5548 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5549 iter->hash, filter_hash);
5550 mutex_unlock(&ftrace_lock);
5552 /* For read only, the hash is the ops hash */
5556 mutex_unlock(&iter->ops->func_hash->regex_lock);
5557 free_ftrace_hash(iter->hash);
5559 trace_array_put(iter->tr);
5565 static const struct file_operations ftrace_avail_fops = {
5566 .open = ftrace_avail_open,
5568 .llseek = seq_lseek,
5569 .release = seq_release_private,
5572 static const struct file_operations ftrace_enabled_fops = {
5573 .open = ftrace_enabled_open,
5575 .llseek = seq_lseek,
5576 .release = seq_release_private,
5579 static const struct file_operations ftrace_filter_fops = {
5580 .open = ftrace_filter_open,
5582 .write = ftrace_filter_write,
5583 .llseek = tracing_lseek,
5584 .release = ftrace_regex_release,
5587 static const struct file_operations ftrace_notrace_fops = {
5588 .open = ftrace_notrace_open,
5590 .write = ftrace_notrace_write,
5591 .llseek = tracing_lseek,
5592 .release = ftrace_regex_release,
5595 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5597 static DEFINE_MUTEX(graph_lock);
5599 struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5600 struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5602 enum graph_filter_type {
5603 GRAPH_FILTER_NOTRACE = 0,
5604 GRAPH_FILTER_FUNCTION,
5607 #define FTRACE_GRAPH_EMPTY ((void *)1)
5609 struct ftrace_graph_data {
5610 struct ftrace_hash *hash;
5611 struct ftrace_func_entry *entry;
5612 int idx; /* for hash table iteration */
5613 enum graph_filter_type type;
5614 struct ftrace_hash *new_hash;
5615 const struct seq_operations *seq_ops;
5616 struct trace_parser parser;
5620 __g_next(struct seq_file *m, loff_t *pos)
5622 struct ftrace_graph_data *fgd = m->private;
5623 struct ftrace_func_entry *entry = fgd->entry;
5624 struct hlist_head *head;
5625 int i, idx = fgd->idx;
5627 if (*pos >= fgd->hash->count)
5631 hlist_for_each_entry_continue(entry, hlist) {
5639 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5640 head = &fgd->hash->buckets[i];
5641 hlist_for_each_entry(entry, head, hlist) {
5651 g_next(struct seq_file *m, void *v, loff_t *pos)
5654 return __g_next(m, pos);
5657 static void *g_start(struct seq_file *m, loff_t *pos)
5659 struct ftrace_graph_data *fgd = m->private;
5661 mutex_lock(&graph_lock);
5663 if (fgd->type == GRAPH_FILTER_FUNCTION)
5664 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5665 lockdep_is_held(&graph_lock));
5667 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5668 lockdep_is_held(&graph_lock));
5670 /* Nothing, tell g_show to print all functions are enabled */
5671 if (ftrace_hash_empty(fgd->hash) && !*pos)
5672 return FTRACE_GRAPH_EMPTY;
5676 return __g_next(m, pos);
5679 static void g_stop(struct seq_file *m, void *p)
5681 mutex_unlock(&graph_lock);
5684 static int g_show(struct seq_file *m, void *v)
5686 struct ftrace_func_entry *entry = v;
5691 if (entry == FTRACE_GRAPH_EMPTY) {
5692 struct ftrace_graph_data *fgd = m->private;
5694 if (fgd->type == GRAPH_FILTER_FUNCTION)
5695 seq_puts(m, "#### all functions enabled ####\n");
5697 seq_puts(m, "#### no functions disabled ####\n");
5701 seq_printf(m, "%ps\n", (void *)entry->ip);
5706 static const struct seq_operations ftrace_graph_seq_ops = {
5714 __ftrace_graph_open(struct inode *inode, struct file *file,
5715 struct ftrace_graph_data *fgd)
5718 struct ftrace_hash *new_hash = NULL;
5720 ret = security_locked_down(LOCKDOWN_TRACEFS);
5724 if (file->f_mode & FMODE_WRITE) {
5725 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5727 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5730 if (file->f_flags & O_TRUNC)
5731 new_hash = alloc_ftrace_hash(size_bits);
5733 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5741 if (file->f_mode & FMODE_READ) {
5742 ret = seq_open(file, &ftrace_graph_seq_ops);
5744 struct seq_file *m = file->private_data;
5748 free_ftrace_hash(new_hash);
5752 file->private_data = fgd;
5755 if (ret < 0 && file->f_mode & FMODE_WRITE)
5756 trace_parser_put(&fgd->parser);
5758 fgd->new_hash = new_hash;
5761 * All uses of fgd->hash must be taken with the graph_lock
5762 * held. The graph_lock is going to be released, so force
5763 * fgd->hash to be reinitialized when it is taken again.
5771 ftrace_graph_open(struct inode *inode, struct file *file)
5773 struct ftrace_graph_data *fgd;
5776 if (unlikely(ftrace_disabled))
5779 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5783 mutex_lock(&graph_lock);
5785 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5786 lockdep_is_held(&graph_lock));
5787 fgd->type = GRAPH_FILTER_FUNCTION;
5788 fgd->seq_ops = &ftrace_graph_seq_ops;
5790 ret = __ftrace_graph_open(inode, file, fgd);
5794 mutex_unlock(&graph_lock);
5799 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5801 struct ftrace_graph_data *fgd;
5804 if (unlikely(ftrace_disabled))
5807 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5811 mutex_lock(&graph_lock);
5813 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5814 lockdep_is_held(&graph_lock));
5815 fgd->type = GRAPH_FILTER_NOTRACE;
5816 fgd->seq_ops = &ftrace_graph_seq_ops;
5818 ret = __ftrace_graph_open(inode, file, fgd);
5822 mutex_unlock(&graph_lock);
5827 ftrace_graph_release(struct inode *inode, struct file *file)
5829 struct ftrace_graph_data *fgd;
5830 struct ftrace_hash *old_hash, *new_hash;
5831 struct trace_parser *parser;
5834 if (file->f_mode & FMODE_READ) {
5835 struct seq_file *m = file->private_data;
5838 seq_release(inode, file);
5840 fgd = file->private_data;
5844 if (file->f_mode & FMODE_WRITE) {
5846 parser = &fgd->parser;
5848 if (trace_parser_loaded((parser))) {
5849 ret = ftrace_graph_set_hash(fgd->new_hash,
5853 trace_parser_put(parser);
5855 new_hash = __ftrace_hash_move(fgd->new_hash);
5861 mutex_lock(&graph_lock);
5863 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5864 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5865 lockdep_is_held(&graph_lock));
5866 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5868 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5869 lockdep_is_held(&graph_lock));
5870 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5873 mutex_unlock(&graph_lock);
5875 /* Wait till all users are no longer using the old hash */
5878 free_ftrace_hash(old_hash);
5882 free_ftrace_hash(fgd->new_hash);
5889 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5891 struct ftrace_glob func_g;
5892 struct dyn_ftrace *rec;
5893 struct ftrace_page *pg;
5894 struct ftrace_func_entry *entry;
5899 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5900 &func_g.search, ¬);
5902 func_g.len = strlen(func_g.search);
5904 mutex_lock(&ftrace_lock);
5906 if (unlikely(ftrace_disabled)) {
5907 mutex_unlock(&ftrace_lock);
5911 do_for_each_ftrace_rec(pg, rec) {
5913 if (rec->flags & FTRACE_FL_DISABLED)
5916 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5917 entry = ftrace_lookup_ip(hash, rec->ip);
5924 if (add_hash_entry(hash, rec->ip) < 0)
5928 free_hash_entry(hash, entry);
5933 } while_for_each_ftrace_rec();
5935 mutex_unlock(&ftrace_lock);
5944 ftrace_graph_write(struct file *file, const char __user *ubuf,
5945 size_t cnt, loff_t *ppos)
5947 ssize_t read, ret = 0;
5948 struct ftrace_graph_data *fgd = file->private_data;
5949 struct trace_parser *parser;
5954 /* Read mode uses seq functions */
5955 if (file->f_mode & FMODE_READ) {
5956 struct seq_file *m = file->private_data;
5960 parser = &fgd->parser;
5962 read = trace_get_user(parser, ubuf, cnt, ppos);
5964 if (read >= 0 && trace_parser_loaded(parser) &&
5965 !trace_parser_cont(parser)) {
5967 ret = ftrace_graph_set_hash(fgd->new_hash,
5969 trace_parser_clear(parser);
5978 static const struct file_operations ftrace_graph_fops = {
5979 .open = ftrace_graph_open,
5981 .write = ftrace_graph_write,
5982 .llseek = tracing_lseek,
5983 .release = ftrace_graph_release,
5986 static const struct file_operations ftrace_graph_notrace_fops = {
5987 .open = ftrace_graph_notrace_open,
5989 .write = ftrace_graph_write,
5990 .llseek = tracing_lseek,
5991 .release = ftrace_graph_release,
5993 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5995 void ftrace_create_filter_files(struct ftrace_ops *ops,
5996 struct dentry *parent)
5999 trace_create_file("set_ftrace_filter", 0644, parent,
6000 ops, &ftrace_filter_fops);
6002 trace_create_file("set_ftrace_notrace", 0644, parent,
6003 ops, &ftrace_notrace_fops);
6007 * The name "destroy_filter_files" is really a misnomer. Although
6008 * in the future, it may actually delete the files, but this is
6009 * really intended to make sure the ops passed in are disabled
6010 * and that when this function returns, the caller is free to
6013 * The "destroy" name is only to match the "create" name that this
6014 * should be paired with.
6016 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
6018 mutex_lock(&ftrace_lock);
6019 if (ops->flags & FTRACE_OPS_FL_ENABLED)
6020 ftrace_shutdown(ops, 0);
6021 ops->flags |= FTRACE_OPS_FL_DELETED;
6022 ftrace_free_filter(ops);
6023 mutex_unlock(&ftrace_lock);
6026 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
6029 trace_create_file("available_filter_functions", 0444,
6030 d_tracer, NULL, &ftrace_avail_fops);
6032 trace_create_file("enabled_functions", 0444,
6033 d_tracer, NULL, &ftrace_enabled_fops);
6035 ftrace_create_filter_files(&global_ops, d_tracer);
6037 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6038 trace_create_file("set_graph_function", 0644, d_tracer,
6040 &ftrace_graph_fops);
6041 trace_create_file("set_graph_notrace", 0644, d_tracer,
6043 &ftrace_graph_notrace_fops);
6044 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6049 static int ftrace_cmp_ips(const void *a, const void *b)
6051 const unsigned long *ipa = a;
6052 const unsigned long *ipb = b;
6061 static int ftrace_process_locs(struct module *mod,
6062 unsigned long *start,
6065 struct ftrace_page *start_pg;
6066 struct ftrace_page *pg;
6067 struct dyn_ftrace *rec;
6068 unsigned long count;
6071 unsigned long flags = 0; /* Shut up gcc */
6074 count = end - start;
6079 sort(start, count, sizeof(*start),
6080 ftrace_cmp_ips, NULL);
6082 start_pg = ftrace_allocate_pages(count);
6086 mutex_lock(&ftrace_lock);
6089 * Core and each module needs their own pages, as
6090 * modules will free them when they are removed.
6091 * Force a new page to be allocated for modules.
6094 WARN_ON(ftrace_pages || ftrace_pages_start);
6095 /* First initialization */
6096 ftrace_pages = ftrace_pages_start = start_pg;
6101 if (WARN_ON(ftrace_pages->next)) {
6102 /* Hmm, we have free pages? */
6103 while (ftrace_pages->next)
6104 ftrace_pages = ftrace_pages->next;
6107 ftrace_pages->next = start_pg;
6113 addr = ftrace_call_adjust(*p++);
6115 * Some architecture linkers will pad between
6116 * the different mcount_loc sections of different
6117 * object files to satisfy alignments.
6118 * Skip any NULL pointers.
6123 if (pg->index == pg->size) {
6124 /* We should have allocated enough */
6125 if (WARN_ON(!pg->next))
6130 rec = &pg->records[pg->index++];
6134 /* We should have used all pages */
6137 /* Assign the last page to ftrace_pages */
6141 * We only need to disable interrupts on start up
6142 * because we are modifying code that an interrupt
6143 * may execute, and the modification is not atomic.
6144 * But for modules, nothing runs the code we modify
6145 * until we are finished with it, and there's no
6146 * reason to cause large interrupt latencies while we do it.
6149 local_irq_save(flags);
6150 ftrace_update_code(mod, start_pg);
6152 local_irq_restore(flags);
6155 mutex_unlock(&ftrace_lock);
6160 struct ftrace_mod_func {
6161 struct list_head list;
6167 struct ftrace_mod_map {
6168 struct rcu_head rcu;
6169 struct list_head list;
6171 unsigned long start_addr;
6172 unsigned long end_addr;
6173 struct list_head funcs;
6174 unsigned int num_funcs;
6177 #ifdef CONFIG_MODULES
6179 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
6181 static LIST_HEAD(ftrace_mod_maps);
6183 static int referenced_filters(struct dyn_ftrace *rec)
6185 struct ftrace_ops *ops;
6188 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
6189 if (ops_references_rec(ops, rec))
6197 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
6199 struct ftrace_func_entry *entry;
6200 struct dyn_ftrace *rec;
6203 if (ftrace_hash_empty(hash))
6206 for (i = 0; i < pg->index; i++) {
6207 rec = &pg->records[i];
6208 entry = __ftrace_lookup_ip(hash, rec->ip);
6210 * Do not allow this rec to match again.
6211 * Yeah, it may waste some memory, but will be removed
6212 * if/when the hash is modified again.
6219 /* Clear any records from hashs */
6220 static void clear_mod_from_hashes(struct ftrace_page *pg)
6222 struct trace_array *tr;
6224 mutex_lock(&trace_types_lock);
6225 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6226 if (!tr->ops || !tr->ops->func_hash)
6228 mutex_lock(&tr->ops->func_hash->regex_lock);
6229 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
6230 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
6231 mutex_unlock(&tr->ops->func_hash->regex_lock);
6233 mutex_unlock(&trace_types_lock);
6236 static void ftrace_free_mod_map(struct rcu_head *rcu)
6238 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
6239 struct ftrace_mod_func *mod_func;
6240 struct ftrace_mod_func *n;
6242 /* All the contents of mod_map are now not visible to readers */
6243 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
6244 kfree(mod_func->name);
6245 list_del(&mod_func->list);
6252 void ftrace_release_mod(struct module *mod)
6254 struct ftrace_mod_map *mod_map;
6255 struct ftrace_mod_map *n;
6256 struct dyn_ftrace *rec;
6257 struct ftrace_page **last_pg;
6258 struct ftrace_page *tmp_page = NULL;
6259 struct ftrace_page *pg;
6262 mutex_lock(&ftrace_lock);
6264 if (ftrace_disabled)
6267 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
6268 if (mod_map->mod == mod) {
6269 list_del_rcu(&mod_map->list);
6270 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
6276 * Each module has its own ftrace_pages, remove
6277 * them from the list.
6279 last_pg = &ftrace_pages_start;
6280 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
6281 rec = &pg->records[0];
6282 if (within_module_core(rec->ip, mod) ||
6283 within_module_init(rec->ip, mod)) {
6285 * As core pages are first, the first
6286 * page should never be a module page.
6288 if (WARN_ON(pg == ftrace_pages_start))
6291 /* Check if we are deleting the last page */
6292 if (pg == ftrace_pages)
6293 ftrace_pages = next_to_ftrace_page(last_pg);
6295 ftrace_update_tot_cnt -= pg->index;
6296 *last_pg = pg->next;
6298 pg->next = tmp_page;
6301 last_pg = &pg->next;
6304 mutex_unlock(&ftrace_lock);
6306 for (pg = tmp_page; pg; pg = tmp_page) {
6308 /* Needs to be called outside of ftrace_lock */
6309 clear_mod_from_hashes(pg);
6311 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6312 free_pages((unsigned long)pg->records, order);
6313 tmp_page = pg->next;
6315 ftrace_number_of_pages -= 1 << order;
6316 ftrace_number_of_groups--;
6320 void ftrace_module_enable(struct module *mod)
6322 struct dyn_ftrace *rec;
6323 struct ftrace_page *pg;
6325 mutex_lock(&ftrace_lock);
6327 if (ftrace_disabled)
6331 * If the tracing is enabled, go ahead and enable the record.
6333 * The reason not to enable the record immediately is the
6334 * inherent check of ftrace_make_nop/ftrace_make_call for
6335 * correct previous instructions. Making first the NOP
6336 * conversion puts the module to the correct state, thus
6337 * passing the ftrace_make_call check.
6339 * We also delay this to after the module code already set the
6340 * text to read-only, as we now need to set it back to read-write
6341 * so that we can modify the text.
6343 if (ftrace_start_up)
6344 ftrace_arch_code_modify_prepare();
6346 do_for_each_ftrace_rec(pg, rec) {
6349 * do_for_each_ftrace_rec() is a double loop.
6350 * module text shares the pg. If a record is
6351 * not part of this module, then skip this pg,
6352 * which the "break" will do.
6354 if (!within_module_core(rec->ip, mod) &&
6355 !within_module_init(rec->ip, mod))
6361 * When adding a module, we need to check if tracers are
6362 * currently enabled and if they are, and can trace this record,
6363 * we need to enable the module functions as well as update the
6364 * reference counts for those function records.
6366 if (ftrace_start_up)
6367 cnt += referenced_filters(rec);
6369 /* This clears FTRACE_FL_DISABLED */
6372 if (ftrace_start_up && cnt) {
6373 int failed = __ftrace_replace_code(rec, 1);
6375 ftrace_bug(failed, rec);
6380 } while_for_each_ftrace_rec();
6383 if (ftrace_start_up)
6384 ftrace_arch_code_modify_post_process();
6387 mutex_unlock(&ftrace_lock);
6389 process_cached_mods(mod->name);
6392 void ftrace_module_init(struct module *mod)
6394 if (ftrace_disabled || !mod->num_ftrace_callsites)
6397 ftrace_process_locs(mod, mod->ftrace_callsites,
6398 mod->ftrace_callsites + mod->num_ftrace_callsites);
6401 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6402 struct dyn_ftrace *rec)
6404 struct ftrace_mod_func *mod_func;
6405 unsigned long symsize;
6406 unsigned long offset;
6407 char str[KSYM_SYMBOL_LEN];
6411 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
6415 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
6419 mod_func->name = kstrdup(str, GFP_KERNEL);
6420 if (!mod_func->name) {
6425 mod_func->ip = rec->ip - offset;
6426 mod_func->size = symsize;
6428 mod_map->num_funcs++;
6430 list_add_rcu(&mod_func->list, &mod_map->funcs);
6433 static struct ftrace_mod_map *
6434 allocate_ftrace_mod_map(struct module *mod,
6435 unsigned long start, unsigned long end)
6437 struct ftrace_mod_map *mod_map;
6439 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
6444 mod_map->start_addr = start;
6445 mod_map->end_addr = end;
6446 mod_map->num_funcs = 0;
6448 INIT_LIST_HEAD_RCU(&mod_map->funcs);
6450 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
6456 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
6457 unsigned long addr, unsigned long *size,
6458 unsigned long *off, char *sym)
6460 struct ftrace_mod_func *found_func = NULL;
6461 struct ftrace_mod_func *mod_func;
6463 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6464 if (addr >= mod_func->ip &&
6465 addr < mod_func->ip + mod_func->size) {
6466 found_func = mod_func;
6473 *size = found_func->size;
6475 *off = addr - found_func->ip;
6477 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
6479 return found_func->name;
6486 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6487 unsigned long *off, char **modname, char *sym)
6489 struct ftrace_mod_map *mod_map;
6490 const char *ret = NULL;
6492 /* mod_map is freed via call_rcu() */
6494 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6495 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6498 *modname = mod_map->mod->name;
6507 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6508 char *type, char *name,
6509 char *module_name, int *exported)
6511 struct ftrace_mod_map *mod_map;
6512 struct ftrace_mod_func *mod_func;
6515 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6517 if (symnum >= mod_map->num_funcs) {
6518 symnum -= mod_map->num_funcs;
6522 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6528 *value = mod_func->ip;
6530 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6531 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6544 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6545 struct dyn_ftrace *rec) { }
6546 static inline struct ftrace_mod_map *
6547 allocate_ftrace_mod_map(struct module *mod,
6548 unsigned long start, unsigned long end)
6552 #endif /* CONFIG_MODULES */
6554 struct ftrace_init_func {
6555 struct list_head list;
6559 /* Clear any init ips from hashes */
6561 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6563 struct ftrace_func_entry *entry;
6565 entry = ftrace_lookup_ip(hash, func->ip);
6567 * Do not allow this rec to match again.
6568 * Yeah, it may waste some memory, but will be removed
6569 * if/when the hash is modified again.
6576 clear_func_from_hashes(struct ftrace_init_func *func)
6578 struct trace_array *tr;
6580 mutex_lock(&trace_types_lock);
6581 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6582 if (!tr->ops || !tr->ops->func_hash)
6584 mutex_lock(&tr->ops->func_hash->regex_lock);
6585 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6586 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6587 mutex_unlock(&tr->ops->func_hash->regex_lock);
6589 mutex_unlock(&trace_types_lock);
6592 static void add_to_clear_hash_list(struct list_head *clear_list,
6593 struct dyn_ftrace *rec)
6595 struct ftrace_init_func *func;
6597 func = kmalloc(sizeof(*func), GFP_KERNEL);
6599 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6604 list_add(&func->list, clear_list);
6607 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
6609 unsigned long start = (unsigned long)(start_ptr);
6610 unsigned long end = (unsigned long)(end_ptr);
6611 struct ftrace_page **last_pg = &ftrace_pages_start;
6612 struct ftrace_page *pg;
6613 struct dyn_ftrace *rec;
6614 struct dyn_ftrace key;
6615 struct ftrace_mod_map *mod_map = NULL;
6616 struct ftrace_init_func *func, *func_next;
6617 struct list_head clear_hash;
6620 INIT_LIST_HEAD(&clear_hash);
6623 key.flags = end; /* overload flags, as it is unsigned long */
6625 mutex_lock(&ftrace_lock);
6628 * If we are freeing module init memory, then check if
6629 * any tracer is active. If so, we need to save a mapping of
6630 * the module functions being freed with the address.
6632 if (mod && ftrace_ops_list != &ftrace_list_end)
6633 mod_map = allocate_ftrace_mod_map(mod, start, end);
6635 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6636 if (end < pg->records[0].ip ||
6637 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6640 rec = bsearch(&key, pg->records, pg->index,
6641 sizeof(struct dyn_ftrace),
6646 /* rec will be cleared from hashes after ftrace_lock unlock */
6647 add_to_clear_hash_list(&clear_hash, rec);
6650 save_ftrace_mod_rec(mod_map, rec);
6653 ftrace_update_tot_cnt--;
6655 *last_pg = pg->next;
6656 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6657 free_pages((unsigned long)pg->records, order);
6658 ftrace_number_of_pages -= 1 << order;
6659 ftrace_number_of_groups--;
6661 pg = container_of(last_pg, struct ftrace_page, next);
6666 memmove(rec, rec + 1,
6667 (pg->index - (rec - pg->records)) * sizeof(*rec));
6668 /* More than one function may be in this block */
6671 mutex_unlock(&ftrace_lock);
6673 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6674 clear_func_from_hashes(func);
6679 void __init ftrace_free_init_mem(void)
6681 void *start = (void *)(&__init_begin);
6682 void *end = (void *)(&__init_end);
6684 ftrace_free_mem(NULL, start, end);
6687 void __init ftrace_init(void)
6689 extern unsigned long __start_mcount_loc[];
6690 extern unsigned long __stop_mcount_loc[];
6691 unsigned long count, flags;
6694 local_irq_save(flags);
6695 ret = ftrace_dyn_arch_init();
6696 local_irq_restore(flags);
6700 count = __stop_mcount_loc - __start_mcount_loc;
6702 pr_info("ftrace: No functions to be traced?\n");
6706 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6707 count, count / ENTRIES_PER_PAGE + 1);
6709 last_ftrace_enabled = ftrace_enabled = 1;
6711 ret = ftrace_process_locs(NULL,
6715 pr_info("ftrace: allocated %ld pages with %ld groups\n",
6716 ftrace_number_of_pages, ftrace_number_of_groups);
6718 set_ftrace_early_filters();
6722 ftrace_disabled = 1;
6725 /* Do nothing if arch does not support this */
6726 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6730 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6732 arch_ftrace_update_trampoline(ops);
6735 void ftrace_init_trace_array(struct trace_array *tr)
6737 INIT_LIST_HEAD(&tr->func_probes);
6738 INIT_LIST_HEAD(&tr->mod_trace);
6739 INIT_LIST_HEAD(&tr->mod_notrace);
6743 struct ftrace_ops global_ops = {
6744 .func = ftrace_stub,
6745 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6746 FTRACE_OPS_FL_INITIALIZED |
6750 static int __init ftrace_nodyn_init(void)
6755 core_initcall(ftrace_nodyn_init);
6757 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6758 static inline void ftrace_startup_enable(int command) { }
6759 static inline void ftrace_startup_all(int command) { }
6761 # define ftrace_startup_sysctl() do { } while (0)
6762 # define ftrace_shutdown_sysctl() do { } while (0)
6764 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6768 #endif /* CONFIG_DYNAMIC_FTRACE */
6770 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6772 tr->ops = &global_ops;
6773 tr->ops->private = tr;
6774 ftrace_init_trace_array(tr);
6777 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6779 /* If we filter on pids, update to use the pid function */
6780 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6781 if (WARN_ON(tr->ops->func != ftrace_stub))
6782 printk("ftrace ops had %pS for function\n",
6785 tr->ops->func = func;
6786 tr->ops->private = tr;
6789 void ftrace_reset_array_ops(struct trace_array *tr)
6791 tr->ops->func = ftrace_stub;
6794 static nokprobe_inline void
6795 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6796 struct ftrace_ops *ignored, struct pt_regs *regs)
6798 struct ftrace_ops *op;
6801 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6806 * Some of the ops may be dynamically allocated,
6807 * they must be freed after a synchronize_rcu().
6809 preempt_disable_notrace();
6811 do_for_each_ftrace_op(op, ftrace_ops_list) {
6812 /* Stub functions don't need to be called nor tested */
6813 if (op->flags & FTRACE_OPS_FL_STUB)
6816 * Check the following for each ops before calling their func:
6817 * if RCU flag is set, then rcu_is_watching() must be true
6818 * if PER_CPU is set, then ftrace_function_local_disable()
6820 * Otherwise test if the ip matches the ops filter
6822 * If any of the above fails then the op->func() is not executed.
6824 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6825 ftrace_ops_test(op, ip, regs)) {
6826 if (FTRACE_WARN_ON(!op->func)) {
6827 pr_warn("op=%p %pS\n", op, op);
6830 op->func(ip, parent_ip, op, regs);
6832 } while_for_each_ftrace_op(op);
6834 preempt_enable_notrace();
6835 trace_clear_recursion(bit);
6839 * Some archs only support passing ip and parent_ip. Even though
6840 * the list function ignores the op parameter, we do not want any
6841 * C side effects, where a function is called without the caller
6842 * sending a third parameter.
6843 * Archs are to support both the regs and ftrace_ops at the same time.
6844 * If they support ftrace_ops, it is assumed they support regs.
6845 * If call backs want to use regs, they must either check for regs
6846 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6847 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6848 * An architecture can pass partial regs with ftrace_ops and still
6849 * set the ARCH_SUPPORTS_FTRACE_OPS.
6851 #if ARCH_SUPPORTS_FTRACE_OPS
6852 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6853 struct ftrace_ops *op, struct pt_regs *regs)
6855 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6857 NOKPROBE_SYMBOL(ftrace_ops_list_func);
6859 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6861 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6863 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
6867 * If there's only one function registered but it does not support
6868 * recursion, needs RCU protection and/or requires per cpu handling, then
6869 * this function will be called by the mcount trampoline.
6871 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6872 struct ftrace_ops *op, struct pt_regs *regs)
6876 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6879 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6883 preempt_disable_notrace();
6885 op->func(ip, parent_ip, op, regs);
6887 preempt_enable_notrace();
6888 trace_clear_recursion(bit);
6890 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
6893 * ftrace_ops_get_func - get the function a trampoline should call
6894 * @ops: the ops to get the function for
6896 * Normally the mcount trampoline will call the ops->func, but there
6897 * are times that it should not. For example, if the ops does not
6898 * have its own recursion protection, then it should call the
6899 * ftrace_ops_assist_func() instead.
6901 * Returns the function that the trampoline should call for @ops.
6903 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6906 * If the function does not handle recursion, needs to be RCU safe,
6907 * or does per cpu logic, then we need to call the assist handler.
6909 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6910 ops->flags & FTRACE_OPS_FL_RCU)
6911 return ftrace_ops_assist_func;
6917 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6918 struct task_struct *prev, struct task_struct *next)
6920 struct trace_array *tr = data;
6921 struct trace_pid_list *pid_list;
6923 pid_list = rcu_dereference_sched(tr->function_pids);
6925 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6926 trace_ignore_this_task(pid_list, next));
6930 ftrace_pid_follow_sched_process_fork(void *data,
6931 struct task_struct *self,
6932 struct task_struct *task)
6934 struct trace_pid_list *pid_list;
6935 struct trace_array *tr = data;
6937 pid_list = rcu_dereference_sched(tr->function_pids);
6938 trace_filter_add_remove_task(pid_list, self, task);
6942 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6944 struct trace_pid_list *pid_list;
6945 struct trace_array *tr = data;
6947 pid_list = rcu_dereference_sched(tr->function_pids);
6948 trace_filter_add_remove_task(pid_list, NULL, task);
6951 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6954 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6956 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6959 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6961 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6966 static void clear_ftrace_pids(struct trace_array *tr)
6968 struct trace_pid_list *pid_list;
6971 pid_list = rcu_dereference_protected(tr->function_pids,
6972 lockdep_is_held(&ftrace_lock));
6976 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6978 for_each_possible_cpu(cpu)
6979 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6981 rcu_assign_pointer(tr->function_pids, NULL);
6983 /* Wait till all users are no longer using pid filtering */
6986 trace_free_pid_list(pid_list);
6989 void ftrace_clear_pids(struct trace_array *tr)
6991 mutex_lock(&ftrace_lock);
6993 clear_ftrace_pids(tr);
6995 mutex_unlock(&ftrace_lock);
6998 static void ftrace_pid_reset(struct trace_array *tr)
7000 mutex_lock(&ftrace_lock);
7001 clear_ftrace_pids(tr);
7003 ftrace_update_pid_func();
7004 ftrace_startup_all(0);
7006 mutex_unlock(&ftrace_lock);
7009 /* Greater than any max PID */
7010 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
7012 static void *fpid_start(struct seq_file *m, loff_t *pos)
7015 struct trace_pid_list *pid_list;
7016 struct trace_array *tr = m->private;
7018 mutex_lock(&ftrace_lock);
7019 rcu_read_lock_sched();
7021 pid_list = rcu_dereference_sched(tr->function_pids);
7024 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7026 return trace_pid_start(pid_list, pos);
7029 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
7031 struct trace_array *tr = m->private;
7032 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
7034 if (v == FTRACE_NO_PIDS)
7037 return trace_pid_next(pid_list, v, pos);
7040 static void fpid_stop(struct seq_file *m, void *p)
7043 rcu_read_unlock_sched();
7044 mutex_unlock(&ftrace_lock);
7047 static int fpid_show(struct seq_file *m, void *v)
7049 if (v == FTRACE_NO_PIDS) {
7050 seq_puts(m, "no pid\n");
7054 return trace_pid_show(m, v);
7057 static const struct seq_operations ftrace_pid_sops = {
7058 .start = fpid_start,
7065 ftrace_pid_open(struct inode *inode, struct file *file)
7067 struct trace_array *tr = inode->i_private;
7071 ret = tracing_check_open_get_tr(tr);
7075 if ((file->f_mode & FMODE_WRITE) &&
7076 (file->f_flags & O_TRUNC))
7077 ftrace_pid_reset(tr);
7079 ret = seq_open(file, &ftrace_pid_sops);
7081 trace_array_put(tr);
7083 m = file->private_data;
7084 /* copy tr over to seq ops */
7091 static void ignore_task_cpu(void *data)
7093 struct trace_array *tr = data;
7094 struct trace_pid_list *pid_list;
7097 * This function is called by on_each_cpu() while the
7098 * event_mutex is held.
7100 pid_list = rcu_dereference_protected(tr->function_pids,
7101 mutex_is_locked(&ftrace_lock));
7103 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
7104 trace_ignore_this_task(pid_list, current));
7108 ftrace_pid_write(struct file *filp, const char __user *ubuf,
7109 size_t cnt, loff_t *ppos)
7111 struct seq_file *m = filp->private_data;
7112 struct trace_array *tr = m->private;
7113 struct trace_pid_list *filtered_pids = NULL;
7114 struct trace_pid_list *pid_list;
7120 mutex_lock(&ftrace_lock);
7122 filtered_pids = rcu_dereference_protected(tr->function_pids,
7123 lockdep_is_held(&ftrace_lock));
7125 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
7129 rcu_assign_pointer(tr->function_pids, pid_list);
7131 if (filtered_pids) {
7133 trace_free_pid_list(filtered_pids);
7134 } else if (pid_list) {
7135 /* Register a probe to set whether to ignore the tracing of a task */
7136 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7140 * Ignoring of pids is done at task switch. But we have to
7141 * check for those tasks that are currently running.
7142 * Always do this in case a pid was appended or removed.
7144 on_each_cpu(ignore_task_cpu, tr, 1);
7146 ftrace_update_pid_func();
7147 ftrace_startup_all(0);
7149 mutex_unlock(&ftrace_lock);
7158 ftrace_pid_release(struct inode *inode, struct file *file)
7160 struct trace_array *tr = inode->i_private;
7162 trace_array_put(tr);
7164 return seq_release(inode, file);
7167 static const struct file_operations ftrace_pid_fops = {
7168 .open = ftrace_pid_open,
7169 .write = ftrace_pid_write,
7171 .llseek = tracing_lseek,
7172 .release = ftrace_pid_release,
7175 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
7177 trace_create_file("set_ftrace_pid", 0644, d_tracer,
7178 tr, &ftrace_pid_fops);
7181 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
7182 struct dentry *d_tracer)
7184 /* Only the top level directory has the dyn_tracefs and profile */
7185 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
7187 ftrace_init_dyn_tracefs(d_tracer);
7188 ftrace_profile_tracefs(d_tracer);
7192 * ftrace_kill - kill ftrace
7194 * This function should be used by panic code. It stops ftrace
7195 * but in a not so nice way. If you need to simply kill ftrace
7196 * from a non-atomic section, use ftrace_kill.
7198 void ftrace_kill(void)
7200 ftrace_disabled = 1;
7202 ftrace_trace_function = ftrace_stub;
7206 * Test if ftrace is dead or not.
7208 int ftrace_is_dead(void)
7210 return ftrace_disabled;
7214 * register_ftrace_function - register a function for profiling
7215 * @ops - ops structure that holds the function for profiling.
7217 * Register a function to be called by all functions in the
7220 * Note: @ops->func and all the functions it calls must be labeled
7221 * with "notrace", otherwise it will go into a
7224 int register_ftrace_function(struct ftrace_ops *ops)
7228 ftrace_ops_init(ops);
7230 mutex_lock(&ftrace_lock);
7232 ret = ftrace_startup(ops, 0);
7234 mutex_unlock(&ftrace_lock);
7238 EXPORT_SYMBOL_GPL(register_ftrace_function);
7241 * unregister_ftrace_function - unregister a function for profiling.
7242 * @ops - ops structure that holds the function to unregister
7244 * Unregister a function that was added to be called by ftrace profiling.
7246 int unregister_ftrace_function(struct ftrace_ops *ops)
7250 mutex_lock(&ftrace_lock);
7251 ret = ftrace_shutdown(ops, 0);
7252 mutex_unlock(&ftrace_lock);
7256 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
7258 static bool is_permanent_ops_registered(void)
7260 struct ftrace_ops *op;
7262 do_for_each_ftrace_op(op, ftrace_ops_list) {
7263 if (op->flags & FTRACE_OPS_FL_PERMANENT)
7265 } while_for_each_ftrace_op(op);
7271 ftrace_enable_sysctl(struct ctl_table *table, int write,
7272 void __user *buffer, size_t *lenp,
7277 mutex_lock(&ftrace_lock);
7279 if (unlikely(ftrace_disabled))
7282 ret = proc_dointvec(table, write, buffer, lenp, ppos);
7284 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
7287 if (ftrace_enabled) {
7289 /* we are starting ftrace again */
7290 if (rcu_dereference_protected(ftrace_ops_list,
7291 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
7292 update_ftrace_function();
7294 ftrace_startup_sysctl();
7297 if (is_permanent_ops_registered()) {
7298 ftrace_enabled = true;
7303 /* stopping ftrace calls (just send to ftrace_stub) */
7304 ftrace_trace_function = ftrace_stub;
7306 ftrace_shutdown_sysctl();
7309 last_ftrace_enabled = !!ftrace_enabled;
7311 mutex_unlock(&ftrace_lock);