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 update_global_ops(void)
242 ftrace_func_t func = ftrace_global_list_func;
243 void *private = NULL;
245 /* The list has its own recursion protection. */
246 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
249 * If there's only one function registered, then call that
250 * function directly. Otherwise, we need to iterate over the
251 * registered callers.
253 if (ftrace_global_list == &ftrace_list_end ||
254 ftrace_global_list->next == &ftrace_list_end) {
255 func = ftrace_global_list->func;
256 private = ftrace_global_list->private;
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;
267 /* If we filter on pids, update to use the pid function */
268 if (!list_empty(&ftrace_pids)) {
269 set_ftrace_pid_function(func);
270 func = ftrace_pid_func;
273 global_ops.func = func;
274 global_ops.private = private;
277 static void ftrace_sync(struct work_struct *work)
280 * This function is just a stub to implement a hard force
281 * of synchronize_sched(). This requires synchronizing
282 * tasks even in userspace and idle.
284 * Yes, function tracing is rude.
288 static void ftrace_sync_ipi(void *data)
290 /* Probably not needed, but do it anyway */
294 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
295 static void update_function_graph_func(void);
297 static inline void update_function_graph_func(void) { }
300 static void update_ftrace_function(void)
307 * If we are at the end of the list and this ops is
308 * recursion safe and not dynamic and the arch supports passing ops,
309 * then have the mcount trampoline call the function directly.
311 if (ftrace_ops_list == &ftrace_list_end ||
312 (ftrace_ops_list->next == &ftrace_list_end &&
313 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
314 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
315 !FTRACE_FORCE_LIST_FUNC)) {
316 /* Set the ftrace_ops that the arch callback uses */
317 if (ftrace_ops_list == &global_ops)
318 set_function_trace_op = ftrace_global_list;
320 set_function_trace_op = ftrace_ops_list;
321 func = ftrace_ops_list->func;
323 /* Just use the default ftrace_ops */
324 set_function_trace_op = &ftrace_list_end;
325 func = ftrace_ops_list_func;
328 /* If there's no change, then do nothing more here */
329 if (ftrace_trace_function == func)
332 update_function_graph_func();
335 * If we are using the list function, it doesn't care
336 * about the function_trace_ops.
338 if (func == ftrace_ops_list_func) {
339 ftrace_trace_function = func;
341 * Don't even bother setting function_trace_ops,
342 * it would be racy to do so anyway.
347 #ifndef CONFIG_DYNAMIC_FTRACE
349 * For static tracing, we need to be a bit more careful.
350 * The function change takes affect immediately. Thus,
351 * we need to coorditate the setting of the function_trace_ops
352 * with the setting of the ftrace_trace_function.
354 * Set the function to the list ops, which will call the
355 * function we want, albeit indirectly, but it handles the
356 * ftrace_ops and doesn't depend on function_trace_op.
358 ftrace_trace_function = ftrace_ops_list_func;
360 * Make sure all CPUs see this. Yes this is slow, but static
361 * tracing is slow and nasty to have enabled.
363 schedule_on_each_cpu(ftrace_sync);
364 /* Now all cpus are using the list ops. */
365 function_trace_op = set_function_trace_op;
366 /* Make sure the function_trace_op is visible on all CPUs */
368 /* Nasty way to force a rmb on all cpus */
369 smp_call_function(ftrace_sync_ipi, NULL, 1);
370 /* OK, we are all set to update the ftrace_trace_function now! */
371 #endif /* !CONFIG_DYNAMIC_FTRACE */
373 ftrace_trace_function = func;
376 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
380 * We are entering ops into the list but another
381 * CPU might be walking that list. We need to make sure
382 * the ops->next pointer is valid before another CPU sees
383 * the ops pointer included into the list.
385 rcu_assign_pointer(*list, ops);
388 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
390 struct ftrace_ops **p;
393 * If we are removing the last function, then simply point
394 * to the ftrace_stub.
396 if (*list == ops && ops->next == &ftrace_list_end) {
397 *list = &ftrace_list_end;
401 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
412 static void add_ftrace_list_ops(struct ftrace_ops **list,
413 struct ftrace_ops *main_ops,
414 struct ftrace_ops *ops)
416 int first = *list == &ftrace_list_end;
417 add_ftrace_ops(list, ops);
419 add_ftrace_ops(&ftrace_ops_list, main_ops);
422 static int remove_ftrace_list_ops(struct ftrace_ops **list,
423 struct ftrace_ops *main_ops,
424 struct ftrace_ops *ops)
426 int ret = remove_ftrace_ops(list, ops);
427 if (!ret && *list == &ftrace_list_end)
428 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
432 static int __register_ftrace_function(struct ftrace_ops *ops)
434 if (ops->flags & FTRACE_OPS_FL_DELETED)
437 if (FTRACE_WARN_ON(ops == &global_ops))
440 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
443 /* We don't support both control and global flags set. */
444 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
447 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
449 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
450 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
451 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
453 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
454 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
457 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
458 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
461 if (!core_kernel_data((unsigned long)ops))
462 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
464 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
465 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
466 ops->flags |= FTRACE_OPS_FL_ENABLED;
467 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
468 if (control_ops_alloc(ops))
470 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
472 add_ftrace_ops(&ftrace_ops_list, ops);
475 update_ftrace_function();
480 static int __unregister_ftrace_function(struct ftrace_ops *ops)
484 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
487 if (FTRACE_WARN_ON(ops == &global_ops))
490 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
491 ret = remove_ftrace_list_ops(&ftrace_global_list,
494 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
495 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
496 ret = remove_ftrace_list_ops(&ftrace_control_list,
499 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
505 update_ftrace_function();
510 static void ftrace_update_pid_func(void)
512 /* Only do something if we are tracing something */
513 if (ftrace_trace_function == ftrace_stub)
516 update_ftrace_function();
519 #ifdef CONFIG_FUNCTION_PROFILER
520 struct ftrace_profile {
521 struct hlist_node node;
523 unsigned long counter;
524 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
525 unsigned long long time;
526 unsigned long long time_squared;
530 struct ftrace_profile_page {
531 struct ftrace_profile_page *next;
533 struct ftrace_profile records[];
536 struct ftrace_profile_stat {
538 struct hlist_head *hash;
539 struct ftrace_profile_page *pages;
540 struct ftrace_profile_page *start;
541 struct tracer_stat stat;
544 #define PROFILE_RECORDS_SIZE \
545 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
547 #define PROFILES_PER_PAGE \
548 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
550 static int ftrace_profile_enabled __read_mostly;
552 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
553 static DEFINE_MUTEX(ftrace_profile_lock);
555 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
557 #define FTRACE_PROFILE_HASH_BITS 10
558 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
561 function_stat_next(void *v, int idx)
563 struct ftrace_profile *rec = v;
564 struct ftrace_profile_page *pg;
566 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
572 if ((void *)rec >= (void *)&pg->records[pg->index]) {
576 rec = &pg->records[0];
584 static void *function_stat_start(struct tracer_stat *trace)
586 struct ftrace_profile_stat *stat =
587 container_of(trace, struct ftrace_profile_stat, stat);
589 if (!stat || !stat->start)
592 return function_stat_next(&stat->start->records[0], 0);
595 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
596 /* function graph compares on total time */
597 static int function_stat_cmp(void *p1, void *p2)
599 struct ftrace_profile *a = p1;
600 struct ftrace_profile *b = p2;
602 if (a->time < b->time)
604 if (a->time > b->time)
610 /* not function graph compares against hits */
611 static int function_stat_cmp(void *p1, void *p2)
613 struct ftrace_profile *a = p1;
614 struct ftrace_profile *b = p2;
616 if (a->counter < b->counter)
618 if (a->counter > b->counter)
625 static int function_stat_headers(struct seq_file *m)
627 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
628 seq_printf(m, " Function "
631 "--- ---- --- ---\n");
633 seq_printf(m, " Function Hit\n"
639 static int function_stat_show(struct seq_file *m, void *v)
641 struct ftrace_profile *rec = v;
642 char str[KSYM_SYMBOL_LEN];
644 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
645 static struct trace_seq s;
646 unsigned long long avg;
647 unsigned long long stddev;
649 mutex_lock(&ftrace_profile_lock);
651 /* we raced with function_profile_reset() */
652 if (unlikely(rec->counter == 0)) {
657 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
658 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
660 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
663 do_div(avg, rec->counter);
665 /* Sample standard deviation (s^2) */
666 if (rec->counter <= 1)
670 * Apply Welford's method:
671 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
673 stddev = rec->counter * rec->time_squared -
674 rec->time * rec->time;
677 * Divide only 1000 for ns^2 -> us^2 conversion.
678 * trace_print_graph_duration will divide 1000 again.
680 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
684 trace_print_graph_duration(rec->time, &s);
685 trace_seq_puts(&s, " ");
686 trace_print_graph_duration(avg, &s);
687 trace_seq_puts(&s, " ");
688 trace_print_graph_duration(stddev, &s);
689 trace_print_seq(m, &s);
693 mutex_unlock(&ftrace_profile_lock);
698 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
700 struct ftrace_profile_page *pg;
702 pg = stat->pages = stat->start;
705 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
710 memset(stat->hash, 0,
711 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
714 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
716 struct ftrace_profile_page *pg;
721 /* If we already allocated, do nothing */
725 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
729 #ifdef CONFIG_DYNAMIC_FTRACE
730 functions = ftrace_update_tot_cnt;
733 * We do not know the number of functions that exist because
734 * dynamic tracing is what counts them. With past experience
735 * we have around 20K functions. That should be more than enough.
736 * It is highly unlikely we will execute every function in
742 pg = stat->start = stat->pages;
744 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
746 for (i = 1; i < pages; i++) {
747 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
758 unsigned long tmp = (unsigned long)pg;
770 static int ftrace_profile_init_cpu(int cpu)
772 struct ftrace_profile_stat *stat;
775 stat = &per_cpu(ftrace_profile_stats, cpu);
778 /* If the profile is already created, simply reset it */
779 ftrace_profile_reset(stat);
784 * We are profiling all functions, but usually only a few thousand
785 * functions are hit. We'll make a hash of 1024 items.
787 size = FTRACE_PROFILE_HASH_SIZE;
789 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
794 /* Preallocate the function profiling pages */
795 if (ftrace_profile_pages_init(stat) < 0) {
804 static int ftrace_profile_init(void)
809 for_each_possible_cpu(cpu) {
810 ret = ftrace_profile_init_cpu(cpu);
818 /* interrupts must be disabled */
819 static struct ftrace_profile *
820 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
822 struct ftrace_profile *rec;
823 struct hlist_head *hhd;
826 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
827 hhd = &stat->hash[key];
829 if (hlist_empty(hhd))
832 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
840 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
841 struct ftrace_profile *rec)
845 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
846 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
850 * The memory is already allocated, this simply finds a new record to use.
852 static struct ftrace_profile *
853 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
855 struct ftrace_profile *rec = NULL;
857 /* prevent recursion (from NMIs) */
858 if (atomic_inc_return(&stat->disabled) != 1)
862 * Try to find the function again since an NMI
863 * could have added it
865 rec = ftrace_find_profiled_func(stat, ip);
869 if (stat->pages->index == PROFILES_PER_PAGE) {
870 if (!stat->pages->next)
872 stat->pages = stat->pages->next;
875 rec = &stat->pages->records[stat->pages->index++];
877 ftrace_add_profile(stat, rec);
880 atomic_dec(&stat->disabled);
886 function_profile_call(unsigned long ip, unsigned long parent_ip,
887 struct ftrace_ops *ops, struct pt_regs *regs)
889 struct ftrace_profile_stat *stat;
890 struct ftrace_profile *rec;
893 if (!ftrace_profile_enabled)
896 local_irq_save(flags);
898 stat = &__get_cpu_var(ftrace_profile_stats);
899 if (!stat->hash || !ftrace_profile_enabled)
902 rec = ftrace_find_profiled_func(stat, ip);
904 rec = ftrace_profile_alloc(stat, ip);
911 local_irq_restore(flags);
914 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
915 static int profile_graph_entry(struct ftrace_graph_ent *trace)
917 function_profile_call(trace->func, 0, NULL, NULL);
921 static void profile_graph_return(struct ftrace_graph_ret *trace)
923 struct ftrace_profile_stat *stat;
924 unsigned long long calltime;
925 struct ftrace_profile *rec;
928 local_irq_save(flags);
929 stat = &__get_cpu_var(ftrace_profile_stats);
930 if (!stat->hash || !ftrace_profile_enabled)
933 /* If the calltime was zero'd ignore it */
934 if (!trace->calltime)
937 calltime = trace->rettime - trace->calltime;
939 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
942 index = trace->depth;
944 /* Append this call time to the parent time to subtract */
946 current->ret_stack[index - 1].subtime += calltime;
948 if (current->ret_stack[index].subtime < calltime)
949 calltime -= current->ret_stack[index].subtime;
954 rec = ftrace_find_profiled_func(stat, trace->func);
956 rec->time += calltime;
957 rec->time_squared += calltime * calltime;
961 local_irq_restore(flags);
964 static int register_ftrace_profiler(void)
966 return register_ftrace_graph(&profile_graph_return,
967 &profile_graph_entry);
970 static void unregister_ftrace_profiler(void)
972 unregister_ftrace_graph();
975 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
976 .func = function_profile_call,
977 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
978 INIT_REGEX_LOCK(ftrace_profile_ops)
981 static int register_ftrace_profiler(void)
983 return register_ftrace_function(&ftrace_profile_ops);
986 static void unregister_ftrace_profiler(void)
988 unregister_ftrace_function(&ftrace_profile_ops);
990 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
993 ftrace_profile_write(struct file *filp, const char __user *ubuf,
994 size_t cnt, loff_t *ppos)
999 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1005 mutex_lock(&ftrace_profile_lock);
1006 if (ftrace_profile_enabled ^ val) {
1008 ret = ftrace_profile_init();
1014 ret = register_ftrace_profiler();
1019 ftrace_profile_enabled = 1;
1021 ftrace_profile_enabled = 0;
1023 * unregister_ftrace_profiler calls stop_machine
1024 * so this acts like an synchronize_sched.
1026 unregister_ftrace_profiler();
1030 mutex_unlock(&ftrace_profile_lock);
1038 ftrace_profile_read(struct file *filp, char __user *ubuf,
1039 size_t cnt, loff_t *ppos)
1041 char buf[64]; /* big enough to hold a number */
1044 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1045 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1048 static const struct file_operations ftrace_profile_fops = {
1049 .open = tracing_open_generic,
1050 .read = ftrace_profile_read,
1051 .write = ftrace_profile_write,
1052 .llseek = default_llseek,
1055 /* used to initialize the real stat files */
1056 static struct tracer_stat function_stats __initdata = {
1057 .name = "functions",
1058 .stat_start = function_stat_start,
1059 .stat_next = function_stat_next,
1060 .stat_cmp = function_stat_cmp,
1061 .stat_headers = function_stat_headers,
1062 .stat_show = function_stat_show
1065 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1067 struct ftrace_profile_stat *stat;
1068 struct dentry *entry;
1073 for_each_possible_cpu(cpu) {
1074 stat = &per_cpu(ftrace_profile_stats, cpu);
1076 /* allocate enough for function name + cpu number */
1077 name = kmalloc(32, GFP_KERNEL);
1080 * The files created are permanent, if something happens
1081 * we still do not free memory.
1084 "Could not allocate stat file for cpu %d\n",
1088 stat->stat = function_stats;
1089 snprintf(name, 32, "function%d", cpu);
1090 stat->stat.name = name;
1091 ret = register_stat_tracer(&stat->stat);
1094 "Could not register function stat for cpu %d\n",
1101 entry = debugfs_create_file("function_profile_enabled", 0644,
1102 d_tracer, NULL, &ftrace_profile_fops);
1104 pr_warning("Could not create debugfs "
1105 "'function_profile_enabled' entry\n");
1108 #else /* CONFIG_FUNCTION_PROFILER */
1109 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1112 #endif /* CONFIG_FUNCTION_PROFILER */
1114 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1116 #ifdef CONFIG_DYNAMIC_FTRACE
1118 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1119 # error Dynamic ftrace depends on MCOUNT_RECORD
1122 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1124 struct ftrace_func_probe {
1125 struct hlist_node node;
1126 struct ftrace_probe_ops *ops;
1127 unsigned long flags;
1130 struct list_head free_list;
1133 struct ftrace_func_entry {
1134 struct hlist_node hlist;
1138 struct ftrace_hash {
1139 unsigned long size_bits;
1140 struct hlist_head *buckets;
1141 unsigned long count;
1142 struct rcu_head rcu;
1146 * We make these constant because no one should touch them,
1147 * but they are used as the default "empty hash", to avoid allocating
1148 * it all the time. These are in a read only section such that if
1149 * anyone does try to modify it, it will cause an exception.
1151 static const struct hlist_head empty_buckets[1];
1152 static const struct ftrace_hash empty_hash = {
1153 .buckets = (struct hlist_head *)empty_buckets,
1155 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1157 static struct ftrace_ops global_ops = {
1158 .func = ftrace_stub,
1159 .notrace_hash = EMPTY_HASH,
1160 .filter_hash = EMPTY_HASH,
1161 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1162 INIT_REGEX_LOCK(global_ops)
1165 struct ftrace_page {
1166 struct ftrace_page *next;
1167 struct dyn_ftrace *records;
1172 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1173 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1175 /* estimate from running different kernels */
1176 #define NR_TO_INIT 10000
1178 static struct ftrace_page *ftrace_pages_start;
1179 static struct ftrace_page *ftrace_pages;
1181 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1183 return !hash || !hash->count;
1186 static struct ftrace_func_entry *
1187 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1190 struct ftrace_func_entry *entry;
1191 struct hlist_head *hhd;
1193 if (ftrace_hash_empty(hash))
1196 if (hash->size_bits > 0)
1197 key = hash_long(ip, hash->size_bits);
1201 hhd = &hash->buckets[key];
1203 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1204 if (entry->ip == ip)
1210 static void __add_hash_entry(struct ftrace_hash *hash,
1211 struct ftrace_func_entry *entry)
1213 struct hlist_head *hhd;
1216 if (hash->size_bits)
1217 key = hash_long(entry->ip, hash->size_bits);
1221 hhd = &hash->buckets[key];
1222 hlist_add_head(&entry->hlist, hhd);
1226 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1228 struct ftrace_func_entry *entry;
1230 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1235 __add_hash_entry(hash, entry);
1241 free_hash_entry(struct ftrace_hash *hash,
1242 struct ftrace_func_entry *entry)
1244 hlist_del(&entry->hlist);
1250 remove_hash_entry(struct ftrace_hash *hash,
1251 struct ftrace_func_entry *entry)
1253 hlist_del(&entry->hlist);
1257 static void ftrace_hash_clear(struct ftrace_hash *hash)
1259 struct hlist_head *hhd;
1260 struct hlist_node *tn;
1261 struct ftrace_func_entry *entry;
1262 int size = 1 << hash->size_bits;
1268 for (i = 0; i < size; i++) {
1269 hhd = &hash->buckets[i];
1270 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1271 free_hash_entry(hash, entry);
1273 FTRACE_WARN_ON(hash->count);
1276 static void free_ftrace_hash(struct ftrace_hash *hash)
1278 if (!hash || hash == EMPTY_HASH)
1280 ftrace_hash_clear(hash);
1281 kfree(hash->buckets);
1285 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1287 struct ftrace_hash *hash;
1289 hash = container_of(rcu, struct ftrace_hash, rcu);
1290 free_ftrace_hash(hash);
1293 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1295 if (!hash || hash == EMPTY_HASH)
1297 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1300 void ftrace_free_filter(struct ftrace_ops *ops)
1302 ftrace_ops_init(ops);
1303 free_ftrace_hash(ops->filter_hash);
1304 free_ftrace_hash(ops->notrace_hash);
1307 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1309 struct ftrace_hash *hash;
1312 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1316 size = 1 << size_bits;
1317 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1319 if (!hash->buckets) {
1324 hash->size_bits = size_bits;
1329 static struct ftrace_hash *
1330 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1332 struct ftrace_func_entry *entry;
1333 struct ftrace_hash *new_hash;
1338 new_hash = alloc_ftrace_hash(size_bits);
1343 if (ftrace_hash_empty(hash))
1346 size = 1 << hash->size_bits;
1347 for (i = 0; i < size; i++) {
1348 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1349 ret = add_hash_entry(new_hash, entry->ip);
1355 FTRACE_WARN_ON(new_hash->count != hash->count);
1360 free_ftrace_hash(new_hash);
1365 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1367 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1370 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1371 struct ftrace_hash **dst, struct ftrace_hash *src)
1373 struct ftrace_func_entry *entry;
1374 struct hlist_node *tn;
1375 struct hlist_head *hhd;
1376 struct ftrace_hash *old_hash;
1377 struct ftrace_hash *new_hash;
1378 int size = src->count;
1384 * Remove the current set, update the hash and add
1387 ftrace_hash_rec_disable(ops, enable);
1390 * If the new source is empty, just free dst and assign it
1394 free_ftrace_hash_rcu(*dst);
1395 rcu_assign_pointer(*dst, EMPTY_HASH);
1396 /* still need to update the function records */
1402 * Make the hash size about 1/2 the # found
1404 for (size /= 2; size; size >>= 1)
1407 /* Don't allocate too much */
1408 if (bits > FTRACE_HASH_MAX_BITS)
1409 bits = FTRACE_HASH_MAX_BITS;
1412 new_hash = alloc_ftrace_hash(bits);
1416 size = 1 << src->size_bits;
1417 for (i = 0; i < size; i++) {
1418 hhd = &src->buckets[i];
1419 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1420 remove_hash_entry(src, entry);
1421 __add_hash_entry(new_hash, entry);
1426 rcu_assign_pointer(*dst, new_hash);
1427 free_ftrace_hash_rcu(old_hash);
1432 * Enable regardless of ret:
1433 * On success, we enable the new hash.
1434 * On failure, we re-enable the original hash.
1436 ftrace_hash_rec_enable(ops, enable);
1442 * Test the hashes for this ops to see if we want to call
1443 * the ops->func or not.
1445 * It's a match if the ip is in the ops->filter_hash or
1446 * the filter_hash does not exist or is empty,
1448 * the ip is not in the ops->notrace_hash.
1450 * This needs to be called with preemption disabled as
1451 * the hashes are freed with call_rcu_sched().
1454 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1456 struct ftrace_hash *filter_hash;
1457 struct ftrace_hash *notrace_hash;
1460 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1462 * There's a small race when adding ops that the ftrace handler
1463 * that wants regs, may be called without them. We can not
1464 * allow that handler to be called if regs is NULL.
1466 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1470 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1471 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1473 if ((ftrace_hash_empty(filter_hash) ||
1474 ftrace_lookup_ip(filter_hash, ip)) &&
1475 (ftrace_hash_empty(notrace_hash) ||
1476 !ftrace_lookup_ip(notrace_hash, ip)))
1485 * This is a double for. Do not use 'break' to break out of the loop,
1486 * you must use a goto.
1488 #define do_for_each_ftrace_rec(pg, rec) \
1489 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1491 for (_____i = 0; _____i < pg->index; _____i++) { \
1492 rec = &pg->records[_____i];
1494 #define while_for_each_ftrace_rec() \
1499 static int ftrace_cmp_recs(const void *a, const void *b)
1501 const struct dyn_ftrace *key = a;
1502 const struct dyn_ftrace *rec = b;
1504 if (key->flags < rec->ip)
1506 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1511 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1513 struct ftrace_page *pg;
1514 struct dyn_ftrace *rec;
1515 struct dyn_ftrace key;
1518 key.flags = end; /* overload flags, as it is unsigned long */
1520 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1521 if (end < pg->records[0].ip ||
1522 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1524 rec = bsearch(&key, pg->records, pg->index,
1525 sizeof(struct dyn_ftrace),
1535 * ftrace_location - return true if the ip giving is a traced location
1536 * @ip: the instruction pointer to check
1538 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1539 * That is, the instruction that is either a NOP or call to
1540 * the function tracer. It checks the ftrace internal tables to
1541 * determine if the address belongs or not.
1543 unsigned long ftrace_location(unsigned long ip)
1545 return ftrace_location_range(ip, ip);
1549 * ftrace_text_reserved - return true if range contains an ftrace location
1550 * @start: start of range to search
1551 * @end: end of range to search (inclusive). @end points to the last byte to check.
1553 * Returns 1 if @start and @end contains a ftrace location.
1554 * That is, the instruction that is either a NOP or call to
1555 * the function tracer. It checks the ftrace internal tables to
1556 * determine if the address belongs or not.
1558 int ftrace_text_reserved(const void *start, const void *end)
1562 ret = ftrace_location_range((unsigned long)start,
1563 (unsigned long)end);
1568 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1572 struct ftrace_hash *hash;
1573 struct ftrace_hash *other_hash;
1574 struct ftrace_page *pg;
1575 struct dyn_ftrace *rec;
1579 /* Only update if the ops has been registered */
1580 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1584 * In the filter_hash case:
1585 * If the count is zero, we update all records.
1586 * Otherwise we just update the items in the hash.
1588 * In the notrace_hash case:
1589 * We enable the update in the hash.
1590 * As disabling notrace means enabling the tracing,
1591 * and enabling notrace means disabling, the inc variable
1595 hash = ops->filter_hash;
1596 other_hash = ops->notrace_hash;
1597 if (ftrace_hash_empty(hash))
1601 hash = ops->notrace_hash;
1602 other_hash = ops->filter_hash;
1604 * If the notrace hash has no items,
1605 * then there's nothing to do.
1607 if (ftrace_hash_empty(hash))
1611 do_for_each_ftrace_rec(pg, rec) {
1612 int in_other_hash = 0;
1618 * Only the filter_hash affects all records.
1619 * Update if the record is not in the notrace hash.
1621 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1624 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1625 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1630 if (filter_hash && in_hash && !in_other_hash)
1632 else if (!filter_hash && in_hash &&
1633 (in_other_hash || ftrace_hash_empty(other_hash)))
1641 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1644 * If any ops wants regs saved for this function
1645 * then all ops will get saved regs.
1647 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1648 rec->flags |= FTRACE_FL_REGS;
1650 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1655 /* Shortcut, if we handled all records, we are done. */
1656 if (!all && count == hash->count)
1658 } while_for_each_ftrace_rec();
1661 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1664 __ftrace_hash_rec_update(ops, filter_hash, 0);
1667 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1670 __ftrace_hash_rec_update(ops, filter_hash, 1);
1673 static void print_ip_ins(const char *fmt, unsigned char *p)
1677 printk(KERN_CONT "%s", fmt);
1679 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1680 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1684 * ftrace_bug - report and shutdown function tracer
1685 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1686 * @ip: The address that failed
1688 * The arch code that enables or disables the function tracing
1689 * can call ftrace_bug() when it has detected a problem in
1690 * modifying the code. @failed should be one of either:
1691 * EFAULT - if the problem happens on reading the @ip address
1692 * EINVAL - if what is read at @ip is not what was expected
1693 * EPERM - if the problem happens on writting to the @ip address
1695 void ftrace_bug(int failed, unsigned long ip)
1699 FTRACE_WARN_ON_ONCE(1);
1700 pr_info("ftrace faulted on modifying ");
1704 FTRACE_WARN_ON_ONCE(1);
1705 pr_info("ftrace failed to modify ");
1707 print_ip_ins(" actual: ", (unsigned char *)ip);
1708 printk(KERN_CONT "\n");
1711 FTRACE_WARN_ON_ONCE(1);
1712 pr_info("ftrace faulted on writing ");
1716 FTRACE_WARN_ON_ONCE(1);
1717 pr_info("ftrace faulted on unknown error ");
1722 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1724 unsigned long flag = 0UL;
1727 * If we are updating calls:
1729 * If the record has a ref count, then we need to enable it
1730 * because someone is using it.
1732 * Otherwise we make sure its disabled.
1734 * If we are disabling calls, then disable all records that
1737 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1738 flag = FTRACE_FL_ENABLED;
1741 * If enabling and the REGS flag does not match the REGS_EN, then
1742 * do not ignore this record. Set flags to fail the compare against
1746 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1747 flag |= FTRACE_FL_REGS;
1749 /* If the state of this record hasn't changed, then do nothing */
1750 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1751 return FTRACE_UPDATE_IGNORE;
1754 /* Save off if rec is being enabled (for return value) */
1755 flag ^= rec->flags & FTRACE_FL_ENABLED;
1758 rec->flags |= FTRACE_FL_ENABLED;
1759 if (flag & FTRACE_FL_REGS) {
1760 if (rec->flags & FTRACE_FL_REGS)
1761 rec->flags |= FTRACE_FL_REGS_EN;
1763 rec->flags &= ~FTRACE_FL_REGS_EN;
1768 * If this record is being updated from a nop, then
1769 * return UPDATE_MAKE_CALL.
1770 * Otherwise, if the EN flag is set, then return
1771 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1772 * from the non-save regs, to a save regs function.
1774 * return UPDATE_MODIFY_CALL to tell the caller to convert
1775 * from the save regs, to a non-save regs function.
1777 if (flag & FTRACE_FL_ENABLED)
1778 return FTRACE_UPDATE_MAKE_CALL;
1779 else if (rec->flags & FTRACE_FL_REGS_EN)
1780 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1782 return FTRACE_UPDATE_MODIFY_CALL;
1786 /* If there's no more users, clear all flags */
1787 if (!(rec->flags & ~FTRACE_FL_MASK))
1790 /* Just disable the record (keep REGS state) */
1791 rec->flags &= ~FTRACE_FL_ENABLED;
1794 return FTRACE_UPDATE_MAKE_NOP;
1798 * ftrace_update_record, set a record that now is tracing or not
1799 * @rec: the record to update
1800 * @enable: set to 1 if the record is tracing, zero to force disable
1802 * The records that represent all functions that can be traced need
1803 * to be updated when tracing has been enabled.
1805 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1807 return ftrace_check_record(rec, enable, 1);
1811 * ftrace_test_record, check if the record has been enabled or not
1812 * @rec: the record to test
1813 * @enable: set to 1 to check if enabled, 0 if it is disabled
1815 * The arch code may need to test if a record is already set to
1816 * tracing to determine how to modify the function code that it
1819 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1821 return ftrace_check_record(rec, enable, 0);
1825 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1827 unsigned long ftrace_old_addr;
1828 unsigned long ftrace_addr;
1831 ret = ftrace_update_record(rec, enable);
1833 if (rec->flags & FTRACE_FL_REGS)
1834 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1836 ftrace_addr = (unsigned long)FTRACE_ADDR;
1839 case FTRACE_UPDATE_IGNORE:
1842 case FTRACE_UPDATE_MAKE_CALL:
1843 return ftrace_make_call(rec, ftrace_addr);
1845 case FTRACE_UPDATE_MAKE_NOP:
1846 return ftrace_make_nop(NULL, rec, ftrace_addr);
1848 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1849 case FTRACE_UPDATE_MODIFY_CALL:
1850 if (rec->flags & FTRACE_FL_REGS)
1851 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1853 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1855 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1858 return -1; /* unknow ftrace bug */
1861 void __weak ftrace_replace_code(int enable)
1863 struct dyn_ftrace *rec;
1864 struct ftrace_page *pg;
1867 if (unlikely(ftrace_disabled))
1870 do_for_each_ftrace_rec(pg, rec) {
1871 failed = __ftrace_replace_code(rec, enable);
1873 ftrace_bug(failed, rec->ip);
1874 /* Stop processing */
1877 } while_for_each_ftrace_rec();
1880 struct ftrace_rec_iter {
1881 struct ftrace_page *pg;
1886 * ftrace_rec_iter_start, start up iterating over traced functions
1888 * Returns an iterator handle that is used to iterate over all
1889 * the records that represent address locations where functions
1892 * May return NULL if no records are available.
1894 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1897 * We only use a single iterator.
1898 * Protected by the ftrace_lock mutex.
1900 static struct ftrace_rec_iter ftrace_rec_iter;
1901 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1903 iter->pg = ftrace_pages_start;
1906 /* Could have empty pages */
1907 while (iter->pg && !iter->pg->index)
1908 iter->pg = iter->pg->next;
1917 * ftrace_rec_iter_next, get the next record to process.
1918 * @iter: The handle to the iterator.
1920 * Returns the next iterator after the given iterator @iter.
1922 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1926 if (iter->index >= iter->pg->index) {
1927 iter->pg = iter->pg->next;
1930 /* Could have empty pages */
1931 while (iter->pg && !iter->pg->index)
1932 iter->pg = iter->pg->next;
1942 * ftrace_rec_iter_record, get the record at the iterator location
1943 * @iter: The current iterator location
1945 * Returns the record that the current @iter is at.
1947 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1949 return &iter->pg->records[iter->index];
1953 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1960 if (unlikely(ftrace_disabled))
1963 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1965 ftrace_bug(ret, ip);
1972 * archs can override this function if they must do something
1973 * before the modifying code is performed.
1975 int __weak ftrace_arch_code_modify_prepare(void)
1981 * archs can override this function if they must do something
1982 * after the modifying code is performed.
1984 int __weak ftrace_arch_code_modify_post_process(void)
1989 void ftrace_modify_all_code(int command)
1991 int update = command & FTRACE_UPDATE_TRACE_FUNC;
1995 * If the ftrace_caller calls a ftrace_ops func directly,
1996 * we need to make sure that it only traces functions it
1997 * expects to trace. When doing the switch of functions,
1998 * we need to update to the ftrace_ops_list_func first
1999 * before the transition between old and new calls are set,
2000 * as the ftrace_ops_list_func will check the ops hashes
2001 * to make sure the ops are having the right functions
2005 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2006 if (FTRACE_WARN_ON(err))
2010 if (command & FTRACE_UPDATE_CALLS)
2011 ftrace_replace_code(1);
2012 else if (command & FTRACE_DISABLE_CALLS)
2013 ftrace_replace_code(0);
2015 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2016 function_trace_op = set_function_trace_op;
2018 /* If irqs are disabled, we are in stop machine */
2019 if (!irqs_disabled())
2020 smp_call_function(ftrace_sync_ipi, NULL, 1);
2021 err = ftrace_update_ftrace_func(ftrace_trace_function);
2022 if (FTRACE_WARN_ON(err))
2026 if (command & FTRACE_START_FUNC_RET)
2027 err = ftrace_enable_ftrace_graph_caller();
2028 else if (command & FTRACE_STOP_FUNC_RET)
2029 err = ftrace_disable_ftrace_graph_caller();
2030 FTRACE_WARN_ON(err);
2033 static int __ftrace_modify_code(void *data)
2035 int *command = data;
2037 ftrace_modify_all_code(*command);
2043 * ftrace_run_stop_machine, go back to the stop machine method
2044 * @command: The command to tell ftrace what to do
2046 * If an arch needs to fall back to the stop machine method, the
2047 * it can call this function.
2049 void ftrace_run_stop_machine(int command)
2051 stop_machine(__ftrace_modify_code, &command, NULL);
2055 * arch_ftrace_update_code, modify the code to trace or not trace
2056 * @command: The command that needs to be done
2058 * Archs can override this function if it does not need to
2059 * run stop_machine() to modify code.
2061 void __weak arch_ftrace_update_code(int command)
2063 ftrace_run_stop_machine(command);
2066 static void ftrace_run_update_code(int command)
2070 ret = ftrace_arch_code_modify_prepare();
2071 FTRACE_WARN_ON(ret);
2075 * Do not call function tracer while we update the code.
2076 * We are in stop machine.
2078 function_trace_stop++;
2081 * By default we use stop_machine() to modify the code.
2082 * But archs can do what ever they want as long as it
2083 * is safe. The stop_machine() is the safest, but also
2084 * produces the most overhead.
2086 arch_ftrace_update_code(command);
2088 function_trace_stop--;
2090 ret = ftrace_arch_code_modify_post_process();
2091 FTRACE_WARN_ON(ret);
2094 static ftrace_func_t saved_ftrace_func;
2095 static int ftrace_start_up;
2096 static int global_start_up;
2098 static void control_ops_free(struct ftrace_ops *ops)
2100 free_percpu(ops->disabled);
2103 static void ftrace_startup_enable(int command)
2105 if (saved_ftrace_func != ftrace_trace_function) {
2106 saved_ftrace_func = ftrace_trace_function;
2107 command |= FTRACE_UPDATE_TRACE_FUNC;
2110 if (!command || !ftrace_enabled)
2113 ftrace_run_update_code(command);
2116 static int ftrace_startup(struct ftrace_ops *ops, int command)
2118 bool hash_enable = true;
2121 if (unlikely(ftrace_disabled))
2124 ret = __register_ftrace_function(ops);
2129 command |= FTRACE_UPDATE_CALLS;
2131 /* ops marked global share the filter hashes */
2132 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2134 /* Don't update hash if global is already set */
2135 if (global_start_up)
2136 hash_enable = false;
2140 ops->flags |= FTRACE_OPS_FL_ENABLED;
2142 ftrace_hash_rec_enable(ops, 1);
2144 ftrace_startup_enable(command);
2149 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2151 bool hash_disable = true;
2154 if (unlikely(ftrace_disabled))
2157 ret = __unregister_ftrace_function(ops);
2163 * Just warn in case of unbalance, no need to kill ftrace, it's not
2164 * critical but the ftrace_call callers may be never nopped again after
2165 * further ftrace uses.
2167 WARN_ON_ONCE(ftrace_start_up < 0);
2169 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2172 WARN_ON_ONCE(global_start_up < 0);
2173 /* Don't update hash if global still has users */
2174 if (global_start_up) {
2175 WARN_ON_ONCE(!ftrace_start_up);
2176 hash_disable = false;
2181 ftrace_hash_rec_disable(ops, 1);
2183 if (ops != &global_ops || !global_start_up)
2184 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2186 command |= FTRACE_UPDATE_CALLS;
2188 if (saved_ftrace_func != ftrace_trace_function) {
2189 saved_ftrace_func = ftrace_trace_function;
2190 command |= FTRACE_UPDATE_TRACE_FUNC;
2193 if (!command || !ftrace_enabled) {
2195 * If these are control ops, they still need their
2196 * per_cpu field freed. Since, function tracing is
2197 * not currently active, we can just free them
2198 * without synchronizing all CPUs.
2200 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2201 control_ops_free(ops);
2205 ftrace_run_update_code(command);
2208 * Dynamic ops may be freed, we must make sure that all
2209 * callers are done before leaving this function.
2210 * The same goes for freeing the per_cpu data of the control
2213 * Again, normal synchronize_sched() is not good enough.
2214 * We need to do a hard force of sched synchronization.
2215 * This is because we use preempt_disable() to do RCU, but
2216 * the function tracers can be called where RCU is not watching
2217 * (like before user_exit()). We can not rely on the RCU
2218 * infrastructure to do the synchronization, thus we must do it
2221 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2222 schedule_on_each_cpu(ftrace_sync);
2224 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2225 control_ops_free(ops);
2231 static void ftrace_startup_sysctl(void)
2233 if (unlikely(ftrace_disabled))
2236 /* Force update next time */
2237 saved_ftrace_func = NULL;
2238 /* ftrace_start_up is true if we want ftrace running */
2239 if (ftrace_start_up)
2240 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2243 static void ftrace_shutdown_sysctl(void)
2245 if (unlikely(ftrace_disabled))
2248 /* ftrace_start_up is true if ftrace is running */
2249 if (ftrace_start_up)
2250 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2253 static cycle_t ftrace_update_time;
2254 unsigned long ftrace_update_tot_cnt;
2256 static inline int ops_traces_mod(struct ftrace_ops *ops)
2259 * Filter_hash being empty will default to trace module.
2260 * But notrace hash requires a test of individual module functions.
2262 return ftrace_hash_empty(ops->filter_hash) &&
2263 ftrace_hash_empty(ops->notrace_hash);
2267 * Check if the current ops references the record.
2269 * If the ops traces all functions, then it was already accounted for.
2270 * If the ops does not trace the current record function, skip it.
2271 * If the ops ignores the function via notrace filter, skip it.
2274 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2276 /* If ops isn't enabled, ignore it */
2277 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2280 /* If ops traces all mods, we already accounted for it */
2281 if (ops_traces_mod(ops))
2284 /* The function must be in the filter */
2285 if (!ftrace_hash_empty(ops->filter_hash) &&
2286 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2289 /* If in notrace hash, we ignore it too */
2290 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2296 static int referenced_filters(struct dyn_ftrace *rec)
2298 struct ftrace_ops *ops;
2301 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2302 if (ops_references_rec(ops, rec))
2309 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2311 struct ftrace_page *pg;
2312 struct dyn_ftrace *p;
2313 cycle_t start, stop;
2314 unsigned long update_cnt = 0;
2315 unsigned long ref = 0;
2320 * When adding a module, we need to check if tracers are
2321 * currently enabled and if they are set to trace all functions.
2322 * If they are, we need to enable the module functions as well
2323 * as update the reference counts for those function records.
2326 struct ftrace_ops *ops;
2328 for (ops = ftrace_ops_list;
2329 ops != &ftrace_list_end; ops = ops->next) {
2330 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2331 if (ops_traces_mod(ops))
2339 start = ftrace_now(raw_smp_processor_id());
2341 for (pg = new_pgs; pg; pg = pg->next) {
2343 for (i = 0; i < pg->index; i++) {
2346 /* If something went wrong, bail without enabling anything */
2347 if (unlikely(ftrace_disabled))
2350 p = &pg->records[i];
2352 cnt += referenced_filters(p);
2356 * Do the initial record conversion from mcount jump
2357 * to the NOP instructions.
2359 if (!ftrace_code_disable(mod, p))
2365 * If the tracing is enabled, go ahead and enable the record.
2367 * The reason not to enable the record immediatelly is the
2368 * inherent check of ftrace_make_nop/ftrace_make_call for
2369 * correct previous instructions. Making first the NOP
2370 * conversion puts the module to the correct state, thus
2371 * passing the ftrace_make_call check.
2373 if (ftrace_start_up && cnt) {
2374 int failed = __ftrace_replace_code(p, 1);
2376 ftrace_bug(failed, p->ip);
2381 stop = ftrace_now(raw_smp_processor_id());
2382 ftrace_update_time = stop - start;
2383 ftrace_update_tot_cnt += update_cnt;
2388 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2393 if (WARN_ON(!count))
2396 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2399 * We want to fill as much as possible. No more than a page
2402 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2406 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2409 /* if we can't allocate this size, try something smaller */
2416 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2425 static struct ftrace_page *
2426 ftrace_allocate_pages(unsigned long num_to_init)
2428 struct ftrace_page *start_pg;
2429 struct ftrace_page *pg;
2436 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2441 * Try to allocate as much as possible in one continues
2442 * location that fills in all of the space. We want to
2443 * waste as little space as possible.
2446 cnt = ftrace_allocate_records(pg, num_to_init);
2454 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2465 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2466 free_pages((unsigned long)pg->records, order);
2467 start_pg = pg->next;
2471 pr_info("ftrace: FAILED to allocate memory for functions\n");
2475 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2477 struct ftrace_iterator {
2480 struct ftrace_page *pg;
2481 struct dyn_ftrace *func;
2482 struct ftrace_func_probe *probe;
2483 struct trace_parser parser;
2484 struct ftrace_hash *hash;
2485 struct ftrace_ops *ops;
2492 t_hash_next(struct seq_file *m, loff_t *pos)
2494 struct ftrace_iterator *iter = m->private;
2495 struct hlist_node *hnd = NULL;
2496 struct hlist_head *hhd;
2502 hnd = &iter->probe->node;
2504 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2507 hhd = &ftrace_func_hash[iter->hidx];
2509 if (hlist_empty(hhd)) {
2525 if (WARN_ON_ONCE(!hnd))
2528 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2533 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2535 struct ftrace_iterator *iter = m->private;
2539 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2542 if (iter->func_pos > *pos)
2546 for (l = 0; l <= (*pos - iter->func_pos); ) {
2547 p = t_hash_next(m, &l);
2554 /* Only set this if we have an item */
2555 iter->flags |= FTRACE_ITER_HASH;
2561 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2563 struct ftrace_func_probe *rec;
2566 if (WARN_ON_ONCE(!rec))
2569 if (rec->ops->print)
2570 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2572 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2575 seq_printf(m, ":%p", rec->data);
2582 t_next(struct seq_file *m, void *v, loff_t *pos)
2584 struct ftrace_iterator *iter = m->private;
2585 struct ftrace_ops *ops = iter->ops;
2586 struct dyn_ftrace *rec = NULL;
2588 if (unlikely(ftrace_disabled))
2591 if (iter->flags & FTRACE_ITER_HASH)
2592 return t_hash_next(m, pos);
2595 iter->pos = iter->func_pos = *pos;
2597 if (iter->flags & FTRACE_ITER_PRINTALL)
2598 return t_hash_start(m, pos);
2601 if (iter->idx >= iter->pg->index) {
2602 if (iter->pg->next) {
2603 iter->pg = iter->pg->next;
2608 rec = &iter->pg->records[iter->idx++];
2609 if (((iter->flags & FTRACE_ITER_FILTER) &&
2610 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2612 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2613 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2615 ((iter->flags & FTRACE_ITER_ENABLED) &&
2616 !(rec->flags & FTRACE_FL_ENABLED))) {
2624 return t_hash_start(m, pos);
2631 static void reset_iter_read(struct ftrace_iterator *iter)
2635 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2638 static void *t_start(struct seq_file *m, loff_t *pos)
2640 struct ftrace_iterator *iter = m->private;
2641 struct ftrace_ops *ops = iter->ops;
2645 mutex_lock(&ftrace_lock);
2647 if (unlikely(ftrace_disabled))
2651 * If an lseek was done, then reset and start from beginning.
2653 if (*pos < iter->pos)
2654 reset_iter_read(iter);
2657 * For set_ftrace_filter reading, if we have the filter
2658 * off, we can short cut and just print out that all
2659 * functions are enabled.
2661 if (iter->flags & FTRACE_ITER_FILTER &&
2662 ftrace_hash_empty(ops->filter_hash)) {
2664 return t_hash_start(m, pos);
2665 iter->flags |= FTRACE_ITER_PRINTALL;
2666 /* reset in case of seek/pread */
2667 iter->flags &= ~FTRACE_ITER_HASH;
2671 if (iter->flags & FTRACE_ITER_HASH)
2672 return t_hash_start(m, pos);
2675 * Unfortunately, we need to restart at ftrace_pages_start
2676 * every time we let go of the ftrace_mutex. This is because
2677 * those pointers can change without the lock.
2679 iter->pg = ftrace_pages_start;
2681 for (l = 0; l <= *pos; ) {
2682 p = t_next(m, p, &l);
2688 return t_hash_start(m, pos);
2693 static void t_stop(struct seq_file *m, void *p)
2695 mutex_unlock(&ftrace_lock);
2698 static int t_show(struct seq_file *m, void *v)
2700 struct ftrace_iterator *iter = m->private;
2701 struct dyn_ftrace *rec;
2703 if (iter->flags & FTRACE_ITER_HASH)
2704 return t_hash_show(m, iter);
2706 if (iter->flags & FTRACE_ITER_PRINTALL) {
2707 seq_printf(m, "#### all functions enabled ####\n");
2716 seq_printf(m, "%ps", (void *)rec->ip);
2717 if (iter->flags & FTRACE_ITER_ENABLED)
2718 seq_printf(m, " (%ld)%s",
2719 rec->flags & ~FTRACE_FL_MASK,
2720 rec->flags & FTRACE_FL_REGS ? " R" : "");
2721 seq_printf(m, "\n");
2726 static const struct seq_operations show_ftrace_seq_ops = {
2734 ftrace_avail_open(struct inode *inode, struct file *file)
2736 struct ftrace_iterator *iter;
2738 if (unlikely(ftrace_disabled))
2741 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2743 iter->pg = ftrace_pages_start;
2744 iter->ops = &global_ops;
2747 return iter ? 0 : -ENOMEM;
2751 ftrace_enabled_open(struct inode *inode, struct file *file)
2753 struct ftrace_iterator *iter;
2755 if (unlikely(ftrace_disabled))
2758 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2760 iter->pg = ftrace_pages_start;
2761 iter->flags = FTRACE_ITER_ENABLED;
2762 iter->ops = &global_ops;
2765 return iter ? 0 : -ENOMEM;
2768 static void ftrace_filter_reset(struct ftrace_hash *hash)
2770 mutex_lock(&ftrace_lock);
2771 ftrace_hash_clear(hash);
2772 mutex_unlock(&ftrace_lock);
2776 * ftrace_regex_open - initialize function tracer filter files
2777 * @ops: The ftrace_ops that hold the hash filters
2778 * @flag: The type of filter to process
2779 * @inode: The inode, usually passed in to your open routine
2780 * @file: The file, usually passed in to your open routine
2782 * ftrace_regex_open() initializes the filter files for the
2783 * @ops. Depending on @flag it may process the filter hash or
2784 * the notrace hash of @ops. With this called from the open
2785 * routine, you can use ftrace_filter_write() for the write
2786 * routine if @flag has FTRACE_ITER_FILTER set, or
2787 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2788 * tracing_lseek() should be used as the lseek routine, and
2789 * release must call ftrace_regex_release().
2792 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2793 struct inode *inode, struct file *file)
2795 struct ftrace_iterator *iter;
2796 struct ftrace_hash *hash;
2799 ftrace_ops_init(ops);
2801 if (unlikely(ftrace_disabled))
2804 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2808 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2816 mutex_lock(&ops->regex_lock);
2818 if (flag & FTRACE_ITER_NOTRACE)
2819 hash = ops->notrace_hash;
2821 hash = ops->filter_hash;
2823 if (file->f_mode & FMODE_WRITE) {
2824 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2826 trace_parser_put(&iter->parser);
2833 if ((file->f_mode & FMODE_WRITE) &&
2834 (file->f_flags & O_TRUNC))
2835 ftrace_filter_reset(iter->hash);
2837 if (file->f_mode & FMODE_READ) {
2838 iter->pg = ftrace_pages_start;
2840 ret = seq_open(file, &show_ftrace_seq_ops);
2842 struct seq_file *m = file->private_data;
2846 free_ftrace_hash(iter->hash);
2847 trace_parser_put(&iter->parser);
2851 file->private_data = iter;
2854 mutex_unlock(&ops->regex_lock);
2860 ftrace_filter_open(struct inode *inode, struct file *file)
2862 struct ftrace_ops *ops = inode->i_private;
2864 return ftrace_regex_open(ops,
2865 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2870 ftrace_notrace_open(struct inode *inode, struct file *file)
2872 struct ftrace_ops *ops = inode->i_private;
2874 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
2878 static int ftrace_match(char *str, char *regex, int len, int type)
2885 if (strcmp(str, regex) == 0)
2888 case MATCH_FRONT_ONLY:
2889 if (strncmp(str, regex, len) == 0)
2892 case MATCH_MIDDLE_ONLY:
2893 if (strstr(str, regex))
2896 case MATCH_END_ONLY:
2898 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2907 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2909 struct ftrace_func_entry *entry;
2912 entry = ftrace_lookup_ip(hash, rec->ip);
2914 /* Do nothing if it doesn't exist */
2918 free_hash_entry(hash, entry);
2920 /* Do nothing if it exists */
2924 ret = add_hash_entry(hash, rec->ip);
2930 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2931 char *regex, int len, int type)
2933 char str[KSYM_SYMBOL_LEN];
2936 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2939 /* module lookup requires matching the module */
2940 if (!modname || strcmp(modname, mod))
2943 /* blank search means to match all funcs in the mod */
2948 return ftrace_match(str, regex, len, type);
2952 match_records(struct ftrace_hash *hash, char *buff,
2953 int len, char *mod, int not)
2955 unsigned search_len = 0;
2956 struct ftrace_page *pg;
2957 struct dyn_ftrace *rec;
2958 int type = MATCH_FULL;
2959 char *search = buff;
2964 type = filter_parse_regex(buff, len, &search, ¬);
2965 search_len = strlen(search);
2968 mutex_lock(&ftrace_lock);
2970 if (unlikely(ftrace_disabled))
2973 do_for_each_ftrace_rec(pg, rec) {
2974 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2975 ret = enter_record(hash, rec, not);
2982 } while_for_each_ftrace_rec();
2984 mutex_unlock(&ftrace_lock);
2990 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2992 return match_records(hash, buff, len, NULL, 0);
2996 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
3000 /* blank or '*' mean the same */
3001 if (strcmp(buff, "*") == 0)
3004 /* handle the case of 'dont filter this module' */
3005 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3010 return match_records(hash, buff, strlen(buff), mod, not);
3014 * We register the module command as a template to show others how
3015 * to register the a command as well.
3019 ftrace_mod_callback(struct ftrace_hash *hash,
3020 char *func, char *cmd, char *param, int enable)
3026 * cmd == 'mod' because we only registered this func
3027 * for the 'mod' ftrace_func_command.
3028 * But if you register one func with multiple commands,
3029 * you can tell which command was used by the cmd
3033 /* we must have a module name */
3037 mod = strsep(¶m, ":");
3041 ret = ftrace_match_module_records(hash, func, mod);
3050 static struct ftrace_func_command ftrace_mod_cmd = {
3052 .func = ftrace_mod_callback,
3055 static int __init ftrace_mod_cmd_init(void)
3057 return register_ftrace_command(&ftrace_mod_cmd);
3059 core_initcall(ftrace_mod_cmd_init);
3061 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3062 struct ftrace_ops *op, struct pt_regs *pt_regs)
3064 struct ftrace_func_probe *entry;
3065 struct hlist_head *hhd;
3068 key = hash_long(ip, FTRACE_HASH_BITS);
3070 hhd = &ftrace_func_hash[key];
3072 if (hlist_empty(hhd))
3076 * Disable preemption for these calls to prevent a RCU grace
3077 * period. This syncs the hash iteration and freeing of items
3078 * on the hash. rcu_read_lock is too dangerous here.
3080 preempt_disable_notrace();
3081 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3082 if (entry->ip == ip)
3083 entry->ops->func(ip, parent_ip, &entry->data);
3085 preempt_enable_notrace();
3088 static struct ftrace_ops trace_probe_ops __read_mostly =
3090 .func = function_trace_probe_call,
3091 .flags = FTRACE_OPS_FL_INITIALIZED,
3092 INIT_REGEX_LOCK(trace_probe_ops)
3095 static int ftrace_probe_registered;
3097 static void __enable_ftrace_function_probe(void)
3102 if (ftrace_probe_registered) {
3103 /* still need to update the function call sites */
3105 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3109 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3110 struct hlist_head *hhd = &ftrace_func_hash[i];
3114 /* Nothing registered? */
3115 if (i == FTRACE_FUNC_HASHSIZE)
3118 ret = ftrace_startup(&trace_probe_ops, 0);
3120 ftrace_probe_registered = 1;
3123 static void __disable_ftrace_function_probe(void)
3127 if (!ftrace_probe_registered)
3130 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3131 struct hlist_head *hhd = &ftrace_func_hash[i];
3136 /* no more funcs left */
3137 ftrace_shutdown(&trace_probe_ops, 0);
3139 ftrace_probe_registered = 0;
3143 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3145 if (entry->ops->free)
3146 entry->ops->free(entry->ops, entry->ip, &entry->data);
3151 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3154 struct ftrace_func_probe *entry;
3155 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3156 struct ftrace_hash *hash;
3157 struct ftrace_page *pg;
3158 struct dyn_ftrace *rec;
3165 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3166 len = strlen(search);
3168 /* we do not support '!' for function probes */
3172 mutex_lock(&trace_probe_ops.regex_lock);
3174 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3180 if (unlikely(ftrace_disabled)) {
3185 mutex_lock(&ftrace_lock);
3187 do_for_each_ftrace_rec(pg, rec) {
3189 if (!ftrace_match_record(rec, NULL, search, len, type))
3192 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3194 /* If we did not process any, then return error */
3205 * The caller might want to do something special
3206 * for each function we find. We call the callback
3207 * to give the caller an opportunity to do so.
3210 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3211 /* caller does not like this func */
3217 ret = enter_record(hash, rec, 0);
3225 entry->ip = rec->ip;
3227 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3228 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3230 } while_for_each_ftrace_rec();
3232 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3236 __enable_ftrace_function_probe();
3239 mutex_unlock(&ftrace_lock);
3241 mutex_unlock(&trace_probe_ops.regex_lock);
3242 free_ftrace_hash(hash);
3248 PROBE_TEST_FUNC = 1,
3253 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3254 void *data, int flags)
3256 struct ftrace_func_entry *rec_entry;
3257 struct ftrace_func_probe *entry;
3258 struct ftrace_func_probe *p;
3259 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3260 struct list_head free_list;
3261 struct ftrace_hash *hash;
3262 struct hlist_node *tmp;
3263 char str[KSYM_SYMBOL_LEN];
3264 int type = MATCH_FULL;
3268 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3273 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3274 len = strlen(search);
3276 /* we do not support '!' for function probes */
3281 mutex_lock(&trace_probe_ops.regex_lock);
3283 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3285 /* Hmm, should report this somehow */
3288 INIT_LIST_HEAD(&free_list);
3290 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3291 struct hlist_head *hhd = &ftrace_func_hash[i];
3293 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3295 /* break up if statements for readability */
3296 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3299 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3302 /* do this last, since it is the most expensive */
3304 kallsyms_lookup(entry->ip, NULL, NULL,
3306 if (!ftrace_match(str, glob, len, type))
3310 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3311 /* It is possible more than one entry had this ip */
3313 free_hash_entry(hash, rec_entry);
3315 hlist_del_rcu(&entry->node);
3316 list_add(&entry->free_list, &free_list);
3319 mutex_lock(&ftrace_lock);
3320 __disable_ftrace_function_probe();
3322 * Remove after the disable is called. Otherwise, if the last
3323 * probe is removed, a null hash means *all enabled*.
3325 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3326 synchronize_sched();
3327 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3328 list_del(&entry->free_list);
3329 ftrace_free_entry(entry);
3331 mutex_unlock(&ftrace_lock);
3334 mutex_unlock(&trace_probe_ops.regex_lock);
3335 free_ftrace_hash(hash);
3339 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3342 __unregister_ftrace_function_probe(glob, ops, data,
3343 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3347 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3349 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3352 void unregister_ftrace_function_probe_all(char *glob)
3354 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3357 static LIST_HEAD(ftrace_commands);
3358 static DEFINE_MUTEX(ftrace_cmd_mutex);
3361 * Currently we only register ftrace commands from __init, so mark this
3364 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3366 struct ftrace_func_command *p;
3369 mutex_lock(&ftrace_cmd_mutex);
3370 list_for_each_entry(p, &ftrace_commands, list) {
3371 if (strcmp(cmd->name, p->name) == 0) {
3376 list_add(&cmd->list, &ftrace_commands);
3378 mutex_unlock(&ftrace_cmd_mutex);
3384 * Currently we only unregister ftrace commands from __init, so mark
3387 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3389 struct ftrace_func_command *p, *n;
3392 mutex_lock(&ftrace_cmd_mutex);
3393 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3394 if (strcmp(cmd->name, p->name) == 0) {
3396 list_del_init(&p->list);
3401 mutex_unlock(&ftrace_cmd_mutex);
3406 static int ftrace_process_regex(struct ftrace_hash *hash,
3407 char *buff, int len, int enable)
3409 char *func, *command, *next = buff;
3410 struct ftrace_func_command *p;
3413 func = strsep(&next, ":");
3416 ret = ftrace_match_records(hash, func, len);
3426 command = strsep(&next, ":");
3428 mutex_lock(&ftrace_cmd_mutex);
3429 list_for_each_entry(p, &ftrace_commands, list) {
3430 if (strcmp(p->name, command) == 0) {
3431 ret = p->func(hash, func, command, next, enable);
3436 mutex_unlock(&ftrace_cmd_mutex);
3442 ftrace_regex_write(struct file *file, const char __user *ubuf,
3443 size_t cnt, loff_t *ppos, int enable)
3445 struct ftrace_iterator *iter;
3446 struct trace_parser *parser;
3452 if (file->f_mode & FMODE_READ) {
3453 struct seq_file *m = file->private_data;
3456 iter = file->private_data;
3458 if (unlikely(ftrace_disabled))
3461 /* iter->hash is a local copy, so we don't need regex_lock */
3463 parser = &iter->parser;
3464 read = trace_get_user(parser, ubuf, cnt, ppos);
3466 if (read >= 0 && trace_parser_loaded(parser) &&
3467 !trace_parser_cont(parser)) {
3468 ret = ftrace_process_regex(iter->hash, parser->buffer,
3469 parser->idx, enable);
3470 trace_parser_clear(parser);
3481 ftrace_filter_write(struct file *file, const char __user *ubuf,
3482 size_t cnt, loff_t *ppos)
3484 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3488 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3489 size_t cnt, loff_t *ppos)
3491 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3495 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3497 struct ftrace_func_entry *entry;
3499 if (!ftrace_location(ip))
3503 entry = ftrace_lookup_ip(hash, ip);
3506 free_hash_entry(hash, entry);
3510 return add_hash_entry(hash, ip);
3513 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3515 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3516 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3520 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3521 unsigned long ip, int remove, int reset, int enable)
3523 struct ftrace_hash **orig_hash;
3524 struct ftrace_hash *hash;
3527 /* All global ops uses the global ops filters */
3528 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3531 if (unlikely(ftrace_disabled))
3534 mutex_lock(&ops->regex_lock);
3537 orig_hash = &ops->filter_hash;
3539 orig_hash = &ops->notrace_hash;
3541 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3544 goto out_regex_unlock;
3548 ftrace_filter_reset(hash);
3549 if (buf && !ftrace_match_records(hash, buf, len)) {
3551 goto out_regex_unlock;
3554 ret = ftrace_match_addr(hash, ip, remove);
3556 goto out_regex_unlock;
3559 mutex_lock(&ftrace_lock);
3560 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3562 ftrace_ops_update_code(ops);
3564 mutex_unlock(&ftrace_lock);
3567 mutex_unlock(&ops->regex_lock);
3569 free_ftrace_hash(hash);
3574 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3575 int reset, int enable)
3577 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3581 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3582 * @ops - the ops to set the filter with
3583 * @ip - the address to add to or remove from the filter.
3584 * @remove - non zero to remove the ip from the filter
3585 * @reset - non zero to reset all filters before applying this filter.
3587 * Filters denote which functions should be enabled when tracing is enabled
3588 * If @ip is NULL, it failes to update filter.
3590 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3591 int remove, int reset)
3593 ftrace_ops_init(ops);
3594 return ftrace_set_addr(ops, ip, remove, reset, 1);
3596 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3599 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3600 int reset, int enable)
3602 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3606 * ftrace_set_filter - set a function to filter on in ftrace
3607 * @ops - the ops to set the filter with
3608 * @buf - the string that holds the function filter text.
3609 * @len - the length of the string.
3610 * @reset - non zero to reset all filters before applying this filter.
3612 * Filters denote which functions should be enabled when tracing is enabled.
3613 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3615 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3618 ftrace_ops_init(ops);
3619 return ftrace_set_regex(ops, buf, len, reset, 1);
3621 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3624 * ftrace_set_notrace - set a function to not trace in ftrace
3625 * @ops - the ops to set the notrace filter with
3626 * @buf - the string that holds the function notrace text.
3627 * @len - the length of the string.
3628 * @reset - non zero to reset all filters before applying this filter.
3630 * Notrace Filters denote which functions should not be enabled when tracing
3631 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3634 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3637 ftrace_ops_init(ops);
3638 return ftrace_set_regex(ops, buf, len, reset, 0);
3640 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3642 * ftrace_set_filter - set a function to filter on in ftrace
3643 * @ops - the ops to set the filter with
3644 * @buf - the string that holds the function filter text.
3645 * @len - the length of the string.
3646 * @reset - non zero to reset all filters before applying this filter.
3648 * Filters denote which functions should be enabled when tracing is enabled.
3649 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3651 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3653 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3655 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3658 * ftrace_set_notrace - set a function to not trace in ftrace
3659 * @ops - the ops to set the notrace filter with
3660 * @buf - the string that holds the function notrace text.
3661 * @len - the length of the string.
3662 * @reset - non zero to reset all filters before applying this filter.
3664 * Notrace Filters denote which functions should not be enabled when tracing
3665 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3668 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3670 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3672 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3675 * command line interface to allow users to set filters on boot up.
3677 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3678 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3679 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3681 /* Used by function selftest to not test if filter is set */
3682 bool ftrace_filter_param __initdata;
3684 static int __init set_ftrace_notrace(char *str)
3686 ftrace_filter_param = true;
3687 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3690 __setup("ftrace_notrace=", set_ftrace_notrace);
3692 static int __init set_ftrace_filter(char *str)
3694 ftrace_filter_param = true;
3695 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3698 __setup("ftrace_filter=", set_ftrace_filter);
3700 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3701 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3702 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3704 static int __init set_graph_function(char *str)
3706 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3709 __setup("ftrace_graph_filter=", set_graph_function);
3711 static void __init set_ftrace_early_graph(char *buf)
3717 func = strsep(&buf, ",");
3718 /* we allow only one expression at a time */
3719 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3720 FTRACE_GRAPH_MAX_FUNCS, func);
3722 printk(KERN_DEBUG "ftrace: function %s not "
3723 "traceable\n", func);
3726 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3729 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3733 ftrace_ops_init(ops);
3736 func = strsep(&buf, ",");
3737 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3741 static void __init set_ftrace_early_filters(void)
3743 if (ftrace_filter_buf[0])
3744 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3745 if (ftrace_notrace_buf[0])
3746 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3747 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3748 if (ftrace_graph_buf[0])
3749 set_ftrace_early_graph(ftrace_graph_buf);
3750 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3753 int ftrace_regex_release(struct inode *inode, struct file *file)
3755 struct seq_file *m = (struct seq_file *)file->private_data;
3756 struct ftrace_iterator *iter;
3757 struct ftrace_hash **orig_hash;
3758 struct trace_parser *parser;
3762 if (file->f_mode & FMODE_READ) {
3764 seq_release(inode, file);
3766 iter = file->private_data;
3768 parser = &iter->parser;
3769 if (trace_parser_loaded(parser)) {
3770 parser->buffer[parser->idx] = 0;
3771 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3774 trace_parser_put(parser);
3776 mutex_lock(&iter->ops->regex_lock);
3778 if (file->f_mode & FMODE_WRITE) {
3779 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3782 orig_hash = &iter->ops->filter_hash;
3784 orig_hash = &iter->ops->notrace_hash;
3786 mutex_lock(&ftrace_lock);
3787 ret = ftrace_hash_move(iter->ops, filter_hash,
3788 orig_hash, iter->hash);
3790 ftrace_ops_update_code(iter->ops);
3792 mutex_unlock(&ftrace_lock);
3795 mutex_unlock(&iter->ops->regex_lock);
3796 free_ftrace_hash(iter->hash);
3802 static const struct file_operations ftrace_avail_fops = {
3803 .open = ftrace_avail_open,
3805 .llseek = seq_lseek,
3806 .release = seq_release_private,
3809 static const struct file_operations ftrace_enabled_fops = {
3810 .open = ftrace_enabled_open,
3812 .llseek = seq_lseek,
3813 .release = seq_release_private,
3816 static const struct file_operations ftrace_filter_fops = {
3817 .open = ftrace_filter_open,
3819 .write = ftrace_filter_write,
3820 .llseek = tracing_lseek,
3821 .release = ftrace_regex_release,
3824 static const struct file_operations ftrace_notrace_fops = {
3825 .open = ftrace_notrace_open,
3827 .write = ftrace_notrace_write,
3828 .llseek = tracing_lseek,
3829 .release = ftrace_regex_release,
3832 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3834 static DEFINE_MUTEX(graph_lock);
3836 int ftrace_graph_count;
3837 int ftrace_graph_notrace_count;
3838 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3839 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3841 struct ftrace_graph_data {
3842 unsigned long *table;
3845 const struct seq_operations *seq_ops;
3849 __g_next(struct seq_file *m, loff_t *pos)
3851 struct ftrace_graph_data *fgd = m->private;
3853 if (*pos >= *fgd->count)
3855 return &fgd->table[*pos];
3859 g_next(struct seq_file *m, void *v, loff_t *pos)
3862 return __g_next(m, pos);
3865 static void *g_start(struct seq_file *m, loff_t *pos)
3867 struct ftrace_graph_data *fgd = m->private;
3869 mutex_lock(&graph_lock);
3871 /* Nothing, tell g_show to print all functions are enabled */
3872 if (!*fgd->count && !*pos)
3875 return __g_next(m, pos);
3878 static void g_stop(struct seq_file *m, void *p)
3880 mutex_unlock(&graph_lock);
3883 static int g_show(struct seq_file *m, void *v)
3885 unsigned long *ptr = v;
3890 if (ptr == (unsigned long *)1) {
3891 seq_printf(m, "#### all functions enabled ####\n");
3895 seq_printf(m, "%ps\n", (void *)*ptr);
3900 static const struct seq_operations ftrace_graph_seq_ops = {
3908 __ftrace_graph_open(struct inode *inode, struct file *file,
3909 struct ftrace_graph_data *fgd)
3913 mutex_lock(&graph_lock);
3914 if ((file->f_mode & FMODE_WRITE) &&
3915 (file->f_flags & O_TRUNC)) {
3917 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
3919 mutex_unlock(&graph_lock);
3921 if (file->f_mode & FMODE_READ) {
3922 ret = seq_open(file, fgd->seq_ops);
3924 struct seq_file *m = file->private_data;
3928 file->private_data = fgd;
3934 ftrace_graph_open(struct inode *inode, struct file *file)
3936 struct ftrace_graph_data *fgd;
3938 if (unlikely(ftrace_disabled))
3941 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3945 fgd->table = ftrace_graph_funcs;
3946 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3947 fgd->count = &ftrace_graph_count;
3948 fgd->seq_ops = &ftrace_graph_seq_ops;
3950 return __ftrace_graph_open(inode, file, fgd);
3954 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3956 struct ftrace_graph_data *fgd;
3958 if (unlikely(ftrace_disabled))
3961 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3965 fgd->table = ftrace_graph_notrace_funcs;
3966 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3967 fgd->count = &ftrace_graph_notrace_count;
3968 fgd->seq_ops = &ftrace_graph_seq_ops;
3970 return __ftrace_graph_open(inode, file, fgd);
3974 ftrace_graph_release(struct inode *inode, struct file *file)
3976 if (file->f_mode & FMODE_READ) {
3977 struct seq_file *m = file->private_data;
3980 seq_release(inode, file);
3982 kfree(file->private_data);
3989 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
3991 struct dyn_ftrace *rec;
3992 struct ftrace_page *pg;
4001 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
4002 if (!not && *idx >= size)
4005 search_len = strlen(search);
4007 mutex_lock(&ftrace_lock);
4009 if (unlikely(ftrace_disabled)) {
4010 mutex_unlock(&ftrace_lock);
4014 do_for_each_ftrace_rec(pg, rec) {
4016 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4017 /* if it is in the array */
4019 for (i = 0; i < *idx; i++) {
4020 if (array[i] == rec->ip) {
4029 array[(*idx)++] = rec->ip;
4035 array[i] = array[--(*idx)];
4041 } while_for_each_ftrace_rec();
4043 mutex_unlock(&ftrace_lock);
4052 ftrace_graph_write(struct file *file, const char __user *ubuf,
4053 size_t cnt, loff_t *ppos)
4055 struct trace_parser parser;
4056 ssize_t read, ret = 0;
4057 struct ftrace_graph_data *fgd = file->private_data;
4062 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4065 read = trace_get_user(&parser, ubuf, cnt, ppos);
4067 if (read >= 0 && trace_parser_loaded((&parser))) {
4068 parser.buffer[parser.idx] = 0;
4070 mutex_lock(&graph_lock);
4072 /* we allow only one expression at a time */
4073 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4076 mutex_unlock(&graph_lock);
4082 trace_parser_put(&parser);
4087 static const struct file_operations ftrace_graph_fops = {
4088 .open = ftrace_graph_open,
4090 .write = ftrace_graph_write,
4091 .llseek = tracing_lseek,
4092 .release = ftrace_graph_release,
4095 static const struct file_operations ftrace_graph_notrace_fops = {
4096 .open = ftrace_graph_notrace_open,
4098 .write = ftrace_graph_write,
4099 .llseek = tracing_lseek,
4100 .release = ftrace_graph_release,
4102 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4104 void ftrace_create_filter_files(struct ftrace_ops *ops,
4105 struct dentry *parent)
4108 trace_create_file("set_ftrace_filter", 0644, parent,
4109 ops, &ftrace_filter_fops);
4111 trace_create_file("set_ftrace_notrace", 0644, parent,
4112 ops, &ftrace_notrace_fops);
4116 * The name "destroy_filter_files" is really a misnomer. Although
4117 * in the future, it may actualy delete the files, but this is
4118 * really intended to make sure the ops passed in are disabled
4119 * and that when this function returns, the caller is free to
4122 * The "destroy" name is only to match the "create" name that this
4123 * should be paired with.
4125 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4127 mutex_lock(&ftrace_lock);
4128 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4129 ftrace_shutdown(ops, 0);
4130 ops->flags |= FTRACE_OPS_FL_DELETED;
4131 mutex_unlock(&ftrace_lock);
4134 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4137 trace_create_file("available_filter_functions", 0444,
4138 d_tracer, NULL, &ftrace_avail_fops);
4140 trace_create_file("enabled_functions", 0444,
4141 d_tracer, NULL, &ftrace_enabled_fops);
4143 ftrace_create_filter_files(&global_ops, d_tracer);
4145 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4146 trace_create_file("set_graph_function", 0444, d_tracer,
4148 &ftrace_graph_fops);
4149 trace_create_file("set_graph_notrace", 0444, d_tracer,
4151 &ftrace_graph_notrace_fops);
4152 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4157 static int ftrace_cmp_ips(const void *a, const void *b)
4159 const unsigned long *ipa = a;
4160 const unsigned long *ipb = b;
4169 static void ftrace_swap_ips(void *a, void *b, int size)
4171 unsigned long *ipa = a;
4172 unsigned long *ipb = b;
4180 static int ftrace_process_locs(struct module *mod,
4181 unsigned long *start,
4184 struct ftrace_page *start_pg;
4185 struct ftrace_page *pg;
4186 struct dyn_ftrace *rec;
4187 unsigned long count;
4190 unsigned long flags = 0; /* Shut up gcc */
4193 count = end - start;
4198 sort(start, count, sizeof(*start),
4199 ftrace_cmp_ips, ftrace_swap_ips);
4201 start_pg = ftrace_allocate_pages(count);
4205 mutex_lock(&ftrace_lock);
4208 * Core and each module needs their own pages, as
4209 * modules will free them when they are removed.
4210 * Force a new page to be allocated for modules.
4213 WARN_ON(ftrace_pages || ftrace_pages_start);
4214 /* First initialization */
4215 ftrace_pages = ftrace_pages_start = start_pg;
4220 if (WARN_ON(ftrace_pages->next)) {
4221 /* Hmm, we have free pages? */
4222 while (ftrace_pages->next)
4223 ftrace_pages = ftrace_pages->next;
4226 ftrace_pages->next = start_pg;
4232 addr = ftrace_call_adjust(*p++);
4234 * Some architecture linkers will pad between
4235 * the different mcount_loc sections of different
4236 * object files to satisfy alignments.
4237 * Skip any NULL pointers.
4242 if (pg->index == pg->size) {
4243 /* We should have allocated enough */
4244 if (WARN_ON(!pg->next))
4249 rec = &pg->records[pg->index++];
4253 /* We should have used all pages */
4256 /* Assign the last page to ftrace_pages */
4260 * We only need to disable interrupts on start up
4261 * because we are modifying code that an interrupt
4262 * may execute, and the modification is not atomic.
4263 * But for modules, nothing runs the code we modify
4264 * until we are finished with it, and there's no
4265 * reason to cause large interrupt latencies while we do it.
4268 local_irq_save(flags);
4269 ftrace_update_code(mod, start_pg);
4271 local_irq_restore(flags);
4274 mutex_unlock(&ftrace_lock);
4279 #ifdef CONFIG_MODULES
4281 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4283 void ftrace_release_mod(struct module *mod)
4285 struct dyn_ftrace *rec;
4286 struct ftrace_page **last_pg;
4287 struct ftrace_page *pg;
4290 mutex_lock(&ftrace_lock);
4292 if (ftrace_disabled)
4296 * Each module has its own ftrace_pages, remove
4297 * them from the list.
4299 last_pg = &ftrace_pages_start;
4300 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4301 rec = &pg->records[0];
4302 if (within_module_core(rec->ip, mod)) {
4304 * As core pages are first, the first
4305 * page should never be a module page.
4307 if (WARN_ON(pg == ftrace_pages_start))
4310 /* Check if we are deleting the last page */
4311 if (pg == ftrace_pages)
4312 ftrace_pages = next_to_ftrace_page(last_pg);
4314 *last_pg = pg->next;
4315 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4316 free_pages((unsigned long)pg->records, order);
4319 last_pg = &pg->next;
4322 mutex_unlock(&ftrace_lock);
4325 static void ftrace_init_module(struct module *mod,
4326 unsigned long *start, unsigned long *end)
4328 if (ftrace_disabled || start == end)
4330 ftrace_process_locs(mod, start, end);
4333 static int ftrace_module_notify_enter(struct notifier_block *self,
4334 unsigned long val, void *data)
4336 struct module *mod = data;
4338 if (val == MODULE_STATE_COMING)
4339 ftrace_init_module(mod, mod->ftrace_callsites,
4340 mod->ftrace_callsites +
4341 mod->num_ftrace_callsites);
4345 static int ftrace_module_notify_exit(struct notifier_block *self,
4346 unsigned long val, void *data)
4348 struct module *mod = data;
4350 if (val == MODULE_STATE_GOING)
4351 ftrace_release_mod(mod);
4356 static int ftrace_module_notify_enter(struct notifier_block *self,
4357 unsigned long val, void *data)
4361 static int ftrace_module_notify_exit(struct notifier_block *self,
4362 unsigned long val, void *data)
4366 #endif /* CONFIG_MODULES */
4368 struct notifier_block ftrace_module_enter_nb = {
4369 .notifier_call = ftrace_module_notify_enter,
4370 .priority = INT_MAX, /* Run before anything that can use kprobes */
4373 struct notifier_block ftrace_module_exit_nb = {
4374 .notifier_call = ftrace_module_notify_exit,
4375 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4378 void __init ftrace_init(void)
4380 extern unsigned long __start_mcount_loc[];
4381 extern unsigned long __stop_mcount_loc[];
4382 unsigned long count, flags;
4385 local_irq_save(flags);
4386 ret = ftrace_dyn_arch_init();
4387 local_irq_restore(flags);
4391 count = __stop_mcount_loc - __start_mcount_loc;
4393 pr_info("ftrace: No functions to be traced?\n");
4397 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4398 count, count / ENTRIES_PER_PAGE + 1);
4400 last_ftrace_enabled = ftrace_enabled = 1;
4402 ret = ftrace_process_locs(NULL,
4406 ret = register_module_notifier(&ftrace_module_enter_nb);
4408 pr_warning("Failed to register trace ftrace module enter notifier\n");
4410 ret = register_module_notifier(&ftrace_module_exit_nb);
4412 pr_warning("Failed to register trace ftrace module exit notifier\n");
4414 set_ftrace_early_filters();
4418 ftrace_disabled = 1;
4423 static struct ftrace_ops global_ops = {
4424 .func = ftrace_stub,
4425 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4426 INIT_REGEX_LOCK(global_ops)
4429 static int __init ftrace_nodyn_init(void)
4434 core_initcall(ftrace_nodyn_init);
4436 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4437 static inline void ftrace_startup_enable(int command) { }
4438 /* Keep as macros so we do not need to define the commands */
4439 # define ftrace_startup(ops, command) \
4441 int ___ret = __register_ftrace_function(ops); \
4443 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4446 # define ftrace_shutdown(ops, command) \
4448 int ___ret = __unregister_ftrace_function(ops); \
4450 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4454 # define ftrace_startup_sysctl() do { } while (0)
4455 # define ftrace_shutdown_sysctl() do { } while (0)
4458 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4463 #endif /* CONFIG_DYNAMIC_FTRACE */
4466 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4467 struct ftrace_ops *op, struct pt_regs *regs)
4469 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4473 * Some of the ops may be dynamically allocated,
4474 * they must be freed after a synchronize_sched().
4476 preempt_disable_notrace();
4477 trace_recursion_set(TRACE_CONTROL_BIT);
4480 * Control funcs (perf) uses RCU. Only trace if
4481 * RCU is currently active.
4483 if (!rcu_is_watching())
4486 do_for_each_ftrace_op(op, ftrace_control_list) {
4487 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4488 !ftrace_function_local_disabled(op) &&
4489 ftrace_ops_test(op, ip, regs))
4490 op->func(ip, parent_ip, op, regs);
4491 } while_for_each_ftrace_op(op);
4493 trace_recursion_clear(TRACE_CONTROL_BIT);
4494 preempt_enable_notrace();
4497 static struct ftrace_ops control_ops = {
4498 .func = ftrace_ops_control_func,
4499 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4500 INIT_REGEX_LOCK(control_ops)
4504 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4505 struct ftrace_ops *ignored, struct pt_regs *regs)
4507 struct ftrace_ops *op;
4510 if (function_trace_stop)
4513 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4518 * Some of the ops may be dynamically allocated,
4519 * they must be freed after a synchronize_sched().
4521 preempt_disable_notrace();
4522 do_for_each_ftrace_op(op, ftrace_ops_list) {
4523 if (ftrace_ops_test(op, ip, regs))
4524 op->func(ip, parent_ip, op, regs);
4525 } while_for_each_ftrace_op(op);
4526 preempt_enable_notrace();
4527 trace_clear_recursion(bit);
4531 * Some archs only support passing ip and parent_ip. Even though
4532 * the list function ignores the op parameter, we do not want any
4533 * C side effects, where a function is called without the caller
4534 * sending a third parameter.
4535 * Archs are to support both the regs and ftrace_ops at the same time.
4536 * If they support ftrace_ops, it is assumed they support regs.
4537 * If call backs want to use regs, they must either check for regs
4538 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4539 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4540 * An architecture can pass partial regs with ftrace_ops and still
4541 * set the ARCH_SUPPORT_FTARCE_OPS.
4543 #if ARCH_SUPPORTS_FTRACE_OPS
4544 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4545 struct ftrace_ops *op, struct pt_regs *regs)
4547 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4550 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4552 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4556 static void clear_ftrace_swapper(void)
4558 struct task_struct *p;
4562 for_each_online_cpu(cpu) {
4564 clear_tsk_trace_trace(p);
4569 static void set_ftrace_swapper(void)
4571 struct task_struct *p;
4575 for_each_online_cpu(cpu) {
4577 set_tsk_trace_trace(p);
4582 static void clear_ftrace_pid(struct pid *pid)
4584 struct task_struct *p;
4587 do_each_pid_task(pid, PIDTYPE_PID, p) {
4588 clear_tsk_trace_trace(p);
4589 } while_each_pid_task(pid, PIDTYPE_PID, p);
4595 static void set_ftrace_pid(struct pid *pid)
4597 struct task_struct *p;
4600 do_each_pid_task(pid, PIDTYPE_PID, p) {
4601 set_tsk_trace_trace(p);
4602 } while_each_pid_task(pid, PIDTYPE_PID, p);
4606 static void clear_ftrace_pid_task(struct pid *pid)
4608 if (pid == ftrace_swapper_pid)
4609 clear_ftrace_swapper();
4611 clear_ftrace_pid(pid);
4614 static void set_ftrace_pid_task(struct pid *pid)
4616 if (pid == ftrace_swapper_pid)
4617 set_ftrace_swapper();
4619 set_ftrace_pid(pid);
4622 static int ftrace_pid_add(int p)
4625 struct ftrace_pid *fpid;
4628 mutex_lock(&ftrace_lock);
4631 pid = ftrace_swapper_pid;
4633 pid = find_get_pid(p);
4640 list_for_each_entry(fpid, &ftrace_pids, list)
4641 if (fpid->pid == pid)
4646 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4650 list_add(&fpid->list, &ftrace_pids);
4653 set_ftrace_pid_task(pid);
4655 ftrace_update_pid_func();
4656 ftrace_startup_enable(0);
4658 mutex_unlock(&ftrace_lock);
4662 if (pid != ftrace_swapper_pid)
4666 mutex_unlock(&ftrace_lock);
4670 static void ftrace_pid_reset(void)
4672 struct ftrace_pid *fpid, *safe;
4674 mutex_lock(&ftrace_lock);
4675 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4676 struct pid *pid = fpid->pid;
4678 clear_ftrace_pid_task(pid);
4680 list_del(&fpid->list);
4684 ftrace_update_pid_func();
4685 ftrace_startup_enable(0);
4687 mutex_unlock(&ftrace_lock);
4690 static void *fpid_start(struct seq_file *m, loff_t *pos)
4692 mutex_lock(&ftrace_lock);
4694 if (list_empty(&ftrace_pids) && (!*pos))
4697 return seq_list_start(&ftrace_pids, *pos);
4700 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4705 return seq_list_next(v, &ftrace_pids, pos);
4708 static void fpid_stop(struct seq_file *m, void *p)
4710 mutex_unlock(&ftrace_lock);
4713 static int fpid_show(struct seq_file *m, void *v)
4715 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4717 if (v == (void *)1) {
4718 seq_printf(m, "no pid\n");
4722 if (fpid->pid == ftrace_swapper_pid)
4723 seq_printf(m, "swapper tasks\n");
4725 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4730 static const struct seq_operations ftrace_pid_sops = {
4731 .start = fpid_start,
4738 ftrace_pid_open(struct inode *inode, struct file *file)
4742 if ((file->f_mode & FMODE_WRITE) &&
4743 (file->f_flags & O_TRUNC))
4746 if (file->f_mode & FMODE_READ)
4747 ret = seq_open(file, &ftrace_pid_sops);
4753 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4754 size_t cnt, loff_t *ppos)
4760 if (cnt >= sizeof(buf))
4763 if (copy_from_user(&buf, ubuf, cnt))
4769 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4770 * to clean the filter quietly.
4772 tmp = strstrip(buf);
4773 if (strlen(tmp) == 0)
4776 ret = kstrtol(tmp, 10, &val);
4780 ret = ftrace_pid_add(val);
4782 return ret ? ret : cnt;
4786 ftrace_pid_release(struct inode *inode, struct file *file)
4788 if (file->f_mode & FMODE_READ)
4789 seq_release(inode, file);
4794 static const struct file_operations ftrace_pid_fops = {
4795 .open = ftrace_pid_open,
4796 .write = ftrace_pid_write,
4798 .llseek = tracing_lseek,
4799 .release = ftrace_pid_release,
4802 static __init int ftrace_init_debugfs(void)
4804 struct dentry *d_tracer;
4806 d_tracer = tracing_init_dentry();
4810 ftrace_init_dyn_debugfs(d_tracer);
4812 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4813 NULL, &ftrace_pid_fops);
4815 ftrace_profile_debugfs(d_tracer);
4819 fs_initcall(ftrace_init_debugfs);
4822 * ftrace_kill - kill ftrace
4824 * This function should be used by panic code. It stops ftrace
4825 * but in a not so nice way. If you need to simply kill ftrace
4826 * from a non-atomic section, use ftrace_kill.
4828 void ftrace_kill(void)
4830 ftrace_disabled = 1;
4832 clear_ftrace_function();
4836 * Test if ftrace is dead or not.
4838 int ftrace_is_dead(void)
4840 return ftrace_disabled;
4844 * register_ftrace_function - register a function for profiling
4845 * @ops - ops structure that holds the function for profiling.
4847 * Register a function to be called by all functions in the
4850 * Note: @ops->func and all the functions it calls must be labeled
4851 * with "notrace", otherwise it will go into a
4854 int register_ftrace_function(struct ftrace_ops *ops)
4858 ftrace_ops_init(ops);
4860 mutex_lock(&ftrace_lock);
4862 ret = ftrace_startup(ops, 0);
4864 mutex_unlock(&ftrace_lock);
4868 EXPORT_SYMBOL_GPL(register_ftrace_function);
4871 * unregister_ftrace_function - unregister a function for profiling.
4872 * @ops - ops structure that holds the function to unregister
4874 * Unregister a function that was added to be called by ftrace profiling.
4876 int unregister_ftrace_function(struct ftrace_ops *ops)
4880 mutex_lock(&ftrace_lock);
4881 ret = ftrace_shutdown(ops, 0);
4882 mutex_unlock(&ftrace_lock);
4886 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4889 ftrace_enable_sysctl(struct ctl_table *table, int write,
4890 void __user *buffer, size_t *lenp,
4895 mutex_lock(&ftrace_lock);
4897 if (unlikely(ftrace_disabled))
4900 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4902 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4905 last_ftrace_enabled = !!ftrace_enabled;
4907 if (ftrace_enabled) {
4909 ftrace_startup_sysctl();
4911 /* we are starting ftrace again */
4912 if (ftrace_ops_list != &ftrace_list_end)
4913 update_ftrace_function();
4916 /* stopping ftrace calls (just send to ftrace_stub) */
4917 ftrace_trace_function = ftrace_stub;
4919 ftrace_shutdown_sysctl();
4923 mutex_unlock(&ftrace_lock);
4927 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4929 static int ftrace_graph_active;
4930 static struct notifier_block ftrace_suspend_notifier;
4932 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4937 /* The callbacks that hook a function */
4938 trace_func_graph_ret_t ftrace_graph_return =
4939 (trace_func_graph_ret_t)ftrace_stub;
4940 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4941 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
4943 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4944 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4948 unsigned long flags;
4949 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4950 struct task_struct *g, *t;
4952 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4953 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4954 * sizeof(struct ftrace_ret_stack),
4956 if (!ret_stack_list[i]) {
4964 read_lock_irqsave(&tasklist_lock, flags);
4965 do_each_thread(g, t) {
4971 if (t->ret_stack == NULL) {
4972 atomic_set(&t->tracing_graph_pause, 0);
4973 atomic_set(&t->trace_overrun, 0);
4974 t->curr_ret_stack = -1;
4975 /* Make sure the tasks see the -1 first: */
4977 t->ret_stack = ret_stack_list[start++];
4979 } while_each_thread(g, t);
4982 read_unlock_irqrestore(&tasklist_lock, flags);
4984 for (i = start; i < end; i++)
4985 kfree(ret_stack_list[i]);
4990 ftrace_graph_probe_sched_switch(void *ignore,
4991 struct task_struct *prev, struct task_struct *next)
4993 unsigned long long timestamp;
4997 * Does the user want to count the time a function was asleep.
4998 * If so, do not update the time stamps.
5000 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5003 timestamp = trace_clock_local();
5005 prev->ftrace_timestamp = timestamp;
5007 /* only process tasks that we timestamped */
5008 if (!next->ftrace_timestamp)
5012 * Update all the counters in next to make up for the
5013 * time next was sleeping.
5015 timestamp -= next->ftrace_timestamp;
5017 for (index = next->curr_ret_stack; index >= 0; index--)
5018 next->ret_stack[index].calltime += timestamp;
5021 /* Allocate a return stack for each task */
5022 static int start_graph_tracing(void)
5024 struct ftrace_ret_stack **ret_stack_list;
5027 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5028 sizeof(struct ftrace_ret_stack *),
5031 if (!ret_stack_list)
5034 /* The cpu_boot init_task->ret_stack will never be freed */
5035 for_each_online_cpu(cpu) {
5036 if (!idle_task(cpu)->ret_stack)
5037 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5041 ret = alloc_retstack_tasklist(ret_stack_list);
5042 } while (ret == -EAGAIN);
5045 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5047 pr_info("ftrace_graph: Couldn't activate tracepoint"
5048 " probe to kernel_sched_switch\n");
5051 kfree(ret_stack_list);
5056 * Hibernation protection.
5057 * The state of the current task is too much unstable during
5058 * suspend/restore to disk. We want to protect against that.
5061 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5065 case PM_HIBERNATION_PREPARE:
5066 pause_graph_tracing();
5069 case PM_POST_HIBERNATION:
5070 unpause_graph_tracing();
5076 /* Just a place holder for function graph */
5077 static struct ftrace_ops fgraph_ops __read_mostly = {
5078 .func = ftrace_stub,
5079 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
5080 FTRACE_OPS_FL_RECURSION_SAFE,
5083 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5085 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5087 return __ftrace_graph_entry(trace);
5091 * The function graph tracer should only trace the functions defined
5092 * by set_ftrace_filter and set_ftrace_notrace. If another function
5093 * tracer ops is registered, the graph tracer requires testing the
5094 * function against the global ops, and not just trace any function
5095 * that any ftrace_ops registered.
5097 static void update_function_graph_func(void)
5099 if (ftrace_ops_list == &ftrace_list_end ||
5100 (ftrace_ops_list == &global_ops &&
5101 global_ops.next == &ftrace_list_end))
5102 ftrace_graph_entry = __ftrace_graph_entry;
5104 ftrace_graph_entry = ftrace_graph_entry_test;
5107 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5108 trace_func_graph_ent_t entryfunc)
5112 mutex_lock(&ftrace_lock);
5114 /* we currently allow only one tracer registered at a time */
5115 if (ftrace_graph_active) {
5120 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5121 register_pm_notifier(&ftrace_suspend_notifier);
5123 ftrace_graph_active++;
5124 ret = start_graph_tracing();
5126 ftrace_graph_active--;
5130 ftrace_graph_return = retfunc;
5133 * Update the indirect function to the entryfunc, and the
5134 * function that gets called to the entry_test first. Then
5135 * call the update fgraph entry function to determine if
5136 * the entryfunc should be called directly or not.
5138 __ftrace_graph_entry = entryfunc;
5139 ftrace_graph_entry = ftrace_graph_entry_test;
5140 update_function_graph_func();
5142 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
5145 mutex_unlock(&ftrace_lock);
5149 void unregister_ftrace_graph(void)
5151 mutex_lock(&ftrace_lock);
5153 if (unlikely(!ftrace_graph_active))
5156 ftrace_graph_active--;
5157 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5158 ftrace_graph_entry = ftrace_graph_entry_stub;
5159 __ftrace_graph_entry = ftrace_graph_entry_stub;
5160 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
5161 unregister_pm_notifier(&ftrace_suspend_notifier);
5162 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5165 mutex_unlock(&ftrace_lock);
5168 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5171 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5173 atomic_set(&t->tracing_graph_pause, 0);
5174 atomic_set(&t->trace_overrun, 0);
5175 t->ftrace_timestamp = 0;
5176 /* make curr_ret_stack visible before we add the ret_stack */
5178 t->ret_stack = ret_stack;
5182 * Allocate a return stack for the idle task. May be the first
5183 * time through, or it may be done by CPU hotplug online.
5185 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5187 t->curr_ret_stack = -1;
5189 * The idle task has no parent, it either has its own
5190 * stack or no stack at all.
5193 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5195 if (ftrace_graph_active) {
5196 struct ftrace_ret_stack *ret_stack;
5198 ret_stack = per_cpu(idle_ret_stack, cpu);
5200 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5201 * sizeof(struct ftrace_ret_stack),
5205 per_cpu(idle_ret_stack, cpu) = ret_stack;
5207 graph_init_task(t, ret_stack);
5211 /* Allocate a return stack for newly created task */
5212 void ftrace_graph_init_task(struct task_struct *t)
5214 /* Make sure we do not use the parent ret_stack */
5215 t->ret_stack = NULL;
5216 t->curr_ret_stack = -1;
5218 if (ftrace_graph_active) {
5219 struct ftrace_ret_stack *ret_stack;
5221 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5222 * sizeof(struct ftrace_ret_stack),
5226 graph_init_task(t, ret_stack);
5230 void ftrace_graph_exit_task(struct task_struct *t)
5232 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5234 t->ret_stack = NULL;
5235 /* NULL must become visible to IRQs before we free it: */
5241 void ftrace_graph_stop(void)