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/debugfs.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>
36 #include <trace/events/sched.h>
38 #include <asm/setup.h>
40 #include "trace_output.h"
41 #include "trace_stat.h"
43 #define FTRACE_WARN_ON(cond) \
51 #define FTRACE_WARN_ON_ONCE(cond) \
54 if (WARN_ON_ONCE(___r)) \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
71 #define INIT_REGEX_LOCK(opsname)
74 static struct ftrace_ops ftrace_list_end __read_mostly = {
76 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
79 /* ftrace_enabled is a method to turn ftrace on or off */
80 int ftrace_enabled __read_mostly;
81 static int last_ftrace_enabled;
83 /* Quick disabling of function tracer. */
84 int function_trace_stop __read_mostly;
86 /* Current function tracing op */
87 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
88 /* What to set function_trace_op to */
89 static struct ftrace_ops *set_function_trace_op;
91 /* List for set_ftrace_pid's pids. */
92 LIST_HEAD(ftrace_pids);
94 struct list_head list;
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
102 static int ftrace_disabled __read_mostly;
104 static DEFINE_MUTEX(ftrace_lock);
106 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
107 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
108 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
109 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
110 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
111 static struct ftrace_ops global_ops;
112 static struct ftrace_ops control_ops;
114 #if ARCH_SUPPORTS_FTRACE_OPS
115 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
116 struct ftrace_ops *op, struct pt_regs *regs);
118 /* See comment below, where ftrace_ops_list_func is defined */
119 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
120 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
124 * Traverse the ftrace_global_list, invoking all entries. The reason that we
125 * can use rcu_dereference_raw_notrace() is that elements removed from this list
126 * are simply leaked, so there is no need to interact with a grace-period
127 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
128 * concurrent insertions into the ftrace_global_list.
130 * Silly Alpha and silly pointer-speculation compiler optimizations!
132 #define do_for_each_ftrace_op(op, list) \
133 op = rcu_dereference_raw_notrace(list); \
137 * Optimized for just a single item in the list (as that is the normal case).
139 #define while_for_each_ftrace_op(op) \
140 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
141 unlikely((op) != &ftrace_list_end))
143 static inline void ftrace_ops_init(struct ftrace_ops *ops)
145 #ifdef CONFIG_DYNAMIC_FTRACE
146 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
147 mutex_init(&ops->regex_lock);
148 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
154 * ftrace_nr_registered_ops - return number of ops registered
156 * Returns the number of ftrace_ops registered and tracing functions
158 int ftrace_nr_registered_ops(void)
160 struct ftrace_ops *ops;
163 mutex_lock(&ftrace_lock);
165 for (ops = ftrace_ops_list;
166 ops != &ftrace_list_end; ops = ops->next)
169 mutex_unlock(&ftrace_lock);
175 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
176 struct ftrace_ops *op, struct pt_regs *regs)
180 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
184 do_for_each_ftrace_op(op, ftrace_global_list) {
185 op->func(ip, parent_ip, op, regs);
186 } while_for_each_ftrace_op(op);
188 trace_clear_recursion(bit);
191 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
192 struct ftrace_ops *op, struct pt_regs *regs)
194 if (!test_tsk_trace_trace(current))
197 ftrace_pid_function(ip, parent_ip, op, regs);
200 static void set_ftrace_pid_function(ftrace_func_t func)
202 /* do not set ftrace_pid_function to itself! */
203 if (func != ftrace_pid_func)
204 ftrace_pid_function = func;
208 * clear_ftrace_function - reset the ftrace function
210 * This NULLs the ftrace function and in essence stops
211 * tracing. There may be lag
213 void clear_ftrace_function(void)
215 ftrace_trace_function = ftrace_stub;
216 ftrace_pid_function = ftrace_stub;
219 static void control_ops_disable_all(struct ftrace_ops *ops)
223 for_each_possible_cpu(cpu)
224 *per_cpu_ptr(ops->disabled, cpu) = 1;
227 static int control_ops_alloc(struct ftrace_ops *ops)
229 int __percpu *disabled;
231 disabled = alloc_percpu(int);
235 ops->disabled = disabled;
236 control_ops_disable_all(ops);
240 static void control_ops_free(struct ftrace_ops *ops)
242 free_percpu(ops->disabled);
245 static void update_global_ops(void)
250 * If there's only one function registered, then call that
251 * function directly. Otherwise, we need to iterate over the
252 * registered callers.
254 if (ftrace_global_list == &ftrace_list_end ||
255 ftrace_global_list->next == &ftrace_list_end) {
256 func = ftrace_global_list->func;
258 * As we are calling the function directly.
259 * If it does not have recursion protection,
260 * the function_trace_op needs to be updated
263 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
264 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
266 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
268 func = ftrace_global_list_func;
269 /* The list has its own recursion protection. */
270 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
274 /* If we filter on pids, update to use the pid function */
275 if (!list_empty(&ftrace_pids)) {
276 set_ftrace_pid_function(func);
277 func = ftrace_pid_func;
280 global_ops.func = func;
283 static void ftrace_sync(struct work_struct *work)
286 * This function is just a stub to implement a hard force
287 * of synchronize_sched(). This requires synchronizing
288 * tasks even in userspace and idle.
290 * Yes, function tracing is rude.
294 static void ftrace_sync_ipi(void *data)
296 /* Probably not needed, but do it anyway */
300 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
301 static void update_function_graph_func(void);
303 static inline void update_function_graph_func(void) { }
306 static void update_ftrace_function(void)
313 * If we are at the end of the list and this ops is
314 * recursion safe and not dynamic and the arch supports passing ops,
315 * then have the mcount trampoline call the function directly.
317 if (ftrace_ops_list == &ftrace_list_end ||
318 (ftrace_ops_list->next == &ftrace_list_end &&
319 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
320 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
321 !FTRACE_FORCE_LIST_FUNC)) {
322 /* Set the ftrace_ops that the arch callback uses */
323 if (ftrace_ops_list == &global_ops)
324 set_function_trace_op = ftrace_global_list;
326 set_function_trace_op = ftrace_ops_list;
327 func = ftrace_ops_list->func;
329 /* Just use the default ftrace_ops */
330 set_function_trace_op = &ftrace_list_end;
331 func = ftrace_ops_list_func;
334 /* If there's no change, then do nothing more here */
335 if (ftrace_trace_function == func)
338 update_function_graph_func();
341 * If we are using the list function, it doesn't care
342 * about the function_trace_ops.
344 if (func == ftrace_ops_list_func) {
345 ftrace_trace_function = func;
347 * Don't even bother setting function_trace_ops,
348 * it would be racy to do so anyway.
353 #ifndef CONFIG_DYNAMIC_FTRACE
355 * For static tracing, we need to be a bit more careful.
356 * The function change takes affect immediately. Thus,
357 * we need to coorditate the setting of the function_trace_ops
358 * with the setting of the ftrace_trace_function.
360 * Set the function to the list ops, which will call the
361 * function we want, albeit indirectly, but it handles the
362 * ftrace_ops and doesn't depend on function_trace_op.
364 ftrace_trace_function = ftrace_ops_list_func;
366 * Make sure all CPUs see this. Yes this is slow, but static
367 * tracing is slow and nasty to have enabled.
369 schedule_on_each_cpu(ftrace_sync);
370 /* Now all cpus are using the list ops. */
371 function_trace_op = set_function_trace_op;
372 /* Make sure the function_trace_op is visible on all CPUs */
374 /* Nasty way to force a rmb on all cpus */
375 smp_call_function(ftrace_sync_ipi, NULL, 1);
376 /* OK, we are all set to update the ftrace_trace_function now! */
377 #endif /* !CONFIG_DYNAMIC_FTRACE */
379 ftrace_trace_function = func;
382 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
386 * We are entering ops into the list but another
387 * CPU might be walking that list. We need to make sure
388 * the ops->next pointer is valid before another CPU sees
389 * the ops pointer included into the list.
391 rcu_assign_pointer(*list, ops);
394 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
396 struct ftrace_ops **p;
399 * If we are removing the last function, then simply point
400 * to the ftrace_stub.
402 if (*list == ops && ops->next == &ftrace_list_end) {
403 *list = &ftrace_list_end;
407 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
418 static void add_ftrace_list_ops(struct ftrace_ops **list,
419 struct ftrace_ops *main_ops,
420 struct ftrace_ops *ops)
422 int first = *list == &ftrace_list_end;
423 add_ftrace_ops(list, ops);
425 add_ftrace_ops(&ftrace_ops_list, main_ops);
428 static int remove_ftrace_list_ops(struct ftrace_ops **list,
429 struct ftrace_ops *main_ops,
430 struct ftrace_ops *ops)
432 int ret = remove_ftrace_ops(list, ops);
433 if (!ret && *list == &ftrace_list_end)
434 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
438 static int __register_ftrace_function(struct ftrace_ops *ops)
440 if (FTRACE_WARN_ON(ops == &global_ops))
443 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
446 /* We don't support both control and global flags set. */
447 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
450 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
452 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
453 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
454 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
456 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
457 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
460 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
461 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
464 if (!core_kernel_data((unsigned long)ops))
465 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
467 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
468 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
469 ops->flags |= FTRACE_OPS_FL_ENABLED;
470 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
471 if (control_ops_alloc(ops))
473 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
475 add_ftrace_ops(&ftrace_ops_list, ops);
478 update_ftrace_function();
483 static int __unregister_ftrace_function(struct ftrace_ops *ops)
487 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
490 if (FTRACE_WARN_ON(ops == &global_ops))
493 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
494 ret = remove_ftrace_list_ops(&ftrace_global_list,
497 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
498 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
499 ret = remove_ftrace_list_ops(&ftrace_control_list,
502 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
508 update_ftrace_function();
513 static void ftrace_update_pid_func(void)
515 /* Only do something if we are tracing something */
516 if (ftrace_trace_function == ftrace_stub)
519 update_ftrace_function();
522 #ifdef CONFIG_FUNCTION_PROFILER
523 struct ftrace_profile {
524 struct hlist_node node;
526 unsigned long counter;
527 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
528 unsigned long long time;
529 unsigned long long time_squared;
533 struct ftrace_profile_page {
534 struct ftrace_profile_page *next;
536 struct ftrace_profile records[];
539 struct ftrace_profile_stat {
541 struct hlist_head *hash;
542 struct ftrace_profile_page *pages;
543 struct ftrace_profile_page *start;
544 struct tracer_stat stat;
547 #define PROFILE_RECORDS_SIZE \
548 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
550 #define PROFILES_PER_PAGE \
551 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
553 static int ftrace_profile_enabled __read_mostly;
555 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
556 static DEFINE_MUTEX(ftrace_profile_lock);
558 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
560 #define FTRACE_PROFILE_HASH_BITS 10
561 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
564 function_stat_next(void *v, int idx)
566 struct ftrace_profile *rec = v;
567 struct ftrace_profile_page *pg;
569 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
575 if ((void *)rec >= (void *)&pg->records[pg->index]) {
579 rec = &pg->records[0];
587 static void *function_stat_start(struct tracer_stat *trace)
589 struct ftrace_profile_stat *stat =
590 container_of(trace, struct ftrace_profile_stat, stat);
592 if (!stat || !stat->start)
595 return function_stat_next(&stat->start->records[0], 0);
598 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
599 /* function graph compares on total time */
600 static int function_stat_cmp(void *p1, void *p2)
602 struct ftrace_profile *a = p1;
603 struct ftrace_profile *b = p2;
605 if (a->time < b->time)
607 if (a->time > b->time)
613 /* not function graph compares against hits */
614 static int function_stat_cmp(void *p1, void *p2)
616 struct ftrace_profile *a = p1;
617 struct ftrace_profile *b = p2;
619 if (a->counter < b->counter)
621 if (a->counter > b->counter)
628 static int function_stat_headers(struct seq_file *m)
630 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
631 seq_printf(m, " Function "
634 "--- ---- --- ---\n");
636 seq_printf(m, " Function Hit\n"
642 static int function_stat_show(struct seq_file *m, void *v)
644 struct ftrace_profile *rec = v;
645 char str[KSYM_SYMBOL_LEN];
647 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
648 static struct trace_seq s;
649 unsigned long long avg;
650 unsigned long long stddev;
652 mutex_lock(&ftrace_profile_lock);
654 /* we raced with function_profile_reset() */
655 if (unlikely(rec->counter == 0)) {
660 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
661 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
663 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
666 do_div(avg, rec->counter);
668 /* Sample standard deviation (s^2) */
669 if (rec->counter <= 1)
673 * Apply Welford's method:
674 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
676 stddev = rec->counter * rec->time_squared -
677 rec->time * rec->time;
680 * Divide only 1000 for ns^2 -> us^2 conversion.
681 * trace_print_graph_duration will divide 1000 again.
683 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
687 trace_print_graph_duration(rec->time, &s);
688 trace_seq_puts(&s, " ");
689 trace_print_graph_duration(avg, &s);
690 trace_seq_puts(&s, " ");
691 trace_print_graph_duration(stddev, &s);
692 trace_print_seq(m, &s);
696 mutex_unlock(&ftrace_profile_lock);
701 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
703 struct ftrace_profile_page *pg;
705 pg = stat->pages = stat->start;
708 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
713 memset(stat->hash, 0,
714 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
717 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
719 struct ftrace_profile_page *pg;
724 /* If we already allocated, do nothing */
728 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
732 #ifdef CONFIG_DYNAMIC_FTRACE
733 functions = ftrace_update_tot_cnt;
736 * We do not know the number of functions that exist because
737 * dynamic tracing is what counts them. With past experience
738 * we have around 20K functions. That should be more than enough.
739 * It is highly unlikely we will execute every function in
745 pg = stat->start = stat->pages;
747 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
749 for (i = 1; i < pages; i++) {
750 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
761 unsigned long tmp = (unsigned long)pg;
773 static int ftrace_profile_init_cpu(int cpu)
775 struct ftrace_profile_stat *stat;
778 stat = &per_cpu(ftrace_profile_stats, cpu);
781 /* If the profile is already created, simply reset it */
782 ftrace_profile_reset(stat);
787 * We are profiling all functions, but usually only a few thousand
788 * functions are hit. We'll make a hash of 1024 items.
790 size = FTRACE_PROFILE_HASH_SIZE;
792 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
797 /* Preallocate the function profiling pages */
798 if (ftrace_profile_pages_init(stat) < 0) {
807 static int ftrace_profile_init(void)
812 for_each_possible_cpu(cpu) {
813 ret = ftrace_profile_init_cpu(cpu);
821 /* interrupts must be disabled */
822 static struct ftrace_profile *
823 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
825 struct ftrace_profile *rec;
826 struct hlist_head *hhd;
829 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
830 hhd = &stat->hash[key];
832 if (hlist_empty(hhd))
835 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
843 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
844 struct ftrace_profile *rec)
848 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
849 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
853 * The memory is already allocated, this simply finds a new record to use.
855 static struct ftrace_profile *
856 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
858 struct ftrace_profile *rec = NULL;
860 /* prevent recursion (from NMIs) */
861 if (atomic_inc_return(&stat->disabled) != 1)
865 * Try to find the function again since an NMI
866 * could have added it
868 rec = ftrace_find_profiled_func(stat, ip);
872 if (stat->pages->index == PROFILES_PER_PAGE) {
873 if (!stat->pages->next)
875 stat->pages = stat->pages->next;
878 rec = &stat->pages->records[stat->pages->index++];
880 ftrace_add_profile(stat, rec);
883 atomic_dec(&stat->disabled);
889 function_profile_call(unsigned long ip, unsigned long parent_ip,
890 struct ftrace_ops *ops, struct pt_regs *regs)
892 struct ftrace_profile_stat *stat;
893 struct ftrace_profile *rec;
896 if (!ftrace_profile_enabled)
899 local_irq_save(flags);
901 stat = &__get_cpu_var(ftrace_profile_stats);
902 if (!stat->hash || !ftrace_profile_enabled)
905 rec = ftrace_find_profiled_func(stat, ip);
907 rec = ftrace_profile_alloc(stat, ip);
914 local_irq_restore(flags);
917 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
918 static int profile_graph_entry(struct ftrace_graph_ent *trace)
920 function_profile_call(trace->func, 0, NULL, NULL);
924 static void profile_graph_return(struct ftrace_graph_ret *trace)
926 struct ftrace_profile_stat *stat;
927 unsigned long long calltime;
928 struct ftrace_profile *rec;
931 local_irq_save(flags);
932 stat = &__get_cpu_var(ftrace_profile_stats);
933 if (!stat->hash || !ftrace_profile_enabled)
936 /* If the calltime was zero'd ignore it */
937 if (!trace->calltime)
940 calltime = trace->rettime - trace->calltime;
942 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
945 index = trace->depth;
947 /* Append this call time to the parent time to subtract */
949 current->ret_stack[index - 1].subtime += calltime;
951 if (current->ret_stack[index].subtime < calltime)
952 calltime -= current->ret_stack[index].subtime;
957 rec = ftrace_find_profiled_func(stat, trace->func);
959 rec->time += calltime;
960 rec->time_squared += calltime * calltime;
964 local_irq_restore(flags);
967 static int register_ftrace_profiler(void)
969 return register_ftrace_graph(&profile_graph_return,
970 &profile_graph_entry);
973 static void unregister_ftrace_profiler(void)
975 unregister_ftrace_graph();
978 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
979 .func = function_profile_call,
980 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
981 INIT_REGEX_LOCK(ftrace_profile_ops)
984 static int register_ftrace_profiler(void)
986 return register_ftrace_function(&ftrace_profile_ops);
989 static void unregister_ftrace_profiler(void)
991 unregister_ftrace_function(&ftrace_profile_ops);
993 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
996 ftrace_profile_write(struct file *filp, const char __user *ubuf,
997 size_t cnt, loff_t *ppos)
1002 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1008 mutex_lock(&ftrace_profile_lock);
1009 if (ftrace_profile_enabled ^ val) {
1011 ret = ftrace_profile_init();
1017 ret = register_ftrace_profiler();
1022 ftrace_profile_enabled = 1;
1024 ftrace_profile_enabled = 0;
1026 * unregister_ftrace_profiler calls stop_machine
1027 * so this acts like an synchronize_sched.
1029 unregister_ftrace_profiler();
1033 mutex_unlock(&ftrace_profile_lock);
1041 ftrace_profile_read(struct file *filp, char __user *ubuf,
1042 size_t cnt, loff_t *ppos)
1044 char buf[64]; /* big enough to hold a number */
1047 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1048 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1051 static const struct file_operations ftrace_profile_fops = {
1052 .open = tracing_open_generic,
1053 .read = ftrace_profile_read,
1054 .write = ftrace_profile_write,
1055 .llseek = default_llseek,
1058 /* used to initialize the real stat files */
1059 static struct tracer_stat function_stats __initdata = {
1060 .name = "functions",
1061 .stat_start = function_stat_start,
1062 .stat_next = function_stat_next,
1063 .stat_cmp = function_stat_cmp,
1064 .stat_headers = function_stat_headers,
1065 .stat_show = function_stat_show
1068 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1070 struct ftrace_profile_stat *stat;
1071 struct dentry *entry;
1076 for_each_possible_cpu(cpu) {
1077 stat = &per_cpu(ftrace_profile_stats, cpu);
1079 /* allocate enough for function name + cpu number */
1080 name = kmalloc(32, GFP_KERNEL);
1083 * The files created are permanent, if something happens
1084 * we still do not free memory.
1087 "Could not allocate stat file for cpu %d\n",
1091 stat->stat = function_stats;
1092 snprintf(name, 32, "function%d", cpu);
1093 stat->stat.name = name;
1094 ret = register_stat_tracer(&stat->stat);
1097 "Could not register function stat for cpu %d\n",
1104 entry = debugfs_create_file("function_profile_enabled", 0644,
1105 d_tracer, NULL, &ftrace_profile_fops);
1107 pr_warning("Could not create debugfs "
1108 "'function_profile_enabled' entry\n");
1111 #else /* CONFIG_FUNCTION_PROFILER */
1112 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1115 #endif /* CONFIG_FUNCTION_PROFILER */
1117 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1119 #ifdef CONFIG_DYNAMIC_FTRACE
1121 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1122 # error Dynamic ftrace depends on MCOUNT_RECORD
1125 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1127 struct ftrace_func_probe {
1128 struct hlist_node node;
1129 struct ftrace_probe_ops *ops;
1130 unsigned long flags;
1133 struct list_head free_list;
1136 struct ftrace_func_entry {
1137 struct hlist_node hlist;
1141 struct ftrace_hash {
1142 unsigned long size_bits;
1143 struct hlist_head *buckets;
1144 unsigned long count;
1145 struct rcu_head rcu;
1149 * We make these constant because no one should touch them,
1150 * but they are used as the default "empty hash", to avoid allocating
1151 * it all the time. These are in a read only section such that if
1152 * anyone does try to modify it, it will cause an exception.
1154 static const struct hlist_head empty_buckets[1];
1155 static const struct ftrace_hash empty_hash = {
1156 .buckets = (struct hlist_head *)empty_buckets,
1158 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1160 static struct ftrace_ops global_ops = {
1161 .func = ftrace_stub,
1162 .notrace_hash = EMPTY_HASH,
1163 .filter_hash = EMPTY_HASH,
1164 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1165 INIT_REGEX_LOCK(global_ops)
1168 struct ftrace_page {
1169 struct ftrace_page *next;
1170 struct dyn_ftrace *records;
1175 static struct ftrace_page *ftrace_new_pgs;
1177 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1178 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1180 /* estimate from running different kernels */
1181 #define NR_TO_INIT 10000
1183 static struct ftrace_page *ftrace_pages_start;
1184 static struct ftrace_page *ftrace_pages;
1186 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1188 return !hash || !hash->count;
1191 static struct ftrace_func_entry *
1192 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1195 struct ftrace_func_entry *entry;
1196 struct hlist_head *hhd;
1198 if (ftrace_hash_empty(hash))
1201 if (hash->size_bits > 0)
1202 key = hash_long(ip, hash->size_bits);
1206 hhd = &hash->buckets[key];
1208 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1209 if (entry->ip == ip)
1215 static void __add_hash_entry(struct ftrace_hash *hash,
1216 struct ftrace_func_entry *entry)
1218 struct hlist_head *hhd;
1221 if (hash->size_bits)
1222 key = hash_long(entry->ip, hash->size_bits);
1226 hhd = &hash->buckets[key];
1227 hlist_add_head(&entry->hlist, hhd);
1231 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1233 struct ftrace_func_entry *entry;
1235 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1240 __add_hash_entry(hash, entry);
1246 free_hash_entry(struct ftrace_hash *hash,
1247 struct ftrace_func_entry *entry)
1249 hlist_del(&entry->hlist);
1255 remove_hash_entry(struct ftrace_hash *hash,
1256 struct ftrace_func_entry *entry)
1258 hlist_del(&entry->hlist);
1262 static void ftrace_hash_clear(struct ftrace_hash *hash)
1264 struct hlist_head *hhd;
1265 struct hlist_node *tn;
1266 struct ftrace_func_entry *entry;
1267 int size = 1 << hash->size_bits;
1273 for (i = 0; i < size; i++) {
1274 hhd = &hash->buckets[i];
1275 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1276 free_hash_entry(hash, entry);
1278 FTRACE_WARN_ON(hash->count);
1281 static void free_ftrace_hash(struct ftrace_hash *hash)
1283 if (!hash || hash == EMPTY_HASH)
1285 ftrace_hash_clear(hash);
1286 kfree(hash->buckets);
1290 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1292 struct ftrace_hash *hash;
1294 hash = container_of(rcu, struct ftrace_hash, rcu);
1295 free_ftrace_hash(hash);
1298 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1300 if (!hash || hash == EMPTY_HASH)
1302 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1305 void ftrace_free_filter(struct ftrace_ops *ops)
1307 ftrace_ops_init(ops);
1308 free_ftrace_hash(ops->filter_hash);
1309 free_ftrace_hash(ops->notrace_hash);
1312 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1314 struct ftrace_hash *hash;
1317 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1321 size = 1 << size_bits;
1322 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1324 if (!hash->buckets) {
1329 hash->size_bits = size_bits;
1334 static struct ftrace_hash *
1335 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1337 struct ftrace_func_entry *entry;
1338 struct ftrace_hash *new_hash;
1343 new_hash = alloc_ftrace_hash(size_bits);
1348 if (ftrace_hash_empty(hash))
1351 size = 1 << hash->size_bits;
1352 for (i = 0; i < size; i++) {
1353 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1354 ret = add_hash_entry(new_hash, entry->ip);
1360 FTRACE_WARN_ON(new_hash->count != hash->count);
1365 free_ftrace_hash(new_hash);
1370 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1372 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1375 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1376 struct ftrace_hash **dst, struct ftrace_hash *src)
1378 struct ftrace_func_entry *entry;
1379 struct hlist_node *tn;
1380 struct hlist_head *hhd;
1381 struct ftrace_hash *old_hash;
1382 struct ftrace_hash *new_hash;
1383 int size = src->count;
1389 * Remove the current set, update the hash and add
1392 ftrace_hash_rec_disable(ops, enable);
1395 * If the new source is empty, just free dst and assign it
1399 free_ftrace_hash_rcu(*dst);
1400 rcu_assign_pointer(*dst, EMPTY_HASH);
1401 /* still need to update the function records */
1407 * Make the hash size about 1/2 the # found
1409 for (size /= 2; size; size >>= 1)
1412 /* Don't allocate too much */
1413 if (bits > FTRACE_HASH_MAX_BITS)
1414 bits = FTRACE_HASH_MAX_BITS;
1417 new_hash = alloc_ftrace_hash(bits);
1421 size = 1 << src->size_bits;
1422 for (i = 0; i < size; i++) {
1423 hhd = &src->buckets[i];
1424 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1425 remove_hash_entry(src, entry);
1426 __add_hash_entry(new_hash, entry);
1431 rcu_assign_pointer(*dst, new_hash);
1432 free_ftrace_hash_rcu(old_hash);
1437 * Enable regardless of ret:
1438 * On success, we enable the new hash.
1439 * On failure, we re-enable the original hash.
1441 ftrace_hash_rec_enable(ops, enable);
1447 * Test the hashes for this ops to see if we want to call
1448 * the ops->func or not.
1450 * It's a match if the ip is in the ops->filter_hash or
1451 * the filter_hash does not exist or is empty,
1453 * the ip is not in the ops->notrace_hash.
1455 * This needs to be called with preemption disabled as
1456 * the hashes are freed with call_rcu_sched().
1459 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1461 struct ftrace_hash *filter_hash;
1462 struct ftrace_hash *notrace_hash;
1465 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1467 * There's a small race when adding ops that the ftrace handler
1468 * that wants regs, may be called without them. We can not
1469 * allow that handler to be called if regs is NULL.
1471 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1475 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1476 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1478 if ((ftrace_hash_empty(filter_hash) ||
1479 ftrace_lookup_ip(filter_hash, ip)) &&
1480 (ftrace_hash_empty(notrace_hash) ||
1481 !ftrace_lookup_ip(notrace_hash, ip)))
1490 * This is a double for. Do not use 'break' to break out of the loop,
1491 * you must use a goto.
1493 #define do_for_each_ftrace_rec(pg, rec) \
1494 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1496 for (_____i = 0; _____i < pg->index; _____i++) { \
1497 rec = &pg->records[_____i];
1499 #define while_for_each_ftrace_rec() \
1504 static int ftrace_cmp_recs(const void *a, const void *b)
1506 const struct dyn_ftrace *key = a;
1507 const struct dyn_ftrace *rec = b;
1509 if (key->flags < rec->ip)
1511 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1516 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1518 struct ftrace_page *pg;
1519 struct dyn_ftrace *rec;
1520 struct dyn_ftrace key;
1523 key.flags = end; /* overload flags, as it is unsigned long */
1525 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1526 if (end < pg->records[0].ip ||
1527 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1529 rec = bsearch(&key, pg->records, pg->index,
1530 sizeof(struct dyn_ftrace),
1540 * ftrace_location - return true if the ip giving is a traced location
1541 * @ip: the instruction pointer to check
1543 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1544 * That is, the instruction that is either a NOP or call to
1545 * the function tracer. It checks the ftrace internal tables to
1546 * determine if the address belongs or not.
1548 unsigned long ftrace_location(unsigned long ip)
1550 return ftrace_location_range(ip, ip);
1554 * ftrace_text_reserved - return true if range contains an ftrace location
1555 * @start: start of range to search
1556 * @end: end of range to search (inclusive). @end points to the last byte to check.
1558 * Returns 1 if @start and @end contains a ftrace location.
1559 * That is, the instruction that is either a NOP or call to
1560 * the function tracer. It checks the ftrace internal tables to
1561 * determine if the address belongs or not.
1563 int ftrace_text_reserved(void *start, void *end)
1567 ret = ftrace_location_range((unsigned long)start,
1568 (unsigned long)end);
1573 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1577 struct ftrace_hash *hash;
1578 struct ftrace_hash *other_hash;
1579 struct ftrace_page *pg;
1580 struct dyn_ftrace *rec;
1584 /* Only update if the ops has been registered */
1585 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1589 * In the filter_hash case:
1590 * If the count is zero, we update all records.
1591 * Otherwise we just update the items in the hash.
1593 * In the notrace_hash case:
1594 * We enable the update in the hash.
1595 * As disabling notrace means enabling the tracing,
1596 * and enabling notrace means disabling, the inc variable
1600 hash = ops->filter_hash;
1601 other_hash = ops->notrace_hash;
1602 if (ftrace_hash_empty(hash))
1606 hash = ops->notrace_hash;
1607 other_hash = ops->filter_hash;
1609 * If the notrace hash has no items,
1610 * then there's nothing to do.
1612 if (ftrace_hash_empty(hash))
1616 do_for_each_ftrace_rec(pg, rec) {
1617 int in_other_hash = 0;
1623 * Only the filter_hash affects all records.
1624 * Update if the record is not in the notrace hash.
1626 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1629 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1630 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1635 if (filter_hash && in_hash && !in_other_hash)
1637 else if (!filter_hash && in_hash &&
1638 (in_other_hash || ftrace_hash_empty(other_hash)))
1646 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1649 * If any ops wants regs saved for this function
1650 * then all ops will get saved regs.
1652 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1653 rec->flags |= FTRACE_FL_REGS;
1655 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1660 /* Shortcut, if we handled all records, we are done. */
1661 if (!all && count == hash->count)
1663 } while_for_each_ftrace_rec();
1666 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1669 __ftrace_hash_rec_update(ops, filter_hash, 0);
1672 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1675 __ftrace_hash_rec_update(ops, filter_hash, 1);
1678 static void print_ip_ins(const char *fmt, unsigned char *p)
1682 printk(KERN_CONT "%s", fmt);
1684 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1685 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1689 * ftrace_bug - report and shutdown function tracer
1690 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1691 * @ip: The address that failed
1693 * The arch code that enables or disables the function tracing
1694 * can call ftrace_bug() when it has detected a problem in
1695 * modifying the code. @failed should be one of either:
1696 * EFAULT - if the problem happens on reading the @ip address
1697 * EINVAL - if what is read at @ip is not what was expected
1698 * EPERM - if the problem happens on writting to the @ip address
1700 void ftrace_bug(int failed, unsigned long ip)
1704 FTRACE_WARN_ON_ONCE(1);
1705 pr_info("ftrace faulted on modifying ");
1709 FTRACE_WARN_ON_ONCE(1);
1710 pr_info("ftrace failed to modify ");
1712 print_ip_ins(" actual: ", (unsigned char *)ip);
1713 printk(KERN_CONT "\n");
1716 FTRACE_WARN_ON_ONCE(1);
1717 pr_info("ftrace faulted on writing ");
1721 FTRACE_WARN_ON_ONCE(1);
1722 pr_info("ftrace faulted on unknown error ");
1727 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1729 unsigned long flag = 0UL;
1732 * If we are updating calls:
1734 * If the record has a ref count, then we need to enable it
1735 * because someone is using it.
1737 * Otherwise we make sure its disabled.
1739 * If we are disabling calls, then disable all records that
1742 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1743 flag = FTRACE_FL_ENABLED;
1746 * If enabling and the REGS flag does not match the REGS_EN, then
1747 * do not ignore this record. Set flags to fail the compare against
1751 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1752 flag |= FTRACE_FL_REGS;
1754 /* If the state of this record hasn't changed, then do nothing */
1755 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1756 return FTRACE_UPDATE_IGNORE;
1759 /* Save off if rec is being enabled (for return value) */
1760 flag ^= rec->flags & FTRACE_FL_ENABLED;
1763 rec->flags |= FTRACE_FL_ENABLED;
1764 if (flag & FTRACE_FL_REGS) {
1765 if (rec->flags & FTRACE_FL_REGS)
1766 rec->flags |= FTRACE_FL_REGS_EN;
1768 rec->flags &= ~FTRACE_FL_REGS_EN;
1773 * If this record is being updated from a nop, then
1774 * return UPDATE_MAKE_CALL.
1775 * Otherwise, if the EN flag is set, then return
1776 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1777 * from the non-save regs, to a save regs function.
1779 * return UPDATE_MODIFY_CALL to tell the caller to convert
1780 * from the save regs, to a non-save regs function.
1782 if (flag & FTRACE_FL_ENABLED)
1783 return FTRACE_UPDATE_MAKE_CALL;
1784 else if (rec->flags & FTRACE_FL_REGS_EN)
1785 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1787 return FTRACE_UPDATE_MODIFY_CALL;
1791 /* If there's no more users, clear all flags */
1792 if (!(rec->flags & ~FTRACE_FL_MASK))
1795 /* Just disable the record (keep REGS state) */
1796 rec->flags &= ~FTRACE_FL_ENABLED;
1799 return FTRACE_UPDATE_MAKE_NOP;
1803 * ftrace_update_record, set a record that now is tracing or not
1804 * @rec: the record to update
1805 * @enable: set to 1 if the record is tracing, zero to force disable
1807 * The records that represent all functions that can be traced need
1808 * to be updated when tracing has been enabled.
1810 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1812 return ftrace_check_record(rec, enable, 1);
1816 * ftrace_test_record, check if the record has been enabled or not
1817 * @rec: the record to test
1818 * @enable: set to 1 to check if enabled, 0 if it is disabled
1820 * The arch code may need to test if a record is already set to
1821 * tracing to determine how to modify the function code that it
1824 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1826 return ftrace_check_record(rec, enable, 0);
1830 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1832 unsigned long ftrace_old_addr;
1833 unsigned long ftrace_addr;
1836 ret = ftrace_update_record(rec, enable);
1838 if (rec->flags & FTRACE_FL_REGS)
1839 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1841 ftrace_addr = (unsigned long)FTRACE_ADDR;
1844 case FTRACE_UPDATE_IGNORE:
1847 case FTRACE_UPDATE_MAKE_CALL:
1848 return ftrace_make_call(rec, ftrace_addr);
1850 case FTRACE_UPDATE_MAKE_NOP:
1851 return ftrace_make_nop(NULL, rec, ftrace_addr);
1853 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1854 case FTRACE_UPDATE_MODIFY_CALL:
1855 if (rec->flags & FTRACE_FL_REGS)
1856 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1858 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1860 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1863 return -1; /* unknow ftrace bug */
1866 void __weak ftrace_replace_code(int enable)
1868 struct dyn_ftrace *rec;
1869 struct ftrace_page *pg;
1872 if (unlikely(ftrace_disabled))
1875 do_for_each_ftrace_rec(pg, rec) {
1876 failed = __ftrace_replace_code(rec, enable);
1878 ftrace_bug(failed, rec->ip);
1879 /* Stop processing */
1882 } while_for_each_ftrace_rec();
1885 struct ftrace_rec_iter {
1886 struct ftrace_page *pg;
1891 * ftrace_rec_iter_start, start up iterating over traced functions
1893 * Returns an iterator handle that is used to iterate over all
1894 * the records that represent address locations where functions
1897 * May return NULL if no records are available.
1899 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1902 * We only use a single iterator.
1903 * Protected by the ftrace_lock mutex.
1905 static struct ftrace_rec_iter ftrace_rec_iter;
1906 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1908 iter->pg = ftrace_pages_start;
1911 /* Could have empty pages */
1912 while (iter->pg && !iter->pg->index)
1913 iter->pg = iter->pg->next;
1922 * ftrace_rec_iter_next, get the next record to process.
1923 * @iter: The handle to the iterator.
1925 * Returns the next iterator after the given iterator @iter.
1927 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1931 if (iter->index >= iter->pg->index) {
1932 iter->pg = iter->pg->next;
1935 /* Could have empty pages */
1936 while (iter->pg && !iter->pg->index)
1937 iter->pg = iter->pg->next;
1947 * ftrace_rec_iter_record, get the record at the iterator location
1948 * @iter: The current iterator location
1950 * Returns the record that the current @iter is at.
1952 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1954 return &iter->pg->records[iter->index];
1958 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1965 if (unlikely(ftrace_disabled))
1968 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1970 ftrace_bug(ret, ip);
1977 * archs can override this function if they must do something
1978 * before the modifying code is performed.
1980 int __weak ftrace_arch_code_modify_prepare(void)
1986 * archs can override this function if they must do something
1987 * after the modifying code is performed.
1989 int __weak ftrace_arch_code_modify_post_process(void)
1994 void ftrace_modify_all_code(int command)
1996 int update = command & FTRACE_UPDATE_TRACE_FUNC;
1999 * If the ftrace_caller calls a ftrace_ops func directly,
2000 * we need to make sure that it only traces functions it
2001 * expects to trace. When doing the switch of functions,
2002 * we need to update to the ftrace_ops_list_func first
2003 * before the transition between old and new calls are set,
2004 * as the ftrace_ops_list_func will check the ops hashes
2005 * to make sure the ops are having the right functions
2009 ftrace_update_ftrace_func(ftrace_ops_list_func);
2011 if (command & FTRACE_UPDATE_CALLS)
2012 ftrace_replace_code(1);
2013 else if (command & FTRACE_DISABLE_CALLS)
2014 ftrace_replace_code(0);
2016 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2017 function_trace_op = set_function_trace_op;
2019 /* If irqs are disabled, we are in stop machine */
2020 if (!irqs_disabled())
2021 smp_call_function(ftrace_sync_ipi, NULL, 1);
2022 ftrace_update_ftrace_func(ftrace_trace_function);
2025 if (command & FTRACE_START_FUNC_RET)
2026 ftrace_enable_ftrace_graph_caller();
2027 else if (command & FTRACE_STOP_FUNC_RET)
2028 ftrace_disable_ftrace_graph_caller();
2031 static int __ftrace_modify_code(void *data)
2033 int *command = data;
2035 ftrace_modify_all_code(*command);
2041 * ftrace_run_stop_machine, go back to the stop machine method
2042 * @command: The command to tell ftrace what to do
2044 * If an arch needs to fall back to the stop machine method, the
2045 * it can call this function.
2047 void ftrace_run_stop_machine(int command)
2049 stop_machine(__ftrace_modify_code, &command, NULL);
2053 * arch_ftrace_update_code, modify the code to trace or not trace
2054 * @command: The command that needs to be done
2056 * Archs can override this function if it does not need to
2057 * run stop_machine() to modify code.
2059 void __weak arch_ftrace_update_code(int command)
2061 ftrace_run_stop_machine(command);
2064 static void ftrace_run_update_code(int command)
2068 ret = ftrace_arch_code_modify_prepare();
2069 FTRACE_WARN_ON(ret);
2073 * Do not call function tracer while we update the code.
2074 * We are in stop machine.
2076 function_trace_stop++;
2079 * By default we use stop_machine() to modify the code.
2080 * But archs can do what ever they want as long as it
2081 * is safe. The stop_machine() is the safest, but also
2082 * produces the most overhead.
2084 arch_ftrace_update_code(command);
2086 function_trace_stop--;
2088 ret = ftrace_arch_code_modify_post_process();
2089 FTRACE_WARN_ON(ret);
2092 static ftrace_func_t saved_ftrace_func;
2093 static int ftrace_start_up;
2094 static int global_start_up;
2096 static void ftrace_startup_enable(int command)
2098 if (saved_ftrace_func != ftrace_trace_function) {
2099 saved_ftrace_func = ftrace_trace_function;
2100 command |= FTRACE_UPDATE_TRACE_FUNC;
2103 if (!command || !ftrace_enabled)
2106 ftrace_run_update_code(command);
2109 static int ftrace_startup(struct ftrace_ops *ops, int command)
2111 bool hash_enable = true;
2114 if (unlikely(ftrace_disabled))
2117 ret = __register_ftrace_function(ops);
2122 command |= FTRACE_UPDATE_CALLS;
2124 /* ops marked global share the filter hashes */
2125 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2127 /* Don't update hash if global is already set */
2128 if (global_start_up)
2129 hash_enable = false;
2133 ops->flags |= FTRACE_OPS_FL_ENABLED;
2135 ftrace_hash_rec_enable(ops, 1);
2137 ftrace_startup_enable(command);
2142 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2144 bool hash_disable = true;
2147 if (unlikely(ftrace_disabled))
2150 ret = __unregister_ftrace_function(ops);
2156 * Just warn in case of unbalance, no need to kill ftrace, it's not
2157 * critical but the ftrace_call callers may be never nopped again after
2158 * further ftrace uses.
2160 WARN_ON_ONCE(ftrace_start_up < 0);
2162 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2165 WARN_ON_ONCE(global_start_up < 0);
2166 /* Don't update hash if global still has users */
2167 if (global_start_up) {
2168 WARN_ON_ONCE(!ftrace_start_up);
2169 hash_disable = false;
2174 ftrace_hash_rec_disable(ops, 1);
2176 if (ops != &global_ops || !global_start_up)
2177 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2179 command |= FTRACE_UPDATE_CALLS;
2181 if (saved_ftrace_func != ftrace_trace_function) {
2182 saved_ftrace_func = ftrace_trace_function;
2183 command |= FTRACE_UPDATE_TRACE_FUNC;
2186 if (!command || !ftrace_enabled) {
2188 * If these are control ops, they still need their
2189 * per_cpu field freed. Since, function tracing is
2190 * not currently active, we can just free them
2191 * without synchronizing all CPUs.
2193 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2194 control_ops_free(ops);
2198 ftrace_run_update_code(command);
2201 * Dynamic ops may be freed, we must make sure that all
2202 * callers are done before leaving this function.
2203 * The same goes for freeing the per_cpu data of the control
2206 * Again, normal synchronize_sched() is not good enough.
2207 * We need to do a hard force of sched synchronization.
2208 * This is because we use preempt_disable() to do RCU, but
2209 * the function tracers can be called where RCU is not watching
2210 * (like before user_exit()). We can not rely on the RCU
2211 * infrastructure to do the synchronization, thus we must do it
2214 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2215 schedule_on_each_cpu(ftrace_sync);
2217 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2218 control_ops_free(ops);
2224 static void ftrace_startup_sysctl(void)
2226 if (unlikely(ftrace_disabled))
2229 /* Force update next time */
2230 saved_ftrace_func = NULL;
2231 /* ftrace_start_up is true if we want ftrace running */
2232 if (ftrace_start_up)
2233 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2236 static void ftrace_shutdown_sysctl(void)
2238 if (unlikely(ftrace_disabled))
2241 /* ftrace_start_up is true if ftrace is running */
2242 if (ftrace_start_up)
2243 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2246 static cycle_t ftrace_update_time;
2247 static unsigned long ftrace_update_cnt;
2248 unsigned long ftrace_update_tot_cnt;
2250 static inline int ops_traces_mod(struct ftrace_ops *ops)
2253 * Filter_hash being empty will default to trace module.
2254 * But notrace hash requires a test of individual module functions.
2256 return ftrace_hash_empty(ops->filter_hash) &&
2257 ftrace_hash_empty(ops->notrace_hash);
2261 * Check if the current ops references the record.
2263 * If the ops traces all functions, then it was already accounted for.
2264 * If the ops does not trace the current record function, skip it.
2265 * If the ops ignores the function via notrace filter, skip it.
2268 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2270 /* If ops isn't enabled, ignore it */
2271 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2274 /* If ops traces all mods, we already accounted for it */
2275 if (ops_traces_mod(ops))
2278 /* The function must be in the filter */
2279 if (!ftrace_hash_empty(ops->filter_hash) &&
2280 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2283 /* If in notrace hash, we ignore it too */
2284 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2290 static int referenced_filters(struct dyn_ftrace *rec)
2292 struct ftrace_ops *ops;
2295 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2296 if (ops_references_rec(ops, rec))
2303 static int ftrace_update_code(struct module *mod)
2305 struct ftrace_page *pg;
2306 struct dyn_ftrace *p;
2307 cycle_t start, stop;
2308 unsigned long ref = 0;
2313 * When adding a module, we need to check if tracers are
2314 * currently enabled and if they are set to trace all functions.
2315 * If they are, we need to enable the module functions as well
2316 * as update the reference counts for those function records.
2319 struct ftrace_ops *ops;
2321 for (ops = ftrace_ops_list;
2322 ops != &ftrace_list_end; ops = ops->next) {
2323 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2324 if (ops_traces_mod(ops))
2332 start = ftrace_now(raw_smp_processor_id());
2333 ftrace_update_cnt = 0;
2335 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2337 for (i = 0; i < pg->index; i++) {
2340 /* If something went wrong, bail without enabling anything */
2341 if (unlikely(ftrace_disabled))
2344 p = &pg->records[i];
2346 cnt += referenced_filters(p);
2350 * Do the initial record conversion from mcount jump
2351 * to the NOP instructions.
2353 if (!ftrace_code_disable(mod, p))
2356 ftrace_update_cnt++;
2359 * If the tracing is enabled, go ahead and enable the record.
2361 * The reason not to enable the record immediatelly is the
2362 * inherent check of ftrace_make_nop/ftrace_make_call for
2363 * correct previous instructions. Making first the NOP
2364 * conversion puts the module to the correct state, thus
2365 * passing the ftrace_make_call check.
2367 if (ftrace_start_up && cnt) {
2368 int failed = __ftrace_replace_code(p, 1);
2370 ftrace_bug(failed, p->ip);
2375 ftrace_new_pgs = NULL;
2377 stop = ftrace_now(raw_smp_processor_id());
2378 ftrace_update_time = stop - start;
2379 ftrace_update_tot_cnt += ftrace_update_cnt;
2384 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2389 if (WARN_ON(!count))
2392 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2395 * We want to fill as much as possible. No more than a page
2398 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2402 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2405 /* if we can't allocate this size, try something smaller */
2412 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2421 static struct ftrace_page *
2422 ftrace_allocate_pages(unsigned long num_to_init)
2424 struct ftrace_page *start_pg;
2425 struct ftrace_page *pg;
2432 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2437 * Try to allocate as much as possible in one continues
2438 * location that fills in all of the space. We want to
2439 * waste as little space as possible.
2442 cnt = ftrace_allocate_records(pg, num_to_init);
2450 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2461 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2462 free_pages((unsigned long)pg->records, order);
2463 start_pg = pg->next;
2467 pr_info("ftrace: FAILED to allocate memory for functions\n");
2471 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2476 pr_info("ftrace: No functions to be traced?\n");
2480 cnt = num_to_init / ENTRIES_PER_PAGE;
2481 pr_info("ftrace: allocating %ld entries in %d pages\n",
2482 num_to_init, cnt + 1);
2487 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2489 struct ftrace_iterator {
2492 struct ftrace_page *pg;
2493 struct dyn_ftrace *func;
2494 struct ftrace_func_probe *probe;
2495 struct trace_parser parser;
2496 struct ftrace_hash *hash;
2497 struct ftrace_ops *ops;
2504 t_hash_next(struct seq_file *m, loff_t *pos)
2506 struct ftrace_iterator *iter = m->private;
2507 struct hlist_node *hnd = NULL;
2508 struct hlist_head *hhd;
2514 hnd = &iter->probe->node;
2516 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2519 hhd = &ftrace_func_hash[iter->hidx];
2521 if (hlist_empty(hhd)) {
2537 if (WARN_ON_ONCE(!hnd))
2540 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2545 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2547 struct ftrace_iterator *iter = m->private;
2551 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2554 if (iter->func_pos > *pos)
2558 for (l = 0; l <= (*pos - iter->func_pos); ) {
2559 p = t_hash_next(m, &l);
2566 /* Only set this if we have an item */
2567 iter->flags |= FTRACE_ITER_HASH;
2573 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2575 struct ftrace_func_probe *rec;
2578 if (WARN_ON_ONCE(!rec))
2581 if (rec->ops->print)
2582 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2584 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2587 seq_printf(m, ":%p", rec->data);
2594 t_next(struct seq_file *m, void *v, loff_t *pos)
2596 struct ftrace_iterator *iter = m->private;
2597 struct ftrace_ops *ops = iter->ops;
2598 struct dyn_ftrace *rec = NULL;
2600 if (unlikely(ftrace_disabled))
2603 if (iter->flags & FTRACE_ITER_HASH)
2604 return t_hash_next(m, pos);
2607 iter->pos = iter->func_pos = *pos;
2609 if (iter->flags & FTRACE_ITER_PRINTALL)
2610 return t_hash_start(m, pos);
2613 if (iter->idx >= iter->pg->index) {
2614 if (iter->pg->next) {
2615 iter->pg = iter->pg->next;
2620 rec = &iter->pg->records[iter->idx++];
2621 if (((iter->flags & FTRACE_ITER_FILTER) &&
2622 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2624 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2625 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2627 ((iter->flags & FTRACE_ITER_ENABLED) &&
2628 !(rec->flags & FTRACE_FL_ENABLED))) {
2636 return t_hash_start(m, pos);
2643 static void reset_iter_read(struct ftrace_iterator *iter)
2647 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2650 static void *t_start(struct seq_file *m, loff_t *pos)
2652 struct ftrace_iterator *iter = m->private;
2653 struct ftrace_ops *ops = iter->ops;
2657 mutex_lock(&ftrace_lock);
2659 if (unlikely(ftrace_disabled))
2663 * If an lseek was done, then reset and start from beginning.
2665 if (*pos < iter->pos)
2666 reset_iter_read(iter);
2669 * For set_ftrace_filter reading, if we have the filter
2670 * off, we can short cut and just print out that all
2671 * functions are enabled.
2673 if (iter->flags & FTRACE_ITER_FILTER &&
2674 ftrace_hash_empty(ops->filter_hash)) {
2676 return t_hash_start(m, pos);
2677 iter->flags |= FTRACE_ITER_PRINTALL;
2678 /* reset in case of seek/pread */
2679 iter->flags &= ~FTRACE_ITER_HASH;
2683 if (iter->flags & FTRACE_ITER_HASH)
2684 return t_hash_start(m, pos);
2687 * Unfortunately, we need to restart at ftrace_pages_start
2688 * every time we let go of the ftrace_mutex. This is because
2689 * those pointers can change without the lock.
2691 iter->pg = ftrace_pages_start;
2693 for (l = 0; l <= *pos; ) {
2694 p = t_next(m, p, &l);
2700 return t_hash_start(m, pos);
2705 static void t_stop(struct seq_file *m, void *p)
2707 mutex_unlock(&ftrace_lock);
2710 static int t_show(struct seq_file *m, void *v)
2712 struct ftrace_iterator *iter = m->private;
2713 struct dyn_ftrace *rec;
2715 if (iter->flags & FTRACE_ITER_HASH)
2716 return t_hash_show(m, iter);
2718 if (iter->flags & FTRACE_ITER_PRINTALL) {
2719 seq_printf(m, "#### all functions enabled ####\n");
2728 seq_printf(m, "%ps", (void *)rec->ip);
2729 if (iter->flags & FTRACE_ITER_ENABLED)
2730 seq_printf(m, " (%ld)%s",
2731 rec->flags & ~FTRACE_FL_MASK,
2732 rec->flags & FTRACE_FL_REGS ? " R" : "");
2733 seq_printf(m, "\n");
2738 static const struct seq_operations show_ftrace_seq_ops = {
2746 ftrace_avail_open(struct inode *inode, struct file *file)
2748 struct ftrace_iterator *iter;
2750 if (unlikely(ftrace_disabled))
2753 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2755 iter->pg = ftrace_pages_start;
2756 iter->ops = &global_ops;
2759 return iter ? 0 : -ENOMEM;
2763 ftrace_enabled_open(struct inode *inode, struct file *file)
2765 struct ftrace_iterator *iter;
2767 if (unlikely(ftrace_disabled))
2770 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2772 iter->pg = ftrace_pages_start;
2773 iter->flags = FTRACE_ITER_ENABLED;
2774 iter->ops = &global_ops;
2777 return iter ? 0 : -ENOMEM;
2780 static void ftrace_filter_reset(struct ftrace_hash *hash)
2782 mutex_lock(&ftrace_lock);
2783 ftrace_hash_clear(hash);
2784 mutex_unlock(&ftrace_lock);
2788 * ftrace_regex_open - initialize function tracer filter files
2789 * @ops: The ftrace_ops that hold the hash filters
2790 * @flag: The type of filter to process
2791 * @inode: The inode, usually passed in to your open routine
2792 * @file: The file, usually passed in to your open routine
2794 * ftrace_regex_open() initializes the filter files for the
2795 * @ops. Depending on @flag it may process the filter hash or
2796 * the notrace hash of @ops. With this called from the open
2797 * routine, you can use ftrace_filter_write() for the write
2798 * routine if @flag has FTRACE_ITER_FILTER set, or
2799 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2800 * tracing_lseek() should be used as the lseek routine, and
2801 * release must call ftrace_regex_release().
2804 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2805 struct inode *inode, struct file *file)
2807 struct ftrace_iterator *iter;
2808 struct ftrace_hash *hash;
2811 ftrace_ops_init(ops);
2813 if (unlikely(ftrace_disabled))
2816 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2820 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2828 mutex_lock(&ops->regex_lock);
2830 if (flag & FTRACE_ITER_NOTRACE)
2831 hash = ops->notrace_hash;
2833 hash = ops->filter_hash;
2835 if (file->f_mode & FMODE_WRITE) {
2836 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2838 trace_parser_put(&iter->parser);
2845 if ((file->f_mode & FMODE_WRITE) &&
2846 (file->f_flags & O_TRUNC))
2847 ftrace_filter_reset(iter->hash);
2849 if (file->f_mode & FMODE_READ) {
2850 iter->pg = ftrace_pages_start;
2852 ret = seq_open(file, &show_ftrace_seq_ops);
2854 struct seq_file *m = file->private_data;
2858 free_ftrace_hash(iter->hash);
2859 trace_parser_put(&iter->parser);
2863 file->private_data = iter;
2866 mutex_unlock(&ops->regex_lock);
2872 ftrace_filter_open(struct inode *inode, struct file *file)
2874 return ftrace_regex_open(&global_ops,
2875 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2880 ftrace_notrace_open(struct inode *inode, struct file *file)
2882 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2886 static int ftrace_match(char *str, char *regex, int len, int type)
2893 if (strcmp(str, regex) == 0)
2896 case MATCH_FRONT_ONLY:
2897 if (strncmp(str, regex, len) == 0)
2900 case MATCH_MIDDLE_ONLY:
2901 if (strstr(str, regex))
2904 case MATCH_END_ONLY:
2906 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2915 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2917 struct ftrace_func_entry *entry;
2920 entry = ftrace_lookup_ip(hash, rec->ip);
2922 /* Do nothing if it doesn't exist */
2926 free_hash_entry(hash, entry);
2928 /* Do nothing if it exists */
2932 ret = add_hash_entry(hash, rec->ip);
2938 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2939 char *regex, int len, int type)
2941 char str[KSYM_SYMBOL_LEN];
2944 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2947 /* module lookup requires matching the module */
2948 if (!modname || strcmp(modname, mod))
2951 /* blank search means to match all funcs in the mod */
2956 return ftrace_match(str, regex, len, type);
2960 match_records(struct ftrace_hash *hash, char *buff,
2961 int len, char *mod, int not)
2963 unsigned search_len = 0;
2964 struct ftrace_page *pg;
2965 struct dyn_ftrace *rec;
2966 int type = MATCH_FULL;
2967 char *search = buff;
2972 type = filter_parse_regex(buff, len, &search, ¬);
2973 search_len = strlen(search);
2976 mutex_lock(&ftrace_lock);
2978 if (unlikely(ftrace_disabled))
2981 do_for_each_ftrace_rec(pg, rec) {
2982 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2983 ret = enter_record(hash, rec, not);
2990 } while_for_each_ftrace_rec();
2992 mutex_unlock(&ftrace_lock);
2998 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3000 return match_records(hash, buff, len, NULL, 0);
3004 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
3008 /* blank or '*' mean the same */
3009 if (strcmp(buff, "*") == 0)
3012 /* handle the case of 'dont filter this module' */
3013 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3018 return match_records(hash, buff, strlen(buff), mod, not);
3022 * We register the module command as a template to show others how
3023 * to register the a command as well.
3027 ftrace_mod_callback(struct ftrace_hash *hash,
3028 char *func, char *cmd, char *param, int enable)
3034 * cmd == 'mod' because we only registered this func
3035 * for the 'mod' ftrace_func_command.
3036 * But if you register one func with multiple commands,
3037 * you can tell which command was used by the cmd
3041 /* we must have a module name */
3045 mod = strsep(¶m, ":");
3049 ret = ftrace_match_module_records(hash, func, mod);
3058 static struct ftrace_func_command ftrace_mod_cmd = {
3060 .func = ftrace_mod_callback,
3063 static int __init ftrace_mod_cmd_init(void)
3065 return register_ftrace_command(&ftrace_mod_cmd);
3067 core_initcall(ftrace_mod_cmd_init);
3069 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3070 struct ftrace_ops *op, struct pt_regs *pt_regs)
3072 struct ftrace_func_probe *entry;
3073 struct hlist_head *hhd;
3076 key = hash_long(ip, FTRACE_HASH_BITS);
3078 hhd = &ftrace_func_hash[key];
3080 if (hlist_empty(hhd))
3084 * Disable preemption for these calls to prevent a RCU grace
3085 * period. This syncs the hash iteration and freeing of items
3086 * on the hash. rcu_read_lock is too dangerous here.
3088 preempt_disable_notrace();
3089 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3090 if (entry->ip == ip)
3091 entry->ops->func(ip, parent_ip, &entry->data);
3093 preempt_enable_notrace();
3096 static struct ftrace_ops trace_probe_ops __read_mostly =
3098 .func = function_trace_probe_call,
3099 .flags = FTRACE_OPS_FL_INITIALIZED,
3100 INIT_REGEX_LOCK(trace_probe_ops)
3103 static int ftrace_probe_registered;
3105 static void __enable_ftrace_function_probe(void)
3110 if (ftrace_probe_registered) {
3111 /* still need to update the function call sites */
3113 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3117 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3118 struct hlist_head *hhd = &ftrace_func_hash[i];
3122 /* Nothing registered? */
3123 if (i == FTRACE_FUNC_HASHSIZE)
3126 ret = ftrace_startup(&trace_probe_ops, 0);
3128 ftrace_probe_registered = 1;
3131 static void __disable_ftrace_function_probe(void)
3135 if (!ftrace_probe_registered)
3138 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3139 struct hlist_head *hhd = &ftrace_func_hash[i];
3144 /* no more funcs left */
3145 ftrace_shutdown(&trace_probe_ops, 0);
3147 ftrace_probe_registered = 0;
3151 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3153 if (entry->ops->free)
3154 entry->ops->free(entry->ops, entry->ip, &entry->data);
3159 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3162 struct ftrace_func_probe *entry;
3163 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3164 struct ftrace_hash *hash;
3165 struct ftrace_page *pg;
3166 struct dyn_ftrace *rec;
3173 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3174 len = strlen(search);
3176 /* we do not support '!' for function probes */
3180 mutex_lock(&trace_probe_ops.regex_lock);
3182 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3188 if (unlikely(ftrace_disabled)) {
3193 mutex_lock(&ftrace_lock);
3195 do_for_each_ftrace_rec(pg, rec) {
3197 if (!ftrace_match_record(rec, NULL, search, len, type))
3200 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3202 /* If we did not process any, then return error */
3213 * The caller might want to do something special
3214 * for each function we find. We call the callback
3215 * to give the caller an opportunity to do so.
3218 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3219 /* caller does not like this func */
3225 ret = enter_record(hash, rec, 0);
3233 entry->ip = rec->ip;
3235 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3236 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3238 } while_for_each_ftrace_rec();
3240 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3244 __enable_ftrace_function_probe();
3247 mutex_unlock(&ftrace_lock);
3249 mutex_unlock(&trace_probe_ops.regex_lock);
3250 free_ftrace_hash(hash);
3256 PROBE_TEST_FUNC = 1,
3261 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3262 void *data, int flags)
3264 struct ftrace_func_entry *rec_entry;
3265 struct ftrace_func_probe *entry;
3266 struct ftrace_func_probe *p;
3267 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3268 struct list_head free_list;
3269 struct ftrace_hash *hash;
3270 struct hlist_node *tmp;
3271 char str[KSYM_SYMBOL_LEN];
3272 int type = MATCH_FULL;
3276 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3281 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3282 len = strlen(search);
3284 /* we do not support '!' for function probes */
3289 mutex_lock(&trace_probe_ops.regex_lock);
3291 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3293 /* Hmm, should report this somehow */
3296 INIT_LIST_HEAD(&free_list);
3298 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3299 struct hlist_head *hhd = &ftrace_func_hash[i];
3301 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3303 /* break up if statements for readability */
3304 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3307 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3310 /* do this last, since it is the most expensive */
3312 kallsyms_lookup(entry->ip, NULL, NULL,
3314 if (!ftrace_match(str, glob, len, type))
3318 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3319 /* It is possible more than one entry had this ip */
3321 free_hash_entry(hash, rec_entry);
3323 hlist_del_rcu(&entry->node);
3324 list_add(&entry->free_list, &free_list);
3327 mutex_lock(&ftrace_lock);
3328 __disable_ftrace_function_probe();
3330 * Remove after the disable is called. Otherwise, if the last
3331 * probe is removed, a null hash means *all enabled*.
3333 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3334 synchronize_sched();
3335 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3336 list_del(&entry->free_list);
3337 ftrace_free_entry(entry);
3339 mutex_unlock(&ftrace_lock);
3342 mutex_unlock(&trace_probe_ops.regex_lock);
3343 free_ftrace_hash(hash);
3347 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3350 __unregister_ftrace_function_probe(glob, ops, data,
3351 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3355 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3357 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3360 void unregister_ftrace_function_probe_all(char *glob)
3362 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3365 static LIST_HEAD(ftrace_commands);
3366 static DEFINE_MUTEX(ftrace_cmd_mutex);
3369 * Currently we only register ftrace commands from __init, so mark this
3372 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3374 struct ftrace_func_command *p;
3377 mutex_lock(&ftrace_cmd_mutex);
3378 list_for_each_entry(p, &ftrace_commands, list) {
3379 if (strcmp(cmd->name, p->name) == 0) {
3384 list_add(&cmd->list, &ftrace_commands);
3386 mutex_unlock(&ftrace_cmd_mutex);
3392 * Currently we only unregister ftrace commands from __init, so mark
3395 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3397 struct ftrace_func_command *p, *n;
3400 mutex_lock(&ftrace_cmd_mutex);
3401 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3402 if (strcmp(cmd->name, p->name) == 0) {
3404 list_del_init(&p->list);
3409 mutex_unlock(&ftrace_cmd_mutex);
3414 static int ftrace_process_regex(struct ftrace_hash *hash,
3415 char *buff, int len, int enable)
3417 char *func, *command, *next = buff;
3418 struct ftrace_func_command *p;
3421 func = strsep(&next, ":");
3424 ret = ftrace_match_records(hash, func, len);
3434 command = strsep(&next, ":");
3436 mutex_lock(&ftrace_cmd_mutex);
3437 list_for_each_entry(p, &ftrace_commands, list) {
3438 if (strcmp(p->name, command) == 0) {
3439 ret = p->func(hash, func, command, next, enable);
3444 mutex_unlock(&ftrace_cmd_mutex);
3450 ftrace_regex_write(struct file *file, const char __user *ubuf,
3451 size_t cnt, loff_t *ppos, int enable)
3453 struct ftrace_iterator *iter;
3454 struct trace_parser *parser;
3460 if (file->f_mode & FMODE_READ) {
3461 struct seq_file *m = file->private_data;
3464 iter = file->private_data;
3466 if (unlikely(ftrace_disabled))
3469 /* iter->hash is a local copy, so we don't need regex_lock */
3471 parser = &iter->parser;
3472 read = trace_get_user(parser, ubuf, cnt, ppos);
3474 if (read >= 0 && trace_parser_loaded(parser) &&
3475 !trace_parser_cont(parser)) {
3476 ret = ftrace_process_regex(iter->hash, parser->buffer,
3477 parser->idx, enable);
3478 trace_parser_clear(parser);
3489 ftrace_filter_write(struct file *file, const char __user *ubuf,
3490 size_t cnt, loff_t *ppos)
3492 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3496 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3497 size_t cnt, loff_t *ppos)
3499 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3503 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3505 struct ftrace_func_entry *entry;
3507 if (!ftrace_location(ip))
3511 entry = ftrace_lookup_ip(hash, ip);
3514 free_hash_entry(hash, entry);
3518 return add_hash_entry(hash, ip);
3521 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3523 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3524 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3528 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3529 unsigned long ip, int remove, int reset, int enable)
3531 struct ftrace_hash **orig_hash;
3532 struct ftrace_hash *hash;
3535 /* All global ops uses the global ops filters */
3536 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3539 if (unlikely(ftrace_disabled))
3542 mutex_lock(&ops->regex_lock);
3545 orig_hash = &ops->filter_hash;
3547 orig_hash = &ops->notrace_hash;
3549 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3552 goto out_regex_unlock;
3556 ftrace_filter_reset(hash);
3557 if (buf && !ftrace_match_records(hash, buf, len)) {
3559 goto out_regex_unlock;
3562 ret = ftrace_match_addr(hash, ip, remove);
3564 goto out_regex_unlock;
3567 mutex_lock(&ftrace_lock);
3568 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3570 ftrace_ops_update_code(ops);
3572 mutex_unlock(&ftrace_lock);
3575 mutex_unlock(&ops->regex_lock);
3577 free_ftrace_hash(hash);
3582 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3583 int reset, int enable)
3585 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3589 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3590 * @ops - the ops to set the filter with
3591 * @ip - the address to add to or remove from the filter.
3592 * @remove - non zero to remove the ip from the filter
3593 * @reset - non zero to reset all filters before applying this filter.
3595 * Filters denote which functions should be enabled when tracing is enabled
3596 * If @ip is NULL, it failes to update filter.
3598 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3599 int remove, int reset)
3601 ftrace_ops_init(ops);
3602 return ftrace_set_addr(ops, ip, remove, reset, 1);
3604 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3607 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3608 int reset, int enable)
3610 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3614 * ftrace_set_filter - set a function to filter on in ftrace
3615 * @ops - the ops to set the filter with
3616 * @buf - the string that holds the function filter text.
3617 * @len - the length of the string.
3618 * @reset - non zero to reset all filters before applying this filter.
3620 * Filters denote which functions should be enabled when tracing is enabled.
3621 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3623 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3626 ftrace_ops_init(ops);
3627 return ftrace_set_regex(ops, buf, len, reset, 1);
3629 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3632 * ftrace_set_notrace - set a function to not trace in ftrace
3633 * @ops - the ops to set the notrace filter with
3634 * @buf - the string that holds the function notrace text.
3635 * @len - the length of the string.
3636 * @reset - non zero to reset all filters before applying this filter.
3638 * Notrace Filters denote which functions should not be enabled when tracing
3639 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3642 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3645 ftrace_ops_init(ops);
3646 return ftrace_set_regex(ops, buf, len, reset, 0);
3648 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3650 * ftrace_set_filter - set a function to filter on in ftrace
3651 * @ops - the ops to set the filter with
3652 * @buf - the string that holds the function filter text.
3653 * @len - the length of the string.
3654 * @reset - non zero to reset all filters before applying this filter.
3656 * Filters denote which functions should be enabled when tracing is enabled.
3657 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3659 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3661 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3663 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3666 * ftrace_set_notrace - set a function to not trace in ftrace
3667 * @ops - the ops to set the notrace filter with
3668 * @buf - the string that holds the function notrace text.
3669 * @len - the length of the string.
3670 * @reset - non zero to reset all filters before applying this filter.
3672 * Notrace Filters denote which functions should not be enabled when tracing
3673 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3676 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3678 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3680 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3683 * command line interface to allow users to set filters on boot up.
3685 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3686 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3687 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3689 /* Used by function selftest to not test if filter is set */
3690 bool ftrace_filter_param __initdata;
3692 static int __init set_ftrace_notrace(char *str)
3694 ftrace_filter_param = true;
3695 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3698 __setup("ftrace_notrace=", set_ftrace_notrace);
3700 static int __init set_ftrace_filter(char *str)
3702 ftrace_filter_param = true;
3703 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3706 __setup("ftrace_filter=", set_ftrace_filter);
3708 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3709 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3710 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3712 static int __init set_graph_function(char *str)
3714 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3717 __setup("ftrace_graph_filter=", set_graph_function);
3719 static void __init set_ftrace_early_graph(char *buf)
3725 func = strsep(&buf, ",");
3726 /* we allow only one expression at a time */
3727 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3728 FTRACE_GRAPH_MAX_FUNCS, func);
3730 printk(KERN_DEBUG "ftrace: function %s not "
3731 "traceable\n", func);
3734 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3737 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3741 ftrace_ops_init(ops);
3744 func = strsep(&buf, ",");
3745 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3749 static void __init set_ftrace_early_filters(void)
3751 if (ftrace_filter_buf[0])
3752 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3753 if (ftrace_notrace_buf[0])
3754 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3755 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3756 if (ftrace_graph_buf[0])
3757 set_ftrace_early_graph(ftrace_graph_buf);
3758 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3761 int ftrace_regex_release(struct inode *inode, struct file *file)
3763 struct seq_file *m = (struct seq_file *)file->private_data;
3764 struct ftrace_iterator *iter;
3765 struct ftrace_hash **orig_hash;
3766 struct trace_parser *parser;
3770 if (file->f_mode & FMODE_READ) {
3772 seq_release(inode, file);
3774 iter = file->private_data;
3776 parser = &iter->parser;
3777 if (trace_parser_loaded(parser)) {
3778 parser->buffer[parser->idx] = 0;
3779 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3782 trace_parser_put(parser);
3784 mutex_lock(&iter->ops->regex_lock);
3786 if (file->f_mode & FMODE_WRITE) {
3787 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3790 orig_hash = &iter->ops->filter_hash;
3792 orig_hash = &iter->ops->notrace_hash;
3794 mutex_lock(&ftrace_lock);
3795 ret = ftrace_hash_move(iter->ops, filter_hash,
3796 orig_hash, iter->hash);
3798 ftrace_ops_update_code(iter->ops);
3800 mutex_unlock(&ftrace_lock);
3803 mutex_unlock(&iter->ops->regex_lock);
3804 free_ftrace_hash(iter->hash);
3810 static const struct file_operations ftrace_avail_fops = {
3811 .open = ftrace_avail_open,
3813 .llseek = seq_lseek,
3814 .release = seq_release_private,
3817 static const struct file_operations ftrace_enabled_fops = {
3818 .open = ftrace_enabled_open,
3820 .llseek = seq_lseek,
3821 .release = seq_release_private,
3824 static const struct file_operations ftrace_filter_fops = {
3825 .open = ftrace_filter_open,
3827 .write = ftrace_filter_write,
3828 .llseek = tracing_lseek,
3829 .release = ftrace_regex_release,
3832 static const struct file_operations ftrace_notrace_fops = {
3833 .open = ftrace_notrace_open,
3835 .write = ftrace_notrace_write,
3836 .llseek = tracing_lseek,
3837 .release = ftrace_regex_release,
3840 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3842 static DEFINE_MUTEX(graph_lock);
3844 int ftrace_graph_count;
3845 int ftrace_graph_notrace_count;
3846 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3847 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3849 struct ftrace_graph_data {
3850 unsigned long *table;
3853 const struct seq_operations *seq_ops;
3857 __g_next(struct seq_file *m, loff_t *pos)
3859 struct ftrace_graph_data *fgd = m->private;
3861 if (*pos >= *fgd->count)
3863 return &fgd->table[*pos];
3867 g_next(struct seq_file *m, void *v, loff_t *pos)
3870 return __g_next(m, pos);
3873 static void *g_start(struct seq_file *m, loff_t *pos)
3875 struct ftrace_graph_data *fgd = m->private;
3877 mutex_lock(&graph_lock);
3879 /* Nothing, tell g_show to print all functions are enabled */
3880 if (!*fgd->count && !*pos)
3883 return __g_next(m, pos);
3886 static void g_stop(struct seq_file *m, void *p)
3888 mutex_unlock(&graph_lock);
3891 static int g_show(struct seq_file *m, void *v)
3893 unsigned long *ptr = v;
3898 if (ptr == (unsigned long *)1) {
3899 seq_printf(m, "#### all functions enabled ####\n");
3903 seq_printf(m, "%ps\n", (void *)*ptr);
3908 static const struct seq_operations ftrace_graph_seq_ops = {
3916 __ftrace_graph_open(struct inode *inode, struct file *file,
3917 struct ftrace_graph_data *fgd)
3921 mutex_lock(&graph_lock);
3922 if ((file->f_mode & FMODE_WRITE) &&
3923 (file->f_flags & O_TRUNC)) {
3925 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
3927 mutex_unlock(&graph_lock);
3929 if (file->f_mode & FMODE_READ) {
3930 ret = seq_open(file, fgd->seq_ops);
3932 struct seq_file *m = file->private_data;
3936 file->private_data = fgd;
3942 ftrace_graph_open(struct inode *inode, struct file *file)
3944 struct ftrace_graph_data *fgd;
3946 if (unlikely(ftrace_disabled))
3949 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3953 fgd->table = ftrace_graph_funcs;
3954 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3955 fgd->count = &ftrace_graph_count;
3956 fgd->seq_ops = &ftrace_graph_seq_ops;
3958 return __ftrace_graph_open(inode, file, fgd);
3962 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3964 struct ftrace_graph_data *fgd;
3966 if (unlikely(ftrace_disabled))
3969 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3973 fgd->table = ftrace_graph_notrace_funcs;
3974 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3975 fgd->count = &ftrace_graph_notrace_count;
3976 fgd->seq_ops = &ftrace_graph_seq_ops;
3978 return __ftrace_graph_open(inode, file, fgd);
3982 ftrace_graph_release(struct inode *inode, struct file *file)
3984 if (file->f_mode & FMODE_READ) {
3985 struct seq_file *m = file->private_data;
3988 seq_release(inode, file);
3990 kfree(file->private_data);
3997 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
3999 struct dyn_ftrace *rec;
4000 struct ftrace_page *pg;
4009 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
4010 if (!not && *idx >= size)
4013 search_len = strlen(search);
4015 mutex_lock(&ftrace_lock);
4017 if (unlikely(ftrace_disabled)) {
4018 mutex_unlock(&ftrace_lock);
4022 do_for_each_ftrace_rec(pg, rec) {
4024 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4025 /* if it is in the array */
4027 for (i = 0; i < *idx; i++) {
4028 if (array[i] == rec->ip) {
4037 array[(*idx)++] = rec->ip;
4043 array[i] = array[--(*idx)];
4049 } while_for_each_ftrace_rec();
4051 mutex_unlock(&ftrace_lock);
4060 ftrace_graph_write(struct file *file, const char __user *ubuf,
4061 size_t cnt, loff_t *ppos)
4063 struct trace_parser parser;
4064 ssize_t read, ret = 0;
4065 struct ftrace_graph_data *fgd = file->private_data;
4070 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4073 read = trace_get_user(&parser, ubuf, cnt, ppos);
4075 if (read >= 0 && trace_parser_loaded((&parser))) {
4076 parser.buffer[parser.idx] = 0;
4078 mutex_lock(&graph_lock);
4080 /* we allow only one expression at a time */
4081 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4084 mutex_unlock(&graph_lock);
4090 trace_parser_put(&parser);
4095 static const struct file_operations ftrace_graph_fops = {
4096 .open = ftrace_graph_open,
4098 .write = ftrace_graph_write,
4099 .llseek = tracing_lseek,
4100 .release = ftrace_graph_release,
4103 static const struct file_operations ftrace_graph_notrace_fops = {
4104 .open = ftrace_graph_notrace_open,
4106 .write = ftrace_graph_write,
4107 .llseek = tracing_lseek,
4108 .release = ftrace_graph_release,
4110 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4112 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4115 trace_create_file("available_filter_functions", 0444,
4116 d_tracer, NULL, &ftrace_avail_fops);
4118 trace_create_file("enabled_functions", 0444,
4119 d_tracer, NULL, &ftrace_enabled_fops);
4121 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4122 NULL, &ftrace_filter_fops);
4124 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
4125 NULL, &ftrace_notrace_fops);
4127 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4128 trace_create_file("set_graph_function", 0444, d_tracer,
4130 &ftrace_graph_fops);
4131 trace_create_file("set_graph_notrace", 0444, d_tracer,
4133 &ftrace_graph_notrace_fops);
4134 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4139 static int ftrace_cmp_ips(const void *a, const void *b)
4141 const unsigned long *ipa = a;
4142 const unsigned long *ipb = b;
4151 static void ftrace_swap_ips(void *a, void *b, int size)
4153 unsigned long *ipa = a;
4154 unsigned long *ipb = b;
4162 static int ftrace_process_locs(struct module *mod,
4163 unsigned long *start,
4166 struct ftrace_page *start_pg;
4167 struct ftrace_page *pg;
4168 struct dyn_ftrace *rec;
4169 unsigned long count;
4172 unsigned long flags = 0; /* Shut up gcc */
4175 count = end - start;
4180 sort(start, count, sizeof(*start),
4181 ftrace_cmp_ips, ftrace_swap_ips);
4183 start_pg = ftrace_allocate_pages(count);
4187 mutex_lock(&ftrace_lock);
4190 * Core and each module needs their own pages, as
4191 * modules will free them when they are removed.
4192 * Force a new page to be allocated for modules.
4195 WARN_ON(ftrace_pages || ftrace_pages_start);
4196 /* First initialization */
4197 ftrace_pages = ftrace_pages_start = start_pg;
4202 if (WARN_ON(ftrace_pages->next)) {
4203 /* Hmm, we have free pages? */
4204 while (ftrace_pages->next)
4205 ftrace_pages = ftrace_pages->next;
4208 ftrace_pages->next = start_pg;
4214 addr = ftrace_call_adjust(*p++);
4216 * Some architecture linkers will pad between
4217 * the different mcount_loc sections of different
4218 * object files to satisfy alignments.
4219 * Skip any NULL pointers.
4224 if (pg->index == pg->size) {
4225 /* We should have allocated enough */
4226 if (WARN_ON(!pg->next))
4231 rec = &pg->records[pg->index++];
4235 /* We should have used all pages */
4238 /* Assign the last page to ftrace_pages */
4241 /* These new locations need to be initialized */
4242 ftrace_new_pgs = start_pg;
4245 * We only need to disable interrupts on start up
4246 * because we are modifying code that an interrupt
4247 * may execute, and the modification is not atomic.
4248 * But for modules, nothing runs the code we modify
4249 * until we are finished with it, and there's no
4250 * reason to cause large interrupt latencies while we do it.
4253 local_irq_save(flags);
4254 ftrace_update_code(mod);
4256 local_irq_restore(flags);
4259 mutex_unlock(&ftrace_lock);
4264 #ifdef CONFIG_MODULES
4266 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4268 void ftrace_release_mod(struct module *mod)
4270 struct dyn_ftrace *rec;
4271 struct ftrace_page **last_pg;
4272 struct ftrace_page *pg;
4275 mutex_lock(&ftrace_lock);
4277 if (ftrace_disabled)
4281 * Each module has its own ftrace_pages, remove
4282 * them from the list.
4284 last_pg = &ftrace_pages_start;
4285 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4286 rec = &pg->records[0];
4287 if (within_module_core(rec->ip, mod)) {
4289 * As core pages are first, the first
4290 * page should never be a module page.
4292 if (WARN_ON(pg == ftrace_pages_start))
4295 /* Check if we are deleting the last page */
4296 if (pg == ftrace_pages)
4297 ftrace_pages = next_to_ftrace_page(last_pg);
4299 *last_pg = pg->next;
4300 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4301 free_pages((unsigned long)pg->records, order);
4304 last_pg = &pg->next;
4307 mutex_unlock(&ftrace_lock);
4310 static void ftrace_init_module(struct module *mod,
4311 unsigned long *start, unsigned long *end)
4313 if (ftrace_disabled || start == end)
4315 ftrace_process_locs(mod, start, end);
4318 static int ftrace_module_notify_enter(struct notifier_block *self,
4319 unsigned long val, void *data)
4321 struct module *mod = data;
4323 if (val == MODULE_STATE_COMING)
4324 ftrace_init_module(mod, mod->ftrace_callsites,
4325 mod->ftrace_callsites +
4326 mod->num_ftrace_callsites);
4330 static int ftrace_module_notify_exit(struct notifier_block *self,
4331 unsigned long val, void *data)
4333 struct module *mod = data;
4335 if (val == MODULE_STATE_GOING)
4336 ftrace_release_mod(mod);
4341 static int ftrace_module_notify_enter(struct notifier_block *self,
4342 unsigned long val, void *data)
4346 static int ftrace_module_notify_exit(struct notifier_block *self,
4347 unsigned long val, void *data)
4351 #endif /* CONFIG_MODULES */
4353 struct notifier_block ftrace_module_enter_nb = {
4354 .notifier_call = ftrace_module_notify_enter,
4355 .priority = INT_MAX, /* Run before anything that can use kprobes */
4358 struct notifier_block ftrace_module_exit_nb = {
4359 .notifier_call = ftrace_module_notify_exit,
4360 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4363 extern unsigned long __start_mcount_loc[];
4364 extern unsigned long __stop_mcount_loc[];
4366 void __init ftrace_init(void)
4368 unsigned long count, addr, flags;
4371 /* Keep the ftrace pointer to the stub */
4372 addr = (unsigned long)ftrace_stub;
4374 local_irq_save(flags);
4375 ftrace_dyn_arch_init(&addr);
4376 local_irq_restore(flags);
4378 /* ftrace_dyn_arch_init places the return code in addr */
4382 count = __stop_mcount_loc - __start_mcount_loc;
4384 ret = ftrace_dyn_table_alloc(count);
4388 last_ftrace_enabled = ftrace_enabled = 1;
4390 ret = ftrace_process_locs(NULL,
4394 ret = register_module_notifier(&ftrace_module_enter_nb);
4396 pr_warning("Failed to register trace ftrace module enter notifier\n");
4398 ret = register_module_notifier(&ftrace_module_exit_nb);
4400 pr_warning("Failed to register trace ftrace module exit notifier\n");
4402 set_ftrace_early_filters();
4406 ftrace_disabled = 1;
4411 static struct ftrace_ops global_ops = {
4412 .func = ftrace_stub,
4413 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4414 INIT_REGEX_LOCK(global_ops)
4417 static int __init ftrace_nodyn_init(void)
4422 core_initcall(ftrace_nodyn_init);
4424 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4425 static inline void ftrace_startup_enable(int command) { }
4426 /* Keep as macros so we do not need to define the commands */
4427 # define ftrace_startup(ops, command) \
4429 int ___ret = __register_ftrace_function(ops); \
4431 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4434 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4436 # define ftrace_startup_sysctl() do { } while (0)
4437 # define ftrace_shutdown_sysctl() do { } while (0)
4440 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4445 #endif /* CONFIG_DYNAMIC_FTRACE */
4448 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4449 struct ftrace_ops *op, struct pt_regs *regs)
4451 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4455 * Some of the ops may be dynamically allocated,
4456 * they must be freed after a synchronize_sched().
4458 preempt_disable_notrace();
4459 trace_recursion_set(TRACE_CONTROL_BIT);
4462 * Control funcs (perf) uses RCU. Only trace if
4463 * RCU is currently active.
4465 if (!rcu_is_watching())
4468 do_for_each_ftrace_op(op, ftrace_control_list) {
4469 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4470 !ftrace_function_local_disabled(op) &&
4471 ftrace_ops_test(op, ip, regs))
4472 op->func(ip, parent_ip, op, regs);
4473 } while_for_each_ftrace_op(op);
4475 trace_recursion_clear(TRACE_CONTROL_BIT);
4476 preempt_enable_notrace();
4479 static struct ftrace_ops control_ops = {
4480 .func = ftrace_ops_control_func,
4481 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4482 INIT_REGEX_LOCK(control_ops)
4486 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4487 struct ftrace_ops *ignored, struct pt_regs *regs)
4489 struct ftrace_ops *op;
4492 if (function_trace_stop)
4495 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4500 * Some of the ops may be dynamically allocated,
4501 * they must be freed after a synchronize_sched().
4503 preempt_disable_notrace();
4504 do_for_each_ftrace_op(op, ftrace_ops_list) {
4505 if (ftrace_ops_test(op, ip, regs))
4506 op->func(ip, parent_ip, op, regs);
4507 } while_for_each_ftrace_op(op);
4508 preempt_enable_notrace();
4509 trace_clear_recursion(bit);
4513 * Some archs only support passing ip and parent_ip. Even though
4514 * the list function ignores the op parameter, we do not want any
4515 * C side effects, where a function is called without the caller
4516 * sending a third parameter.
4517 * Archs are to support both the regs and ftrace_ops at the same time.
4518 * If they support ftrace_ops, it is assumed they support regs.
4519 * If call backs want to use regs, they must either check for regs
4520 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4521 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4522 * An architecture can pass partial regs with ftrace_ops and still
4523 * set the ARCH_SUPPORT_FTARCE_OPS.
4525 #if ARCH_SUPPORTS_FTRACE_OPS
4526 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4527 struct ftrace_ops *op, struct pt_regs *regs)
4529 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4532 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4534 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4538 static void clear_ftrace_swapper(void)
4540 struct task_struct *p;
4544 for_each_online_cpu(cpu) {
4546 clear_tsk_trace_trace(p);
4551 static void set_ftrace_swapper(void)
4553 struct task_struct *p;
4557 for_each_online_cpu(cpu) {
4559 set_tsk_trace_trace(p);
4564 static void clear_ftrace_pid(struct pid *pid)
4566 struct task_struct *p;
4569 do_each_pid_task(pid, PIDTYPE_PID, p) {
4570 clear_tsk_trace_trace(p);
4571 } while_each_pid_task(pid, PIDTYPE_PID, p);
4577 static void set_ftrace_pid(struct pid *pid)
4579 struct task_struct *p;
4582 do_each_pid_task(pid, PIDTYPE_PID, p) {
4583 set_tsk_trace_trace(p);
4584 } while_each_pid_task(pid, PIDTYPE_PID, p);
4588 static void clear_ftrace_pid_task(struct pid *pid)
4590 if (pid == ftrace_swapper_pid)
4591 clear_ftrace_swapper();
4593 clear_ftrace_pid(pid);
4596 static void set_ftrace_pid_task(struct pid *pid)
4598 if (pid == ftrace_swapper_pid)
4599 set_ftrace_swapper();
4601 set_ftrace_pid(pid);
4604 static int ftrace_pid_add(int p)
4607 struct ftrace_pid *fpid;
4610 mutex_lock(&ftrace_lock);
4613 pid = ftrace_swapper_pid;
4615 pid = find_get_pid(p);
4622 list_for_each_entry(fpid, &ftrace_pids, list)
4623 if (fpid->pid == pid)
4628 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4632 list_add(&fpid->list, &ftrace_pids);
4635 set_ftrace_pid_task(pid);
4637 ftrace_update_pid_func();
4638 ftrace_startup_enable(0);
4640 mutex_unlock(&ftrace_lock);
4644 if (pid != ftrace_swapper_pid)
4648 mutex_unlock(&ftrace_lock);
4652 static void ftrace_pid_reset(void)
4654 struct ftrace_pid *fpid, *safe;
4656 mutex_lock(&ftrace_lock);
4657 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4658 struct pid *pid = fpid->pid;
4660 clear_ftrace_pid_task(pid);
4662 list_del(&fpid->list);
4666 ftrace_update_pid_func();
4667 ftrace_startup_enable(0);
4669 mutex_unlock(&ftrace_lock);
4672 static void *fpid_start(struct seq_file *m, loff_t *pos)
4674 mutex_lock(&ftrace_lock);
4676 if (list_empty(&ftrace_pids) && (!*pos))
4679 return seq_list_start(&ftrace_pids, *pos);
4682 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4687 return seq_list_next(v, &ftrace_pids, pos);
4690 static void fpid_stop(struct seq_file *m, void *p)
4692 mutex_unlock(&ftrace_lock);
4695 static int fpid_show(struct seq_file *m, void *v)
4697 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4699 if (v == (void *)1) {
4700 seq_printf(m, "no pid\n");
4704 if (fpid->pid == ftrace_swapper_pid)
4705 seq_printf(m, "swapper tasks\n");
4707 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4712 static const struct seq_operations ftrace_pid_sops = {
4713 .start = fpid_start,
4720 ftrace_pid_open(struct inode *inode, struct file *file)
4724 if ((file->f_mode & FMODE_WRITE) &&
4725 (file->f_flags & O_TRUNC))
4728 if (file->f_mode & FMODE_READ)
4729 ret = seq_open(file, &ftrace_pid_sops);
4735 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4736 size_t cnt, loff_t *ppos)
4742 if (cnt >= sizeof(buf))
4745 if (copy_from_user(&buf, ubuf, cnt))
4751 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4752 * to clean the filter quietly.
4754 tmp = strstrip(buf);
4755 if (strlen(tmp) == 0)
4758 ret = kstrtol(tmp, 10, &val);
4762 ret = ftrace_pid_add(val);
4764 return ret ? ret : cnt;
4768 ftrace_pid_release(struct inode *inode, struct file *file)
4770 if (file->f_mode & FMODE_READ)
4771 seq_release(inode, file);
4776 static const struct file_operations ftrace_pid_fops = {
4777 .open = ftrace_pid_open,
4778 .write = ftrace_pid_write,
4780 .llseek = tracing_lseek,
4781 .release = ftrace_pid_release,
4784 static __init int ftrace_init_debugfs(void)
4786 struct dentry *d_tracer;
4788 d_tracer = tracing_init_dentry();
4792 ftrace_init_dyn_debugfs(d_tracer);
4794 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4795 NULL, &ftrace_pid_fops);
4797 ftrace_profile_debugfs(d_tracer);
4801 fs_initcall(ftrace_init_debugfs);
4804 * ftrace_kill - kill ftrace
4806 * This function should be used by panic code. It stops ftrace
4807 * but in a not so nice way. If you need to simply kill ftrace
4808 * from a non-atomic section, use ftrace_kill.
4810 void ftrace_kill(void)
4812 ftrace_disabled = 1;
4814 clear_ftrace_function();
4818 * Test if ftrace is dead or not.
4820 int ftrace_is_dead(void)
4822 return ftrace_disabled;
4826 * register_ftrace_function - register a function for profiling
4827 * @ops - ops structure that holds the function for profiling.
4829 * Register a function to be called by all functions in the
4832 * Note: @ops->func and all the functions it calls must be labeled
4833 * with "notrace", otherwise it will go into a
4836 int register_ftrace_function(struct ftrace_ops *ops)
4840 ftrace_ops_init(ops);
4842 mutex_lock(&ftrace_lock);
4844 ret = ftrace_startup(ops, 0);
4846 mutex_unlock(&ftrace_lock);
4850 EXPORT_SYMBOL_GPL(register_ftrace_function);
4853 * unregister_ftrace_function - unregister a function for profiling.
4854 * @ops - ops structure that holds the function to unregister
4856 * Unregister a function that was added to be called by ftrace profiling.
4858 int unregister_ftrace_function(struct ftrace_ops *ops)
4862 mutex_lock(&ftrace_lock);
4863 ret = ftrace_shutdown(ops, 0);
4864 mutex_unlock(&ftrace_lock);
4868 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4871 ftrace_enable_sysctl(struct ctl_table *table, int write,
4872 void __user *buffer, size_t *lenp,
4877 mutex_lock(&ftrace_lock);
4879 if (unlikely(ftrace_disabled))
4882 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4884 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4887 last_ftrace_enabled = !!ftrace_enabled;
4889 if (ftrace_enabled) {
4891 ftrace_startup_sysctl();
4893 /* we are starting ftrace again */
4894 if (ftrace_ops_list != &ftrace_list_end)
4895 update_ftrace_function();
4898 /* stopping ftrace calls (just send to ftrace_stub) */
4899 ftrace_trace_function = ftrace_stub;
4901 ftrace_shutdown_sysctl();
4905 mutex_unlock(&ftrace_lock);
4909 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4911 static int ftrace_graph_active;
4912 static struct notifier_block ftrace_suspend_notifier;
4914 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4919 /* The callbacks that hook a function */
4920 trace_func_graph_ret_t ftrace_graph_return =
4921 (trace_func_graph_ret_t)ftrace_stub;
4922 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4923 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
4925 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4926 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4930 unsigned long flags;
4931 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4932 struct task_struct *g, *t;
4934 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4935 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4936 * sizeof(struct ftrace_ret_stack),
4938 if (!ret_stack_list[i]) {
4946 read_lock_irqsave(&tasklist_lock, flags);
4947 do_each_thread(g, t) {
4953 if (t->ret_stack == NULL) {
4954 atomic_set(&t->tracing_graph_pause, 0);
4955 atomic_set(&t->trace_overrun, 0);
4956 t->curr_ret_stack = -1;
4957 /* Make sure the tasks see the -1 first: */
4959 t->ret_stack = ret_stack_list[start++];
4961 } while_each_thread(g, t);
4964 read_unlock_irqrestore(&tasklist_lock, flags);
4966 for (i = start; i < end; i++)
4967 kfree(ret_stack_list[i]);
4972 ftrace_graph_probe_sched_switch(void *ignore,
4973 struct task_struct *prev, struct task_struct *next)
4975 unsigned long long timestamp;
4979 * Does the user want to count the time a function was asleep.
4980 * If so, do not update the time stamps.
4982 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4985 timestamp = trace_clock_local();
4987 prev->ftrace_timestamp = timestamp;
4989 /* only process tasks that we timestamped */
4990 if (!next->ftrace_timestamp)
4994 * Update all the counters in next to make up for the
4995 * time next was sleeping.
4997 timestamp -= next->ftrace_timestamp;
4999 for (index = next->curr_ret_stack; index >= 0; index--)
5000 next->ret_stack[index].calltime += timestamp;
5003 /* Allocate a return stack for each task */
5004 static int start_graph_tracing(void)
5006 struct ftrace_ret_stack **ret_stack_list;
5009 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5010 sizeof(struct ftrace_ret_stack *),
5013 if (!ret_stack_list)
5016 /* The cpu_boot init_task->ret_stack will never be freed */
5017 for_each_online_cpu(cpu) {
5018 if (!idle_task(cpu)->ret_stack)
5019 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5023 ret = alloc_retstack_tasklist(ret_stack_list);
5024 } while (ret == -EAGAIN);
5027 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5029 pr_info("ftrace_graph: Couldn't activate tracepoint"
5030 " probe to kernel_sched_switch\n");
5033 kfree(ret_stack_list);
5038 * Hibernation protection.
5039 * The state of the current task is too much unstable during
5040 * suspend/restore to disk. We want to protect against that.
5043 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5047 case PM_HIBERNATION_PREPARE:
5048 pause_graph_tracing();
5051 case PM_POST_HIBERNATION:
5052 unpause_graph_tracing();
5058 /* Just a place holder for function graph */
5059 static struct ftrace_ops fgraph_ops __read_mostly = {
5060 .func = ftrace_stub,
5061 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
5062 FTRACE_OPS_FL_RECURSION_SAFE,
5065 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5067 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5069 return __ftrace_graph_entry(trace);
5073 * The function graph tracer should only trace the functions defined
5074 * by set_ftrace_filter and set_ftrace_notrace. If another function
5075 * tracer ops is registered, the graph tracer requires testing the
5076 * function against the global ops, and not just trace any function
5077 * that any ftrace_ops registered.
5079 static void update_function_graph_func(void)
5081 if (ftrace_ops_list == &ftrace_list_end ||
5082 (ftrace_ops_list == &global_ops &&
5083 global_ops.next == &ftrace_list_end))
5084 ftrace_graph_entry = __ftrace_graph_entry;
5086 ftrace_graph_entry = ftrace_graph_entry_test;
5089 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5090 trace_func_graph_ent_t entryfunc)
5094 mutex_lock(&ftrace_lock);
5096 /* we currently allow only one tracer registered at a time */
5097 if (ftrace_graph_active) {
5102 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5103 register_pm_notifier(&ftrace_suspend_notifier);
5105 ftrace_graph_active++;
5106 ret = start_graph_tracing();
5108 ftrace_graph_active--;
5112 ftrace_graph_return = retfunc;
5115 * Update the indirect function to the entryfunc, and the
5116 * function that gets called to the entry_test first. Then
5117 * call the update fgraph entry function to determine if
5118 * the entryfunc should be called directly or not.
5120 __ftrace_graph_entry = entryfunc;
5121 ftrace_graph_entry = ftrace_graph_entry_test;
5122 update_function_graph_func();
5124 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
5127 mutex_unlock(&ftrace_lock);
5131 void unregister_ftrace_graph(void)
5133 mutex_lock(&ftrace_lock);
5135 if (unlikely(!ftrace_graph_active))
5138 ftrace_graph_active--;
5139 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5140 ftrace_graph_entry = ftrace_graph_entry_stub;
5141 __ftrace_graph_entry = ftrace_graph_entry_stub;
5142 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
5143 unregister_pm_notifier(&ftrace_suspend_notifier);
5144 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5147 mutex_unlock(&ftrace_lock);
5150 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5153 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5155 atomic_set(&t->tracing_graph_pause, 0);
5156 atomic_set(&t->trace_overrun, 0);
5157 t->ftrace_timestamp = 0;
5158 /* make curr_ret_stack visible before we add the ret_stack */
5160 t->ret_stack = ret_stack;
5164 * Allocate a return stack for the idle task. May be the first
5165 * time through, or it may be done by CPU hotplug online.
5167 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5169 t->curr_ret_stack = -1;
5171 * The idle task has no parent, it either has its own
5172 * stack or no stack at all.
5175 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5177 if (ftrace_graph_active) {
5178 struct ftrace_ret_stack *ret_stack;
5180 ret_stack = per_cpu(idle_ret_stack, cpu);
5182 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5183 * sizeof(struct ftrace_ret_stack),
5187 per_cpu(idle_ret_stack, cpu) = ret_stack;
5189 graph_init_task(t, ret_stack);
5193 /* Allocate a return stack for newly created task */
5194 void ftrace_graph_init_task(struct task_struct *t)
5196 /* Make sure we do not use the parent ret_stack */
5197 t->ret_stack = NULL;
5198 t->curr_ret_stack = -1;
5200 if (ftrace_graph_active) {
5201 struct ftrace_ret_stack *ret_stack;
5203 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5204 * sizeof(struct ftrace_ret_stack),
5208 graph_init_task(t, ret_stack);
5212 void ftrace_graph_exit_task(struct task_struct *t)
5214 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5216 t->ret_stack = NULL;
5217 /* NULL must become visible to IRQs before we free it: */
5223 void ftrace_graph_stop(void)