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 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
68 #ifdef CONFIG_DYNAMIC_FTRACE
69 #define INIT_OPS_HASH(opsname) \
70 .func_hash = &opsname.local_hash, \
71 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
72 #define ASSIGN_OPS_HASH(opsname, val) \
74 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
76 #define INIT_OPS_HASH(opsname)
77 #define ASSIGN_OPS_HASH(opsname, val)
80 static struct ftrace_ops ftrace_list_end __read_mostly = {
82 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
83 INIT_OPS_HASH(ftrace_list_end)
86 /* ftrace_enabled is a method to turn ftrace on or off */
87 int ftrace_enabled __read_mostly;
88 static int last_ftrace_enabled;
90 /* Current function tracing op */
91 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
92 /* What to set function_trace_op to */
93 static struct ftrace_ops *set_function_trace_op;
95 /* List for set_ftrace_pid's pids. */
96 LIST_HEAD(ftrace_pids);
98 struct list_head list;
102 static bool ftrace_pids_enabled(void)
104 return !list_empty(&ftrace_pids);
107 static void ftrace_update_trampoline(struct ftrace_ops *ops);
110 * ftrace_disabled is set when an anomaly is discovered.
111 * ftrace_disabled is much stronger than ftrace_enabled.
113 static int ftrace_disabled __read_mostly;
115 static DEFINE_MUTEX(ftrace_lock);
117 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
118 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
119 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
120 static struct ftrace_ops global_ops;
121 static struct ftrace_ops control_ops;
123 static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
124 struct ftrace_ops *op, struct pt_regs *regs);
126 #if ARCH_SUPPORTS_FTRACE_OPS
127 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
128 struct ftrace_ops *op, struct pt_regs *regs);
130 /* See comment below, where ftrace_ops_list_func is defined */
131 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
132 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
136 * Traverse the ftrace_global_list, invoking all entries. The reason that we
137 * can use rcu_dereference_raw_notrace() is that elements removed from this list
138 * are simply leaked, so there is no need to interact with a grace-period
139 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
140 * concurrent insertions into the ftrace_global_list.
142 * Silly Alpha and silly pointer-speculation compiler optimizations!
144 #define do_for_each_ftrace_op(op, list) \
145 op = rcu_dereference_raw_notrace(list); \
149 * Optimized for just a single item in the list (as that is the normal case).
151 #define while_for_each_ftrace_op(op) \
152 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
153 unlikely((op) != &ftrace_list_end))
155 static inline void ftrace_ops_init(struct ftrace_ops *ops)
157 #ifdef CONFIG_DYNAMIC_FTRACE
158 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
159 mutex_init(&ops->local_hash.regex_lock);
160 ops->func_hash = &ops->local_hash;
161 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
167 * ftrace_nr_registered_ops - return number of ops registered
169 * Returns the number of ftrace_ops registered and tracing functions
171 int ftrace_nr_registered_ops(void)
173 struct ftrace_ops *ops;
176 mutex_lock(&ftrace_lock);
178 for (ops = ftrace_ops_list;
179 ops != &ftrace_list_end; ops = ops->next)
182 mutex_unlock(&ftrace_lock);
187 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
188 struct ftrace_ops *op, struct pt_regs *regs)
190 if (!test_tsk_trace_trace(current))
193 op->saved_func(ip, parent_ip, op, regs);
197 * clear_ftrace_function - reset the ftrace function
199 * This NULLs the ftrace function and in essence stops
200 * tracing. There may be lag
202 void clear_ftrace_function(void)
204 ftrace_trace_function = ftrace_stub;
207 static void control_ops_disable_all(struct ftrace_ops *ops)
211 for_each_possible_cpu(cpu)
212 *per_cpu_ptr(ops->disabled, cpu) = 1;
215 static int control_ops_alloc(struct ftrace_ops *ops)
217 int __percpu *disabled;
219 disabled = alloc_percpu(int);
223 ops->disabled = disabled;
224 control_ops_disable_all(ops);
228 static void ftrace_sync(struct work_struct *work)
231 * This function is just a stub to implement a hard force
232 * of synchronize_sched(). This requires synchronizing
233 * tasks even in userspace and idle.
235 * Yes, function tracing is rude.
239 static void ftrace_sync_ipi(void *data)
241 /* Probably not needed, but do it anyway */
245 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
246 static void update_function_graph_func(void);
248 /* Both enabled by default (can be cleared by function_graph tracer flags */
249 static bool fgraph_sleep_time = true;
250 static bool fgraph_graph_time = true;
253 static inline void update_function_graph_func(void) { }
257 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
260 * If this is a dynamic ops or we force list func,
261 * then it needs to call the list anyway.
263 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || 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 add_ftrace_list_ops(struct ftrace_ops **list,
388 struct ftrace_ops *main_ops,
389 struct ftrace_ops *ops)
391 int first = *list == &ftrace_list_end;
392 add_ftrace_ops(list, ops);
394 add_ftrace_ops(&ftrace_ops_list, main_ops);
397 static int remove_ftrace_list_ops(struct ftrace_ops **list,
398 struct ftrace_ops *main_ops,
399 struct ftrace_ops *ops)
401 int ret = remove_ftrace_ops(list, ops);
402 if (!ret && *list == &ftrace_list_end)
403 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
407 static void ftrace_update_trampoline(struct ftrace_ops *ops);
409 static int __register_ftrace_function(struct ftrace_ops *ops)
411 if (ops->flags & FTRACE_OPS_FL_DELETED)
414 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
417 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
419 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
420 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
421 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
423 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
424 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
427 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
428 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
431 if (!core_kernel_data((unsigned long)ops))
432 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
434 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
435 if (control_ops_alloc(ops))
437 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
438 /* The control_ops needs the trampoline update */
441 add_ftrace_ops(&ftrace_ops_list, ops);
443 /* Always save the function, and reset at unregistering */
444 ops->saved_func = ops->func;
446 if (ops->flags & FTRACE_OPS_FL_PID && ftrace_pids_enabled())
447 ops->func = ftrace_pid_func;
449 ftrace_update_trampoline(ops);
452 update_ftrace_function();
457 static int __unregister_ftrace_function(struct ftrace_ops *ops)
461 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
464 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
465 ret = remove_ftrace_list_ops(&ftrace_control_list,
468 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
474 update_ftrace_function();
476 ops->func = ops->saved_func;
481 static void ftrace_update_pid_func(void)
483 bool enabled = ftrace_pids_enabled();
484 struct ftrace_ops *op;
486 /* Only do something if we are tracing something */
487 if (ftrace_trace_function == ftrace_stub)
490 do_for_each_ftrace_op(op, ftrace_ops_list) {
491 if (op->flags & FTRACE_OPS_FL_PID) {
492 op->func = enabled ? ftrace_pid_func :
494 ftrace_update_trampoline(op);
496 } while_for_each_ftrace_op(op);
498 update_ftrace_function();
501 #ifdef CONFIG_FUNCTION_PROFILER
502 struct ftrace_profile {
503 struct hlist_node node;
505 unsigned long counter;
506 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
507 unsigned long long time;
508 unsigned long long time_squared;
512 struct ftrace_profile_page {
513 struct ftrace_profile_page *next;
515 struct ftrace_profile records[];
518 struct ftrace_profile_stat {
520 struct hlist_head *hash;
521 struct ftrace_profile_page *pages;
522 struct ftrace_profile_page *start;
523 struct tracer_stat stat;
526 #define PROFILE_RECORDS_SIZE \
527 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
529 #define PROFILES_PER_PAGE \
530 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
532 static int ftrace_profile_enabled __read_mostly;
534 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
535 static DEFINE_MUTEX(ftrace_profile_lock);
537 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
539 #define FTRACE_PROFILE_HASH_BITS 10
540 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
543 function_stat_next(void *v, int idx)
545 struct ftrace_profile *rec = v;
546 struct ftrace_profile_page *pg;
548 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
554 if ((void *)rec >= (void *)&pg->records[pg->index]) {
558 rec = &pg->records[0];
566 static void *function_stat_start(struct tracer_stat *trace)
568 struct ftrace_profile_stat *stat =
569 container_of(trace, struct ftrace_profile_stat, stat);
571 if (!stat || !stat->start)
574 return function_stat_next(&stat->start->records[0], 0);
577 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
578 /* function graph compares on total time */
579 static int function_stat_cmp(void *p1, void *p2)
581 struct ftrace_profile *a = p1;
582 struct ftrace_profile *b = p2;
584 if (a->time < b->time)
586 if (a->time > b->time)
592 /* not function graph compares against hits */
593 static int function_stat_cmp(void *p1, void *p2)
595 struct ftrace_profile *a = p1;
596 struct ftrace_profile *b = p2;
598 if (a->counter < b->counter)
600 if (a->counter > b->counter)
607 static int function_stat_headers(struct seq_file *m)
609 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
610 seq_puts(m, " Function "
613 "--- ---- --- ---\n");
615 seq_puts(m, " Function Hit\n"
621 static int function_stat_show(struct seq_file *m, void *v)
623 struct ftrace_profile *rec = v;
624 char str[KSYM_SYMBOL_LEN];
626 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
627 static struct trace_seq s;
628 unsigned long long avg;
629 unsigned long long stddev;
631 mutex_lock(&ftrace_profile_lock);
633 /* we raced with function_profile_reset() */
634 if (unlikely(rec->counter == 0)) {
639 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
640 avg = div64_ul(rec->time, rec->counter);
641 if (tracing_thresh && (avg < tracing_thresh))
645 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
646 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
648 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
651 /* Sample standard deviation (s^2) */
652 if (rec->counter <= 1)
656 * Apply Welford's method:
657 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
659 stddev = rec->counter * rec->time_squared -
660 rec->time * rec->time;
663 * Divide only 1000 for ns^2 -> us^2 conversion.
664 * trace_print_graph_duration will divide 1000 again.
666 stddev = div64_ul(stddev,
667 rec->counter * (rec->counter - 1) * 1000);
671 trace_print_graph_duration(rec->time, &s);
672 trace_seq_puts(&s, " ");
673 trace_print_graph_duration(avg, &s);
674 trace_seq_puts(&s, " ");
675 trace_print_graph_duration(stddev, &s);
676 trace_print_seq(m, &s);
680 mutex_unlock(&ftrace_profile_lock);
685 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
687 struct ftrace_profile_page *pg;
689 pg = stat->pages = stat->start;
692 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
697 memset(stat->hash, 0,
698 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
701 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
703 struct ftrace_profile_page *pg;
708 /* If we already allocated, do nothing */
712 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
716 #ifdef CONFIG_DYNAMIC_FTRACE
717 functions = ftrace_update_tot_cnt;
720 * We do not know the number of functions that exist because
721 * dynamic tracing is what counts them. With past experience
722 * we have around 20K functions. That should be more than enough.
723 * It is highly unlikely we will execute every function in
729 pg = stat->start = stat->pages;
731 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
733 for (i = 1; i < pages; i++) {
734 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
745 unsigned long tmp = (unsigned long)pg;
757 static int ftrace_profile_init_cpu(int cpu)
759 struct ftrace_profile_stat *stat;
762 stat = &per_cpu(ftrace_profile_stats, cpu);
765 /* If the profile is already created, simply reset it */
766 ftrace_profile_reset(stat);
771 * We are profiling all functions, but usually only a few thousand
772 * functions are hit. We'll make a hash of 1024 items.
774 size = FTRACE_PROFILE_HASH_SIZE;
776 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
781 /* Preallocate the function profiling pages */
782 if (ftrace_profile_pages_init(stat) < 0) {
791 static int ftrace_profile_init(void)
796 for_each_possible_cpu(cpu) {
797 ret = ftrace_profile_init_cpu(cpu);
805 /* interrupts must be disabled */
806 static struct ftrace_profile *
807 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
809 struct ftrace_profile *rec;
810 struct hlist_head *hhd;
813 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
814 hhd = &stat->hash[key];
816 if (hlist_empty(hhd))
819 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
827 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
828 struct ftrace_profile *rec)
832 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
833 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
837 * The memory is already allocated, this simply finds a new record to use.
839 static struct ftrace_profile *
840 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
842 struct ftrace_profile *rec = NULL;
844 /* prevent recursion (from NMIs) */
845 if (atomic_inc_return(&stat->disabled) != 1)
849 * Try to find the function again since an NMI
850 * could have added it
852 rec = ftrace_find_profiled_func(stat, ip);
856 if (stat->pages->index == PROFILES_PER_PAGE) {
857 if (!stat->pages->next)
859 stat->pages = stat->pages->next;
862 rec = &stat->pages->records[stat->pages->index++];
864 ftrace_add_profile(stat, rec);
867 atomic_dec(&stat->disabled);
873 function_profile_call(unsigned long ip, unsigned long parent_ip,
874 struct ftrace_ops *ops, struct pt_regs *regs)
876 struct ftrace_profile_stat *stat;
877 struct ftrace_profile *rec;
880 if (!ftrace_profile_enabled)
883 local_irq_save(flags);
885 stat = this_cpu_ptr(&ftrace_profile_stats);
886 if (!stat->hash || !ftrace_profile_enabled)
889 rec = ftrace_find_profiled_func(stat, ip);
891 rec = ftrace_profile_alloc(stat, ip);
898 local_irq_restore(flags);
901 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
902 static int profile_graph_entry(struct ftrace_graph_ent *trace)
904 function_profile_call(trace->func, 0, NULL, NULL);
908 static void profile_graph_return(struct ftrace_graph_ret *trace)
910 struct ftrace_profile_stat *stat;
911 unsigned long long calltime;
912 struct ftrace_profile *rec;
915 local_irq_save(flags);
916 stat = this_cpu_ptr(&ftrace_profile_stats);
917 if (!stat->hash || !ftrace_profile_enabled)
920 /* If the calltime was zero'd ignore it */
921 if (!trace->calltime)
924 calltime = trace->rettime - trace->calltime;
926 if (!fgraph_graph_time) {
929 index = trace->depth;
931 /* Append this call time to the parent time to subtract */
933 current->ret_stack[index - 1].subtime += calltime;
935 if (current->ret_stack[index].subtime < calltime)
936 calltime -= current->ret_stack[index].subtime;
941 rec = ftrace_find_profiled_func(stat, trace->func);
943 rec->time += calltime;
944 rec->time_squared += calltime * calltime;
948 local_irq_restore(flags);
951 static int register_ftrace_profiler(void)
953 return register_ftrace_graph(&profile_graph_return,
954 &profile_graph_entry);
957 static void unregister_ftrace_profiler(void)
959 unregister_ftrace_graph();
962 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
963 .func = function_profile_call,
964 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
965 INIT_OPS_HASH(ftrace_profile_ops)
968 static int register_ftrace_profiler(void)
970 return register_ftrace_function(&ftrace_profile_ops);
973 static void unregister_ftrace_profiler(void)
975 unregister_ftrace_function(&ftrace_profile_ops);
977 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
980 ftrace_profile_write(struct file *filp, const char __user *ubuf,
981 size_t cnt, loff_t *ppos)
986 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
992 mutex_lock(&ftrace_profile_lock);
993 if (ftrace_profile_enabled ^ val) {
995 ret = ftrace_profile_init();
1001 ret = register_ftrace_profiler();
1006 ftrace_profile_enabled = 1;
1008 ftrace_profile_enabled = 0;
1010 * unregister_ftrace_profiler calls stop_machine
1011 * so this acts like an synchronize_sched.
1013 unregister_ftrace_profiler();
1017 mutex_unlock(&ftrace_profile_lock);
1025 ftrace_profile_read(struct file *filp, char __user *ubuf,
1026 size_t cnt, loff_t *ppos)
1028 char buf[64]; /* big enough to hold a number */
1031 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1032 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1035 static const struct file_operations ftrace_profile_fops = {
1036 .open = tracing_open_generic,
1037 .read = ftrace_profile_read,
1038 .write = ftrace_profile_write,
1039 .llseek = default_llseek,
1042 /* used to initialize the real stat files */
1043 static struct tracer_stat function_stats __initdata = {
1044 .name = "functions",
1045 .stat_start = function_stat_start,
1046 .stat_next = function_stat_next,
1047 .stat_cmp = function_stat_cmp,
1048 .stat_headers = function_stat_headers,
1049 .stat_show = function_stat_show
1052 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1054 struct ftrace_profile_stat *stat;
1055 struct dentry *entry;
1060 for_each_possible_cpu(cpu) {
1061 stat = &per_cpu(ftrace_profile_stats, cpu);
1063 /* allocate enough for function name + cpu number */
1064 name = kmalloc(32, GFP_KERNEL);
1067 * The files created are permanent, if something happens
1068 * we still do not free memory.
1071 "Could not allocate stat file for cpu %d\n",
1075 stat->stat = function_stats;
1076 snprintf(name, 32, "function%d", cpu);
1077 stat->stat.name = name;
1078 ret = register_stat_tracer(&stat->stat);
1081 "Could not register function stat for cpu %d\n",
1088 entry = tracefs_create_file("function_profile_enabled", 0644,
1089 d_tracer, NULL, &ftrace_profile_fops);
1091 pr_warning("Could not create tracefs "
1092 "'function_profile_enabled' entry\n");
1095 #else /* CONFIG_FUNCTION_PROFILER */
1096 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1099 #endif /* CONFIG_FUNCTION_PROFILER */
1101 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1103 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1104 static int ftrace_graph_active;
1106 # define ftrace_graph_active 0
1109 #ifdef CONFIG_DYNAMIC_FTRACE
1111 static struct ftrace_ops *removed_ops;
1114 * Set when doing a global update, like enabling all recs or disabling them.
1115 * It is not set when just updating a single ftrace_ops.
1117 static bool update_all_ops;
1119 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1120 # error Dynamic ftrace depends on MCOUNT_RECORD
1123 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1125 struct ftrace_func_probe {
1126 struct hlist_node node;
1127 struct ftrace_probe_ops *ops;
1128 unsigned long flags;
1131 struct list_head free_list;
1134 struct ftrace_func_entry {
1135 struct hlist_node hlist;
1139 struct ftrace_hash {
1140 unsigned long size_bits;
1141 struct hlist_head *buckets;
1142 unsigned long count;
1143 struct rcu_head rcu;
1147 * We make these constant because no one should touch them,
1148 * but they are used as the default "empty hash", to avoid allocating
1149 * it all the time. These are in a read only section such that if
1150 * anyone does try to modify it, it will cause an exception.
1152 static const struct hlist_head empty_buckets[1];
1153 static const struct ftrace_hash empty_hash = {
1154 .buckets = (struct hlist_head *)empty_buckets,
1156 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1158 static struct ftrace_ops global_ops = {
1159 .func = ftrace_stub,
1160 .local_hash.notrace_hash = EMPTY_HASH,
1161 .local_hash.filter_hash = EMPTY_HASH,
1162 INIT_OPS_HASH(global_ops)
1163 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1164 FTRACE_OPS_FL_INITIALIZED |
1169 * This is used by __kernel_text_address() to return true if the
1170 * address is on a dynamically allocated trampoline that would
1171 * not return true for either core_kernel_text() or
1172 * is_module_text_address().
1174 bool is_ftrace_trampoline(unsigned long addr)
1176 struct ftrace_ops *op;
1180 * Some of the ops may be dynamically allocated,
1181 * they are freed after a synchronize_sched().
1183 preempt_disable_notrace();
1185 do_for_each_ftrace_op(op, ftrace_ops_list) {
1187 * This is to check for dynamically allocated trampolines.
1188 * Trampolines that are in kernel text will have
1189 * core_kernel_text() return true.
1191 if (op->trampoline && op->trampoline_size)
1192 if (addr >= op->trampoline &&
1193 addr < op->trampoline + op->trampoline_size) {
1197 } while_for_each_ftrace_op(op);
1200 preempt_enable_notrace();
1205 struct ftrace_page {
1206 struct ftrace_page *next;
1207 struct dyn_ftrace *records;
1212 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1213 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1215 /* estimate from running different kernels */
1216 #define NR_TO_INIT 10000
1218 static struct ftrace_page *ftrace_pages_start;
1219 static struct ftrace_page *ftrace_pages;
1221 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1223 return !hash || !hash->count;
1226 static struct ftrace_func_entry *
1227 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1230 struct ftrace_func_entry *entry;
1231 struct hlist_head *hhd;
1233 if (ftrace_hash_empty(hash))
1236 if (hash->size_bits > 0)
1237 key = hash_long(ip, hash->size_bits);
1241 hhd = &hash->buckets[key];
1243 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1244 if (entry->ip == ip)
1250 static void __add_hash_entry(struct ftrace_hash *hash,
1251 struct ftrace_func_entry *entry)
1253 struct hlist_head *hhd;
1256 if (hash->size_bits)
1257 key = hash_long(entry->ip, hash->size_bits);
1261 hhd = &hash->buckets[key];
1262 hlist_add_head(&entry->hlist, hhd);
1266 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1268 struct ftrace_func_entry *entry;
1270 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1275 __add_hash_entry(hash, entry);
1281 free_hash_entry(struct ftrace_hash *hash,
1282 struct ftrace_func_entry *entry)
1284 hlist_del(&entry->hlist);
1290 remove_hash_entry(struct ftrace_hash *hash,
1291 struct ftrace_func_entry *entry)
1293 hlist_del(&entry->hlist);
1297 static void ftrace_hash_clear(struct ftrace_hash *hash)
1299 struct hlist_head *hhd;
1300 struct hlist_node *tn;
1301 struct ftrace_func_entry *entry;
1302 int size = 1 << hash->size_bits;
1308 for (i = 0; i < size; i++) {
1309 hhd = &hash->buckets[i];
1310 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1311 free_hash_entry(hash, entry);
1313 FTRACE_WARN_ON(hash->count);
1316 static void free_ftrace_hash(struct ftrace_hash *hash)
1318 if (!hash || hash == EMPTY_HASH)
1320 ftrace_hash_clear(hash);
1321 kfree(hash->buckets);
1325 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1327 struct ftrace_hash *hash;
1329 hash = container_of(rcu, struct ftrace_hash, rcu);
1330 free_ftrace_hash(hash);
1333 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1335 if (!hash || hash == EMPTY_HASH)
1337 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1340 void ftrace_free_filter(struct ftrace_ops *ops)
1342 ftrace_ops_init(ops);
1343 free_ftrace_hash(ops->func_hash->filter_hash);
1344 free_ftrace_hash(ops->func_hash->notrace_hash);
1347 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1349 struct ftrace_hash *hash;
1352 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1356 size = 1 << size_bits;
1357 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1359 if (!hash->buckets) {
1364 hash->size_bits = size_bits;
1369 static struct ftrace_hash *
1370 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1372 struct ftrace_func_entry *entry;
1373 struct ftrace_hash *new_hash;
1378 new_hash = alloc_ftrace_hash(size_bits);
1383 if (ftrace_hash_empty(hash))
1386 size = 1 << hash->size_bits;
1387 for (i = 0; i < size; i++) {
1388 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1389 ret = add_hash_entry(new_hash, entry->ip);
1395 FTRACE_WARN_ON(new_hash->count != hash->count);
1400 free_ftrace_hash(new_hash);
1405 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1407 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1409 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1410 struct ftrace_hash *new_hash);
1413 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1414 struct ftrace_hash **dst, struct ftrace_hash *src)
1416 struct ftrace_func_entry *entry;
1417 struct hlist_node *tn;
1418 struct hlist_head *hhd;
1419 struct ftrace_hash *new_hash;
1420 int size = src->count;
1425 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1426 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1430 * If the new source is empty, just free dst and assign it
1434 new_hash = EMPTY_HASH;
1439 * Make the hash size about 1/2 the # found
1441 for (size /= 2; size; size >>= 1)
1444 /* Don't allocate too much */
1445 if (bits > FTRACE_HASH_MAX_BITS)
1446 bits = FTRACE_HASH_MAX_BITS;
1448 new_hash = alloc_ftrace_hash(bits);
1452 size = 1 << src->size_bits;
1453 for (i = 0; i < size; i++) {
1454 hhd = &src->buckets[i];
1455 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1456 remove_hash_entry(src, entry);
1457 __add_hash_entry(new_hash, entry);
1462 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1464 /* IPMODIFY should be updated only when filter_hash updating */
1465 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1467 free_ftrace_hash(new_hash);
1473 * Remove the current set, update the hash and add
1476 ftrace_hash_rec_disable_modify(ops, enable);
1478 rcu_assign_pointer(*dst, new_hash);
1480 ftrace_hash_rec_enable_modify(ops, enable);
1485 static bool hash_contains_ip(unsigned long ip,
1486 struct ftrace_ops_hash *hash)
1489 * The function record is a match if it exists in the filter
1490 * hash and not in the notrace hash. Note, an emty hash is
1491 * considered a match for the filter hash, but an empty
1492 * notrace hash is considered not in the notrace hash.
1494 return (ftrace_hash_empty(hash->filter_hash) ||
1495 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1496 (ftrace_hash_empty(hash->notrace_hash) ||
1497 !ftrace_lookup_ip(hash->notrace_hash, ip));
1501 * Test the hashes for this ops to see if we want to call
1502 * the ops->func or not.
1504 * It's a match if the ip is in the ops->filter_hash or
1505 * the filter_hash does not exist or is empty,
1507 * the ip is not in the ops->notrace_hash.
1509 * This needs to be called with preemption disabled as
1510 * the hashes are freed with call_rcu_sched().
1513 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1515 struct ftrace_ops_hash hash;
1518 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1520 * There's a small race when adding ops that the ftrace handler
1521 * that wants regs, may be called without them. We can not
1522 * allow that handler to be called if regs is NULL.
1524 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1528 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1529 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
1531 if (hash_contains_ip(ip, &hash))
1540 * This is a double for. Do not use 'break' to break out of the loop,
1541 * you must use a goto.
1543 #define do_for_each_ftrace_rec(pg, rec) \
1544 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1546 for (_____i = 0; _____i < pg->index; _____i++) { \
1547 rec = &pg->records[_____i];
1549 #define while_for_each_ftrace_rec() \
1554 static int ftrace_cmp_recs(const void *a, const void *b)
1556 const struct dyn_ftrace *key = a;
1557 const struct dyn_ftrace *rec = b;
1559 if (key->flags < rec->ip)
1561 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1566 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1568 struct ftrace_page *pg;
1569 struct dyn_ftrace *rec;
1570 struct dyn_ftrace key;
1573 key.flags = end; /* overload flags, as it is unsigned long */
1575 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1576 if (end < pg->records[0].ip ||
1577 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1579 rec = bsearch(&key, pg->records, pg->index,
1580 sizeof(struct dyn_ftrace),
1590 * ftrace_location - return true if the ip giving is a traced location
1591 * @ip: the instruction pointer to check
1593 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1594 * That is, the instruction that is either a NOP or call to
1595 * the function tracer. It checks the ftrace internal tables to
1596 * determine if the address belongs or not.
1598 unsigned long ftrace_location(unsigned long ip)
1600 return ftrace_location_range(ip, ip);
1604 * ftrace_text_reserved - return true if range contains an ftrace location
1605 * @start: start of range to search
1606 * @end: end of range to search (inclusive). @end points to the last byte to check.
1608 * Returns 1 if @start and @end contains a ftrace location.
1609 * That is, the instruction that is either a NOP or call to
1610 * the function tracer. It checks the ftrace internal tables to
1611 * determine if the address belongs or not.
1613 int ftrace_text_reserved(const void *start, const void *end)
1617 ret = ftrace_location_range((unsigned long)start,
1618 (unsigned long)end);
1623 /* Test if ops registered to this rec needs regs */
1624 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1626 struct ftrace_ops *ops;
1627 bool keep_regs = false;
1629 for (ops = ftrace_ops_list;
1630 ops != &ftrace_list_end; ops = ops->next) {
1631 /* pass rec in as regs to have non-NULL val */
1632 if (ftrace_ops_test(ops, rec->ip, rec)) {
1633 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1643 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1647 struct ftrace_hash *hash;
1648 struct ftrace_hash *other_hash;
1649 struct ftrace_page *pg;
1650 struct dyn_ftrace *rec;
1654 /* Only update if the ops has been registered */
1655 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1659 * In the filter_hash case:
1660 * If the count is zero, we update all records.
1661 * Otherwise we just update the items in the hash.
1663 * In the notrace_hash case:
1664 * We enable the update in the hash.
1665 * As disabling notrace means enabling the tracing,
1666 * and enabling notrace means disabling, the inc variable
1670 hash = ops->func_hash->filter_hash;
1671 other_hash = ops->func_hash->notrace_hash;
1672 if (ftrace_hash_empty(hash))
1676 hash = ops->func_hash->notrace_hash;
1677 other_hash = ops->func_hash->filter_hash;
1679 * If the notrace hash has no items,
1680 * then there's nothing to do.
1682 if (ftrace_hash_empty(hash))
1686 do_for_each_ftrace_rec(pg, rec) {
1687 int in_other_hash = 0;
1693 * Only the filter_hash affects all records.
1694 * Update if the record is not in the notrace hash.
1696 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1699 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1700 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1703 * If filter_hash is set, we want to match all functions
1704 * that are in the hash but not in the other hash.
1706 * If filter_hash is not set, then we are decrementing.
1707 * That means we match anything that is in the hash
1708 * and also in the other_hash. That is, we need to turn
1709 * off functions in the other hash because they are disabled
1712 if (filter_hash && in_hash && !in_other_hash)
1714 else if (!filter_hash && in_hash &&
1715 (in_other_hash || ftrace_hash_empty(other_hash)))
1723 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1727 * If there's only a single callback registered to a
1728 * function, and the ops has a trampoline registered
1729 * for it, then we can call it directly.
1731 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1732 rec->flags |= FTRACE_FL_TRAMP;
1735 * If we are adding another function callback
1736 * to this function, and the previous had a
1737 * custom trampoline in use, then we need to go
1738 * back to the default trampoline.
1740 rec->flags &= ~FTRACE_FL_TRAMP;
1743 * If any ops wants regs saved for this function
1744 * then all ops will get saved regs.
1746 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1747 rec->flags |= FTRACE_FL_REGS;
1749 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1754 * If the rec had REGS enabled and the ops that is
1755 * being removed had REGS set, then see if there is
1756 * still any ops for this record that wants regs.
1757 * If not, we can stop recording them.
1759 if (ftrace_rec_count(rec) > 0 &&
1760 rec->flags & FTRACE_FL_REGS &&
1761 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1762 if (!test_rec_ops_needs_regs(rec))
1763 rec->flags &= ~FTRACE_FL_REGS;
1767 * If the rec had TRAMP enabled, then it needs to
1768 * be cleared. As TRAMP can only be enabled iff
1769 * there is only a single ops attached to it.
1770 * In otherwords, always disable it on decrementing.
1771 * In the future, we may set it if rec count is
1772 * decremented to one, and the ops that is left
1775 rec->flags &= ~FTRACE_FL_TRAMP;
1778 * flags will be cleared in ftrace_check_record()
1779 * if rec count is zero.
1783 /* Shortcut, if we handled all records, we are done. */
1784 if (!all && count == hash->count)
1786 } while_for_each_ftrace_rec();
1789 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1792 __ftrace_hash_rec_update(ops, filter_hash, 0);
1795 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1798 __ftrace_hash_rec_update(ops, filter_hash, 1);
1801 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1802 int filter_hash, int inc)
1804 struct ftrace_ops *op;
1806 __ftrace_hash_rec_update(ops, filter_hash, inc);
1808 if (ops->func_hash != &global_ops.local_hash)
1812 * If the ops shares the global_ops hash, then we need to update
1813 * all ops that are enabled and use this hash.
1815 do_for_each_ftrace_op(op, ftrace_ops_list) {
1819 if (op->func_hash == &global_ops.local_hash)
1820 __ftrace_hash_rec_update(op, filter_hash, inc);
1821 } while_for_each_ftrace_op(op);
1824 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1827 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1830 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1833 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1837 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1838 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1839 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1840 * Note that old_hash and new_hash has below meanings
1841 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1842 * - If the hash is EMPTY_HASH, it hits nothing
1843 * - Anything else hits the recs which match the hash entries.
1845 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1846 struct ftrace_hash *old_hash,
1847 struct ftrace_hash *new_hash)
1849 struct ftrace_page *pg;
1850 struct dyn_ftrace *rec, *end = NULL;
1853 /* Only update if the ops has been registered */
1854 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1857 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1861 * Since the IPMODIFY is a very address sensitive action, we do not
1862 * allow ftrace_ops to set all functions to new hash.
1864 if (!new_hash || !old_hash)
1867 /* Update rec->flags */
1868 do_for_each_ftrace_rec(pg, rec) {
1869 /* We need to update only differences of filter_hash */
1870 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1871 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1872 if (in_old == in_new)
1876 /* New entries must ensure no others are using it */
1877 if (rec->flags & FTRACE_FL_IPMODIFY)
1879 rec->flags |= FTRACE_FL_IPMODIFY;
1880 } else /* Removed entry */
1881 rec->flags &= ~FTRACE_FL_IPMODIFY;
1882 } while_for_each_ftrace_rec();
1889 /* Roll back what we did above */
1890 do_for_each_ftrace_rec(pg, rec) {
1894 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1895 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1896 if (in_old == in_new)
1900 rec->flags &= ~FTRACE_FL_IPMODIFY;
1902 rec->flags |= FTRACE_FL_IPMODIFY;
1903 } while_for_each_ftrace_rec();
1909 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1911 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1913 if (ftrace_hash_empty(hash))
1916 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1919 /* Disabling always succeeds */
1920 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1922 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1924 if (ftrace_hash_empty(hash))
1927 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1930 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1931 struct ftrace_hash *new_hash)
1933 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1935 if (ftrace_hash_empty(old_hash))
1938 if (ftrace_hash_empty(new_hash))
1941 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1944 static void print_ip_ins(const char *fmt, unsigned char *p)
1946 char ins[MCOUNT_INSN_SIZE];
1949 if (probe_kernel_read(ins, p, MCOUNT_INSN_SIZE)) {
1950 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
1954 printk(KERN_CONT "%s", fmt);
1956 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1957 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
1960 static struct ftrace_ops *
1961 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1964 * ftrace_bug - report and shutdown function tracer
1965 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1966 * @rec: The record that failed
1968 * The arch code that enables or disables the function tracing
1969 * can call ftrace_bug() when it has detected a problem in
1970 * modifying the code. @failed should be one of either:
1971 * EFAULT - if the problem happens on reading the @ip address
1972 * EINVAL - if what is read at @ip is not what was expected
1973 * EPERM - if the problem happens on writting to the @ip address
1975 void ftrace_bug(int failed, struct dyn_ftrace *rec)
1977 unsigned long ip = rec ? rec->ip : 0;
1981 FTRACE_WARN_ON_ONCE(1);
1982 pr_info("ftrace faulted on modifying ");
1986 FTRACE_WARN_ON_ONCE(1);
1987 pr_info("ftrace failed to modify ");
1989 print_ip_ins(" actual: ", (unsigned char *)ip);
1993 FTRACE_WARN_ON_ONCE(1);
1994 pr_info("ftrace faulted on writing ");
1998 FTRACE_WARN_ON_ONCE(1);
1999 pr_info("ftrace faulted on unknown error ");
2003 struct ftrace_ops *ops = NULL;
2005 pr_info("ftrace record flags: %lx\n", rec->flags);
2006 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2007 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2008 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2009 ops = ftrace_find_tramp_ops_any(rec);
2011 pr_cont("\ttramp: %pS",
2012 (void *)ops->trampoline);
2014 pr_cont("\ttramp: ERROR!");
2017 ip = ftrace_get_addr_curr(rec);
2018 pr_cont(" expected tramp: %lx\n", ip);
2022 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2024 unsigned long flag = 0UL;
2027 * If we are updating calls:
2029 * If the record has a ref count, then we need to enable it
2030 * because someone is using it.
2032 * Otherwise we make sure its disabled.
2034 * If we are disabling calls, then disable all records that
2037 if (enable && ftrace_rec_count(rec))
2038 flag = FTRACE_FL_ENABLED;
2041 * If enabling and the REGS flag does not match the REGS_EN, or
2042 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2043 * this record. Set flags to fail the compare against ENABLED.
2046 if (!(rec->flags & FTRACE_FL_REGS) !=
2047 !(rec->flags & FTRACE_FL_REGS_EN))
2048 flag |= FTRACE_FL_REGS;
2050 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2051 !(rec->flags & FTRACE_FL_TRAMP_EN))
2052 flag |= FTRACE_FL_TRAMP;
2055 /* If the state of this record hasn't changed, then do nothing */
2056 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2057 return FTRACE_UPDATE_IGNORE;
2060 /* Save off if rec is being enabled (for return value) */
2061 flag ^= rec->flags & FTRACE_FL_ENABLED;
2064 rec->flags |= FTRACE_FL_ENABLED;
2065 if (flag & FTRACE_FL_REGS) {
2066 if (rec->flags & FTRACE_FL_REGS)
2067 rec->flags |= FTRACE_FL_REGS_EN;
2069 rec->flags &= ~FTRACE_FL_REGS_EN;
2071 if (flag & FTRACE_FL_TRAMP) {
2072 if (rec->flags & FTRACE_FL_TRAMP)
2073 rec->flags |= FTRACE_FL_TRAMP_EN;
2075 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2080 * If this record is being updated from a nop, then
2081 * return UPDATE_MAKE_CALL.
2083 * return UPDATE_MODIFY_CALL to tell the caller to convert
2084 * from the save regs, to a non-save regs function or
2085 * vice versa, or from a trampoline call.
2087 if (flag & FTRACE_FL_ENABLED)
2088 return FTRACE_UPDATE_MAKE_CALL;
2090 return FTRACE_UPDATE_MODIFY_CALL;
2094 /* If there's no more users, clear all flags */
2095 if (!ftrace_rec_count(rec))
2099 * Just disable the record, but keep the ops TRAMP
2100 * and REGS states. The _EN flags must be disabled though.
2102 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2106 return FTRACE_UPDATE_MAKE_NOP;
2110 * ftrace_update_record, set a record that now is tracing or not
2111 * @rec: the record to update
2112 * @enable: set to 1 if the record is tracing, zero to force disable
2114 * The records that represent all functions that can be traced need
2115 * to be updated when tracing has been enabled.
2117 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2119 return ftrace_check_record(rec, enable, 1);
2123 * ftrace_test_record, check if the record has been enabled or not
2124 * @rec: the record to test
2125 * @enable: set to 1 to check if enabled, 0 if it is disabled
2127 * The arch code may need to test if a record is already set to
2128 * tracing to determine how to modify the function code that it
2131 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2133 return ftrace_check_record(rec, enable, 0);
2136 static struct ftrace_ops *
2137 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2139 struct ftrace_ops *op;
2140 unsigned long ip = rec->ip;
2142 do_for_each_ftrace_op(op, ftrace_ops_list) {
2144 if (!op->trampoline)
2147 if (hash_contains_ip(ip, op->func_hash))
2149 } while_for_each_ftrace_op(op);
2154 static struct ftrace_ops *
2155 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2157 struct ftrace_ops *op;
2158 unsigned long ip = rec->ip;
2161 * Need to check removed ops first.
2162 * If they are being removed, and this rec has a tramp,
2163 * and this rec is in the ops list, then it would be the
2164 * one with the tramp.
2167 if (hash_contains_ip(ip, &removed_ops->old_hash))
2172 * Need to find the current trampoline for a rec.
2173 * Now, a trampoline is only attached to a rec if there
2174 * was a single 'ops' attached to it. But this can be called
2175 * when we are adding another op to the rec or removing the
2176 * current one. Thus, if the op is being added, we can
2177 * ignore it because it hasn't attached itself to the rec
2180 * If an ops is being modified (hooking to different functions)
2181 * then we don't care about the new functions that are being
2182 * added, just the old ones (that are probably being removed).
2184 * If we are adding an ops to a function that already is using
2185 * a trampoline, it needs to be removed (trampolines are only
2186 * for single ops connected), then an ops that is not being
2187 * modified also needs to be checked.
2189 do_for_each_ftrace_op(op, ftrace_ops_list) {
2191 if (!op->trampoline)
2195 * If the ops is being added, it hasn't gotten to
2196 * the point to be removed from this tree yet.
2198 if (op->flags & FTRACE_OPS_FL_ADDING)
2203 * If the ops is being modified and is in the old
2204 * hash, then it is probably being removed from this
2207 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2208 hash_contains_ip(ip, &op->old_hash))
2211 * If the ops is not being added or modified, and it's
2212 * in its normal filter hash, then this must be the one
2215 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2216 hash_contains_ip(ip, op->func_hash))
2219 } while_for_each_ftrace_op(op);
2224 static struct ftrace_ops *
2225 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2227 struct ftrace_ops *op;
2228 unsigned long ip = rec->ip;
2230 do_for_each_ftrace_op(op, ftrace_ops_list) {
2231 /* pass rec in as regs to have non-NULL val */
2232 if (hash_contains_ip(ip, op->func_hash))
2234 } while_for_each_ftrace_op(op);
2240 * ftrace_get_addr_new - Get the call address to set to
2241 * @rec: The ftrace record descriptor
2243 * If the record has the FTRACE_FL_REGS set, that means that it
2244 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2245 * is not not set, then it wants to convert to the normal callback.
2247 * Returns the address of the trampoline to set to
2249 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2251 struct ftrace_ops *ops;
2253 /* Trampolines take precedence over regs */
2254 if (rec->flags & FTRACE_FL_TRAMP) {
2255 ops = ftrace_find_tramp_ops_new(rec);
2256 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2257 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2258 (void *)rec->ip, (void *)rec->ip, rec->flags);
2259 /* Ftrace is shutting down, return anything */
2260 return (unsigned long)FTRACE_ADDR;
2262 return ops->trampoline;
2265 if (rec->flags & FTRACE_FL_REGS)
2266 return (unsigned long)FTRACE_REGS_ADDR;
2268 return (unsigned long)FTRACE_ADDR;
2272 * ftrace_get_addr_curr - Get the call address that is already there
2273 * @rec: The ftrace record descriptor
2275 * The FTRACE_FL_REGS_EN is set when the record already points to
2276 * a function that saves all the regs. Basically the '_EN' version
2277 * represents the current state of the function.
2279 * Returns the address of the trampoline that is currently being called
2281 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2283 struct ftrace_ops *ops;
2285 /* Trampolines take precedence over regs */
2286 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2287 ops = ftrace_find_tramp_ops_curr(rec);
2288 if (FTRACE_WARN_ON(!ops)) {
2289 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2290 (void *)rec->ip, (void *)rec->ip);
2291 /* Ftrace is shutting down, return anything */
2292 return (unsigned long)FTRACE_ADDR;
2294 return ops->trampoline;
2297 if (rec->flags & FTRACE_FL_REGS_EN)
2298 return (unsigned long)FTRACE_REGS_ADDR;
2300 return (unsigned long)FTRACE_ADDR;
2304 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2306 unsigned long ftrace_old_addr;
2307 unsigned long ftrace_addr;
2310 ftrace_addr = ftrace_get_addr_new(rec);
2312 /* This needs to be done before we call ftrace_update_record */
2313 ftrace_old_addr = ftrace_get_addr_curr(rec);
2315 ret = ftrace_update_record(rec, enable);
2318 case FTRACE_UPDATE_IGNORE:
2321 case FTRACE_UPDATE_MAKE_CALL:
2322 return ftrace_make_call(rec, ftrace_addr);
2324 case FTRACE_UPDATE_MAKE_NOP:
2325 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2327 case FTRACE_UPDATE_MODIFY_CALL:
2328 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2331 return -1; /* unknow ftrace bug */
2334 void __weak ftrace_replace_code(int enable)
2336 struct dyn_ftrace *rec;
2337 struct ftrace_page *pg;
2340 if (unlikely(ftrace_disabled))
2343 do_for_each_ftrace_rec(pg, rec) {
2344 failed = __ftrace_replace_code(rec, enable);
2346 ftrace_bug(failed, rec);
2347 /* Stop processing */
2350 } while_for_each_ftrace_rec();
2353 struct ftrace_rec_iter {
2354 struct ftrace_page *pg;
2359 * ftrace_rec_iter_start, start up iterating over traced functions
2361 * Returns an iterator handle that is used to iterate over all
2362 * the records that represent address locations where functions
2365 * May return NULL if no records are available.
2367 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2370 * We only use a single iterator.
2371 * Protected by the ftrace_lock mutex.
2373 static struct ftrace_rec_iter ftrace_rec_iter;
2374 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2376 iter->pg = ftrace_pages_start;
2379 /* Could have empty pages */
2380 while (iter->pg && !iter->pg->index)
2381 iter->pg = iter->pg->next;
2390 * ftrace_rec_iter_next, get the next record to process.
2391 * @iter: The handle to the iterator.
2393 * Returns the next iterator after the given iterator @iter.
2395 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2399 if (iter->index >= iter->pg->index) {
2400 iter->pg = iter->pg->next;
2403 /* Could have empty pages */
2404 while (iter->pg && !iter->pg->index)
2405 iter->pg = iter->pg->next;
2415 * ftrace_rec_iter_record, get the record at the iterator location
2416 * @iter: The current iterator location
2418 * Returns the record that the current @iter is at.
2420 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2422 return &iter->pg->records[iter->index];
2426 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2430 if (unlikely(ftrace_disabled))
2433 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2435 ftrace_bug(ret, rec);
2442 * archs can override this function if they must do something
2443 * before the modifying code is performed.
2445 int __weak ftrace_arch_code_modify_prepare(void)
2451 * archs can override this function if they must do something
2452 * after the modifying code is performed.
2454 int __weak ftrace_arch_code_modify_post_process(void)
2459 void ftrace_modify_all_code(int command)
2461 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2465 * If the ftrace_caller calls a ftrace_ops func directly,
2466 * we need to make sure that it only traces functions it
2467 * expects to trace. When doing the switch of functions,
2468 * we need to update to the ftrace_ops_list_func first
2469 * before the transition between old and new calls are set,
2470 * as the ftrace_ops_list_func will check the ops hashes
2471 * to make sure the ops are having the right functions
2475 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2476 if (FTRACE_WARN_ON(err))
2480 if (command & FTRACE_UPDATE_CALLS)
2481 ftrace_replace_code(1);
2482 else if (command & FTRACE_DISABLE_CALLS)
2483 ftrace_replace_code(0);
2485 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2486 function_trace_op = set_function_trace_op;
2488 /* If irqs are disabled, we are in stop machine */
2489 if (!irqs_disabled())
2490 smp_call_function(ftrace_sync_ipi, NULL, 1);
2491 err = ftrace_update_ftrace_func(ftrace_trace_function);
2492 if (FTRACE_WARN_ON(err))
2496 if (command & FTRACE_START_FUNC_RET)
2497 err = ftrace_enable_ftrace_graph_caller();
2498 else if (command & FTRACE_STOP_FUNC_RET)
2499 err = ftrace_disable_ftrace_graph_caller();
2500 FTRACE_WARN_ON(err);
2503 static int __ftrace_modify_code(void *data)
2505 int *command = data;
2507 ftrace_modify_all_code(*command);
2513 * ftrace_run_stop_machine, go back to the stop machine method
2514 * @command: The command to tell ftrace what to do
2516 * If an arch needs to fall back to the stop machine method, the
2517 * it can call this function.
2519 void ftrace_run_stop_machine(int command)
2521 stop_machine(__ftrace_modify_code, &command, NULL);
2525 * arch_ftrace_update_code, modify the code to trace or not trace
2526 * @command: The command that needs to be done
2528 * Archs can override this function if it does not need to
2529 * run stop_machine() to modify code.
2531 void __weak arch_ftrace_update_code(int command)
2533 ftrace_run_stop_machine(command);
2536 static void ftrace_run_update_code(int command)
2540 ret = ftrace_arch_code_modify_prepare();
2541 FTRACE_WARN_ON(ret);
2546 * By default we use stop_machine() to modify the code.
2547 * But archs can do what ever they want as long as it
2548 * is safe. The stop_machine() is the safest, but also
2549 * produces the most overhead.
2551 arch_ftrace_update_code(command);
2553 ret = ftrace_arch_code_modify_post_process();
2554 FTRACE_WARN_ON(ret);
2557 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2558 struct ftrace_ops_hash *old_hash)
2560 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2561 ops->old_hash.filter_hash = old_hash->filter_hash;
2562 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2563 ftrace_run_update_code(command);
2564 ops->old_hash.filter_hash = NULL;
2565 ops->old_hash.notrace_hash = NULL;
2566 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2569 static ftrace_func_t saved_ftrace_func;
2570 static int ftrace_start_up;
2572 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2576 static void control_ops_free(struct ftrace_ops *ops)
2578 free_percpu(ops->disabled);
2581 static void ftrace_startup_enable(int command)
2583 if (saved_ftrace_func != ftrace_trace_function) {
2584 saved_ftrace_func = ftrace_trace_function;
2585 command |= FTRACE_UPDATE_TRACE_FUNC;
2588 if (!command || !ftrace_enabled)
2591 ftrace_run_update_code(command);
2594 static void ftrace_startup_all(int command)
2596 update_all_ops = true;
2597 ftrace_startup_enable(command);
2598 update_all_ops = false;
2601 static int ftrace_startup(struct ftrace_ops *ops, int command)
2605 if (unlikely(ftrace_disabled))
2608 ret = __register_ftrace_function(ops);
2613 command |= FTRACE_UPDATE_CALLS;
2616 * Note that ftrace probes uses this to start up
2617 * and modify functions it will probe. But we still
2618 * set the ADDING flag for modification, as probes
2619 * do not have trampolines. If they add them in the
2620 * future, then the probes will need to distinguish
2621 * between adding and updating probes.
2623 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2625 ret = ftrace_hash_ipmodify_enable(ops);
2627 /* Rollback registration process */
2628 __unregister_ftrace_function(ops);
2630 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2634 ftrace_hash_rec_enable(ops, 1);
2636 ftrace_startup_enable(command);
2638 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2643 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2647 if (unlikely(ftrace_disabled))
2650 ret = __unregister_ftrace_function(ops);
2656 * Just warn in case of unbalance, no need to kill ftrace, it's not
2657 * critical but the ftrace_call callers may be never nopped again after
2658 * further ftrace uses.
2660 WARN_ON_ONCE(ftrace_start_up < 0);
2662 /* Disabling ipmodify never fails */
2663 ftrace_hash_ipmodify_disable(ops);
2664 ftrace_hash_rec_disable(ops, 1);
2666 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2668 command |= FTRACE_UPDATE_CALLS;
2670 if (saved_ftrace_func != ftrace_trace_function) {
2671 saved_ftrace_func = ftrace_trace_function;
2672 command |= FTRACE_UPDATE_TRACE_FUNC;
2675 if (!command || !ftrace_enabled) {
2677 * If these are dynamic or control ops, they still
2678 * need their data freed. Since, function tracing is
2679 * not currently active, we can just free them
2680 * without synchronizing all CPUs.
2682 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL))
2689 * If the ops uses a trampoline, then it needs to be
2690 * tested first on update.
2692 ops->flags |= FTRACE_OPS_FL_REMOVING;
2695 /* The trampoline logic checks the old hashes */
2696 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2697 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2699 ftrace_run_update_code(command);
2702 * If there's no more ops registered with ftrace, run a
2703 * sanity check to make sure all rec flags are cleared.
2705 if (ftrace_ops_list == &ftrace_list_end) {
2706 struct ftrace_page *pg;
2707 struct dyn_ftrace *rec;
2709 do_for_each_ftrace_rec(pg, rec) {
2710 if (FTRACE_WARN_ON_ONCE(rec->flags))
2711 pr_warn(" %pS flags:%lx\n",
2712 (void *)rec->ip, rec->flags);
2713 } while_for_each_ftrace_rec();
2716 ops->old_hash.filter_hash = NULL;
2717 ops->old_hash.notrace_hash = NULL;
2720 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2723 * Dynamic ops may be freed, we must make sure that all
2724 * callers are done before leaving this function.
2725 * The same goes for freeing the per_cpu data of the control
2728 * Again, normal synchronize_sched() is not good enough.
2729 * We need to do a hard force of sched synchronization.
2730 * This is because we use preempt_disable() to do RCU, but
2731 * the function tracers can be called where RCU is not watching
2732 * (like before user_exit()). We can not rely on the RCU
2733 * infrastructure to do the synchronization, thus we must do it
2736 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2737 schedule_on_each_cpu(ftrace_sync);
2740 arch_ftrace_trampoline_free(ops);
2742 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2743 control_ops_free(ops);
2749 static void ftrace_startup_sysctl(void)
2753 if (unlikely(ftrace_disabled))
2756 /* Force update next time */
2757 saved_ftrace_func = NULL;
2758 /* ftrace_start_up is true if we want ftrace running */
2759 if (ftrace_start_up) {
2760 command = FTRACE_UPDATE_CALLS;
2761 if (ftrace_graph_active)
2762 command |= FTRACE_START_FUNC_RET;
2763 ftrace_startup_enable(command);
2767 static void ftrace_shutdown_sysctl(void)
2771 if (unlikely(ftrace_disabled))
2774 /* ftrace_start_up is true if ftrace is running */
2775 if (ftrace_start_up) {
2776 command = FTRACE_DISABLE_CALLS;
2777 if (ftrace_graph_active)
2778 command |= FTRACE_STOP_FUNC_RET;
2779 ftrace_run_update_code(command);
2783 static cycle_t ftrace_update_time;
2784 unsigned long ftrace_update_tot_cnt;
2786 static inline int ops_traces_mod(struct ftrace_ops *ops)
2789 * Filter_hash being empty will default to trace module.
2790 * But notrace hash requires a test of individual module functions.
2792 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2793 ftrace_hash_empty(ops->func_hash->notrace_hash);
2797 * Check if the current ops references the record.
2799 * If the ops traces all functions, then it was already accounted for.
2800 * If the ops does not trace the current record function, skip it.
2801 * If the ops ignores the function via notrace filter, skip it.
2804 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2806 /* If ops isn't enabled, ignore it */
2807 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2810 /* If ops traces all mods, we already accounted for it */
2811 if (ops_traces_mod(ops))
2814 /* The function must be in the filter */
2815 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2816 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2819 /* If in notrace hash, we ignore it too */
2820 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2826 static int referenced_filters(struct dyn_ftrace *rec)
2828 struct ftrace_ops *ops;
2831 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2832 if (ops_references_rec(ops, rec)) {
2834 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
2835 rec->flags |= FTRACE_FL_REGS;
2842 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2844 struct ftrace_page *pg;
2845 struct dyn_ftrace *p;
2846 cycle_t start, stop;
2847 unsigned long update_cnt = 0;
2848 unsigned long ref = 0;
2853 * When adding a module, we need to check if tracers are
2854 * currently enabled and if they are set to trace all functions.
2855 * If they are, we need to enable the module functions as well
2856 * as update the reference counts for those function records.
2859 struct ftrace_ops *ops;
2861 for (ops = ftrace_ops_list;
2862 ops != &ftrace_list_end; ops = ops->next) {
2863 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2864 if (ops_traces_mod(ops))
2872 start = ftrace_now(raw_smp_processor_id());
2874 for (pg = new_pgs; pg; pg = pg->next) {
2876 for (i = 0; i < pg->index; i++) {
2879 /* If something went wrong, bail without enabling anything */
2880 if (unlikely(ftrace_disabled))
2883 p = &pg->records[i];
2885 cnt += referenced_filters(p);
2889 * Do the initial record conversion from mcount jump
2890 * to the NOP instructions.
2892 if (!ftrace_code_disable(mod, p))
2898 * If the tracing is enabled, go ahead and enable the record.
2900 * The reason not to enable the record immediatelly is the
2901 * inherent check of ftrace_make_nop/ftrace_make_call for
2902 * correct previous instructions. Making first the NOP
2903 * conversion puts the module to the correct state, thus
2904 * passing the ftrace_make_call check.
2906 if (ftrace_start_up && cnt) {
2907 int failed = __ftrace_replace_code(p, 1);
2909 ftrace_bug(failed, p);
2914 stop = ftrace_now(raw_smp_processor_id());
2915 ftrace_update_time = stop - start;
2916 ftrace_update_tot_cnt += update_cnt;
2921 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2926 if (WARN_ON(!count))
2929 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2932 * We want to fill as much as possible. No more than a page
2935 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2939 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2942 /* if we can't allocate this size, try something smaller */
2949 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2958 static struct ftrace_page *
2959 ftrace_allocate_pages(unsigned long num_to_init)
2961 struct ftrace_page *start_pg;
2962 struct ftrace_page *pg;
2969 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2974 * Try to allocate as much as possible in one continues
2975 * location that fills in all of the space. We want to
2976 * waste as little space as possible.
2979 cnt = ftrace_allocate_records(pg, num_to_init);
2987 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2999 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3000 free_pages((unsigned long)pg->records, order);
3001 start_pg = pg->next;
3005 pr_info("ftrace: FAILED to allocate memory for functions\n");
3009 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3011 struct ftrace_iterator {
3014 struct ftrace_page *pg;
3015 struct dyn_ftrace *func;
3016 struct ftrace_func_probe *probe;
3017 struct trace_parser parser;
3018 struct ftrace_hash *hash;
3019 struct ftrace_ops *ops;
3026 t_hash_next(struct seq_file *m, loff_t *pos)
3028 struct ftrace_iterator *iter = m->private;
3029 struct hlist_node *hnd = NULL;
3030 struct hlist_head *hhd;
3036 hnd = &iter->probe->node;
3038 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3041 hhd = &ftrace_func_hash[iter->hidx];
3043 if (hlist_empty(hhd)) {
3059 if (WARN_ON_ONCE(!hnd))
3062 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3067 static void *t_hash_start(struct seq_file *m, loff_t *pos)
3069 struct ftrace_iterator *iter = m->private;
3073 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3076 if (iter->func_pos > *pos)
3080 for (l = 0; l <= (*pos - iter->func_pos); ) {
3081 p = t_hash_next(m, &l);
3088 /* Only set this if we have an item */
3089 iter->flags |= FTRACE_ITER_HASH;
3095 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
3097 struct ftrace_func_probe *rec;
3100 if (WARN_ON_ONCE(!rec))
3103 if (rec->ops->print)
3104 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3106 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
3109 seq_printf(m, ":%p", rec->data);
3116 t_next(struct seq_file *m, void *v, loff_t *pos)
3118 struct ftrace_iterator *iter = m->private;
3119 struct ftrace_ops *ops = iter->ops;
3120 struct dyn_ftrace *rec = NULL;
3122 if (unlikely(ftrace_disabled))
3125 if (iter->flags & FTRACE_ITER_HASH)
3126 return t_hash_next(m, pos);
3129 iter->pos = iter->func_pos = *pos;
3131 if (iter->flags & FTRACE_ITER_PRINTALL)
3132 return t_hash_start(m, pos);
3135 if (iter->idx >= iter->pg->index) {
3136 if (iter->pg->next) {
3137 iter->pg = iter->pg->next;
3142 rec = &iter->pg->records[iter->idx++];
3143 if (((iter->flags & FTRACE_ITER_FILTER) &&
3144 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
3146 ((iter->flags & FTRACE_ITER_NOTRACE) &&
3147 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
3149 ((iter->flags & FTRACE_ITER_ENABLED) &&
3150 !(rec->flags & FTRACE_FL_ENABLED))) {
3158 return t_hash_start(m, pos);
3165 static void reset_iter_read(struct ftrace_iterator *iter)
3169 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
3172 static void *t_start(struct seq_file *m, loff_t *pos)
3174 struct ftrace_iterator *iter = m->private;
3175 struct ftrace_ops *ops = iter->ops;
3179 mutex_lock(&ftrace_lock);
3181 if (unlikely(ftrace_disabled))
3185 * If an lseek was done, then reset and start from beginning.
3187 if (*pos < iter->pos)
3188 reset_iter_read(iter);
3191 * For set_ftrace_filter reading, if we have the filter
3192 * off, we can short cut and just print out that all
3193 * functions are enabled.
3195 if ((iter->flags & FTRACE_ITER_FILTER &&
3196 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
3197 (iter->flags & FTRACE_ITER_NOTRACE &&
3198 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
3200 return t_hash_start(m, pos);
3201 iter->flags |= FTRACE_ITER_PRINTALL;
3202 /* reset in case of seek/pread */
3203 iter->flags &= ~FTRACE_ITER_HASH;
3207 if (iter->flags & FTRACE_ITER_HASH)
3208 return t_hash_start(m, pos);
3211 * Unfortunately, we need to restart at ftrace_pages_start
3212 * every time we let go of the ftrace_mutex. This is because
3213 * those pointers can change without the lock.
3215 iter->pg = ftrace_pages_start;
3217 for (l = 0; l <= *pos; ) {
3218 p = t_next(m, p, &l);
3224 return t_hash_start(m, pos);
3229 static void t_stop(struct seq_file *m, void *p)
3231 mutex_unlock(&ftrace_lock);
3235 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3240 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3241 struct dyn_ftrace *rec)
3245 ptr = arch_ftrace_trampoline_func(ops, rec);
3247 seq_printf(m, " ->%pS", ptr);
3250 static int t_show(struct seq_file *m, void *v)
3252 struct ftrace_iterator *iter = m->private;
3253 struct dyn_ftrace *rec;
3255 if (iter->flags & FTRACE_ITER_HASH)
3256 return t_hash_show(m, iter);
3258 if (iter->flags & FTRACE_ITER_PRINTALL) {
3259 if (iter->flags & FTRACE_ITER_NOTRACE)
3260 seq_puts(m, "#### no functions disabled ####\n");
3262 seq_puts(m, "#### all functions enabled ####\n");
3271 seq_printf(m, "%ps", (void *)rec->ip);
3272 if (iter->flags & FTRACE_ITER_ENABLED) {
3273 struct ftrace_ops *ops = NULL;
3275 seq_printf(m, " (%ld)%s%s",
3276 ftrace_rec_count(rec),
3277 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3278 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3279 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3280 ops = ftrace_find_tramp_ops_any(rec);
3282 seq_printf(m, "\ttramp: %pS",
3283 (void *)ops->trampoline);
3285 seq_puts(m, "\ttramp: ERROR!");
3288 add_trampoline_func(m, ops, rec);
3296 static const struct seq_operations show_ftrace_seq_ops = {
3304 ftrace_avail_open(struct inode *inode, struct file *file)
3306 struct ftrace_iterator *iter;
3308 if (unlikely(ftrace_disabled))
3311 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3313 iter->pg = ftrace_pages_start;
3314 iter->ops = &global_ops;
3317 return iter ? 0 : -ENOMEM;
3321 ftrace_enabled_open(struct inode *inode, struct file *file)
3323 struct ftrace_iterator *iter;
3325 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3327 iter->pg = ftrace_pages_start;
3328 iter->flags = FTRACE_ITER_ENABLED;
3329 iter->ops = &global_ops;
3332 return iter ? 0 : -ENOMEM;
3336 * ftrace_regex_open - initialize function tracer filter files
3337 * @ops: The ftrace_ops that hold the hash filters
3338 * @flag: The type of filter to process
3339 * @inode: The inode, usually passed in to your open routine
3340 * @file: The file, usually passed in to your open routine
3342 * ftrace_regex_open() initializes the filter files for the
3343 * @ops. Depending on @flag it may process the filter hash or
3344 * the notrace hash of @ops. With this called from the open
3345 * routine, you can use ftrace_filter_write() for the write
3346 * routine if @flag has FTRACE_ITER_FILTER set, or
3347 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3348 * tracing_lseek() should be used as the lseek routine, and
3349 * release must call ftrace_regex_release().
3352 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3353 struct inode *inode, struct file *file)
3355 struct ftrace_iterator *iter;
3356 struct ftrace_hash *hash;
3359 ftrace_ops_init(ops);
3361 if (unlikely(ftrace_disabled))
3364 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3368 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3376 mutex_lock(&ops->func_hash->regex_lock);
3378 if (flag & FTRACE_ITER_NOTRACE)
3379 hash = ops->func_hash->notrace_hash;
3381 hash = ops->func_hash->filter_hash;
3383 if (file->f_mode & FMODE_WRITE) {
3384 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3386 if (file->f_flags & O_TRUNC)
3387 iter->hash = alloc_ftrace_hash(size_bits);
3389 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3392 trace_parser_put(&iter->parser);
3399 if (file->f_mode & FMODE_READ) {
3400 iter->pg = ftrace_pages_start;
3402 ret = seq_open(file, &show_ftrace_seq_ops);
3404 struct seq_file *m = file->private_data;
3408 free_ftrace_hash(iter->hash);
3409 trace_parser_put(&iter->parser);
3413 file->private_data = iter;
3416 mutex_unlock(&ops->func_hash->regex_lock);
3422 ftrace_filter_open(struct inode *inode, struct file *file)
3424 struct ftrace_ops *ops = inode->i_private;
3426 return ftrace_regex_open(ops,
3427 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3432 ftrace_notrace_open(struct inode *inode, struct file *file)
3434 struct ftrace_ops *ops = inode->i_private;
3436 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3440 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3441 struct ftrace_glob {
3447 static int ftrace_match(char *str, struct ftrace_glob *g)
3454 if (strcmp(str, g->search) == 0)
3457 case MATCH_FRONT_ONLY:
3458 if (strncmp(str, g->search, g->len) == 0)
3461 case MATCH_MIDDLE_ONLY:
3462 if (strstr(str, g->search))
3465 case MATCH_END_ONLY:
3467 if (slen >= g->len &&
3468 memcmp(str + slen - g->len, g->search, g->len) == 0)
3477 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3479 struct ftrace_func_entry *entry;
3482 entry = ftrace_lookup_ip(hash, rec->ip);
3484 /* Do nothing if it doesn't exist */
3488 free_hash_entry(hash, entry);
3490 /* Do nothing if it exists */
3494 ret = add_hash_entry(hash, rec->ip);
3500 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3501 struct ftrace_glob *mod_g, int exclude_mod)
3503 char str[KSYM_SYMBOL_LEN];
3506 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3509 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3511 /* blank module name to match all modules */
3513 /* blank module globbing: modname xor exclude_mod */
3514 if ((!exclude_mod) != (!modname))
3519 /* not matching the module */
3520 if (!modname || !mod_matches) {
3527 if (mod_matches && exclude_mod)
3531 /* blank search means to match all funcs in the mod */
3536 return ftrace_match(str, func_g);
3540 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3542 struct ftrace_page *pg;
3543 struct dyn_ftrace *rec;
3544 struct ftrace_glob func_g = { .type = MATCH_FULL };
3545 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3546 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3547 int exclude_mod = 0;
3550 int clear_filter = 0;
3553 func_g.type = filter_parse_regex(func, len, &func_g.search,
3555 func_g.len = strlen(func_g.search);
3559 mod_g.type = filter_parse_regex(mod, strlen(mod),
3560 &mod_g.search, &exclude_mod);
3561 mod_g.len = strlen(mod_g.search);
3564 mutex_lock(&ftrace_lock);
3566 if (unlikely(ftrace_disabled))
3569 do_for_each_ftrace_rec(pg, rec) {
3570 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3571 ret = enter_record(hash, rec, clear_filter);
3578 } while_for_each_ftrace_rec();
3580 mutex_unlock(&ftrace_lock);
3586 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3588 return match_records(hash, buff, len, NULL);
3593 * We register the module command as a template to show others how
3594 * to register the a command as well.
3598 ftrace_mod_callback(struct ftrace_hash *hash,
3599 char *func, char *cmd, char *module, int enable)
3604 * cmd == 'mod' because we only registered this func
3605 * for the 'mod' ftrace_func_command.
3606 * But if you register one func with multiple commands,
3607 * you can tell which command was used by the cmd
3610 ret = match_records(hash, func, strlen(func), module);
3618 static struct ftrace_func_command ftrace_mod_cmd = {
3620 .func = ftrace_mod_callback,
3623 static int __init ftrace_mod_cmd_init(void)
3625 return register_ftrace_command(&ftrace_mod_cmd);
3627 core_initcall(ftrace_mod_cmd_init);
3629 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3630 struct ftrace_ops *op, struct pt_regs *pt_regs)
3632 struct ftrace_func_probe *entry;
3633 struct hlist_head *hhd;
3636 key = hash_long(ip, FTRACE_HASH_BITS);
3638 hhd = &ftrace_func_hash[key];
3640 if (hlist_empty(hhd))
3644 * Disable preemption for these calls to prevent a RCU grace
3645 * period. This syncs the hash iteration and freeing of items
3646 * on the hash. rcu_read_lock is too dangerous here.
3648 preempt_disable_notrace();
3649 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3650 if (entry->ip == ip)
3651 entry->ops->func(ip, parent_ip, &entry->data);
3653 preempt_enable_notrace();
3656 static struct ftrace_ops trace_probe_ops __read_mostly =
3658 .func = function_trace_probe_call,
3659 .flags = FTRACE_OPS_FL_INITIALIZED,
3660 INIT_OPS_HASH(trace_probe_ops)
3663 static int ftrace_probe_registered;
3665 static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
3670 if (ftrace_probe_registered) {
3671 /* still need to update the function call sites */
3673 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3678 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3679 struct hlist_head *hhd = &ftrace_func_hash[i];
3683 /* Nothing registered? */
3684 if (i == FTRACE_FUNC_HASHSIZE)
3687 ret = ftrace_startup(&trace_probe_ops, 0);
3689 ftrace_probe_registered = 1;
3692 static bool __disable_ftrace_function_probe(void)
3696 if (!ftrace_probe_registered)
3699 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3700 struct hlist_head *hhd = &ftrace_func_hash[i];
3705 /* no more funcs left */
3706 ftrace_shutdown(&trace_probe_ops, 0);
3708 ftrace_probe_registered = 0;
3713 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3715 if (entry->ops->free)
3716 entry->ops->free(entry->ops, entry->ip, &entry->data);
3721 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3724 struct ftrace_ops_hash old_hash_ops;
3725 struct ftrace_func_probe *entry;
3726 struct ftrace_glob func_g;
3727 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3728 struct ftrace_hash *old_hash = *orig_hash;
3729 struct ftrace_hash *hash;
3730 struct ftrace_page *pg;
3731 struct dyn_ftrace *rec;
3737 func_g.type = filter_parse_regex(glob, strlen(glob),
3738 &func_g.search, ¬);
3739 func_g.len = strlen(func_g.search);
3741 /* we do not support '!' for function probes */
3745 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3747 old_hash_ops.filter_hash = old_hash;
3748 /* Probes only have filters */
3749 old_hash_ops.notrace_hash = NULL;
3751 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3757 if (unlikely(ftrace_disabled)) {
3762 mutex_lock(&ftrace_lock);
3764 do_for_each_ftrace_rec(pg, rec) {
3766 if (!ftrace_match_record(rec, &func_g, NULL, 0))
3769 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3771 /* If we did not process any, then return error */
3782 * The caller might want to do something special
3783 * for each function we find. We call the callback
3784 * to give the caller an opportunity to do so.
3787 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3788 /* caller does not like this func */
3794 ret = enter_record(hash, rec, 0);
3802 entry->ip = rec->ip;
3804 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3805 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3807 } while_for_each_ftrace_rec();
3809 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3811 __enable_ftrace_function_probe(&old_hash_ops);
3814 free_ftrace_hash_rcu(old_hash);
3819 mutex_unlock(&ftrace_lock);
3821 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3822 free_ftrace_hash(hash);
3828 PROBE_TEST_FUNC = 1,
3833 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3834 void *data, int flags)
3836 struct ftrace_ops_hash old_hash_ops;
3837 struct ftrace_func_entry *rec_entry;
3838 struct ftrace_func_probe *entry;
3839 struct ftrace_func_probe *p;
3840 struct ftrace_glob func_g;
3841 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3842 struct ftrace_hash *old_hash = *orig_hash;
3843 struct list_head free_list;
3844 struct ftrace_hash *hash;
3845 struct hlist_node *tmp;
3846 char str[KSYM_SYMBOL_LEN];
3850 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3851 func_g.search = NULL;
3855 func_g.type = filter_parse_regex(glob, strlen(glob),
3856 &func_g.search, ¬);
3857 func_g.len = strlen(func_g.search);
3859 /* we do not support '!' for function probes */
3864 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3866 old_hash_ops.filter_hash = old_hash;
3867 /* Probes only have filters */
3868 old_hash_ops.notrace_hash = NULL;
3870 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3872 /* Hmm, should report this somehow */
3875 INIT_LIST_HEAD(&free_list);
3877 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3878 struct hlist_head *hhd = &ftrace_func_hash[i];
3880 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3882 /* break up if statements for readability */
3883 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3886 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3889 /* do this last, since it is the most expensive */
3890 if (func_g.search) {
3891 kallsyms_lookup(entry->ip, NULL, NULL,
3893 if (!ftrace_match(str, &func_g))
3897 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3898 /* It is possible more than one entry had this ip */
3900 free_hash_entry(hash, rec_entry);
3902 hlist_del_rcu(&entry->node);
3903 list_add(&entry->free_list, &free_list);
3906 mutex_lock(&ftrace_lock);
3907 disabled = __disable_ftrace_function_probe();
3909 * Remove after the disable is called. Otherwise, if the last
3910 * probe is removed, a null hash means *all enabled*.
3912 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3914 /* still need to update the function call sites */
3915 if (ftrace_enabled && !disabled)
3916 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3918 synchronize_sched();
3920 free_ftrace_hash_rcu(old_hash);
3922 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3923 list_del(&entry->free_list);
3924 ftrace_free_entry(entry);
3926 mutex_unlock(&ftrace_lock);
3929 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3930 free_ftrace_hash(hash);
3934 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3937 __unregister_ftrace_function_probe(glob, ops, data,
3938 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3942 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3944 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3947 void unregister_ftrace_function_probe_all(char *glob)
3949 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3952 static LIST_HEAD(ftrace_commands);
3953 static DEFINE_MUTEX(ftrace_cmd_mutex);
3956 * Currently we only register ftrace commands from __init, so mark this
3959 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3961 struct ftrace_func_command *p;
3964 mutex_lock(&ftrace_cmd_mutex);
3965 list_for_each_entry(p, &ftrace_commands, list) {
3966 if (strcmp(cmd->name, p->name) == 0) {
3971 list_add(&cmd->list, &ftrace_commands);
3973 mutex_unlock(&ftrace_cmd_mutex);
3979 * Currently we only unregister ftrace commands from __init, so mark
3982 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3984 struct ftrace_func_command *p, *n;
3987 mutex_lock(&ftrace_cmd_mutex);
3988 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3989 if (strcmp(cmd->name, p->name) == 0) {
3991 list_del_init(&p->list);
3996 mutex_unlock(&ftrace_cmd_mutex);
4001 static int ftrace_process_regex(struct ftrace_hash *hash,
4002 char *buff, int len, int enable)
4004 char *func, *command, *next = buff;
4005 struct ftrace_func_command *p;
4008 func = strsep(&next, ":");
4011 ret = ftrace_match_records(hash, func, len);
4021 command = strsep(&next, ":");
4023 mutex_lock(&ftrace_cmd_mutex);
4024 list_for_each_entry(p, &ftrace_commands, list) {
4025 if (strcmp(p->name, command) == 0) {
4026 ret = p->func(hash, func, command, next, enable);
4031 mutex_unlock(&ftrace_cmd_mutex);
4037 ftrace_regex_write(struct file *file, const char __user *ubuf,
4038 size_t cnt, loff_t *ppos, int enable)
4040 struct ftrace_iterator *iter;
4041 struct trace_parser *parser;
4047 if (file->f_mode & FMODE_READ) {
4048 struct seq_file *m = file->private_data;
4051 iter = file->private_data;
4053 if (unlikely(ftrace_disabled))
4056 /* iter->hash is a local copy, so we don't need regex_lock */
4058 parser = &iter->parser;
4059 read = trace_get_user(parser, ubuf, cnt, ppos);
4061 if (read >= 0 && trace_parser_loaded(parser) &&
4062 !trace_parser_cont(parser)) {
4063 ret = ftrace_process_regex(iter->hash, parser->buffer,
4064 parser->idx, enable);
4065 trace_parser_clear(parser);
4076 ftrace_filter_write(struct file *file, const char __user *ubuf,
4077 size_t cnt, loff_t *ppos)
4079 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4083 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4084 size_t cnt, loff_t *ppos)
4086 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4090 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4092 struct ftrace_func_entry *entry;
4094 if (!ftrace_location(ip))
4098 entry = ftrace_lookup_ip(hash, ip);
4101 free_hash_entry(hash, entry);
4105 return add_hash_entry(hash, ip);
4108 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4109 struct ftrace_ops_hash *old_hash)
4111 struct ftrace_ops *op;
4113 if (!ftrace_enabled)
4116 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4117 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4122 * If this is the shared global_ops filter, then we need to
4123 * check if there is another ops that shares it, is enabled.
4124 * If so, we still need to run the modify code.
4126 if (ops->func_hash != &global_ops.local_hash)
4129 do_for_each_ftrace_op(op, ftrace_ops_list) {
4130 if (op->func_hash == &global_ops.local_hash &&
4131 op->flags & FTRACE_OPS_FL_ENABLED) {
4132 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4133 /* Only need to do this once */
4136 } while_for_each_ftrace_op(op);
4140 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4141 unsigned long ip, int remove, int reset, int enable)
4143 struct ftrace_hash **orig_hash;
4144 struct ftrace_ops_hash old_hash_ops;
4145 struct ftrace_hash *old_hash;
4146 struct ftrace_hash *hash;
4149 if (unlikely(ftrace_disabled))
4152 mutex_lock(&ops->func_hash->regex_lock);
4155 orig_hash = &ops->func_hash->filter_hash;
4157 orig_hash = &ops->func_hash->notrace_hash;
4160 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4162 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4166 goto out_regex_unlock;
4169 if (buf && !ftrace_match_records(hash, buf, len)) {
4171 goto out_regex_unlock;
4174 ret = ftrace_match_addr(hash, ip, remove);
4176 goto out_regex_unlock;
4179 mutex_lock(&ftrace_lock);
4180 old_hash = *orig_hash;
4181 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4182 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4183 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4185 ftrace_ops_update_code(ops, &old_hash_ops);
4186 free_ftrace_hash_rcu(old_hash);
4188 mutex_unlock(&ftrace_lock);
4191 mutex_unlock(&ops->func_hash->regex_lock);
4193 free_ftrace_hash(hash);
4198 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4199 int reset, int enable)
4201 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4205 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4206 * @ops - the ops to set the filter with
4207 * @ip - the address to add to or remove from the filter.
4208 * @remove - non zero to remove the ip from the filter
4209 * @reset - non zero to reset all filters before applying this filter.
4211 * Filters denote which functions should be enabled when tracing is enabled
4212 * If @ip is NULL, it failes to update filter.
4214 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4215 int remove, int reset)
4217 ftrace_ops_init(ops);
4218 return ftrace_set_addr(ops, ip, remove, reset, 1);
4220 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4223 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4224 int reset, int enable)
4226 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4230 * ftrace_set_filter - set a function to filter on in ftrace
4231 * @ops - the ops to set the filter with
4232 * @buf - the string that holds the function filter text.
4233 * @len - the length of the string.
4234 * @reset - non zero to reset all filters before applying this filter.
4236 * Filters denote which functions should be enabled when tracing is enabled.
4237 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4239 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4242 ftrace_ops_init(ops);
4243 return ftrace_set_regex(ops, buf, len, reset, 1);
4245 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4248 * ftrace_set_notrace - set a function to not trace in ftrace
4249 * @ops - the ops to set the notrace filter with
4250 * @buf - the string that holds the function notrace text.
4251 * @len - the length of the string.
4252 * @reset - non zero to reset all filters before applying this filter.
4254 * Notrace Filters denote which functions should not be enabled when tracing
4255 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4258 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4261 ftrace_ops_init(ops);
4262 return ftrace_set_regex(ops, buf, len, reset, 0);
4264 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4266 * ftrace_set_global_filter - set a function to filter on with global tracers
4267 * @buf - the string that holds the function filter text.
4268 * @len - the length of the string.
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 @buf is NULL and reset is set, all functions will be enabled for tracing.
4274 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4276 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4278 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4281 * ftrace_set_global_notrace - set a function to not trace with global tracers
4282 * @buf - the string that holds the function notrace text.
4283 * @len - the length of the string.
4284 * @reset - non zero to reset all filters before applying this filter.
4286 * Notrace Filters denote which functions should not be enabled when tracing
4287 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4290 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4292 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4294 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4297 * command line interface to allow users to set filters on boot up.
4299 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4300 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4301 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4303 /* Used by function selftest to not test if filter is set */
4304 bool ftrace_filter_param __initdata;
4306 static int __init set_ftrace_notrace(char *str)
4308 ftrace_filter_param = true;
4309 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4312 __setup("ftrace_notrace=", set_ftrace_notrace);
4314 static int __init set_ftrace_filter(char *str)
4316 ftrace_filter_param = true;
4317 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4320 __setup("ftrace_filter=", set_ftrace_filter);
4322 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4323 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4324 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4325 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
4327 static int __init set_graph_function(char *str)
4329 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4332 __setup("ftrace_graph_filter=", set_graph_function);
4334 static int __init set_graph_notrace_function(char *str)
4336 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4339 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4341 static void __init set_ftrace_early_graph(char *buf, int enable)
4345 unsigned long *table = ftrace_graph_funcs;
4346 int *count = &ftrace_graph_count;
4349 table = ftrace_graph_notrace_funcs;
4350 count = &ftrace_graph_notrace_count;
4354 func = strsep(&buf, ",");
4355 /* we allow only one expression at a time */
4356 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
4358 printk(KERN_DEBUG "ftrace: function %s not "
4359 "traceable\n", func);
4362 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4365 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4369 ftrace_ops_init(ops);
4372 func = strsep(&buf, ",");
4373 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4377 static void __init set_ftrace_early_filters(void)
4379 if (ftrace_filter_buf[0])
4380 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4381 if (ftrace_notrace_buf[0])
4382 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4383 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4384 if (ftrace_graph_buf[0])
4385 set_ftrace_early_graph(ftrace_graph_buf, 1);
4386 if (ftrace_graph_notrace_buf[0])
4387 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4388 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4391 int ftrace_regex_release(struct inode *inode, struct file *file)
4393 struct seq_file *m = (struct seq_file *)file->private_data;
4394 struct ftrace_ops_hash old_hash_ops;
4395 struct ftrace_iterator *iter;
4396 struct ftrace_hash **orig_hash;
4397 struct ftrace_hash *old_hash;
4398 struct trace_parser *parser;
4402 if (file->f_mode & FMODE_READ) {
4404 seq_release(inode, file);
4406 iter = file->private_data;
4408 parser = &iter->parser;
4409 if (trace_parser_loaded(parser)) {
4410 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
4412 parser->buffer[parser->idx] = 0;
4413 ftrace_process_regex(iter->hash, parser->buffer,
4414 parser->idx, enable);
4417 trace_parser_put(parser);
4419 mutex_lock(&iter->ops->func_hash->regex_lock);
4421 if (file->f_mode & FMODE_WRITE) {
4422 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4425 orig_hash = &iter->ops->func_hash->filter_hash;
4427 orig_hash = &iter->ops->func_hash->notrace_hash;
4429 mutex_lock(&ftrace_lock);
4430 old_hash = *orig_hash;
4431 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4432 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
4433 ret = ftrace_hash_move(iter->ops, filter_hash,
4434 orig_hash, iter->hash);
4436 ftrace_ops_update_code(iter->ops, &old_hash_ops);
4437 free_ftrace_hash_rcu(old_hash);
4439 mutex_unlock(&ftrace_lock);
4442 mutex_unlock(&iter->ops->func_hash->regex_lock);
4443 free_ftrace_hash(iter->hash);
4449 static const struct file_operations ftrace_avail_fops = {
4450 .open = ftrace_avail_open,
4452 .llseek = seq_lseek,
4453 .release = seq_release_private,
4456 static const struct file_operations ftrace_enabled_fops = {
4457 .open = ftrace_enabled_open,
4459 .llseek = seq_lseek,
4460 .release = seq_release_private,
4463 static const struct file_operations ftrace_filter_fops = {
4464 .open = ftrace_filter_open,
4466 .write = ftrace_filter_write,
4467 .llseek = tracing_lseek,
4468 .release = ftrace_regex_release,
4471 static const struct file_operations ftrace_notrace_fops = {
4472 .open = ftrace_notrace_open,
4474 .write = ftrace_notrace_write,
4475 .llseek = tracing_lseek,
4476 .release = ftrace_regex_release,
4479 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4481 static DEFINE_MUTEX(graph_lock);
4483 int ftrace_graph_count;
4484 int ftrace_graph_notrace_count;
4485 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4486 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4488 struct ftrace_graph_data {
4489 unsigned long *table;
4492 const struct seq_operations *seq_ops;
4496 __g_next(struct seq_file *m, loff_t *pos)
4498 struct ftrace_graph_data *fgd = m->private;
4500 if (*pos >= *fgd->count)
4502 return &fgd->table[*pos];
4506 g_next(struct seq_file *m, void *v, loff_t *pos)
4509 return __g_next(m, pos);
4512 static void *g_start(struct seq_file *m, loff_t *pos)
4514 struct ftrace_graph_data *fgd = m->private;
4516 mutex_lock(&graph_lock);
4518 /* Nothing, tell g_show to print all functions are enabled */
4519 if (!*fgd->count && !*pos)
4522 return __g_next(m, pos);
4525 static void g_stop(struct seq_file *m, void *p)
4527 mutex_unlock(&graph_lock);
4530 static int g_show(struct seq_file *m, void *v)
4532 unsigned long *ptr = v;
4537 if (ptr == (unsigned long *)1) {
4538 struct ftrace_graph_data *fgd = m->private;
4540 if (fgd->table == ftrace_graph_funcs)
4541 seq_puts(m, "#### all functions enabled ####\n");
4543 seq_puts(m, "#### no functions disabled ####\n");
4547 seq_printf(m, "%ps\n", (void *)*ptr);
4552 static const struct seq_operations ftrace_graph_seq_ops = {
4560 __ftrace_graph_open(struct inode *inode, struct file *file,
4561 struct ftrace_graph_data *fgd)
4565 mutex_lock(&graph_lock);
4566 if ((file->f_mode & FMODE_WRITE) &&
4567 (file->f_flags & O_TRUNC)) {
4569 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4571 mutex_unlock(&graph_lock);
4573 if (file->f_mode & FMODE_READ) {
4574 ret = seq_open(file, fgd->seq_ops);
4576 struct seq_file *m = file->private_data;
4580 file->private_data = fgd;
4586 ftrace_graph_open(struct inode *inode, struct file *file)
4588 struct ftrace_graph_data *fgd;
4590 if (unlikely(ftrace_disabled))
4593 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4597 fgd->table = ftrace_graph_funcs;
4598 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4599 fgd->count = &ftrace_graph_count;
4600 fgd->seq_ops = &ftrace_graph_seq_ops;
4602 return __ftrace_graph_open(inode, file, fgd);
4606 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4608 struct ftrace_graph_data *fgd;
4610 if (unlikely(ftrace_disabled))
4613 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4617 fgd->table = ftrace_graph_notrace_funcs;
4618 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4619 fgd->count = &ftrace_graph_notrace_count;
4620 fgd->seq_ops = &ftrace_graph_seq_ops;
4622 return __ftrace_graph_open(inode, file, fgd);
4626 ftrace_graph_release(struct inode *inode, struct file *file)
4628 if (file->f_mode & FMODE_READ) {
4629 struct seq_file *m = file->private_data;
4632 seq_release(inode, file);
4634 kfree(file->private_data);
4641 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4643 struct ftrace_glob func_g;
4644 struct dyn_ftrace *rec;
4645 struct ftrace_page *pg;
4652 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4653 &func_g.search, ¬);
4654 if (!not && *idx >= size)
4657 func_g.len = strlen(func_g.search);
4659 mutex_lock(&ftrace_lock);
4661 if (unlikely(ftrace_disabled)) {
4662 mutex_unlock(&ftrace_lock);
4666 do_for_each_ftrace_rec(pg, rec) {
4668 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
4669 /* if it is in the array */
4671 for (i = 0; i < *idx; i++) {
4672 if (array[i] == rec->ip) {
4681 array[(*idx)++] = rec->ip;
4687 array[i] = array[--(*idx)];
4693 } while_for_each_ftrace_rec();
4695 mutex_unlock(&ftrace_lock);
4704 ftrace_graph_write(struct file *file, const char __user *ubuf,
4705 size_t cnt, loff_t *ppos)
4707 struct trace_parser parser;
4708 ssize_t read, ret = 0;
4709 struct ftrace_graph_data *fgd = file->private_data;
4714 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4717 read = trace_get_user(&parser, ubuf, cnt, ppos);
4719 if (read >= 0 && trace_parser_loaded((&parser))) {
4720 parser.buffer[parser.idx] = 0;
4722 mutex_lock(&graph_lock);
4724 /* we allow only one expression at a time */
4725 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4728 mutex_unlock(&graph_lock);
4734 trace_parser_put(&parser);
4739 static const struct file_operations ftrace_graph_fops = {
4740 .open = ftrace_graph_open,
4742 .write = ftrace_graph_write,
4743 .llseek = tracing_lseek,
4744 .release = ftrace_graph_release,
4747 static const struct file_operations ftrace_graph_notrace_fops = {
4748 .open = ftrace_graph_notrace_open,
4750 .write = ftrace_graph_write,
4751 .llseek = tracing_lseek,
4752 .release = ftrace_graph_release,
4754 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4756 void ftrace_create_filter_files(struct ftrace_ops *ops,
4757 struct dentry *parent)
4760 trace_create_file("set_ftrace_filter", 0644, parent,
4761 ops, &ftrace_filter_fops);
4763 trace_create_file("set_ftrace_notrace", 0644, parent,
4764 ops, &ftrace_notrace_fops);
4768 * The name "destroy_filter_files" is really a misnomer. Although
4769 * in the future, it may actualy delete the files, but this is
4770 * really intended to make sure the ops passed in are disabled
4771 * and that when this function returns, the caller is free to
4774 * The "destroy" name is only to match the "create" name that this
4775 * should be paired with.
4777 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4779 mutex_lock(&ftrace_lock);
4780 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4781 ftrace_shutdown(ops, 0);
4782 ops->flags |= FTRACE_OPS_FL_DELETED;
4783 ftrace_free_filter(ops);
4784 mutex_unlock(&ftrace_lock);
4787 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
4790 trace_create_file("available_filter_functions", 0444,
4791 d_tracer, NULL, &ftrace_avail_fops);
4793 trace_create_file("enabled_functions", 0444,
4794 d_tracer, NULL, &ftrace_enabled_fops);
4796 ftrace_create_filter_files(&global_ops, d_tracer);
4798 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4799 trace_create_file("set_graph_function", 0444, d_tracer,
4801 &ftrace_graph_fops);
4802 trace_create_file("set_graph_notrace", 0444, d_tracer,
4804 &ftrace_graph_notrace_fops);
4805 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4810 static int ftrace_cmp_ips(const void *a, const void *b)
4812 const unsigned long *ipa = a;
4813 const unsigned long *ipb = b;
4822 static int ftrace_process_locs(struct module *mod,
4823 unsigned long *start,
4826 struct ftrace_page *start_pg;
4827 struct ftrace_page *pg;
4828 struct dyn_ftrace *rec;
4829 unsigned long count;
4832 unsigned long flags = 0; /* Shut up gcc */
4835 count = end - start;
4840 sort(start, count, sizeof(*start),
4841 ftrace_cmp_ips, NULL);
4843 start_pg = ftrace_allocate_pages(count);
4847 mutex_lock(&ftrace_lock);
4850 * Core and each module needs their own pages, as
4851 * modules will free them when they are removed.
4852 * Force a new page to be allocated for modules.
4855 WARN_ON(ftrace_pages || ftrace_pages_start);
4856 /* First initialization */
4857 ftrace_pages = ftrace_pages_start = start_pg;
4862 if (WARN_ON(ftrace_pages->next)) {
4863 /* Hmm, we have free pages? */
4864 while (ftrace_pages->next)
4865 ftrace_pages = ftrace_pages->next;
4868 ftrace_pages->next = start_pg;
4874 addr = ftrace_call_adjust(*p++);
4876 * Some architecture linkers will pad between
4877 * the different mcount_loc sections of different
4878 * object files to satisfy alignments.
4879 * Skip any NULL pointers.
4884 if (pg->index == pg->size) {
4885 /* We should have allocated enough */
4886 if (WARN_ON(!pg->next))
4891 rec = &pg->records[pg->index++];
4895 /* We should have used all pages */
4898 /* Assign the last page to ftrace_pages */
4902 * We only need to disable interrupts on start up
4903 * because we are modifying code that an interrupt
4904 * may execute, and the modification is not atomic.
4905 * But for modules, nothing runs the code we modify
4906 * until we are finished with it, and there's no
4907 * reason to cause large interrupt latencies while we do it.
4910 local_irq_save(flags);
4911 ftrace_update_code(mod, start_pg);
4913 local_irq_restore(flags);
4916 mutex_unlock(&ftrace_lock);
4921 #ifdef CONFIG_MODULES
4923 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4925 void ftrace_release_mod(struct module *mod)
4927 struct dyn_ftrace *rec;
4928 struct ftrace_page **last_pg;
4929 struct ftrace_page *pg;
4932 mutex_lock(&ftrace_lock);
4934 if (ftrace_disabled)
4938 * Each module has its own ftrace_pages, remove
4939 * them from the list.
4941 last_pg = &ftrace_pages_start;
4942 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4943 rec = &pg->records[0];
4944 if (within_module_core(rec->ip, mod)) {
4946 * As core pages are first, the first
4947 * page should never be a module page.
4949 if (WARN_ON(pg == ftrace_pages_start))
4952 /* Check if we are deleting the last page */
4953 if (pg == ftrace_pages)
4954 ftrace_pages = next_to_ftrace_page(last_pg);
4956 *last_pg = pg->next;
4957 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4958 free_pages((unsigned long)pg->records, order);
4961 last_pg = &pg->next;
4964 mutex_unlock(&ftrace_lock);
4967 static void ftrace_init_module(struct module *mod,
4968 unsigned long *start, unsigned long *end)
4970 if (ftrace_disabled || start == end)
4972 ftrace_process_locs(mod, start, end);
4975 void ftrace_module_init(struct module *mod)
4977 ftrace_init_module(mod, mod->ftrace_callsites,
4978 mod->ftrace_callsites +
4979 mod->num_ftrace_callsites);
4982 static int ftrace_module_notify_exit(struct notifier_block *self,
4983 unsigned long val, void *data)
4985 struct module *mod = data;
4987 if (val == MODULE_STATE_GOING)
4988 ftrace_release_mod(mod);
4993 static int ftrace_module_notify_exit(struct notifier_block *self,
4994 unsigned long val, void *data)
4998 #endif /* CONFIG_MODULES */
5000 struct notifier_block ftrace_module_exit_nb = {
5001 .notifier_call = ftrace_module_notify_exit,
5002 .priority = INT_MIN, /* Run after anything that can remove kprobes */
5005 void __init ftrace_init(void)
5007 extern unsigned long __start_mcount_loc[];
5008 extern unsigned long __stop_mcount_loc[];
5009 unsigned long count, flags;
5012 local_irq_save(flags);
5013 ret = ftrace_dyn_arch_init();
5014 local_irq_restore(flags);
5018 count = __stop_mcount_loc - __start_mcount_loc;
5020 pr_info("ftrace: No functions to be traced?\n");
5024 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5025 count, count / ENTRIES_PER_PAGE + 1);
5027 last_ftrace_enabled = ftrace_enabled = 1;
5029 ret = ftrace_process_locs(NULL,
5033 ret = register_module_notifier(&ftrace_module_exit_nb);
5035 pr_warning("Failed to register trace ftrace module exit notifier\n");
5037 set_ftrace_early_filters();
5041 ftrace_disabled = 1;
5044 /* Do nothing if arch does not support this */
5045 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5049 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5053 * Currently there's no safe way to free a trampoline when the kernel
5054 * is configured with PREEMPT. That is because a task could be preempted
5055 * when it jumped to the trampoline, it may be preempted for a long time
5056 * depending on the system load, and currently there's no way to know
5057 * when it will be off the trampoline. If the trampoline is freed
5058 * too early, when the task runs again, it will be executing on freed
5061 #ifdef CONFIG_PREEMPT
5062 /* Currently, only non dynamic ops can have a trampoline */
5063 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5067 arch_ftrace_update_trampoline(ops);
5072 static struct ftrace_ops global_ops = {
5073 .func = ftrace_stub,
5074 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5075 FTRACE_OPS_FL_INITIALIZED |
5079 static int __init ftrace_nodyn_init(void)
5084 core_initcall(ftrace_nodyn_init);
5086 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
5087 static inline void ftrace_startup_enable(int command) { }
5088 static inline void ftrace_startup_all(int command) { }
5089 /* Keep as macros so we do not need to define the commands */
5090 # define ftrace_startup(ops, command) \
5092 int ___ret = __register_ftrace_function(ops); \
5094 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5097 # define ftrace_shutdown(ops, command) \
5099 int ___ret = __unregister_ftrace_function(ops); \
5101 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5105 # define ftrace_startup_sysctl() do { } while (0)
5106 # define ftrace_shutdown_sysctl() do { } while (0)
5109 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
5114 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5118 #endif /* CONFIG_DYNAMIC_FTRACE */
5120 __init void ftrace_init_global_array_ops(struct trace_array *tr)
5122 tr->ops = &global_ops;
5123 tr->ops->private = tr;
5126 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5128 /* If we filter on pids, update to use the pid function */
5129 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5130 if (WARN_ON(tr->ops->func != ftrace_stub))
5131 printk("ftrace ops had %pS for function\n",
5134 tr->ops->func = func;
5135 tr->ops->private = tr;
5138 void ftrace_reset_array_ops(struct trace_array *tr)
5140 tr->ops->func = ftrace_stub;
5144 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
5145 struct ftrace_ops *op, struct pt_regs *regs)
5147 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
5151 * Some of the ops may be dynamically allocated,
5152 * they must be freed after a synchronize_sched().
5154 preempt_disable_notrace();
5155 trace_recursion_set(TRACE_CONTROL_BIT);
5158 * Control funcs (perf) uses RCU. Only trace if
5159 * RCU is currently active.
5161 if (!rcu_is_watching())
5164 do_for_each_ftrace_op(op, ftrace_control_list) {
5165 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
5166 !ftrace_function_local_disabled(op) &&
5167 ftrace_ops_test(op, ip, regs))
5168 op->func(ip, parent_ip, op, regs);
5169 } while_for_each_ftrace_op(op);
5171 trace_recursion_clear(TRACE_CONTROL_BIT);
5172 preempt_enable_notrace();
5175 static struct ftrace_ops control_ops = {
5176 .func = ftrace_ops_control_func,
5177 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
5178 INIT_OPS_HASH(control_ops)
5181 static nokprobe_inline void
5182 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5183 struct ftrace_ops *ignored, struct pt_regs *regs)
5185 struct ftrace_ops *op;
5188 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5193 * Some of the ops may be dynamically allocated,
5194 * they must be freed after a synchronize_sched().
5196 preempt_disable_notrace();
5197 do_for_each_ftrace_op(op, ftrace_ops_list) {
5198 if (ftrace_ops_test(op, ip, regs)) {
5199 if (FTRACE_WARN_ON(!op->func)) {
5200 pr_warn("op=%p %pS\n", op, op);
5203 op->func(ip, parent_ip, op, regs);
5205 } while_for_each_ftrace_op(op);
5207 preempt_enable_notrace();
5208 trace_clear_recursion(bit);
5212 * Some archs only support passing ip and parent_ip. Even though
5213 * the list function ignores the op parameter, we do not want any
5214 * C side effects, where a function is called without the caller
5215 * sending a third parameter.
5216 * Archs are to support both the regs and ftrace_ops at the same time.
5217 * If they support ftrace_ops, it is assumed they support regs.
5218 * If call backs want to use regs, they must either check for regs
5219 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5220 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
5221 * An architecture can pass partial regs with ftrace_ops and still
5222 * set the ARCH_SUPPORT_FTARCE_OPS.
5224 #if ARCH_SUPPORTS_FTRACE_OPS
5225 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5226 struct ftrace_ops *op, struct pt_regs *regs)
5228 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
5230 NOKPROBE_SYMBOL(ftrace_ops_list_func);
5232 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5234 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
5236 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
5240 * If there's only one function registered but it does not support
5241 * recursion, this function will be called by the mcount trampoline.
5242 * This function will handle recursion protection.
5244 static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
5245 struct ftrace_ops *op, struct pt_regs *regs)
5249 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5253 op->func(ip, parent_ip, op, regs);
5255 trace_clear_recursion(bit);
5257 NOKPROBE_SYMBOL(ftrace_ops_recurs_func);
5260 * ftrace_ops_get_func - get the function a trampoline should call
5261 * @ops: the ops to get the function for
5263 * Normally the mcount trampoline will call the ops->func, but there
5264 * are times that it should not. For example, if the ops does not
5265 * have its own recursion protection, then it should call the
5266 * ftrace_ops_recurs_func() instead.
5268 * Returns the function that the trampoline should call for @ops.
5270 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5273 * If the func handles its own recursion, call it directly.
5274 * Otherwise call the recursion protected function that
5275 * will call the ftrace ops function.
5277 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
5278 return ftrace_ops_recurs_func;
5283 static void clear_ftrace_swapper(void)
5285 struct task_struct *p;
5289 for_each_online_cpu(cpu) {
5291 clear_tsk_trace_trace(p);
5296 static void set_ftrace_swapper(void)
5298 struct task_struct *p;
5302 for_each_online_cpu(cpu) {
5304 set_tsk_trace_trace(p);
5309 static void clear_ftrace_pid(struct pid *pid)
5311 struct task_struct *p;
5314 do_each_pid_task(pid, PIDTYPE_PID, p) {
5315 clear_tsk_trace_trace(p);
5316 } while_each_pid_task(pid, PIDTYPE_PID, p);
5322 static void set_ftrace_pid(struct pid *pid)
5324 struct task_struct *p;
5327 do_each_pid_task(pid, PIDTYPE_PID, p) {
5328 set_tsk_trace_trace(p);
5329 } while_each_pid_task(pid, PIDTYPE_PID, p);
5333 static void clear_ftrace_pid_task(struct pid *pid)
5335 if (pid == ftrace_swapper_pid)
5336 clear_ftrace_swapper();
5338 clear_ftrace_pid(pid);
5341 static void set_ftrace_pid_task(struct pid *pid)
5343 if (pid == ftrace_swapper_pid)
5344 set_ftrace_swapper();
5346 set_ftrace_pid(pid);
5349 static int ftrace_pid_add(int p)
5352 struct ftrace_pid *fpid;
5355 mutex_lock(&ftrace_lock);
5358 pid = ftrace_swapper_pid;
5360 pid = find_get_pid(p);
5367 list_for_each_entry(fpid, &ftrace_pids, list)
5368 if (fpid->pid == pid)
5373 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5377 list_add(&fpid->list, &ftrace_pids);
5380 set_ftrace_pid_task(pid);
5382 ftrace_update_pid_func();
5384 ftrace_startup_all(0);
5386 mutex_unlock(&ftrace_lock);
5390 if (pid != ftrace_swapper_pid)
5394 mutex_unlock(&ftrace_lock);
5398 static void ftrace_pid_reset(void)
5400 struct ftrace_pid *fpid, *safe;
5402 mutex_lock(&ftrace_lock);
5403 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5404 struct pid *pid = fpid->pid;
5406 clear_ftrace_pid_task(pid);
5408 list_del(&fpid->list);
5412 ftrace_update_pid_func();
5413 ftrace_startup_all(0);
5415 mutex_unlock(&ftrace_lock);
5418 static void *fpid_start(struct seq_file *m, loff_t *pos)
5420 mutex_lock(&ftrace_lock);
5422 if (!ftrace_pids_enabled() && (!*pos))
5425 return seq_list_start(&ftrace_pids, *pos);
5428 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5433 return seq_list_next(v, &ftrace_pids, pos);
5436 static void fpid_stop(struct seq_file *m, void *p)
5438 mutex_unlock(&ftrace_lock);
5441 static int fpid_show(struct seq_file *m, void *v)
5443 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5445 if (v == (void *)1) {
5446 seq_puts(m, "no pid\n");
5450 if (fpid->pid == ftrace_swapper_pid)
5451 seq_puts(m, "swapper tasks\n");
5453 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5458 static const struct seq_operations ftrace_pid_sops = {
5459 .start = fpid_start,
5466 ftrace_pid_open(struct inode *inode, struct file *file)
5470 if ((file->f_mode & FMODE_WRITE) &&
5471 (file->f_flags & O_TRUNC))
5474 if (file->f_mode & FMODE_READ)
5475 ret = seq_open(file, &ftrace_pid_sops);
5481 ftrace_pid_write(struct file *filp, const char __user *ubuf,
5482 size_t cnt, loff_t *ppos)
5488 if (cnt >= sizeof(buf))
5491 if (copy_from_user(&buf, ubuf, cnt))
5497 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5498 * to clean the filter quietly.
5500 tmp = strstrip(buf);
5501 if (strlen(tmp) == 0)
5504 ret = kstrtol(tmp, 10, &val);
5508 ret = ftrace_pid_add(val);
5510 return ret ? ret : cnt;
5514 ftrace_pid_release(struct inode *inode, struct file *file)
5516 if (file->f_mode & FMODE_READ)
5517 seq_release(inode, file);
5522 static const struct file_operations ftrace_pid_fops = {
5523 .open = ftrace_pid_open,
5524 .write = ftrace_pid_write,
5526 .llseek = tracing_lseek,
5527 .release = ftrace_pid_release,
5530 static __init int ftrace_init_tracefs(void)
5532 struct dentry *d_tracer;
5534 d_tracer = tracing_init_dentry();
5535 if (IS_ERR(d_tracer))
5538 ftrace_init_dyn_tracefs(d_tracer);
5540 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5541 NULL, &ftrace_pid_fops);
5543 ftrace_profile_tracefs(d_tracer);
5547 fs_initcall(ftrace_init_tracefs);
5550 * ftrace_kill - kill ftrace
5552 * This function should be used by panic code. It stops ftrace
5553 * but in a not so nice way. If you need to simply kill ftrace
5554 * from a non-atomic section, use ftrace_kill.
5556 void ftrace_kill(void)
5558 ftrace_disabled = 1;
5560 clear_ftrace_function();
5564 * Test if ftrace is dead or not.
5566 int ftrace_is_dead(void)
5568 return ftrace_disabled;
5572 * register_ftrace_function - register a function for profiling
5573 * @ops - ops structure that holds the function for profiling.
5575 * Register a function to be called by all functions in the
5578 * Note: @ops->func and all the functions it calls must be labeled
5579 * with "notrace", otherwise it will go into a
5582 int register_ftrace_function(struct ftrace_ops *ops)
5586 ftrace_ops_init(ops);
5588 mutex_lock(&ftrace_lock);
5590 ret = ftrace_startup(ops, 0);
5592 mutex_unlock(&ftrace_lock);
5596 EXPORT_SYMBOL_GPL(register_ftrace_function);
5599 * unregister_ftrace_function - unregister a function for profiling.
5600 * @ops - ops structure that holds the function to unregister
5602 * Unregister a function that was added to be called by ftrace profiling.
5604 int unregister_ftrace_function(struct ftrace_ops *ops)
5608 mutex_lock(&ftrace_lock);
5609 ret = ftrace_shutdown(ops, 0);
5610 mutex_unlock(&ftrace_lock);
5614 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5617 ftrace_enable_sysctl(struct ctl_table *table, int write,
5618 void __user *buffer, size_t *lenp,
5623 mutex_lock(&ftrace_lock);
5625 if (unlikely(ftrace_disabled))
5628 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5630 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5633 last_ftrace_enabled = !!ftrace_enabled;
5635 if (ftrace_enabled) {
5637 /* we are starting ftrace again */
5638 if (ftrace_ops_list != &ftrace_list_end)
5639 update_ftrace_function();
5641 ftrace_startup_sysctl();
5644 /* stopping ftrace calls (just send to ftrace_stub) */
5645 ftrace_trace_function = ftrace_stub;
5647 ftrace_shutdown_sysctl();
5651 mutex_unlock(&ftrace_lock);
5655 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5657 static struct ftrace_ops graph_ops = {
5658 .func = ftrace_stub,
5659 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5660 FTRACE_OPS_FL_INITIALIZED |
5663 #ifdef FTRACE_GRAPH_TRAMP_ADDR
5664 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5665 /* trampoline_size is only needed for dynamically allocated tramps */
5667 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5670 void ftrace_graph_sleep_time_control(bool enable)
5672 fgraph_sleep_time = enable;
5675 void ftrace_graph_graph_time_control(bool enable)
5677 fgraph_graph_time = enable;
5680 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5685 /* The callbacks that hook a function */
5686 trace_func_graph_ret_t ftrace_graph_return =
5687 (trace_func_graph_ret_t)ftrace_stub;
5688 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5689 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5691 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5692 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5696 unsigned long flags;
5697 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5698 struct task_struct *g, *t;
5700 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5701 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5702 * sizeof(struct ftrace_ret_stack),
5704 if (!ret_stack_list[i]) {
5712 read_lock_irqsave(&tasklist_lock, flags);
5713 do_each_thread(g, t) {
5719 if (t->ret_stack == NULL) {
5720 atomic_set(&t->trace_overrun, 0);
5721 t->curr_ret_stack = -1;
5722 /* Make sure the tasks see the -1 first: */
5724 t->ret_stack = ret_stack_list[start++];
5726 } while_each_thread(g, t);
5729 read_unlock_irqrestore(&tasklist_lock, flags);
5731 for (i = start; i < end; i++)
5732 kfree(ret_stack_list[i]);
5737 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
5738 struct task_struct *prev, struct task_struct *next)
5740 unsigned long long timestamp;
5744 * Does the user want to count the time a function was asleep.
5745 * If so, do not update the time stamps.
5747 if (fgraph_sleep_time)
5750 timestamp = trace_clock_local();
5752 prev->ftrace_timestamp = timestamp;
5754 /* only process tasks that we timestamped */
5755 if (!next->ftrace_timestamp)
5759 * Update all the counters in next to make up for the
5760 * time next was sleeping.
5762 timestamp -= next->ftrace_timestamp;
5764 for (index = next->curr_ret_stack; index >= 0; index--)
5765 next->ret_stack[index].calltime += timestamp;
5768 /* Allocate a return stack for each task */
5769 static int start_graph_tracing(void)
5771 struct ftrace_ret_stack **ret_stack_list;
5774 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5775 sizeof(struct ftrace_ret_stack *),
5778 if (!ret_stack_list)
5781 /* The cpu_boot init_task->ret_stack will never be freed */
5782 for_each_online_cpu(cpu) {
5783 if (!idle_task(cpu)->ret_stack)
5784 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5788 ret = alloc_retstack_tasklist(ret_stack_list);
5789 } while (ret == -EAGAIN);
5792 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5794 pr_info("ftrace_graph: Couldn't activate tracepoint"
5795 " probe to kernel_sched_switch\n");
5798 kfree(ret_stack_list);
5803 * Hibernation protection.
5804 * The state of the current task is too much unstable during
5805 * suspend/restore to disk. We want to protect against that.
5808 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5812 case PM_HIBERNATION_PREPARE:
5813 pause_graph_tracing();
5816 case PM_POST_HIBERNATION:
5817 unpause_graph_tracing();
5823 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5825 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5827 return __ftrace_graph_entry(trace);
5831 * The function graph tracer should only trace the functions defined
5832 * by set_ftrace_filter and set_ftrace_notrace. If another function
5833 * tracer ops is registered, the graph tracer requires testing the
5834 * function against the global ops, and not just trace any function
5835 * that any ftrace_ops registered.
5837 static void update_function_graph_func(void)
5839 struct ftrace_ops *op;
5840 bool do_test = false;
5843 * The graph and global ops share the same set of functions
5844 * to test. If any other ops is on the list, then
5845 * the graph tracing needs to test if its the function
5848 do_for_each_ftrace_op(op, ftrace_ops_list) {
5849 if (op != &global_ops && op != &graph_ops &&
5850 op != &ftrace_list_end) {
5852 /* in double loop, break out with goto */
5855 } while_for_each_ftrace_op(op);
5858 ftrace_graph_entry = ftrace_graph_entry_test;
5860 ftrace_graph_entry = __ftrace_graph_entry;
5863 static struct notifier_block ftrace_suspend_notifier = {
5864 .notifier_call = ftrace_suspend_notifier_call,
5867 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5868 trace_func_graph_ent_t entryfunc)
5872 mutex_lock(&ftrace_lock);
5874 /* we currently allow only one tracer registered at a time */
5875 if (ftrace_graph_active) {
5880 register_pm_notifier(&ftrace_suspend_notifier);
5882 ftrace_graph_active++;
5883 ret = start_graph_tracing();
5885 ftrace_graph_active--;
5889 ftrace_graph_return = retfunc;
5892 * Update the indirect function to the entryfunc, and the
5893 * function that gets called to the entry_test first. Then
5894 * call the update fgraph entry function to determine if
5895 * the entryfunc should be called directly or not.
5897 __ftrace_graph_entry = entryfunc;
5898 ftrace_graph_entry = ftrace_graph_entry_test;
5899 update_function_graph_func();
5901 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
5903 mutex_unlock(&ftrace_lock);
5907 void unregister_ftrace_graph(void)
5909 mutex_lock(&ftrace_lock);
5911 if (unlikely(!ftrace_graph_active))
5914 ftrace_graph_active--;
5915 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5916 ftrace_graph_entry = ftrace_graph_entry_stub;
5917 __ftrace_graph_entry = ftrace_graph_entry_stub;
5918 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
5919 unregister_pm_notifier(&ftrace_suspend_notifier);
5920 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5923 mutex_unlock(&ftrace_lock);
5926 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5929 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5931 atomic_set(&t->trace_overrun, 0);
5932 t->ftrace_timestamp = 0;
5933 /* make curr_ret_stack visible before we add the ret_stack */
5935 t->ret_stack = ret_stack;
5939 * Allocate a return stack for the idle task. May be the first
5940 * time through, or it may be done by CPU hotplug online.
5942 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5944 t->curr_ret_stack = -1;
5946 * The idle task has no parent, it either has its own
5947 * stack or no stack at all.
5950 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5952 if (ftrace_graph_active) {
5953 struct ftrace_ret_stack *ret_stack;
5955 ret_stack = per_cpu(idle_ret_stack, cpu);
5957 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5958 * sizeof(struct ftrace_ret_stack),
5962 per_cpu(idle_ret_stack, cpu) = ret_stack;
5964 graph_init_task(t, ret_stack);
5968 /* Allocate a return stack for newly created task */
5969 void ftrace_graph_init_task(struct task_struct *t)
5971 /* Make sure we do not use the parent ret_stack */
5972 t->ret_stack = NULL;
5973 t->curr_ret_stack = -1;
5975 if (ftrace_graph_active) {
5976 struct ftrace_ret_stack *ret_stack;
5978 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5979 * sizeof(struct ftrace_ret_stack),
5983 graph_init_task(t, ret_stack);
5987 void ftrace_graph_exit_task(struct task_struct *t)
5989 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5991 t->ret_stack = NULL;
5992 /* NULL must become visible to IRQs before we free it: */