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
499 * Note synchronize_sched() is not enough, as we
500 * use preempt_disable() to do RCU, but the function
501 * tracer can be called where RCU is not active
502 * (before user_exit()).
504 schedule_on_each_cpu(ftrace_sync);
505 control_ops_free(ops);
508 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
514 update_ftrace_function();
517 * Dynamic ops may be freed, we must make sure that all
518 * callers are done before leaving this function.
520 * Again, normal synchronize_sched() is not good enough.
521 * We need to do a hard force of sched synchronization.
523 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
524 schedule_on_each_cpu(ftrace_sync);
530 static void ftrace_update_pid_func(void)
532 /* Only do something if we are tracing something */
533 if (ftrace_trace_function == ftrace_stub)
536 update_ftrace_function();
539 #ifdef CONFIG_FUNCTION_PROFILER
540 struct ftrace_profile {
541 struct hlist_node node;
543 unsigned long counter;
544 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
545 unsigned long long time;
546 unsigned long long time_squared;
550 struct ftrace_profile_page {
551 struct ftrace_profile_page *next;
553 struct ftrace_profile records[];
556 struct ftrace_profile_stat {
558 struct hlist_head *hash;
559 struct ftrace_profile_page *pages;
560 struct ftrace_profile_page *start;
561 struct tracer_stat stat;
564 #define PROFILE_RECORDS_SIZE \
565 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
567 #define PROFILES_PER_PAGE \
568 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
570 static int ftrace_profile_enabled __read_mostly;
572 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
573 static DEFINE_MUTEX(ftrace_profile_lock);
575 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
577 #define FTRACE_PROFILE_HASH_BITS 10
578 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
581 function_stat_next(void *v, int idx)
583 struct ftrace_profile *rec = v;
584 struct ftrace_profile_page *pg;
586 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
592 if ((void *)rec >= (void *)&pg->records[pg->index]) {
596 rec = &pg->records[0];
604 static void *function_stat_start(struct tracer_stat *trace)
606 struct ftrace_profile_stat *stat =
607 container_of(trace, struct ftrace_profile_stat, stat);
609 if (!stat || !stat->start)
612 return function_stat_next(&stat->start->records[0], 0);
615 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
616 /* function graph compares on total time */
617 static int function_stat_cmp(void *p1, void *p2)
619 struct ftrace_profile *a = p1;
620 struct ftrace_profile *b = p2;
622 if (a->time < b->time)
624 if (a->time > b->time)
630 /* not function graph compares against hits */
631 static int function_stat_cmp(void *p1, void *p2)
633 struct ftrace_profile *a = p1;
634 struct ftrace_profile *b = p2;
636 if (a->counter < b->counter)
638 if (a->counter > b->counter)
645 static int function_stat_headers(struct seq_file *m)
647 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
648 seq_printf(m, " Function "
651 "--- ---- --- ---\n");
653 seq_printf(m, " Function Hit\n"
659 static int function_stat_show(struct seq_file *m, void *v)
661 struct ftrace_profile *rec = v;
662 char str[KSYM_SYMBOL_LEN];
664 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
665 static struct trace_seq s;
666 unsigned long long avg;
667 unsigned long long stddev;
669 mutex_lock(&ftrace_profile_lock);
671 /* we raced with function_profile_reset() */
672 if (unlikely(rec->counter == 0)) {
677 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
678 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
680 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
683 do_div(avg, rec->counter);
685 /* Sample standard deviation (s^2) */
686 if (rec->counter <= 1)
690 * Apply Welford's method:
691 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
693 stddev = rec->counter * rec->time_squared -
694 rec->time * rec->time;
697 * Divide only 1000 for ns^2 -> us^2 conversion.
698 * trace_print_graph_duration will divide 1000 again.
700 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
704 trace_print_graph_duration(rec->time, &s);
705 trace_seq_puts(&s, " ");
706 trace_print_graph_duration(avg, &s);
707 trace_seq_puts(&s, " ");
708 trace_print_graph_duration(stddev, &s);
709 trace_print_seq(m, &s);
713 mutex_unlock(&ftrace_profile_lock);
718 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
720 struct ftrace_profile_page *pg;
722 pg = stat->pages = stat->start;
725 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
730 memset(stat->hash, 0,
731 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
734 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
736 struct ftrace_profile_page *pg;
741 /* If we already allocated, do nothing */
745 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
749 #ifdef CONFIG_DYNAMIC_FTRACE
750 functions = ftrace_update_tot_cnt;
753 * We do not know the number of functions that exist because
754 * dynamic tracing is what counts them. With past experience
755 * we have around 20K functions. That should be more than enough.
756 * It is highly unlikely we will execute every function in
762 pg = stat->start = stat->pages;
764 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
766 for (i = 1; i < pages; i++) {
767 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
778 unsigned long tmp = (unsigned long)pg;
790 static int ftrace_profile_init_cpu(int cpu)
792 struct ftrace_profile_stat *stat;
795 stat = &per_cpu(ftrace_profile_stats, cpu);
798 /* If the profile is already created, simply reset it */
799 ftrace_profile_reset(stat);
804 * We are profiling all functions, but usually only a few thousand
805 * functions are hit. We'll make a hash of 1024 items.
807 size = FTRACE_PROFILE_HASH_SIZE;
809 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
814 /* Preallocate the function profiling pages */
815 if (ftrace_profile_pages_init(stat) < 0) {
824 static int ftrace_profile_init(void)
829 for_each_online_cpu(cpu) {
830 ret = ftrace_profile_init_cpu(cpu);
838 /* interrupts must be disabled */
839 static struct ftrace_profile *
840 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
842 struct ftrace_profile *rec;
843 struct hlist_head *hhd;
846 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
847 hhd = &stat->hash[key];
849 if (hlist_empty(hhd))
852 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
860 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
861 struct ftrace_profile *rec)
865 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
866 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
870 * The memory is already allocated, this simply finds a new record to use.
872 static struct ftrace_profile *
873 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
875 struct ftrace_profile *rec = NULL;
877 /* prevent recursion (from NMIs) */
878 if (atomic_inc_return(&stat->disabled) != 1)
882 * Try to find the function again since an NMI
883 * could have added it
885 rec = ftrace_find_profiled_func(stat, ip);
889 if (stat->pages->index == PROFILES_PER_PAGE) {
890 if (!stat->pages->next)
892 stat->pages = stat->pages->next;
895 rec = &stat->pages->records[stat->pages->index++];
897 ftrace_add_profile(stat, rec);
900 atomic_dec(&stat->disabled);
906 function_profile_call(unsigned long ip, unsigned long parent_ip,
907 struct ftrace_ops *ops, struct pt_regs *regs)
909 struct ftrace_profile_stat *stat;
910 struct ftrace_profile *rec;
913 if (!ftrace_profile_enabled)
916 local_irq_save(flags);
918 stat = &__get_cpu_var(ftrace_profile_stats);
919 if (!stat->hash || !ftrace_profile_enabled)
922 rec = ftrace_find_profiled_func(stat, ip);
924 rec = ftrace_profile_alloc(stat, ip);
931 local_irq_restore(flags);
934 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
935 static int profile_graph_entry(struct ftrace_graph_ent *trace)
937 function_profile_call(trace->func, 0, NULL, NULL);
941 static void profile_graph_return(struct ftrace_graph_ret *trace)
943 struct ftrace_profile_stat *stat;
944 unsigned long long calltime;
945 struct ftrace_profile *rec;
948 local_irq_save(flags);
949 stat = &__get_cpu_var(ftrace_profile_stats);
950 if (!stat->hash || !ftrace_profile_enabled)
953 /* If the calltime was zero'd ignore it */
954 if (!trace->calltime)
957 calltime = trace->rettime - trace->calltime;
959 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
962 index = trace->depth;
964 /* Append this call time to the parent time to subtract */
966 current->ret_stack[index - 1].subtime += calltime;
968 if (current->ret_stack[index].subtime < calltime)
969 calltime -= current->ret_stack[index].subtime;
974 rec = ftrace_find_profiled_func(stat, trace->func);
976 rec->time += calltime;
977 rec->time_squared += calltime * calltime;
981 local_irq_restore(flags);
984 static int register_ftrace_profiler(void)
986 return register_ftrace_graph(&profile_graph_return,
987 &profile_graph_entry);
990 static void unregister_ftrace_profiler(void)
992 unregister_ftrace_graph();
995 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
996 .func = function_profile_call,
997 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
998 INIT_REGEX_LOCK(ftrace_profile_ops)
1001 static int register_ftrace_profiler(void)
1003 return register_ftrace_function(&ftrace_profile_ops);
1006 static void unregister_ftrace_profiler(void)
1008 unregister_ftrace_function(&ftrace_profile_ops);
1010 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1013 ftrace_profile_write(struct file *filp, const char __user *ubuf,
1014 size_t cnt, loff_t *ppos)
1019 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1025 mutex_lock(&ftrace_profile_lock);
1026 if (ftrace_profile_enabled ^ val) {
1028 ret = ftrace_profile_init();
1034 ret = register_ftrace_profiler();
1039 ftrace_profile_enabled = 1;
1041 ftrace_profile_enabled = 0;
1043 * unregister_ftrace_profiler calls stop_machine
1044 * so this acts like an synchronize_sched.
1046 unregister_ftrace_profiler();
1050 mutex_unlock(&ftrace_profile_lock);
1058 ftrace_profile_read(struct file *filp, char __user *ubuf,
1059 size_t cnt, loff_t *ppos)
1061 char buf[64]; /* big enough to hold a number */
1064 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1065 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1068 static const struct file_operations ftrace_profile_fops = {
1069 .open = tracing_open_generic,
1070 .read = ftrace_profile_read,
1071 .write = ftrace_profile_write,
1072 .llseek = default_llseek,
1075 /* used to initialize the real stat files */
1076 static struct tracer_stat function_stats __initdata = {
1077 .name = "functions",
1078 .stat_start = function_stat_start,
1079 .stat_next = function_stat_next,
1080 .stat_cmp = function_stat_cmp,
1081 .stat_headers = function_stat_headers,
1082 .stat_show = function_stat_show
1085 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1087 struct ftrace_profile_stat *stat;
1088 struct dentry *entry;
1093 for_each_possible_cpu(cpu) {
1094 stat = &per_cpu(ftrace_profile_stats, cpu);
1096 /* allocate enough for function name + cpu number */
1097 name = kmalloc(32, GFP_KERNEL);
1100 * The files created are permanent, if something happens
1101 * we still do not free memory.
1104 "Could not allocate stat file for cpu %d\n",
1108 stat->stat = function_stats;
1109 snprintf(name, 32, "function%d", cpu);
1110 stat->stat.name = name;
1111 ret = register_stat_tracer(&stat->stat);
1114 "Could not register function stat for cpu %d\n",
1121 entry = debugfs_create_file("function_profile_enabled", 0644,
1122 d_tracer, NULL, &ftrace_profile_fops);
1124 pr_warning("Could not create debugfs "
1125 "'function_profile_enabled' entry\n");
1128 #else /* CONFIG_FUNCTION_PROFILER */
1129 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1132 #endif /* CONFIG_FUNCTION_PROFILER */
1134 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1136 #ifdef CONFIG_DYNAMIC_FTRACE
1138 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1139 # error Dynamic ftrace depends on MCOUNT_RECORD
1142 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1144 struct ftrace_func_probe {
1145 struct hlist_node node;
1146 struct ftrace_probe_ops *ops;
1147 unsigned long flags;
1150 struct list_head free_list;
1153 struct ftrace_func_entry {
1154 struct hlist_node hlist;
1158 struct ftrace_hash {
1159 unsigned long size_bits;
1160 struct hlist_head *buckets;
1161 unsigned long count;
1162 struct rcu_head rcu;
1166 * We make these constant because no one should touch them,
1167 * but they are used as the default "empty hash", to avoid allocating
1168 * it all the time. These are in a read only section such that if
1169 * anyone does try to modify it, it will cause an exception.
1171 static const struct hlist_head empty_buckets[1];
1172 static const struct ftrace_hash empty_hash = {
1173 .buckets = (struct hlist_head *)empty_buckets,
1175 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1177 static struct ftrace_ops global_ops = {
1178 .func = ftrace_stub,
1179 .notrace_hash = EMPTY_HASH,
1180 .filter_hash = EMPTY_HASH,
1181 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1182 INIT_REGEX_LOCK(global_ops)
1185 struct ftrace_page {
1186 struct ftrace_page *next;
1187 struct dyn_ftrace *records;
1192 static struct ftrace_page *ftrace_new_pgs;
1194 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1195 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1197 /* estimate from running different kernels */
1198 #define NR_TO_INIT 10000
1200 static struct ftrace_page *ftrace_pages_start;
1201 static struct ftrace_page *ftrace_pages;
1203 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1205 return !hash || !hash->count;
1208 static struct ftrace_func_entry *
1209 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1212 struct ftrace_func_entry *entry;
1213 struct hlist_head *hhd;
1215 if (ftrace_hash_empty(hash))
1218 if (hash->size_bits > 0)
1219 key = hash_long(ip, hash->size_bits);
1223 hhd = &hash->buckets[key];
1225 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1226 if (entry->ip == ip)
1232 static void __add_hash_entry(struct ftrace_hash *hash,
1233 struct ftrace_func_entry *entry)
1235 struct hlist_head *hhd;
1238 if (hash->size_bits)
1239 key = hash_long(entry->ip, hash->size_bits);
1243 hhd = &hash->buckets[key];
1244 hlist_add_head(&entry->hlist, hhd);
1248 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1250 struct ftrace_func_entry *entry;
1252 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1257 __add_hash_entry(hash, entry);
1263 free_hash_entry(struct ftrace_hash *hash,
1264 struct ftrace_func_entry *entry)
1266 hlist_del(&entry->hlist);
1272 remove_hash_entry(struct ftrace_hash *hash,
1273 struct ftrace_func_entry *entry)
1275 hlist_del(&entry->hlist);
1279 static void ftrace_hash_clear(struct ftrace_hash *hash)
1281 struct hlist_head *hhd;
1282 struct hlist_node *tn;
1283 struct ftrace_func_entry *entry;
1284 int size = 1 << hash->size_bits;
1290 for (i = 0; i < size; i++) {
1291 hhd = &hash->buckets[i];
1292 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1293 free_hash_entry(hash, entry);
1295 FTRACE_WARN_ON(hash->count);
1298 static void free_ftrace_hash(struct ftrace_hash *hash)
1300 if (!hash || hash == EMPTY_HASH)
1302 ftrace_hash_clear(hash);
1303 kfree(hash->buckets);
1307 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1309 struct ftrace_hash *hash;
1311 hash = container_of(rcu, struct ftrace_hash, rcu);
1312 free_ftrace_hash(hash);
1315 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1317 if (!hash || hash == EMPTY_HASH)
1319 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1322 void ftrace_free_filter(struct ftrace_ops *ops)
1324 ftrace_ops_init(ops);
1325 free_ftrace_hash(ops->filter_hash);
1326 free_ftrace_hash(ops->notrace_hash);
1329 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1331 struct ftrace_hash *hash;
1334 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1338 size = 1 << size_bits;
1339 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1341 if (!hash->buckets) {
1346 hash->size_bits = size_bits;
1351 static struct ftrace_hash *
1352 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1354 struct ftrace_func_entry *entry;
1355 struct ftrace_hash *new_hash;
1360 new_hash = alloc_ftrace_hash(size_bits);
1365 if (ftrace_hash_empty(hash))
1368 size = 1 << hash->size_bits;
1369 for (i = 0; i < size; i++) {
1370 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1371 ret = add_hash_entry(new_hash, entry->ip);
1377 FTRACE_WARN_ON(new_hash->count != hash->count);
1382 free_ftrace_hash(new_hash);
1387 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1389 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1392 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1393 struct ftrace_hash **dst, struct ftrace_hash *src)
1395 struct ftrace_func_entry *entry;
1396 struct hlist_node *tn;
1397 struct hlist_head *hhd;
1398 struct ftrace_hash *old_hash;
1399 struct ftrace_hash *new_hash;
1400 int size = src->count;
1406 * Remove the current set, update the hash and add
1409 ftrace_hash_rec_disable(ops, enable);
1412 * If the new source is empty, just free dst and assign it
1416 free_ftrace_hash_rcu(*dst);
1417 rcu_assign_pointer(*dst, EMPTY_HASH);
1418 /* still need to update the function records */
1424 * Make the hash size about 1/2 the # found
1426 for (size /= 2; size; size >>= 1)
1429 /* Don't allocate too much */
1430 if (bits > FTRACE_HASH_MAX_BITS)
1431 bits = FTRACE_HASH_MAX_BITS;
1434 new_hash = alloc_ftrace_hash(bits);
1438 size = 1 << src->size_bits;
1439 for (i = 0; i < size; i++) {
1440 hhd = &src->buckets[i];
1441 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1442 remove_hash_entry(src, entry);
1443 __add_hash_entry(new_hash, entry);
1448 rcu_assign_pointer(*dst, new_hash);
1449 free_ftrace_hash_rcu(old_hash);
1454 * Enable regardless of ret:
1455 * On success, we enable the new hash.
1456 * On failure, we re-enable the original hash.
1458 ftrace_hash_rec_enable(ops, enable);
1464 * Test the hashes for this ops to see if we want to call
1465 * the ops->func or not.
1467 * It's a match if the ip is in the ops->filter_hash or
1468 * the filter_hash does not exist or is empty,
1470 * the ip is not in the ops->notrace_hash.
1472 * This needs to be called with preemption disabled as
1473 * the hashes are freed with call_rcu_sched().
1476 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1478 struct ftrace_hash *filter_hash;
1479 struct ftrace_hash *notrace_hash;
1482 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1484 * There's a small race when adding ops that the ftrace handler
1485 * that wants regs, may be called without them. We can not
1486 * allow that handler to be called if regs is NULL.
1488 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1492 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1493 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1495 if ((ftrace_hash_empty(filter_hash) ||
1496 ftrace_lookup_ip(filter_hash, ip)) &&
1497 (ftrace_hash_empty(notrace_hash) ||
1498 !ftrace_lookup_ip(notrace_hash, ip)))
1507 * This is a double for. Do not use 'break' to break out of the loop,
1508 * you must use a goto.
1510 #define do_for_each_ftrace_rec(pg, rec) \
1511 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1513 for (_____i = 0; _____i < pg->index; _____i++) { \
1514 rec = &pg->records[_____i];
1516 #define while_for_each_ftrace_rec() \
1521 static int ftrace_cmp_recs(const void *a, const void *b)
1523 const struct dyn_ftrace *key = a;
1524 const struct dyn_ftrace *rec = b;
1526 if (key->flags < rec->ip)
1528 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1533 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1535 struct ftrace_page *pg;
1536 struct dyn_ftrace *rec;
1537 struct dyn_ftrace key;
1540 key.flags = end; /* overload flags, as it is unsigned long */
1542 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1543 if (end < pg->records[0].ip ||
1544 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1546 rec = bsearch(&key, pg->records, pg->index,
1547 sizeof(struct dyn_ftrace),
1557 * ftrace_location - return true if the ip giving is a traced location
1558 * @ip: the instruction pointer to check
1560 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1561 * That is, the instruction that is either a NOP or call to
1562 * the function tracer. It checks the ftrace internal tables to
1563 * determine if the address belongs or not.
1565 unsigned long ftrace_location(unsigned long ip)
1567 return ftrace_location_range(ip, ip);
1571 * ftrace_text_reserved - return true if range contains an ftrace location
1572 * @start: start of range to search
1573 * @end: end of range to search (inclusive). @end points to the last byte to check.
1575 * Returns 1 if @start and @end contains a ftrace location.
1576 * That is, the instruction that is either a NOP or call to
1577 * the function tracer. It checks the ftrace internal tables to
1578 * determine if the address belongs or not.
1580 int ftrace_text_reserved(void *start, void *end)
1584 ret = ftrace_location_range((unsigned long)start,
1585 (unsigned long)end);
1590 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1594 struct ftrace_hash *hash;
1595 struct ftrace_hash *other_hash;
1596 struct ftrace_page *pg;
1597 struct dyn_ftrace *rec;
1601 /* Only update if the ops has been registered */
1602 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1606 * In the filter_hash case:
1607 * If the count is zero, we update all records.
1608 * Otherwise we just update the items in the hash.
1610 * In the notrace_hash case:
1611 * We enable the update in the hash.
1612 * As disabling notrace means enabling the tracing,
1613 * and enabling notrace means disabling, the inc variable
1617 hash = ops->filter_hash;
1618 other_hash = ops->notrace_hash;
1619 if (ftrace_hash_empty(hash))
1623 hash = ops->notrace_hash;
1624 other_hash = ops->filter_hash;
1626 * If the notrace hash has no items,
1627 * then there's nothing to do.
1629 if (ftrace_hash_empty(hash))
1633 do_for_each_ftrace_rec(pg, rec) {
1634 int in_other_hash = 0;
1640 * Only the filter_hash affects all records.
1641 * Update if the record is not in the notrace hash.
1643 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1646 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1647 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1652 if (filter_hash && in_hash && !in_other_hash)
1654 else if (!filter_hash && in_hash &&
1655 (in_other_hash || ftrace_hash_empty(other_hash)))
1663 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1666 * If any ops wants regs saved for this function
1667 * then all ops will get saved regs.
1669 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1670 rec->flags |= FTRACE_FL_REGS;
1672 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1677 /* Shortcut, if we handled all records, we are done. */
1678 if (!all && count == hash->count)
1680 } while_for_each_ftrace_rec();
1683 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1686 __ftrace_hash_rec_update(ops, filter_hash, 0);
1689 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1692 __ftrace_hash_rec_update(ops, filter_hash, 1);
1695 static void print_ip_ins(const char *fmt, unsigned char *p)
1699 printk(KERN_CONT "%s", fmt);
1701 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1702 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1706 * ftrace_bug - report and shutdown function tracer
1707 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1708 * @ip: The address that failed
1710 * The arch code that enables or disables the function tracing
1711 * can call ftrace_bug() when it has detected a problem in
1712 * modifying the code. @failed should be one of either:
1713 * EFAULT - if the problem happens on reading the @ip address
1714 * EINVAL - if what is read at @ip is not what was expected
1715 * EPERM - if the problem happens on writting to the @ip address
1717 void ftrace_bug(int failed, unsigned long ip)
1721 FTRACE_WARN_ON_ONCE(1);
1722 pr_info("ftrace faulted on modifying ");
1726 FTRACE_WARN_ON_ONCE(1);
1727 pr_info("ftrace failed to modify ");
1729 print_ip_ins(" actual: ", (unsigned char *)ip);
1730 printk(KERN_CONT "\n");
1733 FTRACE_WARN_ON_ONCE(1);
1734 pr_info("ftrace faulted on writing ");
1738 FTRACE_WARN_ON_ONCE(1);
1739 pr_info("ftrace faulted on unknown error ");
1744 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1746 unsigned long flag = 0UL;
1749 * If we are updating calls:
1751 * If the record has a ref count, then we need to enable it
1752 * because someone is using it.
1754 * Otherwise we make sure its disabled.
1756 * If we are disabling calls, then disable all records that
1759 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1760 flag = FTRACE_FL_ENABLED;
1763 * If enabling and the REGS flag does not match the REGS_EN, then
1764 * do not ignore this record. Set flags to fail the compare against
1768 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1769 flag |= FTRACE_FL_REGS;
1771 /* If the state of this record hasn't changed, then do nothing */
1772 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1773 return FTRACE_UPDATE_IGNORE;
1776 /* Save off if rec is being enabled (for return value) */
1777 flag ^= rec->flags & FTRACE_FL_ENABLED;
1780 rec->flags |= FTRACE_FL_ENABLED;
1781 if (flag & FTRACE_FL_REGS) {
1782 if (rec->flags & FTRACE_FL_REGS)
1783 rec->flags |= FTRACE_FL_REGS_EN;
1785 rec->flags &= ~FTRACE_FL_REGS_EN;
1790 * If this record is being updated from a nop, then
1791 * return UPDATE_MAKE_CALL.
1792 * Otherwise, if the EN flag is set, then return
1793 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1794 * from the non-save regs, to a save regs function.
1796 * return UPDATE_MODIFY_CALL to tell the caller to convert
1797 * from the save regs, to a non-save regs function.
1799 if (flag & FTRACE_FL_ENABLED)
1800 return FTRACE_UPDATE_MAKE_CALL;
1801 else if (rec->flags & FTRACE_FL_REGS_EN)
1802 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1804 return FTRACE_UPDATE_MODIFY_CALL;
1808 /* If there's no more users, clear all flags */
1809 if (!(rec->flags & ~FTRACE_FL_MASK))
1812 /* Just disable the record (keep REGS state) */
1813 rec->flags &= ~FTRACE_FL_ENABLED;
1816 return FTRACE_UPDATE_MAKE_NOP;
1820 * ftrace_update_record, set a record that now is tracing or not
1821 * @rec: the record to update
1822 * @enable: set to 1 if the record is tracing, zero to force disable
1824 * The records that represent all functions that can be traced need
1825 * to be updated when tracing has been enabled.
1827 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1829 return ftrace_check_record(rec, enable, 1);
1833 * ftrace_test_record, check if the record has been enabled or not
1834 * @rec: the record to test
1835 * @enable: set to 1 to check if enabled, 0 if it is disabled
1837 * The arch code may need to test if a record is already set to
1838 * tracing to determine how to modify the function code that it
1841 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1843 return ftrace_check_record(rec, enable, 0);
1847 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1849 unsigned long ftrace_old_addr;
1850 unsigned long ftrace_addr;
1853 ret = ftrace_update_record(rec, enable);
1855 if (rec->flags & FTRACE_FL_REGS)
1856 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1858 ftrace_addr = (unsigned long)FTRACE_ADDR;
1861 case FTRACE_UPDATE_IGNORE:
1864 case FTRACE_UPDATE_MAKE_CALL:
1865 return ftrace_make_call(rec, ftrace_addr);
1867 case FTRACE_UPDATE_MAKE_NOP:
1868 return ftrace_make_nop(NULL, rec, ftrace_addr);
1870 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1871 case FTRACE_UPDATE_MODIFY_CALL:
1872 if (rec->flags & FTRACE_FL_REGS)
1873 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1875 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1877 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1880 return -1; /* unknow ftrace bug */
1883 void __weak ftrace_replace_code(int enable)
1885 struct dyn_ftrace *rec;
1886 struct ftrace_page *pg;
1889 if (unlikely(ftrace_disabled))
1892 do_for_each_ftrace_rec(pg, rec) {
1893 failed = __ftrace_replace_code(rec, enable);
1895 ftrace_bug(failed, rec->ip);
1896 /* Stop processing */
1899 } while_for_each_ftrace_rec();
1902 struct ftrace_rec_iter {
1903 struct ftrace_page *pg;
1908 * ftrace_rec_iter_start, start up iterating over traced functions
1910 * Returns an iterator handle that is used to iterate over all
1911 * the records that represent address locations where functions
1914 * May return NULL if no records are available.
1916 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1919 * We only use a single iterator.
1920 * Protected by the ftrace_lock mutex.
1922 static struct ftrace_rec_iter ftrace_rec_iter;
1923 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1925 iter->pg = ftrace_pages_start;
1928 /* Could have empty pages */
1929 while (iter->pg && !iter->pg->index)
1930 iter->pg = iter->pg->next;
1939 * ftrace_rec_iter_next, get the next record to process.
1940 * @iter: The handle to the iterator.
1942 * Returns the next iterator after the given iterator @iter.
1944 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1948 if (iter->index >= iter->pg->index) {
1949 iter->pg = iter->pg->next;
1952 /* Could have empty pages */
1953 while (iter->pg && !iter->pg->index)
1954 iter->pg = iter->pg->next;
1964 * ftrace_rec_iter_record, get the record at the iterator location
1965 * @iter: The current iterator location
1967 * Returns the record that the current @iter is at.
1969 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1971 return &iter->pg->records[iter->index];
1975 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1982 if (unlikely(ftrace_disabled))
1985 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1987 ftrace_bug(ret, ip);
1994 * archs can override this function if they must do something
1995 * before the modifying code is performed.
1997 int __weak ftrace_arch_code_modify_prepare(void)
2003 * archs can override this function if they must do something
2004 * after the modifying code is performed.
2006 int __weak ftrace_arch_code_modify_post_process(void)
2011 void ftrace_modify_all_code(int command)
2013 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2016 * If the ftrace_caller calls a ftrace_ops func directly,
2017 * we need to make sure that it only traces functions it
2018 * expects to trace. When doing the switch of functions,
2019 * we need to update to the ftrace_ops_list_func first
2020 * before the transition between old and new calls are set,
2021 * as the ftrace_ops_list_func will check the ops hashes
2022 * to make sure the ops are having the right functions
2026 ftrace_update_ftrace_func(ftrace_ops_list_func);
2028 if (command & FTRACE_UPDATE_CALLS)
2029 ftrace_replace_code(1);
2030 else if (command & FTRACE_DISABLE_CALLS)
2031 ftrace_replace_code(0);
2033 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2034 function_trace_op = set_function_trace_op;
2036 /* If irqs are disabled, we are in stop machine */
2037 if (!irqs_disabled())
2038 smp_call_function(ftrace_sync_ipi, NULL, 1);
2039 ftrace_update_ftrace_func(ftrace_trace_function);
2042 if (command & FTRACE_START_FUNC_RET)
2043 ftrace_enable_ftrace_graph_caller();
2044 else if (command & FTRACE_STOP_FUNC_RET)
2045 ftrace_disable_ftrace_graph_caller();
2048 static int __ftrace_modify_code(void *data)
2050 int *command = data;
2052 ftrace_modify_all_code(*command);
2058 * ftrace_run_stop_machine, go back to the stop machine method
2059 * @command: The command to tell ftrace what to do
2061 * If an arch needs to fall back to the stop machine method, the
2062 * it can call this function.
2064 void ftrace_run_stop_machine(int command)
2066 stop_machine(__ftrace_modify_code, &command, NULL);
2070 * arch_ftrace_update_code, modify the code to trace or not trace
2071 * @command: The command that needs to be done
2073 * Archs can override this function if it does not need to
2074 * run stop_machine() to modify code.
2076 void __weak arch_ftrace_update_code(int command)
2078 ftrace_run_stop_machine(command);
2081 static void ftrace_run_update_code(int command)
2085 ret = ftrace_arch_code_modify_prepare();
2086 FTRACE_WARN_ON(ret);
2090 * Do not call function tracer while we update the code.
2091 * We are in stop machine.
2093 function_trace_stop++;
2096 * By default we use stop_machine() to modify the code.
2097 * But archs can do what ever they want as long as it
2098 * is safe. The stop_machine() is the safest, but also
2099 * produces the most overhead.
2101 arch_ftrace_update_code(command);
2103 function_trace_stop--;
2105 ret = ftrace_arch_code_modify_post_process();
2106 FTRACE_WARN_ON(ret);
2109 static ftrace_func_t saved_ftrace_func;
2110 static int ftrace_start_up;
2111 static int global_start_up;
2113 static void ftrace_startup_enable(int command)
2115 if (saved_ftrace_func != ftrace_trace_function) {
2116 saved_ftrace_func = ftrace_trace_function;
2117 command |= FTRACE_UPDATE_TRACE_FUNC;
2120 if (!command || !ftrace_enabled)
2123 ftrace_run_update_code(command);
2126 static int ftrace_startup(struct ftrace_ops *ops, int command)
2128 bool hash_enable = true;
2131 if (unlikely(ftrace_disabled))
2134 ret = __register_ftrace_function(ops);
2139 command |= FTRACE_UPDATE_CALLS;
2141 /* ops marked global share the filter hashes */
2142 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2144 /* Don't update hash if global is already set */
2145 if (global_start_up)
2146 hash_enable = false;
2150 ops->flags |= FTRACE_OPS_FL_ENABLED;
2152 ftrace_hash_rec_enable(ops, 1);
2154 ftrace_startup_enable(command);
2159 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2161 bool hash_disable = true;
2164 if (unlikely(ftrace_disabled))
2167 ret = __unregister_ftrace_function(ops);
2173 * Just warn in case of unbalance, no need to kill ftrace, it's not
2174 * critical but the ftrace_call callers may be never nopped again after
2175 * further ftrace uses.
2177 WARN_ON_ONCE(ftrace_start_up < 0);
2179 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2182 WARN_ON_ONCE(global_start_up < 0);
2183 /* Don't update hash if global still has users */
2184 if (global_start_up) {
2185 WARN_ON_ONCE(!ftrace_start_up);
2186 hash_disable = false;
2191 ftrace_hash_rec_disable(ops, 1);
2193 if (ops != &global_ops || !global_start_up)
2194 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2196 command |= FTRACE_UPDATE_CALLS;
2198 if (saved_ftrace_func != ftrace_trace_function) {
2199 saved_ftrace_func = ftrace_trace_function;
2200 command |= FTRACE_UPDATE_TRACE_FUNC;
2203 if (!command || !ftrace_enabled)
2206 ftrace_run_update_code(command);
2210 static void ftrace_startup_sysctl(void)
2212 if (unlikely(ftrace_disabled))
2215 /* Force update next time */
2216 saved_ftrace_func = NULL;
2217 /* ftrace_start_up is true if we want ftrace running */
2218 if (ftrace_start_up)
2219 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2222 static void ftrace_shutdown_sysctl(void)
2224 if (unlikely(ftrace_disabled))
2227 /* ftrace_start_up is true if ftrace is running */
2228 if (ftrace_start_up)
2229 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2232 static cycle_t ftrace_update_time;
2233 static unsigned long ftrace_update_cnt;
2234 unsigned long ftrace_update_tot_cnt;
2236 static inline int ops_traces_mod(struct ftrace_ops *ops)
2239 * Filter_hash being empty will default to trace module.
2240 * But notrace hash requires a test of individual module functions.
2242 return ftrace_hash_empty(ops->filter_hash) &&
2243 ftrace_hash_empty(ops->notrace_hash);
2247 * Check if the current ops references the record.
2249 * If the ops traces all functions, then it was already accounted for.
2250 * If the ops does not trace the current record function, skip it.
2251 * If the ops ignores the function via notrace filter, skip it.
2254 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2256 /* If ops isn't enabled, ignore it */
2257 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2260 /* If ops traces all mods, we already accounted for it */
2261 if (ops_traces_mod(ops))
2264 /* The function must be in the filter */
2265 if (!ftrace_hash_empty(ops->filter_hash) &&
2266 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2269 /* If in notrace hash, we ignore it too */
2270 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2276 static int referenced_filters(struct dyn_ftrace *rec)
2278 struct ftrace_ops *ops;
2281 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2282 if (ops_references_rec(ops, rec))
2289 static int ftrace_update_code(struct module *mod)
2291 struct ftrace_page *pg;
2292 struct dyn_ftrace *p;
2293 cycle_t start, stop;
2294 unsigned long ref = 0;
2299 * When adding a module, we need to check if tracers are
2300 * currently enabled and if they are set to trace all functions.
2301 * If they are, we need to enable the module functions as well
2302 * as update the reference counts for those function records.
2305 struct ftrace_ops *ops;
2307 for (ops = ftrace_ops_list;
2308 ops != &ftrace_list_end; ops = ops->next) {
2309 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2310 if (ops_traces_mod(ops))
2318 start = ftrace_now(raw_smp_processor_id());
2319 ftrace_update_cnt = 0;
2321 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2323 for (i = 0; i < pg->index; i++) {
2326 /* If something went wrong, bail without enabling anything */
2327 if (unlikely(ftrace_disabled))
2330 p = &pg->records[i];
2332 cnt += referenced_filters(p);
2336 * Do the initial record conversion from mcount jump
2337 * to the NOP instructions.
2339 if (!ftrace_code_disable(mod, p))
2342 ftrace_update_cnt++;
2345 * If the tracing is enabled, go ahead and enable the record.
2347 * The reason not to enable the record immediatelly is the
2348 * inherent check of ftrace_make_nop/ftrace_make_call for
2349 * correct previous instructions. Making first the NOP
2350 * conversion puts the module to the correct state, thus
2351 * passing the ftrace_make_call check.
2353 if (ftrace_start_up && cnt) {
2354 int failed = __ftrace_replace_code(p, 1);
2356 ftrace_bug(failed, p->ip);
2361 ftrace_new_pgs = NULL;
2363 stop = ftrace_now(raw_smp_processor_id());
2364 ftrace_update_time = stop - start;
2365 ftrace_update_tot_cnt += ftrace_update_cnt;
2370 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2375 if (WARN_ON(!count))
2378 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2381 * We want to fill as much as possible. No more than a page
2384 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2388 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2391 /* if we can't allocate this size, try something smaller */
2398 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2407 static struct ftrace_page *
2408 ftrace_allocate_pages(unsigned long num_to_init)
2410 struct ftrace_page *start_pg;
2411 struct ftrace_page *pg;
2418 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2423 * Try to allocate as much as possible in one continues
2424 * location that fills in all of the space. We want to
2425 * waste as little space as possible.
2428 cnt = ftrace_allocate_records(pg, num_to_init);
2436 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2447 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2448 free_pages((unsigned long)pg->records, order);
2449 start_pg = pg->next;
2453 pr_info("ftrace: FAILED to allocate memory for functions\n");
2457 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2462 pr_info("ftrace: No functions to be traced?\n");
2466 cnt = num_to_init / ENTRIES_PER_PAGE;
2467 pr_info("ftrace: allocating %ld entries in %d pages\n",
2468 num_to_init, cnt + 1);
2473 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2475 struct ftrace_iterator {
2478 struct ftrace_page *pg;
2479 struct dyn_ftrace *func;
2480 struct ftrace_func_probe *probe;
2481 struct trace_parser parser;
2482 struct ftrace_hash *hash;
2483 struct ftrace_ops *ops;
2490 t_hash_next(struct seq_file *m, loff_t *pos)
2492 struct ftrace_iterator *iter = m->private;
2493 struct hlist_node *hnd = NULL;
2494 struct hlist_head *hhd;
2500 hnd = &iter->probe->node;
2502 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2505 hhd = &ftrace_func_hash[iter->hidx];
2507 if (hlist_empty(hhd)) {
2523 if (WARN_ON_ONCE(!hnd))
2526 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2531 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2533 struct ftrace_iterator *iter = m->private;
2537 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2540 if (iter->func_pos > *pos)
2544 for (l = 0; l <= (*pos - iter->func_pos); ) {
2545 p = t_hash_next(m, &l);
2552 /* Only set this if we have an item */
2553 iter->flags |= FTRACE_ITER_HASH;
2559 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2561 struct ftrace_func_probe *rec;
2564 if (WARN_ON_ONCE(!rec))
2567 if (rec->ops->print)
2568 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2570 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2573 seq_printf(m, ":%p", rec->data);
2580 t_next(struct seq_file *m, void *v, loff_t *pos)
2582 struct ftrace_iterator *iter = m->private;
2583 struct ftrace_ops *ops = iter->ops;
2584 struct dyn_ftrace *rec = NULL;
2586 if (unlikely(ftrace_disabled))
2589 if (iter->flags & FTRACE_ITER_HASH)
2590 return t_hash_next(m, pos);
2593 iter->pos = iter->func_pos = *pos;
2595 if (iter->flags & FTRACE_ITER_PRINTALL)
2596 return t_hash_start(m, pos);
2599 if (iter->idx >= iter->pg->index) {
2600 if (iter->pg->next) {
2601 iter->pg = iter->pg->next;
2606 rec = &iter->pg->records[iter->idx++];
2607 if (((iter->flags & FTRACE_ITER_FILTER) &&
2608 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2610 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2611 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2613 ((iter->flags & FTRACE_ITER_ENABLED) &&
2614 !(rec->flags & FTRACE_FL_ENABLED))) {
2622 return t_hash_start(m, pos);
2629 static void reset_iter_read(struct ftrace_iterator *iter)
2633 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2636 static void *t_start(struct seq_file *m, loff_t *pos)
2638 struct ftrace_iterator *iter = m->private;
2639 struct ftrace_ops *ops = iter->ops;
2643 mutex_lock(&ftrace_lock);
2645 if (unlikely(ftrace_disabled))
2649 * If an lseek was done, then reset and start from beginning.
2651 if (*pos < iter->pos)
2652 reset_iter_read(iter);
2655 * For set_ftrace_filter reading, if we have the filter
2656 * off, we can short cut and just print out that all
2657 * functions are enabled.
2659 if (iter->flags & FTRACE_ITER_FILTER &&
2660 ftrace_hash_empty(ops->filter_hash)) {
2662 return t_hash_start(m, pos);
2663 iter->flags |= FTRACE_ITER_PRINTALL;
2664 /* reset in case of seek/pread */
2665 iter->flags &= ~FTRACE_ITER_HASH;
2669 if (iter->flags & FTRACE_ITER_HASH)
2670 return t_hash_start(m, pos);
2673 * Unfortunately, we need to restart at ftrace_pages_start
2674 * every time we let go of the ftrace_mutex. This is because
2675 * those pointers can change without the lock.
2677 iter->pg = ftrace_pages_start;
2679 for (l = 0; l <= *pos; ) {
2680 p = t_next(m, p, &l);
2686 return t_hash_start(m, pos);
2691 static void t_stop(struct seq_file *m, void *p)
2693 mutex_unlock(&ftrace_lock);
2696 static int t_show(struct seq_file *m, void *v)
2698 struct ftrace_iterator *iter = m->private;
2699 struct dyn_ftrace *rec;
2701 if (iter->flags & FTRACE_ITER_HASH)
2702 return t_hash_show(m, iter);
2704 if (iter->flags & FTRACE_ITER_PRINTALL) {
2705 seq_printf(m, "#### all functions enabled ####\n");
2714 seq_printf(m, "%ps", (void *)rec->ip);
2715 if (iter->flags & FTRACE_ITER_ENABLED)
2716 seq_printf(m, " (%ld)%s",
2717 rec->flags & ~FTRACE_FL_MASK,
2718 rec->flags & FTRACE_FL_REGS ? " R" : "");
2719 seq_printf(m, "\n");
2724 static const struct seq_operations show_ftrace_seq_ops = {
2732 ftrace_avail_open(struct inode *inode, struct file *file)
2734 struct ftrace_iterator *iter;
2736 if (unlikely(ftrace_disabled))
2739 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2741 iter->pg = ftrace_pages_start;
2742 iter->ops = &global_ops;
2745 return iter ? 0 : -ENOMEM;
2749 ftrace_enabled_open(struct inode *inode, struct file *file)
2751 struct ftrace_iterator *iter;
2753 if (unlikely(ftrace_disabled))
2756 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2758 iter->pg = ftrace_pages_start;
2759 iter->flags = FTRACE_ITER_ENABLED;
2760 iter->ops = &global_ops;
2763 return iter ? 0 : -ENOMEM;
2766 static void ftrace_filter_reset(struct ftrace_hash *hash)
2768 mutex_lock(&ftrace_lock);
2769 ftrace_hash_clear(hash);
2770 mutex_unlock(&ftrace_lock);
2774 * ftrace_regex_open - initialize function tracer filter files
2775 * @ops: The ftrace_ops that hold the hash filters
2776 * @flag: The type of filter to process
2777 * @inode: The inode, usually passed in to your open routine
2778 * @file: The file, usually passed in to your open routine
2780 * ftrace_regex_open() initializes the filter files for the
2781 * @ops. Depending on @flag it may process the filter hash or
2782 * the notrace hash of @ops. With this called from the open
2783 * routine, you can use ftrace_filter_write() for the write
2784 * routine if @flag has FTRACE_ITER_FILTER set, or
2785 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2786 * tracing_lseek() should be used as the lseek routine, and
2787 * release must call ftrace_regex_release().
2790 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2791 struct inode *inode, struct file *file)
2793 struct ftrace_iterator *iter;
2794 struct ftrace_hash *hash;
2797 ftrace_ops_init(ops);
2799 if (unlikely(ftrace_disabled))
2802 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2806 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2814 mutex_lock(&ops->regex_lock);
2816 if (flag & FTRACE_ITER_NOTRACE)
2817 hash = ops->notrace_hash;
2819 hash = ops->filter_hash;
2821 if (file->f_mode & FMODE_WRITE) {
2822 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2824 trace_parser_put(&iter->parser);
2831 if ((file->f_mode & FMODE_WRITE) &&
2832 (file->f_flags & O_TRUNC))
2833 ftrace_filter_reset(iter->hash);
2835 if (file->f_mode & FMODE_READ) {
2836 iter->pg = ftrace_pages_start;
2838 ret = seq_open(file, &show_ftrace_seq_ops);
2840 struct seq_file *m = file->private_data;
2844 free_ftrace_hash(iter->hash);
2845 trace_parser_put(&iter->parser);
2849 file->private_data = iter;
2852 mutex_unlock(&ops->regex_lock);
2858 ftrace_filter_open(struct inode *inode, struct file *file)
2860 return ftrace_regex_open(&global_ops,
2861 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2866 ftrace_notrace_open(struct inode *inode, struct file *file)
2868 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2872 static int ftrace_match(char *str, char *regex, int len, int type)
2879 if (strcmp(str, regex) == 0)
2882 case MATCH_FRONT_ONLY:
2883 if (strncmp(str, regex, len) == 0)
2886 case MATCH_MIDDLE_ONLY:
2887 if (strstr(str, regex))
2890 case MATCH_END_ONLY:
2892 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2901 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2903 struct ftrace_func_entry *entry;
2906 entry = ftrace_lookup_ip(hash, rec->ip);
2908 /* Do nothing if it doesn't exist */
2912 free_hash_entry(hash, entry);
2914 /* Do nothing if it exists */
2918 ret = add_hash_entry(hash, rec->ip);
2924 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2925 char *regex, int len, int type)
2927 char str[KSYM_SYMBOL_LEN];
2930 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2933 /* module lookup requires matching the module */
2934 if (!modname || strcmp(modname, mod))
2937 /* blank search means to match all funcs in the mod */
2942 return ftrace_match(str, regex, len, type);
2946 match_records(struct ftrace_hash *hash, char *buff,
2947 int len, char *mod, int not)
2949 unsigned search_len = 0;
2950 struct ftrace_page *pg;
2951 struct dyn_ftrace *rec;
2952 int type = MATCH_FULL;
2953 char *search = buff;
2958 type = filter_parse_regex(buff, len, &search, ¬);
2959 search_len = strlen(search);
2962 mutex_lock(&ftrace_lock);
2964 if (unlikely(ftrace_disabled))
2967 do_for_each_ftrace_rec(pg, rec) {
2968 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2969 ret = enter_record(hash, rec, not);
2976 } while_for_each_ftrace_rec();
2978 mutex_unlock(&ftrace_lock);
2984 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2986 return match_records(hash, buff, len, NULL, 0);
2990 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2994 /* blank or '*' mean the same */
2995 if (strcmp(buff, "*") == 0)
2998 /* handle the case of 'dont filter this module' */
2999 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3004 return match_records(hash, buff, strlen(buff), mod, not);
3008 * We register the module command as a template to show others how
3009 * to register the a command as well.
3013 ftrace_mod_callback(struct ftrace_hash *hash,
3014 char *func, char *cmd, char *param, int enable)
3020 * cmd == 'mod' because we only registered this func
3021 * for the 'mod' ftrace_func_command.
3022 * But if you register one func with multiple commands,
3023 * you can tell which command was used by the cmd
3027 /* we must have a module name */
3031 mod = strsep(¶m, ":");
3035 ret = ftrace_match_module_records(hash, func, mod);
3044 static struct ftrace_func_command ftrace_mod_cmd = {
3046 .func = ftrace_mod_callback,
3049 static int __init ftrace_mod_cmd_init(void)
3051 return register_ftrace_command(&ftrace_mod_cmd);
3053 core_initcall(ftrace_mod_cmd_init);
3055 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3056 struct ftrace_ops *op, struct pt_regs *pt_regs)
3058 struct ftrace_func_probe *entry;
3059 struct hlist_head *hhd;
3062 key = hash_long(ip, FTRACE_HASH_BITS);
3064 hhd = &ftrace_func_hash[key];
3066 if (hlist_empty(hhd))
3070 * Disable preemption for these calls to prevent a RCU grace
3071 * period. This syncs the hash iteration and freeing of items
3072 * on the hash. rcu_read_lock is too dangerous here.
3074 preempt_disable_notrace();
3075 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3076 if (entry->ip == ip)
3077 entry->ops->func(ip, parent_ip, &entry->data);
3079 preempt_enable_notrace();
3082 static struct ftrace_ops trace_probe_ops __read_mostly =
3084 .func = function_trace_probe_call,
3085 .flags = FTRACE_OPS_FL_INITIALIZED,
3086 INIT_REGEX_LOCK(trace_probe_ops)
3089 static int ftrace_probe_registered;
3091 static void __enable_ftrace_function_probe(void)
3096 if (ftrace_probe_registered) {
3097 /* still need to update the function call sites */
3099 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3103 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3104 struct hlist_head *hhd = &ftrace_func_hash[i];
3108 /* Nothing registered? */
3109 if (i == FTRACE_FUNC_HASHSIZE)
3112 ret = ftrace_startup(&trace_probe_ops, 0);
3114 ftrace_probe_registered = 1;
3117 static void __disable_ftrace_function_probe(void)
3121 if (!ftrace_probe_registered)
3124 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3125 struct hlist_head *hhd = &ftrace_func_hash[i];
3130 /* no more funcs left */
3131 ftrace_shutdown(&trace_probe_ops, 0);
3133 ftrace_probe_registered = 0;
3137 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3139 if (entry->ops->free)
3140 entry->ops->free(entry->ops, entry->ip, &entry->data);
3145 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3148 struct ftrace_func_probe *entry;
3149 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3150 struct ftrace_hash *hash;
3151 struct ftrace_page *pg;
3152 struct dyn_ftrace *rec;
3159 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3160 len = strlen(search);
3162 /* we do not support '!' for function probes */
3166 mutex_lock(&trace_probe_ops.regex_lock);
3168 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3174 if (unlikely(ftrace_disabled)) {
3179 mutex_lock(&ftrace_lock);
3181 do_for_each_ftrace_rec(pg, rec) {
3183 if (!ftrace_match_record(rec, NULL, search, len, type))
3186 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3188 /* If we did not process any, then return error */
3199 * The caller might want to do something special
3200 * for each function we find. We call the callback
3201 * to give the caller an opportunity to do so.
3204 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3205 /* caller does not like this func */
3211 ret = enter_record(hash, rec, 0);
3219 entry->ip = rec->ip;
3221 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3222 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3224 } while_for_each_ftrace_rec();
3226 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3230 __enable_ftrace_function_probe();
3233 mutex_unlock(&ftrace_lock);
3235 mutex_unlock(&trace_probe_ops.regex_lock);
3236 free_ftrace_hash(hash);
3242 PROBE_TEST_FUNC = 1,
3247 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3248 void *data, int flags)
3250 struct ftrace_func_entry *rec_entry;
3251 struct ftrace_func_probe *entry;
3252 struct ftrace_func_probe *p;
3253 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3254 struct list_head free_list;
3255 struct ftrace_hash *hash;
3256 struct hlist_node *tmp;
3257 char str[KSYM_SYMBOL_LEN];
3258 int type = MATCH_FULL;
3262 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3267 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3268 len = strlen(search);
3270 /* we do not support '!' for function probes */
3275 mutex_lock(&trace_probe_ops.regex_lock);
3277 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3279 /* Hmm, should report this somehow */
3282 INIT_LIST_HEAD(&free_list);
3284 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3285 struct hlist_head *hhd = &ftrace_func_hash[i];
3287 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3289 /* break up if statements for readability */
3290 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3293 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3296 /* do this last, since it is the most expensive */
3298 kallsyms_lookup(entry->ip, NULL, NULL,
3300 if (!ftrace_match(str, glob, len, type))
3304 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3305 /* It is possible more than one entry had this ip */
3307 free_hash_entry(hash, rec_entry);
3309 hlist_del_rcu(&entry->node);
3310 list_add(&entry->free_list, &free_list);
3313 mutex_lock(&ftrace_lock);
3314 __disable_ftrace_function_probe();
3316 * Remove after the disable is called. Otherwise, if the last
3317 * probe is removed, a null hash means *all enabled*.
3319 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3320 synchronize_sched();
3321 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3322 list_del(&entry->free_list);
3323 ftrace_free_entry(entry);
3325 mutex_unlock(&ftrace_lock);
3328 mutex_unlock(&trace_probe_ops.regex_lock);
3329 free_ftrace_hash(hash);
3333 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3336 __unregister_ftrace_function_probe(glob, ops, data,
3337 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3341 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3343 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3346 void unregister_ftrace_function_probe_all(char *glob)
3348 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3351 static LIST_HEAD(ftrace_commands);
3352 static DEFINE_MUTEX(ftrace_cmd_mutex);
3355 * Currently we only register ftrace commands from __init, so mark this
3358 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3360 struct ftrace_func_command *p;
3363 mutex_lock(&ftrace_cmd_mutex);
3364 list_for_each_entry(p, &ftrace_commands, list) {
3365 if (strcmp(cmd->name, p->name) == 0) {
3370 list_add(&cmd->list, &ftrace_commands);
3372 mutex_unlock(&ftrace_cmd_mutex);
3378 * Currently we only unregister ftrace commands from __init, so mark
3381 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3383 struct ftrace_func_command *p, *n;
3386 mutex_lock(&ftrace_cmd_mutex);
3387 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3388 if (strcmp(cmd->name, p->name) == 0) {
3390 list_del_init(&p->list);
3395 mutex_unlock(&ftrace_cmd_mutex);
3400 static int ftrace_process_regex(struct ftrace_hash *hash,
3401 char *buff, int len, int enable)
3403 char *func, *command, *next = buff;
3404 struct ftrace_func_command *p;
3407 func = strsep(&next, ":");
3410 ret = ftrace_match_records(hash, func, len);
3420 command = strsep(&next, ":");
3422 mutex_lock(&ftrace_cmd_mutex);
3423 list_for_each_entry(p, &ftrace_commands, list) {
3424 if (strcmp(p->name, command) == 0) {
3425 ret = p->func(hash, func, command, next, enable);
3430 mutex_unlock(&ftrace_cmd_mutex);
3436 ftrace_regex_write(struct file *file, const char __user *ubuf,
3437 size_t cnt, loff_t *ppos, int enable)
3439 struct ftrace_iterator *iter;
3440 struct trace_parser *parser;
3446 if (file->f_mode & FMODE_READ) {
3447 struct seq_file *m = file->private_data;
3450 iter = file->private_data;
3452 if (unlikely(ftrace_disabled))
3455 /* iter->hash is a local copy, so we don't need regex_lock */
3457 parser = &iter->parser;
3458 read = trace_get_user(parser, ubuf, cnt, ppos);
3460 if (read >= 0 && trace_parser_loaded(parser) &&
3461 !trace_parser_cont(parser)) {
3462 ret = ftrace_process_regex(iter->hash, parser->buffer,
3463 parser->idx, enable);
3464 trace_parser_clear(parser);
3475 ftrace_filter_write(struct file *file, const char __user *ubuf,
3476 size_t cnt, loff_t *ppos)
3478 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3482 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3483 size_t cnt, loff_t *ppos)
3485 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3489 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3491 struct ftrace_func_entry *entry;
3493 if (!ftrace_location(ip))
3497 entry = ftrace_lookup_ip(hash, ip);
3500 free_hash_entry(hash, entry);
3504 return add_hash_entry(hash, ip);
3507 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3509 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3510 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3514 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3515 unsigned long ip, int remove, int reset, int enable)
3517 struct ftrace_hash **orig_hash;
3518 struct ftrace_hash *hash;
3521 /* All global ops uses the global ops filters */
3522 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3525 if (unlikely(ftrace_disabled))
3528 mutex_lock(&ops->regex_lock);
3531 orig_hash = &ops->filter_hash;
3533 orig_hash = &ops->notrace_hash;
3535 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3538 goto out_regex_unlock;
3542 ftrace_filter_reset(hash);
3543 if (buf && !ftrace_match_records(hash, buf, len)) {
3545 goto out_regex_unlock;
3548 ret = ftrace_match_addr(hash, ip, remove);
3550 goto out_regex_unlock;
3553 mutex_lock(&ftrace_lock);
3554 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3556 ftrace_ops_update_code(ops);
3558 mutex_unlock(&ftrace_lock);
3561 mutex_unlock(&ops->regex_lock);
3563 free_ftrace_hash(hash);
3568 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3569 int reset, int enable)
3571 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3575 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3576 * @ops - the ops to set the filter with
3577 * @ip - the address to add to or remove from the filter.
3578 * @remove - non zero to remove the ip from the filter
3579 * @reset - non zero to reset all filters before applying this filter.
3581 * Filters denote which functions should be enabled when tracing is enabled
3582 * If @ip is NULL, it failes to update filter.
3584 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3585 int remove, int reset)
3587 ftrace_ops_init(ops);
3588 return ftrace_set_addr(ops, ip, remove, reset, 1);
3590 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3593 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3594 int reset, int enable)
3596 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3600 * ftrace_set_filter - set a function to filter on in ftrace
3601 * @ops - the ops to set the filter with
3602 * @buf - the string that holds the function filter text.
3603 * @len - the length of the string.
3604 * @reset - non zero to reset all filters before applying this filter.
3606 * Filters denote which functions should be enabled when tracing is enabled.
3607 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3609 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3612 ftrace_ops_init(ops);
3613 return ftrace_set_regex(ops, buf, len, reset, 1);
3615 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3618 * ftrace_set_notrace - set a function to not trace in ftrace
3619 * @ops - the ops to set the notrace filter with
3620 * @buf - the string that holds the function notrace text.
3621 * @len - the length of the string.
3622 * @reset - non zero to reset all filters before applying this filter.
3624 * Notrace Filters denote which functions should not be enabled when tracing
3625 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3628 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3631 ftrace_ops_init(ops);
3632 return ftrace_set_regex(ops, buf, len, reset, 0);
3634 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3636 * ftrace_set_filter - set a function to filter on in ftrace
3637 * @ops - the ops to set the filter with
3638 * @buf - the string that holds the function filter text.
3639 * @len - the length of the string.
3640 * @reset - non zero to reset all filters before applying this filter.
3642 * Filters denote which functions should be enabled when tracing is enabled.
3643 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3645 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3647 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3649 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3652 * ftrace_set_notrace - set a function to not trace in ftrace
3653 * @ops - the ops to set the notrace filter with
3654 * @buf - the string that holds the function notrace text.
3655 * @len - the length of the string.
3656 * @reset - non zero to reset all filters before applying this filter.
3658 * Notrace Filters denote which functions should not be enabled when tracing
3659 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3662 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3664 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3666 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3669 * command line interface to allow users to set filters on boot up.
3671 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3672 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3673 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3675 /* Used by function selftest to not test if filter is set */
3676 bool ftrace_filter_param __initdata;
3678 static int __init set_ftrace_notrace(char *str)
3680 ftrace_filter_param = true;
3681 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3684 __setup("ftrace_notrace=", set_ftrace_notrace);
3686 static int __init set_ftrace_filter(char *str)
3688 ftrace_filter_param = true;
3689 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3692 __setup("ftrace_filter=", set_ftrace_filter);
3694 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3695 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3696 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3698 static int __init set_graph_function(char *str)
3700 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3703 __setup("ftrace_graph_filter=", set_graph_function);
3705 static void __init set_ftrace_early_graph(char *buf)
3711 func = strsep(&buf, ",");
3712 /* we allow only one expression at a time */
3713 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3714 FTRACE_GRAPH_MAX_FUNCS, func);
3716 printk(KERN_DEBUG "ftrace: function %s not "
3717 "traceable\n", func);
3720 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3723 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3727 ftrace_ops_init(ops);
3730 func = strsep(&buf, ",");
3731 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3735 static void __init set_ftrace_early_filters(void)
3737 if (ftrace_filter_buf[0])
3738 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3739 if (ftrace_notrace_buf[0])
3740 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3741 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3742 if (ftrace_graph_buf[0])
3743 set_ftrace_early_graph(ftrace_graph_buf);
3744 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3747 int ftrace_regex_release(struct inode *inode, struct file *file)
3749 struct seq_file *m = (struct seq_file *)file->private_data;
3750 struct ftrace_iterator *iter;
3751 struct ftrace_hash **orig_hash;
3752 struct trace_parser *parser;
3756 if (file->f_mode & FMODE_READ) {
3758 seq_release(inode, file);
3760 iter = file->private_data;
3762 parser = &iter->parser;
3763 if (trace_parser_loaded(parser)) {
3764 parser->buffer[parser->idx] = 0;
3765 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3768 trace_parser_put(parser);
3770 mutex_lock(&iter->ops->regex_lock);
3772 if (file->f_mode & FMODE_WRITE) {
3773 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3776 orig_hash = &iter->ops->filter_hash;
3778 orig_hash = &iter->ops->notrace_hash;
3780 mutex_lock(&ftrace_lock);
3781 ret = ftrace_hash_move(iter->ops, filter_hash,
3782 orig_hash, iter->hash);
3784 ftrace_ops_update_code(iter->ops);
3786 mutex_unlock(&ftrace_lock);
3789 mutex_unlock(&iter->ops->regex_lock);
3790 free_ftrace_hash(iter->hash);
3796 static const struct file_operations ftrace_avail_fops = {
3797 .open = ftrace_avail_open,
3799 .llseek = seq_lseek,
3800 .release = seq_release_private,
3803 static const struct file_operations ftrace_enabled_fops = {
3804 .open = ftrace_enabled_open,
3806 .llseek = seq_lseek,
3807 .release = seq_release_private,
3810 static const struct file_operations ftrace_filter_fops = {
3811 .open = ftrace_filter_open,
3813 .write = ftrace_filter_write,
3814 .llseek = tracing_lseek,
3815 .release = ftrace_regex_release,
3818 static const struct file_operations ftrace_notrace_fops = {
3819 .open = ftrace_notrace_open,
3821 .write = ftrace_notrace_write,
3822 .llseek = tracing_lseek,
3823 .release = ftrace_regex_release,
3826 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3828 static DEFINE_MUTEX(graph_lock);
3830 int ftrace_graph_count;
3831 int ftrace_graph_notrace_count;
3832 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3833 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3835 struct ftrace_graph_data {
3836 unsigned long *table;
3839 const struct seq_operations *seq_ops;
3843 __g_next(struct seq_file *m, loff_t *pos)
3845 struct ftrace_graph_data *fgd = m->private;
3847 if (*pos >= *fgd->count)
3849 return &fgd->table[*pos];
3853 g_next(struct seq_file *m, void *v, loff_t *pos)
3856 return __g_next(m, pos);
3859 static void *g_start(struct seq_file *m, loff_t *pos)
3861 struct ftrace_graph_data *fgd = m->private;
3863 mutex_lock(&graph_lock);
3865 /* Nothing, tell g_show to print all functions are enabled */
3866 if (!*fgd->count && !*pos)
3869 return __g_next(m, pos);
3872 static void g_stop(struct seq_file *m, void *p)
3874 mutex_unlock(&graph_lock);
3877 static int g_show(struct seq_file *m, void *v)
3879 unsigned long *ptr = v;
3884 if (ptr == (unsigned long *)1) {
3885 seq_printf(m, "#### all functions enabled ####\n");
3889 seq_printf(m, "%ps\n", (void *)*ptr);
3894 static const struct seq_operations ftrace_graph_seq_ops = {
3902 __ftrace_graph_open(struct inode *inode, struct file *file,
3903 struct ftrace_graph_data *fgd)
3907 mutex_lock(&graph_lock);
3908 if ((file->f_mode & FMODE_WRITE) &&
3909 (file->f_flags & O_TRUNC)) {
3911 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
3913 mutex_unlock(&graph_lock);
3915 if (file->f_mode & FMODE_READ) {
3916 ret = seq_open(file, fgd->seq_ops);
3918 struct seq_file *m = file->private_data;
3922 file->private_data = fgd;
3928 ftrace_graph_open(struct inode *inode, struct file *file)
3930 struct ftrace_graph_data *fgd;
3932 if (unlikely(ftrace_disabled))
3935 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3939 fgd->table = ftrace_graph_funcs;
3940 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3941 fgd->count = &ftrace_graph_count;
3942 fgd->seq_ops = &ftrace_graph_seq_ops;
3944 return __ftrace_graph_open(inode, file, fgd);
3948 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3950 struct ftrace_graph_data *fgd;
3952 if (unlikely(ftrace_disabled))
3955 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3959 fgd->table = ftrace_graph_notrace_funcs;
3960 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3961 fgd->count = &ftrace_graph_notrace_count;
3962 fgd->seq_ops = &ftrace_graph_seq_ops;
3964 return __ftrace_graph_open(inode, file, fgd);
3968 ftrace_graph_release(struct inode *inode, struct file *file)
3970 if (file->f_mode & FMODE_READ) {
3971 struct seq_file *m = file->private_data;
3974 seq_release(inode, file);
3976 kfree(file->private_data);
3983 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
3985 struct dyn_ftrace *rec;
3986 struct ftrace_page *pg;
3995 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3996 if (!not && *idx >= size)
3999 search_len = strlen(search);
4001 mutex_lock(&ftrace_lock);
4003 if (unlikely(ftrace_disabled)) {
4004 mutex_unlock(&ftrace_lock);
4008 do_for_each_ftrace_rec(pg, rec) {
4010 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4011 /* if it is in the array */
4013 for (i = 0; i < *idx; i++) {
4014 if (array[i] == rec->ip) {
4023 array[(*idx)++] = rec->ip;
4029 array[i] = array[--(*idx)];
4035 } while_for_each_ftrace_rec();
4037 mutex_unlock(&ftrace_lock);
4046 ftrace_graph_write(struct file *file, const char __user *ubuf,
4047 size_t cnt, loff_t *ppos)
4049 struct trace_parser parser;
4050 ssize_t read, ret = 0;
4051 struct ftrace_graph_data *fgd = file->private_data;
4056 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4059 read = trace_get_user(&parser, ubuf, cnt, ppos);
4061 if (read >= 0 && trace_parser_loaded((&parser))) {
4062 parser.buffer[parser.idx] = 0;
4064 mutex_lock(&graph_lock);
4066 /* we allow only one expression at a time */
4067 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4070 mutex_unlock(&graph_lock);
4076 trace_parser_put(&parser);
4081 static const struct file_operations ftrace_graph_fops = {
4082 .open = ftrace_graph_open,
4084 .write = ftrace_graph_write,
4085 .llseek = tracing_lseek,
4086 .release = ftrace_graph_release,
4089 static const struct file_operations ftrace_graph_notrace_fops = {
4090 .open = ftrace_graph_notrace_open,
4092 .write = ftrace_graph_write,
4093 .llseek = tracing_lseek,
4094 .release = ftrace_graph_release,
4096 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4098 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4101 trace_create_file("available_filter_functions", 0444,
4102 d_tracer, NULL, &ftrace_avail_fops);
4104 trace_create_file("enabled_functions", 0444,
4105 d_tracer, NULL, &ftrace_enabled_fops);
4107 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4108 NULL, &ftrace_filter_fops);
4110 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
4111 NULL, &ftrace_notrace_fops);
4113 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4114 trace_create_file("set_graph_function", 0444, d_tracer,
4116 &ftrace_graph_fops);
4117 trace_create_file("set_graph_notrace", 0444, d_tracer,
4119 &ftrace_graph_notrace_fops);
4120 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4125 static int ftrace_cmp_ips(const void *a, const void *b)
4127 const unsigned long *ipa = a;
4128 const unsigned long *ipb = b;
4137 static void ftrace_swap_ips(void *a, void *b, int size)
4139 unsigned long *ipa = a;
4140 unsigned long *ipb = b;
4148 static int ftrace_process_locs(struct module *mod,
4149 unsigned long *start,
4152 struct ftrace_page *start_pg;
4153 struct ftrace_page *pg;
4154 struct dyn_ftrace *rec;
4155 unsigned long count;
4158 unsigned long flags = 0; /* Shut up gcc */
4161 count = end - start;
4166 sort(start, count, sizeof(*start),
4167 ftrace_cmp_ips, ftrace_swap_ips);
4169 start_pg = ftrace_allocate_pages(count);
4173 mutex_lock(&ftrace_lock);
4176 * Core and each module needs their own pages, as
4177 * modules will free them when they are removed.
4178 * Force a new page to be allocated for modules.
4181 WARN_ON(ftrace_pages || ftrace_pages_start);
4182 /* First initialization */
4183 ftrace_pages = ftrace_pages_start = start_pg;
4188 if (WARN_ON(ftrace_pages->next)) {
4189 /* Hmm, we have free pages? */
4190 while (ftrace_pages->next)
4191 ftrace_pages = ftrace_pages->next;
4194 ftrace_pages->next = start_pg;
4200 addr = ftrace_call_adjust(*p++);
4202 * Some architecture linkers will pad between
4203 * the different mcount_loc sections of different
4204 * object files to satisfy alignments.
4205 * Skip any NULL pointers.
4210 if (pg->index == pg->size) {
4211 /* We should have allocated enough */
4212 if (WARN_ON(!pg->next))
4217 rec = &pg->records[pg->index++];
4221 /* We should have used all pages */
4224 /* Assign the last page to ftrace_pages */
4227 /* These new locations need to be initialized */
4228 ftrace_new_pgs = start_pg;
4231 * We only need to disable interrupts on start up
4232 * because we are modifying code that an interrupt
4233 * may execute, and the modification is not atomic.
4234 * But for modules, nothing runs the code we modify
4235 * until we are finished with it, and there's no
4236 * reason to cause large interrupt latencies while we do it.
4239 local_irq_save(flags);
4240 ftrace_update_code(mod);
4242 local_irq_restore(flags);
4245 mutex_unlock(&ftrace_lock);
4250 #ifdef CONFIG_MODULES
4252 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4254 void ftrace_release_mod(struct module *mod)
4256 struct dyn_ftrace *rec;
4257 struct ftrace_page **last_pg;
4258 struct ftrace_page *pg;
4261 mutex_lock(&ftrace_lock);
4263 if (ftrace_disabled)
4267 * Each module has its own ftrace_pages, remove
4268 * them from the list.
4270 last_pg = &ftrace_pages_start;
4271 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4272 rec = &pg->records[0];
4273 if (within_module_core(rec->ip, mod)) {
4275 * As core pages are first, the first
4276 * page should never be a module page.
4278 if (WARN_ON(pg == ftrace_pages_start))
4281 /* Check if we are deleting the last page */
4282 if (pg == ftrace_pages)
4283 ftrace_pages = next_to_ftrace_page(last_pg);
4285 *last_pg = pg->next;
4286 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4287 free_pages((unsigned long)pg->records, order);
4290 last_pg = &pg->next;
4293 mutex_unlock(&ftrace_lock);
4296 static void ftrace_init_module(struct module *mod,
4297 unsigned long *start, unsigned long *end)
4299 if (ftrace_disabled || start == end)
4301 ftrace_process_locs(mod, start, end);
4304 static int ftrace_module_notify_enter(struct notifier_block *self,
4305 unsigned long val, void *data)
4307 struct module *mod = data;
4309 if (val == MODULE_STATE_COMING)
4310 ftrace_init_module(mod, mod->ftrace_callsites,
4311 mod->ftrace_callsites +
4312 mod->num_ftrace_callsites);
4316 static int ftrace_module_notify_exit(struct notifier_block *self,
4317 unsigned long val, void *data)
4319 struct module *mod = data;
4321 if (val == MODULE_STATE_GOING)
4322 ftrace_release_mod(mod);
4327 static int ftrace_module_notify_enter(struct notifier_block *self,
4328 unsigned long val, void *data)
4332 static int ftrace_module_notify_exit(struct notifier_block *self,
4333 unsigned long val, void *data)
4337 #endif /* CONFIG_MODULES */
4339 struct notifier_block ftrace_module_enter_nb = {
4340 .notifier_call = ftrace_module_notify_enter,
4341 .priority = INT_MAX, /* Run before anything that can use kprobes */
4344 struct notifier_block ftrace_module_exit_nb = {
4345 .notifier_call = ftrace_module_notify_exit,
4346 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4349 extern unsigned long __start_mcount_loc[];
4350 extern unsigned long __stop_mcount_loc[];
4352 void __init ftrace_init(void)
4354 unsigned long count, addr, flags;
4357 /* Keep the ftrace pointer to the stub */
4358 addr = (unsigned long)ftrace_stub;
4360 local_irq_save(flags);
4361 ftrace_dyn_arch_init(&addr);
4362 local_irq_restore(flags);
4364 /* ftrace_dyn_arch_init places the return code in addr */
4368 count = __stop_mcount_loc - __start_mcount_loc;
4370 ret = ftrace_dyn_table_alloc(count);
4374 last_ftrace_enabled = ftrace_enabled = 1;
4376 ret = ftrace_process_locs(NULL,
4380 ret = register_module_notifier(&ftrace_module_enter_nb);
4382 pr_warning("Failed to register trace ftrace module enter notifier\n");
4384 ret = register_module_notifier(&ftrace_module_exit_nb);
4386 pr_warning("Failed to register trace ftrace module exit notifier\n");
4388 set_ftrace_early_filters();
4392 ftrace_disabled = 1;
4397 static struct ftrace_ops global_ops = {
4398 .func = ftrace_stub,
4399 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4400 INIT_REGEX_LOCK(global_ops)
4403 static int __init ftrace_nodyn_init(void)
4408 core_initcall(ftrace_nodyn_init);
4410 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4411 static inline void ftrace_startup_enable(int command) { }
4412 /* Keep as macros so we do not need to define the commands */
4413 # define ftrace_startup(ops, command) \
4415 int ___ret = __register_ftrace_function(ops); \
4417 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4420 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4422 # define ftrace_startup_sysctl() do { } while (0)
4423 # define ftrace_shutdown_sysctl() do { } while (0)
4426 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4431 #endif /* CONFIG_DYNAMIC_FTRACE */
4434 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4435 struct ftrace_ops *op, struct pt_regs *regs)
4437 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4441 * Some of the ops may be dynamically allocated,
4442 * they must be freed after a synchronize_sched().
4444 preempt_disable_notrace();
4445 trace_recursion_set(TRACE_CONTROL_BIT);
4448 * Control funcs (perf) uses RCU. Only trace if
4449 * RCU is currently active.
4451 if (!rcu_is_watching())
4454 do_for_each_ftrace_op(op, ftrace_control_list) {
4455 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4456 !ftrace_function_local_disabled(op) &&
4457 ftrace_ops_test(op, ip, regs))
4458 op->func(ip, parent_ip, op, regs);
4459 } while_for_each_ftrace_op(op);
4461 trace_recursion_clear(TRACE_CONTROL_BIT);
4462 preempt_enable_notrace();
4465 static struct ftrace_ops control_ops = {
4466 .func = ftrace_ops_control_func,
4467 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4468 INIT_REGEX_LOCK(control_ops)
4472 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4473 struct ftrace_ops *ignored, struct pt_regs *regs)
4475 struct ftrace_ops *op;
4478 if (function_trace_stop)
4481 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4486 * Some of the ops may be dynamically allocated,
4487 * they must be freed after a synchronize_sched().
4489 preempt_disable_notrace();
4490 do_for_each_ftrace_op(op, ftrace_ops_list) {
4491 if (ftrace_ops_test(op, ip, regs))
4492 op->func(ip, parent_ip, op, regs);
4493 } while_for_each_ftrace_op(op);
4494 preempt_enable_notrace();
4495 trace_clear_recursion(bit);
4499 * Some archs only support passing ip and parent_ip. Even though
4500 * the list function ignores the op parameter, we do not want any
4501 * C side effects, where a function is called without the caller
4502 * sending a third parameter.
4503 * Archs are to support both the regs and ftrace_ops at the same time.
4504 * If they support ftrace_ops, it is assumed they support regs.
4505 * If call backs want to use regs, they must either check for regs
4506 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4507 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4508 * An architecture can pass partial regs with ftrace_ops and still
4509 * set the ARCH_SUPPORT_FTARCE_OPS.
4511 #if ARCH_SUPPORTS_FTRACE_OPS
4512 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4513 struct ftrace_ops *op, struct pt_regs *regs)
4515 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4518 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4520 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4524 static void clear_ftrace_swapper(void)
4526 struct task_struct *p;
4530 for_each_online_cpu(cpu) {
4532 clear_tsk_trace_trace(p);
4537 static void set_ftrace_swapper(void)
4539 struct task_struct *p;
4543 for_each_online_cpu(cpu) {
4545 set_tsk_trace_trace(p);
4550 static void clear_ftrace_pid(struct pid *pid)
4552 struct task_struct *p;
4555 do_each_pid_task(pid, PIDTYPE_PID, p) {
4556 clear_tsk_trace_trace(p);
4557 } while_each_pid_task(pid, PIDTYPE_PID, p);
4563 static void set_ftrace_pid(struct pid *pid)
4565 struct task_struct *p;
4568 do_each_pid_task(pid, PIDTYPE_PID, p) {
4569 set_tsk_trace_trace(p);
4570 } while_each_pid_task(pid, PIDTYPE_PID, p);
4574 static void clear_ftrace_pid_task(struct pid *pid)
4576 if (pid == ftrace_swapper_pid)
4577 clear_ftrace_swapper();
4579 clear_ftrace_pid(pid);
4582 static void set_ftrace_pid_task(struct pid *pid)
4584 if (pid == ftrace_swapper_pid)
4585 set_ftrace_swapper();
4587 set_ftrace_pid(pid);
4590 static int ftrace_pid_add(int p)
4593 struct ftrace_pid *fpid;
4596 mutex_lock(&ftrace_lock);
4599 pid = ftrace_swapper_pid;
4601 pid = find_get_pid(p);
4608 list_for_each_entry(fpid, &ftrace_pids, list)
4609 if (fpid->pid == pid)
4614 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4618 list_add(&fpid->list, &ftrace_pids);
4621 set_ftrace_pid_task(pid);
4623 ftrace_update_pid_func();
4624 ftrace_startup_enable(0);
4626 mutex_unlock(&ftrace_lock);
4630 if (pid != ftrace_swapper_pid)
4634 mutex_unlock(&ftrace_lock);
4638 static void ftrace_pid_reset(void)
4640 struct ftrace_pid *fpid, *safe;
4642 mutex_lock(&ftrace_lock);
4643 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4644 struct pid *pid = fpid->pid;
4646 clear_ftrace_pid_task(pid);
4648 list_del(&fpid->list);
4652 ftrace_update_pid_func();
4653 ftrace_startup_enable(0);
4655 mutex_unlock(&ftrace_lock);
4658 static void *fpid_start(struct seq_file *m, loff_t *pos)
4660 mutex_lock(&ftrace_lock);
4662 if (list_empty(&ftrace_pids) && (!*pos))
4665 return seq_list_start(&ftrace_pids, *pos);
4668 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4673 return seq_list_next(v, &ftrace_pids, pos);
4676 static void fpid_stop(struct seq_file *m, void *p)
4678 mutex_unlock(&ftrace_lock);
4681 static int fpid_show(struct seq_file *m, void *v)
4683 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4685 if (v == (void *)1) {
4686 seq_printf(m, "no pid\n");
4690 if (fpid->pid == ftrace_swapper_pid)
4691 seq_printf(m, "swapper tasks\n");
4693 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4698 static const struct seq_operations ftrace_pid_sops = {
4699 .start = fpid_start,
4706 ftrace_pid_open(struct inode *inode, struct file *file)
4710 if ((file->f_mode & FMODE_WRITE) &&
4711 (file->f_flags & O_TRUNC))
4714 if (file->f_mode & FMODE_READ)
4715 ret = seq_open(file, &ftrace_pid_sops);
4721 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4722 size_t cnt, loff_t *ppos)
4728 if (cnt >= sizeof(buf))
4731 if (copy_from_user(&buf, ubuf, cnt))
4737 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4738 * to clean the filter quietly.
4740 tmp = strstrip(buf);
4741 if (strlen(tmp) == 0)
4744 ret = kstrtol(tmp, 10, &val);
4748 ret = ftrace_pid_add(val);
4750 return ret ? ret : cnt;
4754 ftrace_pid_release(struct inode *inode, struct file *file)
4756 if (file->f_mode & FMODE_READ)
4757 seq_release(inode, file);
4762 static const struct file_operations ftrace_pid_fops = {
4763 .open = ftrace_pid_open,
4764 .write = ftrace_pid_write,
4766 .llseek = tracing_lseek,
4767 .release = ftrace_pid_release,
4770 static __init int ftrace_init_debugfs(void)
4772 struct dentry *d_tracer;
4774 d_tracer = tracing_init_dentry();
4778 ftrace_init_dyn_debugfs(d_tracer);
4780 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4781 NULL, &ftrace_pid_fops);
4783 ftrace_profile_debugfs(d_tracer);
4787 fs_initcall(ftrace_init_debugfs);
4790 * ftrace_kill - kill ftrace
4792 * This function should be used by panic code. It stops ftrace
4793 * but in a not so nice way. If you need to simply kill ftrace
4794 * from a non-atomic section, use ftrace_kill.
4796 void ftrace_kill(void)
4798 ftrace_disabled = 1;
4800 clear_ftrace_function();
4804 * Test if ftrace is dead or not.
4806 int ftrace_is_dead(void)
4808 return ftrace_disabled;
4812 * register_ftrace_function - register a function for profiling
4813 * @ops - ops structure that holds the function for profiling.
4815 * Register a function to be called by all functions in the
4818 * Note: @ops->func and all the functions it calls must be labeled
4819 * with "notrace", otherwise it will go into a
4822 int register_ftrace_function(struct ftrace_ops *ops)
4826 ftrace_ops_init(ops);
4828 mutex_lock(&ftrace_lock);
4830 ret = ftrace_startup(ops, 0);
4832 mutex_unlock(&ftrace_lock);
4836 EXPORT_SYMBOL_GPL(register_ftrace_function);
4839 * unregister_ftrace_function - unregister a function for profiling.
4840 * @ops - ops structure that holds the function to unregister
4842 * Unregister a function that was added to be called by ftrace profiling.
4844 int unregister_ftrace_function(struct ftrace_ops *ops)
4848 mutex_lock(&ftrace_lock);
4849 ret = ftrace_shutdown(ops, 0);
4850 mutex_unlock(&ftrace_lock);
4854 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4857 ftrace_enable_sysctl(struct ctl_table *table, int write,
4858 void __user *buffer, size_t *lenp,
4863 mutex_lock(&ftrace_lock);
4865 if (unlikely(ftrace_disabled))
4868 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4870 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4873 last_ftrace_enabled = !!ftrace_enabled;
4875 if (ftrace_enabled) {
4877 ftrace_startup_sysctl();
4879 /* we are starting ftrace again */
4880 if (ftrace_ops_list != &ftrace_list_end)
4881 update_ftrace_function();
4884 /* stopping ftrace calls (just send to ftrace_stub) */
4885 ftrace_trace_function = ftrace_stub;
4887 ftrace_shutdown_sysctl();
4891 mutex_unlock(&ftrace_lock);
4895 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4897 static int ftrace_graph_active;
4898 static struct notifier_block ftrace_suspend_notifier;
4900 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4905 /* The callbacks that hook a function */
4906 trace_func_graph_ret_t ftrace_graph_return =
4907 (trace_func_graph_ret_t)ftrace_stub;
4908 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4910 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4911 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4915 unsigned long flags;
4916 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4917 struct task_struct *g, *t;
4919 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4920 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4921 * sizeof(struct ftrace_ret_stack),
4923 if (!ret_stack_list[i]) {
4931 read_lock_irqsave(&tasklist_lock, flags);
4932 do_each_thread(g, t) {
4938 if (t->ret_stack == NULL) {
4939 atomic_set(&t->tracing_graph_pause, 0);
4940 atomic_set(&t->trace_overrun, 0);
4941 t->curr_ret_stack = -1;
4942 /* Make sure the tasks see the -1 first: */
4944 t->ret_stack = ret_stack_list[start++];
4946 } while_each_thread(g, t);
4949 read_unlock_irqrestore(&tasklist_lock, flags);
4951 for (i = start; i < end; i++)
4952 kfree(ret_stack_list[i]);
4957 ftrace_graph_probe_sched_switch(void *ignore,
4958 struct task_struct *prev, struct task_struct *next)
4960 unsigned long long timestamp;
4964 * Does the user want to count the time a function was asleep.
4965 * If so, do not update the time stamps.
4967 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4970 timestamp = trace_clock_local();
4972 prev->ftrace_timestamp = timestamp;
4974 /* only process tasks that we timestamped */
4975 if (!next->ftrace_timestamp)
4979 * Update all the counters in next to make up for the
4980 * time next was sleeping.
4982 timestamp -= next->ftrace_timestamp;
4984 for (index = next->curr_ret_stack; index >= 0; index--)
4985 next->ret_stack[index].calltime += timestamp;
4988 /* Allocate a return stack for each task */
4989 static int start_graph_tracing(void)
4991 struct ftrace_ret_stack **ret_stack_list;
4994 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4995 sizeof(struct ftrace_ret_stack *),
4998 if (!ret_stack_list)
5001 /* The cpu_boot init_task->ret_stack will never be freed */
5002 for_each_online_cpu(cpu) {
5003 if (!idle_task(cpu)->ret_stack)
5004 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5008 ret = alloc_retstack_tasklist(ret_stack_list);
5009 } while (ret == -EAGAIN);
5012 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5014 pr_info("ftrace_graph: Couldn't activate tracepoint"
5015 " probe to kernel_sched_switch\n");
5018 kfree(ret_stack_list);
5023 * Hibernation protection.
5024 * The state of the current task is too much unstable during
5025 * suspend/restore to disk. We want to protect against that.
5028 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5032 case PM_HIBERNATION_PREPARE:
5033 pause_graph_tracing();
5036 case PM_POST_HIBERNATION:
5037 unpause_graph_tracing();
5043 /* Just a place holder for function graph */
5044 static struct ftrace_ops fgraph_ops __read_mostly = {
5045 .func = ftrace_stub,
5046 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
5047 FTRACE_OPS_FL_RECURSION_SAFE,
5050 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5051 trace_func_graph_ent_t entryfunc)
5055 mutex_lock(&ftrace_lock);
5057 /* we currently allow only one tracer registered at a time */
5058 if (ftrace_graph_active) {
5063 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5064 register_pm_notifier(&ftrace_suspend_notifier);
5066 ftrace_graph_active++;
5067 ret = start_graph_tracing();
5069 ftrace_graph_active--;
5073 ftrace_graph_return = retfunc;
5074 ftrace_graph_entry = entryfunc;
5076 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
5079 mutex_unlock(&ftrace_lock);
5083 void unregister_ftrace_graph(void)
5085 mutex_lock(&ftrace_lock);
5087 if (unlikely(!ftrace_graph_active))
5090 ftrace_graph_active--;
5091 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5092 ftrace_graph_entry = ftrace_graph_entry_stub;
5093 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
5094 unregister_pm_notifier(&ftrace_suspend_notifier);
5095 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5098 mutex_unlock(&ftrace_lock);
5101 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5104 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5106 atomic_set(&t->tracing_graph_pause, 0);
5107 atomic_set(&t->trace_overrun, 0);
5108 t->ftrace_timestamp = 0;
5109 /* make curr_ret_stack visible before we add the ret_stack */
5111 t->ret_stack = ret_stack;
5115 * Allocate a return stack for the idle task. May be the first
5116 * time through, or it may be done by CPU hotplug online.
5118 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5120 t->curr_ret_stack = -1;
5122 * The idle task has no parent, it either has its own
5123 * stack or no stack at all.
5126 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5128 if (ftrace_graph_active) {
5129 struct ftrace_ret_stack *ret_stack;
5131 ret_stack = per_cpu(idle_ret_stack, cpu);
5133 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5134 * sizeof(struct ftrace_ret_stack),
5138 per_cpu(idle_ret_stack, cpu) = ret_stack;
5140 graph_init_task(t, ret_stack);
5144 /* Allocate a return stack for newly created task */
5145 void ftrace_graph_init_task(struct task_struct *t)
5147 /* Make sure we do not use the parent ret_stack */
5148 t->ret_stack = NULL;
5149 t->curr_ret_stack = -1;
5151 if (ftrace_graph_active) {
5152 struct ftrace_ret_stack *ret_stack;
5154 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5155 * sizeof(struct ftrace_ret_stack),
5159 graph_init_task(t, ret_stack);
5163 void ftrace_graph_exit_task(struct task_struct *t)
5165 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5167 t->ret_stack = NULL;
5168 /* NULL must become visible to IRQs before we free it: */
5174 void ftrace_graph_stop(void)