1 // SPDX-License-Identifier: GPL-2.0
3 * Infrastructure for profiling code inserted by 'gcc -pg'.
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
8 * Originally ported from the -rt patch by:
9 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
11 * Based on code in the latency_tracer, that is:
13 * Copyright (C) 2004-2006 Ingo Molnar
14 * Copyright (C) 2004 Nadia Yvette Chambers
17 #include <linux/stop_machine.h>
18 #include <linux/clocksource.h>
19 #include <linux/sched/task.h>
20 #include <linux/kallsyms.h>
21 #include <linux/security.h>
22 #include <linux/seq_file.h>
23 #include <linux/tracefs.h>
24 #include <linux/hardirq.h>
25 #include <linux/kthread.h>
26 #include <linux/uaccess.h>
27 #include <linux/bsearch.h>
28 #include <linux/module.h>
29 #include <linux/ftrace.h>
30 #include <linux/sysctl.h>
31 #include <linux/slab.h>
32 #include <linux/ctype.h>
33 #include <linux/sort.h>
34 #include <linux/list.h>
35 #include <linux/hash.h>
36 #include <linux/rcupdate.h>
37 #include <linux/kprobes.h>
39 #include <trace/events/sched.h>
41 #include <asm/sections.h>
42 #include <asm/setup.h>
44 #include "ftrace_internal.h"
45 #include "trace_output.h"
46 #include "trace_stat.h"
48 #define FTRACE_WARN_ON(cond) \
56 #define FTRACE_WARN_ON_ONCE(cond) \
59 if (WARN_ON_ONCE(___r)) \
64 /* hash bits for specific function selection */
65 #define FTRACE_HASH_BITS 7
66 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
67 #define FTRACE_HASH_DEFAULT_BITS 10
68 #define FTRACE_HASH_MAX_BITS 12
70 #ifdef CONFIG_DYNAMIC_FTRACE
71 #define INIT_OPS_HASH(opsname) \
72 .func_hash = &opsname.local_hash, \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
75 #define INIT_OPS_HASH(opsname)
79 FTRACE_MODIFY_ENABLE_FL = (1 << 0),
80 FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
83 struct ftrace_ops ftrace_list_end __read_mostly = {
85 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
86 INIT_OPS_HASH(ftrace_list_end)
89 /* ftrace_enabled is a method to turn ftrace on or off */
90 int ftrace_enabled __read_mostly;
91 static int last_ftrace_enabled;
93 /* Current function tracing op */
94 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
95 /* What to set function_trace_op to */
96 static struct ftrace_ops *set_function_trace_op;
98 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
100 struct trace_array *tr;
102 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
107 return tr->function_pids != NULL;
110 static void ftrace_update_trampoline(struct ftrace_ops *ops);
113 * ftrace_disabled is set when an anomaly is discovered.
114 * ftrace_disabled is much stronger than ftrace_enabled.
116 static int ftrace_disabled __read_mostly;
118 DEFINE_MUTEX(ftrace_lock);
120 struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
121 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
122 struct ftrace_ops global_ops;
124 #if ARCH_SUPPORTS_FTRACE_OPS
125 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
126 struct ftrace_ops *op, struct pt_regs *regs);
128 /* See comment below, where ftrace_ops_list_func is defined */
129 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
130 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
133 static inline void ftrace_ops_init(struct ftrace_ops *ops)
135 #ifdef CONFIG_DYNAMIC_FTRACE
136 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
137 mutex_init(&ops->local_hash.regex_lock);
138 ops->func_hash = &ops->local_hash;
139 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
144 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
145 struct ftrace_ops *op, struct pt_regs *regs)
147 struct trace_array *tr = op->private;
149 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
152 op->saved_func(ip, parent_ip, op, regs);
155 static void ftrace_sync(struct work_struct *work)
158 * This function is just a stub to implement a hard force
159 * of synchronize_rcu(). This requires synchronizing
160 * tasks even in userspace and idle.
162 * Yes, function tracing is rude.
166 static void ftrace_sync_ipi(void *data)
168 /* Probably not needed, but do it anyway */
172 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
175 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
176 * then it needs to call the list anyway.
178 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
179 FTRACE_FORCE_LIST_FUNC)
180 return ftrace_ops_list_func;
182 return ftrace_ops_get_func(ops);
185 static void update_ftrace_function(void)
190 * Prepare the ftrace_ops that the arch callback will use.
191 * If there's only one ftrace_ops registered, the ftrace_ops_list
192 * will point to the ops we want.
194 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
195 lockdep_is_held(&ftrace_lock));
197 /* If there's no ftrace_ops registered, just call the stub function */
198 if (set_function_trace_op == &ftrace_list_end) {
202 * If we are at the end of the list and this ops is
203 * recursion safe and not dynamic and the arch supports passing ops,
204 * then have the mcount trampoline call the function directly.
206 } else if (rcu_dereference_protected(ftrace_ops_list->next,
207 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
208 func = ftrace_ops_get_list_func(ftrace_ops_list);
211 /* Just use the default ftrace_ops */
212 set_function_trace_op = &ftrace_list_end;
213 func = ftrace_ops_list_func;
216 update_function_graph_func();
218 /* If there's no change, then do nothing more here */
219 if (ftrace_trace_function == func)
223 * If we are using the list function, it doesn't care
224 * about the function_trace_ops.
226 if (func == ftrace_ops_list_func) {
227 ftrace_trace_function = func;
229 * Don't even bother setting function_trace_ops,
230 * it would be racy to do so anyway.
235 #ifndef CONFIG_DYNAMIC_FTRACE
237 * For static tracing, we need to be a bit more careful.
238 * The function change takes affect immediately. Thus,
239 * we need to coorditate the setting of the function_trace_ops
240 * with the setting of the ftrace_trace_function.
242 * Set the function to the list ops, which will call the
243 * function we want, albeit indirectly, but it handles the
244 * ftrace_ops and doesn't depend on function_trace_op.
246 ftrace_trace_function = ftrace_ops_list_func;
248 * Make sure all CPUs see this. Yes this is slow, but static
249 * tracing is slow and nasty to have enabled.
251 schedule_on_each_cpu(ftrace_sync);
252 /* Now all cpus are using the list ops. */
253 function_trace_op = set_function_trace_op;
254 /* Make sure the function_trace_op is visible on all CPUs */
256 /* Nasty way to force a rmb on all cpus */
257 smp_call_function(ftrace_sync_ipi, NULL, 1);
258 /* OK, we are all set to update the ftrace_trace_function now! */
259 #endif /* !CONFIG_DYNAMIC_FTRACE */
261 ftrace_trace_function = func;
264 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
265 struct ftrace_ops *ops)
267 rcu_assign_pointer(ops->next, *list);
270 * We are entering ops into the list but another
271 * CPU might be walking that list. We need to make sure
272 * the ops->next pointer is valid before another CPU sees
273 * the ops pointer included into the list.
275 rcu_assign_pointer(*list, ops);
278 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
279 struct ftrace_ops *ops)
281 struct ftrace_ops **p;
284 * If we are removing the last function, then simply point
285 * to the ftrace_stub.
287 if (rcu_dereference_protected(*list,
288 lockdep_is_held(&ftrace_lock)) == ops &&
289 rcu_dereference_protected(ops->next,
290 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
291 *list = &ftrace_list_end;
295 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
306 static void ftrace_update_trampoline(struct ftrace_ops *ops);
308 int __register_ftrace_function(struct ftrace_ops *ops)
310 if (ops->flags & FTRACE_OPS_FL_DELETED)
313 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
316 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
318 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
319 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
320 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
322 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
323 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
326 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
327 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
329 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
332 if (!core_kernel_data((unsigned long)ops))
333 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
335 add_ftrace_ops(&ftrace_ops_list, ops);
337 /* Always save the function, and reset at unregistering */
338 ops->saved_func = ops->func;
340 if (ftrace_pids_enabled(ops))
341 ops->func = ftrace_pid_func;
343 ftrace_update_trampoline(ops);
346 update_ftrace_function();
351 int __unregister_ftrace_function(struct ftrace_ops *ops)
355 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
358 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
364 update_ftrace_function();
366 ops->func = ops->saved_func;
371 static void ftrace_update_pid_func(void)
373 struct ftrace_ops *op;
375 /* Only do something if we are tracing something */
376 if (ftrace_trace_function == ftrace_stub)
379 do_for_each_ftrace_op(op, ftrace_ops_list) {
380 if (op->flags & FTRACE_OPS_FL_PID) {
381 op->func = ftrace_pids_enabled(op) ?
382 ftrace_pid_func : op->saved_func;
383 ftrace_update_trampoline(op);
385 } while_for_each_ftrace_op(op);
387 update_ftrace_function();
390 #ifdef CONFIG_FUNCTION_PROFILER
391 struct ftrace_profile {
392 struct hlist_node node;
394 unsigned long counter;
395 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
396 unsigned long long time;
397 unsigned long long time_squared;
401 struct ftrace_profile_page {
402 struct ftrace_profile_page *next;
404 struct ftrace_profile records[];
407 struct ftrace_profile_stat {
409 struct hlist_head *hash;
410 struct ftrace_profile_page *pages;
411 struct ftrace_profile_page *start;
412 struct tracer_stat stat;
415 #define PROFILE_RECORDS_SIZE \
416 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
418 #define PROFILES_PER_PAGE \
419 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
421 static int ftrace_profile_enabled __read_mostly;
423 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
424 static DEFINE_MUTEX(ftrace_profile_lock);
426 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
428 #define FTRACE_PROFILE_HASH_BITS 10
429 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
432 function_stat_next(void *v, int idx)
434 struct ftrace_profile *rec = v;
435 struct ftrace_profile_page *pg;
437 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
443 if ((void *)rec >= (void *)&pg->records[pg->index]) {
447 rec = &pg->records[0];
455 static void *function_stat_start(struct tracer_stat *trace)
457 struct ftrace_profile_stat *stat =
458 container_of(trace, struct ftrace_profile_stat, stat);
460 if (!stat || !stat->start)
463 return function_stat_next(&stat->start->records[0], 0);
466 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
467 /* function graph compares on total time */
468 static int function_stat_cmp(void *p1, void *p2)
470 struct ftrace_profile *a = p1;
471 struct ftrace_profile *b = p2;
473 if (a->time < b->time)
475 if (a->time > b->time)
481 /* not function graph compares against hits */
482 static int function_stat_cmp(void *p1, void *p2)
484 struct ftrace_profile *a = p1;
485 struct ftrace_profile *b = p2;
487 if (a->counter < b->counter)
489 if (a->counter > b->counter)
496 static int function_stat_headers(struct seq_file *m)
498 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
499 seq_puts(m, " Function "
502 "--- ---- --- ---\n");
504 seq_puts(m, " Function Hit\n"
510 static int function_stat_show(struct seq_file *m, void *v)
512 struct ftrace_profile *rec = v;
513 char str[KSYM_SYMBOL_LEN];
515 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
516 static struct trace_seq s;
517 unsigned long long avg;
518 unsigned long long stddev;
520 mutex_lock(&ftrace_profile_lock);
522 /* we raced with function_profile_reset() */
523 if (unlikely(rec->counter == 0)) {
528 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
530 do_div(avg, rec->counter);
531 if (tracing_thresh && (avg < tracing_thresh))
535 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
536 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
538 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
541 /* Sample standard deviation (s^2) */
542 if (rec->counter <= 1)
546 * Apply Welford's method:
547 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
549 stddev = rec->counter * rec->time_squared -
550 rec->time * rec->time;
553 * Divide only 1000 for ns^2 -> us^2 conversion.
554 * trace_print_graph_duration will divide 1000 again.
556 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
560 trace_print_graph_duration(rec->time, &s);
561 trace_seq_puts(&s, " ");
562 trace_print_graph_duration(avg, &s);
563 trace_seq_puts(&s, " ");
564 trace_print_graph_duration(stddev, &s);
565 trace_print_seq(m, &s);
569 mutex_unlock(&ftrace_profile_lock);
574 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
576 struct ftrace_profile_page *pg;
578 pg = stat->pages = stat->start;
581 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
586 memset(stat->hash, 0,
587 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
590 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
592 struct ftrace_profile_page *pg;
597 /* If we already allocated, do nothing */
601 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
605 #ifdef CONFIG_DYNAMIC_FTRACE
606 functions = ftrace_update_tot_cnt;
609 * We do not know the number of functions that exist because
610 * dynamic tracing is what counts them. With past experience
611 * we have around 20K functions. That should be more than enough.
612 * It is highly unlikely we will execute every function in
618 pg = stat->start = stat->pages;
620 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
622 for (i = 1; i < pages; i++) {
623 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
634 unsigned long tmp = (unsigned long)pg;
646 static int ftrace_profile_init_cpu(int cpu)
648 struct ftrace_profile_stat *stat;
651 stat = &per_cpu(ftrace_profile_stats, cpu);
654 /* If the profile is already created, simply reset it */
655 ftrace_profile_reset(stat);
660 * We are profiling all functions, but usually only a few thousand
661 * functions are hit. We'll make a hash of 1024 items.
663 size = FTRACE_PROFILE_HASH_SIZE;
665 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
670 /* Preallocate the function profiling pages */
671 if (ftrace_profile_pages_init(stat) < 0) {
680 static int ftrace_profile_init(void)
685 for_each_possible_cpu(cpu) {
686 ret = ftrace_profile_init_cpu(cpu);
694 /* interrupts must be disabled */
695 static struct ftrace_profile *
696 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
698 struct ftrace_profile *rec;
699 struct hlist_head *hhd;
702 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
703 hhd = &stat->hash[key];
705 if (hlist_empty(hhd))
708 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
716 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
717 struct ftrace_profile *rec)
721 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
722 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
726 * The memory is already allocated, this simply finds a new record to use.
728 static struct ftrace_profile *
729 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
731 struct ftrace_profile *rec = NULL;
733 /* prevent recursion (from NMIs) */
734 if (atomic_inc_return(&stat->disabled) != 1)
738 * Try to find the function again since an NMI
739 * could have added it
741 rec = ftrace_find_profiled_func(stat, ip);
745 if (stat->pages->index == PROFILES_PER_PAGE) {
746 if (!stat->pages->next)
748 stat->pages = stat->pages->next;
751 rec = &stat->pages->records[stat->pages->index++];
753 ftrace_add_profile(stat, rec);
756 atomic_dec(&stat->disabled);
762 function_profile_call(unsigned long ip, unsigned long parent_ip,
763 struct ftrace_ops *ops, struct pt_regs *regs)
765 struct ftrace_profile_stat *stat;
766 struct ftrace_profile *rec;
769 if (!ftrace_profile_enabled)
772 local_irq_save(flags);
774 stat = this_cpu_ptr(&ftrace_profile_stats);
775 if (!stat->hash || !ftrace_profile_enabled)
778 rec = ftrace_find_profiled_func(stat, ip);
780 rec = ftrace_profile_alloc(stat, ip);
787 local_irq_restore(flags);
790 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
791 static bool fgraph_graph_time = true;
793 void ftrace_graph_graph_time_control(bool enable)
795 fgraph_graph_time = enable;
798 static int profile_graph_entry(struct ftrace_graph_ent *trace)
800 struct ftrace_ret_stack *ret_stack;
802 function_profile_call(trace->func, 0, NULL, NULL);
804 /* If function graph is shutting down, ret_stack can be NULL */
805 if (!current->ret_stack)
808 ret_stack = ftrace_graph_get_ret_stack(current, 0);
810 ret_stack->subtime = 0;
815 static void profile_graph_return(struct ftrace_graph_ret *trace)
817 struct ftrace_ret_stack *ret_stack;
818 struct ftrace_profile_stat *stat;
819 unsigned long long calltime;
820 struct ftrace_profile *rec;
823 local_irq_save(flags);
824 stat = this_cpu_ptr(&ftrace_profile_stats);
825 if (!stat->hash || !ftrace_profile_enabled)
828 /* If the calltime was zero'd ignore it */
829 if (!trace->calltime)
832 calltime = trace->rettime - trace->calltime;
834 if (!fgraph_graph_time) {
836 /* Append this call time to the parent time to subtract */
837 ret_stack = ftrace_graph_get_ret_stack(current, 1);
839 ret_stack->subtime += calltime;
841 ret_stack = ftrace_graph_get_ret_stack(current, 0);
842 if (ret_stack && ret_stack->subtime < calltime)
843 calltime -= ret_stack->subtime;
848 rec = ftrace_find_profiled_func(stat, trace->func);
850 rec->time += calltime;
851 rec->time_squared += calltime * calltime;
855 local_irq_restore(flags);
858 static struct fgraph_ops fprofiler_ops = {
859 .entryfunc = &profile_graph_entry,
860 .retfunc = &profile_graph_return,
863 static int register_ftrace_profiler(void)
865 return register_ftrace_graph(&fprofiler_ops);
868 static void unregister_ftrace_profiler(void)
870 unregister_ftrace_graph(&fprofiler_ops);
873 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
874 .func = function_profile_call,
875 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
876 INIT_OPS_HASH(ftrace_profile_ops)
879 static int register_ftrace_profiler(void)
881 return register_ftrace_function(&ftrace_profile_ops);
884 static void unregister_ftrace_profiler(void)
886 unregister_ftrace_function(&ftrace_profile_ops);
888 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
891 ftrace_profile_write(struct file *filp, const char __user *ubuf,
892 size_t cnt, loff_t *ppos)
897 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
903 mutex_lock(&ftrace_profile_lock);
904 if (ftrace_profile_enabled ^ val) {
906 ret = ftrace_profile_init();
912 ret = register_ftrace_profiler();
917 ftrace_profile_enabled = 1;
919 ftrace_profile_enabled = 0;
921 * unregister_ftrace_profiler calls stop_machine
922 * so this acts like an synchronize_rcu.
924 unregister_ftrace_profiler();
928 mutex_unlock(&ftrace_profile_lock);
936 ftrace_profile_read(struct file *filp, char __user *ubuf,
937 size_t cnt, loff_t *ppos)
939 char buf[64]; /* big enough to hold a number */
942 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
943 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
946 static const struct file_operations ftrace_profile_fops = {
947 .open = tracing_open_generic,
948 .read = ftrace_profile_read,
949 .write = ftrace_profile_write,
950 .llseek = default_llseek,
953 /* used to initialize the real stat files */
954 static struct tracer_stat function_stats __initdata = {
956 .stat_start = function_stat_start,
957 .stat_next = function_stat_next,
958 .stat_cmp = function_stat_cmp,
959 .stat_headers = function_stat_headers,
960 .stat_show = function_stat_show
963 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
965 struct ftrace_profile_stat *stat;
966 struct dentry *entry;
971 for_each_possible_cpu(cpu) {
972 stat = &per_cpu(ftrace_profile_stats, cpu);
974 name = kasprintf(GFP_KERNEL, "function%d", cpu);
977 * The files created are permanent, if something happens
978 * we still do not free memory.
981 "Could not allocate stat file for cpu %d\n",
985 stat->stat = function_stats;
986 stat->stat.name = name;
987 ret = register_stat_tracer(&stat->stat);
990 "Could not register function stat for cpu %d\n",
997 entry = tracefs_create_file("function_profile_enabled", 0644,
998 d_tracer, NULL, &ftrace_profile_fops);
1000 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1003 #else /* CONFIG_FUNCTION_PROFILER */
1004 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1007 #endif /* CONFIG_FUNCTION_PROFILER */
1009 #ifdef CONFIG_DYNAMIC_FTRACE
1011 static struct ftrace_ops *removed_ops;
1014 * Set when doing a global update, like enabling all recs or disabling them.
1015 * It is not set when just updating a single ftrace_ops.
1017 static bool update_all_ops;
1019 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1020 # error Dynamic ftrace depends on MCOUNT_RECORD
1023 struct ftrace_func_entry {
1024 struct hlist_node hlist;
1028 struct ftrace_func_probe {
1029 struct ftrace_probe_ops *probe_ops;
1030 struct ftrace_ops ops;
1031 struct trace_array *tr;
1032 struct list_head list;
1038 * We make these constant because no one should touch them,
1039 * but they are used as the default "empty hash", to avoid allocating
1040 * it all the time. These are in a read only section such that if
1041 * anyone does try to modify it, it will cause an exception.
1043 static const struct hlist_head empty_buckets[1];
1044 static const struct ftrace_hash empty_hash = {
1045 .buckets = (struct hlist_head *)empty_buckets,
1047 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1049 struct ftrace_ops global_ops = {
1050 .func = ftrace_stub,
1051 .local_hash.notrace_hash = EMPTY_HASH,
1052 .local_hash.filter_hash = EMPTY_HASH,
1053 INIT_OPS_HASH(global_ops)
1054 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1055 FTRACE_OPS_FL_INITIALIZED |
1060 * Used by the stack undwinder to know about dynamic ftrace trampolines.
1062 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1064 struct ftrace_ops *op = NULL;
1067 * Some of the ops may be dynamically allocated,
1068 * they are freed after a synchronize_rcu().
1070 preempt_disable_notrace();
1072 do_for_each_ftrace_op(op, ftrace_ops_list) {
1074 * This is to check for dynamically allocated trampolines.
1075 * Trampolines that are in kernel text will have
1076 * core_kernel_text() return true.
1078 if (op->trampoline && op->trampoline_size)
1079 if (addr >= op->trampoline &&
1080 addr < op->trampoline + op->trampoline_size) {
1081 preempt_enable_notrace();
1084 } while_for_each_ftrace_op(op);
1085 preempt_enable_notrace();
1091 * This is used by __kernel_text_address() to return true if the
1092 * address is on a dynamically allocated trampoline that would
1093 * not return true for either core_kernel_text() or
1094 * is_module_text_address().
1096 bool is_ftrace_trampoline(unsigned long addr)
1098 return ftrace_ops_trampoline(addr) != NULL;
1101 struct ftrace_page {
1102 struct ftrace_page *next;
1103 struct dyn_ftrace *records;
1108 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1109 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1111 /* estimate from running different kernels */
1112 #define NR_TO_INIT 10000
1114 static struct ftrace_page *ftrace_pages_start;
1115 static struct ftrace_page *ftrace_pages;
1117 static __always_inline unsigned long
1118 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1120 if (hash->size_bits > 0)
1121 return hash_long(ip, hash->size_bits);
1126 /* Only use this function if ftrace_hash_empty() has already been tested */
1127 static __always_inline struct ftrace_func_entry *
1128 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1131 struct ftrace_func_entry *entry;
1132 struct hlist_head *hhd;
1134 key = ftrace_hash_key(hash, ip);
1135 hhd = &hash->buckets[key];
1137 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1138 if (entry->ip == ip)
1145 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1146 * @hash: The hash to look at
1147 * @ip: The instruction pointer to test
1149 * Search a given @hash to see if a given instruction pointer (@ip)
1152 * Returns the entry that holds the @ip if found. NULL otherwise.
1154 struct ftrace_func_entry *
1155 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1157 if (ftrace_hash_empty(hash))
1160 return __ftrace_lookup_ip(hash, ip);
1163 static void __add_hash_entry(struct ftrace_hash *hash,
1164 struct ftrace_func_entry *entry)
1166 struct hlist_head *hhd;
1169 key = ftrace_hash_key(hash, entry->ip);
1170 hhd = &hash->buckets[key];
1171 hlist_add_head(&entry->hlist, hhd);
1175 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1177 struct ftrace_func_entry *entry;
1179 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1184 __add_hash_entry(hash, entry);
1190 free_hash_entry(struct ftrace_hash *hash,
1191 struct ftrace_func_entry *entry)
1193 hlist_del(&entry->hlist);
1199 remove_hash_entry(struct ftrace_hash *hash,
1200 struct ftrace_func_entry *entry)
1202 hlist_del_rcu(&entry->hlist);
1206 static void ftrace_hash_clear(struct ftrace_hash *hash)
1208 struct hlist_head *hhd;
1209 struct hlist_node *tn;
1210 struct ftrace_func_entry *entry;
1211 int size = 1 << hash->size_bits;
1217 for (i = 0; i < size; i++) {
1218 hhd = &hash->buckets[i];
1219 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1220 free_hash_entry(hash, entry);
1222 FTRACE_WARN_ON(hash->count);
1225 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1227 list_del(&ftrace_mod->list);
1228 kfree(ftrace_mod->module);
1229 kfree(ftrace_mod->func);
1233 static void clear_ftrace_mod_list(struct list_head *head)
1235 struct ftrace_mod_load *p, *n;
1237 /* stack tracer isn't supported yet */
1241 mutex_lock(&ftrace_lock);
1242 list_for_each_entry_safe(p, n, head, list)
1244 mutex_unlock(&ftrace_lock);
1247 static void free_ftrace_hash(struct ftrace_hash *hash)
1249 if (!hash || hash == EMPTY_HASH)
1251 ftrace_hash_clear(hash);
1252 kfree(hash->buckets);
1256 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1258 struct ftrace_hash *hash;
1260 hash = container_of(rcu, struct ftrace_hash, rcu);
1261 free_ftrace_hash(hash);
1264 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1266 if (!hash || hash == EMPTY_HASH)
1268 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
1271 void ftrace_free_filter(struct ftrace_ops *ops)
1273 ftrace_ops_init(ops);
1274 free_ftrace_hash(ops->func_hash->filter_hash);
1275 free_ftrace_hash(ops->func_hash->notrace_hash);
1278 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1280 struct ftrace_hash *hash;
1283 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1287 size = 1 << size_bits;
1288 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1290 if (!hash->buckets) {
1295 hash->size_bits = size_bits;
1301 static int ftrace_add_mod(struct trace_array *tr,
1302 const char *func, const char *module,
1305 struct ftrace_mod_load *ftrace_mod;
1306 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1308 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1312 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1313 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1314 ftrace_mod->enable = enable;
1316 if (!ftrace_mod->func || !ftrace_mod->module)
1319 list_add(&ftrace_mod->list, mod_head);
1324 free_ftrace_mod(ftrace_mod);
1329 static struct ftrace_hash *
1330 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1332 struct ftrace_func_entry *entry;
1333 struct ftrace_hash *new_hash;
1338 new_hash = alloc_ftrace_hash(size_bits);
1343 new_hash->flags = hash->flags;
1346 if (ftrace_hash_empty(hash))
1349 size = 1 << hash->size_bits;
1350 for (i = 0; i < size; i++) {
1351 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1352 ret = add_hash_entry(new_hash, entry->ip);
1358 FTRACE_WARN_ON(new_hash->count != hash->count);
1363 free_ftrace_hash(new_hash);
1368 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1370 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1372 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1373 struct ftrace_hash *new_hash);
1375 static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
1377 struct ftrace_func_entry *entry;
1378 struct ftrace_hash *new_hash;
1379 struct hlist_head *hhd;
1380 struct hlist_node *tn;
1385 * Make the hash size about 1/2 the # found
1387 for (size /= 2; size; size >>= 1)
1390 /* Don't allocate too much */
1391 if (bits > FTRACE_HASH_MAX_BITS)
1392 bits = FTRACE_HASH_MAX_BITS;
1394 new_hash = alloc_ftrace_hash(bits);
1398 new_hash->flags = src->flags;
1400 size = 1 << src->size_bits;
1401 for (i = 0; i < size; i++) {
1402 hhd = &src->buckets[i];
1403 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1404 remove_hash_entry(src, entry);
1405 __add_hash_entry(new_hash, entry);
1411 static struct ftrace_hash *
1412 __ftrace_hash_move(struct ftrace_hash *src)
1414 int size = src->count;
1417 * If the new source is empty, just return the empty_hash.
1419 if (ftrace_hash_empty(src))
1422 return dup_hash(src, size);
1426 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1427 struct ftrace_hash **dst, struct ftrace_hash *src)
1429 struct ftrace_hash *new_hash;
1432 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1433 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1436 new_hash = __ftrace_hash_move(src);
1440 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1442 /* IPMODIFY should be updated only when filter_hash updating */
1443 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1445 free_ftrace_hash(new_hash);
1451 * Remove the current set, update the hash and add
1454 ftrace_hash_rec_disable_modify(ops, enable);
1456 rcu_assign_pointer(*dst, new_hash);
1458 ftrace_hash_rec_enable_modify(ops, enable);
1463 static bool hash_contains_ip(unsigned long ip,
1464 struct ftrace_ops_hash *hash)
1467 * The function record is a match if it exists in the filter
1468 * hash and not in the notrace hash. Note, an emty hash is
1469 * considered a match for the filter hash, but an empty
1470 * notrace hash is considered not in the notrace hash.
1472 return (ftrace_hash_empty(hash->filter_hash) ||
1473 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1474 (ftrace_hash_empty(hash->notrace_hash) ||
1475 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1479 * Test the hashes for this ops to see if we want to call
1480 * the ops->func or not.
1482 * It's a match if the ip is in the ops->filter_hash or
1483 * the filter_hash does not exist or is empty,
1485 * the ip is not in the ops->notrace_hash.
1487 * This needs to be called with preemption disabled as
1488 * the hashes are freed with call_rcu().
1491 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1493 struct ftrace_ops_hash hash;
1496 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1498 * There's a small race when adding ops that the ftrace handler
1499 * that wants regs, may be called without them. We can not
1500 * allow that handler to be called if regs is NULL.
1502 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1506 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1507 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1509 if (hash_contains_ip(ip, &hash))
1518 * This is a double for. Do not use 'break' to break out of the loop,
1519 * you must use a goto.
1521 #define do_for_each_ftrace_rec(pg, rec) \
1522 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1524 for (_____i = 0; _____i < pg->index; _____i++) { \
1525 rec = &pg->records[_____i];
1527 #define while_for_each_ftrace_rec() \
1532 static int ftrace_cmp_recs(const void *a, const void *b)
1534 const struct dyn_ftrace *key = a;
1535 const struct dyn_ftrace *rec = b;
1537 if (key->flags < rec->ip)
1539 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1545 * ftrace_location_range - return the first address of a traced location
1546 * if it touches the given ip range
1547 * @start: start of range to search.
1548 * @end: end of range to search (inclusive). @end points to the last byte
1551 * Returns rec->ip if the related ftrace location is a least partly within
1552 * the given address range. That is, the first address of the instruction
1553 * that is either a NOP or call to the function tracer. It checks the ftrace
1554 * internal tables to determine if the address belongs or not.
1556 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1558 struct ftrace_page *pg;
1559 struct dyn_ftrace *rec;
1560 struct dyn_ftrace key;
1563 key.flags = end; /* overload flags, as it is unsigned long */
1565 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1566 if (end < pg->records[0].ip ||
1567 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1569 rec = bsearch(&key, pg->records, pg->index,
1570 sizeof(struct dyn_ftrace),
1580 * ftrace_location - return true if the ip giving is a traced location
1581 * @ip: the instruction pointer to check
1583 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1584 * That is, the instruction that is either a NOP or call to
1585 * the function tracer. It checks the ftrace internal tables to
1586 * determine if the address belongs or not.
1588 unsigned long ftrace_location(unsigned long ip)
1590 return ftrace_location_range(ip, ip);
1594 * ftrace_text_reserved - return true if range contains an ftrace location
1595 * @start: start of range to search
1596 * @end: end of range to search (inclusive). @end points to the last byte to check.
1598 * Returns 1 if @start and @end contains a ftrace location.
1599 * That is, the instruction that is either a NOP or call to
1600 * the function tracer. It checks the ftrace internal tables to
1601 * determine if the address belongs or not.
1603 int ftrace_text_reserved(const void *start, const void *end)
1607 ret = ftrace_location_range((unsigned long)start,
1608 (unsigned long)end);
1613 /* Test if ops registered to this rec needs regs */
1614 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1616 struct ftrace_ops *ops;
1617 bool keep_regs = false;
1619 for (ops = ftrace_ops_list;
1620 ops != &ftrace_list_end; ops = ops->next) {
1621 /* pass rec in as regs to have non-NULL val */
1622 if (ftrace_ops_test(ops, rec->ip, rec)) {
1623 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1633 static struct ftrace_ops *
1634 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1635 static struct ftrace_ops *
1636 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1638 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1642 struct ftrace_hash *hash;
1643 struct ftrace_hash *other_hash;
1644 struct ftrace_page *pg;
1645 struct dyn_ftrace *rec;
1646 bool update = false;
1650 /* Only update if the ops has been registered */
1651 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1655 * In the filter_hash case:
1656 * If the count is zero, we update all records.
1657 * Otherwise we just update the items in the hash.
1659 * In the notrace_hash case:
1660 * We enable the update in the hash.
1661 * As disabling notrace means enabling the tracing,
1662 * and enabling notrace means disabling, the inc variable
1666 hash = ops->func_hash->filter_hash;
1667 other_hash = ops->func_hash->notrace_hash;
1668 if (ftrace_hash_empty(hash))
1672 hash = ops->func_hash->notrace_hash;
1673 other_hash = ops->func_hash->filter_hash;
1675 * If the notrace hash has no items,
1676 * then there's nothing to do.
1678 if (ftrace_hash_empty(hash))
1682 do_for_each_ftrace_rec(pg, rec) {
1683 int in_other_hash = 0;
1687 if (rec->flags & FTRACE_FL_DISABLED)
1692 * Only the filter_hash affects all records.
1693 * Update if the record is not in the notrace hash.
1695 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1698 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1699 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1702 * If filter_hash is set, we want to match all functions
1703 * that are in the hash but not in the other hash.
1705 * If filter_hash is not set, then we are decrementing.
1706 * That means we match anything that is in the hash
1707 * and also in the other_hash. That is, we need to turn
1708 * off functions in the other hash because they are disabled
1711 if (filter_hash && in_hash && !in_other_hash)
1713 else if (!filter_hash && in_hash &&
1714 (in_other_hash || ftrace_hash_empty(other_hash)))
1722 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1726 * If there's only a single callback registered to a
1727 * function, and the ops has a trampoline registered
1728 * for it, then we can call it directly.
1730 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1731 rec->flags |= FTRACE_FL_TRAMP;
1734 * If we are adding another function callback
1735 * to this function, and the previous had a
1736 * custom trampoline in use, then we need to go
1737 * back to the default trampoline.
1739 rec->flags &= ~FTRACE_FL_TRAMP;
1742 * If any ops wants regs saved for this function
1743 * then all ops will get saved regs.
1745 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1746 rec->flags |= FTRACE_FL_REGS;
1748 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1753 * If the rec had REGS enabled and the ops that is
1754 * being removed had REGS set, then see if there is
1755 * still any ops for this record that wants regs.
1756 * If not, we can stop recording them.
1758 if (ftrace_rec_count(rec) > 0 &&
1759 rec->flags & FTRACE_FL_REGS &&
1760 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1761 if (!test_rec_ops_needs_regs(rec))
1762 rec->flags &= ~FTRACE_FL_REGS;
1766 * The TRAMP needs to be set only if rec count
1767 * is decremented to one, and the ops that is
1768 * left has a trampoline. As TRAMP can only be
1769 * enabled if there is only a single ops attached
1772 if (ftrace_rec_count(rec) == 1 &&
1773 ftrace_find_tramp_ops_any(rec))
1774 rec->flags |= FTRACE_FL_TRAMP;
1776 rec->flags &= ~FTRACE_FL_TRAMP;
1779 * flags will be cleared in ftrace_check_record()
1780 * if rec count is zero.
1785 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1786 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
1788 /* Shortcut, if we handled all records, we are done. */
1789 if (!all && count == hash->count)
1791 } while_for_each_ftrace_rec();
1796 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1799 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1802 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1805 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1808 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1809 int filter_hash, int inc)
1811 struct ftrace_ops *op;
1813 __ftrace_hash_rec_update(ops, filter_hash, inc);
1815 if (ops->func_hash != &global_ops.local_hash)
1819 * If the ops shares the global_ops hash, then we need to update
1820 * all ops that are enabled and use this hash.
1822 do_for_each_ftrace_op(op, ftrace_ops_list) {
1826 if (op->func_hash == &global_ops.local_hash)
1827 __ftrace_hash_rec_update(op, filter_hash, inc);
1828 } while_for_each_ftrace_op(op);
1831 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1834 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1837 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1840 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1844 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1845 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1846 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1847 * Note that old_hash and new_hash has below meanings
1848 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1849 * - If the hash is EMPTY_HASH, it hits nothing
1850 * - Anything else hits the recs which match the hash entries.
1852 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1853 struct ftrace_hash *old_hash,
1854 struct ftrace_hash *new_hash)
1856 struct ftrace_page *pg;
1857 struct dyn_ftrace *rec, *end = NULL;
1860 /* Only update if the ops has been registered */
1861 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1864 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1868 * Since the IPMODIFY is a very address sensitive action, we do not
1869 * allow ftrace_ops to set all functions to new hash.
1871 if (!new_hash || !old_hash)
1874 /* Update rec->flags */
1875 do_for_each_ftrace_rec(pg, rec) {
1877 if (rec->flags & FTRACE_FL_DISABLED)
1880 /* We need to update only differences of filter_hash */
1881 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1882 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1883 if (in_old == in_new)
1887 /* New entries must ensure no others are using it */
1888 if (rec->flags & FTRACE_FL_IPMODIFY)
1890 rec->flags |= FTRACE_FL_IPMODIFY;
1891 } else /* Removed entry */
1892 rec->flags &= ~FTRACE_FL_IPMODIFY;
1893 } while_for_each_ftrace_rec();
1900 /* Roll back what we did above */
1901 do_for_each_ftrace_rec(pg, rec) {
1903 if (rec->flags & FTRACE_FL_DISABLED)
1909 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1910 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1911 if (in_old == in_new)
1915 rec->flags &= ~FTRACE_FL_IPMODIFY;
1917 rec->flags |= FTRACE_FL_IPMODIFY;
1918 } while_for_each_ftrace_rec();
1924 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1926 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1928 if (ftrace_hash_empty(hash))
1931 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1934 /* Disabling always succeeds */
1935 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1937 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1939 if (ftrace_hash_empty(hash))
1942 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1945 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1946 struct ftrace_hash *new_hash)
1948 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1950 if (ftrace_hash_empty(old_hash))
1953 if (ftrace_hash_empty(new_hash))
1956 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1959 static void print_ip_ins(const char *fmt, const unsigned char *p)
1963 printk(KERN_CONT "%s", fmt);
1965 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1966 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1969 enum ftrace_bug_type ftrace_bug_type;
1970 const void *ftrace_expected;
1972 static void print_bug_type(void)
1974 switch (ftrace_bug_type) {
1975 case FTRACE_BUG_UNKNOWN:
1977 case FTRACE_BUG_INIT:
1978 pr_info("Initializing ftrace call sites\n");
1980 case FTRACE_BUG_NOP:
1981 pr_info("Setting ftrace call site to NOP\n");
1983 case FTRACE_BUG_CALL:
1984 pr_info("Setting ftrace call site to call ftrace function\n");
1986 case FTRACE_BUG_UPDATE:
1987 pr_info("Updating ftrace call site to call a different ftrace function\n");
1993 * ftrace_bug - report and shutdown function tracer
1994 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1995 * @rec: The record that failed
1997 * The arch code that enables or disables the function tracing
1998 * can call ftrace_bug() when it has detected a problem in
1999 * modifying the code. @failed should be one of either:
2000 * EFAULT - if the problem happens on reading the @ip address
2001 * EINVAL - if what is read at @ip is not what was expected
2002 * EPERM - if the problem happens on writing to the @ip address
2004 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2006 unsigned long ip = rec ? rec->ip : 0;
2010 FTRACE_WARN_ON_ONCE(1);
2011 pr_info("ftrace faulted on modifying ");
2015 FTRACE_WARN_ON_ONCE(1);
2016 pr_info("ftrace failed to modify ");
2018 print_ip_ins(" actual: ", (unsigned char *)ip);
2020 if (ftrace_expected) {
2021 print_ip_ins(" expected: ", ftrace_expected);
2026 FTRACE_WARN_ON_ONCE(1);
2027 pr_info("ftrace faulted on writing ");
2031 FTRACE_WARN_ON_ONCE(1);
2032 pr_info("ftrace faulted on unknown error ");
2037 struct ftrace_ops *ops = NULL;
2039 pr_info("ftrace record flags: %lx\n", rec->flags);
2040 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2041 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2042 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2043 ops = ftrace_find_tramp_ops_any(rec);
2046 pr_cont("\ttramp: %pS (%pS)",
2047 (void *)ops->trampoline,
2049 ops = ftrace_find_tramp_ops_next(rec, ops);
2052 pr_cont("\ttramp: ERROR!");
2055 ip = ftrace_get_addr_curr(rec);
2056 pr_cont("\n expected tramp: %lx\n", ip);
2060 static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
2062 unsigned long flag = 0UL;
2064 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2066 if (rec->flags & FTRACE_FL_DISABLED)
2067 return FTRACE_UPDATE_IGNORE;
2070 * If we are updating calls:
2072 * If the record has a ref count, then we need to enable it
2073 * because someone is using it.
2075 * Otherwise we make sure its disabled.
2077 * If we are disabling calls, then disable all records that
2080 if (enable && ftrace_rec_count(rec))
2081 flag = FTRACE_FL_ENABLED;
2084 * If enabling and the REGS flag does not match the REGS_EN, or
2085 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2086 * this record. Set flags to fail the compare against ENABLED.
2089 if (!(rec->flags & FTRACE_FL_REGS) !=
2090 !(rec->flags & FTRACE_FL_REGS_EN))
2091 flag |= FTRACE_FL_REGS;
2093 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2094 !(rec->flags & FTRACE_FL_TRAMP_EN))
2095 flag |= FTRACE_FL_TRAMP;
2098 /* If the state of this record hasn't changed, then do nothing */
2099 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2100 return FTRACE_UPDATE_IGNORE;
2103 /* Save off if rec is being enabled (for return value) */
2104 flag ^= rec->flags & FTRACE_FL_ENABLED;
2107 rec->flags |= FTRACE_FL_ENABLED;
2108 if (flag & FTRACE_FL_REGS) {
2109 if (rec->flags & FTRACE_FL_REGS)
2110 rec->flags |= FTRACE_FL_REGS_EN;
2112 rec->flags &= ~FTRACE_FL_REGS_EN;
2114 if (flag & FTRACE_FL_TRAMP) {
2115 if (rec->flags & FTRACE_FL_TRAMP)
2116 rec->flags |= FTRACE_FL_TRAMP_EN;
2118 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2123 * If this record is being updated from a nop, then
2124 * return UPDATE_MAKE_CALL.
2126 * return UPDATE_MODIFY_CALL to tell the caller to convert
2127 * from the save regs, to a non-save regs function or
2128 * vice versa, or from a trampoline call.
2130 if (flag & FTRACE_FL_ENABLED) {
2131 ftrace_bug_type = FTRACE_BUG_CALL;
2132 return FTRACE_UPDATE_MAKE_CALL;
2135 ftrace_bug_type = FTRACE_BUG_UPDATE;
2136 return FTRACE_UPDATE_MODIFY_CALL;
2140 /* If there's no more users, clear all flags */
2141 if (!ftrace_rec_count(rec))
2145 * Just disable the record, but keep the ops TRAMP
2146 * and REGS states. The _EN flags must be disabled though.
2148 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2152 ftrace_bug_type = FTRACE_BUG_NOP;
2153 return FTRACE_UPDATE_MAKE_NOP;
2157 * ftrace_update_record, set a record that now is tracing or not
2158 * @rec: the record to update
2159 * @enable: set to true if the record is tracing, false to force disable
2161 * The records that represent all functions that can be traced need
2162 * to be updated when tracing has been enabled.
2164 int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
2166 return ftrace_check_record(rec, enable, true);
2170 * ftrace_test_record, check if the record has been enabled or not
2171 * @rec: the record to test
2172 * @enable: set to true to check if enabled, false if it is disabled
2174 * The arch code may need to test if a record is already set to
2175 * tracing to determine how to modify the function code that it
2178 int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
2180 return ftrace_check_record(rec, enable, false);
2183 static struct ftrace_ops *
2184 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2186 struct ftrace_ops *op;
2187 unsigned long ip = rec->ip;
2189 do_for_each_ftrace_op(op, ftrace_ops_list) {
2191 if (!op->trampoline)
2194 if (hash_contains_ip(ip, op->func_hash))
2196 } while_for_each_ftrace_op(op);
2201 static struct ftrace_ops *
2202 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2203 struct ftrace_ops *op)
2205 unsigned long ip = rec->ip;
2207 while_for_each_ftrace_op(op) {
2209 if (!op->trampoline)
2212 if (hash_contains_ip(ip, op->func_hash))
2219 static struct ftrace_ops *
2220 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2222 struct ftrace_ops *op;
2223 unsigned long ip = rec->ip;
2226 * Need to check removed ops first.
2227 * If they are being removed, and this rec has a tramp,
2228 * and this rec is in the ops list, then it would be the
2229 * one with the tramp.
2232 if (hash_contains_ip(ip, &removed_ops->old_hash))
2237 * Need to find the current trampoline for a rec.
2238 * Now, a trampoline is only attached to a rec if there
2239 * was a single 'ops' attached to it. But this can be called
2240 * when we are adding another op to the rec or removing the
2241 * current one. Thus, if the op is being added, we can
2242 * ignore it because it hasn't attached itself to the rec
2245 * If an ops is being modified (hooking to different functions)
2246 * then we don't care about the new functions that are being
2247 * added, just the old ones (that are probably being removed).
2249 * If we are adding an ops to a function that already is using
2250 * a trampoline, it needs to be removed (trampolines are only
2251 * for single ops connected), then an ops that is not being
2252 * modified also needs to be checked.
2254 do_for_each_ftrace_op(op, ftrace_ops_list) {
2256 if (!op->trampoline)
2260 * If the ops is being added, it hasn't gotten to
2261 * the point to be removed from this tree yet.
2263 if (op->flags & FTRACE_OPS_FL_ADDING)
2268 * If the ops is being modified and is in the old
2269 * hash, then it is probably being removed from this
2272 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2273 hash_contains_ip(ip, &op->old_hash))
2276 * If the ops is not being added or modified, and it's
2277 * in its normal filter hash, then this must be the one
2280 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2281 hash_contains_ip(ip, op->func_hash))
2284 } while_for_each_ftrace_op(op);
2289 static struct ftrace_ops *
2290 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2292 struct ftrace_ops *op;
2293 unsigned long ip = rec->ip;
2295 do_for_each_ftrace_op(op, ftrace_ops_list) {
2296 /* pass rec in as regs to have non-NULL val */
2297 if (hash_contains_ip(ip, op->func_hash))
2299 } while_for_each_ftrace_op(op);
2305 * ftrace_get_addr_new - Get the call address to set to
2306 * @rec: The ftrace record descriptor
2308 * If the record has the FTRACE_FL_REGS set, that means that it
2309 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2310 * is not not set, then it wants to convert to the normal callback.
2312 * Returns the address of the trampoline to set to
2314 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2316 struct ftrace_ops *ops;
2318 /* Trampolines take precedence over regs */
2319 if (rec->flags & FTRACE_FL_TRAMP) {
2320 ops = ftrace_find_tramp_ops_new(rec);
2321 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2322 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2323 (void *)rec->ip, (void *)rec->ip, rec->flags);
2324 /* Ftrace is shutting down, return anything */
2325 return (unsigned long)FTRACE_ADDR;
2327 return ops->trampoline;
2330 if (rec->flags & FTRACE_FL_REGS)
2331 return (unsigned long)FTRACE_REGS_ADDR;
2333 return (unsigned long)FTRACE_ADDR;
2337 * ftrace_get_addr_curr - Get the call address that is already there
2338 * @rec: The ftrace record descriptor
2340 * The FTRACE_FL_REGS_EN is set when the record already points to
2341 * a function that saves all the regs. Basically the '_EN' version
2342 * represents the current state of the function.
2344 * Returns the address of the trampoline that is currently being called
2346 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2348 struct ftrace_ops *ops;
2350 /* Trampolines take precedence over regs */
2351 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2352 ops = ftrace_find_tramp_ops_curr(rec);
2353 if (FTRACE_WARN_ON(!ops)) {
2354 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2355 (void *)rec->ip, (void *)rec->ip);
2356 /* Ftrace is shutting down, return anything */
2357 return (unsigned long)FTRACE_ADDR;
2359 return ops->trampoline;
2362 if (rec->flags & FTRACE_FL_REGS_EN)
2363 return (unsigned long)FTRACE_REGS_ADDR;
2365 return (unsigned long)FTRACE_ADDR;
2369 __ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
2371 unsigned long ftrace_old_addr;
2372 unsigned long ftrace_addr;
2375 ftrace_addr = ftrace_get_addr_new(rec);
2377 /* This needs to be done before we call ftrace_update_record */
2378 ftrace_old_addr = ftrace_get_addr_curr(rec);
2380 ret = ftrace_update_record(rec, enable);
2382 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2385 case FTRACE_UPDATE_IGNORE:
2388 case FTRACE_UPDATE_MAKE_CALL:
2389 ftrace_bug_type = FTRACE_BUG_CALL;
2390 return ftrace_make_call(rec, ftrace_addr);
2392 case FTRACE_UPDATE_MAKE_NOP:
2393 ftrace_bug_type = FTRACE_BUG_NOP;
2394 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2396 case FTRACE_UPDATE_MODIFY_CALL:
2397 ftrace_bug_type = FTRACE_BUG_UPDATE;
2398 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2401 return -1; /* unknown ftrace bug */
2404 void __weak ftrace_replace_code(int mod_flags)
2406 struct dyn_ftrace *rec;
2407 struct ftrace_page *pg;
2408 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2409 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
2412 if (unlikely(ftrace_disabled))
2415 do_for_each_ftrace_rec(pg, rec) {
2417 if (rec->flags & FTRACE_FL_DISABLED)
2420 failed = __ftrace_replace_code(rec, enable);
2422 ftrace_bug(failed, rec);
2423 /* Stop processing */
2428 } while_for_each_ftrace_rec();
2431 struct ftrace_rec_iter {
2432 struct ftrace_page *pg;
2437 * ftrace_rec_iter_start, start up iterating over traced functions
2439 * Returns an iterator handle that is used to iterate over all
2440 * the records that represent address locations where functions
2443 * May return NULL if no records are available.
2445 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2448 * We only use a single iterator.
2449 * Protected by the ftrace_lock mutex.
2451 static struct ftrace_rec_iter ftrace_rec_iter;
2452 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2454 iter->pg = ftrace_pages_start;
2457 /* Could have empty pages */
2458 while (iter->pg && !iter->pg->index)
2459 iter->pg = iter->pg->next;
2468 * ftrace_rec_iter_next, get the next record to process.
2469 * @iter: The handle to the iterator.
2471 * Returns the next iterator after the given iterator @iter.
2473 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2477 if (iter->index >= iter->pg->index) {
2478 iter->pg = iter->pg->next;
2481 /* Could have empty pages */
2482 while (iter->pg && !iter->pg->index)
2483 iter->pg = iter->pg->next;
2493 * ftrace_rec_iter_record, get the record at the iterator location
2494 * @iter: The current iterator location
2496 * Returns the record that the current @iter is at.
2498 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2500 return &iter->pg->records[iter->index];
2504 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2508 if (unlikely(ftrace_disabled))
2511 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2513 ftrace_bug_type = FTRACE_BUG_INIT;
2514 ftrace_bug(ret, rec);
2521 * archs can override this function if they must do something
2522 * before the modifying code is performed.
2524 int __weak ftrace_arch_code_modify_prepare(void)
2530 * archs can override this function if they must do something
2531 * after the modifying code is performed.
2533 int __weak ftrace_arch_code_modify_post_process(void)
2538 void ftrace_modify_all_code(int command)
2540 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2544 if (command & FTRACE_MAY_SLEEP)
2545 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2548 * If the ftrace_caller calls a ftrace_ops func directly,
2549 * we need to make sure that it only traces functions it
2550 * expects to trace. When doing the switch of functions,
2551 * we need to update to the ftrace_ops_list_func first
2552 * before the transition between old and new calls are set,
2553 * as the ftrace_ops_list_func will check the ops hashes
2554 * to make sure the ops are having the right functions
2558 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2559 if (FTRACE_WARN_ON(err))
2563 if (command & FTRACE_UPDATE_CALLS)
2564 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
2565 else if (command & FTRACE_DISABLE_CALLS)
2566 ftrace_replace_code(mod_flags);
2568 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2569 function_trace_op = set_function_trace_op;
2571 /* If irqs are disabled, we are in stop machine */
2572 if (!irqs_disabled())
2573 smp_call_function(ftrace_sync_ipi, NULL, 1);
2574 err = ftrace_update_ftrace_func(ftrace_trace_function);
2575 if (FTRACE_WARN_ON(err))
2579 if (command & FTRACE_START_FUNC_RET)
2580 err = ftrace_enable_ftrace_graph_caller();
2581 else if (command & FTRACE_STOP_FUNC_RET)
2582 err = ftrace_disable_ftrace_graph_caller();
2583 FTRACE_WARN_ON(err);
2586 static int __ftrace_modify_code(void *data)
2588 int *command = data;
2590 ftrace_modify_all_code(*command);
2596 * ftrace_run_stop_machine, go back to the stop machine method
2597 * @command: The command to tell ftrace what to do
2599 * If an arch needs to fall back to the stop machine method, the
2600 * it can call this function.
2602 void ftrace_run_stop_machine(int command)
2604 stop_machine(__ftrace_modify_code, &command, NULL);
2608 * arch_ftrace_update_code, modify the code to trace or not trace
2609 * @command: The command that needs to be done
2611 * Archs can override this function if it does not need to
2612 * run stop_machine() to modify code.
2614 void __weak arch_ftrace_update_code(int command)
2616 ftrace_run_stop_machine(command);
2619 static void ftrace_run_update_code(int command)
2623 ret = ftrace_arch_code_modify_prepare();
2624 FTRACE_WARN_ON(ret);
2629 * By default we use stop_machine() to modify the code.
2630 * But archs can do what ever they want as long as it
2631 * is safe. The stop_machine() is the safest, but also
2632 * produces the most overhead.
2634 arch_ftrace_update_code(command);
2636 ret = ftrace_arch_code_modify_post_process();
2637 FTRACE_WARN_ON(ret);
2640 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2641 struct ftrace_ops_hash *old_hash)
2643 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2644 ops->old_hash.filter_hash = old_hash->filter_hash;
2645 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2646 ftrace_run_update_code(command);
2647 ops->old_hash.filter_hash = NULL;
2648 ops->old_hash.notrace_hash = NULL;
2649 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2652 static ftrace_func_t saved_ftrace_func;
2653 static int ftrace_start_up;
2655 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2659 static void ftrace_startup_enable(int command)
2661 if (saved_ftrace_func != ftrace_trace_function) {
2662 saved_ftrace_func = ftrace_trace_function;
2663 command |= FTRACE_UPDATE_TRACE_FUNC;
2666 if (!command || !ftrace_enabled)
2669 ftrace_run_update_code(command);
2672 static void ftrace_startup_all(int command)
2674 update_all_ops = true;
2675 ftrace_startup_enable(command);
2676 update_all_ops = false;
2679 int ftrace_startup(struct ftrace_ops *ops, int command)
2683 if (unlikely(ftrace_disabled))
2686 ret = __register_ftrace_function(ops);
2693 * Note that ftrace probes uses this to start up
2694 * and modify functions it will probe. But we still
2695 * set the ADDING flag for modification, as probes
2696 * do not have trampolines. If they add them in the
2697 * future, then the probes will need to distinguish
2698 * between adding and updating probes.
2700 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2702 ret = ftrace_hash_ipmodify_enable(ops);
2704 /* Rollback registration process */
2705 __unregister_ftrace_function(ops);
2707 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2711 if (ftrace_hash_rec_enable(ops, 1))
2712 command |= FTRACE_UPDATE_CALLS;
2714 ftrace_startup_enable(command);
2716 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2721 int ftrace_shutdown(struct ftrace_ops *ops, int command)
2725 if (unlikely(ftrace_disabled))
2728 ret = __unregister_ftrace_function(ops);
2734 * Just warn in case of unbalance, no need to kill ftrace, it's not
2735 * critical but the ftrace_call callers may be never nopped again after
2736 * further ftrace uses.
2738 WARN_ON_ONCE(ftrace_start_up < 0);
2740 /* Disabling ipmodify never fails */
2741 ftrace_hash_ipmodify_disable(ops);
2743 if (ftrace_hash_rec_disable(ops, 1))
2744 command |= FTRACE_UPDATE_CALLS;
2746 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2748 if (saved_ftrace_func != ftrace_trace_function) {
2749 saved_ftrace_func = ftrace_trace_function;
2750 command |= FTRACE_UPDATE_TRACE_FUNC;
2753 if (!command || !ftrace_enabled) {
2755 * If these are dynamic or per_cpu ops, they still
2756 * need their data freed. Since, function tracing is
2757 * not currently active, we can just free them
2758 * without synchronizing all CPUs.
2760 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2767 * If the ops uses a trampoline, then it needs to be
2768 * tested first on update.
2770 ops->flags |= FTRACE_OPS_FL_REMOVING;
2773 /* The trampoline logic checks the old hashes */
2774 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2775 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2777 ftrace_run_update_code(command);
2780 * If there's no more ops registered with ftrace, run a
2781 * sanity check to make sure all rec flags are cleared.
2783 if (rcu_dereference_protected(ftrace_ops_list,
2784 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2785 struct ftrace_page *pg;
2786 struct dyn_ftrace *rec;
2788 do_for_each_ftrace_rec(pg, rec) {
2789 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2790 pr_warn(" %pS flags:%lx\n",
2791 (void *)rec->ip, rec->flags);
2792 } while_for_each_ftrace_rec();
2795 ops->old_hash.filter_hash = NULL;
2796 ops->old_hash.notrace_hash = NULL;
2799 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2802 * Dynamic ops may be freed, we must make sure that all
2803 * callers are done before leaving this function.
2804 * The same goes for freeing the per_cpu data of the per_cpu
2807 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
2809 * We need to do a hard force of sched synchronization.
2810 * This is because we use preempt_disable() to do RCU, but
2811 * the function tracers can be called where RCU is not watching
2812 * (like before user_exit()). We can not rely on the RCU
2813 * infrastructure to do the synchronization, thus we must do it
2816 schedule_on_each_cpu(ftrace_sync);
2819 * When the kernel is preeptive, tasks can be preempted
2820 * while on a ftrace trampoline. Just scheduling a task on
2821 * a CPU is not good enough to flush them. Calling
2822 * synchornize_rcu_tasks() will wait for those tasks to
2823 * execute and either schedule voluntarily or enter user space.
2825 if (IS_ENABLED(CONFIG_PREEMPTION))
2826 synchronize_rcu_tasks();
2829 arch_ftrace_trampoline_free(ops);
2835 static void ftrace_startup_sysctl(void)
2839 if (unlikely(ftrace_disabled))
2842 /* Force update next time */
2843 saved_ftrace_func = NULL;
2844 /* ftrace_start_up is true if we want ftrace running */
2845 if (ftrace_start_up) {
2846 command = FTRACE_UPDATE_CALLS;
2847 if (ftrace_graph_active)
2848 command |= FTRACE_START_FUNC_RET;
2849 ftrace_startup_enable(command);
2853 static void ftrace_shutdown_sysctl(void)
2857 if (unlikely(ftrace_disabled))
2860 /* ftrace_start_up is true if ftrace is running */
2861 if (ftrace_start_up) {
2862 command = FTRACE_DISABLE_CALLS;
2863 if (ftrace_graph_active)
2864 command |= FTRACE_STOP_FUNC_RET;
2865 ftrace_run_update_code(command);
2869 static u64 ftrace_update_time;
2870 unsigned long ftrace_update_tot_cnt;
2872 static inline int ops_traces_mod(struct ftrace_ops *ops)
2875 * Filter_hash being empty will default to trace module.
2876 * But notrace hash requires a test of individual module functions.
2878 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2879 ftrace_hash_empty(ops->func_hash->notrace_hash);
2883 * Check if the current ops references the record.
2885 * If the ops traces all functions, then it was already accounted for.
2886 * If the ops does not trace the current record function, skip it.
2887 * If the ops ignores the function via notrace filter, skip it.
2890 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2892 /* If ops isn't enabled, ignore it */
2893 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2896 /* If ops traces all then it includes this function */
2897 if (ops_traces_mod(ops))
2900 /* The function must be in the filter */
2901 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2902 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2905 /* If in notrace hash, we ignore it too */
2906 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2912 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2914 struct ftrace_page *pg;
2915 struct dyn_ftrace *p;
2917 unsigned long update_cnt = 0;
2918 unsigned long rec_flags = 0;
2921 start = ftrace_now(raw_smp_processor_id());
2924 * When a module is loaded, this function is called to convert
2925 * the calls to mcount in its text to nops, and also to create
2926 * an entry in the ftrace data. Now, if ftrace is activated
2927 * after this call, but before the module sets its text to
2928 * read-only, the modification of enabling ftrace can fail if
2929 * the read-only is done while ftrace is converting the calls.
2930 * To prevent this, the module's records are set as disabled
2931 * and will be enabled after the call to set the module's text
2935 rec_flags |= FTRACE_FL_DISABLED;
2937 for (pg = new_pgs; pg; pg = pg->next) {
2939 for (i = 0; i < pg->index; i++) {
2941 /* If something went wrong, bail without enabling anything */
2942 if (unlikely(ftrace_disabled))
2945 p = &pg->records[i];
2946 p->flags = rec_flags;
2949 * Do the initial record conversion from mcount jump
2950 * to the NOP instructions.
2952 if (!__is_defined(CC_USING_NOP_MCOUNT) &&
2953 !ftrace_code_disable(mod, p))
2960 stop = ftrace_now(raw_smp_processor_id());
2961 ftrace_update_time = stop - start;
2962 ftrace_update_tot_cnt += update_cnt;
2967 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2972 if (WARN_ON(!count))
2975 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2978 * We want to fill as much as possible. No more than a page
2981 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2985 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2988 /* if we can't allocate this size, try something smaller */
2995 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3004 static struct ftrace_page *
3005 ftrace_allocate_pages(unsigned long num_to_init)
3007 struct ftrace_page *start_pg;
3008 struct ftrace_page *pg;
3015 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3020 * Try to allocate as much as possible in one continues
3021 * location that fills in all of the space. We want to
3022 * waste as little space as possible.
3025 cnt = ftrace_allocate_records(pg, num_to_init);
3033 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3045 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3046 free_pages((unsigned long)pg->records, order);
3047 start_pg = pg->next;
3051 pr_info("ftrace: FAILED to allocate memory for functions\n");
3055 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3057 struct ftrace_iterator {
3061 struct ftrace_page *pg;
3062 struct dyn_ftrace *func;
3063 struct ftrace_func_probe *probe;
3064 struct ftrace_func_entry *probe_entry;
3065 struct trace_parser parser;
3066 struct ftrace_hash *hash;
3067 struct ftrace_ops *ops;
3068 struct trace_array *tr;
3069 struct list_head *mod_list;
3076 t_probe_next(struct seq_file *m, loff_t *pos)
3078 struct ftrace_iterator *iter = m->private;
3079 struct trace_array *tr = iter->ops->private;
3080 struct list_head *func_probes;
3081 struct ftrace_hash *hash;
3082 struct list_head *next;
3083 struct hlist_node *hnd = NULL;
3084 struct hlist_head *hhd;
3093 func_probes = &tr->func_probes;
3094 if (list_empty(func_probes))
3098 next = func_probes->next;
3099 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3102 if (iter->probe_entry)
3103 hnd = &iter->probe_entry->hlist;
3105 hash = iter->probe->ops.func_hash->filter_hash;
3108 * A probe being registered may temporarily have an empty hash
3109 * and it's at the end of the func_probes list.
3111 if (!hash || hash == EMPTY_HASH)
3114 size = 1 << hash->size_bits;
3117 if (iter->pidx >= size) {
3118 if (iter->probe->list.next == func_probes)
3120 next = iter->probe->list.next;
3121 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3122 hash = iter->probe->ops.func_hash->filter_hash;
3123 size = 1 << hash->size_bits;
3127 hhd = &hash->buckets[iter->pidx];
3129 if (hlist_empty(hhd)) {
3145 if (WARN_ON_ONCE(!hnd))
3148 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3153 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3155 struct ftrace_iterator *iter = m->private;
3159 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3162 if (iter->mod_pos > *pos)
3166 iter->probe_entry = NULL;
3168 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3169 p = t_probe_next(m, &l);
3176 /* Only set this if we have an item */
3177 iter->flags |= FTRACE_ITER_PROBE;
3183 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3185 struct ftrace_func_entry *probe_entry;
3186 struct ftrace_probe_ops *probe_ops;
3187 struct ftrace_func_probe *probe;
3189 probe = iter->probe;
3190 probe_entry = iter->probe_entry;
3192 if (WARN_ON_ONCE(!probe || !probe_entry))
3195 probe_ops = probe->probe_ops;
3197 if (probe_ops->print)
3198 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3200 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3201 (void *)probe_ops->func);
3207 t_mod_next(struct seq_file *m, loff_t *pos)
3209 struct ftrace_iterator *iter = m->private;
3210 struct trace_array *tr = iter->tr;
3215 iter->mod_list = iter->mod_list->next;
3217 if (iter->mod_list == &tr->mod_trace ||
3218 iter->mod_list == &tr->mod_notrace) {
3219 iter->flags &= ~FTRACE_ITER_MOD;
3223 iter->mod_pos = *pos;
3228 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3230 struct ftrace_iterator *iter = m->private;
3234 if (iter->func_pos > *pos)
3237 iter->mod_pos = iter->func_pos;
3239 /* probes are only available if tr is set */
3243 for (l = 0; l <= (*pos - iter->func_pos); ) {
3244 p = t_mod_next(m, &l);
3249 iter->flags &= ~FTRACE_ITER_MOD;
3250 return t_probe_start(m, pos);
3253 /* Only set this if we have an item */
3254 iter->flags |= FTRACE_ITER_MOD;
3260 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3262 struct ftrace_mod_load *ftrace_mod;
3263 struct trace_array *tr = iter->tr;
3265 if (WARN_ON_ONCE(!iter->mod_list) ||
3266 iter->mod_list == &tr->mod_trace ||
3267 iter->mod_list == &tr->mod_notrace)
3270 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3272 if (ftrace_mod->func)
3273 seq_printf(m, "%s", ftrace_mod->func);
3277 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3283 t_func_next(struct seq_file *m, loff_t *pos)
3285 struct ftrace_iterator *iter = m->private;
3286 struct dyn_ftrace *rec = NULL;
3291 if (iter->idx >= iter->pg->index) {
3292 if (iter->pg->next) {
3293 iter->pg = iter->pg->next;
3298 rec = &iter->pg->records[iter->idx++];
3299 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3300 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3302 ((iter->flags & FTRACE_ITER_ENABLED) &&
3303 !(rec->flags & FTRACE_FL_ENABLED))) {
3313 iter->pos = iter->func_pos = *pos;
3320 t_next(struct seq_file *m, void *v, loff_t *pos)
3322 struct ftrace_iterator *iter = m->private;
3323 loff_t l = *pos; /* t_probe_start() must use original pos */
3326 if (unlikely(ftrace_disabled))
3329 if (iter->flags & FTRACE_ITER_PROBE)
3330 return t_probe_next(m, pos);
3332 if (iter->flags & FTRACE_ITER_MOD)
3333 return t_mod_next(m, pos);
3335 if (iter->flags & FTRACE_ITER_PRINTALL) {
3336 /* next must increment pos, and t_probe_start does not */
3338 return t_mod_start(m, &l);
3341 ret = t_func_next(m, pos);
3344 return t_mod_start(m, &l);
3349 static void reset_iter_read(struct ftrace_iterator *iter)
3353 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3356 static void *t_start(struct seq_file *m, loff_t *pos)
3358 struct ftrace_iterator *iter = m->private;
3362 mutex_lock(&ftrace_lock);
3364 if (unlikely(ftrace_disabled))
3368 * If an lseek was done, then reset and start from beginning.
3370 if (*pos < iter->pos)
3371 reset_iter_read(iter);
3374 * For set_ftrace_filter reading, if we have the filter
3375 * off, we can short cut and just print out that all
3376 * functions are enabled.
3378 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3379 ftrace_hash_empty(iter->hash)) {
3380 iter->func_pos = 1; /* Account for the message */
3382 return t_mod_start(m, pos);
3383 iter->flags |= FTRACE_ITER_PRINTALL;
3384 /* reset in case of seek/pread */
3385 iter->flags &= ~FTRACE_ITER_PROBE;
3389 if (iter->flags & FTRACE_ITER_MOD)
3390 return t_mod_start(m, pos);
3393 * Unfortunately, we need to restart at ftrace_pages_start
3394 * every time we let go of the ftrace_mutex. This is because
3395 * those pointers can change without the lock.
3397 iter->pg = ftrace_pages_start;
3399 for (l = 0; l <= *pos; ) {
3400 p = t_func_next(m, &l);
3406 return t_mod_start(m, pos);
3411 static void t_stop(struct seq_file *m, void *p)
3413 mutex_unlock(&ftrace_lock);
3417 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3422 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3423 struct dyn_ftrace *rec)
3427 ptr = arch_ftrace_trampoline_func(ops, rec);
3429 seq_printf(m, " ->%pS", ptr);
3432 static int t_show(struct seq_file *m, void *v)
3434 struct ftrace_iterator *iter = m->private;
3435 struct dyn_ftrace *rec;
3437 if (iter->flags & FTRACE_ITER_PROBE)
3438 return t_probe_show(m, iter);
3440 if (iter->flags & FTRACE_ITER_MOD)
3441 return t_mod_show(m, iter);
3443 if (iter->flags & FTRACE_ITER_PRINTALL) {
3444 if (iter->flags & FTRACE_ITER_NOTRACE)
3445 seq_puts(m, "#### no functions disabled ####\n");
3447 seq_puts(m, "#### all functions enabled ####\n");
3456 seq_printf(m, "%ps", (void *)rec->ip);
3457 if (iter->flags & FTRACE_ITER_ENABLED) {
3458 struct ftrace_ops *ops;
3460 seq_printf(m, " (%ld)%s%s",
3461 ftrace_rec_count(rec),
3462 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3463 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3464 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3465 ops = ftrace_find_tramp_ops_any(rec);
3468 seq_printf(m, "\ttramp: %pS (%pS)",
3469 (void *)ops->trampoline,
3471 add_trampoline_func(m, ops, rec);
3472 ops = ftrace_find_tramp_ops_next(rec, ops);
3475 seq_puts(m, "\ttramp: ERROR!");
3477 add_trampoline_func(m, NULL, rec);
3486 static const struct seq_operations show_ftrace_seq_ops = {
3494 ftrace_avail_open(struct inode *inode, struct file *file)
3496 struct ftrace_iterator *iter;
3499 ret = security_locked_down(LOCKDOWN_TRACEFS);
3503 if (unlikely(ftrace_disabled))
3506 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3510 iter->pg = ftrace_pages_start;
3511 iter->ops = &global_ops;
3517 ftrace_enabled_open(struct inode *inode, struct file *file)
3519 struct ftrace_iterator *iter;
3522 * This shows us what functions are currently being
3523 * traced and by what. Not sure if we want lockdown
3524 * to hide such critical information for an admin.
3525 * Although, perhaps it can show information we don't
3526 * want people to see, but if something is tracing
3527 * something, we probably want to know about it.
3530 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3534 iter->pg = ftrace_pages_start;
3535 iter->flags = FTRACE_ITER_ENABLED;
3536 iter->ops = &global_ops;
3542 * ftrace_regex_open - initialize function tracer filter files
3543 * @ops: The ftrace_ops that hold the hash filters
3544 * @flag: The type of filter to process
3545 * @inode: The inode, usually passed in to your open routine
3546 * @file: The file, usually passed in to your open routine
3548 * ftrace_regex_open() initializes the filter files for the
3549 * @ops. Depending on @flag it may process the filter hash or
3550 * the notrace hash of @ops. With this called from the open
3551 * routine, you can use ftrace_filter_write() for the write
3552 * routine if @flag has FTRACE_ITER_FILTER set, or
3553 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3554 * tracing_lseek() should be used as the lseek routine, and
3555 * release must call ftrace_regex_release().
3558 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3559 struct inode *inode, struct file *file)
3561 struct ftrace_iterator *iter;
3562 struct ftrace_hash *hash;
3563 struct list_head *mod_head;
3564 struct trace_array *tr = ops->private;
3567 ftrace_ops_init(ops);
3569 if (unlikely(ftrace_disabled))
3572 if (tracing_check_open_get_tr(tr))
3575 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3579 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3586 mutex_lock(&ops->func_hash->regex_lock);
3588 if (flag & FTRACE_ITER_NOTRACE) {
3589 hash = ops->func_hash->notrace_hash;
3590 mod_head = tr ? &tr->mod_notrace : NULL;
3592 hash = ops->func_hash->filter_hash;
3593 mod_head = tr ? &tr->mod_trace : NULL;
3596 iter->mod_list = mod_head;
3598 if (file->f_mode & FMODE_WRITE) {
3599 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3601 if (file->f_flags & O_TRUNC) {
3602 iter->hash = alloc_ftrace_hash(size_bits);
3603 clear_ftrace_mod_list(mod_head);
3605 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3609 trace_parser_put(&iter->parser);
3617 if (file->f_mode & FMODE_READ) {
3618 iter->pg = ftrace_pages_start;
3620 ret = seq_open(file, &show_ftrace_seq_ops);
3622 struct seq_file *m = file->private_data;
3626 free_ftrace_hash(iter->hash);
3627 trace_parser_put(&iter->parser);
3630 file->private_data = iter;
3633 mutex_unlock(&ops->func_hash->regex_lock);
3639 trace_array_put(tr);
3646 ftrace_filter_open(struct inode *inode, struct file *file)
3648 struct ftrace_ops *ops = inode->i_private;
3650 /* Checks for tracefs lockdown */
3651 return ftrace_regex_open(ops,
3652 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3657 ftrace_notrace_open(struct inode *inode, struct file *file)
3659 struct ftrace_ops *ops = inode->i_private;
3661 /* Checks for tracefs lockdown */
3662 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3666 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3667 struct ftrace_glob {
3674 * If symbols in an architecture don't correspond exactly to the user-visible
3675 * name of what they represent, it is possible to define this function to
3676 * perform the necessary adjustments.
3678 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3683 static int ftrace_match(char *str, struct ftrace_glob *g)
3688 str = arch_ftrace_match_adjust(str, g->search);
3692 if (strcmp(str, g->search) == 0)
3695 case MATCH_FRONT_ONLY:
3696 if (strncmp(str, g->search, g->len) == 0)
3699 case MATCH_MIDDLE_ONLY:
3700 if (strstr(str, g->search))
3703 case MATCH_END_ONLY:
3705 if (slen >= g->len &&
3706 memcmp(str + slen - g->len, g->search, g->len) == 0)
3710 if (glob_match(g->search, str))
3719 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3721 struct ftrace_func_entry *entry;
3724 entry = ftrace_lookup_ip(hash, rec->ip);
3726 /* Do nothing if it doesn't exist */
3730 free_hash_entry(hash, entry);
3732 /* Do nothing if it exists */
3736 ret = add_hash_entry(hash, rec->ip);
3742 add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3745 long index = simple_strtoul(func_g->search, NULL, 0);
3746 struct ftrace_page *pg;
3747 struct dyn_ftrace *rec;
3749 /* The index starts at 1 */
3753 do_for_each_ftrace_rec(pg, rec) {
3754 if (pg->index <= index) {
3756 /* this is a double loop, break goes to the next page */
3759 rec = &pg->records[index];
3760 enter_record(hash, rec, clear_filter);
3762 } while_for_each_ftrace_rec();
3767 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3768 struct ftrace_glob *mod_g, int exclude_mod)
3770 char str[KSYM_SYMBOL_LEN];
3773 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3776 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3778 /* blank module name to match all modules */
3780 /* blank module globbing: modname xor exclude_mod */
3781 if (!exclude_mod != !modname)
3787 * exclude_mod is set to trace everything but the given
3788 * module. If it is set and the module matches, then
3789 * return 0. If it is not set, and the module doesn't match
3790 * also return 0. Otherwise, check the function to see if
3793 if (!mod_matches == !exclude_mod)
3796 /* blank search means to match all funcs in the mod */
3801 return ftrace_match(str, func_g);
3805 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3807 struct ftrace_page *pg;
3808 struct dyn_ftrace *rec;
3809 struct ftrace_glob func_g = { .type = MATCH_FULL };
3810 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3811 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3812 int exclude_mod = 0;
3815 int clear_filter = 0;
3818 func_g.type = filter_parse_regex(func, len, &func_g.search,
3820 func_g.len = strlen(func_g.search);
3824 mod_g.type = filter_parse_regex(mod, strlen(mod),
3825 &mod_g.search, &exclude_mod);
3826 mod_g.len = strlen(mod_g.search);
3829 mutex_lock(&ftrace_lock);
3831 if (unlikely(ftrace_disabled))
3834 if (func_g.type == MATCH_INDEX) {
3835 found = add_rec_by_index(hash, &func_g, clear_filter);
3839 do_for_each_ftrace_rec(pg, rec) {
3841 if (rec->flags & FTRACE_FL_DISABLED)
3844 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3845 ret = enter_record(hash, rec, clear_filter);
3852 } while_for_each_ftrace_rec();
3854 mutex_unlock(&ftrace_lock);
3860 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3862 return match_records(hash, buff, len, NULL);
3865 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3866 struct ftrace_ops_hash *old_hash)
3868 struct ftrace_ops *op;
3870 if (!ftrace_enabled)
3873 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3874 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3879 * If this is the shared global_ops filter, then we need to
3880 * check if there is another ops that shares it, is enabled.
3881 * If so, we still need to run the modify code.
3883 if (ops->func_hash != &global_ops.local_hash)
3886 do_for_each_ftrace_op(op, ftrace_ops_list) {
3887 if (op->func_hash == &global_ops.local_hash &&
3888 op->flags & FTRACE_OPS_FL_ENABLED) {
3889 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3890 /* Only need to do this once */
3893 } while_for_each_ftrace_op(op);
3896 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3897 struct ftrace_hash **orig_hash,
3898 struct ftrace_hash *hash,
3901 struct ftrace_ops_hash old_hash_ops;
3902 struct ftrace_hash *old_hash;
3905 old_hash = *orig_hash;
3906 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3907 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3908 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3910 ftrace_ops_update_code(ops, &old_hash_ops);
3911 free_ftrace_hash_rcu(old_hash);
3916 static bool module_exists(const char *module)
3918 /* All modules have the symbol __this_module */
3919 static const char this_mod[] = "__this_module";
3920 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
3924 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
3926 if (n > sizeof(modname) - 1)
3929 val = module_kallsyms_lookup_name(modname);
3933 static int cache_mod(struct trace_array *tr,
3934 const char *func, char *module, int enable)
3936 struct ftrace_mod_load *ftrace_mod, *n;
3937 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3940 mutex_lock(&ftrace_lock);
3942 /* We do not cache inverse filters */
3943 if (func[0] == '!') {
3947 /* Look to remove this hash */
3948 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3949 if (strcmp(ftrace_mod->module, module) != 0)
3952 /* no func matches all */
3953 if (strcmp(func, "*") == 0 ||
3954 (ftrace_mod->func &&
3955 strcmp(ftrace_mod->func, func) == 0)) {
3957 free_ftrace_mod(ftrace_mod);
3965 /* We only care about modules that have not been loaded yet */
3966 if (module_exists(module))
3969 /* Save this string off, and execute it when the module is loaded */
3970 ret = ftrace_add_mod(tr, func, module, enable);
3972 mutex_unlock(&ftrace_lock);
3978 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3979 int reset, int enable);
3981 #ifdef CONFIG_MODULES
3982 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3983 char *mod, bool enable)
3985 struct ftrace_mod_load *ftrace_mod, *n;
3986 struct ftrace_hash **orig_hash, *new_hash;
3987 LIST_HEAD(process_mods);
3991 mutex_lock(&ops->func_hash->regex_lock);
3994 orig_hash = &ops->func_hash->filter_hash;
3996 orig_hash = &ops->func_hash->notrace_hash;
3998 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4001 goto out; /* warn? */
4003 mutex_lock(&ftrace_lock);
4005 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4007 if (strcmp(ftrace_mod->module, mod) != 0)
4010 if (ftrace_mod->func)
4011 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4013 func = kstrdup("*", GFP_KERNEL);
4015 if (!func) /* warn? */
4018 list_del(&ftrace_mod->list);
4019 list_add(&ftrace_mod->list, &process_mods);
4021 /* Use the newly allocated func, as it may be "*" */
4022 kfree(ftrace_mod->func);
4023 ftrace_mod->func = func;
4026 mutex_unlock(&ftrace_lock);
4028 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4030 func = ftrace_mod->func;
4032 /* Grabs ftrace_lock, which is why we have this extra step */
4033 match_records(new_hash, func, strlen(func), mod);
4034 free_ftrace_mod(ftrace_mod);
4037 if (enable && list_empty(head))
4038 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4040 mutex_lock(&ftrace_lock);
4042 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4044 mutex_unlock(&ftrace_lock);
4047 mutex_unlock(&ops->func_hash->regex_lock);
4049 free_ftrace_hash(new_hash);
4052 static void process_cached_mods(const char *mod_name)
4054 struct trace_array *tr;
4057 mod = kstrdup(mod_name, GFP_KERNEL);
4061 mutex_lock(&trace_types_lock);
4062 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4063 if (!list_empty(&tr->mod_trace))
4064 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4065 if (!list_empty(&tr->mod_notrace))
4066 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4068 mutex_unlock(&trace_types_lock);
4075 * We register the module command as a template to show others how
4076 * to register the a command as well.
4080 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4081 char *func_orig, char *cmd, char *module, int enable)
4086 /* match_records() modifies func, and we need the original */
4087 func = kstrdup(func_orig, GFP_KERNEL);
4092 * cmd == 'mod' because we only registered this func
4093 * for the 'mod' ftrace_func_command.
4094 * But if you register one func with multiple commands,
4095 * you can tell which command was used by the cmd
4098 ret = match_records(hash, func, strlen(func), module);
4102 return cache_mod(tr, func_orig, module, enable);
4108 static struct ftrace_func_command ftrace_mod_cmd = {
4110 .func = ftrace_mod_callback,
4113 static int __init ftrace_mod_cmd_init(void)
4115 return register_ftrace_command(&ftrace_mod_cmd);
4117 core_initcall(ftrace_mod_cmd_init);
4119 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4120 struct ftrace_ops *op, struct pt_regs *pt_regs)
4122 struct ftrace_probe_ops *probe_ops;
4123 struct ftrace_func_probe *probe;
4125 probe = container_of(op, struct ftrace_func_probe, ops);
4126 probe_ops = probe->probe_ops;
4129 * Disable preemption for these calls to prevent a RCU grace
4130 * period. This syncs the hash iteration and freeing of items
4131 * on the hash. rcu_read_lock is too dangerous here.
4133 preempt_disable_notrace();
4134 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4135 preempt_enable_notrace();
4138 struct ftrace_func_map {
4139 struct ftrace_func_entry entry;
4143 struct ftrace_func_mapper {
4144 struct ftrace_hash hash;
4148 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4150 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4152 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4154 struct ftrace_hash *hash;
4157 * The mapper is simply a ftrace_hash, but since the entries
4158 * in the hash are not ftrace_func_entry type, we define it
4159 * as a separate structure.
4161 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4162 return (struct ftrace_func_mapper *)hash;
4166 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4167 * @mapper: The mapper that has the ip maps
4168 * @ip: the instruction pointer to find the data for
4170 * Returns the data mapped to @ip if found otherwise NULL. The return
4171 * is actually the address of the mapper data pointer. The address is
4172 * returned for use cases where the data is no bigger than a long, and
4173 * the user can use the data pointer as its data instead of having to
4174 * allocate more memory for the reference.
4176 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4179 struct ftrace_func_entry *entry;
4180 struct ftrace_func_map *map;
4182 entry = ftrace_lookup_ip(&mapper->hash, ip);
4186 map = (struct ftrace_func_map *)entry;
4191 * ftrace_func_mapper_add_ip - Map some data to an ip
4192 * @mapper: The mapper that has the ip maps
4193 * @ip: The instruction pointer address to map @data to
4194 * @data: The data to map to @ip
4196 * Returns 0 on succes otherwise an error.
4198 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4199 unsigned long ip, void *data)
4201 struct ftrace_func_entry *entry;
4202 struct ftrace_func_map *map;
4204 entry = ftrace_lookup_ip(&mapper->hash, ip);
4208 map = kmalloc(sizeof(*map), GFP_KERNEL);
4215 __add_hash_entry(&mapper->hash, &map->entry);
4221 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4222 * @mapper: The mapper that has the ip maps
4223 * @ip: The instruction pointer address to remove the data from
4225 * Returns the data if it is found, otherwise NULL.
4226 * Note, if the data pointer is used as the data itself, (see
4227 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4228 * if the data pointer was set to zero.
4230 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4233 struct ftrace_func_entry *entry;
4234 struct ftrace_func_map *map;
4237 entry = ftrace_lookup_ip(&mapper->hash, ip);
4241 map = (struct ftrace_func_map *)entry;
4244 remove_hash_entry(&mapper->hash, entry);
4251 * free_ftrace_func_mapper - free a mapping of ips and data
4252 * @mapper: The mapper that has the ip maps
4253 * @free_func: A function to be called on each data item.
4255 * This is used to free the function mapper. The @free_func is optional
4256 * and can be used if the data needs to be freed as well.
4258 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4259 ftrace_mapper_func free_func)
4261 struct ftrace_func_entry *entry;
4262 struct ftrace_func_map *map;
4263 struct hlist_head *hhd;
4269 if (free_func && mapper->hash.count) {
4270 size = 1 << mapper->hash.size_bits;
4271 for (i = 0; i < size; i++) {
4272 hhd = &mapper->hash.buckets[i];
4273 hlist_for_each_entry(entry, hhd, hlist) {
4274 map = (struct ftrace_func_map *)entry;
4279 free_ftrace_hash(&mapper->hash);
4282 static void release_probe(struct ftrace_func_probe *probe)
4284 struct ftrace_probe_ops *probe_ops;
4286 mutex_lock(&ftrace_lock);
4288 WARN_ON(probe->ref <= 0);
4290 /* Subtract the ref that was used to protect this instance */
4294 probe_ops = probe->probe_ops;
4296 * Sending zero as ip tells probe_ops to free
4297 * the probe->data itself
4299 if (probe_ops->free)
4300 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4301 list_del(&probe->list);
4304 mutex_unlock(&ftrace_lock);
4307 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4310 * Add one ref to keep it from being freed when releasing the
4311 * ftrace_lock mutex.
4317 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4318 struct ftrace_probe_ops *probe_ops,
4321 struct ftrace_func_entry *entry;
4322 struct ftrace_func_probe *probe;
4323 struct ftrace_hash **orig_hash;
4324 struct ftrace_hash *old_hash;
4325 struct ftrace_hash *hash;
4334 /* We do not support '!' for function probes */
4335 if (WARN_ON(glob[0] == '!'))
4339 mutex_lock(&ftrace_lock);
4340 /* Check if the probe_ops is already registered */
4341 list_for_each_entry(probe, &tr->func_probes, list) {
4342 if (probe->probe_ops == probe_ops)
4345 if (&probe->list == &tr->func_probes) {
4346 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4348 mutex_unlock(&ftrace_lock);
4351 probe->probe_ops = probe_ops;
4352 probe->ops.func = function_trace_probe_call;
4354 ftrace_ops_init(&probe->ops);
4355 list_add(&probe->list, &tr->func_probes);
4358 acquire_probe_locked(probe);
4360 mutex_unlock(&ftrace_lock);
4363 * Note, there's a small window here that the func_hash->filter_hash
4364 * may be NULL or empty. Need to be carefule when reading the loop.
4366 mutex_lock(&probe->ops.func_hash->regex_lock);
4368 orig_hash = &probe->ops.func_hash->filter_hash;
4369 old_hash = *orig_hash;
4370 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4377 ret = ftrace_match_records(hash, glob, strlen(glob));
4379 /* Nothing found? */
4386 size = 1 << hash->size_bits;
4387 for (i = 0; i < size; i++) {
4388 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4389 if (ftrace_lookup_ip(old_hash, entry->ip))
4392 * The caller might want to do something special
4393 * for each function we find. We call the callback
4394 * to give the caller an opportunity to do so.
4396 if (probe_ops->init) {
4397 ret = probe_ops->init(probe_ops, tr,
4401 if (probe_ops->free && count)
4402 probe_ops->free(probe_ops, tr,
4412 mutex_lock(&ftrace_lock);
4415 /* Nothing was added? */
4420 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4425 /* One ref for each new function traced */
4426 probe->ref += count;
4428 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4429 ret = ftrace_startup(&probe->ops, 0);
4432 mutex_unlock(&ftrace_lock);
4437 mutex_unlock(&probe->ops.func_hash->regex_lock);
4438 free_ftrace_hash(hash);
4440 release_probe(probe);
4445 if (!probe_ops->free || !count)
4448 /* Failed to do the move, need to call the free functions */
4449 for (i = 0; i < size; i++) {
4450 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4451 if (ftrace_lookup_ip(old_hash, entry->ip))
4453 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4460 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4461 struct ftrace_probe_ops *probe_ops)
4463 struct ftrace_ops_hash old_hash_ops;
4464 struct ftrace_func_entry *entry;
4465 struct ftrace_func_probe *probe;
4466 struct ftrace_glob func_g;
4467 struct ftrace_hash **orig_hash;
4468 struct ftrace_hash *old_hash;
4469 struct ftrace_hash *hash = NULL;
4470 struct hlist_node *tmp;
4471 struct hlist_head hhd;
4472 char str[KSYM_SYMBOL_LEN];
4474 int i, ret = -ENODEV;
4477 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4478 func_g.search = NULL;
4482 func_g.type = filter_parse_regex(glob, strlen(glob),
4483 &func_g.search, ¬);
4484 func_g.len = strlen(func_g.search);
4486 /* we do not support '!' for function probes */
4491 mutex_lock(&ftrace_lock);
4492 /* Check if the probe_ops is already registered */
4493 list_for_each_entry(probe, &tr->func_probes, list) {
4494 if (probe->probe_ops == probe_ops)
4497 if (&probe->list == &tr->func_probes)
4498 goto err_unlock_ftrace;
4501 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4502 goto err_unlock_ftrace;
4504 acquire_probe_locked(probe);
4506 mutex_unlock(&ftrace_lock);
4508 mutex_lock(&probe->ops.func_hash->regex_lock);
4510 orig_hash = &probe->ops.func_hash->filter_hash;
4511 old_hash = *orig_hash;
4513 if (ftrace_hash_empty(old_hash))
4516 old_hash_ops.filter_hash = old_hash;
4517 /* Probes only have filters */
4518 old_hash_ops.notrace_hash = NULL;
4521 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4525 INIT_HLIST_HEAD(&hhd);
4527 size = 1 << hash->size_bits;
4528 for (i = 0; i < size; i++) {
4529 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4531 if (func_g.search) {
4532 kallsyms_lookup(entry->ip, NULL, NULL,
4534 if (!ftrace_match(str, &func_g))
4538 remove_hash_entry(hash, entry);
4539 hlist_add_head(&entry->hlist, &hhd);
4543 /* Nothing found? */
4549 mutex_lock(&ftrace_lock);
4551 WARN_ON(probe->ref < count);
4553 probe->ref -= count;
4555 if (ftrace_hash_empty(hash))
4556 ftrace_shutdown(&probe->ops, 0);
4558 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4561 /* still need to update the function call sites */
4562 if (ftrace_enabled && !ftrace_hash_empty(hash))
4563 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4567 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4568 hlist_del(&entry->hlist);
4569 if (probe_ops->free)
4570 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4573 mutex_unlock(&ftrace_lock);
4576 mutex_unlock(&probe->ops.func_hash->regex_lock);
4577 free_ftrace_hash(hash);
4579 release_probe(probe);
4584 mutex_unlock(&ftrace_lock);
4588 void clear_ftrace_function_probes(struct trace_array *tr)
4590 struct ftrace_func_probe *probe, *n;
4592 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4593 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4596 static LIST_HEAD(ftrace_commands);
4597 static DEFINE_MUTEX(ftrace_cmd_mutex);
4600 * Currently we only register ftrace commands from __init, so mark this
4603 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4605 struct ftrace_func_command *p;
4608 mutex_lock(&ftrace_cmd_mutex);
4609 list_for_each_entry(p, &ftrace_commands, list) {
4610 if (strcmp(cmd->name, p->name) == 0) {
4615 list_add(&cmd->list, &ftrace_commands);
4617 mutex_unlock(&ftrace_cmd_mutex);
4623 * Currently we only unregister ftrace commands from __init, so mark
4626 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4628 struct ftrace_func_command *p, *n;
4631 mutex_lock(&ftrace_cmd_mutex);
4632 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4633 if (strcmp(cmd->name, p->name) == 0) {
4635 list_del_init(&p->list);
4640 mutex_unlock(&ftrace_cmd_mutex);
4645 static int ftrace_process_regex(struct ftrace_iterator *iter,
4646 char *buff, int len, int enable)
4648 struct ftrace_hash *hash = iter->hash;
4649 struct trace_array *tr = iter->ops->private;
4650 char *func, *command, *next = buff;
4651 struct ftrace_func_command *p;
4654 func = strsep(&next, ":");
4657 ret = ftrace_match_records(hash, func, len);
4667 command = strsep(&next, ":");
4669 mutex_lock(&ftrace_cmd_mutex);
4670 list_for_each_entry(p, &ftrace_commands, list) {
4671 if (strcmp(p->name, command) == 0) {
4672 ret = p->func(tr, hash, func, command, next, enable);
4677 mutex_unlock(&ftrace_cmd_mutex);
4683 ftrace_regex_write(struct file *file, const char __user *ubuf,
4684 size_t cnt, loff_t *ppos, int enable)
4686 struct ftrace_iterator *iter;
4687 struct trace_parser *parser;
4693 if (file->f_mode & FMODE_READ) {
4694 struct seq_file *m = file->private_data;
4697 iter = file->private_data;
4699 if (unlikely(ftrace_disabled))
4702 /* iter->hash is a local copy, so we don't need regex_lock */
4704 parser = &iter->parser;
4705 read = trace_get_user(parser, ubuf, cnt, ppos);
4707 if (read >= 0 && trace_parser_loaded(parser) &&
4708 !trace_parser_cont(parser)) {
4709 ret = ftrace_process_regex(iter, parser->buffer,
4710 parser->idx, enable);
4711 trace_parser_clear(parser);
4722 ftrace_filter_write(struct file *file, const char __user *ubuf,
4723 size_t cnt, loff_t *ppos)
4725 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4729 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4730 size_t cnt, loff_t *ppos)
4732 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4736 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4738 struct ftrace_func_entry *entry;
4740 if (!ftrace_location(ip))
4744 entry = ftrace_lookup_ip(hash, ip);
4747 free_hash_entry(hash, entry);
4751 return add_hash_entry(hash, ip);
4755 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4756 unsigned long ip, int remove, int reset, int enable)
4758 struct ftrace_hash **orig_hash;
4759 struct ftrace_hash *hash;
4762 if (unlikely(ftrace_disabled))
4765 mutex_lock(&ops->func_hash->regex_lock);
4768 orig_hash = &ops->func_hash->filter_hash;
4770 orig_hash = &ops->func_hash->notrace_hash;
4773 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4775 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4779 goto out_regex_unlock;
4782 if (buf && !ftrace_match_records(hash, buf, len)) {
4784 goto out_regex_unlock;
4787 ret = ftrace_match_addr(hash, ip, remove);
4789 goto out_regex_unlock;
4792 mutex_lock(&ftrace_lock);
4793 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4794 mutex_unlock(&ftrace_lock);
4797 mutex_unlock(&ops->func_hash->regex_lock);
4799 free_ftrace_hash(hash);
4804 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4805 int reset, int enable)
4807 return ftrace_set_hash(ops, NULL, 0, ip, remove, reset, enable);
4811 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4812 * @ops - the ops to set the filter with
4813 * @ip - the address to add to or remove from the filter.
4814 * @remove - non zero to remove the ip from the filter
4815 * @reset - non zero to reset all filters before applying this filter.
4817 * Filters denote which functions should be enabled when tracing is enabled
4818 * If @ip is NULL, it failes to update filter.
4820 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4821 int remove, int reset)
4823 ftrace_ops_init(ops);
4824 return ftrace_set_addr(ops, ip, remove, reset, 1);
4826 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4829 * ftrace_ops_set_global_filter - setup ops to use global filters
4830 * @ops - the ops which will use the global filters
4832 * ftrace users who need global function trace filtering should call this.
4833 * It can set the global filter only if ops were not initialized before.
4835 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4837 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4840 ftrace_ops_init(ops);
4841 ops->func_hash = &global_ops.local_hash;
4843 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4846 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4847 int reset, int enable)
4849 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4853 * ftrace_set_filter - set a function to filter on in ftrace
4854 * @ops - the ops to set the filter with
4855 * @buf - the string that holds the function filter text.
4856 * @len - the length of the string.
4857 * @reset - non zero to reset all filters before applying this filter.
4859 * Filters denote which functions should be enabled when tracing is enabled.
4860 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4862 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4865 ftrace_ops_init(ops);
4866 return ftrace_set_regex(ops, buf, len, reset, 1);
4868 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4871 * ftrace_set_notrace - set a function to not trace in ftrace
4872 * @ops - the ops to set the notrace filter with
4873 * @buf - the string that holds the function notrace text.
4874 * @len - the length of the string.
4875 * @reset - non zero to reset all filters before applying this filter.
4877 * Notrace Filters denote which functions should not be enabled when tracing
4878 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4881 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4884 ftrace_ops_init(ops);
4885 return ftrace_set_regex(ops, buf, len, reset, 0);
4887 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4889 * ftrace_set_global_filter - set a function to filter on with global tracers
4890 * @buf - the string that holds the function filter text.
4891 * @len - the length of the string.
4892 * @reset - non zero to reset all filters before applying this filter.
4894 * Filters denote which functions should be enabled when tracing is enabled.
4895 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4897 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4899 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4901 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4904 * ftrace_set_global_notrace - set a function to not trace with global tracers
4905 * @buf - the string that holds the function notrace text.
4906 * @len - the length of the string.
4907 * @reset - non zero to reset all filters before applying this filter.
4909 * Notrace Filters denote which functions should not be enabled when tracing
4910 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4913 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4915 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4917 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4920 * command line interface to allow users to set filters on boot up.
4922 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4923 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4924 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4926 /* Used by function selftest to not test if filter is set */
4927 bool ftrace_filter_param __initdata;
4929 static int __init set_ftrace_notrace(char *str)
4931 ftrace_filter_param = true;
4932 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4935 __setup("ftrace_notrace=", set_ftrace_notrace);
4937 static int __init set_ftrace_filter(char *str)
4939 ftrace_filter_param = true;
4940 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4943 __setup("ftrace_filter=", set_ftrace_filter);
4945 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4946 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4947 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4948 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
4950 static int __init set_graph_function(char *str)
4952 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4955 __setup("ftrace_graph_filter=", set_graph_function);
4957 static int __init set_graph_notrace_function(char *str)
4959 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4962 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4964 static int __init set_graph_max_depth_function(char *str)
4968 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4971 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4973 static void __init set_ftrace_early_graph(char *buf, int enable)
4977 struct ftrace_hash *hash;
4979 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4984 func = strsep(&buf, ",");
4985 /* we allow only one expression at a time */
4986 ret = ftrace_graph_set_hash(hash, func);
4988 printk(KERN_DEBUG "ftrace: function %s not "
4989 "traceable\n", func);
4993 ftrace_graph_hash = hash;
4995 ftrace_graph_notrace_hash = hash;
4997 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5000 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
5004 ftrace_ops_init(ops);
5007 func = strsep(&buf, ",");
5008 ftrace_set_regex(ops, func, strlen(func), 0, enable);
5012 static void __init set_ftrace_early_filters(void)
5014 if (ftrace_filter_buf[0])
5015 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5016 if (ftrace_notrace_buf[0])
5017 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5018 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5019 if (ftrace_graph_buf[0])
5020 set_ftrace_early_graph(ftrace_graph_buf, 1);
5021 if (ftrace_graph_notrace_buf[0])
5022 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5023 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5026 int ftrace_regex_release(struct inode *inode, struct file *file)
5028 struct seq_file *m = (struct seq_file *)file->private_data;
5029 struct ftrace_iterator *iter;
5030 struct ftrace_hash **orig_hash;
5031 struct trace_parser *parser;
5035 if (file->f_mode & FMODE_READ) {
5037 seq_release(inode, file);
5039 iter = file->private_data;
5041 parser = &iter->parser;
5042 if (trace_parser_loaded(parser)) {
5043 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
5046 trace_parser_put(parser);
5048 mutex_lock(&iter->ops->func_hash->regex_lock);
5050 if (file->f_mode & FMODE_WRITE) {
5051 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5054 orig_hash = &iter->ops->func_hash->filter_hash;
5055 if (iter->tr && !list_empty(&iter->tr->mod_trace))
5056 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5058 orig_hash = &iter->ops->func_hash->notrace_hash;
5060 mutex_lock(&ftrace_lock);
5061 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5062 iter->hash, filter_hash);
5063 mutex_unlock(&ftrace_lock);
5065 /* For read only, the hash is the ops hash */
5069 mutex_unlock(&iter->ops->func_hash->regex_lock);
5070 free_ftrace_hash(iter->hash);
5072 trace_array_put(iter->tr);
5078 static const struct file_operations ftrace_avail_fops = {
5079 .open = ftrace_avail_open,
5081 .llseek = seq_lseek,
5082 .release = seq_release_private,
5085 static const struct file_operations ftrace_enabled_fops = {
5086 .open = ftrace_enabled_open,
5088 .llseek = seq_lseek,
5089 .release = seq_release_private,
5092 static const struct file_operations ftrace_filter_fops = {
5093 .open = ftrace_filter_open,
5095 .write = ftrace_filter_write,
5096 .llseek = tracing_lseek,
5097 .release = ftrace_regex_release,
5100 static const struct file_operations ftrace_notrace_fops = {
5101 .open = ftrace_notrace_open,
5103 .write = ftrace_notrace_write,
5104 .llseek = tracing_lseek,
5105 .release = ftrace_regex_release,
5108 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5110 static DEFINE_MUTEX(graph_lock);
5112 struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5113 struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5115 enum graph_filter_type {
5116 GRAPH_FILTER_NOTRACE = 0,
5117 GRAPH_FILTER_FUNCTION,
5120 #define FTRACE_GRAPH_EMPTY ((void *)1)
5122 struct ftrace_graph_data {
5123 struct ftrace_hash *hash;
5124 struct ftrace_func_entry *entry;
5125 int idx; /* for hash table iteration */
5126 enum graph_filter_type type;
5127 struct ftrace_hash *new_hash;
5128 const struct seq_operations *seq_ops;
5129 struct trace_parser parser;
5133 __g_next(struct seq_file *m, loff_t *pos)
5135 struct ftrace_graph_data *fgd = m->private;
5136 struct ftrace_func_entry *entry = fgd->entry;
5137 struct hlist_head *head;
5138 int i, idx = fgd->idx;
5140 if (*pos >= fgd->hash->count)
5144 hlist_for_each_entry_continue(entry, hlist) {
5152 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5153 head = &fgd->hash->buckets[i];
5154 hlist_for_each_entry(entry, head, hlist) {
5164 g_next(struct seq_file *m, void *v, loff_t *pos)
5167 return __g_next(m, pos);
5170 static void *g_start(struct seq_file *m, loff_t *pos)
5172 struct ftrace_graph_data *fgd = m->private;
5174 mutex_lock(&graph_lock);
5176 if (fgd->type == GRAPH_FILTER_FUNCTION)
5177 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5178 lockdep_is_held(&graph_lock));
5180 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5181 lockdep_is_held(&graph_lock));
5183 /* Nothing, tell g_show to print all functions are enabled */
5184 if (ftrace_hash_empty(fgd->hash) && !*pos)
5185 return FTRACE_GRAPH_EMPTY;
5189 return __g_next(m, pos);
5192 static void g_stop(struct seq_file *m, void *p)
5194 mutex_unlock(&graph_lock);
5197 static int g_show(struct seq_file *m, void *v)
5199 struct ftrace_func_entry *entry = v;
5204 if (entry == FTRACE_GRAPH_EMPTY) {
5205 struct ftrace_graph_data *fgd = m->private;
5207 if (fgd->type == GRAPH_FILTER_FUNCTION)
5208 seq_puts(m, "#### all functions enabled ####\n");
5210 seq_puts(m, "#### no functions disabled ####\n");
5214 seq_printf(m, "%ps\n", (void *)entry->ip);
5219 static const struct seq_operations ftrace_graph_seq_ops = {
5227 __ftrace_graph_open(struct inode *inode, struct file *file,
5228 struct ftrace_graph_data *fgd)
5231 struct ftrace_hash *new_hash = NULL;
5233 ret = security_locked_down(LOCKDOWN_TRACEFS);
5237 if (file->f_mode & FMODE_WRITE) {
5238 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5240 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5243 if (file->f_flags & O_TRUNC)
5244 new_hash = alloc_ftrace_hash(size_bits);
5246 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5254 if (file->f_mode & FMODE_READ) {
5255 ret = seq_open(file, &ftrace_graph_seq_ops);
5257 struct seq_file *m = file->private_data;
5261 free_ftrace_hash(new_hash);
5265 file->private_data = fgd;
5268 if (ret < 0 && file->f_mode & FMODE_WRITE)
5269 trace_parser_put(&fgd->parser);
5271 fgd->new_hash = new_hash;
5274 * All uses of fgd->hash must be taken with the graph_lock
5275 * held. The graph_lock is going to be released, so force
5276 * fgd->hash to be reinitialized when it is taken again.
5284 ftrace_graph_open(struct inode *inode, struct file *file)
5286 struct ftrace_graph_data *fgd;
5289 if (unlikely(ftrace_disabled))
5292 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5296 mutex_lock(&graph_lock);
5298 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5299 lockdep_is_held(&graph_lock));
5300 fgd->type = GRAPH_FILTER_FUNCTION;
5301 fgd->seq_ops = &ftrace_graph_seq_ops;
5303 ret = __ftrace_graph_open(inode, file, fgd);
5307 mutex_unlock(&graph_lock);
5312 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5314 struct ftrace_graph_data *fgd;
5317 if (unlikely(ftrace_disabled))
5320 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5324 mutex_lock(&graph_lock);
5326 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5327 lockdep_is_held(&graph_lock));
5328 fgd->type = GRAPH_FILTER_NOTRACE;
5329 fgd->seq_ops = &ftrace_graph_seq_ops;
5331 ret = __ftrace_graph_open(inode, file, fgd);
5335 mutex_unlock(&graph_lock);
5340 ftrace_graph_release(struct inode *inode, struct file *file)
5342 struct ftrace_graph_data *fgd;
5343 struct ftrace_hash *old_hash, *new_hash;
5344 struct trace_parser *parser;
5347 if (file->f_mode & FMODE_READ) {
5348 struct seq_file *m = file->private_data;
5351 seq_release(inode, file);
5353 fgd = file->private_data;
5357 if (file->f_mode & FMODE_WRITE) {
5359 parser = &fgd->parser;
5361 if (trace_parser_loaded((parser))) {
5362 ret = ftrace_graph_set_hash(fgd->new_hash,
5366 trace_parser_put(parser);
5368 new_hash = __ftrace_hash_move(fgd->new_hash);
5374 mutex_lock(&graph_lock);
5376 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5377 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5378 lockdep_is_held(&graph_lock));
5379 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5381 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5382 lockdep_is_held(&graph_lock));
5383 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5386 mutex_unlock(&graph_lock);
5388 /* Wait till all users are no longer using the old hash */
5391 free_ftrace_hash(old_hash);
5395 free_ftrace_hash(fgd->new_hash);
5402 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5404 struct ftrace_glob func_g;
5405 struct dyn_ftrace *rec;
5406 struct ftrace_page *pg;
5407 struct ftrace_func_entry *entry;
5412 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5413 &func_g.search, ¬);
5415 func_g.len = strlen(func_g.search);
5417 mutex_lock(&ftrace_lock);
5419 if (unlikely(ftrace_disabled)) {
5420 mutex_unlock(&ftrace_lock);
5424 do_for_each_ftrace_rec(pg, rec) {
5426 if (rec->flags & FTRACE_FL_DISABLED)
5429 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5430 entry = ftrace_lookup_ip(hash, rec->ip);
5437 if (add_hash_entry(hash, rec->ip) < 0)
5441 free_hash_entry(hash, entry);
5446 } while_for_each_ftrace_rec();
5448 mutex_unlock(&ftrace_lock);
5457 ftrace_graph_write(struct file *file, const char __user *ubuf,
5458 size_t cnt, loff_t *ppos)
5460 ssize_t read, ret = 0;
5461 struct ftrace_graph_data *fgd = file->private_data;
5462 struct trace_parser *parser;
5467 /* Read mode uses seq functions */
5468 if (file->f_mode & FMODE_READ) {
5469 struct seq_file *m = file->private_data;
5473 parser = &fgd->parser;
5475 read = trace_get_user(parser, ubuf, cnt, ppos);
5477 if (read >= 0 && trace_parser_loaded(parser) &&
5478 !trace_parser_cont(parser)) {
5480 ret = ftrace_graph_set_hash(fgd->new_hash,
5482 trace_parser_clear(parser);
5491 static const struct file_operations ftrace_graph_fops = {
5492 .open = ftrace_graph_open,
5494 .write = ftrace_graph_write,
5495 .llseek = tracing_lseek,
5496 .release = ftrace_graph_release,
5499 static const struct file_operations ftrace_graph_notrace_fops = {
5500 .open = ftrace_graph_notrace_open,
5502 .write = ftrace_graph_write,
5503 .llseek = tracing_lseek,
5504 .release = ftrace_graph_release,
5506 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5508 void ftrace_create_filter_files(struct ftrace_ops *ops,
5509 struct dentry *parent)
5512 trace_create_file("set_ftrace_filter", 0644, parent,
5513 ops, &ftrace_filter_fops);
5515 trace_create_file("set_ftrace_notrace", 0644, parent,
5516 ops, &ftrace_notrace_fops);
5520 * The name "destroy_filter_files" is really a misnomer. Although
5521 * in the future, it may actually delete the files, but this is
5522 * really intended to make sure the ops passed in are disabled
5523 * and that when this function returns, the caller is free to
5526 * The "destroy" name is only to match the "create" name that this
5527 * should be paired with.
5529 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5531 mutex_lock(&ftrace_lock);
5532 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5533 ftrace_shutdown(ops, 0);
5534 ops->flags |= FTRACE_OPS_FL_DELETED;
5535 ftrace_free_filter(ops);
5536 mutex_unlock(&ftrace_lock);
5539 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
5542 trace_create_file("available_filter_functions", 0444,
5543 d_tracer, NULL, &ftrace_avail_fops);
5545 trace_create_file("enabled_functions", 0444,
5546 d_tracer, NULL, &ftrace_enabled_fops);
5548 ftrace_create_filter_files(&global_ops, d_tracer);
5550 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5551 trace_create_file("set_graph_function", 0644, d_tracer,
5553 &ftrace_graph_fops);
5554 trace_create_file("set_graph_notrace", 0644, d_tracer,
5556 &ftrace_graph_notrace_fops);
5557 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5562 static int ftrace_cmp_ips(const void *a, const void *b)
5564 const unsigned long *ipa = a;
5565 const unsigned long *ipb = b;
5574 static int ftrace_process_locs(struct module *mod,
5575 unsigned long *start,
5578 struct ftrace_page *start_pg;
5579 struct ftrace_page *pg;
5580 struct dyn_ftrace *rec;
5581 unsigned long count;
5584 unsigned long flags = 0; /* Shut up gcc */
5587 count = end - start;
5592 sort(start, count, sizeof(*start),
5593 ftrace_cmp_ips, NULL);
5595 start_pg = ftrace_allocate_pages(count);
5599 mutex_lock(&ftrace_lock);
5602 * Core and each module needs their own pages, as
5603 * modules will free them when they are removed.
5604 * Force a new page to be allocated for modules.
5607 WARN_ON(ftrace_pages || ftrace_pages_start);
5608 /* First initialization */
5609 ftrace_pages = ftrace_pages_start = start_pg;
5614 if (WARN_ON(ftrace_pages->next)) {
5615 /* Hmm, we have free pages? */
5616 while (ftrace_pages->next)
5617 ftrace_pages = ftrace_pages->next;
5620 ftrace_pages->next = start_pg;
5626 addr = ftrace_call_adjust(*p++);
5628 * Some architecture linkers will pad between
5629 * the different mcount_loc sections of different
5630 * object files to satisfy alignments.
5631 * Skip any NULL pointers.
5636 if (pg->index == pg->size) {
5637 /* We should have allocated enough */
5638 if (WARN_ON(!pg->next))
5643 rec = &pg->records[pg->index++];
5647 /* We should have used all pages */
5650 /* Assign the last page to ftrace_pages */
5654 * We only need to disable interrupts on start up
5655 * because we are modifying code that an interrupt
5656 * may execute, and the modification is not atomic.
5657 * But for modules, nothing runs the code we modify
5658 * until we are finished with it, and there's no
5659 * reason to cause large interrupt latencies while we do it.
5662 local_irq_save(flags);
5663 ftrace_update_code(mod, start_pg);
5665 local_irq_restore(flags);
5668 mutex_unlock(&ftrace_lock);
5673 struct ftrace_mod_func {
5674 struct list_head list;
5680 struct ftrace_mod_map {
5681 struct rcu_head rcu;
5682 struct list_head list;
5684 unsigned long start_addr;
5685 unsigned long end_addr;
5686 struct list_head funcs;
5687 unsigned int num_funcs;
5690 #ifdef CONFIG_MODULES
5692 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5694 static LIST_HEAD(ftrace_mod_maps);
5696 static int referenced_filters(struct dyn_ftrace *rec)
5698 struct ftrace_ops *ops;
5701 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5702 if (ops_references_rec(ops, rec))
5710 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5712 struct ftrace_func_entry *entry;
5713 struct dyn_ftrace *rec;
5716 if (ftrace_hash_empty(hash))
5719 for (i = 0; i < pg->index; i++) {
5720 rec = &pg->records[i];
5721 entry = __ftrace_lookup_ip(hash, rec->ip);
5723 * Do not allow this rec to match again.
5724 * Yeah, it may waste some memory, but will be removed
5725 * if/when the hash is modified again.
5732 /* Clear any records from hashs */
5733 static void clear_mod_from_hashes(struct ftrace_page *pg)
5735 struct trace_array *tr;
5737 mutex_lock(&trace_types_lock);
5738 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5739 if (!tr->ops || !tr->ops->func_hash)
5741 mutex_lock(&tr->ops->func_hash->regex_lock);
5742 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5743 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5744 mutex_unlock(&tr->ops->func_hash->regex_lock);
5746 mutex_unlock(&trace_types_lock);
5749 static void ftrace_free_mod_map(struct rcu_head *rcu)
5751 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5752 struct ftrace_mod_func *mod_func;
5753 struct ftrace_mod_func *n;
5755 /* All the contents of mod_map are now not visible to readers */
5756 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5757 kfree(mod_func->name);
5758 list_del(&mod_func->list);
5765 void ftrace_release_mod(struct module *mod)
5767 struct ftrace_mod_map *mod_map;
5768 struct ftrace_mod_map *n;
5769 struct dyn_ftrace *rec;
5770 struct ftrace_page **last_pg;
5771 struct ftrace_page *tmp_page = NULL;
5772 struct ftrace_page *pg;
5775 mutex_lock(&ftrace_lock);
5777 if (ftrace_disabled)
5780 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5781 if (mod_map->mod == mod) {
5782 list_del_rcu(&mod_map->list);
5783 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
5789 * Each module has its own ftrace_pages, remove
5790 * them from the list.
5792 last_pg = &ftrace_pages_start;
5793 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5794 rec = &pg->records[0];
5795 if (within_module_core(rec->ip, mod) ||
5796 within_module_init(rec->ip, mod)) {
5798 * As core pages are first, the first
5799 * page should never be a module page.
5801 if (WARN_ON(pg == ftrace_pages_start))
5804 /* Check if we are deleting the last page */
5805 if (pg == ftrace_pages)
5806 ftrace_pages = next_to_ftrace_page(last_pg);
5808 ftrace_update_tot_cnt -= pg->index;
5809 *last_pg = pg->next;
5811 pg->next = tmp_page;
5814 last_pg = &pg->next;
5817 mutex_unlock(&ftrace_lock);
5819 for (pg = tmp_page; pg; pg = tmp_page) {
5821 /* Needs to be called outside of ftrace_lock */
5822 clear_mod_from_hashes(pg);
5824 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5825 free_pages((unsigned long)pg->records, order);
5826 tmp_page = pg->next;
5831 void ftrace_module_enable(struct module *mod)
5833 struct dyn_ftrace *rec;
5834 struct ftrace_page *pg;
5836 mutex_lock(&ftrace_lock);
5838 if (ftrace_disabled)
5842 * If the tracing is enabled, go ahead and enable the record.
5844 * The reason not to enable the record immediately is the
5845 * inherent check of ftrace_make_nop/ftrace_make_call for
5846 * correct previous instructions. Making first the NOP
5847 * conversion puts the module to the correct state, thus
5848 * passing the ftrace_make_call check.
5850 * We also delay this to after the module code already set the
5851 * text to read-only, as we now need to set it back to read-write
5852 * so that we can modify the text.
5854 if (ftrace_start_up)
5855 ftrace_arch_code_modify_prepare();
5857 do_for_each_ftrace_rec(pg, rec) {
5860 * do_for_each_ftrace_rec() is a double loop.
5861 * module text shares the pg. If a record is
5862 * not part of this module, then skip this pg,
5863 * which the "break" will do.
5865 if (!within_module_core(rec->ip, mod) &&
5866 !within_module_init(rec->ip, mod))
5872 * When adding a module, we need to check if tracers are
5873 * currently enabled and if they are, and can trace this record,
5874 * we need to enable the module functions as well as update the
5875 * reference counts for those function records.
5877 if (ftrace_start_up)
5878 cnt += referenced_filters(rec);
5880 /* This clears FTRACE_FL_DISABLED */
5883 if (ftrace_start_up && cnt) {
5884 int failed = __ftrace_replace_code(rec, 1);
5886 ftrace_bug(failed, rec);
5891 } while_for_each_ftrace_rec();
5894 if (ftrace_start_up)
5895 ftrace_arch_code_modify_post_process();
5898 mutex_unlock(&ftrace_lock);
5900 process_cached_mods(mod->name);
5903 void ftrace_module_init(struct module *mod)
5905 if (ftrace_disabled || !mod->num_ftrace_callsites)
5908 ftrace_process_locs(mod, mod->ftrace_callsites,
5909 mod->ftrace_callsites + mod->num_ftrace_callsites);
5912 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5913 struct dyn_ftrace *rec)
5915 struct ftrace_mod_func *mod_func;
5916 unsigned long symsize;
5917 unsigned long offset;
5918 char str[KSYM_SYMBOL_LEN];
5922 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5926 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5930 mod_func->name = kstrdup(str, GFP_KERNEL);
5931 if (!mod_func->name) {
5936 mod_func->ip = rec->ip - offset;
5937 mod_func->size = symsize;
5939 mod_map->num_funcs++;
5941 list_add_rcu(&mod_func->list, &mod_map->funcs);
5944 static struct ftrace_mod_map *
5945 allocate_ftrace_mod_map(struct module *mod,
5946 unsigned long start, unsigned long end)
5948 struct ftrace_mod_map *mod_map;
5950 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5955 mod_map->start_addr = start;
5956 mod_map->end_addr = end;
5957 mod_map->num_funcs = 0;
5959 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5961 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5967 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5968 unsigned long addr, unsigned long *size,
5969 unsigned long *off, char *sym)
5971 struct ftrace_mod_func *found_func = NULL;
5972 struct ftrace_mod_func *mod_func;
5974 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5975 if (addr >= mod_func->ip &&
5976 addr < mod_func->ip + mod_func->size) {
5977 found_func = mod_func;
5984 *size = found_func->size;
5986 *off = addr - found_func->ip;
5988 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5990 return found_func->name;
5997 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
5998 unsigned long *off, char **modname, char *sym)
6000 struct ftrace_mod_map *mod_map;
6001 const char *ret = NULL;
6003 /* mod_map is freed via call_rcu() */
6005 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6006 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6009 *modname = mod_map->mod->name;
6018 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6019 char *type, char *name,
6020 char *module_name, int *exported)
6022 struct ftrace_mod_map *mod_map;
6023 struct ftrace_mod_func *mod_func;
6026 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6028 if (symnum >= mod_map->num_funcs) {
6029 symnum -= mod_map->num_funcs;
6033 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6039 *value = mod_func->ip;
6041 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6042 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6055 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6056 struct dyn_ftrace *rec) { }
6057 static inline struct ftrace_mod_map *
6058 allocate_ftrace_mod_map(struct module *mod,
6059 unsigned long start, unsigned long end)
6063 #endif /* CONFIG_MODULES */
6065 struct ftrace_init_func {
6066 struct list_head list;
6070 /* Clear any init ips from hashes */
6072 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6074 struct ftrace_func_entry *entry;
6076 entry = ftrace_lookup_ip(hash, func->ip);
6078 * Do not allow this rec to match again.
6079 * Yeah, it may waste some memory, but will be removed
6080 * if/when the hash is modified again.
6087 clear_func_from_hashes(struct ftrace_init_func *func)
6089 struct trace_array *tr;
6091 mutex_lock(&trace_types_lock);
6092 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6093 if (!tr->ops || !tr->ops->func_hash)
6095 mutex_lock(&tr->ops->func_hash->regex_lock);
6096 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6097 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6098 mutex_unlock(&tr->ops->func_hash->regex_lock);
6100 mutex_unlock(&trace_types_lock);
6103 static void add_to_clear_hash_list(struct list_head *clear_list,
6104 struct dyn_ftrace *rec)
6106 struct ftrace_init_func *func;
6108 func = kmalloc(sizeof(*func), GFP_KERNEL);
6110 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6115 list_add(&func->list, clear_list);
6118 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
6120 unsigned long start = (unsigned long)(start_ptr);
6121 unsigned long end = (unsigned long)(end_ptr);
6122 struct ftrace_page **last_pg = &ftrace_pages_start;
6123 struct ftrace_page *pg;
6124 struct dyn_ftrace *rec;
6125 struct dyn_ftrace key;
6126 struct ftrace_mod_map *mod_map = NULL;
6127 struct ftrace_init_func *func, *func_next;
6128 struct list_head clear_hash;
6131 INIT_LIST_HEAD(&clear_hash);
6134 key.flags = end; /* overload flags, as it is unsigned long */
6136 mutex_lock(&ftrace_lock);
6139 * If we are freeing module init memory, then check if
6140 * any tracer is active. If so, we need to save a mapping of
6141 * the module functions being freed with the address.
6143 if (mod && ftrace_ops_list != &ftrace_list_end)
6144 mod_map = allocate_ftrace_mod_map(mod, start, end);
6146 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6147 if (end < pg->records[0].ip ||
6148 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6151 rec = bsearch(&key, pg->records, pg->index,
6152 sizeof(struct dyn_ftrace),
6157 /* rec will be cleared from hashes after ftrace_lock unlock */
6158 add_to_clear_hash_list(&clear_hash, rec);
6161 save_ftrace_mod_rec(mod_map, rec);
6164 ftrace_update_tot_cnt--;
6166 *last_pg = pg->next;
6167 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6168 free_pages((unsigned long)pg->records, order);
6170 pg = container_of(last_pg, struct ftrace_page, next);
6175 memmove(rec, rec + 1,
6176 (pg->index - (rec - pg->records)) * sizeof(*rec));
6177 /* More than one function may be in this block */
6180 mutex_unlock(&ftrace_lock);
6182 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6183 clear_func_from_hashes(func);
6188 void __init ftrace_free_init_mem(void)
6190 void *start = (void *)(&__init_begin);
6191 void *end = (void *)(&__init_end);
6193 ftrace_free_mem(NULL, start, end);
6196 void __init ftrace_init(void)
6198 extern unsigned long __start_mcount_loc[];
6199 extern unsigned long __stop_mcount_loc[];
6200 unsigned long count, flags;
6203 local_irq_save(flags);
6204 ret = ftrace_dyn_arch_init();
6205 local_irq_restore(flags);
6209 count = __stop_mcount_loc - __start_mcount_loc;
6211 pr_info("ftrace: No functions to be traced?\n");
6215 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6216 count, count / ENTRIES_PER_PAGE + 1);
6218 last_ftrace_enabled = ftrace_enabled = 1;
6220 ret = ftrace_process_locs(NULL,
6224 set_ftrace_early_filters();
6228 ftrace_disabled = 1;
6231 /* Do nothing if arch does not support this */
6232 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6236 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6238 arch_ftrace_update_trampoline(ops);
6241 void ftrace_init_trace_array(struct trace_array *tr)
6243 INIT_LIST_HEAD(&tr->func_probes);
6244 INIT_LIST_HEAD(&tr->mod_trace);
6245 INIT_LIST_HEAD(&tr->mod_notrace);
6249 struct ftrace_ops global_ops = {
6250 .func = ftrace_stub,
6251 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6252 FTRACE_OPS_FL_INITIALIZED |
6256 static int __init ftrace_nodyn_init(void)
6261 core_initcall(ftrace_nodyn_init);
6263 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6264 static inline void ftrace_startup_enable(int command) { }
6265 static inline void ftrace_startup_all(int command) { }
6267 # define ftrace_startup_sysctl() do { } while (0)
6268 # define ftrace_shutdown_sysctl() do { } while (0)
6270 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6274 #endif /* CONFIG_DYNAMIC_FTRACE */
6276 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6278 tr->ops = &global_ops;
6279 tr->ops->private = tr;
6280 ftrace_init_trace_array(tr);
6283 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6285 /* If we filter on pids, update to use the pid function */
6286 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6287 if (WARN_ON(tr->ops->func != ftrace_stub))
6288 printk("ftrace ops had %pS for function\n",
6291 tr->ops->func = func;
6292 tr->ops->private = tr;
6295 void ftrace_reset_array_ops(struct trace_array *tr)
6297 tr->ops->func = ftrace_stub;
6300 static nokprobe_inline void
6301 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6302 struct ftrace_ops *ignored, struct pt_regs *regs)
6304 struct ftrace_ops *op;
6307 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6312 * Some of the ops may be dynamically allocated,
6313 * they must be freed after a synchronize_rcu().
6315 preempt_disable_notrace();
6317 do_for_each_ftrace_op(op, ftrace_ops_list) {
6318 /* Stub functions don't need to be called nor tested */
6319 if (op->flags & FTRACE_OPS_FL_STUB)
6322 * Check the following for each ops before calling their func:
6323 * if RCU flag is set, then rcu_is_watching() must be true
6324 * if PER_CPU is set, then ftrace_function_local_disable()
6326 * Otherwise test if the ip matches the ops filter
6328 * If any of the above fails then the op->func() is not executed.
6330 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6331 ftrace_ops_test(op, ip, regs)) {
6332 if (FTRACE_WARN_ON(!op->func)) {
6333 pr_warn("op=%p %pS\n", op, op);
6336 op->func(ip, parent_ip, op, regs);
6338 } while_for_each_ftrace_op(op);
6340 preempt_enable_notrace();
6341 trace_clear_recursion(bit);
6345 * Some archs only support passing ip and parent_ip. Even though
6346 * the list function ignores the op parameter, we do not want any
6347 * C side effects, where a function is called without the caller
6348 * sending a third parameter.
6349 * Archs are to support both the regs and ftrace_ops at the same time.
6350 * If they support ftrace_ops, it is assumed they support regs.
6351 * If call backs want to use regs, they must either check for regs
6352 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6353 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6354 * An architecture can pass partial regs with ftrace_ops and still
6355 * set the ARCH_SUPPORTS_FTRACE_OPS.
6357 #if ARCH_SUPPORTS_FTRACE_OPS
6358 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6359 struct ftrace_ops *op, struct pt_regs *regs)
6361 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6363 NOKPROBE_SYMBOL(ftrace_ops_list_func);
6365 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6367 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6369 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
6373 * If there's only one function registered but it does not support
6374 * recursion, needs RCU protection and/or requires per cpu handling, then
6375 * this function will be called by the mcount trampoline.
6377 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6378 struct ftrace_ops *op, struct pt_regs *regs)
6382 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6385 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6389 preempt_disable_notrace();
6391 op->func(ip, parent_ip, op, regs);
6393 preempt_enable_notrace();
6394 trace_clear_recursion(bit);
6396 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
6399 * ftrace_ops_get_func - get the function a trampoline should call
6400 * @ops: the ops to get the function for
6402 * Normally the mcount trampoline will call the ops->func, but there
6403 * are times that it should not. For example, if the ops does not
6404 * have its own recursion protection, then it should call the
6405 * ftrace_ops_assist_func() instead.
6407 * Returns the function that the trampoline should call for @ops.
6409 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6412 * If the function does not handle recursion, needs to be RCU safe,
6413 * or does per cpu logic, then we need to call the assist handler.
6415 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6416 ops->flags & FTRACE_OPS_FL_RCU)
6417 return ftrace_ops_assist_func;
6423 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6424 struct task_struct *prev, struct task_struct *next)
6426 struct trace_array *tr = data;
6427 struct trace_pid_list *pid_list;
6429 pid_list = rcu_dereference_sched(tr->function_pids);
6431 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6432 trace_ignore_this_task(pid_list, next));
6436 ftrace_pid_follow_sched_process_fork(void *data,
6437 struct task_struct *self,
6438 struct task_struct *task)
6440 struct trace_pid_list *pid_list;
6441 struct trace_array *tr = data;
6443 pid_list = rcu_dereference_sched(tr->function_pids);
6444 trace_filter_add_remove_task(pid_list, self, task);
6448 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6450 struct trace_pid_list *pid_list;
6451 struct trace_array *tr = data;
6453 pid_list = rcu_dereference_sched(tr->function_pids);
6454 trace_filter_add_remove_task(pid_list, NULL, task);
6457 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6460 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6462 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6465 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6467 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6472 static void clear_ftrace_pids(struct trace_array *tr)
6474 struct trace_pid_list *pid_list;
6477 pid_list = rcu_dereference_protected(tr->function_pids,
6478 lockdep_is_held(&ftrace_lock));
6482 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6484 for_each_possible_cpu(cpu)
6485 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6487 rcu_assign_pointer(tr->function_pids, NULL);
6489 /* Wait till all users are no longer using pid filtering */
6492 trace_free_pid_list(pid_list);
6495 void ftrace_clear_pids(struct trace_array *tr)
6497 mutex_lock(&ftrace_lock);
6499 clear_ftrace_pids(tr);
6501 mutex_unlock(&ftrace_lock);
6504 static void ftrace_pid_reset(struct trace_array *tr)
6506 mutex_lock(&ftrace_lock);
6507 clear_ftrace_pids(tr);
6509 ftrace_update_pid_func();
6510 ftrace_startup_all(0);
6512 mutex_unlock(&ftrace_lock);
6515 /* Greater than any max PID */
6516 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6518 static void *fpid_start(struct seq_file *m, loff_t *pos)
6521 struct trace_pid_list *pid_list;
6522 struct trace_array *tr = m->private;
6524 mutex_lock(&ftrace_lock);
6525 rcu_read_lock_sched();
6527 pid_list = rcu_dereference_sched(tr->function_pids);
6530 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6532 return trace_pid_start(pid_list, pos);
6535 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6537 struct trace_array *tr = m->private;
6538 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6540 if (v == FTRACE_NO_PIDS)
6543 return trace_pid_next(pid_list, v, pos);
6546 static void fpid_stop(struct seq_file *m, void *p)
6549 rcu_read_unlock_sched();
6550 mutex_unlock(&ftrace_lock);
6553 static int fpid_show(struct seq_file *m, void *v)
6555 if (v == FTRACE_NO_PIDS) {
6556 seq_puts(m, "no pid\n");
6560 return trace_pid_show(m, v);
6563 static const struct seq_operations ftrace_pid_sops = {
6564 .start = fpid_start,
6571 ftrace_pid_open(struct inode *inode, struct file *file)
6573 struct trace_array *tr = inode->i_private;
6577 ret = tracing_check_open_get_tr(tr);
6581 if ((file->f_mode & FMODE_WRITE) &&
6582 (file->f_flags & O_TRUNC))
6583 ftrace_pid_reset(tr);
6585 ret = seq_open(file, &ftrace_pid_sops);
6587 trace_array_put(tr);
6589 m = file->private_data;
6590 /* copy tr over to seq ops */
6597 static void ignore_task_cpu(void *data)
6599 struct trace_array *tr = data;
6600 struct trace_pid_list *pid_list;
6603 * This function is called by on_each_cpu() while the
6604 * event_mutex is held.
6606 pid_list = rcu_dereference_protected(tr->function_pids,
6607 mutex_is_locked(&ftrace_lock));
6609 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6610 trace_ignore_this_task(pid_list, current));
6614 ftrace_pid_write(struct file *filp, const char __user *ubuf,
6615 size_t cnt, loff_t *ppos)
6617 struct seq_file *m = filp->private_data;
6618 struct trace_array *tr = m->private;
6619 struct trace_pid_list *filtered_pids = NULL;
6620 struct trace_pid_list *pid_list;
6626 mutex_lock(&ftrace_lock);
6628 filtered_pids = rcu_dereference_protected(tr->function_pids,
6629 lockdep_is_held(&ftrace_lock));
6631 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6635 rcu_assign_pointer(tr->function_pids, pid_list);
6637 if (filtered_pids) {
6639 trace_free_pid_list(filtered_pids);
6640 } else if (pid_list) {
6641 /* Register a probe to set whether to ignore the tracing of a task */
6642 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6646 * Ignoring of pids is done at task switch. But we have to
6647 * check for those tasks that are currently running.
6648 * Always do this in case a pid was appended or removed.
6650 on_each_cpu(ignore_task_cpu, tr, 1);
6652 ftrace_update_pid_func();
6653 ftrace_startup_all(0);
6655 mutex_unlock(&ftrace_lock);
6664 ftrace_pid_release(struct inode *inode, struct file *file)
6666 struct trace_array *tr = inode->i_private;
6668 trace_array_put(tr);
6670 return seq_release(inode, file);
6673 static const struct file_operations ftrace_pid_fops = {
6674 .open = ftrace_pid_open,
6675 .write = ftrace_pid_write,
6677 .llseek = tracing_lseek,
6678 .release = ftrace_pid_release,
6681 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
6683 trace_create_file("set_ftrace_pid", 0644, d_tracer,
6684 tr, &ftrace_pid_fops);
6687 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6688 struct dentry *d_tracer)
6690 /* Only the top level directory has the dyn_tracefs and profile */
6691 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6693 ftrace_init_dyn_tracefs(d_tracer);
6694 ftrace_profile_tracefs(d_tracer);
6698 * ftrace_kill - kill ftrace
6700 * This function should be used by panic code. It stops ftrace
6701 * but in a not so nice way. If you need to simply kill ftrace
6702 * from a non-atomic section, use ftrace_kill.
6704 void ftrace_kill(void)
6706 ftrace_disabled = 1;
6708 ftrace_trace_function = ftrace_stub;
6712 * Test if ftrace is dead or not.
6714 int ftrace_is_dead(void)
6716 return ftrace_disabled;
6720 * register_ftrace_function - register a function for profiling
6721 * @ops - ops structure that holds the function for profiling.
6723 * Register a function to be called by all functions in the
6726 * Note: @ops->func and all the functions it calls must be labeled
6727 * with "notrace", otherwise it will go into a
6730 int register_ftrace_function(struct ftrace_ops *ops)
6734 ftrace_ops_init(ops);
6736 mutex_lock(&ftrace_lock);
6738 ret = ftrace_startup(ops, 0);
6740 mutex_unlock(&ftrace_lock);
6744 EXPORT_SYMBOL_GPL(register_ftrace_function);
6747 * unregister_ftrace_function - unregister a function for profiling.
6748 * @ops - ops structure that holds the function to unregister
6750 * Unregister a function that was added to be called by ftrace profiling.
6752 int unregister_ftrace_function(struct ftrace_ops *ops)
6756 mutex_lock(&ftrace_lock);
6757 ret = ftrace_shutdown(ops, 0);
6758 mutex_unlock(&ftrace_lock);
6762 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
6764 static bool is_permanent_ops_registered(void)
6766 struct ftrace_ops *op;
6768 do_for_each_ftrace_op(op, ftrace_ops_list) {
6769 if (op->flags & FTRACE_OPS_FL_PERMANENT)
6771 } while_for_each_ftrace_op(op);
6777 ftrace_enable_sysctl(struct ctl_table *table, int write,
6778 void __user *buffer, size_t *lenp,
6783 mutex_lock(&ftrace_lock);
6785 if (unlikely(ftrace_disabled))
6788 ret = proc_dointvec(table, write, buffer, lenp, ppos);
6790 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
6793 if (ftrace_enabled) {
6795 /* we are starting ftrace again */
6796 if (rcu_dereference_protected(ftrace_ops_list,
6797 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
6798 update_ftrace_function();
6800 ftrace_startup_sysctl();
6803 if (is_permanent_ops_registered()) {
6804 ftrace_enabled = true;
6809 /* stopping ftrace calls (just send to ftrace_stub) */
6810 ftrace_trace_function = ftrace_stub;
6812 ftrace_shutdown_sysctl();
6815 last_ftrace_enabled = !!ftrace_enabled;
6817 mutex_unlock(&ftrace_lock);