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/seq_file.h>
22 #include <linux/suspend.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 "trace_output.h"
45 #include "trace_stat.h"
47 #define FTRACE_WARN_ON(cond) \
55 #define FTRACE_WARN_ON_ONCE(cond) \
58 if (WARN_ON_ONCE(___r)) \
63 /* hash bits for specific function selection */
64 #define FTRACE_HASH_BITS 7
65 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
66 #define FTRACE_HASH_DEFAULT_BITS 10
67 #define FTRACE_HASH_MAX_BITS 12
69 #ifdef CONFIG_DYNAMIC_FTRACE
70 #define INIT_OPS_HASH(opsname) \
71 .func_hash = &opsname.local_hash, \
72 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
73 #define ASSIGN_OPS_HASH(opsname, val) \
75 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
77 #define INIT_OPS_HASH(opsname)
78 #define ASSIGN_OPS_HASH(opsname, val)
81 static struct ftrace_ops ftrace_list_end __read_mostly = {
83 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
84 INIT_OPS_HASH(ftrace_list_end)
87 /* ftrace_enabled is a method to turn ftrace on or off */
88 int ftrace_enabled __read_mostly;
89 static int last_ftrace_enabled;
91 /* Current function tracing op */
92 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
93 /* What to set function_trace_op to */
94 static struct ftrace_ops *set_function_trace_op;
96 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
98 struct trace_array *tr;
100 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
105 return tr->function_pids != NULL;
108 static void ftrace_update_trampoline(struct ftrace_ops *ops);
111 * ftrace_disabled is set when an anomaly is discovered.
112 * ftrace_disabled is much stronger than ftrace_enabled.
114 static int ftrace_disabled __read_mostly;
116 static DEFINE_MUTEX(ftrace_lock);
118 static struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
119 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
120 static struct ftrace_ops global_ops;
122 #if ARCH_SUPPORTS_FTRACE_OPS
123 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
124 struct ftrace_ops *op, struct pt_regs *regs);
126 /* See comment below, where ftrace_ops_list_func is defined */
127 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
128 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
132 * Traverse the ftrace_global_list, invoking all entries. The reason that we
133 * can use rcu_dereference_raw_notrace() is that elements removed from this list
134 * are simply leaked, so there is no need to interact with a grace-period
135 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
136 * concurrent insertions into the ftrace_global_list.
138 * Silly Alpha and silly pointer-speculation compiler optimizations!
140 #define do_for_each_ftrace_op(op, list) \
141 op = rcu_dereference_raw_notrace(list); \
145 * Optimized for just a single item in the list (as that is the normal case).
147 #define while_for_each_ftrace_op(op) \
148 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
149 unlikely((op) != &ftrace_list_end))
151 static inline void ftrace_ops_init(struct ftrace_ops *ops)
153 #ifdef CONFIG_DYNAMIC_FTRACE
154 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
155 mutex_init(&ops->local_hash.regex_lock);
156 ops->func_hash = &ops->local_hash;
157 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
162 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
163 struct ftrace_ops *op, struct pt_regs *regs)
165 struct trace_array *tr = op->private;
167 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
170 op->saved_func(ip, parent_ip, op, regs);
173 static void ftrace_sync(struct work_struct *work)
176 * This function is just a stub to implement a hard force
177 * of synchronize_sched(). This requires synchronizing
178 * tasks even in userspace and idle.
180 * Yes, function tracing is rude.
184 static void ftrace_sync_ipi(void *data)
186 /* Probably not needed, but do it anyway */
190 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
191 static void update_function_graph_func(void);
193 /* Both enabled by default (can be cleared by function_graph tracer flags */
194 static bool fgraph_sleep_time = true;
195 static bool fgraph_graph_time = true;
198 static inline void update_function_graph_func(void) { }
202 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
205 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
206 * then it needs to call the list anyway.
208 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
209 FTRACE_FORCE_LIST_FUNC)
210 return ftrace_ops_list_func;
212 return ftrace_ops_get_func(ops);
215 static void update_ftrace_function(void)
220 * Prepare the ftrace_ops that the arch callback will use.
221 * If there's only one ftrace_ops registered, the ftrace_ops_list
222 * will point to the ops we want.
224 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
225 lockdep_is_held(&ftrace_lock));
227 /* If there's no ftrace_ops registered, just call the stub function */
228 if (set_function_trace_op == &ftrace_list_end) {
232 * If we are at the end of the list and this ops is
233 * recursion safe and not dynamic and the arch supports passing ops,
234 * then have the mcount trampoline call the function directly.
236 } else if (rcu_dereference_protected(ftrace_ops_list->next,
237 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
238 func = ftrace_ops_get_list_func(ftrace_ops_list);
241 /* Just use the default ftrace_ops */
242 set_function_trace_op = &ftrace_list_end;
243 func = ftrace_ops_list_func;
246 update_function_graph_func();
248 /* If there's no change, then do nothing more here */
249 if (ftrace_trace_function == func)
253 * If we are using the list function, it doesn't care
254 * about the function_trace_ops.
256 if (func == ftrace_ops_list_func) {
257 ftrace_trace_function = func;
259 * Don't even bother setting function_trace_ops,
260 * it would be racy to do so anyway.
265 #ifndef CONFIG_DYNAMIC_FTRACE
267 * For static tracing, we need to be a bit more careful.
268 * The function change takes affect immediately. Thus,
269 * we need to coorditate the setting of the function_trace_ops
270 * with the setting of the ftrace_trace_function.
272 * Set the function to the list ops, which will call the
273 * function we want, albeit indirectly, but it handles the
274 * ftrace_ops and doesn't depend on function_trace_op.
276 ftrace_trace_function = ftrace_ops_list_func;
278 * Make sure all CPUs see this. Yes this is slow, but static
279 * tracing is slow and nasty to have enabled.
281 schedule_on_each_cpu(ftrace_sync);
282 /* Now all cpus are using the list ops. */
283 function_trace_op = set_function_trace_op;
284 /* Make sure the function_trace_op is visible on all CPUs */
286 /* Nasty way to force a rmb on all cpus */
287 smp_call_function(ftrace_sync_ipi, NULL, 1);
288 /* OK, we are all set to update the ftrace_trace_function now! */
289 #endif /* !CONFIG_DYNAMIC_FTRACE */
291 ftrace_trace_function = func;
294 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
295 struct ftrace_ops *ops)
297 rcu_assign_pointer(ops->next, *list);
300 * We are entering ops into the list but another
301 * CPU might be walking that list. We need to make sure
302 * the ops->next pointer is valid before another CPU sees
303 * the ops pointer included into the list.
305 rcu_assign_pointer(*list, ops);
308 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
309 struct ftrace_ops *ops)
311 struct ftrace_ops **p;
314 * If we are removing the last function, then simply point
315 * to the ftrace_stub.
317 if (rcu_dereference_protected(*list,
318 lockdep_is_held(&ftrace_lock)) == ops &&
319 rcu_dereference_protected(ops->next,
320 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
321 *list = &ftrace_list_end;
325 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
336 static void ftrace_update_trampoline(struct ftrace_ops *ops);
338 static int __register_ftrace_function(struct ftrace_ops *ops)
340 if (ops->flags & FTRACE_OPS_FL_DELETED)
343 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
346 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
348 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
349 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
350 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
352 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
353 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
356 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
357 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
360 if (!core_kernel_data((unsigned long)ops))
361 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
363 add_ftrace_ops(&ftrace_ops_list, ops);
365 /* Always save the function, and reset at unregistering */
366 ops->saved_func = ops->func;
368 if (ftrace_pids_enabled(ops))
369 ops->func = ftrace_pid_func;
371 ftrace_update_trampoline(ops);
374 update_ftrace_function();
379 static int __unregister_ftrace_function(struct ftrace_ops *ops)
383 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
386 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
392 update_ftrace_function();
394 ops->func = ops->saved_func;
399 static void ftrace_update_pid_func(void)
401 struct ftrace_ops *op;
403 /* Only do something if we are tracing something */
404 if (ftrace_trace_function == ftrace_stub)
407 do_for_each_ftrace_op(op, ftrace_ops_list) {
408 if (op->flags & FTRACE_OPS_FL_PID) {
409 op->func = ftrace_pids_enabled(op) ?
410 ftrace_pid_func : op->saved_func;
411 ftrace_update_trampoline(op);
413 } while_for_each_ftrace_op(op);
415 update_ftrace_function();
418 #ifdef CONFIG_FUNCTION_PROFILER
419 struct ftrace_profile {
420 struct hlist_node node;
422 unsigned long counter;
423 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
424 unsigned long long time;
425 unsigned long long time_squared;
429 struct ftrace_profile_page {
430 struct ftrace_profile_page *next;
432 struct ftrace_profile records[];
435 struct ftrace_profile_stat {
437 struct hlist_head *hash;
438 struct ftrace_profile_page *pages;
439 struct ftrace_profile_page *start;
440 struct tracer_stat stat;
443 #define PROFILE_RECORDS_SIZE \
444 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
446 #define PROFILES_PER_PAGE \
447 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
449 static int ftrace_profile_enabled __read_mostly;
451 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
452 static DEFINE_MUTEX(ftrace_profile_lock);
454 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
456 #define FTRACE_PROFILE_HASH_BITS 10
457 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
460 function_stat_next(void *v, int idx)
462 struct ftrace_profile *rec = v;
463 struct ftrace_profile_page *pg;
465 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
471 if ((void *)rec >= (void *)&pg->records[pg->index]) {
475 rec = &pg->records[0];
483 static void *function_stat_start(struct tracer_stat *trace)
485 struct ftrace_profile_stat *stat =
486 container_of(trace, struct ftrace_profile_stat, stat);
488 if (!stat || !stat->start)
491 return function_stat_next(&stat->start->records[0], 0);
494 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
495 /* function graph compares on total time */
496 static int function_stat_cmp(void *p1, void *p2)
498 struct ftrace_profile *a = p1;
499 struct ftrace_profile *b = p2;
501 if (a->time < b->time)
503 if (a->time > b->time)
509 /* not function graph compares against hits */
510 static int function_stat_cmp(void *p1, void *p2)
512 struct ftrace_profile *a = p1;
513 struct ftrace_profile *b = p2;
515 if (a->counter < b->counter)
517 if (a->counter > b->counter)
524 static int function_stat_headers(struct seq_file *m)
526 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
527 seq_puts(m, " Function "
530 "--- ---- --- ---\n");
532 seq_puts(m, " Function Hit\n"
538 static int function_stat_show(struct seq_file *m, void *v)
540 struct ftrace_profile *rec = v;
541 char str[KSYM_SYMBOL_LEN];
543 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
544 static struct trace_seq s;
545 unsigned long long avg;
546 unsigned long long stddev;
548 mutex_lock(&ftrace_profile_lock);
550 /* we raced with function_profile_reset() */
551 if (unlikely(rec->counter == 0)) {
556 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
557 avg = div64_ul(rec->time, rec->counter);
558 if (tracing_thresh && (avg < tracing_thresh))
562 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
563 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
565 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
568 /* Sample standard deviation (s^2) */
569 if (rec->counter <= 1)
573 * Apply Welford's method:
574 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
576 stddev = rec->counter * rec->time_squared -
577 rec->time * rec->time;
580 * Divide only 1000 for ns^2 -> us^2 conversion.
581 * trace_print_graph_duration will divide 1000 again.
583 stddev = div64_ul(stddev,
584 rec->counter * (rec->counter - 1) * 1000);
588 trace_print_graph_duration(rec->time, &s);
589 trace_seq_puts(&s, " ");
590 trace_print_graph_duration(avg, &s);
591 trace_seq_puts(&s, " ");
592 trace_print_graph_duration(stddev, &s);
593 trace_print_seq(m, &s);
597 mutex_unlock(&ftrace_profile_lock);
602 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
604 struct ftrace_profile_page *pg;
606 pg = stat->pages = stat->start;
609 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
614 memset(stat->hash, 0,
615 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
618 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
620 struct ftrace_profile_page *pg;
625 /* If we already allocated, do nothing */
629 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
633 #ifdef CONFIG_DYNAMIC_FTRACE
634 functions = ftrace_update_tot_cnt;
637 * We do not know the number of functions that exist because
638 * dynamic tracing is what counts them. With past experience
639 * we have around 20K functions. That should be more than enough.
640 * It is highly unlikely we will execute every function in
646 pg = stat->start = stat->pages;
648 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
650 for (i = 1; i < pages; i++) {
651 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
662 unsigned long tmp = (unsigned long)pg;
674 static int ftrace_profile_init_cpu(int cpu)
676 struct ftrace_profile_stat *stat;
679 stat = &per_cpu(ftrace_profile_stats, cpu);
682 /* If the profile is already created, simply reset it */
683 ftrace_profile_reset(stat);
688 * We are profiling all functions, but usually only a few thousand
689 * functions are hit. We'll make a hash of 1024 items.
691 size = FTRACE_PROFILE_HASH_SIZE;
693 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
698 /* Preallocate the function profiling pages */
699 if (ftrace_profile_pages_init(stat) < 0) {
708 static int ftrace_profile_init(void)
713 for_each_possible_cpu(cpu) {
714 ret = ftrace_profile_init_cpu(cpu);
722 /* interrupts must be disabled */
723 static struct ftrace_profile *
724 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
726 struct ftrace_profile *rec;
727 struct hlist_head *hhd;
730 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
731 hhd = &stat->hash[key];
733 if (hlist_empty(hhd))
736 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
744 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
745 struct ftrace_profile *rec)
749 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
750 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
754 * The memory is already allocated, this simply finds a new record to use.
756 static struct ftrace_profile *
757 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
759 struct ftrace_profile *rec = NULL;
761 /* prevent recursion (from NMIs) */
762 if (atomic_inc_return(&stat->disabled) != 1)
766 * Try to find the function again since an NMI
767 * could have added it
769 rec = ftrace_find_profiled_func(stat, ip);
773 if (stat->pages->index == PROFILES_PER_PAGE) {
774 if (!stat->pages->next)
776 stat->pages = stat->pages->next;
779 rec = &stat->pages->records[stat->pages->index++];
781 ftrace_add_profile(stat, rec);
784 atomic_dec(&stat->disabled);
790 function_profile_call(unsigned long ip, unsigned long parent_ip,
791 struct ftrace_ops *ops, struct pt_regs *regs)
793 struct ftrace_profile_stat *stat;
794 struct ftrace_profile *rec;
797 if (!ftrace_profile_enabled)
800 local_irq_save(flags);
802 stat = this_cpu_ptr(&ftrace_profile_stats);
803 if (!stat->hash || !ftrace_profile_enabled)
806 rec = ftrace_find_profiled_func(stat, ip);
808 rec = ftrace_profile_alloc(stat, ip);
815 local_irq_restore(flags);
818 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
819 static int profile_graph_entry(struct ftrace_graph_ent *trace)
821 int index = current->curr_ret_stack;
823 function_profile_call(trace->func, 0, NULL, NULL);
825 /* If function graph is shutting down, ret_stack can be NULL */
826 if (!current->ret_stack)
829 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
830 current->ret_stack[index].subtime = 0;
835 static void profile_graph_return(struct ftrace_graph_ret *trace)
837 struct ftrace_profile_stat *stat;
838 unsigned long long calltime;
839 struct ftrace_profile *rec;
842 local_irq_save(flags);
843 stat = this_cpu_ptr(&ftrace_profile_stats);
844 if (!stat->hash || !ftrace_profile_enabled)
847 /* If the calltime was zero'd ignore it */
848 if (!trace->calltime)
851 calltime = trace->rettime - trace->calltime;
853 if (!fgraph_graph_time) {
856 index = current->curr_ret_stack;
858 /* Append this call time to the parent time to subtract */
860 current->ret_stack[index - 1].subtime += calltime;
862 if (current->ret_stack[index].subtime < calltime)
863 calltime -= current->ret_stack[index].subtime;
868 rec = ftrace_find_profiled_func(stat, trace->func);
870 rec->time += calltime;
871 rec->time_squared += calltime * calltime;
875 local_irq_restore(flags);
878 static int register_ftrace_profiler(void)
880 return register_ftrace_graph(&profile_graph_return,
881 &profile_graph_entry);
884 static void unregister_ftrace_profiler(void)
886 unregister_ftrace_graph();
889 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
890 .func = function_profile_call,
891 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
892 INIT_OPS_HASH(ftrace_profile_ops)
895 static int register_ftrace_profiler(void)
897 return register_ftrace_function(&ftrace_profile_ops);
900 static void unregister_ftrace_profiler(void)
902 unregister_ftrace_function(&ftrace_profile_ops);
904 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
907 ftrace_profile_write(struct file *filp, const char __user *ubuf,
908 size_t cnt, loff_t *ppos)
913 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
919 mutex_lock(&ftrace_profile_lock);
920 if (ftrace_profile_enabled ^ val) {
922 ret = ftrace_profile_init();
928 ret = register_ftrace_profiler();
933 ftrace_profile_enabled = 1;
935 ftrace_profile_enabled = 0;
937 * unregister_ftrace_profiler calls stop_machine
938 * so this acts like an synchronize_sched.
940 unregister_ftrace_profiler();
944 mutex_unlock(&ftrace_profile_lock);
952 ftrace_profile_read(struct file *filp, char __user *ubuf,
953 size_t cnt, loff_t *ppos)
955 char buf[64]; /* big enough to hold a number */
958 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
959 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
962 static const struct file_operations ftrace_profile_fops = {
963 .open = tracing_open_generic,
964 .read = ftrace_profile_read,
965 .write = ftrace_profile_write,
966 .llseek = default_llseek,
969 /* used to initialize the real stat files */
970 static struct tracer_stat function_stats __initdata = {
972 .stat_start = function_stat_start,
973 .stat_next = function_stat_next,
974 .stat_cmp = function_stat_cmp,
975 .stat_headers = function_stat_headers,
976 .stat_show = function_stat_show
979 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
981 struct ftrace_profile_stat *stat;
982 struct dentry *entry;
987 for_each_possible_cpu(cpu) {
988 stat = &per_cpu(ftrace_profile_stats, cpu);
990 name = kasprintf(GFP_KERNEL, "function%d", cpu);
993 * The files created are permanent, if something happens
994 * we still do not free memory.
997 "Could not allocate stat file for cpu %d\n",
1001 stat->stat = function_stats;
1002 stat->stat.name = name;
1003 ret = register_stat_tracer(&stat->stat);
1006 "Could not register function stat for cpu %d\n",
1013 entry = tracefs_create_file("function_profile_enabled", 0644,
1014 d_tracer, NULL, &ftrace_profile_fops);
1016 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1019 #else /* CONFIG_FUNCTION_PROFILER */
1020 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1023 #endif /* CONFIG_FUNCTION_PROFILER */
1025 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1026 static int ftrace_graph_active;
1028 # define ftrace_graph_active 0
1031 #ifdef CONFIG_DYNAMIC_FTRACE
1033 static struct ftrace_ops *removed_ops;
1036 * Set when doing a global update, like enabling all recs or disabling them.
1037 * It is not set when just updating a single ftrace_ops.
1039 static bool update_all_ops;
1041 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1042 # error Dynamic ftrace depends on MCOUNT_RECORD
1045 struct ftrace_func_entry {
1046 struct hlist_node hlist;
1050 struct ftrace_func_probe {
1051 struct ftrace_probe_ops *probe_ops;
1052 struct ftrace_ops ops;
1053 struct trace_array *tr;
1054 struct list_head list;
1060 * We make these constant because no one should touch them,
1061 * but they are used as the default "empty hash", to avoid allocating
1062 * it all the time. These are in a read only section such that if
1063 * anyone does try to modify it, it will cause an exception.
1065 static const struct hlist_head empty_buckets[1];
1066 static const struct ftrace_hash empty_hash = {
1067 .buckets = (struct hlist_head *)empty_buckets,
1069 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1071 static struct ftrace_ops global_ops = {
1072 .func = ftrace_stub,
1073 .local_hash.notrace_hash = EMPTY_HASH,
1074 .local_hash.filter_hash = EMPTY_HASH,
1075 INIT_OPS_HASH(global_ops)
1076 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1077 FTRACE_OPS_FL_INITIALIZED |
1082 * Used by the stack undwinder to know about dynamic ftrace trampolines.
1084 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1086 struct ftrace_ops *op = NULL;
1089 * Some of the ops may be dynamically allocated,
1090 * they are freed after a synchronize_sched().
1092 preempt_disable_notrace();
1094 do_for_each_ftrace_op(op, ftrace_ops_list) {
1096 * This is to check for dynamically allocated trampolines.
1097 * Trampolines that are in kernel text will have
1098 * core_kernel_text() return true.
1100 if (op->trampoline && op->trampoline_size)
1101 if (addr >= op->trampoline &&
1102 addr < op->trampoline + op->trampoline_size) {
1103 preempt_enable_notrace();
1106 } while_for_each_ftrace_op(op);
1107 preempt_enable_notrace();
1113 * This is used by __kernel_text_address() to return true if the
1114 * address is on a dynamically allocated trampoline that would
1115 * not return true for either core_kernel_text() or
1116 * is_module_text_address().
1118 bool is_ftrace_trampoline(unsigned long addr)
1120 return ftrace_ops_trampoline(addr) != NULL;
1123 struct ftrace_page {
1124 struct ftrace_page *next;
1125 struct dyn_ftrace *records;
1130 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1131 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1133 /* estimate from running different kernels */
1134 #define NR_TO_INIT 10000
1136 static struct ftrace_page *ftrace_pages_start;
1137 static struct ftrace_page *ftrace_pages;
1139 static __always_inline unsigned long
1140 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1142 if (hash->size_bits > 0)
1143 return hash_long(ip, hash->size_bits);
1148 /* Only use this function if ftrace_hash_empty() has already been tested */
1149 static __always_inline struct ftrace_func_entry *
1150 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1153 struct ftrace_func_entry *entry;
1154 struct hlist_head *hhd;
1156 key = ftrace_hash_key(hash, ip);
1157 hhd = &hash->buckets[key];
1159 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1160 if (entry->ip == ip)
1167 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1168 * @hash: The hash to look at
1169 * @ip: The instruction pointer to test
1171 * Search a given @hash to see if a given instruction pointer (@ip)
1174 * Returns the entry that holds the @ip if found. NULL otherwise.
1176 struct ftrace_func_entry *
1177 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1179 if (ftrace_hash_empty(hash))
1182 return __ftrace_lookup_ip(hash, ip);
1185 static void __add_hash_entry(struct ftrace_hash *hash,
1186 struct ftrace_func_entry *entry)
1188 struct hlist_head *hhd;
1191 key = ftrace_hash_key(hash, entry->ip);
1192 hhd = &hash->buckets[key];
1193 hlist_add_head(&entry->hlist, hhd);
1197 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1199 struct ftrace_func_entry *entry;
1201 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1206 __add_hash_entry(hash, entry);
1212 free_hash_entry(struct ftrace_hash *hash,
1213 struct ftrace_func_entry *entry)
1215 hlist_del(&entry->hlist);
1221 remove_hash_entry(struct ftrace_hash *hash,
1222 struct ftrace_func_entry *entry)
1224 hlist_del_rcu(&entry->hlist);
1228 static void ftrace_hash_clear(struct ftrace_hash *hash)
1230 struct hlist_head *hhd;
1231 struct hlist_node *tn;
1232 struct ftrace_func_entry *entry;
1233 int size = 1 << hash->size_bits;
1239 for (i = 0; i < size; i++) {
1240 hhd = &hash->buckets[i];
1241 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1242 free_hash_entry(hash, entry);
1244 FTRACE_WARN_ON(hash->count);
1247 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1249 list_del(&ftrace_mod->list);
1250 kfree(ftrace_mod->module);
1251 kfree(ftrace_mod->func);
1255 static void clear_ftrace_mod_list(struct list_head *head)
1257 struct ftrace_mod_load *p, *n;
1259 /* stack tracer isn't supported yet */
1263 mutex_lock(&ftrace_lock);
1264 list_for_each_entry_safe(p, n, head, list)
1266 mutex_unlock(&ftrace_lock);
1269 static void free_ftrace_hash(struct ftrace_hash *hash)
1271 if (!hash || hash == EMPTY_HASH)
1273 ftrace_hash_clear(hash);
1274 kfree(hash->buckets);
1278 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1280 struct ftrace_hash *hash;
1282 hash = container_of(rcu, struct ftrace_hash, rcu);
1283 free_ftrace_hash(hash);
1286 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1288 if (!hash || hash == EMPTY_HASH)
1290 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1293 void ftrace_free_filter(struct ftrace_ops *ops)
1295 ftrace_ops_init(ops);
1296 free_ftrace_hash(ops->func_hash->filter_hash);
1297 free_ftrace_hash(ops->func_hash->notrace_hash);
1300 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1302 struct ftrace_hash *hash;
1305 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1309 size = 1 << size_bits;
1310 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1312 if (!hash->buckets) {
1317 hash->size_bits = size_bits;
1323 static int ftrace_add_mod(struct trace_array *tr,
1324 const char *func, const char *module,
1327 struct ftrace_mod_load *ftrace_mod;
1328 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1330 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1334 INIT_LIST_HEAD(&ftrace_mod->list);
1335 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1336 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1337 ftrace_mod->enable = enable;
1339 if (!ftrace_mod->func || !ftrace_mod->module)
1342 list_add(&ftrace_mod->list, mod_head);
1347 free_ftrace_mod(ftrace_mod);
1352 static struct ftrace_hash *
1353 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1355 struct ftrace_func_entry *entry;
1356 struct ftrace_hash *new_hash;
1361 new_hash = alloc_ftrace_hash(size_bits);
1366 new_hash->flags = hash->flags;
1369 if (ftrace_hash_empty(hash))
1372 size = 1 << hash->size_bits;
1373 for (i = 0; i < size; i++) {
1374 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1375 ret = add_hash_entry(new_hash, entry->ip);
1381 FTRACE_WARN_ON(new_hash->count != hash->count);
1386 free_ftrace_hash(new_hash);
1391 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1393 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1395 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1396 struct ftrace_hash *new_hash);
1398 static struct ftrace_hash *
1399 __ftrace_hash_move(struct ftrace_hash *src)
1401 struct ftrace_func_entry *entry;
1402 struct hlist_node *tn;
1403 struct hlist_head *hhd;
1404 struct ftrace_hash *new_hash;
1405 int size = src->count;
1410 * If the new source is empty, just return the empty_hash.
1412 if (ftrace_hash_empty(src))
1416 * Make the hash size about 1/2 the # found
1418 for (size /= 2; size; size >>= 1)
1421 /* Don't allocate too much */
1422 if (bits > FTRACE_HASH_MAX_BITS)
1423 bits = FTRACE_HASH_MAX_BITS;
1425 new_hash = alloc_ftrace_hash(bits);
1429 new_hash->flags = src->flags;
1431 size = 1 << src->size_bits;
1432 for (i = 0; i < size; i++) {
1433 hhd = &src->buckets[i];
1434 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1435 remove_hash_entry(src, entry);
1436 __add_hash_entry(new_hash, entry);
1444 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1445 struct ftrace_hash **dst, struct ftrace_hash *src)
1447 struct ftrace_hash *new_hash;
1450 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1451 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1454 new_hash = __ftrace_hash_move(src);
1458 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1460 /* IPMODIFY should be updated only when filter_hash updating */
1461 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1463 free_ftrace_hash(new_hash);
1469 * Remove the current set, update the hash and add
1472 ftrace_hash_rec_disable_modify(ops, enable);
1474 rcu_assign_pointer(*dst, new_hash);
1476 ftrace_hash_rec_enable_modify(ops, enable);
1481 static bool hash_contains_ip(unsigned long ip,
1482 struct ftrace_ops_hash *hash)
1485 * The function record is a match if it exists in the filter
1486 * hash and not in the notrace hash. Note, an emty hash is
1487 * considered a match for the filter hash, but an empty
1488 * notrace hash is considered not in the notrace hash.
1490 return (ftrace_hash_empty(hash->filter_hash) ||
1491 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1492 (ftrace_hash_empty(hash->notrace_hash) ||
1493 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1497 * Test the hashes for this ops to see if we want to call
1498 * the ops->func or not.
1500 * It's a match if the ip is in the ops->filter_hash or
1501 * the filter_hash does not exist or is empty,
1503 * the ip is not in the ops->notrace_hash.
1505 * This needs to be called with preemption disabled as
1506 * the hashes are freed with call_rcu_sched().
1509 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1511 struct ftrace_ops_hash hash;
1514 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1516 * There's a small race when adding ops that the ftrace handler
1517 * that wants regs, may be called without them. We can not
1518 * allow that handler to be called if regs is NULL.
1520 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1524 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1525 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1527 if (hash_contains_ip(ip, &hash))
1536 * This is a double for. Do not use 'break' to break out of the loop,
1537 * you must use a goto.
1539 #define do_for_each_ftrace_rec(pg, rec) \
1540 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1542 for (_____i = 0; _____i < pg->index; _____i++) { \
1543 rec = &pg->records[_____i];
1545 #define while_for_each_ftrace_rec() \
1550 static int ftrace_cmp_recs(const void *a, const void *b)
1552 const struct dyn_ftrace *key = a;
1553 const struct dyn_ftrace *rec = b;
1555 if (key->flags < rec->ip)
1557 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1563 * ftrace_location_range - return the first address of a traced location
1564 * if it touches the given ip range
1565 * @start: start of range to search.
1566 * @end: end of range to search (inclusive). @end points to the last byte
1569 * Returns rec->ip if the related ftrace location is a least partly within
1570 * the given address range. That is, the first address of the instruction
1571 * that is either a NOP or call to the function tracer. It checks the ftrace
1572 * internal tables to determine if the address belongs or not.
1574 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1576 struct ftrace_page *pg;
1577 struct dyn_ftrace *rec;
1578 struct dyn_ftrace key;
1581 key.flags = end; /* overload flags, as it is unsigned long */
1583 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1584 if (pg->index == 0 ||
1585 end < pg->records[0].ip ||
1586 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1588 rec = bsearch(&key, pg->records, pg->index,
1589 sizeof(struct dyn_ftrace),
1599 * ftrace_location - return true if the ip giving is a traced location
1600 * @ip: the instruction pointer to check
1602 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1603 * That is, the instruction that is either a NOP or call to
1604 * the function tracer. It checks the ftrace internal tables to
1605 * determine if the address belongs or not.
1607 unsigned long ftrace_location(unsigned long ip)
1609 return ftrace_location_range(ip, ip);
1613 * ftrace_text_reserved - return true if range contains an ftrace location
1614 * @start: start of range to search
1615 * @end: end of range to search (inclusive). @end points to the last byte to check.
1617 * Returns 1 if @start and @end contains a ftrace location.
1618 * That is, the instruction that is either a NOP or call to
1619 * the function tracer. It checks the ftrace internal tables to
1620 * determine if the address belongs or not.
1622 int ftrace_text_reserved(const void *start, const void *end)
1626 ret = ftrace_location_range((unsigned long)start,
1627 (unsigned long)end);
1632 /* Test if ops registered to this rec needs regs */
1633 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1635 struct ftrace_ops *ops;
1636 bool keep_regs = false;
1638 for (ops = ftrace_ops_list;
1639 ops != &ftrace_list_end; ops = ops->next) {
1640 /* pass rec in as regs to have non-NULL val */
1641 if (ftrace_ops_test(ops, rec->ip, rec)) {
1642 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1652 static struct ftrace_ops *
1653 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1654 static struct ftrace_ops *
1655 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1656 static struct ftrace_ops *
1657 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1659 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1663 struct ftrace_hash *hash;
1664 struct ftrace_hash *other_hash;
1665 struct ftrace_page *pg;
1666 struct dyn_ftrace *rec;
1667 bool update = false;
1671 /* Only update if the ops has been registered */
1672 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1676 * In the filter_hash case:
1677 * If the count is zero, we update all records.
1678 * Otherwise we just update the items in the hash.
1680 * In the notrace_hash case:
1681 * We enable the update in the hash.
1682 * As disabling notrace means enabling the tracing,
1683 * and enabling notrace means disabling, the inc variable
1687 hash = ops->func_hash->filter_hash;
1688 other_hash = ops->func_hash->notrace_hash;
1689 if (ftrace_hash_empty(hash))
1693 hash = ops->func_hash->notrace_hash;
1694 other_hash = ops->func_hash->filter_hash;
1696 * If the notrace hash has no items,
1697 * then there's nothing to do.
1699 if (ftrace_hash_empty(hash))
1703 do_for_each_ftrace_rec(pg, rec) {
1704 int in_other_hash = 0;
1708 if (rec->flags & FTRACE_FL_DISABLED)
1713 * Only the filter_hash affects all records.
1714 * Update if the record is not in the notrace hash.
1716 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1719 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1720 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1723 * If filter_hash is set, we want to match all functions
1724 * that are in the hash but not in the other hash.
1726 * If filter_hash is not set, then we are decrementing.
1727 * That means we match anything that is in the hash
1728 * and also in the other_hash. That is, we need to turn
1729 * off functions in the other hash because they are disabled
1732 if (filter_hash && in_hash && !in_other_hash)
1734 else if (!filter_hash && in_hash &&
1735 (in_other_hash || ftrace_hash_empty(other_hash)))
1743 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1747 * If there's only a single callback registered to a
1748 * function, and the ops has a trampoline registered
1749 * for it, then we can call it directly.
1751 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1752 rec->flags |= FTRACE_FL_TRAMP;
1755 * If we are adding another function callback
1756 * to this function, and the previous had a
1757 * custom trampoline in use, then we need to go
1758 * back to the default trampoline.
1760 rec->flags &= ~FTRACE_FL_TRAMP;
1763 * If any ops wants regs saved for this function
1764 * then all ops will get saved regs.
1766 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1767 rec->flags |= FTRACE_FL_REGS;
1769 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1774 * If the rec had REGS enabled and the ops that is
1775 * being removed had REGS set, then see if there is
1776 * still any ops for this record that wants regs.
1777 * If not, we can stop recording them.
1779 if (ftrace_rec_count(rec) > 0 &&
1780 rec->flags & FTRACE_FL_REGS &&
1781 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1782 if (!test_rec_ops_needs_regs(rec))
1783 rec->flags &= ~FTRACE_FL_REGS;
1787 * The TRAMP needs to be set only if rec count
1788 * is decremented to one, and the ops that is
1789 * left has a trampoline. As TRAMP can only be
1790 * enabled if there is only a single ops attached
1793 if (ftrace_rec_count(rec) == 1 &&
1794 ftrace_find_tramp_ops_any_other(rec, ops))
1795 rec->flags |= FTRACE_FL_TRAMP;
1797 rec->flags &= ~FTRACE_FL_TRAMP;
1800 * flags will be cleared in ftrace_check_record()
1801 * if rec count is zero.
1806 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1807 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1809 /* Shortcut, if we handled all records, we are done. */
1810 if (!all && count == hash->count)
1812 } while_for_each_ftrace_rec();
1817 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1820 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1823 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1826 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1829 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1830 int filter_hash, int inc)
1832 struct ftrace_ops *op;
1834 __ftrace_hash_rec_update(ops, filter_hash, inc);
1836 if (ops->func_hash != &global_ops.local_hash)
1840 * If the ops shares the global_ops hash, then we need to update
1841 * all ops that are enabled and use this hash.
1843 do_for_each_ftrace_op(op, ftrace_ops_list) {
1847 if (op->func_hash == &global_ops.local_hash)
1848 __ftrace_hash_rec_update(op, filter_hash, inc);
1849 } while_for_each_ftrace_op(op);
1852 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1855 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1858 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1861 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1865 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1866 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1867 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1868 * Note that old_hash and new_hash has below meanings
1869 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1870 * - If the hash is EMPTY_HASH, it hits nothing
1871 * - Anything else hits the recs which match the hash entries.
1873 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1874 struct ftrace_hash *old_hash,
1875 struct ftrace_hash *new_hash)
1877 struct ftrace_page *pg;
1878 struct dyn_ftrace *rec, *end = NULL;
1881 /* Only update if the ops has been registered */
1882 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1885 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1889 * Since the IPMODIFY is a very address sensitive action, we do not
1890 * allow ftrace_ops to set all functions to new hash.
1892 if (!new_hash || !old_hash)
1895 /* Update rec->flags */
1896 do_for_each_ftrace_rec(pg, rec) {
1898 if (rec->flags & FTRACE_FL_DISABLED)
1901 /* We need to update only differences of filter_hash */
1902 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1903 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1904 if (in_old == in_new)
1908 /* New entries must ensure no others are using it */
1909 if (rec->flags & FTRACE_FL_IPMODIFY)
1911 rec->flags |= FTRACE_FL_IPMODIFY;
1912 } else /* Removed entry */
1913 rec->flags &= ~FTRACE_FL_IPMODIFY;
1914 } while_for_each_ftrace_rec();
1921 /* Roll back what we did above */
1922 do_for_each_ftrace_rec(pg, rec) {
1924 if (rec->flags & FTRACE_FL_DISABLED)
1930 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1931 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1932 if (in_old == in_new)
1936 rec->flags &= ~FTRACE_FL_IPMODIFY;
1938 rec->flags |= FTRACE_FL_IPMODIFY;
1939 } while_for_each_ftrace_rec();
1945 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1947 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1949 if (ftrace_hash_empty(hash))
1952 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1955 /* Disabling always succeeds */
1956 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1958 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1960 if (ftrace_hash_empty(hash))
1963 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1966 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1967 struct ftrace_hash *new_hash)
1969 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1971 if (ftrace_hash_empty(old_hash))
1974 if (ftrace_hash_empty(new_hash))
1977 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1980 static void print_ip_ins(const char *fmt, const unsigned char *p)
1982 char ins[MCOUNT_INSN_SIZE];
1985 if (probe_kernel_read(ins, p, MCOUNT_INSN_SIZE)) {
1986 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
1990 printk(KERN_CONT "%s", fmt);
1992 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1993 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
1996 enum ftrace_bug_type ftrace_bug_type;
1997 const void *ftrace_expected;
1999 static void print_bug_type(void)
2001 switch (ftrace_bug_type) {
2002 case FTRACE_BUG_UNKNOWN:
2004 case FTRACE_BUG_INIT:
2005 pr_info("Initializing ftrace call sites\n");
2007 case FTRACE_BUG_NOP:
2008 pr_info("Setting ftrace call site to NOP\n");
2010 case FTRACE_BUG_CALL:
2011 pr_info("Setting ftrace call site to call ftrace function\n");
2013 case FTRACE_BUG_UPDATE:
2014 pr_info("Updating ftrace call site to call a different ftrace function\n");
2020 * ftrace_bug - report and shutdown function tracer
2021 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2022 * @rec: The record that failed
2024 * The arch code that enables or disables the function tracing
2025 * can call ftrace_bug() when it has detected a problem in
2026 * modifying the code. @failed should be one of either:
2027 * EFAULT - if the problem happens on reading the @ip address
2028 * EINVAL - if what is read at @ip is not what was expected
2029 * EPERM - if the problem happens on writting to the @ip address
2031 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2033 unsigned long ip = rec ? rec->ip : 0;
2037 FTRACE_WARN_ON_ONCE(1);
2038 pr_info("ftrace faulted on modifying ");
2042 FTRACE_WARN_ON_ONCE(1);
2043 pr_info("ftrace failed to modify ");
2045 print_ip_ins(" actual: ", (unsigned char *)ip);
2047 if (ftrace_expected) {
2048 print_ip_ins(" expected: ", ftrace_expected);
2053 FTRACE_WARN_ON_ONCE(1);
2054 pr_info("ftrace faulted on writing ");
2058 FTRACE_WARN_ON_ONCE(1);
2059 pr_info("ftrace faulted on unknown error ");
2064 struct ftrace_ops *ops = NULL;
2066 pr_info("ftrace record flags: %lx\n", rec->flags);
2067 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2068 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2069 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2070 ops = ftrace_find_tramp_ops_any(rec);
2073 pr_cont("\ttramp: %pS (%pS)",
2074 (void *)ops->trampoline,
2076 ops = ftrace_find_tramp_ops_next(rec, ops);
2079 pr_cont("\ttramp: ERROR!");
2082 ip = ftrace_get_addr_curr(rec);
2083 pr_cont("\n expected tramp: %lx\n", ip);
2087 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2089 unsigned long flag = 0UL;
2091 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2093 if (rec->flags & FTRACE_FL_DISABLED)
2094 return FTRACE_UPDATE_IGNORE;
2097 * If we are updating calls:
2099 * If the record has a ref count, then we need to enable it
2100 * because someone is using it.
2102 * Otherwise we make sure its disabled.
2104 * If we are disabling calls, then disable all records that
2107 if (enable && ftrace_rec_count(rec))
2108 flag = FTRACE_FL_ENABLED;
2111 * If enabling and the REGS flag does not match the REGS_EN, or
2112 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2113 * this record. Set flags to fail the compare against ENABLED.
2116 if (!(rec->flags & FTRACE_FL_REGS) !=
2117 !(rec->flags & FTRACE_FL_REGS_EN))
2118 flag |= FTRACE_FL_REGS;
2120 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2121 !(rec->flags & FTRACE_FL_TRAMP_EN))
2122 flag |= FTRACE_FL_TRAMP;
2125 /* If the state of this record hasn't changed, then do nothing */
2126 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2127 return FTRACE_UPDATE_IGNORE;
2130 /* Save off if rec is being enabled (for return value) */
2131 flag ^= rec->flags & FTRACE_FL_ENABLED;
2134 rec->flags |= FTRACE_FL_ENABLED;
2135 if (flag & FTRACE_FL_REGS) {
2136 if (rec->flags & FTRACE_FL_REGS)
2137 rec->flags |= FTRACE_FL_REGS_EN;
2139 rec->flags &= ~FTRACE_FL_REGS_EN;
2141 if (flag & FTRACE_FL_TRAMP) {
2142 if (rec->flags & FTRACE_FL_TRAMP)
2143 rec->flags |= FTRACE_FL_TRAMP_EN;
2145 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2150 * If this record is being updated from a nop, then
2151 * return UPDATE_MAKE_CALL.
2153 * return UPDATE_MODIFY_CALL to tell the caller to convert
2154 * from the save regs, to a non-save regs function or
2155 * vice versa, or from a trampoline call.
2157 if (flag & FTRACE_FL_ENABLED) {
2158 ftrace_bug_type = FTRACE_BUG_CALL;
2159 return FTRACE_UPDATE_MAKE_CALL;
2162 ftrace_bug_type = FTRACE_BUG_UPDATE;
2163 return FTRACE_UPDATE_MODIFY_CALL;
2167 /* If there's no more users, clear all flags */
2168 if (!ftrace_rec_count(rec))
2172 * Just disable the record, but keep the ops TRAMP
2173 * and REGS states. The _EN flags must be disabled though.
2175 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2179 ftrace_bug_type = FTRACE_BUG_NOP;
2180 return FTRACE_UPDATE_MAKE_NOP;
2184 * ftrace_update_record, set a record that now is tracing or not
2185 * @rec: the record to update
2186 * @enable: set to 1 if the record is tracing, zero to force disable
2188 * The records that represent all functions that can be traced need
2189 * to be updated when tracing has been enabled.
2191 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2193 return ftrace_check_record(rec, enable, 1);
2197 * ftrace_test_record, check if the record has been enabled or not
2198 * @rec: the record to test
2199 * @enable: set to 1 to check if enabled, 0 if it is disabled
2201 * The arch code may need to test if a record is already set to
2202 * tracing to determine how to modify the function code that it
2205 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2207 return ftrace_check_record(rec, enable, 0);
2210 static struct ftrace_ops *
2211 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2213 struct ftrace_ops *op;
2214 unsigned long ip = rec->ip;
2216 do_for_each_ftrace_op(op, ftrace_ops_list) {
2218 if (!op->trampoline)
2221 if (hash_contains_ip(ip, op->func_hash))
2223 } while_for_each_ftrace_op(op);
2228 static struct ftrace_ops *
2229 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2231 struct ftrace_ops *op;
2232 unsigned long ip = rec->ip;
2234 do_for_each_ftrace_op(op, ftrace_ops_list) {
2236 if (op == op_exclude || !op->trampoline)
2239 if (hash_contains_ip(ip, op->func_hash))
2241 } while_for_each_ftrace_op(op);
2246 static struct ftrace_ops *
2247 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2248 struct ftrace_ops *op)
2250 unsigned long ip = rec->ip;
2252 while_for_each_ftrace_op(op) {
2254 if (!op->trampoline)
2257 if (hash_contains_ip(ip, op->func_hash))
2264 static struct ftrace_ops *
2265 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2267 struct ftrace_ops *op;
2268 unsigned long ip = rec->ip;
2271 * Need to check removed ops first.
2272 * If they are being removed, and this rec has a tramp,
2273 * and this rec is in the ops list, then it would be the
2274 * one with the tramp.
2277 if (hash_contains_ip(ip, &removed_ops->old_hash))
2282 * Need to find the current trampoline for a rec.
2283 * Now, a trampoline is only attached to a rec if there
2284 * was a single 'ops' attached to it. But this can be called
2285 * when we are adding another op to the rec or removing the
2286 * current one. Thus, if the op is being added, we can
2287 * ignore it because it hasn't attached itself to the rec
2290 * If an ops is being modified (hooking to different functions)
2291 * then we don't care about the new functions that are being
2292 * added, just the old ones (that are probably being removed).
2294 * If we are adding an ops to a function that already is using
2295 * a trampoline, it needs to be removed (trampolines are only
2296 * for single ops connected), then an ops that is not being
2297 * modified also needs to be checked.
2299 do_for_each_ftrace_op(op, ftrace_ops_list) {
2301 if (!op->trampoline)
2305 * If the ops is being added, it hasn't gotten to
2306 * the point to be removed from this tree yet.
2308 if (op->flags & FTRACE_OPS_FL_ADDING)
2313 * If the ops is being modified and is in the old
2314 * hash, then it is probably being removed from this
2317 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2318 hash_contains_ip(ip, &op->old_hash))
2321 * If the ops is not being added or modified, and it's
2322 * in its normal filter hash, then this must be the one
2325 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2326 hash_contains_ip(ip, op->func_hash))
2329 } while_for_each_ftrace_op(op);
2334 static struct ftrace_ops *
2335 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2337 struct ftrace_ops *op;
2338 unsigned long ip = rec->ip;
2340 do_for_each_ftrace_op(op, ftrace_ops_list) {
2341 /* pass rec in as regs to have non-NULL val */
2342 if (hash_contains_ip(ip, op->func_hash))
2344 } while_for_each_ftrace_op(op);
2350 * ftrace_get_addr_new - Get the call address to set to
2351 * @rec: The ftrace record descriptor
2353 * If the record has the FTRACE_FL_REGS set, that means that it
2354 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2355 * is not not set, then it wants to convert to the normal callback.
2357 * Returns the address of the trampoline to set to
2359 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2361 struct ftrace_ops *ops;
2363 /* Trampolines take precedence over regs */
2364 if (rec->flags & FTRACE_FL_TRAMP) {
2365 ops = ftrace_find_tramp_ops_new(rec);
2366 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2367 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2368 (void *)rec->ip, (void *)rec->ip, rec->flags);
2369 /* Ftrace is shutting down, return anything */
2370 return (unsigned long)FTRACE_ADDR;
2372 return ops->trampoline;
2375 if (rec->flags & FTRACE_FL_REGS)
2376 return (unsigned long)FTRACE_REGS_ADDR;
2378 return (unsigned long)FTRACE_ADDR;
2382 * ftrace_get_addr_curr - Get the call address that is already there
2383 * @rec: The ftrace record descriptor
2385 * The FTRACE_FL_REGS_EN is set when the record already points to
2386 * a function that saves all the regs. Basically the '_EN' version
2387 * represents the current state of the function.
2389 * Returns the address of the trampoline that is currently being called
2391 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2393 struct ftrace_ops *ops;
2395 /* Trampolines take precedence over regs */
2396 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2397 ops = ftrace_find_tramp_ops_curr(rec);
2398 if (FTRACE_WARN_ON(!ops)) {
2399 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2400 (void *)rec->ip, (void *)rec->ip);
2401 /* Ftrace is shutting down, return anything */
2402 return (unsigned long)FTRACE_ADDR;
2404 return ops->trampoline;
2407 if (rec->flags & FTRACE_FL_REGS_EN)
2408 return (unsigned long)FTRACE_REGS_ADDR;
2410 return (unsigned long)FTRACE_ADDR;
2414 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2416 unsigned long ftrace_old_addr;
2417 unsigned long ftrace_addr;
2420 ftrace_addr = ftrace_get_addr_new(rec);
2422 /* This needs to be done before we call ftrace_update_record */
2423 ftrace_old_addr = ftrace_get_addr_curr(rec);
2425 ret = ftrace_update_record(rec, enable);
2427 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2430 case FTRACE_UPDATE_IGNORE:
2433 case FTRACE_UPDATE_MAKE_CALL:
2434 ftrace_bug_type = FTRACE_BUG_CALL;
2435 return ftrace_make_call(rec, ftrace_addr);
2437 case FTRACE_UPDATE_MAKE_NOP:
2438 ftrace_bug_type = FTRACE_BUG_NOP;
2439 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2441 case FTRACE_UPDATE_MODIFY_CALL:
2442 ftrace_bug_type = FTRACE_BUG_UPDATE;
2443 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2446 return -1; /* unknow ftrace bug */
2449 void __weak ftrace_replace_code(int enable)
2451 struct dyn_ftrace *rec;
2452 struct ftrace_page *pg;
2455 if (unlikely(ftrace_disabled))
2458 do_for_each_ftrace_rec(pg, rec) {
2460 if (rec->flags & FTRACE_FL_DISABLED)
2463 failed = __ftrace_replace_code(rec, enable);
2465 ftrace_bug(failed, rec);
2466 /* Stop processing */
2469 } while_for_each_ftrace_rec();
2472 struct ftrace_rec_iter {
2473 struct ftrace_page *pg;
2478 * ftrace_rec_iter_start, start up iterating over traced functions
2480 * Returns an iterator handle that is used to iterate over all
2481 * the records that represent address locations where functions
2484 * May return NULL if no records are available.
2486 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2489 * We only use a single iterator.
2490 * Protected by the ftrace_lock mutex.
2492 static struct ftrace_rec_iter ftrace_rec_iter;
2493 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2495 iter->pg = ftrace_pages_start;
2498 /* Could have empty pages */
2499 while (iter->pg && !iter->pg->index)
2500 iter->pg = iter->pg->next;
2509 * ftrace_rec_iter_next, get the next record to process.
2510 * @iter: The handle to the iterator.
2512 * Returns the next iterator after the given iterator @iter.
2514 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2518 if (iter->index >= iter->pg->index) {
2519 iter->pg = iter->pg->next;
2522 /* Could have empty pages */
2523 while (iter->pg && !iter->pg->index)
2524 iter->pg = iter->pg->next;
2534 * ftrace_rec_iter_record, get the record at the iterator location
2535 * @iter: The current iterator location
2537 * Returns the record that the current @iter is at.
2539 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2541 return &iter->pg->records[iter->index];
2545 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2549 if (unlikely(ftrace_disabled))
2552 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2554 ftrace_bug_type = FTRACE_BUG_INIT;
2555 ftrace_bug(ret, rec);
2562 * archs can override this function if they must do something
2563 * before the modifying code is performed.
2565 int __weak ftrace_arch_code_modify_prepare(void)
2571 * archs can override this function if they must do something
2572 * after the modifying code is performed.
2574 int __weak ftrace_arch_code_modify_post_process(void)
2579 void ftrace_modify_all_code(int command)
2581 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2585 * If the ftrace_caller calls a ftrace_ops func directly,
2586 * we need to make sure that it only traces functions it
2587 * expects to trace. When doing the switch of functions,
2588 * we need to update to the ftrace_ops_list_func first
2589 * before the transition between old and new calls are set,
2590 * as the ftrace_ops_list_func will check the ops hashes
2591 * to make sure the ops are having the right functions
2595 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2596 if (FTRACE_WARN_ON(err))
2600 if (command & FTRACE_UPDATE_CALLS)
2601 ftrace_replace_code(1);
2602 else if (command & FTRACE_DISABLE_CALLS)
2603 ftrace_replace_code(0);
2605 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2606 function_trace_op = set_function_trace_op;
2608 /* If irqs are disabled, we are in stop machine */
2609 if (!irqs_disabled())
2610 smp_call_function(ftrace_sync_ipi, NULL, 1);
2611 err = ftrace_update_ftrace_func(ftrace_trace_function);
2612 if (FTRACE_WARN_ON(err))
2616 if (command & FTRACE_START_FUNC_RET)
2617 err = ftrace_enable_ftrace_graph_caller();
2618 else if (command & FTRACE_STOP_FUNC_RET)
2619 err = ftrace_disable_ftrace_graph_caller();
2620 FTRACE_WARN_ON(err);
2623 static int __ftrace_modify_code(void *data)
2625 int *command = data;
2627 ftrace_modify_all_code(*command);
2633 * ftrace_run_stop_machine, go back to the stop machine method
2634 * @command: The command to tell ftrace what to do
2636 * If an arch needs to fall back to the stop machine method, the
2637 * it can call this function.
2639 void ftrace_run_stop_machine(int command)
2641 stop_machine(__ftrace_modify_code, &command, NULL);
2645 * arch_ftrace_update_code, modify the code to trace or not trace
2646 * @command: The command that needs to be done
2648 * Archs can override this function if it does not need to
2649 * run stop_machine() to modify code.
2651 void __weak arch_ftrace_update_code(int command)
2653 ftrace_run_stop_machine(command);
2656 static void ftrace_run_update_code(int command)
2660 ret = ftrace_arch_code_modify_prepare();
2661 FTRACE_WARN_ON(ret);
2666 * By default we use stop_machine() to modify the code.
2667 * But archs can do what ever they want as long as it
2668 * is safe. The stop_machine() is the safest, but also
2669 * produces the most overhead.
2671 arch_ftrace_update_code(command);
2673 ret = ftrace_arch_code_modify_post_process();
2674 FTRACE_WARN_ON(ret);
2677 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2678 struct ftrace_ops_hash *old_hash)
2680 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2681 ops->old_hash.filter_hash = old_hash->filter_hash;
2682 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2683 ftrace_run_update_code(command);
2684 ops->old_hash.filter_hash = NULL;
2685 ops->old_hash.notrace_hash = NULL;
2686 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2689 static ftrace_func_t saved_ftrace_func;
2690 static int ftrace_start_up;
2692 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2696 static void ftrace_startup_enable(int command)
2698 if (saved_ftrace_func != ftrace_trace_function) {
2699 saved_ftrace_func = ftrace_trace_function;
2700 command |= FTRACE_UPDATE_TRACE_FUNC;
2703 if (!command || !ftrace_enabled)
2706 ftrace_run_update_code(command);
2709 static void ftrace_startup_all(int command)
2711 update_all_ops = true;
2712 ftrace_startup_enable(command);
2713 update_all_ops = false;
2716 static int ftrace_startup(struct ftrace_ops *ops, int command)
2720 if (unlikely(ftrace_disabled))
2723 ret = __register_ftrace_function(ops);
2730 * Note that ftrace probes uses this to start up
2731 * and modify functions it will probe. But we still
2732 * set the ADDING flag for modification, as probes
2733 * do not have trampolines. If they add them in the
2734 * future, then the probes will need to distinguish
2735 * between adding and updating probes.
2737 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2739 ret = ftrace_hash_ipmodify_enable(ops);
2741 /* Rollback registration process */
2742 __unregister_ftrace_function(ops);
2744 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2748 if (ftrace_hash_rec_enable(ops, 1))
2749 command |= FTRACE_UPDATE_CALLS;
2751 ftrace_startup_enable(command);
2754 * If ftrace is in an undefined state, we just remove ops from list
2755 * to prevent the NULL pointer, instead of totally rolling it back and
2756 * free trampoline, because those actions could cause further damage.
2758 if (unlikely(ftrace_disabled)) {
2759 __unregister_ftrace_function(ops);
2763 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2768 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2772 if (unlikely(ftrace_disabled))
2775 ret = __unregister_ftrace_function(ops);
2781 * Just warn in case of unbalance, no need to kill ftrace, it's not
2782 * critical but the ftrace_call callers may be never nopped again after
2783 * further ftrace uses.
2785 WARN_ON_ONCE(ftrace_start_up < 0);
2787 /* Disabling ipmodify never fails */
2788 ftrace_hash_ipmodify_disable(ops);
2790 if (ftrace_hash_rec_disable(ops, 1))
2791 command |= FTRACE_UPDATE_CALLS;
2793 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2795 if (saved_ftrace_func != ftrace_trace_function) {
2796 saved_ftrace_func = ftrace_trace_function;
2797 command |= FTRACE_UPDATE_TRACE_FUNC;
2800 if (!command || !ftrace_enabled) {
2802 * If these are dynamic or per_cpu ops, they still
2803 * need their data freed. Since, function tracing is
2804 * not currently active, we can just free them
2805 * without synchronizing all CPUs.
2807 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2814 * If the ops uses a trampoline, then it needs to be
2815 * tested first on update.
2817 ops->flags |= FTRACE_OPS_FL_REMOVING;
2820 /* The trampoline logic checks the old hashes */
2821 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2822 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2824 ftrace_run_update_code(command);
2827 * If there's no more ops registered with ftrace, run a
2828 * sanity check to make sure all rec flags are cleared.
2830 if (rcu_dereference_protected(ftrace_ops_list,
2831 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2832 struct ftrace_page *pg;
2833 struct dyn_ftrace *rec;
2835 do_for_each_ftrace_rec(pg, rec) {
2836 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2837 pr_warn(" %pS flags:%lx\n",
2838 (void *)rec->ip, rec->flags);
2839 } while_for_each_ftrace_rec();
2842 ops->old_hash.filter_hash = NULL;
2843 ops->old_hash.notrace_hash = NULL;
2846 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2849 * Dynamic ops may be freed, we must make sure that all
2850 * callers are done before leaving this function.
2851 * The same goes for freeing the per_cpu data of the per_cpu
2854 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
2856 * We need to do a hard force of sched synchronization.
2857 * This is because we use preempt_disable() to do RCU, but
2858 * the function tracers can be called where RCU is not watching
2859 * (like before user_exit()). We can not rely on the RCU
2860 * infrastructure to do the synchronization, thus we must do it
2863 schedule_on_each_cpu(ftrace_sync);
2866 * When the kernel is preeptive, tasks can be preempted
2867 * while on a ftrace trampoline. Just scheduling a task on
2868 * a CPU is not good enough to flush them. Calling
2869 * synchornize_rcu_tasks() will wait for those tasks to
2870 * execute and either schedule voluntarily or enter user space.
2872 if (IS_ENABLED(CONFIG_PREEMPT))
2873 synchronize_rcu_tasks();
2876 arch_ftrace_trampoline_free(ops);
2882 static void ftrace_startup_sysctl(void)
2886 if (unlikely(ftrace_disabled))
2889 /* Force update next time */
2890 saved_ftrace_func = NULL;
2891 /* ftrace_start_up is true if we want ftrace running */
2892 if (ftrace_start_up) {
2893 command = FTRACE_UPDATE_CALLS;
2894 if (ftrace_graph_active)
2895 command |= FTRACE_START_FUNC_RET;
2896 ftrace_startup_enable(command);
2900 static void ftrace_shutdown_sysctl(void)
2904 if (unlikely(ftrace_disabled))
2907 /* ftrace_start_up is true if ftrace is running */
2908 if (ftrace_start_up) {
2909 command = FTRACE_DISABLE_CALLS;
2910 if (ftrace_graph_active)
2911 command |= FTRACE_STOP_FUNC_RET;
2912 ftrace_run_update_code(command);
2916 static u64 ftrace_update_time;
2917 unsigned long ftrace_update_tot_cnt;
2919 static inline int ops_traces_mod(struct ftrace_ops *ops)
2922 * Filter_hash being empty will default to trace module.
2923 * But notrace hash requires a test of individual module functions.
2925 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2926 ftrace_hash_empty(ops->func_hash->notrace_hash);
2930 * Check if the current ops references the record.
2932 * If the ops traces all functions, then it was already accounted for.
2933 * If the ops does not trace the current record function, skip it.
2934 * If the ops ignores the function via notrace filter, skip it.
2937 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2939 /* If ops isn't enabled, ignore it */
2940 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2943 /* If ops traces all then it includes this function */
2944 if (ops_traces_mod(ops))
2947 /* The function must be in the filter */
2948 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2949 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2952 /* If in notrace hash, we ignore it too */
2953 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2959 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2961 struct ftrace_page *pg;
2962 struct dyn_ftrace *p;
2964 unsigned long update_cnt = 0;
2965 unsigned long rec_flags = 0;
2968 start = ftrace_now(raw_smp_processor_id());
2971 * When a module is loaded, this function is called to convert
2972 * the calls to mcount in its text to nops, and also to create
2973 * an entry in the ftrace data. Now, if ftrace is activated
2974 * after this call, but before the module sets its text to
2975 * read-only, the modification of enabling ftrace can fail if
2976 * the read-only is done while ftrace is converting the calls.
2977 * To prevent this, the module's records are set as disabled
2978 * and will be enabled after the call to set the module's text
2982 rec_flags |= FTRACE_FL_DISABLED;
2984 for (pg = new_pgs; pg; pg = pg->next) {
2986 for (i = 0; i < pg->index; i++) {
2988 /* If something went wrong, bail without enabling anything */
2989 if (unlikely(ftrace_disabled))
2992 p = &pg->records[i];
2993 p->flags = rec_flags;
2996 * Do the initial record conversion from mcount jump
2997 * to the NOP instructions.
2999 if (!__is_defined(CC_USING_NOP_MCOUNT) &&
3000 !ftrace_code_disable(mod, p))
3007 stop = ftrace_now(raw_smp_processor_id());
3008 ftrace_update_time = stop - start;
3009 ftrace_update_tot_cnt += update_cnt;
3014 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3019 if (WARN_ON(!count))
3022 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3025 * We want to fill as much as possible. No more than a page
3028 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3032 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3035 /* if we can't allocate this size, try something smaller */
3042 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3051 static struct ftrace_page *
3052 ftrace_allocate_pages(unsigned long num_to_init)
3054 struct ftrace_page *start_pg;
3055 struct ftrace_page *pg;
3062 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3067 * Try to allocate as much as possible in one continues
3068 * location that fills in all of the space. We want to
3069 * waste as little space as possible.
3072 cnt = ftrace_allocate_records(pg, num_to_init);
3080 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3092 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3093 free_pages((unsigned long)pg->records, order);
3094 start_pg = pg->next;
3098 pr_info("ftrace: FAILED to allocate memory for functions\n");
3102 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3104 struct ftrace_iterator {
3108 struct ftrace_page *pg;
3109 struct dyn_ftrace *func;
3110 struct ftrace_func_probe *probe;
3111 struct ftrace_func_entry *probe_entry;
3112 struct trace_parser parser;
3113 struct ftrace_hash *hash;
3114 struct ftrace_ops *ops;
3115 struct trace_array *tr;
3116 struct list_head *mod_list;
3123 t_probe_next(struct seq_file *m, loff_t *pos)
3125 struct ftrace_iterator *iter = m->private;
3126 struct trace_array *tr = iter->ops->private;
3127 struct list_head *func_probes;
3128 struct ftrace_hash *hash;
3129 struct list_head *next;
3130 struct hlist_node *hnd = NULL;
3131 struct hlist_head *hhd;
3140 func_probes = &tr->func_probes;
3141 if (list_empty(func_probes))
3145 next = func_probes->next;
3146 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3149 if (iter->probe_entry)
3150 hnd = &iter->probe_entry->hlist;
3152 hash = iter->probe->ops.func_hash->filter_hash;
3155 * A probe being registered may temporarily have an empty hash
3156 * and it's at the end of the func_probes list.
3158 if (!hash || hash == EMPTY_HASH)
3161 size = 1 << hash->size_bits;
3164 if (iter->pidx >= size) {
3165 if (iter->probe->list.next == func_probes)
3167 next = iter->probe->list.next;
3168 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3169 hash = iter->probe->ops.func_hash->filter_hash;
3170 size = 1 << hash->size_bits;
3174 hhd = &hash->buckets[iter->pidx];
3176 if (hlist_empty(hhd)) {
3192 if (WARN_ON_ONCE(!hnd))
3195 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3200 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3202 struct ftrace_iterator *iter = m->private;
3206 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3209 if (iter->mod_pos > *pos)
3213 iter->probe_entry = NULL;
3215 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3216 p = t_probe_next(m, &l);
3223 /* Only set this if we have an item */
3224 iter->flags |= FTRACE_ITER_PROBE;
3230 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3232 struct ftrace_func_entry *probe_entry;
3233 struct ftrace_probe_ops *probe_ops;
3234 struct ftrace_func_probe *probe;
3236 probe = iter->probe;
3237 probe_entry = iter->probe_entry;
3239 if (WARN_ON_ONCE(!probe || !probe_entry))
3242 probe_ops = probe->probe_ops;
3244 if (probe_ops->print)
3245 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3247 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3248 (void *)probe_ops->func);
3254 t_mod_next(struct seq_file *m, loff_t *pos)
3256 struct ftrace_iterator *iter = m->private;
3257 struct trace_array *tr = iter->tr;
3262 iter->mod_list = iter->mod_list->next;
3264 if (iter->mod_list == &tr->mod_trace ||
3265 iter->mod_list == &tr->mod_notrace) {
3266 iter->flags &= ~FTRACE_ITER_MOD;
3270 iter->mod_pos = *pos;
3275 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3277 struct ftrace_iterator *iter = m->private;
3281 if (iter->func_pos > *pos)
3284 iter->mod_pos = iter->func_pos;
3286 /* probes are only available if tr is set */
3290 for (l = 0; l <= (*pos - iter->func_pos); ) {
3291 p = t_mod_next(m, &l);
3296 iter->flags &= ~FTRACE_ITER_MOD;
3297 return t_probe_start(m, pos);
3300 /* Only set this if we have an item */
3301 iter->flags |= FTRACE_ITER_MOD;
3307 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3309 struct ftrace_mod_load *ftrace_mod;
3310 struct trace_array *tr = iter->tr;
3312 if (WARN_ON_ONCE(!iter->mod_list) ||
3313 iter->mod_list == &tr->mod_trace ||
3314 iter->mod_list == &tr->mod_notrace)
3317 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3319 if (ftrace_mod->func)
3320 seq_printf(m, "%s", ftrace_mod->func);
3324 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3330 t_func_next(struct seq_file *m, loff_t *pos)
3332 struct ftrace_iterator *iter = m->private;
3333 struct dyn_ftrace *rec = NULL;
3338 if (iter->idx >= iter->pg->index) {
3339 if (iter->pg->next) {
3340 iter->pg = iter->pg->next;
3345 rec = &iter->pg->records[iter->idx++];
3346 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3347 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3349 ((iter->flags & FTRACE_ITER_ENABLED) &&
3350 !(rec->flags & FTRACE_FL_ENABLED))) {
3360 iter->pos = iter->func_pos = *pos;
3367 t_next(struct seq_file *m, void *v, loff_t *pos)
3369 struct ftrace_iterator *iter = m->private;
3370 loff_t l = *pos; /* t_probe_start() must use original pos */
3373 if (unlikely(ftrace_disabled))
3376 if (iter->flags & FTRACE_ITER_PROBE)
3377 return t_probe_next(m, pos);
3379 if (iter->flags & FTRACE_ITER_MOD)
3380 return t_mod_next(m, pos);
3382 if (iter->flags & FTRACE_ITER_PRINTALL) {
3383 /* next must increment pos, and t_probe_start does not */
3385 return t_mod_start(m, &l);
3388 ret = t_func_next(m, pos);
3391 return t_mod_start(m, &l);
3396 static void reset_iter_read(struct ftrace_iterator *iter)
3400 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3403 static void *t_start(struct seq_file *m, loff_t *pos)
3405 struct ftrace_iterator *iter = m->private;
3409 mutex_lock(&ftrace_lock);
3411 if (unlikely(ftrace_disabled))
3415 * If an lseek was done, then reset and start from beginning.
3417 if (*pos < iter->pos)
3418 reset_iter_read(iter);
3421 * For set_ftrace_filter reading, if we have the filter
3422 * off, we can short cut and just print out that all
3423 * functions are enabled.
3425 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3426 ftrace_hash_empty(iter->hash)) {
3427 iter->func_pos = 1; /* Account for the message */
3429 return t_mod_start(m, pos);
3430 iter->flags |= FTRACE_ITER_PRINTALL;
3431 /* reset in case of seek/pread */
3432 iter->flags &= ~FTRACE_ITER_PROBE;
3436 if (iter->flags & FTRACE_ITER_MOD)
3437 return t_mod_start(m, pos);
3440 * Unfortunately, we need to restart at ftrace_pages_start
3441 * every time we let go of the ftrace_mutex. This is because
3442 * those pointers can change without the lock.
3444 iter->pg = ftrace_pages_start;
3446 for (l = 0; l <= *pos; ) {
3447 p = t_func_next(m, &l);
3453 return t_mod_start(m, pos);
3458 static void t_stop(struct seq_file *m, void *p)
3460 mutex_unlock(&ftrace_lock);
3464 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3469 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3470 struct dyn_ftrace *rec)
3474 ptr = arch_ftrace_trampoline_func(ops, rec);
3476 seq_printf(m, " ->%pS", ptr);
3479 static int t_show(struct seq_file *m, void *v)
3481 struct ftrace_iterator *iter = m->private;
3482 struct dyn_ftrace *rec;
3484 if (iter->flags & FTRACE_ITER_PROBE)
3485 return t_probe_show(m, iter);
3487 if (iter->flags & FTRACE_ITER_MOD)
3488 return t_mod_show(m, iter);
3490 if (iter->flags & FTRACE_ITER_PRINTALL) {
3491 if (iter->flags & FTRACE_ITER_NOTRACE)
3492 seq_puts(m, "#### no functions disabled ####\n");
3494 seq_puts(m, "#### all functions enabled ####\n");
3503 seq_printf(m, "%ps", (void *)rec->ip);
3504 if (iter->flags & FTRACE_ITER_ENABLED) {
3505 struct ftrace_ops *ops;
3507 seq_printf(m, " (%ld)%s%s",
3508 ftrace_rec_count(rec),
3509 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3510 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3511 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3512 ops = ftrace_find_tramp_ops_any(rec);
3515 seq_printf(m, "\ttramp: %pS (%pS)",
3516 (void *)ops->trampoline,
3518 add_trampoline_func(m, ops, rec);
3519 ops = ftrace_find_tramp_ops_next(rec, ops);
3522 seq_puts(m, "\ttramp: ERROR!");
3524 add_trampoline_func(m, NULL, rec);
3533 static const struct seq_operations show_ftrace_seq_ops = {
3541 ftrace_avail_open(struct inode *inode, struct file *file)
3543 struct ftrace_iterator *iter;
3545 if (unlikely(ftrace_disabled))
3548 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3552 iter->pg = ftrace_pages_start;
3553 iter->ops = &global_ops;
3559 ftrace_enabled_open(struct inode *inode, struct file *file)
3561 struct ftrace_iterator *iter;
3563 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3567 iter->pg = ftrace_pages_start;
3568 iter->flags = FTRACE_ITER_ENABLED;
3569 iter->ops = &global_ops;
3575 * ftrace_regex_open - initialize function tracer filter files
3576 * @ops: The ftrace_ops that hold the hash filters
3577 * @flag: The type of filter to process
3578 * @inode: The inode, usually passed in to your open routine
3579 * @file: The file, usually passed in to your open routine
3581 * ftrace_regex_open() initializes the filter files for the
3582 * @ops. Depending on @flag it may process the filter hash or
3583 * the notrace hash of @ops. With this called from the open
3584 * routine, you can use ftrace_filter_write() for the write
3585 * routine if @flag has FTRACE_ITER_FILTER set, or
3586 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3587 * tracing_lseek() should be used as the lseek routine, and
3588 * release must call ftrace_regex_release().
3591 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3592 struct inode *inode, struct file *file)
3594 struct ftrace_iterator *iter;
3595 struct ftrace_hash *hash;
3596 struct list_head *mod_head;
3597 struct trace_array *tr = ops->private;
3600 ftrace_ops_init(ops);
3602 if (unlikely(ftrace_disabled))
3605 if (tr && trace_array_get(tr) < 0)
3608 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3612 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3619 mutex_lock(&ops->func_hash->regex_lock);
3621 if (flag & FTRACE_ITER_NOTRACE) {
3622 hash = ops->func_hash->notrace_hash;
3623 mod_head = tr ? &tr->mod_notrace : NULL;
3625 hash = ops->func_hash->filter_hash;
3626 mod_head = tr ? &tr->mod_trace : NULL;
3629 iter->mod_list = mod_head;
3631 if (file->f_mode & FMODE_WRITE) {
3632 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3634 if (file->f_flags & O_TRUNC) {
3635 iter->hash = alloc_ftrace_hash(size_bits);
3636 clear_ftrace_mod_list(mod_head);
3638 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3642 trace_parser_put(&iter->parser);
3650 if (file->f_mode & FMODE_READ) {
3651 iter->pg = ftrace_pages_start;
3653 ret = seq_open(file, &show_ftrace_seq_ops);
3655 struct seq_file *m = file->private_data;
3659 free_ftrace_hash(iter->hash);
3660 trace_parser_put(&iter->parser);
3663 file->private_data = iter;
3666 mutex_unlock(&ops->func_hash->regex_lock);
3672 trace_array_put(tr);
3679 ftrace_filter_open(struct inode *inode, struct file *file)
3681 struct ftrace_ops *ops = inode->i_private;
3683 return ftrace_regex_open(ops,
3684 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3689 ftrace_notrace_open(struct inode *inode, struct file *file)
3691 struct ftrace_ops *ops = inode->i_private;
3693 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3697 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3698 struct ftrace_glob {
3705 * If symbols in an architecture don't correspond exactly to the user-visible
3706 * name of what they represent, it is possible to define this function to
3707 * perform the necessary adjustments.
3709 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3714 static int ftrace_match(char *str, struct ftrace_glob *g)
3719 str = arch_ftrace_match_adjust(str, g->search);
3723 if (strcmp(str, g->search) == 0)
3726 case MATCH_FRONT_ONLY:
3727 if (strncmp(str, g->search, g->len) == 0)
3730 case MATCH_MIDDLE_ONLY:
3731 if (strstr(str, g->search))
3734 case MATCH_END_ONLY:
3736 if (slen >= g->len &&
3737 memcmp(str + slen - g->len, g->search, g->len) == 0)
3741 if (glob_match(g->search, str))
3750 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3752 struct ftrace_func_entry *entry;
3755 entry = ftrace_lookup_ip(hash, rec->ip);
3757 /* Do nothing if it doesn't exist */
3761 free_hash_entry(hash, entry);
3763 /* Do nothing if it exists */
3767 ret = add_hash_entry(hash, rec->ip);
3773 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3774 struct ftrace_glob *mod_g, int exclude_mod)
3776 char str[KSYM_SYMBOL_LEN];
3779 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3782 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3784 /* blank module name to match all modules */
3786 /* blank module globbing: modname xor exclude_mod */
3787 if (!exclude_mod != !modname)
3793 * exclude_mod is set to trace everything but the given
3794 * module. If it is set and the module matches, then
3795 * return 0. If it is not set, and the module doesn't match
3796 * also return 0. Otherwise, check the function to see if
3799 if (!mod_matches == !exclude_mod)
3802 /* blank search means to match all funcs in the mod */
3807 return ftrace_match(str, func_g);
3811 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3813 struct ftrace_page *pg;
3814 struct dyn_ftrace *rec;
3815 struct ftrace_glob func_g = { .type = MATCH_FULL };
3816 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3817 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3818 int exclude_mod = 0;
3821 int clear_filter = 0;
3824 func_g.type = filter_parse_regex(func, len, &func_g.search,
3826 func_g.len = strlen(func_g.search);
3830 mod_g.type = filter_parse_regex(mod, strlen(mod),
3831 &mod_g.search, &exclude_mod);
3832 mod_g.len = strlen(mod_g.search);
3835 mutex_lock(&ftrace_lock);
3837 if (unlikely(ftrace_disabled))
3840 do_for_each_ftrace_rec(pg, rec) {
3842 if (rec->flags & FTRACE_FL_DISABLED)
3845 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3846 ret = enter_record(hash, rec, clear_filter);
3853 } while_for_each_ftrace_rec();
3855 mutex_unlock(&ftrace_lock);
3861 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3863 return match_records(hash, buff, len, NULL);
3866 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3867 struct ftrace_ops_hash *old_hash)
3869 struct ftrace_ops *op;
3871 if (!ftrace_enabled)
3874 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3875 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3880 * If this is the shared global_ops filter, then we need to
3881 * check if there is another ops that shares it, is enabled.
3882 * If so, we still need to run the modify code.
3884 if (ops->func_hash != &global_ops.local_hash)
3887 do_for_each_ftrace_op(op, ftrace_ops_list) {
3888 if (op->func_hash == &global_ops.local_hash &&
3889 op->flags & FTRACE_OPS_FL_ENABLED) {
3890 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3891 /* Only need to do this once */
3894 } while_for_each_ftrace_op(op);
3897 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3898 struct ftrace_hash **orig_hash,
3899 struct ftrace_hash *hash,
3902 struct ftrace_ops_hash old_hash_ops;
3903 struct ftrace_hash *old_hash;
3906 old_hash = *orig_hash;
3907 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3908 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3909 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3911 ftrace_ops_update_code(ops, &old_hash_ops);
3912 free_ftrace_hash_rcu(old_hash);
3917 static bool module_exists(const char *module)
3919 /* All modules have the symbol __this_module */
3920 const char this_mod[] = "__this_module";
3921 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
3925 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
3927 if (n > sizeof(modname) - 1)
3930 val = module_kallsyms_lookup_name(modname);
3934 static int cache_mod(struct trace_array *tr,
3935 const char *func, char *module, int enable)
3937 struct ftrace_mod_load *ftrace_mod, *n;
3938 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3941 mutex_lock(&ftrace_lock);
3943 /* We do not cache inverse filters */
3944 if (func[0] == '!') {
3948 /* Look to remove this hash */
3949 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3950 if (strcmp(ftrace_mod->module, module) != 0)
3953 /* no func matches all */
3954 if (strcmp(func, "*") == 0 ||
3955 (ftrace_mod->func &&
3956 strcmp(ftrace_mod->func, func) == 0)) {
3958 free_ftrace_mod(ftrace_mod);
3966 /* We only care about modules that have not been loaded yet */
3967 if (module_exists(module))
3970 /* Save this string off, and execute it when the module is loaded */
3971 ret = ftrace_add_mod(tr, func, module, enable);
3973 mutex_unlock(&ftrace_lock);
3979 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3980 int reset, int enable);
3982 #ifdef CONFIG_MODULES
3983 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3984 char *mod, bool enable)
3986 struct ftrace_mod_load *ftrace_mod, *n;
3987 struct ftrace_hash **orig_hash, *new_hash;
3988 LIST_HEAD(process_mods);
3992 mutex_lock(&ops->func_hash->regex_lock);
3995 orig_hash = &ops->func_hash->filter_hash;
3997 orig_hash = &ops->func_hash->notrace_hash;
3999 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4002 goto out; /* warn? */
4004 mutex_lock(&ftrace_lock);
4006 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4008 if (strcmp(ftrace_mod->module, mod) != 0)
4011 if (ftrace_mod->func)
4012 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4014 func = kstrdup("*", GFP_KERNEL);
4016 if (!func) /* warn? */
4019 list_del(&ftrace_mod->list);
4020 list_add(&ftrace_mod->list, &process_mods);
4022 /* Use the newly allocated func, as it may be "*" */
4023 kfree(ftrace_mod->func);
4024 ftrace_mod->func = func;
4027 mutex_unlock(&ftrace_lock);
4029 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4031 func = ftrace_mod->func;
4033 /* Grabs ftrace_lock, which is why we have this extra step */
4034 match_records(new_hash, func, strlen(func), mod);
4035 free_ftrace_mod(ftrace_mod);
4038 if (enable && list_empty(head))
4039 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4041 mutex_lock(&ftrace_lock);
4043 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4045 mutex_unlock(&ftrace_lock);
4048 mutex_unlock(&ops->func_hash->regex_lock);
4050 free_ftrace_hash(new_hash);
4053 static void process_cached_mods(const char *mod_name)
4055 struct trace_array *tr;
4058 mod = kstrdup(mod_name, GFP_KERNEL);
4062 mutex_lock(&trace_types_lock);
4063 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4064 if (!list_empty(&tr->mod_trace))
4065 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4066 if (!list_empty(&tr->mod_notrace))
4067 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4069 mutex_unlock(&trace_types_lock);
4076 * We register the module command as a template to show others how
4077 * to register the a command as well.
4081 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4082 char *func_orig, char *cmd, char *module, int enable)
4087 /* match_records() modifies func, and we need the original */
4088 func = kstrdup(func_orig, GFP_KERNEL);
4093 * cmd == 'mod' because we only registered this func
4094 * for the 'mod' ftrace_func_command.
4095 * But if you register one func with multiple commands,
4096 * you can tell which command was used by the cmd
4099 ret = match_records(hash, func, strlen(func), module);
4103 return cache_mod(tr, func_orig, module, enable);
4109 static struct ftrace_func_command ftrace_mod_cmd = {
4111 .func = ftrace_mod_callback,
4114 static int __init ftrace_mod_cmd_init(void)
4116 return register_ftrace_command(&ftrace_mod_cmd);
4118 core_initcall(ftrace_mod_cmd_init);
4120 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4121 struct ftrace_ops *op, struct pt_regs *pt_regs)
4123 struct ftrace_probe_ops *probe_ops;
4124 struct ftrace_func_probe *probe;
4126 probe = container_of(op, struct ftrace_func_probe, ops);
4127 probe_ops = probe->probe_ops;
4130 * Disable preemption for these calls to prevent a RCU grace
4131 * period. This syncs the hash iteration and freeing of items
4132 * on the hash. rcu_read_lock is too dangerous here.
4134 preempt_disable_notrace();
4135 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4136 preempt_enable_notrace();
4139 struct ftrace_func_map {
4140 struct ftrace_func_entry entry;
4144 struct ftrace_func_mapper {
4145 struct ftrace_hash hash;
4149 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4151 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4153 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4155 struct ftrace_hash *hash;
4158 * The mapper is simply a ftrace_hash, but since the entries
4159 * in the hash are not ftrace_func_entry type, we define it
4160 * as a separate structure.
4162 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4163 return (struct ftrace_func_mapper *)hash;
4167 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4168 * @mapper: The mapper that has the ip maps
4169 * @ip: the instruction pointer to find the data for
4171 * Returns the data mapped to @ip if found otherwise NULL. The return
4172 * is actually the address of the mapper data pointer. The address is
4173 * returned for use cases where the data is no bigger than a long, and
4174 * the user can use the data pointer as its data instead of having to
4175 * allocate more memory for the reference.
4177 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4180 struct ftrace_func_entry *entry;
4181 struct ftrace_func_map *map;
4183 entry = ftrace_lookup_ip(&mapper->hash, ip);
4187 map = (struct ftrace_func_map *)entry;
4192 * ftrace_func_mapper_add_ip - Map some data to an ip
4193 * @mapper: The mapper that has the ip maps
4194 * @ip: The instruction pointer address to map @data to
4195 * @data: The data to map to @ip
4197 * Returns 0 on succes otherwise an error.
4199 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4200 unsigned long ip, void *data)
4202 struct ftrace_func_entry *entry;
4203 struct ftrace_func_map *map;
4205 entry = ftrace_lookup_ip(&mapper->hash, ip);
4209 map = kmalloc(sizeof(*map), GFP_KERNEL);
4216 __add_hash_entry(&mapper->hash, &map->entry);
4222 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4223 * @mapper: The mapper that has the ip maps
4224 * @ip: The instruction pointer address to remove the data from
4226 * Returns the data if it is found, otherwise NULL.
4227 * Note, if the data pointer is used as the data itself, (see
4228 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4229 * if the data pointer was set to zero.
4231 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4234 struct ftrace_func_entry *entry;
4235 struct ftrace_func_map *map;
4238 entry = ftrace_lookup_ip(&mapper->hash, ip);
4242 map = (struct ftrace_func_map *)entry;
4245 remove_hash_entry(&mapper->hash, entry);
4252 * free_ftrace_func_mapper - free a mapping of ips and data
4253 * @mapper: The mapper that has the ip maps
4254 * @free_func: A function to be called on each data item.
4256 * This is used to free the function mapper. The @free_func is optional
4257 * and can be used if the data needs to be freed as well.
4259 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4260 ftrace_mapper_func free_func)
4262 struct ftrace_func_entry *entry;
4263 struct ftrace_func_map *map;
4264 struct hlist_head *hhd;
4270 if (free_func && mapper->hash.count) {
4271 size = 1 << mapper->hash.size_bits;
4272 for (i = 0; i < size; i++) {
4273 hhd = &mapper->hash.buckets[i];
4274 hlist_for_each_entry(entry, hhd, hlist) {
4275 map = (struct ftrace_func_map *)entry;
4280 free_ftrace_hash(&mapper->hash);
4283 static void release_probe(struct ftrace_func_probe *probe)
4285 struct ftrace_probe_ops *probe_ops;
4287 mutex_lock(&ftrace_lock);
4289 WARN_ON(probe->ref <= 0);
4291 /* Subtract the ref that was used to protect this instance */
4295 probe_ops = probe->probe_ops;
4297 * Sending zero as ip tells probe_ops to free
4298 * the probe->data itself
4300 if (probe_ops->free)
4301 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4302 list_del(&probe->list);
4305 mutex_unlock(&ftrace_lock);
4308 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4311 * Add one ref to keep it from being freed when releasing the
4312 * ftrace_lock mutex.
4318 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4319 struct ftrace_probe_ops *probe_ops,
4322 struct ftrace_func_entry *entry;
4323 struct ftrace_func_probe *probe;
4324 struct ftrace_hash **orig_hash;
4325 struct ftrace_hash *old_hash;
4326 struct ftrace_hash *hash;
4335 /* We do not support '!' for function probes */
4336 if (WARN_ON(glob[0] == '!'))
4340 mutex_lock(&ftrace_lock);
4341 /* Check if the probe_ops is already registered */
4342 list_for_each_entry(probe, &tr->func_probes, list) {
4343 if (probe->probe_ops == probe_ops)
4346 if (&probe->list == &tr->func_probes) {
4347 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4349 mutex_unlock(&ftrace_lock);
4352 probe->probe_ops = probe_ops;
4353 probe->ops.func = function_trace_probe_call;
4355 ftrace_ops_init(&probe->ops);
4356 list_add(&probe->list, &tr->func_probes);
4359 acquire_probe_locked(probe);
4361 mutex_unlock(&ftrace_lock);
4364 * Note, there's a small window here that the func_hash->filter_hash
4365 * may be NULL or empty. Need to be carefule when reading the loop.
4367 mutex_lock(&probe->ops.func_hash->regex_lock);
4369 orig_hash = &probe->ops.func_hash->filter_hash;
4370 old_hash = *orig_hash;
4371 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4378 ret = ftrace_match_records(hash, glob, strlen(glob));
4380 /* Nothing found? */
4387 size = 1 << hash->size_bits;
4388 for (i = 0; i < size; i++) {
4389 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4390 if (ftrace_lookup_ip(old_hash, entry->ip))
4393 * The caller might want to do something special
4394 * for each function we find. We call the callback
4395 * to give the caller an opportunity to do so.
4397 if (probe_ops->init) {
4398 ret = probe_ops->init(probe_ops, tr,
4402 if (probe_ops->free && count)
4403 probe_ops->free(probe_ops, tr,
4413 mutex_lock(&ftrace_lock);
4416 /* Nothing was added? */
4421 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4426 /* One ref for each new function traced */
4427 probe->ref += count;
4429 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4430 ret = ftrace_startup(&probe->ops, 0);
4433 mutex_unlock(&ftrace_lock);
4438 mutex_unlock(&probe->ops.func_hash->regex_lock);
4439 free_ftrace_hash(hash);
4441 release_probe(probe);
4446 if (!probe_ops->free || !count)
4449 /* Failed to do the move, need to call the free functions */
4450 for (i = 0; i < size; i++) {
4451 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4452 if (ftrace_lookup_ip(old_hash, entry->ip))
4454 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4461 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4462 struct ftrace_probe_ops *probe_ops)
4464 struct ftrace_ops_hash old_hash_ops;
4465 struct ftrace_func_entry *entry;
4466 struct ftrace_func_probe *probe;
4467 struct ftrace_glob func_g;
4468 struct ftrace_hash **orig_hash;
4469 struct ftrace_hash *old_hash;
4470 struct ftrace_hash *hash = NULL;
4471 struct hlist_node *tmp;
4472 struct hlist_head hhd;
4473 char str[KSYM_SYMBOL_LEN];
4475 int i, ret = -ENODEV;
4478 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4479 func_g.search = NULL;
4483 func_g.type = filter_parse_regex(glob, strlen(glob),
4484 &func_g.search, ¬);
4485 func_g.len = strlen(func_g.search);
4487 /* we do not support '!' for function probes */
4492 mutex_lock(&ftrace_lock);
4493 /* Check if the probe_ops is already registered */
4494 list_for_each_entry(probe, &tr->func_probes, list) {
4495 if (probe->probe_ops == probe_ops)
4498 if (&probe->list == &tr->func_probes)
4499 goto err_unlock_ftrace;
4502 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4503 goto err_unlock_ftrace;
4505 acquire_probe_locked(probe);
4507 mutex_unlock(&ftrace_lock);
4509 mutex_lock(&probe->ops.func_hash->regex_lock);
4511 orig_hash = &probe->ops.func_hash->filter_hash;
4512 old_hash = *orig_hash;
4514 if (ftrace_hash_empty(old_hash))
4517 old_hash_ops.filter_hash = old_hash;
4518 /* Probes only have filters */
4519 old_hash_ops.notrace_hash = NULL;
4522 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4526 INIT_HLIST_HEAD(&hhd);
4528 size = 1 << hash->size_bits;
4529 for (i = 0; i < size; i++) {
4530 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4532 if (func_g.search) {
4533 kallsyms_lookup(entry->ip, NULL, NULL,
4535 if (!ftrace_match(str, &func_g))
4539 remove_hash_entry(hash, entry);
4540 hlist_add_head(&entry->hlist, &hhd);
4544 /* Nothing found? */
4550 mutex_lock(&ftrace_lock);
4552 WARN_ON(probe->ref < count);
4554 probe->ref -= count;
4556 if (ftrace_hash_empty(hash))
4557 ftrace_shutdown(&probe->ops, 0);
4559 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4562 /* still need to update the function call sites */
4563 if (ftrace_enabled && !ftrace_hash_empty(hash))
4564 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4566 synchronize_sched();
4568 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4569 hlist_del(&entry->hlist);
4570 if (probe_ops->free)
4571 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4574 mutex_unlock(&ftrace_lock);
4577 mutex_unlock(&probe->ops.func_hash->regex_lock);
4578 free_ftrace_hash(hash);
4580 release_probe(probe);
4585 mutex_unlock(&ftrace_lock);
4589 void clear_ftrace_function_probes(struct trace_array *tr)
4591 struct ftrace_func_probe *probe, *n;
4593 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4594 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4597 static LIST_HEAD(ftrace_commands);
4598 static DEFINE_MUTEX(ftrace_cmd_mutex);
4601 * Currently we only register ftrace commands from __init, so mark this
4604 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4606 struct ftrace_func_command *p;
4609 mutex_lock(&ftrace_cmd_mutex);
4610 list_for_each_entry(p, &ftrace_commands, list) {
4611 if (strcmp(cmd->name, p->name) == 0) {
4616 list_add(&cmd->list, &ftrace_commands);
4618 mutex_unlock(&ftrace_cmd_mutex);
4624 * Currently we only unregister ftrace commands from __init, so mark
4627 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4629 struct ftrace_func_command *p, *n;
4632 mutex_lock(&ftrace_cmd_mutex);
4633 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4634 if (strcmp(cmd->name, p->name) == 0) {
4636 list_del_init(&p->list);
4641 mutex_unlock(&ftrace_cmd_mutex);
4646 static int ftrace_process_regex(struct ftrace_iterator *iter,
4647 char *buff, int len, int enable)
4649 struct ftrace_hash *hash = iter->hash;
4650 struct trace_array *tr = iter->ops->private;
4651 char *func, *command, *next = buff;
4652 struct ftrace_func_command *p;
4655 func = strsep(&next, ":");
4658 ret = ftrace_match_records(hash, func, len);
4668 command = strsep(&next, ":");
4670 mutex_lock(&ftrace_cmd_mutex);
4671 list_for_each_entry(p, &ftrace_commands, list) {
4672 if (strcmp(p->name, command) == 0) {
4673 ret = p->func(tr, hash, func, command, next, enable);
4678 mutex_unlock(&ftrace_cmd_mutex);
4684 ftrace_regex_write(struct file *file, const char __user *ubuf,
4685 size_t cnt, loff_t *ppos, int enable)
4687 struct ftrace_iterator *iter;
4688 struct trace_parser *parser;
4694 if (file->f_mode & FMODE_READ) {
4695 struct seq_file *m = file->private_data;
4698 iter = file->private_data;
4700 if (unlikely(ftrace_disabled))
4703 /* iter->hash is a local copy, so we don't need regex_lock */
4705 parser = &iter->parser;
4706 read = trace_get_user(parser, ubuf, cnt, ppos);
4708 if (read >= 0 && trace_parser_loaded(parser) &&
4709 !trace_parser_cont(parser)) {
4710 ret = ftrace_process_regex(iter, parser->buffer,
4711 parser->idx, enable);
4712 trace_parser_clear(parser);
4723 ftrace_filter_write(struct file *file, const char __user *ubuf,
4724 size_t cnt, loff_t *ppos)
4726 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4730 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4731 size_t cnt, loff_t *ppos)
4733 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4737 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4739 struct ftrace_func_entry *entry;
4741 if (!ftrace_location(ip))
4745 entry = ftrace_lookup_ip(hash, ip);
4748 free_hash_entry(hash, entry);
4752 return add_hash_entry(hash, ip);
4756 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4757 unsigned long ip, int remove, int reset, int enable)
4759 struct ftrace_hash **orig_hash;
4760 struct ftrace_hash *hash;
4763 if (unlikely(ftrace_disabled))
4766 mutex_lock(&ops->func_hash->regex_lock);
4769 orig_hash = &ops->func_hash->filter_hash;
4771 orig_hash = &ops->func_hash->notrace_hash;
4774 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4776 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4780 goto out_regex_unlock;
4783 if (buf && !ftrace_match_records(hash, buf, len)) {
4785 goto out_regex_unlock;
4788 ret = ftrace_match_addr(hash, ip, remove);
4790 goto out_regex_unlock;
4793 mutex_lock(&ftrace_lock);
4794 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4795 mutex_unlock(&ftrace_lock);
4798 mutex_unlock(&ops->func_hash->regex_lock);
4800 free_ftrace_hash(hash);
4805 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4806 int reset, int enable)
4808 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4812 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4813 * @ops - the ops to set the filter with
4814 * @ip - the address to add to or remove from the filter.
4815 * @remove - non zero to remove the ip from the filter
4816 * @reset - non zero to reset all filters before applying this filter.
4818 * Filters denote which functions should be enabled when tracing is enabled
4819 * If @ip is NULL, it failes to update filter.
4821 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4822 int remove, int reset)
4824 ftrace_ops_init(ops);
4825 return ftrace_set_addr(ops, ip, remove, reset, 1);
4827 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4830 * ftrace_ops_set_global_filter - setup ops to use global filters
4831 * @ops - the ops which will use the global filters
4833 * ftrace users who need global function trace filtering should call this.
4834 * It can set the global filter only if ops were not initialized before.
4836 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4838 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4841 ftrace_ops_init(ops);
4842 ops->func_hash = &global_ops.local_hash;
4844 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4847 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4848 int reset, int enable)
4850 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4854 * ftrace_set_filter - set a function to filter on in ftrace
4855 * @ops - the ops to set the filter with
4856 * @buf - the string that holds the function filter text.
4857 * @len - the length of the string.
4858 * @reset - non zero to reset all filters before applying this filter.
4860 * Filters denote which functions should be enabled when tracing is enabled.
4861 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4863 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4866 ftrace_ops_init(ops);
4867 return ftrace_set_regex(ops, buf, len, reset, 1);
4869 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4872 * ftrace_set_notrace - set a function to not trace in ftrace
4873 * @ops - the ops to set the notrace filter with
4874 * @buf - the string that holds the function notrace text.
4875 * @len - the length of the string.
4876 * @reset - non zero to reset all filters before applying this filter.
4878 * Notrace Filters denote which functions should not be enabled when tracing
4879 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4882 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4885 ftrace_ops_init(ops);
4886 return ftrace_set_regex(ops, buf, len, reset, 0);
4888 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4890 * ftrace_set_global_filter - set a function to filter on with global tracers
4891 * @buf - the string that holds the function filter text.
4892 * @len - the length of the string.
4893 * @reset - non zero to reset all filters before applying this filter.
4895 * Filters denote which functions should be enabled when tracing is enabled.
4896 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4898 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4900 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4902 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4905 * ftrace_set_global_notrace - set a function to not trace with global tracers
4906 * @buf - the string that holds the function notrace text.
4907 * @len - the length of the string.
4908 * @reset - non zero to reset all filters before applying this filter.
4910 * Notrace Filters denote which functions should not be enabled when tracing
4911 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4914 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4916 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4918 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4921 * command line interface to allow users to set filters on boot up.
4923 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4924 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4925 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4927 /* Used by function selftest to not test if filter is set */
4928 bool ftrace_filter_param __initdata;
4930 static int __init set_ftrace_notrace(char *str)
4932 ftrace_filter_param = true;
4933 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4936 __setup("ftrace_notrace=", set_ftrace_notrace);
4938 static int __init set_ftrace_filter(char *str)
4940 ftrace_filter_param = true;
4941 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4944 __setup("ftrace_filter=", set_ftrace_filter);
4946 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4947 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4948 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4949 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
4951 static int __init set_graph_function(char *str)
4953 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4956 __setup("ftrace_graph_filter=", set_graph_function);
4958 static int __init set_graph_notrace_function(char *str)
4960 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4963 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4965 static int __init set_graph_max_depth_function(char *str)
4969 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4972 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4974 static void __init set_ftrace_early_graph(char *buf, int enable)
4978 struct ftrace_hash *hash;
4980 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4985 func = strsep(&buf, ",");
4986 /* we allow only one expression at a time */
4987 ret = ftrace_graph_set_hash(hash, func);
4989 printk(KERN_DEBUG "ftrace: function %s not "
4990 "traceable\n", func);
4994 ftrace_graph_hash = hash;
4996 ftrace_graph_notrace_hash = hash;
4998 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5001 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
5005 ftrace_ops_init(ops);
5008 func = strsep(&buf, ",");
5009 ftrace_set_regex(ops, func, strlen(func), 0, enable);
5013 static void __init set_ftrace_early_filters(void)
5015 if (ftrace_filter_buf[0])
5016 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5017 if (ftrace_notrace_buf[0])
5018 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5019 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5020 if (ftrace_graph_buf[0])
5021 set_ftrace_early_graph(ftrace_graph_buf, 1);
5022 if (ftrace_graph_notrace_buf[0])
5023 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5024 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5027 int ftrace_regex_release(struct inode *inode, struct file *file)
5029 struct seq_file *m = (struct seq_file *)file->private_data;
5030 struct ftrace_iterator *iter;
5031 struct ftrace_hash **orig_hash;
5032 struct trace_parser *parser;
5036 if (file->f_mode & FMODE_READ) {
5038 seq_release(inode, file);
5040 iter = file->private_data;
5042 parser = &iter->parser;
5043 if (trace_parser_loaded(parser)) {
5044 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
5046 ftrace_process_regex(iter, parser->buffer,
5047 parser->idx, enable);
5050 trace_parser_put(parser);
5052 mutex_lock(&iter->ops->func_hash->regex_lock);
5054 if (file->f_mode & FMODE_WRITE) {
5055 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5058 orig_hash = &iter->ops->func_hash->filter_hash;
5060 if (list_empty(&iter->tr->mod_trace))
5061 iter->hash->flags &= ~FTRACE_HASH_FL_MOD;
5063 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5066 orig_hash = &iter->ops->func_hash->notrace_hash;
5068 mutex_lock(&ftrace_lock);
5069 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5070 iter->hash, filter_hash);
5071 mutex_unlock(&ftrace_lock);
5073 /* For read only, the hash is the ops hash */
5077 mutex_unlock(&iter->ops->func_hash->regex_lock);
5078 free_ftrace_hash(iter->hash);
5080 trace_array_put(iter->tr);
5086 static const struct file_operations ftrace_avail_fops = {
5087 .open = ftrace_avail_open,
5089 .llseek = seq_lseek,
5090 .release = seq_release_private,
5093 static const struct file_operations ftrace_enabled_fops = {
5094 .open = ftrace_enabled_open,
5096 .llseek = seq_lseek,
5097 .release = seq_release_private,
5100 static const struct file_operations ftrace_filter_fops = {
5101 .open = ftrace_filter_open,
5103 .write = ftrace_filter_write,
5104 .llseek = tracing_lseek,
5105 .release = ftrace_regex_release,
5108 static const struct file_operations ftrace_notrace_fops = {
5109 .open = ftrace_notrace_open,
5111 .write = ftrace_notrace_write,
5112 .llseek = tracing_lseek,
5113 .release = ftrace_regex_release,
5116 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5118 static DEFINE_MUTEX(graph_lock);
5120 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
5121 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
5123 enum graph_filter_type {
5124 GRAPH_FILTER_NOTRACE = 0,
5125 GRAPH_FILTER_FUNCTION,
5128 #define FTRACE_GRAPH_EMPTY ((void *)1)
5130 struct ftrace_graph_data {
5131 struct ftrace_hash *hash;
5132 struct ftrace_func_entry *entry;
5133 int idx; /* for hash table iteration */
5134 enum graph_filter_type type;
5135 struct ftrace_hash *new_hash;
5136 const struct seq_operations *seq_ops;
5137 struct trace_parser parser;
5141 __g_next(struct seq_file *m, loff_t *pos)
5143 struct ftrace_graph_data *fgd = m->private;
5144 struct ftrace_func_entry *entry = fgd->entry;
5145 struct hlist_head *head;
5146 int i, idx = fgd->idx;
5148 if (*pos >= fgd->hash->count)
5152 hlist_for_each_entry_continue(entry, hlist) {
5160 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5161 head = &fgd->hash->buckets[i];
5162 hlist_for_each_entry(entry, head, hlist) {
5172 g_next(struct seq_file *m, void *v, loff_t *pos)
5175 return __g_next(m, pos);
5178 static void *g_start(struct seq_file *m, loff_t *pos)
5180 struct ftrace_graph_data *fgd = m->private;
5182 mutex_lock(&graph_lock);
5184 if (fgd->type == GRAPH_FILTER_FUNCTION)
5185 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5186 lockdep_is_held(&graph_lock));
5188 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5189 lockdep_is_held(&graph_lock));
5191 /* Nothing, tell g_show to print all functions are enabled */
5192 if (ftrace_hash_empty(fgd->hash) && !*pos)
5193 return FTRACE_GRAPH_EMPTY;
5197 return __g_next(m, pos);
5200 static void g_stop(struct seq_file *m, void *p)
5202 mutex_unlock(&graph_lock);
5205 static int g_show(struct seq_file *m, void *v)
5207 struct ftrace_func_entry *entry = v;
5212 if (entry == FTRACE_GRAPH_EMPTY) {
5213 struct ftrace_graph_data *fgd = m->private;
5215 if (fgd->type == GRAPH_FILTER_FUNCTION)
5216 seq_puts(m, "#### all functions enabled ####\n");
5218 seq_puts(m, "#### no functions disabled ####\n");
5222 seq_printf(m, "%ps\n", (void *)entry->ip);
5227 static const struct seq_operations ftrace_graph_seq_ops = {
5235 __ftrace_graph_open(struct inode *inode, struct file *file,
5236 struct ftrace_graph_data *fgd)
5239 struct ftrace_hash *new_hash = NULL;
5241 if (file->f_mode & FMODE_WRITE) {
5242 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5244 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5247 if (file->f_flags & O_TRUNC)
5248 new_hash = alloc_ftrace_hash(size_bits);
5250 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5258 if (file->f_mode & FMODE_READ) {
5259 ret = seq_open(file, &ftrace_graph_seq_ops);
5261 struct seq_file *m = file->private_data;
5265 free_ftrace_hash(new_hash);
5269 file->private_data = fgd;
5272 if (ret < 0 && file->f_mode & FMODE_WRITE)
5273 trace_parser_put(&fgd->parser);
5275 fgd->new_hash = new_hash;
5278 * All uses of fgd->hash must be taken with the graph_lock
5279 * held. The graph_lock is going to be released, so force
5280 * fgd->hash to be reinitialized when it is taken again.
5288 ftrace_graph_open(struct inode *inode, struct file *file)
5290 struct ftrace_graph_data *fgd;
5293 if (unlikely(ftrace_disabled))
5296 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5300 mutex_lock(&graph_lock);
5302 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5303 lockdep_is_held(&graph_lock));
5304 fgd->type = GRAPH_FILTER_FUNCTION;
5305 fgd->seq_ops = &ftrace_graph_seq_ops;
5307 ret = __ftrace_graph_open(inode, file, fgd);
5311 mutex_unlock(&graph_lock);
5316 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5318 struct ftrace_graph_data *fgd;
5321 if (unlikely(ftrace_disabled))
5324 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5328 mutex_lock(&graph_lock);
5330 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5331 lockdep_is_held(&graph_lock));
5332 fgd->type = GRAPH_FILTER_NOTRACE;
5333 fgd->seq_ops = &ftrace_graph_seq_ops;
5335 ret = __ftrace_graph_open(inode, file, fgd);
5339 mutex_unlock(&graph_lock);
5344 ftrace_graph_release(struct inode *inode, struct file *file)
5346 struct ftrace_graph_data *fgd;
5347 struct ftrace_hash *old_hash, *new_hash;
5348 struct trace_parser *parser;
5351 if (file->f_mode & FMODE_READ) {
5352 struct seq_file *m = file->private_data;
5355 seq_release(inode, file);
5357 fgd = file->private_data;
5361 if (file->f_mode & FMODE_WRITE) {
5363 parser = &fgd->parser;
5365 if (trace_parser_loaded((parser))) {
5366 ret = ftrace_graph_set_hash(fgd->new_hash,
5370 trace_parser_put(parser);
5372 new_hash = __ftrace_hash_move(fgd->new_hash);
5378 mutex_lock(&graph_lock);
5380 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5381 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5382 lockdep_is_held(&graph_lock));
5383 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5385 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5386 lockdep_is_held(&graph_lock));
5387 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5390 mutex_unlock(&graph_lock);
5393 * We need to do a hard force of sched synchronization.
5394 * This is because we use preempt_disable() to do RCU, but
5395 * the function tracers can be called where RCU is not watching
5396 * (like before user_exit()). We can not rely on the RCU
5397 * infrastructure to do the synchronization, thus we must do it
5400 schedule_on_each_cpu(ftrace_sync);
5402 free_ftrace_hash(old_hash);
5406 free_ftrace_hash(fgd->new_hash);
5413 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5415 struct ftrace_glob func_g;
5416 struct dyn_ftrace *rec;
5417 struct ftrace_page *pg;
5418 struct ftrace_func_entry *entry;
5423 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5424 &func_g.search, ¬);
5426 func_g.len = strlen(func_g.search);
5428 mutex_lock(&ftrace_lock);
5430 if (unlikely(ftrace_disabled)) {
5431 mutex_unlock(&ftrace_lock);
5435 do_for_each_ftrace_rec(pg, rec) {
5437 if (rec->flags & FTRACE_FL_DISABLED)
5440 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5441 entry = ftrace_lookup_ip(hash, rec->ip);
5448 if (add_hash_entry(hash, rec->ip) < 0)
5452 free_hash_entry(hash, entry);
5457 } while_for_each_ftrace_rec();
5459 mutex_unlock(&ftrace_lock);
5468 ftrace_graph_write(struct file *file, const char __user *ubuf,
5469 size_t cnt, loff_t *ppos)
5471 ssize_t read, ret = 0;
5472 struct ftrace_graph_data *fgd = file->private_data;
5473 struct trace_parser *parser;
5478 /* Read mode uses seq functions */
5479 if (file->f_mode & FMODE_READ) {
5480 struct seq_file *m = file->private_data;
5484 parser = &fgd->parser;
5486 read = trace_get_user(parser, ubuf, cnt, ppos);
5488 if (read >= 0 && trace_parser_loaded(parser) &&
5489 !trace_parser_cont(parser)) {
5491 ret = ftrace_graph_set_hash(fgd->new_hash,
5493 trace_parser_clear(parser);
5502 static const struct file_operations ftrace_graph_fops = {
5503 .open = ftrace_graph_open,
5505 .write = ftrace_graph_write,
5506 .llseek = tracing_lseek,
5507 .release = ftrace_graph_release,
5510 static const struct file_operations ftrace_graph_notrace_fops = {
5511 .open = ftrace_graph_notrace_open,
5513 .write = ftrace_graph_write,
5514 .llseek = tracing_lseek,
5515 .release = ftrace_graph_release,
5517 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5519 void ftrace_create_filter_files(struct ftrace_ops *ops,
5520 struct dentry *parent)
5523 trace_create_file("set_ftrace_filter", 0644, parent,
5524 ops, &ftrace_filter_fops);
5526 trace_create_file("set_ftrace_notrace", 0644, parent,
5527 ops, &ftrace_notrace_fops);
5531 * The name "destroy_filter_files" is really a misnomer. Although
5532 * in the future, it may actualy delete the files, but this is
5533 * really intended to make sure the ops passed in are disabled
5534 * and that when this function returns, the caller is free to
5537 * The "destroy" name is only to match the "create" name that this
5538 * should be paired with.
5540 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5542 mutex_lock(&ftrace_lock);
5543 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5544 ftrace_shutdown(ops, 0);
5545 ops->flags |= FTRACE_OPS_FL_DELETED;
5546 ftrace_free_filter(ops);
5547 mutex_unlock(&ftrace_lock);
5550 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
5553 trace_create_file("available_filter_functions", 0444,
5554 d_tracer, NULL, &ftrace_avail_fops);
5556 trace_create_file("enabled_functions", 0444,
5557 d_tracer, NULL, &ftrace_enabled_fops);
5559 ftrace_create_filter_files(&global_ops, d_tracer);
5561 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5562 trace_create_file("set_graph_function", 0644, d_tracer,
5564 &ftrace_graph_fops);
5565 trace_create_file("set_graph_notrace", 0644, d_tracer,
5567 &ftrace_graph_notrace_fops);
5568 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5573 static int ftrace_cmp_ips(const void *a, const void *b)
5575 const unsigned long *ipa = a;
5576 const unsigned long *ipb = b;
5585 static int ftrace_process_locs(struct module *mod,
5586 unsigned long *start,
5589 struct ftrace_page *start_pg;
5590 struct ftrace_page *pg;
5591 struct dyn_ftrace *rec;
5592 unsigned long count;
5595 unsigned long flags = 0; /* Shut up gcc */
5598 count = end - start;
5603 sort(start, count, sizeof(*start),
5604 ftrace_cmp_ips, NULL);
5606 start_pg = ftrace_allocate_pages(count);
5610 mutex_lock(&ftrace_lock);
5613 * Core and each module needs their own pages, as
5614 * modules will free them when they are removed.
5615 * Force a new page to be allocated for modules.
5618 WARN_ON(ftrace_pages || ftrace_pages_start);
5619 /* First initialization */
5620 ftrace_pages = ftrace_pages_start = start_pg;
5625 if (WARN_ON(ftrace_pages->next)) {
5626 /* Hmm, we have free pages? */
5627 while (ftrace_pages->next)
5628 ftrace_pages = ftrace_pages->next;
5631 ftrace_pages->next = start_pg;
5637 addr = ftrace_call_adjust(*p++);
5639 * Some architecture linkers will pad between
5640 * the different mcount_loc sections of different
5641 * object files to satisfy alignments.
5642 * Skip any NULL pointers.
5647 if (pg->index == pg->size) {
5648 /* We should have allocated enough */
5649 if (WARN_ON(!pg->next))
5654 rec = &pg->records[pg->index++];
5658 /* We should have used all pages */
5661 /* Assign the last page to ftrace_pages */
5665 * We only need to disable interrupts on start up
5666 * because we are modifying code that an interrupt
5667 * may execute, and the modification is not atomic.
5668 * But for modules, nothing runs the code we modify
5669 * until we are finished with it, and there's no
5670 * reason to cause large interrupt latencies while we do it.
5673 local_irq_save(flags);
5674 ftrace_update_code(mod, start_pg);
5676 local_irq_restore(flags);
5679 mutex_unlock(&ftrace_lock);
5684 struct ftrace_mod_func {
5685 struct list_head list;
5691 struct ftrace_mod_map {
5692 struct rcu_head rcu;
5693 struct list_head list;
5695 unsigned long start_addr;
5696 unsigned long end_addr;
5697 struct list_head funcs;
5698 unsigned int num_funcs;
5701 #ifdef CONFIG_MODULES
5703 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5705 static LIST_HEAD(ftrace_mod_maps);
5707 static int referenced_filters(struct dyn_ftrace *rec)
5709 struct ftrace_ops *ops;
5712 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5713 if (ops_references_rec(ops, rec)) {
5715 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
5716 rec->flags |= FTRACE_FL_REGS;
5724 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5726 struct ftrace_func_entry *entry;
5727 struct dyn_ftrace *rec;
5730 if (ftrace_hash_empty(hash))
5733 for (i = 0; i < pg->index; i++) {
5734 rec = &pg->records[i];
5735 entry = __ftrace_lookup_ip(hash, rec->ip);
5737 * Do not allow this rec to match again.
5738 * Yeah, it may waste some memory, but will be removed
5739 * if/when the hash is modified again.
5746 /* Clear any records from hashs */
5747 static void clear_mod_from_hashes(struct ftrace_page *pg)
5749 struct trace_array *tr;
5751 mutex_lock(&trace_types_lock);
5752 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5753 if (!tr->ops || !tr->ops->func_hash)
5755 mutex_lock(&tr->ops->func_hash->regex_lock);
5756 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5757 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5758 mutex_unlock(&tr->ops->func_hash->regex_lock);
5760 mutex_unlock(&trace_types_lock);
5763 static void ftrace_free_mod_map(struct rcu_head *rcu)
5765 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5766 struct ftrace_mod_func *mod_func;
5767 struct ftrace_mod_func *n;
5769 /* All the contents of mod_map are now not visible to readers */
5770 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5771 kfree(mod_func->name);
5772 list_del(&mod_func->list);
5779 void ftrace_release_mod(struct module *mod)
5781 struct ftrace_mod_map *mod_map;
5782 struct ftrace_mod_map *n;
5783 struct dyn_ftrace *rec;
5784 struct ftrace_page **last_pg;
5785 struct ftrace_page *tmp_page = NULL;
5786 struct ftrace_page *pg;
5789 mutex_lock(&ftrace_lock);
5791 if (ftrace_disabled)
5794 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5795 if (mod_map->mod == mod) {
5796 list_del_rcu(&mod_map->list);
5797 call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
5803 * Each module has its own ftrace_pages, remove
5804 * them from the list.
5806 last_pg = &ftrace_pages_start;
5807 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5808 rec = &pg->records[0];
5809 if (within_module_core(rec->ip, mod) ||
5810 within_module_init(rec->ip, mod)) {
5812 * As core pages are first, the first
5813 * page should never be a module page.
5815 if (WARN_ON(pg == ftrace_pages_start))
5818 /* Check if we are deleting the last page */
5819 if (pg == ftrace_pages)
5820 ftrace_pages = next_to_ftrace_page(last_pg);
5822 ftrace_update_tot_cnt -= pg->index;
5823 *last_pg = pg->next;
5825 pg->next = tmp_page;
5828 last_pg = &pg->next;
5831 mutex_unlock(&ftrace_lock);
5833 for (pg = tmp_page; pg; pg = tmp_page) {
5835 /* Needs to be called outside of ftrace_lock */
5836 clear_mod_from_hashes(pg);
5838 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5839 free_pages((unsigned long)pg->records, order);
5840 tmp_page = pg->next;
5845 void ftrace_module_enable(struct module *mod)
5847 struct dyn_ftrace *rec;
5848 struct ftrace_page *pg;
5850 mutex_lock(&ftrace_lock);
5852 if (ftrace_disabled)
5856 * If the tracing is enabled, go ahead and enable the record.
5858 * The reason not to enable the record immediatelly is the
5859 * inherent check of ftrace_make_nop/ftrace_make_call for
5860 * correct previous instructions. Making first the NOP
5861 * conversion puts the module to the correct state, thus
5862 * passing the ftrace_make_call check.
5864 * We also delay this to after the module code already set the
5865 * text to read-only, as we now need to set it back to read-write
5866 * so that we can modify the text.
5868 if (ftrace_start_up)
5869 ftrace_arch_code_modify_prepare();
5871 do_for_each_ftrace_rec(pg, rec) {
5874 * do_for_each_ftrace_rec() is a double loop.
5875 * module text shares the pg. If a record is
5876 * not part of this module, then skip this pg,
5877 * which the "break" will do.
5879 if (!within_module_core(rec->ip, mod) &&
5880 !within_module_init(rec->ip, mod))
5886 * When adding a module, we need to check if tracers are
5887 * currently enabled and if they are, and can trace this record,
5888 * we need to enable the module functions as well as update the
5889 * reference counts for those function records.
5891 if (ftrace_start_up)
5892 cnt += referenced_filters(rec);
5894 rec->flags &= ~FTRACE_FL_DISABLED;
5897 if (ftrace_start_up && cnt) {
5898 int failed = __ftrace_replace_code(rec, 1);
5900 ftrace_bug(failed, rec);
5905 } while_for_each_ftrace_rec();
5908 if (ftrace_start_up)
5909 ftrace_arch_code_modify_post_process();
5912 mutex_unlock(&ftrace_lock);
5914 process_cached_mods(mod->name);
5917 void ftrace_module_init(struct module *mod)
5919 if (ftrace_disabled || !mod->num_ftrace_callsites)
5922 ftrace_process_locs(mod, mod->ftrace_callsites,
5923 mod->ftrace_callsites + mod->num_ftrace_callsites);
5926 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5927 struct dyn_ftrace *rec)
5929 struct ftrace_mod_func *mod_func;
5930 unsigned long symsize;
5931 unsigned long offset;
5932 char str[KSYM_SYMBOL_LEN];
5936 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5940 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5944 mod_func->name = kstrdup(str, GFP_KERNEL);
5945 if (!mod_func->name) {
5950 mod_func->ip = rec->ip - offset;
5951 mod_func->size = symsize;
5953 mod_map->num_funcs++;
5955 list_add_rcu(&mod_func->list, &mod_map->funcs);
5958 static struct ftrace_mod_map *
5959 allocate_ftrace_mod_map(struct module *mod,
5960 unsigned long start, unsigned long end)
5962 struct ftrace_mod_map *mod_map;
5964 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5969 mod_map->start_addr = start;
5970 mod_map->end_addr = end;
5971 mod_map->num_funcs = 0;
5973 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5975 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5981 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5982 unsigned long addr, unsigned long *size,
5983 unsigned long *off, char *sym)
5985 struct ftrace_mod_func *found_func = NULL;
5986 struct ftrace_mod_func *mod_func;
5988 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5989 if (addr >= mod_func->ip &&
5990 addr < mod_func->ip + mod_func->size) {
5991 found_func = mod_func;
5998 *size = found_func->size;
6000 *off = addr - found_func->ip;
6002 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
6004 return found_func->name;
6011 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6012 unsigned long *off, char **modname, char *sym)
6014 struct ftrace_mod_map *mod_map;
6015 const char *ret = NULL;
6017 /* mod_map is freed via call_rcu_sched() */
6019 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6020 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6023 *modname = mod_map->mod->name;
6032 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6033 char *type, char *name,
6034 char *module_name, int *exported)
6036 struct ftrace_mod_map *mod_map;
6037 struct ftrace_mod_func *mod_func;
6040 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6042 if (symnum >= mod_map->num_funcs) {
6043 symnum -= mod_map->num_funcs;
6047 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6053 *value = mod_func->ip;
6055 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6056 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6069 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6070 struct dyn_ftrace *rec) { }
6071 static inline struct ftrace_mod_map *
6072 allocate_ftrace_mod_map(struct module *mod,
6073 unsigned long start, unsigned long end)
6077 #endif /* CONFIG_MODULES */
6079 struct ftrace_init_func {
6080 struct list_head list;
6084 /* Clear any init ips from hashes */
6086 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6088 struct ftrace_func_entry *entry;
6090 if (ftrace_hash_empty(hash))
6093 entry = __ftrace_lookup_ip(hash, func->ip);
6096 * Do not allow this rec to match again.
6097 * Yeah, it may waste some memory, but will be removed
6098 * if/when the hash is modified again.
6105 clear_func_from_hashes(struct ftrace_init_func *func)
6107 struct trace_array *tr;
6109 mutex_lock(&trace_types_lock);
6110 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6111 if (!tr->ops || !tr->ops->func_hash)
6113 mutex_lock(&tr->ops->func_hash->regex_lock);
6114 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6115 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6116 mutex_unlock(&tr->ops->func_hash->regex_lock);
6118 mutex_unlock(&trace_types_lock);
6121 static void add_to_clear_hash_list(struct list_head *clear_list,
6122 struct dyn_ftrace *rec)
6124 struct ftrace_init_func *func;
6126 func = kmalloc(sizeof(*func), GFP_KERNEL);
6128 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6133 list_add(&func->list, clear_list);
6136 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
6138 unsigned long start = (unsigned long)(start_ptr);
6139 unsigned long end = (unsigned long)(end_ptr);
6140 struct ftrace_page **last_pg = &ftrace_pages_start;
6141 struct ftrace_page *pg;
6142 struct dyn_ftrace *rec;
6143 struct dyn_ftrace key;
6144 struct ftrace_mod_map *mod_map = NULL;
6145 struct ftrace_init_func *func, *func_next;
6146 struct list_head clear_hash;
6149 INIT_LIST_HEAD(&clear_hash);
6152 key.flags = end; /* overload flags, as it is unsigned long */
6154 mutex_lock(&ftrace_lock);
6157 * If we are freeing module init memory, then check if
6158 * any tracer is active. If so, we need to save a mapping of
6159 * the module functions being freed with the address.
6161 if (mod && ftrace_ops_list != &ftrace_list_end)
6162 mod_map = allocate_ftrace_mod_map(mod, start, end);
6164 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6165 if (end < pg->records[0].ip ||
6166 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6169 rec = bsearch(&key, pg->records, pg->index,
6170 sizeof(struct dyn_ftrace),
6175 /* rec will be cleared from hashes after ftrace_lock unlock */
6176 add_to_clear_hash_list(&clear_hash, rec);
6179 save_ftrace_mod_rec(mod_map, rec);
6182 ftrace_update_tot_cnt--;
6184 *last_pg = pg->next;
6185 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6186 free_pages((unsigned long)pg->records, order);
6188 pg = container_of(last_pg, struct ftrace_page, next);
6193 memmove(rec, rec + 1,
6194 (pg->index - (rec - pg->records)) * sizeof(*rec));
6195 /* More than one function may be in this block */
6198 mutex_unlock(&ftrace_lock);
6200 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6201 clear_func_from_hashes(func);
6206 void __init ftrace_free_init_mem(void)
6208 void *start = (void *)(&__init_begin);
6209 void *end = (void *)(&__init_end);
6211 ftrace_free_mem(NULL, start, end);
6214 void __init ftrace_init(void)
6216 extern unsigned long __start_mcount_loc[];
6217 extern unsigned long __stop_mcount_loc[];
6218 unsigned long count, flags;
6221 local_irq_save(flags);
6222 ret = ftrace_dyn_arch_init();
6223 local_irq_restore(flags);
6227 count = __stop_mcount_loc - __start_mcount_loc;
6229 pr_info("ftrace: No functions to be traced?\n");
6233 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6234 count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
6236 last_ftrace_enabled = ftrace_enabled = 1;
6238 ret = ftrace_process_locs(NULL,
6242 set_ftrace_early_filters();
6246 ftrace_disabled = 1;
6249 /* Do nothing if arch does not support this */
6250 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6254 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6256 arch_ftrace_update_trampoline(ops);
6259 void ftrace_init_trace_array(struct trace_array *tr)
6261 INIT_LIST_HEAD(&tr->func_probes);
6262 INIT_LIST_HEAD(&tr->mod_trace);
6263 INIT_LIST_HEAD(&tr->mod_notrace);
6267 static struct ftrace_ops global_ops = {
6268 .func = ftrace_stub,
6269 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6270 FTRACE_OPS_FL_INITIALIZED |
6274 static int __init ftrace_nodyn_init(void)
6279 core_initcall(ftrace_nodyn_init);
6281 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6282 static inline void ftrace_startup_enable(int command) { }
6283 static inline void ftrace_startup_all(int command) { }
6284 /* Keep as macros so we do not need to define the commands */
6285 # define ftrace_startup(ops, command) \
6287 int ___ret = __register_ftrace_function(ops); \
6289 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
6292 # define ftrace_shutdown(ops, command) \
6294 int ___ret = __unregister_ftrace_function(ops); \
6296 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
6300 # define ftrace_startup_sysctl() do { } while (0)
6301 # define ftrace_shutdown_sysctl() do { } while (0)
6304 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
6309 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6313 #endif /* CONFIG_DYNAMIC_FTRACE */
6315 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6317 tr->ops = &global_ops;
6318 tr->ops->private = tr;
6319 ftrace_init_trace_array(tr);
6322 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6324 /* If we filter on pids, update to use the pid function */
6325 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6326 if (WARN_ON(tr->ops->func != ftrace_stub))
6327 printk("ftrace ops had %pS for function\n",
6330 tr->ops->func = func;
6331 tr->ops->private = tr;
6334 void ftrace_reset_array_ops(struct trace_array *tr)
6336 tr->ops->func = ftrace_stub;
6339 static nokprobe_inline void
6340 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6341 struct ftrace_ops *ignored, struct pt_regs *regs)
6343 struct ftrace_ops *op;
6346 bit = trace_test_and_set_recursion(TRACE_LIST_START);
6351 * Some of the ops may be dynamically allocated,
6352 * they must be freed after a synchronize_sched().
6354 preempt_disable_notrace();
6356 do_for_each_ftrace_op(op, ftrace_ops_list) {
6358 * Check the following for each ops before calling their func:
6359 * if RCU flag is set, then rcu_is_watching() must be true
6360 * if PER_CPU is set, then ftrace_function_local_disable()
6362 * Otherwise test if the ip matches the ops filter
6364 * If any of the above fails then the op->func() is not executed.
6366 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6367 ftrace_ops_test(op, ip, regs)) {
6368 if (FTRACE_WARN_ON(!op->func)) {
6369 pr_warn("op=%p %pS\n", op, op);
6372 op->func(ip, parent_ip, op, regs);
6374 } while_for_each_ftrace_op(op);
6376 preempt_enable_notrace();
6377 trace_clear_recursion(bit);
6381 * Some archs only support passing ip and parent_ip. Even though
6382 * the list function ignores the op parameter, we do not want any
6383 * C side effects, where a function is called without the caller
6384 * sending a third parameter.
6385 * Archs are to support both the regs and ftrace_ops at the same time.
6386 * If they support ftrace_ops, it is assumed they support regs.
6387 * If call backs want to use regs, they must either check for regs
6388 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6389 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6390 * An architecture can pass partial regs with ftrace_ops and still
6391 * set the ARCH_SUPPORTS_FTRACE_OPS.
6393 #if ARCH_SUPPORTS_FTRACE_OPS
6394 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6395 struct ftrace_ops *op, struct pt_regs *regs)
6397 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6399 NOKPROBE_SYMBOL(ftrace_ops_list_func);
6401 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6403 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6405 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
6409 * If there's only one function registered but it does not support
6410 * recursion, needs RCU protection and/or requires per cpu handling, then
6411 * this function will be called by the mcount trampoline.
6413 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6414 struct ftrace_ops *op, struct pt_regs *regs)
6418 bit = trace_test_and_set_recursion(TRACE_LIST_START);
6422 preempt_disable_notrace();
6424 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
6425 op->func(ip, parent_ip, op, regs);
6427 preempt_enable_notrace();
6428 trace_clear_recursion(bit);
6430 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
6433 * ftrace_ops_get_func - get the function a trampoline should call
6434 * @ops: the ops to get the function for
6436 * Normally the mcount trampoline will call the ops->func, but there
6437 * are times that it should not. For example, if the ops does not
6438 * have its own recursion protection, then it should call the
6439 * ftrace_ops_assist_func() instead.
6441 * Returns the function that the trampoline should call for @ops.
6443 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6446 * If the function does not handle recursion, needs to be RCU safe,
6447 * or does per cpu logic, then we need to call the assist handler.
6449 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6450 ops->flags & FTRACE_OPS_FL_RCU)
6451 return ftrace_ops_assist_func;
6457 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6458 struct task_struct *prev, struct task_struct *next)
6460 struct trace_array *tr = data;
6461 struct trace_pid_list *pid_list;
6463 pid_list = rcu_dereference_sched(tr->function_pids);
6465 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6466 trace_ignore_this_task(pid_list, next));
6470 ftrace_pid_follow_sched_process_fork(void *data,
6471 struct task_struct *self,
6472 struct task_struct *task)
6474 struct trace_pid_list *pid_list;
6475 struct trace_array *tr = data;
6477 pid_list = rcu_dereference_sched(tr->function_pids);
6478 trace_filter_add_remove_task(pid_list, self, task);
6482 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6484 struct trace_pid_list *pid_list;
6485 struct trace_array *tr = data;
6487 pid_list = rcu_dereference_sched(tr->function_pids);
6488 trace_filter_add_remove_task(pid_list, NULL, task);
6491 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6494 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6496 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6499 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6501 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6506 static void clear_ftrace_pids(struct trace_array *tr)
6508 struct trace_pid_list *pid_list;
6511 pid_list = rcu_dereference_protected(tr->function_pids,
6512 lockdep_is_held(&ftrace_lock));
6516 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6518 for_each_possible_cpu(cpu)
6519 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6521 rcu_assign_pointer(tr->function_pids, NULL);
6523 /* Wait till all users are no longer using pid filtering */
6524 synchronize_sched();
6526 trace_free_pid_list(pid_list);
6529 void ftrace_clear_pids(struct trace_array *tr)
6531 mutex_lock(&ftrace_lock);
6533 clear_ftrace_pids(tr);
6535 mutex_unlock(&ftrace_lock);
6538 static void ftrace_pid_reset(struct trace_array *tr)
6540 mutex_lock(&ftrace_lock);
6541 clear_ftrace_pids(tr);
6543 ftrace_update_pid_func();
6544 ftrace_startup_all(0);
6546 mutex_unlock(&ftrace_lock);
6549 /* Greater than any max PID */
6550 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6552 static void *fpid_start(struct seq_file *m, loff_t *pos)
6555 struct trace_pid_list *pid_list;
6556 struct trace_array *tr = m->private;
6558 mutex_lock(&ftrace_lock);
6559 rcu_read_lock_sched();
6561 pid_list = rcu_dereference_sched(tr->function_pids);
6564 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6566 return trace_pid_start(pid_list, pos);
6569 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6571 struct trace_array *tr = m->private;
6572 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6574 if (v == FTRACE_NO_PIDS) {
6578 return trace_pid_next(pid_list, v, pos);
6581 static void fpid_stop(struct seq_file *m, void *p)
6584 rcu_read_unlock_sched();
6585 mutex_unlock(&ftrace_lock);
6588 static int fpid_show(struct seq_file *m, void *v)
6590 if (v == FTRACE_NO_PIDS) {
6591 seq_puts(m, "no pid\n");
6595 return trace_pid_show(m, v);
6598 static const struct seq_operations ftrace_pid_sops = {
6599 .start = fpid_start,
6606 ftrace_pid_open(struct inode *inode, struct file *file)
6608 struct trace_array *tr = inode->i_private;
6612 if (trace_array_get(tr) < 0)
6615 if ((file->f_mode & FMODE_WRITE) &&
6616 (file->f_flags & O_TRUNC))
6617 ftrace_pid_reset(tr);
6619 ret = seq_open(file, &ftrace_pid_sops);
6621 trace_array_put(tr);
6623 m = file->private_data;
6624 /* copy tr over to seq ops */
6631 static void ignore_task_cpu(void *data)
6633 struct trace_array *tr = data;
6634 struct trace_pid_list *pid_list;
6637 * This function is called by on_each_cpu() while the
6638 * event_mutex is held.
6640 pid_list = rcu_dereference_protected(tr->function_pids,
6641 mutex_is_locked(&ftrace_lock));
6643 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6644 trace_ignore_this_task(pid_list, current));
6648 ftrace_pid_write(struct file *filp, const char __user *ubuf,
6649 size_t cnt, loff_t *ppos)
6651 struct seq_file *m = filp->private_data;
6652 struct trace_array *tr = m->private;
6653 struct trace_pid_list *filtered_pids = NULL;
6654 struct trace_pid_list *pid_list;
6660 mutex_lock(&ftrace_lock);
6662 filtered_pids = rcu_dereference_protected(tr->function_pids,
6663 lockdep_is_held(&ftrace_lock));
6665 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6669 rcu_assign_pointer(tr->function_pids, pid_list);
6671 if (filtered_pids) {
6672 synchronize_sched();
6673 trace_free_pid_list(filtered_pids);
6674 } else if (pid_list) {
6675 /* Register a probe to set whether to ignore the tracing of a task */
6676 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6680 * Ignoring of pids is done at task switch. But we have to
6681 * check for those tasks that are currently running.
6682 * Always do this in case a pid was appended or removed.
6684 on_each_cpu(ignore_task_cpu, tr, 1);
6686 ftrace_update_pid_func();
6687 ftrace_startup_all(0);
6689 mutex_unlock(&ftrace_lock);
6698 ftrace_pid_release(struct inode *inode, struct file *file)
6700 struct trace_array *tr = inode->i_private;
6702 trace_array_put(tr);
6704 return seq_release(inode, file);
6707 static const struct file_operations ftrace_pid_fops = {
6708 .open = ftrace_pid_open,
6709 .write = ftrace_pid_write,
6711 .llseek = tracing_lseek,
6712 .release = ftrace_pid_release,
6715 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
6717 trace_create_file("set_ftrace_pid", 0644, d_tracer,
6718 tr, &ftrace_pid_fops);
6721 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6722 struct dentry *d_tracer)
6724 /* Only the top level directory has the dyn_tracefs and profile */
6725 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6727 ftrace_init_dyn_tracefs(d_tracer);
6728 ftrace_profile_tracefs(d_tracer);
6732 * ftrace_kill - kill ftrace
6734 * This function should be used by panic code. It stops ftrace
6735 * but in a not so nice way. If you need to simply kill ftrace
6736 * from a non-atomic section, use ftrace_kill.
6738 void ftrace_kill(void)
6740 ftrace_disabled = 1;
6742 ftrace_trace_function = ftrace_stub;
6746 * Test if ftrace is dead or not.
6748 int ftrace_is_dead(void)
6750 return ftrace_disabled;
6754 * register_ftrace_function - register a function for profiling
6755 * @ops - ops structure that holds the function for profiling.
6757 * Register a function to be called by all functions in the
6760 * Note: @ops->func and all the functions it calls must be labeled
6761 * with "notrace", otherwise it will go into a
6764 int register_ftrace_function(struct ftrace_ops *ops)
6768 ftrace_ops_init(ops);
6770 mutex_lock(&ftrace_lock);
6772 ret = ftrace_startup(ops, 0);
6774 mutex_unlock(&ftrace_lock);
6778 EXPORT_SYMBOL_GPL(register_ftrace_function);
6781 * unregister_ftrace_function - unregister a function for profiling.
6782 * @ops - ops structure that holds the function to unregister
6784 * Unregister a function that was added to be called by ftrace profiling.
6786 int unregister_ftrace_function(struct ftrace_ops *ops)
6790 mutex_lock(&ftrace_lock);
6791 ret = ftrace_shutdown(ops, 0);
6792 mutex_unlock(&ftrace_lock);
6796 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
6799 ftrace_enable_sysctl(struct ctl_table *table, int write,
6800 void __user *buffer, size_t *lenp,
6805 mutex_lock(&ftrace_lock);
6807 if (unlikely(ftrace_disabled))
6810 ret = proc_dointvec(table, write, buffer, lenp, ppos);
6812 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
6815 last_ftrace_enabled = !!ftrace_enabled;
6817 if (ftrace_enabled) {
6819 /* we are starting ftrace again */
6820 if (rcu_dereference_protected(ftrace_ops_list,
6821 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
6822 update_ftrace_function();
6824 ftrace_startup_sysctl();
6827 /* stopping ftrace calls (just send to ftrace_stub) */
6828 ftrace_trace_function = ftrace_stub;
6830 ftrace_shutdown_sysctl();
6834 mutex_unlock(&ftrace_lock);
6838 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6840 static struct ftrace_ops graph_ops = {
6841 .func = ftrace_stub,
6842 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6843 FTRACE_OPS_FL_INITIALIZED |
6846 #ifdef FTRACE_GRAPH_TRAMP_ADDR
6847 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
6848 /* trampoline_size is only needed for dynamically allocated tramps */
6850 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6853 void ftrace_graph_sleep_time_control(bool enable)
6855 fgraph_sleep_time = enable;
6858 void ftrace_graph_graph_time_control(bool enable)
6860 fgraph_graph_time = enable;
6863 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6868 /* The callbacks that hook a function */
6869 trace_func_graph_ret_t ftrace_graph_return =
6870 (trace_func_graph_ret_t)ftrace_stub;
6871 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
6872 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
6874 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6875 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6879 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6880 struct task_struct *g, *t;
6882 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6884 kmalloc_array(FTRACE_RETFUNC_DEPTH,
6885 sizeof(struct ftrace_ret_stack),
6887 if (!ret_stack_list[i]) {
6895 read_lock(&tasklist_lock);
6896 do_each_thread(g, t) {
6902 if (t->ret_stack == NULL) {
6903 atomic_set(&t->trace_overrun, 0);
6904 t->curr_ret_stack = -1;
6905 t->curr_ret_depth = -1;
6906 /* Make sure the tasks see the -1 first: */
6908 t->ret_stack = ret_stack_list[start++];
6910 } while_each_thread(g, t);
6913 read_unlock(&tasklist_lock);
6915 for (i = start; i < end; i++)
6916 kfree(ret_stack_list[i]);
6921 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
6922 struct task_struct *prev, struct task_struct *next)
6924 unsigned long long timestamp;
6928 * Does the user want to count the time a function was asleep.
6929 * If so, do not update the time stamps.
6931 if (fgraph_sleep_time)
6934 timestamp = trace_clock_local();
6936 prev->ftrace_timestamp = timestamp;
6938 /* only process tasks that we timestamped */
6939 if (!next->ftrace_timestamp)
6943 * Update all the counters in next to make up for the
6944 * time next was sleeping.
6946 timestamp -= next->ftrace_timestamp;
6948 for (index = next->curr_ret_stack; index >= 0; index--)
6949 next->ret_stack[index].calltime += timestamp;
6952 /* Allocate a return stack for each task */
6953 static int start_graph_tracing(void)
6955 struct ftrace_ret_stack **ret_stack_list;
6958 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
6959 sizeof(struct ftrace_ret_stack *),
6962 if (!ret_stack_list)
6965 /* The cpu_boot init_task->ret_stack will never be freed */
6966 for_each_online_cpu(cpu) {
6967 if (!idle_task(cpu)->ret_stack)
6968 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
6972 ret = alloc_retstack_tasklist(ret_stack_list);
6973 } while (ret == -EAGAIN);
6976 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6978 pr_info("ftrace_graph: Couldn't activate tracepoint"
6979 " probe to kernel_sched_switch\n");
6982 kfree(ret_stack_list);
6987 * Hibernation protection.
6988 * The state of the current task is too much unstable during
6989 * suspend/restore to disk. We want to protect against that.
6992 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6996 case PM_HIBERNATION_PREPARE:
6997 pause_graph_tracing();
7000 case PM_POST_HIBERNATION:
7001 unpause_graph_tracing();
7007 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
7009 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
7011 return __ftrace_graph_entry(trace);
7015 * The function graph tracer should only trace the functions defined
7016 * by set_ftrace_filter and set_ftrace_notrace. If another function
7017 * tracer ops is registered, the graph tracer requires testing the
7018 * function against the global ops, and not just trace any function
7019 * that any ftrace_ops registered.
7021 static void update_function_graph_func(void)
7023 struct ftrace_ops *op;
7024 bool do_test = false;
7027 * The graph and global ops share the same set of functions
7028 * to test. If any other ops is on the list, then
7029 * the graph tracing needs to test if its the function
7032 do_for_each_ftrace_op(op, ftrace_ops_list) {
7033 if (op != &global_ops && op != &graph_ops &&
7034 op != &ftrace_list_end) {
7036 /* in double loop, break out with goto */
7039 } while_for_each_ftrace_op(op);
7042 ftrace_graph_entry = ftrace_graph_entry_test;
7044 ftrace_graph_entry = __ftrace_graph_entry;
7047 static struct notifier_block ftrace_suspend_notifier = {
7048 .notifier_call = ftrace_suspend_notifier_call,
7051 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
7052 trace_func_graph_ent_t entryfunc)
7056 mutex_lock(&ftrace_lock);
7058 /* we currently allow only one tracer registered at a time */
7059 if (ftrace_graph_active) {
7064 register_pm_notifier(&ftrace_suspend_notifier);
7066 ftrace_graph_active++;
7067 ret = start_graph_tracing();
7069 ftrace_graph_active--;
7073 ftrace_graph_return = retfunc;
7076 * Update the indirect function to the entryfunc, and the
7077 * function that gets called to the entry_test first. Then
7078 * call the update fgraph entry function to determine if
7079 * the entryfunc should be called directly or not.
7081 __ftrace_graph_entry = entryfunc;
7082 ftrace_graph_entry = ftrace_graph_entry_test;
7083 update_function_graph_func();
7085 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
7087 mutex_unlock(&ftrace_lock);
7091 void unregister_ftrace_graph(void)
7093 mutex_lock(&ftrace_lock);
7095 if (unlikely(!ftrace_graph_active))
7098 ftrace_graph_active--;
7099 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
7100 ftrace_graph_entry = ftrace_graph_entry_stub;
7101 __ftrace_graph_entry = ftrace_graph_entry_stub;
7102 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
7103 unregister_pm_notifier(&ftrace_suspend_notifier);
7104 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
7107 mutex_unlock(&ftrace_lock);
7110 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
7113 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
7115 atomic_set(&t->trace_overrun, 0);
7116 t->ftrace_timestamp = 0;
7117 /* make curr_ret_stack visible before we add the ret_stack */
7119 t->ret_stack = ret_stack;
7123 * Allocate a return stack for the idle task. May be the first
7124 * time through, or it may be done by CPU hotplug online.
7126 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
7128 t->curr_ret_stack = -1;
7129 t->curr_ret_depth = -1;
7131 * The idle task has no parent, it either has its own
7132 * stack or no stack at all.
7135 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
7137 if (ftrace_graph_active) {
7138 struct ftrace_ret_stack *ret_stack;
7140 ret_stack = per_cpu(idle_ret_stack, cpu);
7143 kmalloc_array(FTRACE_RETFUNC_DEPTH,
7144 sizeof(struct ftrace_ret_stack),
7148 per_cpu(idle_ret_stack, cpu) = ret_stack;
7150 graph_init_task(t, ret_stack);
7154 /* Allocate a return stack for newly created task */
7155 void ftrace_graph_init_task(struct task_struct *t)
7157 /* Make sure we do not use the parent ret_stack */
7158 t->ret_stack = NULL;
7159 t->curr_ret_stack = -1;
7160 t->curr_ret_depth = -1;
7162 if (ftrace_graph_active) {
7163 struct ftrace_ret_stack *ret_stack;
7165 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
7166 sizeof(struct ftrace_ret_stack),
7170 graph_init_task(t, ret_stack);
7174 void ftrace_graph_exit_task(struct task_struct *t)
7176 struct ftrace_ret_stack *ret_stack = t->ret_stack;
7178 t->ret_stack = NULL;
7179 /* NULL must become visible to IRQs before we free it: */