2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
36 #include <trace/events/sched.h>
38 #include <asm/setup.h>
40 #include "trace_output.h"
41 #include "trace_stat.h"
43 #define FTRACE_WARN_ON(cond) \
51 #define FTRACE_WARN_ON_ONCE(cond) \
54 if (WARN_ON_ONCE(___r)) \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
71 #define INIT_REGEX_LOCK(opsname)
74 static struct ftrace_ops ftrace_list_end __read_mostly = {
76 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
79 /* ftrace_enabled is a method to turn ftrace on or off */
80 int ftrace_enabled __read_mostly;
81 static int last_ftrace_enabled;
83 /* Quick disabling of function tracer. */
84 int function_trace_stop __read_mostly;
86 /* Current function tracing op */
87 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
88 /* What to set function_trace_op to */
89 static struct ftrace_ops *set_function_trace_op;
91 /* List for set_ftrace_pid's pids. */
92 LIST_HEAD(ftrace_pids);
94 struct list_head list;
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
102 static int ftrace_disabled __read_mostly;
104 static DEFINE_MUTEX(ftrace_lock);
106 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
107 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
108 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
109 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
110 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
111 static struct ftrace_ops global_ops;
112 static struct ftrace_ops control_ops;
114 #if ARCH_SUPPORTS_FTRACE_OPS
115 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
116 struct ftrace_ops *op, struct pt_regs *regs);
118 /* See comment below, where ftrace_ops_list_func is defined */
119 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
120 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
124 * Traverse the ftrace_global_list, invoking all entries. The reason that we
125 * can use rcu_dereference_raw_notrace() is that elements removed from this list
126 * are simply leaked, so there is no need to interact with a grace-period
127 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
128 * concurrent insertions into the ftrace_global_list.
130 * Silly Alpha and silly pointer-speculation compiler optimizations!
132 #define do_for_each_ftrace_op(op, list) \
133 op = rcu_dereference_raw_notrace(list); \
137 * Optimized for just a single item in the list (as that is the normal case).
139 #define while_for_each_ftrace_op(op) \
140 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
141 unlikely((op) != &ftrace_list_end))
143 static inline void ftrace_ops_init(struct ftrace_ops *ops)
145 #ifdef CONFIG_DYNAMIC_FTRACE
146 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
147 mutex_init(&ops->regex_lock);
148 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
154 * ftrace_nr_registered_ops - return number of ops registered
156 * Returns the number of ftrace_ops registered and tracing functions
158 int ftrace_nr_registered_ops(void)
160 struct ftrace_ops *ops;
163 mutex_lock(&ftrace_lock);
165 for (ops = ftrace_ops_list;
166 ops != &ftrace_list_end; ops = ops->next)
169 mutex_unlock(&ftrace_lock);
175 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
176 struct ftrace_ops *op, struct pt_regs *regs)
180 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
184 do_for_each_ftrace_op(op, ftrace_global_list) {
185 op->func(ip, parent_ip, op, regs);
186 } while_for_each_ftrace_op(op);
188 trace_clear_recursion(bit);
191 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
192 struct ftrace_ops *op, struct pt_regs *regs)
194 if (!test_tsk_trace_trace(current))
197 ftrace_pid_function(ip, parent_ip, op, regs);
200 static void set_ftrace_pid_function(ftrace_func_t func)
202 /* do not set ftrace_pid_function to itself! */
203 if (func != ftrace_pid_func)
204 ftrace_pid_function = func;
208 * clear_ftrace_function - reset the ftrace function
210 * This NULLs the ftrace function and in essence stops
211 * tracing. There may be lag
213 void clear_ftrace_function(void)
215 ftrace_trace_function = ftrace_stub;
216 ftrace_pid_function = ftrace_stub;
219 static void control_ops_disable_all(struct ftrace_ops *ops)
223 for_each_possible_cpu(cpu)
224 *per_cpu_ptr(ops->disabled, cpu) = 1;
227 static int control_ops_alloc(struct ftrace_ops *ops)
229 int __percpu *disabled;
231 disabled = alloc_percpu(int);
235 ops->disabled = disabled;
236 control_ops_disable_all(ops);
240 static void control_ops_free(struct ftrace_ops *ops)
242 free_percpu(ops->disabled);
245 static void update_global_ops(void)
250 * If there's only one function registered, then call that
251 * function directly. Otherwise, we need to iterate over the
252 * registered callers.
254 if (ftrace_global_list == &ftrace_list_end ||
255 ftrace_global_list->next == &ftrace_list_end) {
256 func = ftrace_global_list->func;
258 * As we are calling the function directly.
259 * If it does not have recursion protection,
260 * the function_trace_op needs to be updated
263 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
264 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
266 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
268 func = ftrace_global_list_func;
269 /* The list has its own recursion protection. */
270 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
274 /* If we filter on pids, update to use the pid function */
275 if (!list_empty(&ftrace_pids)) {
276 set_ftrace_pid_function(func);
277 func = ftrace_pid_func;
280 global_ops.func = func;
283 static void ftrace_sync(struct work_struct *work)
286 * This function is just a stub to implement a hard force
287 * of synchronize_sched(). This requires synchronizing
288 * tasks even in userspace and idle.
290 * Yes, function tracing is rude.
294 static void ftrace_sync_ipi(void *data)
296 /* Probably not needed, but do it anyway */
300 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
301 static void update_function_graph_func(void);
303 static inline void update_function_graph_func(void) { }
306 static void update_ftrace_function(void)
313 * If we are at the end of the list and this ops is
314 * recursion safe and not dynamic and the arch supports passing ops,
315 * then have the mcount trampoline call the function directly.
317 if (ftrace_ops_list == &ftrace_list_end ||
318 (ftrace_ops_list->next == &ftrace_list_end &&
319 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
320 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
321 !FTRACE_FORCE_LIST_FUNC)) {
322 /* Set the ftrace_ops that the arch callback uses */
323 if (ftrace_ops_list == &global_ops)
324 set_function_trace_op = ftrace_global_list;
326 set_function_trace_op = ftrace_ops_list;
327 func = ftrace_ops_list->func;
329 /* Just use the default ftrace_ops */
330 set_function_trace_op = &ftrace_list_end;
331 func = ftrace_ops_list_func;
334 /* If there's no change, then do nothing more here */
335 if (ftrace_trace_function == func)
338 update_function_graph_func();
341 * If we are using the list function, it doesn't care
342 * about the function_trace_ops.
344 if (func == ftrace_ops_list_func) {
345 ftrace_trace_function = func;
347 * Don't even bother setting function_trace_ops,
348 * it would be racy to do so anyway.
353 #ifndef CONFIG_DYNAMIC_FTRACE
355 * For static tracing, we need to be a bit more careful.
356 * The function change takes affect immediately. Thus,
357 * we need to coorditate the setting of the function_trace_ops
358 * with the setting of the ftrace_trace_function.
360 * Set the function to the list ops, which will call the
361 * function we want, albeit indirectly, but it handles the
362 * ftrace_ops and doesn't depend on function_trace_op.
364 ftrace_trace_function = ftrace_ops_list_func;
366 * Make sure all CPUs see this. Yes this is slow, but static
367 * tracing is slow and nasty to have enabled.
369 schedule_on_each_cpu(ftrace_sync);
370 /* Now all cpus are using the list ops. */
371 function_trace_op = set_function_trace_op;
372 /* Make sure the function_trace_op is visible on all CPUs */
374 /* Nasty way to force a rmb on all cpus */
375 smp_call_function(ftrace_sync_ipi, NULL, 1);
376 /* OK, we are all set to update the ftrace_trace_function now! */
377 #endif /* !CONFIG_DYNAMIC_FTRACE */
379 ftrace_trace_function = func;
382 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
386 * We are entering ops into the list but another
387 * CPU might be walking that list. We need to make sure
388 * the ops->next pointer is valid before another CPU sees
389 * the ops pointer included into the list.
391 rcu_assign_pointer(*list, ops);
394 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
396 struct ftrace_ops **p;
399 * If we are removing the last function, then simply point
400 * to the ftrace_stub.
402 if (*list == ops && ops->next == &ftrace_list_end) {
403 *list = &ftrace_list_end;
407 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
418 static void add_ftrace_list_ops(struct ftrace_ops **list,
419 struct ftrace_ops *main_ops,
420 struct ftrace_ops *ops)
422 int first = *list == &ftrace_list_end;
423 add_ftrace_ops(list, ops);
425 add_ftrace_ops(&ftrace_ops_list, main_ops);
428 static int remove_ftrace_list_ops(struct ftrace_ops **list,
429 struct ftrace_ops *main_ops,
430 struct ftrace_ops *ops)
432 int ret = remove_ftrace_ops(list, ops);
433 if (!ret && *list == &ftrace_list_end)
434 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
438 static int __register_ftrace_function(struct ftrace_ops *ops)
440 if (FTRACE_WARN_ON(ops == &global_ops))
443 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
446 /* We don't support both control and global flags set. */
447 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
450 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
452 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
453 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
454 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
456 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
457 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
460 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
461 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
464 if (!core_kernel_data((unsigned long)ops))
465 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
467 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
468 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
469 ops->flags |= FTRACE_OPS_FL_ENABLED;
470 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
471 if (control_ops_alloc(ops))
473 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
475 add_ftrace_ops(&ftrace_ops_list, ops);
478 update_ftrace_function();
483 static int __unregister_ftrace_function(struct ftrace_ops *ops)
487 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
490 if (FTRACE_WARN_ON(ops == &global_ops))
493 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
494 ret = remove_ftrace_list_ops(&ftrace_global_list,
497 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
498 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
499 ret = remove_ftrace_list_ops(&ftrace_control_list,
502 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
508 update_ftrace_function();
513 static void ftrace_update_pid_func(void)
515 /* Only do something if we are tracing something */
516 if (ftrace_trace_function == ftrace_stub)
519 update_ftrace_function();
522 #ifdef CONFIG_FUNCTION_PROFILER
523 struct ftrace_profile {
524 struct hlist_node node;
526 unsigned long counter;
527 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
528 unsigned long long time;
529 unsigned long long time_squared;
533 struct ftrace_profile_page {
534 struct ftrace_profile_page *next;
536 struct ftrace_profile records[];
539 struct ftrace_profile_stat {
541 struct hlist_head *hash;
542 struct ftrace_profile_page *pages;
543 struct ftrace_profile_page *start;
544 struct tracer_stat stat;
547 #define PROFILE_RECORDS_SIZE \
548 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
550 #define PROFILES_PER_PAGE \
551 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
553 static int ftrace_profile_enabled __read_mostly;
555 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
556 static DEFINE_MUTEX(ftrace_profile_lock);
558 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
560 #define FTRACE_PROFILE_HASH_BITS 10
561 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
564 function_stat_next(void *v, int idx)
566 struct ftrace_profile *rec = v;
567 struct ftrace_profile_page *pg;
569 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
575 if ((void *)rec >= (void *)&pg->records[pg->index]) {
579 rec = &pg->records[0];
587 static void *function_stat_start(struct tracer_stat *trace)
589 struct ftrace_profile_stat *stat =
590 container_of(trace, struct ftrace_profile_stat, stat);
592 if (!stat || !stat->start)
595 return function_stat_next(&stat->start->records[0], 0);
598 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
599 /* function graph compares on total time */
600 static int function_stat_cmp(void *p1, void *p2)
602 struct ftrace_profile *a = p1;
603 struct ftrace_profile *b = p2;
605 if (a->time < b->time)
607 if (a->time > b->time)
613 /* not function graph compares against hits */
614 static int function_stat_cmp(void *p1, void *p2)
616 struct ftrace_profile *a = p1;
617 struct ftrace_profile *b = p2;
619 if (a->counter < b->counter)
621 if (a->counter > b->counter)
628 static int function_stat_headers(struct seq_file *m)
630 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
631 seq_printf(m, " Function "
634 "--- ---- --- ---\n");
636 seq_printf(m, " Function Hit\n"
642 static int function_stat_show(struct seq_file *m, void *v)
644 struct ftrace_profile *rec = v;
645 char str[KSYM_SYMBOL_LEN];
647 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
648 static struct trace_seq s;
649 unsigned long long avg;
650 unsigned long long stddev;
652 mutex_lock(&ftrace_profile_lock);
654 /* we raced with function_profile_reset() */
655 if (unlikely(rec->counter == 0)) {
660 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
661 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
663 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
666 do_div(avg, rec->counter);
668 /* Sample standard deviation (s^2) */
669 if (rec->counter <= 1)
672 stddev = rec->time_squared - rec->counter * avg * avg;
674 * Divide only 1000 for ns^2 -> us^2 conversion.
675 * trace_print_graph_duration will divide 1000 again.
677 do_div(stddev, (rec->counter - 1) * 1000);
681 trace_print_graph_duration(rec->time, &s);
682 trace_seq_puts(&s, " ");
683 trace_print_graph_duration(avg, &s);
684 trace_seq_puts(&s, " ");
685 trace_print_graph_duration(stddev, &s);
686 trace_print_seq(m, &s);
690 mutex_unlock(&ftrace_profile_lock);
695 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
697 struct ftrace_profile_page *pg;
699 pg = stat->pages = stat->start;
702 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
707 memset(stat->hash, 0,
708 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
711 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
713 struct ftrace_profile_page *pg;
718 /* If we already allocated, do nothing */
722 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
726 #ifdef CONFIG_DYNAMIC_FTRACE
727 functions = ftrace_update_tot_cnt;
730 * We do not know the number of functions that exist because
731 * dynamic tracing is what counts them. With past experience
732 * we have around 20K functions. That should be more than enough.
733 * It is highly unlikely we will execute every function in
739 pg = stat->start = stat->pages;
741 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
743 for (i = 1; i < pages; i++) {
744 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
755 unsigned long tmp = (unsigned long)pg;
767 static int ftrace_profile_init_cpu(int cpu)
769 struct ftrace_profile_stat *stat;
772 stat = &per_cpu(ftrace_profile_stats, cpu);
775 /* If the profile is already created, simply reset it */
776 ftrace_profile_reset(stat);
781 * We are profiling all functions, but usually only a few thousand
782 * functions are hit. We'll make a hash of 1024 items.
784 size = FTRACE_PROFILE_HASH_SIZE;
786 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
791 /* Preallocate the function profiling pages */
792 if (ftrace_profile_pages_init(stat) < 0) {
801 static int ftrace_profile_init(void)
806 for_each_possible_cpu(cpu) {
807 ret = ftrace_profile_init_cpu(cpu);
815 /* interrupts must be disabled */
816 static struct ftrace_profile *
817 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
819 struct ftrace_profile *rec;
820 struct hlist_head *hhd;
823 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
824 hhd = &stat->hash[key];
826 if (hlist_empty(hhd))
829 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
837 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
838 struct ftrace_profile *rec)
842 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
843 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
847 * The memory is already allocated, this simply finds a new record to use.
849 static struct ftrace_profile *
850 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
852 struct ftrace_profile *rec = NULL;
854 /* prevent recursion (from NMIs) */
855 if (atomic_inc_return(&stat->disabled) != 1)
859 * Try to find the function again since an NMI
860 * could have added it
862 rec = ftrace_find_profiled_func(stat, ip);
866 if (stat->pages->index == PROFILES_PER_PAGE) {
867 if (!stat->pages->next)
869 stat->pages = stat->pages->next;
872 rec = &stat->pages->records[stat->pages->index++];
874 ftrace_add_profile(stat, rec);
877 atomic_dec(&stat->disabled);
883 function_profile_call(unsigned long ip, unsigned long parent_ip,
884 struct ftrace_ops *ops, struct pt_regs *regs)
886 struct ftrace_profile_stat *stat;
887 struct ftrace_profile *rec;
890 if (!ftrace_profile_enabled)
893 local_irq_save(flags);
895 stat = &__get_cpu_var(ftrace_profile_stats);
896 if (!stat->hash || !ftrace_profile_enabled)
899 rec = ftrace_find_profiled_func(stat, ip);
901 rec = ftrace_profile_alloc(stat, ip);
908 local_irq_restore(flags);
911 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
912 static int profile_graph_entry(struct ftrace_graph_ent *trace)
914 function_profile_call(trace->func, 0, NULL, NULL);
918 static void profile_graph_return(struct ftrace_graph_ret *trace)
920 struct ftrace_profile_stat *stat;
921 unsigned long long calltime;
922 struct ftrace_profile *rec;
925 local_irq_save(flags);
926 stat = &__get_cpu_var(ftrace_profile_stats);
927 if (!stat->hash || !ftrace_profile_enabled)
930 /* If the calltime was zero'd ignore it */
931 if (!trace->calltime)
934 calltime = trace->rettime - trace->calltime;
936 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
939 index = trace->depth;
941 /* Append this call time to the parent time to subtract */
943 current->ret_stack[index - 1].subtime += calltime;
945 if (current->ret_stack[index].subtime < calltime)
946 calltime -= current->ret_stack[index].subtime;
951 rec = ftrace_find_profiled_func(stat, trace->func);
953 rec->time += calltime;
954 rec->time_squared += calltime * calltime;
958 local_irq_restore(flags);
961 static int register_ftrace_profiler(void)
963 return register_ftrace_graph(&profile_graph_return,
964 &profile_graph_entry);
967 static void unregister_ftrace_profiler(void)
969 unregister_ftrace_graph();
972 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
973 .func = function_profile_call,
974 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
975 INIT_REGEX_LOCK(ftrace_profile_ops)
978 static int register_ftrace_profiler(void)
980 return register_ftrace_function(&ftrace_profile_ops);
983 static void unregister_ftrace_profiler(void)
985 unregister_ftrace_function(&ftrace_profile_ops);
987 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
990 ftrace_profile_write(struct file *filp, const char __user *ubuf,
991 size_t cnt, loff_t *ppos)
996 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1002 mutex_lock(&ftrace_profile_lock);
1003 if (ftrace_profile_enabled ^ val) {
1005 ret = ftrace_profile_init();
1011 ret = register_ftrace_profiler();
1016 ftrace_profile_enabled = 1;
1018 ftrace_profile_enabled = 0;
1020 * unregister_ftrace_profiler calls stop_machine
1021 * so this acts like an synchronize_sched.
1023 unregister_ftrace_profiler();
1027 mutex_unlock(&ftrace_profile_lock);
1035 ftrace_profile_read(struct file *filp, char __user *ubuf,
1036 size_t cnt, loff_t *ppos)
1038 char buf[64]; /* big enough to hold a number */
1041 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1042 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1045 static const struct file_operations ftrace_profile_fops = {
1046 .open = tracing_open_generic,
1047 .read = ftrace_profile_read,
1048 .write = ftrace_profile_write,
1049 .llseek = default_llseek,
1052 /* used to initialize the real stat files */
1053 static struct tracer_stat function_stats __initdata = {
1054 .name = "functions",
1055 .stat_start = function_stat_start,
1056 .stat_next = function_stat_next,
1057 .stat_cmp = function_stat_cmp,
1058 .stat_headers = function_stat_headers,
1059 .stat_show = function_stat_show
1062 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1064 struct ftrace_profile_stat *stat;
1065 struct dentry *entry;
1070 for_each_possible_cpu(cpu) {
1071 stat = &per_cpu(ftrace_profile_stats, cpu);
1073 /* allocate enough for function name + cpu number */
1074 name = kmalloc(32, GFP_KERNEL);
1077 * The files created are permanent, if something happens
1078 * we still do not free memory.
1081 "Could not allocate stat file for cpu %d\n",
1085 stat->stat = function_stats;
1086 snprintf(name, 32, "function%d", cpu);
1087 stat->stat.name = name;
1088 ret = register_stat_tracer(&stat->stat);
1091 "Could not register function stat for cpu %d\n",
1098 entry = debugfs_create_file("function_profile_enabled", 0644,
1099 d_tracer, NULL, &ftrace_profile_fops);
1101 pr_warning("Could not create debugfs "
1102 "'function_profile_enabled' entry\n");
1105 #else /* CONFIG_FUNCTION_PROFILER */
1106 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1109 #endif /* CONFIG_FUNCTION_PROFILER */
1111 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1114 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1118 if (file->f_mode & FMODE_READ)
1119 ret = seq_lseek(file, offset, whence);
1121 file->f_pos = ret = 1;
1126 #ifdef CONFIG_DYNAMIC_FTRACE
1128 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1129 # error Dynamic ftrace depends on MCOUNT_RECORD
1132 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1134 struct ftrace_func_probe {
1135 struct hlist_node node;
1136 struct ftrace_probe_ops *ops;
1137 unsigned long flags;
1140 struct list_head free_list;
1143 struct ftrace_func_entry {
1144 struct hlist_node hlist;
1148 struct ftrace_hash {
1149 unsigned long size_bits;
1150 struct hlist_head *buckets;
1151 unsigned long count;
1152 struct rcu_head rcu;
1156 * We make these constant because no one should touch them,
1157 * but they are used as the default "empty hash", to avoid allocating
1158 * it all the time. These are in a read only section such that if
1159 * anyone does try to modify it, it will cause an exception.
1161 static const struct hlist_head empty_buckets[1];
1162 static const struct ftrace_hash empty_hash = {
1163 .buckets = (struct hlist_head *)empty_buckets,
1165 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1167 static struct ftrace_ops global_ops = {
1168 .func = ftrace_stub,
1169 .notrace_hash = EMPTY_HASH,
1170 .filter_hash = EMPTY_HASH,
1171 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1172 INIT_REGEX_LOCK(global_ops)
1175 struct ftrace_page {
1176 struct ftrace_page *next;
1177 struct dyn_ftrace *records;
1182 static struct ftrace_page *ftrace_new_pgs;
1184 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1185 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1187 /* estimate from running different kernels */
1188 #define NR_TO_INIT 10000
1190 static struct ftrace_page *ftrace_pages_start;
1191 static struct ftrace_page *ftrace_pages;
1193 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1195 return !hash || !hash->count;
1198 static struct ftrace_func_entry *
1199 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1202 struct ftrace_func_entry *entry;
1203 struct hlist_head *hhd;
1205 if (ftrace_hash_empty(hash))
1208 if (hash->size_bits > 0)
1209 key = hash_long(ip, hash->size_bits);
1213 hhd = &hash->buckets[key];
1215 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1216 if (entry->ip == ip)
1222 static void __add_hash_entry(struct ftrace_hash *hash,
1223 struct ftrace_func_entry *entry)
1225 struct hlist_head *hhd;
1228 if (hash->size_bits)
1229 key = hash_long(entry->ip, hash->size_bits);
1233 hhd = &hash->buckets[key];
1234 hlist_add_head(&entry->hlist, hhd);
1238 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1240 struct ftrace_func_entry *entry;
1242 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1247 __add_hash_entry(hash, entry);
1253 free_hash_entry(struct ftrace_hash *hash,
1254 struct ftrace_func_entry *entry)
1256 hlist_del(&entry->hlist);
1262 remove_hash_entry(struct ftrace_hash *hash,
1263 struct ftrace_func_entry *entry)
1265 hlist_del(&entry->hlist);
1269 static void ftrace_hash_clear(struct ftrace_hash *hash)
1271 struct hlist_head *hhd;
1272 struct hlist_node *tn;
1273 struct ftrace_func_entry *entry;
1274 int size = 1 << hash->size_bits;
1280 for (i = 0; i < size; i++) {
1281 hhd = &hash->buckets[i];
1282 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1283 free_hash_entry(hash, entry);
1285 FTRACE_WARN_ON(hash->count);
1288 static void free_ftrace_hash(struct ftrace_hash *hash)
1290 if (!hash || hash == EMPTY_HASH)
1292 ftrace_hash_clear(hash);
1293 kfree(hash->buckets);
1297 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1299 struct ftrace_hash *hash;
1301 hash = container_of(rcu, struct ftrace_hash, rcu);
1302 free_ftrace_hash(hash);
1305 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1307 if (!hash || hash == EMPTY_HASH)
1309 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1312 void ftrace_free_filter(struct ftrace_ops *ops)
1314 ftrace_ops_init(ops);
1315 free_ftrace_hash(ops->filter_hash);
1316 free_ftrace_hash(ops->notrace_hash);
1319 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1321 struct ftrace_hash *hash;
1324 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1328 size = 1 << size_bits;
1329 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1331 if (!hash->buckets) {
1336 hash->size_bits = size_bits;
1341 static struct ftrace_hash *
1342 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1344 struct ftrace_func_entry *entry;
1345 struct ftrace_hash *new_hash;
1350 new_hash = alloc_ftrace_hash(size_bits);
1355 if (ftrace_hash_empty(hash))
1358 size = 1 << hash->size_bits;
1359 for (i = 0; i < size; i++) {
1360 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1361 ret = add_hash_entry(new_hash, entry->ip);
1367 FTRACE_WARN_ON(new_hash->count != hash->count);
1372 free_ftrace_hash(new_hash);
1377 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1379 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1382 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1383 struct ftrace_hash **dst, struct ftrace_hash *src)
1385 struct ftrace_func_entry *entry;
1386 struct hlist_node *tn;
1387 struct hlist_head *hhd;
1388 struct ftrace_hash *old_hash;
1389 struct ftrace_hash *new_hash;
1390 int size = src->count;
1396 * Remove the current set, update the hash and add
1399 ftrace_hash_rec_disable(ops, enable);
1402 * If the new source is empty, just free dst and assign it
1406 free_ftrace_hash_rcu(*dst);
1407 rcu_assign_pointer(*dst, EMPTY_HASH);
1408 /* still need to update the function records */
1414 * Make the hash size about 1/2 the # found
1416 for (size /= 2; size; size >>= 1)
1419 /* Don't allocate too much */
1420 if (bits > FTRACE_HASH_MAX_BITS)
1421 bits = FTRACE_HASH_MAX_BITS;
1424 new_hash = alloc_ftrace_hash(bits);
1428 size = 1 << src->size_bits;
1429 for (i = 0; i < size; i++) {
1430 hhd = &src->buckets[i];
1431 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1432 remove_hash_entry(src, entry);
1433 __add_hash_entry(new_hash, entry);
1438 rcu_assign_pointer(*dst, new_hash);
1439 free_ftrace_hash_rcu(old_hash);
1444 * Enable regardless of ret:
1445 * On success, we enable the new hash.
1446 * On failure, we re-enable the original hash.
1448 ftrace_hash_rec_enable(ops, enable);
1454 * Test the hashes for this ops to see if we want to call
1455 * the ops->func or not.
1457 * It's a match if the ip is in the ops->filter_hash or
1458 * the filter_hash does not exist or is empty,
1460 * the ip is not in the ops->notrace_hash.
1462 * This needs to be called with preemption disabled as
1463 * the hashes are freed with call_rcu_sched().
1466 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1468 struct ftrace_hash *filter_hash;
1469 struct ftrace_hash *notrace_hash;
1472 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1474 * There's a small race when adding ops that the ftrace handler
1475 * that wants regs, may be called without them. We can not
1476 * allow that handler to be called if regs is NULL.
1478 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1482 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1483 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1485 if ((ftrace_hash_empty(filter_hash) ||
1486 ftrace_lookup_ip(filter_hash, ip)) &&
1487 (ftrace_hash_empty(notrace_hash) ||
1488 !ftrace_lookup_ip(notrace_hash, ip)))
1497 * This is a double for. Do not use 'break' to break out of the loop,
1498 * you must use a goto.
1500 #define do_for_each_ftrace_rec(pg, rec) \
1501 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1503 for (_____i = 0; _____i < pg->index; _____i++) { \
1504 rec = &pg->records[_____i];
1506 #define while_for_each_ftrace_rec() \
1511 static int ftrace_cmp_recs(const void *a, const void *b)
1513 const struct dyn_ftrace *key = a;
1514 const struct dyn_ftrace *rec = b;
1516 if (key->flags < rec->ip)
1518 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1523 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1525 struct ftrace_page *pg;
1526 struct dyn_ftrace *rec;
1527 struct dyn_ftrace key;
1530 key.flags = end; /* overload flags, as it is unsigned long */
1532 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1533 if (end < pg->records[0].ip ||
1534 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1536 rec = bsearch(&key, pg->records, pg->index,
1537 sizeof(struct dyn_ftrace),
1547 * ftrace_location - return true if the ip giving is a traced location
1548 * @ip: the instruction pointer to check
1550 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1551 * That is, the instruction that is either a NOP or call to
1552 * the function tracer. It checks the ftrace internal tables to
1553 * determine if the address belongs or not.
1555 unsigned long ftrace_location(unsigned long ip)
1557 return ftrace_location_range(ip, ip);
1561 * ftrace_text_reserved - return true if range contains an ftrace location
1562 * @start: start of range to search
1563 * @end: end of range to search (inclusive). @end points to the last byte to check.
1565 * Returns 1 if @start and @end contains a ftrace location.
1566 * That is, the instruction that is either a NOP or call to
1567 * the function tracer. It checks the ftrace internal tables to
1568 * determine if the address belongs or not.
1570 int ftrace_text_reserved(void *start, void *end)
1574 ret = ftrace_location_range((unsigned long)start,
1575 (unsigned long)end);
1580 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1584 struct ftrace_hash *hash;
1585 struct ftrace_hash *other_hash;
1586 struct ftrace_page *pg;
1587 struct dyn_ftrace *rec;
1591 /* Only update if the ops has been registered */
1592 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1596 * In the filter_hash case:
1597 * If the count is zero, we update all records.
1598 * Otherwise we just update the items in the hash.
1600 * In the notrace_hash case:
1601 * We enable the update in the hash.
1602 * As disabling notrace means enabling the tracing,
1603 * and enabling notrace means disabling, the inc variable
1607 hash = ops->filter_hash;
1608 other_hash = ops->notrace_hash;
1609 if (ftrace_hash_empty(hash))
1613 hash = ops->notrace_hash;
1614 other_hash = ops->filter_hash;
1616 * If the notrace hash has no items,
1617 * then there's nothing to do.
1619 if (ftrace_hash_empty(hash))
1623 do_for_each_ftrace_rec(pg, rec) {
1624 int in_other_hash = 0;
1630 * Only the filter_hash affects all records.
1631 * Update if the record is not in the notrace hash.
1633 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1636 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1637 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1642 if (filter_hash && in_hash && !in_other_hash)
1644 else if (!filter_hash && in_hash &&
1645 (in_other_hash || ftrace_hash_empty(other_hash)))
1653 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1656 * If any ops wants regs saved for this function
1657 * then all ops will get saved regs.
1659 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1660 rec->flags |= FTRACE_FL_REGS;
1662 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1667 /* Shortcut, if we handled all records, we are done. */
1668 if (!all && count == hash->count)
1670 } while_for_each_ftrace_rec();
1673 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1676 __ftrace_hash_rec_update(ops, filter_hash, 0);
1679 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1682 __ftrace_hash_rec_update(ops, filter_hash, 1);
1685 static void print_ip_ins(const char *fmt, unsigned char *p)
1689 printk(KERN_CONT "%s", fmt);
1691 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1692 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1696 * ftrace_bug - report and shutdown function tracer
1697 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1698 * @ip: The address that failed
1700 * The arch code that enables or disables the function tracing
1701 * can call ftrace_bug() when it has detected a problem in
1702 * modifying the code. @failed should be one of either:
1703 * EFAULT - if the problem happens on reading the @ip address
1704 * EINVAL - if what is read at @ip is not what was expected
1705 * EPERM - if the problem happens on writting to the @ip address
1707 void ftrace_bug(int failed, unsigned long ip)
1711 FTRACE_WARN_ON_ONCE(1);
1712 pr_info("ftrace faulted on modifying ");
1716 FTRACE_WARN_ON_ONCE(1);
1717 pr_info("ftrace failed to modify ");
1719 print_ip_ins(" actual: ", (unsigned char *)ip);
1720 printk(KERN_CONT "\n");
1723 FTRACE_WARN_ON_ONCE(1);
1724 pr_info("ftrace faulted on writing ");
1728 FTRACE_WARN_ON_ONCE(1);
1729 pr_info("ftrace faulted on unknown error ");
1734 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1736 unsigned long flag = 0UL;
1739 * If we are updating calls:
1741 * If the record has a ref count, then we need to enable it
1742 * because someone is using it.
1744 * Otherwise we make sure its disabled.
1746 * If we are disabling calls, then disable all records that
1749 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1750 flag = FTRACE_FL_ENABLED;
1753 * If enabling and the REGS flag does not match the REGS_EN, then
1754 * do not ignore this record. Set flags to fail the compare against
1758 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1759 flag |= FTRACE_FL_REGS;
1761 /* If the state of this record hasn't changed, then do nothing */
1762 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1763 return FTRACE_UPDATE_IGNORE;
1766 /* Save off if rec is being enabled (for return value) */
1767 flag ^= rec->flags & FTRACE_FL_ENABLED;
1770 rec->flags |= FTRACE_FL_ENABLED;
1771 if (flag & FTRACE_FL_REGS) {
1772 if (rec->flags & FTRACE_FL_REGS)
1773 rec->flags |= FTRACE_FL_REGS_EN;
1775 rec->flags &= ~FTRACE_FL_REGS_EN;
1780 * If this record is being updated from a nop, then
1781 * return UPDATE_MAKE_CALL.
1782 * Otherwise, if the EN flag is set, then return
1783 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1784 * from the non-save regs, to a save regs function.
1786 * return UPDATE_MODIFY_CALL to tell the caller to convert
1787 * from the save regs, to a non-save regs function.
1789 if (flag & FTRACE_FL_ENABLED)
1790 return FTRACE_UPDATE_MAKE_CALL;
1791 else if (rec->flags & FTRACE_FL_REGS_EN)
1792 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1794 return FTRACE_UPDATE_MODIFY_CALL;
1798 /* If there's no more users, clear all flags */
1799 if (!(rec->flags & ~FTRACE_FL_MASK))
1802 /* Just disable the record (keep REGS state) */
1803 rec->flags &= ~FTRACE_FL_ENABLED;
1806 return FTRACE_UPDATE_MAKE_NOP;
1810 * ftrace_update_record, set a record that now is tracing or not
1811 * @rec: the record to update
1812 * @enable: set to 1 if the record is tracing, zero to force disable
1814 * The records that represent all functions that can be traced need
1815 * to be updated when tracing has been enabled.
1817 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1819 return ftrace_check_record(rec, enable, 1);
1823 * ftrace_test_record, check if the record has been enabled or not
1824 * @rec: the record to test
1825 * @enable: set to 1 to check if enabled, 0 if it is disabled
1827 * The arch code may need to test if a record is already set to
1828 * tracing to determine how to modify the function code that it
1831 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1833 return ftrace_check_record(rec, enable, 0);
1837 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1839 unsigned long ftrace_old_addr;
1840 unsigned long ftrace_addr;
1843 ret = ftrace_update_record(rec, enable);
1845 if (rec->flags & FTRACE_FL_REGS)
1846 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1848 ftrace_addr = (unsigned long)FTRACE_ADDR;
1851 case FTRACE_UPDATE_IGNORE:
1854 case FTRACE_UPDATE_MAKE_CALL:
1855 return ftrace_make_call(rec, ftrace_addr);
1857 case FTRACE_UPDATE_MAKE_NOP:
1858 return ftrace_make_nop(NULL, rec, ftrace_addr);
1860 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1861 case FTRACE_UPDATE_MODIFY_CALL:
1862 if (rec->flags & FTRACE_FL_REGS)
1863 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1865 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1867 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1870 return -1; /* unknow ftrace bug */
1873 void __weak ftrace_replace_code(int enable)
1875 struct dyn_ftrace *rec;
1876 struct ftrace_page *pg;
1879 if (unlikely(ftrace_disabled))
1882 do_for_each_ftrace_rec(pg, rec) {
1883 failed = __ftrace_replace_code(rec, enable);
1885 ftrace_bug(failed, rec->ip);
1886 /* Stop processing */
1889 } while_for_each_ftrace_rec();
1892 struct ftrace_rec_iter {
1893 struct ftrace_page *pg;
1898 * ftrace_rec_iter_start, start up iterating over traced functions
1900 * Returns an iterator handle that is used to iterate over all
1901 * the records that represent address locations where functions
1904 * May return NULL if no records are available.
1906 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1909 * We only use a single iterator.
1910 * Protected by the ftrace_lock mutex.
1912 static struct ftrace_rec_iter ftrace_rec_iter;
1913 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1915 iter->pg = ftrace_pages_start;
1918 /* Could have empty pages */
1919 while (iter->pg && !iter->pg->index)
1920 iter->pg = iter->pg->next;
1929 * ftrace_rec_iter_next, get the next record to process.
1930 * @iter: The handle to the iterator.
1932 * Returns the next iterator after the given iterator @iter.
1934 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1938 if (iter->index >= iter->pg->index) {
1939 iter->pg = iter->pg->next;
1942 /* Could have empty pages */
1943 while (iter->pg && !iter->pg->index)
1944 iter->pg = iter->pg->next;
1954 * ftrace_rec_iter_record, get the record at the iterator location
1955 * @iter: The current iterator location
1957 * Returns the record that the current @iter is at.
1959 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1961 return &iter->pg->records[iter->index];
1965 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1972 if (unlikely(ftrace_disabled))
1975 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1977 ftrace_bug(ret, ip);
1984 * archs can override this function if they must do something
1985 * before the modifying code is performed.
1987 int __weak ftrace_arch_code_modify_prepare(void)
1993 * archs can override this function if they must do something
1994 * after the modifying code is performed.
1996 int __weak ftrace_arch_code_modify_post_process(void)
2001 void ftrace_modify_all_code(int command)
2003 if (command & FTRACE_UPDATE_CALLS)
2004 ftrace_replace_code(1);
2005 else if (command & FTRACE_DISABLE_CALLS)
2006 ftrace_replace_code(0);
2008 if (command & FTRACE_UPDATE_TRACE_FUNC) {
2009 function_trace_op = set_function_trace_op;
2011 /* If irqs are disabled, we are in stop machine */
2012 if (!irqs_disabled())
2013 smp_call_function(ftrace_sync_ipi, NULL, 1);
2014 ftrace_update_ftrace_func(ftrace_trace_function);
2017 if (command & FTRACE_START_FUNC_RET)
2018 ftrace_enable_ftrace_graph_caller();
2019 else if (command & FTRACE_STOP_FUNC_RET)
2020 ftrace_disable_ftrace_graph_caller();
2023 static int __ftrace_modify_code(void *data)
2025 int *command = data;
2027 ftrace_modify_all_code(*command);
2033 * ftrace_run_stop_machine, go back to the stop machine method
2034 * @command: The command to tell ftrace what to do
2036 * If an arch needs to fall back to the stop machine method, the
2037 * it can call this function.
2039 void ftrace_run_stop_machine(int command)
2041 stop_machine(__ftrace_modify_code, &command, NULL);
2045 * arch_ftrace_update_code, modify the code to trace or not trace
2046 * @command: The command that needs to be done
2048 * Archs can override this function if it does not need to
2049 * run stop_machine() to modify code.
2051 void __weak arch_ftrace_update_code(int command)
2053 ftrace_run_stop_machine(command);
2056 static void ftrace_run_update_code(int command)
2060 ret = ftrace_arch_code_modify_prepare();
2061 FTRACE_WARN_ON(ret);
2065 * Do not call function tracer while we update the code.
2066 * We are in stop machine.
2068 function_trace_stop++;
2071 * By default we use stop_machine() to modify the code.
2072 * But archs can do what ever they want as long as it
2073 * is safe. The stop_machine() is the safest, but also
2074 * produces the most overhead.
2076 arch_ftrace_update_code(command);
2078 function_trace_stop--;
2080 ret = ftrace_arch_code_modify_post_process();
2081 FTRACE_WARN_ON(ret);
2084 static ftrace_func_t saved_ftrace_func;
2085 static int ftrace_start_up;
2086 static int global_start_up;
2088 static void ftrace_startup_enable(int command)
2090 if (saved_ftrace_func != ftrace_trace_function) {
2091 saved_ftrace_func = ftrace_trace_function;
2092 command |= FTRACE_UPDATE_TRACE_FUNC;
2095 if (!command || !ftrace_enabled)
2098 ftrace_run_update_code(command);
2101 static int ftrace_startup(struct ftrace_ops *ops, int command)
2103 bool hash_enable = true;
2106 if (unlikely(ftrace_disabled))
2109 ret = __register_ftrace_function(ops);
2114 command |= FTRACE_UPDATE_CALLS;
2116 /* ops marked global share the filter hashes */
2117 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2119 /* Don't update hash if global is already set */
2120 if (global_start_up)
2121 hash_enable = false;
2125 ops->flags |= FTRACE_OPS_FL_ENABLED;
2127 ftrace_hash_rec_enable(ops, 1);
2129 ftrace_startup_enable(command);
2134 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2136 bool hash_disable = true;
2139 if (unlikely(ftrace_disabled))
2142 ret = __unregister_ftrace_function(ops);
2148 * Just warn in case of unbalance, no need to kill ftrace, it's not
2149 * critical but the ftrace_call callers may be never nopped again after
2150 * further ftrace uses.
2152 WARN_ON_ONCE(ftrace_start_up < 0);
2154 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2157 WARN_ON_ONCE(global_start_up < 0);
2158 /* Don't update hash if global still has users */
2159 if (global_start_up) {
2160 WARN_ON_ONCE(!ftrace_start_up);
2161 hash_disable = false;
2166 ftrace_hash_rec_disable(ops, 1);
2168 if (ops != &global_ops || !global_start_up)
2169 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2171 command |= FTRACE_UPDATE_CALLS;
2173 if (saved_ftrace_func != ftrace_trace_function) {
2174 saved_ftrace_func = ftrace_trace_function;
2175 command |= FTRACE_UPDATE_TRACE_FUNC;
2178 if (!command || !ftrace_enabled) {
2180 * If these are control ops, they still need their
2181 * per_cpu field freed. Since, function tracing is
2182 * not currently active, we can just free them
2183 * without synchronizing all CPUs.
2185 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2186 control_ops_free(ops);
2190 ftrace_run_update_code(command);
2193 * Dynamic ops may be freed, we must make sure that all
2194 * callers are done before leaving this function.
2195 * The same goes for freeing the per_cpu data of the control
2198 * Again, normal synchronize_sched() is not good enough.
2199 * We need to do a hard force of sched synchronization.
2200 * This is because we use preempt_disable() to do RCU, but
2201 * the function tracers can be called where RCU is not watching
2202 * (like before user_exit()). We can not rely on the RCU
2203 * infrastructure to do the synchronization, thus we must do it
2206 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2207 schedule_on_each_cpu(ftrace_sync);
2209 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2210 control_ops_free(ops);
2216 static void ftrace_startup_sysctl(void)
2218 if (unlikely(ftrace_disabled))
2221 /* Force update next time */
2222 saved_ftrace_func = NULL;
2223 /* ftrace_start_up is true if we want ftrace running */
2224 if (ftrace_start_up)
2225 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2228 static void ftrace_shutdown_sysctl(void)
2230 if (unlikely(ftrace_disabled))
2233 /* ftrace_start_up is true if ftrace is running */
2234 if (ftrace_start_up)
2235 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2238 static cycle_t ftrace_update_time;
2239 static unsigned long ftrace_update_cnt;
2240 unsigned long ftrace_update_tot_cnt;
2242 static inline int ops_traces_mod(struct ftrace_ops *ops)
2245 * Filter_hash being empty will default to trace module.
2246 * But notrace hash requires a test of individual module functions.
2248 return ftrace_hash_empty(ops->filter_hash) &&
2249 ftrace_hash_empty(ops->notrace_hash);
2253 * Check if the current ops references the record.
2255 * If the ops traces all functions, then it was already accounted for.
2256 * If the ops does not trace the current record function, skip it.
2257 * If the ops ignores the function via notrace filter, skip it.
2260 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2262 /* If ops isn't enabled, ignore it */
2263 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2266 /* If ops traces all mods, we already accounted for it */
2267 if (ops_traces_mod(ops))
2270 /* The function must be in the filter */
2271 if (!ftrace_hash_empty(ops->filter_hash) &&
2272 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2275 /* If in notrace hash, we ignore it too */
2276 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2282 static int referenced_filters(struct dyn_ftrace *rec)
2284 struct ftrace_ops *ops;
2287 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2288 if (ops_references_rec(ops, rec))
2295 static int ftrace_update_code(struct module *mod)
2297 struct ftrace_page *pg;
2298 struct dyn_ftrace *p;
2299 cycle_t start, stop;
2300 unsigned long ref = 0;
2305 * When adding a module, we need to check if tracers are
2306 * currently enabled and if they are set to trace all functions.
2307 * If they are, we need to enable the module functions as well
2308 * as update the reference counts for those function records.
2311 struct ftrace_ops *ops;
2313 for (ops = ftrace_ops_list;
2314 ops != &ftrace_list_end; ops = ops->next) {
2315 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2316 if (ops_traces_mod(ops))
2324 start = ftrace_now(raw_smp_processor_id());
2325 ftrace_update_cnt = 0;
2327 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2329 for (i = 0; i < pg->index; i++) {
2332 /* If something went wrong, bail without enabling anything */
2333 if (unlikely(ftrace_disabled))
2336 p = &pg->records[i];
2338 cnt += referenced_filters(p);
2342 * Do the initial record conversion from mcount jump
2343 * to the NOP instructions.
2345 if (!ftrace_code_disable(mod, p))
2348 ftrace_update_cnt++;
2351 * If the tracing is enabled, go ahead and enable the record.
2353 * The reason not to enable the record immediatelly is the
2354 * inherent check of ftrace_make_nop/ftrace_make_call for
2355 * correct previous instructions. Making first the NOP
2356 * conversion puts the module to the correct state, thus
2357 * passing the ftrace_make_call check.
2359 if (ftrace_start_up && cnt) {
2360 int failed = __ftrace_replace_code(p, 1);
2362 ftrace_bug(failed, p->ip);
2367 ftrace_new_pgs = NULL;
2369 stop = ftrace_now(raw_smp_processor_id());
2370 ftrace_update_time = stop - start;
2371 ftrace_update_tot_cnt += ftrace_update_cnt;
2376 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2381 if (WARN_ON(!count))
2384 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2387 * We want to fill as much as possible. No more than a page
2390 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2394 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2397 /* if we can't allocate this size, try something smaller */
2404 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2413 static struct ftrace_page *
2414 ftrace_allocate_pages(unsigned long num_to_init)
2416 struct ftrace_page *start_pg;
2417 struct ftrace_page *pg;
2424 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2429 * Try to allocate as much as possible in one continues
2430 * location that fills in all of the space. We want to
2431 * waste as little space as possible.
2434 cnt = ftrace_allocate_records(pg, num_to_init);
2442 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2453 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2454 free_pages((unsigned long)pg->records, order);
2455 start_pg = pg->next;
2459 pr_info("ftrace: FAILED to allocate memory for functions\n");
2463 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2468 pr_info("ftrace: No functions to be traced?\n");
2472 cnt = num_to_init / ENTRIES_PER_PAGE;
2473 pr_info("ftrace: allocating %ld entries in %d pages\n",
2474 num_to_init, cnt + 1);
2479 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2481 struct ftrace_iterator {
2484 struct ftrace_page *pg;
2485 struct dyn_ftrace *func;
2486 struct ftrace_func_probe *probe;
2487 struct trace_parser parser;
2488 struct ftrace_hash *hash;
2489 struct ftrace_ops *ops;
2496 t_hash_next(struct seq_file *m, loff_t *pos)
2498 struct ftrace_iterator *iter = m->private;
2499 struct hlist_node *hnd = NULL;
2500 struct hlist_head *hhd;
2506 hnd = &iter->probe->node;
2508 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2511 hhd = &ftrace_func_hash[iter->hidx];
2513 if (hlist_empty(hhd)) {
2529 if (WARN_ON_ONCE(!hnd))
2532 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2537 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2539 struct ftrace_iterator *iter = m->private;
2543 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2546 if (iter->func_pos > *pos)
2550 for (l = 0; l <= (*pos - iter->func_pos); ) {
2551 p = t_hash_next(m, &l);
2558 /* Only set this if we have an item */
2559 iter->flags |= FTRACE_ITER_HASH;
2565 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2567 struct ftrace_func_probe *rec;
2570 if (WARN_ON_ONCE(!rec))
2573 if (rec->ops->print)
2574 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2576 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2579 seq_printf(m, ":%p", rec->data);
2586 t_next(struct seq_file *m, void *v, loff_t *pos)
2588 struct ftrace_iterator *iter = m->private;
2589 struct ftrace_ops *ops = iter->ops;
2590 struct dyn_ftrace *rec = NULL;
2592 if (unlikely(ftrace_disabled))
2595 if (iter->flags & FTRACE_ITER_HASH)
2596 return t_hash_next(m, pos);
2599 iter->pos = iter->func_pos = *pos;
2601 if (iter->flags & FTRACE_ITER_PRINTALL)
2602 return t_hash_start(m, pos);
2605 if (iter->idx >= iter->pg->index) {
2606 if (iter->pg->next) {
2607 iter->pg = iter->pg->next;
2612 rec = &iter->pg->records[iter->idx++];
2613 if (((iter->flags & FTRACE_ITER_FILTER) &&
2614 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2616 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2617 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2619 ((iter->flags & FTRACE_ITER_ENABLED) &&
2620 !(rec->flags & FTRACE_FL_ENABLED))) {
2628 return t_hash_start(m, pos);
2635 static void reset_iter_read(struct ftrace_iterator *iter)
2639 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2642 static void *t_start(struct seq_file *m, loff_t *pos)
2644 struct ftrace_iterator *iter = m->private;
2645 struct ftrace_ops *ops = iter->ops;
2649 mutex_lock(&ftrace_lock);
2651 if (unlikely(ftrace_disabled))
2655 * If an lseek was done, then reset and start from beginning.
2657 if (*pos < iter->pos)
2658 reset_iter_read(iter);
2661 * For set_ftrace_filter reading, if we have the filter
2662 * off, we can short cut and just print out that all
2663 * functions are enabled.
2665 if (iter->flags & FTRACE_ITER_FILTER &&
2666 ftrace_hash_empty(ops->filter_hash)) {
2668 return t_hash_start(m, pos);
2669 iter->flags |= FTRACE_ITER_PRINTALL;
2670 /* reset in case of seek/pread */
2671 iter->flags &= ~FTRACE_ITER_HASH;
2675 if (iter->flags & FTRACE_ITER_HASH)
2676 return t_hash_start(m, pos);
2679 * Unfortunately, we need to restart at ftrace_pages_start
2680 * every time we let go of the ftrace_mutex. This is because
2681 * those pointers can change without the lock.
2683 iter->pg = ftrace_pages_start;
2685 for (l = 0; l <= *pos; ) {
2686 p = t_next(m, p, &l);
2692 return t_hash_start(m, pos);
2697 static void t_stop(struct seq_file *m, void *p)
2699 mutex_unlock(&ftrace_lock);
2702 static int t_show(struct seq_file *m, void *v)
2704 struct ftrace_iterator *iter = m->private;
2705 struct dyn_ftrace *rec;
2707 if (iter->flags & FTRACE_ITER_HASH)
2708 return t_hash_show(m, iter);
2710 if (iter->flags & FTRACE_ITER_PRINTALL) {
2711 seq_printf(m, "#### all functions enabled ####\n");
2720 seq_printf(m, "%ps", (void *)rec->ip);
2721 if (iter->flags & FTRACE_ITER_ENABLED)
2722 seq_printf(m, " (%ld)%s",
2723 rec->flags & ~FTRACE_FL_MASK,
2724 rec->flags & FTRACE_FL_REGS ? " R" : "");
2725 seq_printf(m, "\n");
2730 static const struct seq_operations show_ftrace_seq_ops = {
2738 ftrace_avail_open(struct inode *inode, struct file *file)
2740 struct ftrace_iterator *iter;
2742 if (unlikely(ftrace_disabled))
2745 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2747 iter->pg = ftrace_pages_start;
2748 iter->ops = &global_ops;
2751 return iter ? 0 : -ENOMEM;
2755 ftrace_enabled_open(struct inode *inode, struct file *file)
2757 struct ftrace_iterator *iter;
2759 if (unlikely(ftrace_disabled))
2762 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2764 iter->pg = ftrace_pages_start;
2765 iter->flags = FTRACE_ITER_ENABLED;
2766 iter->ops = &global_ops;
2769 return iter ? 0 : -ENOMEM;
2772 static void ftrace_filter_reset(struct ftrace_hash *hash)
2774 mutex_lock(&ftrace_lock);
2775 ftrace_hash_clear(hash);
2776 mutex_unlock(&ftrace_lock);
2780 * ftrace_regex_open - initialize function tracer filter files
2781 * @ops: The ftrace_ops that hold the hash filters
2782 * @flag: The type of filter to process
2783 * @inode: The inode, usually passed in to your open routine
2784 * @file: The file, usually passed in to your open routine
2786 * ftrace_regex_open() initializes the filter files for the
2787 * @ops. Depending on @flag it may process the filter hash or
2788 * the notrace hash of @ops. With this called from the open
2789 * routine, you can use ftrace_filter_write() for the write
2790 * routine if @flag has FTRACE_ITER_FILTER set, or
2791 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2792 * ftrace_filter_lseek() should be used as the lseek routine, and
2793 * release must call ftrace_regex_release().
2796 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2797 struct inode *inode, struct file *file)
2799 struct ftrace_iterator *iter;
2800 struct ftrace_hash *hash;
2803 ftrace_ops_init(ops);
2805 if (unlikely(ftrace_disabled))
2808 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2812 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2820 mutex_lock(&ops->regex_lock);
2822 if (flag & FTRACE_ITER_NOTRACE)
2823 hash = ops->notrace_hash;
2825 hash = ops->filter_hash;
2827 if (file->f_mode & FMODE_WRITE) {
2828 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2830 trace_parser_put(&iter->parser);
2837 if ((file->f_mode & FMODE_WRITE) &&
2838 (file->f_flags & O_TRUNC))
2839 ftrace_filter_reset(iter->hash);
2841 if (file->f_mode & FMODE_READ) {
2842 iter->pg = ftrace_pages_start;
2844 ret = seq_open(file, &show_ftrace_seq_ops);
2846 struct seq_file *m = file->private_data;
2850 free_ftrace_hash(iter->hash);
2851 trace_parser_put(&iter->parser);
2855 file->private_data = iter;
2858 mutex_unlock(&ops->regex_lock);
2864 ftrace_filter_open(struct inode *inode, struct file *file)
2866 return ftrace_regex_open(&global_ops,
2867 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2872 ftrace_notrace_open(struct inode *inode, struct file *file)
2874 return ftrace_regex_open(&global_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);
3360 int register_ftrace_command(struct ftrace_func_command *cmd)
3362 struct ftrace_func_command *p;
3365 mutex_lock(&ftrace_cmd_mutex);
3366 list_for_each_entry(p, &ftrace_commands, list) {
3367 if (strcmp(cmd->name, p->name) == 0) {
3372 list_add(&cmd->list, &ftrace_commands);
3374 mutex_unlock(&ftrace_cmd_mutex);
3379 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3381 struct ftrace_func_command *p, *n;
3384 mutex_lock(&ftrace_cmd_mutex);
3385 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3386 if (strcmp(cmd->name, p->name) == 0) {
3388 list_del_init(&p->list);
3393 mutex_unlock(&ftrace_cmd_mutex);
3398 static int ftrace_process_regex(struct ftrace_hash *hash,
3399 char *buff, int len, int enable)
3401 char *func, *command, *next = buff;
3402 struct ftrace_func_command *p;
3405 func = strsep(&next, ":");
3408 ret = ftrace_match_records(hash, func, len);
3418 command = strsep(&next, ":");
3420 mutex_lock(&ftrace_cmd_mutex);
3421 list_for_each_entry(p, &ftrace_commands, list) {
3422 if (strcmp(p->name, command) == 0) {
3423 ret = p->func(hash, func, command, next, enable);
3428 mutex_unlock(&ftrace_cmd_mutex);
3434 ftrace_regex_write(struct file *file, const char __user *ubuf,
3435 size_t cnt, loff_t *ppos, int enable)
3437 struct ftrace_iterator *iter;
3438 struct trace_parser *parser;
3444 if (file->f_mode & FMODE_READ) {
3445 struct seq_file *m = file->private_data;
3448 iter = file->private_data;
3450 if (unlikely(ftrace_disabled))
3453 /* iter->hash is a local copy, so we don't need regex_lock */
3455 parser = &iter->parser;
3456 read = trace_get_user(parser, ubuf, cnt, ppos);
3458 if (read >= 0 && trace_parser_loaded(parser) &&
3459 !trace_parser_cont(parser)) {
3460 ret = ftrace_process_regex(iter->hash, parser->buffer,
3461 parser->idx, enable);
3462 trace_parser_clear(parser);
3473 ftrace_filter_write(struct file *file, const char __user *ubuf,
3474 size_t cnt, loff_t *ppos)
3476 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3480 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3481 size_t cnt, loff_t *ppos)
3483 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3487 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3489 struct ftrace_func_entry *entry;
3491 if (!ftrace_location(ip))
3495 entry = ftrace_lookup_ip(hash, ip);
3498 free_hash_entry(hash, entry);
3502 return add_hash_entry(hash, ip);
3506 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3507 unsigned long ip, int remove, int reset, int enable)
3509 struct ftrace_hash **orig_hash;
3510 struct ftrace_hash *hash;
3513 /* All global ops uses the global ops filters */
3514 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3517 if (unlikely(ftrace_disabled))
3520 mutex_lock(&ops->regex_lock);
3523 orig_hash = &ops->filter_hash;
3525 orig_hash = &ops->notrace_hash;
3527 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3530 goto out_regex_unlock;
3534 ftrace_filter_reset(hash);
3535 if (buf && !ftrace_match_records(hash, buf, len)) {
3537 goto out_regex_unlock;
3540 ret = ftrace_match_addr(hash, ip, remove);
3542 goto out_regex_unlock;
3545 mutex_lock(&ftrace_lock);
3546 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3547 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3549 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3551 mutex_unlock(&ftrace_lock);
3554 mutex_unlock(&ops->regex_lock);
3556 free_ftrace_hash(hash);
3561 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3562 int reset, int enable)
3564 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3568 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3569 * @ops - the ops to set the filter with
3570 * @ip - the address to add to or remove from the filter.
3571 * @remove - non zero to remove the ip from the filter
3572 * @reset - non zero to reset all filters before applying this filter.
3574 * Filters denote which functions should be enabled when tracing is enabled
3575 * If @ip is NULL, it failes to update filter.
3577 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3578 int remove, int reset)
3580 ftrace_ops_init(ops);
3581 return ftrace_set_addr(ops, ip, remove, reset, 1);
3583 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3586 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3587 int reset, int enable)
3589 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3593 * ftrace_set_filter - set a function to filter on in ftrace
3594 * @ops - the ops to set the filter with
3595 * @buf - the string that holds the function filter text.
3596 * @len - the length of the string.
3597 * @reset - non zero to reset all filters before applying this filter.
3599 * Filters denote which functions should be enabled when tracing is enabled.
3600 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3602 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3605 ftrace_ops_init(ops);
3606 return ftrace_set_regex(ops, buf, len, reset, 1);
3608 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3611 * ftrace_set_notrace - set a function to not trace in ftrace
3612 * @ops - the ops to set the notrace filter with
3613 * @buf - the string that holds the function notrace text.
3614 * @len - the length of the string.
3615 * @reset - non zero to reset all filters before applying this filter.
3617 * Notrace Filters denote which functions should not be enabled when tracing
3618 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3621 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3624 ftrace_ops_init(ops);
3625 return ftrace_set_regex(ops, buf, len, reset, 0);
3627 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3629 * ftrace_set_filter - set a function to filter on in ftrace
3630 * @ops - the ops to set the filter with
3631 * @buf - the string that holds the function filter text.
3632 * @len - the length of the string.
3633 * @reset - non zero to reset all filters before applying this filter.
3635 * Filters denote which functions should be enabled when tracing is enabled.
3636 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3638 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3640 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3642 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3645 * ftrace_set_notrace - set a function to not trace in ftrace
3646 * @ops - the ops to set the notrace filter with
3647 * @buf - the string that holds the function notrace text.
3648 * @len - the length of the string.
3649 * @reset - non zero to reset all filters before applying this filter.
3651 * Notrace Filters denote which functions should not be enabled when tracing
3652 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3655 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3657 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3659 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3662 * command line interface to allow users to set filters on boot up.
3664 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3665 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3666 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3668 static int __init set_ftrace_notrace(char *str)
3670 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3673 __setup("ftrace_notrace=", set_ftrace_notrace);
3675 static int __init set_ftrace_filter(char *str)
3677 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3680 __setup("ftrace_filter=", set_ftrace_filter);
3682 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3683 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3684 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3686 static int __init set_graph_function(char *str)
3688 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3691 __setup("ftrace_graph_filter=", set_graph_function);
3693 static void __init set_ftrace_early_graph(char *buf)
3699 func = strsep(&buf, ",");
3700 /* we allow only one expression at a time */
3701 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3704 printk(KERN_DEBUG "ftrace: function %s not "
3705 "traceable\n", func);
3708 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3711 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3715 ftrace_ops_init(ops);
3718 func = strsep(&buf, ",");
3719 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3723 static void __init set_ftrace_early_filters(void)
3725 if (ftrace_filter_buf[0])
3726 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3727 if (ftrace_notrace_buf[0])
3728 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3729 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3730 if (ftrace_graph_buf[0])
3731 set_ftrace_early_graph(ftrace_graph_buf);
3732 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3735 int ftrace_regex_release(struct inode *inode, struct file *file)
3737 struct seq_file *m = (struct seq_file *)file->private_data;
3738 struct ftrace_iterator *iter;
3739 struct ftrace_hash **orig_hash;
3740 struct trace_parser *parser;
3744 if (file->f_mode & FMODE_READ) {
3746 seq_release(inode, file);
3748 iter = file->private_data;
3750 parser = &iter->parser;
3751 if (trace_parser_loaded(parser)) {
3752 parser->buffer[parser->idx] = 0;
3753 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3756 trace_parser_put(parser);
3758 mutex_lock(&iter->ops->regex_lock);
3760 if (file->f_mode & FMODE_WRITE) {
3761 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3764 orig_hash = &iter->ops->filter_hash;
3766 orig_hash = &iter->ops->notrace_hash;
3768 mutex_lock(&ftrace_lock);
3769 ret = ftrace_hash_move(iter->ops, filter_hash,
3770 orig_hash, iter->hash);
3771 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3773 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3775 mutex_unlock(&ftrace_lock);
3778 mutex_unlock(&iter->ops->regex_lock);
3779 free_ftrace_hash(iter->hash);
3785 static const struct file_operations ftrace_avail_fops = {
3786 .open = ftrace_avail_open,
3788 .llseek = seq_lseek,
3789 .release = seq_release_private,
3792 static const struct file_operations ftrace_enabled_fops = {
3793 .open = ftrace_enabled_open,
3795 .llseek = seq_lseek,
3796 .release = seq_release_private,
3799 static const struct file_operations ftrace_filter_fops = {
3800 .open = ftrace_filter_open,
3802 .write = ftrace_filter_write,
3803 .llseek = ftrace_filter_lseek,
3804 .release = ftrace_regex_release,
3807 static const struct file_operations ftrace_notrace_fops = {
3808 .open = ftrace_notrace_open,
3810 .write = ftrace_notrace_write,
3811 .llseek = ftrace_filter_lseek,
3812 .release = ftrace_regex_release,
3815 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3817 static DEFINE_MUTEX(graph_lock);
3819 int ftrace_graph_count;
3820 int ftrace_graph_filter_enabled;
3821 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3824 __g_next(struct seq_file *m, loff_t *pos)
3826 if (*pos >= ftrace_graph_count)
3828 return &ftrace_graph_funcs[*pos];
3832 g_next(struct seq_file *m, void *v, loff_t *pos)
3835 return __g_next(m, pos);
3838 static void *g_start(struct seq_file *m, loff_t *pos)
3840 mutex_lock(&graph_lock);
3842 /* Nothing, tell g_show to print all functions are enabled */
3843 if (!ftrace_graph_filter_enabled && !*pos)
3846 return __g_next(m, pos);
3849 static void g_stop(struct seq_file *m, void *p)
3851 mutex_unlock(&graph_lock);
3854 static int g_show(struct seq_file *m, void *v)
3856 unsigned long *ptr = v;
3861 if (ptr == (unsigned long *)1) {
3862 seq_printf(m, "#### all functions enabled ####\n");
3866 seq_printf(m, "%ps\n", (void *)*ptr);
3871 static const struct seq_operations ftrace_graph_seq_ops = {
3879 ftrace_graph_open(struct inode *inode, struct file *file)
3883 if (unlikely(ftrace_disabled))
3886 mutex_lock(&graph_lock);
3887 if ((file->f_mode & FMODE_WRITE) &&
3888 (file->f_flags & O_TRUNC)) {
3889 ftrace_graph_filter_enabled = 0;
3890 ftrace_graph_count = 0;
3891 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3893 mutex_unlock(&graph_lock);
3895 if (file->f_mode & FMODE_READ)
3896 ret = seq_open(file, &ftrace_graph_seq_ops);
3902 ftrace_graph_release(struct inode *inode, struct file *file)
3904 if (file->f_mode & FMODE_READ)
3905 seq_release(inode, file);
3910 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3912 struct dyn_ftrace *rec;
3913 struct ftrace_page *pg;
3922 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3923 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3926 search_len = strlen(search);
3928 mutex_lock(&ftrace_lock);
3930 if (unlikely(ftrace_disabled)) {
3931 mutex_unlock(&ftrace_lock);
3935 do_for_each_ftrace_rec(pg, rec) {
3937 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3938 /* if it is in the array */
3940 for (i = 0; i < *idx; i++) {
3941 if (array[i] == rec->ip) {
3950 array[(*idx)++] = rec->ip;
3951 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3956 array[i] = array[--(*idx)];
3962 } while_for_each_ftrace_rec();
3964 mutex_unlock(&ftrace_lock);
3969 ftrace_graph_filter_enabled = !!(*idx);
3975 ftrace_graph_write(struct file *file, const char __user *ubuf,
3976 size_t cnt, loff_t *ppos)
3978 struct trace_parser parser;
3984 mutex_lock(&graph_lock);
3986 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3991 read = trace_get_user(&parser, ubuf, cnt, ppos);
3993 if (read >= 0 && trace_parser_loaded((&parser))) {
3994 parser.buffer[parser.idx] = 0;
3996 /* we allow only one expression at a time */
3997 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
4006 trace_parser_put(&parser);
4008 mutex_unlock(&graph_lock);
4013 static const struct file_operations ftrace_graph_fops = {
4014 .open = ftrace_graph_open,
4016 .write = ftrace_graph_write,
4017 .llseek = ftrace_filter_lseek,
4018 .release = ftrace_graph_release,
4020 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4022 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4025 trace_create_file("available_filter_functions", 0444,
4026 d_tracer, NULL, &ftrace_avail_fops);
4028 trace_create_file("enabled_functions", 0444,
4029 d_tracer, NULL, &ftrace_enabled_fops);
4031 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4032 NULL, &ftrace_filter_fops);
4034 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
4035 NULL, &ftrace_notrace_fops);
4037 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4038 trace_create_file("set_graph_function", 0444, d_tracer,
4040 &ftrace_graph_fops);
4041 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4046 static int ftrace_cmp_ips(const void *a, const void *b)
4048 const unsigned long *ipa = a;
4049 const unsigned long *ipb = b;
4058 static void ftrace_swap_ips(void *a, void *b, int size)
4060 unsigned long *ipa = a;
4061 unsigned long *ipb = b;
4069 static int ftrace_process_locs(struct module *mod,
4070 unsigned long *start,
4073 struct ftrace_page *start_pg;
4074 struct ftrace_page *pg;
4075 struct dyn_ftrace *rec;
4076 unsigned long count;
4079 unsigned long flags = 0; /* Shut up gcc */
4082 count = end - start;
4087 sort(start, count, sizeof(*start),
4088 ftrace_cmp_ips, ftrace_swap_ips);
4090 start_pg = ftrace_allocate_pages(count);
4094 mutex_lock(&ftrace_lock);
4097 * Core and each module needs their own pages, as
4098 * modules will free them when they are removed.
4099 * Force a new page to be allocated for modules.
4102 WARN_ON(ftrace_pages || ftrace_pages_start);
4103 /* First initialization */
4104 ftrace_pages = ftrace_pages_start = start_pg;
4109 if (WARN_ON(ftrace_pages->next)) {
4110 /* Hmm, we have free pages? */
4111 while (ftrace_pages->next)
4112 ftrace_pages = ftrace_pages->next;
4115 ftrace_pages->next = start_pg;
4121 addr = ftrace_call_adjust(*p++);
4123 * Some architecture linkers will pad between
4124 * the different mcount_loc sections of different
4125 * object files to satisfy alignments.
4126 * Skip any NULL pointers.
4131 if (pg->index == pg->size) {
4132 /* We should have allocated enough */
4133 if (WARN_ON(!pg->next))
4138 rec = &pg->records[pg->index++];
4142 /* We should have used all pages */
4145 /* Assign the last page to ftrace_pages */
4148 /* These new locations need to be initialized */
4149 ftrace_new_pgs = start_pg;
4152 * We only need to disable interrupts on start up
4153 * because we are modifying code that an interrupt
4154 * may execute, and the modification is not atomic.
4155 * But for modules, nothing runs the code we modify
4156 * until we are finished with it, and there's no
4157 * reason to cause large interrupt latencies while we do it.
4160 local_irq_save(flags);
4161 ftrace_update_code(mod);
4163 local_irq_restore(flags);
4166 mutex_unlock(&ftrace_lock);
4171 #ifdef CONFIG_MODULES
4173 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4175 void ftrace_release_mod(struct module *mod)
4177 struct dyn_ftrace *rec;
4178 struct ftrace_page **last_pg;
4179 struct ftrace_page *pg;
4182 mutex_lock(&ftrace_lock);
4184 if (ftrace_disabled)
4188 * Each module has its own ftrace_pages, remove
4189 * them from the list.
4191 last_pg = &ftrace_pages_start;
4192 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4193 rec = &pg->records[0];
4194 if (within_module_core(rec->ip, mod)) {
4196 * As core pages are first, the first
4197 * page should never be a module page.
4199 if (WARN_ON(pg == ftrace_pages_start))
4202 /* Check if we are deleting the last page */
4203 if (pg == ftrace_pages)
4204 ftrace_pages = next_to_ftrace_page(last_pg);
4206 *last_pg = pg->next;
4207 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4208 free_pages((unsigned long)pg->records, order);
4211 last_pg = &pg->next;
4214 mutex_unlock(&ftrace_lock);
4217 static void ftrace_init_module(struct module *mod,
4218 unsigned long *start, unsigned long *end)
4220 if (ftrace_disabled || start == end)
4222 ftrace_process_locs(mod, start, end);
4225 static int ftrace_module_notify_enter(struct notifier_block *self,
4226 unsigned long val, void *data)
4228 struct module *mod = data;
4230 if (val == MODULE_STATE_COMING)
4231 ftrace_init_module(mod, mod->ftrace_callsites,
4232 mod->ftrace_callsites +
4233 mod->num_ftrace_callsites);
4237 static int ftrace_module_notify_exit(struct notifier_block *self,
4238 unsigned long val, void *data)
4240 struct module *mod = data;
4242 if (val == MODULE_STATE_GOING)
4243 ftrace_release_mod(mod);
4248 static int ftrace_module_notify_enter(struct notifier_block *self,
4249 unsigned long val, void *data)
4253 static int ftrace_module_notify_exit(struct notifier_block *self,
4254 unsigned long val, void *data)
4258 #endif /* CONFIG_MODULES */
4260 struct notifier_block ftrace_module_enter_nb = {
4261 .notifier_call = ftrace_module_notify_enter,
4262 .priority = INT_MAX, /* Run before anything that can use kprobes */
4265 struct notifier_block ftrace_module_exit_nb = {
4266 .notifier_call = ftrace_module_notify_exit,
4267 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4270 extern unsigned long __start_mcount_loc[];
4271 extern unsigned long __stop_mcount_loc[];
4273 void __init ftrace_init(void)
4275 unsigned long count, addr, flags;
4278 /* Keep the ftrace pointer to the stub */
4279 addr = (unsigned long)ftrace_stub;
4281 local_irq_save(flags);
4282 ftrace_dyn_arch_init(&addr);
4283 local_irq_restore(flags);
4285 /* ftrace_dyn_arch_init places the return code in addr */
4289 count = __stop_mcount_loc - __start_mcount_loc;
4291 ret = ftrace_dyn_table_alloc(count);
4295 last_ftrace_enabled = ftrace_enabled = 1;
4297 ret = ftrace_process_locs(NULL,
4301 ret = register_module_notifier(&ftrace_module_enter_nb);
4303 pr_warning("Failed to register trace ftrace module enter notifier\n");
4305 ret = register_module_notifier(&ftrace_module_exit_nb);
4307 pr_warning("Failed to register trace ftrace module exit notifier\n");
4309 set_ftrace_early_filters();
4313 ftrace_disabled = 1;
4318 static struct ftrace_ops global_ops = {
4319 .func = ftrace_stub,
4320 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4321 INIT_REGEX_LOCK(global_ops)
4324 static int __init ftrace_nodyn_init(void)
4329 core_initcall(ftrace_nodyn_init);
4331 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4332 static inline void ftrace_startup_enable(int command) { }
4333 /* Keep as macros so we do not need to define the commands */
4334 # define ftrace_startup(ops, command) \
4336 int ___ret = __register_ftrace_function(ops); \
4338 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4341 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4343 # define ftrace_startup_sysctl() do { } while (0)
4344 # define ftrace_shutdown_sysctl() do { } while (0)
4347 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4352 #endif /* CONFIG_DYNAMIC_FTRACE */
4355 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4356 struct ftrace_ops *op, struct pt_regs *regs)
4358 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4362 * Some of the ops may be dynamically allocated,
4363 * they must be freed after a synchronize_sched().
4365 preempt_disable_notrace();
4366 trace_recursion_set(TRACE_CONTROL_BIT);
4367 do_for_each_ftrace_op(op, ftrace_control_list) {
4368 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4369 !ftrace_function_local_disabled(op) &&
4370 ftrace_ops_test(op, ip, regs))
4371 op->func(ip, parent_ip, op, regs);
4372 } while_for_each_ftrace_op(op);
4373 trace_recursion_clear(TRACE_CONTROL_BIT);
4374 preempt_enable_notrace();
4377 static struct ftrace_ops control_ops = {
4378 .func = ftrace_ops_control_func,
4379 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4380 INIT_REGEX_LOCK(control_ops)
4384 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4385 struct ftrace_ops *ignored, struct pt_regs *regs)
4387 struct ftrace_ops *op;
4390 if (function_trace_stop)
4393 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4398 * Some of the ops may be dynamically allocated,
4399 * they must be freed after a synchronize_sched().
4401 preempt_disable_notrace();
4402 do_for_each_ftrace_op(op, ftrace_ops_list) {
4403 if (ftrace_ops_test(op, ip, regs))
4404 op->func(ip, parent_ip, op, regs);
4405 } while_for_each_ftrace_op(op);
4406 preempt_enable_notrace();
4407 trace_clear_recursion(bit);
4411 * Some archs only support passing ip and parent_ip. Even though
4412 * the list function ignores the op parameter, we do not want any
4413 * C side effects, where a function is called without the caller
4414 * sending a third parameter.
4415 * Archs are to support both the regs and ftrace_ops at the same time.
4416 * If they support ftrace_ops, it is assumed they support regs.
4417 * If call backs want to use regs, they must either check for regs
4418 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4419 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4420 * An architecture can pass partial regs with ftrace_ops and still
4421 * set the ARCH_SUPPORT_FTARCE_OPS.
4423 #if ARCH_SUPPORTS_FTRACE_OPS
4424 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4425 struct ftrace_ops *op, struct pt_regs *regs)
4427 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4430 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4432 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4436 static void clear_ftrace_swapper(void)
4438 struct task_struct *p;
4442 for_each_online_cpu(cpu) {
4444 clear_tsk_trace_trace(p);
4449 static void set_ftrace_swapper(void)
4451 struct task_struct *p;
4455 for_each_online_cpu(cpu) {
4457 set_tsk_trace_trace(p);
4462 static void clear_ftrace_pid(struct pid *pid)
4464 struct task_struct *p;
4467 do_each_pid_task(pid, PIDTYPE_PID, p) {
4468 clear_tsk_trace_trace(p);
4469 } while_each_pid_task(pid, PIDTYPE_PID, p);
4475 static void set_ftrace_pid(struct pid *pid)
4477 struct task_struct *p;
4480 do_each_pid_task(pid, PIDTYPE_PID, p) {
4481 set_tsk_trace_trace(p);
4482 } while_each_pid_task(pid, PIDTYPE_PID, p);
4486 static void clear_ftrace_pid_task(struct pid *pid)
4488 if (pid == ftrace_swapper_pid)
4489 clear_ftrace_swapper();
4491 clear_ftrace_pid(pid);
4494 static void set_ftrace_pid_task(struct pid *pid)
4496 if (pid == ftrace_swapper_pid)
4497 set_ftrace_swapper();
4499 set_ftrace_pid(pid);
4502 static int ftrace_pid_add(int p)
4505 struct ftrace_pid *fpid;
4508 mutex_lock(&ftrace_lock);
4511 pid = ftrace_swapper_pid;
4513 pid = find_get_pid(p);
4520 list_for_each_entry(fpid, &ftrace_pids, list)
4521 if (fpid->pid == pid)
4526 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4530 list_add(&fpid->list, &ftrace_pids);
4533 set_ftrace_pid_task(pid);
4535 ftrace_update_pid_func();
4536 ftrace_startup_enable(0);
4538 mutex_unlock(&ftrace_lock);
4542 if (pid != ftrace_swapper_pid)
4546 mutex_unlock(&ftrace_lock);
4550 static void ftrace_pid_reset(void)
4552 struct ftrace_pid *fpid, *safe;
4554 mutex_lock(&ftrace_lock);
4555 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4556 struct pid *pid = fpid->pid;
4558 clear_ftrace_pid_task(pid);
4560 list_del(&fpid->list);
4564 ftrace_update_pid_func();
4565 ftrace_startup_enable(0);
4567 mutex_unlock(&ftrace_lock);
4570 static void *fpid_start(struct seq_file *m, loff_t *pos)
4572 mutex_lock(&ftrace_lock);
4574 if (list_empty(&ftrace_pids) && (!*pos))
4577 return seq_list_start(&ftrace_pids, *pos);
4580 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4585 return seq_list_next(v, &ftrace_pids, pos);
4588 static void fpid_stop(struct seq_file *m, void *p)
4590 mutex_unlock(&ftrace_lock);
4593 static int fpid_show(struct seq_file *m, void *v)
4595 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4597 if (v == (void *)1) {
4598 seq_printf(m, "no pid\n");
4602 if (fpid->pid == ftrace_swapper_pid)
4603 seq_printf(m, "swapper tasks\n");
4605 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4610 static const struct seq_operations ftrace_pid_sops = {
4611 .start = fpid_start,
4618 ftrace_pid_open(struct inode *inode, struct file *file)
4622 if ((file->f_mode & FMODE_WRITE) &&
4623 (file->f_flags & O_TRUNC))
4626 if (file->f_mode & FMODE_READ)
4627 ret = seq_open(file, &ftrace_pid_sops);
4633 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4634 size_t cnt, loff_t *ppos)
4640 if (cnt >= sizeof(buf))
4643 if (copy_from_user(&buf, ubuf, cnt))
4649 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4650 * to clean the filter quietly.
4652 tmp = strstrip(buf);
4653 if (strlen(tmp) == 0)
4656 ret = kstrtol(tmp, 10, &val);
4660 ret = ftrace_pid_add(val);
4662 return ret ? ret : cnt;
4666 ftrace_pid_release(struct inode *inode, struct file *file)
4668 if (file->f_mode & FMODE_READ)
4669 seq_release(inode, file);
4674 static const struct file_operations ftrace_pid_fops = {
4675 .open = ftrace_pid_open,
4676 .write = ftrace_pid_write,
4678 .llseek = ftrace_filter_lseek,
4679 .release = ftrace_pid_release,
4682 static __init int ftrace_init_debugfs(void)
4684 struct dentry *d_tracer;
4686 d_tracer = tracing_init_dentry();
4690 ftrace_init_dyn_debugfs(d_tracer);
4692 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4693 NULL, &ftrace_pid_fops);
4695 ftrace_profile_debugfs(d_tracer);
4699 fs_initcall(ftrace_init_debugfs);
4702 * ftrace_kill - kill ftrace
4704 * This function should be used by panic code. It stops ftrace
4705 * but in a not so nice way. If you need to simply kill ftrace
4706 * from a non-atomic section, use ftrace_kill.
4708 void ftrace_kill(void)
4710 ftrace_disabled = 1;
4712 clear_ftrace_function();
4716 * Test if ftrace is dead or not.
4718 int ftrace_is_dead(void)
4720 return ftrace_disabled;
4724 * register_ftrace_function - register a function for profiling
4725 * @ops - ops structure that holds the function for profiling.
4727 * Register a function to be called by all functions in the
4730 * Note: @ops->func and all the functions it calls must be labeled
4731 * with "notrace", otherwise it will go into a
4734 int register_ftrace_function(struct ftrace_ops *ops)
4738 ftrace_ops_init(ops);
4740 mutex_lock(&ftrace_lock);
4742 ret = ftrace_startup(ops, 0);
4744 mutex_unlock(&ftrace_lock);
4748 EXPORT_SYMBOL_GPL(register_ftrace_function);
4751 * unregister_ftrace_function - unregister a function for profiling.
4752 * @ops - ops structure that holds the function to unregister
4754 * Unregister a function that was added to be called by ftrace profiling.
4756 int unregister_ftrace_function(struct ftrace_ops *ops)
4760 mutex_lock(&ftrace_lock);
4761 ret = ftrace_shutdown(ops, 0);
4762 mutex_unlock(&ftrace_lock);
4766 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4769 ftrace_enable_sysctl(struct ctl_table *table, int write,
4770 void __user *buffer, size_t *lenp,
4775 mutex_lock(&ftrace_lock);
4777 if (unlikely(ftrace_disabled))
4780 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4782 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4785 last_ftrace_enabled = !!ftrace_enabled;
4787 if (ftrace_enabled) {
4789 ftrace_startup_sysctl();
4791 /* we are starting ftrace again */
4792 if (ftrace_ops_list != &ftrace_list_end)
4793 update_ftrace_function();
4796 /* stopping ftrace calls (just send to ftrace_stub) */
4797 ftrace_trace_function = ftrace_stub;
4799 ftrace_shutdown_sysctl();
4803 mutex_unlock(&ftrace_lock);
4807 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4809 static int ftrace_graph_active;
4810 static struct notifier_block ftrace_suspend_notifier;
4812 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4817 /* The callbacks that hook a function */
4818 trace_func_graph_ret_t ftrace_graph_return =
4819 (trace_func_graph_ret_t)ftrace_stub;
4820 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4821 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
4823 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4824 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4828 unsigned long flags;
4829 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4830 struct task_struct *g, *t;
4832 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4833 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4834 * sizeof(struct ftrace_ret_stack),
4836 if (!ret_stack_list[i]) {
4844 read_lock_irqsave(&tasklist_lock, flags);
4845 do_each_thread(g, t) {
4851 if (t->ret_stack == NULL) {
4852 atomic_set(&t->tracing_graph_pause, 0);
4853 atomic_set(&t->trace_overrun, 0);
4854 t->curr_ret_stack = -1;
4855 /* Make sure the tasks see the -1 first: */
4857 t->ret_stack = ret_stack_list[start++];
4859 } while_each_thread(g, t);
4862 read_unlock_irqrestore(&tasklist_lock, flags);
4864 for (i = start; i < end; i++)
4865 kfree(ret_stack_list[i]);
4870 ftrace_graph_probe_sched_switch(void *ignore,
4871 struct task_struct *prev, struct task_struct *next)
4873 unsigned long long timestamp;
4877 * Does the user want to count the time a function was asleep.
4878 * If so, do not update the time stamps.
4880 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4883 timestamp = trace_clock_local();
4885 prev->ftrace_timestamp = timestamp;
4887 /* only process tasks that we timestamped */
4888 if (!next->ftrace_timestamp)
4892 * Update all the counters in next to make up for the
4893 * time next was sleeping.
4895 timestamp -= next->ftrace_timestamp;
4897 for (index = next->curr_ret_stack; index >= 0; index--)
4898 next->ret_stack[index].calltime += timestamp;
4901 /* Allocate a return stack for each task */
4902 static int start_graph_tracing(void)
4904 struct ftrace_ret_stack **ret_stack_list;
4907 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4908 sizeof(struct ftrace_ret_stack *),
4911 if (!ret_stack_list)
4914 /* The cpu_boot init_task->ret_stack will never be freed */
4915 for_each_online_cpu(cpu) {
4916 if (!idle_task(cpu)->ret_stack)
4917 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4921 ret = alloc_retstack_tasklist(ret_stack_list);
4922 } while (ret == -EAGAIN);
4925 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4927 pr_info("ftrace_graph: Couldn't activate tracepoint"
4928 " probe to kernel_sched_switch\n");
4931 kfree(ret_stack_list);
4936 * Hibernation protection.
4937 * The state of the current task is too much unstable during
4938 * suspend/restore to disk. We want to protect against that.
4941 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4945 case PM_HIBERNATION_PREPARE:
4946 pause_graph_tracing();
4949 case PM_POST_HIBERNATION:
4950 unpause_graph_tracing();
4956 /* Just a place holder for function graph */
4957 static struct ftrace_ops fgraph_ops __read_mostly = {
4958 .func = ftrace_stub,
4959 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
4960 FTRACE_OPS_FL_RECURSION_SAFE,
4963 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
4965 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
4967 return __ftrace_graph_entry(trace);
4971 * The function graph tracer should only trace the functions defined
4972 * by set_ftrace_filter and set_ftrace_notrace. If another function
4973 * tracer ops is registered, the graph tracer requires testing the
4974 * function against the global ops, and not just trace any function
4975 * that any ftrace_ops registered.
4977 static void update_function_graph_func(void)
4979 if (ftrace_ops_list == &ftrace_list_end ||
4980 (ftrace_ops_list == &global_ops &&
4981 global_ops.next == &ftrace_list_end))
4982 ftrace_graph_entry = __ftrace_graph_entry;
4984 ftrace_graph_entry = ftrace_graph_entry_test;
4987 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4988 trace_func_graph_ent_t entryfunc)
4992 mutex_lock(&ftrace_lock);
4994 /* we currently allow only one tracer registered at a time */
4995 if (ftrace_graph_active) {
5000 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5001 register_pm_notifier(&ftrace_suspend_notifier);
5003 ftrace_graph_active++;
5004 ret = start_graph_tracing();
5006 ftrace_graph_active--;
5010 ftrace_graph_return = retfunc;
5013 * Update the indirect function to the entryfunc, and the
5014 * function that gets called to the entry_test first. Then
5015 * call the update fgraph entry function to determine if
5016 * the entryfunc should be called directly or not.
5018 __ftrace_graph_entry = entryfunc;
5019 ftrace_graph_entry = ftrace_graph_entry_test;
5020 update_function_graph_func();
5022 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
5025 mutex_unlock(&ftrace_lock);
5029 void unregister_ftrace_graph(void)
5031 mutex_lock(&ftrace_lock);
5033 if (unlikely(!ftrace_graph_active))
5036 ftrace_graph_active--;
5037 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5038 ftrace_graph_entry = ftrace_graph_entry_stub;
5039 __ftrace_graph_entry = ftrace_graph_entry_stub;
5040 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
5041 unregister_pm_notifier(&ftrace_suspend_notifier);
5042 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5045 mutex_unlock(&ftrace_lock);
5048 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5051 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5053 atomic_set(&t->tracing_graph_pause, 0);
5054 atomic_set(&t->trace_overrun, 0);
5055 t->ftrace_timestamp = 0;
5056 /* make curr_ret_stack visible before we add the ret_stack */
5058 t->ret_stack = ret_stack;
5062 * Allocate a return stack for the idle task. May be the first
5063 * time through, or it may be done by CPU hotplug online.
5065 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5067 t->curr_ret_stack = -1;
5069 * The idle task has no parent, it either has its own
5070 * stack or no stack at all.
5073 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5075 if (ftrace_graph_active) {
5076 struct ftrace_ret_stack *ret_stack;
5078 ret_stack = per_cpu(idle_ret_stack, cpu);
5080 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5081 * sizeof(struct ftrace_ret_stack),
5085 per_cpu(idle_ret_stack, cpu) = ret_stack;
5087 graph_init_task(t, ret_stack);
5091 /* Allocate a return stack for newly created task */
5092 void ftrace_graph_init_task(struct task_struct *t)
5094 /* Make sure we do not use the parent ret_stack */
5095 t->ret_stack = NULL;
5096 t->curr_ret_stack = -1;
5098 if (ftrace_graph_active) {
5099 struct ftrace_ret_stack *ret_stack;
5101 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5102 * sizeof(struct ftrace_ret_stack),
5106 graph_init_task(t, ret_stack);
5110 void ftrace_graph_exit_task(struct task_struct *t)
5112 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5114 t->ret_stack = NULL;
5115 /* NULL must become visible to IRQs before we free it: */
5121 void ftrace_graph_stop(void)