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)
247 ftrace_func_t func = ftrace_global_list_func;
248 void *private = NULL;
250 /* The list has its own recursion protection. */
251 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
254 * If there's only one function registered, then call that
255 * function directly. Otherwise, we need to iterate over the
256 * registered callers.
258 if (ftrace_global_list == &ftrace_list_end ||
259 ftrace_global_list->next == &ftrace_list_end) {
260 func = ftrace_global_list->func;
261 private = ftrace_global_list->private;
263 * As we are calling the function directly.
264 * If it does not have recursion protection,
265 * the function_trace_op needs to be updated
268 if (!(ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE))
269 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
272 /* If we filter on pids, update to use the pid function */
273 if (!list_empty(&ftrace_pids)) {
274 set_ftrace_pid_function(func);
275 func = ftrace_pid_func;
278 global_ops.func = func;
279 global_ops.private = private;
282 static void ftrace_sync(struct work_struct *work)
285 * This function is just a stub to implement a hard force
286 * of synchronize_sched(). This requires synchronizing
287 * tasks even in userspace and idle.
289 * Yes, function tracing is rude.
293 static void ftrace_sync_ipi(void *data)
295 /* Probably not needed, but do it anyway */
299 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
300 static void update_function_graph_func(void);
302 static inline void update_function_graph_func(void) { }
305 static void update_ftrace_function(void)
312 * If we are at the end of the list and this ops is
313 * recursion safe and not dynamic and the arch supports passing ops,
314 * then have the mcount trampoline call the function directly.
316 if (ftrace_ops_list == &ftrace_list_end ||
317 (ftrace_ops_list->next == &ftrace_list_end &&
318 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
319 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
320 !FTRACE_FORCE_LIST_FUNC)) {
321 /* Set the ftrace_ops that the arch callback uses */
322 if (ftrace_ops_list == &global_ops)
323 set_function_trace_op = ftrace_global_list;
325 set_function_trace_op = ftrace_ops_list;
326 func = ftrace_ops_list->func;
328 /* Just use the default ftrace_ops */
329 set_function_trace_op = &ftrace_list_end;
330 func = ftrace_ops_list_func;
333 /* If there's no change, then do nothing more here */
334 if (ftrace_trace_function == func)
337 update_function_graph_func();
340 * If we are using the list function, it doesn't care
341 * about the function_trace_ops.
343 if (func == ftrace_ops_list_func) {
344 ftrace_trace_function = func;
346 * Don't even bother setting function_trace_ops,
347 * it would be racy to do so anyway.
352 #ifndef CONFIG_DYNAMIC_FTRACE
354 * For static tracing, we need to be a bit more careful.
355 * The function change takes affect immediately. Thus,
356 * we need to coorditate the setting of the function_trace_ops
357 * with the setting of the ftrace_trace_function.
359 * Set the function to the list ops, which will call the
360 * function we want, albeit indirectly, but it handles the
361 * ftrace_ops and doesn't depend on function_trace_op.
363 ftrace_trace_function = ftrace_ops_list_func;
365 * Make sure all CPUs see this. Yes this is slow, but static
366 * tracing is slow and nasty to have enabled.
368 schedule_on_each_cpu(ftrace_sync);
369 /* Now all cpus are using the list ops. */
370 function_trace_op = set_function_trace_op;
371 /* Make sure the function_trace_op is visible on all CPUs */
373 /* Nasty way to force a rmb on all cpus */
374 smp_call_function(ftrace_sync_ipi, NULL, 1);
375 /* OK, we are all set to update the ftrace_trace_function now! */
376 #endif /* !CONFIG_DYNAMIC_FTRACE */
378 ftrace_trace_function = func;
381 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
385 * We are entering ops into the list but another
386 * CPU might be walking that list. We need to make sure
387 * the ops->next pointer is valid before another CPU sees
388 * the ops pointer included into the list.
390 rcu_assign_pointer(*list, ops);
393 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
395 struct ftrace_ops **p;
398 * If we are removing the last function, then simply point
399 * to the ftrace_stub.
401 if (*list == ops && ops->next == &ftrace_list_end) {
402 *list = &ftrace_list_end;
406 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
417 static void add_ftrace_list_ops(struct ftrace_ops **list,
418 struct ftrace_ops *main_ops,
419 struct ftrace_ops *ops)
421 int first = *list == &ftrace_list_end;
422 add_ftrace_ops(list, ops);
424 add_ftrace_ops(&ftrace_ops_list, main_ops);
427 static int remove_ftrace_list_ops(struct ftrace_ops **list,
428 struct ftrace_ops *main_ops,
429 struct ftrace_ops *ops)
431 int ret = remove_ftrace_ops(list, ops);
432 if (!ret && *list == &ftrace_list_end)
433 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
437 static int __register_ftrace_function(struct ftrace_ops *ops)
439 if (FTRACE_WARN_ON(ops == &global_ops))
442 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
445 /* We don't support both control and global flags set. */
446 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
449 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
451 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
452 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
453 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
455 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
456 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
459 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
460 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
463 if (!core_kernel_data((unsigned long)ops))
464 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
466 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
467 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
468 ops->flags |= FTRACE_OPS_FL_ENABLED;
469 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
470 if (control_ops_alloc(ops))
472 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
474 add_ftrace_ops(&ftrace_ops_list, ops);
477 update_ftrace_function();
482 static int __unregister_ftrace_function(struct ftrace_ops *ops)
486 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
489 if (FTRACE_WARN_ON(ops == &global_ops))
492 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
493 ret = remove_ftrace_list_ops(&ftrace_global_list,
496 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
497 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
498 ret = remove_ftrace_list_ops(&ftrace_control_list,
501 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
507 update_ftrace_function();
512 static void ftrace_update_pid_func(void)
514 /* Only do something if we are tracing something */
515 if (ftrace_trace_function == ftrace_stub)
518 update_ftrace_function();
521 #ifdef CONFIG_FUNCTION_PROFILER
522 struct ftrace_profile {
523 struct hlist_node node;
525 unsigned long counter;
526 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
527 unsigned long long time;
528 unsigned long long time_squared;
532 struct ftrace_profile_page {
533 struct ftrace_profile_page *next;
535 struct ftrace_profile records[];
538 struct ftrace_profile_stat {
540 struct hlist_head *hash;
541 struct ftrace_profile_page *pages;
542 struct ftrace_profile_page *start;
543 struct tracer_stat stat;
546 #define PROFILE_RECORDS_SIZE \
547 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
549 #define PROFILES_PER_PAGE \
550 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
552 static int ftrace_profile_enabled __read_mostly;
554 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
555 static DEFINE_MUTEX(ftrace_profile_lock);
557 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
559 #define FTRACE_PROFILE_HASH_BITS 10
560 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
563 function_stat_next(void *v, int idx)
565 struct ftrace_profile *rec = v;
566 struct ftrace_profile_page *pg;
568 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
574 if ((void *)rec >= (void *)&pg->records[pg->index]) {
578 rec = &pg->records[0];
586 static void *function_stat_start(struct tracer_stat *trace)
588 struct ftrace_profile_stat *stat =
589 container_of(trace, struct ftrace_profile_stat, stat);
591 if (!stat || !stat->start)
594 return function_stat_next(&stat->start->records[0], 0);
597 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
598 /* function graph compares on total time */
599 static int function_stat_cmp(void *p1, void *p2)
601 struct ftrace_profile *a = p1;
602 struct ftrace_profile *b = p2;
604 if (a->time < b->time)
606 if (a->time > b->time)
612 /* not function graph compares against hits */
613 static int function_stat_cmp(void *p1, void *p2)
615 struct ftrace_profile *a = p1;
616 struct ftrace_profile *b = p2;
618 if (a->counter < b->counter)
620 if (a->counter > b->counter)
627 static int function_stat_headers(struct seq_file *m)
629 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
630 seq_printf(m, " Function "
633 "--- ---- --- ---\n");
635 seq_printf(m, " Function Hit\n"
641 static int function_stat_show(struct seq_file *m, void *v)
643 struct ftrace_profile *rec = v;
644 char str[KSYM_SYMBOL_LEN];
646 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
647 static struct trace_seq s;
648 unsigned long long avg;
649 unsigned long long stddev;
651 mutex_lock(&ftrace_profile_lock);
653 /* we raced with function_profile_reset() */
654 if (unlikely(rec->counter == 0)) {
659 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
660 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
662 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
665 do_div(avg, rec->counter);
667 /* Sample standard deviation (s^2) */
668 if (rec->counter <= 1)
672 * Apply Welford's method:
673 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
675 stddev = rec->counter * rec->time_squared -
676 rec->time * rec->time;
679 * Divide only 1000 for ns^2 -> us^2 conversion.
680 * trace_print_graph_duration will divide 1000 again.
682 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
686 trace_print_graph_duration(rec->time, &s);
687 trace_seq_puts(&s, " ");
688 trace_print_graph_duration(avg, &s);
689 trace_seq_puts(&s, " ");
690 trace_print_graph_duration(stddev, &s);
691 trace_print_seq(m, &s);
695 mutex_unlock(&ftrace_profile_lock);
700 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
702 struct ftrace_profile_page *pg;
704 pg = stat->pages = stat->start;
707 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
712 memset(stat->hash, 0,
713 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
716 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
718 struct ftrace_profile_page *pg;
723 /* If we already allocated, do nothing */
727 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
731 #ifdef CONFIG_DYNAMIC_FTRACE
732 functions = ftrace_update_tot_cnt;
735 * We do not know the number of functions that exist because
736 * dynamic tracing is what counts them. With past experience
737 * we have around 20K functions. That should be more than enough.
738 * It is highly unlikely we will execute every function in
744 pg = stat->start = stat->pages;
746 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
748 for (i = 1; i < pages; i++) {
749 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
760 unsigned long tmp = (unsigned long)pg;
772 static int ftrace_profile_init_cpu(int cpu)
774 struct ftrace_profile_stat *stat;
777 stat = &per_cpu(ftrace_profile_stats, cpu);
780 /* If the profile is already created, simply reset it */
781 ftrace_profile_reset(stat);
786 * We are profiling all functions, but usually only a few thousand
787 * functions are hit. We'll make a hash of 1024 items.
789 size = FTRACE_PROFILE_HASH_SIZE;
791 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
796 /* Preallocate the function profiling pages */
797 if (ftrace_profile_pages_init(stat) < 0) {
806 static int ftrace_profile_init(void)
811 for_each_possible_cpu(cpu) {
812 ret = ftrace_profile_init_cpu(cpu);
820 /* interrupts must be disabled */
821 static struct ftrace_profile *
822 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
824 struct ftrace_profile *rec;
825 struct hlist_head *hhd;
828 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
829 hhd = &stat->hash[key];
831 if (hlist_empty(hhd))
834 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
842 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
843 struct ftrace_profile *rec)
847 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
848 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
852 * The memory is already allocated, this simply finds a new record to use.
854 static struct ftrace_profile *
855 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
857 struct ftrace_profile *rec = NULL;
859 /* prevent recursion (from NMIs) */
860 if (atomic_inc_return(&stat->disabled) != 1)
864 * Try to find the function again since an NMI
865 * could have added it
867 rec = ftrace_find_profiled_func(stat, ip);
871 if (stat->pages->index == PROFILES_PER_PAGE) {
872 if (!stat->pages->next)
874 stat->pages = stat->pages->next;
877 rec = &stat->pages->records[stat->pages->index++];
879 ftrace_add_profile(stat, rec);
882 atomic_dec(&stat->disabled);
888 function_profile_call(unsigned long ip, unsigned long parent_ip,
889 struct ftrace_ops *ops, struct pt_regs *regs)
891 struct ftrace_profile_stat *stat;
892 struct ftrace_profile *rec;
895 if (!ftrace_profile_enabled)
898 local_irq_save(flags);
900 stat = &__get_cpu_var(ftrace_profile_stats);
901 if (!stat->hash || !ftrace_profile_enabled)
904 rec = ftrace_find_profiled_func(stat, ip);
906 rec = ftrace_profile_alloc(stat, ip);
913 local_irq_restore(flags);
916 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
917 static int profile_graph_entry(struct ftrace_graph_ent *trace)
919 function_profile_call(trace->func, 0, NULL, NULL);
923 static void profile_graph_return(struct ftrace_graph_ret *trace)
925 struct ftrace_profile_stat *stat;
926 unsigned long long calltime;
927 struct ftrace_profile *rec;
930 local_irq_save(flags);
931 stat = &__get_cpu_var(ftrace_profile_stats);
932 if (!stat->hash || !ftrace_profile_enabled)
935 /* If the calltime was zero'd ignore it */
936 if (!trace->calltime)
939 calltime = trace->rettime - trace->calltime;
941 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
944 index = trace->depth;
946 /* Append this call time to the parent time to subtract */
948 current->ret_stack[index - 1].subtime += calltime;
950 if (current->ret_stack[index].subtime < calltime)
951 calltime -= current->ret_stack[index].subtime;
956 rec = ftrace_find_profiled_func(stat, trace->func);
958 rec->time += calltime;
959 rec->time_squared += calltime * calltime;
963 local_irq_restore(flags);
966 static int register_ftrace_profiler(void)
968 return register_ftrace_graph(&profile_graph_return,
969 &profile_graph_entry);
972 static void unregister_ftrace_profiler(void)
974 unregister_ftrace_graph();
977 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
978 .func = function_profile_call,
979 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
980 INIT_REGEX_LOCK(ftrace_profile_ops)
983 static int register_ftrace_profiler(void)
985 return register_ftrace_function(&ftrace_profile_ops);
988 static void unregister_ftrace_profiler(void)
990 unregister_ftrace_function(&ftrace_profile_ops);
992 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
995 ftrace_profile_write(struct file *filp, const char __user *ubuf,
996 size_t cnt, loff_t *ppos)
1001 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1007 mutex_lock(&ftrace_profile_lock);
1008 if (ftrace_profile_enabled ^ val) {
1010 ret = ftrace_profile_init();
1016 ret = register_ftrace_profiler();
1021 ftrace_profile_enabled = 1;
1023 ftrace_profile_enabled = 0;
1025 * unregister_ftrace_profiler calls stop_machine
1026 * so this acts like an synchronize_sched.
1028 unregister_ftrace_profiler();
1032 mutex_unlock(&ftrace_profile_lock);
1040 ftrace_profile_read(struct file *filp, char __user *ubuf,
1041 size_t cnt, loff_t *ppos)
1043 char buf[64]; /* big enough to hold a number */
1046 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1047 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1050 static const struct file_operations ftrace_profile_fops = {
1051 .open = tracing_open_generic,
1052 .read = ftrace_profile_read,
1053 .write = ftrace_profile_write,
1054 .llseek = default_llseek,
1057 /* used to initialize the real stat files */
1058 static struct tracer_stat function_stats __initdata = {
1059 .name = "functions",
1060 .stat_start = function_stat_start,
1061 .stat_next = function_stat_next,
1062 .stat_cmp = function_stat_cmp,
1063 .stat_headers = function_stat_headers,
1064 .stat_show = function_stat_show
1067 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1069 struct ftrace_profile_stat *stat;
1070 struct dentry *entry;
1075 for_each_possible_cpu(cpu) {
1076 stat = &per_cpu(ftrace_profile_stats, cpu);
1078 /* allocate enough for function name + cpu number */
1079 name = kmalloc(32, GFP_KERNEL);
1082 * The files created are permanent, if something happens
1083 * we still do not free memory.
1086 "Could not allocate stat file for cpu %d\n",
1090 stat->stat = function_stats;
1091 snprintf(name, 32, "function%d", cpu);
1092 stat->stat.name = name;
1093 ret = register_stat_tracer(&stat->stat);
1096 "Could not register function stat for cpu %d\n",
1103 entry = debugfs_create_file("function_profile_enabled", 0644,
1104 d_tracer, NULL, &ftrace_profile_fops);
1106 pr_warning("Could not create debugfs "
1107 "'function_profile_enabled' entry\n");
1110 #else /* CONFIG_FUNCTION_PROFILER */
1111 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1114 #endif /* CONFIG_FUNCTION_PROFILER */
1116 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1118 #ifdef CONFIG_DYNAMIC_FTRACE
1120 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1121 # error Dynamic ftrace depends on MCOUNT_RECORD
1124 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1126 struct ftrace_func_probe {
1127 struct hlist_node node;
1128 struct ftrace_probe_ops *ops;
1129 unsigned long flags;
1132 struct list_head free_list;
1135 struct ftrace_func_entry {
1136 struct hlist_node hlist;
1140 struct ftrace_hash {
1141 unsigned long size_bits;
1142 struct hlist_head *buckets;
1143 unsigned long count;
1144 struct rcu_head rcu;
1148 * We make these constant because no one should touch them,
1149 * but they are used as the default "empty hash", to avoid allocating
1150 * it all the time. These are in a read only section such that if
1151 * anyone does try to modify it, it will cause an exception.
1153 static const struct hlist_head empty_buckets[1];
1154 static const struct ftrace_hash empty_hash = {
1155 .buckets = (struct hlist_head *)empty_buckets,
1157 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1159 static struct ftrace_ops global_ops = {
1160 .func = ftrace_stub,
1161 .notrace_hash = EMPTY_HASH,
1162 .filter_hash = EMPTY_HASH,
1163 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1164 INIT_REGEX_LOCK(global_ops)
1167 struct ftrace_page {
1168 struct ftrace_page *next;
1169 struct dyn_ftrace *records;
1174 static struct ftrace_page *ftrace_new_pgs;
1176 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1177 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1179 /* estimate from running different kernels */
1180 #define NR_TO_INIT 10000
1182 static struct ftrace_page *ftrace_pages_start;
1183 static struct ftrace_page *ftrace_pages;
1185 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1187 return !hash || !hash->count;
1190 static struct ftrace_func_entry *
1191 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1194 struct ftrace_func_entry *entry;
1195 struct hlist_head *hhd;
1197 if (ftrace_hash_empty(hash))
1200 if (hash->size_bits > 0)
1201 key = hash_long(ip, hash->size_bits);
1205 hhd = &hash->buckets[key];
1207 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1208 if (entry->ip == ip)
1214 static void __add_hash_entry(struct ftrace_hash *hash,
1215 struct ftrace_func_entry *entry)
1217 struct hlist_head *hhd;
1220 if (hash->size_bits)
1221 key = hash_long(entry->ip, hash->size_bits);
1225 hhd = &hash->buckets[key];
1226 hlist_add_head(&entry->hlist, hhd);
1230 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1232 struct ftrace_func_entry *entry;
1234 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1239 __add_hash_entry(hash, entry);
1245 free_hash_entry(struct ftrace_hash *hash,
1246 struct ftrace_func_entry *entry)
1248 hlist_del(&entry->hlist);
1254 remove_hash_entry(struct ftrace_hash *hash,
1255 struct ftrace_func_entry *entry)
1257 hlist_del(&entry->hlist);
1261 static void ftrace_hash_clear(struct ftrace_hash *hash)
1263 struct hlist_head *hhd;
1264 struct hlist_node *tn;
1265 struct ftrace_func_entry *entry;
1266 int size = 1 << hash->size_bits;
1272 for (i = 0; i < size; i++) {
1273 hhd = &hash->buckets[i];
1274 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1275 free_hash_entry(hash, entry);
1277 FTRACE_WARN_ON(hash->count);
1280 static void free_ftrace_hash(struct ftrace_hash *hash)
1282 if (!hash || hash == EMPTY_HASH)
1284 ftrace_hash_clear(hash);
1285 kfree(hash->buckets);
1289 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1291 struct ftrace_hash *hash;
1293 hash = container_of(rcu, struct ftrace_hash, rcu);
1294 free_ftrace_hash(hash);
1297 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1299 if (!hash || hash == EMPTY_HASH)
1301 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1304 void ftrace_free_filter(struct ftrace_ops *ops)
1306 ftrace_ops_init(ops);
1307 free_ftrace_hash(ops->filter_hash);
1308 free_ftrace_hash(ops->notrace_hash);
1311 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1313 struct ftrace_hash *hash;
1316 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1320 size = 1 << size_bits;
1321 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1323 if (!hash->buckets) {
1328 hash->size_bits = size_bits;
1333 static struct ftrace_hash *
1334 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1336 struct ftrace_func_entry *entry;
1337 struct ftrace_hash *new_hash;
1342 new_hash = alloc_ftrace_hash(size_bits);
1347 if (ftrace_hash_empty(hash))
1350 size = 1 << hash->size_bits;
1351 for (i = 0; i < size; i++) {
1352 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1353 ret = add_hash_entry(new_hash, entry->ip);
1359 FTRACE_WARN_ON(new_hash->count != hash->count);
1364 free_ftrace_hash(new_hash);
1369 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1371 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1374 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1375 struct ftrace_hash **dst, struct ftrace_hash *src)
1377 struct ftrace_func_entry *entry;
1378 struct hlist_node *tn;
1379 struct hlist_head *hhd;
1380 struct ftrace_hash *old_hash;
1381 struct ftrace_hash *new_hash;
1382 int size = src->count;
1388 * Remove the current set, update the hash and add
1391 ftrace_hash_rec_disable(ops, enable);
1394 * If the new source is empty, just free dst and assign it
1398 free_ftrace_hash_rcu(*dst);
1399 rcu_assign_pointer(*dst, EMPTY_HASH);
1400 /* still need to update the function records */
1406 * Make the hash size about 1/2 the # found
1408 for (size /= 2; size; size >>= 1)
1411 /* Don't allocate too much */
1412 if (bits > FTRACE_HASH_MAX_BITS)
1413 bits = FTRACE_HASH_MAX_BITS;
1416 new_hash = alloc_ftrace_hash(bits);
1420 size = 1 << src->size_bits;
1421 for (i = 0; i < size; i++) {
1422 hhd = &src->buckets[i];
1423 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1424 remove_hash_entry(src, entry);
1425 __add_hash_entry(new_hash, entry);
1430 rcu_assign_pointer(*dst, new_hash);
1431 free_ftrace_hash_rcu(old_hash);
1436 * Enable regardless of ret:
1437 * On success, we enable the new hash.
1438 * On failure, we re-enable the original hash.
1440 ftrace_hash_rec_enable(ops, enable);
1446 * Test the hashes for this ops to see if we want to call
1447 * the ops->func or not.
1449 * It's a match if the ip is in the ops->filter_hash or
1450 * the filter_hash does not exist or is empty,
1452 * the ip is not in the ops->notrace_hash.
1454 * This needs to be called with preemption disabled as
1455 * the hashes are freed with call_rcu_sched().
1458 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1460 struct ftrace_hash *filter_hash;
1461 struct ftrace_hash *notrace_hash;
1464 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1466 * There's a small race when adding ops that the ftrace handler
1467 * that wants regs, may be called without them. We can not
1468 * allow that handler to be called if regs is NULL.
1470 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1474 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1475 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1477 if ((ftrace_hash_empty(filter_hash) ||
1478 ftrace_lookup_ip(filter_hash, ip)) &&
1479 (ftrace_hash_empty(notrace_hash) ||
1480 !ftrace_lookup_ip(notrace_hash, ip)))
1489 * This is a double for. Do not use 'break' to break out of the loop,
1490 * you must use a goto.
1492 #define do_for_each_ftrace_rec(pg, rec) \
1493 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1495 for (_____i = 0; _____i < pg->index; _____i++) { \
1496 rec = &pg->records[_____i];
1498 #define while_for_each_ftrace_rec() \
1503 static int ftrace_cmp_recs(const void *a, const void *b)
1505 const struct dyn_ftrace *key = a;
1506 const struct dyn_ftrace *rec = b;
1508 if (key->flags < rec->ip)
1510 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1515 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1517 struct ftrace_page *pg;
1518 struct dyn_ftrace *rec;
1519 struct dyn_ftrace key;
1522 key.flags = end; /* overload flags, as it is unsigned long */
1524 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1525 if (end < pg->records[0].ip ||
1526 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1528 rec = bsearch(&key, pg->records, pg->index,
1529 sizeof(struct dyn_ftrace),
1539 * ftrace_location - return true if the ip giving is a traced location
1540 * @ip: the instruction pointer to check
1542 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1543 * That is, the instruction that is either a NOP or call to
1544 * the function tracer. It checks the ftrace internal tables to
1545 * determine if the address belongs or not.
1547 unsigned long ftrace_location(unsigned long ip)
1549 return ftrace_location_range(ip, ip);
1553 * ftrace_text_reserved - return true if range contains an ftrace location
1554 * @start: start of range to search
1555 * @end: end of range to search (inclusive). @end points to the last byte to check.
1557 * Returns 1 if @start and @end contains a ftrace location.
1558 * That is, the instruction that is either a NOP or call to
1559 * the function tracer. It checks the ftrace internal tables to
1560 * determine if the address belongs or not.
1562 int ftrace_text_reserved(void *start, void *end)
1566 ret = ftrace_location_range((unsigned long)start,
1567 (unsigned long)end);
1572 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1576 struct ftrace_hash *hash;
1577 struct ftrace_hash *other_hash;
1578 struct ftrace_page *pg;
1579 struct dyn_ftrace *rec;
1583 /* Only update if the ops has been registered */
1584 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1588 * In the filter_hash case:
1589 * If the count is zero, we update all records.
1590 * Otherwise we just update the items in the hash.
1592 * In the notrace_hash case:
1593 * We enable the update in the hash.
1594 * As disabling notrace means enabling the tracing,
1595 * and enabling notrace means disabling, the inc variable
1599 hash = ops->filter_hash;
1600 other_hash = ops->notrace_hash;
1601 if (ftrace_hash_empty(hash))
1605 hash = ops->notrace_hash;
1606 other_hash = ops->filter_hash;
1608 * If the notrace hash has no items,
1609 * then there's nothing to do.
1611 if (ftrace_hash_empty(hash))
1615 do_for_each_ftrace_rec(pg, rec) {
1616 int in_other_hash = 0;
1622 * Only the filter_hash affects all records.
1623 * Update if the record is not in the notrace hash.
1625 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1628 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1629 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1634 if (filter_hash && in_hash && !in_other_hash)
1636 else if (!filter_hash && in_hash &&
1637 (in_other_hash || ftrace_hash_empty(other_hash)))
1645 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1648 * If any ops wants regs saved for this function
1649 * then all ops will get saved regs.
1651 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1652 rec->flags |= FTRACE_FL_REGS;
1654 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1659 /* Shortcut, if we handled all records, we are done. */
1660 if (!all && count == hash->count)
1662 } while_for_each_ftrace_rec();
1665 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1668 __ftrace_hash_rec_update(ops, filter_hash, 0);
1671 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1674 __ftrace_hash_rec_update(ops, filter_hash, 1);
1677 static void print_ip_ins(const char *fmt, unsigned char *p)
1681 printk(KERN_CONT "%s", fmt);
1683 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1684 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1688 * ftrace_bug - report and shutdown function tracer
1689 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1690 * @ip: The address that failed
1692 * The arch code that enables or disables the function tracing
1693 * can call ftrace_bug() when it has detected a problem in
1694 * modifying the code. @failed should be one of either:
1695 * EFAULT - if the problem happens on reading the @ip address
1696 * EINVAL - if what is read at @ip is not what was expected
1697 * EPERM - if the problem happens on writting to the @ip address
1699 void ftrace_bug(int failed, unsigned long ip)
1703 FTRACE_WARN_ON_ONCE(1);
1704 pr_info("ftrace faulted on modifying ");
1708 FTRACE_WARN_ON_ONCE(1);
1709 pr_info("ftrace failed to modify ");
1711 print_ip_ins(" actual: ", (unsigned char *)ip);
1712 printk(KERN_CONT "\n");
1715 FTRACE_WARN_ON_ONCE(1);
1716 pr_info("ftrace faulted on writing ");
1720 FTRACE_WARN_ON_ONCE(1);
1721 pr_info("ftrace faulted on unknown error ");
1726 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1728 unsigned long flag = 0UL;
1731 * If we are updating calls:
1733 * If the record has a ref count, then we need to enable it
1734 * because someone is using it.
1736 * Otherwise we make sure its disabled.
1738 * If we are disabling calls, then disable all records that
1741 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1742 flag = FTRACE_FL_ENABLED;
1745 * If enabling and the REGS flag does not match the REGS_EN, then
1746 * do not ignore this record. Set flags to fail the compare against
1750 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1751 flag |= FTRACE_FL_REGS;
1753 /* If the state of this record hasn't changed, then do nothing */
1754 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1755 return FTRACE_UPDATE_IGNORE;
1758 /* Save off if rec is being enabled (for return value) */
1759 flag ^= rec->flags & FTRACE_FL_ENABLED;
1762 rec->flags |= FTRACE_FL_ENABLED;
1763 if (flag & FTRACE_FL_REGS) {
1764 if (rec->flags & FTRACE_FL_REGS)
1765 rec->flags |= FTRACE_FL_REGS_EN;
1767 rec->flags &= ~FTRACE_FL_REGS_EN;
1772 * If this record is being updated from a nop, then
1773 * return UPDATE_MAKE_CALL.
1774 * Otherwise, if the EN flag is set, then return
1775 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1776 * from the non-save regs, to a save regs function.
1778 * return UPDATE_MODIFY_CALL to tell the caller to convert
1779 * from the save regs, to a non-save regs function.
1781 if (flag & FTRACE_FL_ENABLED)
1782 return FTRACE_UPDATE_MAKE_CALL;
1783 else if (rec->flags & FTRACE_FL_REGS_EN)
1784 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1786 return FTRACE_UPDATE_MODIFY_CALL;
1790 /* If there's no more users, clear all flags */
1791 if (!(rec->flags & ~FTRACE_FL_MASK))
1794 /* Just disable the record (keep REGS state) */
1795 rec->flags &= ~FTRACE_FL_ENABLED;
1798 return FTRACE_UPDATE_MAKE_NOP;
1802 * ftrace_update_record, set a record that now is tracing or not
1803 * @rec: the record to update
1804 * @enable: set to 1 if the record is tracing, zero to force disable
1806 * The records that represent all functions that can be traced need
1807 * to be updated when tracing has been enabled.
1809 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1811 return ftrace_check_record(rec, enable, 1);
1815 * ftrace_test_record, check if the record has been enabled or not
1816 * @rec: the record to test
1817 * @enable: set to 1 to check if enabled, 0 if it is disabled
1819 * The arch code may need to test if a record is already set to
1820 * tracing to determine how to modify the function code that it
1823 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1825 return ftrace_check_record(rec, enable, 0);
1829 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1831 unsigned long ftrace_old_addr;
1832 unsigned long ftrace_addr;
1835 ret = ftrace_update_record(rec, enable);
1837 if (rec->flags & FTRACE_FL_REGS)
1838 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1840 ftrace_addr = (unsigned long)FTRACE_ADDR;
1843 case FTRACE_UPDATE_IGNORE:
1846 case FTRACE_UPDATE_MAKE_CALL:
1847 return ftrace_make_call(rec, ftrace_addr);
1849 case FTRACE_UPDATE_MAKE_NOP:
1850 return ftrace_make_nop(NULL, rec, ftrace_addr);
1852 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1853 case FTRACE_UPDATE_MODIFY_CALL:
1854 if (rec->flags & FTRACE_FL_REGS)
1855 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1857 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1859 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1862 return -1; /* unknow ftrace bug */
1865 void __weak ftrace_replace_code(int enable)
1867 struct dyn_ftrace *rec;
1868 struct ftrace_page *pg;
1871 if (unlikely(ftrace_disabled))
1874 do_for_each_ftrace_rec(pg, rec) {
1875 failed = __ftrace_replace_code(rec, enable);
1877 ftrace_bug(failed, rec->ip);
1878 /* Stop processing */
1881 } while_for_each_ftrace_rec();
1884 struct ftrace_rec_iter {
1885 struct ftrace_page *pg;
1890 * ftrace_rec_iter_start, start up iterating over traced functions
1892 * Returns an iterator handle that is used to iterate over all
1893 * the records that represent address locations where functions
1896 * May return NULL if no records are available.
1898 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1901 * We only use a single iterator.
1902 * Protected by the ftrace_lock mutex.
1904 static struct ftrace_rec_iter ftrace_rec_iter;
1905 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1907 iter->pg = ftrace_pages_start;
1910 /* Could have empty pages */
1911 while (iter->pg && !iter->pg->index)
1912 iter->pg = iter->pg->next;
1921 * ftrace_rec_iter_next, get the next record to process.
1922 * @iter: The handle to the iterator.
1924 * Returns the next iterator after the given iterator @iter.
1926 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1930 if (iter->index >= iter->pg->index) {
1931 iter->pg = iter->pg->next;
1934 /* Could have empty pages */
1935 while (iter->pg && !iter->pg->index)
1936 iter->pg = iter->pg->next;
1946 * ftrace_rec_iter_record, get the record at the iterator location
1947 * @iter: The current iterator location
1949 * Returns the record that the current @iter is at.
1951 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1953 return &iter->pg->records[iter->index];
1957 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1964 if (unlikely(ftrace_disabled))
1967 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1969 ftrace_bug(ret, ip);
1976 * archs can override this function if they must do something
1977 * before the modifying code is performed.
1979 int __weak ftrace_arch_code_modify_prepare(void)
1985 * archs can override this function if they must do something
1986 * after the modifying code is performed.
1988 int __weak ftrace_arch_code_modify_post_process(void)
1993 void ftrace_modify_all_code(int command)
1995 int update = command & FTRACE_UPDATE_TRACE_FUNC;
1998 * If the ftrace_caller calls a ftrace_ops func directly,
1999 * we need to make sure that it only traces functions it
2000 * expects to trace. When doing the switch of functions,
2001 * we need to update to the ftrace_ops_list_func first
2002 * before the transition between old and new calls are set,
2003 * as the ftrace_ops_list_func will check the ops hashes
2004 * to make sure the ops are having the right functions
2008 ftrace_update_ftrace_func(ftrace_ops_list_func);
2010 if (command & FTRACE_UPDATE_CALLS)
2011 ftrace_replace_code(1);
2012 else if (command & FTRACE_DISABLE_CALLS)
2013 ftrace_replace_code(0);
2015 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2016 function_trace_op = set_function_trace_op;
2018 /* If irqs are disabled, we are in stop machine */
2019 if (!irqs_disabled())
2020 smp_call_function(ftrace_sync_ipi, NULL, 1);
2021 ftrace_update_ftrace_func(ftrace_trace_function);
2024 if (command & FTRACE_START_FUNC_RET)
2025 ftrace_enable_ftrace_graph_caller();
2026 else if (command & FTRACE_STOP_FUNC_RET)
2027 ftrace_disable_ftrace_graph_caller();
2030 static int __ftrace_modify_code(void *data)
2032 int *command = data;
2034 ftrace_modify_all_code(*command);
2040 * ftrace_run_stop_machine, go back to the stop machine method
2041 * @command: The command to tell ftrace what to do
2043 * If an arch needs to fall back to the stop machine method, the
2044 * it can call this function.
2046 void ftrace_run_stop_machine(int command)
2048 stop_machine(__ftrace_modify_code, &command, NULL);
2052 * arch_ftrace_update_code, modify the code to trace or not trace
2053 * @command: The command that needs to be done
2055 * Archs can override this function if it does not need to
2056 * run stop_machine() to modify code.
2058 void __weak arch_ftrace_update_code(int command)
2060 ftrace_run_stop_machine(command);
2063 static void ftrace_run_update_code(int command)
2067 ret = ftrace_arch_code_modify_prepare();
2068 FTRACE_WARN_ON(ret);
2072 * Do not call function tracer while we update the code.
2073 * We are in stop machine.
2075 function_trace_stop++;
2078 * By default we use stop_machine() to modify the code.
2079 * But archs can do what ever they want as long as it
2080 * is safe. The stop_machine() is the safest, but also
2081 * produces the most overhead.
2083 arch_ftrace_update_code(command);
2085 function_trace_stop--;
2087 ret = ftrace_arch_code_modify_post_process();
2088 FTRACE_WARN_ON(ret);
2091 static ftrace_func_t saved_ftrace_func;
2092 static int ftrace_start_up;
2093 static int global_start_up;
2095 static void ftrace_startup_enable(int command)
2097 if (saved_ftrace_func != ftrace_trace_function) {
2098 saved_ftrace_func = ftrace_trace_function;
2099 command |= FTRACE_UPDATE_TRACE_FUNC;
2102 if (!command || !ftrace_enabled)
2105 ftrace_run_update_code(command);
2108 static int ftrace_startup(struct ftrace_ops *ops, int command)
2110 bool hash_enable = true;
2113 if (unlikely(ftrace_disabled))
2116 ret = __register_ftrace_function(ops);
2121 command |= FTRACE_UPDATE_CALLS;
2123 /* ops marked global share the filter hashes */
2124 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2126 /* Don't update hash if global is already set */
2127 if (global_start_up)
2128 hash_enable = false;
2132 ops->flags |= FTRACE_OPS_FL_ENABLED;
2134 ftrace_hash_rec_enable(ops, 1);
2136 ftrace_startup_enable(command);
2141 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2143 bool hash_disable = true;
2146 if (unlikely(ftrace_disabled))
2149 ret = __unregister_ftrace_function(ops);
2155 * Just warn in case of unbalance, no need to kill ftrace, it's not
2156 * critical but the ftrace_call callers may be never nopped again after
2157 * further ftrace uses.
2159 WARN_ON_ONCE(ftrace_start_up < 0);
2161 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2164 WARN_ON_ONCE(global_start_up < 0);
2165 /* Don't update hash if global still has users */
2166 if (global_start_up) {
2167 WARN_ON_ONCE(!ftrace_start_up);
2168 hash_disable = false;
2173 ftrace_hash_rec_disable(ops, 1);
2175 if (ops != &global_ops || !global_start_up)
2176 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2178 command |= FTRACE_UPDATE_CALLS;
2180 if (saved_ftrace_func != ftrace_trace_function) {
2181 saved_ftrace_func = ftrace_trace_function;
2182 command |= FTRACE_UPDATE_TRACE_FUNC;
2185 if (!command || !ftrace_enabled) {
2187 * If these are control ops, they still need their
2188 * per_cpu field freed. Since, function tracing is
2189 * not currently active, we can just free them
2190 * without synchronizing all CPUs.
2192 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2193 control_ops_free(ops);
2197 ftrace_run_update_code(command);
2200 * Dynamic ops may be freed, we must make sure that all
2201 * callers are done before leaving this function.
2202 * The same goes for freeing the per_cpu data of the control
2205 * Again, normal synchronize_sched() is not good enough.
2206 * We need to do a hard force of sched synchronization.
2207 * This is because we use preempt_disable() to do RCU, but
2208 * the function tracers can be called where RCU is not watching
2209 * (like before user_exit()). We can not rely on the RCU
2210 * infrastructure to do the synchronization, thus we must do it
2213 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2214 schedule_on_each_cpu(ftrace_sync);
2216 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2217 control_ops_free(ops);
2223 static void ftrace_startup_sysctl(void)
2225 if (unlikely(ftrace_disabled))
2228 /* Force update next time */
2229 saved_ftrace_func = NULL;
2230 /* ftrace_start_up is true if we want ftrace running */
2231 if (ftrace_start_up)
2232 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2235 static void ftrace_shutdown_sysctl(void)
2237 if (unlikely(ftrace_disabled))
2240 /* ftrace_start_up is true if ftrace is running */
2241 if (ftrace_start_up)
2242 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2245 static cycle_t ftrace_update_time;
2246 static unsigned long ftrace_update_cnt;
2247 unsigned long ftrace_update_tot_cnt;
2249 static inline int ops_traces_mod(struct ftrace_ops *ops)
2252 * Filter_hash being empty will default to trace module.
2253 * But notrace hash requires a test of individual module functions.
2255 return ftrace_hash_empty(ops->filter_hash) &&
2256 ftrace_hash_empty(ops->notrace_hash);
2260 * Check if the current ops references the record.
2262 * If the ops traces all functions, then it was already accounted for.
2263 * If the ops does not trace the current record function, skip it.
2264 * If the ops ignores the function via notrace filter, skip it.
2267 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2269 /* If ops isn't enabled, ignore it */
2270 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2273 /* If ops traces all mods, we already accounted for it */
2274 if (ops_traces_mod(ops))
2277 /* The function must be in the filter */
2278 if (!ftrace_hash_empty(ops->filter_hash) &&
2279 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2282 /* If in notrace hash, we ignore it too */
2283 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2289 static int referenced_filters(struct dyn_ftrace *rec)
2291 struct ftrace_ops *ops;
2294 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2295 if (ops_references_rec(ops, rec))
2302 static int ftrace_update_code(struct module *mod)
2304 struct ftrace_page *pg;
2305 struct dyn_ftrace *p;
2306 cycle_t start, stop;
2307 unsigned long ref = 0;
2312 * When adding a module, we need to check if tracers are
2313 * currently enabled and if they are set to trace all functions.
2314 * If they are, we need to enable the module functions as well
2315 * as update the reference counts for those function records.
2318 struct ftrace_ops *ops;
2320 for (ops = ftrace_ops_list;
2321 ops != &ftrace_list_end; ops = ops->next) {
2322 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2323 if (ops_traces_mod(ops))
2331 start = ftrace_now(raw_smp_processor_id());
2332 ftrace_update_cnt = 0;
2334 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2336 for (i = 0; i < pg->index; i++) {
2339 /* If something went wrong, bail without enabling anything */
2340 if (unlikely(ftrace_disabled))
2343 p = &pg->records[i];
2345 cnt += referenced_filters(p);
2349 * Do the initial record conversion from mcount jump
2350 * to the NOP instructions.
2352 if (!ftrace_code_disable(mod, p))
2355 ftrace_update_cnt++;
2358 * If the tracing is enabled, go ahead and enable the record.
2360 * The reason not to enable the record immediatelly is the
2361 * inherent check of ftrace_make_nop/ftrace_make_call for
2362 * correct previous instructions. Making first the NOP
2363 * conversion puts the module to the correct state, thus
2364 * passing the ftrace_make_call check.
2366 if (ftrace_start_up && cnt) {
2367 int failed = __ftrace_replace_code(p, 1);
2369 ftrace_bug(failed, p->ip);
2374 ftrace_new_pgs = NULL;
2376 stop = ftrace_now(raw_smp_processor_id());
2377 ftrace_update_time = stop - start;
2378 ftrace_update_tot_cnt += ftrace_update_cnt;
2383 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2388 if (WARN_ON(!count))
2391 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2394 * We want to fill as much as possible. No more than a page
2397 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2401 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2404 /* if we can't allocate this size, try something smaller */
2411 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2420 static struct ftrace_page *
2421 ftrace_allocate_pages(unsigned long num_to_init)
2423 struct ftrace_page *start_pg;
2424 struct ftrace_page *pg;
2431 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2436 * Try to allocate as much as possible in one continues
2437 * location that fills in all of the space. We want to
2438 * waste as little space as possible.
2441 cnt = ftrace_allocate_records(pg, num_to_init);
2449 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2460 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2461 free_pages((unsigned long)pg->records, order);
2462 start_pg = pg->next;
2466 pr_info("ftrace: FAILED to allocate memory for functions\n");
2470 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2475 pr_info("ftrace: No functions to be traced?\n");
2479 cnt = num_to_init / ENTRIES_PER_PAGE;
2480 pr_info("ftrace: allocating %ld entries in %d pages\n",
2481 num_to_init, cnt + 1);
2486 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2488 struct ftrace_iterator {
2491 struct ftrace_page *pg;
2492 struct dyn_ftrace *func;
2493 struct ftrace_func_probe *probe;
2494 struct trace_parser parser;
2495 struct ftrace_hash *hash;
2496 struct ftrace_ops *ops;
2503 t_hash_next(struct seq_file *m, loff_t *pos)
2505 struct ftrace_iterator *iter = m->private;
2506 struct hlist_node *hnd = NULL;
2507 struct hlist_head *hhd;
2513 hnd = &iter->probe->node;
2515 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2518 hhd = &ftrace_func_hash[iter->hidx];
2520 if (hlist_empty(hhd)) {
2536 if (WARN_ON_ONCE(!hnd))
2539 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2544 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2546 struct ftrace_iterator *iter = m->private;
2550 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2553 if (iter->func_pos > *pos)
2557 for (l = 0; l <= (*pos - iter->func_pos); ) {
2558 p = t_hash_next(m, &l);
2565 /* Only set this if we have an item */
2566 iter->flags |= FTRACE_ITER_HASH;
2572 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2574 struct ftrace_func_probe *rec;
2577 if (WARN_ON_ONCE(!rec))
2580 if (rec->ops->print)
2581 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2583 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2586 seq_printf(m, ":%p", rec->data);
2593 t_next(struct seq_file *m, void *v, loff_t *pos)
2595 struct ftrace_iterator *iter = m->private;
2596 struct ftrace_ops *ops = iter->ops;
2597 struct dyn_ftrace *rec = NULL;
2599 if (unlikely(ftrace_disabled))
2602 if (iter->flags & FTRACE_ITER_HASH)
2603 return t_hash_next(m, pos);
2606 iter->pos = iter->func_pos = *pos;
2608 if (iter->flags & FTRACE_ITER_PRINTALL)
2609 return t_hash_start(m, pos);
2612 if (iter->idx >= iter->pg->index) {
2613 if (iter->pg->next) {
2614 iter->pg = iter->pg->next;
2619 rec = &iter->pg->records[iter->idx++];
2620 if (((iter->flags & FTRACE_ITER_FILTER) &&
2621 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2623 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2624 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2626 ((iter->flags & FTRACE_ITER_ENABLED) &&
2627 !(rec->flags & FTRACE_FL_ENABLED))) {
2635 return t_hash_start(m, pos);
2642 static void reset_iter_read(struct ftrace_iterator *iter)
2646 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2649 static void *t_start(struct seq_file *m, loff_t *pos)
2651 struct ftrace_iterator *iter = m->private;
2652 struct ftrace_ops *ops = iter->ops;
2656 mutex_lock(&ftrace_lock);
2658 if (unlikely(ftrace_disabled))
2662 * If an lseek was done, then reset and start from beginning.
2664 if (*pos < iter->pos)
2665 reset_iter_read(iter);
2668 * For set_ftrace_filter reading, if we have the filter
2669 * off, we can short cut and just print out that all
2670 * functions are enabled.
2672 if (iter->flags & FTRACE_ITER_FILTER &&
2673 ftrace_hash_empty(ops->filter_hash)) {
2675 return t_hash_start(m, pos);
2676 iter->flags |= FTRACE_ITER_PRINTALL;
2677 /* reset in case of seek/pread */
2678 iter->flags &= ~FTRACE_ITER_HASH;
2682 if (iter->flags & FTRACE_ITER_HASH)
2683 return t_hash_start(m, pos);
2686 * Unfortunately, we need to restart at ftrace_pages_start
2687 * every time we let go of the ftrace_mutex. This is because
2688 * those pointers can change without the lock.
2690 iter->pg = ftrace_pages_start;
2692 for (l = 0; l <= *pos; ) {
2693 p = t_next(m, p, &l);
2699 return t_hash_start(m, pos);
2704 static void t_stop(struct seq_file *m, void *p)
2706 mutex_unlock(&ftrace_lock);
2709 static int t_show(struct seq_file *m, void *v)
2711 struct ftrace_iterator *iter = m->private;
2712 struct dyn_ftrace *rec;
2714 if (iter->flags & FTRACE_ITER_HASH)
2715 return t_hash_show(m, iter);
2717 if (iter->flags & FTRACE_ITER_PRINTALL) {
2718 seq_printf(m, "#### all functions enabled ####\n");
2727 seq_printf(m, "%ps", (void *)rec->ip);
2728 if (iter->flags & FTRACE_ITER_ENABLED)
2729 seq_printf(m, " (%ld)%s",
2730 rec->flags & ~FTRACE_FL_MASK,
2731 rec->flags & FTRACE_FL_REGS ? " R" : "");
2732 seq_printf(m, "\n");
2737 static const struct seq_operations show_ftrace_seq_ops = {
2745 ftrace_avail_open(struct inode *inode, struct file *file)
2747 struct ftrace_iterator *iter;
2749 if (unlikely(ftrace_disabled))
2752 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2754 iter->pg = ftrace_pages_start;
2755 iter->ops = &global_ops;
2758 return iter ? 0 : -ENOMEM;
2762 ftrace_enabled_open(struct inode *inode, struct file *file)
2764 struct ftrace_iterator *iter;
2766 if (unlikely(ftrace_disabled))
2769 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2771 iter->pg = ftrace_pages_start;
2772 iter->flags = FTRACE_ITER_ENABLED;
2773 iter->ops = &global_ops;
2776 return iter ? 0 : -ENOMEM;
2779 static void ftrace_filter_reset(struct ftrace_hash *hash)
2781 mutex_lock(&ftrace_lock);
2782 ftrace_hash_clear(hash);
2783 mutex_unlock(&ftrace_lock);
2787 * ftrace_regex_open - initialize function tracer filter files
2788 * @ops: The ftrace_ops that hold the hash filters
2789 * @flag: The type of filter to process
2790 * @inode: The inode, usually passed in to your open routine
2791 * @file: The file, usually passed in to your open routine
2793 * ftrace_regex_open() initializes the filter files for the
2794 * @ops. Depending on @flag it may process the filter hash or
2795 * the notrace hash of @ops. With this called from the open
2796 * routine, you can use ftrace_filter_write() for the write
2797 * routine if @flag has FTRACE_ITER_FILTER set, or
2798 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2799 * tracing_lseek() should be used as the lseek routine, and
2800 * release must call ftrace_regex_release().
2803 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2804 struct inode *inode, struct file *file)
2806 struct ftrace_iterator *iter;
2807 struct ftrace_hash *hash;
2810 ftrace_ops_init(ops);
2812 if (unlikely(ftrace_disabled))
2815 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2819 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2827 mutex_lock(&ops->regex_lock);
2829 if (flag & FTRACE_ITER_NOTRACE)
2830 hash = ops->notrace_hash;
2832 hash = ops->filter_hash;
2834 if (file->f_mode & FMODE_WRITE) {
2835 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2837 trace_parser_put(&iter->parser);
2844 if ((file->f_mode & FMODE_WRITE) &&
2845 (file->f_flags & O_TRUNC))
2846 ftrace_filter_reset(iter->hash);
2848 if (file->f_mode & FMODE_READ) {
2849 iter->pg = ftrace_pages_start;
2851 ret = seq_open(file, &show_ftrace_seq_ops);
2853 struct seq_file *m = file->private_data;
2857 free_ftrace_hash(iter->hash);
2858 trace_parser_put(&iter->parser);
2862 file->private_data = iter;
2865 mutex_unlock(&ops->regex_lock);
2871 ftrace_filter_open(struct inode *inode, struct file *file)
2873 return ftrace_regex_open(&global_ops,
2874 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2879 ftrace_notrace_open(struct inode *inode, struct file *file)
2881 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2885 static int ftrace_match(char *str, char *regex, int len, int type)
2892 if (strcmp(str, regex) == 0)
2895 case MATCH_FRONT_ONLY:
2896 if (strncmp(str, regex, len) == 0)
2899 case MATCH_MIDDLE_ONLY:
2900 if (strstr(str, regex))
2903 case MATCH_END_ONLY:
2905 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2914 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2916 struct ftrace_func_entry *entry;
2919 entry = ftrace_lookup_ip(hash, rec->ip);
2921 /* Do nothing if it doesn't exist */
2925 free_hash_entry(hash, entry);
2927 /* Do nothing if it exists */
2931 ret = add_hash_entry(hash, rec->ip);
2937 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2938 char *regex, int len, int type)
2940 char str[KSYM_SYMBOL_LEN];
2943 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2946 /* module lookup requires matching the module */
2947 if (!modname || strcmp(modname, mod))
2950 /* blank search means to match all funcs in the mod */
2955 return ftrace_match(str, regex, len, type);
2959 match_records(struct ftrace_hash *hash, char *buff,
2960 int len, char *mod, int not)
2962 unsigned search_len = 0;
2963 struct ftrace_page *pg;
2964 struct dyn_ftrace *rec;
2965 int type = MATCH_FULL;
2966 char *search = buff;
2971 type = filter_parse_regex(buff, len, &search, ¬);
2972 search_len = strlen(search);
2975 mutex_lock(&ftrace_lock);
2977 if (unlikely(ftrace_disabled))
2980 do_for_each_ftrace_rec(pg, rec) {
2981 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2982 ret = enter_record(hash, rec, not);
2989 } while_for_each_ftrace_rec();
2991 mutex_unlock(&ftrace_lock);
2997 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2999 return match_records(hash, buff, len, NULL, 0);
3003 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
3007 /* blank or '*' mean the same */
3008 if (strcmp(buff, "*") == 0)
3011 /* handle the case of 'dont filter this module' */
3012 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3017 return match_records(hash, buff, strlen(buff), mod, not);
3021 * We register the module command as a template to show others how
3022 * to register the a command as well.
3026 ftrace_mod_callback(struct ftrace_hash *hash,
3027 char *func, char *cmd, char *param, int enable)
3033 * cmd == 'mod' because we only registered this func
3034 * for the 'mod' ftrace_func_command.
3035 * But if you register one func with multiple commands,
3036 * you can tell which command was used by the cmd
3040 /* we must have a module name */
3044 mod = strsep(¶m, ":");
3048 ret = ftrace_match_module_records(hash, func, mod);
3057 static struct ftrace_func_command ftrace_mod_cmd = {
3059 .func = ftrace_mod_callback,
3062 static int __init ftrace_mod_cmd_init(void)
3064 return register_ftrace_command(&ftrace_mod_cmd);
3066 core_initcall(ftrace_mod_cmd_init);
3068 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3069 struct ftrace_ops *op, struct pt_regs *pt_regs)
3071 struct ftrace_func_probe *entry;
3072 struct hlist_head *hhd;
3075 key = hash_long(ip, FTRACE_HASH_BITS);
3077 hhd = &ftrace_func_hash[key];
3079 if (hlist_empty(hhd))
3083 * Disable preemption for these calls to prevent a RCU grace
3084 * period. This syncs the hash iteration and freeing of items
3085 * on the hash. rcu_read_lock is too dangerous here.
3087 preempt_disable_notrace();
3088 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3089 if (entry->ip == ip)
3090 entry->ops->func(ip, parent_ip, &entry->data);
3092 preempt_enable_notrace();
3095 static struct ftrace_ops trace_probe_ops __read_mostly =
3097 .func = function_trace_probe_call,
3098 .flags = FTRACE_OPS_FL_INITIALIZED,
3099 INIT_REGEX_LOCK(trace_probe_ops)
3102 static int ftrace_probe_registered;
3104 static void __enable_ftrace_function_probe(void)
3109 if (ftrace_probe_registered) {
3110 /* still need to update the function call sites */
3112 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3116 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3117 struct hlist_head *hhd = &ftrace_func_hash[i];
3121 /* Nothing registered? */
3122 if (i == FTRACE_FUNC_HASHSIZE)
3125 ret = ftrace_startup(&trace_probe_ops, 0);
3127 ftrace_probe_registered = 1;
3130 static void __disable_ftrace_function_probe(void)
3134 if (!ftrace_probe_registered)
3137 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3138 struct hlist_head *hhd = &ftrace_func_hash[i];
3143 /* no more funcs left */
3144 ftrace_shutdown(&trace_probe_ops, 0);
3146 ftrace_probe_registered = 0;
3150 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3152 if (entry->ops->free)
3153 entry->ops->free(entry->ops, entry->ip, &entry->data);
3158 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3161 struct ftrace_func_probe *entry;
3162 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3163 struct ftrace_hash *hash;
3164 struct ftrace_page *pg;
3165 struct dyn_ftrace *rec;
3172 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3173 len = strlen(search);
3175 /* we do not support '!' for function probes */
3179 mutex_lock(&trace_probe_ops.regex_lock);
3181 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3187 if (unlikely(ftrace_disabled)) {
3192 mutex_lock(&ftrace_lock);
3194 do_for_each_ftrace_rec(pg, rec) {
3196 if (!ftrace_match_record(rec, NULL, search, len, type))
3199 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3201 /* If we did not process any, then return error */
3212 * The caller might want to do something special
3213 * for each function we find. We call the callback
3214 * to give the caller an opportunity to do so.
3217 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3218 /* caller does not like this func */
3224 ret = enter_record(hash, rec, 0);
3232 entry->ip = rec->ip;
3234 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3235 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3237 } while_for_each_ftrace_rec();
3239 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3243 __enable_ftrace_function_probe();
3246 mutex_unlock(&ftrace_lock);
3248 mutex_unlock(&trace_probe_ops.regex_lock);
3249 free_ftrace_hash(hash);
3255 PROBE_TEST_FUNC = 1,
3260 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3261 void *data, int flags)
3263 struct ftrace_func_entry *rec_entry;
3264 struct ftrace_func_probe *entry;
3265 struct ftrace_func_probe *p;
3266 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3267 struct list_head free_list;
3268 struct ftrace_hash *hash;
3269 struct hlist_node *tmp;
3270 char str[KSYM_SYMBOL_LEN];
3271 int type = MATCH_FULL;
3275 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3280 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3281 len = strlen(search);
3283 /* we do not support '!' for function probes */
3288 mutex_lock(&trace_probe_ops.regex_lock);
3290 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3292 /* Hmm, should report this somehow */
3295 INIT_LIST_HEAD(&free_list);
3297 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3298 struct hlist_head *hhd = &ftrace_func_hash[i];
3300 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3302 /* break up if statements for readability */
3303 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3306 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3309 /* do this last, since it is the most expensive */
3311 kallsyms_lookup(entry->ip, NULL, NULL,
3313 if (!ftrace_match(str, glob, len, type))
3317 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3318 /* It is possible more than one entry had this ip */
3320 free_hash_entry(hash, rec_entry);
3322 hlist_del_rcu(&entry->node);
3323 list_add(&entry->free_list, &free_list);
3326 mutex_lock(&ftrace_lock);
3327 __disable_ftrace_function_probe();
3329 * Remove after the disable is called. Otherwise, if the last
3330 * probe is removed, a null hash means *all enabled*.
3332 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3333 synchronize_sched();
3334 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3335 list_del(&entry->free_list);
3336 ftrace_free_entry(entry);
3338 mutex_unlock(&ftrace_lock);
3341 mutex_unlock(&trace_probe_ops.regex_lock);
3342 free_ftrace_hash(hash);
3346 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3349 __unregister_ftrace_function_probe(glob, ops, data,
3350 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3354 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3356 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3359 void unregister_ftrace_function_probe_all(char *glob)
3361 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3364 static LIST_HEAD(ftrace_commands);
3365 static DEFINE_MUTEX(ftrace_cmd_mutex);
3368 * Currently we only register ftrace commands from __init, so mark this
3371 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3373 struct ftrace_func_command *p;
3376 mutex_lock(&ftrace_cmd_mutex);
3377 list_for_each_entry(p, &ftrace_commands, list) {
3378 if (strcmp(cmd->name, p->name) == 0) {
3383 list_add(&cmd->list, &ftrace_commands);
3385 mutex_unlock(&ftrace_cmd_mutex);
3391 * Currently we only unregister ftrace commands from __init, so mark
3394 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3396 struct ftrace_func_command *p, *n;
3399 mutex_lock(&ftrace_cmd_mutex);
3400 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3401 if (strcmp(cmd->name, p->name) == 0) {
3403 list_del_init(&p->list);
3408 mutex_unlock(&ftrace_cmd_mutex);
3413 static int ftrace_process_regex(struct ftrace_hash *hash,
3414 char *buff, int len, int enable)
3416 char *func, *command, *next = buff;
3417 struct ftrace_func_command *p;
3420 func = strsep(&next, ":");
3423 ret = ftrace_match_records(hash, func, len);
3433 command = strsep(&next, ":");
3435 mutex_lock(&ftrace_cmd_mutex);
3436 list_for_each_entry(p, &ftrace_commands, list) {
3437 if (strcmp(p->name, command) == 0) {
3438 ret = p->func(hash, func, command, next, enable);
3443 mutex_unlock(&ftrace_cmd_mutex);
3449 ftrace_regex_write(struct file *file, const char __user *ubuf,
3450 size_t cnt, loff_t *ppos, int enable)
3452 struct ftrace_iterator *iter;
3453 struct trace_parser *parser;
3459 if (file->f_mode & FMODE_READ) {
3460 struct seq_file *m = file->private_data;
3463 iter = file->private_data;
3465 if (unlikely(ftrace_disabled))
3468 /* iter->hash is a local copy, so we don't need regex_lock */
3470 parser = &iter->parser;
3471 read = trace_get_user(parser, ubuf, cnt, ppos);
3473 if (read >= 0 && trace_parser_loaded(parser) &&
3474 !trace_parser_cont(parser)) {
3475 ret = ftrace_process_regex(iter->hash, parser->buffer,
3476 parser->idx, enable);
3477 trace_parser_clear(parser);
3488 ftrace_filter_write(struct file *file, const char __user *ubuf,
3489 size_t cnt, loff_t *ppos)
3491 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3495 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3496 size_t cnt, loff_t *ppos)
3498 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3502 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3504 struct ftrace_func_entry *entry;
3506 if (!ftrace_location(ip))
3510 entry = ftrace_lookup_ip(hash, ip);
3513 free_hash_entry(hash, entry);
3517 return add_hash_entry(hash, ip);
3520 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3522 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3523 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3527 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3528 unsigned long ip, int remove, int reset, int enable)
3530 struct ftrace_hash **orig_hash;
3531 struct ftrace_hash *hash;
3534 /* All global ops uses the global ops filters */
3535 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3538 if (unlikely(ftrace_disabled))
3541 mutex_lock(&ops->regex_lock);
3544 orig_hash = &ops->filter_hash;
3546 orig_hash = &ops->notrace_hash;
3548 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3551 goto out_regex_unlock;
3555 ftrace_filter_reset(hash);
3556 if (buf && !ftrace_match_records(hash, buf, len)) {
3558 goto out_regex_unlock;
3561 ret = ftrace_match_addr(hash, ip, remove);
3563 goto out_regex_unlock;
3566 mutex_lock(&ftrace_lock);
3567 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3569 ftrace_ops_update_code(ops);
3571 mutex_unlock(&ftrace_lock);
3574 mutex_unlock(&ops->regex_lock);
3576 free_ftrace_hash(hash);
3581 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3582 int reset, int enable)
3584 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3588 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3589 * @ops - the ops to set the filter with
3590 * @ip - the address to add to or remove from the filter.
3591 * @remove - non zero to remove the ip from the filter
3592 * @reset - non zero to reset all filters before applying this filter.
3594 * Filters denote which functions should be enabled when tracing is enabled
3595 * If @ip is NULL, it failes to update filter.
3597 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3598 int remove, int reset)
3600 ftrace_ops_init(ops);
3601 return ftrace_set_addr(ops, ip, remove, reset, 1);
3603 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3606 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3607 int reset, int enable)
3609 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3613 * ftrace_set_filter - set a function to filter on in ftrace
3614 * @ops - the ops to set the filter with
3615 * @buf - the string that holds the function filter text.
3616 * @len - the length of the string.
3617 * @reset - non zero to reset all filters before applying this filter.
3619 * Filters denote which functions should be enabled when tracing is enabled.
3620 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3622 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3625 ftrace_ops_init(ops);
3626 return ftrace_set_regex(ops, buf, len, reset, 1);
3628 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3631 * ftrace_set_notrace - set a function to not trace in ftrace
3632 * @ops - the ops to set the notrace filter with
3633 * @buf - the string that holds the function notrace text.
3634 * @len - the length of the string.
3635 * @reset - non zero to reset all filters before applying this filter.
3637 * Notrace Filters denote which functions should not be enabled when tracing
3638 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3641 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3644 ftrace_ops_init(ops);
3645 return ftrace_set_regex(ops, buf, len, reset, 0);
3647 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3649 * ftrace_set_filter - set a function to filter on in ftrace
3650 * @ops - the ops to set the filter with
3651 * @buf - the string that holds the function filter text.
3652 * @len - the length of the string.
3653 * @reset - non zero to reset all filters before applying this filter.
3655 * Filters denote which functions should be enabled when tracing is enabled.
3656 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3658 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3660 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3662 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3665 * ftrace_set_notrace - set a function to not trace in ftrace
3666 * @ops - the ops to set the notrace filter with
3667 * @buf - the string that holds the function notrace text.
3668 * @len - the length of the string.
3669 * @reset - non zero to reset all filters before applying this filter.
3671 * Notrace Filters denote which functions should not be enabled when tracing
3672 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3675 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3677 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3679 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3682 * command line interface to allow users to set filters on boot up.
3684 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3685 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3686 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3688 /* Used by function selftest to not test if filter is set */
3689 bool ftrace_filter_param __initdata;
3691 static int __init set_ftrace_notrace(char *str)
3693 ftrace_filter_param = true;
3694 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3697 __setup("ftrace_notrace=", set_ftrace_notrace);
3699 static int __init set_ftrace_filter(char *str)
3701 ftrace_filter_param = true;
3702 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3705 __setup("ftrace_filter=", set_ftrace_filter);
3707 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3708 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3709 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3711 static int __init set_graph_function(char *str)
3713 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3716 __setup("ftrace_graph_filter=", set_graph_function);
3718 static void __init set_ftrace_early_graph(char *buf)
3724 func = strsep(&buf, ",");
3725 /* we allow only one expression at a time */
3726 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3727 FTRACE_GRAPH_MAX_FUNCS, func);
3729 printk(KERN_DEBUG "ftrace: function %s not "
3730 "traceable\n", func);
3733 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3736 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3740 ftrace_ops_init(ops);
3743 func = strsep(&buf, ",");
3744 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3748 static void __init set_ftrace_early_filters(void)
3750 if (ftrace_filter_buf[0])
3751 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3752 if (ftrace_notrace_buf[0])
3753 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3754 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3755 if (ftrace_graph_buf[0])
3756 set_ftrace_early_graph(ftrace_graph_buf);
3757 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3760 int ftrace_regex_release(struct inode *inode, struct file *file)
3762 struct seq_file *m = (struct seq_file *)file->private_data;
3763 struct ftrace_iterator *iter;
3764 struct ftrace_hash **orig_hash;
3765 struct trace_parser *parser;
3769 if (file->f_mode & FMODE_READ) {
3771 seq_release(inode, file);
3773 iter = file->private_data;
3775 parser = &iter->parser;
3776 if (trace_parser_loaded(parser)) {
3777 parser->buffer[parser->idx] = 0;
3778 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3781 trace_parser_put(parser);
3783 mutex_lock(&iter->ops->regex_lock);
3785 if (file->f_mode & FMODE_WRITE) {
3786 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3789 orig_hash = &iter->ops->filter_hash;
3791 orig_hash = &iter->ops->notrace_hash;
3793 mutex_lock(&ftrace_lock);
3794 ret = ftrace_hash_move(iter->ops, filter_hash,
3795 orig_hash, iter->hash);
3797 ftrace_ops_update_code(iter->ops);
3799 mutex_unlock(&ftrace_lock);
3802 mutex_unlock(&iter->ops->regex_lock);
3803 free_ftrace_hash(iter->hash);
3809 static const struct file_operations ftrace_avail_fops = {
3810 .open = ftrace_avail_open,
3812 .llseek = seq_lseek,
3813 .release = seq_release_private,
3816 static const struct file_operations ftrace_enabled_fops = {
3817 .open = ftrace_enabled_open,
3819 .llseek = seq_lseek,
3820 .release = seq_release_private,
3823 static const struct file_operations ftrace_filter_fops = {
3824 .open = ftrace_filter_open,
3826 .write = ftrace_filter_write,
3827 .llseek = tracing_lseek,
3828 .release = ftrace_regex_release,
3831 static const struct file_operations ftrace_notrace_fops = {
3832 .open = ftrace_notrace_open,
3834 .write = ftrace_notrace_write,
3835 .llseek = tracing_lseek,
3836 .release = ftrace_regex_release,
3839 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3841 static DEFINE_MUTEX(graph_lock);
3843 int ftrace_graph_count;
3844 int ftrace_graph_notrace_count;
3845 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3846 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3848 struct ftrace_graph_data {
3849 unsigned long *table;
3852 const struct seq_operations *seq_ops;
3856 __g_next(struct seq_file *m, loff_t *pos)
3858 struct ftrace_graph_data *fgd = m->private;
3860 if (*pos >= *fgd->count)
3862 return &fgd->table[*pos];
3866 g_next(struct seq_file *m, void *v, loff_t *pos)
3869 return __g_next(m, pos);
3872 static void *g_start(struct seq_file *m, loff_t *pos)
3874 struct ftrace_graph_data *fgd = m->private;
3876 mutex_lock(&graph_lock);
3878 /* Nothing, tell g_show to print all functions are enabled */
3879 if (!*fgd->count && !*pos)
3882 return __g_next(m, pos);
3885 static void g_stop(struct seq_file *m, void *p)
3887 mutex_unlock(&graph_lock);
3890 static int g_show(struct seq_file *m, void *v)
3892 unsigned long *ptr = v;
3897 if (ptr == (unsigned long *)1) {
3898 seq_printf(m, "#### all functions enabled ####\n");
3902 seq_printf(m, "%ps\n", (void *)*ptr);
3907 static const struct seq_operations ftrace_graph_seq_ops = {
3915 __ftrace_graph_open(struct inode *inode, struct file *file,
3916 struct ftrace_graph_data *fgd)
3920 mutex_lock(&graph_lock);
3921 if ((file->f_mode & FMODE_WRITE) &&
3922 (file->f_flags & O_TRUNC)) {
3924 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
3926 mutex_unlock(&graph_lock);
3928 if (file->f_mode & FMODE_READ) {
3929 ret = seq_open(file, fgd->seq_ops);
3931 struct seq_file *m = file->private_data;
3935 file->private_data = fgd;
3941 ftrace_graph_open(struct inode *inode, struct file *file)
3943 struct ftrace_graph_data *fgd;
3945 if (unlikely(ftrace_disabled))
3948 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3952 fgd->table = ftrace_graph_funcs;
3953 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3954 fgd->count = &ftrace_graph_count;
3955 fgd->seq_ops = &ftrace_graph_seq_ops;
3957 return __ftrace_graph_open(inode, file, fgd);
3961 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3963 struct ftrace_graph_data *fgd;
3965 if (unlikely(ftrace_disabled))
3968 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3972 fgd->table = ftrace_graph_notrace_funcs;
3973 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3974 fgd->count = &ftrace_graph_notrace_count;
3975 fgd->seq_ops = &ftrace_graph_seq_ops;
3977 return __ftrace_graph_open(inode, file, fgd);
3981 ftrace_graph_release(struct inode *inode, struct file *file)
3983 if (file->f_mode & FMODE_READ) {
3984 struct seq_file *m = file->private_data;
3987 seq_release(inode, file);
3989 kfree(file->private_data);
3996 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
3998 struct dyn_ftrace *rec;
3999 struct ftrace_page *pg;
4008 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
4009 if (!not && *idx >= size)
4012 search_len = strlen(search);
4014 mutex_lock(&ftrace_lock);
4016 if (unlikely(ftrace_disabled)) {
4017 mutex_unlock(&ftrace_lock);
4021 do_for_each_ftrace_rec(pg, rec) {
4023 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4024 /* if it is in the array */
4026 for (i = 0; i < *idx; i++) {
4027 if (array[i] == rec->ip) {
4036 array[(*idx)++] = rec->ip;
4042 array[i] = array[--(*idx)];
4048 } while_for_each_ftrace_rec();
4050 mutex_unlock(&ftrace_lock);
4059 ftrace_graph_write(struct file *file, const char __user *ubuf,
4060 size_t cnt, loff_t *ppos)
4062 struct trace_parser parser;
4063 ssize_t read, ret = 0;
4064 struct ftrace_graph_data *fgd = file->private_data;
4069 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4072 read = trace_get_user(&parser, ubuf, cnt, ppos);
4074 if (read >= 0 && trace_parser_loaded((&parser))) {
4075 parser.buffer[parser.idx] = 0;
4077 mutex_lock(&graph_lock);
4079 /* we allow only one expression at a time */
4080 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4083 mutex_unlock(&graph_lock);
4089 trace_parser_put(&parser);
4094 static const struct file_operations ftrace_graph_fops = {
4095 .open = ftrace_graph_open,
4097 .write = ftrace_graph_write,
4098 .llseek = tracing_lseek,
4099 .release = ftrace_graph_release,
4102 static const struct file_operations ftrace_graph_notrace_fops = {
4103 .open = ftrace_graph_notrace_open,
4105 .write = ftrace_graph_write,
4106 .llseek = tracing_lseek,
4107 .release = ftrace_graph_release,
4109 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4111 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4114 trace_create_file("available_filter_functions", 0444,
4115 d_tracer, NULL, &ftrace_avail_fops);
4117 trace_create_file("enabled_functions", 0444,
4118 d_tracer, NULL, &ftrace_enabled_fops);
4120 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4121 NULL, &ftrace_filter_fops);
4123 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
4124 NULL, &ftrace_notrace_fops);
4126 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4127 trace_create_file("set_graph_function", 0444, d_tracer,
4129 &ftrace_graph_fops);
4130 trace_create_file("set_graph_notrace", 0444, d_tracer,
4132 &ftrace_graph_notrace_fops);
4133 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4138 static int ftrace_cmp_ips(const void *a, const void *b)
4140 const unsigned long *ipa = a;
4141 const unsigned long *ipb = b;
4150 static void ftrace_swap_ips(void *a, void *b, int size)
4152 unsigned long *ipa = a;
4153 unsigned long *ipb = b;
4161 static int ftrace_process_locs(struct module *mod,
4162 unsigned long *start,
4165 struct ftrace_page *start_pg;
4166 struct ftrace_page *pg;
4167 struct dyn_ftrace *rec;
4168 unsigned long count;
4171 unsigned long flags = 0; /* Shut up gcc */
4174 count = end - start;
4179 sort(start, count, sizeof(*start),
4180 ftrace_cmp_ips, ftrace_swap_ips);
4182 start_pg = ftrace_allocate_pages(count);
4186 mutex_lock(&ftrace_lock);
4189 * Core and each module needs their own pages, as
4190 * modules will free them when they are removed.
4191 * Force a new page to be allocated for modules.
4194 WARN_ON(ftrace_pages || ftrace_pages_start);
4195 /* First initialization */
4196 ftrace_pages = ftrace_pages_start = start_pg;
4201 if (WARN_ON(ftrace_pages->next)) {
4202 /* Hmm, we have free pages? */
4203 while (ftrace_pages->next)
4204 ftrace_pages = ftrace_pages->next;
4207 ftrace_pages->next = start_pg;
4213 addr = ftrace_call_adjust(*p++);
4215 * Some architecture linkers will pad between
4216 * the different mcount_loc sections of different
4217 * object files to satisfy alignments.
4218 * Skip any NULL pointers.
4223 if (pg->index == pg->size) {
4224 /* We should have allocated enough */
4225 if (WARN_ON(!pg->next))
4230 rec = &pg->records[pg->index++];
4234 /* We should have used all pages */
4237 /* Assign the last page to ftrace_pages */
4240 /* These new locations need to be initialized */
4241 ftrace_new_pgs = start_pg;
4244 * We only need to disable interrupts on start up
4245 * because we are modifying code that an interrupt
4246 * may execute, and the modification is not atomic.
4247 * But for modules, nothing runs the code we modify
4248 * until we are finished with it, and there's no
4249 * reason to cause large interrupt latencies while we do it.
4252 local_irq_save(flags);
4253 ftrace_update_code(mod);
4255 local_irq_restore(flags);
4258 mutex_unlock(&ftrace_lock);
4263 #ifdef CONFIG_MODULES
4265 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4267 void ftrace_release_mod(struct module *mod)
4269 struct dyn_ftrace *rec;
4270 struct ftrace_page **last_pg;
4271 struct ftrace_page *pg;
4274 mutex_lock(&ftrace_lock);
4276 if (ftrace_disabled)
4280 * Each module has its own ftrace_pages, remove
4281 * them from the list.
4283 last_pg = &ftrace_pages_start;
4284 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4285 rec = &pg->records[0];
4286 if (within_module_core(rec->ip, mod)) {
4288 * As core pages are first, the first
4289 * page should never be a module page.
4291 if (WARN_ON(pg == ftrace_pages_start))
4294 /* Check if we are deleting the last page */
4295 if (pg == ftrace_pages)
4296 ftrace_pages = next_to_ftrace_page(last_pg);
4298 *last_pg = pg->next;
4299 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4300 free_pages((unsigned long)pg->records, order);
4303 last_pg = &pg->next;
4306 mutex_unlock(&ftrace_lock);
4309 static void ftrace_init_module(struct module *mod,
4310 unsigned long *start, unsigned long *end)
4312 if (ftrace_disabled || start == end)
4314 ftrace_process_locs(mod, start, end);
4317 static int ftrace_module_notify_enter(struct notifier_block *self,
4318 unsigned long val, void *data)
4320 struct module *mod = data;
4322 if (val == MODULE_STATE_COMING)
4323 ftrace_init_module(mod, mod->ftrace_callsites,
4324 mod->ftrace_callsites +
4325 mod->num_ftrace_callsites);
4329 static int ftrace_module_notify_exit(struct notifier_block *self,
4330 unsigned long val, void *data)
4332 struct module *mod = data;
4334 if (val == MODULE_STATE_GOING)
4335 ftrace_release_mod(mod);
4340 static int ftrace_module_notify_enter(struct notifier_block *self,
4341 unsigned long val, void *data)
4345 static int ftrace_module_notify_exit(struct notifier_block *self,
4346 unsigned long val, void *data)
4350 #endif /* CONFIG_MODULES */
4352 struct notifier_block ftrace_module_enter_nb = {
4353 .notifier_call = ftrace_module_notify_enter,
4354 .priority = INT_MAX, /* Run before anything that can use kprobes */
4357 struct notifier_block ftrace_module_exit_nb = {
4358 .notifier_call = ftrace_module_notify_exit,
4359 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4362 extern unsigned long __start_mcount_loc[];
4363 extern unsigned long __stop_mcount_loc[];
4365 void __init ftrace_init(void)
4367 unsigned long count, addr, flags;
4370 /* Keep the ftrace pointer to the stub */
4371 addr = (unsigned long)ftrace_stub;
4373 local_irq_save(flags);
4374 ftrace_dyn_arch_init(&addr);
4375 local_irq_restore(flags);
4377 /* ftrace_dyn_arch_init places the return code in addr */
4381 count = __stop_mcount_loc - __start_mcount_loc;
4383 ret = ftrace_dyn_table_alloc(count);
4387 last_ftrace_enabled = ftrace_enabled = 1;
4389 ret = ftrace_process_locs(NULL,
4393 ret = register_module_notifier(&ftrace_module_enter_nb);
4395 pr_warning("Failed to register trace ftrace module enter notifier\n");
4397 ret = register_module_notifier(&ftrace_module_exit_nb);
4399 pr_warning("Failed to register trace ftrace module exit notifier\n");
4401 set_ftrace_early_filters();
4405 ftrace_disabled = 1;
4410 static struct ftrace_ops global_ops = {
4411 .func = ftrace_stub,
4412 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4413 INIT_REGEX_LOCK(global_ops)
4416 static int __init ftrace_nodyn_init(void)
4421 core_initcall(ftrace_nodyn_init);
4423 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4424 static inline void ftrace_startup_enable(int command) { }
4425 /* Keep as macros so we do not need to define the commands */
4426 # define ftrace_startup(ops, command) \
4428 int ___ret = __register_ftrace_function(ops); \
4430 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4433 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4435 # define ftrace_startup_sysctl() do { } while (0)
4436 # define ftrace_shutdown_sysctl() do { } while (0)
4439 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4444 #endif /* CONFIG_DYNAMIC_FTRACE */
4447 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4448 struct ftrace_ops *op, struct pt_regs *regs)
4450 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4454 * Some of the ops may be dynamically allocated,
4455 * they must be freed after a synchronize_sched().
4457 preempt_disable_notrace();
4458 trace_recursion_set(TRACE_CONTROL_BIT);
4461 * Control funcs (perf) uses RCU. Only trace if
4462 * RCU is currently active.
4464 if (!rcu_is_watching())
4467 do_for_each_ftrace_op(op, ftrace_control_list) {
4468 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4469 !ftrace_function_local_disabled(op) &&
4470 ftrace_ops_test(op, ip, regs))
4471 op->func(ip, parent_ip, op, regs);
4472 } while_for_each_ftrace_op(op);
4474 trace_recursion_clear(TRACE_CONTROL_BIT);
4475 preempt_enable_notrace();
4478 static struct ftrace_ops control_ops = {
4479 .func = ftrace_ops_control_func,
4480 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4481 INIT_REGEX_LOCK(control_ops)
4485 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4486 struct ftrace_ops *ignored, struct pt_regs *regs)
4488 struct ftrace_ops *op;
4491 if (function_trace_stop)
4494 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4499 * Some of the ops may be dynamically allocated,
4500 * they must be freed after a synchronize_sched().
4502 preempt_disable_notrace();
4503 do_for_each_ftrace_op(op, ftrace_ops_list) {
4504 if (ftrace_ops_test(op, ip, regs))
4505 op->func(ip, parent_ip, op, regs);
4506 } while_for_each_ftrace_op(op);
4507 preempt_enable_notrace();
4508 trace_clear_recursion(bit);
4512 * Some archs only support passing ip and parent_ip. Even though
4513 * the list function ignores the op parameter, we do not want any
4514 * C side effects, where a function is called without the caller
4515 * sending a third parameter.
4516 * Archs are to support both the regs and ftrace_ops at the same time.
4517 * If they support ftrace_ops, it is assumed they support regs.
4518 * If call backs want to use regs, they must either check for regs
4519 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4520 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4521 * An architecture can pass partial regs with ftrace_ops and still
4522 * set the ARCH_SUPPORT_FTARCE_OPS.
4524 #if ARCH_SUPPORTS_FTRACE_OPS
4525 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4526 struct ftrace_ops *op, struct pt_regs *regs)
4528 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4531 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4533 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4537 static void clear_ftrace_swapper(void)
4539 struct task_struct *p;
4543 for_each_online_cpu(cpu) {
4545 clear_tsk_trace_trace(p);
4550 static void set_ftrace_swapper(void)
4552 struct task_struct *p;
4556 for_each_online_cpu(cpu) {
4558 set_tsk_trace_trace(p);
4563 static void clear_ftrace_pid(struct pid *pid)
4565 struct task_struct *p;
4568 do_each_pid_task(pid, PIDTYPE_PID, p) {
4569 clear_tsk_trace_trace(p);
4570 } while_each_pid_task(pid, PIDTYPE_PID, p);
4576 static void set_ftrace_pid(struct pid *pid)
4578 struct task_struct *p;
4581 do_each_pid_task(pid, PIDTYPE_PID, p) {
4582 set_tsk_trace_trace(p);
4583 } while_each_pid_task(pid, PIDTYPE_PID, p);
4587 static void clear_ftrace_pid_task(struct pid *pid)
4589 if (pid == ftrace_swapper_pid)
4590 clear_ftrace_swapper();
4592 clear_ftrace_pid(pid);
4595 static void set_ftrace_pid_task(struct pid *pid)
4597 if (pid == ftrace_swapper_pid)
4598 set_ftrace_swapper();
4600 set_ftrace_pid(pid);
4603 static int ftrace_pid_add(int p)
4606 struct ftrace_pid *fpid;
4609 mutex_lock(&ftrace_lock);
4612 pid = ftrace_swapper_pid;
4614 pid = find_get_pid(p);
4621 list_for_each_entry(fpid, &ftrace_pids, list)
4622 if (fpid->pid == pid)
4627 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4631 list_add(&fpid->list, &ftrace_pids);
4634 set_ftrace_pid_task(pid);
4636 ftrace_update_pid_func();
4637 ftrace_startup_enable(0);
4639 mutex_unlock(&ftrace_lock);
4643 if (pid != ftrace_swapper_pid)
4647 mutex_unlock(&ftrace_lock);
4651 static void ftrace_pid_reset(void)
4653 struct ftrace_pid *fpid, *safe;
4655 mutex_lock(&ftrace_lock);
4656 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4657 struct pid *pid = fpid->pid;
4659 clear_ftrace_pid_task(pid);
4661 list_del(&fpid->list);
4665 ftrace_update_pid_func();
4666 ftrace_startup_enable(0);
4668 mutex_unlock(&ftrace_lock);
4671 static void *fpid_start(struct seq_file *m, loff_t *pos)
4673 mutex_lock(&ftrace_lock);
4675 if (list_empty(&ftrace_pids) && (!*pos))
4678 return seq_list_start(&ftrace_pids, *pos);
4681 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4686 return seq_list_next(v, &ftrace_pids, pos);
4689 static void fpid_stop(struct seq_file *m, void *p)
4691 mutex_unlock(&ftrace_lock);
4694 static int fpid_show(struct seq_file *m, void *v)
4696 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4698 if (v == (void *)1) {
4699 seq_printf(m, "no pid\n");
4703 if (fpid->pid == ftrace_swapper_pid)
4704 seq_printf(m, "swapper tasks\n");
4706 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4711 static const struct seq_operations ftrace_pid_sops = {
4712 .start = fpid_start,
4719 ftrace_pid_open(struct inode *inode, struct file *file)
4723 if ((file->f_mode & FMODE_WRITE) &&
4724 (file->f_flags & O_TRUNC))
4727 if (file->f_mode & FMODE_READ)
4728 ret = seq_open(file, &ftrace_pid_sops);
4734 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4735 size_t cnt, loff_t *ppos)
4741 if (cnt >= sizeof(buf))
4744 if (copy_from_user(&buf, ubuf, cnt))
4750 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4751 * to clean the filter quietly.
4753 tmp = strstrip(buf);
4754 if (strlen(tmp) == 0)
4757 ret = kstrtol(tmp, 10, &val);
4761 ret = ftrace_pid_add(val);
4763 return ret ? ret : cnt;
4767 ftrace_pid_release(struct inode *inode, struct file *file)
4769 if (file->f_mode & FMODE_READ)
4770 seq_release(inode, file);
4775 static const struct file_operations ftrace_pid_fops = {
4776 .open = ftrace_pid_open,
4777 .write = ftrace_pid_write,
4779 .llseek = tracing_lseek,
4780 .release = ftrace_pid_release,
4783 static __init int ftrace_init_debugfs(void)
4785 struct dentry *d_tracer;
4787 d_tracer = tracing_init_dentry();
4791 ftrace_init_dyn_debugfs(d_tracer);
4793 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4794 NULL, &ftrace_pid_fops);
4796 ftrace_profile_debugfs(d_tracer);
4800 fs_initcall(ftrace_init_debugfs);
4803 * ftrace_kill - kill ftrace
4805 * This function should be used by panic code. It stops ftrace
4806 * but in a not so nice way. If you need to simply kill ftrace
4807 * from a non-atomic section, use ftrace_kill.
4809 void ftrace_kill(void)
4811 ftrace_disabled = 1;
4813 clear_ftrace_function();
4817 * Test if ftrace is dead or not.
4819 int ftrace_is_dead(void)
4821 return ftrace_disabled;
4825 * register_ftrace_function - register a function for profiling
4826 * @ops - ops structure that holds the function for profiling.
4828 * Register a function to be called by all functions in the
4831 * Note: @ops->func and all the functions it calls must be labeled
4832 * with "notrace", otherwise it will go into a
4835 int register_ftrace_function(struct ftrace_ops *ops)
4839 ftrace_ops_init(ops);
4841 mutex_lock(&ftrace_lock);
4843 ret = ftrace_startup(ops, 0);
4845 mutex_unlock(&ftrace_lock);
4849 EXPORT_SYMBOL_GPL(register_ftrace_function);
4852 * unregister_ftrace_function - unregister a function for profiling.
4853 * @ops - ops structure that holds the function to unregister
4855 * Unregister a function that was added to be called by ftrace profiling.
4857 int unregister_ftrace_function(struct ftrace_ops *ops)
4861 mutex_lock(&ftrace_lock);
4862 ret = ftrace_shutdown(ops, 0);
4863 mutex_unlock(&ftrace_lock);
4867 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4870 ftrace_enable_sysctl(struct ctl_table *table, int write,
4871 void __user *buffer, size_t *lenp,
4876 mutex_lock(&ftrace_lock);
4878 if (unlikely(ftrace_disabled))
4881 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4883 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4886 last_ftrace_enabled = !!ftrace_enabled;
4888 if (ftrace_enabled) {
4890 ftrace_startup_sysctl();
4892 /* we are starting ftrace again */
4893 if (ftrace_ops_list != &ftrace_list_end)
4894 update_ftrace_function();
4897 /* stopping ftrace calls (just send to ftrace_stub) */
4898 ftrace_trace_function = ftrace_stub;
4900 ftrace_shutdown_sysctl();
4904 mutex_unlock(&ftrace_lock);
4908 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4910 static int ftrace_graph_active;
4911 static struct notifier_block ftrace_suspend_notifier;
4913 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4918 /* The callbacks that hook a function */
4919 trace_func_graph_ret_t ftrace_graph_return =
4920 (trace_func_graph_ret_t)ftrace_stub;
4921 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4922 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
4924 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4925 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4929 unsigned long flags;
4930 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4931 struct task_struct *g, *t;
4933 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4934 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4935 * sizeof(struct ftrace_ret_stack),
4937 if (!ret_stack_list[i]) {
4945 read_lock_irqsave(&tasklist_lock, flags);
4946 do_each_thread(g, t) {
4952 if (t->ret_stack == NULL) {
4953 atomic_set(&t->tracing_graph_pause, 0);
4954 atomic_set(&t->trace_overrun, 0);
4955 t->curr_ret_stack = -1;
4956 /* Make sure the tasks see the -1 first: */
4958 t->ret_stack = ret_stack_list[start++];
4960 } while_each_thread(g, t);
4963 read_unlock_irqrestore(&tasklist_lock, flags);
4965 for (i = start; i < end; i++)
4966 kfree(ret_stack_list[i]);
4971 ftrace_graph_probe_sched_switch(void *ignore,
4972 struct task_struct *prev, struct task_struct *next)
4974 unsigned long long timestamp;
4978 * Does the user want to count the time a function was asleep.
4979 * If so, do not update the time stamps.
4981 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4984 timestamp = trace_clock_local();
4986 prev->ftrace_timestamp = timestamp;
4988 /* only process tasks that we timestamped */
4989 if (!next->ftrace_timestamp)
4993 * Update all the counters in next to make up for the
4994 * time next was sleeping.
4996 timestamp -= next->ftrace_timestamp;
4998 for (index = next->curr_ret_stack; index >= 0; index--)
4999 next->ret_stack[index].calltime += timestamp;
5002 /* Allocate a return stack for each task */
5003 static int start_graph_tracing(void)
5005 struct ftrace_ret_stack **ret_stack_list;
5008 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5009 sizeof(struct ftrace_ret_stack *),
5012 if (!ret_stack_list)
5015 /* The cpu_boot init_task->ret_stack will never be freed */
5016 for_each_online_cpu(cpu) {
5017 if (!idle_task(cpu)->ret_stack)
5018 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5022 ret = alloc_retstack_tasklist(ret_stack_list);
5023 } while (ret == -EAGAIN);
5026 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5028 pr_info("ftrace_graph: Couldn't activate tracepoint"
5029 " probe to kernel_sched_switch\n");
5032 kfree(ret_stack_list);
5037 * Hibernation protection.
5038 * The state of the current task is too much unstable during
5039 * suspend/restore to disk. We want to protect against that.
5042 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5046 case PM_HIBERNATION_PREPARE:
5047 pause_graph_tracing();
5050 case PM_POST_HIBERNATION:
5051 unpause_graph_tracing();
5057 /* Just a place holder for function graph */
5058 static struct ftrace_ops fgraph_ops __read_mostly = {
5059 .func = ftrace_stub,
5060 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
5061 FTRACE_OPS_FL_RECURSION_SAFE,
5064 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5066 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5068 return __ftrace_graph_entry(trace);
5072 * The function graph tracer should only trace the functions defined
5073 * by set_ftrace_filter and set_ftrace_notrace. If another function
5074 * tracer ops is registered, the graph tracer requires testing the
5075 * function against the global ops, and not just trace any function
5076 * that any ftrace_ops registered.
5078 static void update_function_graph_func(void)
5080 if (ftrace_ops_list == &ftrace_list_end ||
5081 (ftrace_ops_list == &global_ops &&
5082 global_ops.next == &ftrace_list_end))
5083 ftrace_graph_entry = __ftrace_graph_entry;
5085 ftrace_graph_entry = ftrace_graph_entry_test;
5088 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5089 trace_func_graph_ent_t entryfunc)
5093 mutex_lock(&ftrace_lock);
5095 /* we currently allow only one tracer registered at a time */
5096 if (ftrace_graph_active) {
5101 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5102 register_pm_notifier(&ftrace_suspend_notifier);
5104 ftrace_graph_active++;
5105 ret = start_graph_tracing();
5107 ftrace_graph_active--;
5111 ftrace_graph_return = retfunc;
5114 * Update the indirect function to the entryfunc, and the
5115 * function that gets called to the entry_test first. Then
5116 * call the update fgraph entry function to determine if
5117 * the entryfunc should be called directly or not.
5119 __ftrace_graph_entry = entryfunc;
5120 ftrace_graph_entry = ftrace_graph_entry_test;
5121 update_function_graph_func();
5123 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
5126 mutex_unlock(&ftrace_lock);
5130 void unregister_ftrace_graph(void)
5132 mutex_lock(&ftrace_lock);
5134 if (unlikely(!ftrace_graph_active))
5137 ftrace_graph_active--;
5138 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5139 ftrace_graph_entry = ftrace_graph_entry_stub;
5140 __ftrace_graph_entry = ftrace_graph_entry_stub;
5141 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
5142 unregister_pm_notifier(&ftrace_suspend_notifier);
5143 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5146 mutex_unlock(&ftrace_lock);
5149 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5152 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5154 atomic_set(&t->tracing_graph_pause, 0);
5155 atomic_set(&t->trace_overrun, 0);
5156 t->ftrace_timestamp = 0;
5157 /* make curr_ret_stack visible before we add the ret_stack */
5159 t->ret_stack = ret_stack;
5163 * Allocate a return stack for the idle task. May be the first
5164 * time through, or it may be done by CPU hotplug online.
5166 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5168 t->curr_ret_stack = -1;
5170 * The idle task has no parent, it either has its own
5171 * stack or no stack at all.
5174 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5176 if (ftrace_graph_active) {
5177 struct ftrace_ret_stack *ret_stack;
5179 ret_stack = per_cpu(idle_ret_stack, cpu);
5181 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5182 * sizeof(struct ftrace_ret_stack),
5186 per_cpu(idle_ret_stack, cpu) = ret_stack;
5188 graph_init_task(t, ret_stack);
5192 /* Allocate a return stack for newly created task */
5193 void ftrace_graph_init_task(struct task_struct *t)
5195 /* Make sure we do not use the parent ret_stack */
5196 t->ret_stack = NULL;
5197 t->curr_ret_stack = -1;
5199 if (ftrace_graph_active) {
5200 struct ftrace_ret_stack *ret_stack;
5202 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5203 * sizeof(struct ftrace_ret_stack),
5207 graph_init_task(t, ret_stack);
5211 void ftrace_graph_exit_task(struct task_struct *t)
5213 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5215 t->ret_stack = NULL;
5216 /* NULL must become visible to IRQs before we free it: */
5222 void ftrace_graph_stop(void)