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_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1655 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1659 struct ftrace_hash *hash;
1660 struct ftrace_hash *other_hash;
1661 struct ftrace_page *pg;
1662 struct dyn_ftrace *rec;
1663 bool update = false;
1667 /* Only update if the ops has been registered */
1668 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1672 * In the filter_hash case:
1673 * If the count is zero, we update all records.
1674 * Otherwise we just update the items in the hash.
1676 * In the notrace_hash case:
1677 * We enable the update in the hash.
1678 * As disabling notrace means enabling the tracing,
1679 * and enabling notrace means disabling, the inc variable
1683 hash = ops->func_hash->filter_hash;
1684 other_hash = ops->func_hash->notrace_hash;
1685 if (ftrace_hash_empty(hash))
1689 hash = ops->func_hash->notrace_hash;
1690 other_hash = ops->func_hash->filter_hash;
1692 * If the notrace hash has no items,
1693 * then there's nothing to do.
1695 if (ftrace_hash_empty(hash))
1699 do_for_each_ftrace_rec(pg, rec) {
1700 int in_other_hash = 0;
1704 if (rec->flags & FTRACE_FL_DISABLED)
1709 * Only the filter_hash affects all records.
1710 * Update if the record is not in the notrace hash.
1712 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1715 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1716 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1719 * If filter_hash is set, we want to match all functions
1720 * that are in the hash but not in the other hash.
1722 * If filter_hash is not set, then we are decrementing.
1723 * That means we match anything that is in the hash
1724 * and also in the other_hash. That is, we need to turn
1725 * off functions in the other hash because they are disabled
1728 if (filter_hash && in_hash && !in_other_hash)
1730 else if (!filter_hash && in_hash &&
1731 (in_other_hash || ftrace_hash_empty(other_hash)))
1739 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1743 * If there's only a single callback registered to a
1744 * function, and the ops has a trampoline registered
1745 * for it, then we can call it directly.
1747 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1748 rec->flags |= FTRACE_FL_TRAMP;
1751 * If we are adding another function callback
1752 * to this function, and the previous had a
1753 * custom trampoline in use, then we need to go
1754 * back to the default trampoline.
1756 rec->flags &= ~FTRACE_FL_TRAMP;
1759 * If any ops wants regs saved for this function
1760 * then all ops will get saved regs.
1762 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1763 rec->flags |= FTRACE_FL_REGS;
1765 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1770 * If the rec had REGS enabled and the ops that is
1771 * being removed had REGS set, then see if there is
1772 * still any ops for this record that wants regs.
1773 * If not, we can stop recording them.
1775 if (ftrace_rec_count(rec) > 0 &&
1776 rec->flags & FTRACE_FL_REGS &&
1777 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1778 if (!test_rec_ops_needs_regs(rec))
1779 rec->flags &= ~FTRACE_FL_REGS;
1783 * The TRAMP needs to be set only if rec count
1784 * is decremented to one, and the ops that is
1785 * left has a trampoline. As TRAMP can only be
1786 * enabled if there is only a single ops attached
1789 if (ftrace_rec_count(rec) == 1 &&
1790 ftrace_find_tramp_ops_any(rec))
1791 rec->flags |= FTRACE_FL_TRAMP;
1793 rec->flags &= ~FTRACE_FL_TRAMP;
1796 * flags will be cleared in ftrace_check_record()
1797 * if rec count is zero.
1802 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1803 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1805 /* Shortcut, if we handled all records, we are done. */
1806 if (!all && count == hash->count)
1808 } while_for_each_ftrace_rec();
1813 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1816 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1819 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1822 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1825 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1826 int filter_hash, int inc)
1828 struct ftrace_ops *op;
1830 __ftrace_hash_rec_update(ops, filter_hash, inc);
1832 if (ops->func_hash != &global_ops.local_hash)
1836 * If the ops shares the global_ops hash, then we need to update
1837 * all ops that are enabled and use this hash.
1839 do_for_each_ftrace_op(op, ftrace_ops_list) {
1843 if (op->func_hash == &global_ops.local_hash)
1844 __ftrace_hash_rec_update(op, filter_hash, inc);
1845 } while_for_each_ftrace_op(op);
1848 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1851 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1854 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1857 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1861 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1862 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1863 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1864 * Note that old_hash and new_hash has below meanings
1865 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1866 * - If the hash is EMPTY_HASH, it hits nothing
1867 * - Anything else hits the recs which match the hash entries.
1869 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1870 struct ftrace_hash *old_hash,
1871 struct ftrace_hash *new_hash)
1873 struct ftrace_page *pg;
1874 struct dyn_ftrace *rec, *end = NULL;
1877 /* Only update if the ops has been registered */
1878 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1881 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1885 * Since the IPMODIFY is a very address sensitive action, we do not
1886 * allow ftrace_ops to set all functions to new hash.
1888 if (!new_hash || !old_hash)
1891 /* Update rec->flags */
1892 do_for_each_ftrace_rec(pg, rec) {
1894 if (rec->flags & FTRACE_FL_DISABLED)
1897 /* We need to update only differences of filter_hash */
1898 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1899 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1900 if (in_old == in_new)
1904 /* New entries must ensure no others are using it */
1905 if (rec->flags & FTRACE_FL_IPMODIFY)
1907 rec->flags |= FTRACE_FL_IPMODIFY;
1908 } else /* Removed entry */
1909 rec->flags &= ~FTRACE_FL_IPMODIFY;
1910 } while_for_each_ftrace_rec();
1917 /* Roll back what we did above */
1918 do_for_each_ftrace_rec(pg, rec) {
1920 if (rec->flags & FTRACE_FL_DISABLED)
1926 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1927 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1928 if (in_old == in_new)
1932 rec->flags &= ~FTRACE_FL_IPMODIFY;
1934 rec->flags |= FTRACE_FL_IPMODIFY;
1935 } while_for_each_ftrace_rec();
1941 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1943 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1945 if (ftrace_hash_empty(hash))
1948 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1951 /* Disabling always succeeds */
1952 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1954 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1956 if (ftrace_hash_empty(hash))
1959 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1962 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1963 struct ftrace_hash *new_hash)
1965 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1967 if (ftrace_hash_empty(old_hash))
1970 if (ftrace_hash_empty(new_hash))
1973 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1976 static void print_ip_ins(const char *fmt, const unsigned char *p)
1980 printk(KERN_CONT "%s", fmt);
1982 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1983 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1986 enum ftrace_bug_type ftrace_bug_type;
1987 const void *ftrace_expected;
1989 static void print_bug_type(void)
1991 switch (ftrace_bug_type) {
1992 case FTRACE_BUG_UNKNOWN:
1994 case FTRACE_BUG_INIT:
1995 pr_info("Initializing ftrace call sites\n");
1997 case FTRACE_BUG_NOP:
1998 pr_info("Setting ftrace call site to NOP\n");
2000 case FTRACE_BUG_CALL:
2001 pr_info("Setting ftrace call site to call ftrace function\n");
2003 case FTRACE_BUG_UPDATE:
2004 pr_info("Updating ftrace call site to call a different ftrace function\n");
2010 * ftrace_bug - report and shutdown function tracer
2011 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2012 * @rec: The record that failed
2014 * The arch code that enables or disables the function tracing
2015 * can call ftrace_bug() when it has detected a problem in
2016 * modifying the code. @failed should be one of either:
2017 * EFAULT - if the problem happens on reading the @ip address
2018 * EINVAL - if what is read at @ip is not what was expected
2019 * EPERM - if the problem happens on writting to the @ip address
2021 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2023 unsigned long ip = rec ? rec->ip : 0;
2027 FTRACE_WARN_ON_ONCE(1);
2028 pr_info("ftrace faulted on modifying ");
2032 FTRACE_WARN_ON_ONCE(1);
2033 pr_info("ftrace failed to modify ");
2035 print_ip_ins(" actual: ", (unsigned char *)ip);
2037 if (ftrace_expected) {
2038 print_ip_ins(" expected: ", ftrace_expected);
2043 FTRACE_WARN_ON_ONCE(1);
2044 pr_info("ftrace faulted on writing ");
2048 FTRACE_WARN_ON_ONCE(1);
2049 pr_info("ftrace faulted on unknown error ");
2054 struct ftrace_ops *ops = NULL;
2056 pr_info("ftrace record flags: %lx\n", rec->flags);
2057 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2058 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2059 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2060 ops = ftrace_find_tramp_ops_any(rec);
2063 pr_cont("\ttramp: %pS (%pS)",
2064 (void *)ops->trampoline,
2066 ops = ftrace_find_tramp_ops_next(rec, ops);
2069 pr_cont("\ttramp: ERROR!");
2072 ip = ftrace_get_addr_curr(rec);
2073 pr_cont("\n expected tramp: %lx\n", ip);
2077 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2079 unsigned long flag = 0UL;
2081 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2083 if (rec->flags & FTRACE_FL_DISABLED)
2084 return FTRACE_UPDATE_IGNORE;
2087 * If we are updating calls:
2089 * If the record has a ref count, then we need to enable it
2090 * because someone is using it.
2092 * Otherwise we make sure its disabled.
2094 * If we are disabling calls, then disable all records that
2097 if (enable && ftrace_rec_count(rec))
2098 flag = FTRACE_FL_ENABLED;
2101 * If enabling and the REGS flag does not match the REGS_EN, or
2102 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2103 * this record. Set flags to fail the compare against ENABLED.
2106 if (!(rec->flags & FTRACE_FL_REGS) !=
2107 !(rec->flags & FTRACE_FL_REGS_EN))
2108 flag |= FTRACE_FL_REGS;
2110 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2111 !(rec->flags & FTRACE_FL_TRAMP_EN))
2112 flag |= FTRACE_FL_TRAMP;
2115 /* If the state of this record hasn't changed, then do nothing */
2116 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2117 return FTRACE_UPDATE_IGNORE;
2120 /* Save off if rec is being enabled (for return value) */
2121 flag ^= rec->flags & FTRACE_FL_ENABLED;
2124 rec->flags |= FTRACE_FL_ENABLED;
2125 if (flag & FTRACE_FL_REGS) {
2126 if (rec->flags & FTRACE_FL_REGS)
2127 rec->flags |= FTRACE_FL_REGS_EN;
2129 rec->flags &= ~FTRACE_FL_REGS_EN;
2131 if (flag & FTRACE_FL_TRAMP) {
2132 if (rec->flags & FTRACE_FL_TRAMP)
2133 rec->flags |= FTRACE_FL_TRAMP_EN;
2135 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2140 * If this record is being updated from a nop, then
2141 * return UPDATE_MAKE_CALL.
2143 * return UPDATE_MODIFY_CALL to tell the caller to convert
2144 * from the save regs, to a non-save regs function or
2145 * vice versa, or from a trampoline call.
2147 if (flag & FTRACE_FL_ENABLED) {
2148 ftrace_bug_type = FTRACE_BUG_CALL;
2149 return FTRACE_UPDATE_MAKE_CALL;
2152 ftrace_bug_type = FTRACE_BUG_UPDATE;
2153 return FTRACE_UPDATE_MODIFY_CALL;
2157 /* If there's no more users, clear all flags */
2158 if (!ftrace_rec_count(rec))
2162 * Just disable the record, but keep the ops TRAMP
2163 * and REGS states. The _EN flags must be disabled though.
2165 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2169 ftrace_bug_type = FTRACE_BUG_NOP;
2170 return FTRACE_UPDATE_MAKE_NOP;
2174 * ftrace_update_record, set a record that now is tracing or not
2175 * @rec: the record to update
2176 * @enable: set to 1 if the record is tracing, zero to force disable
2178 * The records that represent all functions that can be traced need
2179 * to be updated when tracing has been enabled.
2181 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2183 return ftrace_check_record(rec, enable, 1);
2187 * ftrace_test_record, check if the record has been enabled or not
2188 * @rec: the record to test
2189 * @enable: set to 1 to check if enabled, 0 if it is disabled
2191 * The arch code may need to test if a record is already set to
2192 * tracing to determine how to modify the function code that it
2195 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2197 return ftrace_check_record(rec, enable, 0);
2200 static struct ftrace_ops *
2201 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2203 struct ftrace_ops *op;
2204 unsigned long ip = rec->ip;
2206 do_for_each_ftrace_op(op, ftrace_ops_list) {
2208 if (!op->trampoline)
2211 if (hash_contains_ip(ip, op->func_hash))
2213 } while_for_each_ftrace_op(op);
2218 static struct ftrace_ops *
2219 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2220 struct ftrace_ops *op)
2222 unsigned long ip = rec->ip;
2224 while_for_each_ftrace_op(op) {
2226 if (!op->trampoline)
2229 if (hash_contains_ip(ip, op->func_hash))
2236 static struct ftrace_ops *
2237 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2239 struct ftrace_ops *op;
2240 unsigned long ip = rec->ip;
2243 * Need to check removed ops first.
2244 * If they are being removed, and this rec has a tramp,
2245 * and this rec is in the ops list, then it would be the
2246 * one with the tramp.
2249 if (hash_contains_ip(ip, &removed_ops->old_hash))
2254 * Need to find the current trampoline for a rec.
2255 * Now, a trampoline is only attached to a rec if there
2256 * was a single 'ops' attached to it. But this can be called
2257 * when we are adding another op to the rec or removing the
2258 * current one. Thus, if the op is being added, we can
2259 * ignore it because it hasn't attached itself to the rec
2262 * If an ops is being modified (hooking to different functions)
2263 * then we don't care about the new functions that are being
2264 * added, just the old ones (that are probably being removed).
2266 * If we are adding an ops to a function that already is using
2267 * a trampoline, it needs to be removed (trampolines are only
2268 * for single ops connected), then an ops that is not being
2269 * modified also needs to be checked.
2271 do_for_each_ftrace_op(op, ftrace_ops_list) {
2273 if (!op->trampoline)
2277 * If the ops is being added, it hasn't gotten to
2278 * the point to be removed from this tree yet.
2280 if (op->flags & FTRACE_OPS_FL_ADDING)
2285 * If the ops is being modified and is in the old
2286 * hash, then it is probably being removed from this
2289 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2290 hash_contains_ip(ip, &op->old_hash))
2293 * If the ops is not being added or modified, and it's
2294 * in its normal filter hash, then this must be the one
2297 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2298 hash_contains_ip(ip, op->func_hash))
2301 } while_for_each_ftrace_op(op);
2306 static struct ftrace_ops *
2307 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2309 struct ftrace_ops *op;
2310 unsigned long ip = rec->ip;
2312 do_for_each_ftrace_op(op, ftrace_ops_list) {
2313 /* pass rec in as regs to have non-NULL val */
2314 if (hash_contains_ip(ip, op->func_hash))
2316 } while_for_each_ftrace_op(op);
2322 * ftrace_get_addr_new - Get the call address to set to
2323 * @rec: The ftrace record descriptor
2325 * If the record has the FTRACE_FL_REGS set, that means that it
2326 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2327 * is not not set, then it wants to convert to the normal callback.
2329 * Returns the address of the trampoline to set to
2331 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2333 struct ftrace_ops *ops;
2335 /* Trampolines take precedence over regs */
2336 if (rec->flags & FTRACE_FL_TRAMP) {
2337 ops = ftrace_find_tramp_ops_new(rec);
2338 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2339 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2340 (void *)rec->ip, (void *)rec->ip, rec->flags);
2341 /* Ftrace is shutting down, return anything */
2342 return (unsigned long)FTRACE_ADDR;
2344 return ops->trampoline;
2347 if (rec->flags & FTRACE_FL_REGS)
2348 return (unsigned long)FTRACE_REGS_ADDR;
2350 return (unsigned long)FTRACE_ADDR;
2354 * ftrace_get_addr_curr - Get the call address that is already there
2355 * @rec: The ftrace record descriptor
2357 * The FTRACE_FL_REGS_EN is set when the record already points to
2358 * a function that saves all the regs. Basically the '_EN' version
2359 * represents the current state of the function.
2361 * Returns the address of the trampoline that is currently being called
2363 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2365 struct ftrace_ops *ops;
2367 /* Trampolines take precedence over regs */
2368 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2369 ops = ftrace_find_tramp_ops_curr(rec);
2370 if (FTRACE_WARN_ON(!ops)) {
2371 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2372 (void *)rec->ip, (void *)rec->ip);
2373 /* Ftrace is shutting down, return anything */
2374 return (unsigned long)FTRACE_ADDR;
2376 return ops->trampoline;
2379 if (rec->flags & FTRACE_FL_REGS_EN)
2380 return (unsigned long)FTRACE_REGS_ADDR;
2382 return (unsigned long)FTRACE_ADDR;
2386 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2388 unsigned long ftrace_old_addr;
2389 unsigned long ftrace_addr;
2392 ftrace_addr = ftrace_get_addr_new(rec);
2394 /* This needs to be done before we call ftrace_update_record */
2395 ftrace_old_addr = ftrace_get_addr_curr(rec);
2397 ret = ftrace_update_record(rec, enable);
2399 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2402 case FTRACE_UPDATE_IGNORE:
2405 case FTRACE_UPDATE_MAKE_CALL:
2406 ftrace_bug_type = FTRACE_BUG_CALL;
2407 return ftrace_make_call(rec, ftrace_addr);
2409 case FTRACE_UPDATE_MAKE_NOP:
2410 ftrace_bug_type = FTRACE_BUG_NOP;
2411 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2413 case FTRACE_UPDATE_MODIFY_CALL:
2414 ftrace_bug_type = FTRACE_BUG_UPDATE;
2415 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2418 return -1; /* unknow ftrace bug */
2421 void __weak ftrace_replace_code(int enable)
2423 struct dyn_ftrace *rec;
2424 struct ftrace_page *pg;
2427 if (unlikely(ftrace_disabled))
2430 do_for_each_ftrace_rec(pg, rec) {
2432 if (rec->flags & FTRACE_FL_DISABLED)
2435 failed = __ftrace_replace_code(rec, enable);
2437 ftrace_bug(failed, rec);
2438 /* Stop processing */
2441 } while_for_each_ftrace_rec();
2444 struct ftrace_rec_iter {
2445 struct ftrace_page *pg;
2450 * ftrace_rec_iter_start, start up iterating over traced functions
2452 * Returns an iterator handle that is used to iterate over all
2453 * the records that represent address locations where functions
2456 * May return NULL if no records are available.
2458 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2461 * We only use a single iterator.
2462 * Protected by the ftrace_lock mutex.
2464 static struct ftrace_rec_iter ftrace_rec_iter;
2465 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2467 iter->pg = ftrace_pages_start;
2470 /* Could have empty pages */
2471 while (iter->pg && !iter->pg->index)
2472 iter->pg = iter->pg->next;
2481 * ftrace_rec_iter_next, get the next record to process.
2482 * @iter: The handle to the iterator.
2484 * Returns the next iterator after the given iterator @iter.
2486 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2490 if (iter->index >= iter->pg->index) {
2491 iter->pg = iter->pg->next;
2494 /* Could have empty pages */
2495 while (iter->pg && !iter->pg->index)
2496 iter->pg = iter->pg->next;
2506 * ftrace_rec_iter_record, get the record at the iterator location
2507 * @iter: The current iterator location
2509 * Returns the record that the current @iter is at.
2511 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2513 return &iter->pg->records[iter->index];
2517 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2521 if (unlikely(ftrace_disabled))
2524 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2526 ftrace_bug_type = FTRACE_BUG_INIT;
2527 ftrace_bug(ret, rec);
2534 * archs can override this function if they must do something
2535 * before the modifying code is performed.
2537 int __weak ftrace_arch_code_modify_prepare(void)
2543 * archs can override this function if they must do something
2544 * after the modifying code is performed.
2546 int __weak ftrace_arch_code_modify_post_process(void)
2551 void ftrace_modify_all_code(int command)
2553 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2557 * If the ftrace_caller calls a ftrace_ops func directly,
2558 * we need to make sure that it only traces functions it
2559 * expects to trace. When doing the switch of functions,
2560 * we need to update to the ftrace_ops_list_func first
2561 * before the transition between old and new calls are set,
2562 * as the ftrace_ops_list_func will check the ops hashes
2563 * to make sure the ops are having the right functions
2567 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2568 if (FTRACE_WARN_ON(err))
2572 if (command & FTRACE_UPDATE_CALLS)
2573 ftrace_replace_code(1);
2574 else if (command & FTRACE_DISABLE_CALLS)
2575 ftrace_replace_code(0);
2577 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2578 function_trace_op = set_function_trace_op;
2580 /* If irqs are disabled, we are in stop machine */
2581 if (!irqs_disabled())
2582 smp_call_function(ftrace_sync_ipi, NULL, 1);
2583 err = ftrace_update_ftrace_func(ftrace_trace_function);
2584 if (FTRACE_WARN_ON(err))
2588 if (command & FTRACE_START_FUNC_RET)
2589 err = ftrace_enable_ftrace_graph_caller();
2590 else if (command & FTRACE_STOP_FUNC_RET)
2591 err = ftrace_disable_ftrace_graph_caller();
2592 FTRACE_WARN_ON(err);
2595 static int __ftrace_modify_code(void *data)
2597 int *command = data;
2599 ftrace_modify_all_code(*command);
2605 * ftrace_run_stop_machine, go back to the stop machine method
2606 * @command: The command to tell ftrace what to do
2608 * If an arch needs to fall back to the stop machine method, the
2609 * it can call this function.
2611 void ftrace_run_stop_machine(int command)
2613 stop_machine(__ftrace_modify_code, &command, NULL);
2617 * arch_ftrace_update_code, modify the code to trace or not trace
2618 * @command: The command that needs to be done
2620 * Archs can override this function if it does not need to
2621 * run stop_machine() to modify code.
2623 void __weak arch_ftrace_update_code(int command)
2625 ftrace_run_stop_machine(command);
2628 static void ftrace_run_update_code(int command)
2632 ret = ftrace_arch_code_modify_prepare();
2633 FTRACE_WARN_ON(ret);
2638 * By default we use stop_machine() to modify the code.
2639 * But archs can do what ever they want as long as it
2640 * is safe. The stop_machine() is the safest, but also
2641 * produces the most overhead.
2643 arch_ftrace_update_code(command);
2645 ret = ftrace_arch_code_modify_post_process();
2646 FTRACE_WARN_ON(ret);
2649 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2650 struct ftrace_ops_hash *old_hash)
2652 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2653 ops->old_hash.filter_hash = old_hash->filter_hash;
2654 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2655 ftrace_run_update_code(command);
2656 ops->old_hash.filter_hash = NULL;
2657 ops->old_hash.notrace_hash = NULL;
2658 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2661 static ftrace_func_t saved_ftrace_func;
2662 static int ftrace_start_up;
2664 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2668 static void ftrace_startup_enable(int command)
2670 if (saved_ftrace_func != ftrace_trace_function) {
2671 saved_ftrace_func = ftrace_trace_function;
2672 command |= FTRACE_UPDATE_TRACE_FUNC;
2675 if (!command || !ftrace_enabled)
2678 ftrace_run_update_code(command);
2681 static void ftrace_startup_all(int command)
2683 update_all_ops = true;
2684 ftrace_startup_enable(command);
2685 update_all_ops = false;
2688 static int ftrace_startup(struct ftrace_ops *ops, int command)
2692 if (unlikely(ftrace_disabled))
2695 ret = __register_ftrace_function(ops);
2702 * Note that ftrace probes uses this to start up
2703 * and modify functions it will probe. But we still
2704 * set the ADDING flag for modification, as probes
2705 * do not have trampolines. If they add them in the
2706 * future, then the probes will need to distinguish
2707 * between adding and updating probes.
2709 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2711 ret = ftrace_hash_ipmodify_enable(ops);
2713 /* Rollback registration process */
2714 __unregister_ftrace_function(ops);
2716 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2720 if (ftrace_hash_rec_enable(ops, 1))
2721 command |= FTRACE_UPDATE_CALLS;
2723 ftrace_startup_enable(command);
2725 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2730 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2734 if (unlikely(ftrace_disabled))
2737 ret = __unregister_ftrace_function(ops);
2743 * Just warn in case of unbalance, no need to kill ftrace, it's not
2744 * critical but the ftrace_call callers may be never nopped again after
2745 * further ftrace uses.
2747 WARN_ON_ONCE(ftrace_start_up < 0);
2749 /* Disabling ipmodify never fails */
2750 ftrace_hash_ipmodify_disable(ops);
2752 if (ftrace_hash_rec_disable(ops, 1))
2753 command |= FTRACE_UPDATE_CALLS;
2755 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2757 if (saved_ftrace_func != ftrace_trace_function) {
2758 saved_ftrace_func = ftrace_trace_function;
2759 command |= FTRACE_UPDATE_TRACE_FUNC;
2762 if (!command || !ftrace_enabled) {
2764 * If these are dynamic or per_cpu ops, they still
2765 * need their data freed. Since, function tracing is
2766 * not currently active, we can just free them
2767 * without synchronizing all CPUs.
2769 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2776 * If the ops uses a trampoline, then it needs to be
2777 * tested first on update.
2779 ops->flags |= FTRACE_OPS_FL_REMOVING;
2782 /* The trampoline logic checks the old hashes */
2783 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2784 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2786 ftrace_run_update_code(command);
2789 * If there's no more ops registered with ftrace, run a
2790 * sanity check to make sure all rec flags are cleared.
2792 if (rcu_dereference_protected(ftrace_ops_list,
2793 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2794 struct ftrace_page *pg;
2795 struct dyn_ftrace *rec;
2797 do_for_each_ftrace_rec(pg, rec) {
2798 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2799 pr_warn(" %pS flags:%lx\n",
2800 (void *)rec->ip, rec->flags);
2801 } while_for_each_ftrace_rec();
2804 ops->old_hash.filter_hash = NULL;
2805 ops->old_hash.notrace_hash = NULL;
2808 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2811 * Dynamic ops may be freed, we must make sure that all
2812 * callers are done before leaving this function.
2813 * The same goes for freeing the per_cpu data of the per_cpu
2816 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
2818 * We need to do a hard force of sched synchronization.
2819 * This is because we use preempt_disable() to do RCU, but
2820 * the function tracers can be called where RCU is not watching
2821 * (like before user_exit()). We can not rely on the RCU
2822 * infrastructure to do the synchronization, thus we must do it
2825 schedule_on_each_cpu(ftrace_sync);
2828 * When the kernel is preeptive, tasks can be preempted
2829 * while on a ftrace trampoline. Just scheduling a task on
2830 * a CPU is not good enough to flush them. Calling
2831 * synchornize_rcu_tasks() will wait for those tasks to
2832 * execute and either schedule voluntarily or enter user space.
2834 if (IS_ENABLED(CONFIG_PREEMPT))
2835 synchronize_rcu_tasks();
2838 arch_ftrace_trampoline_free(ops);
2844 static void ftrace_startup_sysctl(void)
2848 if (unlikely(ftrace_disabled))
2851 /* Force update next time */
2852 saved_ftrace_func = NULL;
2853 /* ftrace_start_up is true if we want ftrace running */
2854 if (ftrace_start_up) {
2855 command = FTRACE_UPDATE_CALLS;
2856 if (ftrace_graph_active)
2857 command |= FTRACE_START_FUNC_RET;
2858 ftrace_startup_enable(command);
2862 static void ftrace_shutdown_sysctl(void)
2866 if (unlikely(ftrace_disabled))
2869 /* ftrace_start_up is true if ftrace is running */
2870 if (ftrace_start_up) {
2871 command = FTRACE_DISABLE_CALLS;
2872 if (ftrace_graph_active)
2873 command |= FTRACE_STOP_FUNC_RET;
2874 ftrace_run_update_code(command);
2878 static u64 ftrace_update_time;
2879 unsigned long ftrace_update_tot_cnt;
2881 static inline int ops_traces_mod(struct ftrace_ops *ops)
2884 * Filter_hash being empty will default to trace module.
2885 * But notrace hash requires a test of individual module functions.
2887 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2888 ftrace_hash_empty(ops->func_hash->notrace_hash);
2892 * Check if the current ops references the record.
2894 * If the ops traces all functions, then it was already accounted for.
2895 * If the ops does not trace the current record function, skip it.
2896 * If the ops ignores the function via notrace filter, skip it.
2899 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2901 /* If ops isn't enabled, ignore it */
2902 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2905 /* If ops traces all then it includes this function */
2906 if (ops_traces_mod(ops))
2909 /* The function must be in the filter */
2910 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2911 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2914 /* If in notrace hash, we ignore it too */
2915 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2921 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2923 struct ftrace_page *pg;
2924 struct dyn_ftrace *p;
2926 unsigned long update_cnt = 0;
2927 unsigned long rec_flags = 0;
2930 start = ftrace_now(raw_smp_processor_id());
2933 * When a module is loaded, this function is called to convert
2934 * the calls to mcount in its text to nops, and also to create
2935 * an entry in the ftrace data. Now, if ftrace is activated
2936 * after this call, but before the module sets its text to
2937 * read-only, the modification of enabling ftrace can fail if
2938 * the read-only is done while ftrace is converting the calls.
2939 * To prevent this, the module's records are set as disabled
2940 * and will be enabled after the call to set the module's text
2944 rec_flags |= FTRACE_FL_DISABLED;
2946 for (pg = new_pgs; pg; pg = pg->next) {
2948 for (i = 0; i < pg->index; i++) {
2950 /* If something went wrong, bail without enabling anything */
2951 if (unlikely(ftrace_disabled))
2954 p = &pg->records[i];
2955 p->flags = rec_flags;
2958 * Do the initial record conversion from mcount jump
2959 * to the NOP instructions.
2961 if (!__is_defined(CC_USING_NOP_MCOUNT) &&
2962 !ftrace_code_disable(mod, p))
2969 stop = ftrace_now(raw_smp_processor_id());
2970 ftrace_update_time = stop - start;
2971 ftrace_update_tot_cnt += update_cnt;
2976 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2981 if (WARN_ON(!count))
2984 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2987 * We want to fill as much as possible. No more than a page
2990 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2994 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2997 /* if we can't allocate this size, try something smaller */
3004 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3013 static struct ftrace_page *
3014 ftrace_allocate_pages(unsigned long num_to_init)
3016 struct ftrace_page *start_pg;
3017 struct ftrace_page *pg;
3024 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3029 * Try to allocate as much as possible in one continues
3030 * location that fills in all of the space. We want to
3031 * waste as little space as possible.
3034 cnt = ftrace_allocate_records(pg, num_to_init);
3042 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3054 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3055 free_pages((unsigned long)pg->records, order);
3056 start_pg = pg->next;
3060 pr_info("ftrace: FAILED to allocate memory for functions\n");
3064 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3066 struct ftrace_iterator {
3070 struct ftrace_page *pg;
3071 struct dyn_ftrace *func;
3072 struct ftrace_func_probe *probe;
3073 struct ftrace_func_entry *probe_entry;
3074 struct trace_parser parser;
3075 struct ftrace_hash *hash;
3076 struct ftrace_ops *ops;
3077 struct trace_array *tr;
3078 struct list_head *mod_list;
3085 t_probe_next(struct seq_file *m, loff_t *pos)
3087 struct ftrace_iterator *iter = m->private;
3088 struct trace_array *tr = iter->ops->private;
3089 struct list_head *func_probes;
3090 struct ftrace_hash *hash;
3091 struct list_head *next;
3092 struct hlist_node *hnd = NULL;
3093 struct hlist_head *hhd;
3102 func_probes = &tr->func_probes;
3103 if (list_empty(func_probes))
3107 next = func_probes->next;
3108 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3111 if (iter->probe_entry)
3112 hnd = &iter->probe_entry->hlist;
3114 hash = iter->probe->ops.func_hash->filter_hash;
3117 * A probe being registered may temporarily have an empty hash
3118 * and it's at the end of the func_probes list.
3120 if (!hash || hash == EMPTY_HASH)
3123 size = 1 << hash->size_bits;
3126 if (iter->pidx >= size) {
3127 if (iter->probe->list.next == func_probes)
3129 next = iter->probe->list.next;
3130 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3131 hash = iter->probe->ops.func_hash->filter_hash;
3132 size = 1 << hash->size_bits;
3136 hhd = &hash->buckets[iter->pidx];
3138 if (hlist_empty(hhd)) {
3154 if (WARN_ON_ONCE(!hnd))
3157 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3162 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3164 struct ftrace_iterator *iter = m->private;
3168 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3171 if (iter->mod_pos > *pos)
3175 iter->probe_entry = NULL;
3177 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3178 p = t_probe_next(m, &l);
3185 /* Only set this if we have an item */
3186 iter->flags |= FTRACE_ITER_PROBE;
3192 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3194 struct ftrace_func_entry *probe_entry;
3195 struct ftrace_probe_ops *probe_ops;
3196 struct ftrace_func_probe *probe;
3198 probe = iter->probe;
3199 probe_entry = iter->probe_entry;
3201 if (WARN_ON_ONCE(!probe || !probe_entry))
3204 probe_ops = probe->probe_ops;
3206 if (probe_ops->print)
3207 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3209 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3210 (void *)probe_ops->func);
3216 t_mod_next(struct seq_file *m, loff_t *pos)
3218 struct ftrace_iterator *iter = m->private;
3219 struct trace_array *tr = iter->tr;
3224 iter->mod_list = iter->mod_list->next;
3226 if (iter->mod_list == &tr->mod_trace ||
3227 iter->mod_list == &tr->mod_notrace) {
3228 iter->flags &= ~FTRACE_ITER_MOD;
3232 iter->mod_pos = *pos;
3237 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3239 struct ftrace_iterator *iter = m->private;
3243 if (iter->func_pos > *pos)
3246 iter->mod_pos = iter->func_pos;
3248 /* probes are only available if tr is set */
3252 for (l = 0; l <= (*pos - iter->func_pos); ) {
3253 p = t_mod_next(m, &l);
3258 iter->flags &= ~FTRACE_ITER_MOD;
3259 return t_probe_start(m, pos);
3262 /* Only set this if we have an item */
3263 iter->flags |= FTRACE_ITER_MOD;
3269 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3271 struct ftrace_mod_load *ftrace_mod;
3272 struct trace_array *tr = iter->tr;
3274 if (WARN_ON_ONCE(!iter->mod_list) ||
3275 iter->mod_list == &tr->mod_trace ||
3276 iter->mod_list == &tr->mod_notrace)
3279 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3281 if (ftrace_mod->func)
3282 seq_printf(m, "%s", ftrace_mod->func);
3286 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3292 t_func_next(struct seq_file *m, loff_t *pos)
3294 struct ftrace_iterator *iter = m->private;
3295 struct dyn_ftrace *rec = NULL;
3300 if (iter->idx >= iter->pg->index) {
3301 if (iter->pg->next) {
3302 iter->pg = iter->pg->next;
3307 rec = &iter->pg->records[iter->idx++];
3308 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3309 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3311 ((iter->flags & FTRACE_ITER_ENABLED) &&
3312 !(rec->flags & FTRACE_FL_ENABLED))) {
3322 iter->pos = iter->func_pos = *pos;
3329 t_next(struct seq_file *m, void *v, loff_t *pos)
3331 struct ftrace_iterator *iter = m->private;
3332 loff_t l = *pos; /* t_probe_start() must use original pos */
3335 if (unlikely(ftrace_disabled))
3338 if (iter->flags & FTRACE_ITER_PROBE)
3339 return t_probe_next(m, pos);
3341 if (iter->flags & FTRACE_ITER_MOD)
3342 return t_mod_next(m, pos);
3344 if (iter->flags & FTRACE_ITER_PRINTALL) {
3345 /* next must increment pos, and t_probe_start does not */
3347 return t_mod_start(m, &l);
3350 ret = t_func_next(m, pos);
3353 return t_mod_start(m, &l);
3358 static void reset_iter_read(struct ftrace_iterator *iter)
3362 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3365 static void *t_start(struct seq_file *m, loff_t *pos)
3367 struct ftrace_iterator *iter = m->private;
3371 mutex_lock(&ftrace_lock);
3373 if (unlikely(ftrace_disabled))
3377 * If an lseek was done, then reset and start from beginning.
3379 if (*pos < iter->pos)
3380 reset_iter_read(iter);
3383 * For set_ftrace_filter reading, if we have the filter
3384 * off, we can short cut and just print out that all
3385 * functions are enabled.
3387 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3388 ftrace_hash_empty(iter->hash)) {
3389 iter->func_pos = 1; /* Account for the message */
3391 return t_mod_start(m, pos);
3392 iter->flags |= FTRACE_ITER_PRINTALL;
3393 /* reset in case of seek/pread */
3394 iter->flags &= ~FTRACE_ITER_PROBE;
3398 if (iter->flags & FTRACE_ITER_MOD)
3399 return t_mod_start(m, pos);
3402 * Unfortunately, we need to restart at ftrace_pages_start
3403 * every time we let go of the ftrace_mutex. This is because
3404 * those pointers can change without the lock.
3406 iter->pg = ftrace_pages_start;
3408 for (l = 0; l <= *pos; ) {
3409 p = t_func_next(m, &l);
3415 return t_mod_start(m, pos);
3420 static void t_stop(struct seq_file *m, void *p)
3422 mutex_unlock(&ftrace_lock);
3426 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3431 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3432 struct dyn_ftrace *rec)
3436 ptr = arch_ftrace_trampoline_func(ops, rec);
3438 seq_printf(m, " ->%pS", ptr);
3441 static int t_show(struct seq_file *m, void *v)
3443 struct ftrace_iterator *iter = m->private;
3444 struct dyn_ftrace *rec;
3446 if (iter->flags & FTRACE_ITER_PROBE)
3447 return t_probe_show(m, iter);
3449 if (iter->flags & FTRACE_ITER_MOD)
3450 return t_mod_show(m, iter);
3452 if (iter->flags & FTRACE_ITER_PRINTALL) {
3453 if (iter->flags & FTRACE_ITER_NOTRACE)
3454 seq_puts(m, "#### no functions disabled ####\n");
3456 seq_puts(m, "#### all functions enabled ####\n");
3465 seq_printf(m, "%ps", (void *)rec->ip);
3466 if (iter->flags & FTRACE_ITER_ENABLED) {
3467 struct ftrace_ops *ops;
3469 seq_printf(m, " (%ld)%s%s",
3470 ftrace_rec_count(rec),
3471 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3472 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3473 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3474 ops = ftrace_find_tramp_ops_any(rec);
3477 seq_printf(m, "\ttramp: %pS (%pS)",
3478 (void *)ops->trampoline,
3480 add_trampoline_func(m, ops, rec);
3481 ops = ftrace_find_tramp_ops_next(rec, ops);
3484 seq_puts(m, "\ttramp: ERROR!");
3486 add_trampoline_func(m, NULL, rec);
3495 static const struct seq_operations show_ftrace_seq_ops = {
3503 ftrace_avail_open(struct inode *inode, struct file *file)
3505 struct ftrace_iterator *iter;
3507 if (unlikely(ftrace_disabled))
3510 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3514 iter->pg = ftrace_pages_start;
3515 iter->ops = &global_ops;
3521 ftrace_enabled_open(struct inode *inode, struct file *file)
3523 struct ftrace_iterator *iter;
3525 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3529 iter->pg = ftrace_pages_start;
3530 iter->flags = FTRACE_ITER_ENABLED;
3531 iter->ops = &global_ops;
3537 * ftrace_regex_open - initialize function tracer filter files
3538 * @ops: The ftrace_ops that hold the hash filters
3539 * @flag: The type of filter to process
3540 * @inode: The inode, usually passed in to your open routine
3541 * @file: The file, usually passed in to your open routine
3543 * ftrace_regex_open() initializes the filter files for the
3544 * @ops. Depending on @flag it may process the filter hash or
3545 * the notrace hash of @ops. With this called from the open
3546 * routine, you can use ftrace_filter_write() for the write
3547 * routine if @flag has FTRACE_ITER_FILTER set, or
3548 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3549 * tracing_lseek() should be used as the lseek routine, and
3550 * release must call ftrace_regex_release().
3553 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3554 struct inode *inode, struct file *file)
3556 struct ftrace_iterator *iter;
3557 struct ftrace_hash *hash;
3558 struct list_head *mod_head;
3559 struct trace_array *tr = ops->private;
3562 ftrace_ops_init(ops);
3564 if (unlikely(ftrace_disabled))
3567 if (tr && trace_array_get(tr) < 0)
3570 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3574 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3581 mutex_lock(&ops->func_hash->regex_lock);
3583 if (flag & FTRACE_ITER_NOTRACE) {
3584 hash = ops->func_hash->notrace_hash;
3585 mod_head = tr ? &tr->mod_notrace : NULL;
3587 hash = ops->func_hash->filter_hash;
3588 mod_head = tr ? &tr->mod_trace : NULL;
3591 iter->mod_list = mod_head;
3593 if (file->f_mode & FMODE_WRITE) {
3594 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3596 if (file->f_flags & O_TRUNC) {
3597 iter->hash = alloc_ftrace_hash(size_bits);
3598 clear_ftrace_mod_list(mod_head);
3600 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3604 trace_parser_put(&iter->parser);
3612 if (file->f_mode & FMODE_READ) {
3613 iter->pg = ftrace_pages_start;
3615 ret = seq_open(file, &show_ftrace_seq_ops);
3617 struct seq_file *m = file->private_data;
3621 free_ftrace_hash(iter->hash);
3622 trace_parser_put(&iter->parser);
3625 file->private_data = iter;
3628 mutex_unlock(&ops->func_hash->regex_lock);
3634 trace_array_put(tr);
3641 ftrace_filter_open(struct inode *inode, struct file *file)
3643 struct ftrace_ops *ops = inode->i_private;
3645 return ftrace_regex_open(ops,
3646 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3651 ftrace_notrace_open(struct inode *inode, struct file *file)
3653 struct ftrace_ops *ops = inode->i_private;
3655 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3659 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3660 struct ftrace_glob {
3667 * If symbols in an architecture don't correspond exactly to the user-visible
3668 * name of what they represent, it is possible to define this function to
3669 * perform the necessary adjustments.
3671 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3676 static int ftrace_match(char *str, struct ftrace_glob *g)
3681 str = arch_ftrace_match_adjust(str, g->search);
3685 if (strcmp(str, g->search) == 0)
3688 case MATCH_FRONT_ONLY:
3689 if (strncmp(str, g->search, g->len) == 0)
3692 case MATCH_MIDDLE_ONLY:
3693 if (strstr(str, g->search))
3696 case MATCH_END_ONLY:
3698 if (slen >= g->len &&
3699 memcmp(str + slen - g->len, g->search, g->len) == 0)
3703 if (glob_match(g->search, str))
3712 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3714 struct ftrace_func_entry *entry;
3717 entry = ftrace_lookup_ip(hash, rec->ip);
3719 /* Do nothing if it doesn't exist */
3723 free_hash_entry(hash, entry);
3725 /* Do nothing if it exists */
3729 ret = add_hash_entry(hash, rec->ip);
3735 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3736 struct ftrace_glob *mod_g, int exclude_mod)
3738 char str[KSYM_SYMBOL_LEN];
3741 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3744 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3746 /* blank module name to match all modules */
3748 /* blank module globbing: modname xor exclude_mod */
3749 if (!exclude_mod != !modname)
3755 * exclude_mod is set to trace everything but the given
3756 * module. If it is set and the module matches, then
3757 * return 0. If it is not set, and the module doesn't match
3758 * also return 0. Otherwise, check the function to see if
3761 if (!mod_matches == !exclude_mod)
3764 /* blank search means to match all funcs in the mod */
3769 return ftrace_match(str, func_g);
3773 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3775 struct ftrace_page *pg;
3776 struct dyn_ftrace *rec;
3777 struct ftrace_glob func_g = { .type = MATCH_FULL };
3778 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3779 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3780 int exclude_mod = 0;
3783 int clear_filter = 0;
3786 func_g.type = filter_parse_regex(func, len, &func_g.search,
3788 func_g.len = strlen(func_g.search);
3792 mod_g.type = filter_parse_regex(mod, strlen(mod),
3793 &mod_g.search, &exclude_mod);
3794 mod_g.len = strlen(mod_g.search);
3797 mutex_lock(&ftrace_lock);
3799 if (unlikely(ftrace_disabled))
3802 do_for_each_ftrace_rec(pg, rec) {
3804 if (rec->flags & FTRACE_FL_DISABLED)
3807 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3808 ret = enter_record(hash, rec, clear_filter);
3815 } while_for_each_ftrace_rec();
3817 mutex_unlock(&ftrace_lock);
3823 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3825 return match_records(hash, buff, len, NULL);
3828 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3829 struct ftrace_ops_hash *old_hash)
3831 struct ftrace_ops *op;
3833 if (!ftrace_enabled)
3836 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3837 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3842 * If this is the shared global_ops filter, then we need to
3843 * check if there is another ops that shares it, is enabled.
3844 * If so, we still need to run the modify code.
3846 if (ops->func_hash != &global_ops.local_hash)
3849 do_for_each_ftrace_op(op, ftrace_ops_list) {
3850 if (op->func_hash == &global_ops.local_hash &&
3851 op->flags & FTRACE_OPS_FL_ENABLED) {
3852 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3853 /* Only need to do this once */
3856 } while_for_each_ftrace_op(op);
3859 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3860 struct ftrace_hash **orig_hash,
3861 struct ftrace_hash *hash,
3864 struct ftrace_ops_hash old_hash_ops;
3865 struct ftrace_hash *old_hash;
3868 old_hash = *orig_hash;
3869 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3870 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3871 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3873 ftrace_ops_update_code(ops, &old_hash_ops);
3874 free_ftrace_hash_rcu(old_hash);
3879 static bool module_exists(const char *module)
3881 /* All modules have the symbol __this_module */
3882 const char this_mod[] = "__this_module";
3883 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
3887 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
3889 if (n > sizeof(modname) - 1)
3892 val = module_kallsyms_lookup_name(modname);
3896 static int cache_mod(struct trace_array *tr,
3897 const char *func, char *module, int enable)
3899 struct ftrace_mod_load *ftrace_mod, *n;
3900 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3903 mutex_lock(&ftrace_lock);
3905 /* We do not cache inverse filters */
3906 if (func[0] == '!') {
3910 /* Look to remove this hash */
3911 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3912 if (strcmp(ftrace_mod->module, module) != 0)
3915 /* no func matches all */
3916 if (strcmp(func, "*") == 0 ||
3917 (ftrace_mod->func &&
3918 strcmp(ftrace_mod->func, func) == 0)) {
3920 free_ftrace_mod(ftrace_mod);
3928 /* We only care about modules that have not been loaded yet */
3929 if (module_exists(module))
3932 /* Save this string off, and execute it when the module is loaded */
3933 ret = ftrace_add_mod(tr, func, module, enable);
3935 mutex_unlock(&ftrace_lock);
3941 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3942 int reset, int enable);
3944 #ifdef CONFIG_MODULES
3945 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3946 char *mod, bool enable)
3948 struct ftrace_mod_load *ftrace_mod, *n;
3949 struct ftrace_hash **orig_hash, *new_hash;
3950 LIST_HEAD(process_mods);
3954 mutex_lock(&ops->func_hash->regex_lock);
3957 orig_hash = &ops->func_hash->filter_hash;
3959 orig_hash = &ops->func_hash->notrace_hash;
3961 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
3964 goto out; /* warn? */
3966 mutex_lock(&ftrace_lock);
3968 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3970 if (strcmp(ftrace_mod->module, mod) != 0)
3973 if (ftrace_mod->func)
3974 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
3976 func = kstrdup("*", GFP_KERNEL);
3978 if (!func) /* warn? */
3981 list_del(&ftrace_mod->list);
3982 list_add(&ftrace_mod->list, &process_mods);
3984 /* Use the newly allocated func, as it may be "*" */
3985 kfree(ftrace_mod->func);
3986 ftrace_mod->func = func;
3989 mutex_unlock(&ftrace_lock);
3991 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
3993 func = ftrace_mod->func;
3995 /* Grabs ftrace_lock, which is why we have this extra step */
3996 match_records(new_hash, func, strlen(func), mod);
3997 free_ftrace_mod(ftrace_mod);
4000 if (enable && list_empty(head))
4001 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4003 mutex_lock(&ftrace_lock);
4005 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4007 mutex_unlock(&ftrace_lock);
4010 mutex_unlock(&ops->func_hash->regex_lock);
4012 free_ftrace_hash(new_hash);
4015 static void process_cached_mods(const char *mod_name)
4017 struct trace_array *tr;
4020 mod = kstrdup(mod_name, GFP_KERNEL);
4024 mutex_lock(&trace_types_lock);
4025 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4026 if (!list_empty(&tr->mod_trace))
4027 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4028 if (!list_empty(&tr->mod_notrace))
4029 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4031 mutex_unlock(&trace_types_lock);
4038 * We register the module command as a template to show others how
4039 * to register the a command as well.
4043 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4044 char *func_orig, char *cmd, char *module, int enable)
4049 /* match_records() modifies func, and we need the original */
4050 func = kstrdup(func_orig, GFP_KERNEL);
4055 * cmd == 'mod' because we only registered this func
4056 * for the 'mod' ftrace_func_command.
4057 * But if you register one func with multiple commands,
4058 * you can tell which command was used by the cmd
4061 ret = match_records(hash, func, strlen(func), module);
4065 return cache_mod(tr, func_orig, module, enable);
4071 static struct ftrace_func_command ftrace_mod_cmd = {
4073 .func = ftrace_mod_callback,
4076 static int __init ftrace_mod_cmd_init(void)
4078 return register_ftrace_command(&ftrace_mod_cmd);
4080 core_initcall(ftrace_mod_cmd_init);
4082 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4083 struct ftrace_ops *op, struct pt_regs *pt_regs)
4085 struct ftrace_probe_ops *probe_ops;
4086 struct ftrace_func_probe *probe;
4088 probe = container_of(op, struct ftrace_func_probe, ops);
4089 probe_ops = probe->probe_ops;
4092 * Disable preemption for these calls to prevent a RCU grace
4093 * period. This syncs the hash iteration and freeing of items
4094 * on the hash. rcu_read_lock is too dangerous here.
4096 preempt_disable_notrace();
4097 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4098 preempt_enable_notrace();
4101 struct ftrace_func_map {
4102 struct ftrace_func_entry entry;
4106 struct ftrace_func_mapper {
4107 struct ftrace_hash hash;
4111 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4113 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4115 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4117 struct ftrace_hash *hash;
4120 * The mapper is simply a ftrace_hash, but since the entries
4121 * in the hash are not ftrace_func_entry type, we define it
4122 * as a separate structure.
4124 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4125 return (struct ftrace_func_mapper *)hash;
4129 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4130 * @mapper: The mapper that has the ip maps
4131 * @ip: the instruction pointer to find the data for
4133 * Returns the data mapped to @ip if found otherwise NULL. The return
4134 * is actually the address of the mapper data pointer. The address is
4135 * returned for use cases where the data is no bigger than a long, and
4136 * the user can use the data pointer as its data instead of having to
4137 * allocate more memory for the reference.
4139 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4142 struct ftrace_func_entry *entry;
4143 struct ftrace_func_map *map;
4145 entry = ftrace_lookup_ip(&mapper->hash, ip);
4149 map = (struct ftrace_func_map *)entry;
4154 * ftrace_func_mapper_add_ip - Map some data to an ip
4155 * @mapper: The mapper that has the ip maps
4156 * @ip: The instruction pointer address to map @data to
4157 * @data: The data to map to @ip
4159 * Returns 0 on succes otherwise an error.
4161 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4162 unsigned long ip, void *data)
4164 struct ftrace_func_entry *entry;
4165 struct ftrace_func_map *map;
4167 entry = ftrace_lookup_ip(&mapper->hash, ip);
4171 map = kmalloc(sizeof(*map), GFP_KERNEL);
4178 __add_hash_entry(&mapper->hash, &map->entry);
4184 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4185 * @mapper: The mapper that has the ip maps
4186 * @ip: The instruction pointer address to remove the data from
4188 * Returns the data if it is found, otherwise NULL.
4189 * Note, if the data pointer is used as the data itself, (see
4190 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4191 * if the data pointer was set to zero.
4193 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4196 struct ftrace_func_entry *entry;
4197 struct ftrace_func_map *map;
4200 entry = ftrace_lookup_ip(&mapper->hash, ip);
4204 map = (struct ftrace_func_map *)entry;
4207 remove_hash_entry(&mapper->hash, entry);
4214 * free_ftrace_func_mapper - free a mapping of ips and data
4215 * @mapper: The mapper that has the ip maps
4216 * @free_func: A function to be called on each data item.
4218 * This is used to free the function mapper. The @free_func is optional
4219 * and can be used if the data needs to be freed as well.
4221 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4222 ftrace_mapper_func free_func)
4224 struct ftrace_func_entry *entry;
4225 struct ftrace_func_map *map;
4226 struct hlist_head *hhd;
4232 if (free_func && mapper->hash.count) {
4233 size = 1 << mapper->hash.size_bits;
4234 for (i = 0; i < size; i++) {
4235 hhd = &mapper->hash.buckets[i];
4236 hlist_for_each_entry(entry, hhd, hlist) {
4237 map = (struct ftrace_func_map *)entry;
4242 free_ftrace_hash(&mapper->hash);
4245 static void release_probe(struct ftrace_func_probe *probe)
4247 struct ftrace_probe_ops *probe_ops;
4249 mutex_lock(&ftrace_lock);
4251 WARN_ON(probe->ref <= 0);
4253 /* Subtract the ref that was used to protect this instance */
4257 probe_ops = probe->probe_ops;
4259 * Sending zero as ip tells probe_ops to free
4260 * the probe->data itself
4262 if (probe_ops->free)
4263 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4264 list_del(&probe->list);
4267 mutex_unlock(&ftrace_lock);
4270 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4273 * Add one ref to keep it from being freed when releasing the
4274 * ftrace_lock mutex.
4280 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4281 struct ftrace_probe_ops *probe_ops,
4284 struct ftrace_func_entry *entry;
4285 struct ftrace_func_probe *probe;
4286 struct ftrace_hash **orig_hash;
4287 struct ftrace_hash *old_hash;
4288 struct ftrace_hash *hash;
4297 /* We do not support '!' for function probes */
4298 if (WARN_ON(glob[0] == '!'))
4302 mutex_lock(&ftrace_lock);
4303 /* Check if the probe_ops is already registered */
4304 list_for_each_entry(probe, &tr->func_probes, list) {
4305 if (probe->probe_ops == probe_ops)
4308 if (&probe->list == &tr->func_probes) {
4309 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4311 mutex_unlock(&ftrace_lock);
4314 probe->probe_ops = probe_ops;
4315 probe->ops.func = function_trace_probe_call;
4317 ftrace_ops_init(&probe->ops);
4318 list_add(&probe->list, &tr->func_probes);
4321 acquire_probe_locked(probe);
4323 mutex_unlock(&ftrace_lock);
4326 * Note, there's a small window here that the func_hash->filter_hash
4327 * may be NULL or empty. Need to be carefule when reading the loop.
4329 mutex_lock(&probe->ops.func_hash->regex_lock);
4331 orig_hash = &probe->ops.func_hash->filter_hash;
4332 old_hash = *orig_hash;
4333 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4340 ret = ftrace_match_records(hash, glob, strlen(glob));
4342 /* Nothing found? */
4349 size = 1 << hash->size_bits;
4350 for (i = 0; i < size; i++) {
4351 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4352 if (ftrace_lookup_ip(old_hash, entry->ip))
4355 * The caller might want to do something special
4356 * for each function we find. We call the callback
4357 * to give the caller an opportunity to do so.
4359 if (probe_ops->init) {
4360 ret = probe_ops->init(probe_ops, tr,
4364 if (probe_ops->free && count)
4365 probe_ops->free(probe_ops, tr,
4375 mutex_lock(&ftrace_lock);
4378 /* Nothing was added? */
4383 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4388 /* One ref for each new function traced */
4389 probe->ref += count;
4391 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4392 ret = ftrace_startup(&probe->ops, 0);
4395 mutex_unlock(&ftrace_lock);
4400 mutex_unlock(&probe->ops.func_hash->regex_lock);
4401 free_ftrace_hash(hash);
4403 release_probe(probe);
4408 if (!probe_ops->free || !count)
4411 /* Failed to do the move, need to call the free functions */
4412 for (i = 0; i < size; i++) {
4413 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4414 if (ftrace_lookup_ip(old_hash, entry->ip))
4416 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4423 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4424 struct ftrace_probe_ops *probe_ops)
4426 struct ftrace_ops_hash old_hash_ops;
4427 struct ftrace_func_entry *entry;
4428 struct ftrace_func_probe *probe;
4429 struct ftrace_glob func_g;
4430 struct ftrace_hash **orig_hash;
4431 struct ftrace_hash *old_hash;
4432 struct ftrace_hash *hash = NULL;
4433 struct hlist_node *tmp;
4434 struct hlist_head hhd;
4435 char str[KSYM_SYMBOL_LEN];
4437 int i, ret = -ENODEV;
4440 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4441 func_g.search = NULL;
4445 func_g.type = filter_parse_regex(glob, strlen(glob),
4446 &func_g.search, ¬);
4447 func_g.len = strlen(func_g.search);
4449 /* we do not support '!' for function probes */
4454 mutex_lock(&ftrace_lock);
4455 /* Check if the probe_ops is already registered */
4456 list_for_each_entry(probe, &tr->func_probes, list) {
4457 if (probe->probe_ops == probe_ops)
4460 if (&probe->list == &tr->func_probes)
4461 goto err_unlock_ftrace;
4464 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4465 goto err_unlock_ftrace;
4467 acquire_probe_locked(probe);
4469 mutex_unlock(&ftrace_lock);
4471 mutex_lock(&probe->ops.func_hash->regex_lock);
4473 orig_hash = &probe->ops.func_hash->filter_hash;
4474 old_hash = *orig_hash;
4476 if (ftrace_hash_empty(old_hash))
4479 old_hash_ops.filter_hash = old_hash;
4480 /* Probes only have filters */
4481 old_hash_ops.notrace_hash = NULL;
4484 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4488 INIT_HLIST_HEAD(&hhd);
4490 size = 1 << hash->size_bits;
4491 for (i = 0; i < size; i++) {
4492 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4494 if (func_g.search) {
4495 kallsyms_lookup(entry->ip, NULL, NULL,
4497 if (!ftrace_match(str, &func_g))
4501 remove_hash_entry(hash, entry);
4502 hlist_add_head(&entry->hlist, &hhd);
4506 /* Nothing found? */
4512 mutex_lock(&ftrace_lock);
4514 WARN_ON(probe->ref < count);
4516 probe->ref -= count;
4518 if (ftrace_hash_empty(hash))
4519 ftrace_shutdown(&probe->ops, 0);
4521 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4524 /* still need to update the function call sites */
4525 if (ftrace_enabled && !ftrace_hash_empty(hash))
4526 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4528 synchronize_sched();
4530 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4531 hlist_del(&entry->hlist);
4532 if (probe_ops->free)
4533 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4536 mutex_unlock(&ftrace_lock);
4539 mutex_unlock(&probe->ops.func_hash->regex_lock);
4540 free_ftrace_hash(hash);
4542 release_probe(probe);
4547 mutex_unlock(&ftrace_lock);
4551 void clear_ftrace_function_probes(struct trace_array *tr)
4553 struct ftrace_func_probe *probe, *n;
4555 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4556 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4559 static LIST_HEAD(ftrace_commands);
4560 static DEFINE_MUTEX(ftrace_cmd_mutex);
4563 * Currently we only register ftrace commands from __init, so mark this
4566 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4568 struct ftrace_func_command *p;
4571 mutex_lock(&ftrace_cmd_mutex);
4572 list_for_each_entry(p, &ftrace_commands, list) {
4573 if (strcmp(cmd->name, p->name) == 0) {
4578 list_add(&cmd->list, &ftrace_commands);
4580 mutex_unlock(&ftrace_cmd_mutex);
4586 * Currently we only unregister ftrace commands from __init, so mark
4589 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4591 struct ftrace_func_command *p, *n;
4594 mutex_lock(&ftrace_cmd_mutex);
4595 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4596 if (strcmp(cmd->name, p->name) == 0) {
4598 list_del_init(&p->list);
4603 mutex_unlock(&ftrace_cmd_mutex);
4608 static int ftrace_process_regex(struct ftrace_iterator *iter,
4609 char *buff, int len, int enable)
4611 struct ftrace_hash *hash = iter->hash;
4612 struct trace_array *tr = iter->ops->private;
4613 char *func, *command, *next = buff;
4614 struct ftrace_func_command *p;
4617 func = strsep(&next, ":");
4620 ret = ftrace_match_records(hash, func, len);
4630 command = strsep(&next, ":");
4632 mutex_lock(&ftrace_cmd_mutex);
4633 list_for_each_entry(p, &ftrace_commands, list) {
4634 if (strcmp(p->name, command) == 0) {
4635 ret = p->func(tr, hash, func, command, next, enable);
4640 mutex_unlock(&ftrace_cmd_mutex);
4646 ftrace_regex_write(struct file *file, const char __user *ubuf,
4647 size_t cnt, loff_t *ppos, int enable)
4649 struct ftrace_iterator *iter;
4650 struct trace_parser *parser;
4656 if (file->f_mode & FMODE_READ) {
4657 struct seq_file *m = file->private_data;
4660 iter = file->private_data;
4662 if (unlikely(ftrace_disabled))
4665 /* iter->hash is a local copy, so we don't need regex_lock */
4667 parser = &iter->parser;
4668 read = trace_get_user(parser, ubuf, cnt, ppos);
4670 if (read >= 0 && trace_parser_loaded(parser) &&
4671 !trace_parser_cont(parser)) {
4672 ret = ftrace_process_regex(iter, parser->buffer,
4673 parser->idx, enable);
4674 trace_parser_clear(parser);
4685 ftrace_filter_write(struct file *file, const char __user *ubuf,
4686 size_t cnt, loff_t *ppos)
4688 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4692 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4693 size_t cnt, loff_t *ppos)
4695 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4699 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4701 struct ftrace_func_entry *entry;
4703 if (!ftrace_location(ip))
4707 entry = ftrace_lookup_ip(hash, ip);
4710 free_hash_entry(hash, entry);
4714 return add_hash_entry(hash, ip);
4718 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4719 unsigned long ip, int remove, int reset, int enable)
4721 struct ftrace_hash **orig_hash;
4722 struct ftrace_hash *hash;
4725 if (unlikely(ftrace_disabled))
4728 mutex_lock(&ops->func_hash->regex_lock);
4731 orig_hash = &ops->func_hash->filter_hash;
4733 orig_hash = &ops->func_hash->notrace_hash;
4736 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4738 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4742 goto out_regex_unlock;
4745 if (buf && !ftrace_match_records(hash, buf, len)) {
4747 goto out_regex_unlock;
4750 ret = ftrace_match_addr(hash, ip, remove);
4752 goto out_regex_unlock;
4755 mutex_lock(&ftrace_lock);
4756 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4757 mutex_unlock(&ftrace_lock);
4760 mutex_unlock(&ops->func_hash->regex_lock);
4762 free_ftrace_hash(hash);
4767 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4768 int reset, int enable)
4770 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4774 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4775 * @ops - the ops to set the filter with
4776 * @ip - the address to add to or remove from the filter.
4777 * @remove - non zero to remove the ip from the filter
4778 * @reset - non zero to reset all filters before applying this filter.
4780 * Filters denote which functions should be enabled when tracing is enabled
4781 * If @ip is NULL, it failes to update filter.
4783 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4784 int remove, int reset)
4786 ftrace_ops_init(ops);
4787 return ftrace_set_addr(ops, ip, remove, reset, 1);
4789 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4792 * ftrace_ops_set_global_filter - setup ops to use global filters
4793 * @ops - the ops which will use the global filters
4795 * ftrace users who need global function trace filtering should call this.
4796 * It can set the global filter only if ops were not initialized before.
4798 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4800 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4803 ftrace_ops_init(ops);
4804 ops->func_hash = &global_ops.local_hash;
4806 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4809 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4810 int reset, int enable)
4812 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4816 * ftrace_set_filter - set a function to filter on in ftrace
4817 * @ops - the ops to set the filter with
4818 * @buf - the string that holds the function filter text.
4819 * @len - the length of the string.
4820 * @reset - non zero to reset all filters before applying this filter.
4822 * Filters denote which functions should be enabled when tracing is enabled.
4823 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4825 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4828 ftrace_ops_init(ops);
4829 return ftrace_set_regex(ops, buf, len, reset, 1);
4831 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4834 * ftrace_set_notrace - set a function to not trace in ftrace
4835 * @ops - the ops to set the notrace filter with
4836 * @buf - the string that holds the function notrace text.
4837 * @len - the length of the string.
4838 * @reset - non zero to reset all filters before applying this filter.
4840 * Notrace Filters denote which functions should not be enabled when tracing
4841 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4844 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4847 ftrace_ops_init(ops);
4848 return ftrace_set_regex(ops, buf, len, reset, 0);
4850 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4852 * ftrace_set_global_filter - set a function to filter on with global tracers
4853 * @buf - the string that holds the function filter text.
4854 * @len - the length of the string.
4855 * @reset - non zero to reset all filters before applying this filter.
4857 * Filters denote which functions should be enabled when tracing is enabled.
4858 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4860 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4862 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4864 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4867 * ftrace_set_global_notrace - set a function to not trace with global tracers
4868 * @buf - the string that holds the function notrace text.
4869 * @len - the length of the string.
4870 * @reset - non zero to reset all filters before applying this filter.
4872 * Notrace Filters denote which functions should not be enabled when tracing
4873 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4876 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4878 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4880 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4883 * command line interface to allow users to set filters on boot up.
4885 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4886 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4887 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4889 /* Used by function selftest to not test if filter is set */
4890 bool ftrace_filter_param __initdata;
4892 static int __init set_ftrace_notrace(char *str)
4894 ftrace_filter_param = true;
4895 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4898 __setup("ftrace_notrace=", set_ftrace_notrace);
4900 static int __init set_ftrace_filter(char *str)
4902 ftrace_filter_param = true;
4903 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4906 __setup("ftrace_filter=", set_ftrace_filter);
4908 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4909 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4910 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4911 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
4913 static int __init set_graph_function(char *str)
4915 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4918 __setup("ftrace_graph_filter=", set_graph_function);
4920 static int __init set_graph_notrace_function(char *str)
4922 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4925 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4927 static int __init set_graph_max_depth_function(char *str)
4931 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4934 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4936 static void __init set_ftrace_early_graph(char *buf, int enable)
4940 struct ftrace_hash *hash;
4942 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4947 func = strsep(&buf, ",");
4948 /* we allow only one expression at a time */
4949 ret = ftrace_graph_set_hash(hash, func);
4951 printk(KERN_DEBUG "ftrace: function %s not "
4952 "traceable\n", func);
4956 ftrace_graph_hash = hash;
4958 ftrace_graph_notrace_hash = hash;
4960 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4963 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4967 ftrace_ops_init(ops);
4970 func = strsep(&buf, ",");
4971 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4975 static void __init set_ftrace_early_filters(void)
4977 if (ftrace_filter_buf[0])
4978 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4979 if (ftrace_notrace_buf[0])
4980 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4981 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4982 if (ftrace_graph_buf[0])
4983 set_ftrace_early_graph(ftrace_graph_buf, 1);
4984 if (ftrace_graph_notrace_buf[0])
4985 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4986 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4989 int ftrace_regex_release(struct inode *inode, struct file *file)
4991 struct seq_file *m = (struct seq_file *)file->private_data;
4992 struct ftrace_iterator *iter;
4993 struct ftrace_hash **orig_hash;
4994 struct trace_parser *parser;
4998 if (file->f_mode & FMODE_READ) {
5000 seq_release(inode, file);
5002 iter = file->private_data;
5004 parser = &iter->parser;
5005 if (trace_parser_loaded(parser)) {
5006 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
5009 trace_parser_put(parser);
5011 mutex_lock(&iter->ops->func_hash->regex_lock);
5013 if (file->f_mode & FMODE_WRITE) {
5014 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5017 orig_hash = &iter->ops->func_hash->filter_hash;
5018 if (iter->tr && !list_empty(&iter->tr->mod_trace))
5019 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5021 orig_hash = &iter->ops->func_hash->notrace_hash;
5023 mutex_lock(&ftrace_lock);
5024 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5025 iter->hash, filter_hash);
5026 mutex_unlock(&ftrace_lock);
5028 /* For read only, the hash is the ops hash */
5032 mutex_unlock(&iter->ops->func_hash->regex_lock);
5033 free_ftrace_hash(iter->hash);
5035 trace_array_put(iter->tr);
5041 static const struct file_operations ftrace_avail_fops = {
5042 .open = ftrace_avail_open,
5044 .llseek = seq_lseek,
5045 .release = seq_release_private,
5048 static const struct file_operations ftrace_enabled_fops = {
5049 .open = ftrace_enabled_open,
5051 .llseek = seq_lseek,
5052 .release = seq_release_private,
5055 static const struct file_operations ftrace_filter_fops = {
5056 .open = ftrace_filter_open,
5058 .write = ftrace_filter_write,
5059 .llseek = tracing_lseek,
5060 .release = ftrace_regex_release,
5063 static const struct file_operations ftrace_notrace_fops = {
5064 .open = ftrace_notrace_open,
5066 .write = ftrace_notrace_write,
5067 .llseek = tracing_lseek,
5068 .release = ftrace_regex_release,
5071 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5073 static DEFINE_MUTEX(graph_lock);
5075 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
5076 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
5078 enum graph_filter_type {
5079 GRAPH_FILTER_NOTRACE = 0,
5080 GRAPH_FILTER_FUNCTION,
5083 #define FTRACE_GRAPH_EMPTY ((void *)1)
5085 struct ftrace_graph_data {
5086 struct ftrace_hash *hash;
5087 struct ftrace_func_entry *entry;
5088 int idx; /* for hash table iteration */
5089 enum graph_filter_type type;
5090 struct ftrace_hash *new_hash;
5091 const struct seq_operations *seq_ops;
5092 struct trace_parser parser;
5096 __g_next(struct seq_file *m, loff_t *pos)
5098 struct ftrace_graph_data *fgd = m->private;
5099 struct ftrace_func_entry *entry = fgd->entry;
5100 struct hlist_head *head;
5101 int i, idx = fgd->idx;
5103 if (*pos >= fgd->hash->count)
5107 hlist_for_each_entry_continue(entry, hlist) {
5115 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5116 head = &fgd->hash->buckets[i];
5117 hlist_for_each_entry(entry, head, hlist) {
5127 g_next(struct seq_file *m, void *v, loff_t *pos)
5130 return __g_next(m, pos);
5133 static void *g_start(struct seq_file *m, loff_t *pos)
5135 struct ftrace_graph_data *fgd = m->private;
5137 mutex_lock(&graph_lock);
5139 if (fgd->type == GRAPH_FILTER_FUNCTION)
5140 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5141 lockdep_is_held(&graph_lock));
5143 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5144 lockdep_is_held(&graph_lock));
5146 /* Nothing, tell g_show to print all functions are enabled */
5147 if (ftrace_hash_empty(fgd->hash) && !*pos)
5148 return FTRACE_GRAPH_EMPTY;
5152 return __g_next(m, pos);
5155 static void g_stop(struct seq_file *m, void *p)
5157 mutex_unlock(&graph_lock);
5160 static int g_show(struct seq_file *m, void *v)
5162 struct ftrace_func_entry *entry = v;
5167 if (entry == FTRACE_GRAPH_EMPTY) {
5168 struct ftrace_graph_data *fgd = m->private;
5170 if (fgd->type == GRAPH_FILTER_FUNCTION)
5171 seq_puts(m, "#### all functions enabled ####\n");
5173 seq_puts(m, "#### no functions disabled ####\n");
5177 seq_printf(m, "%ps\n", (void *)entry->ip);
5182 static const struct seq_operations ftrace_graph_seq_ops = {
5190 __ftrace_graph_open(struct inode *inode, struct file *file,
5191 struct ftrace_graph_data *fgd)
5194 struct ftrace_hash *new_hash = NULL;
5196 if (file->f_mode & FMODE_WRITE) {
5197 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5199 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5202 if (file->f_flags & O_TRUNC)
5203 new_hash = alloc_ftrace_hash(size_bits);
5205 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5213 if (file->f_mode & FMODE_READ) {
5214 ret = seq_open(file, &ftrace_graph_seq_ops);
5216 struct seq_file *m = file->private_data;
5220 free_ftrace_hash(new_hash);
5224 file->private_data = fgd;
5227 if (ret < 0 && file->f_mode & FMODE_WRITE)
5228 trace_parser_put(&fgd->parser);
5230 fgd->new_hash = new_hash;
5233 * All uses of fgd->hash must be taken with the graph_lock
5234 * held. The graph_lock is going to be released, so force
5235 * fgd->hash to be reinitialized when it is taken again.
5243 ftrace_graph_open(struct inode *inode, struct file *file)
5245 struct ftrace_graph_data *fgd;
5248 if (unlikely(ftrace_disabled))
5251 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5255 mutex_lock(&graph_lock);
5257 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5258 lockdep_is_held(&graph_lock));
5259 fgd->type = GRAPH_FILTER_FUNCTION;
5260 fgd->seq_ops = &ftrace_graph_seq_ops;
5262 ret = __ftrace_graph_open(inode, file, fgd);
5266 mutex_unlock(&graph_lock);
5271 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5273 struct ftrace_graph_data *fgd;
5276 if (unlikely(ftrace_disabled))
5279 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5283 mutex_lock(&graph_lock);
5285 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5286 lockdep_is_held(&graph_lock));
5287 fgd->type = GRAPH_FILTER_NOTRACE;
5288 fgd->seq_ops = &ftrace_graph_seq_ops;
5290 ret = __ftrace_graph_open(inode, file, fgd);
5294 mutex_unlock(&graph_lock);
5299 ftrace_graph_release(struct inode *inode, struct file *file)
5301 struct ftrace_graph_data *fgd;
5302 struct ftrace_hash *old_hash, *new_hash;
5303 struct trace_parser *parser;
5306 if (file->f_mode & FMODE_READ) {
5307 struct seq_file *m = file->private_data;
5310 seq_release(inode, file);
5312 fgd = file->private_data;
5316 if (file->f_mode & FMODE_WRITE) {
5318 parser = &fgd->parser;
5320 if (trace_parser_loaded((parser))) {
5321 ret = ftrace_graph_set_hash(fgd->new_hash,
5325 trace_parser_put(parser);
5327 new_hash = __ftrace_hash_move(fgd->new_hash);
5333 mutex_lock(&graph_lock);
5335 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5336 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5337 lockdep_is_held(&graph_lock));
5338 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5340 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5341 lockdep_is_held(&graph_lock));
5342 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5345 mutex_unlock(&graph_lock);
5348 * We need to do a hard force of sched synchronization.
5349 * This is because we use preempt_disable() to do RCU, but
5350 * the function tracers can be called where RCU is not watching
5351 * (like before user_exit()). We can not rely on the RCU
5352 * infrastructure to do the synchronization, thus we must do it
5355 schedule_on_each_cpu(ftrace_sync);
5357 free_ftrace_hash(old_hash);
5361 free_ftrace_hash(fgd->new_hash);
5368 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5370 struct ftrace_glob func_g;
5371 struct dyn_ftrace *rec;
5372 struct ftrace_page *pg;
5373 struct ftrace_func_entry *entry;
5378 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5379 &func_g.search, ¬);
5381 func_g.len = strlen(func_g.search);
5383 mutex_lock(&ftrace_lock);
5385 if (unlikely(ftrace_disabled)) {
5386 mutex_unlock(&ftrace_lock);
5390 do_for_each_ftrace_rec(pg, rec) {
5392 if (rec->flags & FTRACE_FL_DISABLED)
5395 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5396 entry = ftrace_lookup_ip(hash, rec->ip);
5403 if (add_hash_entry(hash, rec->ip) < 0)
5407 free_hash_entry(hash, entry);
5412 } while_for_each_ftrace_rec();
5414 mutex_unlock(&ftrace_lock);
5423 ftrace_graph_write(struct file *file, const char __user *ubuf,
5424 size_t cnt, loff_t *ppos)
5426 ssize_t read, ret = 0;
5427 struct ftrace_graph_data *fgd = file->private_data;
5428 struct trace_parser *parser;
5433 /* Read mode uses seq functions */
5434 if (file->f_mode & FMODE_READ) {
5435 struct seq_file *m = file->private_data;
5439 parser = &fgd->parser;
5441 read = trace_get_user(parser, ubuf, cnt, ppos);
5443 if (read >= 0 && trace_parser_loaded(parser) &&
5444 !trace_parser_cont(parser)) {
5446 ret = ftrace_graph_set_hash(fgd->new_hash,
5448 trace_parser_clear(parser);
5457 static const struct file_operations ftrace_graph_fops = {
5458 .open = ftrace_graph_open,
5460 .write = ftrace_graph_write,
5461 .llseek = tracing_lseek,
5462 .release = ftrace_graph_release,
5465 static const struct file_operations ftrace_graph_notrace_fops = {
5466 .open = ftrace_graph_notrace_open,
5468 .write = ftrace_graph_write,
5469 .llseek = tracing_lseek,
5470 .release = ftrace_graph_release,
5472 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5474 void ftrace_create_filter_files(struct ftrace_ops *ops,
5475 struct dentry *parent)
5478 trace_create_file("set_ftrace_filter", 0644, parent,
5479 ops, &ftrace_filter_fops);
5481 trace_create_file("set_ftrace_notrace", 0644, parent,
5482 ops, &ftrace_notrace_fops);
5486 * The name "destroy_filter_files" is really a misnomer. Although
5487 * in the future, it may actualy delete the files, but this is
5488 * really intended to make sure the ops passed in are disabled
5489 * and that when this function returns, the caller is free to
5492 * The "destroy" name is only to match the "create" name that this
5493 * should be paired with.
5495 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5497 mutex_lock(&ftrace_lock);
5498 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5499 ftrace_shutdown(ops, 0);
5500 ops->flags |= FTRACE_OPS_FL_DELETED;
5501 ftrace_free_filter(ops);
5502 mutex_unlock(&ftrace_lock);
5505 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
5508 trace_create_file("available_filter_functions", 0444,
5509 d_tracer, NULL, &ftrace_avail_fops);
5511 trace_create_file("enabled_functions", 0444,
5512 d_tracer, NULL, &ftrace_enabled_fops);
5514 ftrace_create_filter_files(&global_ops, d_tracer);
5516 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5517 trace_create_file("set_graph_function", 0644, d_tracer,
5519 &ftrace_graph_fops);
5520 trace_create_file("set_graph_notrace", 0644, d_tracer,
5522 &ftrace_graph_notrace_fops);
5523 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5528 static int ftrace_cmp_ips(const void *a, const void *b)
5530 const unsigned long *ipa = a;
5531 const unsigned long *ipb = b;
5540 static int ftrace_process_locs(struct module *mod,
5541 unsigned long *start,
5544 struct ftrace_page *start_pg;
5545 struct ftrace_page *pg;
5546 struct dyn_ftrace *rec;
5547 unsigned long count;
5550 unsigned long flags = 0; /* Shut up gcc */
5553 count = end - start;
5558 sort(start, count, sizeof(*start),
5559 ftrace_cmp_ips, NULL);
5561 start_pg = ftrace_allocate_pages(count);
5565 mutex_lock(&ftrace_lock);
5568 * Core and each module needs their own pages, as
5569 * modules will free them when they are removed.
5570 * Force a new page to be allocated for modules.
5573 WARN_ON(ftrace_pages || ftrace_pages_start);
5574 /* First initialization */
5575 ftrace_pages = ftrace_pages_start = start_pg;
5580 if (WARN_ON(ftrace_pages->next)) {
5581 /* Hmm, we have free pages? */
5582 while (ftrace_pages->next)
5583 ftrace_pages = ftrace_pages->next;
5586 ftrace_pages->next = start_pg;
5592 addr = ftrace_call_adjust(*p++);
5594 * Some architecture linkers will pad between
5595 * the different mcount_loc sections of different
5596 * object files to satisfy alignments.
5597 * Skip any NULL pointers.
5602 if (pg->index == pg->size) {
5603 /* We should have allocated enough */
5604 if (WARN_ON(!pg->next))
5609 rec = &pg->records[pg->index++];
5613 /* We should have used all pages */
5616 /* Assign the last page to ftrace_pages */
5620 * We only need to disable interrupts on start up
5621 * because we are modifying code that an interrupt
5622 * may execute, and the modification is not atomic.
5623 * But for modules, nothing runs the code we modify
5624 * until we are finished with it, and there's no
5625 * reason to cause large interrupt latencies while we do it.
5628 local_irq_save(flags);
5629 ftrace_update_code(mod, start_pg);
5631 local_irq_restore(flags);
5634 mutex_unlock(&ftrace_lock);
5639 struct ftrace_mod_func {
5640 struct list_head list;
5646 struct ftrace_mod_map {
5647 struct rcu_head rcu;
5648 struct list_head list;
5650 unsigned long start_addr;
5651 unsigned long end_addr;
5652 struct list_head funcs;
5653 unsigned int num_funcs;
5656 #ifdef CONFIG_MODULES
5658 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5660 static LIST_HEAD(ftrace_mod_maps);
5662 static int referenced_filters(struct dyn_ftrace *rec)
5664 struct ftrace_ops *ops;
5667 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5668 if (ops_references_rec(ops, rec)) {
5670 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
5671 rec->flags |= FTRACE_FL_REGS;
5679 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5681 struct ftrace_func_entry *entry;
5682 struct dyn_ftrace *rec;
5685 if (ftrace_hash_empty(hash))
5688 for (i = 0; i < pg->index; i++) {
5689 rec = &pg->records[i];
5690 entry = __ftrace_lookup_ip(hash, rec->ip);
5692 * Do not allow this rec to match again.
5693 * Yeah, it may waste some memory, but will be removed
5694 * if/when the hash is modified again.
5701 /* Clear any records from hashs */
5702 static void clear_mod_from_hashes(struct ftrace_page *pg)
5704 struct trace_array *tr;
5706 mutex_lock(&trace_types_lock);
5707 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5708 if (!tr->ops || !tr->ops->func_hash)
5710 mutex_lock(&tr->ops->func_hash->regex_lock);
5711 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5712 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5713 mutex_unlock(&tr->ops->func_hash->regex_lock);
5715 mutex_unlock(&trace_types_lock);
5718 static void ftrace_free_mod_map(struct rcu_head *rcu)
5720 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5721 struct ftrace_mod_func *mod_func;
5722 struct ftrace_mod_func *n;
5724 /* All the contents of mod_map are now not visible to readers */
5725 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5726 kfree(mod_func->name);
5727 list_del(&mod_func->list);
5734 void ftrace_release_mod(struct module *mod)
5736 struct ftrace_mod_map *mod_map;
5737 struct ftrace_mod_map *n;
5738 struct dyn_ftrace *rec;
5739 struct ftrace_page **last_pg;
5740 struct ftrace_page *tmp_page = NULL;
5741 struct ftrace_page *pg;
5744 mutex_lock(&ftrace_lock);
5746 if (ftrace_disabled)
5749 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5750 if (mod_map->mod == mod) {
5751 list_del_rcu(&mod_map->list);
5752 call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
5758 * Each module has its own ftrace_pages, remove
5759 * them from the list.
5761 last_pg = &ftrace_pages_start;
5762 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5763 rec = &pg->records[0];
5764 if (within_module_core(rec->ip, mod) ||
5765 within_module_init(rec->ip, mod)) {
5767 * As core pages are first, the first
5768 * page should never be a module page.
5770 if (WARN_ON(pg == ftrace_pages_start))
5773 /* Check if we are deleting the last page */
5774 if (pg == ftrace_pages)
5775 ftrace_pages = next_to_ftrace_page(last_pg);
5777 ftrace_update_tot_cnt -= pg->index;
5778 *last_pg = pg->next;
5780 pg->next = tmp_page;
5783 last_pg = &pg->next;
5786 mutex_unlock(&ftrace_lock);
5788 for (pg = tmp_page; pg; pg = tmp_page) {
5790 /* Needs to be called outside of ftrace_lock */
5791 clear_mod_from_hashes(pg);
5793 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5794 free_pages((unsigned long)pg->records, order);
5795 tmp_page = pg->next;
5800 void ftrace_module_enable(struct module *mod)
5802 struct dyn_ftrace *rec;
5803 struct ftrace_page *pg;
5805 mutex_lock(&ftrace_lock);
5807 if (ftrace_disabled)
5811 * If the tracing is enabled, go ahead and enable the record.
5813 * The reason not to enable the record immediatelly is the
5814 * inherent check of ftrace_make_nop/ftrace_make_call for
5815 * correct previous instructions. Making first the NOP
5816 * conversion puts the module to the correct state, thus
5817 * passing the ftrace_make_call check.
5819 * We also delay this to after the module code already set the
5820 * text to read-only, as we now need to set it back to read-write
5821 * so that we can modify the text.
5823 if (ftrace_start_up)
5824 ftrace_arch_code_modify_prepare();
5826 do_for_each_ftrace_rec(pg, rec) {
5829 * do_for_each_ftrace_rec() is a double loop.
5830 * module text shares the pg. If a record is
5831 * not part of this module, then skip this pg,
5832 * which the "break" will do.
5834 if (!within_module_core(rec->ip, mod) &&
5835 !within_module_init(rec->ip, mod))
5841 * When adding a module, we need to check if tracers are
5842 * currently enabled and if they are, and can trace this record,
5843 * we need to enable the module functions as well as update the
5844 * reference counts for those function records.
5846 if (ftrace_start_up)
5847 cnt += referenced_filters(rec);
5849 rec->flags &= ~FTRACE_FL_DISABLED;
5852 if (ftrace_start_up && cnt) {
5853 int failed = __ftrace_replace_code(rec, 1);
5855 ftrace_bug(failed, rec);
5860 } while_for_each_ftrace_rec();
5863 if (ftrace_start_up)
5864 ftrace_arch_code_modify_post_process();
5867 mutex_unlock(&ftrace_lock);
5869 process_cached_mods(mod->name);
5872 void ftrace_module_init(struct module *mod)
5874 if (ftrace_disabled || !mod->num_ftrace_callsites)
5877 ftrace_process_locs(mod, mod->ftrace_callsites,
5878 mod->ftrace_callsites + mod->num_ftrace_callsites);
5881 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5882 struct dyn_ftrace *rec)
5884 struct ftrace_mod_func *mod_func;
5885 unsigned long symsize;
5886 unsigned long offset;
5887 char str[KSYM_SYMBOL_LEN];
5891 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5895 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5899 mod_func->name = kstrdup(str, GFP_KERNEL);
5900 if (!mod_func->name) {
5905 mod_func->ip = rec->ip - offset;
5906 mod_func->size = symsize;
5908 mod_map->num_funcs++;
5910 list_add_rcu(&mod_func->list, &mod_map->funcs);
5913 static struct ftrace_mod_map *
5914 allocate_ftrace_mod_map(struct module *mod,
5915 unsigned long start, unsigned long end)
5917 struct ftrace_mod_map *mod_map;
5919 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5924 mod_map->start_addr = start;
5925 mod_map->end_addr = end;
5926 mod_map->num_funcs = 0;
5928 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5930 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5936 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5937 unsigned long addr, unsigned long *size,
5938 unsigned long *off, char *sym)
5940 struct ftrace_mod_func *found_func = NULL;
5941 struct ftrace_mod_func *mod_func;
5943 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5944 if (addr >= mod_func->ip &&
5945 addr < mod_func->ip + mod_func->size) {
5946 found_func = mod_func;
5953 *size = found_func->size;
5955 *off = addr - found_func->ip;
5957 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5959 return found_func->name;
5966 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
5967 unsigned long *off, char **modname, char *sym)
5969 struct ftrace_mod_map *mod_map;
5970 const char *ret = NULL;
5972 /* mod_map is freed via call_rcu_sched() */
5974 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5975 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
5978 *modname = mod_map->mod->name;
5987 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
5988 char *type, char *name,
5989 char *module_name, int *exported)
5991 struct ftrace_mod_map *mod_map;
5992 struct ftrace_mod_func *mod_func;
5995 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5997 if (symnum >= mod_map->num_funcs) {
5998 symnum -= mod_map->num_funcs;
6002 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6008 *value = mod_func->ip;
6010 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6011 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6024 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6025 struct dyn_ftrace *rec) { }
6026 static inline struct ftrace_mod_map *
6027 allocate_ftrace_mod_map(struct module *mod,
6028 unsigned long start, unsigned long end)
6032 #endif /* CONFIG_MODULES */
6034 struct ftrace_init_func {
6035 struct list_head list;
6039 /* Clear any init ips from hashes */
6041 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6043 struct ftrace_func_entry *entry;
6045 if (ftrace_hash_empty(hash))
6048 entry = __ftrace_lookup_ip(hash, func->ip);
6051 * Do not allow this rec to match again.
6052 * Yeah, it may waste some memory, but will be removed
6053 * if/when the hash is modified again.
6060 clear_func_from_hashes(struct ftrace_init_func *func)
6062 struct trace_array *tr;
6064 mutex_lock(&trace_types_lock);
6065 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6066 if (!tr->ops || !tr->ops->func_hash)
6068 mutex_lock(&tr->ops->func_hash->regex_lock);
6069 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6070 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6071 mutex_unlock(&tr->ops->func_hash->regex_lock);
6073 mutex_unlock(&trace_types_lock);
6076 static void add_to_clear_hash_list(struct list_head *clear_list,
6077 struct dyn_ftrace *rec)
6079 struct ftrace_init_func *func;
6081 func = kmalloc(sizeof(*func), GFP_KERNEL);
6083 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6088 list_add(&func->list, clear_list);
6091 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
6093 unsigned long start = (unsigned long)(start_ptr);
6094 unsigned long end = (unsigned long)(end_ptr);
6095 struct ftrace_page **last_pg = &ftrace_pages_start;
6096 struct ftrace_page *pg;
6097 struct dyn_ftrace *rec;
6098 struct dyn_ftrace key;
6099 struct ftrace_mod_map *mod_map = NULL;
6100 struct ftrace_init_func *func, *func_next;
6101 struct list_head clear_hash;
6104 INIT_LIST_HEAD(&clear_hash);
6107 key.flags = end; /* overload flags, as it is unsigned long */
6109 mutex_lock(&ftrace_lock);
6112 * If we are freeing module init memory, then check if
6113 * any tracer is active. If so, we need to save a mapping of
6114 * the module functions being freed with the address.
6116 if (mod && ftrace_ops_list != &ftrace_list_end)
6117 mod_map = allocate_ftrace_mod_map(mod, start, end);
6119 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6120 if (end < pg->records[0].ip ||
6121 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6124 rec = bsearch(&key, pg->records, pg->index,
6125 sizeof(struct dyn_ftrace),
6130 /* rec will be cleared from hashes after ftrace_lock unlock */
6131 add_to_clear_hash_list(&clear_hash, rec);
6134 save_ftrace_mod_rec(mod_map, rec);
6137 ftrace_update_tot_cnt--;
6139 *last_pg = pg->next;
6140 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6141 free_pages((unsigned long)pg->records, order);
6143 pg = container_of(last_pg, struct ftrace_page, next);
6148 memmove(rec, rec + 1,
6149 (pg->index - (rec - pg->records)) * sizeof(*rec));
6150 /* More than one function may be in this block */
6153 mutex_unlock(&ftrace_lock);
6155 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6156 clear_func_from_hashes(func);
6161 void __init ftrace_free_init_mem(void)
6163 void *start = (void *)(&__init_begin);
6164 void *end = (void *)(&__init_end);
6166 ftrace_free_mem(NULL, start, end);
6169 void __init ftrace_init(void)
6171 extern unsigned long __start_mcount_loc[];
6172 extern unsigned long __stop_mcount_loc[];
6173 unsigned long count, flags;
6176 local_irq_save(flags);
6177 ret = ftrace_dyn_arch_init();
6178 local_irq_restore(flags);
6182 count = __stop_mcount_loc - __start_mcount_loc;
6184 pr_info("ftrace: No functions to be traced?\n");
6188 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6189 count, count / ENTRIES_PER_PAGE + 1);
6191 last_ftrace_enabled = ftrace_enabled = 1;
6193 ret = ftrace_process_locs(NULL,
6197 set_ftrace_early_filters();
6201 ftrace_disabled = 1;
6204 /* Do nothing if arch does not support this */
6205 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6209 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6211 arch_ftrace_update_trampoline(ops);
6214 void ftrace_init_trace_array(struct trace_array *tr)
6216 INIT_LIST_HEAD(&tr->func_probes);
6217 INIT_LIST_HEAD(&tr->mod_trace);
6218 INIT_LIST_HEAD(&tr->mod_notrace);
6222 static struct ftrace_ops global_ops = {
6223 .func = ftrace_stub,
6224 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6225 FTRACE_OPS_FL_INITIALIZED |
6229 static int __init ftrace_nodyn_init(void)
6234 core_initcall(ftrace_nodyn_init);
6236 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6237 static inline void ftrace_startup_enable(int command) { }
6238 static inline void ftrace_startup_all(int command) { }
6239 /* Keep as macros so we do not need to define the commands */
6240 # define ftrace_startup(ops, command) \
6242 int ___ret = __register_ftrace_function(ops); \
6244 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
6247 # define ftrace_shutdown(ops, command) \
6249 int ___ret = __unregister_ftrace_function(ops); \
6251 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
6255 # define ftrace_startup_sysctl() do { } while (0)
6256 # define ftrace_shutdown_sysctl() do { } while (0)
6259 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
6264 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6268 #endif /* CONFIG_DYNAMIC_FTRACE */
6270 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6272 tr->ops = &global_ops;
6273 tr->ops->private = tr;
6274 ftrace_init_trace_array(tr);
6277 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6279 /* If we filter on pids, update to use the pid function */
6280 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6281 if (WARN_ON(tr->ops->func != ftrace_stub))
6282 printk("ftrace ops had %pS for function\n",
6285 tr->ops->func = func;
6286 tr->ops->private = tr;
6289 void ftrace_reset_array_ops(struct trace_array *tr)
6291 tr->ops->func = ftrace_stub;
6294 static nokprobe_inline void
6295 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6296 struct ftrace_ops *ignored, struct pt_regs *regs)
6298 struct ftrace_ops *op;
6301 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6306 * Some of the ops may be dynamically allocated,
6307 * they must be freed after a synchronize_sched().
6309 preempt_disable_notrace();
6311 do_for_each_ftrace_op(op, ftrace_ops_list) {
6313 * Check the following for each ops before calling their func:
6314 * if RCU flag is set, then rcu_is_watching() must be true
6315 * if PER_CPU is set, then ftrace_function_local_disable()
6317 * Otherwise test if the ip matches the ops filter
6319 * If any of the above fails then the op->func() is not executed.
6321 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6322 ftrace_ops_test(op, ip, regs)) {
6323 if (FTRACE_WARN_ON(!op->func)) {
6324 pr_warn("op=%p %pS\n", op, op);
6327 op->func(ip, parent_ip, op, regs);
6329 } while_for_each_ftrace_op(op);
6331 preempt_enable_notrace();
6332 trace_clear_recursion(bit);
6336 * Some archs only support passing ip and parent_ip. Even though
6337 * the list function ignores the op parameter, we do not want any
6338 * C side effects, where a function is called without the caller
6339 * sending a third parameter.
6340 * Archs are to support both the regs and ftrace_ops at the same time.
6341 * If they support ftrace_ops, it is assumed they support regs.
6342 * If call backs want to use regs, they must either check for regs
6343 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6344 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6345 * An architecture can pass partial regs with ftrace_ops and still
6346 * set the ARCH_SUPPORTS_FTRACE_OPS.
6348 #if ARCH_SUPPORTS_FTRACE_OPS
6349 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6350 struct ftrace_ops *op, struct pt_regs *regs)
6352 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6354 NOKPROBE_SYMBOL(ftrace_ops_list_func);
6356 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6358 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6360 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
6364 * If there's only one function registered but it does not support
6365 * recursion, needs RCU protection and/or requires per cpu handling, then
6366 * this function will be called by the mcount trampoline.
6368 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6369 struct ftrace_ops *op, struct pt_regs *regs)
6373 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6376 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6380 preempt_disable_notrace();
6382 op->func(ip, parent_ip, op, regs);
6384 preempt_enable_notrace();
6385 trace_clear_recursion(bit);
6387 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
6390 * ftrace_ops_get_func - get the function a trampoline should call
6391 * @ops: the ops to get the function for
6393 * Normally the mcount trampoline will call the ops->func, but there
6394 * are times that it should not. For example, if the ops does not
6395 * have its own recursion protection, then it should call the
6396 * ftrace_ops_assist_func() instead.
6398 * Returns the function that the trampoline should call for @ops.
6400 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6403 * If the function does not handle recursion, needs to be RCU safe,
6404 * or does per cpu logic, then we need to call the assist handler.
6406 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6407 ops->flags & FTRACE_OPS_FL_RCU)
6408 return ftrace_ops_assist_func;
6414 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6415 struct task_struct *prev, struct task_struct *next)
6417 struct trace_array *tr = data;
6418 struct trace_pid_list *pid_list;
6420 pid_list = rcu_dereference_sched(tr->function_pids);
6422 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6423 trace_ignore_this_task(pid_list, next));
6427 ftrace_pid_follow_sched_process_fork(void *data,
6428 struct task_struct *self,
6429 struct task_struct *task)
6431 struct trace_pid_list *pid_list;
6432 struct trace_array *tr = data;
6434 pid_list = rcu_dereference_sched(tr->function_pids);
6435 trace_filter_add_remove_task(pid_list, self, task);
6439 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6441 struct trace_pid_list *pid_list;
6442 struct trace_array *tr = data;
6444 pid_list = rcu_dereference_sched(tr->function_pids);
6445 trace_filter_add_remove_task(pid_list, NULL, task);
6448 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6451 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6453 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6456 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6458 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6463 static void clear_ftrace_pids(struct trace_array *tr)
6465 struct trace_pid_list *pid_list;
6468 pid_list = rcu_dereference_protected(tr->function_pids,
6469 lockdep_is_held(&ftrace_lock));
6473 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6475 for_each_possible_cpu(cpu)
6476 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6478 rcu_assign_pointer(tr->function_pids, NULL);
6480 /* Wait till all users are no longer using pid filtering */
6481 synchronize_sched();
6483 trace_free_pid_list(pid_list);
6486 void ftrace_clear_pids(struct trace_array *tr)
6488 mutex_lock(&ftrace_lock);
6490 clear_ftrace_pids(tr);
6492 mutex_unlock(&ftrace_lock);
6495 static void ftrace_pid_reset(struct trace_array *tr)
6497 mutex_lock(&ftrace_lock);
6498 clear_ftrace_pids(tr);
6500 ftrace_update_pid_func();
6501 ftrace_startup_all(0);
6503 mutex_unlock(&ftrace_lock);
6506 /* Greater than any max PID */
6507 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6509 static void *fpid_start(struct seq_file *m, loff_t *pos)
6512 struct trace_pid_list *pid_list;
6513 struct trace_array *tr = m->private;
6515 mutex_lock(&ftrace_lock);
6516 rcu_read_lock_sched();
6518 pid_list = rcu_dereference_sched(tr->function_pids);
6521 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6523 return trace_pid_start(pid_list, pos);
6526 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6528 struct trace_array *tr = m->private;
6529 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6531 if (v == FTRACE_NO_PIDS) {
6535 return trace_pid_next(pid_list, v, pos);
6538 static void fpid_stop(struct seq_file *m, void *p)
6541 rcu_read_unlock_sched();
6542 mutex_unlock(&ftrace_lock);
6545 static int fpid_show(struct seq_file *m, void *v)
6547 if (v == FTRACE_NO_PIDS) {
6548 seq_puts(m, "no pid\n");
6552 return trace_pid_show(m, v);
6555 static const struct seq_operations ftrace_pid_sops = {
6556 .start = fpid_start,
6563 ftrace_pid_open(struct inode *inode, struct file *file)
6565 struct trace_array *tr = inode->i_private;
6569 if (trace_array_get(tr) < 0)
6572 if ((file->f_mode & FMODE_WRITE) &&
6573 (file->f_flags & O_TRUNC))
6574 ftrace_pid_reset(tr);
6576 ret = seq_open(file, &ftrace_pid_sops);
6578 trace_array_put(tr);
6580 m = file->private_data;
6581 /* copy tr over to seq ops */
6588 static void ignore_task_cpu(void *data)
6590 struct trace_array *tr = data;
6591 struct trace_pid_list *pid_list;
6594 * This function is called by on_each_cpu() while the
6595 * event_mutex is held.
6597 pid_list = rcu_dereference_protected(tr->function_pids,
6598 mutex_is_locked(&ftrace_lock));
6600 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6601 trace_ignore_this_task(pid_list, current));
6605 ftrace_pid_write(struct file *filp, const char __user *ubuf,
6606 size_t cnt, loff_t *ppos)
6608 struct seq_file *m = filp->private_data;
6609 struct trace_array *tr = m->private;
6610 struct trace_pid_list *filtered_pids = NULL;
6611 struct trace_pid_list *pid_list;
6617 mutex_lock(&ftrace_lock);
6619 filtered_pids = rcu_dereference_protected(tr->function_pids,
6620 lockdep_is_held(&ftrace_lock));
6622 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6626 rcu_assign_pointer(tr->function_pids, pid_list);
6628 if (filtered_pids) {
6629 synchronize_sched();
6630 trace_free_pid_list(filtered_pids);
6631 } else if (pid_list) {
6632 /* Register a probe to set whether to ignore the tracing of a task */
6633 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6637 * Ignoring of pids is done at task switch. But we have to
6638 * check for those tasks that are currently running.
6639 * Always do this in case a pid was appended or removed.
6641 on_each_cpu(ignore_task_cpu, tr, 1);
6643 ftrace_update_pid_func();
6644 ftrace_startup_all(0);
6646 mutex_unlock(&ftrace_lock);
6655 ftrace_pid_release(struct inode *inode, struct file *file)
6657 struct trace_array *tr = inode->i_private;
6659 trace_array_put(tr);
6661 return seq_release(inode, file);
6664 static const struct file_operations ftrace_pid_fops = {
6665 .open = ftrace_pid_open,
6666 .write = ftrace_pid_write,
6668 .llseek = tracing_lseek,
6669 .release = ftrace_pid_release,
6672 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
6674 trace_create_file("set_ftrace_pid", 0644, d_tracer,
6675 tr, &ftrace_pid_fops);
6678 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6679 struct dentry *d_tracer)
6681 /* Only the top level directory has the dyn_tracefs and profile */
6682 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6684 ftrace_init_dyn_tracefs(d_tracer);
6685 ftrace_profile_tracefs(d_tracer);
6689 * ftrace_kill - kill ftrace
6691 * This function should be used by panic code. It stops ftrace
6692 * but in a not so nice way. If you need to simply kill ftrace
6693 * from a non-atomic section, use ftrace_kill.
6695 void ftrace_kill(void)
6697 ftrace_disabled = 1;
6699 ftrace_trace_function = ftrace_stub;
6703 * Test if ftrace is dead or not.
6705 int ftrace_is_dead(void)
6707 return ftrace_disabled;
6711 * register_ftrace_function - register a function for profiling
6712 * @ops - ops structure that holds the function for profiling.
6714 * Register a function to be called by all functions in the
6717 * Note: @ops->func and all the functions it calls must be labeled
6718 * with "notrace", otherwise it will go into a
6721 int register_ftrace_function(struct ftrace_ops *ops)
6725 ftrace_ops_init(ops);
6727 mutex_lock(&ftrace_lock);
6729 ret = ftrace_startup(ops, 0);
6731 mutex_unlock(&ftrace_lock);
6735 EXPORT_SYMBOL_GPL(register_ftrace_function);
6738 * unregister_ftrace_function - unregister a function for profiling.
6739 * @ops - ops structure that holds the function to unregister
6741 * Unregister a function that was added to be called by ftrace profiling.
6743 int unregister_ftrace_function(struct ftrace_ops *ops)
6747 mutex_lock(&ftrace_lock);
6748 ret = ftrace_shutdown(ops, 0);
6749 mutex_unlock(&ftrace_lock);
6753 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
6756 ftrace_enable_sysctl(struct ctl_table *table, int write,
6757 void __user *buffer, size_t *lenp,
6762 mutex_lock(&ftrace_lock);
6764 if (unlikely(ftrace_disabled))
6767 ret = proc_dointvec(table, write, buffer, lenp, ppos);
6769 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
6772 last_ftrace_enabled = !!ftrace_enabled;
6774 if (ftrace_enabled) {
6776 /* we are starting ftrace again */
6777 if (rcu_dereference_protected(ftrace_ops_list,
6778 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
6779 update_ftrace_function();
6781 ftrace_startup_sysctl();
6784 /* stopping ftrace calls (just send to ftrace_stub) */
6785 ftrace_trace_function = ftrace_stub;
6787 ftrace_shutdown_sysctl();
6791 mutex_unlock(&ftrace_lock);
6795 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6797 static struct ftrace_ops graph_ops = {
6798 .func = ftrace_stub,
6799 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6800 FTRACE_OPS_FL_INITIALIZED |
6803 #ifdef FTRACE_GRAPH_TRAMP_ADDR
6804 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
6805 /* trampoline_size is only needed for dynamically allocated tramps */
6807 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6810 void ftrace_graph_sleep_time_control(bool enable)
6812 fgraph_sleep_time = enable;
6815 void ftrace_graph_graph_time_control(bool enable)
6817 fgraph_graph_time = enable;
6820 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6825 /* The callbacks that hook a function */
6826 trace_func_graph_ret_t ftrace_graph_return =
6827 (trace_func_graph_ret_t)ftrace_stub;
6828 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
6829 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
6831 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6832 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6836 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6837 struct task_struct *g, *t;
6839 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6841 kmalloc_array(FTRACE_RETFUNC_DEPTH,
6842 sizeof(struct ftrace_ret_stack),
6844 if (!ret_stack_list[i]) {
6852 read_lock(&tasklist_lock);
6853 do_each_thread(g, t) {
6859 if (t->ret_stack == NULL) {
6860 atomic_set(&t->tracing_graph_pause, 0);
6861 atomic_set(&t->trace_overrun, 0);
6862 t->curr_ret_stack = -1;
6863 t->curr_ret_depth = -1;
6864 /* Make sure the tasks see the -1 first: */
6866 t->ret_stack = ret_stack_list[start++];
6868 } while_each_thread(g, t);
6871 read_unlock(&tasklist_lock);
6873 for (i = start; i < end; i++)
6874 kfree(ret_stack_list[i]);
6879 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
6880 struct task_struct *prev, struct task_struct *next)
6882 unsigned long long timestamp;
6886 * Does the user want to count the time a function was asleep.
6887 * If so, do not update the time stamps.
6889 if (fgraph_sleep_time)
6892 timestamp = trace_clock_local();
6894 prev->ftrace_timestamp = timestamp;
6896 /* only process tasks that we timestamped */
6897 if (!next->ftrace_timestamp)
6901 * Update all the counters in next to make up for the
6902 * time next was sleeping.
6904 timestamp -= next->ftrace_timestamp;
6906 for (index = next->curr_ret_stack; index >= 0; index--)
6907 next->ret_stack[index].calltime += timestamp;
6910 /* Allocate a return stack for each task */
6911 static int start_graph_tracing(void)
6913 struct ftrace_ret_stack **ret_stack_list;
6916 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
6917 sizeof(struct ftrace_ret_stack *),
6920 if (!ret_stack_list)
6923 /* The cpu_boot init_task->ret_stack will never be freed */
6924 for_each_online_cpu(cpu) {
6925 if (!idle_task(cpu)->ret_stack)
6926 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
6930 ret = alloc_retstack_tasklist(ret_stack_list);
6931 } while (ret == -EAGAIN);
6934 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6936 pr_info("ftrace_graph: Couldn't activate tracepoint"
6937 " probe to kernel_sched_switch\n");
6940 kfree(ret_stack_list);
6945 * Hibernation protection.
6946 * The state of the current task is too much unstable during
6947 * suspend/restore to disk. We want to protect against that.
6950 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6954 case PM_HIBERNATION_PREPARE:
6955 pause_graph_tracing();
6958 case PM_POST_HIBERNATION:
6959 unpause_graph_tracing();
6965 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6967 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6969 return __ftrace_graph_entry(trace);
6973 * The function graph tracer should only trace the functions defined
6974 * by set_ftrace_filter and set_ftrace_notrace. If another function
6975 * tracer ops is registered, the graph tracer requires testing the
6976 * function against the global ops, and not just trace any function
6977 * that any ftrace_ops registered.
6979 static void update_function_graph_func(void)
6981 struct ftrace_ops *op;
6982 bool do_test = false;
6985 * The graph and global ops share the same set of functions
6986 * to test. If any other ops is on the list, then
6987 * the graph tracing needs to test if its the function
6990 do_for_each_ftrace_op(op, ftrace_ops_list) {
6991 if (op != &global_ops && op != &graph_ops &&
6992 op != &ftrace_list_end) {
6994 /* in double loop, break out with goto */
6997 } while_for_each_ftrace_op(op);
7000 ftrace_graph_entry = ftrace_graph_entry_test;
7002 ftrace_graph_entry = __ftrace_graph_entry;
7005 static struct notifier_block ftrace_suspend_notifier = {
7006 .notifier_call = ftrace_suspend_notifier_call,
7009 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
7010 trace_func_graph_ent_t entryfunc)
7014 mutex_lock(&ftrace_lock);
7016 /* we currently allow only one tracer registered at a time */
7017 if (ftrace_graph_active) {
7022 register_pm_notifier(&ftrace_suspend_notifier);
7024 ftrace_graph_active++;
7025 ret = start_graph_tracing();
7027 ftrace_graph_active--;
7031 ftrace_graph_return = retfunc;
7034 * Update the indirect function to the entryfunc, and the
7035 * function that gets called to the entry_test first. Then
7036 * call the update fgraph entry function to determine if
7037 * the entryfunc should be called directly or not.
7039 __ftrace_graph_entry = entryfunc;
7040 ftrace_graph_entry = ftrace_graph_entry_test;
7041 update_function_graph_func();
7043 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
7045 mutex_unlock(&ftrace_lock);
7049 void unregister_ftrace_graph(void)
7051 mutex_lock(&ftrace_lock);
7053 if (unlikely(!ftrace_graph_active))
7056 ftrace_graph_active--;
7057 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
7058 ftrace_graph_entry = ftrace_graph_entry_stub;
7059 __ftrace_graph_entry = ftrace_graph_entry_stub;
7060 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
7061 unregister_pm_notifier(&ftrace_suspend_notifier);
7062 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
7065 mutex_unlock(&ftrace_lock);
7068 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
7071 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
7073 atomic_set(&t->tracing_graph_pause, 0);
7074 atomic_set(&t->trace_overrun, 0);
7075 t->ftrace_timestamp = 0;
7076 /* make curr_ret_stack visible before we add the ret_stack */
7078 t->ret_stack = ret_stack;
7082 * Allocate a return stack for the idle task. May be the first
7083 * time through, or it may be done by CPU hotplug online.
7085 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
7087 t->curr_ret_stack = -1;
7088 t->curr_ret_depth = -1;
7090 * The idle task has no parent, it either has its own
7091 * stack or no stack at all.
7094 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
7096 if (ftrace_graph_active) {
7097 struct ftrace_ret_stack *ret_stack;
7099 ret_stack = per_cpu(idle_ret_stack, cpu);
7102 kmalloc_array(FTRACE_RETFUNC_DEPTH,
7103 sizeof(struct ftrace_ret_stack),
7107 per_cpu(idle_ret_stack, cpu) = ret_stack;
7109 graph_init_task(t, ret_stack);
7113 /* Allocate a return stack for newly created task */
7114 void ftrace_graph_init_task(struct task_struct *t)
7116 /* Make sure we do not use the parent ret_stack */
7117 t->ret_stack = NULL;
7118 t->curr_ret_stack = -1;
7119 t->curr_ret_depth = -1;
7121 if (ftrace_graph_active) {
7122 struct ftrace_ret_stack *ret_stack;
7124 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
7125 sizeof(struct ftrace_ret_stack),
7129 graph_init_task(t, ret_stack);
7133 void ftrace_graph_exit_task(struct task_struct *t)
7135 struct ftrace_ret_stack *ret_stack = t->ret_stack;
7137 t->ret_stack = NULL;
7138 /* NULL must become visible to IRQs before we free it: */