2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/sched/task.h>
19 #include <linux/kallsyms.h>
20 #include <linux/seq_file.h>
21 #include <linux/suspend.h>
22 #include <linux/tracefs.h>
23 #include <linux/hardirq.h>
24 #include <linux/kthread.h>
25 #include <linux/uaccess.h>
26 #include <linux/bsearch.h>
27 #include <linux/module.h>
28 #include <linux/ftrace.h>
29 #include <linux/sysctl.h>
30 #include <linux/slab.h>
31 #include <linux/ctype.h>
32 #include <linux/sort.h>
33 #include <linux/list.h>
34 #include <linux/hash.h>
35 #include <linux/rcupdate.h>
37 #include <trace/events/sched.h>
39 #include <asm/sections.h>
40 #include <asm/setup.h>
42 #include "trace_output.h"
43 #include "trace_stat.h"
45 #define FTRACE_WARN_ON(cond) \
53 #define FTRACE_WARN_ON_ONCE(cond) \
56 if (WARN_ON_ONCE(___r)) \
61 /* hash bits for specific function selection */
62 #define FTRACE_HASH_BITS 7
63 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
64 #define FTRACE_HASH_DEFAULT_BITS 10
65 #define FTRACE_HASH_MAX_BITS 12
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
71 #define ASSIGN_OPS_HASH(opsname, val) \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
75 #define INIT_OPS_HASH(opsname)
76 #define ASSIGN_OPS_HASH(opsname, val)
79 static struct ftrace_ops ftrace_list_end __read_mostly = {
81 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
82 INIT_OPS_HASH(ftrace_list_end)
85 /* ftrace_enabled is a method to turn ftrace on or off */
86 int ftrace_enabled __read_mostly;
87 static int last_ftrace_enabled;
89 /* Current function tracing op */
90 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
91 /* What to set function_trace_op to */
92 static struct ftrace_ops *set_function_trace_op;
94 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
96 struct trace_array *tr;
98 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
103 return tr->function_pids != NULL;
106 static void ftrace_update_trampoline(struct ftrace_ops *ops);
109 * ftrace_disabled is set when an anomaly is discovered.
110 * ftrace_disabled is much stronger than ftrace_enabled.
112 static int ftrace_disabled __read_mostly;
114 static DEFINE_MUTEX(ftrace_lock);
116 static struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
117 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
118 static struct ftrace_ops global_ops;
120 #if ARCH_SUPPORTS_FTRACE_OPS
121 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
122 struct ftrace_ops *op, struct pt_regs *regs);
124 /* See comment below, where ftrace_ops_list_func is defined */
125 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
126 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
130 * Traverse the ftrace_global_list, invoking all entries. The reason that we
131 * can use rcu_dereference_raw_notrace() is that elements removed from this list
132 * are simply leaked, so there is no need to interact with a grace-period
133 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
134 * concurrent insertions into the ftrace_global_list.
136 * Silly Alpha and silly pointer-speculation compiler optimizations!
138 #define do_for_each_ftrace_op(op, list) \
139 op = rcu_dereference_raw_notrace(list); \
143 * Optimized for just a single item in the list (as that is the normal case).
145 #define while_for_each_ftrace_op(op) \
146 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
147 unlikely((op) != &ftrace_list_end))
149 static inline void ftrace_ops_init(struct ftrace_ops *ops)
151 #ifdef CONFIG_DYNAMIC_FTRACE
152 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
153 mutex_init(&ops->local_hash.regex_lock);
154 ops->func_hash = &ops->local_hash;
155 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
161 * ftrace_nr_registered_ops - return number of ops registered
163 * Returns the number of ftrace_ops registered and tracing functions
165 int ftrace_nr_registered_ops(void)
167 struct ftrace_ops *ops;
170 mutex_lock(&ftrace_lock);
172 for (ops = rcu_dereference_protected(ftrace_ops_list,
173 lockdep_is_held(&ftrace_lock));
174 ops != &ftrace_list_end;
175 ops = rcu_dereference_protected(ops->next,
176 lockdep_is_held(&ftrace_lock)))
179 mutex_unlock(&ftrace_lock);
184 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
185 struct ftrace_ops *op, struct pt_regs *regs)
187 struct trace_array *tr = op->private;
189 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
192 op->saved_func(ip, parent_ip, op, regs);
195 static void ftrace_sync(struct work_struct *work)
198 * This function is just a stub to implement a hard force
199 * of synchronize_sched(). This requires synchronizing
200 * tasks even in userspace and idle.
202 * Yes, function tracing is rude.
206 static void ftrace_sync_ipi(void *data)
208 /* Probably not needed, but do it anyway */
212 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
213 static void update_function_graph_func(void);
215 /* Both enabled by default (can be cleared by function_graph tracer flags */
216 static bool fgraph_sleep_time = true;
217 static bool fgraph_graph_time = true;
220 static inline void update_function_graph_func(void) { }
224 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
227 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
228 * then it needs to call the list anyway.
230 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
231 FTRACE_FORCE_LIST_FUNC)
232 return ftrace_ops_list_func;
234 return ftrace_ops_get_func(ops);
237 static void update_ftrace_function(void)
242 * Prepare the ftrace_ops that the arch callback will use.
243 * If there's only one ftrace_ops registered, the ftrace_ops_list
244 * will point to the ops we want.
246 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
247 lockdep_is_held(&ftrace_lock));
249 /* If there's no ftrace_ops registered, just call the stub function */
250 if (set_function_trace_op == &ftrace_list_end) {
254 * If we are at the end of the list and this ops is
255 * recursion safe and not dynamic and the arch supports passing ops,
256 * then have the mcount trampoline call the function directly.
258 } else if (rcu_dereference_protected(ftrace_ops_list->next,
259 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
260 func = ftrace_ops_get_list_func(ftrace_ops_list);
263 /* Just use the default ftrace_ops */
264 set_function_trace_op = &ftrace_list_end;
265 func = ftrace_ops_list_func;
268 update_function_graph_func();
270 /* If there's no change, then do nothing more here */
271 if (ftrace_trace_function == func)
275 * If we are using the list function, it doesn't care
276 * about the function_trace_ops.
278 if (func == ftrace_ops_list_func) {
279 ftrace_trace_function = func;
281 * Don't even bother setting function_trace_ops,
282 * it would be racy to do so anyway.
287 #ifndef CONFIG_DYNAMIC_FTRACE
289 * For static tracing, we need to be a bit more careful.
290 * The function change takes affect immediately. Thus,
291 * we need to coorditate the setting of the function_trace_ops
292 * with the setting of the ftrace_trace_function.
294 * Set the function to the list ops, which will call the
295 * function we want, albeit indirectly, but it handles the
296 * ftrace_ops and doesn't depend on function_trace_op.
298 ftrace_trace_function = ftrace_ops_list_func;
300 * Make sure all CPUs see this. Yes this is slow, but static
301 * tracing is slow and nasty to have enabled.
303 schedule_on_each_cpu(ftrace_sync);
304 /* Now all cpus are using the list ops. */
305 function_trace_op = set_function_trace_op;
306 /* Make sure the function_trace_op is visible on all CPUs */
308 /* Nasty way to force a rmb on all cpus */
309 smp_call_function(ftrace_sync_ipi, NULL, 1);
310 /* OK, we are all set to update the ftrace_trace_function now! */
311 #endif /* !CONFIG_DYNAMIC_FTRACE */
313 ftrace_trace_function = func;
316 int using_ftrace_ops_list_func(void)
318 return ftrace_trace_function == ftrace_ops_list_func;
321 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
322 struct ftrace_ops *ops)
324 rcu_assign_pointer(ops->next, *list);
327 * We are entering ops into the list but another
328 * CPU might be walking that list. We need to make sure
329 * the ops->next pointer is valid before another CPU sees
330 * the ops pointer included into the list.
332 rcu_assign_pointer(*list, ops);
335 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
336 struct ftrace_ops *ops)
338 struct ftrace_ops **p;
341 * If we are removing the last function, then simply point
342 * to the ftrace_stub.
344 if (rcu_dereference_protected(*list,
345 lockdep_is_held(&ftrace_lock)) == ops &&
346 rcu_dereference_protected(ops->next,
347 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
348 *list = &ftrace_list_end;
352 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
363 static void ftrace_update_trampoline(struct ftrace_ops *ops);
365 static int __register_ftrace_function(struct ftrace_ops *ops)
367 if (ops->flags & FTRACE_OPS_FL_DELETED)
370 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
373 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
375 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
376 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
377 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
379 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
380 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
383 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
384 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
387 if (!core_kernel_data((unsigned long)ops))
388 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
390 add_ftrace_ops(&ftrace_ops_list, ops);
392 /* Always save the function, and reset at unregistering */
393 ops->saved_func = ops->func;
395 if (ftrace_pids_enabled(ops))
396 ops->func = ftrace_pid_func;
398 ftrace_update_trampoline(ops);
401 update_ftrace_function();
406 static int __unregister_ftrace_function(struct ftrace_ops *ops)
410 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
413 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
419 update_ftrace_function();
421 ops->func = ops->saved_func;
426 static void ftrace_update_pid_func(void)
428 struct ftrace_ops *op;
430 /* Only do something if we are tracing something */
431 if (ftrace_trace_function == ftrace_stub)
434 do_for_each_ftrace_op(op, ftrace_ops_list) {
435 if (op->flags & FTRACE_OPS_FL_PID) {
436 op->func = ftrace_pids_enabled(op) ?
437 ftrace_pid_func : op->saved_func;
438 ftrace_update_trampoline(op);
440 } while_for_each_ftrace_op(op);
442 update_ftrace_function();
445 #ifdef CONFIG_FUNCTION_PROFILER
446 struct ftrace_profile {
447 struct hlist_node node;
449 unsigned long counter;
450 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
451 unsigned long long time;
452 unsigned long long time_squared;
456 struct ftrace_profile_page {
457 struct ftrace_profile_page *next;
459 struct ftrace_profile records[];
462 struct ftrace_profile_stat {
464 struct hlist_head *hash;
465 struct ftrace_profile_page *pages;
466 struct ftrace_profile_page *start;
467 struct tracer_stat stat;
470 #define PROFILE_RECORDS_SIZE \
471 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
473 #define PROFILES_PER_PAGE \
474 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
476 static int ftrace_profile_enabled __read_mostly;
478 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
479 static DEFINE_MUTEX(ftrace_profile_lock);
481 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
483 #define FTRACE_PROFILE_HASH_BITS 10
484 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
487 function_stat_next(void *v, int idx)
489 struct ftrace_profile *rec = v;
490 struct ftrace_profile_page *pg;
492 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
498 if ((void *)rec >= (void *)&pg->records[pg->index]) {
502 rec = &pg->records[0];
510 static void *function_stat_start(struct tracer_stat *trace)
512 struct ftrace_profile_stat *stat =
513 container_of(trace, struct ftrace_profile_stat, stat);
515 if (!stat || !stat->start)
518 return function_stat_next(&stat->start->records[0], 0);
521 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
522 /* function graph compares on total time */
523 static int function_stat_cmp(void *p1, void *p2)
525 struct ftrace_profile *a = p1;
526 struct ftrace_profile *b = p2;
528 if (a->time < b->time)
530 if (a->time > b->time)
536 /* not function graph compares against hits */
537 static int function_stat_cmp(void *p1, void *p2)
539 struct ftrace_profile *a = p1;
540 struct ftrace_profile *b = p2;
542 if (a->counter < b->counter)
544 if (a->counter > b->counter)
551 static int function_stat_headers(struct seq_file *m)
553 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
554 seq_puts(m, " Function "
557 "--- ---- --- ---\n");
559 seq_puts(m, " Function Hit\n"
565 static int function_stat_show(struct seq_file *m, void *v)
567 struct ftrace_profile *rec = v;
568 char str[KSYM_SYMBOL_LEN];
570 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
571 static struct trace_seq s;
572 unsigned long long avg;
573 unsigned long long stddev;
575 mutex_lock(&ftrace_profile_lock);
577 /* we raced with function_profile_reset() */
578 if (unlikely(rec->counter == 0)) {
583 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
585 do_div(avg, rec->counter);
586 if (tracing_thresh && (avg < tracing_thresh))
590 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
591 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
593 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
596 /* Sample standard deviation (s^2) */
597 if (rec->counter <= 1)
601 * Apply Welford's method:
602 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
604 stddev = rec->counter * rec->time_squared -
605 rec->time * rec->time;
608 * Divide only 1000 for ns^2 -> us^2 conversion.
609 * trace_print_graph_duration will divide 1000 again.
611 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
615 trace_print_graph_duration(rec->time, &s);
616 trace_seq_puts(&s, " ");
617 trace_print_graph_duration(avg, &s);
618 trace_seq_puts(&s, " ");
619 trace_print_graph_duration(stddev, &s);
620 trace_print_seq(m, &s);
624 mutex_unlock(&ftrace_profile_lock);
629 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
631 struct ftrace_profile_page *pg;
633 pg = stat->pages = stat->start;
636 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
641 memset(stat->hash, 0,
642 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
645 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
647 struct ftrace_profile_page *pg;
652 /* If we already allocated, do nothing */
656 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
660 #ifdef CONFIG_DYNAMIC_FTRACE
661 functions = ftrace_update_tot_cnt;
664 * We do not know the number of functions that exist because
665 * dynamic tracing is what counts them. With past experience
666 * we have around 20K functions. That should be more than enough.
667 * It is highly unlikely we will execute every function in
673 pg = stat->start = stat->pages;
675 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
677 for (i = 1; i < pages; i++) {
678 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
689 unsigned long tmp = (unsigned long)pg;
701 static int ftrace_profile_init_cpu(int cpu)
703 struct ftrace_profile_stat *stat;
706 stat = &per_cpu(ftrace_profile_stats, cpu);
709 /* If the profile is already created, simply reset it */
710 ftrace_profile_reset(stat);
715 * We are profiling all functions, but usually only a few thousand
716 * functions are hit. We'll make a hash of 1024 items.
718 size = FTRACE_PROFILE_HASH_SIZE;
720 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
725 /* Preallocate the function profiling pages */
726 if (ftrace_profile_pages_init(stat) < 0) {
735 static int ftrace_profile_init(void)
740 for_each_possible_cpu(cpu) {
741 ret = ftrace_profile_init_cpu(cpu);
749 /* interrupts must be disabled */
750 static struct ftrace_profile *
751 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
753 struct ftrace_profile *rec;
754 struct hlist_head *hhd;
757 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
758 hhd = &stat->hash[key];
760 if (hlist_empty(hhd))
763 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
771 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
772 struct ftrace_profile *rec)
776 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
777 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
781 * The memory is already allocated, this simply finds a new record to use.
783 static struct ftrace_profile *
784 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
786 struct ftrace_profile *rec = NULL;
788 /* prevent recursion (from NMIs) */
789 if (atomic_inc_return(&stat->disabled) != 1)
793 * Try to find the function again since an NMI
794 * could have added it
796 rec = ftrace_find_profiled_func(stat, ip);
800 if (stat->pages->index == PROFILES_PER_PAGE) {
801 if (!stat->pages->next)
803 stat->pages = stat->pages->next;
806 rec = &stat->pages->records[stat->pages->index++];
808 ftrace_add_profile(stat, rec);
811 atomic_dec(&stat->disabled);
817 function_profile_call(unsigned long ip, unsigned long parent_ip,
818 struct ftrace_ops *ops, struct pt_regs *regs)
820 struct ftrace_profile_stat *stat;
821 struct ftrace_profile *rec;
824 if (!ftrace_profile_enabled)
827 local_irq_save(flags);
829 stat = this_cpu_ptr(&ftrace_profile_stats);
830 if (!stat->hash || !ftrace_profile_enabled)
833 rec = ftrace_find_profiled_func(stat, ip);
835 rec = ftrace_profile_alloc(stat, ip);
842 local_irq_restore(flags);
845 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
846 static int profile_graph_entry(struct ftrace_graph_ent *trace)
848 int index = trace->depth;
850 function_profile_call(trace->func, 0, NULL, NULL);
852 /* If function graph is shutting down, ret_stack can be NULL */
853 if (!current->ret_stack)
856 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
857 current->ret_stack[index].subtime = 0;
862 static void profile_graph_return(struct ftrace_graph_ret *trace)
864 struct ftrace_profile_stat *stat;
865 unsigned long long calltime;
866 struct ftrace_profile *rec;
869 local_irq_save(flags);
870 stat = this_cpu_ptr(&ftrace_profile_stats);
871 if (!stat->hash || !ftrace_profile_enabled)
874 /* If the calltime was zero'd ignore it */
875 if (!trace->calltime)
878 calltime = trace->rettime - trace->calltime;
880 if (!fgraph_graph_time) {
883 index = trace->depth;
885 /* Append this call time to the parent time to subtract */
887 current->ret_stack[index - 1].subtime += calltime;
889 if (current->ret_stack[index].subtime < calltime)
890 calltime -= current->ret_stack[index].subtime;
895 rec = ftrace_find_profiled_func(stat, trace->func);
897 rec->time += calltime;
898 rec->time_squared += calltime * calltime;
902 local_irq_restore(flags);
905 static int register_ftrace_profiler(void)
907 return register_ftrace_graph(&profile_graph_return,
908 &profile_graph_entry);
911 static void unregister_ftrace_profiler(void)
913 unregister_ftrace_graph();
916 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
917 .func = function_profile_call,
918 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
919 INIT_OPS_HASH(ftrace_profile_ops)
922 static int register_ftrace_profiler(void)
924 return register_ftrace_function(&ftrace_profile_ops);
927 static void unregister_ftrace_profiler(void)
929 unregister_ftrace_function(&ftrace_profile_ops);
931 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
934 ftrace_profile_write(struct file *filp, const char __user *ubuf,
935 size_t cnt, loff_t *ppos)
940 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
946 mutex_lock(&ftrace_profile_lock);
947 if (ftrace_profile_enabled ^ val) {
949 ret = ftrace_profile_init();
955 ret = register_ftrace_profiler();
960 ftrace_profile_enabled = 1;
962 ftrace_profile_enabled = 0;
964 * unregister_ftrace_profiler calls stop_machine
965 * so this acts like an synchronize_sched.
967 unregister_ftrace_profiler();
971 mutex_unlock(&ftrace_profile_lock);
979 ftrace_profile_read(struct file *filp, char __user *ubuf,
980 size_t cnt, loff_t *ppos)
982 char buf[64]; /* big enough to hold a number */
985 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
986 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
989 static const struct file_operations ftrace_profile_fops = {
990 .open = tracing_open_generic,
991 .read = ftrace_profile_read,
992 .write = ftrace_profile_write,
993 .llseek = default_llseek,
996 /* used to initialize the real stat files */
997 static struct tracer_stat function_stats __initdata = {
999 .stat_start = function_stat_start,
1000 .stat_next = function_stat_next,
1001 .stat_cmp = function_stat_cmp,
1002 .stat_headers = function_stat_headers,
1003 .stat_show = function_stat_show
1006 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1008 struct ftrace_profile_stat *stat;
1009 struct dentry *entry;
1014 for_each_possible_cpu(cpu) {
1015 stat = &per_cpu(ftrace_profile_stats, cpu);
1017 name = kasprintf(GFP_KERNEL, "function%d", cpu);
1020 * The files created are permanent, if something happens
1021 * we still do not free memory.
1024 "Could not allocate stat file for cpu %d\n",
1028 stat->stat = function_stats;
1029 stat->stat.name = name;
1030 ret = register_stat_tracer(&stat->stat);
1033 "Could not register function stat for cpu %d\n",
1040 entry = tracefs_create_file("function_profile_enabled", 0644,
1041 d_tracer, NULL, &ftrace_profile_fops);
1043 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1046 #else /* CONFIG_FUNCTION_PROFILER */
1047 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1050 #endif /* CONFIG_FUNCTION_PROFILER */
1052 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1054 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1055 static int ftrace_graph_active;
1057 # define ftrace_graph_active 0
1060 #ifdef CONFIG_DYNAMIC_FTRACE
1062 static struct ftrace_ops *removed_ops;
1065 * Set when doing a global update, like enabling all recs or disabling them.
1066 * It is not set when just updating a single ftrace_ops.
1068 static bool update_all_ops;
1070 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1071 # error Dynamic ftrace depends on MCOUNT_RECORD
1074 struct ftrace_func_entry {
1075 struct hlist_node hlist;
1079 struct ftrace_func_probe {
1080 struct ftrace_probe_ops *probe_ops;
1081 struct ftrace_ops ops;
1082 struct trace_array *tr;
1083 struct list_head list;
1089 * We make these constant because no one should touch them,
1090 * but they are used as the default "empty hash", to avoid allocating
1091 * it all the time. These are in a read only section such that if
1092 * anyone does try to modify it, it will cause an exception.
1094 static const struct hlist_head empty_buckets[1];
1095 static const struct ftrace_hash empty_hash = {
1096 .buckets = (struct hlist_head *)empty_buckets,
1098 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1100 static struct ftrace_ops global_ops = {
1101 .func = ftrace_stub,
1102 .local_hash.notrace_hash = EMPTY_HASH,
1103 .local_hash.filter_hash = EMPTY_HASH,
1104 INIT_OPS_HASH(global_ops)
1105 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1106 FTRACE_OPS_FL_INITIALIZED |
1111 * Used by the stack undwinder to know about dynamic ftrace trampolines.
1113 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1115 struct ftrace_ops *op = NULL;
1118 * Some of the ops may be dynamically allocated,
1119 * they are freed after a synchronize_sched().
1121 preempt_disable_notrace();
1123 do_for_each_ftrace_op(op, ftrace_ops_list) {
1125 * This is to check for dynamically allocated trampolines.
1126 * Trampolines that are in kernel text will have
1127 * core_kernel_text() return true.
1129 if (op->trampoline && op->trampoline_size)
1130 if (addr >= op->trampoline &&
1131 addr < op->trampoline + op->trampoline_size) {
1132 preempt_enable_notrace();
1135 } while_for_each_ftrace_op(op);
1136 preempt_enable_notrace();
1142 * This is used by __kernel_text_address() to return true if the
1143 * address is on a dynamically allocated trampoline that would
1144 * not return true for either core_kernel_text() or
1145 * is_module_text_address().
1147 bool is_ftrace_trampoline(unsigned long addr)
1149 return ftrace_ops_trampoline(addr) != NULL;
1152 struct ftrace_page {
1153 struct ftrace_page *next;
1154 struct dyn_ftrace *records;
1159 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1160 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1162 /* estimate from running different kernels */
1163 #define NR_TO_INIT 10000
1165 static struct ftrace_page *ftrace_pages_start;
1166 static struct ftrace_page *ftrace_pages;
1168 static __always_inline unsigned long
1169 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1171 if (hash->size_bits > 0)
1172 return hash_long(ip, hash->size_bits);
1177 /* Only use this function if ftrace_hash_empty() has already been tested */
1178 static __always_inline struct ftrace_func_entry *
1179 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1182 struct ftrace_func_entry *entry;
1183 struct hlist_head *hhd;
1185 key = ftrace_hash_key(hash, ip);
1186 hhd = &hash->buckets[key];
1188 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1189 if (entry->ip == ip)
1196 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1197 * @hash: The hash to look at
1198 * @ip: The instruction pointer to test
1200 * Search a given @hash to see if a given instruction pointer (@ip)
1203 * Returns the entry that holds the @ip if found. NULL otherwise.
1205 struct ftrace_func_entry *
1206 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1208 if (ftrace_hash_empty(hash))
1211 return __ftrace_lookup_ip(hash, ip);
1214 static void __add_hash_entry(struct ftrace_hash *hash,
1215 struct ftrace_func_entry *entry)
1217 struct hlist_head *hhd;
1220 key = ftrace_hash_key(hash, entry->ip);
1221 hhd = &hash->buckets[key];
1222 hlist_add_head(&entry->hlist, hhd);
1226 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1228 struct ftrace_func_entry *entry;
1230 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1235 __add_hash_entry(hash, entry);
1241 free_hash_entry(struct ftrace_hash *hash,
1242 struct ftrace_func_entry *entry)
1244 hlist_del(&entry->hlist);
1250 remove_hash_entry(struct ftrace_hash *hash,
1251 struct ftrace_func_entry *entry)
1253 hlist_del_rcu(&entry->hlist);
1257 static void ftrace_hash_clear(struct ftrace_hash *hash)
1259 struct hlist_head *hhd;
1260 struct hlist_node *tn;
1261 struct ftrace_func_entry *entry;
1262 int size = 1 << hash->size_bits;
1268 for (i = 0; i < size; i++) {
1269 hhd = &hash->buckets[i];
1270 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1271 free_hash_entry(hash, entry);
1273 FTRACE_WARN_ON(hash->count);
1276 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1278 list_del(&ftrace_mod->list);
1279 kfree(ftrace_mod->module);
1280 kfree(ftrace_mod->func);
1284 static void clear_ftrace_mod_list(struct list_head *head)
1286 struct ftrace_mod_load *p, *n;
1288 /* stack tracer isn't supported yet */
1292 mutex_lock(&ftrace_lock);
1293 list_for_each_entry_safe(p, n, head, list)
1295 mutex_unlock(&ftrace_lock);
1298 static void free_ftrace_hash(struct ftrace_hash *hash)
1300 if (!hash || hash == EMPTY_HASH)
1302 ftrace_hash_clear(hash);
1303 kfree(hash->buckets);
1307 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1309 struct ftrace_hash *hash;
1311 hash = container_of(rcu, struct ftrace_hash, rcu);
1312 free_ftrace_hash(hash);
1315 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1317 if (!hash || hash == EMPTY_HASH)
1319 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1322 void ftrace_free_filter(struct ftrace_ops *ops)
1324 ftrace_ops_init(ops);
1325 free_ftrace_hash(ops->func_hash->filter_hash);
1326 free_ftrace_hash(ops->func_hash->notrace_hash);
1329 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1331 struct ftrace_hash *hash;
1334 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1338 size = 1 << size_bits;
1339 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1341 if (!hash->buckets) {
1346 hash->size_bits = size_bits;
1352 static int ftrace_add_mod(struct trace_array *tr,
1353 const char *func, const char *module,
1356 struct ftrace_mod_load *ftrace_mod;
1357 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1359 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1363 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1364 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1365 ftrace_mod->enable = enable;
1367 if (!ftrace_mod->func || !ftrace_mod->module)
1370 list_add(&ftrace_mod->list, mod_head);
1375 free_ftrace_mod(ftrace_mod);
1380 static struct ftrace_hash *
1381 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1383 struct ftrace_func_entry *entry;
1384 struct ftrace_hash *new_hash;
1389 new_hash = alloc_ftrace_hash(size_bits);
1394 new_hash->flags = hash->flags;
1397 if (ftrace_hash_empty(hash))
1400 size = 1 << hash->size_bits;
1401 for (i = 0; i < size; i++) {
1402 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1403 ret = add_hash_entry(new_hash, entry->ip);
1409 FTRACE_WARN_ON(new_hash->count != hash->count);
1414 free_ftrace_hash(new_hash);
1419 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1421 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1423 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1424 struct ftrace_hash *new_hash);
1426 static struct ftrace_hash *
1427 __ftrace_hash_move(struct ftrace_hash *src)
1429 struct ftrace_func_entry *entry;
1430 struct hlist_node *tn;
1431 struct hlist_head *hhd;
1432 struct ftrace_hash *new_hash;
1433 int size = src->count;
1438 * If the new source is empty, just return the empty_hash.
1440 if (ftrace_hash_empty(src))
1444 * Make the hash size about 1/2 the # found
1446 for (size /= 2; size; size >>= 1)
1449 /* Don't allocate too much */
1450 if (bits > FTRACE_HASH_MAX_BITS)
1451 bits = FTRACE_HASH_MAX_BITS;
1453 new_hash = alloc_ftrace_hash(bits);
1457 new_hash->flags = src->flags;
1459 size = 1 << src->size_bits;
1460 for (i = 0; i < size; i++) {
1461 hhd = &src->buckets[i];
1462 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1463 remove_hash_entry(src, entry);
1464 __add_hash_entry(new_hash, entry);
1472 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1473 struct ftrace_hash **dst, struct ftrace_hash *src)
1475 struct ftrace_hash *new_hash;
1478 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1479 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1482 new_hash = __ftrace_hash_move(src);
1486 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1488 /* IPMODIFY should be updated only when filter_hash updating */
1489 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1491 free_ftrace_hash(new_hash);
1497 * Remove the current set, update the hash and add
1500 ftrace_hash_rec_disable_modify(ops, enable);
1502 rcu_assign_pointer(*dst, new_hash);
1504 ftrace_hash_rec_enable_modify(ops, enable);
1509 static bool hash_contains_ip(unsigned long ip,
1510 struct ftrace_ops_hash *hash)
1513 * The function record is a match if it exists in the filter
1514 * hash and not in the notrace hash. Note, an emty hash is
1515 * considered a match for the filter hash, but an empty
1516 * notrace hash is considered not in the notrace hash.
1518 return (ftrace_hash_empty(hash->filter_hash) ||
1519 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1520 (ftrace_hash_empty(hash->notrace_hash) ||
1521 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1525 * Test the hashes for this ops to see if we want to call
1526 * the ops->func or not.
1528 * It's a match if the ip is in the ops->filter_hash or
1529 * the filter_hash does not exist or is empty,
1531 * the ip is not in the ops->notrace_hash.
1533 * This needs to be called with preemption disabled as
1534 * the hashes are freed with call_rcu_sched().
1537 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1539 struct ftrace_ops_hash hash;
1542 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1544 * There's a small race when adding ops that the ftrace handler
1545 * that wants regs, may be called without them. We can not
1546 * allow that handler to be called if regs is NULL.
1548 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1552 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1553 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1555 if (hash_contains_ip(ip, &hash))
1564 * This is a double for. Do not use 'break' to break out of the loop,
1565 * you must use a goto.
1567 #define do_for_each_ftrace_rec(pg, rec) \
1568 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1570 for (_____i = 0; _____i < pg->index; _____i++) { \
1571 rec = &pg->records[_____i];
1573 #define while_for_each_ftrace_rec() \
1578 static int ftrace_cmp_recs(const void *a, const void *b)
1580 const struct dyn_ftrace *key = a;
1581 const struct dyn_ftrace *rec = b;
1583 if (key->flags < rec->ip)
1585 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1591 * ftrace_location_range - return the first address of a traced location
1592 * if it touches the given ip range
1593 * @start: start of range to search.
1594 * @end: end of range to search (inclusive). @end points to the last byte
1597 * Returns rec->ip if the related ftrace location is a least partly within
1598 * the given address range. That is, the first address of the instruction
1599 * that is either a NOP or call to the function tracer. It checks the ftrace
1600 * internal tables to determine if the address belongs or not.
1602 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1604 struct ftrace_page *pg;
1605 struct dyn_ftrace *rec;
1606 struct dyn_ftrace key;
1609 key.flags = end; /* overload flags, as it is unsigned long */
1611 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1612 if (end < pg->records[0].ip ||
1613 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1615 rec = bsearch(&key, pg->records, pg->index,
1616 sizeof(struct dyn_ftrace),
1626 * ftrace_location - return true if the ip giving is a traced location
1627 * @ip: the instruction pointer to check
1629 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1630 * That is, the instruction that is either a NOP or call to
1631 * the function tracer. It checks the ftrace internal tables to
1632 * determine if the address belongs or not.
1634 unsigned long ftrace_location(unsigned long ip)
1636 return ftrace_location_range(ip, ip);
1640 * ftrace_text_reserved - return true if range contains an ftrace location
1641 * @start: start of range to search
1642 * @end: end of range to search (inclusive). @end points to the last byte to check.
1644 * Returns 1 if @start and @end contains a ftrace location.
1645 * That is, the instruction that is either a NOP or call to
1646 * the function tracer. It checks the ftrace internal tables to
1647 * determine if the address belongs or not.
1649 int ftrace_text_reserved(const void *start, const void *end)
1653 ret = ftrace_location_range((unsigned long)start,
1654 (unsigned long)end);
1659 /* Test if ops registered to this rec needs regs */
1660 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1662 struct ftrace_ops *ops;
1663 bool keep_regs = false;
1665 for (ops = ftrace_ops_list;
1666 ops != &ftrace_list_end; ops = ops->next) {
1667 /* pass rec in as regs to have non-NULL val */
1668 if (ftrace_ops_test(ops, rec->ip, rec)) {
1669 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1679 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1683 struct ftrace_hash *hash;
1684 struct ftrace_hash *other_hash;
1685 struct ftrace_page *pg;
1686 struct dyn_ftrace *rec;
1687 bool update = false;
1691 /* Only update if the ops has been registered */
1692 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1696 * In the filter_hash case:
1697 * If the count is zero, we update all records.
1698 * Otherwise we just update the items in the hash.
1700 * In the notrace_hash case:
1701 * We enable the update in the hash.
1702 * As disabling notrace means enabling the tracing,
1703 * and enabling notrace means disabling, the inc variable
1707 hash = ops->func_hash->filter_hash;
1708 other_hash = ops->func_hash->notrace_hash;
1709 if (ftrace_hash_empty(hash))
1713 hash = ops->func_hash->notrace_hash;
1714 other_hash = ops->func_hash->filter_hash;
1716 * If the notrace hash has no items,
1717 * then there's nothing to do.
1719 if (ftrace_hash_empty(hash))
1723 do_for_each_ftrace_rec(pg, rec) {
1724 int in_other_hash = 0;
1728 if (rec->flags & FTRACE_FL_DISABLED)
1733 * Only the filter_hash affects all records.
1734 * Update if the record is not in the notrace hash.
1736 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1739 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1740 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1743 * If filter_hash is set, we want to match all functions
1744 * that are in the hash but not in the other hash.
1746 * If filter_hash is not set, then we are decrementing.
1747 * That means we match anything that is in the hash
1748 * and also in the other_hash. That is, we need to turn
1749 * off functions in the other hash because they are disabled
1752 if (filter_hash && in_hash && !in_other_hash)
1754 else if (!filter_hash && in_hash &&
1755 (in_other_hash || ftrace_hash_empty(other_hash)))
1763 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1767 * If there's only a single callback registered to a
1768 * function, and the ops has a trampoline registered
1769 * for it, then we can call it directly.
1771 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1772 rec->flags |= FTRACE_FL_TRAMP;
1775 * If we are adding another function callback
1776 * to this function, and the previous had a
1777 * custom trampoline in use, then we need to go
1778 * back to the default trampoline.
1780 rec->flags &= ~FTRACE_FL_TRAMP;
1783 * If any ops wants regs saved for this function
1784 * then all ops will get saved regs.
1786 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1787 rec->flags |= FTRACE_FL_REGS;
1789 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1794 * If the rec had REGS enabled and the ops that is
1795 * being removed had REGS set, then see if there is
1796 * still any ops for this record that wants regs.
1797 * If not, we can stop recording them.
1799 if (ftrace_rec_count(rec) > 0 &&
1800 rec->flags & FTRACE_FL_REGS &&
1801 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1802 if (!test_rec_ops_needs_regs(rec))
1803 rec->flags &= ~FTRACE_FL_REGS;
1807 * If the rec had TRAMP enabled, then it needs to
1808 * be cleared. As TRAMP can only be enabled iff
1809 * there is only a single ops attached to it.
1810 * In otherwords, always disable it on decrementing.
1811 * In the future, we may set it if rec count is
1812 * decremented to one, and the ops that is left
1815 rec->flags &= ~FTRACE_FL_TRAMP;
1818 * flags will be cleared in ftrace_check_record()
1819 * if rec count is zero.
1824 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1825 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1827 /* Shortcut, if we handled all records, we are done. */
1828 if (!all && count == hash->count)
1830 } while_for_each_ftrace_rec();
1835 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1838 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1841 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1844 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1847 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1848 int filter_hash, int inc)
1850 struct ftrace_ops *op;
1852 __ftrace_hash_rec_update(ops, filter_hash, inc);
1854 if (ops->func_hash != &global_ops.local_hash)
1858 * If the ops shares the global_ops hash, then we need to update
1859 * all ops that are enabled and use this hash.
1861 do_for_each_ftrace_op(op, ftrace_ops_list) {
1865 if (op->func_hash == &global_ops.local_hash)
1866 __ftrace_hash_rec_update(op, filter_hash, inc);
1867 } while_for_each_ftrace_op(op);
1870 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1873 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1876 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1879 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1883 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1884 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1885 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1886 * Note that old_hash and new_hash has below meanings
1887 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1888 * - If the hash is EMPTY_HASH, it hits nothing
1889 * - Anything else hits the recs which match the hash entries.
1891 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1892 struct ftrace_hash *old_hash,
1893 struct ftrace_hash *new_hash)
1895 struct ftrace_page *pg;
1896 struct dyn_ftrace *rec, *end = NULL;
1899 /* Only update if the ops has been registered */
1900 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1903 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1907 * Since the IPMODIFY is a very address sensitive action, we do not
1908 * allow ftrace_ops to set all functions to new hash.
1910 if (!new_hash || !old_hash)
1913 /* Update rec->flags */
1914 do_for_each_ftrace_rec(pg, rec) {
1916 if (rec->flags & FTRACE_FL_DISABLED)
1919 /* We need to update only differences of filter_hash */
1920 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1921 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1922 if (in_old == in_new)
1926 /* New entries must ensure no others are using it */
1927 if (rec->flags & FTRACE_FL_IPMODIFY)
1929 rec->flags |= FTRACE_FL_IPMODIFY;
1930 } else /* Removed entry */
1931 rec->flags &= ~FTRACE_FL_IPMODIFY;
1932 } while_for_each_ftrace_rec();
1939 /* Roll back what we did above */
1940 do_for_each_ftrace_rec(pg, rec) {
1942 if (rec->flags & FTRACE_FL_DISABLED)
1948 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1949 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1950 if (in_old == in_new)
1954 rec->flags &= ~FTRACE_FL_IPMODIFY;
1956 rec->flags |= FTRACE_FL_IPMODIFY;
1957 } while_for_each_ftrace_rec();
1963 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1965 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1967 if (ftrace_hash_empty(hash))
1970 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1973 /* Disabling always succeeds */
1974 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1976 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1978 if (ftrace_hash_empty(hash))
1981 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1984 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1985 struct ftrace_hash *new_hash)
1987 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1989 if (ftrace_hash_empty(old_hash))
1992 if (ftrace_hash_empty(new_hash))
1995 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1998 static void print_ip_ins(const char *fmt, const unsigned char *p)
2002 printk(KERN_CONT "%s", fmt);
2004 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
2005 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
2008 static struct ftrace_ops *
2009 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
2010 static struct ftrace_ops *
2011 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
2013 enum ftrace_bug_type ftrace_bug_type;
2014 const void *ftrace_expected;
2016 static void print_bug_type(void)
2018 switch (ftrace_bug_type) {
2019 case FTRACE_BUG_UNKNOWN:
2021 case FTRACE_BUG_INIT:
2022 pr_info("Initializing ftrace call sites\n");
2024 case FTRACE_BUG_NOP:
2025 pr_info("Setting ftrace call site to NOP\n");
2027 case FTRACE_BUG_CALL:
2028 pr_info("Setting ftrace call site to call ftrace function\n");
2030 case FTRACE_BUG_UPDATE:
2031 pr_info("Updating ftrace call site to call a different ftrace function\n");
2037 * ftrace_bug - report and shutdown function tracer
2038 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2039 * @rec: The record that failed
2041 * The arch code that enables or disables the function tracing
2042 * can call ftrace_bug() when it has detected a problem in
2043 * modifying the code. @failed should be one of either:
2044 * EFAULT - if the problem happens on reading the @ip address
2045 * EINVAL - if what is read at @ip is not what was expected
2046 * EPERM - if the problem happens on writting to the @ip address
2048 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2050 unsigned long ip = rec ? rec->ip : 0;
2054 FTRACE_WARN_ON_ONCE(1);
2055 pr_info("ftrace faulted on modifying ");
2059 FTRACE_WARN_ON_ONCE(1);
2060 pr_info("ftrace failed to modify ");
2062 print_ip_ins(" actual: ", (unsigned char *)ip);
2064 if (ftrace_expected) {
2065 print_ip_ins(" expected: ", ftrace_expected);
2070 FTRACE_WARN_ON_ONCE(1);
2071 pr_info("ftrace faulted on writing ");
2075 FTRACE_WARN_ON_ONCE(1);
2076 pr_info("ftrace faulted on unknown error ");
2081 struct ftrace_ops *ops = NULL;
2083 pr_info("ftrace record flags: %lx\n", rec->flags);
2084 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2085 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2086 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2087 ops = ftrace_find_tramp_ops_any(rec);
2090 pr_cont("\ttramp: %pS (%pS)",
2091 (void *)ops->trampoline,
2093 ops = ftrace_find_tramp_ops_next(rec, ops);
2096 pr_cont("\ttramp: ERROR!");
2099 ip = ftrace_get_addr_curr(rec);
2100 pr_cont("\n expected tramp: %lx\n", ip);
2104 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2106 unsigned long flag = 0UL;
2108 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2110 if (rec->flags & FTRACE_FL_DISABLED)
2111 return FTRACE_UPDATE_IGNORE;
2114 * If we are updating calls:
2116 * If the record has a ref count, then we need to enable it
2117 * because someone is using it.
2119 * Otherwise we make sure its disabled.
2121 * If we are disabling calls, then disable all records that
2124 if (enable && ftrace_rec_count(rec))
2125 flag = FTRACE_FL_ENABLED;
2128 * If enabling and the REGS flag does not match the REGS_EN, or
2129 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2130 * this record. Set flags to fail the compare against ENABLED.
2133 if (!(rec->flags & FTRACE_FL_REGS) !=
2134 !(rec->flags & FTRACE_FL_REGS_EN))
2135 flag |= FTRACE_FL_REGS;
2137 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2138 !(rec->flags & FTRACE_FL_TRAMP_EN))
2139 flag |= FTRACE_FL_TRAMP;
2142 /* If the state of this record hasn't changed, then do nothing */
2143 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2144 return FTRACE_UPDATE_IGNORE;
2147 /* Save off if rec is being enabled (for return value) */
2148 flag ^= rec->flags & FTRACE_FL_ENABLED;
2151 rec->flags |= FTRACE_FL_ENABLED;
2152 if (flag & FTRACE_FL_REGS) {
2153 if (rec->flags & FTRACE_FL_REGS)
2154 rec->flags |= FTRACE_FL_REGS_EN;
2156 rec->flags &= ~FTRACE_FL_REGS_EN;
2158 if (flag & FTRACE_FL_TRAMP) {
2159 if (rec->flags & FTRACE_FL_TRAMP)
2160 rec->flags |= FTRACE_FL_TRAMP_EN;
2162 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2167 * If this record is being updated from a nop, then
2168 * return UPDATE_MAKE_CALL.
2170 * return UPDATE_MODIFY_CALL to tell the caller to convert
2171 * from the save regs, to a non-save regs function or
2172 * vice versa, or from a trampoline call.
2174 if (flag & FTRACE_FL_ENABLED) {
2175 ftrace_bug_type = FTRACE_BUG_CALL;
2176 return FTRACE_UPDATE_MAKE_CALL;
2179 ftrace_bug_type = FTRACE_BUG_UPDATE;
2180 return FTRACE_UPDATE_MODIFY_CALL;
2184 /* If there's no more users, clear all flags */
2185 if (!ftrace_rec_count(rec))
2189 * Just disable the record, but keep the ops TRAMP
2190 * and REGS states. The _EN flags must be disabled though.
2192 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2196 ftrace_bug_type = FTRACE_BUG_NOP;
2197 return FTRACE_UPDATE_MAKE_NOP;
2201 * ftrace_update_record, set a record that now is tracing or not
2202 * @rec: the record to update
2203 * @enable: set to 1 if the record is tracing, zero to force disable
2205 * The records that represent all functions that can be traced need
2206 * to be updated when tracing has been enabled.
2208 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2210 return ftrace_check_record(rec, enable, 1);
2214 * ftrace_test_record, check if the record has been enabled or not
2215 * @rec: the record to test
2216 * @enable: set to 1 to check if enabled, 0 if it is disabled
2218 * The arch code may need to test if a record is already set to
2219 * tracing to determine how to modify the function code that it
2222 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2224 return ftrace_check_record(rec, enable, 0);
2227 static struct ftrace_ops *
2228 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2230 struct ftrace_ops *op;
2231 unsigned long ip = rec->ip;
2233 do_for_each_ftrace_op(op, ftrace_ops_list) {
2235 if (!op->trampoline)
2238 if (hash_contains_ip(ip, op->func_hash))
2240 } while_for_each_ftrace_op(op);
2245 static struct ftrace_ops *
2246 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2247 struct ftrace_ops *op)
2249 unsigned long ip = rec->ip;
2251 while_for_each_ftrace_op(op) {
2253 if (!op->trampoline)
2256 if (hash_contains_ip(ip, op->func_hash))
2263 static struct ftrace_ops *
2264 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2266 struct ftrace_ops *op;
2267 unsigned long ip = rec->ip;
2270 * Need to check removed ops first.
2271 * If they are being removed, and this rec has a tramp,
2272 * and this rec is in the ops list, then it would be the
2273 * one with the tramp.
2276 if (hash_contains_ip(ip, &removed_ops->old_hash))
2281 * Need to find the current trampoline for a rec.
2282 * Now, a trampoline is only attached to a rec if there
2283 * was a single 'ops' attached to it. But this can be called
2284 * when we are adding another op to the rec or removing the
2285 * current one. Thus, if the op is being added, we can
2286 * ignore it because it hasn't attached itself to the rec
2289 * If an ops is being modified (hooking to different functions)
2290 * then we don't care about the new functions that are being
2291 * added, just the old ones (that are probably being removed).
2293 * If we are adding an ops to a function that already is using
2294 * a trampoline, it needs to be removed (trampolines are only
2295 * for single ops connected), then an ops that is not being
2296 * modified also needs to be checked.
2298 do_for_each_ftrace_op(op, ftrace_ops_list) {
2300 if (!op->trampoline)
2304 * If the ops is being added, it hasn't gotten to
2305 * the point to be removed from this tree yet.
2307 if (op->flags & FTRACE_OPS_FL_ADDING)
2312 * If the ops is being modified and is in the old
2313 * hash, then it is probably being removed from this
2316 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2317 hash_contains_ip(ip, &op->old_hash))
2320 * If the ops is not being added or modified, and it's
2321 * in its normal filter hash, then this must be the one
2324 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2325 hash_contains_ip(ip, op->func_hash))
2328 } while_for_each_ftrace_op(op);
2333 static struct ftrace_ops *
2334 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2336 struct ftrace_ops *op;
2337 unsigned long ip = rec->ip;
2339 do_for_each_ftrace_op(op, ftrace_ops_list) {
2340 /* pass rec in as regs to have non-NULL val */
2341 if (hash_contains_ip(ip, op->func_hash))
2343 } while_for_each_ftrace_op(op);
2349 * ftrace_get_addr_new - Get the call address to set to
2350 * @rec: The ftrace record descriptor
2352 * If the record has the FTRACE_FL_REGS set, that means that it
2353 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2354 * is not not set, then it wants to convert to the normal callback.
2356 * Returns the address of the trampoline to set to
2358 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2360 struct ftrace_ops *ops;
2362 /* Trampolines take precedence over regs */
2363 if (rec->flags & FTRACE_FL_TRAMP) {
2364 ops = ftrace_find_tramp_ops_new(rec);
2365 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2366 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2367 (void *)rec->ip, (void *)rec->ip, rec->flags);
2368 /* Ftrace is shutting down, return anything */
2369 return (unsigned long)FTRACE_ADDR;
2371 return ops->trampoline;
2374 if (rec->flags & FTRACE_FL_REGS)
2375 return (unsigned long)FTRACE_REGS_ADDR;
2377 return (unsigned long)FTRACE_ADDR;
2381 * ftrace_get_addr_curr - Get the call address that is already there
2382 * @rec: The ftrace record descriptor
2384 * The FTRACE_FL_REGS_EN is set when the record already points to
2385 * a function that saves all the regs. Basically the '_EN' version
2386 * represents the current state of the function.
2388 * Returns the address of the trampoline that is currently being called
2390 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2392 struct ftrace_ops *ops;
2394 /* Trampolines take precedence over regs */
2395 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2396 ops = ftrace_find_tramp_ops_curr(rec);
2397 if (FTRACE_WARN_ON(!ops)) {
2398 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2399 (void *)rec->ip, (void *)rec->ip);
2400 /* Ftrace is shutting down, return anything */
2401 return (unsigned long)FTRACE_ADDR;
2403 return ops->trampoline;
2406 if (rec->flags & FTRACE_FL_REGS_EN)
2407 return (unsigned long)FTRACE_REGS_ADDR;
2409 return (unsigned long)FTRACE_ADDR;
2413 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2415 unsigned long ftrace_old_addr;
2416 unsigned long ftrace_addr;
2419 ftrace_addr = ftrace_get_addr_new(rec);
2421 /* This needs to be done before we call ftrace_update_record */
2422 ftrace_old_addr = ftrace_get_addr_curr(rec);
2424 ret = ftrace_update_record(rec, enable);
2426 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2429 case FTRACE_UPDATE_IGNORE:
2432 case FTRACE_UPDATE_MAKE_CALL:
2433 ftrace_bug_type = FTRACE_BUG_CALL;
2434 return ftrace_make_call(rec, ftrace_addr);
2436 case FTRACE_UPDATE_MAKE_NOP:
2437 ftrace_bug_type = FTRACE_BUG_NOP;
2438 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2440 case FTRACE_UPDATE_MODIFY_CALL:
2441 ftrace_bug_type = FTRACE_BUG_UPDATE;
2442 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2445 return -1; /* unknow ftrace bug */
2448 void __weak ftrace_replace_code(int enable)
2450 struct dyn_ftrace *rec;
2451 struct ftrace_page *pg;
2454 if (unlikely(ftrace_disabled))
2457 do_for_each_ftrace_rec(pg, rec) {
2459 if (rec->flags & FTRACE_FL_DISABLED)
2462 failed = __ftrace_replace_code(rec, enable);
2464 ftrace_bug(failed, rec);
2465 /* Stop processing */
2468 } while_for_each_ftrace_rec();
2471 struct ftrace_rec_iter {
2472 struct ftrace_page *pg;
2477 * ftrace_rec_iter_start, start up iterating over traced functions
2479 * Returns an iterator handle that is used to iterate over all
2480 * the records that represent address locations where functions
2483 * May return NULL if no records are available.
2485 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2488 * We only use a single iterator.
2489 * Protected by the ftrace_lock mutex.
2491 static struct ftrace_rec_iter ftrace_rec_iter;
2492 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2494 iter->pg = ftrace_pages_start;
2497 /* Could have empty pages */
2498 while (iter->pg && !iter->pg->index)
2499 iter->pg = iter->pg->next;
2508 * ftrace_rec_iter_next, get the next record to process.
2509 * @iter: The handle to the iterator.
2511 * Returns the next iterator after the given iterator @iter.
2513 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2517 if (iter->index >= iter->pg->index) {
2518 iter->pg = iter->pg->next;
2521 /* Could have empty pages */
2522 while (iter->pg && !iter->pg->index)
2523 iter->pg = iter->pg->next;
2533 * ftrace_rec_iter_record, get the record at the iterator location
2534 * @iter: The current iterator location
2536 * Returns the record that the current @iter is at.
2538 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2540 return &iter->pg->records[iter->index];
2544 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2548 if (unlikely(ftrace_disabled))
2551 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2553 ftrace_bug_type = FTRACE_BUG_INIT;
2554 ftrace_bug(ret, rec);
2561 * archs can override this function if they must do something
2562 * before the modifying code is performed.
2564 int __weak ftrace_arch_code_modify_prepare(void)
2570 * archs can override this function if they must do something
2571 * after the modifying code is performed.
2573 int __weak ftrace_arch_code_modify_post_process(void)
2578 void ftrace_modify_all_code(int command)
2580 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2584 * If the ftrace_caller calls a ftrace_ops func directly,
2585 * we need to make sure that it only traces functions it
2586 * expects to trace. When doing the switch of functions,
2587 * we need to update to the ftrace_ops_list_func first
2588 * before the transition between old and new calls are set,
2589 * as the ftrace_ops_list_func will check the ops hashes
2590 * to make sure the ops are having the right functions
2594 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2595 if (FTRACE_WARN_ON(err))
2599 if (command & FTRACE_UPDATE_CALLS)
2600 ftrace_replace_code(1);
2601 else if (command & FTRACE_DISABLE_CALLS)
2602 ftrace_replace_code(0);
2604 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2605 function_trace_op = set_function_trace_op;
2607 /* If irqs are disabled, we are in stop machine */
2608 if (!irqs_disabled())
2609 smp_call_function(ftrace_sync_ipi, NULL, 1);
2610 err = ftrace_update_ftrace_func(ftrace_trace_function);
2611 if (FTRACE_WARN_ON(err))
2615 if (command & FTRACE_START_FUNC_RET)
2616 err = ftrace_enable_ftrace_graph_caller();
2617 else if (command & FTRACE_STOP_FUNC_RET)
2618 err = ftrace_disable_ftrace_graph_caller();
2619 FTRACE_WARN_ON(err);
2622 static int __ftrace_modify_code(void *data)
2624 int *command = data;
2626 ftrace_modify_all_code(*command);
2632 * ftrace_run_stop_machine, go back to the stop machine method
2633 * @command: The command to tell ftrace what to do
2635 * If an arch needs to fall back to the stop machine method, the
2636 * it can call this function.
2638 void ftrace_run_stop_machine(int command)
2640 stop_machine(__ftrace_modify_code, &command, NULL);
2644 * arch_ftrace_update_code, modify the code to trace or not trace
2645 * @command: The command that needs to be done
2647 * Archs can override this function if it does not need to
2648 * run stop_machine() to modify code.
2650 void __weak arch_ftrace_update_code(int command)
2652 ftrace_run_stop_machine(command);
2655 static void ftrace_run_update_code(int command)
2659 ret = ftrace_arch_code_modify_prepare();
2660 FTRACE_WARN_ON(ret);
2665 * By default we use stop_machine() to modify the code.
2666 * But archs can do what ever they want as long as it
2667 * is safe. The stop_machine() is the safest, but also
2668 * produces the most overhead.
2670 arch_ftrace_update_code(command);
2672 ret = ftrace_arch_code_modify_post_process();
2673 FTRACE_WARN_ON(ret);
2676 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2677 struct ftrace_ops_hash *old_hash)
2679 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2680 ops->old_hash.filter_hash = old_hash->filter_hash;
2681 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2682 ftrace_run_update_code(command);
2683 ops->old_hash.filter_hash = NULL;
2684 ops->old_hash.notrace_hash = NULL;
2685 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2688 static ftrace_func_t saved_ftrace_func;
2689 static int ftrace_start_up;
2691 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2695 static void ftrace_startup_enable(int command)
2697 if (saved_ftrace_func != ftrace_trace_function) {
2698 saved_ftrace_func = ftrace_trace_function;
2699 command |= FTRACE_UPDATE_TRACE_FUNC;
2702 if (!command || !ftrace_enabled)
2705 ftrace_run_update_code(command);
2708 static void ftrace_startup_all(int command)
2710 update_all_ops = true;
2711 ftrace_startup_enable(command);
2712 update_all_ops = false;
2715 static int ftrace_startup(struct ftrace_ops *ops, int command)
2719 if (unlikely(ftrace_disabled))
2722 ret = __register_ftrace_function(ops);
2729 * Note that ftrace probes uses this to start up
2730 * and modify functions it will probe. But we still
2731 * set the ADDING flag for modification, as probes
2732 * do not have trampolines. If they add them in the
2733 * future, then the probes will need to distinguish
2734 * between adding and updating probes.
2736 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2738 ret = ftrace_hash_ipmodify_enable(ops);
2740 /* Rollback registration process */
2741 __unregister_ftrace_function(ops);
2743 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2747 if (ftrace_hash_rec_enable(ops, 1))
2748 command |= FTRACE_UPDATE_CALLS;
2750 ftrace_startup_enable(command);
2752 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2757 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2761 if (unlikely(ftrace_disabled))
2764 ret = __unregister_ftrace_function(ops);
2770 * Just warn in case of unbalance, no need to kill ftrace, it's not
2771 * critical but the ftrace_call callers may be never nopped again after
2772 * further ftrace uses.
2774 WARN_ON_ONCE(ftrace_start_up < 0);
2776 /* Disabling ipmodify never fails */
2777 ftrace_hash_ipmodify_disable(ops);
2779 if (ftrace_hash_rec_disable(ops, 1))
2780 command |= FTRACE_UPDATE_CALLS;
2782 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2784 if (saved_ftrace_func != ftrace_trace_function) {
2785 saved_ftrace_func = ftrace_trace_function;
2786 command |= FTRACE_UPDATE_TRACE_FUNC;
2789 if (!command || !ftrace_enabled) {
2791 * If these are dynamic or per_cpu ops, they still
2792 * need their data freed. Since, function tracing is
2793 * not currently active, we can just free them
2794 * without synchronizing all CPUs.
2796 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2803 * If the ops uses a trampoline, then it needs to be
2804 * tested first on update.
2806 ops->flags |= FTRACE_OPS_FL_REMOVING;
2809 /* The trampoline logic checks the old hashes */
2810 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2811 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2813 ftrace_run_update_code(command);
2816 * If there's no more ops registered with ftrace, run a
2817 * sanity check to make sure all rec flags are cleared.
2819 if (rcu_dereference_protected(ftrace_ops_list,
2820 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2821 struct ftrace_page *pg;
2822 struct dyn_ftrace *rec;
2824 do_for_each_ftrace_rec(pg, rec) {
2825 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2826 pr_warn(" %pS flags:%lx\n",
2827 (void *)rec->ip, rec->flags);
2828 } while_for_each_ftrace_rec();
2831 ops->old_hash.filter_hash = NULL;
2832 ops->old_hash.notrace_hash = NULL;
2835 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2838 * Dynamic ops may be freed, we must make sure that all
2839 * callers are done before leaving this function.
2840 * The same goes for freeing the per_cpu data of the per_cpu
2843 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
2845 * We need to do a hard force of sched synchronization.
2846 * This is because we use preempt_disable() to do RCU, but
2847 * the function tracers can be called where RCU is not watching
2848 * (like before user_exit()). We can not rely on the RCU
2849 * infrastructure to do the synchronization, thus we must do it
2852 schedule_on_each_cpu(ftrace_sync);
2855 * When the kernel is preeptive, tasks can be preempted
2856 * while on a ftrace trampoline. Just scheduling a task on
2857 * a CPU is not good enough to flush them. Calling
2858 * synchornize_rcu_tasks() will wait for those tasks to
2859 * execute and either schedule voluntarily or enter user space.
2861 if (IS_ENABLED(CONFIG_PREEMPT))
2862 synchronize_rcu_tasks();
2865 arch_ftrace_trampoline_free(ops);
2871 static void ftrace_startup_sysctl(void)
2875 if (unlikely(ftrace_disabled))
2878 /* Force update next time */
2879 saved_ftrace_func = NULL;
2880 /* ftrace_start_up is true if we want ftrace running */
2881 if (ftrace_start_up) {
2882 command = FTRACE_UPDATE_CALLS;
2883 if (ftrace_graph_active)
2884 command |= FTRACE_START_FUNC_RET;
2885 ftrace_startup_enable(command);
2889 static void ftrace_shutdown_sysctl(void)
2893 if (unlikely(ftrace_disabled))
2896 /* ftrace_start_up is true if ftrace is running */
2897 if (ftrace_start_up) {
2898 command = FTRACE_DISABLE_CALLS;
2899 if (ftrace_graph_active)
2900 command |= FTRACE_STOP_FUNC_RET;
2901 ftrace_run_update_code(command);
2905 static u64 ftrace_update_time;
2906 unsigned long ftrace_update_tot_cnt;
2908 static inline int ops_traces_mod(struct ftrace_ops *ops)
2911 * Filter_hash being empty will default to trace module.
2912 * But notrace hash requires a test of individual module functions.
2914 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2915 ftrace_hash_empty(ops->func_hash->notrace_hash);
2919 * Check if the current ops references the record.
2921 * If the ops traces all functions, then it was already accounted for.
2922 * If the ops does not trace the current record function, skip it.
2923 * If the ops ignores the function via notrace filter, skip it.
2926 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2928 /* If ops isn't enabled, ignore it */
2929 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2932 /* If ops traces all then it includes this function */
2933 if (ops_traces_mod(ops))
2936 /* The function must be in the filter */
2937 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2938 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2941 /* If in notrace hash, we ignore it too */
2942 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2948 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2950 struct ftrace_page *pg;
2951 struct dyn_ftrace *p;
2953 unsigned long update_cnt = 0;
2954 unsigned long rec_flags = 0;
2957 start = ftrace_now(raw_smp_processor_id());
2960 * When a module is loaded, this function is called to convert
2961 * the calls to mcount in its text to nops, and also to create
2962 * an entry in the ftrace data. Now, if ftrace is activated
2963 * after this call, but before the module sets its text to
2964 * read-only, the modification of enabling ftrace can fail if
2965 * the read-only is done while ftrace is converting the calls.
2966 * To prevent this, the module's records are set as disabled
2967 * and will be enabled after the call to set the module's text
2971 rec_flags |= FTRACE_FL_DISABLED;
2973 for (pg = new_pgs; pg; pg = pg->next) {
2975 for (i = 0; i < pg->index; i++) {
2977 /* If something went wrong, bail without enabling anything */
2978 if (unlikely(ftrace_disabled))
2981 p = &pg->records[i];
2982 p->flags = rec_flags;
2985 * Do the initial record conversion from mcount jump
2986 * to the NOP instructions.
2988 if (!ftrace_code_disable(mod, p))
2995 stop = ftrace_now(raw_smp_processor_id());
2996 ftrace_update_time = stop - start;
2997 ftrace_update_tot_cnt += update_cnt;
3002 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3007 if (WARN_ON(!count))
3010 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3013 * We want to fill as much as possible. No more than a page
3016 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3020 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3023 /* if we can't allocate this size, try something smaller */
3030 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3039 static struct ftrace_page *
3040 ftrace_allocate_pages(unsigned long num_to_init)
3042 struct ftrace_page *start_pg;
3043 struct ftrace_page *pg;
3050 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3055 * Try to allocate as much as possible in one continues
3056 * location that fills in all of the space. We want to
3057 * waste as little space as possible.
3060 cnt = ftrace_allocate_records(pg, num_to_init);
3068 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3080 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3081 free_pages((unsigned long)pg->records, order);
3082 start_pg = pg->next;
3086 pr_info("ftrace: FAILED to allocate memory for functions\n");
3090 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3092 struct ftrace_iterator {
3096 struct ftrace_page *pg;
3097 struct dyn_ftrace *func;
3098 struct ftrace_func_probe *probe;
3099 struct ftrace_func_entry *probe_entry;
3100 struct trace_parser parser;
3101 struct ftrace_hash *hash;
3102 struct ftrace_ops *ops;
3103 struct trace_array *tr;
3104 struct list_head *mod_list;
3111 t_probe_next(struct seq_file *m, loff_t *pos)
3113 struct ftrace_iterator *iter = m->private;
3114 struct trace_array *tr = iter->ops->private;
3115 struct list_head *func_probes;
3116 struct ftrace_hash *hash;
3117 struct list_head *next;
3118 struct hlist_node *hnd = NULL;
3119 struct hlist_head *hhd;
3128 func_probes = &tr->func_probes;
3129 if (list_empty(func_probes))
3133 next = func_probes->next;
3134 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3137 if (iter->probe_entry)
3138 hnd = &iter->probe_entry->hlist;
3140 hash = iter->probe->ops.func_hash->filter_hash;
3141 size = 1 << hash->size_bits;
3144 if (iter->pidx >= size) {
3145 if (iter->probe->list.next == func_probes)
3147 next = iter->probe->list.next;
3148 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3149 hash = iter->probe->ops.func_hash->filter_hash;
3150 size = 1 << hash->size_bits;
3154 hhd = &hash->buckets[iter->pidx];
3156 if (hlist_empty(hhd)) {
3172 if (WARN_ON_ONCE(!hnd))
3175 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3180 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3182 struct ftrace_iterator *iter = m->private;
3186 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3189 if (iter->mod_pos > *pos)
3193 iter->probe_entry = NULL;
3195 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3196 p = t_probe_next(m, &l);
3203 /* Only set this if we have an item */
3204 iter->flags |= FTRACE_ITER_PROBE;
3210 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3212 struct ftrace_func_entry *probe_entry;
3213 struct ftrace_probe_ops *probe_ops;
3214 struct ftrace_func_probe *probe;
3216 probe = iter->probe;
3217 probe_entry = iter->probe_entry;
3219 if (WARN_ON_ONCE(!probe || !probe_entry))
3222 probe_ops = probe->probe_ops;
3224 if (probe_ops->print)
3225 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3227 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3228 (void *)probe_ops->func);
3234 t_mod_next(struct seq_file *m, loff_t *pos)
3236 struct ftrace_iterator *iter = m->private;
3237 struct trace_array *tr = iter->tr;
3242 iter->mod_list = iter->mod_list->next;
3244 if (iter->mod_list == &tr->mod_trace ||
3245 iter->mod_list == &tr->mod_notrace) {
3246 iter->flags &= ~FTRACE_ITER_MOD;
3250 iter->mod_pos = *pos;
3255 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3257 struct ftrace_iterator *iter = m->private;
3261 if (iter->func_pos > *pos)
3264 iter->mod_pos = iter->func_pos;
3266 /* probes are only available if tr is set */
3270 for (l = 0; l <= (*pos - iter->func_pos); ) {
3271 p = t_mod_next(m, &l);
3276 iter->flags &= ~FTRACE_ITER_MOD;
3277 return t_probe_start(m, pos);
3280 /* Only set this if we have an item */
3281 iter->flags |= FTRACE_ITER_MOD;
3287 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3289 struct ftrace_mod_load *ftrace_mod;
3290 struct trace_array *tr = iter->tr;
3292 if (WARN_ON_ONCE(!iter->mod_list) ||
3293 iter->mod_list == &tr->mod_trace ||
3294 iter->mod_list == &tr->mod_notrace)
3297 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3299 if (ftrace_mod->func)
3300 seq_printf(m, "%s", ftrace_mod->func);
3304 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3310 t_func_next(struct seq_file *m, loff_t *pos)
3312 struct ftrace_iterator *iter = m->private;
3313 struct dyn_ftrace *rec = NULL;
3318 if (iter->idx >= iter->pg->index) {
3319 if (iter->pg->next) {
3320 iter->pg = iter->pg->next;
3325 rec = &iter->pg->records[iter->idx++];
3326 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3327 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3329 ((iter->flags & FTRACE_ITER_ENABLED) &&
3330 !(rec->flags & FTRACE_FL_ENABLED))) {
3340 iter->pos = iter->func_pos = *pos;
3347 t_next(struct seq_file *m, void *v, loff_t *pos)
3349 struct ftrace_iterator *iter = m->private;
3350 loff_t l = *pos; /* t_probe_start() must use original pos */
3353 if (unlikely(ftrace_disabled))
3356 if (iter->flags & FTRACE_ITER_PROBE)
3357 return t_probe_next(m, pos);
3359 if (iter->flags & FTRACE_ITER_MOD)
3360 return t_mod_next(m, pos);
3362 if (iter->flags & FTRACE_ITER_PRINTALL) {
3363 /* next must increment pos, and t_probe_start does not */
3365 return t_mod_start(m, &l);
3368 ret = t_func_next(m, pos);
3371 return t_mod_start(m, &l);
3376 static void reset_iter_read(struct ftrace_iterator *iter)
3380 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3383 static void *t_start(struct seq_file *m, loff_t *pos)
3385 struct ftrace_iterator *iter = m->private;
3389 mutex_lock(&ftrace_lock);
3391 if (unlikely(ftrace_disabled))
3395 * If an lseek was done, then reset and start from beginning.
3397 if (*pos < iter->pos)
3398 reset_iter_read(iter);
3401 * For set_ftrace_filter reading, if we have the filter
3402 * off, we can short cut and just print out that all
3403 * functions are enabled.
3405 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3406 ftrace_hash_empty(iter->hash)) {
3407 iter->func_pos = 1; /* Account for the message */
3409 return t_mod_start(m, pos);
3410 iter->flags |= FTRACE_ITER_PRINTALL;
3411 /* reset in case of seek/pread */
3412 iter->flags &= ~FTRACE_ITER_PROBE;
3416 if (iter->flags & FTRACE_ITER_MOD)
3417 return t_mod_start(m, pos);
3420 * Unfortunately, we need to restart at ftrace_pages_start
3421 * every time we let go of the ftrace_mutex. This is because
3422 * those pointers can change without the lock.
3424 iter->pg = ftrace_pages_start;
3426 for (l = 0; l <= *pos; ) {
3427 p = t_func_next(m, &l);
3433 return t_mod_start(m, pos);
3438 static void t_stop(struct seq_file *m, void *p)
3440 mutex_unlock(&ftrace_lock);
3444 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3449 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3450 struct dyn_ftrace *rec)
3454 ptr = arch_ftrace_trampoline_func(ops, rec);
3456 seq_printf(m, " ->%pS", ptr);
3459 static int t_show(struct seq_file *m, void *v)
3461 struct ftrace_iterator *iter = m->private;
3462 struct dyn_ftrace *rec;
3464 if (iter->flags & FTRACE_ITER_PROBE)
3465 return t_probe_show(m, iter);
3467 if (iter->flags & FTRACE_ITER_MOD)
3468 return t_mod_show(m, iter);
3470 if (iter->flags & FTRACE_ITER_PRINTALL) {
3471 if (iter->flags & FTRACE_ITER_NOTRACE)
3472 seq_puts(m, "#### no functions disabled ####\n");
3474 seq_puts(m, "#### all functions enabled ####\n");
3483 seq_printf(m, "%ps", (void *)rec->ip);
3484 if (iter->flags & FTRACE_ITER_ENABLED) {
3485 struct ftrace_ops *ops;
3487 seq_printf(m, " (%ld)%s%s",
3488 ftrace_rec_count(rec),
3489 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3490 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3491 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3492 ops = ftrace_find_tramp_ops_any(rec);
3495 seq_printf(m, "\ttramp: %pS (%pS)",
3496 (void *)ops->trampoline,
3498 add_trampoline_func(m, ops, rec);
3499 ops = ftrace_find_tramp_ops_next(rec, ops);
3502 seq_puts(m, "\ttramp: ERROR!");
3504 add_trampoline_func(m, NULL, rec);
3513 static const struct seq_operations show_ftrace_seq_ops = {
3521 ftrace_avail_open(struct inode *inode, struct file *file)
3523 struct ftrace_iterator *iter;
3525 if (unlikely(ftrace_disabled))
3528 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3532 iter->pg = ftrace_pages_start;
3533 iter->ops = &global_ops;
3539 ftrace_enabled_open(struct inode *inode, struct file *file)
3541 struct ftrace_iterator *iter;
3543 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3547 iter->pg = ftrace_pages_start;
3548 iter->flags = FTRACE_ITER_ENABLED;
3549 iter->ops = &global_ops;
3555 * ftrace_regex_open - initialize function tracer filter files
3556 * @ops: The ftrace_ops that hold the hash filters
3557 * @flag: The type of filter to process
3558 * @inode: The inode, usually passed in to your open routine
3559 * @file: The file, usually passed in to your open routine
3561 * ftrace_regex_open() initializes the filter files for the
3562 * @ops. Depending on @flag it may process the filter hash or
3563 * the notrace hash of @ops. With this called from the open
3564 * routine, you can use ftrace_filter_write() for the write
3565 * routine if @flag has FTRACE_ITER_FILTER set, or
3566 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3567 * tracing_lseek() should be used as the lseek routine, and
3568 * release must call ftrace_regex_release().
3571 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3572 struct inode *inode, struct file *file)
3574 struct ftrace_iterator *iter;
3575 struct ftrace_hash *hash;
3576 struct list_head *mod_head;
3577 struct trace_array *tr = ops->private;
3580 ftrace_ops_init(ops);
3582 if (unlikely(ftrace_disabled))
3585 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3589 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3598 mutex_lock(&ops->func_hash->regex_lock);
3600 if (flag & FTRACE_ITER_NOTRACE) {
3601 hash = ops->func_hash->notrace_hash;
3602 mod_head = tr ? &tr->mod_notrace : NULL;
3604 hash = ops->func_hash->filter_hash;
3605 mod_head = tr ? &tr->mod_trace : NULL;
3608 iter->mod_list = mod_head;
3610 if (file->f_mode & FMODE_WRITE) {
3611 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3613 if (file->f_flags & O_TRUNC) {
3614 iter->hash = alloc_ftrace_hash(size_bits);
3615 clear_ftrace_mod_list(mod_head);
3617 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3621 trace_parser_put(&iter->parser);
3629 if (file->f_mode & FMODE_READ) {
3630 iter->pg = ftrace_pages_start;
3632 ret = seq_open(file, &show_ftrace_seq_ops);
3634 struct seq_file *m = file->private_data;
3638 free_ftrace_hash(iter->hash);
3639 trace_parser_put(&iter->parser);
3643 file->private_data = iter;
3646 mutex_unlock(&ops->func_hash->regex_lock);
3652 ftrace_filter_open(struct inode *inode, struct file *file)
3654 struct ftrace_ops *ops = inode->i_private;
3656 return ftrace_regex_open(ops,
3657 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3662 ftrace_notrace_open(struct inode *inode, struct file *file)
3664 struct ftrace_ops *ops = inode->i_private;
3666 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3670 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3671 struct ftrace_glob {
3678 * If symbols in an architecture don't correspond exactly to the user-visible
3679 * name of what they represent, it is possible to define this function to
3680 * perform the necessary adjustments.
3682 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3687 static int ftrace_match(char *str, struct ftrace_glob *g)
3692 str = arch_ftrace_match_adjust(str, g->search);
3696 if (strcmp(str, g->search) == 0)
3699 case MATCH_FRONT_ONLY:
3700 if (strncmp(str, g->search, g->len) == 0)
3703 case MATCH_MIDDLE_ONLY:
3704 if (strstr(str, g->search))
3707 case MATCH_END_ONLY:
3709 if (slen >= g->len &&
3710 memcmp(str + slen - g->len, g->search, g->len) == 0)
3714 if (glob_match(g->search, str))
3723 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3725 struct ftrace_func_entry *entry;
3728 entry = ftrace_lookup_ip(hash, rec->ip);
3730 /* Do nothing if it doesn't exist */
3734 free_hash_entry(hash, entry);
3736 /* Do nothing if it exists */
3740 ret = add_hash_entry(hash, rec->ip);
3746 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3747 struct ftrace_glob *mod_g, int exclude_mod)
3749 char str[KSYM_SYMBOL_LEN];
3752 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3755 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3757 /* blank module name to match all modules */
3759 /* blank module globbing: modname xor exclude_mod */
3760 if (!exclude_mod != !modname)
3766 * exclude_mod is set to trace everything but the given
3767 * module. If it is set and the module matches, then
3768 * return 0. If it is not set, and the module doesn't match
3769 * also return 0. Otherwise, check the function to see if
3772 if (!mod_matches == !exclude_mod)
3775 /* blank search means to match all funcs in the mod */
3780 return ftrace_match(str, func_g);
3784 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3786 struct ftrace_page *pg;
3787 struct dyn_ftrace *rec;
3788 struct ftrace_glob func_g = { .type = MATCH_FULL };
3789 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3790 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3791 int exclude_mod = 0;
3794 int clear_filter = 0;
3797 func_g.type = filter_parse_regex(func, len, &func_g.search,
3799 func_g.len = strlen(func_g.search);
3803 mod_g.type = filter_parse_regex(mod, strlen(mod),
3804 &mod_g.search, &exclude_mod);
3805 mod_g.len = strlen(mod_g.search);
3808 mutex_lock(&ftrace_lock);
3810 if (unlikely(ftrace_disabled))
3813 do_for_each_ftrace_rec(pg, rec) {
3815 if (rec->flags & FTRACE_FL_DISABLED)
3818 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3819 ret = enter_record(hash, rec, clear_filter);
3826 } while_for_each_ftrace_rec();
3828 mutex_unlock(&ftrace_lock);
3834 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3836 return match_records(hash, buff, len, NULL);
3839 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3840 struct ftrace_ops_hash *old_hash)
3842 struct ftrace_ops *op;
3844 if (!ftrace_enabled)
3847 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3848 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3853 * If this is the shared global_ops filter, then we need to
3854 * check if there is another ops that shares it, is enabled.
3855 * If so, we still need to run the modify code.
3857 if (ops->func_hash != &global_ops.local_hash)
3860 do_for_each_ftrace_op(op, ftrace_ops_list) {
3861 if (op->func_hash == &global_ops.local_hash &&
3862 op->flags & FTRACE_OPS_FL_ENABLED) {
3863 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3864 /* Only need to do this once */
3867 } while_for_each_ftrace_op(op);
3870 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3871 struct ftrace_hash **orig_hash,
3872 struct ftrace_hash *hash,
3875 struct ftrace_ops_hash old_hash_ops;
3876 struct ftrace_hash *old_hash;
3879 old_hash = *orig_hash;
3880 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3881 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3882 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3884 ftrace_ops_update_code(ops, &old_hash_ops);
3885 free_ftrace_hash_rcu(old_hash);
3890 static bool module_exists(const char *module)
3892 /* All modules have the symbol __this_module */
3893 const char this_mod[] = "__this_module";
3894 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
3898 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
3900 if (n > sizeof(modname) - 1)
3903 val = module_kallsyms_lookup_name(modname);
3907 static int cache_mod(struct trace_array *tr,
3908 const char *func, char *module, int enable)
3910 struct ftrace_mod_load *ftrace_mod, *n;
3911 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3914 mutex_lock(&ftrace_lock);
3916 /* We do not cache inverse filters */
3917 if (func[0] == '!') {
3921 /* Look to remove this hash */
3922 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3923 if (strcmp(ftrace_mod->module, module) != 0)
3926 /* no func matches all */
3927 if (strcmp(func, "*") == 0 ||
3928 (ftrace_mod->func &&
3929 strcmp(ftrace_mod->func, func) == 0)) {
3931 free_ftrace_mod(ftrace_mod);
3939 /* We only care about modules that have not been loaded yet */
3940 if (module_exists(module))
3943 /* Save this string off, and execute it when the module is loaded */
3944 ret = ftrace_add_mod(tr, func, module, enable);
3946 mutex_unlock(&ftrace_lock);
3952 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3953 int reset, int enable);
3955 #ifdef CONFIG_MODULES
3956 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3957 char *mod, bool enable)
3959 struct ftrace_mod_load *ftrace_mod, *n;
3960 struct ftrace_hash **orig_hash, *new_hash;
3961 LIST_HEAD(process_mods);
3965 mutex_lock(&ops->func_hash->regex_lock);
3968 orig_hash = &ops->func_hash->filter_hash;
3970 orig_hash = &ops->func_hash->notrace_hash;
3972 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
3975 goto out; /* warn? */
3977 mutex_lock(&ftrace_lock);
3979 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3981 if (strcmp(ftrace_mod->module, mod) != 0)
3984 if (ftrace_mod->func)
3985 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
3987 func = kstrdup("*", GFP_KERNEL);
3989 if (!func) /* warn? */
3992 list_del(&ftrace_mod->list);
3993 list_add(&ftrace_mod->list, &process_mods);
3995 /* Use the newly allocated func, as it may be "*" */
3996 kfree(ftrace_mod->func);
3997 ftrace_mod->func = func;
4000 mutex_unlock(&ftrace_lock);
4002 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4004 func = ftrace_mod->func;
4006 /* Grabs ftrace_lock, which is why we have this extra step */
4007 match_records(new_hash, func, strlen(func), mod);
4008 free_ftrace_mod(ftrace_mod);
4011 if (enable && list_empty(head))
4012 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4014 mutex_lock(&ftrace_lock);
4016 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4018 mutex_unlock(&ftrace_lock);
4021 mutex_unlock(&ops->func_hash->regex_lock);
4023 free_ftrace_hash(new_hash);
4026 static void process_cached_mods(const char *mod_name)
4028 struct trace_array *tr;
4031 mod = kstrdup(mod_name, GFP_KERNEL);
4035 mutex_lock(&trace_types_lock);
4036 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4037 if (!list_empty(&tr->mod_trace))
4038 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4039 if (!list_empty(&tr->mod_notrace))
4040 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4042 mutex_unlock(&trace_types_lock);
4049 * We register the module command as a template to show others how
4050 * to register the a command as well.
4054 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4055 char *func_orig, char *cmd, char *module, int enable)
4060 /* match_records() modifies func, and we need the original */
4061 func = kstrdup(func_orig, GFP_KERNEL);
4066 * cmd == 'mod' because we only registered this func
4067 * for the 'mod' ftrace_func_command.
4068 * But if you register one func with multiple commands,
4069 * you can tell which command was used by the cmd
4072 ret = match_records(hash, func, strlen(func), module);
4076 return cache_mod(tr, func_orig, module, enable);
4082 static struct ftrace_func_command ftrace_mod_cmd = {
4084 .func = ftrace_mod_callback,
4087 static int __init ftrace_mod_cmd_init(void)
4089 return register_ftrace_command(&ftrace_mod_cmd);
4091 core_initcall(ftrace_mod_cmd_init);
4093 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4094 struct ftrace_ops *op, struct pt_regs *pt_regs)
4096 struct ftrace_probe_ops *probe_ops;
4097 struct ftrace_func_probe *probe;
4099 probe = container_of(op, struct ftrace_func_probe, ops);
4100 probe_ops = probe->probe_ops;
4103 * Disable preemption for these calls to prevent a RCU grace
4104 * period. This syncs the hash iteration and freeing of items
4105 * on the hash. rcu_read_lock is too dangerous here.
4107 preempt_disable_notrace();
4108 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4109 preempt_enable_notrace();
4112 struct ftrace_func_map {
4113 struct ftrace_func_entry entry;
4117 struct ftrace_func_mapper {
4118 struct ftrace_hash hash;
4122 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4124 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4126 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4128 struct ftrace_hash *hash;
4131 * The mapper is simply a ftrace_hash, but since the entries
4132 * in the hash are not ftrace_func_entry type, we define it
4133 * as a separate structure.
4135 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4136 return (struct ftrace_func_mapper *)hash;
4140 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4141 * @mapper: The mapper that has the ip maps
4142 * @ip: the instruction pointer to find the data for
4144 * Returns the data mapped to @ip if found otherwise NULL. The return
4145 * is actually the address of the mapper data pointer. The address is
4146 * returned for use cases where the data is no bigger than a long, and
4147 * the user can use the data pointer as its data instead of having to
4148 * allocate more memory for the reference.
4150 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4153 struct ftrace_func_entry *entry;
4154 struct ftrace_func_map *map;
4156 entry = ftrace_lookup_ip(&mapper->hash, ip);
4160 map = (struct ftrace_func_map *)entry;
4165 * ftrace_func_mapper_add_ip - Map some data to an ip
4166 * @mapper: The mapper that has the ip maps
4167 * @ip: The instruction pointer address to map @data to
4168 * @data: The data to map to @ip
4170 * Returns 0 on succes otherwise an error.
4172 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4173 unsigned long ip, void *data)
4175 struct ftrace_func_entry *entry;
4176 struct ftrace_func_map *map;
4178 entry = ftrace_lookup_ip(&mapper->hash, ip);
4182 map = kmalloc(sizeof(*map), GFP_KERNEL);
4189 __add_hash_entry(&mapper->hash, &map->entry);
4195 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4196 * @mapper: The mapper that has the ip maps
4197 * @ip: The instruction pointer address to remove the data from
4199 * Returns the data if it is found, otherwise NULL.
4200 * Note, if the data pointer is used as the data itself, (see
4201 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4202 * if the data pointer was set to zero.
4204 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4207 struct ftrace_func_entry *entry;
4208 struct ftrace_func_map *map;
4211 entry = ftrace_lookup_ip(&mapper->hash, ip);
4215 map = (struct ftrace_func_map *)entry;
4218 remove_hash_entry(&mapper->hash, entry);
4225 * free_ftrace_func_mapper - free a mapping of ips and data
4226 * @mapper: The mapper that has the ip maps
4227 * @free_func: A function to be called on each data item.
4229 * This is used to free the function mapper. The @free_func is optional
4230 * and can be used if the data needs to be freed as well.
4232 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4233 ftrace_mapper_func free_func)
4235 struct ftrace_func_entry *entry;
4236 struct ftrace_func_map *map;
4237 struct hlist_head *hhd;
4238 int size = 1 << mapper->hash.size_bits;
4241 if (free_func && mapper->hash.count) {
4242 for (i = 0; i < size; i++) {
4243 hhd = &mapper->hash.buckets[i];
4244 hlist_for_each_entry(entry, hhd, hlist) {
4245 map = (struct ftrace_func_map *)entry;
4250 free_ftrace_hash(&mapper->hash);
4253 static void release_probe(struct ftrace_func_probe *probe)
4255 struct ftrace_probe_ops *probe_ops;
4257 mutex_lock(&ftrace_lock);
4259 WARN_ON(probe->ref <= 0);
4261 /* Subtract the ref that was used to protect this instance */
4265 probe_ops = probe->probe_ops;
4267 * Sending zero as ip tells probe_ops to free
4268 * the probe->data itself
4270 if (probe_ops->free)
4271 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4272 list_del(&probe->list);
4275 mutex_unlock(&ftrace_lock);
4278 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4281 * Add one ref to keep it from being freed when releasing the
4282 * ftrace_lock mutex.
4288 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4289 struct ftrace_probe_ops *probe_ops,
4292 struct ftrace_func_entry *entry;
4293 struct ftrace_func_probe *probe;
4294 struct ftrace_hash **orig_hash;
4295 struct ftrace_hash *old_hash;
4296 struct ftrace_hash *hash;
4305 /* We do not support '!' for function probes */
4306 if (WARN_ON(glob[0] == '!'))
4310 mutex_lock(&ftrace_lock);
4311 /* Check if the probe_ops is already registered */
4312 list_for_each_entry(probe, &tr->func_probes, list) {
4313 if (probe->probe_ops == probe_ops)
4316 if (&probe->list == &tr->func_probes) {
4317 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4319 mutex_unlock(&ftrace_lock);
4322 probe->probe_ops = probe_ops;
4323 probe->ops.func = function_trace_probe_call;
4325 ftrace_ops_init(&probe->ops);
4326 list_add(&probe->list, &tr->func_probes);
4329 acquire_probe_locked(probe);
4331 mutex_unlock(&ftrace_lock);
4333 mutex_lock(&probe->ops.func_hash->regex_lock);
4335 orig_hash = &probe->ops.func_hash->filter_hash;
4336 old_hash = *orig_hash;
4337 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4339 ret = ftrace_match_records(hash, glob, strlen(glob));
4341 /* Nothing found? */
4348 size = 1 << hash->size_bits;
4349 for (i = 0; i < size; i++) {
4350 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4351 if (ftrace_lookup_ip(old_hash, entry->ip))
4354 * The caller might want to do something special
4355 * for each function we find. We call the callback
4356 * to give the caller an opportunity to do so.
4358 if (probe_ops->init) {
4359 ret = probe_ops->init(probe_ops, tr,
4363 if (probe_ops->free && count)
4364 probe_ops->free(probe_ops, tr,
4374 mutex_lock(&ftrace_lock);
4377 /* Nothing was added? */
4382 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4387 /* One ref for each new function traced */
4388 probe->ref += count;
4390 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4391 ret = ftrace_startup(&probe->ops, 0);
4394 mutex_unlock(&ftrace_lock);
4399 mutex_unlock(&probe->ops.func_hash->regex_lock);
4400 free_ftrace_hash(hash);
4402 release_probe(probe);
4407 if (!probe_ops->free || !count)
4410 /* Failed to do the move, need to call the free functions */
4411 for (i = 0; i < size; i++) {
4412 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4413 if (ftrace_lookup_ip(old_hash, entry->ip))
4415 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4422 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4423 struct ftrace_probe_ops *probe_ops)
4425 struct ftrace_ops_hash old_hash_ops;
4426 struct ftrace_func_entry *entry;
4427 struct ftrace_func_probe *probe;
4428 struct ftrace_glob func_g;
4429 struct ftrace_hash **orig_hash;
4430 struct ftrace_hash *old_hash;
4431 struct ftrace_hash *hash = NULL;
4432 struct hlist_node *tmp;
4433 struct hlist_head hhd;
4434 char str[KSYM_SYMBOL_LEN];
4436 int i, ret = -ENODEV;
4439 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4440 func_g.search = NULL;
4444 func_g.type = filter_parse_regex(glob, strlen(glob),
4445 &func_g.search, ¬);
4446 func_g.len = strlen(func_g.search);
4448 /* we do not support '!' for function probes */
4453 mutex_lock(&ftrace_lock);
4454 /* Check if the probe_ops is already registered */
4455 list_for_each_entry(probe, &tr->func_probes, list) {
4456 if (probe->probe_ops == probe_ops)
4459 if (&probe->list == &tr->func_probes)
4460 goto err_unlock_ftrace;
4463 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4464 goto err_unlock_ftrace;
4466 acquire_probe_locked(probe);
4468 mutex_unlock(&ftrace_lock);
4470 mutex_lock(&probe->ops.func_hash->regex_lock);
4472 orig_hash = &probe->ops.func_hash->filter_hash;
4473 old_hash = *orig_hash;
4475 if (ftrace_hash_empty(old_hash))
4478 old_hash_ops.filter_hash = old_hash;
4479 /* Probes only have filters */
4480 old_hash_ops.notrace_hash = NULL;
4483 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4487 INIT_HLIST_HEAD(&hhd);
4489 size = 1 << hash->size_bits;
4490 for (i = 0; i < size; i++) {
4491 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4493 if (func_g.search) {
4494 kallsyms_lookup(entry->ip, NULL, NULL,
4496 if (!ftrace_match(str, &func_g))
4500 remove_hash_entry(hash, entry);
4501 hlist_add_head(&entry->hlist, &hhd);
4505 /* Nothing found? */
4511 mutex_lock(&ftrace_lock);
4513 WARN_ON(probe->ref < count);
4515 probe->ref -= count;
4517 if (ftrace_hash_empty(hash))
4518 ftrace_shutdown(&probe->ops, 0);
4520 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4523 /* still need to update the function call sites */
4524 if (ftrace_enabled && !ftrace_hash_empty(hash))
4525 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4527 synchronize_sched();
4529 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4530 hlist_del(&entry->hlist);
4531 if (probe_ops->free)
4532 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4535 mutex_unlock(&ftrace_lock);
4538 mutex_unlock(&probe->ops.func_hash->regex_lock);
4539 free_ftrace_hash(hash);
4541 release_probe(probe);
4546 mutex_unlock(&ftrace_lock);
4550 void clear_ftrace_function_probes(struct trace_array *tr)
4552 struct ftrace_func_probe *probe, *n;
4554 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4555 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4558 static LIST_HEAD(ftrace_commands);
4559 static DEFINE_MUTEX(ftrace_cmd_mutex);
4562 * Currently we only register ftrace commands from __init, so mark this
4565 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4567 struct ftrace_func_command *p;
4570 mutex_lock(&ftrace_cmd_mutex);
4571 list_for_each_entry(p, &ftrace_commands, list) {
4572 if (strcmp(cmd->name, p->name) == 0) {
4577 list_add(&cmd->list, &ftrace_commands);
4579 mutex_unlock(&ftrace_cmd_mutex);
4585 * Currently we only unregister ftrace commands from __init, so mark
4588 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4590 struct ftrace_func_command *p, *n;
4593 mutex_lock(&ftrace_cmd_mutex);
4594 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4595 if (strcmp(cmd->name, p->name) == 0) {
4597 list_del_init(&p->list);
4602 mutex_unlock(&ftrace_cmd_mutex);
4607 static int ftrace_process_regex(struct ftrace_iterator *iter,
4608 char *buff, int len, int enable)
4610 struct ftrace_hash *hash = iter->hash;
4611 struct trace_array *tr = iter->ops->private;
4612 char *func, *command, *next = buff;
4613 struct ftrace_func_command *p;
4616 func = strsep(&next, ":");
4619 ret = ftrace_match_records(hash, func, len);
4629 command = strsep(&next, ":");
4631 mutex_lock(&ftrace_cmd_mutex);
4632 list_for_each_entry(p, &ftrace_commands, list) {
4633 if (strcmp(p->name, command) == 0) {
4634 ret = p->func(tr, hash, func, command, next, enable);
4639 mutex_unlock(&ftrace_cmd_mutex);
4645 ftrace_regex_write(struct file *file, const char __user *ubuf,
4646 size_t cnt, loff_t *ppos, int enable)
4648 struct ftrace_iterator *iter;
4649 struct trace_parser *parser;
4655 if (file->f_mode & FMODE_READ) {
4656 struct seq_file *m = file->private_data;
4659 iter = file->private_data;
4661 if (unlikely(ftrace_disabled))
4664 /* iter->hash is a local copy, so we don't need regex_lock */
4666 parser = &iter->parser;
4667 read = trace_get_user(parser, ubuf, cnt, ppos);
4669 if (read >= 0 && trace_parser_loaded(parser) &&
4670 !trace_parser_cont(parser)) {
4671 ret = ftrace_process_regex(iter, parser->buffer,
4672 parser->idx, enable);
4673 trace_parser_clear(parser);
4684 ftrace_filter_write(struct file *file, const char __user *ubuf,
4685 size_t cnt, loff_t *ppos)
4687 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4691 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4692 size_t cnt, loff_t *ppos)
4694 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4698 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4700 struct ftrace_func_entry *entry;
4702 if (!ftrace_location(ip))
4706 entry = ftrace_lookup_ip(hash, ip);
4709 free_hash_entry(hash, entry);
4713 return add_hash_entry(hash, ip);
4717 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4718 unsigned long ip, int remove, int reset, int enable)
4720 struct ftrace_hash **orig_hash;
4721 struct ftrace_hash *hash;
4724 if (unlikely(ftrace_disabled))
4727 mutex_lock(&ops->func_hash->regex_lock);
4730 orig_hash = &ops->func_hash->filter_hash;
4732 orig_hash = &ops->func_hash->notrace_hash;
4735 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4737 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4741 goto out_regex_unlock;
4744 if (buf && !ftrace_match_records(hash, buf, len)) {
4746 goto out_regex_unlock;
4749 ret = ftrace_match_addr(hash, ip, remove);
4751 goto out_regex_unlock;
4754 mutex_lock(&ftrace_lock);
4755 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4756 mutex_unlock(&ftrace_lock);
4759 mutex_unlock(&ops->func_hash->regex_lock);
4761 free_ftrace_hash(hash);
4766 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4767 int reset, int enable)
4769 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4773 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4774 * @ops - the ops to set the filter with
4775 * @ip - the address to add to or remove from the filter.
4776 * @remove - non zero to remove the ip from the filter
4777 * @reset - non zero to reset all filters before applying this filter.
4779 * Filters denote which functions should be enabled when tracing is enabled
4780 * If @ip is NULL, it failes to update filter.
4782 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4783 int remove, int reset)
4785 ftrace_ops_init(ops);
4786 return ftrace_set_addr(ops, ip, remove, reset, 1);
4788 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4791 * ftrace_ops_set_global_filter - setup ops to use global filters
4792 * @ops - the ops which will use the global filters
4794 * ftrace users who need global function trace filtering should call this.
4795 * It can set the global filter only if ops were not initialized before.
4797 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4799 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4802 ftrace_ops_init(ops);
4803 ops->func_hash = &global_ops.local_hash;
4805 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4808 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4809 int reset, int enable)
4811 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4815 * ftrace_set_filter - set a function to filter on in ftrace
4816 * @ops - the ops to set the filter with
4817 * @buf - the string that holds the function filter text.
4818 * @len - the length of the string.
4819 * @reset - non zero to reset all filters before applying this filter.
4821 * Filters denote which functions should be enabled when tracing is enabled.
4822 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4824 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4827 ftrace_ops_init(ops);
4828 return ftrace_set_regex(ops, buf, len, reset, 1);
4830 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4833 * ftrace_set_notrace - set a function to not trace in ftrace
4834 * @ops - the ops to set the notrace filter with
4835 * @buf - the string that holds the function notrace text.
4836 * @len - the length of the string.
4837 * @reset - non zero to reset all filters before applying this filter.
4839 * Notrace Filters denote which functions should not be enabled when tracing
4840 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4843 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4846 ftrace_ops_init(ops);
4847 return ftrace_set_regex(ops, buf, len, reset, 0);
4849 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4851 * ftrace_set_global_filter - set a function to filter on with global tracers
4852 * @buf - the string that holds the function filter text.
4853 * @len - the length of the string.
4854 * @reset - non zero to reset all filters before applying this filter.
4856 * Filters denote which functions should be enabled when tracing is enabled.
4857 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4859 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4861 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4863 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4866 * ftrace_set_global_notrace - set a function to not trace with global tracers
4867 * @buf - the string that holds the function notrace text.
4868 * @len - the length of the string.
4869 * @reset - non zero to reset all filters before applying this filter.
4871 * Notrace Filters denote which functions should not be enabled when tracing
4872 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4875 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4877 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4879 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4882 * command line interface to allow users to set filters on boot up.
4884 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4885 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4886 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4888 /* Used by function selftest to not test if filter is set */
4889 bool ftrace_filter_param __initdata;
4891 static int __init set_ftrace_notrace(char *str)
4893 ftrace_filter_param = true;
4894 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4897 __setup("ftrace_notrace=", set_ftrace_notrace);
4899 static int __init set_ftrace_filter(char *str)
4901 ftrace_filter_param = true;
4902 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4905 __setup("ftrace_filter=", set_ftrace_filter);
4907 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4908 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4909 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4910 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
4912 static int __init set_graph_function(char *str)
4914 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4917 __setup("ftrace_graph_filter=", set_graph_function);
4919 static int __init set_graph_notrace_function(char *str)
4921 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4924 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4926 static int __init set_graph_max_depth_function(char *str)
4930 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4933 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4935 static void __init set_ftrace_early_graph(char *buf, int enable)
4939 struct ftrace_hash *hash;
4941 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4946 func = strsep(&buf, ",");
4947 /* we allow only one expression at a time */
4948 ret = ftrace_graph_set_hash(hash, func);
4950 printk(KERN_DEBUG "ftrace: function %s not "
4951 "traceable\n", func);
4955 ftrace_graph_hash = hash;
4957 ftrace_graph_notrace_hash = hash;
4959 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4962 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4966 ftrace_ops_init(ops);
4969 func = strsep(&buf, ",");
4970 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4974 static void __init set_ftrace_early_filters(void)
4976 if (ftrace_filter_buf[0])
4977 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4978 if (ftrace_notrace_buf[0])
4979 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4980 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4981 if (ftrace_graph_buf[0])
4982 set_ftrace_early_graph(ftrace_graph_buf, 1);
4983 if (ftrace_graph_notrace_buf[0])
4984 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4985 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4988 int ftrace_regex_release(struct inode *inode, struct file *file)
4990 struct seq_file *m = (struct seq_file *)file->private_data;
4991 struct ftrace_iterator *iter;
4992 struct ftrace_hash **orig_hash;
4993 struct trace_parser *parser;
4997 if (file->f_mode & FMODE_READ) {
4999 seq_release(inode, file);
5001 iter = file->private_data;
5003 parser = &iter->parser;
5004 if (trace_parser_loaded(parser)) {
5005 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
5008 trace_parser_put(parser);
5010 mutex_lock(&iter->ops->func_hash->regex_lock);
5012 if (file->f_mode & FMODE_WRITE) {
5013 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5016 orig_hash = &iter->ops->func_hash->filter_hash;
5017 if (iter->tr && !list_empty(&iter->tr->mod_trace))
5018 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5020 orig_hash = &iter->ops->func_hash->notrace_hash;
5022 mutex_lock(&ftrace_lock);
5023 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5024 iter->hash, filter_hash);
5025 mutex_unlock(&ftrace_lock);
5027 /* For read only, the hash is the ops hash */
5031 mutex_unlock(&iter->ops->func_hash->regex_lock);
5032 free_ftrace_hash(iter->hash);
5038 static const struct file_operations ftrace_avail_fops = {
5039 .open = ftrace_avail_open,
5041 .llseek = seq_lseek,
5042 .release = seq_release_private,
5045 static const struct file_operations ftrace_enabled_fops = {
5046 .open = ftrace_enabled_open,
5048 .llseek = seq_lseek,
5049 .release = seq_release_private,
5052 static const struct file_operations ftrace_filter_fops = {
5053 .open = ftrace_filter_open,
5055 .write = ftrace_filter_write,
5056 .llseek = tracing_lseek,
5057 .release = ftrace_regex_release,
5060 static const struct file_operations ftrace_notrace_fops = {
5061 .open = ftrace_notrace_open,
5063 .write = ftrace_notrace_write,
5064 .llseek = tracing_lseek,
5065 .release = ftrace_regex_release,
5068 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5070 static DEFINE_MUTEX(graph_lock);
5072 struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5073 struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5075 enum graph_filter_type {
5076 GRAPH_FILTER_NOTRACE = 0,
5077 GRAPH_FILTER_FUNCTION,
5080 #define FTRACE_GRAPH_EMPTY ((void *)1)
5082 struct ftrace_graph_data {
5083 struct ftrace_hash *hash;
5084 struct ftrace_func_entry *entry;
5085 int idx; /* for hash table iteration */
5086 enum graph_filter_type type;
5087 struct ftrace_hash *new_hash;
5088 const struct seq_operations *seq_ops;
5089 struct trace_parser parser;
5093 __g_next(struct seq_file *m, loff_t *pos)
5095 struct ftrace_graph_data *fgd = m->private;
5096 struct ftrace_func_entry *entry = fgd->entry;
5097 struct hlist_head *head;
5098 int i, idx = fgd->idx;
5100 if (*pos >= fgd->hash->count)
5104 hlist_for_each_entry_continue(entry, hlist) {
5112 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5113 head = &fgd->hash->buckets[i];
5114 hlist_for_each_entry(entry, head, hlist) {
5124 g_next(struct seq_file *m, void *v, loff_t *pos)
5127 return __g_next(m, pos);
5130 static void *g_start(struct seq_file *m, loff_t *pos)
5132 struct ftrace_graph_data *fgd = m->private;
5134 mutex_lock(&graph_lock);
5136 if (fgd->type == GRAPH_FILTER_FUNCTION)
5137 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5138 lockdep_is_held(&graph_lock));
5140 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5141 lockdep_is_held(&graph_lock));
5143 /* Nothing, tell g_show to print all functions are enabled */
5144 if (ftrace_hash_empty(fgd->hash) && !*pos)
5145 return FTRACE_GRAPH_EMPTY;
5149 return __g_next(m, pos);
5152 static void g_stop(struct seq_file *m, void *p)
5154 mutex_unlock(&graph_lock);
5157 static int g_show(struct seq_file *m, void *v)
5159 struct ftrace_func_entry *entry = v;
5164 if (entry == FTRACE_GRAPH_EMPTY) {
5165 struct ftrace_graph_data *fgd = m->private;
5167 if (fgd->type == GRAPH_FILTER_FUNCTION)
5168 seq_puts(m, "#### all functions enabled ####\n");
5170 seq_puts(m, "#### no functions disabled ####\n");
5174 seq_printf(m, "%ps\n", (void *)entry->ip);
5179 static const struct seq_operations ftrace_graph_seq_ops = {
5187 __ftrace_graph_open(struct inode *inode, struct file *file,
5188 struct ftrace_graph_data *fgd)
5191 struct ftrace_hash *new_hash = NULL;
5193 if (file->f_mode & FMODE_WRITE) {
5194 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5196 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5199 if (file->f_flags & O_TRUNC)
5200 new_hash = alloc_ftrace_hash(size_bits);
5202 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5210 if (file->f_mode & FMODE_READ) {
5211 ret = seq_open(file, &ftrace_graph_seq_ops);
5213 struct seq_file *m = file->private_data;
5217 free_ftrace_hash(new_hash);
5221 file->private_data = fgd;
5224 if (ret < 0 && file->f_mode & FMODE_WRITE)
5225 trace_parser_put(&fgd->parser);
5227 fgd->new_hash = new_hash;
5230 * All uses of fgd->hash must be taken with the graph_lock
5231 * held. The graph_lock is going to be released, so force
5232 * fgd->hash to be reinitialized when it is taken again.
5240 ftrace_graph_open(struct inode *inode, struct file *file)
5242 struct ftrace_graph_data *fgd;
5245 if (unlikely(ftrace_disabled))
5248 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5252 mutex_lock(&graph_lock);
5254 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5255 lockdep_is_held(&graph_lock));
5256 fgd->type = GRAPH_FILTER_FUNCTION;
5257 fgd->seq_ops = &ftrace_graph_seq_ops;
5259 ret = __ftrace_graph_open(inode, file, fgd);
5263 mutex_unlock(&graph_lock);
5268 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5270 struct ftrace_graph_data *fgd;
5273 if (unlikely(ftrace_disabled))
5276 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5280 mutex_lock(&graph_lock);
5282 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5283 lockdep_is_held(&graph_lock));
5284 fgd->type = GRAPH_FILTER_NOTRACE;
5285 fgd->seq_ops = &ftrace_graph_seq_ops;
5287 ret = __ftrace_graph_open(inode, file, fgd);
5291 mutex_unlock(&graph_lock);
5296 ftrace_graph_release(struct inode *inode, struct file *file)
5298 struct ftrace_graph_data *fgd;
5299 struct ftrace_hash *old_hash, *new_hash;
5300 struct trace_parser *parser;
5303 if (file->f_mode & FMODE_READ) {
5304 struct seq_file *m = file->private_data;
5307 seq_release(inode, file);
5309 fgd = file->private_data;
5313 if (file->f_mode & FMODE_WRITE) {
5315 parser = &fgd->parser;
5317 if (trace_parser_loaded((parser))) {
5318 ret = ftrace_graph_set_hash(fgd->new_hash,
5322 trace_parser_put(parser);
5324 new_hash = __ftrace_hash_move(fgd->new_hash);
5330 mutex_lock(&graph_lock);
5332 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5333 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5334 lockdep_is_held(&graph_lock));
5335 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5337 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5338 lockdep_is_held(&graph_lock));
5339 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5342 mutex_unlock(&graph_lock);
5344 /* Wait till all users are no longer using the old hash */
5345 synchronize_sched();
5347 free_ftrace_hash(old_hash);
5351 free_ftrace_hash(fgd->new_hash);
5358 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5360 struct ftrace_glob func_g;
5361 struct dyn_ftrace *rec;
5362 struct ftrace_page *pg;
5363 struct ftrace_func_entry *entry;
5368 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5369 &func_g.search, ¬);
5371 func_g.len = strlen(func_g.search);
5373 mutex_lock(&ftrace_lock);
5375 if (unlikely(ftrace_disabled)) {
5376 mutex_unlock(&ftrace_lock);
5380 do_for_each_ftrace_rec(pg, rec) {
5382 if (rec->flags & FTRACE_FL_DISABLED)
5385 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5386 entry = ftrace_lookup_ip(hash, rec->ip);
5393 if (add_hash_entry(hash, rec->ip) < 0)
5397 free_hash_entry(hash, entry);
5402 } while_for_each_ftrace_rec();
5404 mutex_unlock(&ftrace_lock);
5413 ftrace_graph_write(struct file *file, const char __user *ubuf,
5414 size_t cnt, loff_t *ppos)
5416 ssize_t read, ret = 0;
5417 struct ftrace_graph_data *fgd = file->private_data;
5418 struct trace_parser *parser;
5423 /* Read mode uses seq functions */
5424 if (file->f_mode & FMODE_READ) {
5425 struct seq_file *m = file->private_data;
5429 parser = &fgd->parser;
5431 read = trace_get_user(parser, ubuf, cnt, ppos);
5433 if (read >= 0 && trace_parser_loaded(parser) &&
5434 !trace_parser_cont(parser)) {
5436 ret = ftrace_graph_set_hash(fgd->new_hash,
5438 trace_parser_clear(parser);
5447 static const struct file_operations ftrace_graph_fops = {
5448 .open = ftrace_graph_open,
5450 .write = ftrace_graph_write,
5451 .llseek = tracing_lseek,
5452 .release = ftrace_graph_release,
5455 static const struct file_operations ftrace_graph_notrace_fops = {
5456 .open = ftrace_graph_notrace_open,
5458 .write = ftrace_graph_write,
5459 .llseek = tracing_lseek,
5460 .release = ftrace_graph_release,
5462 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5464 void ftrace_create_filter_files(struct ftrace_ops *ops,
5465 struct dentry *parent)
5468 trace_create_file("set_ftrace_filter", 0644, parent,
5469 ops, &ftrace_filter_fops);
5471 trace_create_file("set_ftrace_notrace", 0644, parent,
5472 ops, &ftrace_notrace_fops);
5476 * The name "destroy_filter_files" is really a misnomer. Although
5477 * in the future, it may actualy delete the files, but this is
5478 * really intended to make sure the ops passed in are disabled
5479 * and that when this function returns, the caller is free to
5482 * The "destroy" name is only to match the "create" name that this
5483 * should be paired with.
5485 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5487 mutex_lock(&ftrace_lock);
5488 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5489 ftrace_shutdown(ops, 0);
5490 ops->flags |= FTRACE_OPS_FL_DELETED;
5491 mutex_unlock(&ftrace_lock);
5494 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
5497 trace_create_file("available_filter_functions", 0444,
5498 d_tracer, NULL, &ftrace_avail_fops);
5500 trace_create_file("enabled_functions", 0444,
5501 d_tracer, NULL, &ftrace_enabled_fops);
5503 ftrace_create_filter_files(&global_ops, d_tracer);
5505 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5506 trace_create_file("set_graph_function", 0644, d_tracer,
5508 &ftrace_graph_fops);
5509 trace_create_file("set_graph_notrace", 0644, d_tracer,
5511 &ftrace_graph_notrace_fops);
5512 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5517 static int ftrace_cmp_ips(const void *a, const void *b)
5519 const unsigned long *ipa = a;
5520 const unsigned long *ipb = b;
5529 static int ftrace_process_locs(struct module *mod,
5530 unsigned long *start,
5533 struct ftrace_page *start_pg;
5534 struct ftrace_page *pg;
5535 struct dyn_ftrace *rec;
5536 unsigned long count;
5539 unsigned long flags = 0; /* Shut up gcc */
5542 count = end - start;
5547 sort(start, count, sizeof(*start),
5548 ftrace_cmp_ips, NULL);
5550 start_pg = ftrace_allocate_pages(count);
5554 mutex_lock(&ftrace_lock);
5557 * Core and each module needs their own pages, as
5558 * modules will free them when they are removed.
5559 * Force a new page to be allocated for modules.
5562 WARN_ON(ftrace_pages || ftrace_pages_start);
5563 /* First initialization */
5564 ftrace_pages = ftrace_pages_start = start_pg;
5569 if (WARN_ON(ftrace_pages->next)) {
5570 /* Hmm, we have free pages? */
5571 while (ftrace_pages->next)
5572 ftrace_pages = ftrace_pages->next;
5575 ftrace_pages->next = start_pg;
5581 addr = ftrace_call_adjust(*p++);
5583 * Some architecture linkers will pad between
5584 * the different mcount_loc sections of different
5585 * object files to satisfy alignments.
5586 * Skip any NULL pointers.
5591 if (pg->index == pg->size) {
5592 /* We should have allocated enough */
5593 if (WARN_ON(!pg->next))
5598 rec = &pg->records[pg->index++];
5602 /* We should have used all pages */
5605 /* Assign the last page to ftrace_pages */
5609 * We only need to disable interrupts on start up
5610 * because we are modifying code that an interrupt
5611 * may execute, and the modification is not atomic.
5612 * But for modules, nothing runs the code we modify
5613 * until we are finished with it, and there's no
5614 * reason to cause large interrupt latencies while we do it.
5617 local_irq_save(flags);
5618 ftrace_update_code(mod, start_pg);
5620 local_irq_restore(flags);
5623 mutex_unlock(&ftrace_lock);
5628 struct ftrace_mod_func {
5629 struct list_head list;
5635 struct ftrace_mod_map {
5636 struct rcu_head rcu;
5637 struct list_head list;
5639 unsigned long start_addr;
5640 unsigned long end_addr;
5641 struct list_head funcs;
5642 unsigned int num_funcs;
5645 #ifdef CONFIG_MODULES
5647 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5649 static LIST_HEAD(ftrace_mod_maps);
5651 static int referenced_filters(struct dyn_ftrace *rec)
5653 struct ftrace_ops *ops;
5656 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5657 if (ops_references_rec(ops, rec))
5665 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5667 struct ftrace_func_entry *entry;
5668 struct dyn_ftrace *rec;
5671 if (ftrace_hash_empty(hash))
5674 for (i = 0; i < pg->index; i++) {
5675 rec = &pg->records[i];
5676 entry = __ftrace_lookup_ip(hash, rec->ip);
5678 * Do not allow this rec to match again.
5679 * Yeah, it may waste some memory, but will be removed
5680 * if/when the hash is modified again.
5687 /* Clear any records from hashs */
5688 static void clear_mod_from_hashes(struct ftrace_page *pg)
5690 struct trace_array *tr;
5692 mutex_lock(&trace_types_lock);
5693 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5694 if (!tr->ops || !tr->ops->func_hash)
5696 mutex_lock(&tr->ops->func_hash->regex_lock);
5697 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5698 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5699 mutex_unlock(&tr->ops->func_hash->regex_lock);
5701 mutex_unlock(&trace_types_lock);
5704 static void ftrace_free_mod_map(struct rcu_head *rcu)
5706 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5707 struct ftrace_mod_func *mod_func;
5708 struct ftrace_mod_func *n;
5710 /* All the contents of mod_map are now not visible to readers */
5711 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5712 kfree(mod_func->name);
5713 list_del(&mod_func->list);
5720 void ftrace_release_mod(struct module *mod)
5722 struct ftrace_mod_map *mod_map;
5723 struct ftrace_mod_map *n;
5724 struct dyn_ftrace *rec;
5725 struct ftrace_page **last_pg;
5726 struct ftrace_page *tmp_page = NULL;
5727 struct ftrace_page *pg;
5730 mutex_lock(&ftrace_lock);
5732 if (ftrace_disabled)
5735 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5736 if (mod_map->mod == mod) {
5737 list_del_rcu(&mod_map->list);
5738 call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
5744 * Each module has its own ftrace_pages, remove
5745 * them from the list.
5747 last_pg = &ftrace_pages_start;
5748 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5749 rec = &pg->records[0];
5750 if (within_module_core(rec->ip, mod) ||
5751 within_module_init(rec->ip, mod)) {
5753 * As core pages are first, the first
5754 * page should never be a module page.
5756 if (WARN_ON(pg == ftrace_pages_start))
5759 /* Check if we are deleting the last page */
5760 if (pg == ftrace_pages)
5761 ftrace_pages = next_to_ftrace_page(last_pg);
5763 ftrace_update_tot_cnt -= pg->index;
5764 *last_pg = pg->next;
5766 pg->next = tmp_page;
5769 last_pg = &pg->next;
5772 mutex_unlock(&ftrace_lock);
5774 for (pg = tmp_page; pg; pg = tmp_page) {
5776 /* Needs to be called outside of ftrace_lock */
5777 clear_mod_from_hashes(pg);
5779 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5780 free_pages((unsigned long)pg->records, order);
5781 tmp_page = pg->next;
5786 void ftrace_module_enable(struct module *mod)
5788 struct dyn_ftrace *rec;
5789 struct ftrace_page *pg;
5791 mutex_lock(&ftrace_lock);
5793 if (ftrace_disabled)
5797 * If the tracing is enabled, go ahead and enable the record.
5799 * The reason not to enable the record immediatelly is the
5800 * inherent check of ftrace_make_nop/ftrace_make_call for
5801 * correct previous instructions. Making first the NOP
5802 * conversion puts the module to the correct state, thus
5803 * passing the ftrace_make_call check.
5805 * We also delay this to after the module code already set the
5806 * text to read-only, as we now need to set it back to read-write
5807 * so that we can modify the text.
5809 if (ftrace_start_up)
5810 ftrace_arch_code_modify_prepare();
5812 do_for_each_ftrace_rec(pg, rec) {
5815 * do_for_each_ftrace_rec() is a double loop.
5816 * module text shares the pg. If a record is
5817 * not part of this module, then skip this pg,
5818 * which the "break" will do.
5820 if (!within_module_core(rec->ip, mod) &&
5821 !within_module_init(rec->ip, mod))
5827 * When adding a module, we need to check if tracers are
5828 * currently enabled and if they are, and can trace this record,
5829 * we need to enable the module functions as well as update the
5830 * reference counts for those function records.
5832 if (ftrace_start_up)
5833 cnt += referenced_filters(rec);
5835 /* This clears FTRACE_FL_DISABLED */
5838 if (ftrace_start_up && cnt) {
5839 int failed = __ftrace_replace_code(rec, 1);
5841 ftrace_bug(failed, rec);
5846 } while_for_each_ftrace_rec();
5849 if (ftrace_start_up)
5850 ftrace_arch_code_modify_post_process();
5853 mutex_unlock(&ftrace_lock);
5855 process_cached_mods(mod->name);
5858 void ftrace_module_init(struct module *mod)
5860 if (ftrace_disabled || !mod->num_ftrace_callsites)
5863 ftrace_process_locs(mod, mod->ftrace_callsites,
5864 mod->ftrace_callsites + mod->num_ftrace_callsites);
5867 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5868 struct dyn_ftrace *rec)
5870 struct ftrace_mod_func *mod_func;
5871 unsigned long symsize;
5872 unsigned long offset;
5873 char str[KSYM_SYMBOL_LEN];
5877 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5881 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5885 mod_func->name = kstrdup(str, GFP_KERNEL);
5886 if (!mod_func->name) {
5891 mod_func->ip = rec->ip - offset;
5892 mod_func->size = symsize;
5894 mod_map->num_funcs++;
5896 list_add_rcu(&mod_func->list, &mod_map->funcs);
5899 static struct ftrace_mod_map *
5900 allocate_ftrace_mod_map(struct module *mod,
5901 unsigned long start, unsigned long end)
5903 struct ftrace_mod_map *mod_map;
5905 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5910 mod_map->start_addr = start;
5911 mod_map->end_addr = end;
5912 mod_map->num_funcs = 0;
5914 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5916 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5922 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5923 unsigned long addr, unsigned long *size,
5924 unsigned long *off, char *sym)
5926 struct ftrace_mod_func *found_func = NULL;
5927 struct ftrace_mod_func *mod_func;
5929 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5930 if (addr >= mod_func->ip &&
5931 addr < mod_func->ip + mod_func->size) {
5932 found_func = mod_func;
5939 *size = found_func->size;
5941 *off = addr - found_func->ip;
5943 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5945 return found_func->name;
5952 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
5953 unsigned long *off, char **modname, char *sym)
5955 struct ftrace_mod_map *mod_map;
5956 const char *ret = NULL;
5958 /* mod_map is freed via call_rcu_sched() */
5960 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5961 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
5964 *modname = mod_map->mod->name;
5973 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
5974 char *type, char *name,
5975 char *module_name, int *exported)
5977 struct ftrace_mod_map *mod_map;
5978 struct ftrace_mod_func *mod_func;
5981 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5983 if (symnum >= mod_map->num_funcs) {
5984 symnum -= mod_map->num_funcs;
5988 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5994 *value = mod_func->ip;
5996 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
5997 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6010 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6011 struct dyn_ftrace *rec) { }
6012 static inline struct ftrace_mod_map *
6013 allocate_ftrace_mod_map(struct module *mod,
6014 unsigned long start, unsigned long end)
6018 #endif /* CONFIG_MODULES */
6020 struct ftrace_init_func {
6021 struct list_head list;
6025 /* Clear any init ips from hashes */
6027 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6029 struct ftrace_func_entry *entry;
6031 if (ftrace_hash_empty(hash))
6034 entry = __ftrace_lookup_ip(hash, func->ip);
6037 * Do not allow this rec to match again.
6038 * Yeah, it may waste some memory, but will be removed
6039 * if/when the hash is modified again.
6046 clear_func_from_hashes(struct ftrace_init_func *func)
6048 struct trace_array *tr;
6050 mutex_lock(&trace_types_lock);
6051 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6052 if (!tr->ops || !tr->ops->func_hash)
6054 mutex_lock(&tr->ops->func_hash->regex_lock);
6055 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6056 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6057 mutex_unlock(&tr->ops->func_hash->regex_lock);
6059 mutex_unlock(&trace_types_lock);
6062 static void add_to_clear_hash_list(struct list_head *clear_list,
6063 struct dyn_ftrace *rec)
6065 struct ftrace_init_func *func;
6067 func = kmalloc(sizeof(*func), GFP_KERNEL);
6069 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6074 list_add(&func->list, clear_list);
6077 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
6079 unsigned long start = (unsigned long)(start_ptr);
6080 unsigned long end = (unsigned long)(end_ptr);
6081 struct ftrace_page **last_pg = &ftrace_pages_start;
6082 struct ftrace_page *pg;
6083 struct dyn_ftrace *rec;
6084 struct dyn_ftrace key;
6085 struct ftrace_mod_map *mod_map = NULL;
6086 struct ftrace_init_func *func, *func_next;
6087 struct list_head clear_hash;
6090 INIT_LIST_HEAD(&clear_hash);
6093 key.flags = end; /* overload flags, as it is unsigned long */
6095 mutex_lock(&ftrace_lock);
6098 * If we are freeing module init memory, then check if
6099 * any tracer is active. If so, we need to save a mapping of
6100 * the module functions being freed with the address.
6102 if (mod && ftrace_ops_list != &ftrace_list_end)
6103 mod_map = allocate_ftrace_mod_map(mod, start, end);
6105 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6106 if (end < pg->records[0].ip ||
6107 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6110 rec = bsearch(&key, pg->records, pg->index,
6111 sizeof(struct dyn_ftrace),
6116 /* rec will be cleared from hashes after ftrace_lock unlock */
6117 add_to_clear_hash_list(&clear_hash, rec);
6120 save_ftrace_mod_rec(mod_map, rec);
6123 ftrace_update_tot_cnt--;
6125 *last_pg = pg->next;
6126 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6127 free_pages((unsigned long)pg->records, order);
6129 pg = container_of(last_pg, struct ftrace_page, next);
6134 memmove(rec, rec + 1,
6135 (pg->index - (rec - pg->records)) * sizeof(*rec));
6136 /* More than one function may be in this block */
6139 mutex_unlock(&ftrace_lock);
6141 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6142 clear_func_from_hashes(func);
6147 void __init ftrace_free_init_mem(void)
6149 void *start = (void *)(&__init_begin);
6150 void *end = (void *)(&__init_end);
6152 ftrace_free_mem(NULL, start, end);
6155 void __init ftrace_init(void)
6157 extern unsigned long __start_mcount_loc[];
6158 extern unsigned long __stop_mcount_loc[];
6159 unsigned long count, flags;
6162 local_irq_save(flags);
6163 ret = ftrace_dyn_arch_init();
6164 local_irq_restore(flags);
6168 count = __stop_mcount_loc - __start_mcount_loc;
6170 pr_info("ftrace: No functions to be traced?\n");
6174 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6175 count, count / ENTRIES_PER_PAGE + 1);
6177 last_ftrace_enabled = ftrace_enabled = 1;
6179 ret = ftrace_process_locs(NULL,
6183 set_ftrace_early_filters();
6187 ftrace_disabled = 1;
6190 /* Do nothing if arch does not support this */
6191 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6195 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6197 arch_ftrace_update_trampoline(ops);
6200 void ftrace_init_trace_array(struct trace_array *tr)
6202 INIT_LIST_HEAD(&tr->func_probes);
6203 INIT_LIST_HEAD(&tr->mod_trace);
6204 INIT_LIST_HEAD(&tr->mod_notrace);
6208 static struct ftrace_ops global_ops = {
6209 .func = ftrace_stub,
6210 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6211 FTRACE_OPS_FL_INITIALIZED |
6215 static int __init ftrace_nodyn_init(void)
6220 core_initcall(ftrace_nodyn_init);
6222 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6223 static inline void ftrace_startup_enable(int command) { }
6224 static inline void ftrace_startup_all(int command) { }
6225 /* Keep as macros so we do not need to define the commands */
6226 # define ftrace_startup(ops, command) \
6228 int ___ret = __register_ftrace_function(ops); \
6230 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
6233 # define ftrace_shutdown(ops, command) \
6235 int ___ret = __unregister_ftrace_function(ops); \
6237 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
6241 # define ftrace_startup_sysctl() do { } while (0)
6242 # define ftrace_shutdown_sysctl() do { } while (0)
6245 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
6250 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6254 #endif /* CONFIG_DYNAMIC_FTRACE */
6256 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6258 tr->ops = &global_ops;
6259 tr->ops->private = tr;
6260 ftrace_init_trace_array(tr);
6263 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6265 /* If we filter on pids, update to use the pid function */
6266 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6267 if (WARN_ON(tr->ops->func != ftrace_stub))
6268 printk("ftrace ops had %pS for function\n",
6271 tr->ops->func = func;
6272 tr->ops->private = tr;
6275 void ftrace_reset_array_ops(struct trace_array *tr)
6277 tr->ops->func = ftrace_stub;
6281 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6282 struct ftrace_ops *ignored, struct pt_regs *regs)
6284 struct ftrace_ops *op;
6287 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6292 * Some of the ops may be dynamically allocated,
6293 * they must be freed after a synchronize_sched().
6295 preempt_disable_notrace();
6297 do_for_each_ftrace_op(op, ftrace_ops_list) {
6299 * Check the following for each ops before calling their func:
6300 * if RCU flag is set, then rcu_is_watching() must be true
6301 * if PER_CPU is set, then ftrace_function_local_disable()
6303 * Otherwise test if the ip matches the ops filter
6305 * If any of the above fails then the op->func() is not executed.
6307 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6308 ftrace_ops_test(op, ip, regs)) {
6309 if (FTRACE_WARN_ON(!op->func)) {
6310 pr_warn("op=%p %pS\n", op, op);
6313 op->func(ip, parent_ip, op, regs);
6315 } while_for_each_ftrace_op(op);
6317 preempt_enable_notrace();
6318 trace_clear_recursion(bit);
6322 * Some archs only support passing ip and parent_ip. Even though
6323 * the list function ignores the op parameter, we do not want any
6324 * C side effects, where a function is called without the caller
6325 * sending a third parameter.
6326 * Archs are to support both the regs and ftrace_ops at the same time.
6327 * If they support ftrace_ops, it is assumed they support regs.
6328 * If call backs want to use regs, they must either check for regs
6329 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6330 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6331 * An architecture can pass partial regs with ftrace_ops and still
6332 * set the ARCH_SUPPORTS_FTRACE_OPS.
6334 #if ARCH_SUPPORTS_FTRACE_OPS
6335 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6336 struct ftrace_ops *op, struct pt_regs *regs)
6338 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6341 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6343 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6348 * If there's only one function registered but it does not support
6349 * recursion, needs RCU protection and/or requires per cpu handling, then
6350 * this function will be called by the mcount trampoline.
6352 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6353 struct ftrace_ops *op, struct pt_regs *regs)
6357 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6360 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6364 preempt_disable_notrace();
6366 op->func(ip, parent_ip, op, regs);
6368 preempt_enable_notrace();
6369 trace_clear_recursion(bit);
6373 * ftrace_ops_get_func - get the function a trampoline should call
6374 * @ops: the ops to get the function for
6376 * Normally the mcount trampoline will call the ops->func, but there
6377 * are times that it should not. For example, if the ops does not
6378 * have its own recursion protection, then it should call the
6379 * ftrace_ops_assist_func() instead.
6381 * Returns the function that the trampoline should call for @ops.
6383 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6386 * If the function does not handle recursion, needs to be RCU safe,
6387 * or does per cpu logic, then we need to call the assist handler.
6389 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6390 ops->flags & FTRACE_OPS_FL_RCU)
6391 return ftrace_ops_assist_func;
6397 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6398 struct task_struct *prev, struct task_struct *next)
6400 struct trace_array *tr = data;
6401 struct trace_pid_list *pid_list;
6403 pid_list = rcu_dereference_sched(tr->function_pids);
6405 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6406 trace_ignore_this_task(pid_list, next));
6410 ftrace_pid_follow_sched_process_fork(void *data,
6411 struct task_struct *self,
6412 struct task_struct *task)
6414 struct trace_pid_list *pid_list;
6415 struct trace_array *tr = data;
6417 pid_list = rcu_dereference_sched(tr->function_pids);
6418 trace_filter_add_remove_task(pid_list, self, task);
6422 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6424 struct trace_pid_list *pid_list;
6425 struct trace_array *tr = data;
6427 pid_list = rcu_dereference_sched(tr->function_pids);
6428 trace_filter_add_remove_task(pid_list, NULL, task);
6431 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6434 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6436 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6439 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6441 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6446 static void clear_ftrace_pids(struct trace_array *tr)
6448 struct trace_pid_list *pid_list;
6451 pid_list = rcu_dereference_protected(tr->function_pids,
6452 lockdep_is_held(&ftrace_lock));
6456 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6458 for_each_possible_cpu(cpu)
6459 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6461 rcu_assign_pointer(tr->function_pids, NULL);
6463 /* Wait till all users are no longer using pid filtering */
6464 synchronize_sched();
6466 trace_free_pid_list(pid_list);
6469 void ftrace_clear_pids(struct trace_array *tr)
6471 mutex_lock(&ftrace_lock);
6473 clear_ftrace_pids(tr);
6475 mutex_unlock(&ftrace_lock);
6478 static void ftrace_pid_reset(struct trace_array *tr)
6480 mutex_lock(&ftrace_lock);
6481 clear_ftrace_pids(tr);
6483 ftrace_update_pid_func();
6484 ftrace_startup_all(0);
6486 mutex_unlock(&ftrace_lock);
6489 /* Greater than any max PID */
6490 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6492 static void *fpid_start(struct seq_file *m, loff_t *pos)
6495 struct trace_pid_list *pid_list;
6496 struct trace_array *tr = m->private;
6498 mutex_lock(&ftrace_lock);
6499 rcu_read_lock_sched();
6501 pid_list = rcu_dereference_sched(tr->function_pids);
6504 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6506 return trace_pid_start(pid_list, pos);
6509 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6511 struct trace_array *tr = m->private;
6512 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6514 if (v == FTRACE_NO_PIDS)
6517 return trace_pid_next(pid_list, v, pos);
6520 static void fpid_stop(struct seq_file *m, void *p)
6523 rcu_read_unlock_sched();
6524 mutex_unlock(&ftrace_lock);
6527 static int fpid_show(struct seq_file *m, void *v)
6529 if (v == FTRACE_NO_PIDS) {
6530 seq_puts(m, "no pid\n");
6534 return trace_pid_show(m, v);
6537 static const struct seq_operations ftrace_pid_sops = {
6538 .start = fpid_start,
6545 ftrace_pid_open(struct inode *inode, struct file *file)
6547 struct trace_array *tr = inode->i_private;
6551 if (trace_array_get(tr) < 0)
6554 if ((file->f_mode & FMODE_WRITE) &&
6555 (file->f_flags & O_TRUNC))
6556 ftrace_pid_reset(tr);
6558 ret = seq_open(file, &ftrace_pid_sops);
6560 trace_array_put(tr);
6562 m = file->private_data;
6563 /* copy tr over to seq ops */
6570 static void ignore_task_cpu(void *data)
6572 struct trace_array *tr = data;
6573 struct trace_pid_list *pid_list;
6576 * This function is called by on_each_cpu() while the
6577 * event_mutex is held.
6579 pid_list = rcu_dereference_protected(tr->function_pids,
6580 mutex_is_locked(&ftrace_lock));
6582 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6583 trace_ignore_this_task(pid_list, current));
6587 ftrace_pid_write(struct file *filp, const char __user *ubuf,
6588 size_t cnt, loff_t *ppos)
6590 struct seq_file *m = filp->private_data;
6591 struct trace_array *tr = m->private;
6592 struct trace_pid_list *filtered_pids = NULL;
6593 struct trace_pid_list *pid_list;
6599 mutex_lock(&ftrace_lock);
6601 filtered_pids = rcu_dereference_protected(tr->function_pids,
6602 lockdep_is_held(&ftrace_lock));
6604 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6608 rcu_assign_pointer(tr->function_pids, pid_list);
6610 if (filtered_pids) {
6611 synchronize_sched();
6612 trace_free_pid_list(filtered_pids);
6613 } else if (pid_list) {
6614 /* Register a probe to set whether to ignore the tracing of a task */
6615 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6619 * Ignoring of pids is done at task switch. But we have to
6620 * check for those tasks that are currently running.
6621 * Always do this in case a pid was appended or removed.
6623 on_each_cpu(ignore_task_cpu, tr, 1);
6625 ftrace_update_pid_func();
6626 ftrace_startup_all(0);
6628 mutex_unlock(&ftrace_lock);
6637 ftrace_pid_release(struct inode *inode, struct file *file)
6639 struct trace_array *tr = inode->i_private;
6641 trace_array_put(tr);
6643 return seq_release(inode, file);
6646 static const struct file_operations ftrace_pid_fops = {
6647 .open = ftrace_pid_open,
6648 .write = ftrace_pid_write,
6650 .llseek = tracing_lseek,
6651 .release = ftrace_pid_release,
6654 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
6656 trace_create_file("set_ftrace_pid", 0644, d_tracer,
6657 tr, &ftrace_pid_fops);
6660 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6661 struct dentry *d_tracer)
6663 /* Only the top level directory has the dyn_tracefs and profile */
6664 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6666 ftrace_init_dyn_tracefs(d_tracer);
6667 ftrace_profile_tracefs(d_tracer);
6671 * ftrace_kill - kill ftrace
6673 * This function should be used by panic code. It stops ftrace
6674 * but in a not so nice way. If you need to simply kill ftrace
6675 * from a non-atomic section, use ftrace_kill.
6677 void ftrace_kill(void)
6679 ftrace_disabled = 1;
6681 ftrace_trace_function = ftrace_stub;
6685 * Test if ftrace is dead or not.
6687 int ftrace_is_dead(void)
6689 return ftrace_disabled;
6693 * register_ftrace_function - register a function for profiling
6694 * @ops - ops structure that holds the function for profiling.
6696 * Register a function to be called by all functions in the
6699 * Note: @ops->func and all the functions it calls must be labeled
6700 * with "notrace", otherwise it will go into a
6703 int register_ftrace_function(struct ftrace_ops *ops)
6707 ftrace_ops_init(ops);
6709 mutex_lock(&ftrace_lock);
6711 ret = ftrace_startup(ops, 0);
6713 mutex_unlock(&ftrace_lock);
6717 EXPORT_SYMBOL_GPL(register_ftrace_function);
6720 * unregister_ftrace_function - unregister a function for profiling.
6721 * @ops - ops structure that holds the function to unregister
6723 * Unregister a function that was added to be called by ftrace profiling.
6725 int unregister_ftrace_function(struct ftrace_ops *ops)
6729 mutex_lock(&ftrace_lock);
6730 ret = ftrace_shutdown(ops, 0);
6731 mutex_unlock(&ftrace_lock);
6735 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
6738 ftrace_enable_sysctl(struct ctl_table *table, int write,
6739 void __user *buffer, size_t *lenp,
6744 mutex_lock(&ftrace_lock);
6746 if (unlikely(ftrace_disabled))
6749 ret = proc_dointvec(table, write, buffer, lenp, ppos);
6751 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
6754 last_ftrace_enabled = !!ftrace_enabled;
6756 if (ftrace_enabled) {
6758 /* we are starting ftrace again */
6759 if (rcu_dereference_protected(ftrace_ops_list,
6760 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
6761 update_ftrace_function();
6763 ftrace_startup_sysctl();
6766 /* stopping ftrace calls (just send to ftrace_stub) */
6767 ftrace_trace_function = ftrace_stub;
6769 ftrace_shutdown_sysctl();
6773 mutex_unlock(&ftrace_lock);
6777 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6779 static struct ftrace_ops graph_ops = {
6780 .func = ftrace_stub,
6781 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6782 FTRACE_OPS_FL_INITIALIZED |
6785 #ifdef FTRACE_GRAPH_TRAMP_ADDR
6786 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
6787 /* trampoline_size is only needed for dynamically allocated tramps */
6789 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6792 void ftrace_graph_sleep_time_control(bool enable)
6794 fgraph_sleep_time = enable;
6797 void ftrace_graph_graph_time_control(bool enable)
6799 fgraph_graph_time = enable;
6802 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6807 /* The callbacks that hook a function */
6808 trace_func_graph_ret_t ftrace_graph_return =
6809 (trace_func_graph_ret_t)ftrace_stub;
6810 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
6811 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
6813 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6814 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6818 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6819 struct task_struct *g, *t;
6821 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6823 kmalloc_array(FTRACE_RETFUNC_DEPTH,
6824 sizeof(struct ftrace_ret_stack),
6826 if (!ret_stack_list[i]) {
6834 read_lock(&tasklist_lock);
6835 do_each_thread(g, t) {
6841 if (t->ret_stack == NULL) {
6842 atomic_set(&t->tracing_graph_pause, 0);
6843 atomic_set(&t->trace_overrun, 0);
6844 t->curr_ret_stack = -1;
6845 /* Make sure the tasks see the -1 first: */
6847 t->ret_stack = ret_stack_list[start++];
6849 } while_each_thread(g, t);
6852 read_unlock(&tasklist_lock);
6854 for (i = start; i < end; i++)
6855 kfree(ret_stack_list[i]);
6860 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
6861 struct task_struct *prev, struct task_struct *next)
6863 unsigned long long timestamp;
6867 * Does the user want to count the time a function was asleep.
6868 * If so, do not update the time stamps.
6870 if (fgraph_sleep_time)
6873 timestamp = trace_clock_local();
6875 prev->ftrace_timestamp = timestamp;
6877 /* only process tasks that we timestamped */
6878 if (!next->ftrace_timestamp)
6882 * Update all the counters in next to make up for the
6883 * time next was sleeping.
6885 timestamp -= next->ftrace_timestamp;
6887 for (index = next->curr_ret_stack; index >= 0; index--)
6888 next->ret_stack[index].calltime += timestamp;
6891 /* Allocate a return stack for each task */
6892 static int start_graph_tracing(void)
6894 struct ftrace_ret_stack **ret_stack_list;
6897 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
6898 sizeof(struct ftrace_ret_stack *),
6901 if (!ret_stack_list)
6904 /* The cpu_boot init_task->ret_stack will never be freed */
6905 for_each_online_cpu(cpu) {
6906 if (!idle_task(cpu)->ret_stack)
6907 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
6911 ret = alloc_retstack_tasklist(ret_stack_list);
6912 } while (ret == -EAGAIN);
6915 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6917 pr_info("ftrace_graph: Couldn't activate tracepoint"
6918 " probe to kernel_sched_switch\n");
6921 kfree(ret_stack_list);
6926 * Hibernation protection.
6927 * The state of the current task is too much unstable during
6928 * suspend/restore to disk. We want to protect against that.
6931 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6935 case PM_HIBERNATION_PREPARE:
6936 pause_graph_tracing();
6939 case PM_POST_HIBERNATION:
6940 unpause_graph_tracing();
6946 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6948 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6950 return __ftrace_graph_entry(trace);
6954 * The function graph tracer should only trace the functions defined
6955 * by set_ftrace_filter and set_ftrace_notrace. If another function
6956 * tracer ops is registered, the graph tracer requires testing the
6957 * function against the global ops, and not just trace any function
6958 * that any ftrace_ops registered.
6960 static void update_function_graph_func(void)
6962 struct ftrace_ops *op;
6963 bool do_test = false;
6966 * The graph and global ops share the same set of functions
6967 * to test. If any other ops is on the list, then
6968 * the graph tracing needs to test if its the function
6971 do_for_each_ftrace_op(op, ftrace_ops_list) {
6972 if (op != &global_ops && op != &graph_ops &&
6973 op != &ftrace_list_end) {
6975 /* in double loop, break out with goto */
6978 } while_for_each_ftrace_op(op);
6981 ftrace_graph_entry = ftrace_graph_entry_test;
6983 ftrace_graph_entry = __ftrace_graph_entry;
6986 static struct notifier_block ftrace_suspend_notifier = {
6987 .notifier_call = ftrace_suspend_notifier_call,
6990 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
6991 trace_func_graph_ent_t entryfunc)
6995 mutex_lock(&ftrace_lock);
6997 /* we currently allow only one tracer registered at a time */
6998 if (ftrace_graph_active) {
7003 register_pm_notifier(&ftrace_suspend_notifier);
7005 ftrace_graph_active++;
7006 ret = start_graph_tracing();
7008 ftrace_graph_active--;
7012 ftrace_graph_return = retfunc;
7015 * Update the indirect function to the entryfunc, and the
7016 * function that gets called to the entry_test first. Then
7017 * call the update fgraph entry function to determine if
7018 * the entryfunc should be called directly or not.
7020 __ftrace_graph_entry = entryfunc;
7021 ftrace_graph_entry = ftrace_graph_entry_test;
7022 update_function_graph_func();
7024 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
7026 mutex_unlock(&ftrace_lock);
7030 void unregister_ftrace_graph(void)
7032 mutex_lock(&ftrace_lock);
7034 if (unlikely(!ftrace_graph_active))
7037 ftrace_graph_active--;
7038 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
7039 ftrace_graph_entry = ftrace_graph_entry_stub;
7040 __ftrace_graph_entry = ftrace_graph_entry_stub;
7041 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
7042 unregister_pm_notifier(&ftrace_suspend_notifier);
7043 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
7046 mutex_unlock(&ftrace_lock);
7049 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
7052 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
7054 atomic_set(&t->tracing_graph_pause, 0);
7055 atomic_set(&t->trace_overrun, 0);
7056 t->ftrace_timestamp = 0;
7057 /* make curr_ret_stack visible before we add the ret_stack */
7059 t->ret_stack = ret_stack;
7063 * Allocate a return stack for the idle task. May be the first
7064 * time through, or it may be done by CPU hotplug online.
7066 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
7068 t->curr_ret_stack = -1;
7070 * The idle task has no parent, it either has its own
7071 * stack or no stack at all.
7074 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
7076 if (ftrace_graph_active) {
7077 struct ftrace_ret_stack *ret_stack;
7079 ret_stack = per_cpu(idle_ret_stack, cpu);
7082 kmalloc_array(FTRACE_RETFUNC_DEPTH,
7083 sizeof(struct ftrace_ret_stack),
7087 per_cpu(idle_ret_stack, cpu) = ret_stack;
7089 graph_init_task(t, ret_stack);
7093 /* Allocate a return stack for newly created task */
7094 void ftrace_graph_init_task(struct task_struct *t)
7096 /* Make sure we do not use the parent ret_stack */
7097 t->ret_stack = NULL;
7098 t->curr_ret_stack = -1;
7100 if (ftrace_graph_active) {
7101 struct ftrace_ret_stack *ret_stack;
7103 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
7104 sizeof(struct ftrace_ret_stack),
7108 graph_init_task(t, ret_stack);
7112 void ftrace_graph_exit_task(struct task_struct *t)
7114 struct ftrace_ret_stack *ret_stack = t->ret_stack;
7116 t->ret_stack = NULL;
7117 /* NULL must become visible to IRQs before we free it: */