2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/tracefs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
35 #include <linux/kprobes.h>
37 #include <trace/events/sched.h>
39 #include <asm/setup.h>
41 #include "trace_output.h"
42 #include "trace_stat.h"
44 #define FTRACE_WARN_ON(cond) \
52 #define FTRACE_WARN_ON_ONCE(cond) \
55 if (WARN_ON_ONCE(___r)) \
60 /* hash bits for specific function selection */
61 #define FTRACE_HASH_BITS 7
62 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
63 #define FTRACE_HASH_DEFAULT_BITS 10
64 #define FTRACE_HASH_MAX_BITS 12
66 #ifdef CONFIG_DYNAMIC_FTRACE
67 #define INIT_OPS_HASH(opsname) \
68 .func_hash = &opsname.local_hash, \
69 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
70 #define ASSIGN_OPS_HASH(opsname, val) \
72 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
74 #define INIT_OPS_HASH(opsname)
75 #define ASSIGN_OPS_HASH(opsname, val)
78 static struct ftrace_ops ftrace_list_end __read_mostly = {
80 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
81 INIT_OPS_HASH(ftrace_list_end)
84 /* ftrace_enabled is a method to turn ftrace on or off */
85 int ftrace_enabled __read_mostly;
86 static int last_ftrace_enabled;
88 /* Current function tracing op */
89 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
90 /* What to set function_trace_op to */
91 static struct ftrace_ops *set_function_trace_op;
93 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
95 struct trace_array *tr;
97 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
102 return tr->function_pids != NULL;
105 static void ftrace_update_trampoline(struct ftrace_ops *ops);
108 * ftrace_disabled is set when an anomaly is discovered.
109 * ftrace_disabled is much stronger than ftrace_enabled.
111 static int ftrace_disabled __read_mostly;
113 static DEFINE_MUTEX(ftrace_lock);
115 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
116 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
117 static struct ftrace_ops global_ops;
119 #if ARCH_SUPPORTS_FTRACE_OPS
120 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
121 struct ftrace_ops *op, struct pt_regs *regs);
123 /* See comment below, where ftrace_ops_list_func is defined */
124 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
125 struct ftrace_ops *op, struct pt_regs *regs);
126 #define ftrace_ops_list_func ftrace_ops_no_ops
130 * Traverse the ftrace_global_list, invoking all entries. The reason that we
131 * can use rcu_dereference_raw_notrace() is that elements removed from this list
132 * are simply leaked, so there is no need to interact with a grace-period
133 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
134 * concurrent insertions into the ftrace_global_list.
136 * Silly Alpha and silly pointer-speculation compiler optimizations!
138 #define do_for_each_ftrace_op(op, list) \
139 op = rcu_dereference_raw_notrace(list); \
143 * Optimized for just a single item in the list (as that is the normal case).
145 #define while_for_each_ftrace_op(op) \
146 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
147 unlikely((op) != &ftrace_list_end))
149 static inline void ftrace_ops_init(struct ftrace_ops *ops)
151 #ifdef CONFIG_DYNAMIC_FTRACE
152 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
153 mutex_init(&ops->local_hash.regex_lock);
154 ops->func_hash = &ops->local_hash;
155 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
161 * ftrace_nr_registered_ops - return number of ops registered
163 * Returns the number of ftrace_ops registered and tracing functions
165 int ftrace_nr_registered_ops(void)
167 struct ftrace_ops *ops;
170 mutex_lock(&ftrace_lock);
172 for (ops = ftrace_ops_list;
173 ops != &ftrace_list_end; ops = ops->next)
176 mutex_unlock(&ftrace_lock);
181 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
182 struct ftrace_ops *op, struct pt_regs *regs)
184 struct trace_array *tr = op->private;
186 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
189 op->saved_func(ip, parent_ip, op, regs);
193 * clear_ftrace_function - reset the ftrace function
195 * This NULLs the ftrace function and in essence stops
196 * tracing. There may be lag
198 void clear_ftrace_function(void)
200 ftrace_trace_function = ftrace_stub;
203 static void per_cpu_ops_disable_all(struct ftrace_ops *ops)
207 for_each_possible_cpu(cpu)
208 *per_cpu_ptr(ops->disabled, cpu) = 1;
211 static int per_cpu_ops_alloc(struct ftrace_ops *ops)
213 int __percpu *disabled;
215 if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU)))
218 disabled = alloc_percpu(int);
222 ops->disabled = disabled;
223 per_cpu_ops_disable_all(ops);
227 static void ftrace_sync(struct work_struct *work)
230 * This function is just a stub to implement a hard force
231 * of synchronize_sched(). This requires synchronizing
232 * tasks even in userspace and idle.
234 * Yes, function tracing is rude.
238 static void ftrace_sync_ipi(void *data)
240 /* Probably not needed, but do it anyway */
244 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
245 static void update_function_graph_func(void);
247 /* Both enabled by default (can be cleared by function_graph tracer flags */
248 static bool fgraph_sleep_time = true;
249 static bool fgraph_graph_time = true;
252 static inline void update_function_graph_func(void) { }
256 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
259 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
260 * then it needs to call the list anyway.
262 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU |
263 FTRACE_OPS_FL_RCU) || FTRACE_FORCE_LIST_FUNC)
264 return ftrace_ops_list_func;
266 return ftrace_ops_get_func(ops);
269 static void update_ftrace_function(void)
274 * Prepare the ftrace_ops that the arch callback will use.
275 * If there's only one ftrace_ops registered, the ftrace_ops_list
276 * will point to the ops we want.
278 set_function_trace_op = ftrace_ops_list;
280 /* If there's no ftrace_ops registered, just call the stub function */
281 if (ftrace_ops_list == &ftrace_list_end) {
285 * If we are at the end of the list and this ops is
286 * recursion safe and not dynamic and the arch supports passing ops,
287 * then have the mcount trampoline call the function directly.
289 } else if (ftrace_ops_list->next == &ftrace_list_end) {
290 func = ftrace_ops_get_list_func(ftrace_ops_list);
293 /* Just use the default ftrace_ops */
294 set_function_trace_op = &ftrace_list_end;
295 func = ftrace_ops_list_func;
298 update_function_graph_func();
300 /* If there's no change, then do nothing more here */
301 if (ftrace_trace_function == func)
305 * If we are using the list function, it doesn't care
306 * about the function_trace_ops.
308 if (func == ftrace_ops_list_func) {
309 ftrace_trace_function = func;
311 * Don't even bother setting function_trace_ops,
312 * it would be racy to do so anyway.
317 #ifndef CONFIG_DYNAMIC_FTRACE
319 * For static tracing, we need to be a bit more careful.
320 * The function change takes affect immediately. Thus,
321 * we need to coorditate the setting of the function_trace_ops
322 * with the setting of the ftrace_trace_function.
324 * Set the function to the list ops, which will call the
325 * function we want, albeit indirectly, but it handles the
326 * ftrace_ops and doesn't depend on function_trace_op.
328 ftrace_trace_function = ftrace_ops_list_func;
330 * Make sure all CPUs see this. Yes this is slow, but static
331 * tracing is slow and nasty to have enabled.
333 schedule_on_each_cpu(ftrace_sync);
334 /* Now all cpus are using the list ops. */
335 function_trace_op = set_function_trace_op;
336 /* Make sure the function_trace_op is visible on all CPUs */
338 /* Nasty way to force a rmb on all cpus */
339 smp_call_function(ftrace_sync_ipi, NULL, 1);
340 /* OK, we are all set to update the ftrace_trace_function now! */
341 #endif /* !CONFIG_DYNAMIC_FTRACE */
343 ftrace_trace_function = func;
346 int using_ftrace_ops_list_func(void)
348 return ftrace_trace_function == ftrace_ops_list_func;
351 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
355 * We are entering ops into the list but another
356 * CPU might be walking that list. We need to make sure
357 * the ops->next pointer is valid before another CPU sees
358 * the ops pointer included into the list.
360 rcu_assign_pointer(*list, ops);
363 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
365 struct ftrace_ops **p;
368 * If we are removing the last function, then simply point
369 * to the ftrace_stub.
371 if (*list == ops && ops->next == &ftrace_list_end) {
372 *list = &ftrace_list_end;
376 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
387 static void ftrace_update_trampoline(struct ftrace_ops *ops);
389 static int __register_ftrace_function(struct ftrace_ops *ops)
391 if (ops->flags & FTRACE_OPS_FL_DELETED)
394 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
397 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
399 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
400 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
401 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
403 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
404 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
407 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
408 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
411 if (!core_kernel_data((unsigned long)ops))
412 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
414 if (ops->flags & FTRACE_OPS_FL_PER_CPU) {
415 if (per_cpu_ops_alloc(ops))
419 add_ftrace_ops(&ftrace_ops_list, ops);
421 /* Always save the function, and reset at unregistering */
422 ops->saved_func = ops->func;
424 if (ftrace_pids_enabled(ops))
425 ops->func = ftrace_pid_func;
427 ftrace_update_trampoline(ops);
430 update_ftrace_function();
435 static int __unregister_ftrace_function(struct ftrace_ops *ops)
439 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
442 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
448 update_ftrace_function();
450 ops->func = ops->saved_func;
455 static void ftrace_update_pid_func(void)
457 struct ftrace_ops *op;
459 /* Only do something if we are tracing something */
460 if (ftrace_trace_function == ftrace_stub)
463 do_for_each_ftrace_op(op, ftrace_ops_list) {
464 if (op->flags & FTRACE_OPS_FL_PID) {
465 op->func = ftrace_pids_enabled(op) ?
466 ftrace_pid_func : op->saved_func;
467 ftrace_update_trampoline(op);
469 } while_for_each_ftrace_op(op);
471 update_ftrace_function();
474 #ifdef CONFIG_FUNCTION_PROFILER
475 struct ftrace_profile {
476 struct hlist_node node;
478 unsigned long counter;
479 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
480 unsigned long long time;
481 unsigned long long time_squared;
485 struct ftrace_profile_page {
486 struct ftrace_profile_page *next;
488 struct ftrace_profile records[];
491 struct ftrace_profile_stat {
493 struct hlist_head *hash;
494 struct ftrace_profile_page *pages;
495 struct ftrace_profile_page *start;
496 struct tracer_stat stat;
499 #define PROFILE_RECORDS_SIZE \
500 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
502 #define PROFILES_PER_PAGE \
503 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
505 static int ftrace_profile_enabled __read_mostly;
507 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
508 static DEFINE_MUTEX(ftrace_profile_lock);
510 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
512 #define FTRACE_PROFILE_HASH_BITS 10
513 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
516 function_stat_next(void *v, int idx)
518 struct ftrace_profile *rec = v;
519 struct ftrace_profile_page *pg;
521 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
527 if ((void *)rec >= (void *)&pg->records[pg->index]) {
531 rec = &pg->records[0];
539 static void *function_stat_start(struct tracer_stat *trace)
541 struct ftrace_profile_stat *stat =
542 container_of(trace, struct ftrace_profile_stat, stat);
544 if (!stat || !stat->start)
547 return function_stat_next(&stat->start->records[0], 0);
550 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
551 /* function graph compares on total time */
552 static int function_stat_cmp(void *p1, void *p2)
554 struct ftrace_profile *a = p1;
555 struct ftrace_profile *b = p2;
557 if (a->time < b->time)
559 if (a->time > b->time)
565 /* not function graph compares against hits */
566 static int function_stat_cmp(void *p1, void *p2)
568 struct ftrace_profile *a = p1;
569 struct ftrace_profile *b = p2;
571 if (a->counter < b->counter)
573 if (a->counter > b->counter)
580 static int function_stat_headers(struct seq_file *m)
582 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
583 seq_puts(m, " Function "
586 "--- ---- --- ---\n");
588 seq_puts(m, " Function Hit\n"
594 static int function_stat_show(struct seq_file *m, void *v)
596 struct ftrace_profile *rec = v;
597 char str[KSYM_SYMBOL_LEN];
599 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
600 static struct trace_seq s;
601 unsigned long long avg;
602 unsigned long long stddev;
604 mutex_lock(&ftrace_profile_lock);
606 /* we raced with function_profile_reset() */
607 if (unlikely(rec->counter == 0)) {
612 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
613 avg = div64_ul(rec->time, rec->counter);
614 if (tracing_thresh && (avg < tracing_thresh))
618 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
619 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
621 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
624 /* Sample standard deviation (s^2) */
625 if (rec->counter <= 1)
629 * Apply Welford's method:
630 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
632 stddev = rec->counter * rec->time_squared -
633 rec->time * rec->time;
636 * Divide only 1000 for ns^2 -> us^2 conversion.
637 * trace_print_graph_duration will divide 1000 again.
639 stddev = div64_ul(stddev,
640 rec->counter * (rec->counter - 1) * 1000);
644 trace_print_graph_duration(rec->time, &s);
645 trace_seq_puts(&s, " ");
646 trace_print_graph_duration(avg, &s);
647 trace_seq_puts(&s, " ");
648 trace_print_graph_duration(stddev, &s);
649 trace_print_seq(m, &s);
653 mutex_unlock(&ftrace_profile_lock);
658 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
660 struct ftrace_profile_page *pg;
662 pg = stat->pages = stat->start;
665 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
670 memset(stat->hash, 0,
671 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
674 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
676 struct ftrace_profile_page *pg;
681 /* If we already allocated, do nothing */
685 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
689 #ifdef CONFIG_DYNAMIC_FTRACE
690 functions = ftrace_update_tot_cnt;
693 * We do not know the number of functions that exist because
694 * dynamic tracing is what counts them. With past experience
695 * we have around 20K functions. That should be more than enough.
696 * It is highly unlikely we will execute every function in
702 pg = stat->start = stat->pages;
704 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
706 for (i = 1; i < pages; i++) {
707 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
718 unsigned long tmp = (unsigned long)pg;
730 static int ftrace_profile_init_cpu(int cpu)
732 struct ftrace_profile_stat *stat;
735 stat = &per_cpu(ftrace_profile_stats, cpu);
738 /* If the profile is already created, simply reset it */
739 ftrace_profile_reset(stat);
744 * We are profiling all functions, but usually only a few thousand
745 * functions are hit. We'll make a hash of 1024 items.
747 size = FTRACE_PROFILE_HASH_SIZE;
749 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
754 /* Preallocate the function profiling pages */
755 if (ftrace_profile_pages_init(stat) < 0) {
764 static int ftrace_profile_init(void)
769 for_each_possible_cpu(cpu) {
770 ret = ftrace_profile_init_cpu(cpu);
778 /* interrupts must be disabled */
779 static struct ftrace_profile *
780 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
782 struct ftrace_profile *rec;
783 struct hlist_head *hhd;
786 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
787 hhd = &stat->hash[key];
789 if (hlist_empty(hhd))
792 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
800 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
801 struct ftrace_profile *rec)
805 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
806 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
810 * The memory is already allocated, this simply finds a new record to use.
812 static struct ftrace_profile *
813 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
815 struct ftrace_profile *rec = NULL;
817 /* prevent recursion (from NMIs) */
818 if (atomic_inc_return(&stat->disabled) != 1)
822 * Try to find the function again since an NMI
823 * could have added it
825 rec = ftrace_find_profiled_func(stat, ip);
829 if (stat->pages->index == PROFILES_PER_PAGE) {
830 if (!stat->pages->next)
832 stat->pages = stat->pages->next;
835 rec = &stat->pages->records[stat->pages->index++];
837 ftrace_add_profile(stat, rec);
840 atomic_dec(&stat->disabled);
846 function_profile_call(unsigned long ip, unsigned long parent_ip,
847 struct ftrace_ops *ops, struct pt_regs *regs)
849 struct ftrace_profile_stat *stat;
850 struct ftrace_profile *rec;
853 if (!ftrace_profile_enabled)
856 local_irq_save(flags);
858 stat = this_cpu_ptr(&ftrace_profile_stats);
859 if (!stat->hash || !ftrace_profile_enabled)
862 rec = ftrace_find_profiled_func(stat, ip);
864 rec = ftrace_profile_alloc(stat, ip);
871 local_irq_restore(flags);
874 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
875 static int profile_graph_entry(struct ftrace_graph_ent *trace)
877 int index = trace->depth;
879 function_profile_call(trace->func, 0, NULL, NULL);
881 /* If function graph is shutting down, ret_stack can be NULL */
882 if (!current->ret_stack)
885 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
886 current->ret_stack[index].subtime = 0;
891 static void profile_graph_return(struct ftrace_graph_ret *trace)
893 struct ftrace_profile_stat *stat;
894 unsigned long long calltime;
895 struct ftrace_profile *rec;
898 local_irq_save(flags);
899 stat = this_cpu_ptr(&ftrace_profile_stats);
900 if (!stat->hash || !ftrace_profile_enabled)
903 /* If the calltime was zero'd ignore it */
904 if (!trace->calltime)
907 calltime = trace->rettime - trace->calltime;
909 if (!fgraph_graph_time) {
912 index = trace->depth;
914 /* Append this call time to the parent time to subtract */
916 current->ret_stack[index - 1].subtime += calltime;
918 if (current->ret_stack[index].subtime < calltime)
919 calltime -= current->ret_stack[index].subtime;
924 rec = ftrace_find_profiled_func(stat, trace->func);
926 rec->time += calltime;
927 rec->time_squared += calltime * calltime;
931 local_irq_restore(flags);
934 static int register_ftrace_profiler(void)
936 return register_ftrace_graph(&profile_graph_return,
937 &profile_graph_entry);
940 static void unregister_ftrace_profiler(void)
942 unregister_ftrace_graph();
945 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
946 .func = function_profile_call,
947 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
948 INIT_OPS_HASH(ftrace_profile_ops)
951 static int register_ftrace_profiler(void)
953 return register_ftrace_function(&ftrace_profile_ops);
956 static void unregister_ftrace_profiler(void)
958 unregister_ftrace_function(&ftrace_profile_ops);
960 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
963 ftrace_profile_write(struct file *filp, const char __user *ubuf,
964 size_t cnt, loff_t *ppos)
969 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
975 mutex_lock(&ftrace_profile_lock);
976 if (ftrace_profile_enabled ^ val) {
978 ret = ftrace_profile_init();
984 ret = register_ftrace_profiler();
989 ftrace_profile_enabled = 1;
991 ftrace_profile_enabled = 0;
993 * unregister_ftrace_profiler calls stop_machine
994 * so this acts like an synchronize_sched.
996 unregister_ftrace_profiler();
1000 mutex_unlock(&ftrace_profile_lock);
1008 ftrace_profile_read(struct file *filp, char __user *ubuf,
1009 size_t cnt, loff_t *ppos)
1011 char buf[64]; /* big enough to hold a number */
1014 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1015 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1018 static const struct file_operations ftrace_profile_fops = {
1019 .open = tracing_open_generic,
1020 .read = ftrace_profile_read,
1021 .write = ftrace_profile_write,
1022 .llseek = default_llseek,
1025 /* used to initialize the real stat files */
1026 static struct tracer_stat function_stats __initdata = {
1027 .name = "functions",
1028 .stat_start = function_stat_start,
1029 .stat_next = function_stat_next,
1030 .stat_cmp = function_stat_cmp,
1031 .stat_headers = function_stat_headers,
1032 .stat_show = function_stat_show
1035 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1037 struct ftrace_profile_stat *stat;
1038 struct dentry *entry;
1043 for_each_possible_cpu(cpu) {
1044 stat = &per_cpu(ftrace_profile_stats, cpu);
1046 name = kasprintf(GFP_KERNEL, "function%d", cpu);
1049 * The files created are permanent, if something happens
1050 * we still do not free memory.
1053 "Could not allocate stat file for cpu %d\n",
1057 stat->stat = function_stats;
1058 stat->stat.name = name;
1059 ret = register_stat_tracer(&stat->stat);
1062 "Could not register function stat for cpu %d\n",
1069 entry = tracefs_create_file("function_profile_enabled", 0644,
1070 d_tracer, NULL, &ftrace_profile_fops);
1072 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1075 #else /* CONFIG_FUNCTION_PROFILER */
1076 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1079 #endif /* CONFIG_FUNCTION_PROFILER */
1081 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1083 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1084 static int ftrace_graph_active;
1086 # define ftrace_graph_active 0
1089 #ifdef CONFIG_DYNAMIC_FTRACE
1091 static struct ftrace_ops *removed_ops;
1094 * Set when doing a global update, like enabling all recs or disabling them.
1095 * It is not set when just updating a single ftrace_ops.
1097 static bool update_all_ops;
1099 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1100 # error Dynamic ftrace depends on MCOUNT_RECORD
1103 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1105 struct ftrace_func_probe {
1106 struct hlist_node node;
1107 struct ftrace_probe_ops *ops;
1108 unsigned long flags;
1111 struct list_head free_list;
1114 struct ftrace_func_entry {
1115 struct hlist_node hlist;
1119 struct ftrace_hash {
1120 unsigned long size_bits;
1121 struct hlist_head *buckets;
1122 unsigned long count;
1123 struct rcu_head rcu;
1127 * We make these constant because no one should touch them,
1128 * but they are used as the default "empty hash", to avoid allocating
1129 * it all the time. These are in a read only section such that if
1130 * anyone does try to modify it, it will cause an exception.
1132 static const struct hlist_head empty_buckets[1];
1133 static const struct ftrace_hash empty_hash = {
1134 .buckets = (struct hlist_head *)empty_buckets,
1136 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1138 static struct ftrace_ops global_ops = {
1139 .func = ftrace_stub,
1140 .local_hash.notrace_hash = EMPTY_HASH,
1141 .local_hash.filter_hash = EMPTY_HASH,
1142 INIT_OPS_HASH(global_ops)
1143 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1144 FTRACE_OPS_FL_INITIALIZED |
1149 * This is used by __kernel_text_address() to return true if the
1150 * address is on a dynamically allocated trampoline that would
1151 * not return true for either core_kernel_text() or
1152 * is_module_text_address().
1154 bool is_ftrace_trampoline(unsigned long addr)
1156 struct ftrace_ops *op;
1160 * Some of the ops may be dynamically allocated,
1161 * they are freed after a synchronize_sched().
1163 preempt_disable_notrace();
1165 do_for_each_ftrace_op(op, ftrace_ops_list) {
1167 * This is to check for dynamically allocated trampolines.
1168 * Trampolines that are in kernel text will have
1169 * core_kernel_text() return true.
1171 if (op->trampoline && op->trampoline_size)
1172 if (addr >= op->trampoline &&
1173 addr < op->trampoline + op->trampoline_size) {
1177 } while_for_each_ftrace_op(op);
1180 preempt_enable_notrace();
1185 struct ftrace_page {
1186 struct ftrace_page *next;
1187 struct dyn_ftrace *records;
1192 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1193 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1195 /* estimate from running different kernels */
1196 #define NR_TO_INIT 10000
1198 static struct ftrace_page *ftrace_pages_start;
1199 static struct ftrace_page *ftrace_pages;
1201 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1203 return !hash || !hash->count;
1206 static struct ftrace_func_entry *
1207 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1210 struct ftrace_func_entry *entry;
1211 struct hlist_head *hhd;
1213 if (ftrace_hash_empty(hash))
1216 if (hash->size_bits > 0)
1217 key = hash_long(ip, hash->size_bits);
1221 hhd = &hash->buckets[key];
1223 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1224 if (entry->ip == ip)
1230 static void __add_hash_entry(struct ftrace_hash *hash,
1231 struct ftrace_func_entry *entry)
1233 struct hlist_head *hhd;
1236 if (hash->size_bits)
1237 key = hash_long(entry->ip, hash->size_bits);
1241 hhd = &hash->buckets[key];
1242 hlist_add_head(&entry->hlist, hhd);
1246 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1248 struct ftrace_func_entry *entry;
1250 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1255 __add_hash_entry(hash, entry);
1261 free_hash_entry(struct ftrace_hash *hash,
1262 struct ftrace_func_entry *entry)
1264 hlist_del(&entry->hlist);
1270 remove_hash_entry(struct ftrace_hash *hash,
1271 struct ftrace_func_entry *entry)
1273 hlist_del(&entry->hlist);
1277 static void ftrace_hash_clear(struct ftrace_hash *hash)
1279 struct hlist_head *hhd;
1280 struct hlist_node *tn;
1281 struct ftrace_func_entry *entry;
1282 int size = 1 << hash->size_bits;
1288 for (i = 0; i < size; i++) {
1289 hhd = &hash->buckets[i];
1290 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1291 free_hash_entry(hash, entry);
1293 FTRACE_WARN_ON(hash->count);
1296 static void free_ftrace_hash(struct ftrace_hash *hash)
1298 if (!hash || hash == EMPTY_HASH)
1300 ftrace_hash_clear(hash);
1301 kfree(hash->buckets);
1305 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1307 struct ftrace_hash *hash;
1309 hash = container_of(rcu, struct ftrace_hash, rcu);
1310 free_ftrace_hash(hash);
1313 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1315 if (!hash || hash == EMPTY_HASH)
1317 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1320 void ftrace_free_filter(struct ftrace_ops *ops)
1322 ftrace_ops_init(ops);
1323 free_ftrace_hash(ops->func_hash->filter_hash);
1324 free_ftrace_hash(ops->func_hash->notrace_hash);
1327 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1329 struct ftrace_hash *hash;
1332 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1336 size = 1 << size_bits;
1337 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1339 if (!hash->buckets) {
1344 hash->size_bits = size_bits;
1349 static struct ftrace_hash *
1350 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1352 struct ftrace_func_entry *entry;
1353 struct ftrace_hash *new_hash;
1358 new_hash = alloc_ftrace_hash(size_bits);
1363 if (ftrace_hash_empty(hash))
1366 size = 1 << hash->size_bits;
1367 for (i = 0; i < size; i++) {
1368 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1369 ret = add_hash_entry(new_hash, entry->ip);
1375 FTRACE_WARN_ON(new_hash->count != hash->count);
1380 free_ftrace_hash(new_hash);
1385 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1387 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1389 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1390 struct ftrace_hash *new_hash);
1393 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1394 struct ftrace_hash **dst, struct ftrace_hash *src)
1396 struct ftrace_func_entry *entry;
1397 struct hlist_node *tn;
1398 struct hlist_head *hhd;
1399 struct ftrace_hash *new_hash;
1400 int size = src->count;
1405 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1406 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1410 * If the new source is empty, just free dst and assign it
1414 new_hash = EMPTY_HASH;
1419 * Make the hash size about 1/2 the # found
1421 for (size /= 2; size; size >>= 1)
1424 /* Don't allocate too much */
1425 if (bits > FTRACE_HASH_MAX_BITS)
1426 bits = FTRACE_HASH_MAX_BITS;
1428 new_hash = alloc_ftrace_hash(bits);
1432 size = 1 << src->size_bits;
1433 for (i = 0; i < size; i++) {
1434 hhd = &src->buckets[i];
1435 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1436 remove_hash_entry(src, entry);
1437 __add_hash_entry(new_hash, entry);
1442 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1444 /* IPMODIFY should be updated only when filter_hash updating */
1445 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1447 free_ftrace_hash(new_hash);
1453 * Remove the current set, update the hash and add
1456 ftrace_hash_rec_disable_modify(ops, enable);
1458 rcu_assign_pointer(*dst, new_hash);
1460 ftrace_hash_rec_enable_modify(ops, enable);
1465 static bool hash_contains_ip(unsigned long ip,
1466 struct ftrace_ops_hash *hash)
1469 * The function record is a match if it exists in the filter
1470 * hash and not in the notrace hash. Note, an emty hash is
1471 * considered a match for the filter hash, but an empty
1472 * notrace hash is considered not in the notrace hash.
1474 return (ftrace_hash_empty(hash->filter_hash) ||
1475 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1476 (ftrace_hash_empty(hash->notrace_hash) ||
1477 !ftrace_lookup_ip(hash->notrace_hash, ip));
1481 * Test the hashes for this ops to see if we want to call
1482 * the ops->func or not.
1484 * It's a match if the ip is in the ops->filter_hash or
1485 * the filter_hash does not exist or is empty,
1487 * the ip is not in the ops->notrace_hash.
1489 * This needs to be called with preemption disabled as
1490 * the hashes are freed with call_rcu_sched().
1493 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1495 struct ftrace_ops_hash hash;
1498 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1500 * There's a small race when adding ops that the ftrace handler
1501 * that wants regs, may be called without them. We can not
1502 * allow that handler to be called if regs is NULL.
1504 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1508 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1509 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
1511 if (hash_contains_ip(ip, &hash))
1520 * This is a double for. Do not use 'break' to break out of the loop,
1521 * you must use a goto.
1523 #define do_for_each_ftrace_rec(pg, rec) \
1524 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1526 for (_____i = 0; _____i < pg->index; _____i++) { \
1527 rec = &pg->records[_____i];
1529 #define while_for_each_ftrace_rec() \
1534 static int ftrace_cmp_recs(const void *a, const void *b)
1536 const struct dyn_ftrace *key = a;
1537 const struct dyn_ftrace *rec = b;
1539 if (key->flags < rec->ip)
1541 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1547 * ftrace_location_range - return the first address of a traced location
1548 * if it touches the given ip range
1549 * @start: start of range to search.
1550 * @end: end of range to search (inclusive). @end points to the last byte
1553 * Returns rec->ip if the related ftrace location is a least partly within
1554 * the given address range. That is, the first address of the instruction
1555 * that is either a NOP or call to the function tracer. It checks the ftrace
1556 * internal tables to determine if the address belongs or not.
1558 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1560 struct ftrace_page *pg;
1561 struct dyn_ftrace *rec;
1562 struct dyn_ftrace key;
1565 key.flags = end; /* overload flags, as it is unsigned long */
1567 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1568 if (end < pg->records[0].ip ||
1569 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1571 rec = bsearch(&key, pg->records, pg->index,
1572 sizeof(struct dyn_ftrace),
1582 * ftrace_location - return true if the ip giving is a traced location
1583 * @ip: the instruction pointer to check
1585 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1586 * That is, the instruction that is either a NOP or call to
1587 * the function tracer. It checks the ftrace internal tables to
1588 * determine if the address belongs or not.
1590 unsigned long ftrace_location(unsigned long ip)
1592 return ftrace_location_range(ip, ip);
1596 * ftrace_text_reserved - return true if range contains an ftrace location
1597 * @start: start of range to search
1598 * @end: end of range to search (inclusive). @end points to the last byte to check.
1600 * Returns 1 if @start and @end contains a ftrace location.
1601 * That is, the instruction that is either a NOP or call to
1602 * the function tracer. It checks the ftrace internal tables to
1603 * determine if the address belongs or not.
1605 int ftrace_text_reserved(const void *start, const void *end)
1609 ret = ftrace_location_range((unsigned long)start,
1610 (unsigned long)end);
1615 /* Test if ops registered to this rec needs regs */
1616 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1618 struct ftrace_ops *ops;
1619 bool keep_regs = false;
1621 for (ops = ftrace_ops_list;
1622 ops != &ftrace_list_end; ops = ops->next) {
1623 /* pass rec in as regs to have non-NULL val */
1624 if (ftrace_ops_test(ops, rec->ip, rec)) {
1625 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1635 static struct ftrace_ops *
1636 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1637 static struct ftrace_ops *
1638 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1640 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1644 struct ftrace_hash *hash;
1645 struct ftrace_hash *other_hash;
1646 struct ftrace_page *pg;
1647 struct dyn_ftrace *rec;
1648 bool update = false;
1652 /* Only update if the ops has been registered */
1653 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1657 * In the filter_hash case:
1658 * If the count is zero, we update all records.
1659 * Otherwise we just update the items in the hash.
1661 * In the notrace_hash case:
1662 * We enable the update in the hash.
1663 * As disabling notrace means enabling the tracing,
1664 * and enabling notrace means disabling, the inc variable
1668 hash = ops->func_hash->filter_hash;
1669 other_hash = ops->func_hash->notrace_hash;
1670 if (ftrace_hash_empty(hash))
1674 hash = ops->func_hash->notrace_hash;
1675 other_hash = ops->func_hash->filter_hash;
1677 * If the notrace hash has no items,
1678 * then there's nothing to do.
1680 if (ftrace_hash_empty(hash))
1684 do_for_each_ftrace_rec(pg, rec) {
1685 int in_other_hash = 0;
1689 if (rec->flags & FTRACE_FL_DISABLED)
1694 * Only the filter_hash affects all records.
1695 * Update if the record is not in the notrace hash.
1697 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1700 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1701 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1704 * If filter_hash is set, we want to match all functions
1705 * that are in the hash but not in the other hash.
1707 * If filter_hash is not set, then we are decrementing.
1708 * That means we match anything that is in the hash
1709 * and also in the other_hash. That is, we need to turn
1710 * off functions in the other hash because they are disabled
1713 if (filter_hash && in_hash && !in_other_hash)
1715 else if (!filter_hash && in_hash &&
1716 (in_other_hash || ftrace_hash_empty(other_hash)))
1724 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1728 * If there's only a single callback registered to a
1729 * function, and the ops has a trampoline registered
1730 * for it, then we can call it directly.
1732 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1733 rec->flags |= FTRACE_FL_TRAMP;
1736 * If we are adding another function callback
1737 * to this function, and the previous had a
1738 * custom trampoline in use, then we need to go
1739 * back to the default trampoline.
1741 rec->flags &= ~FTRACE_FL_TRAMP;
1744 * If any ops wants regs saved for this function
1745 * then all ops will get saved regs.
1747 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1748 rec->flags |= FTRACE_FL_REGS;
1750 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1755 * If the rec had REGS enabled and the ops that is
1756 * being removed had REGS set, then see if there is
1757 * still any ops for this record that wants regs.
1758 * If not, we can stop recording them.
1760 if (ftrace_rec_count(rec) > 0 &&
1761 rec->flags & FTRACE_FL_REGS &&
1762 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1763 if (!test_rec_ops_needs_regs(rec))
1764 rec->flags &= ~FTRACE_FL_REGS;
1768 * The TRAMP needs to be set only if rec count
1769 * is decremented to one, and the ops that is
1770 * left has a trampoline. As TRAMP can only be
1771 * enabled if there is only a single ops attached
1774 if (ftrace_rec_count(rec) == 1 &&
1775 ftrace_find_tramp_ops_any(rec))
1776 rec->flags |= FTRACE_FL_TRAMP;
1778 rec->flags &= ~FTRACE_FL_TRAMP;
1781 * flags will be cleared in ftrace_check_record()
1782 * if rec count is zero.
1787 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1788 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1790 /* Shortcut, if we handled all records, we are done. */
1791 if (!all && count == hash->count)
1793 } while_for_each_ftrace_rec();
1798 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1801 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1804 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1807 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1810 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1811 int filter_hash, int inc)
1813 struct ftrace_ops *op;
1815 __ftrace_hash_rec_update(ops, filter_hash, inc);
1817 if (ops->func_hash != &global_ops.local_hash)
1821 * If the ops shares the global_ops hash, then we need to update
1822 * all ops that are enabled and use this hash.
1824 do_for_each_ftrace_op(op, ftrace_ops_list) {
1828 if (op->func_hash == &global_ops.local_hash)
1829 __ftrace_hash_rec_update(op, filter_hash, inc);
1830 } while_for_each_ftrace_op(op);
1833 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1836 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1839 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1842 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1846 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1847 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1848 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1849 * Note that old_hash and new_hash has below meanings
1850 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1851 * - If the hash is EMPTY_HASH, it hits nothing
1852 * - Anything else hits the recs which match the hash entries.
1854 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1855 struct ftrace_hash *old_hash,
1856 struct ftrace_hash *new_hash)
1858 struct ftrace_page *pg;
1859 struct dyn_ftrace *rec, *end = NULL;
1862 /* Only update if the ops has been registered */
1863 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1866 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1870 * Since the IPMODIFY is a very address sensitive action, we do not
1871 * allow ftrace_ops to set all functions to new hash.
1873 if (!new_hash || !old_hash)
1876 /* Update rec->flags */
1877 do_for_each_ftrace_rec(pg, rec) {
1879 if (rec->flags & FTRACE_FL_DISABLED)
1882 /* We need to update only differences of filter_hash */
1883 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1884 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1885 if (in_old == in_new)
1889 /* New entries must ensure no others are using it */
1890 if (rec->flags & FTRACE_FL_IPMODIFY)
1892 rec->flags |= FTRACE_FL_IPMODIFY;
1893 } else /* Removed entry */
1894 rec->flags &= ~FTRACE_FL_IPMODIFY;
1895 } while_for_each_ftrace_rec();
1902 /* Roll back what we did above */
1903 do_for_each_ftrace_rec(pg, rec) {
1905 if (rec->flags & FTRACE_FL_DISABLED)
1911 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1912 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1913 if (in_old == in_new)
1917 rec->flags &= ~FTRACE_FL_IPMODIFY;
1919 rec->flags |= FTRACE_FL_IPMODIFY;
1920 } while_for_each_ftrace_rec();
1926 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1928 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1930 if (ftrace_hash_empty(hash))
1933 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1936 /* Disabling always succeeds */
1937 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1939 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1941 if (ftrace_hash_empty(hash))
1944 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1947 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1948 struct ftrace_hash *new_hash)
1950 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1952 if (ftrace_hash_empty(old_hash))
1955 if (ftrace_hash_empty(new_hash))
1958 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1961 static void print_ip_ins(const char *fmt, const unsigned char *p)
1965 printk(KERN_CONT "%s", fmt);
1967 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1968 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1971 enum ftrace_bug_type ftrace_bug_type;
1972 const void *ftrace_expected;
1974 static void print_bug_type(void)
1976 switch (ftrace_bug_type) {
1977 case FTRACE_BUG_UNKNOWN:
1979 case FTRACE_BUG_INIT:
1980 pr_info("Initializing ftrace call sites\n");
1982 case FTRACE_BUG_NOP:
1983 pr_info("Setting ftrace call site to NOP\n");
1985 case FTRACE_BUG_CALL:
1986 pr_info("Setting ftrace call site to call ftrace function\n");
1988 case FTRACE_BUG_UPDATE:
1989 pr_info("Updating ftrace call site to call a different ftrace function\n");
1995 * ftrace_bug - report and shutdown function tracer
1996 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1997 * @rec: The record that failed
1999 * The arch code that enables or disables the function tracing
2000 * can call ftrace_bug() when it has detected a problem in
2001 * modifying the code. @failed should be one of either:
2002 * EFAULT - if the problem happens on reading the @ip address
2003 * EINVAL - if what is read at @ip is not what was expected
2004 * EPERM - if the problem happens on writting to the @ip address
2006 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2008 unsigned long ip = rec ? rec->ip : 0;
2012 FTRACE_WARN_ON_ONCE(1);
2013 pr_info("ftrace faulted on modifying ");
2017 FTRACE_WARN_ON_ONCE(1);
2018 pr_info("ftrace failed to modify ");
2020 print_ip_ins(" actual: ", (unsigned char *)ip);
2022 if (ftrace_expected) {
2023 print_ip_ins(" expected: ", ftrace_expected);
2028 FTRACE_WARN_ON_ONCE(1);
2029 pr_info("ftrace faulted on writing ");
2033 FTRACE_WARN_ON_ONCE(1);
2034 pr_info("ftrace faulted on unknown error ");
2039 struct ftrace_ops *ops = NULL;
2041 pr_info("ftrace record flags: %lx\n", rec->flags);
2042 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2043 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2044 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2045 ops = ftrace_find_tramp_ops_any(rec);
2048 pr_cont("\ttramp: %pS (%pS)",
2049 (void *)ops->trampoline,
2051 ops = ftrace_find_tramp_ops_next(rec, ops);
2054 pr_cont("\ttramp: ERROR!");
2057 ip = ftrace_get_addr_curr(rec);
2058 pr_cont("\n expected tramp: %lx\n", ip);
2062 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2064 unsigned long flag = 0UL;
2066 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2068 if (rec->flags & FTRACE_FL_DISABLED)
2069 return FTRACE_UPDATE_IGNORE;
2072 * If we are updating calls:
2074 * If the record has a ref count, then we need to enable it
2075 * because someone is using it.
2077 * Otherwise we make sure its disabled.
2079 * If we are disabling calls, then disable all records that
2082 if (enable && ftrace_rec_count(rec))
2083 flag = FTRACE_FL_ENABLED;
2086 * If enabling and the REGS flag does not match the REGS_EN, or
2087 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2088 * this record. Set flags to fail the compare against ENABLED.
2091 if (!(rec->flags & FTRACE_FL_REGS) !=
2092 !(rec->flags & FTRACE_FL_REGS_EN))
2093 flag |= FTRACE_FL_REGS;
2095 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2096 !(rec->flags & FTRACE_FL_TRAMP_EN))
2097 flag |= FTRACE_FL_TRAMP;
2100 /* If the state of this record hasn't changed, then do nothing */
2101 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2102 return FTRACE_UPDATE_IGNORE;
2105 /* Save off if rec is being enabled (for return value) */
2106 flag ^= rec->flags & FTRACE_FL_ENABLED;
2109 rec->flags |= FTRACE_FL_ENABLED;
2110 if (flag & FTRACE_FL_REGS) {
2111 if (rec->flags & FTRACE_FL_REGS)
2112 rec->flags |= FTRACE_FL_REGS_EN;
2114 rec->flags &= ~FTRACE_FL_REGS_EN;
2116 if (flag & FTRACE_FL_TRAMP) {
2117 if (rec->flags & FTRACE_FL_TRAMP)
2118 rec->flags |= FTRACE_FL_TRAMP_EN;
2120 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2125 * If this record is being updated from a nop, then
2126 * return UPDATE_MAKE_CALL.
2128 * return UPDATE_MODIFY_CALL to tell the caller to convert
2129 * from the save regs, to a non-save regs function or
2130 * vice versa, or from a trampoline call.
2132 if (flag & FTRACE_FL_ENABLED) {
2133 ftrace_bug_type = FTRACE_BUG_CALL;
2134 return FTRACE_UPDATE_MAKE_CALL;
2137 ftrace_bug_type = FTRACE_BUG_UPDATE;
2138 return FTRACE_UPDATE_MODIFY_CALL;
2142 /* If there's no more users, clear all flags */
2143 if (!ftrace_rec_count(rec))
2147 * Just disable the record, but keep the ops TRAMP
2148 * and REGS states. The _EN flags must be disabled though.
2150 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2154 ftrace_bug_type = FTRACE_BUG_NOP;
2155 return FTRACE_UPDATE_MAKE_NOP;
2159 * ftrace_update_record, set a record that now is tracing or not
2160 * @rec: the record to update
2161 * @enable: set to 1 if the record is tracing, zero to force disable
2163 * The records that represent all functions that can be traced need
2164 * to be updated when tracing has been enabled.
2166 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2168 return ftrace_check_record(rec, enable, 1);
2172 * ftrace_test_record, check if the record has been enabled or not
2173 * @rec: the record to test
2174 * @enable: set to 1 to check if enabled, 0 if it is disabled
2176 * The arch code may need to test if a record is already set to
2177 * tracing to determine how to modify the function code that it
2180 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2182 return ftrace_check_record(rec, enable, 0);
2185 static struct ftrace_ops *
2186 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2188 struct ftrace_ops *op;
2189 unsigned long ip = rec->ip;
2191 do_for_each_ftrace_op(op, ftrace_ops_list) {
2193 if (!op->trampoline)
2196 if (hash_contains_ip(ip, op->func_hash))
2198 } while_for_each_ftrace_op(op);
2203 static struct ftrace_ops *
2204 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2205 struct ftrace_ops *op)
2207 unsigned long ip = rec->ip;
2209 while_for_each_ftrace_op(op) {
2211 if (!op->trampoline)
2214 if (hash_contains_ip(ip, op->func_hash))
2221 static struct ftrace_ops *
2222 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2224 struct ftrace_ops *op;
2225 unsigned long ip = rec->ip;
2228 * Need to check removed ops first.
2229 * If they are being removed, and this rec has a tramp,
2230 * and this rec is in the ops list, then it would be the
2231 * one with the tramp.
2234 if (hash_contains_ip(ip, &removed_ops->old_hash))
2239 * Need to find the current trampoline for a rec.
2240 * Now, a trampoline is only attached to a rec if there
2241 * was a single 'ops' attached to it. But this can be called
2242 * when we are adding another op to the rec or removing the
2243 * current one. Thus, if the op is being added, we can
2244 * ignore it because it hasn't attached itself to the rec
2247 * If an ops is being modified (hooking to different functions)
2248 * then we don't care about the new functions that are being
2249 * added, just the old ones (that are probably being removed).
2251 * If we are adding an ops to a function that already is using
2252 * a trampoline, it needs to be removed (trampolines are only
2253 * for single ops connected), then an ops that is not being
2254 * modified also needs to be checked.
2256 do_for_each_ftrace_op(op, ftrace_ops_list) {
2258 if (!op->trampoline)
2262 * If the ops is being added, it hasn't gotten to
2263 * the point to be removed from this tree yet.
2265 if (op->flags & FTRACE_OPS_FL_ADDING)
2270 * If the ops is being modified and is in the old
2271 * hash, then it is probably being removed from this
2274 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2275 hash_contains_ip(ip, &op->old_hash))
2278 * If the ops is not being added or modified, and it's
2279 * in its normal filter hash, then this must be the one
2282 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2283 hash_contains_ip(ip, op->func_hash))
2286 } while_for_each_ftrace_op(op);
2291 static struct ftrace_ops *
2292 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2294 struct ftrace_ops *op;
2295 unsigned long ip = rec->ip;
2297 do_for_each_ftrace_op(op, ftrace_ops_list) {
2298 /* pass rec in as regs to have non-NULL val */
2299 if (hash_contains_ip(ip, op->func_hash))
2301 } while_for_each_ftrace_op(op);
2307 * ftrace_get_addr_new - Get the call address to set to
2308 * @rec: The ftrace record descriptor
2310 * If the record has the FTRACE_FL_REGS set, that means that it
2311 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2312 * is not not set, then it wants to convert to the normal callback.
2314 * Returns the address of the trampoline to set to
2316 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2318 struct ftrace_ops *ops;
2320 /* Trampolines take precedence over regs */
2321 if (rec->flags & FTRACE_FL_TRAMP) {
2322 ops = ftrace_find_tramp_ops_new(rec);
2323 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2324 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2325 (void *)rec->ip, (void *)rec->ip, rec->flags);
2326 /* Ftrace is shutting down, return anything */
2327 return (unsigned long)FTRACE_ADDR;
2329 return ops->trampoline;
2332 if (rec->flags & FTRACE_FL_REGS)
2333 return (unsigned long)FTRACE_REGS_ADDR;
2335 return (unsigned long)FTRACE_ADDR;
2339 * ftrace_get_addr_curr - Get the call address that is already there
2340 * @rec: The ftrace record descriptor
2342 * The FTRACE_FL_REGS_EN is set when the record already points to
2343 * a function that saves all the regs. Basically the '_EN' version
2344 * represents the current state of the function.
2346 * Returns the address of the trampoline that is currently being called
2348 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2350 struct ftrace_ops *ops;
2352 /* Trampolines take precedence over regs */
2353 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2354 ops = ftrace_find_tramp_ops_curr(rec);
2355 if (FTRACE_WARN_ON(!ops)) {
2356 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2357 (void *)rec->ip, (void *)rec->ip);
2358 /* Ftrace is shutting down, return anything */
2359 return (unsigned long)FTRACE_ADDR;
2361 return ops->trampoline;
2364 if (rec->flags & FTRACE_FL_REGS_EN)
2365 return (unsigned long)FTRACE_REGS_ADDR;
2367 return (unsigned long)FTRACE_ADDR;
2371 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2373 unsigned long ftrace_old_addr;
2374 unsigned long ftrace_addr;
2377 ftrace_addr = ftrace_get_addr_new(rec);
2379 /* This needs to be done before we call ftrace_update_record */
2380 ftrace_old_addr = ftrace_get_addr_curr(rec);
2382 ret = ftrace_update_record(rec, enable);
2384 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2387 case FTRACE_UPDATE_IGNORE:
2390 case FTRACE_UPDATE_MAKE_CALL:
2391 ftrace_bug_type = FTRACE_BUG_CALL;
2392 return ftrace_make_call(rec, ftrace_addr);
2394 case FTRACE_UPDATE_MAKE_NOP:
2395 ftrace_bug_type = FTRACE_BUG_NOP;
2396 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2398 case FTRACE_UPDATE_MODIFY_CALL:
2399 ftrace_bug_type = FTRACE_BUG_UPDATE;
2400 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2403 return -1; /* unknow ftrace bug */
2406 void __weak ftrace_replace_code(int enable)
2408 struct dyn_ftrace *rec;
2409 struct ftrace_page *pg;
2412 if (unlikely(ftrace_disabled))
2415 do_for_each_ftrace_rec(pg, rec) {
2417 if (rec->flags & FTRACE_FL_DISABLED)
2420 failed = __ftrace_replace_code(rec, enable);
2422 ftrace_bug(failed, rec);
2423 /* Stop processing */
2426 } while_for_each_ftrace_rec();
2429 struct ftrace_rec_iter {
2430 struct ftrace_page *pg;
2435 * ftrace_rec_iter_start, start up iterating over traced functions
2437 * Returns an iterator handle that is used to iterate over all
2438 * the records that represent address locations where functions
2441 * May return NULL if no records are available.
2443 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2446 * We only use a single iterator.
2447 * Protected by the ftrace_lock mutex.
2449 static struct ftrace_rec_iter ftrace_rec_iter;
2450 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2452 iter->pg = ftrace_pages_start;
2455 /* Could have empty pages */
2456 while (iter->pg && !iter->pg->index)
2457 iter->pg = iter->pg->next;
2466 * ftrace_rec_iter_next, get the next record to process.
2467 * @iter: The handle to the iterator.
2469 * Returns the next iterator after the given iterator @iter.
2471 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2475 if (iter->index >= iter->pg->index) {
2476 iter->pg = iter->pg->next;
2479 /* Could have empty pages */
2480 while (iter->pg && !iter->pg->index)
2481 iter->pg = iter->pg->next;
2491 * ftrace_rec_iter_record, get the record at the iterator location
2492 * @iter: The current iterator location
2494 * Returns the record that the current @iter is at.
2496 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2498 return &iter->pg->records[iter->index];
2502 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2506 if (unlikely(ftrace_disabled))
2509 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2511 ftrace_bug_type = FTRACE_BUG_INIT;
2512 ftrace_bug(ret, rec);
2519 * archs can override this function if they must do something
2520 * before the modifying code is performed.
2522 int __weak ftrace_arch_code_modify_prepare(void)
2528 * archs can override this function if they must do something
2529 * after the modifying code is performed.
2531 int __weak ftrace_arch_code_modify_post_process(void)
2536 void ftrace_modify_all_code(int command)
2538 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2542 * If the ftrace_caller calls a ftrace_ops func directly,
2543 * we need to make sure that it only traces functions it
2544 * expects to trace. When doing the switch of functions,
2545 * we need to update to the ftrace_ops_list_func first
2546 * before the transition between old and new calls are set,
2547 * as the ftrace_ops_list_func will check the ops hashes
2548 * to make sure the ops are having the right functions
2552 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2553 if (FTRACE_WARN_ON(err))
2557 if (command & FTRACE_UPDATE_CALLS)
2558 ftrace_replace_code(1);
2559 else if (command & FTRACE_DISABLE_CALLS)
2560 ftrace_replace_code(0);
2562 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2563 function_trace_op = set_function_trace_op;
2565 /* If irqs are disabled, we are in stop machine */
2566 if (!irqs_disabled())
2567 smp_call_function(ftrace_sync_ipi, NULL, 1);
2568 err = ftrace_update_ftrace_func(ftrace_trace_function);
2569 if (FTRACE_WARN_ON(err))
2573 if (command & FTRACE_START_FUNC_RET)
2574 err = ftrace_enable_ftrace_graph_caller();
2575 else if (command & FTRACE_STOP_FUNC_RET)
2576 err = ftrace_disable_ftrace_graph_caller();
2577 FTRACE_WARN_ON(err);
2580 static int __ftrace_modify_code(void *data)
2582 int *command = data;
2584 ftrace_modify_all_code(*command);
2590 * ftrace_run_stop_machine, go back to the stop machine method
2591 * @command: The command to tell ftrace what to do
2593 * If an arch needs to fall back to the stop machine method, the
2594 * it can call this function.
2596 void ftrace_run_stop_machine(int command)
2598 stop_machine(__ftrace_modify_code, &command, NULL);
2602 * arch_ftrace_update_code, modify the code to trace or not trace
2603 * @command: The command that needs to be done
2605 * Archs can override this function if it does not need to
2606 * run stop_machine() to modify code.
2608 void __weak arch_ftrace_update_code(int command)
2610 ftrace_run_stop_machine(command);
2613 static void ftrace_run_update_code(int command)
2617 ret = ftrace_arch_code_modify_prepare();
2618 FTRACE_WARN_ON(ret);
2623 * By default we use stop_machine() to modify the code.
2624 * But archs can do what ever they want as long as it
2625 * is safe. The stop_machine() is the safest, but also
2626 * produces the most overhead.
2628 arch_ftrace_update_code(command);
2630 ret = ftrace_arch_code_modify_post_process();
2631 FTRACE_WARN_ON(ret);
2634 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2635 struct ftrace_ops_hash *old_hash)
2637 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2638 ops->old_hash.filter_hash = old_hash->filter_hash;
2639 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2640 ftrace_run_update_code(command);
2641 ops->old_hash.filter_hash = NULL;
2642 ops->old_hash.notrace_hash = NULL;
2643 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2646 static ftrace_func_t saved_ftrace_func;
2647 static int ftrace_start_up;
2649 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2653 static void per_cpu_ops_free(struct ftrace_ops *ops)
2655 free_percpu(ops->disabled);
2658 static void ftrace_startup_enable(int command)
2660 if (saved_ftrace_func != ftrace_trace_function) {
2661 saved_ftrace_func = ftrace_trace_function;
2662 command |= FTRACE_UPDATE_TRACE_FUNC;
2665 if (!command || !ftrace_enabled)
2668 ftrace_run_update_code(command);
2671 static void ftrace_startup_all(int command)
2673 update_all_ops = true;
2674 ftrace_startup_enable(command);
2675 update_all_ops = false;
2678 static int ftrace_startup(struct ftrace_ops *ops, int command)
2682 if (unlikely(ftrace_disabled))
2685 ret = __register_ftrace_function(ops);
2692 * Note that ftrace probes uses this to start up
2693 * and modify functions it will probe. But we still
2694 * set the ADDING flag for modification, as probes
2695 * do not have trampolines. If they add them in the
2696 * future, then the probes will need to distinguish
2697 * between adding and updating probes.
2699 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2701 ret = ftrace_hash_ipmodify_enable(ops);
2703 /* Rollback registration process */
2704 __unregister_ftrace_function(ops);
2706 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2710 if (ftrace_hash_rec_enable(ops, 1))
2711 command |= FTRACE_UPDATE_CALLS;
2713 ftrace_startup_enable(command);
2715 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2720 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2724 if (unlikely(ftrace_disabled))
2727 ret = __unregister_ftrace_function(ops);
2733 * Just warn in case of unbalance, no need to kill ftrace, it's not
2734 * critical but the ftrace_call callers may be never nopped again after
2735 * further ftrace uses.
2737 WARN_ON_ONCE(ftrace_start_up < 0);
2739 /* Disabling ipmodify never fails */
2740 ftrace_hash_ipmodify_disable(ops);
2742 if (ftrace_hash_rec_disable(ops, 1))
2743 command |= FTRACE_UPDATE_CALLS;
2745 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2747 if (saved_ftrace_func != ftrace_trace_function) {
2748 saved_ftrace_func = ftrace_trace_function;
2749 command |= FTRACE_UPDATE_TRACE_FUNC;
2752 if (!command || !ftrace_enabled) {
2754 * If these are dynamic or per_cpu ops, they still
2755 * need their data freed. Since, function tracing is
2756 * not currently active, we can just free them
2757 * without synchronizing all CPUs.
2759 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU))
2766 * If the ops uses a trampoline, then it needs to be
2767 * tested first on update.
2769 ops->flags |= FTRACE_OPS_FL_REMOVING;
2772 /* The trampoline logic checks the old hashes */
2773 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2774 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2776 ftrace_run_update_code(command);
2779 * If there's no more ops registered with ftrace, run a
2780 * sanity check to make sure all rec flags are cleared.
2782 if (ftrace_ops_list == &ftrace_list_end) {
2783 struct ftrace_page *pg;
2784 struct dyn_ftrace *rec;
2786 do_for_each_ftrace_rec(pg, rec) {
2787 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2788 pr_warn(" %pS flags:%lx\n",
2789 (void *)rec->ip, rec->flags);
2790 } while_for_each_ftrace_rec();
2793 ops->old_hash.filter_hash = NULL;
2794 ops->old_hash.notrace_hash = NULL;
2797 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2800 * Dynamic ops may be freed, we must make sure that all
2801 * callers are done before leaving this function.
2802 * The same goes for freeing the per_cpu data of the per_cpu
2805 * Again, normal synchronize_sched() is not good enough.
2806 * We need to do a hard force of sched synchronization.
2807 * This is because we use preempt_disable() to do RCU, but
2808 * the function tracers can be called where RCU is not watching
2809 * (like before user_exit()). We can not rely on the RCU
2810 * infrastructure to do the synchronization, thus we must do it
2813 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
2814 schedule_on_each_cpu(ftrace_sync);
2817 arch_ftrace_trampoline_free(ops);
2819 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2820 per_cpu_ops_free(ops);
2826 static void ftrace_startup_sysctl(void)
2830 if (unlikely(ftrace_disabled))
2833 /* Force update next time */
2834 saved_ftrace_func = NULL;
2835 /* ftrace_start_up is true if we want ftrace running */
2836 if (ftrace_start_up) {
2837 command = FTRACE_UPDATE_CALLS;
2838 if (ftrace_graph_active)
2839 command |= FTRACE_START_FUNC_RET;
2840 ftrace_startup_enable(command);
2844 static void ftrace_shutdown_sysctl(void)
2848 if (unlikely(ftrace_disabled))
2851 /* ftrace_start_up is true if ftrace is running */
2852 if (ftrace_start_up) {
2853 command = FTRACE_DISABLE_CALLS;
2854 if (ftrace_graph_active)
2855 command |= FTRACE_STOP_FUNC_RET;
2856 ftrace_run_update_code(command);
2860 static cycle_t ftrace_update_time;
2861 unsigned long ftrace_update_tot_cnt;
2863 static inline int ops_traces_mod(struct ftrace_ops *ops)
2866 * Filter_hash being empty will default to trace module.
2867 * But notrace hash requires a test of individual module functions.
2869 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2870 ftrace_hash_empty(ops->func_hash->notrace_hash);
2874 * Check if the current ops references the record.
2876 * If the ops traces all functions, then it was already accounted for.
2877 * If the ops does not trace the current record function, skip it.
2878 * If the ops ignores the function via notrace filter, skip it.
2881 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2883 /* If ops isn't enabled, ignore it */
2884 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2887 /* If ops traces all then it includes this function */
2888 if (ops_traces_mod(ops))
2891 /* The function must be in the filter */
2892 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2893 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2896 /* If in notrace hash, we ignore it too */
2897 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2903 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2905 struct ftrace_page *pg;
2906 struct dyn_ftrace *p;
2907 cycle_t start, stop;
2908 unsigned long update_cnt = 0;
2909 unsigned long rec_flags = 0;
2912 start = ftrace_now(raw_smp_processor_id());
2915 * When a module is loaded, this function is called to convert
2916 * the calls to mcount in its text to nops, and also to create
2917 * an entry in the ftrace data. Now, if ftrace is activated
2918 * after this call, but before the module sets its text to
2919 * read-only, the modification of enabling ftrace can fail if
2920 * the read-only is done while ftrace is converting the calls.
2921 * To prevent this, the module's records are set as disabled
2922 * and will be enabled after the call to set the module's text
2926 rec_flags |= FTRACE_FL_DISABLED;
2928 for (pg = new_pgs; pg; pg = pg->next) {
2930 for (i = 0; i < pg->index; i++) {
2932 /* If something went wrong, bail without enabling anything */
2933 if (unlikely(ftrace_disabled))
2936 p = &pg->records[i];
2937 p->flags = rec_flags;
2940 * Do the initial record conversion from mcount jump
2941 * to the NOP instructions.
2943 if (!ftrace_code_disable(mod, p))
2950 stop = ftrace_now(raw_smp_processor_id());
2951 ftrace_update_time = stop - start;
2952 ftrace_update_tot_cnt += update_cnt;
2957 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2962 if (WARN_ON(!count))
2965 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2968 * We want to fill as much as possible. No more than a page
2971 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2975 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2978 /* if we can't allocate this size, try something smaller */
2985 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2994 static struct ftrace_page *
2995 ftrace_allocate_pages(unsigned long num_to_init)
2997 struct ftrace_page *start_pg;
2998 struct ftrace_page *pg;
3005 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3010 * Try to allocate as much as possible in one continues
3011 * location that fills in all of the space. We want to
3012 * waste as little space as possible.
3015 cnt = ftrace_allocate_records(pg, num_to_init);
3023 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3035 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3036 free_pages((unsigned long)pg->records, order);
3037 start_pg = pg->next;
3041 pr_info("ftrace: FAILED to allocate memory for functions\n");
3045 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3047 struct ftrace_iterator {
3050 struct ftrace_page *pg;
3051 struct dyn_ftrace *func;
3052 struct ftrace_func_probe *probe;
3053 struct trace_parser parser;
3054 struct ftrace_hash *hash;
3055 struct ftrace_ops *ops;
3062 t_hash_next(struct seq_file *m, loff_t *pos)
3064 struct ftrace_iterator *iter = m->private;
3065 struct hlist_node *hnd = NULL;
3066 struct hlist_head *hhd;
3072 hnd = &iter->probe->node;
3074 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3077 hhd = &ftrace_func_hash[iter->hidx];
3079 if (hlist_empty(hhd)) {
3095 if (WARN_ON_ONCE(!hnd))
3098 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3103 static void *t_hash_start(struct seq_file *m, loff_t *pos)
3105 struct ftrace_iterator *iter = m->private;
3109 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3112 if (iter->func_pos > *pos)
3116 for (l = 0; l <= (*pos - iter->func_pos); ) {
3117 p = t_hash_next(m, &l);
3124 /* Only set this if we have an item */
3125 iter->flags |= FTRACE_ITER_HASH;
3131 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
3133 struct ftrace_func_probe *rec;
3136 if (WARN_ON_ONCE(!rec))
3139 if (rec->ops->print)
3140 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3142 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
3145 seq_printf(m, ":%p", rec->data);
3152 t_next(struct seq_file *m, void *v, loff_t *pos)
3154 struct ftrace_iterator *iter = m->private;
3155 struct ftrace_ops *ops = iter->ops;
3156 struct dyn_ftrace *rec = NULL;
3158 if (unlikely(ftrace_disabled))
3161 if (iter->flags & FTRACE_ITER_HASH)
3162 return t_hash_next(m, pos);
3165 iter->pos = iter->func_pos = *pos;
3167 if (iter->flags & FTRACE_ITER_PRINTALL)
3168 return t_hash_start(m, pos);
3171 if (iter->idx >= iter->pg->index) {
3172 if (iter->pg->next) {
3173 iter->pg = iter->pg->next;
3178 rec = &iter->pg->records[iter->idx++];
3179 if (((iter->flags & FTRACE_ITER_FILTER) &&
3180 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
3182 ((iter->flags & FTRACE_ITER_NOTRACE) &&
3183 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
3185 ((iter->flags & FTRACE_ITER_ENABLED) &&
3186 !(rec->flags & FTRACE_FL_ENABLED))) {
3194 return t_hash_start(m, pos);
3201 static void reset_iter_read(struct ftrace_iterator *iter)
3205 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
3208 static void *t_start(struct seq_file *m, loff_t *pos)
3210 struct ftrace_iterator *iter = m->private;
3211 struct ftrace_ops *ops = iter->ops;
3215 mutex_lock(&ftrace_lock);
3217 if (unlikely(ftrace_disabled))
3221 * If an lseek was done, then reset and start from beginning.
3223 if (*pos < iter->pos)
3224 reset_iter_read(iter);
3227 * For set_ftrace_filter reading, if we have the filter
3228 * off, we can short cut and just print out that all
3229 * functions are enabled.
3231 if ((iter->flags & FTRACE_ITER_FILTER &&
3232 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
3233 (iter->flags & FTRACE_ITER_NOTRACE &&
3234 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
3236 return t_hash_start(m, pos);
3237 iter->flags |= FTRACE_ITER_PRINTALL;
3238 /* reset in case of seek/pread */
3239 iter->flags &= ~FTRACE_ITER_HASH;
3243 if (iter->flags & FTRACE_ITER_HASH)
3244 return t_hash_start(m, pos);
3247 * Unfortunately, we need to restart at ftrace_pages_start
3248 * every time we let go of the ftrace_mutex. This is because
3249 * those pointers can change without the lock.
3251 iter->pg = ftrace_pages_start;
3253 for (l = 0; l <= *pos; ) {
3254 p = t_next(m, p, &l);
3260 return t_hash_start(m, pos);
3265 static void t_stop(struct seq_file *m, void *p)
3267 mutex_unlock(&ftrace_lock);
3271 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3276 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3277 struct dyn_ftrace *rec)
3281 ptr = arch_ftrace_trampoline_func(ops, rec);
3283 seq_printf(m, " ->%pS", ptr);
3286 static int t_show(struct seq_file *m, void *v)
3288 struct ftrace_iterator *iter = m->private;
3289 struct dyn_ftrace *rec;
3291 if (iter->flags & FTRACE_ITER_HASH)
3292 return t_hash_show(m, iter);
3294 if (iter->flags & FTRACE_ITER_PRINTALL) {
3295 if (iter->flags & FTRACE_ITER_NOTRACE)
3296 seq_puts(m, "#### no functions disabled ####\n");
3298 seq_puts(m, "#### all functions enabled ####\n");
3307 seq_printf(m, "%ps", (void *)rec->ip);
3308 if (iter->flags & FTRACE_ITER_ENABLED) {
3309 struct ftrace_ops *ops;
3311 seq_printf(m, " (%ld)%s%s",
3312 ftrace_rec_count(rec),
3313 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3314 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3315 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3316 ops = ftrace_find_tramp_ops_any(rec);
3319 seq_printf(m, "\ttramp: %pS (%pS)",
3320 (void *)ops->trampoline,
3322 add_trampoline_func(m, ops, rec);
3323 ops = ftrace_find_tramp_ops_next(rec, ops);
3326 seq_puts(m, "\ttramp: ERROR!");
3328 add_trampoline_func(m, NULL, rec);
3337 static const struct seq_operations show_ftrace_seq_ops = {
3345 ftrace_avail_open(struct inode *inode, struct file *file)
3347 struct ftrace_iterator *iter;
3349 if (unlikely(ftrace_disabled))
3352 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3354 iter->pg = ftrace_pages_start;
3355 iter->ops = &global_ops;
3358 return iter ? 0 : -ENOMEM;
3362 ftrace_enabled_open(struct inode *inode, struct file *file)
3364 struct ftrace_iterator *iter;
3366 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3368 iter->pg = ftrace_pages_start;
3369 iter->flags = FTRACE_ITER_ENABLED;
3370 iter->ops = &global_ops;
3373 return iter ? 0 : -ENOMEM;
3377 * ftrace_regex_open - initialize function tracer filter files
3378 * @ops: The ftrace_ops that hold the hash filters
3379 * @flag: The type of filter to process
3380 * @inode: The inode, usually passed in to your open routine
3381 * @file: The file, usually passed in to your open routine
3383 * ftrace_regex_open() initializes the filter files for the
3384 * @ops. Depending on @flag it may process the filter hash or
3385 * the notrace hash of @ops. With this called from the open
3386 * routine, you can use ftrace_filter_write() for the write
3387 * routine if @flag has FTRACE_ITER_FILTER set, or
3388 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3389 * tracing_lseek() should be used as the lseek routine, and
3390 * release must call ftrace_regex_release().
3393 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3394 struct inode *inode, struct file *file)
3396 struct ftrace_iterator *iter;
3397 struct ftrace_hash *hash;
3400 ftrace_ops_init(ops);
3402 if (unlikely(ftrace_disabled))
3405 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3409 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3417 mutex_lock(&ops->func_hash->regex_lock);
3419 if (flag & FTRACE_ITER_NOTRACE)
3420 hash = ops->func_hash->notrace_hash;
3422 hash = ops->func_hash->filter_hash;
3424 if (file->f_mode & FMODE_WRITE) {
3425 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3427 if (file->f_flags & O_TRUNC)
3428 iter->hash = alloc_ftrace_hash(size_bits);
3430 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3433 trace_parser_put(&iter->parser);
3440 if (file->f_mode & FMODE_READ) {
3441 iter->pg = ftrace_pages_start;
3443 ret = seq_open(file, &show_ftrace_seq_ops);
3445 struct seq_file *m = file->private_data;
3449 free_ftrace_hash(iter->hash);
3450 trace_parser_put(&iter->parser);
3454 file->private_data = iter;
3457 mutex_unlock(&ops->func_hash->regex_lock);
3463 ftrace_filter_open(struct inode *inode, struct file *file)
3465 struct ftrace_ops *ops = inode->i_private;
3467 return ftrace_regex_open(ops,
3468 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3473 ftrace_notrace_open(struct inode *inode, struct file *file)
3475 struct ftrace_ops *ops = inode->i_private;
3477 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3481 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3482 struct ftrace_glob {
3489 * If symbols in an architecture don't correspond exactly to the user-visible
3490 * name of what they represent, it is possible to define this function to
3491 * perform the necessary adjustments.
3493 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3498 static int ftrace_match(char *str, struct ftrace_glob *g)
3503 str = arch_ftrace_match_adjust(str, g->search);
3507 if (strcmp(str, g->search) == 0)
3510 case MATCH_FRONT_ONLY:
3511 if (strncmp(str, g->search, g->len) == 0)
3514 case MATCH_MIDDLE_ONLY:
3515 if (strstr(str, g->search))
3518 case MATCH_END_ONLY:
3520 if (slen >= g->len &&
3521 memcmp(str + slen - g->len, g->search, g->len) == 0)
3530 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3532 struct ftrace_func_entry *entry;
3535 entry = ftrace_lookup_ip(hash, rec->ip);
3537 /* Do nothing if it doesn't exist */
3541 free_hash_entry(hash, entry);
3543 /* Do nothing if it exists */
3547 ret = add_hash_entry(hash, rec->ip);
3553 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3554 struct ftrace_glob *mod_g, int exclude_mod)
3556 char str[KSYM_SYMBOL_LEN];
3559 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3562 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3564 /* blank module name to match all modules */
3566 /* blank module globbing: modname xor exclude_mod */
3567 if ((!exclude_mod) != (!modname))
3572 /* not matching the module */
3573 if (!modname || !mod_matches) {
3580 if (mod_matches && exclude_mod)
3584 /* blank search means to match all funcs in the mod */
3589 return ftrace_match(str, func_g);
3593 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3595 struct ftrace_page *pg;
3596 struct dyn_ftrace *rec;
3597 struct ftrace_glob func_g = { .type = MATCH_FULL };
3598 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3599 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3600 int exclude_mod = 0;
3603 int clear_filter = 0;
3606 func_g.type = filter_parse_regex(func, len, &func_g.search,
3608 func_g.len = strlen(func_g.search);
3612 mod_g.type = filter_parse_regex(mod, strlen(mod),
3613 &mod_g.search, &exclude_mod);
3614 mod_g.len = strlen(mod_g.search);
3617 mutex_lock(&ftrace_lock);
3619 if (unlikely(ftrace_disabled))
3622 do_for_each_ftrace_rec(pg, rec) {
3624 if (rec->flags & FTRACE_FL_DISABLED)
3627 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3628 ret = enter_record(hash, rec, clear_filter);
3635 } while_for_each_ftrace_rec();
3637 mutex_unlock(&ftrace_lock);
3643 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3645 return match_records(hash, buff, len, NULL);
3650 * We register the module command as a template to show others how
3651 * to register the a command as well.
3655 ftrace_mod_callback(struct ftrace_hash *hash,
3656 char *func, char *cmd, char *module, int enable)
3661 * cmd == 'mod' because we only registered this func
3662 * for the 'mod' ftrace_func_command.
3663 * But if you register one func with multiple commands,
3664 * you can tell which command was used by the cmd
3667 ret = match_records(hash, func, strlen(func), module);
3675 static struct ftrace_func_command ftrace_mod_cmd = {
3677 .func = ftrace_mod_callback,
3680 static int __init ftrace_mod_cmd_init(void)
3682 return register_ftrace_command(&ftrace_mod_cmd);
3684 core_initcall(ftrace_mod_cmd_init);
3686 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3687 struct ftrace_ops *op, struct pt_regs *pt_regs)
3689 struct ftrace_func_probe *entry;
3690 struct hlist_head *hhd;
3693 key = hash_long(ip, FTRACE_HASH_BITS);
3695 hhd = &ftrace_func_hash[key];
3697 if (hlist_empty(hhd))
3701 * Disable preemption for these calls to prevent a RCU grace
3702 * period. This syncs the hash iteration and freeing of items
3703 * on the hash. rcu_read_lock is too dangerous here.
3705 preempt_disable_notrace();
3706 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3707 if (entry->ip == ip)
3708 entry->ops->func(ip, parent_ip, &entry->data);
3710 preempt_enable_notrace();
3713 static struct ftrace_ops trace_probe_ops __read_mostly =
3715 .func = function_trace_probe_call,
3716 .flags = FTRACE_OPS_FL_INITIALIZED,
3717 INIT_OPS_HASH(trace_probe_ops)
3720 static int ftrace_probe_registered;
3722 static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
3727 if (ftrace_probe_registered) {
3728 /* still need to update the function call sites */
3730 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3735 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3736 struct hlist_head *hhd = &ftrace_func_hash[i];
3740 /* Nothing registered? */
3741 if (i == FTRACE_FUNC_HASHSIZE)
3744 ret = ftrace_startup(&trace_probe_ops, 0);
3746 ftrace_probe_registered = 1;
3749 static bool __disable_ftrace_function_probe(void)
3753 if (!ftrace_probe_registered)
3756 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3757 struct hlist_head *hhd = &ftrace_func_hash[i];
3762 /* no more funcs left */
3763 ftrace_shutdown(&trace_probe_ops, 0);
3765 ftrace_probe_registered = 0;
3770 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3772 if (entry->ops->free)
3773 entry->ops->free(entry->ops, entry->ip, &entry->data);
3778 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3781 struct ftrace_ops_hash old_hash_ops;
3782 struct ftrace_func_probe *entry;
3783 struct ftrace_glob func_g;
3784 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3785 struct ftrace_hash *old_hash = *orig_hash;
3786 struct ftrace_hash *hash;
3787 struct ftrace_page *pg;
3788 struct dyn_ftrace *rec;
3794 func_g.type = filter_parse_regex(glob, strlen(glob),
3795 &func_g.search, ¬);
3796 func_g.len = strlen(func_g.search);
3798 /* we do not support '!' for function probes */
3802 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3804 old_hash_ops.filter_hash = old_hash;
3805 /* Probes only have filters */
3806 old_hash_ops.notrace_hash = NULL;
3808 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3814 if (unlikely(ftrace_disabled)) {
3819 mutex_lock(&ftrace_lock);
3821 do_for_each_ftrace_rec(pg, rec) {
3823 if (rec->flags & FTRACE_FL_DISABLED)
3826 if (!ftrace_match_record(rec, &func_g, NULL, 0))
3829 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3831 /* If we did not process any, then return error */
3842 * The caller might want to do something special
3843 * for each function we find. We call the callback
3844 * to give the caller an opportunity to do so.
3847 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3848 /* caller does not like this func */
3854 ret = enter_record(hash, rec, 0);
3862 entry->ip = rec->ip;
3864 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3865 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3867 } while_for_each_ftrace_rec();
3869 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3871 __enable_ftrace_function_probe(&old_hash_ops);
3874 free_ftrace_hash_rcu(old_hash);
3879 mutex_unlock(&ftrace_lock);
3881 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3882 free_ftrace_hash(hash);
3888 PROBE_TEST_FUNC = 1,
3893 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3894 void *data, int flags)
3896 struct ftrace_ops_hash old_hash_ops;
3897 struct ftrace_func_entry *rec_entry;
3898 struct ftrace_func_probe *entry;
3899 struct ftrace_func_probe *p;
3900 struct ftrace_glob func_g;
3901 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3902 struct ftrace_hash *old_hash = *orig_hash;
3903 struct list_head free_list;
3904 struct ftrace_hash *hash;
3905 struct hlist_node *tmp;
3906 char str[KSYM_SYMBOL_LEN];
3910 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3911 func_g.search = NULL;
3915 func_g.type = filter_parse_regex(glob, strlen(glob),
3916 &func_g.search, ¬);
3917 func_g.len = strlen(func_g.search);
3919 /* we do not support '!' for function probes */
3924 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3926 old_hash_ops.filter_hash = old_hash;
3927 /* Probes only have filters */
3928 old_hash_ops.notrace_hash = NULL;
3930 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3932 /* Hmm, should report this somehow */
3935 INIT_LIST_HEAD(&free_list);
3937 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3938 struct hlist_head *hhd = &ftrace_func_hash[i];
3940 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3942 /* break up if statements for readability */
3943 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3946 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3949 /* do this last, since it is the most expensive */
3950 if (func_g.search) {
3951 kallsyms_lookup(entry->ip, NULL, NULL,
3953 if (!ftrace_match(str, &func_g))
3957 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3958 /* It is possible more than one entry had this ip */
3960 free_hash_entry(hash, rec_entry);
3962 hlist_del_rcu(&entry->node);
3963 list_add(&entry->free_list, &free_list);
3966 mutex_lock(&ftrace_lock);
3967 disabled = __disable_ftrace_function_probe();
3969 * Remove after the disable is called. Otherwise, if the last
3970 * probe is removed, a null hash means *all enabled*.
3972 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3974 /* still need to update the function call sites */
3975 if (ftrace_enabled && !disabled)
3976 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3978 synchronize_sched();
3980 free_ftrace_hash_rcu(old_hash);
3982 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3983 list_del(&entry->free_list);
3984 ftrace_free_entry(entry);
3986 mutex_unlock(&ftrace_lock);
3989 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3990 free_ftrace_hash(hash);
3994 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3997 __unregister_ftrace_function_probe(glob, ops, data,
3998 PROBE_TEST_FUNC | PROBE_TEST_DATA);
4002 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
4004 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
4007 void unregister_ftrace_function_probe_all(char *glob)
4009 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
4012 static LIST_HEAD(ftrace_commands);
4013 static DEFINE_MUTEX(ftrace_cmd_mutex);
4016 * Currently we only register ftrace commands from __init, so mark this
4019 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4021 struct ftrace_func_command *p;
4024 mutex_lock(&ftrace_cmd_mutex);
4025 list_for_each_entry(p, &ftrace_commands, list) {
4026 if (strcmp(cmd->name, p->name) == 0) {
4031 list_add(&cmd->list, &ftrace_commands);
4033 mutex_unlock(&ftrace_cmd_mutex);
4039 * Currently we only unregister ftrace commands from __init, so mark
4042 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4044 struct ftrace_func_command *p, *n;
4047 mutex_lock(&ftrace_cmd_mutex);
4048 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4049 if (strcmp(cmd->name, p->name) == 0) {
4051 list_del_init(&p->list);
4056 mutex_unlock(&ftrace_cmd_mutex);
4061 static int ftrace_process_regex(struct ftrace_hash *hash,
4062 char *buff, int len, int enable)
4064 char *func, *command, *next = buff;
4065 struct ftrace_func_command *p;
4068 func = strsep(&next, ":");
4071 ret = ftrace_match_records(hash, func, len);
4081 command = strsep(&next, ":");
4083 mutex_lock(&ftrace_cmd_mutex);
4084 list_for_each_entry(p, &ftrace_commands, list) {
4085 if (strcmp(p->name, command) == 0) {
4086 ret = p->func(hash, func, command, next, enable);
4091 mutex_unlock(&ftrace_cmd_mutex);
4097 ftrace_regex_write(struct file *file, const char __user *ubuf,
4098 size_t cnt, loff_t *ppos, int enable)
4100 struct ftrace_iterator *iter;
4101 struct trace_parser *parser;
4107 if (file->f_mode & FMODE_READ) {
4108 struct seq_file *m = file->private_data;
4111 iter = file->private_data;
4113 if (unlikely(ftrace_disabled))
4116 /* iter->hash is a local copy, so we don't need regex_lock */
4118 parser = &iter->parser;
4119 read = trace_get_user(parser, ubuf, cnt, ppos);
4121 if (read >= 0 && trace_parser_loaded(parser) &&
4122 !trace_parser_cont(parser)) {
4123 ret = ftrace_process_regex(iter->hash, parser->buffer,
4124 parser->idx, enable);
4125 trace_parser_clear(parser);
4136 ftrace_filter_write(struct file *file, const char __user *ubuf,
4137 size_t cnt, loff_t *ppos)
4139 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4143 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4144 size_t cnt, loff_t *ppos)
4146 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4150 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4152 struct ftrace_func_entry *entry;
4154 if (!ftrace_location(ip))
4158 entry = ftrace_lookup_ip(hash, ip);
4161 free_hash_entry(hash, entry);
4165 return add_hash_entry(hash, ip);
4168 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4169 struct ftrace_ops_hash *old_hash)
4171 struct ftrace_ops *op;
4173 if (!ftrace_enabled)
4176 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4177 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4182 * If this is the shared global_ops filter, then we need to
4183 * check if there is another ops that shares it, is enabled.
4184 * If so, we still need to run the modify code.
4186 if (ops->func_hash != &global_ops.local_hash)
4189 do_for_each_ftrace_op(op, ftrace_ops_list) {
4190 if (op->func_hash == &global_ops.local_hash &&
4191 op->flags & FTRACE_OPS_FL_ENABLED) {
4192 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4193 /* Only need to do this once */
4196 } while_for_each_ftrace_op(op);
4200 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4201 unsigned long ip, int remove, int reset, int enable)
4203 struct ftrace_hash **orig_hash;
4204 struct ftrace_ops_hash old_hash_ops;
4205 struct ftrace_hash *old_hash;
4206 struct ftrace_hash *hash;
4209 if (unlikely(ftrace_disabled))
4212 mutex_lock(&ops->func_hash->regex_lock);
4215 orig_hash = &ops->func_hash->filter_hash;
4217 orig_hash = &ops->func_hash->notrace_hash;
4220 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4222 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4226 goto out_regex_unlock;
4229 if (buf && !ftrace_match_records(hash, buf, len)) {
4231 goto out_regex_unlock;
4234 ret = ftrace_match_addr(hash, ip, remove);
4236 goto out_regex_unlock;
4239 mutex_lock(&ftrace_lock);
4240 old_hash = *orig_hash;
4241 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4242 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4243 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4245 ftrace_ops_update_code(ops, &old_hash_ops);
4246 free_ftrace_hash_rcu(old_hash);
4248 mutex_unlock(&ftrace_lock);
4251 mutex_unlock(&ops->func_hash->regex_lock);
4253 free_ftrace_hash(hash);
4258 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4259 int reset, int enable)
4261 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4265 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4266 * @ops - the ops to set the filter with
4267 * @ip - the address to add to or remove from the filter.
4268 * @remove - non zero to remove the ip from the filter
4269 * @reset - non zero to reset all filters before applying this filter.
4271 * Filters denote which functions should be enabled when tracing is enabled
4272 * If @ip is NULL, it failes to update filter.
4274 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4275 int remove, int reset)
4277 ftrace_ops_init(ops);
4278 return ftrace_set_addr(ops, ip, remove, reset, 1);
4280 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4283 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4284 int reset, int enable)
4286 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4290 * ftrace_set_filter - set a function to filter on in ftrace
4291 * @ops - the ops to set the filter with
4292 * @buf - the string that holds the function filter text.
4293 * @len - the length of the string.
4294 * @reset - non zero to reset all filters before applying this filter.
4296 * Filters denote which functions should be enabled when tracing is enabled.
4297 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4299 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4302 ftrace_ops_init(ops);
4303 return ftrace_set_regex(ops, buf, len, reset, 1);
4305 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4308 * ftrace_set_notrace - set a function to not trace in ftrace
4309 * @ops - the ops to set the notrace filter with
4310 * @buf - the string that holds the function notrace text.
4311 * @len - the length of the string.
4312 * @reset - non zero to reset all filters before applying this filter.
4314 * Notrace Filters denote which functions should not be enabled when tracing
4315 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4318 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4321 ftrace_ops_init(ops);
4322 return ftrace_set_regex(ops, buf, len, reset, 0);
4324 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4326 * ftrace_set_global_filter - set a function to filter on with global tracers
4327 * @buf - the string that holds the function filter text.
4328 * @len - the length of the string.
4329 * @reset - non zero to reset all filters before applying this filter.
4331 * Filters denote which functions should be enabled when tracing is enabled.
4332 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4334 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4336 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4338 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4341 * ftrace_set_global_notrace - set a function to not trace with global tracers
4342 * @buf - the string that holds the function notrace text.
4343 * @len - the length of the string.
4344 * @reset - non zero to reset all filters before applying this filter.
4346 * Notrace Filters denote which functions should not be enabled when tracing
4347 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4350 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4352 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4354 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4357 * command line interface to allow users to set filters on boot up.
4359 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4360 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4361 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4363 /* Used by function selftest to not test if filter is set */
4364 bool ftrace_filter_param __initdata;
4366 static int __init set_ftrace_notrace(char *str)
4368 ftrace_filter_param = true;
4369 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4372 __setup("ftrace_notrace=", set_ftrace_notrace);
4374 static int __init set_ftrace_filter(char *str)
4376 ftrace_filter_param = true;
4377 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4380 __setup("ftrace_filter=", set_ftrace_filter);
4382 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4383 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4384 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4385 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
4387 static int __init set_graph_function(char *str)
4389 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4392 __setup("ftrace_graph_filter=", set_graph_function);
4394 static int __init set_graph_notrace_function(char *str)
4396 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4399 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4401 static void __init set_ftrace_early_graph(char *buf, int enable)
4405 unsigned long *table = ftrace_graph_funcs;
4406 int *count = &ftrace_graph_count;
4409 table = ftrace_graph_notrace_funcs;
4410 count = &ftrace_graph_notrace_count;
4414 func = strsep(&buf, ",");
4415 /* we allow only one expression at a time */
4416 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
4418 printk(KERN_DEBUG "ftrace: function %s not "
4419 "traceable\n", func);
4422 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4425 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4429 ftrace_ops_init(ops);
4432 func = strsep(&buf, ",");
4433 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4437 static void __init set_ftrace_early_filters(void)
4439 if (ftrace_filter_buf[0])
4440 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4441 if (ftrace_notrace_buf[0])
4442 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4443 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4444 if (ftrace_graph_buf[0])
4445 set_ftrace_early_graph(ftrace_graph_buf, 1);
4446 if (ftrace_graph_notrace_buf[0])
4447 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4448 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4451 int ftrace_regex_release(struct inode *inode, struct file *file)
4453 struct seq_file *m = (struct seq_file *)file->private_data;
4454 struct ftrace_ops_hash old_hash_ops;
4455 struct ftrace_iterator *iter;
4456 struct ftrace_hash **orig_hash;
4457 struct ftrace_hash *old_hash;
4458 struct trace_parser *parser;
4462 if (file->f_mode & FMODE_READ) {
4464 seq_release(inode, file);
4466 iter = file->private_data;
4468 parser = &iter->parser;
4469 if (trace_parser_loaded(parser)) {
4470 parser->buffer[parser->idx] = 0;
4471 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
4474 trace_parser_put(parser);
4476 mutex_lock(&iter->ops->func_hash->regex_lock);
4478 if (file->f_mode & FMODE_WRITE) {
4479 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4482 orig_hash = &iter->ops->func_hash->filter_hash;
4484 orig_hash = &iter->ops->func_hash->notrace_hash;
4486 mutex_lock(&ftrace_lock);
4487 old_hash = *orig_hash;
4488 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4489 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
4490 ret = ftrace_hash_move(iter->ops, filter_hash,
4491 orig_hash, iter->hash);
4493 ftrace_ops_update_code(iter->ops, &old_hash_ops);
4494 free_ftrace_hash_rcu(old_hash);
4496 mutex_unlock(&ftrace_lock);
4499 mutex_unlock(&iter->ops->func_hash->regex_lock);
4500 free_ftrace_hash(iter->hash);
4506 static const struct file_operations ftrace_avail_fops = {
4507 .open = ftrace_avail_open,
4509 .llseek = seq_lseek,
4510 .release = seq_release_private,
4513 static const struct file_operations ftrace_enabled_fops = {
4514 .open = ftrace_enabled_open,
4516 .llseek = seq_lseek,
4517 .release = seq_release_private,
4520 static const struct file_operations ftrace_filter_fops = {
4521 .open = ftrace_filter_open,
4523 .write = ftrace_filter_write,
4524 .llseek = tracing_lseek,
4525 .release = ftrace_regex_release,
4528 static const struct file_operations ftrace_notrace_fops = {
4529 .open = ftrace_notrace_open,
4531 .write = ftrace_notrace_write,
4532 .llseek = tracing_lseek,
4533 .release = ftrace_regex_release,
4536 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4538 static DEFINE_MUTEX(graph_lock);
4540 int ftrace_graph_count;
4541 int ftrace_graph_notrace_count;
4542 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4543 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4545 struct ftrace_graph_data {
4546 unsigned long *table;
4549 const struct seq_operations *seq_ops;
4553 __g_next(struct seq_file *m, loff_t *pos)
4555 struct ftrace_graph_data *fgd = m->private;
4557 if (*pos >= *fgd->count)
4559 return &fgd->table[*pos];
4563 g_next(struct seq_file *m, void *v, loff_t *pos)
4566 return __g_next(m, pos);
4569 static void *g_start(struct seq_file *m, loff_t *pos)
4571 struct ftrace_graph_data *fgd = m->private;
4573 mutex_lock(&graph_lock);
4575 /* Nothing, tell g_show to print all functions are enabled */
4576 if (!*fgd->count && !*pos)
4579 return __g_next(m, pos);
4582 static void g_stop(struct seq_file *m, void *p)
4584 mutex_unlock(&graph_lock);
4587 static int g_show(struct seq_file *m, void *v)
4589 unsigned long *ptr = v;
4594 if (ptr == (unsigned long *)1) {
4595 struct ftrace_graph_data *fgd = m->private;
4597 if (fgd->table == ftrace_graph_funcs)
4598 seq_puts(m, "#### all functions enabled ####\n");
4600 seq_puts(m, "#### no functions disabled ####\n");
4604 seq_printf(m, "%ps\n", (void *)*ptr);
4609 static const struct seq_operations ftrace_graph_seq_ops = {
4617 __ftrace_graph_open(struct inode *inode, struct file *file,
4618 struct ftrace_graph_data *fgd)
4622 mutex_lock(&graph_lock);
4623 if ((file->f_mode & FMODE_WRITE) &&
4624 (file->f_flags & O_TRUNC)) {
4626 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4628 mutex_unlock(&graph_lock);
4630 if (file->f_mode & FMODE_READ) {
4631 ret = seq_open(file, fgd->seq_ops);
4633 struct seq_file *m = file->private_data;
4637 file->private_data = fgd;
4643 ftrace_graph_open(struct inode *inode, struct file *file)
4645 struct ftrace_graph_data *fgd;
4647 if (unlikely(ftrace_disabled))
4650 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4654 fgd->table = ftrace_graph_funcs;
4655 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4656 fgd->count = &ftrace_graph_count;
4657 fgd->seq_ops = &ftrace_graph_seq_ops;
4659 return __ftrace_graph_open(inode, file, fgd);
4663 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4665 struct ftrace_graph_data *fgd;
4667 if (unlikely(ftrace_disabled))
4670 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4674 fgd->table = ftrace_graph_notrace_funcs;
4675 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4676 fgd->count = &ftrace_graph_notrace_count;
4677 fgd->seq_ops = &ftrace_graph_seq_ops;
4679 return __ftrace_graph_open(inode, file, fgd);
4683 ftrace_graph_release(struct inode *inode, struct file *file)
4685 if (file->f_mode & FMODE_READ) {
4686 struct seq_file *m = file->private_data;
4689 seq_release(inode, file);
4691 kfree(file->private_data);
4698 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4700 struct ftrace_glob func_g;
4701 struct dyn_ftrace *rec;
4702 struct ftrace_page *pg;
4709 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4710 &func_g.search, ¬);
4711 if (!not && *idx >= size)
4714 func_g.len = strlen(func_g.search);
4716 mutex_lock(&ftrace_lock);
4718 if (unlikely(ftrace_disabled)) {
4719 mutex_unlock(&ftrace_lock);
4723 do_for_each_ftrace_rec(pg, rec) {
4725 if (rec->flags & FTRACE_FL_DISABLED)
4728 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
4729 /* if it is in the array */
4731 for (i = 0; i < *idx; i++) {
4732 if (array[i] == rec->ip) {
4741 array[(*idx)++] = rec->ip;
4747 array[i] = array[--(*idx)];
4753 } while_for_each_ftrace_rec();
4755 mutex_unlock(&ftrace_lock);
4764 ftrace_graph_write(struct file *file, const char __user *ubuf,
4765 size_t cnt, loff_t *ppos)
4767 struct trace_parser parser;
4768 ssize_t read, ret = 0;
4769 struct ftrace_graph_data *fgd = file->private_data;
4774 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4777 read = trace_get_user(&parser, ubuf, cnt, ppos);
4779 if (read >= 0 && trace_parser_loaded((&parser))) {
4780 parser.buffer[parser.idx] = 0;
4782 mutex_lock(&graph_lock);
4784 /* we allow only one expression at a time */
4785 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4788 mutex_unlock(&graph_lock);
4794 trace_parser_put(&parser);
4799 static const struct file_operations ftrace_graph_fops = {
4800 .open = ftrace_graph_open,
4802 .write = ftrace_graph_write,
4803 .llseek = tracing_lseek,
4804 .release = ftrace_graph_release,
4807 static const struct file_operations ftrace_graph_notrace_fops = {
4808 .open = ftrace_graph_notrace_open,
4810 .write = ftrace_graph_write,
4811 .llseek = tracing_lseek,
4812 .release = ftrace_graph_release,
4814 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4816 void ftrace_create_filter_files(struct ftrace_ops *ops,
4817 struct dentry *parent)
4820 trace_create_file("set_ftrace_filter", 0644, parent,
4821 ops, &ftrace_filter_fops);
4823 trace_create_file("set_ftrace_notrace", 0644, parent,
4824 ops, &ftrace_notrace_fops);
4828 * The name "destroy_filter_files" is really a misnomer. Although
4829 * in the future, it may actualy delete the files, but this is
4830 * really intended to make sure the ops passed in are disabled
4831 * and that when this function returns, the caller is free to
4834 * The "destroy" name is only to match the "create" name that this
4835 * should be paired with.
4837 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4839 mutex_lock(&ftrace_lock);
4840 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4841 ftrace_shutdown(ops, 0);
4842 ops->flags |= FTRACE_OPS_FL_DELETED;
4843 ftrace_free_filter(ops);
4844 mutex_unlock(&ftrace_lock);
4847 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
4850 trace_create_file("available_filter_functions", 0444,
4851 d_tracer, NULL, &ftrace_avail_fops);
4853 trace_create_file("enabled_functions", 0444,
4854 d_tracer, NULL, &ftrace_enabled_fops);
4856 ftrace_create_filter_files(&global_ops, d_tracer);
4858 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4859 trace_create_file("set_graph_function", 0444, d_tracer,
4861 &ftrace_graph_fops);
4862 trace_create_file("set_graph_notrace", 0444, d_tracer,
4864 &ftrace_graph_notrace_fops);
4865 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4870 static int ftrace_cmp_ips(const void *a, const void *b)
4872 const unsigned long *ipa = a;
4873 const unsigned long *ipb = b;
4882 static int __norecordmcount ftrace_process_locs(struct module *mod,
4883 unsigned long *start,
4886 struct ftrace_page *start_pg;
4887 struct ftrace_page *pg;
4888 struct dyn_ftrace *rec;
4889 unsigned long count;
4892 unsigned long flags = 0; /* Shut up gcc */
4895 count = end - start;
4900 sort(start, count, sizeof(*start),
4901 ftrace_cmp_ips, NULL);
4903 start_pg = ftrace_allocate_pages(count);
4907 mutex_lock(&ftrace_lock);
4910 * Core and each module needs their own pages, as
4911 * modules will free them when they are removed.
4912 * Force a new page to be allocated for modules.
4915 WARN_ON(ftrace_pages || ftrace_pages_start);
4916 /* First initialization */
4917 ftrace_pages = ftrace_pages_start = start_pg;
4922 if (WARN_ON(ftrace_pages->next)) {
4923 /* Hmm, we have free pages? */
4924 while (ftrace_pages->next)
4925 ftrace_pages = ftrace_pages->next;
4928 ftrace_pages->next = start_pg;
4934 addr = ftrace_call_adjust(*p++);
4936 * Some architecture linkers will pad between
4937 * the different mcount_loc sections of different
4938 * object files to satisfy alignments.
4939 * Skip any NULL pointers.
4944 if (pg->index == pg->size) {
4945 /* We should have allocated enough */
4946 if (WARN_ON(!pg->next))
4951 rec = &pg->records[pg->index++];
4955 /* We should have used all pages */
4958 /* Assign the last page to ftrace_pages */
4962 * We only need to disable interrupts on start up
4963 * because we are modifying code that an interrupt
4964 * may execute, and the modification is not atomic.
4965 * But for modules, nothing runs the code we modify
4966 * until we are finished with it, and there's no
4967 * reason to cause large interrupt latencies while we do it.
4970 local_irq_save(flags);
4971 ftrace_update_code(mod, start_pg);
4973 local_irq_restore(flags);
4976 mutex_unlock(&ftrace_lock);
4981 #ifdef CONFIG_MODULES
4983 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4985 static int referenced_filters(struct dyn_ftrace *rec)
4987 struct ftrace_ops *ops;
4990 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
4991 if (ops_references_rec(ops, rec))
4998 void ftrace_release_mod(struct module *mod)
5000 struct dyn_ftrace *rec;
5001 struct ftrace_page **last_pg;
5002 struct ftrace_page *pg;
5005 mutex_lock(&ftrace_lock);
5007 if (ftrace_disabled)
5011 * Each module has its own ftrace_pages, remove
5012 * them from the list.
5014 last_pg = &ftrace_pages_start;
5015 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5016 rec = &pg->records[0];
5017 if (within_module_core(rec->ip, mod)) {
5019 * As core pages are first, the first
5020 * page should never be a module page.
5022 if (WARN_ON(pg == ftrace_pages_start))
5025 /* Check if we are deleting the last page */
5026 if (pg == ftrace_pages)
5027 ftrace_pages = next_to_ftrace_page(last_pg);
5029 *last_pg = pg->next;
5030 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5031 free_pages((unsigned long)pg->records, order);
5034 last_pg = &pg->next;
5037 mutex_unlock(&ftrace_lock);
5040 void ftrace_module_enable(struct module *mod)
5042 struct dyn_ftrace *rec;
5043 struct ftrace_page *pg;
5045 mutex_lock(&ftrace_lock);
5047 if (ftrace_disabled)
5051 * If the tracing is enabled, go ahead and enable the record.
5053 * The reason not to enable the record immediatelly is the
5054 * inherent check of ftrace_make_nop/ftrace_make_call for
5055 * correct previous instructions. Making first the NOP
5056 * conversion puts the module to the correct state, thus
5057 * passing the ftrace_make_call check.
5059 * We also delay this to after the module code already set the
5060 * text to read-only, as we now need to set it back to read-write
5061 * so that we can modify the text.
5063 if (ftrace_start_up)
5064 ftrace_arch_code_modify_prepare();
5066 do_for_each_ftrace_rec(pg, rec) {
5069 * do_for_each_ftrace_rec() is a double loop.
5070 * module text shares the pg. If a record is
5071 * not part of this module, then skip this pg,
5072 * which the "break" will do.
5074 if (!within_module_core(rec->ip, mod))
5080 * When adding a module, we need to check if tracers are
5081 * currently enabled and if they are, and can trace this record,
5082 * we need to enable the module functions as well as update the
5083 * reference counts for those function records.
5085 if (ftrace_start_up)
5086 cnt += referenced_filters(rec);
5088 /* This clears FTRACE_FL_DISABLED */
5091 if (ftrace_start_up && cnt) {
5092 int failed = __ftrace_replace_code(rec, 1);
5094 ftrace_bug(failed, rec);
5099 } while_for_each_ftrace_rec();
5102 if (ftrace_start_up)
5103 ftrace_arch_code_modify_post_process();
5106 mutex_unlock(&ftrace_lock);
5109 void ftrace_module_init(struct module *mod)
5111 if (ftrace_disabled || !mod->num_ftrace_callsites)
5114 ftrace_process_locs(mod, mod->ftrace_callsites,
5115 mod->ftrace_callsites + mod->num_ftrace_callsites);
5117 #endif /* CONFIG_MODULES */
5119 void __init ftrace_init(void)
5121 extern unsigned long __start_mcount_loc[];
5122 extern unsigned long __stop_mcount_loc[];
5123 unsigned long count, flags;
5126 local_irq_save(flags);
5127 ret = ftrace_dyn_arch_init();
5128 local_irq_restore(flags);
5132 count = __stop_mcount_loc - __start_mcount_loc;
5134 pr_info("ftrace: No functions to be traced?\n");
5138 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5139 count, count / ENTRIES_PER_PAGE + 1);
5141 last_ftrace_enabled = ftrace_enabled = 1;
5143 ret = ftrace_process_locs(NULL,
5147 set_ftrace_early_filters();
5151 ftrace_disabled = 1;
5154 /* Do nothing if arch does not support this */
5155 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5159 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5163 * Currently there's no safe way to free a trampoline when the kernel
5164 * is configured with PREEMPT. That is because a task could be preempted
5165 * when it jumped to the trampoline, it may be preempted for a long time
5166 * depending on the system load, and currently there's no way to know
5167 * when it will be off the trampoline. If the trampoline is freed
5168 * too early, when the task runs again, it will be executing on freed
5171 #ifdef CONFIG_PREEMPT
5172 /* Currently, only non dynamic ops can have a trampoline */
5173 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5177 arch_ftrace_update_trampoline(ops);
5182 static struct ftrace_ops global_ops = {
5183 .func = ftrace_stub,
5184 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5185 FTRACE_OPS_FL_INITIALIZED |
5189 static int __init ftrace_nodyn_init(void)
5194 core_initcall(ftrace_nodyn_init);
5196 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
5197 static inline void ftrace_startup_enable(int command) { }
5198 static inline void ftrace_startup_all(int command) { }
5199 /* Keep as macros so we do not need to define the commands */
5200 # define ftrace_startup(ops, command) \
5202 int ___ret = __register_ftrace_function(ops); \
5204 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5207 # define ftrace_shutdown(ops, command) \
5209 int ___ret = __unregister_ftrace_function(ops); \
5211 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5215 # define ftrace_startup_sysctl() do { } while (0)
5216 # define ftrace_shutdown_sysctl() do { } while (0)
5219 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
5224 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5228 #endif /* CONFIG_DYNAMIC_FTRACE */
5230 __init void ftrace_init_global_array_ops(struct trace_array *tr)
5232 tr->ops = &global_ops;
5233 tr->ops->private = tr;
5236 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5238 /* If we filter on pids, update to use the pid function */
5239 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5240 if (WARN_ON(tr->ops->func != ftrace_stub))
5241 printk("ftrace ops had %pS for function\n",
5244 tr->ops->func = func;
5245 tr->ops->private = tr;
5248 void ftrace_reset_array_ops(struct trace_array *tr)
5250 tr->ops->func = ftrace_stub;
5253 static nokprobe_inline void
5254 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5255 struct ftrace_ops *ignored, struct pt_regs *regs)
5257 struct ftrace_ops *op;
5260 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5265 * Some of the ops may be dynamically allocated,
5266 * they must be freed after a synchronize_sched().
5268 preempt_disable_notrace();
5270 do_for_each_ftrace_op(op, ftrace_ops_list) {
5272 * Check the following for each ops before calling their func:
5273 * if RCU flag is set, then rcu_is_watching() must be true
5274 * if PER_CPU is set, then ftrace_function_local_disable()
5276 * Otherwise test if the ip matches the ops filter
5278 * If any of the above fails then the op->func() is not executed.
5280 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5281 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5282 !ftrace_function_local_disabled(op)) &&
5283 ftrace_ops_test(op, ip, regs)) {
5285 if (FTRACE_WARN_ON(!op->func)) {
5286 pr_warn("op=%p %pS\n", op, op);
5289 op->func(ip, parent_ip, op, regs);
5291 } while_for_each_ftrace_op(op);
5293 preempt_enable_notrace();
5294 trace_clear_recursion(bit);
5298 * Some archs only support passing ip and parent_ip. Even though
5299 * the list function ignores the op parameter, we do not want any
5300 * C side effects, where a function is called without the caller
5301 * sending a third parameter.
5302 * Archs are to support both the regs and ftrace_ops at the same time.
5303 * If they support ftrace_ops, it is assumed they support regs.
5304 * If call backs want to use regs, they must either check for regs
5305 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5306 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
5307 * An architecture can pass partial regs with ftrace_ops and still
5308 * set the ARCH_SUPPORTS_FTRACE_OPS.
5310 #if ARCH_SUPPORTS_FTRACE_OPS
5311 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5312 struct ftrace_ops *op, struct pt_regs *regs)
5314 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
5316 NOKPROBE_SYMBOL(ftrace_ops_list_func);
5318 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
5319 struct ftrace_ops *op, struct pt_regs *regs)
5321 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
5323 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
5327 * If there's only one function registered but it does not support
5328 * recursion, needs RCU protection and/or requires per cpu handling, then
5329 * this function will be called by the mcount trampoline.
5331 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
5332 struct ftrace_ops *op, struct pt_regs *regs)
5336 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
5339 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5343 preempt_disable_notrace();
5345 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5346 !ftrace_function_local_disabled(op)) {
5347 op->func(ip, parent_ip, op, regs);
5350 preempt_enable_notrace();
5351 trace_clear_recursion(bit);
5353 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
5356 * ftrace_ops_get_func - get the function a trampoline should call
5357 * @ops: the ops to get the function for
5359 * Normally the mcount trampoline will call the ops->func, but there
5360 * are times that it should not. For example, if the ops does not
5361 * have its own recursion protection, then it should call the
5362 * ftrace_ops_recurs_func() instead.
5364 * Returns the function that the trampoline should call for @ops.
5366 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5369 * If the function does not handle recursion, needs to be RCU safe,
5370 * or does per cpu logic, then we need to call the assist handler.
5372 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5373 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5374 return ftrace_ops_assist_func;
5380 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5381 struct task_struct *prev, struct task_struct *next)
5383 struct trace_array *tr = data;
5384 struct trace_pid_list *pid_list;
5386 pid_list = rcu_dereference_sched(tr->function_pids);
5388 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5389 trace_ignore_this_task(pid_list, next));
5392 static void clear_ftrace_pids(struct trace_array *tr)
5394 struct trace_pid_list *pid_list;
5397 pid_list = rcu_dereference_protected(tr->function_pids,
5398 lockdep_is_held(&ftrace_lock));
5402 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5404 for_each_possible_cpu(cpu)
5405 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5407 rcu_assign_pointer(tr->function_pids, NULL);
5409 /* Wait till all users are no longer using pid filtering */
5410 synchronize_sched();
5412 trace_free_pid_list(pid_list);
5415 void ftrace_clear_pids(struct trace_array *tr)
5417 mutex_lock(&ftrace_lock);
5419 clear_ftrace_pids(tr);
5421 mutex_unlock(&ftrace_lock);
5424 static void ftrace_pid_reset(struct trace_array *tr)
5426 mutex_lock(&ftrace_lock);
5427 clear_ftrace_pids(tr);
5429 ftrace_update_pid_func();
5430 ftrace_startup_all(0);
5432 mutex_unlock(&ftrace_lock);
5435 /* Greater than any max PID */
5436 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5438 static void *fpid_start(struct seq_file *m, loff_t *pos)
5441 struct trace_pid_list *pid_list;
5442 struct trace_array *tr = m->private;
5444 mutex_lock(&ftrace_lock);
5445 rcu_read_lock_sched();
5447 pid_list = rcu_dereference_sched(tr->function_pids);
5450 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5452 return trace_pid_start(pid_list, pos);
5455 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5457 struct trace_array *tr = m->private;
5458 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5460 if (v == FTRACE_NO_PIDS)
5463 return trace_pid_next(pid_list, v, pos);
5466 static void fpid_stop(struct seq_file *m, void *p)
5469 rcu_read_unlock_sched();
5470 mutex_unlock(&ftrace_lock);
5473 static int fpid_show(struct seq_file *m, void *v)
5475 if (v == FTRACE_NO_PIDS) {
5476 seq_puts(m, "no pid\n");
5480 return trace_pid_show(m, v);
5483 static const struct seq_operations ftrace_pid_sops = {
5484 .start = fpid_start,
5491 ftrace_pid_open(struct inode *inode, struct file *file)
5493 struct trace_array *tr = inode->i_private;
5497 if (trace_array_get(tr) < 0)
5500 if ((file->f_mode & FMODE_WRITE) &&
5501 (file->f_flags & O_TRUNC))
5502 ftrace_pid_reset(tr);
5504 ret = seq_open(file, &ftrace_pid_sops);
5506 trace_array_put(tr);
5508 m = file->private_data;
5509 /* copy tr over to seq ops */
5516 static void ignore_task_cpu(void *data)
5518 struct trace_array *tr = data;
5519 struct trace_pid_list *pid_list;
5522 * This function is called by on_each_cpu() while the
5523 * event_mutex is held.
5525 pid_list = rcu_dereference_protected(tr->function_pids,
5526 mutex_is_locked(&ftrace_lock));
5528 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5529 trace_ignore_this_task(pid_list, current));
5533 ftrace_pid_write(struct file *filp, const char __user *ubuf,
5534 size_t cnt, loff_t *ppos)
5536 struct seq_file *m = filp->private_data;
5537 struct trace_array *tr = m->private;
5538 struct trace_pid_list *filtered_pids = NULL;
5539 struct trace_pid_list *pid_list;
5545 mutex_lock(&ftrace_lock);
5547 filtered_pids = rcu_dereference_protected(tr->function_pids,
5548 lockdep_is_held(&ftrace_lock));
5550 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5554 rcu_assign_pointer(tr->function_pids, pid_list);
5556 if (filtered_pids) {
5557 synchronize_sched();
5558 trace_free_pid_list(filtered_pids);
5559 } else if (pid_list) {
5560 /* Register a probe to set whether to ignore the tracing of a task */
5561 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5565 * Ignoring of pids is done at task switch. But we have to
5566 * check for those tasks that are currently running.
5567 * Always do this in case a pid was appended or removed.
5569 on_each_cpu(ignore_task_cpu, tr, 1);
5571 ftrace_update_pid_func();
5572 ftrace_startup_all(0);
5574 mutex_unlock(&ftrace_lock);
5583 ftrace_pid_release(struct inode *inode, struct file *file)
5585 struct trace_array *tr = inode->i_private;
5587 trace_array_put(tr);
5589 return seq_release(inode, file);
5592 static const struct file_operations ftrace_pid_fops = {
5593 .open = ftrace_pid_open,
5594 .write = ftrace_pid_write,
5596 .llseek = tracing_lseek,
5597 .release = ftrace_pid_release,
5600 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
5602 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5603 tr, &ftrace_pid_fops);
5606 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5607 struct dentry *d_tracer)
5609 /* Only the top level directory has the dyn_tracefs and profile */
5610 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5612 ftrace_init_dyn_tracefs(d_tracer);
5613 ftrace_profile_tracefs(d_tracer);
5617 * ftrace_kill - kill ftrace
5619 * This function should be used by panic code. It stops ftrace
5620 * but in a not so nice way. If you need to simply kill ftrace
5621 * from a non-atomic section, use ftrace_kill.
5623 void ftrace_kill(void)
5625 ftrace_disabled = 1;
5627 clear_ftrace_function();
5631 * Test if ftrace is dead or not.
5633 int ftrace_is_dead(void)
5635 return ftrace_disabled;
5639 * register_ftrace_function - register a function for profiling
5640 * @ops - ops structure that holds the function for profiling.
5642 * Register a function to be called by all functions in the
5645 * Note: @ops->func and all the functions it calls must be labeled
5646 * with "notrace", otherwise it will go into a
5649 int register_ftrace_function(struct ftrace_ops *ops)
5653 ftrace_ops_init(ops);
5655 mutex_lock(&ftrace_lock);
5657 ret = ftrace_startup(ops, 0);
5659 mutex_unlock(&ftrace_lock);
5663 EXPORT_SYMBOL_GPL(register_ftrace_function);
5666 * unregister_ftrace_function - unregister a function for profiling.
5667 * @ops - ops structure that holds the function to unregister
5669 * Unregister a function that was added to be called by ftrace profiling.
5671 int unregister_ftrace_function(struct ftrace_ops *ops)
5675 mutex_lock(&ftrace_lock);
5676 ret = ftrace_shutdown(ops, 0);
5677 mutex_unlock(&ftrace_lock);
5681 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5684 ftrace_enable_sysctl(struct ctl_table *table, int write,
5685 void __user *buffer, size_t *lenp,
5690 mutex_lock(&ftrace_lock);
5692 if (unlikely(ftrace_disabled))
5695 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5697 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5700 last_ftrace_enabled = !!ftrace_enabled;
5702 if (ftrace_enabled) {
5704 /* we are starting ftrace again */
5705 if (ftrace_ops_list != &ftrace_list_end)
5706 update_ftrace_function();
5708 ftrace_startup_sysctl();
5711 /* stopping ftrace calls (just send to ftrace_stub) */
5712 ftrace_trace_function = ftrace_stub;
5714 ftrace_shutdown_sysctl();
5718 mutex_unlock(&ftrace_lock);
5722 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5724 static struct ftrace_ops graph_ops = {
5725 .func = ftrace_stub,
5726 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5727 FTRACE_OPS_FL_INITIALIZED |
5730 #ifdef FTRACE_GRAPH_TRAMP_ADDR
5731 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5732 /* trampoline_size is only needed for dynamically allocated tramps */
5734 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5737 void ftrace_graph_sleep_time_control(bool enable)
5739 fgraph_sleep_time = enable;
5742 void ftrace_graph_graph_time_control(bool enable)
5744 fgraph_graph_time = enable;
5747 void ftrace_graph_return_stub(struct ftrace_graph_ret *trace)
5751 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5756 /* The callbacks that hook a function */
5757 trace_func_graph_ret_t ftrace_graph_return = ftrace_graph_return_stub;
5758 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5759 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5761 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5762 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5766 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5767 struct task_struct *g, *t;
5769 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5770 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5771 * sizeof(struct ftrace_ret_stack),
5773 if (!ret_stack_list[i]) {
5781 read_lock(&tasklist_lock);
5782 do_each_thread(g, t) {
5788 if (t->ret_stack == NULL) {
5789 atomic_set(&t->tracing_graph_pause, 0);
5790 atomic_set(&t->trace_overrun, 0);
5791 t->curr_ret_stack = -1;
5792 /* Make sure the tasks see the -1 first: */
5794 t->ret_stack = ret_stack_list[start++];
5796 } while_each_thread(g, t);
5799 read_unlock(&tasklist_lock);
5801 for (i = start; i < end; i++)
5802 kfree(ret_stack_list[i]);
5807 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
5808 struct task_struct *prev, struct task_struct *next)
5810 unsigned long long timestamp;
5814 * Does the user want to count the time a function was asleep.
5815 * If so, do not update the time stamps.
5817 if (fgraph_sleep_time)
5820 timestamp = trace_clock_local();
5822 prev->ftrace_timestamp = timestamp;
5824 /* only process tasks that we timestamped */
5825 if (!next->ftrace_timestamp)
5829 * Update all the counters in next to make up for the
5830 * time next was sleeping.
5832 timestamp -= next->ftrace_timestamp;
5834 for (index = next->curr_ret_stack; index >= 0; index--)
5835 next->ret_stack[index].calltime += timestamp;
5838 /* Allocate a return stack for each task */
5839 static int start_graph_tracing(void)
5841 struct ftrace_ret_stack **ret_stack_list;
5844 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5845 sizeof(struct ftrace_ret_stack *),
5848 if (!ret_stack_list)
5851 /* The cpu_boot init_task->ret_stack will never be freed */
5852 for_each_online_cpu(cpu) {
5853 if (!idle_task(cpu)->ret_stack)
5854 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5858 ret = alloc_retstack_tasklist(ret_stack_list);
5859 } while (ret == -EAGAIN);
5862 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5864 pr_info("ftrace_graph: Couldn't activate tracepoint"
5865 " probe to kernel_sched_switch\n");
5868 kfree(ret_stack_list);
5873 * Hibernation protection.
5874 * The state of the current task is too much unstable during
5875 * suspend/restore to disk. We want to protect against that.
5878 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5882 case PM_HIBERNATION_PREPARE:
5883 pause_graph_tracing();
5886 case PM_POST_HIBERNATION:
5887 unpause_graph_tracing();
5893 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5895 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5897 return __ftrace_graph_entry(trace);
5901 * The function graph tracer should only trace the functions defined
5902 * by set_ftrace_filter and set_ftrace_notrace. If another function
5903 * tracer ops is registered, the graph tracer requires testing the
5904 * function against the global ops, and not just trace any function
5905 * that any ftrace_ops registered.
5907 static void update_function_graph_func(void)
5909 struct ftrace_ops *op;
5910 bool do_test = false;
5913 * The graph and global ops share the same set of functions
5914 * to test. If any other ops is on the list, then
5915 * the graph tracing needs to test if its the function
5918 do_for_each_ftrace_op(op, ftrace_ops_list) {
5919 if (op != &global_ops && op != &graph_ops &&
5920 op != &ftrace_list_end) {
5922 /* in double loop, break out with goto */
5925 } while_for_each_ftrace_op(op);
5928 ftrace_graph_entry = ftrace_graph_entry_test;
5930 ftrace_graph_entry = __ftrace_graph_entry;
5933 static struct notifier_block ftrace_suspend_notifier = {
5934 .notifier_call = ftrace_suspend_notifier_call,
5937 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5938 trace_func_graph_ent_t entryfunc)
5942 mutex_lock(&ftrace_lock);
5944 /* we currently allow only one tracer registered at a time */
5945 if (ftrace_graph_active) {
5950 register_pm_notifier(&ftrace_suspend_notifier);
5952 ftrace_graph_active++;
5953 ret = start_graph_tracing();
5955 ftrace_graph_active--;
5959 ftrace_graph_return = retfunc;
5962 * Update the indirect function to the entryfunc, and the
5963 * function that gets called to the entry_test first. Then
5964 * call the update fgraph entry function to determine if
5965 * the entryfunc should be called directly or not.
5967 __ftrace_graph_entry = entryfunc;
5968 ftrace_graph_entry = ftrace_graph_entry_test;
5969 update_function_graph_func();
5971 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
5973 mutex_unlock(&ftrace_lock);
5977 void unregister_ftrace_graph(void)
5979 mutex_lock(&ftrace_lock);
5981 if (unlikely(!ftrace_graph_active))
5984 ftrace_graph_active--;
5985 ftrace_graph_return = ftrace_graph_return_stub;
5986 ftrace_graph_entry = ftrace_graph_entry_stub;
5987 __ftrace_graph_entry = ftrace_graph_entry_stub;
5988 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
5989 unregister_pm_notifier(&ftrace_suspend_notifier);
5990 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5993 mutex_unlock(&ftrace_lock);
5996 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5999 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6001 atomic_set(&t->tracing_graph_pause, 0);
6002 atomic_set(&t->trace_overrun, 0);
6003 t->ftrace_timestamp = 0;
6004 /* make curr_ret_stack visible before we add the ret_stack */
6006 t->ret_stack = ret_stack;
6010 * Allocate a return stack for the idle task. May be the first
6011 * time through, or it may be done by CPU hotplug online.
6013 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6015 t->curr_ret_stack = -1;
6017 * The idle task has no parent, it either has its own
6018 * stack or no stack at all.
6021 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6023 if (ftrace_graph_active) {
6024 struct ftrace_ret_stack *ret_stack;
6026 ret_stack = per_cpu(idle_ret_stack, cpu);
6028 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6029 * sizeof(struct ftrace_ret_stack),
6033 per_cpu(idle_ret_stack, cpu) = ret_stack;
6035 graph_init_task(t, ret_stack);
6039 /* Allocate a return stack for newly created task */
6040 void ftrace_graph_init_task(struct task_struct *t)
6042 /* Make sure we do not use the parent ret_stack */
6043 t->ret_stack = NULL;
6044 t->curr_ret_stack = -1;
6046 if (ftrace_graph_active) {
6047 struct ftrace_ret_stack *ret_stack;
6049 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6050 * sizeof(struct ftrace_ret_stack),
6054 graph_init_task(t, ret_stack);
6058 void ftrace_graph_exit_task(struct task_struct *t)
6060 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6062 t->ret_stack = NULL;
6063 /* NULL must become visible to IRQs before we free it: */