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_DEFAULT_BITS 10
66 #define FTRACE_HASH_MAX_BITS 12
68 #ifdef CONFIG_DYNAMIC_FTRACE
69 #define INIT_OPS_HASH(opsname) \
70 .func_hash = &opsname.local_hash, \
71 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
73 #define INIT_OPS_HASH(opsname)
77 FTRACE_MODIFY_ENABLE_FL = (1 << 0),
78 FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
81 struct ftrace_ops ftrace_list_end __read_mostly = {
83 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
84 INIT_OPS_HASH(ftrace_list_end)
87 /* ftrace_enabled is a method to turn ftrace on or off */
88 int ftrace_enabled __read_mostly;
89 static int last_ftrace_enabled;
91 /* Current function tracing op */
92 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
93 /* What to set function_trace_op to */
94 static struct ftrace_ops *set_function_trace_op;
96 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
98 struct trace_array *tr;
100 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
105 return tr->function_pids != NULL || tr->function_no_pids != NULL;
108 static void ftrace_update_trampoline(struct ftrace_ops *ops);
111 * ftrace_disabled is set when an anomaly is discovered.
112 * ftrace_disabled is much stronger than ftrace_enabled.
114 static int ftrace_disabled __read_mostly;
116 DEFINE_MUTEX(ftrace_lock);
118 struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
119 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
120 struct ftrace_ops global_ops;
122 #if ARCH_SUPPORTS_FTRACE_OPS
123 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
124 struct ftrace_ops *op, struct pt_regs *regs);
126 /* See comment below, where ftrace_ops_list_func is defined */
127 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
128 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
131 static inline void ftrace_ops_init(struct ftrace_ops *ops)
133 #ifdef CONFIG_DYNAMIC_FTRACE
134 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
135 mutex_init(&ops->local_hash.regex_lock);
136 ops->func_hash = &ops->local_hash;
137 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
142 #define FTRACE_PID_IGNORE -1
143 #define FTRACE_PID_TRACE -2
145 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
146 struct ftrace_ops *op, struct pt_regs *regs)
148 struct trace_array *tr = op->private;
152 pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid);
153 if (pid == FTRACE_PID_IGNORE)
155 if (pid != FTRACE_PID_TRACE &&
160 op->saved_func(ip, parent_ip, op, regs);
163 static void ftrace_sync(struct work_struct *work)
166 * This function is just a stub to implement a hard force
167 * of synchronize_rcu(). This requires synchronizing
168 * tasks even in userspace and idle.
170 * Yes, function tracing is rude.
174 static void ftrace_sync_ipi(void *data)
176 /* Probably not needed, but do it anyway */
180 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
183 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
184 * then it needs to call the list anyway.
186 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
187 FTRACE_FORCE_LIST_FUNC)
188 return ftrace_ops_list_func;
190 return ftrace_ops_get_func(ops);
193 static void update_ftrace_function(void)
198 * Prepare the ftrace_ops that the arch callback will use.
199 * If there's only one ftrace_ops registered, the ftrace_ops_list
200 * will point to the ops we want.
202 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
203 lockdep_is_held(&ftrace_lock));
205 /* If there's no ftrace_ops registered, just call the stub function */
206 if (set_function_trace_op == &ftrace_list_end) {
210 * If we are at the end of the list and this ops is
211 * recursion safe and not dynamic and the arch supports passing ops,
212 * then have the mcount trampoline call the function directly.
214 } else if (rcu_dereference_protected(ftrace_ops_list->next,
215 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
216 func = ftrace_ops_get_list_func(ftrace_ops_list);
219 /* Just use the default ftrace_ops */
220 set_function_trace_op = &ftrace_list_end;
221 func = ftrace_ops_list_func;
224 update_function_graph_func();
226 /* If there's no change, then do nothing more here */
227 if (ftrace_trace_function == func)
231 * If we are using the list function, it doesn't care
232 * about the function_trace_ops.
234 if (func == ftrace_ops_list_func) {
235 ftrace_trace_function = func;
237 * Don't even bother setting function_trace_ops,
238 * it would be racy to do so anyway.
243 #ifndef CONFIG_DYNAMIC_FTRACE
245 * For static tracing, we need to be a bit more careful.
246 * The function change takes affect immediately. Thus,
247 * we need to coorditate the setting of the function_trace_ops
248 * with the setting of the ftrace_trace_function.
250 * Set the function to the list ops, which will call the
251 * function we want, albeit indirectly, but it handles the
252 * ftrace_ops and doesn't depend on function_trace_op.
254 ftrace_trace_function = ftrace_ops_list_func;
256 * Make sure all CPUs see this. Yes this is slow, but static
257 * tracing is slow and nasty to have enabled.
259 schedule_on_each_cpu(ftrace_sync);
260 /* Now all cpus are using the list ops. */
261 function_trace_op = set_function_trace_op;
262 /* Make sure the function_trace_op is visible on all CPUs */
264 /* Nasty way to force a rmb on all cpus */
265 smp_call_function(ftrace_sync_ipi, NULL, 1);
266 /* OK, we are all set to update the ftrace_trace_function now! */
267 #endif /* !CONFIG_DYNAMIC_FTRACE */
269 ftrace_trace_function = func;
272 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
273 struct ftrace_ops *ops)
275 rcu_assign_pointer(ops->next, *list);
278 * We are entering ops into the list but another
279 * CPU might be walking that list. We need to make sure
280 * the ops->next pointer is valid before another CPU sees
281 * the ops pointer included into the list.
283 rcu_assign_pointer(*list, ops);
286 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
287 struct ftrace_ops *ops)
289 struct ftrace_ops **p;
292 * If we are removing the last function, then simply point
293 * to the ftrace_stub.
295 if (rcu_dereference_protected(*list,
296 lockdep_is_held(&ftrace_lock)) == ops &&
297 rcu_dereference_protected(ops->next,
298 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
299 *list = &ftrace_list_end;
303 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
314 static void ftrace_update_trampoline(struct ftrace_ops *ops);
316 int __register_ftrace_function(struct ftrace_ops *ops)
318 if (ops->flags & FTRACE_OPS_FL_DELETED)
321 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
324 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
326 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
327 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
328 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
330 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
331 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
334 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
335 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
337 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
340 if (!core_kernel_data((unsigned long)ops))
341 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
343 add_ftrace_ops(&ftrace_ops_list, ops);
345 /* Always save the function, and reset at unregistering */
346 ops->saved_func = ops->func;
348 if (ftrace_pids_enabled(ops))
349 ops->func = ftrace_pid_func;
351 ftrace_update_trampoline(ops);
354 update_ftrace_function();
359 int __unregister_ftrace_function(struct ftrace_ops *ops)
363 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
366 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
372 update_ftrace_function();
374 ops->func = ops->saved_func;
379 static void ftrace_update_pid_func(void)
381 struct ftrace_ops *op;
383 /* Only do something if we are tracing something */
384 if (ftrace_trace_function == ftrace_stub)
387 do_for_each_ftrace_op(op, ftrace_ops_list) {
388 if (op->flags & FTRACE_OPS_FL_PID) {
389 op->func = ftrace_pids_enabled(op) ?
390 ftrace_pid_func : op->saved_func;
391 ftrace_update_trampoline(op);
393 } while_for_each_ftrace_op(op);
395 update_ftrace_function();
398 #ifdef CONFIG_FUNCTION_PROFILER
399 struct ftrace_profile {
400 struct hlist_node node;
402 unsigned long counter;
403 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
404 unsigned long long time;
405 unsigned long long time_squared;
409 struct ftrace_profile_page {
410 struct ftrace_profile_page *next;
412 struct ftrace_profile records[];
415 struct ftrace_profile_stat {
417 struct hlist_head *hash;
418 struct ftrace_profile_page *pages;
419 struct ftrace_profile_page *start;
420 struct tracer_stat stat;
423 #define PROFILE_RECORDS_SIZE \
424 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
426 #define PROFILES_PER_PAGE \
427 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
429 static int ftrace_profile_enabled __read_mostly;
431 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
432 static DEFINE_MUTEX(ftrace_profile_lock);
434 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
436 #define FTRACE_PROFILE_HASH_BITS 10
437 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
440 function_stat_next(void *v, int idx)
442 struct ftrace_profile *rec = v;
443 struct ftrace_profile_page *pg;
445 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
451 if ((void *)rec >= (void *)&pg->records[pg->index]) {
455 rec = &pg->records[0];
463 static void *function_stat_start(struct tracer_stat *trace)
465 struct ftrace_profile_stat *stat =
466 container_of(trace, struct ftrace_profile_stat, stat);
468 if (!stat || !stat->start)
471 return function_stat_next(&stat->start->records[0], 0);
474 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
475 /* function graph compares on total time */
476 static int function_stat_cmp(const void *p1, const void *p2)
478 const struct ftrace_profile *a = p1;
479 const struct ftrace_profile *b = p2;
481 if (a->time < b->time)
483 if (a->time > b->time)
489 /* not function graph compares against hits */
490 static int function_stat_cmp(const void *p1, const void *p2)
492 const struct ftrace_profile *a = p1;
493 const struct ftrace_profile *b = p2;
495 if (a->counter < b->counter)
497 if (a->counter > b->counter)
504 static int function_stat_headers(struct seq_file *m)
506 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
507 seq_puts(m, " Function "
510 "--- ---- --- ---\n");
512 seq_puts(m, " Function Hit\n"
518 static int function_stat_show(struct seq_file *m, void *v)
520 struct ftrace_profile *rec = v;
521 char str[KSYM_SYMBOL_LEN];
523 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
524 static struct trace_seq s;
525 unsigned long long avg;
526 unsigned long long stddev;
528 mutex_lock(&ftrace_profile_lock);
530 /* we raced with function_profile_reset() */
531 if (unlikely(rec->counter == 0)) {
536 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
537 avg = div64_ul(rec->time, rec->counter);
538 if (tracing_thresh && (avg < tracing_thresh))
542 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
543 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
545 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
548 /* Sample standard deviation (s^2) */
549 if (rec->counter <= 1)
553 * Apply Welford's method:
554 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
556 stddev = rec->counter * rec->time_squared -
557 rec->time * rec->time;
560 * Divide only 1000 for ns^2 -> us^2 conversion.
561 * trace_print_graph_duration will divide 1000 again.
563 stddev = div64_ul(stddev,
564 rec->counter * (rec->counter - 1) * 1000);
568 trace_print_graph_duration(rec->time, &s);
569 trace_seq_puts(&s, " ");
570 trace_print_graph_duration(avg, &s);
571 trace_seq_puts(&s, " ");
572 trace_print_graph_duration(stddev, &s);
573 trace_print_seq(m, &s);
577 mutex_unlock(&ftrace_profile_lock);
582 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
584 struct ftrace_profile_page *pg;
586 pg = stat->pages = stat->start;
589 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
594 memset(stat->hash, 0,
595 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
598 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
600 struct ftrace_profile_page *pg;
605 /* If we already allocated, do nothing */
609 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
613 #ifdef CONFIG_DYNAMIC_FTRACE
614 functions = ftrace_update_tot_cnt;
617 * We do not know the number of functions that exist because
618 * dynamic tracing is what counts them. With past experience
619 * we have around 20K functions. That should be more than enough.
620 * It is highly unlikely we will execute every function in
626 pg = stat->start = stat->pages;
628 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
630 for (i = 1; i < pages; i++) {
631 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
642 unsigned long tmp = (unsigned long)pg;
654 static int ftrace_profile_init_cpu(int cpu)
656 struct ftrace_profile_stat *stat;
659 stat = &per_cpu(ftrace_profile_stats, cpu);
662 /* If the profile is already created, simply reset it */
663 ftrace_profile_reset(stat);
668 * We are profiling all functions, but usually only a few thousand
669 * functions are hit. We'll make a hash of 1024 items.
671 size = FTRACE_PROFILE_HASH_SIZE;
673 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
678 /* Preallocate the function profiling pages */
679 if (ftrace_profile_pages_init(stat) < 0) {
688 static int ftrace_profile_init(void)
693 for_each_possible_cpu(cpu) {
694 ret = ftrace_profile_init_cpu(cpu);
702 /* interrupts must be disabled */
703 static struct ftrace_profile *
704 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
706 struct ftrace_profile *rec;
707 struct hlist_head *hhd;
710 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
711 hhd = &stat->hash[key];
713 if (hlist_empty(hhd))
716 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
724 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
725 struct ftrace_profile *rec)
729 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
730 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
734 * The memory is already allocated, this simply finds a new record to use.
736 static struct ftrace_profile *
737 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
739 struct ftrace_profile *rec = NULL;
741 /* prevent recursion (from NMIs) */
742 if (atomic_inc_return(&stat->disabled) != 1)
746 * Try to find the function again since an NMI
747 * could have added it
749 rec = ftrace_find_profiled_func(stat, ip);
753 if (stat->pages->index == PROFILES_PER_PAGE) {
754 if (!stat->pages->next)
756 stat->pages = stat->pages->next;
759 rec = &stat->pages->records[stat->pages->index++];
761 ftrace_add_profile(stat, rec);
764 atomic_dec(&stat->disabled);
770 function_profile_call(unsigned long ip, unsigned long parent_ip,
771 struct ftrace_ops *ops, struct pt_regs *regs)
773 struct ftrace_profile_stat *stat;
774 struct ftrace_profile *rec;
777 if (!ftrace_profile_enabled)
780 local_irq_save(flags);
782 stat = this_cpu_ptr(&ftrace_profile_stats);
783 if (!stat->hash || !ftrace_profile_enabled)
786 rec = ftrace_find_profiled_func(stat, ip);
788 rec = ftrace_profile_alloc(stat, ip);
795 local_irq_restore(flags);
798 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
799 static bool fgraph_graph_time = true;
801 void ftrace_graph_graph_time_control(bool enable)
803 fgraph_graph_time = enable;
806 static int profile_graph_entry(struct ftrace_graph_ent *trace)
808 struct ftrace_ret_stack *ret_stack;
810 function_profile_call(trace->func, 0, NULL, NULL);
812 /* If function graph is shutting down, ret_stack can be NULL */
813 if (!current->ret_stack)
816 ret_stack = ftrace_graph_get_ret_stack(current, 0);
818 ret_stack->subtime = 0;
823 static void profile_graph_return(struct ftrace_graph_ret *trace)
825 struct ftrace_ret_stack *ret_stack;
826 struct ftrace_profile_stat *stat;
827 unsigned long long calltime;
828 struct ftrace_profile *rec;
831 local_irq_save(flags);
832 stat = this_cpu_ptr(&ftrace_profile_stats);
833 if (!stat->hash || !ftrace_profile_enabled)
836 /* If the calltime was zero'd ignore it */
837 if (!trace->calltime)
840 calltime = trace->rettime - trace->calltime;
842 if (!fgraph_graph_time) {
844 /* Append this call time to the parent time to subtract */
845 ret_stack = ftrace_graph_get_ret_stack(current, 1);
847 ret_stack->subtime += calltime;
849 ret_stack = ftrace_graph_get_ret_stack(current, 0);
850 if (ret_stack && ret_stack->subtime < calltime)
851 calltime -= ret_stack->subtime;
856 rec = ftrace_find_profiled_func(stat, trace->func);
858 rec->time += calltime;
859 rec->time_squared += calltime * calltime;
863 local_irq_restore(flags);
866 static struct fgraph_ops fprofiler_ops = {
867 .entryfunc = &profile_graph_entry,
868 .retfunc = &profile_graph_return,
871 static int register_ftrace_profiler(void)
873 return register_ftrace_graph(&fprofiler_ops);
876 static void unregister_ftrace_profiler(void)
878 unregister_ftrace_graph(&fprofiler_ops);
881 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
882 .func = function_profile_call,
883 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
884 INIT_OPS_HASH(ftrace_profile_ops)
887 static int register_ftrace_profiler(void)
889 return register_ftrace_function(&ftrace_profile_ops);
892 static void unregister_ftrace_profiler(void)
894 unregister_ftrace_function(&ftrace_profile_ops);
896 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
899 ftrace_profile_write(struct file *filp, const char __user *ubuf,
900 size_t cnt, loff_t *ppos)
905 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
911 mutex_lock(&ftrace_profile_lock);
912 if (ftrace_profile_enabled ^ val) {
914 ret = ftrace_profile_init();
920 ret = register_ftrace_profiler();
925 ftrace_profile_enabled = 1;
927 ftrace_profile_enabled = 0;
929 * unregister_ftrace_profiler calls stop_machine
930 * so this acts like an synchronize_rcu.
932 unregister_ftrace_profiler();
936 mutex_unlock(&ftrace_profile_lock);
944 ftrace_profile_read(struct file *filp, char __user *ubuf,
945 size_t cnt, loff_t *ppos)
947 char buf[64]; /* big enough to hold a number */
950 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
951 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
954 static const struct file_operations ftrace_profile_fops = {
955 .open = tracing_open_generic,
956 .read = ftrace_profile_read,
957 .write = ftrace_profile_write,
958 .llseek = default_llseek,
961 /* used to initialize the real stat files */
962 static struct tracer_stat function_stats __initdata = {
964 .stat_start = function_stat_start,
965 .stat_next = function_stat_next,
966 .stat_cmp = function_stat_cmp,
967 .stat_headers = function_stat_headers,
968 .stat_show = function_stat_show
971 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
973 struct ftrace_profile_stat *stat;
974 struct dentry *entry;
979 for_each_possible_cpu(cpu) {
980 stat = &per_cpu(ftrace_profile_stats, cpu);
982 name = kasprintf(GFP_KERNEL, "function%d", cpu);
985 * The files created are permanent, if something happens
986 * we still do not free memory.
989 "Could not allocate stat file for cpu %d\n",
993 stat->stat = function_stats;
994 stat->stat.name = name;
995 ret = register_stat_tracer(&stat->stat);
998 "Could not register function stat for cpu %d\n",
1005 entry = tracefs_create_file("function_profile_enabled", 0644,
1006 d_tracer, NULL, &ftrace_profile_fops);
1008 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1011 #else /* CONFIG_FUNCTION_PROFILER */
1012 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1015 #endif /* CONFIG_FUNCTION_PROFILER */
1017 #ifdef CONFIG_DYNAMIC_FTRACE
1019 static struct ftrace_ops *removed_ops;
1022 * Set when doing a global update, like enabling all recs or disabling them.
1023 * It is not set when just updating a single ftrace_ops.
1025 static bool update_all_ops;
1027 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1028 # error Dynamic ftrace depends on MCOUNT_RECORD
1031 struct ftrace_func_probe {
1032 struct ftrace_probe_ops *probe_ops;
1033 struct ftrace_ops ops;
1034 struct trace_array *tr;
1035 struct list_head list;
1041 * We make these constant because no one should touch them,
1042 * but they are used as the default "empty hash", to avoid allocating
1043 * it all the time. These are in a read only section such that if
1044 * anyone does try to modify it, it will cause an exception.
1046 static const struct hlist_head empty_buckets[1];
1047 static const struct ftrace_hash empty_hash = {
1048 .buckets = (struct hlist_head *)empty_buckets,
1050 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1052 struct ftrace_ops global_ops = {
1053 .func = ftrace_stub,
1054 .local_hash.notrace_hash = EMPTY_HASH,
1055 .local_hash.filter_hash = EMPTY_HASH,
1056 INIT_OPS_HASH(global_ops)
1057 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1058 FTRACE_OPS_FL_INITIALIZED |
1063 * Used by the stack undwinder to know about dynamic ftrace trampolines.
1065 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1067 struct ftrace_ops *op = NULL;
1070 * Some of the ops may be dynamically allocated,
1071 * they are freed after a synchronize_rcu().
1073 preempt_disable_notrace();
1075 do_for_each_ftrace_op(op, ftrace_ops_list) {
1077 * This is to check for dynamically allocated trampolines.
1078 * Trampolines that are in kernel text will have
1079 * core_kernel_text() return true.
1081 if (op->trampoline && op->trampoline_size)
1082 if (addr >= op->trampoline &&
1083 addr < op->trampoline + op->trampoline_size) {
1084 preempt_enable_notrace();
1087 } while_for_each_ftrace_op(op);
1088 preempt_enable_notrace();
1094 * This is used by __kernel_text_address() to return true if the
1095 * address is on a dynamically allocated trampoline that would
1096 * not return true for either core_kernel_text() or
1097 * is_module_text_address().
1099 bool is_ftrace_trampoline(unsigned long addr)
1101 return ftrace_ops_trampoline(addr) != NULL;
1104 struct ftrace_page {
1105 struct ftrace_page *next;
1106 struct dyn_ftrace *records;
1111 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1112 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
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)
1544 static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
1546 struct ftrace_page *pg;
1547 struct dyn_ftrace *rec = NULL;
1548 struct dyn_ftrace key;
1551 key.flags = end; /* overload flags, as it is unsigned long */
1553 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1554 if (end < pg->records[0].ip ||
1555 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1557 rec = bsearch(&key, pg->records, pg->index,
1558 sizeof(struct dyn_ftrace),
1567 * ftrace_location_range - return the first address of a traced location
1568 * if it touches the given ip range
1569 * @start: start of range to search.
1570 * @end: end of range to search (inclusive). @end points to the last byte
1573 * Returns rec->ip if the related ftrace location is a least partly within
1574 * the given address range. That is, the first address of the instruction
1575 * that is either a NOP or call to the function tracer. It checks the ftrace
1576 * internal tables to determine if the address belongs or not.
1578 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1580 struct dyn_ftrace *rec;
1582 rec = lookup_rec(start, end);
1590 * ftrace_location - return true if the ip giving is a traced location
1591 * @ip: the instruction pointer to check
1593 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1594 * That is, the instruction that is either a NOP or call to
1595 * the function tracer. It checks the ftrace internal tables to
1596 * determine if the address belongs or not.
1598 unsigned long ftrace_location(unsigned long ip)
1600 return ftrace_location_range(ip, ip);
1604 * ftrace_text_reserved - return true if range contains an ftrace location
1605 * @start: start of range to search
1606 * @end: end of range to search (inclusive). @end points to the last byte to check.
1608 * Returns 1 if @start and @end contains a ftrace location.
1609 * That is, the instruction that is either a NOP or call to
1610 * the function tracer. It checks the ftrace internal tables to
1611 * determine if the address belongs or not.
1613 int ftrace_text_reserved(const void *start, const void *end)
1617 ret = ftrace_location_range((unsigned long)start,
1618 (unsigned long)end);
1623 /* Test if ops registered to this rec needs regs */
1624 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1626 struct ftrace_ops *ops;
1627 bool keep_regs = false;
1629 for (ops = ftrace_ops_list;
1630 ops != &ftrace_list_end; ops = ops->next) {
1631 /* pass rec in as regs to have non-NULL val */
1632 if (ftrace_ops_test(ops, rec->ip, rec)) {
1633 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1643 static struct ftrace_ops *
1644 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1645 static struct ftrace_ops *
1646 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1648 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1652 struct ftrace_hash *hash;
1653 struct ftrace_hash *other_hash;
1654 struct ftrace_page *pg;
1655 struct dyn_ftrace *rec;
1656 bool update = false;
1660 /* Only update if the ops has been registered */
1661 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1665 * In the filter_hash case:
1666 * If the count is zero, we update all records.
1667 * Otherwise we just update the items in the hash.
1669 * In the notrace_hash case:
1670 * We enable the update in the hash.
1671 * As disabling notrace means enabling the tracing,
1672 * and enabling notrace means disabling, the inc variable
1676 hash = ops->func_hash->filter_hash;
1677 other_hash = ops->func_hash->notrace_hash;
1678 if (ftrace_hash_empty(hash))
1682 hash = ops->func_hash->notrace_hash;
1683 other_hash = ops->func_hash->filter_hash;
1685 * If the notrace hash has no items,
1686 * then there's nothing to do.
1688 if (ftrace_hash_empty(hash))
1692 do_for_each_ftrace_rec(pg, rec) {
1693 int in_other_hash = 0;
1697 if (rec->flags & FTRACE_FL_DISABLED)
1702 * Only the filter_hash affects all records.
1703 * Update if the record is not in the notrace hash.
1705 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1708 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1709 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1712 * If filter_hash is set, we want to match all functions
1713 * that are in the hash but not in the other hash.
1715 * If filter_hash is not set, then we are decrementing.
1716 * That means we match anything that is in the hash
1717 * and also in the other_hash. That is, we need to turn
1718 * off functions in the other hash because they are disabled
1721 if (filter_hash && in_hash && !in_other_hash)
1723 else if (!filter_hash && in_hash &&
1724 (in_other_hash || ftrace_hash_empty(other_hash)))
1732 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1735 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1736 rec->flags |= FTRACE_FL_DIRECT;
1739 * If there's only a single callback registered to a
1740 * function, and the ops has a trampoline registered
1741 * for it, then we can call it directly.
1743 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1744 rec->flags |= FTRACE_FL_TRAMP;
1747 * If we are adding another function callback
1748 * to this function, and the previous had a
1749 * custom trampoline in use, then we need to go
1750 * back to the default trampoline.
1752 rec->flags &= ~FTRACE_FL_TRAMP;
1755 * If any ops wants regs saved for this function
1756 * then all ops will get saved regs.
1758 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1759 rec->flags |= FTRACE_FL_REGS;
1761 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1766 * Only the internal direct_ops should have the
1767 * DIRECT flag set. Thus, if it is removing a
1768 * function, then that function should no longer
1771 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1772 rec->flags &= ~FTRACE_FL_DIRECT;
1775 * If the rec had REGS enabled and the ops that is
1776 * being removed had REGS set, then see if there is
1777 * still any ops for this record that wants regs.
1778 * If not, we can stop recording them.
1780 if (ftrace_rec_count(rec) > 0 &&
1781 rec->flags & FTRACE_FL_REGS &&
1782 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1783 if (!test_rec_ops_needs_regs(rec))
1784 rec->flags &= ~FTRACE_FL_REGS;
1788 * The TRAMP needs to be set only if rec count
1789 * is decremented to one, and the ops that is
1790 * left has a trampoline. As TRAMP can only be
1791 * enabled if there is only a single ops attached
1794 if (ftrace_rec_count(rec) == 1 &&
1795 ftrace_find_tramp_ops_any(rec))
1796 rec->flags |= FTRACE_FL_TRAMP;
1798 rec->flags &= ~FTRACE_FL_TRAMP;
1801 * flags will be cleared in ftrace_check_record()
1802 * if rec count is zero.
1807 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1808 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
1810 /* Shortcut, if we handled all records, we are done. */
1811 if (!all && count == hash->count)
1813 } while_for_each_ftrace_rec();
1818 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1821 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1824 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1827 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1830 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1831 int filter_hash, int inc)
1833 struct ftrace_ops *op;
1835 __ftrace_hash_rec_update(ops, filter_hash, inc);
1837 if (ops->func_hash != &global_ops.local_hash)
1841 * If the ops shares the global_ops hash, then we need to update
1842 * all ops that are enabled and use this hash.
1844 do_for_each_ftrace_op(op, ftrace_ops_list) {
1848 if (op->func_hash == &global_ops.local_hash)
1849 __ftrace_hash_rec_update(op, filter_hash, inc);
1850 } while_for_each_ftrace_op(op);
1853 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1856 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1859 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1862 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1866 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1867 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1868 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1869 * Note that old_hash and new_hash has below meanings
1870 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1871 * - If the hash is EMPTY_HASH, it hits nothing
1872 * - Anything else hits the recs which match the hash entries.
1874 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1875 struct ftrace_hash *old_hash,
1876 struct ftrace_hash *new_hash)
1878 struct ftrace_page *pg;
1879 struct dyn_ftrace *rec, *end = NULL;
1882 /* Only update if the ops has been registered */
1883 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1886 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1890 * Since the IPMODIFY is a very address sensitive action, we do not
1891 * allow ftrace_ops to set all functions to new hash.
1893 if (!new_hash || !old_hash)
1896 /* Update rec->flags */
1897 do_for_each_ftrace_rec(pg, rec) {
1899 if (rec->flags & FTRACE_FL_DISABLED)
1902 /* We need to update only differences of filter_hash */
1903 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1904 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1905 if (in_old == in_new)
1909 /* New entries must ensure no others are using it */
1910 if (rec->flags & FTRACE_FL_IPMODIFY)
1912 rec->flags |= FTRACE_FL_IPMODIFY;
1913 } else /* Removed entry */
1914 rec->flags &= ~FTRACE_FL_IPMODIFY;
1915 } while_for_each_ftrace_rec();
1922 /* Roll back what we did above */
1923 do_for_each_ftrace_rec(pg, rec) {
1925 if (rec->flags & FTRACE_FL_DISABLED)
1931 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1932 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1933 if (in_old == in_new)
1937 rec->flags &= ~FTRACE_FL_IPMODIFY;
1939 rec->flags |= FTRACE_FL_IPMODIFY;
1940 } while_for_each_ftrace_rec();
1946 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1948 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1950 if (ftrace_hash_empty(hash))
1953 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1956 /* Disabling always succeeds */
1957 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1959 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1961 if (ftrace_hash_empty(hash))
1964 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1967 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1968 struct ftrace_hash *new_hash)
1970 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1972 if (ftrace_hash_empty(old_hash))
1975 if (ftrace_hash_empty(new_hash))
1978 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1981 static void print_ip_ins(const char *fmt, const unsigned char *p)
1985 printk(KERN_CONT "%s", fmt);
1987 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1988 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1991 enum ftrace_bug_type ftrace_bug_type;
1992 const void *ftrace_expected;
1994 static void print_bug_type(void)
1996 switch (ftrace_bug_type) {
1997 case FTRACE_BUG_UNKNOWN:
1999 case FTRACE_BUG_INIT:
2000 pr_info("Initializing ftrace call sites\n");
2002 case FTRACE_BUG_NOP:
2003 pr_info("Setting ftrace call site to NOP\n");
2005 case FTRACE_BUG_CALL:
2006 pr_info("Setting ftrace call site to call ftrace function\n");
2008 case FTRACE_BUG_UPDATE:
2009 pr_info("Updating ftrace call site to call a different ftrace function\n");
2015 * ftrace_bug - report and shutdown function tracer
2016 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2017 * @rec: The record that failed
2019 * The arch code that enables or disables the function tracing
2020 * can call ftrace_bug() when it has detected a problem in
2021 * modifying the code. @failed should be one of either:
2022 * EFAULT - if the problem happens on reading the @ip address
2023 * EINVAL - if what is read at @ip is not what was expected
2024 * EPERM - if the problem happens on writing to the @ip address
2026 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2028 unsigned long ip = rec ? rec->ip : 0;
2032 FTRACE_WARN_ON_ONCE(1);
2033 pr_info("ftrace faulted on modifying ");
2037 FTRACE_WARN_ON_ONCE(1);
2038 pr_info("ftrace failed to modify ");
2040 print_ip_ins(" actual: ", (unsigned char *)ip);
2042 if (ftrace_expected) {
2043 print_ip_ins(" expected: ", ftrace_expected);
2048 FTRACE_WARN_ON_ONCE(1);
2049 pr_info("ftrace faulted on writing ");
2053 FTRACE_WARN_ON_ONCE(1);
2054 pr_info("ftrace faulted on unknown error ");
2059 struct ftrace_ops *ops = NULL;
2061 pr_info("ftrace record flags: %lx\n", rec->flags);
2062 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2063 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2064 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2065 ops = ftrace_find_tramp_ops_any(rec);
2068 pr_cont("\ttramp: %pS (%pS)",
2069 (void *)ops->trampoline,
2071 ops = ftrace_find_tramp_ops_next(rec, ops);
2074 pr_cont("\ttramp: ERROR!");
2077 ip = ftrace_get_addr_curr(rec);
2078 pr_cont("\n expected tramp: %lx\n", ip);
2082 static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
2084 unsigned long flag = 0UL;
2086 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2088 if (rec->flags & FTRACE_FL_DISABLED)
2089 return FTRACE_UPDATE_IGNORE;
2092 * If we are updating calls:
2094 * If the record has a ref count, then we need to enable it
2095 * because someone is using it.
2097 * Otherwise we make sure its disabled.
2099 * If we are disabling calls, then disable all records that
2102 if (enable && ftrace_rec_count(rec))
2103 flag = FTRACE_FL_ENABLED;
2106 * If enabling and the REGS flag does not match the REGS_EN, or
2107 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2108 * this record. Set flags to fail the compare against ENABLED.
2109 * Same for direct calls.
2112 if (!(rec->flags & FTRACE_FL_REGS) !=
2113 !(rec->flags & FTRACE_FL_REGS_EN))
2114 flag |= FTRACE_FL_REGS;
2116 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2117 !(rec->flags & FTRACE_FL_TRAMP_EN))
2118 flag |= FTRACE_FL_TRAMP;
2121 * Direct calls are special, as count matters.
2122 * We must test the record for direct, if the
2123 * DIRECT and DIRECT_EN do not match, but only
2124 * if the count is 1. That's because, if the
2125 * count is something other than one, we do not
2126 * want the direct enabled (it will be done via the
2127 * direct helper). But if DIRECT_EN is set, and
2128 * the count is not one, we need to clear it.
2130 if (ftrace_rec_count(rec) == 1) {
2131 if (!(rec->flags & FTRACE_FL_DIRECT) !=
2132 !(rec->flags & FTRACE_FL_DIRECT_EN))
2133 flag |= FTRACE_FL_DIRECT;
2134 } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
2135 flag |= FTRACE_FL_DIRECT;
2139 /* If the state of this record hasn't changed, then do nothing */
2140 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2141 return FTRACE_UPDATE_IGNORE;
2144 /* Save off if rec is being enabled (for return value) */
2145 flag ^= rec->flags & FTRACE_FL_ENABLED;
2148 rec->flags |= FTRACE_FL_ENABLED;
2149 if (flag & FTRACE_FL_REGS) {
2150 if (rec->flags & FTRACE_FL_REGS)
2151 rec->flags |= FTRACE_FL_REGS_EN;
2153 rec->flags &= ~FTRACE_FL_REGS_EN;
2155 if (flag & FTRACE_FL_TRAMP) {
2156 if (rec->flags & FTRACE_FL_TRAMP)
2157 rec->flags |= FTRACE_FL_TRAMP_EN;
2159 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2161 if (flag & FTRACE_FL_DIRECT) {
2163 * If there's only one user (direct_ops helper)
2164 * then we can call the direct function
2165 * directly (no ftrace trampoline).
2167 if (ftrace_rec_count(rec) == 1) {
2168 if (rec->flags & FTRACE_FL_DIRECT)
2169 rec->flags |= FTRACE_FL_DIRECT_EN;
2171 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2174 * Can only call directly if there's
2175 * only one callback to the function.
2177 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2183 * If this record is being updated from a nop, then
2184 * return UPDATE_MAKE_CALL.
2186 * return UPDATE_MODIFY_CALL to tell the caller to convert
2187 * from the save regs, to a non-save regs function or
2188 * vice versa, or from a trampoline call.
2190 if (flag & FTRACE_FL_ENABLED) {
2191 ftrace_bug_type = FTRACE_BUG_CALL;
2192 return FTRACE_UPDATE_MAKE_CALL;
2195 ftrace_bug_type = FTRACE_BUG_UPDATE;
2196 return FTRACE_UPDATE_MODIFY_CALL;
2200 /* If there's no more users, clear all flags */
2201 if (!ftrace_rec_count(rec))
2205 * Just disable the record, but keep the ops TRAMP
2206 * and REGS states. The _EN flags must be disabled though.
2208 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2209 FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN);
2212 ftrace_bug_type = FTRACE_BUG_NOP;
2213 return FTRACE_UPDATE_MAKE_NOP;
2217 * ftrace_update_record, set a record that now is tracing or not
2218 * @rec: the record to update
2219 * @enable: set to true if the record is tracing, false to force disable
2221 * The records that represent all functions that can be traced need
2222 * to be updated when tracing has been enabled.
2224 int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
2226 return ftrace_check_record(rec, enable, true);
2230 * ftrace_test_record, check if the record has been enabled or not
2231 * @rec: the record to test
2232 * @enable: set to true to check if enabled, false if it is disabled
2234 * The arch code may need to test if a record is already set to
2235 * tracing to determine how to modify the function code that it
2238 int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
2240 return ftrace_check_record(rec, enable, false);
2243 static struct ftrace_ops *
2244 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2246 struct ftrace_ops *op;
2247 unsigned long ip = rec->ip;
2249 do_for_each_ftrace_op(op, ftrace_ops_list) {
2251 if (!op->trampoline)
2254 if (hash_contains_ip(ip, op->func_hash))
2256 } while_for_each_ftrace_op(op);
2261 static struct ftrace_ops *
2262 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2263 struct ftrace_ops *op)
2265 unsigned long ip = rec->ip;
2267 while_for_each_ftrace_op(op) {
2269 if (!op->trampoline)
2272 if (hash_contains_ip(ip, op->func_hash))
2279 static struct ftrace_ops *
2280 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2282 struct ftrace_ops *op;
2283 unsigned long ip = rec->ip;
2286 * Need to check removed ops first.
2287 * If they are being removed, and this rec has a tramp,
2288 * and this rec is in the ops list, then it would be the
2289 * one with the tramp.
2292 if (hash_contains_ip(ip, &removed_ops->old_hash))
2297 * Need to find the current trampoline for a rec.
2298 * Now, a trampoline is only attached to a rec if there
2299 * was a single 'ops' attached to it. But this can be called
2300 * when we are adding another op to the rec or removing the
2301 * current one. Thus, if the op is being added, we can
2302 * ignore it because it hasn't attached itself to the rec
2305 * If an ops is being modified (hooking to different functions)
2306 * then we don't care about the new functions that are being
2307 * added, just the old ones (that are probably being removed).
2309 * If we are adding an ops to a function that already is using
2310 * a trampoline, it needs to be removed (trampolines are only
2311 * for single ops connected), then an ops that is not being
2312 * modified also needs to be checked.
2314 do_for_each_ftrace_op(op, ftrace_ops_list) {
2316 if (!op->trampoline)
2320 * If the ops is being added, it hasn't gotten to
2321 * the point to be removed from this tree yet.
2323 if (op->flags & FTRACE_OPS_FL_ADDING)
2328 * If the ops is being modified and is in the old
2329 * hash, then it is probably being removed from this
2332 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2333 hash_contains_ip(ip, &op->old_hash))
2336 * If the ops is not being added or modified, and it's
2337 * in its normal filter hash, then this must be the one
2340 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2341 hash_contains_ip(ip, op->func_hash))
2344 } while_for_each_ftrace_op(op);
2349 static struct ftrace_ops *
2350 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2352 struct ftrace_ops *op;
2353 unsigned long ip = rec->ip;
2355 do_for_each_ftrace_op(op, ftrace_ops_list) {
2356 /* pass rec in as regs to have non-NULL val */
2357 if (hash_contains_ip(ip, op->func_hash))
2359 } while_for_each_ftrace_op(op);
2364 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2365 /* Protected by rcu_tasks for reading, and direct_mutex for writing */
2366 static struct ftrace_hash *direct_functions = EMPTY_HASH;
2367 static DEFINE_MUTEX(direct_mutex);
2368 int ftrace_direct_func_count;
2371 * Search the direct_functions hash to see if the given instruction pointer
2372 * has a direct caller attached to it.
2374 unsigned long ftrace_find_rec_direct(unsigned long ip)
2376 struct ftrace_func_entry *entry;
2378 entry = __ftrace_lookup_ip(direct_functions, ip);
2382 return entry->direct;
2385 static void call_direct_funcs(unsigned long ip, unsigned long pip,
2386 struct ftrace_ops *ops, struct pt_regs *regs)
2390 addr = ftrace_find_rec_direct(ip);
2394 arch_ftrace_set_direct_caller(regs, addr);
2397 struct ftrace_ops direct_ops = {
2398 .func = call_direct_funcs,
2399 .flags = FTRACE_OPS_FL_IPMODIFY | FTRACE_OPS_FL_RECURSION_SAFE
2400 | FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS
2401 | FTRACE_OPS_FL_PERMANENT,
2403 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2406 * ftrace_get_addr_new - Get the call address to set to
2407 * @rec: The ftrace record descriptor
2409 * If the record has the FTRACE_FL_REGS set, that means that it
2410 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2411 * is not not set, then it wants to convert to the normal callback.
2413 * Returns the address of the trampoline to set to
2415 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2417 struct ftrace_ops *ops;
2420 if ((rec->flags & FTRACE_FL_DIRECT) &&
2421 (ftrace_rec_count(rec) == 1)) {
2422 addr = ftrace_find_rec_direct(rec->ip);
2428 /* Trampolines take precedence over regs */
2429 if (rec->flags & FTRACE_FL_TRAMP) {
2430 ops = ftrace_find_tramp_ops_new(rec);
2431 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2432 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2433 (void *)rec->ip, (void *)rec->ip, rec->flags);
2434 /* Ftrace is shutting down, return anything */
2435 return (unsigned long)FTRACE_ADDR;
2437 return ops->trampoline;
2440 if (rec->flags & FTRACE_FL_REGS)
2441 return (unsigned long)FTRACE_REGS_ADDR;
2443 return (unsigned long)FTRACE_ADDR;
2447 * ftrace_get_addr_curr - Get the call address that is already there
2448 * @rec: The ftrace record descriptor
2450 * The FTRACE_FL_REGS_EN is set when the record already points to
2451 * a function that saves all the regs. Basically the '_EN' version
2452 * represents the current state of the function.
2454 * Returns the address of the trampoline that is currently being called
2456 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2458 struct ftrace_ops *ops;
2461 /* Direct calls take precedence over trampolines */
2462 if (rec->flags & FTRACE_FL_DIRECT_EN) {
2463 addr = ftrace_find_rec_direct(rec->ip);
2469 /* Trampolines take precedence over regs */
2470 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2471 ops = ftrace_find_tramp_ops_curr(rec);
2472 if (FTRACE_WARN_ON(!ops)) {
2473 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2474 (void *)rec->ip, (void *)rec->ip);
2475 /* Ftrace is shutting down, return anything */
2476 return (unsigned long)FTRACE_ADDR;
2478 return ops->trampoline;
2481 if (rec->flags & FTRACE_FL_REGS_EN)
2482 return (unsigned long)FTRACE_REGS_ADDR;
2484 return (unsigned long)FTRACE_ADDR;
2488 __ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
2490 unsigned long ftrace_old_addr;
2491 unsigned long ftrace_addr;
2494 ftrace_addr = ftrace_get_addr_new(rec);
2496 /* This needs to be done before we call ftrace_update_record */
2497 ftrace_old_addr = ftrace_get_addr_curr(rec);
2499 ret = ftrace_update_record(rec, enable);
2501 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2504 case FTRACE_UPDATE_IGNORE:
2507 case FTRACE_UPDATE_MAKE_CALL:
2508 ftrace_bug_type = FTRACE_BUG_CALL;
2509 return ftrace_make_call(rec, ftrace_addr);
2511 case FTRACE_UPDATE_MAKE_NOP:
2512 ftrace_bug_type = FTRACE_BUG_NOP;
2513 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2515 case FTRACE_UPDATE_MODIFY_CALL:
2516 ftrace_bug_type = FTRACE_BUG_UPDATE;
2517 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2520 return -1; /* unknown ftrace bug */
2523 void __weak ftrace_replace_code(int mod_flags)
2525 struct dyn_ftrace *rec;
2526 struct ftrace_page *pg;
2527 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2528 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
2531 if (unlikely(ftrace_disabled))
2534 do_for_each_ftrace_rec(pg, rec) {
2536 if (rec->flags & FTRACE_FL_DISABLED)
2539 failed = __ftrace_replace_code(rec, enable);
2541 ftrace_bug(failed, rec);
2542 /* Stop processing */
2547 } while_for_each_ftrace_rec();
2550 struct ftrace_rec_iter {
2551 struct ftrace_page *pg;
2556 * ftrace_rec_iter_start, start up iterating over traced functions
2558 * Returns an iterator handle that is used to iterate over all
2559 * the records that represent address locations where functions
2562 * May return NULL if no records are available.
2564 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2567 * We only use a single iterator.
2568 * Protected by the ftrace_lock mutex.
2570 static struct ftrace_rec_iter ftrace_rec_iter;
2571 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2573 iter->pg = ftrace_pages_start;
2576 /* Could have empty pages */
2577 while (iter->pg && !iter->pg->index)
2578 iter->pg = iter->pg->next;
2587 * ftrace_rec_iter_next, get the next record to process.
2588 * @iter: The handle to the iterator.
2590 * Returns the next iterator after the given iterator @iter.
2592 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2596 if (iter->index >= iter->pg->index) {
2597 iter->pg = iter->pg->next;
2600 /* Could have empty pages */
2601 while (iter->pg && !iter->pg->index)
2602 iter->pg = iter->pg->next;
2612 * ftrace_rec_iter_record, get the record at the iterator location
2613 * @iter: The current iterator location
2615 * Returns the record that the current @iter is at.
2617 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2619 return &iter->pg->records[iter->index];
2623 ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
2627 if (unlikely(ftrace_disabled))
2630 ret = ftrace_init_nop(mod, rec);
2632 ftrace_bug_type = FTRACE_BUG_INIT;
2633 ftrace_bug(ret, rec);
2640 * archs can override this function if they must do something
2641 * before the modifying code is performed.
2643 int __weak ftrace_arch_code_modify_prepare(void)
2649 * archs can override this function if they must do something
2650 * after the modifying code is performed.
2652 int __weak ftrace_arch_code_modify_post_process(void)
2657 void ftrace_modify_all_code(int command)
2659 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2663 if (command & FTRACE_MAY_SLEEP)
2664 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2667 * If the ftrace_caller calls a ftrace_ops func directly,
2668 * we need to make sure that it only traces functions it
2669 * expects to trace. When doing the switch of functions,
2670 * we need to update to the ftrace_ops_list_func first
2671 * before the transition between old and new calls are set,
2672 * as the ftrace_ops_list_func will check the ops hashes
2673 * to make sure the ops are having the right functions
2677 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2678 if (FTRACE_WARN_ON(err))
2682 if (command & FTRACE_UPDATE_CALLS)
2683 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
2684 else if (command & FTRACE_DISABLE_CALLS)
2685 ftrace_replace_code(mod_flags);
2687 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2688 function_trace_op = set_function_trace_op;
2690 /* If irqs are disabled, we are in stop machine */
2691 if (!irqs_disabled())
2692 smp_call_function(ftrace_sync_ipi, NULL, 1);
2693 err = ftrace_update_ftrace_func(ftrace_trace_function);
2694 if (FTRACE_WARN_ON(err))
2698 if (command & FTRACE_START_FUNC_RET)
2699 err = ftrace_enable_ftrace_graph_caller();
2700 else if (command & FTRACE_STOP_FUNC_RET)
2701 err = ftrace_disable_ftrace_graph_caller();
2702 FTRACE_WARN_ON(err);
2705 static int __ftrace_modify_code(void *data)
2707 int *command = data;
2709 ftrace_modify_all_code(*command);
2715 * ftrace_run_stop_machine, go back to the stop machine method
2716 * @command: The command to tell ftrace what to do
2718 * If an arch needs to fall back to the stop machine method, the
2719 * it can call this function.
2721 void ftrace_run_stop_machine(int command)
2723 stop_machine(__ftrace_modify_code, &command, NULL);
2727 * arch_ftrace_update_code, modify the code to trace or not trace
2728 * @command: The command that needs to be done
2730 * Archs can override this function if it does not need to
2731 * run stop_machine() to modify code.
2733 void __weak arch_ftrace_update_code(int command)
2735 ftrace_run_stop_machine(command);
2738 static void ftrace_run_update_code(int command)
2742 ret = ftrace_arch_code_modify_prepare();
2743 FTRACE_WARN_ON(ret);
2748 * By default we use stop_machine() to modify the code.
2749 * But archs can do what ever they want as long as it
2750 * is safe. The stop_machine() is the safest, but also
2751 * produces the most overhead.
2753 arch_ftrace_update_code(command);
2755 ret = ftrace_arch_code_modify_post_process();
2756 FTRACE_WARN_ON(ret);
2759 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2760 struct ftrace_ops_hash *old_hash)
2762 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2763 ops->old_hash.filter_hash = old_hash->filter_hash;
2764 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2765 ftrace_run_update_code(command);
2766 ops->old_hash.filter_hash = NULL;
2767 ops->old_hash.notrace_hash = NULL;
2768 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2771 static ftrace_func_t saved_ftrace_func;
2772 static int ftrace_start_up;
2774 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2778 static void ftrace_startup_enable(int command)
2780 if (saved_ftrace_func != ftrace_trace_function) {
2781 saved_ftrace_func = ftrace_trace_function;
2782 command |= FTRACE_UPDATE_TRACE_FUNC;
2785 if (!command || !ftrace_enabled)
2788 ftrace_run_update_code(command);
2791 static void ftrace_startup_all(int command)
2793 update_all_ops = true;
2794 ftrace_startup_enable(command);
2795 update_all_ops = false;
2798 int ftrace_startup(struct ftrace_ops *ops, int command)
2802 if (unlikely(ftrace_disabled))
2805 ret = __register_ftrace_function(ops);
2812 * Note that ftrace probes uses this to start up
2813 * and modify functions it will probe. But we still
2814 * set the ADDING flag for modification, as probes
2815 * do not have trampolines. If they add them in the
2816 * future, then the probes will need to distinguish
2817 * between adding and updating probes.
2819 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2821 ret = ftrace_hash_ipmodify_enable(ops);
2823 /* Rollback registration process */
2824 __unregister_ftrace_function(ops);
2826 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2830 if (ftrace_hash_rec_enable(ops, 1))
2831 command |= FTRACE_UPDATE_CALLS;
2833 ftrace_startup_enable(command);
2835 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2840 int ftrace_shutdown(struct ftrace_ops *ops, int command)
2844 if (unlikely(ftrace_disabled))
2847 ret = __unregister_ftrace_function(ops);
2853 * Just warn in case of unbalance, no need to kill ftrace, it's not
2854 * critical but the ftrace_call callers may be never nopped again after
2855 * further ftrace uses.
2857 WARN_ON_ONCE(ftrace_start_up < 0);
2859 /* Disabling ipmodify never fails */
2860 ftrace_hash_ipmodify_disable(ops);
2862 if (ftrace_hash_rec_disable(ops, 1))
2863 command |= FTRACE_UPDATE_CALLS;
2865 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2867 if (saved_ftrace_func != ftrace_trace_function) {
2868 saved_ftrace_func = ftrace_trace_function;
2869 command |= FTRACE_UPDATE_TRACE_FUNC;
2872 if (!command || !ftrace_enabled) {
2874 * If these are dynamic or per_cpu ops, they still
2875 * need their data freed. Since, function tracing is
2876 * not currently active, we can just free them
2877 * without synchronizing all CPUs.
2879 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2886 * If the ops uses a trampoline, then it needs to be
2887 * tested first on update.
2889 ops->flags |= FTRACE_OPS_FL_REMOVING;
2892 /* The trampoline logic checks the old hashes */
2893 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2894 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2896 ftrace_run_update_code(command);
2899 * If there's no more ops registered with ftrace, run a
2900 * sanity check to make sure all rec flags are cleared.
2902 if (rcu_dereference_protected(ftrace_ops_list,
2903 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2904 struct ftrace_page *pg;
2905 struct dyn_ftrace *rec;
2907 do_for_each_ftrace_rec(pg, rec) {
2908 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2909 pr_warn(" %pS flags:%lx\n",
2910 (void *)rec->ip, rec->flags);
2911 } while_for_each_ftrace_rec();
2914 ops->old_hash.filter_hash = NULL;
2915 ops->old_hash.notrace_hash = NULL;
2918 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2921 * Dynamic ops may be freed, we must make sure that all
2922 * callers are done before leaving this function.
2923 * The same goes for freeing the per_cpu data of the per_cpu
2926 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
2928 * We need to do a hard force of sched synchronization.
2929 * This is because we use preempt_disable() to do RCU, but
2930 * the function tracers can be called where RCU is not watching
2931 * (like before user_exit()). We can not rely on the RCU
2932 * infrastructure to do the synchronization, thus we must do it
2935 schedule_on_each_cpu(ftrace_sync);
2938 * When the kernel is preeptive, tasks can be preempted
2939 * while on a ftrace trampoline. Just scheduling a task on
2940 * a CPU is not good enough to flush them. Calling
2941 * synchornize_rcu_tasks() will wait for those tasks to
2942 * execute and either schedule voluntarily or enter user space.
2944 if (IS_ENABLED(CONFIG_PREEMPTION))
2945 synchronize_rcu_tasks();
2948 arch_ftrace_trampoline_free(ops);
2954 static void ftrace_startup_sysctl(void)
2958 if (unlikely(ftrace_disabled))
2961 /* Force update next time */
2962 saved_ftrace_func = NULL;
2963 /* ftrace_start_up is true if we want ftrace running */
2964 if (ftrace_start_up) {
2965 command = FTRACE_UPDATE_CALLS;
2966 if (ftrace_graph_active)
2967 command |= FTRACE_START_FUNC_RET;
2968 ftrace_startup_enable(command);
2972 static void ftrace_shutdown_sysctl(void)
2976 if (unlikely(ftrace_disabled))
2979 /* ftrace_start_up is true if ftrace is running */
2980 if (ftrace_start_up) {
2981 command = FTRACE_DISABLE_CALLS;
2982 if (ftrace_graph_active)
2983 command |= FTRACE_STOP_FUNC_RET;
2984 ftrace_run_update_code(command);
2988 static u64 ftrace_update_time;
2989 unsigned long ftrace_update_tot_cnt;
2990 unsigned long ftrace_number_of_pages;
2991 unsigned long ftrace_number_of_groups;
2993 static inline int ops_traces_mod(struct ftrace_ops *ops)
2996 * Filter_hash being empty will default to trace module.
2997 * But notrace hash requires a test of individual module functions.
2999 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
3000 ftrace_hash_empty(ops->func_hash->notrace_hash);
3004 * Check if the current ops references the record.
3006 * If the ops traces all functions, then it was already accounted for.
3007 * If the ops does not trace the current record function, skip it.
3008 * If the ops ignores the function via notrace filter, skip it.
3011 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3013 /* If ops isn't enabled, ignore it */
3014 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
3017 /* If ops traces all then it includes this function */
3018 if (ops_traces_mod(ops))
3021 /* The function must be in the filter */
3022 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
3023 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
3026 /* If in notrace hash, we ignore it too */
3027 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
3033 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
3035 struct ftrace_page *pg;
3036 struct dyn_ftrace *p;
3038 unsigned long update_cnt = 0;
3039 unsigned long rec_flags = 0;
3042 start = ftrace_now(raw_smp_processor_id());
3045 * When a module is loaded, this function is called to convert
3046 * the calls to mcount in its text to nops, and also to create
3047 * an entry in the ftrace data. Now, if ftrace is activated
3048 * after this call, but before the module sets its text to
3049 * read-only, the modification of enabling ftrace can fail if
3050 * the read-only is done while ftrace is converting the calls.
3051 * To prevent this, the module's records are set as disabled
3052 * and will be enabled after the call to set the module's text
3056 rec_flags |= FTRACE_FL_DISABLED;
3058 for (pg = new_pgs; pg; pg = pg->next) {
3060 for (i = 0; i < pg->index; i++) {
3062 /* If something went wrong, bail without enabling anything */
3063 if (unlikely(ftrace_disabled))
3066 p = &pg->records[i];
3067 p->flags = rec_flags;
3070 * Do the initial record conversion from mcount jump
3071 * to the NOP instructions.
3073 if (!__is_defined(CC_USING_NOP_MCOUNT) &&
3074 !ftrace_nop_initialize(mod, p))
3081 stop = ftrace_now(raw_smp_processor_id());
3082 ftrace_update_time = stop - start;
3083 ftrace_update_tot_cnt += update_cnt;
3088 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3093 if (WARN_ON(!count))
3096 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3099 * We want to fill as much as possible. No more than a page
3102 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3106 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3109 /* if we can't allocate this size, try something smaller */
3116 ftrace_number_of_pages += 1 << order;
3117 ftrace_number_of_groups++;
3119 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3128 static struct ftrace_page *
3129 ftrace_allocate_pages(unsigned long num_to_init)
3131 struct ftrace_page *start_pg;
3132 struct ftrace_page *pg;
3139 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3144 * Try to allocate as much as possible in one continues
3145 * location that fills in all of the space. We want to
3146 * waste as little space as possible.
3149 cnt = ftrace_allocate_records(pg, num_to_init);
3157 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3169 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3170 free_pages((unsigned long)pg->records, order);
3171 start_pg = pg->next;
3174 ftrace_number_of_pages -= 1 << order;
3175 ftrace_number_of_groups--;
3177 pr_info("ftrace: FAILED to allocate memory for functions\n");
3181 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3183 struct ftrace_iterator {
3187 struct ftrace_page *pg;
3188 struct dyn_ftrace *func;
3189 struct ftrace_func_probe *probe;
3190 struct ftrace_func_entry *probe_entry;
3191 struct trace_parser parser;
3192 struct ftrace_hash *hash;
3193 struct ftrace_ops *ops;
3194 struct trace_array *tr;
3195 struct list_head *mod_list;
3202 t_probe_next(struct seq_file *m, loff_t *pos)
3204 struct ftrace_iterator *iter = m->private;
3205 struct trace_array *tr = iter->ops->private;
3206 struct list_head *func_probes;
3207 struct ftrace_hash *hash;
3208 struct list_head *next;
3209 struct hlist_node *hnd = NULL;
3210 struct hlist_head *hhd;
3219 func_probes = &tr->func_probes;
3220 if (list_empty(func_probes))
3224 next = func_probes->next;
3225 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3228 if (iter->probe_entry)
3229 hnd = &iter->probe_entry->hlist;
3231 hash = iter->probe->ops.func_hash->filter_hash;
3234 * A probe being registered may temporarily have an empty hash
3235 * and it's at the end of the func_probes list.
3237 if (!hash || hash == EMPTY_HASH)
3240 size = 1 << hash->size_bits;
3243 if (iter->pidx >= size) {
3244 if (iter->probe->list.next == func_probes)
3246 next = iter->probe->list.next;
3247 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3248 hash = iter->probe->ops.func_hash->filter_hash;
3249 size = 1 << hash->size_bits;
3253 hhd = &hash->buckets[iter->pidx];
3255 if (hlist_empty(hhd)) {
3271 if (WARN_ON_ONCE(!hnd))
3274 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3279 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3281 struct ftrace_iterator *iter = m->private;
3285 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3288 if (iter->mod_pos > *pos)
3292 iter->probe_entry = NULL;
3294 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3295 p = t_probe_next(m, &l);
3302 /* Only set this if we have an item */
3303 iter->flags |= FTRACE_ITER_PROBE;
3309 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3311 struct ftrace_func_entry *probe_entry;
3312 struct ftrace_probe_ops *probe_ops;
3313 struct ftrace_func_probe *probe;
3315 probe = iter->probe;
3316 probe_entry = iter->probe_entry;
3318 if (WARN_ON_ONCE(!probe || !probe_entry))
3321 probe_ops = probe->probe_ops;
3323 if (probe_ops->print)
3324 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3326 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3327 (void *)probe_ops->func);
3333 t_mod_next(struct seq_file *m, loff_t *pos)
3335 struct ftrace_iterator *iter = m->private;
3336 struct trace_array *tr = iter->tr;
3341 iter->mod_list = iter->mod_list->next;
3343 if (iter->mod_list == &tr->mod_trace ||
3344 iter->mod_list == &tr->mod_notrace) {
3345 iter->flags &= ~FTRACE_ITER_MOD;
3349 iter->mod_pos = *pos;
3354 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3356 struct ftrace_iterator *iter = m->private;
3360 if (iter->func_pos > *pos)
3363 iter->mod_pos = iter->func_pos;
3365 /* probes are only available if tr is set */
3369 for (l = 0; l <= (*pos - iter->func_pos); ) {
3370 p = t_mod_next(m, &l);
3375 iter->flags &= ~FTRACE_ITER_MOD;
3376 return t_probe_start(m, pos);
3379 /* Only set this if we have an item */
3380 iter->flags |= FTRACE_ITER_MOD;
3386 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3388 struct ftrace_mod_load *ftrace_mod;
3389 struct trace_array *tr = iter->tr;
3391 if (WARN_ON_ONCE(!iter->mod_list) ||
3392 iter->mod_list == &tr->mod_trace ||
3393 iter->mod_list == &tr->mod_notrace)
3396 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3398 if (ftrace_mod->func)
3399 seq_printf(m, "%s", ftrace_mod->func);
3403 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3409 t_func_next(struct seq_file *m, loff_t *pos)
3411 struct ftrace_iterator *iter = m->private;
3412 struct dyn_ftrace *rec = NULL;
3417 if (iter->idx >= iter->pg->index) {
3418 if (iter->pg->next) {
3419 iter->pg = iter->pg->next;
3424 rec = &iter->pg->records[iter->idx++];
3425 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3426 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3428 ((iter->flags & FTRACE_ITER_ENABLED) &&
3429 !(rec->flags & FTRACE_FL_ENABLED))) {
3439 iter->pos = iter->func_pos = *pos;
3446 t_next(struct seq_file *m, void *v, loff_t *pos)
3448 struct ftrace_iterator *iter = m->private;
3449 loff_t l = *pos; /* t_probe_start() must use original pos */
3452 if (unlikely(ftrace_disabled))
3455 if (iter->flags & FTRACE_ITER_PROBE)
3456 return t_probe_next(m, pos);
3458 if (iter->flags & FTRACE_ITER_MOD)
3459 return t_mod_next(m, pos);
3461 if (iter->flags & FTRACE_ITER_PRINTALL) {
3462 /* next must increment pos, and t_probe_start does not */
3464 return t_mod_start(m, &l);
3467 ret = t_func_next(m, pos);
3470 return t_mod_start(m, &l);
3475 static void reset_iter_read(struct ftrace_iterator *iter)
3479 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3482 static void *t_start(struct seq_file *m, loff_t *pos)
3484 struct ftrace_iterator *iter = m->private;
3488 mutex_lock(&ftrace_lock);
3490 if (unlikely(ftrace_disabled))
3494 * If an lseek was done, then reset and start from beginning.
3496 if (*pos < iter->pos)
3497 reset_iter_read(iter);
3500 * For set_ftrace_filter reading, if we have the filter
3501 * off, we can short cut and just print out that all
3502 * functions are enabled.
3504 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3505 ftrace_hash_empty(iter->hash)) {
3506 iter->func_pos = 1; /* Account for the message */
3508 return t_mod_start(m, pos);
3509 iter->flags |= FTRACE_ITER_PRINTALL;
3510 /* reset in case of seek/pread */
3511 iter->flags &= ~FTRACE_ITER_PROBE;
3515 if (iter->flags & FTRACE_ITER_MOD)
3516 return t_mod_start(m, pos);
3519 * Unfortunately, we need to restart at ftrace_pages_start
3520 * every time we let go of the ftrace_mutex. This is because
3521 * those pointers can change without the lock.
3523 iter->pg = ftrace_pages_start;
3525 for (l = 0; l <= *pos; ) {
3526 p = t_func_next(m, &l);
3532 return t_mod_start(m, pos);
3537 static void t_stop(struct seq_file *m, void *p)
3539 mutex_unlock(&ftrace_lock);
3543 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3548 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3549 struct dyn_ftrace *rec)
3553 ptr = arch_ftrace_trampoline_func(ops, rec);
3555 seq_printf(m, " ->%pS", ptr);
3558 static int t_show(struct seq_file *m, void *v)
3560 struct ftrace_iterator *iter = m->private;
3561 struct dyn_ftrace *rec;
3563 if (iter->flags & FTRACE_ITER_PROBE)
3564 return t_probe_show(m, iter);
3566 if (iter->flags & FTRACE_ITER_MOD)
3567 return t_mod_show(m, iter);
3569 if (iter->flags & FTRACE_ITER_PRINTALL) {
3570 if (iter->flags & FTRACE_ITER_NOTRACE)
3571 seq_puts(m, "#### no functions disabled ####\n");
3573 seq_puts(m, "#### all functions enabled ####\n");
3582 seq_printf(m, "%ps", (void *)rec->ip);
3583 if (iter->flags & FTRACE_ITER_ENABLED) {
3584 struct ftrace_ops *ops;
3586 seq_printf(m, " (%ld)%s%s%s",
3587 ftrace_rec_count(rec),
3588 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3589 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
3590 rec->flags & FTRACE_FL_DIRECT ? " D" : " ");
3591 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3592 ops = ftrace_find_tramp_ops_any(rec);
3595 seq_printf(m, "\ttramp: %pS (%pS)",
3596 (void *)ops->trampoline,
3598 add_trampoline_func(m, ops, rec);
3599 ops = ftrace_find_tramp_ops_next(rec, ops);
3602 seq_puts(m, "\ttramp: ERROR!");
3604 add_trampoline_func(m, NULL, rec);
3606 if (rec->flags & FTRACE_FL_DIRECT) {
3607 unsigned long direct;
3609 direct = ftrace_find_rec_direct(rec->ip);
3611 seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3620 static const struct seq_operations show_ftrace_seq_ops = {
3628 ftrace_avail_open(struct inode *inode, struct file *file)
3630 struct ftrace_iterator *iter;
3633 ret = security_locked_down(LOCKDOWN_TRACEFS);
3637 if (unlikely(ftrace_disabled))
3640 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3644 iter->pg = ftrace_pages_start;
3645 iter->ops = &global_ops;
3651 ftrace_enabled_open(struct inode *inode, struct file *file)
3653 struct ftrace_iterator *iter;
3656 * This shows us what functions are currently being
3657 * traced and by what. Not sure if we want lockdown
3658 * to hide such critical information for an admin.
3659 * Although, perhaps it can show information we don't
3660 * want people to see, but if something is tracing
3661 * something, we probably want to know about it.
3664 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3668 iter->pg = ftrace_pages_start;
3669 iter->flags = FTRACE_ITER_ENABLED;
3670 iter->ops = &global_ops;
3676 * ftrace_regex_open - initialize function tracer filter files
3677 * @ops: The ftrace_ops that hold the hash filters
3678 * @flag: The type of filter to process
3679 * @inode: The inode, usually passed in to your open routine
3680 * @file: The file, usually passed in to your open routine
3682 * ftrace_regex_open() initializes the filter files for the
3683 * @ops. Depending on @flag it may process the filter hash or
3684 * the notrace hash of @ops. With this called from the open
3685 * routine, you can use ftrace_filter_write() for the write
3686 * routine if @flag has FTRACE_ITER_FILTER set, or
3687 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3688 * tracing_lseek() should be used as the lseek routine, and
3689 * release must call ftrace_regex_release().
3692 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3693 struct inode *inode, struct file *file)
3695 struct ftrace_iterator *iter;
3696 struct ftrace_hash *hash;
3697 struct list_head *mod_head;
3698 struct trace_array *tr = ops->private;
3701 ftrace_ops_init(ops);
3703 if (unlikely(ftrace_disabled))
3706 if (tracing_check_open_get_tr(tr))
3709 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3713 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3720 mutex_lock(&ops->func_hash->regex_lock);
3722 if (flag & FTRACE_ITER_NOTRACE) {
3723 hash = ops->func_hash->notrace_hash;
3724 mod_head = tr ? &tr->mod_notrace : NULL;
3726 hash = ops->func_hash->filter_hash;
3727 mod_head = tr ? &tr->mod_trace : NULL;
3730 iter->mod_list = mod_head;
3732 if (file->f_mode & FMODE_WRITE) {
3733 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3735 if (file->f_flags & O_TRUNC) {
3736 iter->hash = alloc_ftrace_hash(size_bits);
3737 clear_ftrace_mod_list(mod_head);
3739 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3743 trace_parser_put(&iter->parser);
3751 if (file->f_mode & FMODE_READ) {
3752 iter->pg = ftrace_pages_start;
3754 ret = seq_open(file, &show_ftrace_seq_ops);
3756 struct seq_file *m = file->private_data;
3760 free_ftrace_hash(iter->hash);
3761 trace_parser_put(&iter->parser);
3764 file->private_data = iter;
3767 mutex_unlock(&ops->func_hash->regex_lock);
3773 trace_array_put(tr);
3780 ftrace_filter_open(struct inode *inode, struct file *file)
3782 struct ftrace_ops *ops = inode->i_private;
3784 /* Checks for tracefs lockdown */
3785 return ftrace_regex_open(ops,
3786 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3791 ftrace_notrace_open(struct inode *inode, struct file *file)
3793 struct ftrace_ops *ops = inode->i_private;
3795 /* Checks for tracefs lockdown */
3796 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3800 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3801 struct ftrace_glob {
3808 * If symbols in an architecture don't correspond exactly to the user-visible
3809 * name of what they represent, it is possible to define this function to
3810 * perform the necessary adjustments.
3812 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3817 static int ftrace_match(char *str, struct ftrace_glob *g)
3822 str = arch_ftrace_match_adjust(str, g->search);
3826 if (strcmp(str, g->search) == 0)
3829 case MATCH_FRONT_ONLY:
3830 if (strncmp(str, g->search, g->len) == 0)
3833 case MATCH_MIDDLE_ONLY:
3834 if (strstr(str, g->search))
3837 case MATCH_END_ONLY:
3839 if (slen >= g->len &&
3840 memcmp(str + slen - g->len, g->search, g->len) == 0)
3844 if (glob_match(g->search, str))
3853 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3855 struct ftrace_func_entry *entry;
3858 entry = ftrace_lookup_ip(hash, rec->ip);
3860 /* Do nothing if it doesn't exist */
3864 free_hash_entry(hash, entry);
3866 /* Do nothing if it exists */
3870 ret = add_hash_entry(hash, rec->ip);
3876 add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3879 long index = simple_strtoul(func_g->search, NULL, 0);
3880 struct ftrace_page *pg;
3881 struct dyn_ftrace *rec;
3883 /* The index starts at 1 */
3887 do_for_each_ftrace_rec(pg, rec) {
3888 if (pg->index <= index) {
3890 /* this is a double loop, break goes to the next page */
3893 rec = &pg->records[index];
3894 enter_record(hash, rec, clear_filter);
3896 } while_for_each_ftrace_rec();
3901 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3902 struct ftrace_glob *mod_g, int exclude_mod)
3904 char str[KSYM_SYMBOL_LEN];
3907 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3910 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3912 /* blank module name to match all modules */
3914 /* blank module globbing: modname xor exclude_mod */
3915 if (!exclude_mod != !modname)
3921 * exclude_mod is set to trace everything but the given
3922 * module. If it is set and the module matches, then
3923 * return 0. If it is not set, and the module doesn't match
3924 * also return 0. Otherwise, check the function to see if
3927 if (!mod_matches == !exclude_mod)
3930 /* blank search means to match all funcs in the mod */
3935 return ftrace_match(str, func_g);
3939 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3941 struct ftrace_page *pg;
3942 struct dyn_ftrace *rec;
3943 struct ftrace_glob func_g = { .type = MATCH_FULL };
3944 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3945 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3946 int exclude_mod = 0;
3949 int clear_filter = 0;
3952 func_g.type = filter_parse_regex(func, len, &func_g.search,
3954 func_g.len = strlen(func_g.search);
3958 mod_g.type = filter_parse_regex(mod, strlen(mod),
3959 &mod_g.search, &exclude_mod);
3960 mod_g.len = strlen(mod_g.search);
3963 mutex_lock(&ftrace_lock);
3965 if (unlikely(ftrace_disabled))
3968 if (func_g.type == MATCH_INDEX) {
3969 found = add_rec_by_index(hash, &func_g, clear_filter);
3973 do_for_each_ftrace_rec(pg, rec) {
3975 if (rec->flags & FTRACE_FL_DISABLED)
3978 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3979 ret = enter_record(hash, rec, clear_filter);
3986 } while_for_each_ftrace_rec();
3988 mutex_unlock(&ftrace_lock);
3994 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3996 return match_records(hash, buff, len, NULL);
3999 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4000 struct ftrace_ops_hash *old_hash)
4002 struct ftrace_ops *op;
4004 if (!ftrace_enabled)
4007 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4008 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4013 * If this is the shared global_ops filter, then we need to
4014 * check if there is another ops that shares it, is enabled.
4015 * If so, we still need to run the modify code.
4017 if (ops->func_hash != &global_ops.local_hash)
4020 do_for_each_ftrace_op(op, ftrace_ops_list) {
4021 if (op->func_hash == &global_ops.local_hash &&
4022 op->flags & FTRACE_OPS_FL_ENABLED) {
4023 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4024 /* Only need to do this once */
4027 } while_for_each_ftrace_op(op);
4030 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
4031 struct ftrace_hash **orig_hash,
4032 struct ftrace_hash *hash,
4035 struct ftrace_ops_hash old_hash_ops;
4036 struct ftrace_hash *old_hash;
4039 old_hash = *orig_hash;
4040 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4041 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4042 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4044 ftrace_ops_update_code(ops, &old_hash_ops);
4045 free_ftrace_hash_rcu(old_hash);
4050 static bool module_exists(const char *module)
4052 /* All modules have the symbol __this_module */
4053 static const char this_mod[] = "__this_module";
4054 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
4058 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
4060 if (n > sizeof(modname) - 1)
4063 val = module_kallsyms_lookup_name(modname);
4067 static int cache_mod(struct trace_array *tr,
4068 const char *func, char *module, int enable)
4070 struct ftrace_mod_load *ftrace_mod, *n;
4071 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4074 mutex_lock(&ftrace_lock);
4076 /* We do not cache inverse filters */
4077 if (func[0] == '!') {
4081 /* Look to remove this hash */
4082 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4083 if (strcmp(ftrace_mod->module, module) != 0)
4086 /* no func matches all */
4087 if (strcmp(func, "*") == 0 ||
4088 (ftrace_mod->func &&
4089 strcmp(ftrace_mod->func, func) == 0)) {
4091 free_ftrace_mod(ftrace_mod);
4099 /* We only care about modules that have not been loaded yet */
4100 if (module_exists(module))
4103 /* Save this string off, and execute it when the module is loaded */
4104 ret = ftrace_add_mod(tr, func, module, enable);
4106 mutex_unlock(&ftrace_lock);
4112 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4113 int reset, int enable);
4115 #ifdef CONFIG_MODULES
4116 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4117 char *mod, bool enable)
4119 struct ftrace_mod_load *ftrace_mod, *n;
4120 struct ftrace_hash **orig_hash, *new_hash;
4121 LIST_HEAD(process_mods);
4125 mutex_lock(&ops->func_hash->regex_lock);
4128 orig_hash = &ops->func_hash->filter_hash;
4130 orig_hash = &ops->func_hash->notrace_hash;
4132 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4135 goto out; /* warn? */
4137 mutex_lock(&ftrace_lock);
4139 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4141 if (strcmp(ftrace_mod->module, mod) != 0)
4144 if (ftrace_mod->func)
4145 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4147 func = kstrdup("*", GFP_KERNEL);
4149 if (!func) /* warn? */
4152 list_del(&ftrace_mod->list);
4153 list_add(&ftrace_mod->list, &process_mods);
4155 /* Use the newly allocated func, as it may be "*" */
4156 kfree(ftrace_mod->func);
4157 ftrace_mod->func = func;
4160 mutex_unlock(&ftrace_lock);
4162 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4164 func = ftrace_mod->func;
4166 /* Grabs ftrace_lock, which is why we have this extra step */
4167 match_records(new_hash, func, strlen(func), mod);
4168 free_ftrace_mod(ftrace_mod);
4171 if (enable && list_empty(head))
4172 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4174 mutex_lock(&ftrace_lock);
4176 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4178 mutex_unlock(&ftrace_lock);
4181 mutex_unlock(&ops->func_hash->regex_lock);
4183 free_ftrace_hash(new_hash);
4186 static void process_cached_mods(const char *mod_name)
4188 struct trace_array *tr;
4191 mod = kstrdup(mod_name, GFP_KERNEL);
4195 mutex_lock(&trace_types_lock);
4196 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4197 if (!list_empty(&tr->mod_trace))
4198 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4199 if (!list_empty(&tr->mod_notrace))
4200 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4202 mutex_unlock(&trace_types_lock);
4209 * We register the module command as a template to show others how
4210 * to register the a command as well.
4214 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4215 char *func_orig, char *cmd, char *module, int enable)
4220 /* match_records() modifies func, and we need the original */
4221 func = kstrdup(func_orig, GFP_KERNEL);
4226 * cmd == 'mod' because we only registered this func
4227 * for the 'mod' ftrace_func_command.
4228 * But if you register one func with multiple commands,
4229 * you can tell which command was used by the cmd
4232 ret = match_records(hash, func, strlen(func), module);
4236 return cache_mod(tr, func_orig, module, enable);
4242 static struct ftrace_func_command ftrace_mod_cmd = {
4244 .func = ftrace_mod_callback,
4247 static int __init ftrace_mod_cmd_init(void)
4249 return register_ftrace_command(&ftrace_mod_cmd);
4251 core_initcall(ftrace_mod_cmd_init);
4253 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4254 struct ftrace_ops *op, struct pt_regs *pt_regs)
4256 struct ftrace_probe_ops *probe_ops;
4257 struct ftrace_func_probe *probe;
4259 probe = container_of(op, struct ftrace_func_probe, ops);
4260 probe_ops = probe->probe_ops;
4263 * Disable preemption for these calls to prevent a RCU grace
4264 * period. This syncs the hash iteration and freeing of items
4265 * on the hash. rcu_read_lock is too dangerous here.
4267 preempt_disable_notrace();
4268 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4269 preempt_enable_notrace();
4272 struct ftrace_func_map {
4273 struct ftrace_func_entry entry;
4277 struct ftrace_func_mapper {
4278 struct ftrace_hash hash;
4282 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4284 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4286 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4288 struct ftrace_hash *hash;
4291 * The mapper is simply a ftrace_hash, but since the entries
4292 * in the hash are not ftrace_func_entry type, we define it
4293 * as a separate structure.
4295 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4296 return (struct ftrace_func_mapper *)hash;
4300 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4301 * @mapper: The mapper that has the ip maps
4302 * @ip: the instruction pointer to find the data for
4304 * Returns the data mapped to @ip if found otherwise NULL. The return
4305 * is actually the address of the mapper data pointer. The address is
4306 * returned for use cases where the data is no bigger than a long, and
4307 * the user can use the data pointer as its data instead of having to
4308 * allocate more memory for the reference.
4310 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4313 struct ftrace_func_entry *entry;
4314 struct ftrace_func_map *map;
4316 entry = ftrace_lookup_ip(&mapper->hash, ip);
4320 map = (struct ftrace_func_map *)entry;
4325 * ftrace_func_mapper_add_ip - Map some data to an ip
4326 * @mapper: The mapper that has the ip maps
4327 * @ip: The instruction pointer address to map @data to
4328 * @data: The data to map to @ip
4330 * Returns 0 on succes otherwise an error.
4332 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4333 unsigned long ip, void *data)
4335 struct ftrace_func_entry *entry;
4336 struct ftrace_func_map *map;
4338 entry = ftrace_lookup_ip(&mapper->hash, ip);
4342 map = kmalloc(sizeof(*map), GFP_KERNEL);
4349 __add_hash_entry(&mapper->hash, &map->entry);
4355 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4356 * @mapper: The mapper that has the ip maps
4357 * @ip: The instruction pointer address to remove the data from
4359 * Returns the data if it is found, otherwise NULL.
4360 * Note, if the data pointer is used as the data itself, (see
4361 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4362 * if the data pointer was set to zero.
4364 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4367 struct ftrace_func_entry *entry;
4368 struct ftrace_func_map *map;
4371 entry = ftrace_lookup_ip(&mapper->hash, ip);
4375 map = (struct ftrace_func_map *)entry;
4378 remove_hash_entry(&mapper->hash, entry);
4385 * free_ftrace_func_mapper - free a mapping of ips and data
4386 * @mapper: The mapper that has the ip maps
4387 * @free_func: A function to be called on each data item.
4389 * This is used to free the function mapper. The @free_func is optional
4390 * and can be used if the data needs to be freed as well.
4392 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4393 ftrace_mapper_func free_func)
4395 struct ftrace_func_entry *entry;
4396 struct ftrace_func_map *map;
4397 struct hlist_head *hhd;
4403 if (free_func && mapper->hash.count) {
4404 size = 1 << mapper->hash.size_bits;
4405 for (i = 0; i < size; i++) {
4406 hhd = &mapper->hash.buckets[i];
4407 hlist_for_each_entry(entry, hhd, hlist) {
4408 map = (struct ftrace_func_map *)entry;
4413 free_ftrace_hash(&mapper->hash);
4416 static void release_probe(struct ftrace_func_probe *probe)
4418 struct ftrace_probe_ops *probe_ops;
4420 mutex_lock(&ftrace_lock);
4422 WARN_ON(probe->ref <= 0);
4424 /* Subtract the ref that was used to protect this instance */
4428 probe_ops = probe->probe_ops;
4430 * Sending zero as ip tells probe_ops to free
4431 * the probe->data itself
4433 if (probe_ops->free)
4434 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4435 list_del(&probe->list);
4438 mutex_unlock(&ftrace_lock);
4441 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4444 * Add one ref to keep it from being freed when releasing the
4445 * ftrace_lock mutex.
4451 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4452 struct ftrace_probe_ops *probe_ops,
4455 struct ftrace_func_entry *entry;
4456 struct ftrace_func_probe *probe;
4457 struct ftrace_hash **orig_hash;
4458 struct ftrace_hash *old_hash;
4459 struct ftrace_hash *hash;
4468 /* We do not support '!' for function probes */
4469 if (WARN_ON(glob[0] == '!'))
4473 mutex_lock(&ftrace_lock);
4474 /* Check if the probe_ops is already registered */
4475 list_for_each_entry(probe, &tr->func_probes, list) {
4476 if (probe->probe_ops == probe_ops)
4479 if (&probe->list == &tr->func_probes) {
4480 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4482 mutex_unlock(&ftrace_lock);
4485 probe->probe_ops = probe_ops;
4486 probe->ops.func = function_trace_probe_call;
4488 ftrace_ops_init(&probe->ops);
4489 list_add(&probe->list, &tr->func_probes);
4492 acquire_probe_locked(probe);
4494 mutex_unlock(&ftrace_lock);
4497 * Note, there's a small window here that the func_hash->filter_hash
4498 * may be NULL or empty. Need to be carefule when reading the loop.
4500 mutex_lock(&probe->ops.func_hash->regex_lock);
4502 orig_hash = &probe->ops.func_hash->filter_hash;
4503 old_hash = *orig_hash;
4504 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4511 ret = ftrace_match_records(hash, glob, strlen(glob));
4513 /* Nothing found? */
4520 size = 1 << hash->size_bits;
4521 for (i = 0; i < size; i++) {
4522 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4523 if (ftrace_lookup_ip(old_hash, entry->ip))
4526 * The caller might want to do something special
4527 * for each function we find. We call the callback
4528 * to give the caller an opportunity to do so.
4530 if (probe_ops->init) {
4531 ret = probe_ops->init(probe_ops, tr,
4535 if (probe_ops->free && count)
4536 probe_ops->free(probe_ops, tr,
4546 mutex_lock(&ftrace_lock);
4549 /* Nothing was added? */
4554 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4559 /* One ref for each new function traced */
4560 probe->ref += count;
4562 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4563 ret = ftrace_startup(&probe->ops, 0);
4566 mutex_unlock(&ftrace_lock);
4571 mutex_unlock(&probe->ops.func_hash->regex_lock);
4572 free_ftrace_hash(hash);
4574 release_probe(probe);
4579 if (!probe_ops->free || !count)
4582 /* Failed to do the move, need to call the free functions */
4583 for (i = 0; i < size; i++) {
4584 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4585 if (ftrace_lookup_ip(old_hash, entry->ip))
4587 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4594 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4595 struct ftrace_probe_ops *probe_ops)
4597 struct ftrace_ops_hash old_hash_ops;
4598 struct ftrace_func_entry *entry;
4599 struct ftrace_func_probe *probe;
4600 struct ftrace_glob func_g;
4601 struct ftrace_hash **orig_hash;
4602 struct ftrace_hash *old_hash;
4603 struct ftrace_hash *hash = NULL;
4604 struct hlist_node *tmp;
4605 struct hlist_head hhd;
4606 char str[KSYM_SYMBOL_LEN];
4608 int i, ret = -ENODEV;
4611 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4612 func_g.search = NULL;
4616 func_g.type = filter_parse_regex(glob, strlen(glob),
4617 &func_g.search, ¬);
4618 func_g.len = strlen(func_g.search);
4620 /* we do not support '!' for function probes */
4625 mutex_lock(&ftrace_lock);
4626 /* Check if the probe_ops is already registered */
4627 list_for_each_entry(probe, &tr->func_probes, list) {
4628 if (probe->probe_ops == probe_ops)
4631 if (&probe->list == &tr->func_probes)
4632 goto err_unlock_ftrace;
4635 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4636 goto err_unlock_ftrace;
4638 acquire_probe_locked(probe);
4640 mutex_unlock(&ftrace_lock);
4642 mutex_lock(&probe->ops.func_hash->regex_lock);
4644 orig_hash = &probe->ops.func_hash->filter_hash;
4645 old_hash = *orig_hash;
4647 if (ftrace_hash_empty(old_hash))
4650 old_hash_ops.filter_hash = old_hash;
4651 /* Probes only have filters */
4652 old_hash_ops.notrace_hash = NULL;
4655 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4659 INIT_HLIST_HEAD(&hhd);
4661 size = 1 << hash->size_bits;
4662 for (i = 0; i < size; i++) {
4663 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4665 if (func_g.search) {
4666 kallsyms_lookup(entry->ip, NULL, NULL,
4668 if (!ftrace_match(str, &func_g))
4672 remove_hash_entry(hash, entry);
4673 hlist_add_head(&entry->hlist, &hhd);
4677 /* Nothing found? */
4683 mutex_lock(&ftrace_lock);
4685 WARN_ON(probe->ref < count);
4687 probe->ref -= count;
4689 if (ftrace_hash_empty(hash))
4690 ftrace_shutdown(&probe->ops, 0);
4692 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4695 /* still need to update the function call sites */
4696 if (ftrace_enabled && !ftrace_hash_empty(hash))
4697 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4701 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4702 hlist_del(&entry->hlist);
4703 if (probe_ops->free)
4704 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4707 mutex_unlock(&ftrace_lock);
4710 mutex_unlock(&probe->ops.func_hash->regex_lock);
4711 free_ftrace_hash(hash);
4713 release_probe(probe);
4718 mutex_unlock(&ftrace_lock);
4722 void clear_ftrace_function_probes(struct trace_array *tr)
4724 struct ftrace_func_probe *probe, *n;
4726 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4727 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4730 static LIST_HEAD(ftrace_commands);
4731 static DEFINE_MUTEX(ftrace_cmd_mutex);
4734 * Currently we only register ftrace commands from __init, so mark this
4737 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4739 struct ftrace_func_command *p;
4742 mutex_lock(&ftrace_cmd_mutex);
4743 list_for_each_entry(p, &ftrace_commands, list) {
4744 if (strcmp(cmd->name, p->name) == 0) {
4749 list_add(&cmd->list, &ftrace_commands);
4751 mutex_unlock(&ftrace_cmd_mutex);
4757 * Currently we only unregister ftrace commands from __init, so mark
4760 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4762 struct ftrace_func_command *p, *n;
4765 mutex_lock(&ftrace_cmd_mutex);
4766 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4767 if (strcmp(cmd->name, p->name) == 0) {
4769 list_del_init(&p->list);
4774 mutex_unlock(&ftrace_cmd_mutex);
4779 static int ftrace_process_regex(struct ftrace_iterator *iter,
4780 char *buff, int len, int enable)
4782 struct ftrace_hash *hash = iter->hash;
4783 struct trace_array *tr = iter->ops->private;
4784 char *func, *command, *next = buff;
4785 struct ftrace_func_command *p;
4788 func = strsep(&next, ":");
4791 ret = ftrace_match_records(hash, func, len);
4801 command = strsep(&next, ":");
4803 mutex_lock(&ftrace_cmd_mutex);
4804 list_for_each_entry(p, &ftrace_commands, list) {
4805 if (strcmp(p->name, command) == 0) {
4806 ret = p->func(tr, hash, func, command, next, enable);
4811 mutex_unlock(&ftrace_cmd_mutex);
4817 ftrace_regex_write(struct file *file, const char __user *ubuf,
4818 size_t cnt, loff_t *ppos, int enable)
4820 struct ftrace_iterator *iter;
4821 struct trace_parser *parser;
4827 if (file->f_mode & FMODE_READ) {
4828 struct seq_file *m = file->private_data;
4831 iter = file->private_data;
4833 if (unlikely(ftrace_disabled))
4836 /* iter->hash is a local copy, so we don't need regex_lock */
4838 parser = &iter->parser;
4839 read = trace_get_user(parser, ubuf, cnt, ppos);
4841 if (read >= 0 && trace_parser_loaded(parser) &&
4842 !trace_parser_cont(parser)) {
4843 ret = ftrace_process_regex(iter, parser->buffer,
4844 parser->idx, enable);
4845 trace_parser_clear(parser);
4856 ftrace_filter_write(struct file *file, const char __user *ubuf,
4857 size_t cnt, loff_t *ppos)
4859 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4863 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4864 size_t cnt, loff_t *ppos)
4866 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4870 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4872 struct ftrace_func_entry *entry;
4874 if (!ftrace_location(ip))
4878 entry = ftrace_lookup_ip(hash, ip);
4881 free_hash_entry(hash, entry);
4885 return add_hash_entry(hash, ip);
4889 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4890 unsigned long ip, int remove, int reset, int enable)
4892 struct ftrace_hash **orig_hash;
4893 struct ftrace_hash *hash;
4896 if (unlikely(ftrace_disabled))
4899 mutex_lock(&ops->func_hash->regex_lock);
4902 orig_hash = &ops->func_hash->filter_hash;
4904 orig_hash = &ops->func_hash->notrace_hash;
4907 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4909 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4913 goto out_regex_unlock;
4916 if (buf && !ftrace_match_records(hash, buf, len)) {
4918 goto out_regex_unlock;
4921 ret = ftrace_match_addr(hash, ip, remove);
4923 goto out_regex_unlock;
4926 mutex_lock(&ftrace_lock);
4927 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4928 mutex_unlock(&ftrace_lock);
4931 mutex_unlock(&ops->func_hash->regex_lock);
4933 free_ftrace_hash(hash);
4938 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4939 int reset, int enable)
4941 return ftrace_set_hash(ops, NULL, 0, ip, remove, reset, enable);
4944 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
4946 struct ftrace_direct_func {
4947 struct list_head next;
4952 static LIST_HEAD(ftrace_direct_funcs);
4955 * ftrace_find_direct_func - test an address if it is a registered direct caller
4956 * @addr: The address of a registered direct caller
4958 * This searches to see if a ftrace direct caller has been registered
4959 * at a specific address, and if so, it returns a descriptor for it.
4961 * This can be used by architecture code to see if an address is
4962 * a direct caller (trampoline) attached to a fentry/mcount location.
4963 * This is useful for the function_graph tracer, as it may need to
4964 * do adjustments if it traced a location that also has a direct
4965 * trampoline attached to it.
4967 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
4969 struct ftrace_direct_func *entry;
4972 /* May be called by fgraph trampoline (protected by rcu tasks) */
4973 list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
4974 if (entry->addr == addr) {
4986 * register_ftrace_direct - Call a custom trampoline directly
4987 * @ip: The address of the nop at the beginning of a function
4988 * @addr: The address of the trampoline to call at @ip
4990 * This is used to connect a direct call from the nop location (@ip)
4991 * at the start of ftrace traced functions. The location that it calls
4992 * (@addr) must be able to handle a direct call, and save the parameters
4993 * of the function being traced, and restore them (or inject new ones
4994 * if needed), before returning.
4998 * -EBUSY - Another direct function is already attached (there can be only one)
4999 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5000 * -ENOMEM - There was an allocation failure.
5002 int register_ftrace_direct(unsigned long ip, unsigned long addr)
5004 struct ftrace_direct_func *direct;
5005 struct ftrace_func_entry *entry;
5006 struct ftrace_hash *free_hash = NULL;
5007 struct dyn_ftrace *rec;
5010 mutex_lock(&direct_mutex);
5012 /* See if there's a direct function at @ip already */
5013 if (ftrace_find_rec_direct(ip))
5017 rec = lookup_rec(ip, ip);
5022 * Check if the rec says it has a direct call but we didn't
5025 if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
5028 /* Make sure the ip points to the exact record */
5029 if (ip != rec->ip) {
5031 /* Need to check this ip for a direct. */
5032 if (ftrace_find_rec_direct(ip))
5037 if (ftrace_hash_empty(direct_functions) ||
5038 direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
5039 struct ftrace_hash *new_hash;
5040 int size = ftrace_hash_empty(direct_functions) ? 0 :
5041 direct_functions->count + 1;
5046 new_hash = dup_hash(direct_functions, size);
5050 free_hash = direct_functions;
5051 direct_functions = new_hash;
5054 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
5058 direct = ftrace_find_direct_func(addr);
5060 direct = kmalloc(sizeof(*direct), GFP_KERNEL);
5065 direct->addr = addr;
5067 list_add_rcu(&direct->next, &ftrace_direct_funcs);
5068 ftrace_direct_func_count++;
5072 entry->direct = addr;
5073 __add_hash_entry(direct_functions, entry);
5075 ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
5077 remove_hash_entry(direct_functions, entry);
5079 if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
5080 ret = register_ftrace_function(&direct_ops);
5082 ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5087 if (!direct->count) {
5088 list_del_rcu(&direct->next);
5089 synchronize_rcu_tasks();
5092 free_ftrace_hash(free_hash);
5094 ftrace_direct_func_count--;
5100 mutex_unlock(&direct_mutex);
5103 synchronize_rcu_tasks();
5104 free_ftrace_hash(free_hash);
5109 EXPORT_SYMBOL_GPL(register_ftrace_direct);
5111 static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
5112 struct dyn_ftrace **recp)
5114 struct ftrace_func_entry *entry;
5115 struct dyn_ftrace *rec;
5117 rec = lookup_rec(*ip, *ip);
5121 entry = __ftrace_lookup_ip(direct_functions, rec->ip);
5123 WARN_ON(rec->flags & FTRACE_FL_DIRECT);
5127 WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
5129 /* Passed in ip just needs to be on the call site */
5138 int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
5140 struct ftrace_direct_func *direct;
5141 struct ftrace_func_entry *entry;
5144 mutex_lock(&direct_mutex);
5146 entry = find_direct_entry(&ip, NULL);
5150 if (direct_functions->count == 1)
5151 unregister_ftrace_function(&direct_ops);
5153 ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5157 remove_hash_entry(direct_functions, entry);
5159 direct = ftrace_find_direct_func(addr);
5160 if (!WARN_ON(!direct)) {
5161 /* This is the good path (see the ! before WARN) */
5163 WARN_ON(direct->count < 0);
5164 if (!direct->count) {
5165 list_del_rcu(&direct->next);
5166 synchronize_rcu_tasks();
5169 ftrace_direct_func_count--;
5173 mutex_unlock(&direct_mutex);
5177 EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
5179 static struct ftrace_ops stub_ops = {
5180 .func = ftrace_stub,
5184 * ftrace_modify_direct_caller - modify ftrace nop directly
5185 * @entry: The ftrace hash entry of the direct helper for @rec
5186 * @rec: The record representing the function site to patch
5187 * @old_addr: The location that the site at @rec->ip currently calls
5188 * @new_addr: The location that the site at @rec->ip should call
5190 * An architecture may overwrite this function to optimize the
5191 * changing of the direct callback on an ftrace nop location.
5192 * This is called with the ftrace_lock mutex held, and no other
5193 * ftrace callbacks are on the associated record (@rec). Thus,
5194 * it is safe to modify the ftrace record, where it should be
5195 * currently calling @old_addr directly, to call @new_addr.
5197 * Safety checks should be made to make sure that the code at
5198 * @rec->ip is currently calling @old_addr. And this must
5199 * also update entry->direct to @new_addr.
5201 int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
5202 struct dyn_ftrace *rec,
5203 unsigned long old_addr,
5204 unsigned long new_addr)
5206 unsigned long ip = rec->ip;
5210 * The ftrace_lock was used to determine if the record
5211 * had more than one registered user to it. If it did,
5212 * we needed to prevent that from changing to do the quick
5213 * switch. But if it did not (only a direct caller was attached)
5214 * then this function is called. But this function can deal
5215 * with attached callers to the rec that we care about, and
5216 * since this function uses standard ftrace calls that take
5217 * the ftrace_lock mutex, we need to release it.
5219 mutex_unlock(&ftrace_lock);
5222 * By setting a stub function at the same address, we force
5223 * the code to call the iterator and the direct_ops helper.
5224 * This means that @ip does not call the direct call, and
5225 * we can simply modify it.
5227 ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
5231 ret = register_ftrace_function(&stub_ops);
5233 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5237 entry->direct = new_addr;
5240 * By removing the stub, we put back the direct call, calling
5243 unregister_ftrace_function(&stub_ops);
5244 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5247 mutex_lock(&ftrace_lock);
5253 * modify_ftrace_direct - Modify an existing direct call to call something else
5254 * @ip: The instruction pointer to modify
5255 * @old_addr: The address that the current @ip calls directly
5256 * @new_addr: The address that the @ip should call
5258 * This modifies a ftrace direct caller at an instruction pointer without
5259 * having to disable it first. The direct call will switch over to the
5260 * @new_addr without missing anything.
5262 * Returns: zero on success. Non zero on error, which includes:
5263 * -ENODEV : the @ip given has no direct caller attached
5264 * -EINVAL : the @old_addr does not match the current direct caller
5266 int modify_ftrace_direct(unsigned long ip,
5267 unsigned long old_addr, unsigned long new_addr)
5269 struct ftrace_func_entry *entry;
5270 struct dyn_ftrace *rec;
5273 mutex_lock(&direct_mutex);
5275 mutex_lock(&ftrace_lock);
5276 entry = find_direct_entry(&ip, &rec);
5281 if (entry->direct != old_addr)
5285 * If there's no other ftrace callback on the rec->ip location,
5286 * then it can be changed directly by the architecture.
5287 * If there is another caller, then we just need to change the
5288 * direct caller helper to point to @new_addr.
5290 if (ftrace_rec_count(rec) == 1) {
5291 ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
5293 entry->direct = new_addr;
5298 mutex_unlock(&ftrace_lock);
5299 mutex_unlock(&direct_mutex);
5302 EXPORT_SYMBOL_GPL(modify_ftrace_direct);
5303 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
5306 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
5307 * @ops - the ops to set the filter with
5308 * @ip - the address to add to or remove from the filter.
5309 * @remove - non zero to remove the ip from the filter
5310 * @reset - non zero to reset all filters before applying this filter.
5312 * Filters denote which functions should be enabled when tracing is enabled
5313 * If @ip is NULL, it failes to update filter.
5315 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
5316 int remove, int reset)
5318 ftrace_ops_init(ops);
5319 return ftrace_set_addr(ops, ip, remove, reset, 1);
5321 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
5324 * ftrace_ops_set_global_filter - setup ops to use global filters
5325 * @ops - the ops which will use the global filters
5327 * ftrace users who need global function trace filtering should call this.
5328 * It can set the global filter only if ops were not initialized before.
5330 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
5332 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
5335 ftrace_ops_init(ops);
5336 ops->func_hash = &global_ops.local_hash;
5338 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
5341 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
5342 int reset, int enable)
5344 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
5348 * ftrace_set_filter - set a function to filter on in ftrace
5349 * @ops - the ops to set the filter with
5350 * @buf - the string that holds the function filter text.
5351 * @len - the length of the string.
5352 * @reset - non zero to reset all filters before applying this filter.
5354 * Filters denote which functions should be enabled when tracing is enabled.
5355 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5357 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
5360 ftrace_ops_init(ops);
5361 return ftrace_set_regex(ops, buf, len, reset, 1);
5363 EXPORT_SYMBOL_GPL(ftrace_set_filter);
5366 * ftrace_set_notrace - set a function to not trace in ftrace
5367 * @ops - the ops to set the notrace filter with
5368 * @buf - the string that holds the function notrace text.
5369 * @len - the length of the string.
5370 * @reset - non zero to reset all filters before applying this filter.
5372 * Notrace Filters denote which functions should not be enabled when tracing
5373 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5376 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
5379 ftrace_ops_init(ops);
5380 return ftrace_set_regex(ops, buf, len, reset, 0);
5382 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
5384 * ftrace_set_global_filter - set a function to filter on with global tracers
5385 * @buf - the string that holds the function filter text.
5386 * @len - the length of the string.
5387 * @reset - non zero to reset all filters before applying this filter.
5389 * Filters denote which functions should be enabled when tracing is enabled.
5390 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5392 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
5394 ftrace_set_regex(&global_ops, buf, len, reset, 1);
5396 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
5399 * ftrace_set_global_notrace - set a function to not trace with global tracers
5400 * @buf - the string that holds the function notrace text.
5401 * @len - the length of the string.
5402 * @reset - non zero to reset all filters before applying this filter.
5404 * Notrace Filters denote which functions should not be enabled when tracing
5405 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5408 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
5410 ftrace_set_regex(&global_ops, buf, len, reset, 0);
5412 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
5415 * command line interface to allow users to set filters on boot up.
5417 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
5418 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5419 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
5421 /* Used by function selftest to not test if filter is set */
5422 bool ftrace_filter_param __initdata;
5424 static int __init set_ftrace_notrace(char *str)
5426 ftrace_filter_param = true;
5427 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
5430 __setup("ftrace_notrace=", set_ftrace_notrace);
5432 static int __init set_ftrace_filter(char *str)
5434 ftrace_filter_param = true;
5435 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
5438 __setup("ftrace_filter=", set_ftrace_filter);
5440 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5441 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
5442 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5443 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
5445 static int __init set_graph_function(char *str)
5447 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
5450 __setup("ftrace_graph_filter=", set_graph_function);
5452 static int __init set_graph_notrace_function(char *str)
5454 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
5457 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
5459 static int __init set_graph_max_depth_function(char *str)
5463 fgraph_max_depth = simple_strtoul(str, NULL, 0);
5466 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
5468 static void __init set_ftrace_early_graph(char *buf, int enable)
5472 struct ftrace_hash *hash;
5474 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5475 if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
5479 func = strsep(&buf, ",");
5480 /* we allow only one expression at a time */
5481 ret = ftrace_graph_set_hash(hash, func);
5483 printk(KERN_DEBUG "ftrace: function %s not "
5484 "traceable\n", func);
5488 ftrace_graph_hash = hash;
5490 ftrace_graph_notrace_hash = hash;
5492 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5495 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
5499 ftrace_ops_init(ops);
5502 func = strsep(&buf, ",");
5503 ftrace_set_regex(ops, func, strlen(func), 0, enable);
5507 static void __init set_ftrace_early_filters(void)
5509 if (ftrace_filter_buf[0])
5510 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5511 if (ftrace_notrace_buf[0])
5512 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5513 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5514 if (ftrace_graph_buf[0])
5515 set_ftrace_early_graph(ftrace_graph_buf, 1);
5516 if (ftrace_graph_notrace_buf[0])
5517 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5518 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5521 int ftrace_regex_release(struct inode *inode, struct file *file)
5523 struct seq_file *m = (struct seq_file *)file->private_data;
5524 struct ftrace_iterator *iter;
5525 struct ftrace_hash **orig_hash;
5526 struct trace_parser *parser;
5530 if (file->f_mode & FMODE_READ) {
5532 seq_release(inode, file);
5534 iter = file->private_data;
5536 parser = &iter->parser;
5537 if (trace_parser_loaded(parser)) {
5538 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
5541 trace_parser_put(parser);
5543 mutex_lock(&iter->ops->func_hash->regex_lock);
5545 if (file->f_mode & FMODE_WRITE) {
5546 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5549 orig_hash = &iter->ops->func_hash->filter_hash;
5550 if (iter->tr && !list_empty(&iter->tr->mod_trace))
5551 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5553 orig_hash = &iter->ops->func_hash->notrace_hash;
5555 mutex_lock(&ftrace_lock);
5556 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5557 iter->hash, filter_hash);
5558 mutex_unlock(&ftrace_lock);
5560 /* For read only, the hash is the ops hash */
5564 mutex_unlock(&iter->ops->func_hash->regex_lock);
5565 free_ftrace_hash(iter->hash);
5567 trace_array_put(iter->tr);
5573 static const struct file_operations ftrace_avail_fops = {
5574 .open = ftrace_avail_open,
5576 .llseek = seq_lseek,
5577 .release = seq_release_private,
5580 static const struct file_operations ftrace_enabled_fops = {
5581 .open = ftrace_enabled_open,
5583 .llseek = seq_lseek,
5584 .release = seq_release_private,
5587 static const struct file_operations ftrace_filter_fops = {
5588 .open = ftrace_filter_open,
5590 .write = ftrace_filter_write,
5591 .llseek = tracing_lseek,
5592 .release = ftrace_regex_release,
5595 static const struct file_operations ftrace_notrace_fops = {
5596 .open = ftrace_notrace_open,
5598 .write = ftrace_notrace_write,
5599 .llseek = tracing_lseek,
5600 .release = ftrace_regex_release,
5603 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5605 static DEFINE_MUTEX(graph_lock);
5607 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
5608 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
5610 enum graph_filter_type {
5611 GRAPH_FILTER_NOTRACE = 0,
5612 GRAPH_FILTER_FUNCTION,
5615 #define FTRACE_GRAPH_EMPTY ((void *)1)
5617 struct ftrace_graph_data {
5618 struct ftrace_hash *hash;
5619 struct ftrace_func_entry *entry;
5620 int idx; /* for hash table iteration */
5621 enum graph_filter_type type;
5622 struct ftrace_hash *new_hash;
5623 const struct seq_operations *seq_ops;
5624 struct trace_parser parser;
5628 __g_next(struct seq_file *m, loff_t *pos)
5630 struct ftrace_graph_data *fgd = m->private;
5631 struct ftrace_func_entry *entry = fgd->entry;
5632 struct hlist_head *head;
5633 int i, idx = fgd->idx;
5635 if (*pos >= fgd->hash->count)
5639 hlist_for_each_entry_continue(entry, hlist) {
5647 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5648 head = &fgd->hash->buckets[i];
5649 hlist_for_each_entry(entry, head, hlist) {
5659 g_next(struct seq_file *m, void *v, loff_t *pos)
5662 return __g_next(m, pos);
5665 static void *g_start(struct seq_file *m, loff_t *pos)
5667 struct ftrace_graph_data *fgd = m->private;
5669 mutex_lock(&graph_lock);
5671 if (fgd->type == GRAPH_FILTER_FUNCTION)
5672 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5673 lockdep_is_held(&graph_lock));
5675 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5676 lockdep_is_held(&graph_lock));
5678 /* Nothing, tell g_show to print all functions are enabled */
5679 if (ftrace_hash_empty(fgd->hash) && !*pos)
5680 return FTRACE_GRAPH_EMPTY;
5684 return __g_next(m, pos);
5687 static void g_stop(struct seq_file *m, void *p)
5689 mutex_unlock(&graph_lock);
5692 static int g_show(struct seq_file *m, void *v)
5694 struct ftrace_func_entry *entry = v;
5699 if (entry == FTRACE_GRAPH_EMPTY) {
5700 struct ftrace_graph_data *fgd = m->private;
5702 if (fgd->type == GRAPH_FILTER_FUNCTION)
5703 seq_puts(m, "#### all functions enabled ####\n");
5705 seq_puts(m, "#### no functions disabled ####\n");
5709 seq_printf(m, "%ps\n", (void *)entry->ip);
5714 static const struct seq_operations ftrace_graph_seq_ops = {
5722 __ftrace_graph_open(struct inode *inode, struct file *file,
5723 struct ftrace_graph_data *fgd)
5726 struct ftrace_hash *new_hash = NULL;
5728 ret = security_locked_down(LOCKDOWN_TRACEFS);
5732 if (file->f_mode & FMODE_WRITE) {
5733 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5735 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5738 if (file->f_flags & O_TRUNC)
5739 new_hash = alloc_ftrace_hash(size_bits);
5741 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5749 if (file->f_mode & FMODE_READ) {
5750 ret = seq_open(file, &ftrace_graph_seq_ops);
5752 struct seq_file *m = file->private_data;
5756 free_ftrace_hash(new_hash);
5760 file->private_data = fgd;
5763 if (ret < 0 && file->f_mode & FMODE_WRITE)
5764 trace_parser_put(&fgd->parser);
5766 fgd->new_hash = new_hash;
5769 * All uses of fgd->hash must be taken with the graph_lock
5770 * held. The graph_lock is going to be released, so force
5771 * fgd->hash to be reinitialized when it is taken again.
5779 ftrace_graph_open(struct inode *inode, struct file *file)
5781 struct ftrace_graph_data *fgd;
5784 if (unlikely(ftrace_disabled))
5787 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5791 mutex_lock(&graph_lock);
5793 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5794 lockdep_is_held(&graph_lock));
5795 fgd->type = GRAPH_FILTER_FUNCTION;
5796 fgd->seq_ops = &ftrace_graph_seq_ops;
5798 ret = __ftrace_graph_open(inode, file, fgd);
5802 mutex_unlock(&graph_lock);
5807 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5809 struct ftrace_graph_data *fgd;
5812 if (unlikely(ftrace_disabled))
5815 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5819 mutex_lock(&graph_lock);
5821 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5822 lockdep_is_held(&graph_lock));
5823 fgd->type = GRAPH_FILTER_NOTRACE;
5824 fgd->seq_ops = &ftrace_graph_seq_ops;
5826 ret = __ftrace_graph_open(inode, file, fgd);
5830 mutex_unlock(&graph_lock);
5835 ftrace_graph_release(struct inode *inode, struct file *file)
5837 struct ftrace_graph_data *fgd;
5838 struct ftrace_hash *old_hash, *new_hash;
5839 struct trace_parser *parser;
5842 if (file->f_mode & FMODE_READ) {
5843 struct seq_file *m = file->private_data;
5846 seq_release(inode, file);
5848 fgd = file->private_data;
5852 if (file->f_mode & FMODE_WRITE) {
5854 parser = &fgd->parser;
5856 if (trace_parser_loaded((parser))) {
5857 ret = ftrace_graph_set_hash(fgd->new_hash,
5861 trace_parser_put(parser);
5863 new_hash = __ftrace_hash_move(fgd->new_hash);
5869 mutex_lock(&graph_lock);
5871 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5872 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5873 lockdep_is_held(&graph_lock));
5874 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5876 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5877 lockdep_is_held(&graph_lock));
5878 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5881 mutex_unlock(&graph_lock);
5884 * We need to do a hard force of sched synchronization.
5885 * This is because we use preempt_disable() to do RCU, but
5886 * the function tracers can be called where RCU is not watching
5887 * (like before user_exit()). We can not rely on the RCU
5888 * infrastructure to do the synchronization, thus we must do it
5891 schedule_on_each_cpu(ftrace_sync);
5893 free_ftrace_hash(old_hash);
5897 free_ftrace_hash(fgd->new_hash);
5904 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5906 struct ftrace_glob func_g;
5907 struct dyn_ftrace *rec;
5908 struct ftrace_page *pg;
5909 struct ftrace_func_entry *entry;
5914 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5915 &func_g.search, ¬);
5917 func_g.len = strlen(func_g.search);
5919 mutex_lock(&ftrace_lock);
5921 if (unlikely(ftrace_disabled)) {
5922 mutex_unlock(&ftrace_lock);
5926 do_for_each_ftrace_rec(pg, rec) {
5928 if (rec->flags & FTRACE_FL_DISABLED)
5931 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5932 entry = ftrace_lookup_ip(hash, rec->ip);
5939 if (add_hash_entry(hash, rec->ip) < 0)
5943 free_hash_entry(hash, entry);
5948 } while_for_each_ftrace_rec();
5950 mutex_unlock(&ftrace_lock);
5959 ftrace_graph_write(struct file *file, const char __user *ubuf,
5960 size_t cnt, loff_t *ppos)
5962 ssize_t read, ret = 0;
5963 struct ftrace_graph_data *fgd = file->private_data;
5964 struct trace_parser *parser;
5969 /* Read mode uses seq functions */
5970 if (file->f_mode & FMODE_READ) {
5971 struct seq_file *m = file->private_data;
5975 parser = &fgd->parser;
5977 read = trace_get_user(parser, ubuf, cnt, ppos);
5979 if (read >= 0 && trace_parser_loaded(parser) &&
5980 !trace_parser_cont(parser)) {
5982 ret = ftrace_graph_set_hash(fgd->new_hash,
5984 trace_parser_clear(parser);
5993 static const struct file_operations ftrace_graph_fops = {
5994 .open = ftrace_graph_open,
5996 .write = ftrace_graph_write,
5997 .llseek = tracing_lseek,
5998 .release = ftrace_graph_release,
6001 static const struct file_operations ftrace_graph_notrace_fops = {
6002 .open = ftrace_graph_notrace_open,
6004 .write = ftrace_graph_write,
6005 .llseek = tracing_lseek,
6006 .release = ftrace_graph_release,
6008 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6010 void ftrace_create_filter_files(struct ftrace_ops *ops,
6011 struct dentry *parent)
6014 trace_create_file("set_ftrace_filter", 0644, parent,
6015 ops, &ftrace_filter_fops);
6017 trace_create_file("set_ftrace_notrace", 0644, parent,
6018 ops, &ftrace_notrace_fops);
6022 * The name "destroy_filter_files" is really a misnomer. Although
6023 * in the future, it may actually delete the files, but this is
6024 * really intended to make sure the ops passed in are disabled
6025 * and that when this function returns, the caller is free to
6028 * The "destroy" name is only to match the "create" name that this
6029 * should be paired with.
6031 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
6033 mutex_lock(&ftrace_lock);
6034 if (ops->flags & FTRACE_OPS_FL_ENABLED)
6035 ftrace_shutdown(ops, 0);
6036 ops->flags |= FTRACE_OPS_FL_DELETED;
6037 ftrace_free_filter(ops);
6038 mutex_unlock(&ftrace_lock);
6041 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
6044 trace_create_file("available_filter_functions", 0444,
6045 d_tracer, NULL, &ftrace_avail_fops);
6047 trace_create_file("enabled_functions", 0444,
6048 d_tracer, NULL, &ftrace_enabled_fops);
6050 ftrace_create_filter_files(&global_ops, d_tracer);
6052 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6053 trace_create_file("set_graph_function", 0644, d_tracer,
6055 &ftrace_graph_fops);
6056 trace_create_file("set_graph_notrace", 0644, d_tracer,
6058 &ftrace_graph_notrace_fops);
6059 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6064 static int ftrace_cmp_ips(const void *a, const void *b)
6066 const unsigned long *ipa = a;
6067 const unsigned long *ipb = b;
6076 static int ftrace_process_locs(struct module *mod,
6077 unsigned long *start,
6080 struct ftrace_page *start_pg;
6081 struct ftrace_page *pg;
6082 struct dyn_ftrace *rec;
6083 unsigned long count;
6086 unsigned long flags = 0; /* Shut up gcc */
6089 count = end - start;
6094 sort(start, count, sizeof(*start),
6095 ftrace_cmp_ips, NULL);
6097 start_pg = ftrace_allocate_pages(count);
6101 mutex_lock(&ftrace_lock);
6104 * Core and each module needs their own pages, as
6105 * modules will free them when they are removed.
6106 * Force a new page to be allocated for modules.
6109 WARN_ON(ftrace_pages || ftrace_pages_start);
6110 /* First initialization */
6111 ftrace_pages = ftrace_pages_start = start_pg;
6116 if (WARN_ON(ftrace_pages->next)) {
6117 /* Hmm, we have free pages? */
6118 while (ftrace_pages->next)
6119 ftrace_pages = ftrace_pages->next;
6122 ftrace_pages->next = start_pg;
6128 addr = ftrace_call_adjust(*p++);
6130 * Some architecture linkers will pad between
6131 * the different mcount_loc sections of different
6132 * object files to satisfy alignments.
6133 * Skip any NULL pointers.
6138 if (pg->index == pg->size) {
6139 /* We should have allocated enough */
6140 if (WARN_ON(!pg->next))
6145 rec = &pg->records[pg->index++];
6149 /* We should have used all pages */
6152 /* Assign the last page to ftrace_pages */
6156 * We only need to disable interrupts on start up
6157 * because we are modifying code that an interrupt
6158 * may execute, and the modification is not atomic.
6159 * But for modules, nothing runs the code we modify
6160 * until we are finished with it, and there's no
6161 * reason to cause large interrupt latencies while we do it.
6164 local_irq_save(flags);
6165 ftrace_update_code(mod, start_pg);
6167 local_irq_restore(flags);
6170 mutex_unlock(&ftrace_lock);
6175 struct ftrace_mod_func {
6176 struct list_head list;
6182 struct ftrace_mod_map {
6183 struct rcu_head rcu;
6184 struct list_head list;
6186 unsigned long start_addr;
6187 unsigned long end_addr;
6188 struct list_head funcs;
6189 unsigned int num_funcs;
6192 #ifdef CONFIG_MODULES
6194 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
6196 static LIST_HEAD(ftrace_mod_maps);
6198 static int referenced_filters(struct dyn_ftrace *rec)
6200 struct ftrace_ops *ops;
6203 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
6204 if (ops_references_rec(ops, rec))
6212 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
6214 struct ftrace_func_entry *entry;
6215 struct dyn_ftrace *rec;
6218 if (ftrace_hash_empty(hash))
6221 for (i = 0; i < pg->index; i++) {
6222 rec = &pg->records[i];
6223 entry = __ftrace_lookup_ip(hash, rec->ip);
6225 * Do not allow this rec to match again.
6226 * Yeah, it may waste some memory, but will be removed
6227 * if/when the hash is modified again.
6234 /* Clear any records from hashs */
6235 static void clear_mod_from_hashes(struct ftrace_page *pg)
6237 struct trace_array *tr;
6239 mutex_lock(&trace_types_lock);
6240 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6241 if (!tr->ops || !tr->ops->func_hash)
6243 mutex_lock(&tr->ops->func_hash->regex_lock);
6244 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
6245 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
6246 mutex_unlock(&tr->ops->func_hash->regex_lock);
6248 mutex_unlock(&trace_types_lock);
6251 static void ftrace_free_mod_map(struct rcu_head *rcu)
6253 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
6254 struct ftrace_mod_func *mod_func;
6255 struct ftrace_mod_func *n;
6257 /* All the contents of mod_map are now not visible to readers */
6258 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
6259 kfree(mod_func->name);
6260 list_del(&mod_func->list);
6267 void ftrace_release_mod(struct module *mod)
6269 struct ftrace_mod_map *mod_map;
6270 struct ftrace_mod_map *n;
6271 struct dyn_ftrace *rec;
6272 struct ftrace_page **last_pg;
6273 struct ftrace_page *tmp_page = NULL;
6274 struct ftrace_page *pg;
6277 mutex_lock(&ftrace_lock);
6279 if (ftrace_disabled)
6282 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
6283 if (mod_map->mod == mod) {
6284 list_del_rcu(&mod_map->list);
6285 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
6291 * Each module has its own ftrace_pages, remove
6292 * them from the list.
6294 last_pg = &ftrace_pages_start;
6295 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
6296 rec = &pg->records[0];
6297 if (within_module_core(rec->ip, mod) ||
6298 within_module_init(rec->ip, mod)) {
6300 * As core pages are first, the first
6301 * page should never be a module page.
6303 if (WARN_ON(pg == ftrace_pages_start))
6306 /* Check if we are deleting the last page */
6307 if (pg == ftrace_pages)
6308 ftrace_pages = next_to_ftrace_page(last_pg);
6310 ftrace_update_tot_cnt -= pg->index;
6311 *last_pg = pg->next;
6313 pg->next = tmp_page;
6316 last_pg = &pg->next;
6319 mutex_unlock(&ftrace_lock);
6321 for (pg = tmp_page; pg; pg = tmp_page) {
6323 /* Needs to be called outside of ftrace_lock */
6324 clear_mod_from_hashes(pg);
6326 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6327 free_pages((unsigned long)pg->records, order);
6328 tmp_page = pg->next;
6330 ftrace_number_of_pages -= 1 << order;
6331 ftrace_number_of_groups--;
6335 void ftrace_module_enable(struct module *mod)
6337 struct dyn_ftrace *rec;
6338 struct ftrace_page *pg;
6340 mutex_lock(&ftrace_lock);
6342 if (ftrace_disabled)
6346 * If the tracing is enabled, go ahead and enable the record.
6348 * The reason not to enable the record immediately is the
6349 * inherent check of ftrace_make_nop/ftrace_make_call for
6350 * correct previous instructions. Making first the NOP
6351 * conversion puts the module to the correct state, thus
6352 * passing the ftrace_make_call check.
6354 * We also delay this to after the module code already set the
6355 * text to read-only, as we now need to set it back to read-write
6356 * so that we can modify the text.
6358 if (ftrace_start_up)
6359 ftrace_arch_code_modify_prepare();
6361 do_for_each_ftrace_rec(pg, rec) {
6364 * do_for_each_ftrace_rec() is a double loop.
6365 * module text shares the pg. If a record is
6366 * not part of this module, then skip this pg,
6367 * which the "break" will do.
6369 if (!within_module_core(rec->ip, mod) &&
6370 !within_module_init(rec->ip, mod))
6376 * When adding a module, we need to check if tracers are
6377 * currently enabled and if they are, and can trace this record,
6378 * we need to enable the module functions as well as update the
6379 * reference counts for those function records.
6381 if (ftrace_start_up)
6382 cnt += referenced_filters(rec);
6384 /* This clears FTRACE_FL_DISABLED */
6387 if (ftrace_start_up && cnt) {
6388 int failed = __ftrace_replace_code(rec, 1);
6390 ftrace_bug(failed, rec);
6395 } while_for_each_ftrace_rec();
6398 if (ftrace_start_up)
6399 ftrace_arch_code_modify_post_process();
6402 mutex_unlock(&ftrace_lock);
6404 process_cached_mods(mod->name);
6407 void ftrace_module_init(struct module *mod)
6409 if (ftrace_disabled || !mod->num_ftrace_callsites)
6412 ftrace_process_locs(mod, mod->ftrace_callsites,
6413 mod->ftrace_callsites + mod->num_ftrace_callsites);
6416 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6417 struct dyn_ftrace *rec)
6419 struct ftrace_mod_func *mod_func;
6420 unsigned long symsize;
6421 unsigned long offset;
6422 char str[KSYM_SYMBOL_LEN];
6426 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
6430 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
6434 mod_func->name = kstrdup(str, GFP_KERNEL);
6435 if (!mod_func->name) {
6440 mod_func->ip = rec->ip - offset;
6441 mod_func->size = symsize;
6443 mod_map->num_funcs++;
6445 list_add_rcu(&mod_func->list, &mod_map->funcs);
6448 static struct ftrace_mod_map *
6449 allocate_ftrace_mod_map(struct module *mod,
6450 unsigned long start, unsigned long end)
6452 struct ftrace_mod_map *mod_map;
6454 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
6459 mod_map->start_addr = start;
6460 mod_map->end_addr = end;
6461 mod_map->num_funcs = 0;
6463 INIT_LIST_HEAD_RCU(&mod_map->funcs);
6465 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
6471 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
6472 unsigned long addr, unsigned long *size,
6473 unsigned long *off, char *sym)
6475 struct ftrace_mod_func *found_func = NULL;
6476 struct ftrace_mod_func *mod_func;
6478 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6479 if (addr >= mod_func->ip &&
6480 addr < mod_func->ip + mod_func->size) {
6481 found_func = mod_func;
6488 *size = found_func->size;
6490 *off = addr - found_func->ip;
6492 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
6494 return found_func->name;
6501 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6502 unsigned long *off, char **modname, char *sym)
6504 struct ftrace_mod_map *mod_map;
6505 const char *ret = NULL;
6507 /* mod_map is freed via call_rcu() */
6509 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6510 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6513 *modname = mod_map->mod->name;
6522 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6523 char *type, char *name,
6524 char *module_name, int *exported)
6526 struct ftrace_mod_map *mod_map;
6527 struct ftrace_mod_func *mod_func;
6530 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6532 if (symnum >= mod_map->num_funcs) {
6533 symnum -= mod_map->num_funcs;
6537 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6543 *value = mod_func->ip;
6545 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6546 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6559 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6560 struct dyn_ftrace *rec) { }
6561 static inline struct ftrace_mod_map *
6562 allocate_ftrace_mod_map(struct module *mod,
6563 unsigned long start, unsigned long end)
6567 #endif /* CONFIG_MODULES */
6569 struct ftrace_init_func {
6570 struct list_head list;
6574 /* Clear any init ips from hashes */
6576 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6578 struct ftrace_func_entry *entry;
6580 entry = ftrace_lookup_ip(hash, func->ip);
6582 * Do not allow this rec to match again.
6583 * Yeah, it may waste some memory, but will be removed
6584 * if/when the hash is modified again.
6591 clear_func_from_hashes(struct ftrace_init_func *func)
6593 struct trace_array *tr;
6595 mutex_lock(&trace_types_lock);
6596 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6597 if (!tr->ops || !tr->ops->func_hash)
6599 mutex_lock(&tr->ops->func_hash->regex_lock);
6600 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6601 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6602 mutex_unlock(&tr->ops->func_hash->regex_lock);
6604 mutex_unlock(&trace_types_lock);
6607 static void add_to_clear_hash_list(struct list_head *clear_list,
6608 struct dyn_ftrace *rec)
6610 struct ftrace_init_func *func;
6612 func = kmalloc(sizeof(*func), GFP_KERNEL);
6614 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
6619 list_add(&func->list, clear_list);
6622 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
6624 unsigned long start = (unsigned long)(start_ptr);
6625 unsigned long end = (unsigned long)(end_ptr);
6626 struct ftrace_page **last_pg = &ftrace_pages_start;
6627 struct ftrace_page *pg;
6628 struct dyn_ftrace *rec;
6629 struct dyn_ftrace key;
6630 struct ftrace_mod_map *mod_map = NULL;
6631 struct ftrace_init_func *func, *func_next;
6632 struct list_head clear_hash;
6635 INIT_LIST_HEAD(&clear_hash);
6638 key.flags = end; /* overload flags, as it is unsigned long */
6640 mutex_lock(&ftrace_lock);
6643 * If we are freeing module init memory, then check if
6644 * any tracer is active. If so, we need to save a mapping of
6645 * the module functions being freed with the address.
6647 if (mod && ftrace_ops_list != &ftrace_list_end)
6648 mod_map = allocate_ftrace_mod_map(mod, start, end);
6650 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6651 if (end < pg->records[0].ip ||
6652 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6655 rec = bsearch(&key, pg->records, pg->index,
6656 sizeof(struct dyn_ftrace),
6661 /* rec will be cleared from hashes after ftrace_lock unlock */
6662 add_to_clear_hash_list(&clear_hash, rec);
6665 save_ftrace_mod_rec(mod_map, rec);
6668 ftrace_update_tot_cnt--;
6670 *last_pg = pg->next;
6671 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6672 free_pages((unsigned long)pg->records, order);
6673 ftrace_number_of_pages -= 1 << order;
6674 ftrace_number_of_groups--;
6676 pg = container_of(last_pg, struct ftrace_page, next);
6681 memmove(rec, rec + 1,
6682 (pg->index - (rec - pg->records)) * sizeof(*rec));
6683 /* More than one function may be in this block */
6686 mutex_unlock(&ftrace_lock);
6688 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6689 clear_func_from_hashes(func);
6694 void __init ftrace_free_init_mem(void)
6696 void *start = (void *)(&__init_begin);
6697 void *end = (void *)(&__init_end);
6699 ftrace_free_mem(NULL, start, end);
6702 void __init ftrace_init(void)
6704 extern unsigned long __start_mcount_loc[];
6705 extern unsigned long __stop_mcount_loc[];
6706 unsigned long count, flags;
6709 local_irq_save(flags);
6710 ret = ftrace_dyn_arch_init();
6711 local_irq_restore(flags);
6715 count = __stop_mcount_loc - __start_mcount_loc;
6717 pr_info("ftrace: No functions to be traced?\n");
6721 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6722 count, count / ENTRIES_PER_PAGE + 1);
6724 last_ftrace_enabled = ftrace_enabled = 1;
6726 ret = ftrace_process_locs(NULL,
6730 pr_info("ftrace: allocated %ld pages with %ld groups\n",
6731 ftrace_number_of_pages, ftrace_number_of_groups);
6733 set_ftrace_early_filters();
6737 ftrace_disabled = 1;
6740 /* Do nothing if arch does not support this */
6741 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6745 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6747 arch_ftrace_update_trampoline(ops);
6750 void ftrace_init_trace_array(struct trace_array *tr)
6752 INIT_LIST_HEAD(&tr->func_probes);
6753 INIT_LIST_HEAD(&tr->mod_trace);
6754 INIT_LIST_HEAD(&tr->mod_notrace);
6758 struct ftrace_ops global_ops = {
6759 .func = ftrace_stub,
6760 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6761 FTRACE_OPS_FL_INITIALIZED |
6765 static int __init ftrace_nodyn_init(void)
6770 core_initcall(ftrace_nodyn_init);
6772 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6773 static inline void ftrace_startup_enable(int command) { }
6774 static inline void ftrace_startup_all(int command) { }
6776 # define ftrace_startup_sysctl() do { } while (0)
6777 # define ftrace_shutdown_sysctl() do { } while (0)
6779 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6783 #endif /* CONFIG_DYNAMIC_FTRACE */
6785 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6787 tr->ops = &global_ops;
6788 tr->ops->private = tr;
6789 ftrace_init_trace_array(tr);
6792 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6794 /* If we filter on pids, update to use the pid function */
6795 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6796 if (WARN_ON(tr->ops->func != ftrace_stub))
6797 printk("ftrace ops had %pS for function\n",
6800 tr->ops->func = func;
6801 tr->ops->private = tr;
6804 void ftrace_reset_array_ops(struct trace_array *tr)
6806 tr->ops->func = ftrace_stub;
6809 static nokprobe_inline void
6810 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6811 struct ftrace_ops *ignored, struct pt_regs *regs)
6813 struct ftrace_ops *op;
6816 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6821 * Some of the ops may be dynamically allocated,
6822 * they must be freed after a synchronize_rcu().
6824 preempt_disable_notrace();
6826 do_for_each_ftrace_op(op, ftrace_ops_list) {
6827 /* Stub functions don't need to be called nor tested */
6828 if (op->flags & FTRACE_OPS_FL_STUB)
6831 * Check the following for each ops before calling their func:
6832 * if RCU flag is set, then rcu_is_watching() must be true
6833 * if PER_CPU is set, then ftrace_function_local_disable()
6835 * Otherwise test if the ip matches the ops filter
6837 * If any of the above fails then the op->func() is not executed.
6839 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6840 ftrace_ops_test(op, ip, regs)) {
6841 if (FTRACE_WARN_ON(!op->func)) {
6842 pr_warn("op=%p %pS\n", op, op);
6845 op->func(ip, parent_ip, op, regs);
6847 } while_for_each_ftrace_op(op);
6849 preempt_enable_notrace();
6850 trace_clear_recursion(bit);
6854 * Some archs only support passing ip and parent_ip. Even though
6855 * the list function ignores the op parameter, we do not want any
6856 * C side effects, where a function is called without the caller
6857 * sending a third parameter.
6858 * Archs are to support both the regs and ftrace_ops at the same time.
6859 * If they support ftrace_ops, it is assumed they support regs.
6860 * If call backs want to use regs, they must either check for regs
6861 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6862 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6863 * An architecture can pass partial regs with ftrace_ops and still
6864 * set the ARCH_SUPPORTS_FTRACE_OPS.
6866 #if ARCH_SUPPORTS_FTRACE_OPS
6867 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6868 struct ftrace_ops *op, struct pt_regs *regs)
6870 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6872 NOKPROBE_SYMBOL(ftrace_ops_list_func);
6874 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6876 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6878 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
6882 * If there's only one function registered but it does not support
6883 * recursion, needs RCU protection and/or requires per cpu handling, then
6884 * this function will be called by the mcount trampoline.
6886 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6887 struct ftrace_ops *op, struct pt_regs *regs)
6891 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6894 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6898 preempt_disable_notrace();
6900 op->func(ip, parent_ip, op, regs);
6902 preempt_enable_notrace();
6903 trace_clear_recursion(bit);
6905 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
6908 * ftrace_ops_get_func - get the function a trampoline should call
6909 * @ops: the ops to get the function for
6911 * Normally the mcount trampoline will call the ops->func, but there
6912 * are times that it should not. For example, if the ops does not
6913 * have its own recursion protection, then it should call the
6914 * ftrace_ops_assist_func() instead.
6916 * Returns the function that the trampoline should call for @ops.
6918 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6921 * If the function does not handle recursion, needs to be RCU safe,
6922 * or does per cpu logic, then we need to call the assist handler.
6924 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6925 ops->flags & FTRACE_OPS_FL_RCU)
6926 return ftrace_ops_assist_func;
6932 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6933 struct task_struct *prev, struct task_struct *next)
6935 struct trace_array *tr = data;
6936 struct trace_pid_list *pid_list;
6937 struct trace_pid_list *no_pid_list;
6939 pid_list = rcu_dereference_sched(tr->function_pids);
6940 no_pid_list = rcu_dereference_sched(tr->function_no_pids);
6942 if (trace_ignore_this_task(pid_list, no_pid_list, next))
6943 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
6946 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
6951 ftrace_pid_follow_sched_process_fork(void *data,
6952 struct task_struct *self,
6953 struct task_struct *task)
6955 struct trace_pid_list *pid_list;
6956 struct trace_array *tr = data;
6958 pid_list = rcu_dereference_sched(tr->function_pids);
6959 trace_filter_add_remove_task(pid_list, self, task);
6961 pid_list = rcu_dereference_sched(tr->function_no_pids);
6962 trace_filter_add_remove_task(pid_list, self, task);
6966 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6968 struct trace_pid_list *pid_list;
6969 struct trace_array *tr = data;
6971 pid_list = rcu_dereference_sched(tr->function_pids);
6972 trace_filter_add_remove_task(pid_list, NULL, task);
6974 pid_list = rcu_dereference_sched(tr->function_no_pids);
6975 trace_filter_add_remove_task(pid_list, NULL, task);
6978 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6981 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6983 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6986 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6988 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6993 static void clear_ftrace_pids(struct trace_array *tr, int type)
6995 struct trace_pid_list *pid_list;
6996 struct trace_pid_list *no_pid_list;
6999 pid_list = rcu_dereference_protected(tr->function_pids,
7000 lockdep_is_held(&ftrace_lock));
7001 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7002 lockdep_is_held(&ftrace_lock));
7004 /* Make sure there's something to do */
7005 if (!pid_type_enabled(type, pid_list, no_pid_list))
7008 /* See if the pids still need to be checked after this */
7009 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
7010 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7011 for_each_possible_cpu(cpu)
7012 per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE;
7015 if (type & TRACE_PIDS)
7016 rcu_assign_pointer(tr->function_pids, NULL);
7018 if (type & TRACE_NO_PIDS)
7019 rcu_assign_pointer(tr->function_no_pids, NULL);
7021 /* Wait till all users are no longer using pid filtering */
7024 if ((type & TRACE_PIDS) && pid_list)
7025 trace_free_pid_list(pid_list);
7027 if ((type & TRACE_NO_PIDS) && no_pid_list)
7028 trace_free_pid_list(no_pid_list);
7031 void ftrace_clear_pids(struct trace_array *tr)
7033 mutex_lock(&ftrace_lock);
7035 clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
7037 mutex_unlock(&ftrace_lock);
7040 static void ftrace_pid_reset(struct trace_array *tr, int type)
7042 mutex_lock(&ftrace_lock);
7043 clear_ftrace_pids(tr, type);
7045 ftrace_update_pid_func();
7046 ftrace_startup_all(0);
7048 mutex_unlock(&ftrace_lock);
7051 /* Greater than any max PID */
7052 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
7054 static void *fpid_start(struct seq_file *m, loff_t *pos)
7057 struct trace_pid_list *pid_list;
7058 struct trace_array *tr = m->private;
7060 mutex_lock(&ftrace_lock);
7061 rcu_read_lock_sched();
7063 pid_list = rcu_dereference_sched(tr->function_pids);
7066 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7068 return trace_pid_start(pid_list, pos);
7071 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
7073 struct trace_array *tr = m->private;
7074 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
7076 if (v == FTRACE_NO_PIDS) {
7080 return trace_pid_next(pid_list, v, pos);
7083 static void fpid_stop(struct seq_file *m, void *p)
7086 rcu_read_unlock_sched();
7087 mutex_unlock(&ftrace_lock);
7090 static int fpid_show(struct seq_file *m, void *v)
7092 if (v == FTRACE_NO_PIDS) {
7093 seq_puts(m, "no pid\n");
7097 return trace_pid_show(m, v);
7100 static const struct seq_operations ftrace_pid_sops = {
7101 .start = fpid_start,
7107 static void *fnpid_start(struct seq_file *m, loff_t *pos)
7110 struct trace_pid_list *pid_list;
7111 struct trace_array *tr = m->private;
7113 mutex_lock(&ftrace_lock);
7114 rcu_read_lock_sched();
7116 pid_list = rcu_dereference_sched(tr->function_no_pids);
7119 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7121 return trace_pid_start(pid_list, pos);
7124 static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos)
7126 struct trace_array *tr = m->private;
7127 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids);
7129 if (v == FTRACE_NO_PIDS) {
7133 return trace_pid_next(pid_list, v, pos);
7136 static const struct seq_operations ftrace_no_pid_sops = {
7137 .start = fnpid_start,
7143 static int pid_open(struct inode *inode, struct file *file, int type)
7145 const struct seq_operations *seq_ops;
7146 struct trace_array *tr = inode->i_private;
7150 ret = tracing_check_open_get_tr(tr);
7154 if ((file->f_mode & FMODE_WRITE) &&
7155 (file->f_flags & O_TRUNC))
7156 ftrace_pid_reset(tr, type);
7160 seq_ops = &ftrace_pid_sops;
7163 seq_ops = &ftrace_no_pid_sops;
7167 ret = seq_open(file, seq_ops);
7169 trace_array_put(tr);
7171 m = file->private_data;
7172 /* copy tr over to seq ops */
7180 ftrace_pid_open(struct inode *inode, struct file *file)
7182 return pid_open(inode, file, TRACE_PIDS);
7186 ftrace_no_pid_open(struct inode *inode, struct file *file)
7188 return pid_open(inode, file, TRACE_NO_PIDS);
7191 static void ignore_task_cpu(void *data)
7193 struct trace_array *tr = data;
7194 struct trace_pid_list *pid_list;
7195 struct trace_pid_list *no_pid_list;
7198 * This function is called by on_each_cpu() while the
7199 * event_mutex is held.
7201 pid_list = rcu_dereference_protected(tr->function_pids,
7202 mutex_is_locked(&ftrace_lock));
7203 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7204 mutex_is_locked(&ftrace_lock));
7206 if (trace_ignore_this_task(pid_list, no_pid_list, current))
7207 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7210 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7215 pid_write(struct file *filp, const char __user *ubuf,
7216 size_t cnt, loff_t *ppos, int type)
7218 struct seq_file *m = filp->private_data;
7219 struct trace_array *tr = m->private;
7220 struct trace_pid_list *filtered_pids;
7221 struct trace_pid_list *other_pids;
7222 struct trace_pid_list *pid_list;
7228 mutex_lock(&ftrace_lock);
7232 filtered_pids = rcu_dereference_protected(tr->function_pids,
7233 lockdep_is_held(&ftrace_lock));
7234 other_pids = rcu_dereference_protected(tr->function_no_pids,
7235 lockdep_is_held(&ftrace_lock));
7238 filtered_pids = rcu_dereference_protected(tr->function_no_pids,
7239 lockdep_is_held(&ftrace_lock));
7240 other_pids = rcu_dereference_protected(tr->function_pids,
7241 lockdep_is_held(&ftrace_lock));
7245 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
7251 rcu_assign_pointer(tr->function_pids, pid_list);
7254 rcu_assign_pointer(tr->function_no_pids, pid_list);
7259 if (filtered_pids) {
7261 trace_free_pid_list(filtered_pids);
7262 } else if (pid_list && !other_pids) {
7263 /* Register a probe to set whether to ignore the tracing of a task */
7264 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7268 * Ignoring of pids is done at task switch. But we have to
7269 * check for those tasks that are currently running.
7270 * Always do this in case a pid was appended or removed.
7272 on_each_cpu(ignore_task_cpu, tr, 1);
7274 ftrace_update_pid_func();
7275 ftrace_startup_all(0);
7277 mutex_unlock(&ftrace_lock);
7286 ftrace_pid_write(struct file *filp, const char __user *ubuf,
7287 size_t cnt, loff_t *ppos)
7289 return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
7293 ftrace_no_pid_write(struct file *filp, const char __user *ubuf,
7294 size_t cnt, loff_t *ppos)
7296 return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
7300 ftrace_pid_release(struct inode *inode, struct file *file)
7302 struct trace_array *tr = inode->i_private;
7304 trace_array_put(tr);
7306 return seq_release(inode, file);
7309 static const struct file_operations ftrace_pid_fops = {
7310 .open = ftrace_pid_open,
7311 .write = ftrace_pid_write,
7313 .llseek = tracing_lseek,
7314 .release = ftrace_pid_release,
7317 static const struct file_operations ftrace_no_pid_fops = {
7318 .open = ftrace_no_pid_open,
7319 .write = ftrace_no_pid_write,
7321 .llseek = tracing_lseek,
7322 .release = ftrace_pid_release,
7325 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
7327 trace_create_file("set_ftrace_pid", 0644, d_tracer,
7328 tr, &ftrace_pid_fops);
7329 trace_create_file("set_ftrace_notrace_pid", 0644, d_tracer,
7330 tr, &ftrace_no_pid_fops);
7333 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
7334 struct dentry *d_tracer)
7336 /* Only the top level directory has the dyn_tracefs and profile */
7337 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
7339 ftrace_init_dyn_tracefs(d_tracer);
7340 ftrace_profile_tracefs(d_tracer);
7344 * ftrace_kill - kill ftrace
7346 * This function should be used by panic code. It stops ftrace
7347 * but in a not so nice way. If you need to simply kill ftrace
7348 * from a non-atomic section, use ftrace_kill.
7350 void ftrace_kill(void)
7352 ftrace_disabled = 1;
7354 ftrace_trace_function = ftrace_stub;
7358 * Test if ftrace is dead or not.
7360 int ftrace_is_dead(void)
7362 return ftrace_disabled;
7366 * register_ftrace_function - register a function for profiling
7367 * @ops - ops structure that holds the function for profiling.
7369 * Register a function to be called by all functions in the
7372 * Note: @ops->func and all the functions it calls must be labeled
7373 * with "notrace", otherwise it will go into a
7376 int register_ftrace_function(struct ftrace_ops *ops)
7380 ftrace_ops_init(ops);
7382 mutex_lock(&ftrace_lock);
7384 ret = ftrace_startup(ops, 0);
7386 mutex_unlock(&ftrace_lock);
7390 EXPORT_SYMBOL_GPL(register_ftrace_function);
7393 * unregister_ftrace_function - unregister a function for profiling.
7394 * @ops - ops structure that holds the function to unregister
7396 * Unregister a function that was added to be called by ftrace profiling.
7398 int unregister_ftrace_function(struct ftrace_ops *ops)
7402 mutex_lock(&ftrace_lock);
7403 ret = ftrace_shutdown(ops, 0);
7404 mutex_unlock(&ftrace_lock);
7408 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
7410 static bool is_permanent_ops_registered(void)
7412 struct ftrace_ops *op;
7414 do_for_each_ftrace_op(op, ftrace_ops_list) {
7415 if (op->flags & FTRACE_OPS_FL_PERMANENT)
7417 } while_for_each_ftrace_op(op);
7423 ftrace_enable_sysctl(struct ctl_table *table, int write,
7424 void __user *buffer, size_t *lenp,
7429 mutex_lock(&ftrace_lock);
7431 if (unlikely(ftrace_disabled))
7434 ret = proc_dointvec(table, write, buffer, lenp, ppos);
7436 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
7439 if (ftrace_enabled) {
7441 /* we are starting ftrace again */
7442 if (rcu_dereference_protected(ftrace_ops_list,
7443 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
7444 update_ftrace_function();
7446 ftrace_startup_sysctl();
7449 if (is_permanent_ops_registered()) {
7450 ftrace_enabled = true;
7455 /* stopping ftrace calls (just send to ftrace_stub) */
7456 ftrace_trace_function = ftrace_stub;
7458 ftrace_shutdown_sysctl();
7461 last_ftrace_enabled = !!ftrace_enabled;
7463 mutex_unlock(&ftrace_lock);