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 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1335 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1336 ftrace_mod->enable = enable;
1338 if (!ftrace_mod->func || !ftrace_mod->module)
1341 list_add(&ftrace_mod->list, mod_head);
1346 free_ftrace_mod(ftrace_mod);
1351 static struct ftrace_hash *
1352 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1354 struct ftrace_func_entry *entry;
1355 struct ftrace_hash *new_hash;
1360 new_hash = alloc_ftrace_hash(size_bits);
1365 new_hash->flags = hash->flags;
1368 if (ftrace_hash_empty(hash))
1371 size = 1 << hash->size_bits;
1372 for (i = 0; i < size; i++) {
1373 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1374 ret = add_hash_entry(new_hash, entry->ip);
1380 FTRACE_WARN_ON(new_hash->count != hash->count);
1385 free_ftrace_hash(new_hash);
1390 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1392 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1394 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1395 struct ftrace_hash *new_hash);
1397 static struct ftrace_hash *
1398 __ftrace_hash_move(struct ftrace_hash *src)
1400 struct ftrace_func_entry *entry;
1401 struct hlist_node *tn;
1402 struct hlist_head *hhd;
1403 struct ftrace_hash *new_hash;
1404 int size = src->count;
1409 * If the new source is empty, just return the empty_hash.
1411 if (ftrace_hash_empty(src))
1415 * Make the hash size about 1/2 the # found
1417 for (size /= 2; size; size >>= 1)
1420 /* Don't allocate too much */
1421 if (bits > FTRACE_HASH_MAX_BITS)
1422 bits = FTRACE_HASH_MAX_BITS;
1424 new_hash = alloc_ftrace_hash(bits);
1428 new_hash->flags = src->flags;
1430 size = 1 << src->size_bits;
1431 for (i = 0; i < size; i++) {
1432 hhd = &src->buckets[i];
1433 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1434 remove_hash_entry(src, entry);
1435 __add_hash_entry(new_hash, entry);
1443 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1444 struct ftrace_hash **dst, struct ftrace_hash *src)
1446 struct ftrace_hash *new_hash;
1449 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1450 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1453 new_hash = __ftrace_hash_move(src);
1457 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1459 /* IPMODIFY should be updated only when filter_hash updating */
1460 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1462 free_ftrace_hash(new_hash);
1468 * Remove the current set, update the hash and add
1471 ftrace_hash_rec_disable_modify(ops, enable);
1473 rcu_assign_pointer(*dst, new_hash);
1475 ftrace_hash_rec_enable_modify(ops, enable);
1480 static bool hash_contains_ip(unsigned long ip,
1481 struct ftrace_ops_hash *hash)
1484 * The function record is a match if it exists in the filter
1485 * hash and not in the notrace hash. Note, an emty hash is
1486 * considered a match for the filter hash, but an empty
1487 * notrace hash is considered not in the notrace hash.
1489 return (ftrace_hash_empty(hash->filter_hash) ||
1490 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1491 (ftrace_hash_empty(hash->notrace_hash) ||
1492 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1496 * Test the hashes for this ops to see if we want to call
1497 * the ops->func or not.
1499 * It's a match if the ip is in the ops->filter_hash or
1500 * the filter_hash does not exist or is empty,
1502 * the ip is not in the ops->notrace_hash.
1504 * This needs to be called with preemption disabled as
1505 * the hashes are freed with call_rcu_sched().
1508 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1510 struct ftrace_ops_hash hash;
1513 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1515 * There's a small race when adding ops that the ftrace handler
1516 * that wants regs, may be called without them. We can not
1517 * allow that handler to be called if regs is NULL.
1519 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1523 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1524 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1526 if (hash_contains_ip(ip, &hash))
1535 * This is a double for. Do not use 'break' to break out of the loop,
1536 * you must use a goto.
1538 #define do_for_each_ftrace_rec(pg, rec) \
1539 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1541 for (_____i = 0; _____i < pg->index; _____i++) { \
1542 rec = &pg->records[_____i];
1544 #define while_for_each_ftrace_rec() \
1549 static int ftrace_cmp_recs(const void *a, const void *b)
1551 const struct dyn_ftrace *key = a;
1552 const struct dyn_ftrace *rec = b;
1554 if (key->flags < rec->ip)
1556 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1562 * ftrace_location_range - return the first address of a traced location
1563 * if it touches the given ip range
1564 * @start: start of range to search.
1565 * @end: end of range to search (inclusive). @end points to the last byte
1568 * Returns rec->ip if the related ftrace location is a least partly within
1569 * the given address range. That is, the first address of the instruction
1570 * that is either a NOP or call to the function tracer. It checks the ftrace
1571 * internal tables to determine if the address belongs or not.
1573 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1575 struct ftrace_page *pg;
1576 struct dyn_ftrace *rec;
1577 struct dyn_ftrace key;
1580 key.flags = end; /* overload flags, as it is unsigned long */
1582 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1583 if (end < pg->records[0].ip ||
1584 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1586 rec = bsearch(&key, pg->records, pg->index,
1587 sizeof(struct dyn_ftrace),
1597 * ftrace_location - return true if the ip giving is a traced location
1598 * @ip: the instruction pointer to check
1600 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1601 * That is, the instruction that is either a NOP or call to
1602 * the function tracer. It checks the ftrace internal tables to
1603 * determine if the address belongs or not.
1605 unsigned long ftrace_location(unsigned long ip)
1607 return ftrace_location_range(ip, ip);
1611 * ftrace_text_reserved - return true if range contains an ftrace location
1612 * @start: start of range to search
1613 * @end: end of range to search (inclusive). @end points to the last byte to check.
1615 * Returns 1 if @start and @end contains a ftrace location.
1616 * That is, the instruction that is either a NOP or call to
1617 * the function tracer. It checks the ftrace internal tables to
1618 * determine if the address belongs or not.
1620 int ftrace_text_reserved(const void *start, const void *end)
1624 ret = ftrace_location_range((unsigned long)start,
1625 (unsigned long)end);
1630 /* Test if ops registered to this rec needs regs */
1631 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1633 struct ftrace_ops *ops;
1634 bool keep_regs = false;
1636 for (ops = ftrace_ops_list;
1637 ops != &ftrace_list_end; ops = ops->next) {
1638 /* pass rec in as regs to have non-NULL val */
1639 if (ftrace_ops_test(ops, rec->ip, rec)) {
1640 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1650 static struct ftrace_ops *
1651 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1652 static struct ftrace_ops *
1653 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1654 static struct ftrace_ops *
1655 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1657 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1661 struct ftrace_hash *hash;
1662 struct ftrace_hash *other_hash;
1663 struct ftrace_page *pg;
1664 struct dyn_ftrace *rec;
1665 bool update = false;
1669 /* Only update if the ops has been registered */
1670 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1674 * In the filter_hash case:
1675 * If the count is zero, we update all records.
1676 * Otherwise we just update the items in the hash.
1678 * In the notrace_hash case:
1679 * We enable the update in the hash.
1680 * As disabling notrace means enabling the tracing,
1681 * and enabling notrace means disabling, the inc variable
1685 hash = ops->func_hash->filter_hash;
1686 other_hash = ops->func_hash->notrace_hash;
1687 if (ftrace_hash_empty(hash))
1691 hash = ops->func_hash->notrace_hash;
1692 other_hash = ops->func_hash->filter_hash;
1694 * If the notrace hash has no items,
1695 * then there's nothing to do.
1697 if (ftrace_hash_empty(hash))
1701 do_for_each_ftrace_rec(pg, rec) {
1702 int in_other_hash = 0;
1706 if (rec->flags & FTRACE_FL_DISABLED)
1711 * Only the filter_hash affects all records.
1712 * Update if the record is not in the notrace hash.
1714 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1717 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1718 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1721 * If filter_hash is set, we want to match all functions
1722 * that are in the hash but not in the other hash.
1724 * If filter_hash is not set, then we are decrementing.
1725 * That means we match anything that is in the hash
1726 * and also in the other_hash. That is, we need to turn
1727 * off functions in the other hash because they are disabled
1730 if (filter_hash && in_hash && !in_other_hash)
1732 else if (!filter_hash && in_hash &&
1733 (in_other_hash || ftrace_hash_empty(other_hash)))
1741 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1745 * If there's only a single callback registered to a
1746 * function, and the ops has a trampoline registered
1747 * for it, then we can call it directly.
1749 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1750 rec->flags |= FTRACE_FL_TRAMP;
1753 * If we are adding another function callback
1754 * to this function, and the previous had a
1755 * custom trampoline in use, then we need to go
1756 * back to the default trampoline.
1758 rec->flags &= ~FTRACE_FL_TRAMP;
1761 * If any ops wants regs saved for this function
1762 * then all ops will get saved regs.
1764 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1765 rec->flags |= FTRACE_FL_REGS;
1767 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1772 * If the rec had REGS enabled and the ops that is
1773 * being removed had REGS set, then see if there is
1774 * still any ops for this record that wants regs.
1775 * If not, we can stop recording them.
1777 if (ftrace_rec_count(rec) > 0 &&
1778 rec->flags & FTRACE_FL_REGS &&
1779 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1780 if (!test_rec_ops_needs_regs(rec))
1781 rec->flags &= ~FTRACE_FL_REGS;
1785 * The TRAMP needs to be set only if rec count
1786 * is decremented to one, and the ops that is
1787 * left has a trampoline. As TRAMP can only be
1788 * enabled if there is only a single ops attached
1791 if (ftrace_rec_count(rec) == 1 &&
1792 ftrace_find_tramp_ops_any_other(rec, ops))
1793 rec->flags |= FTRACE_FL_TRAMP;
1795 rec->flags &= ~FTRACE_FL_TRAMP;
1798 * flags will be cleared in ftrace_check_record()
1799 * if rec count is zero.
1804 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1805 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1807 /* Shortcut, if we handled all records, we are done. */
1808 if (!all && count == hash->count)
1810 } while_for_each_ftrace_rec();
1815 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1818 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1821 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1824 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1827 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1828 int filter_hash, int inc)
1830 struct ftrace_ops *op;
1832 __ftrace_hash_rec_update(ops, filter_hash, inc);
1834 if (ops->func_hash != &global_ops.local_hash)
1838 * If the ops shares the global_ops hash, then we need to update
1839 * all ops that are enabled and use this hash.
1841 do_for_each_ftrace_op(op, ftrace_ops_list) {
1845 if (op->func_hash == &global_ops.local_hash)
1846 __ftrace_hash_rec_update(op, filter_hash, inc);
1847 } while_for_each_ftrace_op(op);
1850 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1853 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1856 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1859 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1863 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1864 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1865 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1866 * Note that old_hash and new_hash has below meanings
1867 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1868 * - If the hash is EMPTY_HASH, it hits nothing
1869 * - Anything else hits the recs which match the hash entries.
1871 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1872 struct ftrace_hash *old_hash,
1873 struct ftrace_hash *new_hash)
1875 struct ftrace_page *pg;
1876 struct dyn_ftrace *rec, *end = NULL;
1879 /* Only update if the ops has been registered */
1880 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1883 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1887 * Since the IPMODIFY is a very address sensitive action, we do not
1888 * allow ftrace_ops to set all functions to new hash.
1890 if (!new_hash || !old_hash)
1893 /* Update rec->flags */
1894 do_for_each_ftrace_rec(pg, rec) {
1896 if (rec->flags & FTRACE_FL_DISABLED)
1899 /* We need to update only differences of filter_hash */
1900 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1901 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1902 if (in_old == in_new)
1906 /* New entries must ensure no others are using it */
1907 if (rec->flags & FTRACE_FL_IPMODIFY)
1909 rec->flags |= FTRACE_FL_IPMODIFY;
1910 } else /* Removed entry */
1911 rec->flags &= ~FTRACE_FL_IPMODIFY;
1912 } while_for_each_ftrace_rec();
1919 /* Roll back what we did above */
1920 do_for_each_ftrace_rec(pg, rec) {
1922 if (rec->flags & FTRACE_FL_DISABLED)
1928 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1929 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1930 if (in_old == in_new)
1934 rec->flags &= ~FTRACE_FL_IPMODIFY;
1936 rec->flags |= FTRACE_FL_IPMODIFY;
1937 } while_for_each_ftrace_rec();
1943 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1945 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1947 if (ftrace_hash_empty(hash))
1950 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1953 /* Disabling always succeeds */
1954 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1956 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1958 if (ftrace_hash_empty(hash))
1961 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1964 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1965 struct ftrace_hash *new_hash)
1967 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1969 if (ftrace_hash_empty(old_hash))
1972 if (ftrace_hash_empty(new_hash))
1975 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1978 static void print_ip_ins(const char *fmt, const unsigned char *p)
1980 char ins[MCOUNT_INSN_SIZE];
1983 if (probe_kernel_read(ins, p, MCOUNT_INSN_SIZE)) {
1984 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
1988 printk(KERN_CONT "%s", fmt);
1990 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1991 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
1994 enum ftrace_bug_type ftrace_bug_type;
1995 const void *ftrace_expected;
1997 static void print_bug_type(void)
1999 switch (ftrace_bug_type) {
2000 case FTRACE_BUG_UNKNOWN:
2002 case FTRACE_BUG_INIT:
2003 pr_info("Initializing ftrace call sites\n");
2005 case FTRACE_BUG_NOP:
2006 pr_info("Setting ftrace call site to NOP\n");
2008 case FTRACE_BUG_CALL:
2009 pr_info("Setting ftrace call site to call ftrace function\n");
2011 case FTRACE_BUG_UPDATE:
2012 pr_info("Updating ftrace call site to call a different ftrace function\n");
2018 * ftrace_bug - report and shutdown function tracer
2019 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2020 * @rec: The record that failed
2022 * The arch code that enables or disables the function tracing
2023 * can call ftrace_bug() when it has detected a problem in
2024 * modifying the code. @failed should be one of either:
2025 * EFAULT - if the problem happens on reading the @ip address
2026 * EINVAL - if what is read at @ip is not what was expected
2027 * EPERM - if the problem happens on writting to the @ip address
2029 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2031 unsigned long ip = rec ? rec->ip : 0;
2035 FTRACE_WARN_ON_ONCE(1);
2036 pr_info("ftrace faulted on modifying ");
2040 FTRACE_WARN_ON_ONCE(1);
2041 pr_info("ftrace failed to modify ");
2043 print_ip_ins(" actual: ", (unsigned char *)ip);
2045 if (ftrace_expected) {
2046 print_ip_ins(" expected: ", ftrace_expected);
2051 FTRACE_WARN_ON_ONCE(1);
2052 pr_info("ftrace faulted on writing ");
2056 FTRACE_WARN_ON_ONCE(1);
2057 pr_info("ftrace faulted on unknown error ");
2062 struct ftrace_ops *ops = NULL;
2064 pr_info("ftrace record flags: %lx\n", rec->flags);
2065 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2066 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2067 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2068 ops = ftrace_find_tramp_ops_any(rec);
2071 pr_cont("\ttramp: %pS (%pS)",
2072 (void *)ops->trampoline,
2074 ops = ftrace_find_tramp_ops_next(rec, ops);
2077 pr_cont("\ttramp: ERROR!");
2080 ip = ftrace_get_addr_curr(rec);
2081 pr_cont("\n expected tramp: %lx\n", ip);
2085 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2087 unsigned long flag = 0UL;
2089 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2091 if (rec->flags & FTRACE_FL_DISABLED)
2092 return FTRACE_UPDATE_IGNORE;
2095 * If we are updating calls:
2097 * If the record has a ref count, then we need to enable it
2098 * because someone is using it.
2100 * Otherwise we make sure its disabled.
2102 * If we are disabling calls, then disable all records that
2105 if (enable && ftrace_rec_count(rec))
2106 flag = FTRACE_FL_ENABLED;
2109 * If enabling and the REGS flag does not match the REGS_EN, or
2110 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2111 * this record. Set flags to fail the compare against ENABLED.
2114 if (!(rec->flags & FTRACE_FL_REGS) !=
2115 !(rec->flags & FTRACE_FL_REGS_EN))
2116 flag |= FTRACE_FL_REGS;
2118 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2119 !(rec->flags & FTRACE_FL_TRAMP_EN))
2120 flag |= FTRACE_FL_TRAMP;
2123 /* If the state of this record hasn't changed, then do nothing */
2124 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2125 return FTRACE_UPDATE_IGNORE;
2128 /* Save off if rec is being enabled (for return value) */
2129 flag ^= rec->flags & FTRACE_FL_ENABLED;
2132 rec->flags |= FTRACE_FL_ENABLED;
2133 if (flag & FTRACE_FL_REGS) {
2134 if (rec->flags & FTRACE_FL_REGS)
2135 rec->flags |= FTRACE_FL_REGS_EN;
2137 rec->flags &= ~FTRACE_FL_REGS_EN;
2139 if (flag & FTRACE_FL_TRAMP) {
2140 if (rec->flags & FTRACE_FL_TRAMP)
2141 rec->flags |= FTRACE_FL_TRAMP_EN;
2143 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2148 * If this record is being updated from a nop, then
2149 * return UPDATE_MAKE_CALL.
2151 * return UPDATE_MODIFY_CALL to tell the caller to convert
2152 * from the save regs, to a non-save regs function or
2153 * vice versa, or from a trampoline call.
2155 if (flag & FTRACE_FL_ENABLED) {
2156 ftrace_bug_type = FTRACE_BUG_CALL;
2157 return FTRACE_UPDATE_MAKE_CALL;
2160 ftrace_bug_type = FTRACE_BUG_UPDATE;
2161 return FTRACE_UPDATE_MODIFY_CALL;
2165 /* If there's no more users, clear all flags */
2166 if (!ftrace_rec_count(rec))
2170 * Just disable the record, but keep the ops TRAMP
2171 * and REGS states. The _EN flags must be disabled though.
2173 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2177 ftrace_bug_type = FTRACE_BUG_NOP;
2178 return FTRACE_UPDATE_MAKE_NOP;
2182 * ftrace_update_record, set a record that now is tracing or not
2183 * @rec: the record to update
2184 * @enable: set to 1 if the record is tracing, zero to force disable
2186 * The records that represent all functions that can be traced need
2187 * to be updated when tracing has been enabled.
2189 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2191 return ftrace_check_record(rec, enable, 1);
2195 * ftrace_test_record, check if the record has been enabled or not
2196 * @rec: the record to test
2197 * @enable: set to 1 to check if enabled, 0 if it is disabled
2199 * The arch code may need to test if a record is already set to
2200 * tracing to determine how to modify the function code that it
2203 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2205 return ftrace_check_record(rec, enable, 0);
2208 static struct ftrace_ops *
2209 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2211 struct ftrace_ops *op;
2212 unsigned long ip = rec->ip;
2214 do_for_each_ftrace_op(op, ftrace_ops_list) {
2216 if (!op->trampoline)
2219 if (hash_contains_ip(ip, op->func_hash))
2221 } while_for_each_ftrace_op(op);
2226 static struct ftrace_ops *
2227 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2229 struct ftrace_ops *op;
2230 unsigned long ip = rec->ip;
2232 do_for_each_ftrace_op(op, ftrace_ops_list) {
2234 if (op == op_exclude || !op->trampoline)
2237 if (hash_contains_ip(ip, op->func_hash))
2239 } while_for_each_ftrace_op(op);
2244 static struct ftrace_ops *
2245 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2246 struct ftrace_ops *op)
2248 unsigned long ip = rec->ip;
2250 while_for_each_ftrace_op(op) {
2252 if (!op->trampoline)
2255 if (hash_contains_ip(ip, op->func_hash))
2262 static struct ftrace_ops *
2263 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2265 struct ftrace_ops *op;
2266 unsigned long ip = rec->ip;
2269 * Need to check removed ops first.
2270 * If they are being removed, and this rec has a tramp,
2271 * and this rec is in the ops list, then it would be the
2272 * one with the tramp.
2275 if (hash_contains_ip(ip, &removed_ops->old_hash))
2280 * Need to find the current trampoline for a rec.
2281 * Now, a trampoline is only attached to a rec if there
2282 * was a single 'ops' attached to it. But this can be called
2283 * when we are adding another op to the rec or removing the
2284 * current one. Thus, if the op is being added, we can
2285 * ignore it because it hasn't attached itself to the rec
2288 * If an ops is being modified (hooking to different functions)
2289 * then we don't care about the new functions that are being
2290 * added, just the old ones (that are probably being removed).
2292 * If we are adding an ops to a function that already is using
2293 * a trampoline, it needs to be removed (trampolines are only
2294 * for single ops connected), then an ops that is not being
2295 * modified also needs to be checked.
2297 do_for_each_ftrace_op(op, ftrace_ops_list) {
2299 if (!op->trampoline)
2303 * If the ops is being added, it hasn't gotten to
2304 * the point to be removed from this tree yet.
2306 if (op->flags & FTRACE_OPS_FL_ADDING)
2311 * If the ops is being modified and is in the old
2312 * hash, then it is probably being removed from this
2315 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2316 hash_contains_ip(ip, &op->old_hash))
2319 * If the ops is not being added or modified, and it's
2320 * in its normal filter hash, then this must be the one
2323 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2324 hash_contains_ip(ip, op->func_hash))
2327 } while_for_each_ftrace_op(op);
2332 static struct ftrace_ops *
2333 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2335 struct ftrace_ops *op;
2336 unsigned long ip = rec->ip;
2338 do_for_each_ftrace_op(op, ftrace_ops_list) {
2339 /* pass rec in as regs to have non-NULL val */
2340 if (hash_contains_ip(ip, op->func_hash))
2342 } while_for_each_ftrace_op(op);
2348 * ftrace_get_addr_new - Get the call address to set to
2349 * @rec: The ftrace record descriptor
2351 * If the record has the FTRACE_FL_REGS set, that means that it
2352 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2353 * is not not set, then it wants to convert to the normal callback.
2355 * Returns the address of the trampoline to set to
2357 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2359 struct ftrace_ops *ops;
2361 /* Trampolines take precedence over regs */
2362 if (rec->flags & FTRACE_FL_TRAMP) {
2363 ops = ftrace_find_tramp_ops_new(rec);
2364 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2365 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2366 (void *)rec->ip, (void *)rec->ip, rec->flags);
2367 /* Ftrace is shutting down, return anything */
2368 return (unsigned long)FTRACE_ADDR;
2370 return ops->trampoline;
2373 if (rec->flags & FTRACE_FL_REGS)
2374 return (unsigned long)FTRACE_REGS_ADDR;
2376 return (unsigned long)FTRACE_ADDR;
2380 * ftrace_get_addr_curr - Get the call address that is already there
2381 * @rec: The ftrace record descriptor
2383 * The FTRACE_FL_REGS_EN is set when the record already points to
2384 * a function that saves all the regs. Basically the '_EN' version
2385 * represents the current state of the function.
2387 * Returns the address of the trampoline that is currently being called
2389 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2391 struct ftrace_ops *ops;
2393 /* Trampolines take precedence over regs */
2394 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2395 ops = ftrace_find_tramp_ops_curr(rec);
2396 if (FTRACE_WARN_ON(!ops)) {
2397 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2398 (void *)rec->ip, (void *)rec->ip);
2399 /* Ftrace is shutting down, return anything */
2400 return (unsigned long)FTRACE_ADDR;
2402 return ops->trampoline;
2405 if (rec->flags & FTRACE_FL_REGS_EN)
2406 return (unsigned long)FTRACE_REGS_ADDR;
2408 return (unsigned long)FTRACE_ADDR;
2412 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2414 unsigned long ftrace_old_addr;
2415 unsigned long ftrace_addr;
2418 ftrace_addr = ftrace_get_addr_new(rec);
2420 /* This needs to be done before we call ftrace_update_record */
2421 ftrace_old_addr = ftrace_get_addr_curr(rec);
2423 ret = ftrace_update_record(rec, enable);
2425 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2428 case FTRACE_UPDATE_IGNORE:
2431 case FTRACE_UPDATE_MAKE_CALL:
2432 ftrace_bug_type = FTRACE_BUG_CALL;
2433 return ftrace_make_call(rec, ftrace_addr);
2435 case FTRACE_UPDATE_MAKE_NOP:
2436 ftrace_bug_type = FTRACE_BUG_NOP;
2437 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2439 case FTRACE_UPDATE_MODIFY_CALL:
2440 ftrace_bug_type = FTRACE_BUG_UPDATE;
2441 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2444 return -1; /* unknow ftrace bug */
2447 void __weak ftrace_replace_code(int enable)
2449 struct dyn_ftrace *rec;
2450 struct ftrace_page *pg;
2453 if (unlikely(ftrace_disabled))
2456 do_for_each_ftrace_rec(pg, rec) {
2458 if (rec->flags & FTRACE_FL_DISABLED)
2461 failed = __ftrace_replace_code(rec, enable);
2463 ftrace_bug(failed, rec);
2464 /* Stop processing */
2467 } while_for_each_ftrace_rec();
2470 struct ftrace_rec_iter {
2471 struct ftrace_page *pg;
2476 * ftrace_rec_iter_start, start up iterating over traced functions
2478 * Returns an iterator handle that is used to iterate over all
2479 * the records that represent address locations where functions
2482 * May return NULL if no records are available.
2484 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2487 * We only use a single iterator.
2488 * Protected by the ftrace_lock mutex.
2490 static struct ftrace_rec_iter ftrace_rec_iter;
2491 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2493 iter->pg = ftrace_pages_start;
2496 /* Could have empty pages */
2497 while (iter->pg && !iter->pg->index)
2498 iter->pg = iter->pg->next;
2507 * ftrace_rec_iter_next, get the next record to process.
2508 * @iter: The handle to the iterator.
2510 * Returns the next iterator after the given iterator @iter.
2512 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2516 if (iter->index >= iter->pg->index) {
2517 iter->pg = iter->pg->next;
2520 /* Could have empty pages */
2521 while (iter->pg && !iter->pg->index)
2522 iter->pg = iter->pg->next;
2532 * ftrace_rec_iter_record, get the record at the iterator location
2533 * @iter: The current iterator location
2535 * Returns the record that the current @iter is at.
2537 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2539 return &iter->pg->records[iter->index];
2543 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2547 if (unlikely(ftrace_disabled))
2550 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2552 ftrace_bug_type = FTRACE_BUG_INIT;
2553 ftrace_bug(ret, rec);
2560 * archs can override this function if they must do something
2561 * before the modifying code is performed.
2563 int __weak ftrace_arch_code_modify_prepare(void)
2569 * archs can override this function if they must do something
2570 * after the modifying code is performed.
2572 int __weak ftrace_arch_code_modify_post_process(void)
2577 void ftrace_modify_all_code(int command)
2579 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2583 * If the ftrace_caller calls a ftrace_ops func directly,
2584 * we need to make sure that it only traces functions it
2585 * expects to trace. When doing the switch of functions,
2586 * we need to update to the ftrace_ops_list_func first
2587 * before the transition between old and new calls are set,
2588 * as the ftrace_ops_list_func will check the ops hashes
2589 * to make sure the ops are having the right functions
2593 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2594 if (FTRACE_WARN_ON(err))
2598 if (command & FTRACE_UPDATE_CALLS)
2599 ftrace_replace_code(1);
2600 else if (command & FTRACE_DISABLE_CALLS)
2601 ftrace_replace_code(0);
2603 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2604 function_trace_op = set_function_trace_op;
2606 /* If irqs are disabled, we are in stop machine */
2607 if (!irqs_disabled())
2608 smp_call_function(ftrace_sync_ipi, NULL, 1);
2609 err = ftrace_update_ftrace_func(ftrace_trace_function);
2610 if (FTRACE_WARN_ON(err))
2614 if (command & FTRACE_START_FUNC_RET)
2615 err = ftrace_enable_ftrace_graph_caller();
2616 else if (command & FTRACE_STOP_FUNC_RET)
2617 err = ftrace_disable_ftrace_graph_caller();
2618 FTRACE_WARN_ON(err);
2621 static int __ftrace_modify_code(void *data)
2623 int *command = data;
2625 ftrace_modify_all_code(*command);
2631 * ftrace_run_stop_machine, go back to the stop machine method
2632 * @command: The command to tell ftrace what to do
2634 * If an arch needs to fall back to the stop machine method, the
2635 * it can call this function.
2637 void ftrace_run_stop_machine(int command)
2639 stop_machine(__ftrace_modify_code, &command, NULL);
2643 * arch_ftrace_update_code, modify the code to trace or not trace
2644 * @command: The command that needs to be done
2646 * Archs can override this function if it does not need to
2647 * run stop_machine() to modify code.
2649 void __weak arch_ftrace_update_code(int command)
2651 ftrace_run_stop_machine(command);
2654 static void ftrace_run_update_code(int command)
2658 ret = ftrace_arch_code_modify_prepare();
2659 FTRACE_WARN_ON(ret);
2664 * By default we use stop_machine() to modify the code.
2665 * But archs can do what ever they want as long as it
2666 * is safe. The stop_machine() is the safest, but also
2667 * produces the most overhead.
2669 arch_ftrace_update_code(command);
2671 ret = ftrace_arch_code_modify_post_process();
2672 FTRACE_WARN_ON(ret);
2675 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2676 struct ftrace_ops_hash *old_hash)
2678 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2679 ops->old_hash.filter_hash = old_hash->filter_hash;
2680 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2681 ftrace_run_update_code(command);
2682 ops->old_hash.filter_hash = NULL;
2683 ops->old_hash.notrace_hash = NULL;
2684 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2687 static ftrace_func_t saved_ftrace_func;
2688 static int ftrace_start_up;
2690 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2694 static void ftrace_startup_enable(int command)
2696 if (saved_ftrace_func != ftrace_trace_function) {
2697 saved_ftrace_func = ftrace_trace_function;
2698 command |= FTRACE_UPDATE_TRACE_FUNC;
2701 if (!command || !ftrace_enabled)
2704 ftrace_run_update_code(command);
2707 static void ftrace_startup_all(int command)
2709 update_all_ops = true;
2710 ftrace_startup_enable(command);
2711 update_all_ops = false;
2714 static int ftrace_startup(struct ftrace_ops *ops, int command)
2718 if (unlikely(ftrace_disabled))
2721 ret = __register_ftrace_function(ops);
2728 * Note that ftrace probes uses this to start up
2729 * and modify functions it will probe. But we still
2730 * set the ADDING flag for modification, as probes
2731 * do not have trampolines. If they add them in the
2732 * future, then the probes will need to distinguish
2733 * between adding and updating probes.
2735 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2737 ret = ftrace_hash_ipmodify_enable(ops);
2739 /* Rollback registration process */
2740 __unregister_ftrace_function(ops);
2742 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2746 if (ftrace_hash_rec_enable(ops, 1))
2747 command |= FTRACE_UPDATE_CALLS;
2749 ftrace_startup_enable(command);
2751 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2756 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2760 if (unlikely(ftrace_disabled))
2763 ret = __unregister_ftrace_function(ops);
2769 * Just warn in case of unbalance, no need to kill ftrace, it's not
2770 * critical but the ftrace_call callers may be never nopped again after
2771 * further ftrace uses.
2773 WARN_ON_ONCE(ftrace_start_up < 0);
2775 /* Disabling ipmodify never fails */
2776 ftrace_hash_ipmodify_disable(ops);
2778 if (ftrace_hash_rec_disable(ops, 1))
2779 command |= FTRACE_UPDATE_CALLS;
2781 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2783 if (saved_ftrace_func != ftrace_trace_function) {
2784 saved_ftrace_func = ftrace_trace_function;
2785 command |= FTRACE_UPDATE_TRACE_FUNC;
2788 if (!command || !ftrace_enabled) {
2790 * If these are dynamic or per_cpu ops, they still
2791 * need their data freed. Since, function tracing is
2792 * not currently active, we can just free them
2793 * without synchronizing all CPUs.
2795 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2802 * If the ops uses a trampoline, then it needs to be
2803 * tested first on update.
2805 ops->flags |= FTRACE_OPS_FL_REMOVING;
2808 /* The trampoline logic checks the old hashes */
2809 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2810 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2812 ftrace_run_update_code(command);
2815 * If there's no more ops registered with ftrace, run a
2816 * sanity check to make sure all rec flags are cleared.
2818 if (rcu_dereference_protected(ftrace_ops_list,
2819 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2820 struct ftrace_page *pg;
2821 struct dyn_ftrace *rec;
2823 do_for_each_ftrace_rec(pg, rec) {
2824 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2825 pr_warn(" %pS flags:%lx\n",
2826 (void *)rec->ip, rec->flags);
2827 } while_for_each_ftrace_rec();
2830 ops->old_hash.filter_hash = NULL;
2831 ops->old_hash.notrace_hash = NULL;
2834 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2837 * Dynamic ops may be freed, we must make sure that all
2838 * callers are done before leaving this function.
2839 * The same goes for freeing the per_cpu data of the per_cpu
2842 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
2844 * We need to do a hard force of sched synchronization.
2845 * This is because we use preempt_disable() to do RCU, but
2846 * the function tracers can be called where RCU is not watching
2847 * (like before user_exit()). We can not rely on the RCU
2848 * infrastructure to do the synchronization, thus we must do it
2851 schedule_on_each_cpu(ftrace_sync);
2854 * When the kernel is preeptive, tasks can be preempted
2855 * while on a ftrace trampoline. Just scheduling a task on
2856 * a CPU is not good enough to flush them. Calling
2857 * synchornize_rcu_tasks() will wait for those tasks to
2858 * execute and either schedule voluntarily or enter user space.
2860 if (IS_ENABLED(CONFIG_PREEMPT))
2861 synchronize_rcu_tasks();
2864 arch_ftrace_trampoline_free(ops);
2870 static void ftrace_startup_sysctl(void)
2874 if (unlikely(ftrace_disabled))
2877 /* Force update next time */
2878 saved_ftrace_func = NULL;
2879 /* ftrace_start_up is true if we want ftrace running */
2880 if (ftrace_start_up) {
2881 command = FTRACE_UPDATE_CALLS;
2882 if (ftrace_graph_active)
2883 command |= FTRACE_START_FUNC_RET;
2884 ftrace_startup_enable(command);
2888 static void ftrace_shutdown_sysctl(void)
2892 if (unlikely(ftrace_disabled))
2895 /* ftrace_start_up is true if ftrace is running */
2896 if (ftrace_start_up) {
2897 command = FTRACE_DISABLE_CALLS;
2898 if (ftrace_graph_active)
2899 command |= FTRACE_STOP_FUNC_RET;
2900 ftrace_run_update_code(command);
2904 static u64 ftrace_update_time;
2905 unsigned long ftrace_update_tot_cnt;
2907 static inline int ops_traces_mod(struct ftrace_ops *ops)
2910 * Filter_hash being empty will default to trace module.
2911 * But notrace hash requires a test of individual module functions.
2913 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2914 ftrace_hash_empty(ops->func_hash->notrace_hash);
2918 * Check if the current ops references the record.
2920 * If the ops traces all functions, then it was already accounted for.
2921 * If the ops does not trace the current record function, skip it.
2922 * If the ops ignores the function via notrace filter, skip it.
2925 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2927 /* If ops isn't enabled, ignore it */
2928 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2931 /* If ops traces all then it includes this function */
2932 if (ops_traces_mod(ops))
2935 /* The function must be in the filter */
2936 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2937 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2940 /* If in notrace hash, we ignore it too */
2941 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2947 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2949 struct ftrace_page *pg;
2950 struct dyn_ftrace *p;
2952 unsigned long update_cnt = 0;
2953 unsigned long rec_flags = 0;
2956 start = ftrace_now(raw_smp_processor_id());
2959 * When a module is loaded, this function is called to convert
2960 * the calls to mcount in its text to nops, and also to create
2961 * an entry in the ftrace data. Now, if ftrace is activated
2962 * after this call, but before the module sets its text to
2963 * read-only, the modification of enabling ftrace can fail if
2964 * the read-only is done while ftrace is converting the calls.
2965 * To prevent this, the module's records are set as disabled
2966 * and will be enabled after the call to set the module's text
2970 rec_flags |= FTRACE_FL_DISABLED;
2972 for (pg = new_pgs; pg; pg = pg->next) {
2974 for (i = 0; i < pg->index; i++) {
2976 /* If something went wrong, bail without enabling anything */
2977 if (unlikely(ftrace_disabled))
2980 p = &pg->records[i];
2981 p->flags = rec_flags;
2984 * Do the initial record conversion from mcount jump
2985 * to the NOP instructions.
2987 if (!__is_defined(CC_USING_NOP_MCOUNT) &&
2988 !ftrace_code_disable(mod, p))
2995 stop = ftrace_now(raw_smp_processor_id());
2996 ftrace_update_time = stop - start;
2997 ftrace_update_tot_cnt += update_cnt;
3002 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3007 if (WARN_ON(!count))
3010 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3013 * We want to fill as much as possible. No more than a page
3016 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3020 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3023 /* if we can't allocate this size, try something smaller */
3030 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3039 static struct ftrace_page *
3040 ftrace_allocate_pages(unsigned long num_to_init)
3042 struct ftrace_page *start_pg;
3043 struct ftrace_page *pg;
3050 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3055 * Try to allocate as much as possible in one continues
3056 * location that fills in all of the space. We want to
3057 * waste as little space as possible.
3060 cnt = ftrace_allocate_records(pg, num_to_init);
3068 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3080 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3081 free_pages((unsigned long)pg->records, order);
3082 start_pg = pg->next;
3086 pr_info("ftrace: FAILED to allocate memory for functions\n");
3090 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3092 struct ftrace_iterator {
3096 struct ftrace_page *pg;
3097 struct dyn_ftrace *func;
3098 struct ftrace_func_probe *probe;
3099 struct ftrace_func_entry *probe_entry;
3100 struct trace_parser parser;
3101 struct ftrace_hash *hash;
3102 struct ftrace_ops *ops;
3103 struct trace_array *tr;
3104 struct list_head *mod_list;
3111 t_probe_next(struct seq_file *m, loff_t *pos)
3113 struct ftrace_iterator *iter = m->private;
3114 struct trace_array *tr = iter->ops->private;
3115 struct list_head *func_probes;
3116 struct ftrace_hash *hash;
3117 struct list_head *next;
3118 struct hlist_node *hnd = NULL;
3119 struct hlist_head *hhd;
3128 func_probes = &tr->func_probes;
3129 if (list_empty(func_probes))
3133 next = func_probes->next;
3134 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3137 if (iter->probe_entry)
3138 hnd = &iter->probe_entry->hlist;
3140 hash = iter->probe->ops.func_hash->filter_hash;
3143 * A probe being registered may temporarily have an empty hash
3144 * and it's at the end of the func_probes list.
3146 if (!hash || hash == EMPTY_HASH)
3149 size = 1 << hash->size_bits;
3152 if (iter->pidx >= size) {
3153 if (iter->probe->list.next == func_probes)
3155 next = iter->probe->list.next;
3156 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3157 hash = iter->probe->ops.func_hash->filter_hash;
3158 size = 1 << hash->size_bits;
3162 hhd = &hash->buckets[iter->pidx];
3164 if (hlist_empty(hhd)) {
3180 if (WARN_ON_ONCE(!hnd))
3183 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3188 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3190 struct ftrace_iterator *iter = m->private;
3194 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3197 if (iter->mod_pos > *pos)
3201 iter->probe_entry = NULL;
3203 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3204 p = t_probe_next(m, &l);
3211 /* Only set this if we have an item */
3212 iter->flags |= FTRACE_ITER_PROBE;
3218 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3220 struct ftrace_func_entry *probe_entry;
3221 struct ftrace_probe_ops *probe_ops;
3222 struct ftrace_func_probe *probe;
3224 probe = iter->probe;
3225 probe_entry = iter->probe_entry;
3227 if (WARN_ON_ONCE(!probe || !probe_entry))
3230 probe_ops = probe->probe_ops;
3232 if (probe_ops->print)
3233 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3235 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3236 (void *)probe_ops->func);
3242 t_mod_next(struct seq_file *m, loff_t *pos)
3244 struct ftrace_iterator *iter = m->private;
3245 struct trace_array *tr = iter->tr;
3250 iter->mod_list = iter->mod_list->next;
3252 if (iter->mod_list == &tr->mod_trace ||
3253 iter->mod_list == &tr->mod_notrace) {
3254 iter->flags &= ~FTRACE_ITER_MOD;
3258 iter->mod_pos = *pos;
3263 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3265 struct ftrace_iterator *iter = m->private;
3269 if (iter->func_pos > *pos)
3272 iter->mod_pos = iter->func_pos;
3274 /* probes are only available if tr is set */
3278 for (l = 0; l <= (*pos - iter->func_pos); ) {
3279 p = t_mod_next(m, &l);
3284 iter->flags &= ~FTRACE_ITER_MOD;
3285 return t_probe_start(m, pos);
3288 /* Only set this if we have an item */
3289 iter->flags |= FTRACE_ITER_MOD;
3295 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3297 struct ftrace_mod_load *ftrace_mod;
3298 struct trace_array *tr = iter->tr;
3300 if (WARN_ON_ONCE(!iter->mod_list) ||
3301 iter->mod_list == &tr->mod_trace ||
3302 iter->mod_list == &tr->mod_notrace)
3305 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3307 if (ftrace_mod->func)
3308 seq_printf(m, "%s", ftrace_mod->func);
3312 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3318 t_func_next(struct seq_file *m, loff_t *pos)
3320 struct ftrace_iterator *iter = m->private;
3321 struct dyn_ftrace *rec = NULL;
3326 if (iter->idx >= iter->pg->index) {
3327 if (iter->pg->next) {
3328 iter->pg = iter->pg->next;
3333 rec = &iter->pg->records[iter->idx++];
3334 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3335 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3337 ((iter->flags & FTRACE_ITER_ENABLED) &&
3338 !(rec->flags & FTRACE_FL_ENABLED))) {
3348 iter->pos = iter->func_pos = *pos;
3355 t_next(struct seq_file *m, void *v, loff_t *pos)
3357 struct ftrace_iterator *iter = m->private;
3358 loff_t l = *pos; /* t_probe_start() must use original pos */
3361 if (unlikely(ftrace_disabled))
3364 if (iter->flags & FTRACE_ITER_PROBE)
3365 return t_probe_next(m, pos);
3367 if (iter->flags & FTRACE_ITER_MOD)
3368 return t_mod_next(m, pos);
3370 if (iter->flags & FTRACE_ITER_PRINTALL) {
3371 /* next must increment pos, and t_probe_start does not */
3373 return t_mod_start(m, &l);
3376 ret = t_func_next(m, pos);
3379 return t_mod_start(m, &l);
3384 static void reset_iter_read(struct ftrace_iterator *iter)
3388 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3391 static void *t_start(struct seq_file *m, loff_t *pos)
3393 struct ftrace_iterator *iter = m->private;
3397 mutex_lock(&ftrace_lock);
3399 if (unlikely(ftrace_disabled))
3403 * If an lseek was done, then reset and start from beginning.
3405 if (*pos < iter->pos)
3406 reset_iter_read(iter);
3409 * For set_ftrace_filter reading, if we have the filter
3410 * off, we can short cut and just print out that all
3411 * functions are enabled.
3413 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3414 ftrace_hash_empty(iter->hash)) {
3415 iter->func_pos = 1; /* Account for the message */
3417 return t_mod_start(m, pos);
3418 iter->flags |= FTRACE_ITER_PRINTALL;
3419 /* reset in case of seek/pread */
3420 iter->flags &= ~FTRACE_ITER_PROBE;
3424 if (iter->flags & FTRACE_ITER_MOD)
3425 return t_mod_start(m, pos);
3428 * Unfortunately, we need to restart at ftrace_pages_start
3429 * every time we let go of the ftrace_mutex. This is because
3430 * those pointers can change without the lock.
3432 iter->pg = ftrace_pages_start;
3434 for (l = 0; l <= *pos; ) {
3435 p = t_func_next(m, &l);
3441 return t_mod_start(m, pos);
3446 static void t_stop(struct seq_file *m, void *p)
3448 mutex_unlock(&ftrace_lock);
3452 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3457 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3458 struct dyn_ftrace *rec)
3462 ptr = arch_ftrace_trampoline_func(ops, rec);
3464 seq_printf(m, " ->%pS", ptr);
3467 static int t_show(struct seq_file *m, void *v)
3469 struct ftrace_iterator *iter = m->private;
3470 struct dyn_ftrace *rec;
3472 if (iter->flags & FTRACE_ITER_PROBE)
3473 return t_probe_show(m, iter);
3475 if (iter->flags & FTRACE_ITER_MOD)
3476 return t_mod_show(m, iter);
3478 if (iter->flags & FTRACE_ITER_PRINTALL) {
3479 if (iter->flags & FTRACE_ITER_NOTRACE)
3480 seq_puts(m, "#### no functions disabled ####\n");
3482 seq_puts(m, "#### all functions enabled ####\n");
3491 seq_printf(m, "%ps", (void *)rec->ip);
3492 if (iter->flags & FTRACE_ITER_ENABLED) {
3493 struct ftrace_ops *ops;
3495 seq_printf(m, " (%ld)%s%s",
3496 ftrace_rec_count(rec),
3497 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3498 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3499 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3500 ops = ftrace_find_tramp_ops_any(rec);
3503 seq_printf(m, "\ttramp: %pS (%pS)",
3504 (void *)ops->trampoline,
3506 add_trampoline_func(m, ops, rec);
3507 ops = ftrace_find_tramp_ops_next(rec, ops);
3510 seq_puts(m, "\ttramp: ERROR!");
3512 add_trampoline_func(m, NULL, rec);
3521 static const struct seq_operations show_ftrace_seq_ops = {
3529 ftrace_avail_open(struct inode *inode, struct file *file)
3531 struct ftrace_iterator *iter;
3533 if (unlikely(ftrace_disabled))
3536 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3540 iter->pg = ftrace_pages_start;
3541 iter->ops = &global_ops;
3547 ftrace_enabled_open(struct inode *inode, struct file *file)
3549 struct ftrace_iterator *iter;
3551 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3555 iter->pg = ftrace_pages_start;
3556 iter->flags = FTRACE_ITER_ENABLED;
3557 iter->ops = &global_ops;
3563 * ftrace_regex_open - initialize function tracer filter files
3564 * @ops: The ftrace_ops that hold the hash filters
3565 * @flag: The type of filter to process
3566 * @inode: The inode, usually passed in to your open routine
3567 * @file: The file, usually passed in to your open routine
3569 * ftrace_regex_open() initializes the filter files for the
3570 * @ops. Depending on @flag it may process the filter hash or
3571 * the notrace hash of @ops. With this called from the open
3572 * routine, you can use ftrace_filter_write() for the write
3573 * routine if @flag has FTRACE_ITER_FILTER set, or
3574 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3575 * tracing_lseek() should be used as the lseek routine, and
3576 * release must call ftrace_regex_release().
3579 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3580 struct inode *inode, struct file *file)
3582 struct ftrace_iterator *iter;
3583 struct ftrace_hash *hash;
3584 struct list_head *mod_head;
3585 struct trace_array *tr = ops->private;
3588 ftrace_ops_init(ops);
3590 if (unlikely(ftrace_disabled))
3593 if (tr && trace_array_get(tr) < 0)
3596 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3600 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3607 mutex_lock(&ops->func_hash->regex_lock);
3609 if (flag & FTRACE_ITER_NOTRACE) {
3610 hash = ops->func_hash->notrace_hash;
3611 mod_head = tr ? &tr->mod_notrace : NULL;
3613 hash = ops->func_hash->filter_hash;
3614 mod_head = tr ? &tr->mod_trace : NULL;
3617 iter->mod_list = mod_head;
3619 if (file->f_mode & FMODE_WRITE) {
3620 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3622 if (file->f_flags & O_TRUNC) {
3623 iter->hash = alloc_ftrace_hash(size_bits);
3624 clear_ftrace_mod_list(mod_head);
3626 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3630 trace_parser_put(&iter->parser);
3638 if (file->f_mode & FMODE_READ) {
3639 iter->pg = ftrace_pages_start;
3641 ret = seq_open(file, &show_ftrace_seq_ops);
3643 struct seq_file *m = file->private_data;
3647 free_ftrace_hash(iter->hash);
3648 trace_parser_put(&iter->parser);
3651 file->private_data = iter;
3654 mutex_unlock(&ops->func_hash->regex_lock);
3660 trace_array_put(tr);
3667 ftrace_filter_open(struct inode *inode, struct file *file)
3669 struct ftrace_ops *ops = inode->i_private;
3671 return ftrace_regex_open(ops,
3672 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3677 ftrace_notrace_open(struct inode *inode, struct file *file)
3679 struct ftrace_ops *ops = inode->i_private;
3681 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3685 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3686 struct ftrace_glob {
3693 * If symbols in an architecture don't correspond exactly to the user-visible
3694 * name of what they represent, it is possible to define this function to
3695 * perform the necessary adjustments.
3697 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3702 static int ftrace_match(char *str, struct ftrace_glob *g)
3707 str = arch_ftrace_match_adjust(str, g->search);
3711 if (strcmp(str, g->search) == 0)
3714 case MATCH_FRONT_ONLY:
3715 if (strncmp(str, g->search, g->len) == 0)
3718 case MATCH_MIDDLE_ONLY:
3719 if (strstr(str, g->search))
3722 case MATCH_END_ONLY:
3724 if (slen >= g->len &&
3725 memcmp(str + slen - g->len, g->search, g->len) == 0)
3729 if (glob_match(g->search, str))
3738 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3740 struct ftrace_func_entry *entry;
3743 entry = ftrace_lookup_ip(hash, rec->ip);
3745 /* Do nothing if it doesn't exist */
3749 free_hash_entry(hash, entry);
3751 /* Do nothing if it exists */
3755 ret = add_hash_entry(hash, rec->ip);
3761 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3762 struct ftrace_glob *mod_g, int exclude_mod)
3764 char str[KSYM_SYMBOL_LEN];
3767 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3770 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3772 /* blank module name to match all modules */
3774 /* blank module globbing: modname xor exclude_mod */
3775 if (!exclude_mod != !modname)
3781 * exclude_mod is set to trace everything but the given
3782 * module. If it is set and the module matches, then
3783 * return 0. If it is not set, and the module doesn't match
3784 * also return 0. Otherwise, check the function to see if
3787 if (!mod_matches == !exclude_mod)
3790 /* blank search means to match all funcs in the mod */
3795 return ftrace_match(str, func_g);
3799 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3801 struct ftrace_page *pg;
3802 struct dyn_ftrace *rec;
3803 struct ftrace_glob func_g = { .type = MATCH_FULL };
3804 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3805 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3806 int exclude_mod = 0;
3809 int clear_filter = 0;
3812 func_g.type = filter_parse_regex(func, len, &func_g.search,
3814 func_g.len = strlen(func_g.search);
3818 mod_g.type = filter_parse_regex(mod, strlen(mod),
3819 &mod_g.search, &exclude_mod);
3820 mod_g.len = strlen(mod_g.search);
3823 mutex_lock(&ftrace_lock);
3825 if (unlikely(ftrace_disabled))
3828 do_for_each_ftrace_rec(pg, rec) {
3830 if (rec->flags & FTRACE_FL_DISABLED)
3833 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3834 ret = enter_record(hash, rec, clear_filter);
3841 } while_for_each_ftrace_rec();
3843 mutex_unlock(&ftrace_lock);
3849 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3851 return match_records(hash, buff, len, NULL);
3854 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3855 struct ftrace_ops_hash *old_hash)
3857 struct ftrace_ops *op;
3859 if (!ftrace_enabled)
3862 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3863 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3868 * If this is the shared global_ops filter, then we need to
3869 * check if there is another ops that shares it, is enabled.
3870 * If so, we still need to run the modify code.
3872 if (ops->func_hash != &global_ops.local_hash)
3875 do_for_each_ftrace_op(op, ftrace_ops_list) {
3876 if (op->func_hash == &global_ops.local_hash &&
3877 op->flags & FTRACE_OPS_FL_ENABLED) {
3878 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3879 /* Only need to do this once */
3882 } while_for_each_ftrace_op(op);
3885 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3886 struct ftrace_hash **orig_hash,
3887 struct ftrace_hash *hash,
3890 struct ftrace_ops_hash old_hash_ops;
3891 struct ftrace_hash *old_hash;
3894 old_hash = *orig_hash;
3895 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3896 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3897 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3899 ftrace_ops_update_code(ops, &old_hash_ops);
3900 free_ftrace_hash_rcu(old_hash);
3905 static bool module_exists(const char *module)
3907 /* All modules have the symbol __this_module */
3908 const char this_mod[] = "__this_module";
3909 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
3913 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
3915 if (n > sizeof(modname) - 1)
3918 val = module_kallsyms_lookup_name(modname);
3922 static int cache_mod(struct trace_array *tr,
3923 const char *func, char *module, int enable)
3925 struct ftrace_mod_load *ftrace_mod, *n;
3926 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3929 mutex_lock(&ftrace_lock);
3931 /* We do not cache inverse filters */
3932 if (func[0] == '!') {
3936 /* Look to remove this hash */
3937 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3938 if (strcmp(ftrace_mod->module, module) != 0)
3941 /* no func matches all */
3942 if (strcmp(func, "*") == 0 ||
3943 (ftrace_mod->func &&
3944 strcmp(ftrace_mod->func, func) == 0)) {
3946 free_ftrace_mod(ftrace_mod);
3954 /* We only care about modules that have not been loaded yet */
3955 if (module_exists(module))
3958 /* Save this string off, and execute it when the module is loaded */
3959 ret = ftrace_add_mod(tr, func, module, enable);
3961 mutex_unlock(&ftrace_lock);
3967 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3968 int reset, int enable);
3970 #ifdef CONFIG_MODULES
3971 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3972 char *mod, bool enable)
3974 struct ftrace_mod_load *ftrace_mod, *n;
3975 struct ftrace_hash **orig_hash, *new_hash;
3976 LIST_HEAD(process_mods);
3980 mutex_lock(&ops->func_hash->regex_lock);
3983 orig_hash = &ops->func_hash->filter_hash;
3985 orig_hash = &ops->func_hash->notrace_hash;
3987 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
3990 goto out; /* warn? */
3992 mutex_lock(&ftrace_lock);
3994 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3996 if (strcmp(ftrace_mod->module, mod) != 0)
3999 if (ftrace_mod->func)
4000 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4002 func = kstrdup("*", GFP_KERNEL);
4004 if (!func) /* warn? */
4007 list_del(&ftrace_mod->list);
4008 list_add(&ftrace_mod->list, &process_mods);
4010 /* Use the newly allocated func, as it may be "*" */
4011 kfree(ftrace_mod->func);
4012 ftrace_mod->func = func;
4015 mutex_unlock(&ftrace_lock);
4017 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4019 func = ftrace_mod->func;
4021 /* Grabs ftrace_lock, which is why we have this extra step */
4022 match_records(new_hash, func, strlen(func), mod);
4023 free_ftrace_mod(ftrace_mod);
4026 if (enable && list_empty(head))
4027 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4029 mutex_lock(&ftrace_lock);
4031 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4033 mutex_unlock(&ftrace_lock);
4036 mutex_unlock(&ops->func_hash->regex_lock);
4038 free_ftrace_hash(new_hash);
4041 static void process_cached_mods(const char *mod_name)
4043 struct trace_array *tr;
4046 mod = kstrdup(mod_name, GFP_KERNEL);
4050 mutex_lock(&trace_types_lock);
4051 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4052 if (!list_empty(&tr->mod_trace))
4053 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4054 if (!list_empty(&tr->mod_notrace))
4055 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4057 mutex_unlock(&trace_types_lock);
4064 * We register the module command as a template to show others how
4065 * to register the a command as well.
4069 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4070 char *func_orig, char *cmd, char *module, int enable)
4075 /* match_records() modifies func, and we need the original */
4076 func = kstrdup(func_orig, GFP_KERNEL);
4081 * cmd == 'mod' because we only registered this func
4082 * for the 'mod' ftrace_func_command.
4083 * But if you register one func with multiple commands,
4084 * you can tell which command was used by the cmd
4087 ret = match_records(hash, func, strlen(func), module);
4091 return cache_mod(tr, func_orig, module, enable);
4097 static struct ftrace_func_command ftrace_mod_cmd = {
4099 .func = ftrace_mod_callback,
4102 static int __init ftrace_mod_cmd_init(void)
4104 return register_ftrace_command(&ftrace_mod_cmd);
4106 core_initcall(ftrace_mod_cmd_init);
4108 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4109 struct ftrace_ops *op, struct pt_regs *pt_regs)
4111 struct ftrace_probe_ops *probe_ops;
4112 struct ftrace_func_probe *probe;
4114 probe = container_of(op, struct ftrace_func_probe, ops);
4115 probe_ops = probe->probe_ops;
4118 * Disable preemption for these calls to prevent a RCU grace
4119 * period. This syncs the hash iteration and freeing of items
4120 * on the hash. rcu_read_lock is too dangerous here.
4122 preempt_disable_notrace();
4123 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4124 preempt_enable_notrace();
4127 struct ftrace_func_map {
4128 struct ftrace_func_entry entry;
4132 struct ftrace_func_mapper {
4133 struct ftrace_hash hash;
4137 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4139 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4141 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4143 struct ftrace_hash *hash;
4146 * The mapper is simply a ftrace_hash, but since the entries
4147 * in the hash are not ftrace_func_entry type, we define it
4148 * as a separate structure.
4150 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4151 return (struct ftrace_func_mapper *)hash;
4155 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4156 * @mapper: The mapper that has the ip maps
4157 * @ip: the instruction pointer to find the data for
4159 * Returns the data mapped to @ip if found otherwise NULL. The return
4160 * is actually the address of the mapper data pointer. The address is
4161 * returned for use cases where the data is no bigger than a long, and
4162 * the user can use the data pointer as its data instead of having to
4163 * allocate more memory for the reference.
4165 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4168 struct ftrace_func_entry *entry;
4169 struct ftrace_func_map *map;
4171 entry = ftrace_lookup_ip(&mapper->hash, ip);
4175 map = (struct ftrace_func_map *)entry;
4180 * ftrace_func_mapper_add_ip - Map some data to an ip
4181 * @mapper: The mapper that has the ip maps
4182 * @ip: The instruction pointer address to map @data to
4183 * @data: The data to map to @ip
4185 * Returns 0 on succes otherwise an error.
4187 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4188 unsigned long ip, void *data)
4190 struct ftrace_func_entry *entry;
4191 struct ftrace_func_map *map;
4193 entry = ftrace_lookup_ip(&mapper->hash, ip);
4197 map = kmalloc(sizeof(*map), GFP_KERNEL);
4204 __add_hash_entry(&mapper->hash, &map->entry);
4210 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4211 * @mapper: The mapper that has the ip maps
4212 * @ip: The instruction pointer address to remove the data from
4214 * Returns the data if it is found, otherwise NULL.
4215 * Note, if the data pointer is used as the data itself, (see
4216 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4217 * if the data pointer was set to zero.
4219 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4222 struct ftrace_func_entry *entry;
4223 struct ftrace_func_map *map;
4226 entry = ftrace_lookup_ip(&mapper->hash, ip);
4230 map = (struct ftrace_func_map *)entry;
4233 remove_hash_entry(&mapper->hash, entry);
4240 * free_ftrace_func_mapper - free a mapping of ips and data
4241 * @mapper: The mapper that has the ip maps
4242 * @free_func: A function to be called on each data item.
4244 * This is used to free the function mapper. The @free_func is optional
4245 * and can be used if the data needs to be freed as well.
4247 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4248 ftrace_mapper_func free_func)
4250 struct ftrace_func_entry *entry;
4251 struct ftrace_func_map *map;
4252 struct hlist_head *hhd;
4258 if (free_func && mapper->hash.count) {
4259 size = 1 << mapper->hash.size_bits;
4260 for (i = 0; i < size; i++) {
4261 hhd = &mapper->hash.buckets[i];
4262 hlist_for_each_entry(entry, hhd, hlist) {
4263 map = (struct ftrace_func_map *)entry;
4268 free_ftrace_hash(&mapper->hash);
4271 static void release_probe(struct ftrace_func_probe *probe)
4273 struct ftrace_probe_ops *probe_ops;
4275 mutex_lock(&ftrace_lock);
4277 WARN_ON(probe->ref <= 0);
4279 /* Subtract the ref that was used to protect this instance */
4283 probe_ops = probe->probe_ops;
4285 * Sending zero as ip tells probe_ops to free
4286 * the probe->data itself
4288 if (probe_ops->free)
4289 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4290 list_del(&probe->list);
4293 mutex_unlock(&ftrace_lock);
4296 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4299 * Add one ref to keep it from being freed when releasing the
4300 * ftrace_lock mutex.
4306 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4307 struct ftrace_probe_ops *probe_ops,
4310 struct ftrace_func_entry *entry;
4311 struct ftrace_func_probe *probe;
4312 struct ftrace_hash **orig_hash;
4313 struct ftrace_hash *old_hash;
4314 struct ftrace_hash *hash;
4323 /* We do not support '!' for function probes */
4324 if (WARN_ON(glob[0] == '!'))
4328 mutex_lock(&ftrace_lock);
4329 /* Check if the probe_ops is already registered */
4330 list_for_each_entry(probe, &tr->func_probes, list) {
4331 if (probe->probe_ops == probe_ops)
4334 if (&probe->list == &tr->func_probes) {
4335 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4337 mutex_unlock(&ftrace_lock);
4340 probe->probe_ops = probe_ops;
4341 probe->ops.func = function_trace_probe_call;
4343 ftrace_ops_init(&probe->ops);
4344 list_add(&probe->list, &tr->func_probes);
4347 acquire_probe_locked(probe);
4349 mutex_unlock(&ftrace_lock);
4352 * Note, there's a small window here that the func_hash->filter_hash
4353 * may be NULL or empty. Need to be carefule when reading the loop.
4355 mutex_lock(&probe->ops.func_hash->regex_lock);
4357 orig_hash = &probe->ops.func_hash->filter_hash;
4358 old_hash = *orig_hash;
4359 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4366 ret = ftrace_match_records(hash, glob, strlen(glob));
4368 /* Nothing found? */
4375 size = 1 << hash->size_bits;
4376 for (i = 0; i < size; i++) {
4377 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4378 if (ftrace_lookup_ip(old_hash, entry->ip))
4381 * The caller might want to do something special
4382 * for each function we find. We call the callback
4383 * to give the caller an opportunity to do so.
4385 if (probe_ops->init) {
4386 ret = probe_ops->init(probe_ops, tr,
4390 if (probe_ops->free && count)
4391 probe_ops->free(probe_ops, tr,
4401 mutex_lock(&ftrace_lock);
4404 /* Nothing was added? */
4409 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4414 /* One ref for each new function traced */
4415 probe->ref += count;
4417 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4418 ret = ftrace_startup(&probe->ops, 0);
4421 mutex_unlock(&ftrace_lock);
4426 mutex_unlock(&probe->ops.func_hash->regex_lock);
4427 free_ftrace_hash(hash);
4429 release_probe(probe);
4434 if (!probe_ops->free || !count)
4437 /* Failed to do the move, need to call the free functions */
4438 for (i = 0; i < size; i++) {
4439 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4440 if (ftrace_lookup_ip(old_hash, entry->ip))
4442 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4449 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4450 struct ftrace_probe_ops *probe_ops)
4452 struct ftrace_ops_hash old_hash_ops;
4453 struct ftrace_func_entry *entry;
4454 struct ftrace_func_probe *probe;
4455 struct ftrace_glob func_g;
4456 struct ftrace_hash **orig_hash;
4457 struct ftrace_hash *old_hash;
4458 struct ftrace_hash *hash = NULL;
4459 struct hlist_node *tmp;
4460 struct hlist_head hhd;
4461 char str[KSYM_SYMBOL_LEN];
4463 int i, ret = -ENODEV;
4466 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4467 func_g.search = NULL;
4471 func_g.type = filter_parse_regex(glob, strlen(glob),
4472 &func_g.search, ¬);
4473 func_g.len = strlen(func_g.search);
4475 /* we do not support '!' for function probes */
4480 mutex_lock(&ftrace_lock);
4481 /* Check if the probe_ops is already registered */
4482 list_for_each_entry(probe, &tr->func_probes, list) {
4483 if (probe->probe_ops == probe_ops)
4486 if (&probe->list == &tr->func_probes)
4487 goto err_unlock_ftrace;
4490 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4491 goto err_unlock_ftrace;
4493 acquire_probe_locked(probe);
4495 mutex_unlock(&ftrace_lock);
4497 mutex_lock(&probe->ops.func_hash->regex_lock);
4499 orig_hash = &probe->ops.func_hash->filter_hash;
4500 old_hash = *orig_hash;
4502 if (ftrace_hash_empty(old_hash))
4505 old_hash_ops.filter_hash = old_hash;
4506 /* Probes only have filters */
4507 old_hash_ops.notrace_hash = NULL;
4510 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4514 INIT_HLIST_HEAD(&hhd);
4516 size = 1 << hash->size_bits;
4517 for (i = 0; i < size; i++) {
4518 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4520 if (func_g.search) {
4521 kallsyms_lookup(entry->ip, NULL, NULL,
4523 if (!ftrace_match(str, &func_g))
4527 remove_hash_entry(hash, entry);
4528 hlist_add_head(&entry->hlist, &hhd);
4532 /* Nothing found? */
4538 mutex_lock(&ftrace_lock);
4540 WARN_ON(probe->ref < count);
4542 probe->ref -= count;
4544 if (ftrace_hash_empty(hash))
4545 ftrace_shutdown(&probe->ops, 0);
4547 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4550 /* still need to update the function call sites */
4551 if (ftrace_enabled && !ftrace_hash_empty(hash))
4552 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4554 synchronize_sched();
4556 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4557 hlist_del(&entry->hlist);
4558 if (probe_ops->free)
4559 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4562 mutex_unlock(&ftrace_lock);
4565 mutex_unlock(&probe->ops.func_hash->regex_lock);
4566 free_ftrace_hash(hash);
4568 release_probe(probe);
4573 mutex_unlock(&ftrace_lock);
4577 void clear_ftrace_function_probes(struct trace_array *tr)
4579 struct ftrace_func_probe *probe, *n;
4581 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4582 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4585 static LIST_HEAD(ftrace_commands);
4586 static DEFINE_MUTEX(ftrace_cmd_mutex);
4589 * Currently we only register ftrace commands from __init, so mark this
4592 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4594 struct ftrace_func_command *p;
4597 mutex_lock(&ftrace_cmd_mutex);
4598 list_for_each_entry(p, &ftrace_commands, list) {
4599 if (strcmp(cmd->name, p->name) == 0) {
4604 list_add(&cmd->list, &ftrace_commands);
4606 mutex_unlock(&ftrace_cmd_mutex);
4612 * Currently we only unregister ftrace commands from __init, so mark
4615 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4617 struct ftrace_func_command *p, *n;
4620 mutex_lock(&ftrace_cmd_mutex);
4621 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4622 if (strcmp(cmd->name, p->name) == 0) {
4624 list_del_init(&p->list);
4629 mutex_unlock(&ftrace_cmd_mutex);
4634 static int ftrace_process_regex(struct ftrace_iterator *iter,
4635 char *buff, int len, int enable)
4637 struct ftrace_hash *hash = iter->hash;
4638 struct trace_array *tr = iter->ops->private;
4639 char *func, *command, *next = buff;
4640 struct ftrace_func_command *p;
4643 func = strsep(&next, ":");
4646 ret = ftrace_match_records(hash, func, len);
4656 command = strsep(&next, ":");
4658 mutex_lock(&ftrace_cmd_mutex);
4659 list_for_each_entry(p, &ftrace_commands, list) {
4660 if (strcmp(p->name, command) == 0) {
4661 ret = p->func(tr, hash, func, command, next, enable);
4666 mutex_unlock(&ftrace_cmd_mutex);
4672 ftrace_regex_write(struct file *file, const char __user *ubuf,
4673 size_t cnt, loff_t *ppos, int enable)
4675 struct ftrace_iterator *iter;
4676 struct trace_parser *parser;
4682 if (file->f_mode & FMODE_READ) {
4683 struct seq_file *m = file->private_data;
4686 iter = file->private_data;
4688 if (unlikely(ftrace_disabled))
4691 /* iter->hash is a local copy, so we don't need regex_lock */
4693 parser = &iter->parser;
4694 read = trace_get_user(parser, ubuf, cnt, ppos);
4696 if (read >= 0 && trace_parser_loaded(parser) &&
4697 !trace_parser_cont(parser)) {
4698 ret = ftrace_process_regex(iter, parser->buffer,
4699 parser->idx, enable);
4700 trace_parser_clear(parser);
4711 ftrace_filter_write(struct file *file, const char __user *ubuf,
4712 size_t cnt, loff_t *ppos)
4714 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4718 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4719 size_t cnt, loff_t *ppos)
4721 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4725 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4727 struct ftrace_func_entry *entry;
4729 if (!ftrace_location(ip))
4733 entry = ftrace_lookup_ip(hash, ip);
4736 free_hash_entry(hash, entry);
4740 return add_hash_entry(hash, ip);
4744 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4745 unsigned long ip, int remove, int reset, int enable)
4747 struct ftrace_hash **orig_hash;
4748 struct ftrace_hash *hash;
4751 if (unlikely(ftrace_disabled))
4754 mutex_lock(&ops->func_hash->regex_lock);
4757 orig_hash = &ops->func_hash->filter_hash;
4759 orig_hash = &ops->func_hash->notrace_hash;
4762 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4764 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4768 goto out_regex_unlock;
4771 if (buf && !ftrace_match_records(hash, buf, len)) {
4773 goto out_regex_unlock;
4776 ret = ftrace_match_addr(hash, ip, remove);
4778 goto out_regex_unlock;
4781 mutex_lock(&ftrace_lock);
4782 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4783 mutex_unlock(&ftrace_lock);
4786 mutex_unlock(&ops->func_hash->regex_lock);
4788 free_ftrace_hash(hash);
4793 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4794 int reset, int enable)
4796 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4800 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4801 * @ops - the ops to set the filter with
4802 * @ip - the address to add to or remove from the filter.
4803 * @remove - non zero to remove the ip from the filter
4804 * @reset - non zero to reset all filters before applying this filter.
4806 * Filters denote which functions should be enabled when tracing is enabled
4807 * If @ip is NULL, it failes to update filter.
4809 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4810 int remove, int reset)
4812 ftrace_ops_init(ops);
4813 return ftrace_set_addr(ops, ip, remove, reset, 1);
4815 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4818 * ftrace_ops_set_global_filter - setup ops to use global filters
4819 * @ops - the ops which will use the global filters
4821 * ftrace users who need global function trace filtering should call this.
4822 * It can set the global filter only if ops were not initialized before.
4824 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4826 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4829 ftrace_ops_init(ops);
4830 ops->func_hash = &global_ops.local_hash;
4832 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4835 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4836 int reset, int enable)
4838 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4842 * ftrace_set_filter - set a function to filter on in ftrace
4843 * @ops - the ops to set the filter with
4844 * @buf - the string that holds the function filter text.
4845 * @len - the length of the string.
4846 * @reset - non zero to reset all filters before applying this filter.
4848 * Filters denote which functions should be enabled when tracing is enabled.
4849 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4851 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4854 ftrace_ops_init(ops);
4855 return ftrace_set_regex(ops, buf, len, reset, 1);
4857 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4860 * ftrace_set_notrace - set a function to not trace in ftrace
4861 * @ops - the ops to set the notrace filter with
4862 * @buf - the string that holds the function notrace text.
4863 * @len - the length of the string.
4864 * @reset - non zero to reset all filters before applying this filter.
4866 * Notrace Filters denote which functions should not be enabled when tracing
4867 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4870 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4873 ftrace_ops_init(ops);
4874 return ftrace_set_regex(ops, buf, len, reset, 0);
4876 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4878 * ftrace_set_global_filter - set a function to filter on with global tracers
4879 * @buf - the string that holds the function filter text.
4880 * @len - the length of the string.
4881 * @reset - non zero to reset all filters before applying this filter.
4883 * Filters denote which functions should be enabled when tracing is enabled.
4884 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4886 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4888 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4890 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4893 * ftrace_set_global_notrace - set a function to not trace with global tracers
4894 * @buf - the string that holds the function notrace text.
4895 * @len - the length of the string.
4896 * @reset - non zero to reset all filters before applying this filter.
4898 * Notrace Filters denote which functions should not be enabled when tracing
4899 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4902 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4904 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4906 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4909 * command line interface to allow users to set filters on boot up.
4911 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4912 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4913 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4915 /* Used by function selftest to not test if filter is set */
4916 bool ftrace_filter_param __initdata;
4918 static int __init set_ftrace_notrace(char *str)
4920 ftrace_filter_param = true;
4921 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4924 __setup("ftrace_notrace=", set_ftrace_notrace);
4926 static int __init set_ftrace_filter(char *str)
4928 ftrace_filter_param = true;
4929 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4932 __setup("ftrace_filter=", set_ftrace_filter);
4934 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4935 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4936 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4937 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
4939 static int __init set_graph_function(char *str)
4941 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4944 __setup("ftrace_graph_filter=", set_graph_function);
4946 static int __init set_graph_notrace_function(char *str)
4948 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4951 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4953 static int __init set_graph_max_depth_function(char *str)
4957 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4960 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4962 static void __init set_ftrace_early_graph(char *buf, int enable)
4966 struct ftrace_hash *hash;
4968 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4973 func = strsep(&buf, ",");
4974 /* we allow only one expression at a time */
4975 ret = ftrace_graph_set_hash(hash, func);
4977 printk(KERN_DEBUG "ftrace: function %s not "
4978 "traceable\n", func);
4982 ftrace_graph_hash = hash;
4984 ftrace_graph_notrace_hash = hash;
4986 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4989 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4993 ftrace_ops_init(ops);
4996 func = strsep(&buf, ",");
4997 ftrace_set_regex(ops, func, strlen(func), 0, enable);
5001 static void __init set_ftrace_early_filters(void)
5003 if (ftrace_filter_buf[0])
5004 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5005 if (ftrace_notrace_buf[0])
5006 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5007 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5008 if (ftrace_graph_buf[0])
5009 set_ftrace_early_graph(ftrace_graph_buf, 1);
5010 if (ftrace_graph_notrace_buf[0])
5011 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5012 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5015 int ftrace_regex_release(struct inode *inode, struct file *file)
5017 struct seq_file *m = (struct seq_file *)file->private_data;
5018 struct ftrace_iterator *iter;
5019 struct ftrace_hash **orig_hash;
5020 struct trace_parser *parser;
5024 if (file->f_mode & FMODE_READ) {
5026 seq_release(inode, file);
5028 iter = file->private_data;
5030 parser = &iter->parser;
5031 if (trace_parser_loaded(parser)) {
5032 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
5034 ftrace_process_regex(iter, parser->buffer,
5035 parser->idx, enable);
5038 trace_parser_put(parser);
5040 mutex_lock(&iter->ops->func_hash->regex_lock);
5042 if (file->f_mode & FMODE_WRITE) {
5043 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5046 orig_hash = &iter->ops->func_hash->filter_hash;
5047 if (iter->tr && !list_empty(&iter->tr->mod_trace))
5048 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5050 orig_hash = &iter->ops->func_hash->notrace_hash;
5052 mutex_lock(&ftrace_lock);
5053 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5054 iter->hash, filter_hash);
5055 mutex_unlock(&ftrace_lock);
5057 /* For read only, the hash is the ops hash */
5061 mutex_unlock(&iter->ops->func_hash->regex_lock);
5062 free_ftrace_hash(iter->hash);
5064 trace_array_put(iter->tr);
5070 static const struct file_operations ftrace_avail_fops = {
5071 .open = ftrace_avail_open,
5073 .llseek = seq_lseek,
5074 .release = seq_release_private,
5077 static const struct file_operations ftrace_enabled_fops = {
5078 .open = ftrace_enabled_open,
5080 .llseek = seq_lseek,
5081 .release = seq_release_private,
5084 static const struct file_operations ftrace_filter_fops = {
5085 .open = ftrace_filter_open,
5087 .write = ftrace_filter_write,
5088 .llseek = tracing_lseek,
5089 .release = ftrace_regex_release,
5092 static const struct file_operations ftrace_notrace_fops = {
5093 .open = ftrace_notrace_open,
5095 .write = ftrace_notrace_write,
5096 .llseek = tracing_lseek,
5097 .release = ftrace_regex_release,
5100 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5102 static DEFINE_MUTEX(graph_lock);
5104 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
5105 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
5107 enum graph_filter_type {
5108 GRAPH_FILTER_NOTRACE = 0,
5109 GRAPH_FILTER_FUNCTION,
5112 #define FTRACE_GRAPH_EMPTY ((void *)1)
5114 struct ftrace_graph_data {
5115 struct ftrace_hash *hash;
5116 struct ftrace_func_entry *entry;
5117 int idx; /* for hash table iteration */
5118 enum graph_filter_type type;
5119 struct ftrace_hash *new_hash;
5120 const struct seq_operations *seq_ops;
5121 struct trace_parser parser;
5125 __g_next(struct seq_file *m, loff_t *pos)
5127 struct ftrace_graph_data *fgd = m->private;
5128 struct ftrace_func_entry *entry = fgd->entry;
5129 struct hlist_head *head;
5130 int i, idx = fgd->idx;
5132 if (*pos >= fgd->hash->count)
5136 hlist_for_each_entry_continue(entry, hlist) {
5144 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5145 head = &fgd->hash->buckets[i];
5146 hlist_for_each_entry(entry, head, hlist) {
5156 g_next(struct seq_file *m, void *v, loff_t *pos)
5159 return __g_next(m, pos);
5162 static void *g_start(struct seq_file *m, loff_t *pos)
5164 struct ftrace_graph_data *fgd = m->private;
5166 mutex_lock(&graph_lock);
5168 if (fgd->type == GRAPH_FILTER_FUNCTION)
5169 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5170 lockdep_is_held(&graph_lock));
5172 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5173 lockdep_is_held(&graph_lock));
5175 /* Nothing, tell g_show to print all functions are enabled */
5176 if (ftrace_hash_empty(fgd->hash) && !*pos)
5177 return FTRACE_GRAPH_EMPTY;
5181 return __g_next(m, pos);
5184 static void g_stop(struct seq_file *m, void *p)
5186 mutex_unlock(&graph_lock);
5189 static int g_show(struct seq_file *m, void *v)
5191 struct ftrace_func_entry *entry = v;
5196 if (entry == FTRACE_GRAPH_EMPTY) {
5197 struct ftrace_graph_data *fgd = m->private;
5199 if (fgd->type == GRAPH_FILTER_FUNCTION)
5200 seq_puts(m, "#### all functions enabled ####\n");
5202 seq_puts(m, "#### no functions disabled ####\n");
5206 seq_printf(m, "%ps\n", (void *)entry->ip);
5211 static const struct seq_operations ftrace_graph_seq_ops = {
5219 __ftrace_graph_open(struct inode *inode, struct file *file,
5220 struct ftrace_graph_data *fgd)
5223 struct ftrace_hash *new_hash = NULL;
5225 if (file->f_mode & FMODE_WRITE) {
5226 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5228 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5231 if (file->f_flags & O_TRUNC)
5232 new_hash = alloc_ftrace_hash(size_bits);
5234 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5242 if (file->f_mode & FMODE_READ) {
5243 ret = seq_open(file, &ftrace_graph_seq_ops);
5245 struct seq_file *m = file->private_data;
5249 free_ftrace_hash(new_hash);
5253 file->private_data = fgd;
5256 if (ret < 0 && file->f_mode & FMODE_WRITE)
5257 trace_parser_put(&fgd->parser);
5259 fgd->new_hash = new_hash;
5262 * All uses of fgd->hash must be taken with the graph_lock
5263 * held. The graph_lock is going to be released, so force
5264 * fgd->hash to be reinitialized when it is taken again.
5272 ftrace_graph_open(struct inode *inode, struct file *file)
5274 struct ftrace_graph_data *fgd;
5277 if (unlikely(ftrace_disabled))
5280 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5284 mutex_lock(&graph_lock);
5286 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5287 lockdep_is_held(&graph_lock));
5288 fgd->type = GRAPH_FILTER_FUNCTION;
5289 fgd->seq_ops = &ftrace_graph_seq_ops;
5291 ret = __ftrace_graph_open(inode, file, fgd);
5295 mutex_unlock(&graph_lock);
5300 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5302 struct ftrace_graph_data *fgd;
5305 if (unlikely(ftrace_disabled))
5308 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5312 mutex_lock(&graph_lock);
5314 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5315 lockdep_is_held(&graph_lock));
5316 fgd->type = GRAPH_FILTER_NOTRACE;
5317 fgd->seq_ops = &ftrace_graph_seq_ops;
5319 ret = __ftrace_graph_open(inode, file, fgd);
5323 mutex_unlock(&graph_lock);
5328 ftrace_graph_release(struct inode *inode, struct file *file)
5330 struct ftrace_graph_data *fgd;
5331 struct ftrace_hash *old_hash, *new_hash;
5332 struct trace_parser *parser;
5335 if (file->f_mode & FMODE_READ) {
5336 struct seq_file *m = file->private_data;
5339 seq_release(inode, file);
5341 fgd = file->private_data;
5345 if (file->f_mode & FMODE_WRITE) {
5347 parser = &fgd->parser;
5349 if (trace_parser_loaded((parser))) {
5350 ret = ftrace_graph_set_hash(fgd->new_hash,
5354 trace_parser_put(parser);
5356 new_hash = __ftrace_hash_move(fgd->new_hash);
5362 mutex_lock(&graph_lock);
5364 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5365 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5366 lockdep_is_held(&graph_lock));
5367 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5369 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5370 lockdep_is_held(&graph_lock));
5371 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5374 mutex_unlock(&graph_lock);
5377 * We need to do a hard force of sched synchronization.
5378 * This is because we use preempt_disable() to do RCU, but
5379 * the function tracers can be called where RCU is not watching
5380 * (like before user_exit()). We can not rely on the RCU
5381 * infrastructure to do the synchronization, thus we must do it
5384 schedule_on_each_cpu(ftrace_sync);
5386 free_ftrace_hash(old_hash);
5390 free_ftrace_hash(fgd->new_hash);
5397 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5399 struct ftrace_glob func_g;
5400 struct dyn_ftrace *rec;
5401 struct ftrace_page *pg;
5402 struct ftrace_func_entry *entry;
5407 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5408 &func_g.search, ¬);
5410 func_g.len = strlen(func_g.search);
5412 mutex_lock(&ftrace_lock);
5414 if (unlikely(ftrace_disabled)) {
5415 mutex_unlock(&ftrace_lock);
5419 do_for_each_ftrace_rec(pg, rec) {
5421 if (rec->flags & FTRACE_FL_DISABLED)
5424 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5425 entry = ftrace_lookup_ip(hash, rec->ip);
5432 if (add_hash_entry(hash, rec->ip) < 0)
5436 free_hash_entry(hash, entry);
5441 } while_for_each_ftrace_rec();
5443 mutex_unlock(&ftrace_lock);
5452 ftrace_graph_write(struct file *file, const char __user *ubuf,
5453 size_t cnt, loff_t *ppos)
5455 ssize_t read, ret = 0;
5456 struct ftrace_graph_data *fgd = file->private_data;
5457 struct trace_parser *parser;
5462 /* Read mode uses seq functions */
5463 if (file->f_mode & FMODE_READ) {
5464 struct seq_file *m = file->private_data;
5468 parser = &fgd->parser;
5470 read = trace_get_user(parser, ubuf, cnt, ppos);
5472 if (read >= 0 && trace_parser_loaded(parser) &&
5473 !trace_parser_cont(parser)) {
5475 ret = ftrace_graph_set_hash(fgd->new_hash,
5477 trace_parser_clear(parser);
5486 static const struct file_operations ftrace_graph_fops = {
5487 .open = ftrace_graph_open,
5489 .write = ftrace_graph_write,
5490 .llseek = tracing_lseek,
5491 .release = ftrace_graph_release,
5494 static const struct file_operations ftrace_graph_notrace_fops = {
5495 .open = ftrace_graph_notrace_open,
5497 .write = ftrace_graph_write,
5498 .llseek = tracing_lseek,
5499 .release = ftrace_graph_release,
5501 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5503 void ftrace_create_filter_files(struct ftrace_ops *ops,
5504 struct dentry *parent)
5507 trace_create_file("set_ftrace_filter", 0644, parent,
5508 ops, &ftrace_filter_fops);
5510 trace_create_file("set_ftrace_notrace", 0644, parent,
5511 ops, &ftrace_notrace_fops);
5515 * The name "destroy_filter_files" is really a misnomer. Although
5516 * in the future, it may actualy delete the files, but this is
5517 * really intended to make sure the ops passed in are disabled
5518 * and that when this function returns, the caller is free to
5521 * The "destroy" name is only to match the "create" name that this
5522 * should be paired with.
5524 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5526 mutex_lock(&ftrace_lock);
5527 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5528 ftrace_shutdown(ops, 0);
5529 ops->flags |= FTRACE_OPS_FL_DELETED;
5530 ftrace_free_filter(ops);
5531 mutex_unlock(&ftrace_lock);
5534 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
5537 trace_create_file("available_filter_functions", 0444,
5538 d_tracer, NULL, &ftrace_avail_fops);
5540 trace_create_file("enabled_functions", 0444,
5541 d_tracer, NULL, &ftrace_enabled_fops);
5543 ftrace_create_filter_files(&global_ops, d_tracer);
5545 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5546 trace_create_file("set_graph_function", 0644, d_tracer,
5548 &ftrace_graph_fops);
5549 trace_create_file("set_graph_notrace", 0644, d_tracer,
5551 &ftrace_graph_notrace_fops);
5552 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5557 static int ftrace_cmp_ips(const void *a, const void *b)
5559 const unsigned long *ipa = a;
5560 const unsigned long *ipb = b;
5569 static int ftrace_process_locs(struct module *mod,
5570 unsigned long *start,
5573 struct ftrace_page *start_pg;
5574 struct ftrace_page *pg;
5575 struct dyn_ftrace *rec;
5576 unsigned long count;
5579 unsigned long flags = 0; /* Shut up gcc */
5582 count = end - start;
5587 sort(start, count, sizeof(*start),
5588 ftrace_cmp_ips, NULL);
5590 start_pg = ftrace_allocate_pages(count);
5594 mutex_lock(&ftrace_lock);
5597 * Core and each module needs their own pages, as
5598 * modules will free them when they are removed.
5599 * Force a new page to be allocated for modules.
5602 WARN_ON(ftrace_pages || ftrace_pages_start);
5603 /* First initialization */
5604 ftrace_pages = ftrace_pages_start = start_pg;
5609 if (WARN_ON(ftrace_pages->next)) {
5610 /* Hmm, we have free pages? */
5611 while (ftrace_pages->next)
5612 ftrace_pages = ftrace_pages->next;
5615 ftrace_pages->next = start_pg;
5621 addr = ftrace_call_adjust(*p++);
5623 * Some architecture linkers will pad between
5624 * the different mcount_loc sections of different
5625 * object files to satisfy alignments.
5626 * Skip any NULL pointers.
5631 if (pg->index == pg->size) {
5632 /* We should have allocated enough */
5633 if (WARN_ON(!pg->next))
5638 rec = &pg->records[pg->index++];
5642 /* We should have used all pages */
5645 /* Assign the last page to ftrace_pages */
5649 * We only need to disable interrupts on start up
5650 * because we are modifying code that an interrupt
5651 * may execute, and the modification is not atomic.
5652 * But for modules, nothing runs the code we modify
5653 * until we are finished with it, and there's no
5654 * reason to cause large interrupt latencies while we do it.
5657 local_irq_save(flags);
5658 ftrace_update_code(mod, start_pg);
5660 local_irq_restore(flags);
5663 mutex_unlock(&ftrace_lock);
5668 struct ftrace_mod_func {
5669 struct list_head list;
5675 struct ftrace_mod_map {
5676 struct rcu_head rcu;
5677 struct list_head list;
5679 unsigned long start_addr;
5680 unsigned long end_addr;
5681 struct list_head funcs;
5682 unsigned int num_funcs;
5685 #ifdef CONFIG_MODULES
5687 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5689 static LIST_HEAD(ftrace_mod_maps);
5691 static int referenced_filters(struct dyn_ftrace *rec)
5693 struct ftrace_ops *ops;
5696 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5697 if (ops_references_rec(ops, rec)) {
5699 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
5700 rec->flags |= FTRACE_FL_REGS;
5708 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5710 struct ftrace_func_entry *entry;
5711 struct dyn_ftrace *rec;
5714 if (ftrace_hash_empty(hash))
5717 for (i = 0; i < pg->index; i++) {
5718 rec = &pg->records[i];
5719 entry = __ftrace_lookup_ip(hash, rec->ip);
5721 * Do not allow this rec to match again.
5722 * Yeah, it may waste some memory, but will be removed
5723 * if/when the hash is modified again.
5730 /* Clear any records from hashs */
5731 static void clear_mod_from_hashes(struct ftrace_page *pg)
5733 struct trace_array *tr;
5735 mutex_lock(&trace_types_lock);
5736 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5737 if (!tr->ops || !tr->ops->func_hash)
5739 mutex_lock(&tr->ops->func_hash->regex_lock);
5740 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5741 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5742 mutex_unlock(&tr->ops->func_hash->regex_lock);
5744 mutex_unlock(&trace_types_lock);
5747 static void ftrace_free_mod_map(struct rcu_head *rcu)
5749 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5750 struct ftrace_mod_func *mod_func;
5751 struct ftrace_mod_func *n;
5753 /* All the contents of mod_map are now not visible to readers */
5754 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5755 kfree(mod_func->name);
5756 list_del(&mod_func->list);
5763 void ftrace_release_mod(struct module *mod)
5765 struct ftrace_mod_map *mod_map;
5766 struct ftrace_mod_map *n;
5767 struct dyn_ftrace *rec;
5768 struct ftrace_page **last_pg;
5769 struct ftrace_page *tmp_page = NULL;
5770 struct ftrace_page *pg;
5773 mutex_lock(&ftrace_lock);
5775 if (ftrace_disabled)
5778 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5779 if (mod_map->mod == mod) {
5780 list_del_rcu(&mod_map->list);
5781 call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
5787 * Each module has its own ftrace_pages, remove
5788 * them from the list.
5790 last_pg = &ftrace_pages_start;
5791 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5792 rec = &pg->records[0];
5793 if (within_module_core(rec->ip, mod) ||
5794 within_module_init(rec->ip, mod)) {
5796 * As core pages are first, the first
5797 * page should never be a module page.
5799 if (WARN_ON(pg == ftrace_pages_start))
5802 /* Check if we are deleting the last page */
5803 if (pg == ftrace_pages)
5804 ftrace_pages = next_to_ftrace_page(last_pg);
5806 ftrace_update_tot_cnt -= pg->index;
5807 *last_pg = pg->next;
5809 pg->next = tmp_page;
5812 last_pg = &pg->next;
5815 mutex_unlock(&ftrace_lock);
5817 for (pg = tmp_page; pg; pg = tmp_page) {
5819 /* Needs to be called outside of ftrace_lock */
5820 clear_mod_from_hashes(pg);
5822 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5823 free_pages((unsigned long)pg->records, order);
5824 tmp_page = pg->next;
5829 void ftrace_module_enable(struct module *mod)
5831 struct dyn_ftrace *rec;
5832 struct ftrace_page *pg;
5834 mutex_lock(&ftrace_lock);
5836 if (ftrace_disabled)
5840 * If the tracing is enabled, go ahead and enable the record.
5842 * The reason not to enable the record immediatelly is the
5843 * inherent check of ftrace_make_nop/ftrace_make_call for
5844 * correct previous instructions. Making first the NOP
5845 * conversion puts the module to the correct state, thus
5846 * passing the ftrace_make_call check.
5848 * We also delay this to after the module code already set the
5849 * text to read-only, as we now need to set it back to read-write
5850 * so that we can modify the text.
5852 if (ftrace_start_up)
5853 ftrace_arch_code_modify_prepare();
5855 do_for_each_ftrace_rec(pg, rec) {
5858 * do_for_each_ftrace_rec() is a double loop.
5859 * module text shares the pg. If a record is
5860 * not part of this module, then skip this pg,
5861 * which the "break" will do.
5863 if (!within_module_core(rec->ip, mod) &&
5864 !within_module_init(rec->ip, mod))
5870 * When adding a module, we need to check if tracers are
5871 * currently enabled and if they are, and can trace this record,
5872 * we need to enable the module functions as well as update the
5873 * reference counts for those function records.
5875 if (ftrace_start_up)
5876 cnt += referenced_filters(rec);
5878 rec->flags &= ~FTRACE_FL_DISABLED;
5881 if (ftrace_start_up && cnt) {
5882 int failed = __ftrace_replace_code(rec, 1);
5884 ftrace_bug(failed, rec);
5889 } while_for_each_ftrace_rec();
5892 if (ftrace_start_up)
5893 ftrace_arch_code_modify_post_process();
5896 mutex_unlock(&ftrace_lock);
5898 process_cached_mods(mod->name);
5901 void ftrace_module_init(struct module *mod)
5903 if (ftrace_disabled || !mod->num_ftrace_callsites)
5906 ftrace_process_locs(mod, mod->ftrace_callsites,
5907 mod->ftrace_callsites + mod->num_ftrace_callsites);
5910 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5911 struct dyn_ftrace *rec)
5913 struct ftrace_mod_func *mod_func;
5914 unsigned long symsize;
5915 unsigned long offset;
5916 char str[KSYM_SYMBOL_LEN];
5920 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5924 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5928 mod_func->name = kstrdup(str, GFP_KERNEL);
5929 if (!mod_func->name) {
5934 mod_func->ip = rec->ip - offset;
5935 mod_func->size = symsize;
5937 mod_map->num_funcs++;
5939 list_add_rcu(&mod_func->list, &mod_map->funcs);
5942 static struct ftrace_mod_map *
5943 allocate_ftrace_mod_map(struct module *mod,
5944 unsigned long start, unsigned long end)
5946 struct ftrace_mod_map *mod_map;
5948 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5953 mod_map->start_addr = start;
5954 mod_map->end_addr = end;
5955 mod_map->num_funcs = 0;
5957 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5959 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5965 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5966 unsigned long addr, unsigned long *size,
5967 unsigned long *off, char *sym)
5969 struct ftrace_mod_func *found_func = NULL;
5970 struct ftrace_mod_func *mod_func;
5972 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5973 if (addr >= mod_func->ip &&
5974 addr < mod_func->ip + mod_func->size) {
5975 found_func = mod_func;
5982 *size = found_func->size;
5984 *off = addr - found_func->ip;
5986 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5988 return found_func->name;
5995 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
5996 unsigned long *off, char **modname, char *sym)
5998 struct ftrace_mod_map *mod_map;
5999 const char *ret = NULL;
6001 /* mod_map is freed via call_rcu_sched() */
6003 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6004 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6007 *modname = mod_map->mod->name;
6016 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6017 char *type, char *name,
6018 char *module_name, int *exported)
6020 struct ftrace_mod_map *mod_map;
6021 struct ftrace_mod_func *mod_func;
6024 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6026 if (symnum >= mod_map->num_funcs) {
6027 symnum -= mod_map->num_funcs;
6031 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6037 *value = mod_func->ip;
6039 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6040 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6053 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6054 struct dyn_ftrace *rec) { }
6055 static inline struct ftrace_mod_map *
6056 allocate_ftrace_mod_map(struct module *mod,
6057 unsigned long start, unsigned long end)
6061 #endif /* CONFIG_MODULES */
6063 struct ftrace_init_func {
6064 struct list_head list;
6068 /* Clear any init ips from hashes */
6070 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6072 struct ftrace_func_entry *entry;
6074 if (ftrace_hash_empty(hash))
6077 entry = __ftrace_lookup_ip(hash, func->ip);
6080 * Do not allow this rec to match again.
6081 * Yeah, it may waste some memory, but will be removed
6082 * if/when the hash is modified again.
6089 clear_func_from_hashes(struct ftrace_init_func *func)
6091 struct trace_array *tr;
6093 mutex_lock(&trace_types_lock);
6094 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6095 if (!tr->ops || !tr->ops->func_hash)
6097 mutex_lock(&tr->ops->func_hash->regex_lock);
6098 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6099 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6100 mutex_unlock(&tr->ops->func_hash->regex_lock);
6102 mutex_unlock(&trace_types_lock);
6105 static void add_to_clear_hash_list(struct list_head *clear_list,
6106 struct dyn_ftrace *rec)
6108 struct ftrace_init_func *func;
6110 func = kmalloc(sizeof(*func), GFP_KERNEL);
6112 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6117 list_add(&func->list, clear_list);
6120 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
6122 unsigned long start = (unsigned long)(start_ptr);
6123 unsigned long end = (unsigned long)(end_ptr);
6124 struct ftrace_page **last_pg = &ftrace_pages_start;
6125 struct ftrace_page *pg;
6126 struct dyn_ftrace *rec;
6127 struct dyn_ftrace key;
6128 struct ftrace_mod_map *mod_map = NULL;
6129 struct ftrace_init_func *func, *func_next;
6130 struct list_head clear_hash;
6133 INIT_LIST_HEAD(&clear_hash);
6136 key.flags = end; /* overload flags, as it is unsigned long */
6138 mutex_lock(&ftrace_lock);
6141 * If we are freeing module init memory, then check if
6142 * any tracer is active. If so, we need to save a mapping of
6143 * the module functions being freed with the address.
6145 if (mod && ftrace_ops_list != &ftrace_list_end)
6146 mod_map = allocate_ftrace_mod_map(mod, start, end);
6148 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6149 if (end < pg->records[0].ip ||
6150 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6153 rec = bsearch(&key, pg->records, pg->index,
6154 sizeof(struct dyn_ftrace),
6159 /* rec will be cleared from hashes after ftrace_lock unlock */
6160 add_to_clear_hash_list(&clear_hash, rec);
6163 save_ftrace_mod_rec(mod_map, rec);
6166 ftrace_update_tot_cnt--;
6168 *last_pg = pg->next;
6169 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6170 free_pages((unsigned long)pg->records, order);
6172 pg = container_of(last_pg, struct ftrace_page, next);
6177 memmove(rec, rec + 1,
6178 (pg->index - (rec - pg->records)) * sizeof(*rec));
6179 /* More than one function may be in this block */
6182 mutex_unlock(&ftrace_lock);
6184 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6185 clear_func_from_hashes(func);
6190 void __init ftrace_free_init_mem(void)
6192 void *start = (void *)(&__init_begin);
6193 void *end = (void *)(&__init_end);
6195 ftrace_free_mem(NULL, start, end);
6198 void __init ftrace_init(void)
6200 extern unsigned long __start_mcount_loc[];
6201 extern unsigned long __stop_mcount_loc[];
6202 unsigned long count, flags;
6205 local_irq_save(flags);
6206 ret = ftrace_dyn_arch_init();
6207 local_irq_restore(flags);
6211 count = __stop_mcount_loc - __start_mcount_loc;
6213 pr_info("ftrace: No functions to be traced?\n");
6217 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6218 count, count / ENTRIES_PER_PAGE + 1);
6220 last_ftrace_enabled = ftrace_enabled = 1;
6222 ret = ftrace_process_locs(NULL,
6226 set_ftrace_early_filters();
6230 ftrace_disabled = 1;
6233 /* Do nothing if arch does not support this */
6234 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6238 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6240 arch_ftrace_update_trampoline(ops);
6243 void ftrace_init_trace_array(struct trace_array *tr)
6245 INIT_LIST_HEAD(&tr->func_probes);
6246 INIT_LIST_HEAD(&tr->mod_trace);
6247 INIT_LIST_HEAD(&tr->mod_notrace);
6251 static struct ftrace_ops global_ops = {
6252 .func = ftrace_stub,
6253 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6254 FTRACE_OPS_FL_INITIALIZED |
6258 static int __init ftrace_nodyn_init(void)
6263 core_initcall(ftrace_nodyn_init);
6265 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6266 static inline void ftrace_startup_enable(int command) { }
6267 static inline void ftrace_startup_all(int command) { }
6268 /* Keep as macros so we do not need to define the commands */
6269 # define ftrace_startup(ops, command) \
6271 int ___ret = __register_ftrace_function(ops); \
6273 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
6276 # define ftrace_shutdown(ops, command) \
6278 int ___ret = __unregister_ftrace_function(ops); \
6280 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
6284 # define ftrace_startup_sysctl() do { } while (0)
6285 # define ftrace_shutdown_sysctl() do { } while (0)
6288 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
6293 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6297 #endif /* CONFIG_DYNAMIC_FTRACE */
6299 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6301 tr->ops = &global_ops;
6302 tr->ops->private = tr;
6303 ftrace_init_trace_array(tr);
6306 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6308 /* If we filter on pids, update to use the pid function */
6309 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6310 if (WARN_ON(tr->ops->func != ftrace_stub))
6311 printk("ftrace ops had %pS for function\n",
6314 tr->ops->func = func;
6315 tr->ops->private = tr;
6318 void ftrace_reset_array_ops(struct trace_array *tr)
6320 tr->ops->func = ftrace_stub;
6323 static nokprobe_inline void
6324 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6325 struct ftrace_ops *ignored, struct pt_regs *regs)
6327 struct ftrace_ops *op;
6330 bit = trace_test_and_set_recursion(TRACE_LIST_START);
6335 * Some of the ops may be dynamically allocated,
6336 * they must be freed after a synchronize_sched().
6338 preempt_disable_notrace();
6340 do_for_each_ftrace_op(op, ftrace_ops_list) {
6342 * Check the following for each ops before calling their func:
6343 * if RCU flag is set, then rcu_is_watching() must be true
6344 * if PER_CPU is set, then ftrace_function_local_disable()
6346 * Otherwise test if the ip matches the ops filter
6348 * If any of the above fails then the op->func() is not executed.
6350 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6351 ftrace_ops_test(op, ip, regs)) {
6352 if (FTRACE_WARN_ON(!op->func)) {
6353 pr_warn("op=%p %pS\n", op, op);
6356 op->func(ip, parent_ip, op, regs);
6358 } while_for_each_ftrace_op(op);
6360 preempt_enable_notrace();
6361 trace_clear_recursion(bit);
6365 * Some archs only support passing ip and parent_ip. Even though
6366 * the list function ignores the op parameter, we do not want any
6367 * C side effects, where a function is called without the caller
6368 * sending a third parameter.
6369 * Archs are to support both the regs and ftrace_ops at the same time.
6370 * If they support ftrace_ops, it is assumed they support regs.
6371 * If call backs want to use regs, they must either check for regs
6372 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6373 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6374 * An architecture can pass partial regs with ftrace_ops and still
6375 * set the ARCH_SUPPORTS_FTRACE_OPS.
6377 #if ARCH_SUPPORTS_FTRACE_OPS
6378 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6379 struct ftrace_ops *op, struct pt_regs *regs)
6381 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6383 NOKPROBE_SYMBOL(ftrace_ops_list_func);
6385 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6387 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6389 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
6393 * If there's only one function registered but it does not support
6394 * recursion, needs RCU protection and/or requires per cpu handling, then
6395 * this function will be called by the mcount trampoline.
6397 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6398 struct ftrace_ops *op, struct pt_regs *regs)
6402 bit = trace_test_and_set_recursion(TRACE_LIST_START);
6406 preempt_disable_notrace();
6408 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
6409 op->func(ip, parent_ip, op, regs);
6411 preempt_enable_notrace();
6412 trace_clear_recursion(bit);
6414 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
6417 * ftrace_ops_get_func - get the function a trampoline should call
6418 * @ops: the ops to get the function for
6420 * Normally the mcount trampoline will call the ops->func, but there
6421 * are times that it should not. For example, if the ops does not
6422 * have its own recursion protection, then it should call the
6423 * ftrace_ops_assist_func() instead.
6425 * Returns the function that the trampoline should call for @ops.
6427 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6430 * If the function does not handle recursion, needs to be RCU safe,
6431 * or does per cpu logic, then we need to call the assist handler.
6433 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6434 ops->flags & FTRACE_OPS_FL_RCU)
6435 return ftrace_ops_assist_func;
6441 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6442 struct task_struct *prev, struct task_struct *next)
6444 struct trace_array *tr = data;
6445 struct trace_pid_list *pid_list;
6447 pid_list = rcu_dereference_sched(tr->function_pids);
6449 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6450 trace_ignore_this_task(pid_list, next));
6454 ftrace_pid_follow_sched_process_fork(void *data,
6455 struct task_struct *self,
6456 struct task_struct *task)
6458 struct trace_pid_list *pid_list;
6459 struct trace_array *tr = data;
6461 pid_list = rcu_dereference_sched(tr->function_pids);
6462 trace_filter_add_remove_task(pid_list, self, task);
6466 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6468 struct trace_pid_list *pid_list;
6469 struct trace_array *tr = data;
6471 pid_list = rcu_dereference_sched(tr->function_pids);
6472 trace_filter_add_remove_task(pid_list, NULL, task);
6475 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6478 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6480 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6483 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6485 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6490 static void clear_ftrace_pids(struct trace_array *tr)
6492 struct trace_pid_list *pid_list;
6495 pid_list = rcu_dereference_protected(tr->function_pids,
6496 lockdep_is_held(&ftrace_lock));
6500 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6502 for_each_possible_cpu(cpu)
6503 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6505 rcu_assign_pointer(tr->function_pids, NULL);
6507 /* Wait till all users are no longer using pid filtering */
6508 synchronize_sched();
6510 trace_free_pid_list(pid_list);
6513 void ftrace_clear_pids(struct trace_array *tr)
6515 mutex_lock(&ftrace_lock);
6517 clear_ftrace_pids(tr);
6519 mutex_unlock(&ftrace_lock);
6522 static void ftrace_pid_reset(struct trace_array *tr)
6524 mutex_lock(&ftrace_lock);
6525 clear_ftrace_pids(tr);
6527 ftrace_update_pid_func();
6528 ftrace_startup_all(0);
6530 mutex_unlock(&ftrace_lock);
6533 /* Greater than any max PID */
6534 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6536 static void *fpid_start(struct seq_file *m, loff_t *pos)
6539 struct trace_pid_list *pid_list;
6540 struct trace_array *tr = m->private;
6542 mutex_lock(&ftrace_lock);
6543 rcu_read_lock_sched();
6545 pid_list = rcu_dereference_sched(tr->function_pids);
6548 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6550 return trace_pid_start(pid_list, pos);
6553 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6555 struct trace_array *tr = m->private;
6556 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6558 if (v == FTRACE_NO_PIDS) {
6562 return trace_pid_next(pid_list, v, pos);
6565 static void fpid_stop(struct seq_file *m, void *p)
6568 rcu_read_unlock_sched();
6569 mutex_unlock(&ftrace_lock);
6572 static int fpid_show(struct seq_file *m, void *v)
6574 if (v == FTRACE_NO_PIDS) {
6575 seq_puts(m, "no pid\n");
6579 return trace_pid_show(m, v);
6582 static const struct seq_operations ftrace_pid_sops = {
6583 .start = fpid_start,
6590 ftrace_pid_open(struct inode *inode, struct file *file)
6592 struct trace_array *tr = inode->i_private;
6596 if (trace_array_get(tr) < 0)
6599 if ((file->f_mode & FMODE_WRITE) &&
6600 (file->f_flags & O_TRUNC))
6601 ftrace_pid_reset(tr);
6603 ret = seq_open(file, &ftrace_pid_sops);
6605 trace_array_put(tr);
6607 m = file->private_data;
6608 /* copy tr over to seq ops */
6615 static void ignore_task_cpu(void *data)
6617 struct trace_array *tr = data;
6618 struct trace_pid_list *pid_list;
6621 * This function is called by on_each_cpu() while the
6622 * event_mutex is held.
6624 pid_list = rcu_dereference_protected(tr->function_pids,
6625 mutex_is_locked(&ftrace_lock));
6627 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6628 trace_ignore_this_task(pid_list, current));
6632 ftrace_pid_write(struct file *filp, const char __user *ubuf,
6633 size_t cnt, loff_t *ppos)
6635 struct seq_file *m = filp->private_data;
6636 struct trace_array *tr = m->private;
6637 struct trace_pid_list *filtered_pids = NULL;
6638 struct trace_pid_list *pid_list;
6644 mutex_lock(&ftrace_lock);
6646 filtered_pids = rcu_dereference_protected(tr->function_pids,
6647 lockdep_is_held(&ftrace_lock));
6649 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6653 rcu_assign_pointer(tr->function_pids, pid_list);
6655 if (filtered_pids) {
6656 synchronize_sched();
6657 trace_free_pid_list(filtered_pids);
6658 } else if (pid_list) {
6659 /* Register a probe to set whether to ignore the tracing of a task */
6660 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6664 * Ignoring of pids is done at task switch. But we have to
6665 * check for those tasks that are currently running.
6666 * Always do this in case a pid was appended or removed.
6668 on_each_cpu(ignore_task_cpu, tr, 1);
6670 ftrace_update_pid_func();
6671 ftrace_startup_all(0);
6673 mutex_unlock(&ftrace_lock);
6682 ftrace_pid_release(struct inode *inode, struct file *file)
6684 struct trace_array *tr = inode->i_private;
6686 trace_array_put(tr);
6688 return seq_release(inode, file);
6691 static const struct file_operations ftrace_pid_fops = {
6692 .open = ftrace_pid_open,
6693 .write = ftrace_pid_write,
6695 .llseek = tracing_lseek,
6696 .release = ftrace_pid_release,
6699 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
6701 trace_create_file("set_ftrace_pid", 0644, d_tracer,
6702 tr, &ftrace_pid_fops);
6705 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6706 struct dentry *d_tracer)
6708 /* Only the top level directory has the dyn_tracefs and profile */
6709 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6711 ftrace_init_dyn_tracefs(d_tracer);
6712 ftrace_profile_tracefs(d_tracer);
6716 * ftrace_kill - kill ftrace
6718 * This function should be used by panic code. It stops ftrace
6719 * but in a not so nice way. If you need to simply kill ftrace
6720 * from a non-atomic section, use ftrace_kill.
6722 void ftrace_kill(void)
6724 ftrace_disabled = 1;
6726 ftrace_trace_function = ftrace_stub;
6730 * Test if ftrace is dead or not.
6732 int ftrace_is_dead(void)
6734 return ftrace_disabled;
6738 * register_ftrace_function - register a function for profiling
6739 * @ops - ops structure that holds the function for profiling.
6741 * Register a function to be called by all functions in the
6744 * Note: @ops->func and all the functions it calls must be labeled
6745 * with "notrace", otherwise it will go into a
6748 int register_ftrace_function(struct ftrace_ops *ops)
6752 ftrace_ops_init(ops);
6754 mutex_lock(&ftrace_lock);
6756 ret = ftrace_startup(ops, 0);
6758 mutex_unlock(&ftrace_lock);
6762 EXPORT_SYMBOL_GPL(register_ftrace_function);
6765 * unregister_ftrace_function - unregister a function for profiling.
6766 * @ops - ops structure that holds the function to unregister
6768 * Unregister a function that was added to be called by ftrace profiling.
6770 int unregister_ftrace_function(struct ftrace_ops *ops)
6774 mutex_lock(&ftrace_lock);
6775 ret = ftrace_shutdown(ops, 0);
6776 mutex_unlock(&ftrace_lock);
6780 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
6783 ftrace_enable_sysctl(struct ctl_table *table, int write,
6784 void __user *buffer, size_t *lenp,
6789 mutex_lock(&ftrace_lock);
6791 if (unlikely(ftrace_disabled))
6794 ret = proc_dointvec(table, write, buffer, lenp, ppos);
6796 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
6799 last_ftrace_enabled = !!ftrace_enabled;
6801 if (ftrace_enabled) {
6803 /* we are starting ftrace again */
6804 if (rcu_dereference_protected(ftrace_ops_list,
6805 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
6806 update_ftrace_function();
6808 ftrace_startup_sysctl();
6811 /* stopping ftrace calls (just send to ftrace_stub) */
6812 ftrace_trace_function = ftrace_stub;
6814 ftrace_shutdown_sysctl();
6818 mutex_unlock(&ftrace_lock);
6822 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6824 static struct ftrace_ops graph_ops = {
6825 .func = ftrace_stub,
6826 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6827 FTRACE_OPS_FL_INITIALIZED |
6830 #ifdef FTRACE_GRAPH_TRAMP_ADDR
6831 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
6832 /* trampoline_size is only needed for dynamically allocated tramps */
6834 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6837 void ftrace_graph_sleep_time_control(bool enable)
6839 fgraph_sleep_time = enable;
6842 void ftrace_graph_graph_time_control(bool enable)
6844 fgraph_graph_time = enable;
6847 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6852 /* The callbacks that hook a function */
6853 trace_func_graph_ret_t ftrace_graph_return =
6854 (trace_func_graph_ret_t)ftrace_stub;
6855 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
6856 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
6858 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6859 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6863 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6864 struct task_struct *g, *t;
6866 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6868 kmalloc_array(FTRACE_RETFUNC_DEPTH,
6869 sizeof(struct ftrace_ret_stack),
6871 if (!ret_stack_list[i]) {
6879 read_lock(&tasklist_lock);
6880 do_each_thread(g, t) {
6886 if (t->ret_stack == NULL) {
6887 atomic_set(&t->trace_overrun, 0);
6888 t->curr_ret_stack = -1;
6889 t->curr_ret_depth = -1;
6890 /* Make sure the tasks see the -1 first: */
6892 t->ret_stack = ret_stack_list[start++];
6894 } while_each_thread(g, t);
6897 read_unlock(&tasklist_lock);
6899 for (i = start; i < end; i++)
6900 kfree(ret_stack_list[i]);
6905 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
6906 struct task_struct *prev, struct task_struct *next)
6908 unsigned long long timestamp;
6912 * Does the user want to count the time a function was asleep.
6913 * If so, do not update the time stamps.
6915 if (fgraph_sleep_time)
6918 timestamp = trace_clock_local();
6920 prev->ftrace_timestamp = timestamp;
6922 /* only process tasks that we timestamped */
6923 if (!next->ftrace_timestamp)
6927 * Update all the counters in next to make up for the
6928 * time next was sleeping.
6930 timestamp -= next->ftrace_timestamp;
6932 for (index = next->curr_ret_stack; index >= 0; index--)
6933 next->ret_stack[index].calltime += timestamp;
6936 /* Allocate a return stack for each task */
6937 static int start_graph_tracing(void)
6939 struct ftrace_ret_stack **ret_stack_list;
6942 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
6943 sizeof(struct ftrace_ret_stack *),
6946 if (!ret_stack_list)
6949 /* The cpu_boot init_task->ret_stack will never be freed */
6950 for_each_online_cpu(cpu) {
6951 if (!idle_task(cpu)->ret_stack)
6952 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
6956 ret = alloc_retstack_tasklist(ret_stack_list);
6957 } while (ret == -EAGAIN);
6960 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6962 pr_info("ftrace_graph: Couldn't activate tracepoint"
6963 " probe to kernel_sched_switch\n");
6966 kfree(ret_stack_list);
6971 * Hibernation protection.
6972 * The state of the current task is too much unstable during
6973 * suspend/restore to disk. We want to protect against that.
6976 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6980 case PM_HIBERNATION_PREPARE:
6981 pause_graph_tracing();
6984 case PM_POST_HIBERNATION:
6985 unpause_graph_tracing();
6991 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6993 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6995 return __ftrace_graph_entry(trace);
6999 * The function graph tracer should only trace the functions defined
7000 * by set_ftrace_filter and set_ftrace_notrace. If another function
7001 * tracer ops is registered, the graph tracer requires testing the
7002 * function against the global ops, and not just trace any function
7003 * that any ftrace_ops registered.
7005 static void update_function_graph_func(void)
7007 struct ftrace_ops *op;
7008 bool do_test = false;
7011 * The graph and global ops share the same set of functions
7012 * to test. If any other ops is on the list, then
7013 * the graph tracing needs to test if its the function
7016 do_for_each_ftrace_op(op, ftrace_ops_list) {
7017 if (op != &global_ops && op != &graph_ops &&
7018 op != &ftrace_list_end) {
7020 /* in double loop, break out with goto */
7023 } while_for_each_ftrace_op(op);
7026 ftrace_graph_entry = ftrace_graph_entry_test;
7028 ftrace_graph_entry = __ftrace_graph_entry;
7031 static struct notifier_block ftrace_suspend_notifier = {
7032 .notifier_call = ftrace_suspend_notifier_call,
7035 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
7036 trace_func_graph_ent_t entryfunc)
7040 mutex_lock(&ftrace_lock);
7042 /* we currently allow only one tracer registered at a time */
7043 if (ftrace_graph_active) {
7048 register_pm_notifier(&ftrace_suspend_notifier);
7050 ftrace_graph_active++;
7051 ret = start_graph_tracing();
7053 ftrace_graph_active--;
7057 ftrace_graph_return = retfunc;
7060 * Update the indirect function to the entryfunc, and the
7061 * function that gets called to the entry_test first. Then
7062 * call the update fgraph entry function to determine if
7063 * the entryfunc should be called directly or not.
7065 __ftrace_graph_entry = entryfunc;
7066 ftrace_graph_entry = ftrace_graph_entry_test;
7067 update_function_graph_func();
7069 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
7071 mutex_unlock(&ftrace_lock);
7075 void unregister_ftrace_graph(void)
7077 mutex_lock(&ftrace_lock);
7079 if (unlikely(!ftrace_graph_active))
7082 ftrace_graph_active--;
7083 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
7084 ftrace_graph_entry = ftrace_graph_entry_stub;
7085 __ftrace_graph_entry = ftrace_graph_entry_stub;
7086 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
7087 unregister_pm_notifier(&ftrace_suspend_notifier);
7088 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
7091 mutex_unlock(&ftrace_lock);
7094 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
7097 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
7099 atomic_set(&t->trace_overrun, 0);
7100 t->ftrace_timestamp = 0;
7101 /* make curr_ret_stack visible before we add the ret_stack */
7103 t->ret_stack = ret_stack;
7107 * Allocate a return stack for the idle task. May be the first
7108 * time through, or it may be done by CPU hotplug online.
7110 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
7112 t->curr_ret_stack = -1;
7113 t->curr_ret_depth = -1;
7115 * The idle task has no parent, it either has its own
7116 * stack or no stack at all.
7119 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
7121 if (ftrace_graph_active) {
7122 struct ftrace_ret_stack *ret_stack;
7124 ret_stack = per_cpu(idle_ret_stack, cpu);
7127 kmalloc_array(FTRACE_RETFUNC_DEPTH,
7128 sizeof(struct ftrace_ret_stack),
7132 per_cpu(idle_ret_stack, cpu) = ret_stack;
7134 graph_init_task(t, ret_stack);
7138 /* Allocate a return stack for newly created task */
7139 void ftrace_graph_init_task(struct task_struct *t)
7141 /* Make sure we do not use the parent ret_stack */
7142 t->ret_stack = NULL;
7143 t->curr_ret_stack = -1;
7144 t->curr_ret_depth = -1;
7146 if (ftrace_graph_active) {
7147 struct ftrace_ret_stack *ret_stack;
7149 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
7150 sizeof(struct ftrace_ret_stack),
7154 graph_init_task(t, ret_stack);
7158 void ftrace_graph_exit_task(struct task_struct *t)
7160 struct ftrace_ret_stack *ret_stack = t->ret_stack;
7162 t->ret_stack = NULL;
7163 /* NULL must become visible to IRQs before we free it: */