2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
36 #include <trace/events/sched.h>
38 #include <asm/setup.h>
40 #include "trace_output.h"
41 #include "trace_stat.h"
43 #define FTRACE_WARN_ON(cond) \
51 #define FTRACE_WARN_ON_ONCE(cond) \
54 if (WARN_ON_ONCE(___r)) \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
71 #define INIT_REGEX_LOCK(opsname)
74 static struct ftrace_ops ftrace_list_end __read_mostly = {
76 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
79 /* ftrace_enabled is a method to turn ftrace on or off */
80 int ftrace_enabled __read_mostly;
81 static int last_ftrace_enabled;
83 /* Quick disabling of function tracer. */
84 int function_trace_stop __read_mostly;
86 /* Current function tracing op */
87 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
88 /* What to set function_trace_op to */
89 static struct ftrace_ops *set_function_trace_op;
91 /* List for set_ftrace_pid's pids. */
92 LIST_HEAD(ftrace_pids);
94 struct list_head list;
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
102 static int ftrace_disabled __read_mostly;
104 static DEFINE_MUTEX(ftrace_lock);
106 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
107 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
108 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
109 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
110 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
111 static struct ftrace_ops global_ops;
112 static struct ftrace_ops control_ops;
114 #if ARCH_SUPPORTS_FTRACE_OPS
115 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
116 struct ftrace_ops *op, struct pt_regs *regs);
118 /* See comment below, where ftrace_ops_list_func is defined */
119 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
120 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
124 * Traverse the ftrace_global_list, invoking all entries. The reason that we
125 * can use rcu_dereference_raw_notrace() is that elements removed from this list
126 * are simply leaked, so there is no need to interact with a grace-period
127 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
128 * concurrent insertions into the ftrace_global_list.
130 * Silly Alpha and silly pointer-speculation compiler optimizations!
132 #define do_for_each_ftrace_op(op, list) \
133 op = rcu_dereference_raw_notrace(list); \
137 * Optimized for just a single item in the list (as that is the normal case).
139 #define while_for_each_ftrace_op(op) \
140 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
141 unlikely((op) != &ftrace_list_end))
143 static inline void ftrace_ops_init(struct ftrace_ops *ops)
145 #ifdef CONFIG_DYNAMIC_FTRACE
146 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
147 mutex_init(&ops->regex_lock);
148 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
154 * ftrace_nr_registered_ops - return number of ops registered
156 * Returns the number of ftrace_ops registered and tracing functions
158 int ftrace_nr_registered_ops(void)
160 struct ftrace_ops *ops;
163 mutex_lock(&ftrace_lock);
165 for (ops = ftrace_ops_list;
166 ops != &ftrace_list_end; ops = ops->next)
169 mutex_unlock(&ftrace_lock);
175 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
176 struct ftrace_ops *op, struct pt_regs *regs)
180 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
184 do_for_each_ftrace_op(op, ftrace_global_list) {
185 op->func(ip, parent_ip, op, regs);
186 } while_for_each_ftrace_op(op);
188 trace_clear_recursion(bit);
191 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
192 struct ftrace_ops *op, struct pt_regs *regs)
194 if (!test_tsk_trace_trace(current))
197 ftrace_pid_function(ip, parent_ip, op, regs);
200 static void set_ftrace_pid_function(ftrace_func_t func)
202 /* do not set ftrace_pid_function to itself! */
203 if (func != ftrace_pid_func)
204 ftrace_pid_function = func;
208 * clear_ftrace_function - reset the ftrace function
210 * This NULLs the ftrace function and in essence stops
211 * tracing. There may be lag
213 void clear_ftrace_function(void)
215 ftrace_trace_function = ftrace_stub;
216 ftrace_pid_function = ftrace_stub;
219 static void control_ops_disable_all(struct ftrace_ops *ops)
223 for_each_possible_cpu(cpu)
224 *per_cpu_ptr(ops->disabled, cpu) = 1;
227 static int control_ops_alloc(struct ftrace_ops *ops)
229 int __percpu *disabled;
231 disabled = alloc_percpu(int);
235 ops->disabled = disabled;
236 control_ops_disable_all(ops);
240 static void control_ops_free(struct ftrace_ops *ops)
242 free_percpu(ops->disabled);
245 static void update_global_ops(void)
250 * If there's only one function registered, then call that
251 * function directly. Otherwise, we need to iterate over the
252 * registered callers.
254 if (ftrace_global_list == &ftrace_list_end ||
255 ftrace_global_list->next == &ftrace_list_end) {
256 func = ftrace_global_list->func;
258 * As we are calling the function directly.
259 * If it does not have recursion protection,
260 * the function_trace_op needs to be updated
263 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
264 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
266 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
268 func = ftrace_global_list_func;
269 /* The list has its own recursion protection. */
270 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
274 /* If we filter on pids, update to use the pid function */
275 if (!list_empty(&ftrace_pids)) {
276 set_ftrace_pid_function(func);
277 func = ftrace_pid_func;
280 global_ops.func = func;
283 static void ftrace_sync(struct work_struct *work)
286 * This function is just a stub to implement a hard force
287 * of synchronize_sched(). This requires synchronizing
288 * tasks even in userspace and idle.
290 * Yes, function tracing is rude.
294 static void ftrace_sync_ipi(void *data)
296 /* Probably not needed, but do it anyway */
300 static void update_ftrace_function(void)
307 * If we are at the end of the list and this ops is
308 * recursion safe and not dynamic and the arch supports passing ops,
309 * then have the mcount trampoline call the function directly.
311 if (ftrace_ops_list == &ftrace_list_end ||
312 (ftrace_ops_list->next == &ftrace_list_end &&
313 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
314 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
315 !FTRACE_FORCE_LIST_FUNC)) {
316 /* Set the ftrace_ops that the arch callback uses */
317 if (ftrace_ops_list == &global_ops)
318 set_function_trace_op = ftrace_global_list;
320 set_function_trace_op = ftrace_ops_list;
321 func = ftrace_ops_list->func;
323 /* Just use the default ftrace_ops */
324 set_function_trace_op = &ftrace_list_end;
325 func = ftrace_ops_list_func;
328 /* If there's no change, then do nothing more here */
329 if (ftrace_trace_function == func)
333 * If we are using the list function, it doesn't care
334 * about the function_trace_ops.
336 if (func == ftrace_ops_list_func) {
337 ftrace_trace_function = func;
339 * Don't even bother setting function_trace_ops,
340 * it would be racy to do so anyway.
345 #ifndef CONFIG_DYNAMIC_FTRACE
347 * For static tracing, we need to be a bit more careful.
348 * The function change takes affect immediately. Thus,
349 * we need to coorditate the setting of the function_trace_ops
350 * with the setting of the ftrace_trace_function.
352 * Set the function to the list ops, which will call the
353 * function we want, albeit indirectly, but it handles the
354 * ftrace_ops and doesn't depend on function_trace_op.
356 ftrace_trace_function = ftrace_ops_list_func;
358 * Make sure all CPUs see this. Yes this is slow, but static
359 * tracing is slow and nasty to have enabled.
361 schedule_on_each_cpu(ftrace_sync);
362 /* Now all cpus are using the list ops. */
363 function_trace_op = set_function_trace_op;
364 /* Make sure the function_trace_op is visible on all CPUs */
366 /* Nasty way to force a rmb on all cpus */
367 smp_call_function(ftrace_sync_ipi, NULL, 1);
368 /* OK, we are all set to update the ftrace_trace_function now! */
369 #endif /* !CONFIG_DYNAMIC_FTRACE */
371 ftrace_trace_function = func;
374 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
378 * We are entering ops into the list but another
379 * CPU might be walking that list. We need to make sure
380 * the ops->next pointer is valid before another CPU sees
381 * the ops pointer included into the list.
383 rcu_assign_pointer(*list, ops);
386 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
388 struct ftrace_ops **p;
391 * If we are removing the last function, then simply point
392 * to the ftrace_stub.
394 if (*list == ops && ops->next == &ftrace_list_end) {
395 *list = &ftrace_list_end;
399 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
410 static void add_ftrace_list_ops(struct ftrace_ops **list,
411 struct ftrace_ops *main_ops,
412 struct ftrace_ops *ops)
414 int first = *list == &ftrace_list_end;
415 add_ftrace_ops(list, ops);
417 add_ftrace_ops(&ftrace_ops_list, main_ops);
420 static int remove_ftrace_list_ops(struct ftrace_ops **list,
421 struct ftrace_ops *main_ops,
422 struct ftrace_ops *ops)
424 int ret = remove_ftrace_ops(list, ops);
425 if (!ret && *list == &ftrace_list_end)
426 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
430 static int __register_ftrace_function(struct ftrace_ops *ops)
432 if (FTRACE_WARN_ON(ops == &global_ops))
435 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
438 /* We don't support both control and global flags set. */
439 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
442 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
444 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
445 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
446 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
448 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
449 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
452 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
453 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
456 if (!core_kernel_data((unsigned long)ops))
457 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
459 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
460 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
461 ops->flags |= FTRACE_OPS_FL_ENABLED;
462 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
463 if (control_ops_alloc(ops))
465 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
467 add_ftrace_ops(&ftrace_ops_list, ops);
470 update_ftrace_function();
475 static int __unregister_ftrace_function(struct ftrace_ops *ops)
479 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
482 if (FTRACE_WARN_ON(ops == &global_ops))
485 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
486 ret = remove_ftrace_list_ops(&ftrace_global_list,
489 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
490 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
491 ret = remove_ftrace_list_ops(&ftrace_control_list,
495 * The ftrace_ops is now removed from the list,
496 * so there'll be no new users. We must ensure
497 * all current users are done before we free
501 control_ops_free(ops);
504 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
510 update_ftrace_function();
513 * Dynamic ops may be freed, we must make sure that all
514 * callers are done before leaving this function.
516 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
522 static void ftrace_update_pid_func(void)
524 /* Only do something if we are tracing something */
525 if (ftrace_trace_function == ftrace_stub)
528 update_ftrace_function();
531 #ifdef CONFIG_FUNCTION_PROFILER
532 struct ftrace_profile {
533 struct hlist_node node;
535 unsigned long counter;
536 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
537 unsigned long long time;
538 unsigned long long time_squared;
542 struct ftrace_profile_page {
543 struct ftrace_profile_page *next;
545 struct ftrace_profile records[];
548 struct ftrace_profile_stat {
550 struct hlist_head *hash;
551 struct ftrace_profile_page *pages;
552 struct ftrace_profile_page *start;
553 struct tracer_stat stat;
556 #define PROFILE_RECORDS_SIZE \
557 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
559 #define PROFILES_PER_PAGE \
560 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
562 static int ftrace_profile_enabled __read_mostly;
564 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
565 static DEFINE_MUTEX(ftrace_profile_lock);
567 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
569 #define FTRACE_PROFILE_HASH_BITS 10
570 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
573 function_stat_next(void *v, int idx)
575 struct ftrace_profile *rec = v;
576 struct ftrace_profile_page *pg;
578 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
584 if ((void *)rec >= (void *)&pg->records[pg->index]) {
588 rec = &pg->records[0];
596 static void *function_stat_start(struct tracer_stat *trace)
598 struct ftrace_profile_stat *stat =
599 container_of(trace, struct ftrace_profile_stat, stat);
601 if (!stat || !stat->start)
604 return function_stat_next(&stat->start->records[0], 0);
607 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
608 /* function graph compares on total time */
609 static int function_stat_cmp(void *p1, void *p2)
611 struct ftrace_profile *a = p1;
612 struct ftrace_profile *b = p2;
614 if (a->time < b->time)
616 if (a->time > b->time)
622 /* not function graph compares against hits */
623 static int function_stat_cmp(void *p1, void *p2)
625 struct ftrace_profile *a = p1;
626 struct ftrace_profile *b = p2;
628 if (a->counter < b->counter)
630 if (a->counter > b->counter)
637 static int function_stat_headers(struct seq_file *m)
639 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
640 seq_printf(m, " Function "
643 "--- ---- --- ---\n");
645 seq_printf(m, " Function Hit\n"
651 static int function_stat_show(struct seq_file *m, void *v)
653 struct ftrace_profile *rec = v;
654 char str[KSYM_SYMBOL_LEN];
656 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
657 static struct trace_seq s;
658 unsigned long long avg;
659 unsigned long long stddev;
661 mutex_lock(&ftrace_profile_lock);
663 /* we raced with function_profile_reset() */
664 if (unlikely(rec->counter == 0)) {
669 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
670 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
672 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
675 do_div(avg, rec->counter);
677 /* Sample standard deviation (s^2) */
678 if (rec->counter <= 1)
681 stddev = rec->time_squared - rec->counter * avg * avg;
683 * Divide only 1000 for ns^2 -> us^2 conversion.
684 * trace_print_graph_duration will divide 1000 again.
686 do_div(stddev, (rec->counter - 1) * 1000);
690 trace_print_graph_duration(rec->time, &s);
691 trace_seq_puts(&s, " ");
692 trace_print_graph_duration(avg, &s);
693 trace_seq_puts(&s, " ");
694 trace_print_graph_duration(stddev, &s);
695 trace_print_seq(m, &s);
699 mutex_unlock(&ftrace_profile_lock);
704 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
706 struct ftrace_profile_page *pg;
708 pg = stat->pages = stat->start;
711 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
716 memset(stat->hash, 0,
717 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
720 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
722 struct ftrace_profile_page *pg;
727 /* If we already allocated, do nothing */
731 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
735 #ifdef CONFIG_DYNAMIC_FTRACE
736 functions = ftrace_update_tot_cnt;
739 * We do not know the number of functions that exist because
740 * dynamic tracing is what counts them. With past experience
741 * we have around 20K functions. That should be more than enough.
742 * It is highly unlikely we will execute every function in
748 pg = stat->start = stat->pages;
750 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
752 for (i = 1; i < pages; i++) {
753 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
764 unsigned long tmp = (unsigned long)pg;
776 static int ftrace_profile_init_cpu(int cpu)
778 struct ftrace_profile_stat *stat;
781 stat = &per_cpu(ftrace_profile_stats, cpu);
784 /* If the profile is already created, simply reset it */
785 ftrace_profile_reset(stat);
790 * We are profiling all functions, but usually only a few thousand
791 * functions are hit. We'll make a hash of 1024 items.
793 size = FTRACE_PROFILE_HASH_SIZE;
795 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
800 /* Preallocate the function profiling pages */
801 if (ftrace_profile_pages_init(stat) < 0) {
810 static int ftrace_profile_init(void)
815 for_each_possible_cpu(cpu) {
816 ret = ftrace_profile_init_cpu(cpu);
824 /* interrupts must be disabled */
825 static struct ftrace_profile *
826 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
828 struct ftrace_profile *rec;
829 struct hlist_head *hhd;
832 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
833 hhd = &stat->hash[key];
835 if (hlist_empty(hhd))
838 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
846 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
847 struct ftrace_profile *rec)
851 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
852 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
856 * The memory is already allocated, this simply finds a new record to use.
858 static struct ftrace_profile *
859 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
861 struct ftrace_profile *rec = NULL;
863 /* prevent recursion (from NMIs) */
864 if (atomic_inc_return(&stat->disabled) != 1)
868 * Try to find the function again since an NMI
869 * could have added it
871 rec = ftrace_find_profiled_func(stat, ip);
875 if (stat->pages->index == PROFILES_PER_PAGE) {
876 if (!stat->pages->next)
878 stat->pages = stat->pages->next;
881 rec = &stat->pages->records[stat->pages->index++];
883 ftrace_add_profile(stat, rec);
886 atomic_dec(&stat->disabled);
892 function_profile_call(unsigned long ip, unsigned long parent_ip,
893 struct ftrace_ops *ops, struct pt_regs *regs)
895 struct ftrace_profile_stat *stat;
896 struct ftrace_profile *rec;
899 if (!ftrace_profile_enabled)
902 local_irq_save(flags);
904 stat = &__get_cpu_var(ftrace_profile_stats);
905 if (!stat->hash || !ftrace_profile_enabled)
908 rec = ftrace_find_profiled_func(stat, ip);
910 rec = ftrace_profile_alloc(stat, ip);
917 local_irq_restore(flags);
920 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
921 static int profile_graph_entry(struct ftrace_graph_ent *trace)
923 function_profile_call(trace->func, 0, NULL, NULL);
927 static void profile_graph_return(struct ftrace_graph_ret *trace)
929 struct ftrace_profile_stat *stat;
930 unsigned long long calltime;
931 struct ftrace_profile *rec;
934 local_irq_save(flags);
935 stat = &__get_cpu_var(ftrace_profile_stats);
936 if (!stat->hash || !ftrace_profile_enabled)
939 /* If the calltime was zero'd ignore it */
940 if (!trace->calltime)
943 calltime = trace->rettime - trace->calltime;
945 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
948 index = trace->depth;
950 /* Append this call time to the parent time to subtract */
952 current->ret_stack[index - 1].subtime += calltime;
954 if (current->ret_stack[index].subtime < calltime)
955 calltime -= current->ret_stack[index].subtime;
960 rec = ftrace_find_profiled_func(stat, trace->func);
962 rec->time += calltime;
963 rec->time_squared += calltime * calltime;
967 local_irq_restore(flags);
970 static int register_ftrace_profiler(void)
972 return register_ftrace_graph(&profile_graph_return,
973 &profile_graph_entry);
976 static void unregister_ftrace_profiler(void)
978 unregister_ftrace_graph();
981 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
982 .func = function_profile_call,
983 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
984 INIT_REGEX_LOCK(ftrace_profile_ops)
987 static int register_ftrace_profiler(void)
989 return register_ftrace_function(&ftrace_profile_ops);
992 static void unregister_ftrace_profiler(void)
994 unregister_ftrace_function(&ftrace_profile_ops);
996 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
999 ftrace_profile_write(struct file *filp, const char __user *ubuf,
1000 size_t cnt, loff_t *ppos)
1005 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1011 mutex_lock(&ftrace_profile_lock);
1012 if (ftrace_profile_enabled ^ val) {
1014 ret = ftrace_profile_init();
1020 ret = register_ftrace_profiler();
1025 ftrace_profile_enabled = 1;
1027 ftrace_profile_enabled = 0;
1029 * unregister_ftrace_profiler calls stop_machine
1030 * so this acts like an synchronize_sched.
1032 unregister_ftrace_profiler();
1036 mutex_unlock(&ftrace_profile_lock);
1044 ftrace_profile_read(struct file *filp, char __user *ubuf,
1045 size_t cnt, loff_t *ppos)
1047 char buf[64]; /* big enough to hold a number */
1050 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1051 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1054 static const struct file_operations ftrace_profile_fops = {
1055 .open = tracing_open_generic,
1056 .read = ftrace_profile_read,
1057 .write = ftrace_profile_write,
1058 .llseek = default_llseek,
1061 /* used to initialize the real stat files */
1062 static struct tracer_stat function_stats __initdata = {
1063 .name = "functions",
1064 .stat_start = function_stat_start,
1065 .stat_next = function_stat_next,
1066 .stat_cmp = function_stat_cmp,
1067 .stat_headers = function_stat_headers,
1068 .stat_show = function_stat_show
1071 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1073 struct ftrace_profile_stat *stat;
1074 struct dentry *entry;
1079 for_each_possible_cpu(cpu) {
1080 stat = &per_cpu(ftrace_profile_stats, cpu);
1082 /* allocate enough for function name + cpu number */
1083 name = kmalloc(32, GFP_KERNEL);
1086 * The files created are permanent, if something happens
1087 * we still do not free memory.
1090 "Could not allocate stat file for cpu %d\n",
1094 stat->stat = function_stats;
1095 snprintf(name, 32, "function%d", cpu);
1096 stat->stat.name = name;
1097 ret = register_stat_tracer(&stat->stat);
1100 "Could not register function stat for cpu %d\n",
1107 entry = debugfs_create_file("function_profile_enabled", 0644,
1108 d_tracer, NULL, &ftrace_profile_fops);
1110 pr_warning("Could not create debugfs "
1111 "'function_profile_enabled' entry\n");
1114 #else /* CONFIG_FUNCTION_PROFILER */
1115 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1118 #endif /* CONFIG_FUNCTION_PROFILER */
1120 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1123 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1127 if (file->f_mode & FMODE_READ)
1128 ret = seq_lseek(file, offset, whence);
1130 file->f_pos = ret = 1;
1135 #ifdef CONFIG_DYNAMIC_FTRACE
1137 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1138 # error Dynamic ftrace depends on MCOUNT_RECORD
1141 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1143 struct ftrace_func_probe {
1144 struct hlist_node node;
1145 struct ftrace_probe_ops *ops;
1146 unsigned long flags;
1149 struct list_head free_list;
1152 struct ftrace_func_entry {
1153 struct hlist_node hlist;
1157 struct ftrace_hash {
1158 unsigned long size_bits;
1159 struct hlist_head *buckets;
1160 unsigned long count;
1161 struct rcu_head rcu;
1165 * We make these constant because no one should touch them,
1166 * but they are used as the default "empty hash", to avoid allocating
1167 * it all the time. These are in a read only section such that if
1168 * anyone does try to modify it, it will cause an exception.
1170 static const struct hlist_head empty_buckets[1];
1171 static const struct ftrace_hash empty_hash = {
1172 .buckets = (struct hlist_head *)empty_buckets,
1174 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1176 static struct ftrace_ops global_ops = {
1177 .func = ftrace_stub,
1178 .notrace_hash = EMPTY_HASH,
1179 .filter_hash = EMPTY_HASH,
1180 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1181 INIT_REGEX_LOCK(global_ops)
1184 struct ftrace_page {
1185 struct ftrace_page *next;
1186 struct dyn_ftrace *records;
1191 static struct ftrace_page *ftrace_new_pgs;
1193 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1194 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1196 /* estimate from running different kernels */
1197 #define NR_TO_INIT 10000
1199 static struct ftrace_page *ftrace_pages_start;
1200 static struct ftrace_page *ftrace_pages;
1202 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1204 return !hash || !hash->count;
1207 static struct ftrace_func_entry *
1208 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1211 struct ftrace_func_entry *entry;
1212 struct hlist_head *hhd;
1214 if (ftrace_hash_empty(hash))
1217 if (hash->size_bits > 0)
1218 key = hash_long(ip, hash->size_bits);
1222 hhd = &hash->buckets[key];
1224 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1225 if (entry->ip == ip)
1231 static void __add_hash_entry(struct ftrace_hash *hash,
1232 struct ftrace_func_entry *entry)
1234 struct hlist_head *hhd;
1237 if (hash->size_bits)
1238 key = hash_long(entry->ip, hash->size_bits);
1242 hhd = &hash->buckets[key];
1243 hlist_add_head(&entry->hlist, hhd);
1247 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1249 struct ftrace_func_entry *entry;
1251 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1256 __add_hash_entry(hash, entry);
1262 free_hash_entry(struct ftrace_hash *hash,
1263 struct ftrace_func_entry *entry)
1265 hlist_del(&entry->hlist);
1271 remove_hash_entry(struct ftrace_hash *hash,
1272 struct ftrace_func_entry *entry)
1274 hlist_del(&entry->hlist);
1278 static void ftrace_hash_clear(struct ftrace_hash *hash)
1280 struct hlist_head *hhd;
1281 struct hlist_node *tn;
1282 struct ftrace_func_entry *entry;
1283 int size = 1 << hash->size_bits;
1289 for (i = 0; i < size; i++) {
1290 hhd = &hash->buckets[i];
1291 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1292 free_hash_entry(hash, entry);
1294 FTRACE_WARN_ON(hash->count);
1297 static void free_ftrace_hash(struct ftrace_hash *hash)
1299 if (!hash || hash == EMPTY_HASH)
1301 ftrace_hash_clear(hash);
1302 kfree(hash->buckets);
1306 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1308 struct ftrace_hash *hash;
1310 hash = container_of(rcu, struct ftrace_hash, rcu);
1311 free_ftrace_hash(hash);
1314 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1316 if (!hash || hash == EMPTY_HASH)
1318 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1321 void ftrace_free_filter(struct ftrace_ops *ops)
1323 ftrace_ops_init(ops);
1324 free_ftrace_hash(ops->filter_hash);
1325 free_ftrace_hash(ops->notrace_hash);
1328 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1330 struct ftrace_hash *hash;
1333 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1337 size = 1 << size_bits;
1338 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1340 if (!hash->buckets) {
1345 hash->size_bits = size_bits;
1350 static struct ftrace_hash *
1351 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1353 struct ftrace_func_entry *entry;
1354 struct ftrace_hash *new_hash;
1359 new_hash = alloc_ftrace_hash(size_bits);
1364 if (ftrace_hash_empty(hash))
1367 size = 1 << hash->size_bits;
1368 for (i = 0; i < size; i++) {
1369 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1370 ret = add_hash_entry(new_hash, entry->ip);
1376 FTRACE_WARN_ON(new_hash->count != hash->count);
1381 free_ftrace_hash(new_hash);
1386 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1388 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1391 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1392 struct ftrace_hash **dst, struct ftrace_hash *src)
1394 struct ftrace_func_entry *entry;
1395 struct hlist_node *tn;
1396 struct hlist_head *hhd;
1397 struct ftrace_hash *old_hash;
1398 struct ftrace_hash *new_hash;
1399 int size = src->count;
1405 * Remove the current set, update the hash and add
1408 ftrace_hash_rec_disable(ops, enable);
1411 * If the new source is empty, just free dst and assign it
1415 free_ftrace_hash_rcu(*dst);
1416 rcu_assign_pointer(*dst, EMPTY_HASH);
1417 /* still need to update the function records */
1423 * Make the hash size about 1/2 the # found
1425 for (size /= 2; size; size >>= 1)
1428 /* Don't allocate too much */
1429 if (bits > FTRACE_HASH_MAX_BITS)
1430 bits = FTRACE_HASH_MAX_BITS;
1433 new_hash = alloc_ftrace_hash(bits);
1437 size = 1 << src->size_bits;
1438 for (i = 0; i < size; i++) {
1439 hhd = &src->buckets[i];
1440 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1441 remove_hash_entry(src, entry);
1442 __add_hash_entry(new_hash, entry);
1447 rcu_assign_pointer(*dst, new_hash);
1448 free_ftrace_hash_rcu(old_hash);
1453 * Enable regardless of ret:
1454 * On success, we enable the new hash.
1455 * On failure, we re-enable the original hash.
1457 ftrace_hash_rec_enable(ops, enable);
1463 * Test the hashes for this ops to see if we want to call
1464 * the ops->func or not.
1466 * It's a match if the ip is in the ops->filter_hash or
1467 * the filter_hash does not exist or is empty,
1469 * the ip is not in the ops->notrace_hash.
1471 * This needs to be called with preemption disabled as
1472 * the hashes are freed with call_rcu_sched().
1475 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1477 struct ftrace_hash *filter_hash;
1478 struct ftrace_hash *notrace_hash;
1481 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1483 * There's a small race when adding ops that the ftrace handler
1484 * that wants regs, may be called without them. We can not
1485 * allow that handler to be called if regs is NULL.
1487 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1491 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1492 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1494 if ((ftrace_hash_empty(filter_hash) ||
1495 ftrace_lookup_ip(filter_hash, ip)) &&
1496 (ftrace_hash_empty(notrace_hash) ||
1497 !ftrace_lookup_ip(notrace_hash, ip)))
1506 * This is a double for. Do not use 'break' to break out of the loop,
1507 * you must use a goto.
1509 #define do_for_each_ftrace_rec(pg, rec) \
1510 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1512 for (_____i = 0; _____i < pg->index; _____i++) { \
1513 rec = &pg->records[_____i];
1515 #define while_for_each_ftrace_rec() \
1520 static int ftrace_cmp_recs(const void *a, const void *b)
1522 const struct dyn_ftrace *key = a;
1523 const struct dyn_ftrace *rec = b;
1525 if (key->flags < rec->ip)
1527 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1532 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1534 struct ftrace_page *pg;
1535 struct dyn_ftrace *rec;
1536 struct dyn_ftrace key;
1539 key.flags = end; /* overload flags, as it is unsigned long */
1541 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1542 if (end < pg->records[0].ip ||
1543 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1545 rec = bsearch(&key, pg->records, pg->index,
1546 sizeof(struct dyn_ftrace),
1556 * ftrace_location - return true if the ip giving is a traced location
1557 * @ip: the instruction pointer to check
1559 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1560 * That is, the instruction that is either a NOP or call to
1561 * the function tracer. It checks the ftrace internal tables to
1562 * determine if the address belongs or not.
1564 unsigned long ftrace_location(unsigned long ip)
1566 return ftrace_location_range(ip, ip);
1570 * ftrace_text_reserved - return true if range contains an ftrace location
1571 * @start: start of range to search
1572 * @end: end of range to search (inclusive). @end points to the last byte to check.
1574 * Returns 1 if @start and @end contains a ftrace location.
1575 * That is, the instruction that is either a NOP or call to
1576 * the function tracer. It checks the ftrace internal tables to
1577 * determine if the address belongs or not.
1579 int ftrace_text_reserved(void *start, void *end)
1583 ret = ftrace_location_range((unsigned long)start,
1584 (unsigned long)end);
1589 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1593 struct ftrace_hash *hash;
1594 struct ftrace_hash *other_hash;
1595 struct ftrace_page *pg;
1596 struct dyn_ftrace *rec;
1600 /* Only update if the ops has been registered */
1601 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1605 * In the filter_hash case:
1606 * If the count is zero, we update all records.
1607 * Otherwise we just update the items in the hash.
1609 * In the notrace_hash case:
1610 * We enable the update in the hash.
1611 * As disabling notrace means enabling the tracing,
1612 * and enabling notrace means disabling, the inc variable
1616 hash = ops->filter_hash;
1617 other_hash = ops->notrace_hash;
1618 if (ftrace_hash_empty(hash))
1622 hash = ops->notrace_hash;
1623 other_hash = ops->filter_hash;
1625 * If the notrace hash has no items,
1626 * then there's nothing to do.
1628 if (ftrace_hash_empty(hash))
1632 do_for_each_ftrace_rec(pg, rec) {
1633 int in_other_hash = 0;
1639 * Only the filter_hash affects all records.
1640 * Update if the record is not in the notrace hash.
1642 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1645 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1646 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1651 if (filter_hash && in_hash && !in_other_hash)
1653 else if (!filter_hash && in_hash &&
1654 (in_other_hash || ftrace_hash_empty(other_hash)))
1662 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1665 * If any ops wants regs saved for this function
1666 * then all ops will get saved regs.
1668 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1669 rec->flags |= FTRACE_FL_REGS;
1671 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1676 /* Shortcut, if we handled all records, we are done. */
1677 if (!all && count == hash->count)
1679 } while_for_each_ftrace_rec();
1682 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1685 __ftrace_hash_rec_update(ops, filter_hash, 0);
1688 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1691 __ftrace_hash_rec_update(ops, filter_hash, 1);
1694 static void print_ip_ins(const char *fmt, unsigned char *p)
1698 printk(KERN_CONT "%s", fmt);
1700 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1701 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1705 * ftrace_bug - report and shutdown function tracer
1706 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1707 * @ip: The address that failed
1709 * The arch code that enables or disables the function tracing
1710 * can call ftrace_bug() when it has detected a problem in
1711 * modifying the code. @failed should be one of either:
1712 * EFAULT - if the problem happens on reading the @ip address
1713 * EINVAL - if what is read at @ip is not what was expected
1714 * EPERM - if the problem happens on writting to the @ip address
1716 void ftrace_bug(int failed, unsigned long ip)
1720 FTRACE_WARN_ON_ONCE(1);
1721 pr_info("ftrace faulted on modifying ");
1725 FTRACE_WARN_ON_ONCE(1);
1726 pr_info("ftrace failed to modify ");
1728 print_ip_ins(" actual: ", (unsigned char *)ip);
1729 printk(KERN_CONT "\n");
1732 FTRACE_WARN_ON_ONCE(1);
1733 pr_info("ftrace faulted on writing ");
1737 FTRACE_WARN_ON_ONCE(1);
1738 pr_info("ftrace faulted on unknown error ");
1743 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1745 unsigned long flag = 0UL;
1748 * If we are updating calls:
1750 * If the record has a ref count, then we need to enable it
1751 * because someone is using it.
1753 * Otherwise we make sure its disabled.
1755 * If we are disabling calls, then disable all records that
1758 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1759 flag = FTRACE_FL_ENABLED;
1762 * If enabling and the REGS flag does not match the REGS_EN, then
1763 * do not ignore this record. Set flags to fail the compare against
1767 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1768 flag |= FTRACE_FL_REGS;
1770 /* If the state of this record hasn't changed, then do nothing */
1771 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1772 return FTRACE_UPDATE_IGNORE;
1775 /* Save off if rec is being enabled (for return value) */
1776 flag ^= rec->flags & FTRACE_FL_ENABLED;
1779 rec->flags |= FTRACE_FL_ENABLED;
1780 if (flag & FTRACE_FL_REGS) {
1781 if (rec->flags & FTRACE_FL_REGS)
1782 rec->flags |= FTRACE_FL_REGS_EN;
1784 rec->flags &= ~FTRACE_FL_REGS_EN;
1789 * If this record is being updated from a nop, then
1790 * return UPDATE_MAKE_CALL.
1791 * Otherwise, if the EN flag is set, then return
1792 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1793 * from the non-save regs, to a save regs function.
1795 * return UPDATE_MODIFY_CALL to tell the caller to convert
1796 * from the save regs, to a non-save regs function.
1798 if (flag & FTRACE_FL_ENABLED)
1799 return FTRACE_UPDATE_MAKE_CALL;
1800 else if (rec->flags & FTRACE_FL_REGS_EN)
1801 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1803 return FTRACE_UPDATE_MODIFY_CALL;
1807 /* If there's no more users, clear all flags */
1808 if (!(rec->flags & ~FTRACE_FL_MASK))
1811 /* Just disable the record (keep REGS state) */
1812 rec->flags &= ~FTRACE_FL_ENABLED;
1815 return FTRACE_UPDATE_MAKE_NOP;
1819 * ftrace_update_record, set a record that now is tracing or not
1820 * @rec: the record to update
1821 * @enable: set to 1 if the record is tracing, zero to force disable
1823 * The records that represent all functions that can be traced need
1824 * to be updated when tracing has been enabled.
1826 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1828 return ftrace_check_record(rec, enable, 1);
1832 * ftrace_test_record, check if the record has been enabled or not
1833 * @rec: the record to test
1834 * @enable: set to 1 to check if enabled, 0 if it is disabled
1836 * The arch code may need to test if a record is already set to
1837 * tracing to determine how to modify the function code that it
1840 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1842 return ftrace_check_record(rec, enable, 0);
1846 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1848 unsigned long ftrace_old_addr;
1849 unsigned long ftrace_addr;
1852 ret = ftrace_update_record(rec, enable);
1854 if (rec->flags & FTRACE_FL_REGS)
1855 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1857 ftrace_addr = (unsigned long)FTRACE_ADDR;
1860 case FTRACE_UPDATE_IGNORE:
1863 case FTRACE_UPDATE_MAKE_CALL:
1864 return ftrace_make_call(rec, ftrace_addr);
1866 case FTRACE_UPDATE_MAKE_NOP:
1867 return ftrace_make_nop(NULL, rec, ftrace_addr);
1869 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1870 case FTRACE_UPDATE_MODIFY_CALL:
1871 if (rec->flags & FTRACE_FL_REGS)
1872 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1874 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1876 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1879 return -1; /* unknow ftrace bug */
1882 void __weak ftrace_replace_code(int enable)
1884 struct dyn_ftrace *rec;
1885 struct ftrace_page *pg;
1888 if (unlikely(ftrace_disabled))
1891 do_for_each_ftrace_rec(pg, rec) {
1892 failed = __ftrace_replace_code(rec, enable);
1894 ftrace_bug(failed, rec->ip);
1895 /* Stop processing */
1898 } while_for_each_ftrace_rec();
1901 struct ftrace_rec_iter {
1902 struct ftrace_page *pg;
1907 * ftrace_rec_iter_start, start up iterating over traced functions
1909 * Returns an iterator handle that is used to iterate over all
1910 * the records that represent address locations where functions
1913 * May return NULL if no records are available.
1915 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1918 * We only use a single iterator.
1919 * Protected by the ftrace_lock mutex.
1921 static struct ftrace_rec_iter ftrace_rec_iter;
1922 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1924 iter->pg = ftrace_pages_start;
1927 /* Could have empty pages */
1928 while (iter->pg && !iter->pg->index)
1929 iter->pg = iter->pg->next;
1938 * ftrace_rec_iter_next, get the next record to process.
1939 * @iter: The handle to the iterator.
1941 * Returns the next iterator after the given iterator @iter.
1943 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1947 if (iter->index >= iter->pg->index) {
1948 iter->pg = iter->pg->next;
1951 /* Could have empty pages */
1952 while (iter->pg && !iter->pg->index)
1953 iter->pg = iter->pg->next;
1963 * ftrace_rec_iter_record, get the record at the iterator location
1964 * @iter: The current iterator location
1966 * Returns the record that the current @iter is at.
1968 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1970 return &iter->pg->records[iter->index];
1974 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1981 if (unlikely(ftrace_disabled))
1984 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1986 ftrace_bug(ret, ip);
1993 * archs can override this function if they must do something
1994 * before the modifying code is performed.
1996 int __weak ftrace_arch_code_modify_prepare(void)
2002 * archs can override this function if they must do something
2003 * after the modifying code is performed.
2005 int __weak ftrace_arch_code_modify_post_process(void)
2010 void ftrace_modify_all_code(int command)
2012 if (command & FTRACE_UPDATE_CALLS)
2013 ftrace_replace_code(1);
2014 else if (command & FTRACE_DISABLE_CALLS)
2015 ftrace_replace_code(0);
2017 if (command & FTRACE_UPDATE_TRACE_FUNC) {
2018 function_trace_op = set_function_trace_op;
2020 /* If irqs are disabled, we are in stop machine */
2021 if (!irqs_disabled())
2022 smp_call_function(ftrace_sync_ipi, NULL, 1);
2023 ftrace_update_ftrace_func(ftrace_trace_function);
2026 if (command & FTRACE_START_FUNC_RET)
2027 ftrace_enable_ftrace_graph_caller();
2028 else if (command & FTRACE_STOP_FUNC_RET)
2029 ftrace_disable_ftrace_graph_caller();
2032 static int __ftrace_modify_code(void *data)
2034 int *command = data;
2036 ftrace_modify_all_code(*command);
2042 * ftrace_run_stop_machine, go back to the stop machine method
2043 * @command: The command to tell ftrace what to do
2045 * If an arch needs to fall back to the stop machine method, the
2046 * it can call this function.
2048 void ftrace_run_stop_machine(int command)
2050 stop_machine(__ftrace_modify_code, &command, NULL);
2054 * arch_ftrace_update_code, modify the code to trace or not trace
2055 * @command: The command that needs to be done
2057 * Archs can override this function if it does not need to
2058 * run stop_machine() to modify code.
2060 void __weak arch_ftrace_update_code(int command)
2062 ftrace_run_stop_machine(command);
2065 static void ftrace_run_update_code(int command)
2069 ret = ftrace_arch_code_modify_prepare();
2070 FTRACE_WARN_ON(ret);
2074 * Do not call function tracer while we update the code.
2075 * We are in stop machine.
2077 function_trace_stop++;
2080 * By default we use stop_machine() to modify the code.
2081 * But archs can do what ever they want as long as it
2082 * is safe. The stop_machine() is the safest, but also
2083 * produces the most overhead.
2085 arch_ftrace_update_code(command);
2087 function_trace_stop--;
2089 ret = ftrace_arch_code_modify_post_process();
2090 FTRACE_WARN_ON(ret);
2093 static ftrace_func_t saved_ftrace_func;
2094 static int ftrace_start_up;
2095 static int global_start_up;
2097 static void ftrace_startup_enable(int command)
2099 if (saved_ftrace_func != ftrace_trace_function) {
2100 saved_ftrace_func = ftrace_trace_function;
2101 command |= FTRACE_UPDATE_TRACE_FUNC;
2104 if (!command || !ftrace_enabled)
2107 ftrace_run_update_code(command);
2110 static int ftrace_startup(struct ftrace_ops *ops, int command)
2112 bool hash_enable = true;
2115 if (unlikely(ftrace_disabled))
2118 ret = __register_ftrace_function(ops);
2123 command |= FTRACE_UPDATE_CALLS;
2125 /* ops marked global share the filter hashes */
2126 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2128 /* Don't update hash if global is already set */
2129 if (global_start_up)
2130 hash_enable = false;
2134 ops->flags |= FTRACE_OPS_FL_ENABLED;
2136 ftrace_hash_rec_enable(ops, 1);
2138 ftrace_startup_enable(command);
2143 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2145 bool hash_disable = true;
2148 if (unlikely(ftrace_disabled))
2151 ret = __unregister_ftrace_function(ops);
2157 * Just warn in case of unbalance, no need to kill ftrace, it's not
2158 * critical but the ftrace_call callers may be never nopped again after
2159 * further ftrace uses.
2161 WARN_ON_ONCE(ftrace_start_up < 0);
2163 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2166 WARN_ON_ONCE(global_start_up < 0);
2167 /* Don't update hash if global still has users */
2168 if (global_start_up) {
2169 WARN_ON_ONCE(!ftrace_start_up);
2170 hash_disable = false;
2175 ftrace_hash_rec_disable(ops, 1);
2177 if (ops != &global_ops || !global_start_up)
2178 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2180 command |= FTRACE_UPDATE_CALLS;
2182 if (saved_ftrace_func != ftrace_trace_function) {
2183 saved_ftrace_func = ftrace_trace_function;
2184 command |= FTRACE_UPDATE_TRACE_FUNC;
2187 if (!command || !ftrace_enabled)
2190 ftrace_run_update_code(command);
2194 static void ftrace_startup_sysctl(void)
2196 if (unlikely(ftrace_disabled))
2199 /* Force update next time */
2200 saved_ftrace_func = NULL;
2201 /* ftrace_start_up is true if we want ftrace running */
2202 if (ftrace_start_up)
2203 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2206 static void ftrace_shutdown_sysctl(void)
2208 if (unlikely(ftrace_disabled))
2211 /* ftrace_start_up is true if ftrace is running */
2212 if (ftrace_start_up)
2213 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2216 static cycle_t ftrace_update_time;
2217 static unsigned long ftrace_update_cnt;
2218 unsigned long ftrace_update_tot_cnt;
2220 static inline int ops_traces_mod(struct ftrace_ops *ops)
2223 * Filter_hash being empty will default to trace module.
2224 * But notrace hash requires a test of individual module functions.
2226 return ftrace_hash_empty(ops->filter_hash) &&
2227 ftrace_hash_empty(ops->notrace_hash);
2231 * Check if the current ops references the record.
2233 * If the ops traces all functions, then it was already accounted for.
2234 * If the ops does not trace the current record function, skip it.
2235 * If the ops ignores the function via notrace filter, skip it.
2238 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2240 /* If ops isn't enabled, ignore it */
2241 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2244 /* If ops traces all mods, we already accounted for it */
2245 if (ops_traces_mod(ops))
2248 /* The function must be in the filter */
2249 if (!ftrace_hash_empty(ops->filter_hash) &&
2250 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2253 /* If in notrace hash, we ignore it too */
2254 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2260 static int referenced_filters(struct dyn_ftrace *rec)
2262 struct ftrace_ops *ops;
2265 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2266 if (ops_references_rec(ops, rec))
2273 static int ftrace_update_code(struct module *mod)
2275 struct ftrace_page *pg;
2276 struct dyn_ftrace *p;
2277 cycle_t start, stop;
2278 unsigned long ref = 0;
2283 * When adding a module, we need to check if tracers are
2284 * currently enabled and if they are set to trace all functions.
2285 * If they are, we need to enable the module functions as well
2286 * as update the reference counts for those function records.
2289 struct ftrace_ops *ops;
2291 for (ops = ftrace_ops_list;
2292 ops != &ftrace_list_end; ops = ops->next) {
2293 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2294 if (ops_traces_mod(ops))
2302 start = ftrace_now(raw_smp_processor_id());
2303 ftrace_update_cnt = 0;
2305 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2307 for (i = 0; i < pg->index; i++) {
2310 /* If something went wrong, bail without enabling anything */
2311 if (unlikely(ftrace_disabled))
2314 p = &pg->records[i];
2316 cnt += referenced_filters(p);
2320 * Do the initial record conversion from mcount jump
2321 * to the NOP instructions.
2323 if (!ftrace_code_disable(mod, p))
2326 ftrace_update_cnt++;
2329 * If the tracing is enabled, go ahead and enable the record.
2331 * The reason not to enable the record immediatelly is the
2332 * inherent check of ftrace_make_nop/ftrace_make_call for
2333 * correct previous instructions. Making first the NOP
2334 * conversion puts the module to the correct state, thus
2335 * passing the ftrace_make_call check.
2337 if (ftrace_start_up && cnt) {
2338 int failed = __ftrace_replace_code(p, 1);
2340 ftrace_bug(failed, p->ip);
2345 ftrace_new_pgs = NULL;
2347 stop = ftrace_now(raw_smp_processor_id());
2348 ftrace_update_time = stop - start;
2349 ftrace_update_tot_cnt += ftrace_update_cnt;
2354 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2359 if (WARN_ON(!count))
2362 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2365 * We want to fill as much as possible. No more than a page
2368 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2372 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2375 /* if we can't allocate this size, try something smaller */
2382 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2391 static struct ftrace_page *
2392 ftrace_allocate_pages(unsigned long num_to_init)
2394 struct ftrace_page *start_pg;
2395 struct ftrace_page *pg;
2402 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2407 * Try to allocate as much as possible in one continues
2408 * location that fills in all of the space. We want to
2409 * waste as little space as possible.
2412 cnt = ftrace_allocate_records(pg, num_to_init);
2420 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2431 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2432 free_pages((unsigned long)pg->records, order);
2433 start_pg = pg->next;
2437 pr_info("ftrace: FAILED to allocate memory for functions\n");
2441 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2446 pr_info("ftrace: No functions to be traced?\n");
2450 cnt = num_to_init / ENTRIES_PER_PAGE;
2451 pr_info("ftrace: allocating %ld entries in %d pages\n",
2452 num_to_init, cnt + 1);
2457 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2459 struct ftrace_iterator {
2462 struct ftrace_page *pg;
2463 struct dyn_ftrace *func;
2464 struct ftrace_func_probe *probe;
2465 struct trace_parser parser;
2466 struct ftrace_hash *hash;
2467 struct ftrace_ops *ops;
2474 t_hash_next(struct seq_file *m, loff_t *pos)
2476 struct ftrace_iterator *iter = m->private;
2477 struct hlist_node *hnd = NULL;
2478 struct hlist_head *hhd;
2484 hnd = &iter->probe->node;
2486 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2489 hhd = &ftrace_func_hash[iter->hidx];
2491 if (hlist_empty(hhd)) {
2507 if (WARN_ON_ONCE(!hnd))
2510 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2515 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2517 struct ftrace_iterator *iter = m->private;
2521 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2524 if (iter->func_pos > *pos)
2528 for (l = 0; l <= (*pos - iter->func_pos); ) {
2529 p = t_hash_next(m, &l);
2536 /* Only set this if we have an item */
2537 iter->flags |= FTRACE_ITER_HASH;
2543 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2545 struct ftrace_func_probe *rec;
2548 if (WARN_ON_ONCE(!rec))
2551 if (rec->ops->print)
2552 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2554 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2557 seq_printf(m, ":%p", rec->data);
2564 t_next(struct seq_file *m, void *v, loff_t *pos)
2566 struct ftrace_iterator *iter = m->private;
2567 struct ftrace_ops *ops = iter->ops;
2568 struct dyn_ftrace *rec = NULL;
2570 if (unlikely(ftrace_disabled))
2573 if (iter->flags & FTRACE_ITER_HASH)
2574 return t_hash_next(m, pos);
2577 iter->pos = iter->func_pos = *pos;
2579 if (iter->flags & FTRACE_ITER_PRINTALL)
2580 return t_hash_start(m, pos);
2583 if (iter->idx >= iter->pg->index) {
2584 if (iter->pg->next) {
2585 iter->pg = iter->pg->next;
2590 rec = &iter->pg->records[iter->idx++];
2591 if (((iter->flags & FTRACE_ITER_FILTER) &&
2592 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2594 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2595 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2597 ((iter->flags & FTRACE_ITER_ENABLED) &&
2598 !(rec->flags & FTRACE_FL_ENABLED))) {
2606 return t_hash_start(m, pos);
2613 static void reset_iter_read(struct ftrace_iterator *iter)
2617 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2620 static void *t_start(struct seq_file *m, loff_t *pos)
2622 struct ftrace_iterator *iter = m->private;
2623 struct ftrace_ops *ops = iter->ops;
2627 mutex_lock(&ftrace_lock);
2629 if (unlikely(ftrace_disabled))
2633 * If an lseek was done, then reset and start from beginning.
2635 if (*pos < iter->pos)
2636 reset_iter_read(iter);
2639 * For set_ftrace_filter reading, if we have the filter
2640 * off, we can short cut and just print out that all
2641 * functions are enabled.
2643 if (iter->flags & FTRACE_ITER_FILTER &&
2644 ftrace_hash_empty(ops->filter_hash)) {
2646 return t_hash_start(m, pos);
2647 iter->flags |= FTRACE_ITER_PRINTALL;
2648 /* reset in case of seek/pread */
2649 iter->flags &= ~FTRACE_ITER_HASH;
2653 if (iter->flags & FTRACE_ITER_HASH)
2654 return t_hash_start(m, pos);
2657 * Unfortunately, we need to restart at ftrace_pages_start
2658 * every time we let go of the ftrace_mutex. This is because
2659 * those pointers can change without the lock.
2661 iter->pg = ftrace_pages_start;
2663 for (l = 0; l <= *pos; ) {
2664 p = t_next(m, p, &l);
2670 return t_hash_start(m, pos);
2675 static void t_stop(struct seq_file *m, void *p)
2677 mutex_unlock(&ftrace_lock);
2680 static int t_show(struct seq_file *m, void *v)
2682 struct ftrace_iterator *iter = m->private;
2683 struct dyn_ftrace *rec;
2685 if (iter->flags & FTRACE_ITER_HASH)
2686 return t_hash_show(m, iter);
2688 if (iter->flags & FTRACE_ITER_PRINTALL) {
2689 seq_printf(m, "#### all functions enabled ####\n");
2698 seq_printf(m, "%ps", (void *)rec->ip);
2699 if (iter->flags & FTRACE_ITER_ENABLED)
2700 seq_printf(m, " (%ld)%s",
2701 rec->flags & ~FTRACE_FL_MASK,
2702 rec->flags & FTRACE_FL_REGS ? " R" : "");
2703 seq_printf(m, "\n");
2708 static const struct seq_operations show_ftrace_seq_ops = {
2716 ftrace_avail_open(struct inode *inode, struct file *file)
2718 struct ftrace_iterator *iter;
2720 if (unlikely(ftrace_disabled))
2723 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2725 iter->pg = ftrace_pages_start;
2726 iter->ops = &global_ops;
2729 return iter ? 0 : -ENOMEM;
2733 ftrace_enabled_open(struct inode *inode, struct file *file)
2735 struct ftrace_iterator *iter;
2737 if (unlikely(ftrace_disabled))
2740 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2742 iter->pg = ftrace_pages_start;
2743 iter->flags = FTRACE_ITER_ENABLED;
2744 iter->ops = &global_ops;
2747 return iter ? 0 : -ENOMEM;
2750 static void ftrace_filter_reset(struct ftrace_hash *hash)
2752 mutex_lock(&ftrace_lock);
2753 ftrace_hash_clear(hash);
2754 mutex_unlock(&ftrace_lock);
2758 * ftrace_regex_open - initialize function tracer filter files
2759 * @ops: The ftrace_ops that hold the hash filters
2760 * @flag: The type of filter to process
2761 * @inode: The inode, usually passed in to your open routine
2762 * @file: The file, usually passed in to your open routine
2764 * ftrace_regex_open() initializes the filter files for the
2765 * @ops. Depending on @flag it may process the filter hash or
2766 * the notrace hash of @ops. With this called from the open
2767 * routine, you can use ftrace_filter_write() for the write
2768 * routine if @flag has FTRACE_ITER_FILTER set, or
2769 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2770 * ftrace_filter_lseek() should be used as the lseek routine, and
2771 * release must call ftrace_regex_release().
2774 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2775 struct inode *inode, struct file *file)
2777 struct ftrace_iterator *iter;
2778 struct ftrace_hash *hash;
2781 ftrace_ops_init(ops);
2783 if (unlikely(ftrace_disabled))
2786 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2790 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2798 mutex_lock(&ops->regex_lock);
2800 if (flag & FTRACE_ITER_NOTRACE)
2801 hash = ops->notrace_hash;
2803 hash = ops->filter_hash;
2805 if (file->f_mode & FMODE_WRITE) {
2806 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2808 trace_parser_put(&iter->parser);
2815 if ((file->f_mode & FMODE_WRITE) &&
2816 (file->f_flags & O_TRUNC))
2817 ftrace_filter_reset(iter->hash);
2819 if (file->f_mode & FMODE_READ) {
2820 iter->pg = ftrace_pages_start;
2822 ret = seq_open(file, &show_ftrace_seq_ops);
2824 struct seq_file *m = file->private_data;
2828 free_ftrace_hash(iter->hash);
2829 trace_parser_put(&iter->parser);
2833 file->private_data = iter;
2836 mutex_unlock(&ops->regex_lock);
2842 ftrace_filter_open(struct inode *inode, struct file *file)
2844 return ftrace_regex_open(&global_ops,
2845 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2850 ftrace_notrace_open(struct inode *inode, struct file *file)
2852 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2856 static int ftrace_match(char *str, char *regex, int len, int type)
2863 if (strcmp(str, regex) == 0)
2866 case MATCH_FRONT_ONLY:
2867 if (strncmp(str, regex, len) == 0)
2870 case MATCH_MIDDLE_ONLY:
2871 if (strstr(str, regex))
2874 case MATCH_END_ONLY:
2876 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2885 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2887 struct ftrace_func_entry *entry;
2890 entry = ftrace_lookup_ip(hash, rec->ip);
2892 /* Do nothing if it doesn't exist */
2896 free_hash_entry(hash, entry);
2898 /* Do nothing if it exists */
2902 ret = add_hash_entry(hash, rec->ip);
2908 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2909 char *regex, int len, int type)
2911 char str[KSYM_SYMBOL_LEN];
2914 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2917 /* module lookup requires matching the module */
2918 if (!modname || strcmp(modname, mod))
2921 /* blank search means to match all funcs in the mod */
2926 return ftrace_match(str, regex, len, type);
2930 match_records(struct ftrace_hash *hash, char *buff,
2931 int len, char *mod, int not)
2933 unsigned search_len = 0;
2934 struct ftrace_page *pg;
2935 struct dyn_ftrace *rec;
2936 int type = MATCH_FULL;
2937 char *search = buff;
2942 type = filter_parse_regex(buff, len, &search, ¬);
2943 search_len = strlen(search);
2946 mutex_lock(&ftrace_lock);
2948 if (unlikely(ftrace_disabled))
2951 do_for_each_ftrace_rec(pg, rec) {
2952 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2953 ret = enter_record(hash, rec, not);
2960 } while_for_each_ftrace_rec();
2962 mutex_unlock(&ftrace_lock);
2968 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2970 return match_records(hash, buff, len, NULL, 0);
2974 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2978 /* blank or '*' mean the same */
2979 if (strcmp(buff, "*") == 0)
2982 /* handle the case of 'dont filter this module' */
2983 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2988 return match_records(hash, buff, strlen(buff), mod, not);
2992 * We register the module command as a template to show others how
2993 * to register the a command as well.
2997 ftrace_mod_callback(struct ftrace_hash *hash,
2998 char *func, char *cmd, char *param, int enable)
3004 * cmd == 'mod' because we only registered this func
3005 * for the 'mod' ftrace_func_command.
3006 * But if you register one func with multiple commands,
3007 * you can tell which command was used by the cmd
3011 /* we must have a module name */
3015 mod = strsep(¶m, ":");
3019 ret = ftrace_match_module_records(hash, func, mod);
3028 static struct ftrace_func_command ftrace_mod_cmd = {
3030 .func = ftrace_mod_callback,
3033 static int __init ftrace_mod_cmd_init(void)
3035 return register_ftrace_command(&ftrace_mod_cmd);
3037 core_initcall(ftrace_mod_cmd_init);
3039 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3040 struct ftrace_ops *op, struct pt_regs *pt_regs)
3042 struct ftrace_func_probe *entry;
3043 struct hlist_head *hhd;
3046 key = hash_long(ip, FTRACE_HASH_BITS);
3048 hhd = &ftrace_func_hash[key];
3050 if (hlist_empty(hhd))
3054 * Disable preemption for these calls to prevent a RCU grace
3055 * period. This syncs the hash iteration and freeing of items
3056 * on the hash. rcu_read_lock is too dangerous here.
3058 preempt_disable_notrace();
3059 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3060 if (entry->ip == ip)
3061 entry->ops->func(ip, parent_ip, &entry->data);
3063 preempt_enable_notrace();
3066 static struct ftrace_ops trace_probe_ops __read_mostly =
3068 .func = function_trace_probe_call,
3069 .flags = FTRACE_OPS_FL_INITIALIZED,
3070 INIT_REGEX_LOCK(trace_probe_ops)
3073 static int ftrace_probe_registered;
3075 static void __enable_ftrace_function_probe(void)
3080 if (ftrace_probe_registered) {
3081 /* still need to update the function call sites */
3083 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3087 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3088 struct hlist_head *hhd = &ftrace_func_hash[i];
3092 /* Nothing registered? */
3093 if (i == FTRACE_FUNC_HASHSIZE)
3096 ret = ftrace_startup(&trace_probe_ops, 0);
3098 ftrace_probe_registered = 1;
3101 static void __disable_ftrace_function_probe(void)
3105 if (!ftrace_probe_registered)
3108 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3109 struct hlist_head *hhd = &ftrace_func_hash[i];
3114 /* no more funcs left */
3115 ftrace_shutdown(&trace_probe_ops, 0);
3117 ftrace_probe_registered = 0;
3121 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3123 if (entry->ops->free)
3124 entry->ops->free(entry->ops, entry->ip, &entry->data);
3129 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3132 struct ftrace_func_probe *entry;
3133 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3134 struct ftrace_hash *hash;
3135 struct ftrace_page *pg;
3136 struct dyn_ftrace *rec;
3143 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3144 len = strlen(search);
3146 /* we do not support '!' for function probes */
3150 mutex_lock(&trace_probe_ops.regex_lock);
3152 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3158 if (unlikely(ftrace_disabled)) {
3163 mutex_lock(&ftrace_lock);
3165 do_for_each_ftrace_rec(pg, rec) {
3167 if (!ftrace_match_record(rec, NULL, search, len, type))
3170 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3172 /* If we did not process any, then return error */
3183 * The caller might want to do something special
3184 * for each function we find. We call the callback
3185 * to give the caller an opportunity to do so.
3188 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3189 /* caller does not like this func */
3195 ret = enter_record(hash, rec, 0);
3203 entry->ip = rec->ip;
3205 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3206 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3208 } while_for_each_ftrace_rec();
3210 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3214 __enable_ftrace_function_probe();
3217 mutex_unlock(&ftrace_lock);
3219 mutex_unlock(&trace_probe_ops.regex_lock);
3220 free_ftrace_hash(hash);
3226 PROBE_TEST_FUNC = 1,
3231 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3232 void *data, int flags)
3234 struct ftrace_func_entry *rec_entry;
3235 struct ftrace_func_probe *entry;
3236 struct ftrace_func_probe *p;
3237 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3238 struct list_head free_list;
3239 struct ftrace_hash *hash;
3240 struct hlist_node *tmp;
3241 char str[KSYM_SYMBOL_LEN];
3242 int type = MATCH_FULL;
3246 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3251 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3252 len = strlen(search);
3254 /* we do not support '!' for function probes */
3259 mutex_lock(&trace_probe_ops.regex_lock);
3261 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3263 /* Hmm, should report this somehow */
3266 INIT_LIST_HEAD(&free_list);
3268 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3269 struct hlist_head *hhd = &ftrace_func_hash[i];
3271 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3273 /* break up if statements for readability */
3274 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3277 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3280 /* do this last, since it is the most expensive */
3282 kallsyms_lookup(entry->ip, NULL, NULL,
3284 if (!ftrace_match(str, glob, len, type))
3288 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3289 /* It is possible more than one entry had this ip */
3291 free_hash_entry(hash, rec_entry);
3293 hlist_del_rcu(&entry->node);
3294 list_add(&entry->free_list, &free_list);
3297 mutex_lock(&ftrace_lock);
3298 __disable_ftrace_function_probe();
3300 * Remove after the disable is called. Otherwise, if the last
3301 * probe is removed, a null hash means *all enabled*.
3303 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3304 synchronize_sched();
3305 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3306 list_del(&entry->free_list);
3307 ftrace_free_entry(entry);
3309 mutex_unlock(&ftrace_lock);
3312 mutex_unlock(&trace_probe_ops.regex_lock);
3313 free_ftrace_hash(hash);
3317 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3320 __unregister_ftrace_function_probe(glob, ops, data,
3321 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3325 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3327 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3330 void unregister_ftrace_function_probe_all(char *glob)
3332 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3335 static LIST_HEAD(ftrace_commands);
3336 static DEFINE_MUTEX(ftrace_cmd_mutex);
3338 int register_ftrace_command(struct ftrace_func_command *cmd)
3340 struct ftrace_func_command *p;
3343 mutex_lock(&ftrace_cmd_mutex);
3344 list_for_each_entry(p, &ftrace_commands, list) {
3345 if (strcmp(cmd->name, p->name) == 0) {
3350 list_add(&cmd->list, &ftrace_commands);
3352 mutex_unlock(&ftrace_cmd_mutex);
3357 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3359 struct ftrace_func_command *p, *n;
3362 mutex_lock(&ftrace_cmd_mutex);
3363 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3364 if (strcmp(cmd->name, p->name) == 0) {
3366 list_del_init(&p->list);
3371 mutex_unlock(&ftrace_cmd_mutex);
3376 static int ftrace_process_regex(struct ftrace_hash *hash,
3377 char *buff, int len, int enable)
3379 char *func, *command, *next = buff;
3380 struct ftrace_func_command *p;
3383 func = strsep(&next, ":");
3386 ret = ftrace_match_records(hash, func, len);
3396 command = strsep(&next, ":");
3398 mutex_lock(&ftrace_cmd_mutex);
3399 list_for_each_entry(p, &ftrace_commands, list) {
3400 if (strcmp(p->name, command) == 0) {
3401 ret = p->func(hash, func, command, next, enable);
3406 mutex_unlock(&ftrace_cmd_mutex);
3412 ftrace_regex_write(struct file *file, const char __user *ubuf,
3413 size_t cnt, loff_t *ppos, int enable)
3415 struct ftrace_iterator *iter;
3416 struct trace_parser *parser;
3422 if (file->f_mode & FMODE_READ) {
3423 struct seq_file *m = file->private_data;
3426 iter = file->private_data;
3428 if (unlikely(ftrace_disabled))
3431 /* iter->hash is a local copy, so we don't need regex_lock */
3433 parser = &iter->parser;
3434 read = trace_get_user(parser, ubuf, cnt, ppos);
3436 if (read >= 0 && trace_parser_loaded(parser) &&
3437 !trace_parser_cont(parser)) {
3438 ret = ftrace_process_regex(iter->hash, parser->buffer,
3439 parser->idx, enable);
3440 trace_parser_clear(parser);
3451 ftrace_filter_write(struct file *file, const char __user *ubuf,
3452 size_t cnt, loff_t *ppos)
3454 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3458 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3459 size_t cnt, loff_t *ppos)
3461 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3465 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3467 struct ftrace_func_entry *entry;
3469 if (!ftrace_location(ip))
3473 entry = ftrace_lookup_ip(hash, ip);
3476 free_hash_entry(hash, entry);
3480 return add_hash_entry(hash, ip);
3484 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3485 unsigned long ip, int remove, int reset, int enable)
3487 struct ftrace_hash **orig_hash;
3488 struct ftrace_hash *hash;
3491 /* All global ops uses the global ops filters */
3492 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3495 if (unlikely(ftrace_disabled))
3498 mutex_lock(&ops->regex_lock);
3501 orig_hash = &ops->filter_hash;
3503 orig_hash = &ops->notrace_hash;
3505 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3508 goto out_regex_unlock;
3512 ftrace_filter_reset(hash);
3513 if (buf && !ftrace_match_records(hash, buf, len)) {
3515 goto out_regex_unlock;
3518 ret = ftrace_match_addr(hash, ip, remove);
3520 goto out_regex_unlock;
3523 mutex_lock(&ftrace_lock);
3524 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3525 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3527 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3529 mutex_unlock(&ftrace_lock);
3532 mutex_unlock(&ops->regex_lock);
3534 free_ftrace_hash(hash);
3539 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3540 int reset, int enable)
3542 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3546 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3547 * @ops - the ops to set the filter with
3548 * @ip - the address to add to or remove from the filter.
3549 * @remove - non zero to remove the ip from the filter
3550 * @reset - non zero to reset all filters before applying this filter.
3552 * Filters denote which functions should be enabled when tracing is enabled
3553 * If @ip is NULL, it failes to update filter.
3555 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3556 int remove, int reset)
3558 ftrace_ops_init(ops);
3559 return ftrace_set_addr(ops, ip, remove, reset, 1);
3561 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3564 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3565 int reset, int enable)
3567 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3571 * ftrace_set_filter - set a function to filter on in ftrace
3572 * @ops - the ops to set the filter with
3573 * @buf - the string that holds the function filter text.
3574 * @len - the length of the string.
3575 * @reset - non zero to reset all filters before applying this filter.
3577 * Filters denote which functions should be enabled when tracing is enabled.
3578 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3580 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3583 ftrace_ops_init(ops);
3584 return ftrace_set_regex(ops, buf, len, reset, 1);
3586 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3589 * ftrace_set_notrace - set a function to not trace in ftrace
3590 * @ops - the ops to set the notrace filter with
3591 * @buf - the string that holds the function notrace text.
3592 * @len - the length of the string.
3593 * @reset - non zero to reset all filters before applying this filter.
3595 * Notrace Filters denote which functions should not be enabled when tracing
3596 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3599 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3602 ftrace_ops_init(ops);
3603 return ftrace_set_regex(ops, buf, len, reset, 0);
3605 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3607 * ftrace_set_filter - set a function to filter on in ftrace
3608 * @ops - the ops to set the filter with
3609 * @buf - the string that holds the function filter text.
3610 * @len - the length of the string.
3611 * @reset - non zero to reset all filters before applying this filter.
3613 * Filters denote which functions should be enabled when tracing is enabled.
3614 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3616 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3618 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3620 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3623 * ftrace_set_notrace - set a function to not trace in ftrace
3624 * @ops - the ops to set the notrace filter with
3625 * @buf - the string that holds the function notrace text.
3626 * @len - the length of the string.
3627 * @reset - non zero to reset all filters before applying this filter.
3629 * Notrace Filters denote which functions should not be enabled when tracing
3630 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3633 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3635 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3637 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3640 * command line interface to allow users to set filters on boot up.
3642 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3643 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3644 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3646 static int __init set_ftrace_notrace(char *str)
3648 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3651 __setup("ftrace_notrace=", set_ftrace_notrace);
3653 static int __init set_ftrace_filter(char *str)
3655 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3658 __setup("ftrace_filter=", set_ftrace_filter);
3660 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3661 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3662 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3664 static int __init set_graph_function(char *str)
3666 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3669 __setup("ftrace_graph_filter=", set_graph_function);
3671 static void __init set_ftrace_early_graph(char *buf)
3677 func = strsep(&buf, ",");
3678 /* we allow only one expression at a time */
3679 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3682 printk(KERN_DEBUG "ftrace: function %s not "
3683 "traceable\n", func);
3686 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3689 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3693 ftrace_ops_init(ops);
3696 func = strsep(&buf, ",");
3697 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3701 static void __init set_ftrace_early_filters(void)
3703 if (ftrace_filter_buf[0])
3704 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3705 if (ftrace_notrace_buf[0])
3706 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3707 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3708 if (ftrace_graph_buf[0])
3709 set_ftrace_early_graph(ftrace_graph_buf);
3710 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3713 int ftrace_regex_release(struct inode *inode, struct file *file)
3715 struct seq_file *m = (struct seq_file *)file->private_data;
3716 struct ftrace_iterator *iter;
3717 struct ftrace_hash **orig_hash;
3718 struct trace_parser *parser;
3722 if (file->f_mode & FMODE_READ) {
3724 seq_release(inode, file);
3726 iter = file->private_data;
3728 parser = &iter->parser;
3729 if (trace_parser_loaded(parser)) {
3730 parser->buffer[parser->idx] = 0;
3731 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3734 trace_parser_put(parser);
3736 mutex_lock(&iter->ops->regex_lock);
3738 if (file->f_mode & FMODE_WRITE) {
3739 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3742 orig_hash = &iter->ops->filter_hash;
3744 orig_hash = &iter->ops->notrace_hash;
3746 mutex_lock(&ftrace_lock);
3747 ret = ftrace_hash_move(iter->ops, filter_hash,
3748 orig_hash, iter->hash);
3749 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3751 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3753 mutex_unlock(&ftrace_lock);
3756 mutex_unlock(&iter->ops->regex_lock);
3757 free_ftrace_hash(iter->hash);
3763 static const struct file_operations ftrace_avail_fops = {
3764 .open = ftrace_avail_open,
3766 .llseek = seq_lseek,
3767 .release = seq_release_private,
3770 static const struct file_operations ftrace_enabled_fops = {
3771 .open = ftrace_enabled_open,
3773 .llseek = seq_lseek,
3774 .release = seq_release_private,
3777 static const struct file_operations ftrace_filter_fops = {
3778 .open = ftrace_filter_open,
3780 .write = ftrace_filter_write,
3781 .llseek = ftrace_filter_lseek,
3782 .release = ftrace_regex_release,
3785 static const struct file_operations ftrace_notrace_fops = {
3786 .open = ftrace_notrace_open,
3788 .write = ftrace_notrace_write,
3789 .llseek = ftrace_filter_lseek,
3790 .release = ftrace_regex_release,
3793 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3795 static DEFINE_MUTEX(graph_lock);
3797 int ftrace_graph_count;
3798 int ftrace_graph_filter_enabled;
3799 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3802 __g_next(struct seq_file *m, loff_t *pos)
3804 if (*pos >= ftrace_graph_count)
3806 return &ftrace_graph_funcs[*pos];
3810 g_next(struct seq_file *m, void *v, loff_t *pos)
3813 return __g_next(m, pos);
3816 static void *g_start(struct seq_file *m, loff_t *pos)
3818 mutex_lock(&graph_lock);
3820 /* Nothing, tell g_show to print all functions are enabled */
3821 if (!ftrace_graph_filter_enabled && !*pos)
3824 return __g_next(m, pos);
3827 static void g_stop(struct seq_file *m, void *p)
3829 mutex_unlock(&graph_lock);
3832 static int g_show(struct seq_file *m, void *v)
3834 unsigned long *ptr = v;
3839 if (ptr == (unsigned long *)1) {
3840 seq_printf(m, "#### all functions enabled ####\n");
3844 seq_printf(m, "%ps\n", (void *)*ptr);
3849 static const struct seq_operations ftrace_graph_seq_ops = {
3857 ftrace_graph_open(struct inode *inode, struct file *file)
3861 if (unlikely(ftrace_disabled))
3864 mutex_lock(&graph_lock);
3865 if ((file->f_mode & FMODE_WRITE) &&
3866 (file->f_flags & O_TRUNC)) {
3867 ftrace_graph_filter_enabled = 0;
3868 ftrace_graph_count = 0;
3869 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3871 mutex_unlock(&graph_lock);
3873 if (file->f_mode & FMODE_READ)
3874 ret = seq_open(file, &ftrace_graph_seq_ops);
3880 ftrace_graph_release(struct inode *inode, struct file *file)
3882 if (file->f_mode & FMODE_READ)
3883 seq_release(inode, file);
3888 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3890 struct dyn_ftrace *rec;
3891 struct ftrace_page *pg;
3900 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3901 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3904 search_len = strlen(search);
3906 mutex_lock(&ftrace_lock);
3908 if (unlikely(ftrace_disabled)) {
3909 mutex_unlock(&ftrace_lock);
3913 do_for_each_ftrace_rec(pg, rec) {
3915 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3916 /* if it is in the array */
3918 for (i = 0; i < *idx; i++) {
3919 if (array[i] == rec->ip) {
3928 array[(*idx)++] = rec->ip;
3929 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3934 array[i] = array[--(*idx)];
3940 } while_for_each_ftrace_rec();
3942 mutex_unlock(&ftrace_lock);
3947 ftrace_graph_filter_enabled = !!(*idx);
3953 ftrace_graph_write(struct file *file, const char __user *ubuf,
3954 size_t cnt, loff_t *ppos)
3956 struct trace_parser parser;
3962 mutex_lock(&graph_lock);
3964 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3969 read = trace_get_user(&parser, ubuf, cnt, ppos);
3971 if (read >= 0 && trace_parser_loaded((&parser))) {
3972 parser.buffer[parser.idx] = 0;
3974 /* we allow only one expression at a time */
3975 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3984 trace_parser_put(&parser);
3986 mutex_unlock(&graph_lock);
3991 static const struct file_operations ftrace_graph_fops = {
3992 .open = ftrace_graph_open,
3994 .write = ftrace_graph_write,
3995 .llseek = ftrace_filter_lseek,
3996 .release = ftrace_graph_release,
3998 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4000 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4003 trace_create_file("available_filter_functions", 0444,
4004 d_tracer, NULL, &ftrace_avail_fops);
4006 trace_create_file("enabled_functions", 0444,
4007 d_tracer, NULL, &ftrace_enabled_fops);
4009 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4010 NULL, &ftrace_filter_fops);
4012 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
4013 NULL, &ftrace_notrace_fops);
4015 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4016 trace_create_file("set_graph_function", 0444, d_tracer,
4018 &ftrace_graph_fops);
4019 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4024 static int ftrace_cmp_ips(const void *a, const void *b)
4026 const unsigned long *ipa = a;
4027 const unsigned long *ipb = b;
4036 static void ftrace_swap_ips(void *a, void *b, int size)
4038 unsigned long *ipa = a;
4039 unsigned long *ipb = b;
4047 static int ftrace_process_locs(struct module *mod,
4048 unsigned long *start,
4051 struct ftrace_page *start_pg;
4052 struct ftrace_page *pg;
4053 struct dyn_ftrace *rec;
4054 unsigned long count;
4057 unsigned long flags = 0; /* Shut up gcc */
4060 count = end - start;
4065 sort(start, count, sizeof(*start),
4066 ftrace_cmp_ips, ftrace_swap_ips);
4068 start_pg = ftrace_allocate_pages(count);
4072 mutex_lock(&ftrace_lock);
4075 * Core and each module needs their own pages, as
4076 * modules will free them when they are removed.
4077 * Force a new page to be allocated for modules.
4080 WARN_ON(ftrace_pages || ftrace_pages_start);
4081 /* First initialization */
4082 ftrace_pages = ftrace_pages_start = start_pg;
4087 if (WARN_ON(ftrace_pages->next)) {
4088 /* Hmm, we have free pages? */
4089 while (ftrace_pages->next)
4090 ftrace_pages = ftrace_pages->next;
4093 ftrace_pages->next = start_pg;
4099 addr = ftrace_call_adjust(*p++);
4101 * Some architecture linkers will pad between
4102 * the different mcount_loc sections of different
4103 * object files to satisfy alignments.
4104 * Skip any NULL pointers.
4109 if (pg->index == pg->size) {
4110 /* We should have allocated enough */
4111 if (WARN_ON(!pg->next))
4116 rec = &pg->records[pg->index++];
4120 /* We should have used all pages */
4123 /* Assign the last page to ftrace_pages */
4126 /* These new locations need to be initialized */
4127 ftrace_new_pgs = start_pg;
4130 * We only need to disable interrupts on start up
4131 * because we are modifying code that an interrupt
4132 * may execute, and the modification is not atomic.
4133 * But for modules, nothing runs the code we modify
4134 * until we are finished with it, and there's no
4135 * reason to cause large interrupt latencies while we do it.
4138 local_irq_save(flags);
4139 ftrace_update_code(mod);
4141 local_irq_restore(flags);
4144 mutex_unlock(&ftrace_lock);
4149 #ifdef CONFIG_MODULES
4151 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4153 void ftrace_release_mod(struct module *mod)
4155 struct dyn_ftrace *rec;
4156 struct ftrace_page **last_pg;
4157 struct ftrace_page *pg;
4160 mutex_lock(&ftrace_lock);
4162 if (ftrace_disabled)
4166 * Each module has its own ftrace_pages, remove
4167 * them from the list.
4169 last_pg = &ftrace_pages_start;
4170 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4171 rec = &pg->records[0];
4172 if (within_module_core(rec->ip, mod)) {
4174 * As core pages are first, the first
4175 * page should never be a module page.
4177 if (WARN_ON(pg == ftrace_pages_start))
4180 /* Check if we are deleting the last page */
4181 if (pg == ftrace_pages)
4182 ftrace_pages = next_to_ftrace_page(last_pg);
4184 *last_pg = pg->next;
4185 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4186 free_pages((unsigned long)pg->records, order);
4189 last_pg = &pg->next;
4192 mutex_unlock(&ftrace_lock);
4195 static void ftrace_init_module(struct module *mod,
4196 unsigned long *start, unsigned long *end)
4198 if (ftrace_disabled || start == end)
4200 ftrace_process_locs(mod, start, end);
4203 static int ftrace_module_notify_enter(struct notifier_block *self,
4204 unsigned long val, void *data)
4206 struct module *mod = data;
4208 if (val == MODULE_STATE_COMING)
4209 ftrace_init_module(mod, mod->ftrace_callsites,
4210 mod->ftrace_callsites +
4211 mod->num_ftrace_callsites);
4215 static int ftrace_module_notify_exit(struct notifier_block *self,
4216 unsigned long val, void *data)
4218 struct module *mod = data;
4220 if (val == MODULE_STATE_GOING)
4221 ftrace_release_mod(mod);
4226 static int ftrace_module_notify_enter(struct notifier_block *self,
4227 unsigned long val, void *data)
4231 static int ftrace_module_notify_exit(struct notifier_block *self,
4232 unsigned long val, void *data)
4236 #endif /* CONFIG_MODULES */
4238 struct notifier_block ftrace_module_enter_nb = {
4239 .notifier_call = ftrace_module_notify_enter,
4240 .priority = INT_MAX, /* Run before anything that can use kprobes */
4243 struct notifier_block ftrace_module_exit_nb = {
4244 .notifier_call = ftrace_module_notify_exit,
4245 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4248 extern unsigned long __start_mcount_loc[];
4249 extern unsigned long __stop_mcount_loc[];
4251 void __init ftrace_init(void)
4253 unsigned long count, addr, flags;
4256 /* Keep the ftrace pointer to the stub */
4257 addr = (unsigned long)ftrace_stub;
4259 local_irq_save(flags);
4260 ftrace_dyn_arch_init(&addr);
4261 local_irq_restore(flags);
4263 /* ftrace_dyn_arch_init places the return code in addr */
4267 count = __stop_mcount_loc - __start_mcount_loc;
4269 ret = ftrace_dyn_table_alloc(count);
4273 last_ftrace_enabled = ftrace_enabled = 1;
4275 ret = ftrace_process_locs(NULL,
4279 ret = register_module_notifier(&ftrace_module_enter_nb);
4281 pr_warning("Failed to register trace ftrace module enter notifier\n");
4283 ret = register_module_notifier(&ftrace_module_exit_nb);
4285 pr_warning("Failed to register trace ftrace module exit notifier\n");
4287 set_ftrace_early_filters();
4291 ftrace_disabled = 1;
4296 static struct ftrace_ops global_ops = {
4297 .func = ftrace_stub,
4298 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4299 INIT_REGEX_LOCK(global_ops)
4302 static int __init ftrace_nodyn_init(void)
4307 core_initcall(ftrace_nodyn_init);
4309 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4310 static inline void ftrace_startup_enable(int command) { }
4311 /* Keep as macros so we do not need to define the commands */
4312 # define ftrace_startup(ops, command) \
4314 int ___ret = __register_ftrace_function(ops); \
4316 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4319 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4321 # define ftrace_startup_sysctl() do { } while (0)
4322 # define ftrace_shutdown_sysctl() do { } while (0)
4325 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4330 #endif /* CONFIG_DYNAMIC_FTRACE */
4333 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4334 struct ftrace_ops *op, struct pt_regs *regs)
4336 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4340 * Some of the ops may be dynamically allocated,
4341 * they must be freed after a synchronize_sched().
4343 preempt_disable_notrace();
4344 trace_recursion_set(TRACE_CONTROL_BIT);
4345 do_for_each_ftrace_op(op, ftrace_control_list) {
4346 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4347 !ftrace_function_local_disabled(op) &&
4348 ftrace_ops_test(op, ip, regs))
4349 op->func(ip, parent_ip, op, regs);
4350 } while_for_each_ftrace_op(op);
4351 trace_recursion_clear(TRACE_CONTROL_BIT);
4352 preempt_enable_notrace();
4355 static struct ftrace_ops control_ops = {
4356 .func = ftrace_ops_control_func,
4357 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4358 INIT_REGEX_LOCK(control_ops)
4362 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4363 struct ftrace_ops *ignored, struct pt_regs *regs)
4365 struct ftrace_ops *op;
4368 if (function_trace_stop)
4371 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4376 * Some of the ops may be dynamically allocated,
4377 * they must be freed after a synchronize_sched().
4379 preempt_disable_notrace();
4380 do_for_each_ftrace_op(op, ftrace_ops_list) {
4381 if (ftrace_ops_test(op, ip, regs))
4382 op->func(ip, parent_ip, op, regs);
4383 } while_for_each_ftrace_op(op);
4384 preempt_enable_notrace();
4385 trace_clear_recursion(bit);
4389 * Some archs only support passing ip and parent_ip. Even though
4390 * the list function ignores the op parameter, we do not want any
4391 * C side effects, where a function is called without the caller
4392 * sending a third parameter.
4393 * Archs are to support both the regs and ftrace_ops at the same time.
4394 * If they support ftrace_ops, it is assumed they support regs.
4395 * If call backs want to use regs, they must either check for regs
4396 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4397 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4398 * An architecture can pass partial regs with ftrace_ops and still
4399 * set the ARCH_SUPPORT_FTARCE_OPS.
4401 #if ARCH_SUPPORTS_FTRACE_OPS
4402 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4403 struct ftrace_ops *op, struct pt_regs *regs)
4405 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4408 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4410 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4414 static void clear_ftrace_swapper(void)
4416 struct task_struct *p;
4420 for_each_online_cpu(cpu) {
4422 clear_tsk_trace_trace(p);
4427 static void set_ftrace_swapper(void)
4429 struct task_struct *p;
4433 for_each_online_cpu(cpu) {
4435 set_tsk_trace_trace(p);
4440 static void clear_ftrace_pid(struct pid *pid)
4442 struct task_struct *p;
4445 do_each_pid_task(pid, PIDTYPE_PID, p) {
4446 clear_tsk_trace_trace(p);
4447 } while_each_pid_task(pid, PIDTYPE_PID, p);
4453 static void set_ftrace_pid(struct pid *pid)
4455 struct task_struct *p;
4458 do_each_pid_task(pid, PIDTYPE_PID, p) {
4459 set_tsk_trace_trace(p);
4460 } while_each_pid_task(pid, PIDTYPE_PID, p);
4464 static void clear_ftrace_pid_task(struct pid *pid)
4466 if (pid == ftrace_swapper_pid)
4467 clear_ftrace_swapper();
4469 clear_ftrace_pid(pid);
4472 static void set_ftrace_pid_task(struct pid *pid)
4474 if (pid == ftrace_swapper_pid)
4475 set_ftrace_swapper();
4477 set_ftrace_pid(pid);
4480 static int ftrace_pid_add(int p)
4483 struct ftrace_pid *fpid;
4486 mutex_lock(&ftrace_lock);
4489 pid = ftrace_swapper_pid;
4491 pid = find_get_pid(p);
4498 list_for_each_entry(fpid, &ftrace_pids, list)
4499 if (fpid->pid == pid)
4504 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4508 list_add(&fpid->list, &ftrace_pids);
4511 set_ftrace_pid_task(pid);
4513 ftrace_update_pid_func();
4514 ftrace_startup_enable(0);
4516 mutex_unlock(&ftrace_lock);
4520 if (pid != ftrace_swapper_pid)
4524 mutex_unlock(&ftrace_lock);
4528 static void ftrace_pid_reset(void)
4530 struct ftrace_pid *fpid, *safe;
4532 mutex_lock(&ftrace_lock);
4533 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4534 struct pid *pid = fpid->pid;
4536 clear_ftrace_pid_task(pid);
4538 list_del(&fpid->list);
4542 ftrace_update_pid_func();
4543 ftrace_startup_enable(0);
4545 mutex_unlock(&ftrace_lock);
4548 static void *fpid_start(struct seq_file *m, loff_t *pos)
4550 mutex_lock(&ftrace_lock);
4552 if (list_empty(&ftrace_pids) && (!*pos))
4555 return seq_list_start(&ftrace_pids, *pos);
4558 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4563 return seq_list_next(v, &ftrace_pids, pos);
4566 static void fpid_stop(struct seq_file *m, void *p)
4568 mutex_unlock(&ftrace_lock);
4571 static int fpid_show(struct seq_file *m, void *v)
4573 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4575 if (v == (void *)1) {
4576 seq_printf(m, "no pid\n");
4580 if (fpid->pid == ftrace_swapper_pid)
4581 seq_printf(m, "swapper tasks\n");
4583 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4588 static const struct seq_operations ftrace_pid_sops = {
4589 .start = fpid_start,
4596 ftrace_pid_open(struct inode *inode, struct file *file)
4600 if ((file->f_mode & FMODE_WRITE) &&
4601 (file->f_flags & O_TRUNC))
4604 if (file->f_mode & FMODE_READ)
4605 ret = seq_open(file, &ftrace_pid_sops);
4611 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4612 size_t cnt, loff_t *ppos)
4618 if (cnt >= sizeof(buf))
4621 if (copy_from_user(&buf, ubuf, cnt))
4627 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4628 * to clean the filter quietly.
4630 tmp = strstrip(buf);
4631 if (strlen(tmp) == 0)
4634 ret = kstrtol(tmp, 10, &val);
4638 ret = ftrace_pid_add(val);
4640 return ret ? ret : cnt;
4644 ftrace_pid_release(struct inode *inode, struct file *file)
4646 if (file->f_mode & FMODE_READ)
4647 seq_release(inode, file);
4652 static const struct file_operations ftrace_pid_fops = {
4653 .open = ftrace_pid_open,
4654 .write = ftrace_pid_write,
4656 .llseek = ftrace_filter_lseek,
4657 .release = ftrace_pid_release,
4660 static __init int ftrace_init_debugfs(void)
4662 struct dentry *d_tracer;
4664 d_tracer = tracing_init_dentry();
4668 ftrace_init_dyn_debugfs(d_tracer);
4670 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4671 NULL, &ftrace_pid_fops);
4673 ftrace_profile_debugfs(d_tracer);
4677 fs_initcall(ftrace_init_debugfs);
4680 * ftrace_kill - kill ftrace
4682 * This function should be used by panic code. It stops ftrace
4683 * but in a not so nice way. If you need to simply kill ftrace
4684 * from a non-atomic section, use ftrace_kill.
4686 void ftrace_kill(void)
4688 ftrace_disabled = 1;
4690 clear_ftrace_function();
4694 * Test if ftrace is dead or not.
4696 int ftrace_is_dead(void)
4698 return ftrace_disabled;
4702 * register_ftrace_function - register a function for profiling
4703 * @ops - ops structure that holds the function for profiling.
4705 * Register a function to be called by all functions in the
4708 * Note: @ops->func and all the functions it calls must be labeled
4709 * with "notrace", otherwise it will go into a
4712 int register_ftrace_function(struct ftrace_ops *ops)
4716 ftrace_ops_init(ops);
4718 mutex_lock(&ftrace_lock);
4720 ret = ftrace_startup(ops, 0);
4722 mutex_unlock(&ftrace_lock);
4726 EXPORT_SYMBOL_GPL(register_ftrace_function);
4729 * unregister_ftrace_function - unregister a function for profiling.
4730 * @ops - ops structure that holds the function to unregister
4732 * Unregister a function that was added to be called by ftrace profiling.
4734 int unregister_ftrace_function(struct ftrace_ops *ops)
4738 mutex_lock(&ftrace_lock);
4739 ret = ftrace_shutdown(ops, 0);
4740 mutex_unlock(&ftrace_lock);
4744 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4747 ftrace_enable_sysctl(struct ctl_table *table, int write,
4748 void __user *buffer, size_t *lenp,
4753 mutex_lock(&ftrace_lock);
4755 if (unlikely(ftrace_disabled))
4758 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4760 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4763 last_ftrace_enabled = !!ftrace_enabled;
4765 if (ftrace_enabled) {
4767 ftrace_startup_sysctl();
4769 /* we are starting ftrace again */
4770 if (ftrace_ops_list != &ftrace_list_end)
4771 update_ftrace_function();
4774 /* stopping ftrace calls (just send to ftrace_stub) */
4775 ftrace_trace_function = ftrace_stub;
4777 ftrace_shutdown_sysctl();
4781 mutex_unlock(&ftrace_lock);
4785 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4787 static int ftrace_graph_active;
4788 static struct notifier_block ftrace_suspend_notifier;
4790 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4795 /* The callbacks that hook a function */
4796 trace_func_graph_ret_t ftrace_graph_return =
4797 (trace_func_graph_ret_t)ftrace_stub;
4798 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4800 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4801 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4805 unsigned long flags;
4806 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4807 struct task_struct *g, *t;
4809 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4810 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4811 * sizeof(struct ftrace_ret_stack),
4813 if (!ret_stack_list[i]) {
4821 read_lock_irqsave(&tasklist_lock, flags);
4822 do_each_thread(g, t) {
4828 if (t->ret_stack == NULL) {
4829 atomic_set(&t->tracing_graph_pause, 0);
4830 atomic_set(&t->trace_overrun, 0);
4831 t->curr_ret_stack = -1;
4832 /* Make sure the tasks see the -1 first: */
4834 t->ret_stack = ret_stack_list[start++];
4836 } while_each_thread(g, t);
4839 read_unlock_irqrestore(&tasklist_lock, flags);
4841 for (i = start; i < end; i++)
4842 kfree(ret_stack_list[i]);
4847 ftrace_graph_probe_sched_switch(void *ignore,
4848 struct task_struct *prev, struct task_struct *next)
4850 unsigned long long timestamp;
4854 * Does the user want to count the time a function was asleep.
4855 * If so, do not update the time stamps.
4857 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4860 timestamp = trace_clock_local();
4862 prev->ftrace_timestamp = timestamp;
4864 /* only process tasks that we timestamped */
4865 if (!next->ftrace_timestamp)
4869 * Update all the counters in next to make up for the
4870 * time next was sleeping.
4872 timestamp -= next->ftrace_timestamp;
4874 for (index = next->curr_ret_stack; index >= 0; index--)
4875 next->ret_stack[index].calltime += timestamp;
4878 /* Allocate a return stack for each task */
4879 static int start_graph_tracing(void)
4881 struct ftrace_ret_stack **ret_stack_list;
4884 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4885 sizeof(struct ftrace_ret_stack *),
4888 if (!ret_stack_list)
4891 /* The cpu_boot init_task->ret_stack will never be freed */
4892 for_each_online_cpu(cpu) {
4893 if (!idle_task(cpu)->ret_stack)
4894 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4898 ret = alloc_retstack_tasklist(ret_stack_list);
4899 } while (ret == -EAGAIN);
4902 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4904 pr_info("ftrace_graph: Couldn't activate tracepoint"
4905 " probe to kernel_sched_switch\n");
4908 kfree(ret_stack_list);
4913 * Hibernation protection.
4914 * The state of the current task is too much unstable during
4915 * suspend/restore to disk. We want to protect against that.
4918 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4922 case PM_HIBERNATION_PREPARE:
4923 pause_graph_tracing();
4926 case PM_POST_HIBERNATION:
4927 unpause_graph_tracing();
4933 /* Just a place holder for function graph */
4934 static struct ftrace_ops fgraph_ops __read_mostly = {
4935 .func = ftrace_stub,
4936 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
4937 FTRACE_OPS_FL_RECURSION_SAFE,
4940 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4941 trace_func_graph_ent_t entryfunc)
4945 mutex_lock(&ftrace_lock);
4947 /* we currently allow only one tracer registered at a time */
4948 if (ftrace_graph_active) {
4953 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4954 register_pm_notifier(&ftrace_suspend_notifier);
4956 ftrace_graph_active++;
4957 ret = start_graph_tracing();
4959 ftrace_graph_active--;
4963 ftrace_graph_return = retfunc;
4964 ftrace_graph_entry = entryfunc;
4966 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
4969 mutex_unlock(&ftrace_lock);
4973 void unregister_ftrace_graph(void)
4975 mutex_lock(&ftrace_lock);
4977 if (unlikely(!ftrace_graph_active))
4980 ftrace_graph_active--;
4981 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4982 ftrace_graph_entry = ftrace_graph_entry_stub;
4983 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
4984 unregister_pm_notifier(&ftrace_suspend_notifier);
4985 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4988 mutex_unlock(&ftrace_lock);
4991 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4994 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4996 atomic_set(&t->tracing_graph_pause, 0);
4997 atomic_set(&t->trace_overrun, 0);
4998 t->ftrace_timestamp = 0;
4999 /* make curr_ret_stack visible before we add the ret_stack */
5001 t->ret_stack = ret_stack;
5005 * Allocate a return stack for the idle task. May be the first
5006 * time through, or it may be done by CPU hotplug online.
5008 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5010 t->curr_ret_stack = -1;
5012 * The idle task has no parent, it either has its own
5013 * stack or no stack at all.
5016 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5018 if (ftrace_graph_active) {
5019 struct ftrace_ret_stack *ret_stack;
5021 ret_stack = per_cpu(idle_ret_stack, cpu);
5023 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5024 * sizeof(struct ftrace_ret_stack),
5028 per_cpu(idle_ret_stack, cpu) = ret_stack;
5030 graph_init_task(t, ret_stack);
5034 /* Allocate a return stack for newly created task */
5035 void ftrace_graph_init_task(struct task_struct *t)
5037 /* Make sure we do not use the parent ret_stack */
5038 t->ret_stack = NULL;
5039 t->curr_ret_stack = -1;
5041 if (ftrace_graph_active) {
5042 struct ftrace_ret_stack *ret_stack;
5044 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5045 * sizeof(struct ftrace_ret_stack),
5049 graph_init_task(t, ret_stack);
5053 void ftrace_graph_exit_task(struct task_struct *t)
5055 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5057 t->ret_stack = NULL;
5058 /* NULL must become visible to IRQs before we free it: */
5064 void ftrace_graph_stop(void)